2 * Public Key layer for writing key files and structures
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PK_WRITE_C)
30 #include "mbedtls/pk.h"
31 #include "mbedtls/asn1write.h"
32 #include "mbedtls/oid.h"
33 #include "mbedtls/platform_util.h"
37 #if defined(MBEDTLS_RSA_C)
38 #include "mbedtls/rsa.h"
40 #if defined(MBEDTLS_ECP_C)
41 #include "mbedtls/ecp.h"
43 #if defined(MBEDTLS_ECDSA_C)
44 #include "mbedtls/ecdsa.h"
46 #if defined(MBEDTLS_PEM_WRITE_C)
47 #include "mbedtls/pem.h"
50 #if defined(MBEDTLS_USE_PSA_CRYPTO)
51 #include "psa/crypto.h"
52 #include "mbedtls/psa_util.h"
54 #if defined(MBEDTLS_PLATFORM_C)
55 #include "mbedtls/platform.h"
58 #define mbedtls_calloc calloc
59 #define mbedtls_free free
62 /* Parameter validation macros based on platform_util.h */
63 #define PK_VALIDATE_RET( cond ) \
64 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
65 #define PK_VALIDATE( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE( cond )
68 #if defined(MBEDTLS_RSA_C)
70 * RSAPublicKey ::= SEQUENCE {
71 * modulus INTEGER, -- n
72 * publicExponent INTEGER -- e
75 static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
76 mbedtls_rsa_context *rsa )
82 mbedtls_mpi_init( &T );
85 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
86 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
91 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
92 ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
98 mbedtls_mpi_free( &T );
102 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
103 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
104 MBEDTLS_ASN1_SEQUENCE ) );
108 #endif /* MBEDTLS_RSA_C */
110 #if defined(MBEDTLS_ECP_C)
112 * EC public key is an EC point
114 static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
115 mbedtls_ecp_keypair *ec )
119 unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
121 if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
122 MBEDTLS_ECP_PF_UNCOMPRESSED,
123 &len, buf, sizeof( buf ) ) ) != 0 )
128 if( *p < start || (size_t)( *p - start ) < len )
129 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
132 memcpy( *p, buf, len );
138 * ECParameters ::= CHOICE {
139 * namedCurve OBJECT IDENTIFIER
142 static int pk_write_ec_param( unsigned char **p, unsigned char *start,
143 mbedtls_ecp_keypair *ec )
150 if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
153 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
157 #endif /* MBEDTLS_ECP_C */
159 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
160 const mbedtls_pk_context *key )
165 PK_VALIDATE_RET( p != NULL );
166 PK_VALIDATE_RET( *p != NULL );
167 PK_VALIDATE_RET( start != NULL );
168 PK_VALIDATE_RET( key != NULL );
170 #if defined(MBEDTLS_RSA_C)
171 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
172 MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
175 #if defined(MBEDTLS_ECP_C)
176 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
177 MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
180 #if defined(MBEDTLS_USE_PSA_CRYPTO)
181 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_OPAQUE )
184 psa_key_handle_t* key_slot = (psa_key_handle_t*) key->pk_ctx;
187 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
189 buffer_size = (size_t)( *p - start );
190 if ( psa_export_public_key( *key_slot, start, buffer_size, &len )
193 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
198 memmove( *p, start, len );
202 #endif /* MBEDTLS_USE_PSA_CRYPTO */
203 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
208 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
212 size_t len = 0, par_len = 0, oid_len;
213 mbedtls_pk_type_t pk_type;
216 PK_VALIDATE_RET( key != NULL );
218 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
219 PK_VALIDATE_RET( buf != NULL );
223 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
226 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
229 * SubjectPublicKeyInfo ::= SEQUENCE {
230 * algorithm AlgorithmIdentifier,
231 * subjectPublicKey BIT STRING }
236 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
237 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
239 pk_type = mbedtls_pk_get_type( key );
240 #if defined(MBEDTLS_ECP_C)
241 if( pk_type == MBEDTLS_PK_ECKEY )
243 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
246 #if defined(MBEDTLS_USE_PSA_CRYPTO)
247 if( pk_type == MBEDTLS_PK_OPAQUE )
250 psa_key_type_t key_type;
251 psa_key_handle_t handle;
252 psa_ecc_curve_t curve;
254 handle = *((psa_key_handle_t*) key->pk_ctx );
256 status = psa_get_key_information( handle, &key_type,
257 NULL /* bitsize not needed */ );
258 if( status != PSA_SUCCESS )
259 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
261 curve = PSA_KEY_TYPE_GET_CURVE( key_type );
263 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
265 ret = mbedtls_psa_get_ecc_oid_from_id( curve, &oid, &oid_len );
267 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
269 /* Write EC algorithm parameters; that's akin
270 * to pk_write_ec_param() above. */
271 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_oid( &c, buf,
274 /* The rest of the function works as for legacy EC contexts. */
275 pk_type = MBEDTLS_PK_ECKEY;
277 #endif /* MBEDTLS_USE_PSA_CRYPTO */
279 if( ( ret = mbedtls_oid_get_oid_by_pk_alg( pk_type, &oid,
285 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
288 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
289 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
290 MBEDTLS_ASN1_SEQUENCE ) );
295 int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
301 PK_VALIDATE_RET( key != NULL );
303 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
304 PK_VALIDATE_RET( buf != NULL );
308 #if defined(MBEDTLS_RSA_C)
309 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
311 mbedtls_mpi T; /* Temporary holding the exported parameters */
312 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
315 * Export the parameters one after another to avoid simultaneous copies.
318 mbedtls_mpi_init( &T );
321 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
322 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
327 if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
328 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
333 if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
334 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
339 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
340 &T, NULL, NULL ) ) != 0 ||
341 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
346 if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
347 NULL, NULL, NULL ) ) != 0 ||
348 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
353 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
354 NULL, &T, NULL ) ) != 0 ||
355 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
360 if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
361 NULL, NULL, &T ) ) != 0 ||
362 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
367 if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
368 NULL, NULL, NULL ) ) != 0 ||
369 ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
375 mbedtls_mpi_free( &T );
379 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
380 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
381 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
382 buf, MBEDTLS_ASN1_CONSTRUCTED |
383 MBEDTLS_ASN1_SEQUENCE ) );
386 #endif /* MBEDTLS_RSA_C */
387 #if defined(MBEDTLS_ECP_C)
388 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
390 mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
391 size_t pub_len = 0, par_len = 0;
394 * RFC 5915, or SEC1 Appendix C.4
396 * ECPrivateKey ::= SEQUENCE {
397 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
398 * privateKey OCTET STRING,
399 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
400 * publicKey [1] BIT STRING OPTIONAL
405 MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
408 return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
412 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
413 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
415 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
416 MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
417 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
421 MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
423 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
424 MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
425 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
428 /* privateKey: write as MPI then fix tag */
429 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
430 *c = MBEDTLS_ASN1_OCTET_STRING;
433 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
435 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
436 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
437 MBEDTLS_ASN1_SEQUENCE ) );
440 #endif /* MBEDTLS_ECP_C */
441 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
446 #if defined(MBEDTLS_PEM_WRITE_C)
448 #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
449 #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
451 #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
452 #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
453 #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
454 #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
457 * Max sizes of key per types. Shown as tag + len (+ content).
460 #if defined(MBEDTLS_RSA_C)
463 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
464 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
465 * + 1 + 1 + 9 (rsa oid)
466 * + 1 + 1 (params null)
467 * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
468 * RSAPublicKey ::= SEQUENCE { 1 + 3
469 * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
470 * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
473 #define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
477 * RSAPrivateKey ::= SEQUENCE { 1 + 3
478 * version Version, 1 + 1 + 1
479 * modulus INTEGER, 1 + 3 + MPI_MAX + 1
480 * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
481 * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
482 * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
483 * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
484 * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
485 * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
486 * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
487 * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
490 #define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
491 MBEDTLS_MPI_MAX_SIZE % 2
492 #define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
495 #else /* MBEDTLS_RSA_C */
497 #define RSA_PUB_DER_MAX_BYTES 0
498 #define RSA_PRV_DER_MAX_BYTES 0
500 #endif /* MBEDTLS_RSA_C */
502 #if defined(MBEDTLS_ECP_C)
505 * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
506 * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
507 * + 1 + 1 + 7 (ec oid)
508 * + 1 + 1 + 9 (namedCurve oid)
509 * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
510 * + 1 (point format) [1]
511 * + 2 * ECP_MAX (coords) [1]
514 #define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
518 * ECPrivateKey ::= SEQUENCE { 1 + 2
519 * version INTEGER , 1 + 1 + 1
520 * privateKey OCTET STRING, 1 + 1 + ECP_MAX
521 * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
522 * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
525 #define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
527 #else /* MBEDTLS_ECP_C */
529 #define ECP_PUB_DER_MAX_BYTES 0
530 #define ECP_PRV_DER_MAX_BYTES 0
532 #endif /* MBEDTLS_ECP_C */
534 #define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
535 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
536 #define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
537 RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
539 int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
542 unsigned char output_buf[PUB_DER_MAX_BYTES];
545 PK_VALIDATE_RET( key != NULL );
546 PK_VALIDATE_RET( buf != NULL || size == 0 );
548 if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
549 sizeof(output_buf) ) ) < 0 )
554 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
555 output_buf + sizeof(output_buf) - ret,
556 ret, buf, size, &olen ) ) != 0 )
564 int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
567 unsigned char output_buf[PRV_DER_MAX_BYTES];
568 const char *begin, *end;
571 PK_VALIDATE_RET( key != NULL );
572 PK_VALIDATE_RET( buf != NULL || size == 0 );
574 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
577 #if defined(MBEDTLS_RSA_C)
578 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
580 begin = PEM_BEGIN_PRIVATE_KEY_RSA;
581 end = PEM_END_PRIVATE_KEY_RSA;
585 #if defined(MBEDTLS_ECP_C)
586 if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
588 begin = PEM_BEGIN_PRIVATE_KEY_EC;
589 end = PEM_END_PRIVATE_KEY_EC;
593 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
595 if( ( ret = mbedtls_pem_write_buffer( begin, end,
596 output_buf + sizeof(output_buf) - ret,
597 ret, buf, size, &olen ) ) != 0 )
604 #endif /* MBEDTLS_PEM_WRITE_C */
606 #endif /* MBEDTLS_PK_WRITE_C */