blob: a770dfb93ecec04cc219869b4ee75c57c6be33fc [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útia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
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 * **********
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"
Gilles Peskinee97dc602018-12-19 00:51:38 +010058#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <string.h>
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_ECP_C)
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020066#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/ecp.h"
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020068#include "mbedtls/platform_util.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 Peskinee97dc602018-12-19 00:51:38 +010085/* Parameter validation macros based on platform_util.h */
86#define PK_VALIDATE_RET( cond ) \
87 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
88#define PK_VALIDATE( cond ) \
89 MBEDTLS_INTERNAL_VALIDATE( cond )
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020092/*
93 * RSAPublicKey ::= SEQUENCE {
94 * modulus INTEGER, -- n
95 * publicExponent INTEGER -- e
96 * }
97 */
98static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010099 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200100{
101 int ret;
102 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +0100103 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200104
Hanno Becker15f81fa2017-08-23 12:38:27 +0100105 mbedtls_mpi_init( &T );
106
107 /* Export E */
108 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
109 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
110 goto end_of_export;
111 len += ret;
112
113 /* Export N */
114 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
115 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
116 goto end_of_export;
117 len += ret;
118
119end_of_export:
120
121 mbedtls_mpi_free( &T );
122 if( ret < 0 )
123 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
126 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
127 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200128
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200129 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200130}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200134/*
135 * EC public key is an EC point
136 */
137static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100138 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200139{
140 int ret;
141 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
145 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200146 &len, buf, sizeof( buf ) ) ) != 0 )
147 {
148 return( ret );
149 }
150
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200151 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200153
154 *p -= len;
155 memcpy( *p, buf, len );
156
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200157 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200158}
159
160/*
161 * ECParameters ::= CHOICE {
162 * namedCurve OBJECT IDENTIFIER
163 * }
164 */
165static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100166 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200167{
168 int ret;
169 size_t len = 0;
170 const char *oid;
171 size_t oid_len;
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200174 return( ret );
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200177
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200178 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200179}
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200180
181/*
182 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
183 */
184static int pk_write_ec_private( unsigned char **p, unsigned char *start,
185 mbedtls_ecp_keypair *ec )
186{
187 int ret;
188 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
189 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
190
191 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
192 if( ret != 0 )
193 goto exit;
194 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
195
196exit:
197 mbedtls_platform_zeroize( tmp, byte_length );
198 return( ret );
199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100203 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200204{
205 int ret;
206 size_t len = 0;
207
Gilles Peskinee97dc602018-12-19 00:51:38 +0100208 PK_VALIDATE_RET( p != NULL );
209 PK_VALIDATE_RET( *p != NULL );
210 PK_VALIDATE_RET( start != NULL );
211 PK_VALIDATE_RET( key != NULL );
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_RSA_C)
214 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
215 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200216 else
217#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_ECP_C)
219 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
220 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200221 else
222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200224
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200225 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200226}
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200229{
230 int ret;
231 unsigned char *c;
232 size_t len = 0, par_len = 0, oid_len;
233 const char *oid;
234
Gilles Peskinee97dc602018-12-19 00:51:38 +0100235 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100236 if( size == 0 )
237 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
238 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100239
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200240 c = buf + size;
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200243
244 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200246
247 /*
248 * SubjectPublicKeyInfo ::= SEQUENCE {
249 * algorithm AlgorithmIdentifier,
250 * subjectPublicKey BIT STRING }
251 */
252 *--c = 0;
253 len += 1;
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
256 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200259 &oid, &oid_len ) ) != 0 )
260 {
261 return( ret );
262 }
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_ECP_C)
265 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200268 }
269#endif
270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200272 par_len ) );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
275 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
276 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200277
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200278 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200279}
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200282{
283 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100284 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200285 size_t len = 0;
286
Gilles Peskinee97dc602018-12-19 00:51:38 +0100287 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100288 if( size == 0 )
289 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
290 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100291
292 c = buf + size;
293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294#if defined(MBEDTLS_RSA_C)
295 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200296 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100297 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200299
Hanno Becker15f81fa2017-08-23 12:38:27 +0100300 /*
301 * Export the parameters one after another to avoid simultaneous copies.
302 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200303
Hanno Becker15f81fa2017-08-23 12:38:27 +0100304 mbedtls_mpi_init( &T );
305
306 /* Export QP */
307 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
308 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
309 goto end_of_export;
310 len += ret;
311
312 /* Export DQ */
313 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
314 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
315 goto end_of_export;
316 len += ret;
317
318 /* Export DP */
319 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
320 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
321 goto end_of_export;
322 len += ret;
323
324 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100325 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
326 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100327 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
328 goto end_of_export;
329 len += ret;
330
331 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100332 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
333 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100334 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
335 goto end_of_export;
336 len += ret;
337
338 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100339 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
340 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100341 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
342 goto end_of_export;
343 len += ret;
344
345 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100346 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
347 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100348 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
349 goto end_of_export;
350 len += ret;
351
352 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100353 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
354 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100355 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
356 goto end_of_export;
357 len += ret;
358
359 end_of_export:
360
361 mbedtls_mpi_free( &T );
362 if( ret < 0 )
363 return( ret );
364
365 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100367 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
368 buf, MBEDTLS_ASN1_CONSTRUCTED |
369 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200370 }
371 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#endif /* MBEDTLS_RSA_C */
373#if defined(MBEDTLS_ECP_C)
374 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200377 size_t pub_len = 0, par_len = 0;
378
379 /*
380 * RFC 5915, or SEC1 Appendix C.4
381 *
382 * ECPrivateKey ::= SEQUENCE {
383 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
384 * privateKey OCTET STRING,
385 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
386 * publicKey [1] BIT STRING OPTIONAL
387 * }
388 */
389
390 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200392
393 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200395 *--c = 0;
396 pub_len += 1;
397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
399 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
402 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
403 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200404 len += pub_len;
405
406 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
410 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
411 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200412 len += par_len;
413
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200414 /* privateKey */
415 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200416
417 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
421 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
422 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200423 }
424 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#endif /* MBEDTLS_ECP_C */
426 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200427
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200428 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200429}
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200432
433#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
434#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
435
436#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
437#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
438#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
439#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
440
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200441/*
442 * Max sizes of key per types. Shown as tag + len (+ content).
443 */
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200446/*
447 * RSA public keys:
448 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
449 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
450 * + 1 + 1 + 9 (rsa oid)
451 * + 1 + 1 (params null)
452 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
453 * RSAPublicKey ::= SEQUENCE { 1 + 3
454 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
455 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
456 * }
457 */
Daniel Otte9c6cb212021-02-01 14:26:08 +0100458#define RSA_PUB_DER_MAX_BYTES ( 38 + 2 * MBEDTLS_MPI_MAX_SIZE )
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200459
460/*
461 * RSA private keys:
462 * RSAPrivateKey ::= SEQUENCE { 1 + 3
463 * version Version, 1 + 1 + 1
464 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
465 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
466 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
467 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
468 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
469 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
470 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
471 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
472 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
473 * }
474 */
Daniel Otte80a2c2a2021-02-01 14:23:30 +0100475#define MPI_MAX_SIZE_2 ( MBEDTLS_MPI_MAX_SIZE / 2 + \
476 MBEDTLS_MPI_MAX_SIZE % 2 )
Daniel Otte9c6cb212021-02-01 14:26:08 +0100477#define RSA_PRV_DER_MAX_BYTES ( 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
478 + 5 * MPI_MAX_SIZE_2 )
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200481
482#define RSA_PUB_DER_MAX_BYTES 0
483#define RSA_PRV_DER_MAX_BYTES 0
484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200488/*
489 * EC public keys:
490 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
491 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
492 * + 1 + 1 + 7 (ec oid)
493 * + 1 + 1 + 9 (namedCurve oid)
494 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
495 * + 1 (point format) [1]
496 * + 2 * ECP_MAX (coords) [1]
497 * }
498 */
Daniel Otte9c6cb212021-02-01 14:26:08 +0100499#define ECP_PUB_DER_MAX_BYTES ( 30 + 2 * MBEDTLS_ECP_MAX_BYTES )
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200500
501/*
502 * EC private keys:
503 * ECPrivateKey ::= SEQUENCE { 1 + 2
504 * version INTEGER , 1 + 1 + 1
505 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
506 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
507 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
508 * }
509 */
Daniel Otte9c6cb212021-02-01 14:26:08 +0100510#define ECP_PRV_DER_MAX_BYTES ( 29 + 3 * MBEDTLS_ECP_MAX_BYTES )
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200513
514#define ECP_PUB_DER_MAX_BYTES 0
515#define ECP_PRV_DER_MAX_BYTES 0
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200518
Daniel Otte9c6cb212021-02-01 14:26:08 +0100519#define PUB_DER_MAX_BYTES ( RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
520 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES )
521#define PRV_DER_MAX_BYTES ( RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
522 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES )
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524int mbedtls_pk_write_pubkey_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[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200528 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200529
Gilles Peskinee97dc602018-12-19 00:51:38 +0100530 PK_VALIDATE_RET( key != NULL );
531 PK_VALIDATE_RET( buf != NULL || size == 0 );
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200534 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200535 {
536 return( ret );
537 }
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200540 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200541 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200542 {
543 return( ret );
544 }
545
546 return( 0 );
547}
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200550{
551 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200552 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200553 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200554 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200555
Gilles Peskinee97dc602018-12-19 00:51:38 +0100556 PK_VALIDATE_RET( key != NULL );
557 PK_VALIDATE_RET( buf != NULL || size == 0 );
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200560 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562#if defined(MBEDTLS_RSA_C)
563 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200564 {
565 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
566 end = PEM_END_PRIVATE_KEY_RSA;
567 }
568 else
569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#if defined(MBEDTLS_ECP_C)
571 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200572 {
573 begin = PEM_BEGIN_PRIVATE_KEY_EC;
574 end = PEM_END_PRIVATE_KEY_EC;
575 }
576 else
577#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200581 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200582 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200583 {
584 return( ret );
585 }
586
587 return( 0 );
588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_PK_WRITE_C */