blob: 6bb31bc3b50cf322281bc6465e0dd97a0cbfe355 [file] [log] [blame]
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001/*
2 * Public Key layer for writing key files and structures
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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 * **********
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pk.h"
56#include "mbedtls/asn1write.h"
57#include "mbedtls/oid.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020058
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <string.h>
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_ECP_C)
Gilles Peskine98add4f2018-08-11 00:48:44 +020065#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/ecp.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020073#endif
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020077#else
78#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020079#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020081#endif
82
Gilles Peskine98add4f2018-08-11 00:48:44 +020083#if defined(MBEDTLS_ECP_C)
84/* Implementation that should never be optimized out by the compiler */
85static void mbedtls_zeroize( void *v, size_t n ) {
86 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
87}
88#endif /* MBEDTLS_ECP_C */
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020091/*
92 * RSAPublicKey ::= SEQUENCE {
93 * modulus INTEGER, -- n
94 * publicExponent INTEGER -- e
95 * }
96 */
97static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010098 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020099{
100 int ret;
101 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +0100102 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103
Hanno Becker15f81fa2017-08-23 12:38:27 +0100104 mbedtls_mpi_init( &T );
105
106 /* Export E */
107 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
108 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
109 goto end_of_export;
110 len += ret;
111
112 /* Export N */
113 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
114 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
115 goto end_of_export;
116 len += ret;
117
118end_of_export:
119
120 mbedtls_mpi_free( &T );
121 if( ret < 0 )
122 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
125 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
126 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200127
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200128 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200129}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200133/*
134 * EC public key is an EC point
135 */
136static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100137 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200138{
139 int ret;
140 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
144 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200145 &len, buf, sizeof( buf ) ) ) != 0 )
146 {
147 return( ret );
148 }
149
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200150 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200152
153 *p -= len;
154 memcpy( *p, buf, len );
155
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200156 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200157}
158
159/*
160 * ECParameters ::= CHOICE {
161 * namedCurve OBJECT IDENTIFIER
162 * }
163 */
164static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100165 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200166{
167 int ret;
168 size_t len = 0;
169 const char *oid;
170 size_t oid_len;
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200173 return( ret );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200176
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200177 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200178}
Gilles Peskine98add4f2018-08-11 00:48:44 +0200179
180/*
181 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
182 */
183static int pk_write_ec_private( unsigned char **p, unsigned char *start,
184 mbedtls_ecp_keypair *ec )
185{
186 int ret;
187 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
188 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
189
190 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
191 if( ret != 0 )
192 goto exit;
193 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
194
195exit:
196 mbedtls_zeroize( tmp, byte_length );
197 return( ret );
198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100202 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200203{
204 int ret;
205 size_t len = 0;
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_RSA_C)
208 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
209 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200210 else
211#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_ECP_C)
213 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
214 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200215 else
216#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200218
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200219 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200220}
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200223{
224 int ret;
225 unsigned char *c;
226 size_t len = 0, par_len = 0, oid_len;
227 const char *oid;
228
229 c = buf + size;
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200232
233 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200235
236 /*
237 * SubjectPublicKeyInfo ::= SEQUENCE {
238 * algorithm AlgorithmIdentifier,
239 * subjectPublicKey BIT STRING }
240 */
241 *--c = 0;
242 len += 1;
243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
245 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200248 &oid, &oid_len ) ) != 0 )
249 {
250 return( ret );
251 }
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_ECP_C)
254 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200257 }
258#endif
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200261 par_len ) );
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
264 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
265 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200266
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200267 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200268}
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200271{
272 int ret;
273 unsigned char *c = buf + size;
274 size_t len = 0;
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276#if defined(MBEDTLS_RSA_C)
277 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200278 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100279 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200281
Hanno Becker15f81fa2017-08-23 12:38:27 +0100282 /*
283 * Export the parameters one after another to avoid simultaneous copies.
284 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200285
Hanno Becker15f81fa2017-08-23 12:38:27 +0100286 mbedtls_mpi_init( &T );
287
288 /* Export QP */
289 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
290 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
291 goto end_of_export;
292 len += ret;
293
294 /* Export DQ */
295 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
296 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
297 goto end_of_export;
298 len += ret;
299
300 /* Export DP */
301 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
302 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
303 goto end_of_export;
304 len += ret;
305
306 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100307 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
308 &T, 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 P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100314 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
315 NULL, NULL, 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 D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100321 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
322 NULL, &T, NULL ) ) != 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 E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100328 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
329 NULL, NULL, &T ) ) != 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 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100335 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
336 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100337 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
338 goto end_of_export;
339 len += ret;
340
341 end_of_export:
342
343 mbedtls_mpi_free( &T );
344 if( ret < 0 )
345 return( ret );
346
347 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100349 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
350 buf, MBEDTLS_ASN1_CONSTRUCTED |
351 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200352 }
353 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#endif /* MBEDTLS_RSA_C */
355#if defined(MBEDTLS_ECP_C)
356 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200359 size_t pub_len = 0, par_len = 0;
360
361 /*
362 * RFC 5915, or SEC1 Appendix C.4
363 *
364 * ECPrivateKey ::= SEQUENCE {
365 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
366 * privateKey OCTET STRING,
367 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
368 * publicKey [1] BIT STRING OPTIONAL
369 * }
370 */
371
372 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200374
375 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200377 *--c = 0;
378 pub_len += 1;
379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
381 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
384 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
385 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200386 len += pub_len;
387
388 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
392 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
393 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200394 len += par_len;
395
Gilles Peskine98add4f2018-08-11 00:48:44 +0200396 /* privateKey */
397 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200398
399 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
403 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
404 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200405 }
406 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#endif /* MBEDTLS_ECP_C */
408 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200409
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200410 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200411}
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200414
415#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
416#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
417
418#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
419#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
420#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
421#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
422
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200423/*
424 * Max sizes of key per types. Shown as tag + len (+ content).
425 */
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200428/*
429 * RSA public keys:
430 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
431 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
432 * + 1 + 1 + 9 (rsa oid)
433 * + 1 + 1 (params null)
434 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
435 * RSAPublicKey ::= SEQUENCE { 1 + 3
436 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
437 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
438 * }
439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200441
442/*
443 * RSA private keys:
444 * RSAPrivateKey ::= SEQUENCE { 1 + 3
445 * version Version, 1 + 1 + 1
446 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
447 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
448 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
449 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
450 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
451 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
452 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
453 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
454 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
455 * }
456 */
Daniel Ottef9111bb2021-02-01 14:23:30 +0100457#define MPI_MAX_SIZE_2 ( MBEDTLS_MPI_MAX_SIZE / 2 + \
458 MBEDTLS_MPI_MAX_SIZE % 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200460 + 5 * MPI_MAX_SIZE_2
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200463
464#define RSA_PUB_DER_MAX_BYTES 0
465#define RSA_PRV_DER_MAX_BYTES 0
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200470/*
471 * EC public keys:
472 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
473 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
474 * + 1 + 1 + 7 (ec oid)
475 * + 1 + 1 + 9 (namedCurve oid)
476 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
477 * + 1 (point format) [1]
478 * + 2 * ECP_MAX (coords) [1]
479 * }
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200482
483/*
484 * EC private keys:
485 * ECPrivateKey ::= SEQUENCE { 1 + 2
486 * version INTEGER , 1 + 1 + 1
487 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
488 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
489 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
490 * }
491 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200495
496#define ECP_PUB_DER_MAX_BYTES 0
497#define ECP_PRV_DER_MAX_BYTES 0
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200500
501#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
502 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
503#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
504 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200507{
508 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200509 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200510 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200513 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200514 {
515 return( ret );
516 }
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200519 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200520 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200521 {
522 return( ret );
523 }
524
525 return( 0 );
526}
527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200529{
530 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200531 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200532 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200533 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200536 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_RSA_C)
539 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200540 {
541 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
542 end = PEM_END_PRIVATE_KEY_RSA;
543 }
544 else
545#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_ECP_C)
547 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200548 {
549 begin = PEM_BEGIN_PRIVATE_KEY_EC;
550 end = PEM_END_PRIVATE_KEY_EC;
551 }
552 else
553#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200557 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200558 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200559 {
560 return( ret );
561 }
562
563 return( 0 );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_PK_WRITE_C */