blob: 76159e5a80a149605f49a2667fce45c3e28c3dfb [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/pk.h"
58#include "mbedtls/asn1write.h"
59#include "mbedtls/oid.h"
Gilles Peskinee97dc602018-12-19 00:51:38 +010060#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020061
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <string.h>
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ECP_C)
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020068#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/ecp.h"
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020070#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020077#endif
78
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020081#else
82#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020083#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020085#endif
86
Gilles Peskinee97dc602018-12-19 00:51:38 +010087/* Parameter validation macros based on platform_util.h */
88#define PK_VALIDATE_RET( cond ) \
89 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
90#define PK_VALIDATE( cond ) \
91 MBEDTLS_INTERNAL_VALIDATE( cond )
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020094/*
95 * RSAPublicKey ::= SEQUENCE {
96 * modulus INTEGER, -- n
97 * publicExponent INTEGER -- e
98 * }
99 */
100static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100101 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200102{
103 int ret;
104 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +0100105 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200106
Hanno Becker15f81fa2017-08-23 12:38:27 +0100107 mbedtls_mpi_init( &T );
108
109 /* Export E */
110 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
111 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
112 goto end_of_export;
113 len += ret;
114
115 /* Export N */
116 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
117 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
118 goto end_of_export;
119 len += ret;
120
121end_of_export:
122
123 mbedtls_mpi_free( &T );
124 if( ret < 0 )
125 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
128 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
129 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200130
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200131 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200132}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200136/*
137 * EC public key is an EC point
138 */
139static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100140 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200141{
142 int ret;
143 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
147 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200148 &len, buf, sizeof( buf ) ) ) != 0 )
149 {
150 return( ret );
151 }
152
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200153 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200155
156 *p -= len;
157 memcpy( *p, buf, len );
158
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200159 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200160}
161
162/*
163 * ECParameters ::= CHOICE {
164 * namedCurve OBJECT IDENTIFIER
165 * }
166 */
167static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100168 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200169{
170 int ret;
171 size_t len = 0;
172 const char *oid;
173 size_t oid_len;
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200176 return( ret );
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200179
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200180 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200181}
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200182
183/*
184 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
185 */
186static int pk_write_ec_private( unsigned char **p, unsigned char *start,
187 mbedtls_ecp_keypair *ec )
188{
189 int ret;
190 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
191 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
192
193 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
194 if( ret != 0 )
195 goto exit;
196 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
197
198exit:
199 mbedtls_platform_zeroize( tmp, byte_length );
200 return( ret );
201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100205 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200206{
207 int ret;
208 size_t len = 0;
209
Gilles Peskinee97dc602018-12-19 00:51:38 +0100210 PK_VALIDATE_RET( p != NULL );
211 PK_VALIDATE_RET( *p != NULL );
212 PK_VALIDATE_RET( start != NULL );
213 PK_VALIDATE_RET( key != NULL );
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_RSA_C)
216 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
217 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200218 else
219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_ECP_C)
221 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
222 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200223 else
224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200226
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200227 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200228}
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200231{
232 int ret;
233 unsigned char *c;
234 size_t len = 0, par_len = 0, oid_len;
235 const char *oid;
236
Gilles Peskinee97dc602018-12-19 00:51:38 +0100237 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100238 if( size == 0 )
239 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
240 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100241
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200242 c = buf + size;
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200245
246 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200248
249 /*
250 * SubjectPublicKeyInfo ::= SEQUENCE {
251 * algorithm AlgorithmIdentifier,
252 * subjectPublicKey BIT STRING }
253 */
254 *--c = 0;
255 len += 1;
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200261 &oid, &oid_len ) ) != 0 )
262 {
263 return( ret );
264 }
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#if defined(MBEDTLS_ECP_C)
267 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200270 }
271#endif
272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200274 par_len ) );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
277 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
278 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200279
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200280 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200281}
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200284{
285 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100286 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200287 size_t len = 0;
288
Gilles Peskinee97dc602018-12-19 00:51:38 +0100289 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100290 if( size == 0 )
291 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
292 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100293
294 c = buf + size;
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296#if defined(MBEDTLS_RSA_C)
297 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200298 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100299 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200301
Hanno Becker15f81fa2017-08-23 12:38:27 +0100302 /*
303 * Export the parameters one after another to avoid simultaneous copies.
304 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200305
Hanno Becker15f81fa2017-08-23 12:38:27 +0100306 mbedtls_mpi_init( &T );
307
308 /* Export QP */
309 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
310 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
311 goto end_of_export;
312 len += ret;
313
314 /* Export DQ */
315 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
316 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
317 goto end_of_export;
318 len += ret;
319
320 /* Export DP */
321 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
322 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
323 goto end_of_export;
324 len += ret;
325
326 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100327 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
328 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100329 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
330 goto end_of_export;
331 len += ret;
332
333 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100334 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
335 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100336 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
337 goto end_of_export;
338 len += ret;
339
340 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100341 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
342 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100343 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
344 goto end_of_export;
345 len += ret;
346
347 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100348 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
349 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100350 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
351 goto end_of_export;
352 len += ret;
353
354 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100355 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
356 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100357 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
358 goto end_of_export;
359 len += ret;
360
361 end_of_export:
362
363 mbedtls_mpi_free( &T );
364 if( ret < 0 )
365 return( ret );
366
367 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100369 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
370 buf, MBEDTLS_ASN1_CONSTRUCTED |
371 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200372 }
373 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374#endif /* MBEDTLS_RSA_C */
375#if defined(MBEDTLS_ECP_C)
376 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200379 size_t pub_len = 0, par_len = 0;
380
381 /*
382 * RFC 5915, or SEC1 Appendix C.4
383 *
384 * ECPrivateKey ::= SEQUENCE {
385 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
386 * privateKey OCTET STRING,
387 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
388 * publicKey [1] BIT STRING OPTIONAL
389 * }
390 */
391
392 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200394
395 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200397 *--c = 0;
398 pub_len += 1;
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
401 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
404 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
405 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200406 len += pub_len;
407
408 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
412 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
413 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200414 len += par_len;
415
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200416 /* privateKey */
417 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200418
419 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
423 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
424 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200425 }
426 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#endif /* MBEDTLS_ECP_C */
428 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200429
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200430 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200431}
432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200434
435#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
436#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
437
438#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
439#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
440#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
441#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
442
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200443/*
444 * Max sizes of key per types. Shown as tag + len (+ content).
445 */
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200448/*
449 * RSA public keys:
450 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
451 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
452 * + 1 + 1 + 9 (rsa oid)
453 * + 1 + 1 (params null)
454 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
455 * RSAPublicKey ::= SEQUENCE { 1 + 3
456 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
457 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
458 * }
459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200461
462/*
463 * RSA private keys:
464 * RSAPrivateKey ::= SEQUENCE { 1 + 3
465 * version Version, 1 + 1 + 1
466 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
467 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
468 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
469 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
470 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
471 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
472 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
473 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
474 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
475 * }
476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
478 MBEDTLS_MPI_MAX_SIZE % 2
479#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200480 + 5 * MPI_MAX_SIZE_2
481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200483
484#define RSA_PUB_DER_MAX_BYTES 0
485#define RSA_PRV_DER_MAX_BYTES 0
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200490/*
491 * EC public keys:
492 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
493 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
494 * + 1 + 1 + 7 (ec oid)
495 * + 1 + 1 + 9 (namedCurve oid)
496 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
497 * + 1 (point format) [1]
498 * + 2 * ECP_MAX (coords) [1]
499 * }
500 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200502
503/*
504 * EC private keys:
505 * ECPrivateKey ::= SEQUENCE { 1 + 2
506 * version INTEGER , 1 + 1 + 1
507 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
508 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
509 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
510 * }
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200515
516#define ECP_PUB_DER_MAX_BYTES 0
517#define ECP_PRV_DER_MAX_BYTES 0
518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200520
521#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
522 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
523#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
524 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200527{
528 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200529 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200530 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200531
Gilles Peskinee97dc602018-12-19 00:51:38 +0100532 PK_VALIDATE_RET( key != NULL );
533 PK_VALIDATE_RET( buf != NULL || size == 0 );
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200536 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200537 {
538 return( ret );
539 }
540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200542 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200543 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200544 {
545 return( ret );
546 }
547
548 return( 0 );
549}
550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200552{
553 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200554 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200555 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200556 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200557
Gilles Peskinee97dc602018-12-19 00:51:38 +0100558 PK_VALIDATE_RET( key != NULL );
559 PK_VALIDATE_RET( buf != NULL || size == 0 );
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200562 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564#if defined(MBEDTLS_RSA_C)
565 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200566 {
567 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
568 end = PEM_END_PRIVATE_KEY_RSA;
569 }
570 else
571#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572#if defined(MBEDTLS_ECP_C)
573 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200574 {
575 begin = PEM_BEGIN_PRIVATE_KEY_EC;
576 end = PEM_END_PRIVATE_KEY_EC;
577 }
578 else
579#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200583 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200584 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200585 {
586 return( ret );
587 }
588
589 return( 0 );
590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_PK_WRITE_C */