blob: 03d14f2ff97333b6190fa5674b1e0cdb838d67a8 [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)
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020054#else
55#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020056#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020058#endif
59
Gilles Peskinee97dc602018-12-19 00:51:38 +010060/* Parameter validation macros based on platform_util.h */
61#define PK_VALIDATE_RET( cond ) \
62 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
63#define PK_VALIDATE( cond ) \
64 MBEDTLS_INTERNAL_VALIDATE( cond )
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020067/*
68 * RSAPublicKey ::= SEQUENCE {
69 * modulus INTEGER, -- n
70 * publicExponent INTEGER -- e
71 * }
72 */
73static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010074 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020075{
76 int ret;
77 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010078 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020079
Hanno Becker15f81fa2017-08-23 12:38:27 +010080 mbedtls_mpi_init( &T );
81
82 /* Export E */
83 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
84 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
85 goto end_of_export;
86 len += ret;
87
88 /* Export N */
89 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
90 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
91 goto end_of_export;
92 len += ret;
93
94end_of_export:
95
96 mbedtls_mpi_free( &T );
97 if( ret < 0 )
98 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
101 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
102 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200104 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200109/*
110 * EC public key is an EC point
111 */
112static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100113 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200114{
115 int ret;
116 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
120 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200121 &len, buf, sizeof( buf ) ) ) != 0 )
122 {
123 return( ret );
124 }
125
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200126 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200128
129 *p -= len;
130 memcpy( *p, buf, len );
131
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200132 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200133}
134
135/*
136 * ECParameters ::= CHOICE {
137 * namedCurve OBJECT IDENTIFIER
138 * }
139 */
140static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100141 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200142{
143 int ret;
144 size_t len = 0;
145 const char *oid;
146 size_t oid_len;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200149 return( ret );
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200152
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200153 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200154}
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200155
156/*
157 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
158 */
159static int pk_write_ec_private( unsigned char **p, unsigned char *start,
160 mbedtls_ecp_keypair *ec )
161{
162 int ret;
163 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
164 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
165
166 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
167 if( ret != 0 )
168 goto exit;
169 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
170
171exit:
172 mbedtls_platform_zeroize( tmp, byte_length );
173 return( ret );
174}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100178 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200179{
180 int ret;
181 size_t len = 0;
182
Gilles Peskinee97dc602018-12-19 00:51:38 +0100183 PK_VALIDATE_RET( p != NULL );
184 PK_VALIDATE_RET( *p != NULL );
185 PK_VALIDATE_RET( start != NULL );
186 PK_VALIDATE_RET( key != NULL );
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_RSA_C)
189 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
190 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200191 else
192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_ECP_C)
194 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
195 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200196 else
197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200199
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200200 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200201}
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200204{
205 int ret;
206 unsigned char *c;
207 size_t len = 0, par_len = 0, oid_len;
208 const char *oid;
209
Gilles Peskinee97dc602018-12-19 00:51:38 +0100210 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100211 if( size == 0 )
212 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
213 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100214
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200215 c = buf + size;
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200218
219 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200221
222 /*
223 * SubjectPublicKeyInfo ::= SEQUENCE {
224 * algorithm AlgorithmIdentifier,
225 * subjectPublicKey BIT STRING }
226 */
227 *--c = 0;
228 len += 1;
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
231 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234 &oid, &oid_len ) ) != 0 )
235 {
236 return( ret );
237 }
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_ECP_C)
240 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200243 }
244#endif
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200247 par_len ) );
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
250 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
251 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200252
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200253 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200254}
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200257{
258 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100259 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200260 size_t len = 0;
261
Gilles Peskinee97dc602018-12-19 00:51:38 +0100262 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100263 if( size == 0 )
264 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
265 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100266
267 c = buf + size;
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_RSA_C)
270 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200271 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100272 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200274
Hanno Becker15f81fa2017-08-23 12:38:27 +0100275 /*
276 * Export the parameters one after another to avoid simultaneous copies.
277 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200278
Hanno Becker15f81fa2017-08-23 12:38:27 +0100279 mbedtls_mpi_init( &T );
280
281 /* Export QP */
282 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
283 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
284 goto end_of_export;
285 len += ret;
286
287 /* Export DQ */
288 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
289 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
290 goto end_of_export;
291 len += ret;
292
293 /* Export DP */
294 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
295 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
296 goto end_of_export;
297 len += ret;
298
299 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100300 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
301 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100302 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
303 goto end_of_export;
304 len += ret;
305
306 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100307 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
308 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100309 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
310 goto end_of_export;
311 len += ret;
312
313 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100314 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
315 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100316 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
317 goto end_of_export;
318 len += ret;
319
320 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100321 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
322 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100323 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
324 goto end_of_export;
325 len += ret;
326
327 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100328 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
329 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100330 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
331 goto end_of_export;
332 len += ret;
333
334 end_of_export:
335
336 mbedtls_mpi_free( &T );
337 if( ret < 0 )
338 return( ret );
339
340 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100342 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
343 buf, MBEDTLS_ASN1_CONSTRUCTED |
344 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200345 }
346 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#endif /* MBEDTLS_RSA_C */
348#if defined(MBEDTLS_ECP_C)
349 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200352 size_t pub_len = 0, par_len = 0;
353
354 /*
355 * RFC 5915, or SEC1 Appendix C.4
356 *
357 * ECPrivateKey ::= SEQUENCE {
358 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
359 * privateKey OCTET STRING,
360 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
361 * publicKey [1] BIT STRING OPTIONAL
362 * }
363 */
364
365 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200367
368 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200370 *--c = 0;
371 pub_len += 1;
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
374 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
377 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
378 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200379 len += pub_len;
380
381 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
385 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
386 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200387 len += par_len;
388
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200389 /* privateKey */
390 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200391
392 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
396 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
397 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200398 }
399 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_ECP_C */
401 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200402
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200403 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200404}
405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200407
408#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
409#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
410
411#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
412#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
413#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
414#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
415
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200416/*
417 * Max sizes of key per types. Shown as tag + len (+ content).
418 */
419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200421/*
422 * RSA public keys:
423 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
424 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
425 * + 1 + 1 + 9 (rsa oid)
426 * + 1 + 1 (params null)
427 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
428 * RSAPublicKey ::= SEQUENCE { 1 + 3
429 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
430 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
431 * }
432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200434
435/*
436 * RSA private keys:
437 * RSAPrivateKey ::= SEQUENCE { 1 + 3
438 * version Version, 1 + 1 + 1
439 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
440 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
441 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
442 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
443 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
444 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
445 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
446 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
447 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
448 * }
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
451 MBEDTLS_MPI_MAX_SIZE % 2
452#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200453 + 5 * MPI_MAX_SIZE_2
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200456
457#define RSA_PUB_DER_MAX_BYTES 0
458#define RSA_PRV_DER_MAX_BYTES 0
459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200463/*
464 * EC public keys:
465 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
466 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
467 * + 1 + 1 + 7 (ec oid)
468 * + 1 + 1 + 9 (namedCurve oid)
469 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
470 * + 1 (point format) [1]
471 * + 2 * ECP_MAX (coords) [1]
472 * }
473 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200475
476/*
477 * EC private keys:
478 * ECPrivateKey ::= SEQUENCE { 1 + 2
479 * version INTEGER , 1 + 1 + 1
480 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
481 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
482 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
483 * }
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200488
489#define ECP_PUB_DER_MAX_BYTES 0
490#define ECP_PRV_DER_MAX_BYTES 0
491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200493
494#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
495 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
496#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
497 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200500{
501 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200502 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200503 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200504
Gilles Peskinee97dc602018-12-19 00:51:38 +0100505 PK_VALIDATE_RET( key != NULL );
506 PK_VALIDATE_RET( buf != NULL || size == 0 );
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200509 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200510 {
511 return( ret );
512 }
513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200515 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200516 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200517 {
518 return( ret );
519 }
520
521 return( 0 );
522}
523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200525{
526 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200527 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200528 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200529 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200530
Gilles Peskinee97dc602018-12-19 00:51:38 +0100531 PK_VALIDATE_RET( key != NULL );
532 PK_VALIDATE_RET( buf != NULL || size == 0 );
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200535 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537#if defined(MBEDTLS_RSA_C)
538 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200539 {
540 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
541 end = PEM_END_PRIVATE_KEY_RSA;
542 }
543 else
544#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_ECP_C)
546 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200547 {
548 begin = PEM_BEGIN_PRIVATE_KEY_EC;
549 end = PEM_END_PRIVATE_KEY_EC;
550 }
551 else
552#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200556 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200557 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200558 {
559 return( ret );
560 }
561
562 return( 0 );
563}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566#endif /* MBEDTLS_PK_WRITE_C */