blob: c8d92300dfa0bd369821639f00f27e84e539c89c [file] [log] [blame]
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001/*
2 * Public Key layer for writing key files and structures
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk.h"
31#include "mbedtls/asn1write.h"
32#include "mbedtls/oid.h"
Gilles Peskinee97dc602018-12-19 00:51:38 +010033#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020041#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/ecp.h"
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020043#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020050#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020054#else
55#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020056#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020058#endif
59
Gilles Peskinee97dc602018-12-19 00:51:38 +010060/* Parameter validation macros based on platform_util.h */
61#define PK_VALIDATE_RET( cond ) \
62 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
63#define PK_VALIDATE( cond ) \
64 MBEDTLS_INTERNAL_VALIDATE( cond )
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020067/*
68 * RSAPublicKey ::= SEQUENCE {
69 * modulus INTEGER, -- n
70 * publicExponent INTEGER -- e
71 * }
72 */
73static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010074 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020075{
76 int ret;
77 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010078 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020079
Hanno Becker15f81fa2017-08-23 12:38:27 +010080 mbedtls_mpi_init( &T );
81
82 /* Export E */
83 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
84 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
85 goto end_of_export;
86 len += ret;
87
88 /* Export N */
89 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
90 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
91 goto end_of_export;
92 len += ret;
93
94end_of_export:
95
96 mbedtls_mpi_free( &T );
97 if( ret < 0 )
98 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020099
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
101 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
102 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200104 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107
Hanno Becker28332a52019-08-21 16:19:55 +0100108#if defined(MBEDTLS_USE_TINYCRYPT)
109static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
110 mbedtls_pk_context const *key )
111{
112 size_t const len = 1 + 2 * NUM_ECC_BYTES;
113 mbedtls_uecc_keypair const * const uecc = mbedtls_pk_uecc( *key );
114
115 if( *p < start || (size_t)( *p - start ) < len )
116 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
117
118 *p -= len;
119 (*p)[0] = 0x04;
120 memcpy( *p + 1, uecc->public_key, 2 * NUM_ECC_BYTES );
121
122 return( (int) len );
123}
124
Manuel Pégourié-Gonnardfa4cbe02019-09-10 12:20:43 +0200125/*
126 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
127 */
Hanno Becker28332a52019-08-21 16:19:55 +0100128static int pk_write_ec_privkey( unsigned char **p, unsigned char *start,
129 mbedtls_pk_context const *key )
130{
131 mbedtls_uecc_keypair const * const uecc = mbedtls_pk_uecc( *key );
132 return( mbedtls_asn1_write_octet_string(
133 p, start,
134 uecc->private_key,
135 NUM_ECC_BYTES ) );
136}
137
138/*
139 * ECParameters ::= CHOICE {
140 * namedCurve OBJECT IDENTIFIER
141 * }
142 */
143static int pk_write_ec_param( unsigned char **p, unsigned char *start,
144 mbedtls_pk_context const *key )
145{
146 int ret;
147 size_t len = 0;
148 const char *oid;
149 size_t oid_len;
150 ((void) key);
151
152 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( MBEDTLS_UECC_DP_SECP256R1,
153 &oid, &oid_len ) ) != 0 )
154 return( ret );
155
156 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
157
158 return( (int) len );
159}
160#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200162/*
163 * EC public key is an EC point
164 */
165static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker64a81b02019-08-21 16:08:17 +0100166 mbedtls_pk_context const *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200167{
168 int ret;
169 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Hanno Becker64a81b02019-08-21 16:08:17 +0100171 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
174 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200175 &len, buf, sizeof( buf ) ) ) != 0 )
176 {
177 return( ret );
178 }
179
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200180 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182
183 *p -= len;
184 memcpy( *p, buf, len );
185
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200186 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200187}
188
Manuel Pégourié-Gonnardfa4cbe02019-09-10 12:20:43 +0200189/*
190 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
191 */
Hanno Becker64a81b02019-08-21 16:08:17 +0100192static int pk_write_ec_privkey( unsigned char **p, unsigned char *start,
193 mbedtls_pk_context const *key )
194{
Manuel Pégourié-Gonnardfa4cbe02019-09-10 12:20:43 +0200195 int ret;
Hanno Becker64a81b02019-08-21 16:08:17 +0100196 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
Manuel Pégourié-Gonnardfa4cbe02019-09-10 12:20:43 +0200197 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
198 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
199
200 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
201 if( ret != 0 )
202 goto exit;
203 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
204
205exit:
206 mbedtls_platform_zeroize( tmp, byte_length );
207 return( ret );
Hanno Becker64a81b02019-08-21 16:08:17 +0100208}
209
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200210/*
211 * ECParameters ::= CHOICE {
212 * namedCurve OBJECT IDENTIFIER
213 * }
214 */
215static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker64a81b02019-08-21 16:08:17 +0100216 mbedtls_pk_context const *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200217{
218 int ret;
219 size_t len = 0;
220 const char *oid;
221 size_t oid_len;
Hanno Becker64a81b02019-08-21 16:08:17 +0100222 mbedtls_ecp_keypair const * const ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200225 return( ret );
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200228
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200229 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200230}
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* MBEDTLS_ECP_C */
Hanno Becker28332a52019-08-21 16:19:55 +0100233#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100236 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200237{
238 int ret;
239 size_t len = 0;
240
Gilles Peskinee97dc602018-12-19 00:51:38 +0100241 PK_VALIDATE_RET( p != NULL );
242 PK_VALIDATE_RET( *p != NULL );
243 PK_VALIDATE_RET( start != NULL );
244 PK_VALIDATE_RET( key != NULL );
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246#if defined(MBEDTLS_RSA_C)
247 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
248 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200249 else
250#endif
Hanno Becker88889c62019-08-23 12:01:45 +0100251#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Hanno Becker64a81b02019-08-21 16:08:17 +0100253 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200254 else
Hanno Becker88889c62019-08-23 12:01:45 +0100255#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200257
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200258 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200259}
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200262{
263 int ret;
264 unsigned char *c;
265 size_t len = 0, par_len = 0, oid_len;
266 const char *oid;
267
Gilles Peskinee97dc602018-12-19 00:51:38 +0100268 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100269 if( size == 0 )
270 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
271 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100272
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200273 c = buf + size;
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200276
277 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200279
280 /*
281 * SubjectPublicKeyInfo ::= SEQUENCE {
282 * algorithm AlgorithmIdentifier,
283 * subjectPublicKey BIT STRING }
284 */
285 *--c = 0;
286 len += 1;
287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
289 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200292 &oid, &oid_len ) ) != 0 )
293 {
294 return( ret );
295 }
296
Hanno Becker88889c62019-08-23 12:01:45 +0100297#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200299 {
Hanno Becker64a81b02019-08-21 16:08:17 +0100300 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200301 }
Hanno Becker88889c62019-08-23 12:01:45 +0100302#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200305 par_len ) );
306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
308 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
309 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200310
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200311 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200312}
313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200315{
316 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100317 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200318 size_t len = 0;
319
Gilles Peskinee97dc602018-12-19 00:51:38 +0100320 PK_VALIDATE_RET( key != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +0100321 if( size == 0 )
322 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
323 PK_VALIDATE_RET( buf != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100324
325 c = buf + size;
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_RSA_C)
328 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200329 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100330 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200332
Hanno Becker15f81fa2017-08-23 12:38:27 +0100333 /*
334 * Export the parameters one after another to avoid simultaneous copies.
335 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200336
Hanno Becker15f81fa2017-08-23 12:38:27 +0100337 mbedtls_mpi_init( &T );
338
339 /* Export QP */
340 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
341 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
342 goto end_of_export;
343 len += ret;
344
345 /* Export DQ */
346 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
347 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
348 goto end_of_export;
349 len += ret;
350
351 /* Export DP */
352 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
353 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
354 goto end_of_export;
355 len += ret;
356
357 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100358 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
359 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100360 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
361 goto end_of_export;
362 len += ret;
363
364 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100365 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
366 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100367 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
368 goto end_of_export;
369 len += ret;
370
371 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100372 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
373 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100374 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
375 goto end_of_export;
376 len += ret;
377
378 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100379 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
380 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100381 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
382 goto end_of_export;
383 len += ret;
384
385 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100386 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
387 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100388 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
389 goto end_of_export;
390 len += ret;
391
392 end_of_export:
393
394 mbedtls_mpi_free( &T );
395 if( ret < 0 )
396 return( ret );
397
398 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100400 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
401 buf, MBEDTLS_ASN1_CONSTRUCTED |
402 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200403 }
404 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#endif /* MBEDTLS_RSA_C */
Hanno Becker88889c62019-08-23 12:01:45 +0100406#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200408 {
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200409 size_t pub_len = 0, par_len = 0;
410
411 /*
412 * RFC 5915, or SEC1 Appendix C.4
413 *
414 * ECPrivateKey ::= SEQUENCE {
415 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
416 * privateKey OCTET STRING,
417 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
418 * publicKey [1] BIT STRING OPTIONAL
419 * }
420 */
421
422 /* publicKey */
Hanno Becker64a81b02019-08-21 16:08:17 +0100423 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200424
425 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200427 *--c = 0;
428 pub_len += 1;
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
431 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
434 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
435 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200436 len += pub_len;
437
438 /* parameters */
Hanno Becker64a81b02019-08-21 16:08:17 +0100439 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
442 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
443 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200444 len += par_len;
445
Manuel Pégourié-Gonnardfa4cbe02019-09-10 12:20:43 +0200446 /* privateKey */
Hanno Becker64a81b02019-08-21 16:08:17 +0100447 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_privkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200448
449 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
453 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
454 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200455 }
456 else
Hanno Becker88889c62019-08-23 12:01:45 +0100457#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200459
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200460 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200461}
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200464
465#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
466#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
467
468#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
469#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
470#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
471#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
472
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200473/*
474 * Max sizes of key per types. Shown as tag + len (+ content).
475 */
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200478/*
479 * RSA public keys:
480 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
481 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
482 * + 1 + 1 + 9 (rsa oid)
483 * + 1 + 1 (params null)
484 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
485 * RSAPublicKey ::= SEQUENCE { 1 + 3
486 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
487 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
488 * }
489 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200491
492/*
493 * RSA private keys:
494 * RSAPrivateKey ::= SEQUENCE { 1 + 3
495 * version Version, 1 + 1 + 1
496 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
497 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
498 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
499 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
500 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
501 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
502 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
503 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
504 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
505 * }
506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
508 MBEDTLS_MPI_MAX_SIZE % 2
509#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200510 + 5 * MPI_MAX_SIZE_2
511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200513
514#define RSA_PUB_DER_MAX_BYTES 0
515#define RSA_PRV_DER_MAX_BYTES 0
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200520/*
521 * EC public keys:
522 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
523 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
524 * + 1 + 1 + 7 (ec oid)
525 * + 1 + 1 + 9 (namedCurve oid)
526 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
527 * + 1 (point format) [1]
528 * + 2 * ECP_MAX (coords) [1]
529 * }
530 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200532
533/*
534 * EC private keys:
535 * ECPrivateKey ::= SEQUENCE { 1 + 2
536 * version INTEGER , 1 + 1 + 1
537 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
538 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
539 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
540 * }
541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200545
546#define ECP_PUB_DER_MAX_BYTES 0
547#define ECP_PRV_DER_MAX_BYTES 0
548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200550
551#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
552 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
553#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
554 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200557{
558 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200559 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200560 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200561
Gilles Peskinee97dc602018-12-19 00:51:38 +0100562 PK_VALIDATE_RET( key != NULL );
563 PK_VALIDATE_RET( buf != NULL || size == 0 );
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200566 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200567 {
568 return( ret );
569 }
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200572 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200573 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200574 {
575 return( ret );
576 }
577
578 return( 0 );
579}
580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200582{
583 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200584 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200585 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200586 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200587
Gilles Peskinee97dc602018-12-19 00:51:38 +0100588 PK_VALIDATE_RET( key != NULL );
589 PK_VALIDATE_RET( buf != NULL || size == 0 );
590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200592 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594#if defined(MBEDTLS_RSA_C)
595 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200596 {
597 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
598 end = PEM_END_PRIVATE_KEY_RSA;
599 }
600 else
Hanno Becker88889c62019-08-23 12:01:45 +0100601#endif /* MBEDTLS_RSA_C */
602#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200604 {
605 begin = PEM_BEGIN_PRIVATE_KEY_EC;
606 end = PEM_END_PRIVATE_KEY_EC;
607 }
608 else
Hanno Becker88889c62019-08-23 12:01:45 +0100609#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200613 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200614 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200615 {
616 return( ret );
617 }
618
619 return( 0 );
620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#endif /* MBEDTLS_PK_WRITE_C */