blob: dcbc86cac79be2f4d42d9fa46493f5d973bc570d [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107/*
108 * EC public key is an EC point
109 */
110static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker64a81b02019-08-21 16:08:17 +0100111 mbedtls_pk_context const *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200112{
113 int ret;
114 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Hanno Becker64a81b02019-08-21 16:08:17 +0100116 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
119 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200120 &len, buf, sizeof( buf ) ) ) != 0 )
121 {
122 return( ret );
123 }
124
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200125 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200127
128 *p -= len;
129 memcpy( *p, buf, len );
130
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200131 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200132}
133
Hanno Becker64a81b02019-08-21 16:08:17 +0100134static int pk_write_ec_privkey( unsigned char **p, unsigned char *start,
135 mbedtls_pk_context const *key )
136{
137 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
138 return( mbedtls_asn1_write_mpi( p, start, &ec->d ) );
139}
140
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200141/*
142 * ECParameters ::= CHOICE {
143 * namedCurve OBJECT IDENTIFIER
144 * }
145 */
146static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker64a81b02019-08-21 16:08:17 +0100147 mbedtls_pk_context const *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200148{
149 int ret;
150 size_t len = 0;
151 const char *oid;
152 size_t oid_len;
Hanno Becker64a81b02019-08-21 16:08:17 +0100153 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200156 return( ret );
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200159
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200160 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100165 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200166{
167 int ret;
168 size_t len = 0;
169
Gilles Peskinee97dc602018-12-19 00:51:38 +0100170 PK_VALIDATE_RET( p != NULL );
171 PK_VALIDATE_RET( *p != NULL );
172 PK_VALIDATE_RET( start != NULL );
173 PK_VALIDATE_RET( key != NULL );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_RSA_C)
176 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
177 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200178 else
179#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_ECP_C)
181 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Hanno Becker64a81b02019-08-21 16:08:17 +0100182 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200183 else
184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200186
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200187 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200188}
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200191{
192 int ret;
193 unsigned char *c;
194 size_t len = 0, par_len = 0, oid_len;
195 const char *oid;
196
Gilles Peskinee97dc602018-12-19 00:51:38 +0100197 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100198 if( size == 0 )
199 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
200 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100201
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200202 c = buf + size;
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200205
206 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200208
209 /*
210 * SubjectPublicKeyInfo ::= SEQUENCE {
211 * algorithm AlgorithmIdentifier,
212 * subjectPublicKey BIT STRING }
213 */
214 *--c = 0;
215 len += 1;
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
218 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200221 &oid, &oid_len ) ) != 0 )
222 {
223 return( ret );
224 }
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_ECP_C)
227 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200228 {
Hanno Becker64a81b02019-08-21 16:08:17 +0100229 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200230 }
231#endif
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234 par_len ) );
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
237 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
238 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200239
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200240 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200241}
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200244{
245 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100246 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200247 size_t len = 0;
248
Gilles Peskinee97dc602018-12-19 00:51:38 +0100249 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100250 if( size == 0 )
251 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
252 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100253
254 c = buf + size;
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_RSA_C)
257 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200258 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100259 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200261
Hanno Becker15f81fa2017-08-23 12:38:27 +0100262 /*
263 * Export the parameters one after another to avoid simultaneous copies.
264 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200265
Hanno Becker15f81fa2017-08-23 12:38:27 +0100266 mbedtls_mpi_init( &T );
267
268 /* Export QP */
269 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
270 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
271 goto end_of_export;
272 len += ret;
273
274 /* Export DQ */
275 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
276 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
277 goto end_of_export;
278 len += ret;
279
280 /* Export DP */
281 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
282 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
283 goto end_of_export;
284 len += ret;
285
286 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100287 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
288 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100289 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
290 goto end_of_export;
291 len += ret;
292
293 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100294 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
295 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100296 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
297 goto end_of_export;
298 len += ret;
299
300 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100301 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
302 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100303 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
304 goto end_of_export;
305 len += ret;
306
307 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100308 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
309 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100310 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
311 goto end_of_export;
312 len += ret;
313
314 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100315 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
316 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100317 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
318 goto end_of_export;
319 len += ret;
320
321 end_of_export:
322
323 mbedtls_mpi_free( &T );
324 if( ret < 0 )
325 return( ret );
326
327 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100329 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
330 buf, MBEDTLS_ASN1_CONSTRUCTED |
331 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200332 }
333 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#endif /* MBEDTLS_RSA_C */
335#if defined(MBEDTLS_ECP_C)
336 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200337 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200338 size_t pub_len = 0, par_len = 0;
339
340 /*
341 * RFC 5915, or SEC1 Appendix C.4
342 *
343 * ECPrivateKey ::= SEQUENCE {
344 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
345 * privateKey OCTET STRING,
346 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
347 * publicKey [1] BIT STRING OPTIONAL
348 * }
349 */
350
351 /* publicKey */
Hanno Becker64a81b02019-08-21 16:08:17 +0100352 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200353
354 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200356 *--c = 0;
357 pub_len += 1;
358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
360 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
363 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
364 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200365 len += pub_len;
366
367 /* parameters */
Hanno Becker64a81b02019-08-21 16:08:17 +0100368 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
371 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
372 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200373 len += par_len;
374
375 /* privateKey: write as MPI then fix tag */
Hanno Becker64a81b02019-08-21 16:08:17 +0100376 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_privkey( &c, buf, key ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 *c = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200378
379 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
383 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
384 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200385 }
386 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387#endif /* MBEDTLS_ECP_C */
388 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200389
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200390 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200391}
392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200394
395#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
396#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
397
398#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
399#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
400#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
401#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
402
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200403/*
404 * Max sizes of key per types. Shown as tag + len (+ content).
405 */
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200408/*
409 * RSA public keys:
410 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
411 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
412 * + 1 + 1 + 9 (rsa oid)
413 * + 1 + 1 (params null)
414 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
415 * RSAPublicKey ::= SEQUENCE { 1 + 3
416 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
417 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
418 * }
419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200421
422/*
423 * RSA private keys:
424 * RSAPrivateKey ::= SEQUENCE { 1 + 3
425 * version Version, 1 + 1 + 1
426 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
427 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
428 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
429 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
430 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
431 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
432 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
433 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
434 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
435 * }
436 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
438 MBEDTLS_MPI_MAX_SIZE % 2
439#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200440 + 5 * MPI_MAX_SIZE_2
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200443
444#define RSA_PUB_DER_MAX_BYTES 0
445#define RSA_PRV_DER_MAX_BYTES 0
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200450/*
451 * EC public keys:
452 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
453 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
454 * + 1 + 1 + 7 (ec oid)
455 * + 1 + 1 + 9 (namedCurve oid)
456 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
457 * + 1 (point format) [1]
458 * + 2 * ECP_MAX (coords) [1]
459 * }
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200462
463/*
464 * EC private keys:
465 * ECPrivateKey ::= SEQUENCE { 1 + 2
466 * version INTEGER , 1 + 1 + 1
467 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
468 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
469 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
470 * }
471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200475
476#define ECP_PUB_DER_MAX_BYTES 0
477#define ECP_PRV_DER_MAX_BYTES 0
478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200480
481#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
482 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
483#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
484 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200487{
488 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200489 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200490 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200491
Gilles Peskinee97dc602018-12-19 00:51:38 +0100492 PK_VALIDATE_RET( key != NULL );
493 PK_VALIDATE_RET( buf != NULL || size == 0 );
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200496 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200497 {
498 return( ret );
499 }
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200502 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200503 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200504 {
505 return( ret );
506 }
507
508 return( 0 );
509}
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200512{
513 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200514 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200515 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200516 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200517
Gilles Peskinee97dc602018-12-19 00:51:38 +0100518 PK_VALIDATE_RET( key != NULL );
519 PK_VALIDATE_RET( buf != NULL || size == 0 );
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200522 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_RSA_C)
525 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200526 {
527 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
528 end = PEM_END_PRIVATE_KEY_RSA;
529 }
530 else
531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532#if defined(MBEDTLS_ECP_C)
533 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200534 {
535 begin = PEM_BEGIN_PRIVATE_KEY_EC;
536 end = PEM_END_PRIVATE_KEY_EC;
537 }
538 else
539#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200543 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200544 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200545 {
546 return( ret );
547 }
548
549 return( 0 );
550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553#endif /* MBEDTLS_PK_WRITE_C */