blob: 8d1da2f757fac27dfd56859f64cc0310da8a5f4c [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 Becker8fd55482017-08-23 14:07:48 +0100111 mbedtls_ecp_keypair *ec )
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];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
118 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200119 &len, buf, sizeof( buf ) ) ) != 0 )
120 {
121 return( ret );
122 }
123
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200124 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200126
127 *p -= len;
128 memcpy( *p, buf, len );
129
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200130 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200131}
132
133/*
134 * ECParameters ::= CHOICE {
135 * namedCurve OBJECT IDENTIFIER
136 * }
137 */
138static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100139 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200140{
141 int ret;
142 size_t len = 0;
143 const char *oid;
144 size_t oid_len;
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200147 return( ret );
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200150
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200151 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100156 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200157{
158 int ret;
159 size_t len = 0;
160
Gilles Peskinee97dc602018-12-19 00:51:38 +0100161 PK_VALIDATE_RET( p != NULL );
162 PK_VALIDATE_RET( *p != NULL );
163 PK_VALIDATE_RET( start != NULL );
164 PK_VALIDATE_RET( key != NULL );
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_RSA_C)
167 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
168 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200169 else
170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_ECP_C)
172 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
173 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200174 else
175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200177
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200178 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200179}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182{
183 int ret;
184 unsigned char *c;
185 size_t len = 0, par_len = 0, oid_len;
186 const char *oid;
187
Gilles Peskinee97dc602018-12-19 00:51:38 +0100188 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100189 if( size == 0 )
190 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
191 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100192
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200193 c = buf + size;
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200196
197 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200199
200 /*
201 * SubjectPublicKeyInfo ::= SEQUENCE {
202 * algorithm AlgorithmIdentifier,
203 * subjectPublicKey BIT STRING }
204 */
205 *--c = 0;
206 len += 1;
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
209 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200212 &oid, &oid_len ) ) != 0 )
213 {
214 return( ret );
215 }
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_ECP_C)
218 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200221 }
222#endif
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200225 par_len ) );
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
228 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
229 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200230
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200231 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200232}
233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200235{
236 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100237 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200238 size_t len = 0;
239
Gilles Peskinee97dc602018-12-19 00:51:38 +0100240 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100241 if( size == 0 )
242 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
243 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100244
245 c = buf + size;
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#if defined(MBEDTLS_RSA_C)
248 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200249 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100250 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200252
Hanno Becker15f81fa2017-08-23 12:38:27 +0100253 /*
254 * Export the parameters one after another to avoid simultaneous copies.
255 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200256
Hanno Becker15f81fa2017-08-23 12:38:27 +0100257 mbedtls_mpi_init( &T );
258
259 /* Export QP */
260 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
261 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
262 goto end_of_export;
263 len += ret;
264
265 /* Export DQ */
266 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
267 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
268 goto end_of_export;
269 len += ret;
270
271 /* Export DP */
272 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
273 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
274 goto end_of_export;
275 len += ret;
276
277 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100278 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
279 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100280 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
281 goto end_of_export;
282 len += ret;
283
284 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100285 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
286 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100287 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
288 goto end_of_export;
289 len += ret;
290
291 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100292 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
293 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100294 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
295 goto end_of_export;
296 len += ret;
297
298 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100299 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
300 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100301 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
302 goto end_of_export;
303 len += ret;
304
305 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100306 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
307 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100308 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
309 goto end_of_export;
310 len += ret;
311
312 end_of_export:
313
314 mbedtls_mpi_free( &T );
315 if( ret < 0 )
316 return( ret );
317
318 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100320 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
321 buf, MBEDTLS_ASN1_CONSTRUCTED |
322 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200323 }
324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#endif /* MBEDTLS_RSA_C */
326#if defined(MBEDTLS_ECP_C)
327 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200330 size_t pub_len = 0, par_len = 0;
331
332 /*
333 * RFC 5915, or SEC1 Appendix C.4
334 *
335 * ECPrivateKey ::= SEQUENCE {
336 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
337 * privateKey OCTET STRING,
338 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
339 * publicKey [1] BIT STRING OPTIONAL
340 * }
341 */
342
343 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200345
346 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200348 *--c = 0;
349 pub_len += 1;
350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
352 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
355 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
356 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200357 len += pub_len;
358
359 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
363 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
364 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200365 len += par_len;
366
367 /* privateKey: write as MPI then fix tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
369 *c = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200370
371 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
375 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
376 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200377 }
378 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379#endif /* MBEDTLS_ECP_C */
380 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200381
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200382 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200383}
384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200386
387#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
388#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
389
390#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
391#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
392#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
393#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
394
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200395/*
396 * Max sizes of key per types. Shown as tag + len (+ content).
397 */
398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200400/*
401 * RSA public keys:
402 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
403 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
404 * + 1 + 1 + 9 (rsa oid)
405 * + 1 + 1 (params null)
406 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
407 * RSAPublicKey ::= SEQUENCE { 1 + 3
408 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
409 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
410 * }
411 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200413
414/*
415 * RSA private keys:
416 * RSAPrivateKey ::= SEQUENCE { 1 + 3
417 * version Version, 1 + 1 + 1
418 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
419 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
420 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
421 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
422 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
423 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
424 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
425 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
426 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
427 * }
428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
430 MBEDTLS_MPI_MAX_SIZE % 2
431#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200432 + 5 * MPI_MAX_SIZE_2
433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200435
436#define RSA_PUB_DER_MAX_BYTES 0
437#define RSA_PRV_DER_MAX_BYTES 0
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200442/*
443 * EC public keys:
444 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
445 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
446 * + 1 + 1 + 7 (ec oid)
447 * + 1 + 1 + 9 (namedCurve oid)
448 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
449 * + 1 (point format) [1]
450 * + 2 * ECP_MAX (coords) [1]
451 * }
452 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200454
455/*
456 * EC private keys:
457 * ECPrivateKey ::= SEQUENCE { 1 + 2
458 * version INTEGER , 1 + 1 + 1
459 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
460 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
461 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
462 * }
463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200467
468#define ECP_PUB_DER_MAX_BYTES 0
469#define ECP_PRV_DER_MAX_BYTES 0
470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200472
473#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
474 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
475#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
476 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200479{
480 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200481 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200482 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200483
Gilles Peskinee97dc602018-12-19 00:51:38 +0100484 PK_VALIDATE_RET( key != NULL );
485 PK_VALIDATE_RET( buf != NULL || size == 0 );
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200488 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200489 {
490 return( ret );
491 }
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200494 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200495 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200496 {
497 return( ret );
498 }
499
500 return( 0 );
501}
502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200504{
505 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200506 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200507 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200508 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200509
Gilles Peskinee97dc602018-12-19 00:51:38 +0100510 PK_VALIDATE_RET( key != NULL );
511 PK_VALIDATE_RET( buf != NULL || size == 0 );
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200514 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_RSA_C)
517 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200518 {
519 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
520 end = PEM_END_PRIVATE_KEY_RSA;
521 }
522 else
523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_ECP_C)
525 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200526 {
527 begin = PEM_BEGIN_PRIVATE_KEY_EC;
528 end = PEM_END_PRIVATE_KEY_EC;
529 }
530 else
531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200535 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200536 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200537 {
538 return( ret );
539 }
540
541 return( 0 );
542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_PK_WRITE_C */