blob: e1076251f00f97e680e07f92f3c74877118cdf37 [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ú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 * **********
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"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020060
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <string.h>
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_ECP_C)
Gilles Peskine98add4f2018-08-11 00:48:44 +020067#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/ecp.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020075#endif
76
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020079#else
80#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020081#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020083#endif
84
Gilles Peskine98add4f2018-08-11 00:48:44 +020085#if defined(MBEDTLS_ECP_C)
86/* Implementation that should never be optimized out by the compiler */
87static void mbedtls_zeroize( void *v, size_t n ) {
88 volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
89}
90#endif /* MBEDTLS_ECP_C */
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020093/*
94 * RSAPublicKey ::= SEQUENCE {
95 * modulus INTEGER, -- n
96 * publicExponent INTEGER -- e
97 * }
98 */
99static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100100 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200101{
102 int ret;
103 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +0100104 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105
Hanno Becker15f81fa2017-08-23 12:38:27 +0100106 mbedtls_mpi_init( &T );
107
108 /* Export E */
109 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
110 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
111 goto end_of_export;
112 len += ret;
113
114 /* Export N */
115 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
116 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
117 goto end_of_export;
118 len += ret;
119
120end_of_export:
121
122 mbedtls_mpi_free( &T );
123 if( ret < 0 )
124 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
127 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
128 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200129
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200130 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200135/*
136 * EC public key is an EC point
137 */
138static int pk_write_ec_pubkey( 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;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
146 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200147 &len, buf, sizeof( buf ) ) ) != 0 )
148 {
149 return( ret );
150 }
151
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200152 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200154
155 *p -= len;
156 memcpy( *p, buf, len );
157
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200158 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200159}
160
161/*
162 * ECParameters ::= CHOICE {
163 * namedCurve OBJECT IDENTIFIER
164 * }
165 */
166static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100167 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200168{
169 int ret;
170 size_t len = 0;
171 const char *oid;
172 size_t oid_len;
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200175 return( ret );
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200178
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200179 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200180}
Gilles Peskine98add4f2018-08-11 00:48:44 +0200181
182/*
183 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
184 */
185static int pk_write_ec_private( unsigned char **p, unsigned char *start,
186 mbedtls_ecp_keypair *ec )
187{
188 int ret;
189 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
190 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
191
192 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
193 if( ret != 0 )
194 goto exit;
195 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
196
197exit:
198 mbedtls_zeroize( tmp, byte_length );
199 return( ret );
200}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100204 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200205{
206 int ret;
207 size_t len = 0;
208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#if defined(MBEDTLS_RSA_C)
210 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
211 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200212 else
213#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_ECP_C)
215 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
216 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200217 else
218#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200220
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200221 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200222}
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200225{
226 int ret;
227 unsigned char *c;
228 size_t len = 0, par_len = 0, oid_len;
229 const char *oid;
230
231 c = buf + size;
232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234
235 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200237
238 /*
239 * SubjectPublicKeyInfo ::= SEQUENCE {
240 * algorithm AlgorithmIdentifier,
241 * subjectPublicKey BIT STRING }
242 */
243 *--c = 0;
244 len += 1;
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
247 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200250 &oid, &oid_len ) ) != 0 )
251 {
252 return( ret );
253 }
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_ECP_C)
256 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200257 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200259 }
260#endif
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200263 par_len ) );
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
266 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
267 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200268
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200269 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200273{
274 int ret;
275 unsigned char *c = buf + size;
276 size_t len = 0;
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#if defined(MBEDTLS_RSA_C)
279 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200280 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100281 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200283
Hanno Becker15f81fa2017-08-23 12:38:27 +0100284 /*
285 * Export the parameters one after another to avoid simultaneous copies.
286 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200287
Hanno Becker15f81fa2017-08-23 12:38:27 +0100288 mbedtls_mpi_init( &T );
289
290 /* Export QP */
291 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
292 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
293 goto end_of_export;
294 len += ret;
295
296 /* Export DQ */
297 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
298 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
299 goto end_of_export;
300 len += ret;
301
302 /* Export DP */
303 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
304 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
305 goto end_of_export;
306 len += ret;
307
308 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100309 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
310 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100311 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
312 goto end_of_export;
313 len += ret;
314
315 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100316 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
317 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100318 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
319 goto end_of_export;
320 len += ret;
321
322 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100323 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
324 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100325 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
326 goto end_of_export;
327 len += ret;
328
329 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100330 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
331 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100332 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
333 goto end_of_export;
334 len += ret;
335
336 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100337 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
338 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100339 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
340 goto end_of_export;
341 len += ret;
342
343 end_of_export:
344
345 mbedtls_mpi_free( &T );
346 if( ret < 0 )
347 return( ret );
348
349 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100351 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
352 buf, MBEDTLS_ASN1_CONSTRUCTED |
353 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200354 }
355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* MBEDTLS_RSA_C */
357#if defined(MBEDTLS_ECP_C)
358 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200361 size_t pub_len = 0, par_len = 0;
362
363 /*
364 * RFC 5915, or SEC1 Appendix C.4
365 *
366 * ECPrivateKey ::= SEQUENCE {
367 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
368 * privateKey OCTET STRING,
369 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
370 * publicKey [1] BIT STRING OPTIONAL
371 * }
372 */
373
374 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200376
377 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200379 *--c = 0;
380 pub_len += 1;
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
383 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
386 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
387 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200388 len += pub_len;
389
390 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
394 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
395 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200396 len += par_len;
397
Gilles Peskine98add4f2018-08-11 00:48:44 +0200398 /* privateKey */
399 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200400
401 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
405 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
406 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200407 }
408 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#endif /* MBEDTLS_ECP_C */
410 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200411
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200412 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200413}
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200416
417#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
418#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
419
420#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
421#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
422#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
423#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
424
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200425/*
426 * Max sizes of key per types. Shown as tag + len (+ content).
427 */
428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200430/*
431 * RSA public keys:
432 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
433 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
434 * + 1 + 1 + 9 (rsa oid)
435 * + 1 + 1 (params null)
436 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
437 * RSAPublicKey ::= SEQUENCE { 1 + 3
438 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
439 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
440 * }
441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200443
444/*
445 * RSA private keys:
446 * RSAPrivateKey ::= SEQUENCE { 1 + 3
447 * version Version, 1 + 1 + 1
448 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
449 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
450 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
451 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
452 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
453 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
454 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
455 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
456 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
457 * }
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
460 MBEDTLS_MPI_MAX_SIZE % 2
461#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200462 + 5 * MPI_MAX_SIZE_2
463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200465
466#define RSA_PUB_DER_MAX_BYTES 0
467#define RSA_PRV_DER_MAX_BYTES 0
468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200472/*
473 * EC public keys:
474 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
475 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
476 * + 1 + 1 + 7 (ec oid)
477 * + 1 + 1 + 9 (namedCurve oid)
478 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
479 * + 1 (point format) [1]
480 * + 2 * ECP_MAX (coords) [1]
481 * }
482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200484
485/*
486 * EC private keys:
487 * ECPrivateKey ::= SEQUENCE { 1 + 2
488 * version INTEGER , 1 + 1 + 1
489 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
490 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
491 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
492 * }
493 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200497
498#define ECP_PUB_DER_MAX_BYTES 0
499#define ECP_PRV_DER_MAX_BYTES 0
500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200502
503#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
504 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
505#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
506 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200509{
510 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200511 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200512 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200515 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200516 {
517 return( ret );
518 }
519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200521 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200522 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200523 {
524 return( ret );
525 }
526
527 return( 0 );
528}
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200531{
532 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200533 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200534 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200535 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200538 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#if defined(MBEDTLS_RSA_C)
541 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200542 {
543 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
544 end = PEM_END_PRIVATE_KEY_RSA;
545 }
546 else
547#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_ECP_C)
549 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200550 {
551 begin = PEM_BEGIN_PRIVATE_KEY_EC;
552 end = PEM_END_PRIVATE_KEY_EC;
553 }
554 else
555#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200559 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200560 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200561 {
562 return( ret );
563 }
564
565 return( 0 );
566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569#endif /* MBEDTLS_PK_WRITE_C */