blob: 4ae38af0af1e1d10274d2b72a67616de24cddf59 [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"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/rsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECP_C)
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020040#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Gilles Peskinea7cfdad2018-08-11 00:48:44 +020042#include "mbedtls/platform_util.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020053#else
54#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020057#endif
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020060/*
61 * RSAPublicKey ::= SEQUENCE {
62 * modulus INTEGER, -- n
63 * publicExponent INTEGER -- e
64 * }
65 */
66static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010067 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020068{
69 int ret;
70 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010071 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020072
Hanno Becker15f81fa2017-08-23 12:38:27 +010073 mbedtls_mpi_init( &T );
74
75 /* Export E */
76 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
77 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
78 goto end_of_export;
79 len += ret;
80
81 /* Export N */
82 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
83 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
84 goto end_of_export;
85 len += ret;
86
87end_of_export:
88
89 mbedtls_mpi_free( &T );
90 if( ret < 0 )
91 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
94 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
95 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020096
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200102/*
103 * EC public key is an EC point
104 */
105static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100106 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107{
108 int ret;
109 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
113 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200114 &len, buf, sizeof( buf ) ) ) != 0 )
115 {
116 return( ret );
117 }
118
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200119 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200121
122 *p -= len;
123 memcpy( *p, buf, len );
124
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200125 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200126}
127
128/*
129 * ECParameters ::= CHOICE {
130 * namedCurve OBJECT IDENTIFIER
131 * }
132 */
133static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100134 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200135{
136 int ret;
137 size_t len = 0;
138 const char *oid;
139 size_t oid_len;
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200142 return( ret );
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200145
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200146 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200147}
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200148
149/*
150 * privateKey OCTET STRING -- always of length ceil(log2(n)/8)
151 */
152static int pk_write_ec_private( unsigned char **p, unsigned char *start,
153 mbedtls_ecp_keypair *ec )
154{
155 int ret;
156 size_t byte_length = ( ec->grp.pbits + 7 ) / 8;
157 unsigned char tmp[MBEDTLS_ECP_MAX_BYTES];
158
159 ret = mbedtls_mpi_write_binary( &ec->d, tmp, byte_length );
160 if( ret != 0 )
161 goto exit;
162 ret = mbedtls_asn1_write_octet_string( p, start, tmp, byte_length );
163
164exit:
165 mbedtls_platform_zeroize( tmp, byte_length );
166 return( ret );
167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100171 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200172{
173 int ret;
174 size_t len = 0;
175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_RSA_C)
177 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
178 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200179 else
180#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_ECP_C)
182 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
183 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200184 else
185#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200187
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200188 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200189}
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200192{
193 int ret;
194 unsigned char *c;
195 size_t len = 0, par_len = 0, oid_len;
196 const char *oid;
197
198 c = buf + size;
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200201
202 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200204
205 /*
206 * SubjectPublicKeyInfo ::= SEQUENCE {
207 * algorithm AlgorithmIdentifier,
208 * subjectPublicKey BIT STRING }
209 */
210 *--c = 0;
211 len += 1;
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
214 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200217 &oid, &oid_len ) ) != 0 )
218 {
219 return( ret );
220 }
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_ECP_C)
223 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200226 }
227#endif
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200230 par_len ) );
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
233 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
234 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200235
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200236 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200237}
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200240{
241 int ret;
242 unsigned char *c = buf + size;
243 size_t len = 0;
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_RSA_C)
246 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200247 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100248 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200250
Hanno Becker15f81fa2017-08-23 12:38:27 +0100251 /*
252 * Export the parameters one after another to avoid simultaneous copies.
253 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200254
Hanno Becker15f81fa2017-08-23 12:38:27 +0100255 mbedtls_mpi_init( &T );
256
257 /* Export QP */
258 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
259 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
260 goto end_of_export;
261 len += ret;
262
263 /* Export DQ */
264 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
265 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
266 goto end_of_export;
267 len += ret;
268
269 /* Export DP */
270 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
271 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
272 goto end_of_export;
273 len += ret;
274
275 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100276 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
277 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100278 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
279 goto end_of_export;
280 len += ret;
281
282 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100283 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
284 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100285 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
286 goto end_of_export;
287 len += ret;
288
289 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100290 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
291 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100292 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
293 goto end_of_export;
294 len += ret;
295
296 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100297 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
298 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100299 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
300 goto end_of_export;
301 len += ret;
302
303 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100304 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
305 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100306 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
307 goto end_of_export;
308 len += ret;
309
310 end_of_export:
311
312 mbedtls_mpi_free( &T );
313 if( ret < 0 )
314 return( ret );
315
316 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100318 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
319 buf, MBEDTLS_ASN1_CONSTRUCTED |
320 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200321 }
322 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#endif /* MBEDTLS_RSA_C */
324#if defined(MBEDTLS_ECP_C)
325 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200328 size_t pub_len = 0, par_len = 0;
329
330 /*
331 * RFC 5915, or SEC1 Appendix C.4
332 *
333 * ECPrivateKey ::= SEQUENCE {
334 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
335 * privateKey OCTET STRING,
336 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
337 * publicKey [1] BIT STRING OPTIONAL
338 * }
339 */
340
341 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200343
344 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200346 *--c = 0;
347 pub_len += 1;
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
350 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
353 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
354 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200355 len += pub_len;
356
357 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
361 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
362 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200363 len += par_len;
364
Gilles Peskinea7cfdad2018-08-11 00:48:44 +0200365 /* privateKey */
366 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_private( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200367
368 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
372 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
373 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200374 }
375 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#endif /* MBEDTLS_ECP_C */
377 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200378
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200379 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200380}
381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200383
384#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
385#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
386
387#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
388#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
389#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
390#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
391
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200392/*
393 * Max sizes of key per types. Shown as tag + len (+ content).
394 */
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200397/*
398 * RSA public keys:
399 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
400 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
401 * + 1 + 1 + 9 (rsa oid)
402 * + 1 + 1 (params null)
403 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
404 * RSAPublicKey ::= SEQUENCE { 1 + 3
405 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
406 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
407 * }
408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200410
411/*
412 * RSA private keys:
413 * RSAPrivateKey ::= SEQUENCE { 1 + 3
414 * version Version, 1 + 1 + 1
415 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
416 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
417 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
418 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
419 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
420 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
421 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
422 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
423 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
424 * }
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
427 MBEDTLS_MPI_MAX_SIZE % 2
428#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200429 + 5 * MPI_MAX_SIZE_2
430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200432
433#define RSA_PUB_DER_MAX_BYTES 0
434#define RSA_PRV_DER_MAX_BYTES 0
435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200439/*
440 * EC public keys:
441 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
442 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
443 * + 1 + 1 + 7 (ec oid)
444 * + 1 + 1 + 9 (namedCurve oid)
445 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
446 * + 1 (point format) [1]
447 * + 2 * ECP_MAX (coords) [1]
448 * }
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200451
452/*
453 * EC private keys:
454 * ECPrivateKey ::= SEQUENCE { 1 + 2
455 * version INTEGER , 1 + 1 + 1
456 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
457 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
458 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
459 * }
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200464
465#define ECP_PUB_DER_MAX_BYTES 0
466#define ECP_PRV_DER_MAX_BYTES 0
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200469
470#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
471 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
472#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
473 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200476{
477 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200478 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200479 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200482 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200483 {
484 return( ret );
485 }
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200488 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200489 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200490 {
491 return( ret );
492 }
493
494 return( 0 );
495}
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200498{
499 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200500 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200501 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200502 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200505 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_RSA_C)
508 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200509 {
510 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
511 end = PEM_END_PRIVATE_KEY_RSA;
512 }
513 else
514#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_ECP_C)
516 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200517 {
518 begin = PEM_BEGIN_PRIVATE_KEY_EC;
519 end = PEM_END_PRIVATE_KEY_EC;
520 }
521 else
522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200526 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200527 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200528 {
529 return( ret );
530 }
531
532 return( 0 );
533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_PK_WRITE_C */