blob: 51d0c56f1845d0d0ea433523993ec085cbcbdffc [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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020048#endif
49
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020052#else
53#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020054#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020056#endif
57
Gilles Peskinee97dc602018-12-19 00:51:38 +010058/* Parameter validation macros based on platform_util.h */
59#define PK_VALIDATE_RET( cond ) \
60 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
61#define PK_VALIDATE( cond ) \
62 MBEDTLS_INTERNAL_VALIDATE( cond )
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020065/*
66 * RSAPublicKey ::= SEQUENCE {
67 * modulus INTEGER, -- n
68 * publicExponent INTEGER -- e
69 * }
70 */
71static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010072 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020073{
74 int ret;
75 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010076 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020077
Hanno Becker15f81fa2017-08-23 12:38:27 +010078 mbedtls_mpi_init( &T );
79
80 /* Export E */
81 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
82 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
83 goto end_of_export;
84 len += ret;
85
86 /* Export N */
87 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
88 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
89 goto end_of_export;
90 len += ret;
91
92end_of_export:
93
94 mbedtls_mpi_free( &T );
95 if( ret < 0 )
96 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
99 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
100 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200101
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200102 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200107/*
108 * EC public key is an EC point
109 */
110static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100111 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200112{
113 int ret;
114 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
118 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200119 &len, buf, sizeof( buf ) ) ) != 0 )
120 {
121 return( ret );
122 }
123
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200124 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200126
127 *p -= len;
128 memcpy( *p, buf, len );
129
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200130 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200131}
132
133/*
134 * ECParameters ::= CHOICE {
135 * namedCurve OBJECT IDENTIFIER
136 * }
137 */
138static int pk_write_ec_param( 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;
143 const char *oid;
144 size_t oid_len;
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200147 return( ret );
148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200150
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200151 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200152}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100156 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200157{
158 int ret;
159 size_t len = 0;
160
Gilles Peskinee97dc602018-12-19 00:51:38 +0100161 PK_VALIDATE_RET( p != NULL );
162 PK_VALIDATE_RET( *p != NULL );
163 PK_VALIDATE_RET( start != NULL );
164 PK_VALIDATE_RET( key != NULL );
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_RSA_C)
167 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
168 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200169 else
170#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#if defined(MBEDTLS_ECP_C)
172 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
173 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200174 else
175#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
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}
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182{
183 int ret;
184 unsigned char *c;
185 size_t len = 0, par_len = 0, oid_len;
186 const char *oid;
187
Gilles Peskinee97dc602018-12-19 00:51:38 +0100188 PK_VALIDATE_RET( key != NULL );
189 PK_VALIDATE_RET( buf != NULL || size == 0 );
190
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200191 c = buf + size;
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200194
195 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200197
198 /*
199 * SubjectPublicKeyInfo ::= SEQUENCE {
200 * algorithm AlgorithmIdentifier,
201 * subjectPublicKey BIT STRING }
202 */
203 *--c = 0;
204 len += 1;
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200210 &oid, &oid_len ) ) != 0 )
211 {
212 return( ret );
213 }
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_ECP_C)
216 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200219 }
220#endif
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200223 par_len ) );
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
227 MBEDTLS_ASN1_SEQUENCE ) );
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}
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200233{
234 int ret;
Gilles Peskinee97dc602018-12-19 00:51:38 +0100235 unsigned char *c;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200236 size_t len = 0;
237
Gilles Peskinee97dc602018-12-19 00:51:38 +0100238 PK_VALIDATE_RET( key != NULL );
239 PK_VALIDATE_RET( buf != NULL || size == 0 );
240
241 c = buf + size;
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#if defined(MBEDTLS_RSA_C)
244 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200245 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100246 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200248
Hanno Becker15f81fa2017-08-23 12:38:27 +0100249 /*
250 * Export the parameters one after another to avoid simultaneous copies.
251 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200252
Hanno Becker15f81fa2017-08-23 12:38:27 +0100253 mbedtls_mpi_init( &T );
254
255 /* Export QP */
256 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
257 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
258 goto end_of_export;
259 len += ret;
260
261 /* Export DQ */
262 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
263 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
264 goto end_of_export;
265 len += ret;
266
267 /* Export DP */
268 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
269 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
270 goto end_of_export;
271 len += ret;
272
273 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100274 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
275 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100276 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
277 goto end_of_export;
278 len += ret;
279
280 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100281 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
282 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100283 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
284 goto end_of_export;
285 len += ret;
286
287 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100288 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
289 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100290 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
291 goto end_of_export;
292 len += ret;
293
294 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100295 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
296 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100297 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
298 goto end_of_export;
299 len += ret;
300
301 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100302 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
303 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100304 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
305 goto end_of_export;
306 len += ret;
307
308 end_of_export:
309
310 mbedtls_mpi_free( &T );
311 if( ret < 0 )
312 return( ret );
313
314 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100316 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
317 buf, MBEDTLS_ASN1_CONSTRUCTED |
318 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200319 }
320 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#endif /* MBEDTLS_RSA_C */
322#if defined(MBEDTLS_ECP_C)
323 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200326 size_t pub_len = 0, par_len = 0;
327
328 /*
329 * RFC 5915, or SEC1 Appendix C.4
330 *
331 * ECPrivateKey ::= SEQUENCE {
332 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
333 * privateKey OCTET STRING,
334 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
335 * publicKey [1] BIT STRING OPTIONAL
336 * }
337 */
338
339 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200341
342 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200344 *--c = 0;
345 pub_len += 1;
346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
348 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
351 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
352 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200353 len += pub_len;
354
355 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
359 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
360 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200361 len += par_len;
362
363 /* privateKey: write as MPI then fix tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
365 *c = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200366
367 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
371 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
372 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200373 }
374 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#endif /* MBEDTLS_ECP_C */
376 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200377
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200378 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200379}
380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200382
383#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
384#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
385
386#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
387#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
388#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
389#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
390
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200391/*
392 * Max sizes of key per types. Shown as tag + len (+ content).
393 */
394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200396/*
397 * RSA public keys:
398 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
399 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
400 * + 1 + 1 + 9 (rsa oid)
401 * + 1 + 1 (params null)
402 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
403 * RSAPublicKey ::= SEQUENCE { 1 + 3
404 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
405 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
406 * }
407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200409
410/*
411 * RSA private keys:
412 * RSAPrivateKey ::= SEQUENCE { 1 + 3
413 * version Version, 1 + 1 + 1
414 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
415 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
416 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
417 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
418 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
419 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
420 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
421 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
422 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
423 * }
424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
426 MBEDTLS_MPI_MAX_SIZE % 2
427#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200428 + 5 * MPI_MAX_SIZE_2
429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200431
432#define RSA_PUB_DER_MAX_BYTES 0
433#define RSA_PRV_DER_MAX_BYTES 0
434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200438/*
439 * EC public keys:
440 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
441 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
442 * + 1 + 1 + 7 (ec oid)
443 * + 1 + 1 + 9 (namedCurve oid)
444 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
445 * + 1 (point format) [1]
446 * + 2 * ECP_MAX (coords) [1]
447 * }
448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200450
451/*
452 * EC private keys:
453 * ECPrivateKey ::= SEQUENCE { 1 + 2
454 * version INTEGER , 1 + 1 + 1
455 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
456 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
457 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
458 * }
459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200463
464#define ECP_PUB_DER_MAX_BYTES 0
465#define ECP_PRV_DER_MAX_BYTES 0
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200468
469#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
470 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
471#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
472 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200475{
476 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200477 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200478 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200479
Gilles Peskinee97dc602018-12-19 00:51:38 +0100480 PK_VALIDATE_RET( key != NULL );
481 PK_VALIDATE_RET( buf != NULL || size == 0 );
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200484 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200485 {
486 return( ret );
487 }
488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200490 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200491 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200492 {
493 return( ret );
494 }
495
496 return( 0 );
497}
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200500{
501 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200502 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200503 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200504 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200505
Gilles Peskinee97dc602018-12-19 00:51:38 +0100506 PK_VALIDATE_RET( key != NULL );
507 PK_VALIDATE_RET( buf != NULL || size == 0 );
508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200510 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#if defined(MBEDTLS_RSA_C)
513 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200514 {
515 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
516 end = PEM_END_PRIVATE_KEY_RSA;
517 }
518 else
519#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_ECP_C)
521 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200522 {
523 begin = PEM_BEGIN_PRIVATE_KEY_EC;
524 end = PEM_END_PRIVATE_KEY_EC;
525 }
526 else
527#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200531 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200532 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200533 {
534 return( ret );
535 }
536
537 return( 0 );
538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541#endif /* MBEDTLS_PK_WRITE_C */