blob: d34714b342eb8401787821d448b1a022fd95484d [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 Kurek4b114072018-11-19 18:04:01 -0500171 psa_key_slot_t* key_slot = (psa_key_slot_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 {
184 memmove( *p - len, start, len );
185 }
186 }
187 else
188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200190
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200191 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200192}
193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200195{
196 int ret;
197 unsigned char *c;
198 size_t len = 0, par_len = 0, oid_len;
199 const char *oid;
200
201 c = buf + size;
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200204
Andrzej Kurek5fec0862018-11-19 10:07:36 -0500205 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_OPAQUE )
206 {
207 return( (int) len );
208 }
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200209 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200211
212 /*
213 * SubjectPublicKeyInfo ::= SEQUENCE {
214 * algorithm AlgorithmIdentifier,
215 * subjectPublicKey BIT STRING }
216 */
217 *--c = 0;
218 len += 1;
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
221 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200224 &oid, &oid_len ) ) != 0 )
225 {
226 return( ret );
227 }
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#if defined(MBEDTLS_ECP_C)
230 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200233 }
234#endif
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200237 par_len ) );
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
240 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
241 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200242
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200243 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200244}
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200247{
248 int ret;
249 unsigned char *c = buf + size;
250 size_t len = 0;
251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_RSA_C)
253 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200254 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100255 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200257
Hanno Becker15f81fa2017-08-23 12:38:27 +0100258 /*
259 * Export the parameters one after another to avoid simultaneous copies.
260 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200261
Hanno Becker15f81fa2017-08-23 12:38:27 +0100262 mbedtls_mpi_init( &T );
263
264 /* Export QP */
265 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
266 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
267 goto end_of_export;
268 len += ret;
269
270 /* Export DQ */
271 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
272 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
273 goto end_of_export;
274 len += ret;
275
276 /* Export DP */
277 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
278 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
279 goto end_of_export;
280 len += ret;
281
282 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100283 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
284 &T, 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 P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100290 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
291 NULL, NULL, 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 D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100297 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
298 NULL, &T, NULL ) ) != 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 E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100304 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
305 NULL, NULL, &T ) ) != 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 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100311 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
312 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100313 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
314 goto end_of_export;
315 len += ret;
316
317 end_of_export:
318
319 mbedtls_mpi_free( &T );
320 if( ret < 0 )
321 return( ret );
322
323 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100325 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
326 buf, MBEDTLS_ASN1_CONSTRUCTED |
327 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200328 }
329 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330#endif /* MBEDTLS_RSA_C */
331#if defined(MBEDTLS_ECP_C)
332 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200335 size_t pub_len = 0, par_len = 0;
336
337 /*
338 * RFC 5915, or SEC1 Appendix C.4
339 *
340 * ECPrivateKey ::= SEQUENCE {
341 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
342 * privateKey OCTET STRING,
343 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
344 * publicKey [1] BIT STRING OPTIONAL
345 * }
346 */
347
348 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200350
351 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200353 *--c = 0;
354 pub_len += 1;
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
357 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
360 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
361 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200362 len += pub_len;
363
364 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
368 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
369 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200370 len += par_len;
371
372 /* privateKey: write as MPI then fix tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
374 *c = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200375
376 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
380 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
381 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200382 }
383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_ECP_C */
385 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200386
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200387 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200388}
389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200391
392#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
393#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
394
395#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
396#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
397#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
398#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
399
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200400/*
401 * Max sizes of key per types. Shown as tag + len (+ content).
402 */
403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200405/*
406 * RSA public keys:
407 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
408 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
409 * + 1 + 1 + 9 (rsa oid)
410 * + 1 + 1 (params null)
411 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
412 * RSAPublicKey ::= SEQUENCE { 1 + 3
413 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
414 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
415 * }
416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200418
419/*
420 * RSA private keys:
421 * RSAPrivateKey ::= SEQUENCE { 1 + 3
422 * version Version, 1 + 1 + 1
423 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
424 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
425 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
426 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
427 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
428 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
429 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
430 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
431 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
432 * }
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
435 MBEDTLS_MPI_MAX_SIZE % 2
436#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200437 + 5 * MPI_MAX_SIZE_2
438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200440
441#define RSA_PUB_DER_MAX_BYTES 0
442#define RSA_PRV_DER_MAX_BYTES 0
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200447/*
448 * EC public keys:
449 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
450 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
451 * + 1 + 1 + 7 (ec oid)
452 * + 1 + 1 + 9 (namedCurve oid)
453 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
454 * + 1 (point format) [1]
455 * + 2 * ECP_MAX (coords) [1]
456 * }
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200459
460/*
461 * EC private keys:
462 * ECPrivateKey ::= SEQUENCE { 1 + 2
463 * version INTEGER , 1 + 1 + 1
464 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
465 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
466 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
467 * }
468 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200472
473#define ECP_PUB_DER_MAX_BYTES 0
474#define ECP_PRV_DER_MAX_BYTES 0
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200477
478#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
479 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
480#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
481 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200484{
485 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200486 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200487 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200490 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200491 {
492 return( ret );
493 }
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200496 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200497 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200498 {
499 return( ret );
500 }
501
502 return( 0 );
503}
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200506{
507 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200508 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200509 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200510 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200513 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#if defined(MBEDTLS_RSA_C)
516 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200517 {
518 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
519 end = PEM_END_PRIVATE_KEY_RSA;
520 }
521 else
522#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_ECP_C)
524 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200525 {
526 begin = PEM_BEGIN_PRIVATE_KEY_EC;
527 end = PEM_END_PRIVATE_KEY_EC;
528 }
529 else
530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200534 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200535 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200536 {
537 return( ret );
538 }
539
540 return( 0 );
541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#endif /* MBEDTLS_PK_WRITE_C */