blob: f816f0ee63bbb19e11657437deb74c70e3053243 [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"
Gilles Peskinee97dc602018-12-19 00:51:38 +010033#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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020048#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020052#else
53#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020054#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020056#endif
57
Gilles Peskinee97dc602018-12-19 00:51:38 +010058/* Parameter validation macros based on platform_util.h */
59#define PK_VALIDATE_RET( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
61#define PK_VALIDATE( cond ) \
62 MBEDTLS_INTERNAL_VALIDATE( cond )
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020065/*
66 * RSAPublicKey ::= SEQUENCE {
67 * modulus INTEGER, -- n
68 * publicExponent INTEGER -- e
69 * }
70 */
71static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010072 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020073{
74 int ret;
75 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010076 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020077
Hanno Becker15f81fa2017-08-23 12:38:27 +010078 mbedtls_mpi_init( &T );
79
80 /* Export E */
81 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
82 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
83 goto end_of_export;
84 len += ret;
85
86 /* Export N */
87 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
88 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
89 goto end_of_export;
90 len += ret;
91
92end_of_export:
93
94 mbedtls_mpi_free( &T );
95 if( ret < 0 )
96 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
99 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
100 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200101
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200102 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105
Hanno Becker28332a52019-08-21 16:19:55 +0100106#if defined(MBEDTLS_USE_TINYCRYPT)
107static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
108 mbedtls_pk_context const *key )
109{
110 size_t const len = 1 + 2 * NUM_ECC_BYTES;
111 mbedtls_uecc_keypair const * const uecc = mbedtls_pk_uecc( *key );
112
113 if( *p < start || (size_t)( *p - start ) < len )
114 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
115
116 *p -= len;
117 (*p)[0] = 0x04;
118 memcpy( *p + 1, uecc->public_key, 2 * NUM_ECC_BYTES );
119
120 return( (int) len );
121}
122
123static int pk_write_ec_privkey( unsigned char **p, unsigned char *start,
124 mbedtls_pk_context const *key )
125{
126 mbedtls_uecc_keypair const * const uecc = mbedtls_pk_uecc( *key );
127 return( mbedtls_asn1_write_octet_string(
128 p, start,
129 uecc->private_key,
130 NUM_ECC_BYTES ) );
131}
132
133/*
134 * ECParameters ::= CHOICE {
135 * namedCurve OBJECT IDENTIFIER
136 * }
137 */
138static int pk_write_ec_param( unsigned char **p, unsigned char *start,
139 mbedtls_pk_context const *key )
140{
141 int ret;
142 size_t len = 0;
143 const char *oid;
144 size_t oid_len;
145 ((void) key);
146
147 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( MBEDTLS_UECC_DP_SECP256R1,
148 &oid, &oid_len ) ) != 0 )
149 return( ret );
150
151 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
152
153 return( (int) len );
154}
155#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200157/*
158 * EC public key is an EC point
159 */
160static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker64a81b02019-08-21 16:08:17 +0100161 mbedtls_pk_context const *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200162{
163 int ret;
164 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Hanno Becker64a81b02019-08-21 16:08:17 +0100166 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
169 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200170 &len, buf, sizeof( buf ) ) ) != 0 )
171 {
172 return( ret );
173 }
174
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200175 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200177
178 *p -= len;
179 memcpy( *p, buf, len );
180
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200181 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182}
183
Hanno Becker64a81b02019-08-21 16:08:17 +0100184static int pk_write_ec_privkey( unsigned char **p, unsigned char *start,
185 mbedtls_pk_context const *key )
186{
187 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
188 return( mbedtls_asn1_write_mpi( p, start, &ec->d ) );
189}
190
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200191/*
192 * ECParameters ::= CHOICE {
193 * namedCurve OBJECT IDENTIFIER
194 * }
195 */
196static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker64a81b02019-08-21 16:08:17 +0100197 mbedtls_pk_context const *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200198{
199 int ret;
200 size_t len = 0;
201 const char *oid;
202 size_t oid_len;
Hanno Becker64a81b02019-08-21 16:08:17 +0100203 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200206 return( ret );
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200209
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200210 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200211}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#endif /* MBEDTLS_ECP_C */
Hanno Becker28332a52019-08-21 16:19:55 +0100213#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100216 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200217{
218 int ret;
219 size_t len = 0;
220
Gilles Peskinee97dc602018-12-19 00:51:38 +0100221 PK_VALIDATE_RET( p != NULL );
222 PK_VALIDATE_RET( *p != NULL );
223 PK_VALIDATE_RET( start != NULL );
224 PK_VALIDATE_RET( key != NULL );
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_RSA_C)
227 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
228 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200229 else
230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_ECP_C)
232 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Hanno Becker64a81b02019-08-21 16:08:17 +0100233 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234 else
235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200237
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200238 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200239}
240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200242{
243 int ret;
244 unsigned char *c;
245 size_t len = 0, par_len = 0, oid_len;
246 const char *oid;
247
Gilles Peskinee97dc602018-12-19 00:51:38 +0100248 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100249 if( size == 0 )
250 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
251 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100252
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200253 c = buf + size;
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200256
257 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200259
260 /*
261 * SubjectPublicKeyInfo ::= SEQUENCE {
262 * algorithm AlgorithmIdentifier,
263 * subjectPublicKey BIT STRING }
264 */
265 *--c = 0;
266 len += 1;
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
269 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200272 &oid, &oid_len ) ) != 0 )
273 {
274 return( ret );
275 }
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#if defined(MBEDTLS_ECP_C)
278 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200279 {
Hanno Becker64a81b02019-08-21 16:08:17 +0100280 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200281 }
282#endif
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200285 par_len ) );
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
289 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200290
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200291 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200292}
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200295{
296 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100297 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200298 size_t len = 0;
299
Gilles Peskinee97dc602018-12-19 00:51:38 +0100300 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100301 if( size == 0 )
302 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
303 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100304
305 c = buf + size;
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307#if defined(MBEDTLS_RSA_C)
308 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200309 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100310 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200312
Hanno Becker15f81fa2017-08-23 12:38:27 +0100313 /*
314 * Export the parameters one after another to avoid simultaneous copies.
315 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200316
Hanno Becker15f81fa2017-08-23 12:38:27 +0100317 mbedtls_mpi_init( &T );
318
319 /* Export QP */
320 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
321 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
322 goto end_of_export;
323 len += ret;
324
325 /* Export DQ */
326 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
327 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
328 goto end_of_export;
329 len += ret;
330
331 /* Export DP */
332 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
333 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
334 goto end_of_export;
335 len += ret;
336
337 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100338 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
339 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100340 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
341 goto end_of_export;
342 len += ret;
343
344 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100345 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
346 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100347 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
348 goto end_of_export;
349 len += ret;
350
351 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100352 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
353 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100354 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
355 goto end_of_export;
356 len += ret;
357
358 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100359 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
360 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100361 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
362 goto end_of_export;
363 len += ret;
364
365 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100366 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
367 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100368 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
369 goto end_of_export;
370 len += ret;
371
372 end_of_export:
373
374 mbedtls_mpi_free( &T );
375 if( ret < 0 )
376 return( ret );
377
378 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100380 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
381 buf, MBEDTLS_ASN1_CONSTRUCTED |
382 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200383 }
384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_RSA_C */
386#if defined(MBEDTLS_ECP_C)
387 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200388 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200389 size_t pub_len = 0, par_len = 0;
390
391 /*
392 * RFC 5915, or SEC1 Appendix C.4
393 *
394 * ECPrivateKey ::= SEQUENCE {
395 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
396 * privateKey OCTET STRING,
397 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
398 * publicKey [1] BIT STRING OPTIONAL
399 * }
400 */
401
402 /* publicKey */
Hanno Becker64a81b02019-08-21 16:08:17 +0100403 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200404
405 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200407 *--c = 0;
408 pub_len += 1;
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
411 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
414 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
415 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200416 len += pub_len;
417
418 /* parameters */
Hanno Becker64a81b02019-08-21 16:08:17 +0100419 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
422 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
423 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200424 len += par_len;
425
426 /* privateKey: write as MPI then fix tag */
Hanno Becker64a81b02019-08-21 16:08:17 +0100427 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_privkey( &c, buf, key ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 *c = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200429
430 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
434 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
435 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200436 }
437 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /* MBEDTLS_ECP_C */
439 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200440
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200441 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200442}
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200445
446#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
447#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
448
449#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
450#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
451#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
452#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
453
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200454/*
455 * Max sizes of key per types. Shown as tag + len (+ content).
456 */
457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200459/*
460 * RSA public keys:
461 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
462 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
463 * + 1 + 1 + 9 (rsa oid)
464 * + 1 + 1 (params null)
465 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
466 * RSAPublicKey ::= SEQUENCE { 1 + 3
467 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
468 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
469 * }
470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200472
473/*
474 * RSA private keys:
475 * RSAPrivateKey ::= SEQUENCE { 1 + 3
476 * version Version, 1 + 1 + 1
477 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
478 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
479 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
480 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
481 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
482 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
483 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
484 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
485 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
486 * }
487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
489 MBEDTLS_MPI_MAX_SIZE % 2
490#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200491 + 5 * MPI_MAX_SIZE_2
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200494
495#define RSA_PUB_DER_MAX_BYTES 0
496#define RSA_PRV_DER_MAX_BYTES 0
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200501/*
502 * EC public keys:
503 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
504 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
505 * + 1 + 1 + 7 (ec oid)
506 * + 1 + 1 + 9 (namedCurve oid)
507 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
508 * + 1 (point format) [1]
509 * + 2 * ECP_MAX (coords) [1]
510 * }
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200513
514/*
515 * EC private keys:
516 * ECPrivateKey ::= SEQUENCE { 1 + 2
517 * version INTEGER , 1 + 1 + 1
518 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
519 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
520 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
521 * }
522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200526
527#define ECP_PUB_DER_MAX_BYTES 0
528#define ECP_PRV_DER_MAX_BYTES 0
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200531
532#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
533 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
534#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
535 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200538{
539 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200540 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200541 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200542
Gilles Peskinee97dc602018-12-19 00:51:38 +0100543 PK_VALIDATE_RET( key != NULL );
544 PK_VALIDATE_RET( buf != NULL || size == 0 );
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200547 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200548 {
549 return( ret );
550 }
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200553 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200554 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200555 {
556 return( ret );
557 }
558
559 return( 0 );
560}
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200563{
564 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200565 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200566 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200567 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200568
Gilles Peskinee97dc602018-12-19 00:51:38 +0100569 PK_VALIDATE_RET( key != NULL );
570 PK_VALIDATE_RET( buf != NULL || size == 0 );
571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200573 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#if defined(MBEDTLS_RSA_C)
576 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200577 {
578 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
579 end = PEM_END_PRIVATE_KEY_RSA;
580 }
581 else
582#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_ECP_C)
584 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200585 {
586 begin = PEM_BEGIN_PRIVATE_KEY_EC;
587 end = PEM_END_PRIVATE_KEY_EC;
588 }
589 else
590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200594 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200595 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200596 {
597 return( ret );
598 }
599
600 return( 0 );
601}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#endif /* MBEDTLS_PK_WRITE_C */