blob: 943dbca01612cb689df2e670fee0b576c8f25284 [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)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecp.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/ecdsa.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/pem.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020047#endif
48
Andrzej Kurek5fec0862018-11-19 10:07:36 -050049#if defined(MBEDTLS_USE_PSA_CRYPTO)
50#include "psa/crypto.h"
51#endif
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020061/*
62 * RSAPublicKey ::= SEQUENCE {
63 * modulus INTEGER, -- n
64 * publicExponent INTEGER -- e
65 * }
66 */
67static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010068 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020069{
70 int ret;
71 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010072 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020073
Hanno Becker15f81fa2017-08-23 12:38:27 +010074 mbedtls_mpi_init( &T );
75
76 /* Export E */
77 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
78 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
79 goto end_of_export;
80 len += ret;
81
82 /* Export N */
83 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
84 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
85 goto end_of_export;
86 len += ret;
87
88end_of_export:
89
90 mbedtls_mpi_free( &T );
91 if( ret < 0 )
92 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
95 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
96 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020097
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020098 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200103/*
104 * EC public key is an EC point
105 */
106static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100107 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200108{
109 int ret;
110 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
114 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200115 &len, buf, sizeof( buf ) ) ) != 0 )
116 {
117 return( ret );
118 }
119
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200120 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200122
123 *p -= len;
124 memcpy( *p, buf, len );
125
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200126 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200127}
128
129/*
130 * ECParameters ::= CHOICE {
131 * namedCurve OBJECT IDENTIFIER
132 * }
133 */
134static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100135 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200136{
137 int ret;
138 size_t len = 0;
139 const char *oid;
140 size_t oid_len;
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200143 return( ret );
144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200146
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200147 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100152 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200153{
154 int ret;
155 size_t len = 0;
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_RSA_C)
158 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
159 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200160 else
161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_ECP_C)
163 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
164 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200165 else
166#endif
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500167#if defined(MBEDTLS_USE_PSA_CRYPTO)
168 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_OPAQUE )
169 {
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500170 size_t buffer_size;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500171 psa_key_handle_t* key_slot = (psa_key_handle_t*) key->pk_ctx;
Andrzej Kurek158c3d12018-11-19 18:09:59 -0500172
173 if ( *p < start )
174 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
175
Andrzej Kurekb7f3ac62018-11-20 03:03:28 -0500176 buffer_size = (size_t)( *p - start );
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500177 if ( psa_export_public_key( *key_slot, start, buffer_size, &len )
178 != PSA_SUCCESS )
179 {
Andrzej Kurek4b114072018-11-19 18:04:01 -0500180 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500181 }
182 else
183 {
Hanno Becker4fb8db22019-02-01 09:57:20 +0000184 *p -= len;
185 memmove( *p, start, len );
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500186 }
187 }
188 else
189#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200191
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200192 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200193}
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200196{
197 int ret;
198 unsigned char *c;
199 size_t len = 0, par_len = 0, oid_len;
200 const char *oid;
201
202 c = buf + size;
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200205
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500206 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_OPAQUE )
207 {
208 return( (int) len );
209 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200210 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200212
213 /*
214 * SubjectPublicKeyInfo ::= SEQUENCE {
215 * algorithm AlgorithmIdentifier,
216 * subjectPublicKey BIT STRING }
217 */
218 *--c = 0;
219 len += 1;
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
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_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200225 &oid, &oid_len ) ) != 0 )
226 {
227 return( ret );
228 }
229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_ECP_C)
231 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200232 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200234 }
235#endif
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200238 par_len ) );
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
241 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
242 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200243
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200244 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200245}
246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200248{
249 int ret;
250 unsigned char *c = buf + size;
251 size_t len = 0;
252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_RSA_C)
254 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200255 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100256 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200258
Hanno Becker15f81fa2017-08-23 12:38:27 +0100259 /*
260 * Export the parameters one after another to avoid simultaneous copies.
261 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200262
Hanno Becker15f81fa2017-08-23 12:38:27 +0100263 mbedtls_mpi_init( &T );
264
265 /* Export QP */
266 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
267 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
268 goto end_of_export;
269 len += ret;
270
271 /* Export DQ */
272 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
273 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
274 goto end_of_export;
275 len += ret;
276
277 /* Export DP */
278 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
279 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
280 goto end_of_export;
281 len += ret;
282
283 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100284 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
285 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100286 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
287 goto end_of_export;
288 len += ret;
289
290 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100291 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
292 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100293 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
294 goto end_of_export;
295 len += ret;
296
297 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100298 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
299 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100300 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
301 goto end_of_export;
302 len += ret;
303
304 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100305 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
306 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100307 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
308 goto end_of_export;
309 len += ret;
310
311 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100312 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
313 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100314 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
315 goto end_of_export;
316 len += ret;
317
318 end_of_export:
319
320 mbedtls_mpi_free( &T );
321 if( ret < 0 )
322 return( ret );
323
324 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100326 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
327 buf, MBEDTLS_ASN1_CONSTRUCTED |
328 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200329 }
330 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#endif /* MBEDTLS_RSA_C */
332#if defined(MBEDTLS_ECP_C)
333 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200336 size_t pub_len = 0, par_len = 0;
337
338 /*
339 * RFC 5915, or SEC1 Appendix C.4
340 *
341 * ECPrivateKey ::= SEQUENCE {
342 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
343 * privateKey OCTET STRING,
344 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
345 * publicKey [1] BIT STRING OPTIONAL
346 * }
347 */
348
349 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200351
352 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200354 *--c = 0;
355 pub_len += 1;
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
358 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
361 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
362 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200363 len += pub_len;
364
365 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
369 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
370 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200371 len += par_len;
372
373 /* privateKey: write as MPI then fix tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
375 *c = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200376
377 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
381 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
382 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200383 }
384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_ECP_C */
386 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200387
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200388 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200389}
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200392
393#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
394#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
395
396#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
397#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
398#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
399#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
400
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200401/*
402 * Max sizes of key per types. Shown as tag + len (+ content).
403 */
404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200406/*
407 * RSA public keys:
408 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
409 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
410 * + 1 + 1 + 9 (rsa oid)
411 * + 1 + 1 (params null)
412 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
413 * RSAPublicKey ::= SEQUENCE { 1 + 3
414 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
415 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
416 * }
417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200419
420/*
421 * RSA private keys:
422 * RSAPrivateKey ::= SEQUENCE { 1 + 3
423 * version Version, 1 + 1 + 1
424 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
425 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
426 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
427 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
428 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
429 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
430 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
431 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
432 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
433 * }
434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
436 MBEDTLS_MPI_MAX_SIZE % 2
437#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200438 + 5 * MPI_MAX_SIZE_2
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200441
442#define RSA_PUB_DER_MAX_BYTES 0
443#define RSA_PRV_DER_MAX_BYTES 0
444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200448/*
449 * EC public keys:
450 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
451 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
452 * + 1 + 1 + 7 (ec oid)
453 * + 1 + 1 + 9 (namedCurve oid)
454 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
455 * + 1 (point format) [1]
456 * + 2 * ECP_MAX (coords) [1]
457 * }
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200460
461/*
462 * EC private keys:
463 * ECPrivateKey ::= SEQUENCE { 1 + 2
464 * version INTEGER , 1 + 1 + 1
465 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
466 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
467 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
468 * }
469 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200473
474#define ECP_PUB_DER_MAX_BYTES 0
475#define ECP_PRV_DER_MAX_BYTES 0
476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200478
479#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
480 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
481#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
482 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200485{
486 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200487 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200488 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200491 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200492 {
493 return( ret );
494 }
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200497 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200498 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200499 {
500 return( ret );
501 }
502
503 return( 0 );
504}
505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200507{
508 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200509 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200510 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200511 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200514 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_RSA_C)
517 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200518 {
519 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
520 end = PEM_END_PRIVATE_KEY_RSA;
521 }
522 else
523#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524#if defined(MBEDTLS_ECP_C)
525 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200526 {
527 begin = PEM_BEGIN_PRIVATE_KEY_EC;
528 end = PEM_END_PRIVATE_KEY_EC;
529 }
530 else
531#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200535 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200536 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200537 {
538 return( ret );
539 }
540
541 return( 0 );
542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_PK_WRITE_C */