blob: aff8246b1ffb188cb0822d9b72c67ac70d275612 [file] [log] [blame]
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001/*
2 * Public Key layer for writing key files and structures
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakkerc7bb02b2013-09-15 14:54:56 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk.h"
31#include "mbedtls/asn1write.h"
32#include "mbedtls/oid.h"
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050033#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020041#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/ecp.h"
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020043#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020050#endif
51
Andrzej Kurek5fec0862018-11-19 10:07:36 -050052#if defined(MBEDTLS_USE_PSA_CRYPTO)
53#include "psa/crypto.h"
Hanno Becker65935d92019-02-01 11:55:03 +000054#include "mbedtls/psa_util.h"
Andrzej Kurek5fec0862018-11-19 10:07:36 -050055#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020058#else
59#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020062#endif
63
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050064/* Parameter validation macros based on platform_util.h */
65#define PK_VALIDATE_RET( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
67#define PK_VALIDATE( cond ) \
68 MBEDTLS_INTERNAL_VALIDATE( cond )
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020071/*
72 * RSAPublicKey ::= SEQUENCE {
73 * modulus INTEGER, -- n
74 * publicExponent INTEGER -- e
75 * }
76 */
77static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010078 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020079{
80 int ret;
81 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010082 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020083
Hanno Becker15f81fa2017-08-23 12:38:27 +010084 mbedtls_mpi_init( &T );
85
86 /* Export E */
87 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
88 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
89 goto end_of_export;
90 len += ret;
91
92 /* Export N */
93 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
94 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
95 goto end_of_export;
96 len += ret;
97
98end_of_export:
99
100 mbedtls_mpi_free( &T );
101 if( ret < 0 )
102 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
105 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
106 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200108 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200113/*
114 * EC public key is an EC point
115 */
116static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100117 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200118{
119 int ret;
120 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
124 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200125 &len, buf, sizeof( buf ) ) ) != 0 )
126 {
127 return( ret );
128 }
129
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200130 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200132
133 *p -= len;
134 memcpy( *p, buf, len );
135
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200136 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200137}
138
139/*
140 * ECParameters ::= CHOICE {
141 * namedCurve OBJECT IDENTIFIER
142 * }
143 */
144static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100145 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200146{
147 int ret;
148 size_t len = 0;
149 const char *oid;
150 size_t oid_len;
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200153 return( ret );
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200156
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200157 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200158}
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200159
160/*
161 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
162 */
163static int pk_write_ec_private( unsigned char **p, unsigned char *start,
164 mbedtls_ecp_keypair *ec )
165{
166 int ret;
167 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
168 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
169
170 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
171 if( ret != 0 )
172 goto exit;
173 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
174
175exit:
176 mbedtls_platform_zeroize( tmp, byte_length );
177 return( ret );
178}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100182 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200183{
184 int ret;
185 size_t len = 0;
186
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500187 PK_VALIDATE_RET( p != NULL );
188 PK_VALIDATE_RET( *p != NULL );
189 PK_VALIDATE_RET( start != NULL );
190 PK_VALIDATE_RET( key != NULL );
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_RSA_C)
193 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
194 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200195 else
196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_ECP_C)
198 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
199 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200200 else
201#endif
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500202#if defined(MBEDTLS_USE_PSA_CRYPTO)
203 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_OPAQUE )
204 {
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500205 size_t buffer_size;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500206 psa_key_handle_t* key_slot = (psa_key_handle_t*) key->pk_ctx;
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500207
208 if ( *p < start )
209 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
210
Andrzej Kurekb7f3ac62018-11-20 03:03:28 -0500211 buffer_size = (size_t)( *p - start );
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500212 if ( psa_export_public_key( *key_slot, start, buffer_size, &len )
213 != PSA_SUCCESS )
214 {
Andrzej Kurek4b114072018-11-19 18:04:01 -0500215 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500216 }
217 else
218 {
Hanno Becker4fb8db22019-02-01 09:57:20 +0000219 *p -= len;
220 memmove( *p, start, len );
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500221 }
222 }
223 else
224#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200226
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200227 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200228}
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200231{
232 int ret;
233 unsigned char *c;
234 size_t len = 0, par_len = 0, oid_len;
Hanno Becker493c1712019-02-01 10:07:07 +0000235 mbedtls_pk_type_t pk_type;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200236 const char *oid;
237
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500238 PK_VALIDATE_RET( key != NULL );
239 if( size == 0 )
240 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
241 PK_VALIDATE_RET( buf != NULL );
242
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200243 c = buf + size;
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200246
247 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200249
250 /*
251 * SubjectPublicKeyInfo ::= SEQUENCE {
252 * algorithm AlgorithmIdentifier,
253 * subjectPublicKey BIT STRING }
254 */
255 *--c = 0;
256 len += 1;
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
259 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200260
Hanno Becker493c1712019-02-01 10:07:07 +0000261 pk_type = mbedtls_pk_get_type( key );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_ECP_C)
Hanno Becker493c1712019-02-01 10:07:07 +0000263 if( pk_type == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200266 }
267#endif
Hanno Becker493c1712019-02-01 10:07:07 +0000268#if defined(MBEDTLS_USE_PSA_CRYPTO)
269 if( pk_type == MBEDTLS_PK_OPAQUE )
270 {
271 psa_status_t status;
272 psa_key_type_t key_type;
273 psa_key_handle_t handle;
274 psa_ecc_curve_t curve;
275
276 handle = *((psa_key_handle_t*) key->pk_ctx );
277
278 status = psa_get_key_information( handle, &key_type,
279 NULL /* bitsize not needed */ );
280 if( status != PSA_SUCCESS )
281 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
282
283 curve = PSA_KEY_TYPE_GET_CURVE( key_type );
284 if( curve == 0 )
285 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
286
287 ret = mbedtls_psa_get_ecc_oid_from_id( curve, &oid, &oid_len );
288 if( ret != 0 )
289 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
290
291 /* Write EC algorithm parameters; that's akin
292 * to pk_write_ec_param() above. */
293 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_oid( &c, buf,
294 oid, oid_len ) );
295
296 /* The rest of the function works as for legacy EC contexts. */
297 pk_type = MBEDTLS_PK_ECKEY;
298 }
299#endif /* MBEDTLS_USE_PSA_CRYPTO */
300
301 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( pk_type, &oid,
302 &oid_len ) ) != 0 )
303 {
304 return( ret );
305 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200308 par_len ) );
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
311 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
312 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200313
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200314 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200315}
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200318{
319 int ret;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200321 size_t len = 0;
322
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500323 PK_VALIDATE_RET( key != NULL );
324 if( size == 0 )
325 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
326 PK_VALIDATE_RET( buf != NULL );
327
328 c = buf + size;
329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#if defined(MBEDTLS_RSA_C)
331 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200332 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100333 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200335
Hanno Becker15f81fa2017-08-23 12:38:27 +0100336 /*
337 * Export the parameters one after another to avoid simultaneous copies.
338 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200339
Hanno Becker15f81fa2017-08-23 12:38:27 +0100340 mbedtls_mpi_init( &T );
341
342 /* Export QP */
343 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
344 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
345 goto end_of_export;
346 len += ret;
347
348 /* Export DQ */
349 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
350 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
351 goto end_of_export;
352 len += ret;
353
354 /* Export DP */
355 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
356 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
357 goto end_of_export;
358 len += ret;
359
360 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100361 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
362 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100363 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
364 goto end_of_export;
365 len += ret;
366
367 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100368 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
369 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100370 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
371 goto end_of_export;
372 len += ret;
373
374 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100375 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
376 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100377 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
378 goto end_of_export;
379 len += ret;
380
381 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100382 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
383 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100384 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
385 goto end_of_export;
386 len += ret;
387
388 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100389 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
390 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100391 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
392 goto end_of_export;
393 len += ret;
394
395 end_of_export:
396
397 mbedtls_mpi_free( &T );
398 if( ret < 0 )
399 return( ret );
400
401 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100403 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
404 buf, MBEDTLS_ASN1_CONSTRUCTED |
405 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200406 }
407 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#endif /* MBEDTLS_RSA_C */
409#if defined(MBEDTLS_ECP_C)
410 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200413 size_t pub_len = 0, par_len = 0;
414
415 /*
416 * RFC 5915, or SEC1 Appendix C.4
417 *
418 * ECPrivateKey ::= SEQUENCE {
419 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
420 * privateKey OCTET STRING,
421 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
422 * publicKey [1] BIT STRING OPTIONAL
423 * }
424 */
425
426 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200428
429 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200431 *--c = 0;
432 pub_len += 1;
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
435 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
438 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
439 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200440 len += pub_len;
441
442 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
446 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
447 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200448 len += par_len;
449
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200450 /* privateKey */
451 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200452
453 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
457 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
458 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200459 }
460 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#endif /* MBEDTLS_ECP_C */
462 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200463
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200464 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200465}
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200468
469#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
470#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
471
472#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
473#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
474#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
475#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
476
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200477/*
478 * Max sizes of key per types. Shown as tag + len (+ content).
479 */
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200482/*
483 * RSA public keys:
484 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
485 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
486 * + 1 + 1 + 9 (rsa oid)
487 * + 1 + 1 (params null)
488 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
489 * RSAPublicKey ::= SEQUENCE { 1 + 3
490 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
491 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
492 * }
493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200495
496/*
497 * RSA private keys:
498 * RSAPrivateKey ::= SEQUENCE { 1 + 3
499 * version Version, 1 + 1 + 1
500 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
501 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
502 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
503 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
504 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
505 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
506 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
507 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
508 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
509 * }
510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
512 MBEDTLS_MPI_MAX_SIZE % 2
513#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200514 + 5 * MPI_MAX_SIZE_2
515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200517
518#define RSA_PUB_DER_MAX_BYTES 0
519#define RSA_PRV_DER_MAX_BYTES 0
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200524/*
525 * EC public keys:
526 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
527 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
528 * + 1 + 1 + 7 (ec oid)
529 * + 1 + 1 + 9 (namedCurve oid)
530 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
531 * + 1 (point format) [1]
532 * + 2 * ECP_MAX (coords) [1]
533 * }
534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200536
537/*
538 * EC private keys:
539 * ECPrivateKey ::= SEQUENCE { 1 + 2
540 * version INTEGER , 1 + 1 + 1
541 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
542 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
543 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
544 * }
545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200549
550#define ECP_PUB_DER_MAX_BYTES 0
551#define ECP_PRV_DER_MAX_BYTES 0
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200554
555#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
556 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
557#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
558 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200561{
562 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200563 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200564 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200565
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500566 PK_VALIDATE_RET( key != NULL );
567 PK_VALIDATE_RET( buf != NULL || size == 0 );
568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200570 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200571 {
572 return( ret );
573 }
574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200576 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200577 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200578 {
579 return( ret );
580 }
581
582 return( 0 );
583}
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200586{
587 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200588 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200589 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200590 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200591
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500592 PK_VALIDATE_RET( key != NULL );
593 PK_VALIDATE_RET( buf != NULL || size == 0 );
594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200596 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#if defined(MBEDTLS_RSA_C)
599 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200600 {
601 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
602 end = PEM_END_PRIVATE_KEY_RSA;
603 }
604 else
605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606#if defined(MBEDTLS_ECP_C)
607 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200608 {
609 begin = PEM_BEGIN_PRIVATE_KEY_EC;
610 end = PEM_END_PRIVATE_KEY_EC;
611 }
612 else
613#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200617 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200618 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200619 {
620 return( ret );
621 }
622
623 return( 0 );
624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#endif /* MBEDTLS_PK_WRITE_C */