| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Public Key layer for writing key files and structures | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 20 | #include "common.h" | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if defined(MBEDTLS_PK_WRITE_C) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 23 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/pk.h" | 
|  | 25 | #include "mbedtls/asn1write.h" | 
|  | 26 | #include "mbedtls/oid.h" | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 27 | #include "mbedtls/platform_util.h" | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 28 | #include "mbedtls/error.h" | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 29 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 30 | #include <string.h> | 
|  | 31 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #if defined(MBEDTLS_RSA_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 33 | #include "mbedtls/rsa.h" | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 34 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_ECP_C) | 
| Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 36 | #include "mbedtls/bignum.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/ecp.h" | 
| Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 38 | #include "mbedtls/platform_util.h" | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 39 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_ECDSA_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/ecdsa.h" | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 42 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_PEM_WRITE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 44 | #include "mbedtls/pem.h" | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 45 | #endif | 
|  | 46 |  | 
| Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 47 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 48 | #include "psa/crypto.h" | 
| Hanno Becker | 65935d9 | 2019-02-01 11:55:03 +0000 | [diff] [blame] | 49 | #include "mbedtls/psa_util.h" | 
| Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 50 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 52 | #include "mbedtls/platform.h" | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 53 | #else | 
|  | 54 | #include <stdlib.h> | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 55 | #define mbedtls_calloc    calloc | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #define mbedtls_free       free | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 57 | #endif | 
|  | 58 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 59 | /* Parameter validation macros based on platform_util.h */ | 
|  | 60 | #define PK_VALIDATE_RET( cond )    \ | 
|  | 61 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA ) | 
|  | 62 | #define PK_VALIDATE( cond )        \ | 
|  | 63 | MBEDTLS_INTERNAL_VALIDATE( cond ) | 
|  | 64 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #if defined(MBEDTLS_RSA_C) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 66 | /* | 
|  | 67 | *  RSAPublicKey ::= SEQUENCE { | 
|  | 68 | *      modulus           INTEGER,  -- n | 
|  | 69 | *      publicExponent    INTEGER   -- e | 
|  | 70 | *  } | 
|  | 71 | */ | 
|  | 72 | static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 73 | mbedtls_rsa_context *rsa ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 74 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 75 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 76 | size_t len = 0; | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 77 | mbedtls_mpi T; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 78 |  | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 79 | mbedtls_mpi_init( &T ); | 
|  | 80 |  | 
|  | 81 | /* Export E */ | 
|  | 82 | if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 || | 
|  | 83 | ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 ) | 
|  | 84 | goto end_of_export; | 
|  | 85 | len += ret; | 
|  | 86 |  | 
|  | 87 | /* Export N */ | 
|  | 88 | if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 || | 
|  | 89 | ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 ) | 
|  | 90 | goto end_of_export; | 
|  | 91 | len += ret; | 
|  | 92 |  | 
|  | 93 | end_of_export: | 
|  | 94 |  | 
|  | 95 | mbedtls_mpi_free( &T ); | 
|  | 96 | if( ret < 0 ) | 
|  | 97 | return( ret ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 98 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 99 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); | 
|  | 100 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 101 | MBEDTLS_ASN1_SEQUENCE ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 102 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 103 | return( (int) len ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 104 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | #endif /* MBEDTLS_RSA_C */ | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 106 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 107 | #if defined(MBEDTLS_ECP_C) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 108 | /* | 
|  | 109 | * EC public key is an EC point | 
|  | 110 | */ | 
|  | 111 | static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 112 | mbedtls_ecp_keypair *ec ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 113 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 114 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 115 | size_t len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN]; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 117 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q, | 
|  | 119 | MBEDTLS_ECP_PF_UNCOMPRESSED, | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 120 | &len, buf, sizeof( buf ) ) ) != 0 ) | 
|  | 121 | { | 
|  | 122 | return( ret ); | 
|  | 123 | } | 
|  | 124 |  | 
| Manuel Pégourié-Gonnard | 4dc9b39 | 2015-10-21 12:23:09 +0200 | [diff] [blame] | 125 | if( *p < start || (size_t)( *p - start ) < len ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 127 |  | 
|  | 128 | *p -= len; | 
|  | 129 | memcpy( *p, buf, len ); | 
|  | 130 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 131 | return( (int) len ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 132 | } | 
|  | 133 |  | 
|  | 134 | /* | 
|  | 135 | * ECParameters ::= CHOICE { | 
|  | 136 | *   namedCurve         OBJECT IDENTIFIER | 
|  | 137 | * } | 
|  | 138 | */ | 
|  | 139 | static int pk_write_ec_param( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 140 | mbedtls_ecp_keypair *ec ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 141 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 142 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 143 | size_t len = 0; | 
|  | 144 | const char *oid; | 
|  | 145 | size_t oid_len; | 
|  | 146 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 148 | return( ret ); | 
|  | 149 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 151 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 152 | return( (int) len ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 153 | } | 
| Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 154 |  | 
|  | 155 | /* | 
|  | 156 | * privateKey  OCTET STRING -- always of length ceil(log2(n)/8) | 
|  | 157 | */ | 
|  | 158 | static int pk_write_ec_private( unsigned char **p, unsigned char *start, | 
|  | 159 | mbedtls_ecp_keypair *ec ) | 
|  | 160 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 161 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 162 | size_t byte_length = ( ec->grp.pbits + 7 ) / 8; | 
|  | 163 | unsigned char tmp[MBEDTLS_ECP_MAX_BYTES]; | 
|  | 164 |  | 
| Steven Cooreman | 0024df6 | 2020-07-13 10:59:40 +0200 | [diff] [blame] | 165 | ret = mbedtls_ecp_write_key( ec, tmp, byte_length ); | 
| Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 166 | if( ret != 0 ) | 
|  | 167 | goto exit; | 
|  | 168 | ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length ); | 
|  | 169 |  | 
|  | 170 | exit: | 
|  | 171 | mbedtls_platform_zeroize( tmp, byte_length ); | 
|  | 172 | return( ret ); | 
|  | 173 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | #endif /* MBEDTLS_ECP_C */ | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 175 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start, | 
| Hanno Becker | 8fd5548 | 2017-08-23 14:07:48 +0100 | [diff] [blame] | 177 | const mbedtls_pk_context *key ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 178 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 179 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 180 | size_t len = 0; | 
|  | 181 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 182 | PK_VALIDATE_RET( p != NULL ); | 
|  | 183 | PK_VALIDATE_RET( *p != NULL ); | 
|  | 184 | PK_VALIDATE_RET( start != NULL ); | 
|  | 185 | PK_VALIDATE_RET( key != NULL ); | 
|  | 186 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | #if defined(MBEDTLS_RSA_C) | 
|  | 188 | if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA ) | 
|  | 189 | MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 190 | else | 
|  | 191 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #if defined(MBEDTLS_ECP_C) | 
|  | 193 | if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY ) | 
|  | 194 | MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 195 | else | 
|  | 196 | #endif | 
| Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 197 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 198 | if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_OPAQUE ) | 
|  | 199 | { | 
| Andrzej Kurek | 158c3d1 | 2018-11-19 18:09:59 -0500 | [diff] [blame] | 200 | size_t buffer_size; | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 201 | psa_key_id_t* key_id = (psa_key_id_t*) key->pk_ctx; | 
| Andrzej Kurek | 158c3d1 | 2018-11-19 18:09:59 -0500 | [diff] [blame] | 202 |  | 
|  | 203 | if ( *p < start ) | 
|  | 204 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
|  | 205 |  | 
| Andrzej Kurek | b7f3ac6 | 2018-11-20 03:03:28 -0500 | [diff] [blame] | 206 | buffer_size = (size_t)( *p - start ); | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 207 | if ( psa_export_public_key( *key_id, start, buffer_size, &len ) | 
| Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 208 | != PSA_SUCCESS ) | 
|  | 209 | { | 
| Andrzej Kurek | 4b11407 | 2018-11-19 18:04:01 -0500 | [diff] [blame] | 210 | return( MBEDTLS_ERR_PK_BAD_INPUT_DATA ); | 
| Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 211 | } | 
|  | 212 | else | 
|  | 213 | { | 
| Hanno Becker | 4fb8db2 | 2019-02-01 09:57:20 +0000 | [diff] [blame] | 214 | *p -= len; | 
|  | 215 | memmove( *p, start, len ); | 
| Andrzej Kurek | 5fec086 | 2018-11-19 10:07:36 -0500 | [diff] [blame] | 216 | } | 
|  | 217 | } | 
|  | 218 | else | 
|  | 219 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 221 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 222 | return( (int) len ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 223 | } | 
|  | 224 |  | 
| Mateusz Starzyk | 3292453 | 2021-02-02 15:22:19 +0100 | [diff] [blame] | 225 | int mbedtls_pk_write_pubkey_der( const mbedtls_pk_context *key, unsigned char *buf, size_t size ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 226 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 227 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 228 | unsigned char *c; | 
|  | 229 | size_t len = 0, par_len = 0, oid_len; | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 230 | mbedtls_pk_type_t pk_type; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 231 | const char *oid; | 
|  | 232 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 233 | PK_VALIDATE_RET( key != NULL ); | 
|  | 234 | if( size == 0 ) | 
|  | 235 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 236 | PK_VALIDATE_RET( buf != NULL ); | 
|  | 237 |  | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 238 | c = buf + size; | 
|  | 239 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 241 |  | 
|  | 242 | if( c - buf < 1 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 244 |  | 
|  | 245 | /* | 
|  | 246 | *  SubjectPublicKeyInfo  ::=  SEQUENCE  { | 
|  | 247 | *       algorithm            AlgorithmIdentifier, | 
|  | 248 | *       subjectPublicKey     BIT STRING } | 
|  | 249 | */ | 
|  | 250 | *--c = 0; | 
|  | 251 | len += 1; | 
|  | 252 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); | 
|  | 254 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 255 |  | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 256 | pk_type = mbedtls_pk_get_type( key ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | #if defined(MBEDTLS_ECP_C) | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 258 | if( pk_type == MBEDTLS_PK_ECKEY ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 259 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 261 | } | 
|  | 262 | #endif | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 263 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 264 | if( pk_type == MBEDTLS_PK_OPAQUE ) | 
|  | 265 | { | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 266 | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 267 | psa_key_type_t key_type; | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 268 | psa_key_id_t key_id; | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 269 | psa_ecc_family_t curve; | 
| Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 270 | size_t bits; | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 271 |  | 
| Ronald Cron | cf56a0a | 2020-08-04 09:51:30 +0200 | [diff] [blame] | 272 | key_id = *((psa_key_id_t*) key->pk_ctx ); | 
|  | 273 | if( PSA_SUCCESS != psa_get_key_attributes( key_id, &attributes ) ) | 
| TRodziewicz | b579ccd | 2021-04-13 14:28:28 +0200 | [diff] [blame] | 274 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 275 | key_type = psa_get_key_type( &attributes ); | 
| Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 276 | bits = psa_get_key_bits( &attributes ); | 
| Gilles Peskine | d2d45c1 | 2019-05-27 14:53:13 +0200 | [diff] [blame] | 277 | psa_reset_key_attributes( &attributes ); | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 278 |  | 
| Paul Elliott | 8ff510a | 2020-06-02 17:19:28 +0100 | [diff] [blame] | 279 | curve = PSA_KEY_TYPE_ECC_GET_FAMILY( key_type ); | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 280 | if( curve == 0 ) | 
|  | 281 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); | 
|  | 282 |  | 
| Gilles Peskine | 89177e8 | 2019-12-03 21:19:09 +0100 | [diff] [blame] | 283 | ret = mbedtls_psa_get_ecc_oid_from_id( curve, bits, &oid, &oid_len ); | 
| Hanno Becker | 493c171 | 2019-02-01 10:07:07 +0000 | [diff] [blame] | 284 | if( ret != 0 ) | 
|  | 285 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); | 
|  | 286 |  | 
|  | 287 | /* Write EC algorithm parameters; that's akin | 
|  | 288 | * to pk_write_ec_param() above. */ | 
|  | 289 | MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_oid( &c, buf, | 
|  | 290 | oid, oid_len ) ); | 
|  | 291 |  | 
|  | 292 | /* The rest of the function works as for legacy EC contexts. */ | 
|  | 293 | pk_type = MBEDTLS_PK_ECKEY; | 
|  | 294 | } | 
|  | 295 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 296 |  | 
|  | 297 | if( ( ret = mbedtls_oid_get_oid_by_pk_alg( pk_type, &oid, | 
|  | 298 | &oid_len ) ) != 0 ) | 
|  | 299 | { | 
|  | 300 | return( ret ); | 
|  | 301 | } | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 302 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len, | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 304 | par_len ) ); | 
|  | 305 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); | 
|  | 307 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 308 | MBEDTLS_ASN1_SEQUENCE ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 309 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 310 | return( (int) len ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 311 | } | 
|  | 312 |  | 
| Mateusz Starzyk | 3292453 | 2021-02-02 15:22:19 +0100 | [diff] [blame] | 313 | int mbedtls_pk_write_key_der( const mbedtls_pk_context *key, unsigned char *buf, size_t size ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 314 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 315 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 316 | unsigned char *c; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 317 | size_t len = 0; | 
|  | 318 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 319 | PK_VALIDATE_RET( key != NULL ); | 
|  | 320 | if( size == 0 ) | 
|  | 321 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
|  | 322 | PK_VALIDATE_RET( buf != NULL ); | 
|  | 323 |  | 
|  | 324 | c = buf + size; | 
|  | 325 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | #if defined(MBEDTLS_RSA_C) | 
|  | 327 | if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 328 | { | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 329 | mbedtls_mpi T; /* Temporary holding the exported parameters */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 330 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 331 |  | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 332 | /* | 
|  | 333 | * Export the parameters one after another to avoid simultaneous copies. | 
|  | 334 | */ | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 335 |  | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 336 | mbedtls_mpi_init( &T ); | 
|  | 337 |  | 
|  | 338 | /* Export QP */ | 
|  | 339 | if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 || | 
|  | 340 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 341 | goto end_of_export; | 
|  | 342 | len += ret; | 
|  | 343 |  | 
|  | 344 | /* Export DQ */ | 
|  | 345 | if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 || | 
|  | 346 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 347 | goto end_of_export; | 
|  | 348 | len += ret; | 
|  | 349 |  | 
|  | 350 | /* Export DP */ | 
|  | 351 | if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 || | 
|  | 352 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 353 | goto end_of_export; | 
|  | 354 | len += ret; | 
|  | 355 |  | 
|  | 356 | /* Export Q */ | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 357 | if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, | 
|  | 358 | &T, NULL, NULL ) ) != 0 || | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 359 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 360 | goto end_of_export; | 
|  | 361 | len += ret; | 
|  | 362 |  | 
|  | 363 | /* Export P */ | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 364 | if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T, | 
|  | 365 | NULL, NULL, NULL ) ) != 0 || | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 366 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 367 | goto end_of_export; | 
|  | 368 | len += ret; | 
|  | 369 |  | 
|  | 370 | /* Export D */ | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 371 | if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, | 
|  | 372 | NULL, &T, NULL ) ) != 0 || | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 373 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 374 | goto end_of_export; | 
|  | 375 | len += ret; | 
|  | 376 |  | 
|  | 377 | /* Export E */ | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 378 | if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, | 
|  | 379 | NULL, NULL, &T ) ) != 0 || | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 380 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 381 | goto end_of_export; | 
|  | 382 | len += ret; | 
|  | 383 |  | 
|  | 384 | /* Export N */ | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 385 | if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, | 
|  | 386 | NULL, NULL, NULL ) ) != 0 || | 
| Hanno Becker | 15f81fa | 2017-08-23 12:38:27 +0100 | [diff] [blame] | 387 | ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 ) | 
|  | 388 | goto end_of_export; | 
|  | 389 | len += ret; | 
|  | 390 |  | 
|  | 391 | end_of_export: | 
|  | 392 |  | 
|  | 393 | mbedtls_mpi_free( &T ); | 
|  | 394 | if( ret < 0 ) | 
|  | 395 | return( ret ); | 
|  | 396 |  | 
|  | 397 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 398 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); | 
| Hanno Becker | d71dc15 | 2017-08-23 06:32:42 +0100 | [diff] [blame] | 399 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, | 
|  | 400 | buf, MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 401 | MBEDTLS_ASN1_SEQUENCE ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 402 | } | 
|  | 403 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | #endif /* MBEDTLS_RSA_C */ | 
|  | 405 | #if defined(MBEDTLS_ECP_C) | 
|  | 406 | if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 407 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 408 | mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 409 | size_t pub_len = 0, par_len = 0; | 
|  | 410 |  | 
|  | 411 | /* | 
|  | 412 | * RFC 5915, or SEC1 Appendix C.4 | 
|  | 413 | * | 
|  | 414 | * ECPrivateKey ::= SEQUENCE { | 
|  | 415 | *      version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), | 
|  | 416 | *      privateKey     OCTET STRING, | 
|  | 417 | *      parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, | 
|  | 418 | *      publicKey  [1] BIT STRING OPTIONAL | 
|  | 419 | *    } | 
|  | 420 | */ | 
|  | 421 |  | 
|  | 422 | /* publicKey */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 424 |  | 
|  | 425 | if( c - buf < 1 ) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 427 | *--c = 0; | 
|  | 428 | pub_len += 1; | 
|  | 429 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) ); | 
|  | 431 | MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 432 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) ); | 
|  | 434 | MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, | 
|  | 435 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 436 | len += pub_len; | 
|  | 437 |  | 
|  | 438 | /* parameters */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 440 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 441 | MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) ); | 
|  | 442 | MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf, | 
|  | 443 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 444 | len += par_len; | 
|  | 445 |  | 
| Gilles Peskine | 2700cfb | 2018-08-11 00:48:44 +0200 | [diff] [blame] | 446 | /* privateKey */ | 
|  | 447 | MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 448 |  | 
|  | 449 | /* version */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 450 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 451 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 452 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); | 
|  | 453 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 454 | MBEDTLS_ASN1_SEQUENCE ) ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 455 | } | 
|  | 456 | else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 457 | #endif /* MBEDTLS_ECP_C */ | 
|  | 458 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 459 |  | 
| Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 460 | return( (int) len ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 461 | } | 
|  | 462 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | #if defined(MBEDTLS_PEM_WRITE_C) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 464 |  | 
|  | 465 | #define PEM_BEGIN_PUBLIC_KEY    "-----BEGIN PUBLIC KEY-----\n" | 
|  | 466 | #define PEM_END_PUBLIC_KEY      "-----END PUBLIC KEY-----\n" | 
|  | 467 |  | 
|  | 468 | #define PEM_BEGIN_PRIVATE_KEY_RSA   "-----BEGIN RSA PRIVATE KEY-----\n" | 
|  | 469 | #define PEM_END_PRIVATE_KEY_RSA     "-----END RSA PRIVATE KEY-----\n" | 
|  | 470 | #define PEM_BEGIN_PRIVATE_KEY_EC    "-----BEGIN EC PRIVATE KEY-----\n" | 
|  | 471 | #define PEM_END_PRIVATE_KEY_EC      "-----END EC PRIVATE KEY-----\n" | 
|  | 472 |  | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 473 | /* | 
|  | 474 | * Max sizes of key per types. Shown as tag + len (+ content). | 
|  | 475 | */ | 
|  | 476 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | #if defined(MBEDTLS_RSA_C) | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 478 | /* | 
|  | 479 | * RSA public keys: | 
|  | 480 | *  SubjectPublicKeyInfo  ::=  SEQUENCE  {          1 + 3 | 
|  | 481 | *       algorithm            AlgorithmIdentifier,  1 + 1 (sequence) | 
|  | 482 | *                                                + 1 + 1 + 9 (rsa oid) | 
|  | 483 | *                                                + 1 + 1 (params null) | 
|  | 484 | *       subjectPublicKey     BIT STRING }          1 + 3 + (1 + below) | 
|  | 485 | *  RSAPublicKey ::= SEQUENCE {                     1 + 3 | 
|  | 486 | *      modulus           INTEGER,  -- n            1 + 3 + MPI_MAX + 1 | 
|  | 487 | *      publicExponent    INTEGER   -- e            1 + 3 + MPI_MAX + 1 | 
|  | 488 | *  } | 
|  | 489 | */ | 
| Daniel Otte | 79fb5da | 2021-02-01 14:26:08 +0100 | [diff] [blame] | 490 | #define RSA_PUB_DER_MAX_BYTES   ( 38 + 2 * MBEDTLS_MPI_MAX_SIZE ) | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 491 |  | 
|  | 492 | /* | 
|  | 493 | * RSA private keys: | 
|  | 494 | *  RSAPrivateKey ::= SEQUENCE {                    1 + 3 | 
|  | 495 | *      version           Version,                  1 + 1 + 1 | 
|  | 496 | *      modulus           INTEGER,                  1 + 3 + MPI_MAX + 1 | 
|  | 497 | *      publicExponent    INTEGER,                  1 + 3 + MPI_MAX + 1 | 
|  | 498 | *      privateExponent   INTEGER,                  1 + 3 + MPI_MAX + 1 | 
|  | 499 | *      prime1            INTEGER,                  1 + 3 + MPI_MAX / 2 + 1 | 
|  | 500 | *      prime2            INTEGER,                  1 + 3 + MPI_MAX / 2 + 1 | 
|  | 501 | *      exponent1         INTEGER,                  1 + 3 + MPI_MAX / 2 + 1 | 
|  | 502 | *      exponent2         INTEGER,                  1 + 3 + MPI_MAX / 2 + 1 | 
|  | 503 | *      coefficient       INTEGER,                  1 + 3 + MPI_MAX / 2 + 1 | 
|  | 504 | *      otherPrimeInfos   OtherPrimeInfos OPTIONAL  0 (not supported) | 
|  | 505 | *  } | 
|  | 506 | */ | 
| Daniel Otte | 39f3614 | 2021-02-01 14:23:30 +0100 | [diff] [blame] | 507 | #define MPI_MAX_SIZE_2          ( MBEDTLS_MPI_MAX_SIZE / 2 + \ | 
|  | 508 | MBEDTLS_MPI_MAX_SIZE % 2 ) | 
| Daniel Otte | 79fb5da | 2021-02-01 14:26:08 +0100 | [diff] [blame] | 509 | #define RSA_PRV_DER_MAX_BYTES   ( 47 + 3 * MBEDTLS_MPI_MAX_SIZE \ | 
|  | 510 | + 5 * MPI_MAX_SIZE_2 ) | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 511 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 512 | #else /* MBEDTLS_RSA_C */ | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 513 |  | 
|  | 514 | #define RSA_PUB_DER_MAX_BYTES   0 | 
|  | 515 | #define RSA_PRV_DER_MAX_BYTES   0 | 
|  | 516 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | #endif /* MBEDTLS_RSA_C */ | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 518 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 519 | #if defined(MBEDTLS_ECP_C) | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 520 | /* | 
|  | 521 | * EC public keys: | 
|  | 522 | *  SubjectPublicKeyInfo  ::=  SEQUENCE  {      1 + 2 | 
|  | 523 | *    algorithm         AlgorithmIdentifier,    1 + 1 (sequence) | 
|  | 524 | *                                            + 1 + 1 + 7 (ec oid) | 
|  | 525 | *                                            + 1 + 1 + 9 (namedCurve oid) | 
|  | 526 | *    subjectPublicKey  BIT STRING              1 + 2 + 1               [1] | 
|  | 527 | *                                            + 1 (point format)        [1] | 
|  | 528 | *                                            + 2 * ECP_MAX (coords)    [1] | 
|  | 529 | *  } | 
|  | 530 | */ | 
| Daniel Otte | 79fb5da | 2021-02-01 14:26:08 +0100 | [diff] [blame] | 531 | #define ECP_PUB_DER_MAX_BYTES   ( 30 + 2 * MBEDTLS_ECP_MAX_BYTES ) | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 532 |  | 
|  | 533 | /* | 
|  | 534 | * EC private keys: | 
|  | 535 | * ECPrivateKey ::= SEQUENCE {                  1 + 2 | 
|  | 536 | *      version        INTEGER ,                1 + 1 + 1 | 
|  | 537 | *      privateKey     OCTET STRING,            1 + 1 + ECP_MAX | 
|  | 538 | *      parameters [0] ECParameters OPTIONAL,   1 + 1 + (1 + 1 + 9) | 
|  | 539 | *      publicKey  [1] BIT STRING OPTIONAL      1 + 2 + [1] above | 
|  | 540 | *    } | 
|  | 541 | */ | 
| Daniel Otte | 79fb5da | 2021-02-01 14:26:08 +0100 | [diff] [blame] | 542 | #define ECP_PRV_DER_MAX_BYTES   ( 29 + 3 * MBEDTLS_ECP_MAX_BYTES ) | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 543 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | #else /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 545 |  | 
|  | 546 | #define ECP_PUB_DER_MAX_BYTES   0 | 
|  | 547 | #define ECP_PRV_DER_MAX_BYTES   0 | 
|  | 548 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 549 | #endif /* MBEDTLS_ECP_C */ | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 550 |  | 
| Daniel Otte | 79fb5da | 2021-02-01 14:26:08 +0100 | [diff] [blame] | 551 | #define PUB_DER_MAX_BYTES   ( RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \ | 
|  | 552 | RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES ) | 
|  | 553 | #define PRV_DER_MAX_BYTES   ( RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \ | 
|  | 554 | RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES ) | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 555 |  | 
| Mateusz Starzyk | 3292453 | 2021-02-02 15:22:19 +0100 | [diff] [blame] | 556 | int mbedtls_pk_write_pubkey_pem( const mbedtls_pk_context *key, unsigned char *buf, size_t size ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 557 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 558 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 559 | unsigned char output_buf[PUB_DER_MAX_BYTES]; | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 560 | size_t olen = 0; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 561 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 562 | PK_VALIDATE_RET( key != NULL ); | 
|  | 563 | PK_VALIDATE_RET( buf != NULL || size == 0 ); | 
|  | 564 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 566 | sizeof(output_buf) ) ) < 0 ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 567 | { | 
|  | 568 | return( ret ); | 
|  | 569 | } | 
|  | 570 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 571 | if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 572 | output_buf + sizeof(output_buf) - ret, | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 573 | ret, buf, size, &olen ) ) != 0 ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 574 | { | 
|  | 575 | return( ret ); | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | return( 0 ); | 
|  | 579 | } | 
|  | 580 |  | 
| Mateusz Starzyk | 3292453 | 2021-02-02 15:22:19 +0100 | [diff] [blame] | 581 | int mbedtls_pk_write_key_pem( const mbedtls_pk_context *key, unsigned char *buf, size_t size ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 582 | { | 
| Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 583 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 192253a | 2014-07-21 16:37:15 +0200 | [diff] [blame] | 584 | unsigned char output_buf[PRV_DER_MAX_BYTES]; | 
| Paul Bakker | fcc1721 | 2013-10-11 09:36:52 +0200 | [diff] [blame] | 585 | const char *begin, *end; | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 586 | size_t olen = 0; | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 587 |  | 
| Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 588 | PK_VALIDATE_RET( key != NULL ); | 
|  | 589 | PK_VALIDATE_RET( buf != NULL || size == 0 ); | 
|  | 590 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 591 | if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 592 | return( ret ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 593 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 594 | #if defined(MBEDTLS_RSA_C) | 
|  | 595 | if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 596 | { | 
|  | 597 | begin = PEM_BEGIN_PRIVATE_KEY_RSA; | 
|  | 598 | end = PEM_END_PRIVATE_KEY_RSA; | 
|  | 599 | } | 
|  | 600 | else | 
|  | 601 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | #if defined(MBEDTLS_ECP_C) | 
|  | 603 | if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 604 | { | 
|  | 605 | begin = PEM_BEGIN_PRIVATE_KEY_EC; | 
|  | 606 | end = PEM_END_PRIVATE_KEY_EC; | 
|  | 607 | } | 
|  | 608 | else | 
|  | 609 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 610 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 611 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | if( ( ret = mbedtls_pem_write_buffer( begin, end, | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 613 | output_buf + sizeof(output_buf) - ret, | 
| Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 614 | ret, buf, size, &olen ) ) != 0 ) | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 615 | { | 
|  | 616 | return( ret ); | 
|  | 617 | } | 
|  | 618 |  | 
|  | 619 | return( 0 ); | 
|  | 620 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 621 | #endif /* MBEDTLS_PEM_WRITE_C */ | 
| Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 622 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 623 | #endif /* MBEDTLS_PK_WRITE_C */ |