blob: 8eabd889b5590c52ac9a0bef5a520ddae227b0d5 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/platform.h"
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020051#else
52#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020053#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define mbedtls_free free
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_RSA_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020058/*
59 * RSAPublicKey ::= SEQUENCE {
60 * modulus INTEGER, -- n
61 * publicExponent INTEGER -- e
62 * }
63 */
64static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +010065 mbedtls_rsa_context *rsa )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020066{
67 int ret;
68 size_t len = 0;
Hanno Becker15f81fa2017-08-23 12:38:27 +010069 mbedtls_mpi T;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020070
Hanno Becker15f81fa2017-08-23 12:38:27 +010071 mbedtls_mpi_init( &T );
72
73 /* Export E */
74 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
75 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
76 goto end_of_export;
77 len += ret;
78
79 /* Export N */
80 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
81 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
82 goto end_of_export;
83 len += ret;
84
85end_of_export:
86
87 mbedtls_mpi_free( &T );
88 if( ret < 0 )
89 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
92 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
93 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020094
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020095 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#endif /* MBEDTLS_RSA_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099#if defined(MBEDTLS_ECP_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200100/*
101 * EC public key is an EC point
102 */
103static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100104 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200105{
106 int ret;
107 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
111 MBEDTLS_ECP_PF_UNCOMPRESSED,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200112 &len, buf, sizeof( buf ) ) ) != 0 )
113 {
114 return( ret );
115 }
116
Manuel Pégourié-Gonnard4dc9b392015-10-21 12:23:09 +0200117 if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200119
120 *p -= len;
121 memcpy( *p, buf, len );
122
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200123 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200124}
125
126/*
127 * ECParameters ::= CHOICE {
128 * namedCurve OBJECT IDENTIFIER
129 * }
130 */
131static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100132 mbedtls_ecp_keypair *ec )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200133{
134 int ret;
135 size_t len = 0;
136 const char *oid;
137 size_t oid_len;
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200140 return( ret );
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200143
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200144 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200145}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146#endif /* MBEDTLS_ECP_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker8fd55482017-08-23 14:07:48 +0100149 const mbedtls_pk_context *key )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200150{
151 int ret;
152 size_t len = 0;
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#if defined(MBEDTLS_RSA_C)
155 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
156 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200157 else
158#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_ECP_C)
160 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
161 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200162 else
163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200165
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200166 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200170{
171 int ret;
172 unsigned char *c;
173 size_t len = 0, par_len = 0, oid_len;
174 const char *oid;
175
176 c = buf + size;
177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200179
180 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200182
183 /*
184 * SubjectPublicKeyInfo ::= SEQUENCE {
185 * algorithm AlgorithmIdentifier,
186 * subjectPublicKey BIT STRING }
187 */
188 *--c = 0;
189 len += 1;
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
192 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200195 &oid, &oid_len ) ) != 0 )
196 {
197 return( ret );
198 }
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200#if defined(MBEDTLS_ECP_C)
201 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200204 }
205#endif
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200208 par_len ) );
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
211 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
212 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200213
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200214 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200215}
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200218{
219 int ret;
220 unsigned char *c = buf + size;
221 size_t len = 0;
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_RSA_C)
224 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200225 {
Hanno Becker15f81fa2017-08-23 12:38:27 +0100226 mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200228
Hanno Becker15f81fa2017-08-23 12:38:27 +0100229 /*
230 * Export the parameters one after another to avoid simultaneous copies.
231 */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200232
Hanno Becker15f81fa2017-08-23 12:38:27 +0100233 mbedtls_mpi_init( &T );
234
235 /* Export QP */
236 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
237 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
238 goto end_of_export;
239 len += ret;
240
241 /* Export DQ */
242 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
243 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
244 goto end_of_export;
245 len += ret;
246
247 /* Export DP */
248 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
249 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
250 goto end_of_export;
251 len += ret;
252
253 /* Export Q */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100254 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
255 &T, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100256 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
257 goto end_of_export;
258 len += ret;
259
260 /* Export P */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100261 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
262 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100263 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
264 goto end_of_export;
265 len += ret;
266
267 /* Export D */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100268 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
269 NULL, &T, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100270 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
271 goto end_of_export;
272 len += ret;
273
274 /* Export E */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100275 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
276 NULL, NULL, &T ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100277 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
278 goto end_of_export;
279 len += ret;
280
281 /* Export N */
Hanno Beckerd71dc152017-08-23 06:32:42 +0100282 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
283 NULL, NULL, NULL ) ) != 0 ||
Hanno Becker15f81fa2017-08-23 12:38:27 +0100284 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
285 goto end_of_export;
286 len += ret;
287
288 end_of_export:
289
290 mbedtls_mpi_free( &T );
291 if( ret < 0 )
292 return( ret );
293
294 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Beckerd71dc152017-08-23 06:32:42 +0100296 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
297 buf, MBEDTLS_ASN1_CONSTRUCTED |
298 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200299 }
300 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_RSA_C */
302#if defined(MBEDTLS_ECP_C)
303 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200306 size_t pub_len = 0, par_len = 0;
307
308 /*
309 * RFC 5915, or SEC1 Appendix C.4
310 *
311 * ECPrivateKey ::= SEQUENCE {
312 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
313 * privateKey OCTET STRING,
314 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
315 * publicKey [1] BIT STRING OPTIONAL
316 * }
317 */
318
319 /* publicKey */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200321
322 if( c - buf < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200324 *--c = 0;
325 pub_len += 1;
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
328 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
331 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
332 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200333 len += pub_len;
334
335 /* parameters */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
339 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
340 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200341 len += par_len;
342
343 /* privateKey: write as MPI then fix tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
345 *c = MBEDTLS_ASN1_OCTET_STRING;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200346
347 /* version */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
351 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
352 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200353 }
354 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_ECP_C */
356 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200357
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200358 return( (int) len );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200359}
360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361#if defined(MBEDTLS_PEM_WRITE_C)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200362
363#define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
364#define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
365
366#define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
367#define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
368#define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
369#define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
370
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200371/*
372 * Max sizes of key per types. Shown as tag + len (+ content).
373 */
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200376/*
377 * RSA public keys:
378 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
379 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
380 * + 1 + 1 + 9 (rsa oid)
381 * + 1 + 1 (params null)
382 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
383 * RSAPublicKey ::= SEQUENCE { 1 + 3
384 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
385 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
386 * }
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200389
390/*
391 * RSA private keys:
392 * RSAPrivateKey ::= SEQUENCE { 1 + 3
393 * version Version, 1 + 1 + 1
394 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
395 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
396 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
397 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
398 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
399 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
400 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
401 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
402 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
403 * }
404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405#define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
406 MBEDTLS_MPI_MAX_SIZE % 2
407#define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200408 + 5 * MPI_MAX_SIZE_2
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#else /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200411
412#define RSA_PUB_DER_MAX_BYTES 0
413#define RSA_PRV_DER_MAX_BYTES 0
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200418/*
419 * EC public keys:
420 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
421 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
422 * + 1 + 1 + 7 (ec oid)
423 * + 1 + 1 + 9 (namedCurve oid)
424 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
425 * + 1 (point format) [1]
426 * + 2 * ECP_MAX (coords) [1]
427 * }
428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429#define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200430
431/*
432 * EC private keys:
433 * ECPrivateKey ::= SEQUENCE { 1 + 2
434 * version INTEGER , 1 + 1 + 1
435 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
436 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
437 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
438 * }
439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#else /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200443
444#define ECP_PUB_DER_MAX_BYTES 0
445#define ECP_PRV_DER_MAX_BYTES 0
446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200448
449#define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
450 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
451#define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
452 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200455{
456 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200457 unsigned char output_buf[PUB_DER_MAX_BYTES];
Paul Bakker77e23fb2013-09-15 20:03:26 +0200458 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200461 sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200462 {
463 return( ret );
464 }
465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200467 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200468 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200469 {
470 return( ret );
471 }
472
473 return( 0 );
474}
475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200477{
478 int ret;
Manuel Pégourié-Gonnard192253a2014-07-21 16:37:15 +0200479 unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakkerfcc17212013-10-11 09:36:52 +0200480 const char *begin, *end;
Paul Bakker77e23fb2013-09-15 20:03:26 +0200481 size_t olen = 0;
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200484 return( ret );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_RSA_C)
487 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200488 {
489 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
490 end = PEM_END_PRIVATE_KEY_RSA;
491 }
492 else
493#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_ECP_C)
495 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200496 {
497 begin = PEM_BEGIN_PRIVATE_KEY_EC;
498 end = PEM_END_PRIVATE_KEY_EC;
499 }
500 else
501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( ( ret = mbedtls_pem_write_buffer( begin, end,
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200505 output_buf + sizeof(output_buf) - ret,
Paul Bakker77e23fb2013-09-15 20:03:26 +0200506 ret, buf, size, &olen ) ) != 0 )
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200507 {
508 return( ret );
509 }
510
511 return( 0 );
512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +0200514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_PK_WRITE_C */