Cleanup armsrc/string.c and string.h (#964)
[legacy-proxmark3.git] / common / mbedtls / pkparse.c
bloba687076b07f7be98ac41085e0e940df23e7b7ec5
1 /*
2 * Public Key layer for parsing key files and structures
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
30 #if defined(MBEDTLS_PK_PARSE_C)
32 #include "mbedtls/pk.h"
33 #include "mbedtls/asn1.h"
34 #include "mbedtls/oid.h"
35 #include "mbedtls/platform_util.h"
37 #include <string.h>
39 #if defined(MBEDTLS_RSA_C)
40 #include "mbedtls/rsa.h"
41 #endif
42 #if defined(MBEDTLS_ECP_C)
43 #include "mbedtls/ecp.h"
44 #endif
45 #if defined(MBEDTLS_ECDSA_C)
46 #include "mbedtls/ecdsa.h"
47 #endif
48 #if defined(MBEDTLS_PEM_PARSE_C)
49 #include "mbedtls/pem.h"
50 #endif
51 #if defined(MBEDTLS_PKCS5_C)
52 #include "mbedtls/pkcs5.h"
53 #endif
54 #if defined(MBEDTLS_PKCS12_C)
55 #include "mbedtls/pkcs12.h"
56 #endif
58 #if defined(MBEDTLS_PLATFORM_C)
59 #include "mbedtls/platform.h"
60 #else
61 #include <stdlib.h>
62 #define mbedtls_calloc calloc
63 #define mbedtls_free free
64 #endif
66 #if defined(MBEDTLS_FS_IO)
68 * Load all data from a file into a given buffer.
70 * The file is expected to contain either PEM or DER encoded data.
71 * A terminating null byte is always appended. It is included in the announced
72 * length only if the data looks like it is PEM encoded.
74 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
76 FILE *f;
77 long size;
79 if( ( f = fopen( path, "rb" ) ) == NULL )
80 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
82 fseek( f, 0, SEEK_END );
83 if( ( size = ftell( f ) ) == -1 )
85 fclose( f );
86 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
88 fseek( f, 0, SEEK_SET );
90 *n = (size_t) size;
92 if( *n + 1 == 0 ||
93 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
95 fclose( f );
96 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
99 if( fread( *buf, 1, *n, f ) != *n )
101 fclose( f );
103 mbedtls_platform_zeroize( *buf, *n );
104 mbedtls_free( *buf );
106 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
109 fclose( f );
111 (*buf)[*n] = '\0';
113 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
114 ++*n;
116 return( 0 );
120 * Load and parse a private key
122 int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
123 const char *path, const char *pwd )
125 int ret;
126 size_t n;
127 unsigned char *buf;
129 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
130 return( ret );
132 if( pwd == NULL )
133 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
134 else
135 ret = mbedtls_pk_parse_key( ctx, buf, n,
136 (const unsigned char *) pwd, strlen( pwd ) );
138 mbedtls_platform_zeroize( buf, n );
139 mbedtls_free( buf );
141 return( ret );
145 * Load and parse a public key
147 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
149 int ret;
150 size_t n;
151 unsigned char *buf;
153 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
154 return( ret );
156 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
158 mbedtls_platform_zeroize( buf, n );
159 mbedtls_free( buf );
161 return( ret );
163 #endif /* MBEDTLS_FS_IO */
165 #if defined(MBEDTLS_ECP_C)
166 /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
168 * ECParameters ::= CHOICE {
169 * namedCurve OBJECT IDENTIFIER
170 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
171 * -- implicitCurve NULL
174 static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
175 mbedtls_asn1_buf *params )
177 int ret;
179 if ( end - *p < 1 )
180 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
181 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
183 /* Tag may be either OID or SEQUENCE */
184 params->tag = **p;
185 if( params->tag != MBEDTLS_ASN1_OID
186 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
187 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
188 #endif
191 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
192 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
195 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
197 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
200 params->p = *p;
201 *p += params->len;
203 if( *p != end )
204 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
205 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
207 return( 0 );
210 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
212 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
213 * WARNING: the resulting group should only be used with
214 * pk_group_id_from_specified(), since its base point may not be set correctly
215 * if it was encoded compressed.
217 * SpecifiedECDomain ::= SEQUENCE {
218 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
219 * fieldID FieldID {{FieldTypes}},
220 * curve Curve,
221 * base ECPoint,
222 * order INTEGER,
223 * cofactor INTEGER OPTIONAL,
224 * hash HashAlgorithm OPTIONAL,
225 * ...
228 * We only support prime-field as field type, and ignore hash and cofactor.
230 static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
232 int ret;
233 unsigned char *p = params->p;
234 const unsigned char * const end = params->p + params->len;
235 const unsigned char *end_field, *end_curve;
236 size_t len;
237 int ver;
239 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
240 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
241 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
243 if( ver < 1 || ver > 3 )
244 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
247 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
248 * fieldType FIELD-ID.&id({IOSet}),
249 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
252 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
253 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
254 return( ret );
256 end_field = p + len;
259 * FIELD-ID ::= TYPE-IDENTIFIER
260 * FieldTypes FIELD-ID ::= {
261 * { Prime-p IDENTIFIED BY prime-field } |
262 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
264 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
266 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
267 return( ret );
269 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
270 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
272 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
275 p += len;
277 /* Prime-p ::= INTEGER -- Field of size p. */
278 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
279 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
281 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
283 if( p != end_field )
284 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
285 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
288 * Curve ::= SEQUENCE {
289 * a FieldElement,
290 * b FieldElement,
291 * seed BIT STRING OPTIONAL
292 * -- Shall be present if used in SpecifiedECDomain
293 * -- with version equal to ecdpVer2 or ecdpVer3
296 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
297 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
298 return( ret );
300 end_curve = p + len;
303 * FieldElement ::= OCTET STRING
304 * containing an integer in the case of a prime field
306 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
307 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
309 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
312 p += len;
314 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
315 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
317 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
320 p += len;
322 /* Ignore seed BIT STRING OPTIONAL */
323 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
324 p += len;
326 if( p != end_curve )
327 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
328 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
331 * ECPoint ::= OCTET STRING
333 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
334 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
336 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
337 ( const unsigned char *) p, len ) ) != 0 )
340 * If we can't read the point because it's compressed, cheat by
341 * reading only the X coordinate and the parity bit of Y.
343 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
344 ( p[0] != 0x02 && p[0] != 0x03 ) ||
345 len != mbedtls_mpi_size( &grp->P ) + 1 ||
346 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
347 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
348 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
350 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
354 p += len;
357 * order INTEGER
359 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
360 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
362 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
365 * Allow optional elements by purposefully not enforcing p == end here.
368 return( 0 );
372 * Find the group id associated with an (almost filled) group as generated by
373 * pk_group_from_specified(), or return an error if unknown.
375 static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
377 int ret = 0;
378 mbedtls_ecp_group ref;
379 const mbedtls_ecp_group_id *id;
381 mbedtls_ecp_group_init( &ref );
383 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
385 /* Load the group associated to that id */
386 mbedtls_ecp_group_free( &ref );
387 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
389 /* Compare to the group we were given, starting with easy tests */
390 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
391 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
392 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
393 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
394 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
395 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
396 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
397 /* For Y we may only know the parity bit, so compare only that */
398 mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
400 break;
405 cleanup:
406 mbedtls_ecp_group_free( &ref );
408 *grp_id = *id;
410 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
411 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
413 return( ret );
417 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
419 static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
420 mbedtls_ecp_group_id *grp_id )
422 int ret;
423 mbedtls_ecp_group grp;
425 mbedtls_ecp_group_init( &grp );
427 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
428 goto cleanup;
430 ret = pk_group_id_from_group( &grp, grp_id );
432 cleanup:
433 mbedtls_ecp_group_free( &grp );
435 return( ret );
437 #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
440 * Use EC parameters to initialise an EC group
442 * ECParameters ::= CHOICE {
443 * namedCurve OBJECT IDENTIFIER
444 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
445 * -- implicitCurve NULL
447 static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
449 int ret;
450 mbedtls_ecp_group_id grp_id;
452 if( params->tag == MBEDTLS_ASN1_OID )
454 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
455 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
457 else
459 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
460 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
461 return( ret );
462 #else
463 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
464 #endif
468 * grp may already be initilialized; if so, make sure IDs match
470 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
471 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
473 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
474 return( ret );
476 return( 0 );
480 * EC public key is an EC point
482 * The caller is responsible for clearing the structure upon failure if
483 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
484 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
486 static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
487 mbedtls_ecp_keypair *key )
489 int ret;
491 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
492 (const unsigned char *) *p, end - *p ) ) == 0 )
494 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
498 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
500 *p = (unsigned char *) end;
502 return( ret );
504 #endif /* MBEDTLS_ECP_C */
506 #if defined(MBEDTLS_RSA_C)
508 * RSAPublicKey ::= SEQUENCE {
509 * modulus INTEGER, -- n
510 * publicExponent INTEGER -- e
513 static int pk_get_rsapubkey( unsigned char **p,
514 const unsigned char *end,
515 mbedtls_rsa_context *rsa )
517 int ret;
518 size_t len;
520 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
521 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
522 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
524 if( *p + len != end )
525 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
526 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
528 /* Import N */
529 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
530 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
532 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
533 NULL, 0, NULL, 0 ) ) != 0 )
534 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
536 *p += len;
538 /* Import E */
539 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
540 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
542 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
543 NULL, 0, *p, len ) ) != 0 )
544 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
546 *p += len;
548 if( mbedtls_rsa_complete( rsa ) != 0 ||
549 mbedtls_rsa_check_pubkey( rsa ) != 0 )
551 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
554 if( *p != end )
555 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
556 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
558 return( 0 );
560 #endif /* MBEDTLS_RSA_C */
562 /* Get a PK algorithm identifier
564 * AlgorithmIdentifier ::= SEQUENCE {
565 * algorithm OBJECT IDENTIFIER,
566 * parameters ANY DEFINED BY algorithm OPTIONAL }
568 static int pk_get_pk_alg( unsigned char **p,
569 const unsigned char *end,
570 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
572 int ret;
573 mbedtls_asn1_buf alg_oid;
575 memset( params, 0, sizeof(mbedtls_asn1_buf) );
577 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
578 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
580 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
581 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
584 * No parameters with RSA (only for EC)
586 if( *pk_alg == MBEDTLS_PK_RSA &&
587 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
588 params->len != 0 ) )
590 return( MBEDTLS_ERR_PK_INVALID_ALG );
593 return( 0 );
597 * SubjectPublicKeyInfo ::= SEQUENCE {
598 * algorithm AlgorithmIdentifier,
599 * subjectPublicKey BIT STRING }
601 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
602 mbedtls_pk_context *pk )
604 int ret;
605 size_t len;
606 mbedtls_asn1_buf alg_params;
607 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
608 const mbedtls_pk_info_t *pk_info;
610 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
611 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
613 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
616 end = *p + len;
618 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
619 return( ret );
621 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
622 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
624 if( *p + len != end )
625 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
626 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
628 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
629 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
631 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
632 return( ret );
634 #if defined(MBEDTLS_RSA_C)
635 if( pk_alg == MBEDTLS_PK_RSA )
637 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
638 } else
639 #endif /* MBEDTLS_RSA_C */
640 #if defined(MBEDTLS_ECP_C)
641 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
643 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
644 if( ret == 0 )
645 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
646 } else
647 #endif /* MBEDTLS_ECP_C */
648 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
650 if( ret == 0 && *p != end )
651 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
652 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
654 if( ret != 0 )
655 mbedtls_pk_free( pk );
657 return( ret );
660 #if defined(MBEDTLS_RSA_C)
662 * Parse a PKCS#1 encoded private RSA key
664 static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
665 const unsigned char *key,
666 size_t keylen )
668 int ret, version;
669 size_t len;
670 unsigned char *p, *end;
672 mbedtls_mpi T;
673 mbedtls_mpi_init( &T );
675 p = (unsigned char *) key;
676 end = p + keylen;
679 * This function parses the RSAPrivateKey (PKCS#1)
681 * RSAPrivateKey ::= SEQUENCE {
682 * version Version,
683 * modulus INTEGER, -- n
684 * publicExponent INTEGER, -- e
685 * privateExponent INTEGER, -- d
686 * prime1 INTEGER, -- p
687 * prime2 INTEGER, -- q
688 * exponent1 INTEGER, -- d mod (p-1)
689 * exponent2 INTEGER, -- d mod (q-1)
690 * coefficient INTEGER, -- (inverse of q) mod p
691 * otherPrimeInfos OtherPrimeInfos OPTIONAL
694 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
695 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
697 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
700 end = p + len;
702 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
704 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
707 if( version != 0 )
709 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
712 /* Import N */
713 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
714 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
715 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
716 NULL, 0, NULL, 0 ) ) != 0 )
717 goto cleanup;
718 p += len;
720 /* Import E */
721 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
722 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
723 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
724 NULL, 0, p, len ) ) != 0 )
725 goto cleanup;
726 p += len;
728 /* Import D */
729 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
730 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
731 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
732 p, len, NULL, 0 ) ) != 0 )
733 goto cleanup;
734 p += len;
736 /* Import P */
737 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
738 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
739 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
740 NULL, 0, NULL, 0 ) ) != 0 )
741 goto cleanup;
742 p += len;
744 /* Import Q */
745 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
746 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
747 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
748 NULL, 0, NULL, 0 ) ) != 0 )
749 goto cleanup;
750 p += len;
752 /* Complete the RSA private key */
753 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
754 goto cleanup;
756 /* Check optional parameters */
757 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
758 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
759 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
760 goto cleanup;
762 if( p != end )
764 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
765 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
768 cleanup:
770 mbedtls_mpi_free( &T );
772 if( ret != 0 )
774 /* Wrap error code if it's coming from a lower level */
775 if( ( ret & 0xff80 ) == 0 )
776 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
777 else
778 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
780 mbedtls_rsa_free( rsa );
783 return( ret );
785 #endif /* MBEDTLS_RSA_C */
787 #if defined(MBEDTLS_ECP_C)
789 * Parse a SEC1 encoded private EC key
791 static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
792 const unsigned char *key,
793 size_t keylen )
795 int ret;
796 int version, pubkey_done;
797 size_t len;
798 mbedtls_asn1_buf params;
799 unsigned char *p = (unsigned char *) key;
800 unsigned char *end = p + keylen;
801 unsigned char *end2;
804 * RFC 5915, or SEC1 Appendix C.4
806 * ECPrivateKey ::= SEQUENCE {
807 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
808 * privateKey OCTET STRING,
809 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
810 * publicKey [1] BIT STRING OPTIONAL
813 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
814 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
816 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
819 end = p + len;
821 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
822 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
824 if( version != 1 )
825 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
827 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
828 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
830 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
832 mbedtls_ecp_keypair_free( eck );
833 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
836 p += len;
838 pubkey_done = 0;
839 if( p != end )
842 * Is 'parameters' present?
844 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
845 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
847 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
848 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
850 mbedtls_ecp_keypair_free( eck );
851 return( ret );
854 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
856 mbedtls_ecp_keypair_free( eck );
857 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
861 if( p != end )
864 * Is 'publickey' present? If not, or if we can't read it (eg because it
865 * is compressed), create it from the private key.
867 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
868 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
870 end2 = p + len;
872 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
873 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
875 if( p + len != end2 )
876 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
877 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
879 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
880 pubkey_done = 1;
881 else
884 * The only acceptable failure mode of pk_get_ecpubkey() above
885 * is if the point format is not recognized.
887 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
888 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
891 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
893 mbedtls_ecp_keypair_free( eck );
894 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
898 if( ! pubkey_done &&
899 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
900 NULL, NULL ) ) != 0 )
902 mbedtls_ecp_keypair_free( eck );
903 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
906 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
908 mbedtls_ecp_keypair_free( eck );
909 return( ret );
912 return( 0 );
914 #endif /* MBEDTLS_ECP_C */
917 * Parse an unencrypted PKCS#8 encoded private key
919 * Notes:
921 * - This function does not own the key buffer. It is the
922 * responsibility of the caller to take care of zeroizing
923 * and freeing it after use.
925 * - The function is responsible for freeing the provided
926 * PK context on failure.
929 static int pk_parse_key_pkcs8_unencrypted_der(
930 mbedtls_pk_context *pk,
931 const unsigned char* key,
932 size_t keylen )
934 int ret, version;
935 size_t len;
936 mbedtls_asn1_buf params;
937 unsigned char *p = (unsigned char *) key;
938 unsigned char *end = p + keylen;
939 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
940 const mbedtls_pk_info_t *pk_info;
943 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
945 * PrivateKeyInfo ::= SEQUENCE {
946 * version Version,
947 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
948 * privateKey PrivateKey,
949 * attributes [0] IMPLICIT Attributes OPTIONAL }
951 * Version ::= INTEGER
952 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
953 * PrivateKey ::= OCTET STRING
955 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
958 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
959 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
961 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
964 end = p + len;
966 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
967 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
969 if( version != 0 )
970 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
972 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
973 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
975 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
976 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
978 if( len < 1 )
979 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
980 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
982 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
983 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
985 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
986 return( ret );
988 #if defined(MBEDTLS_RSA_C)
989 if( pk_alg == MBEDTLS_PK_RSA )
991 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
993 mbedtls_pk_free( pk );
994 return( ret );
996 } else
997 #endif /* MBEDTLS_RSA_C */
998 #if defined(MBEDTLS_ECP_C)
999 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
1001 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1002 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
1004 mbedtls_pk_free( pk );
1005 return( ret );
1007 } else
1008 #endif /* MBEDTLS_ECP_C */
1009 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1011 return( 0 );
1015 * Parse an encrypted PKCS#8 encoded private key
1017 * To save space, the decryption happens in-place on the given key buffer.
1018 * Also, while this function may modify the keybuffer, it doesn't own it,
1019 * and instead it is the responsibility of the caller to zeroize and properly
1020 * free it after use.
1023 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1024 static int pk_parse_key_pkcs8_encrypted_der(
1025 mbedtls_pk_context *pk,
1026 unsigned char *key, size_t keylen,
1027 const unsigned char *pwd, size_t pwdlen )
1029 int ret, decrypted = 0;
1030 size_t len;
1031 unsigned char *buf;
1032 unsigned char *p, *end;
1033 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1034 #if defined(MBEDTLS_PKCS12_C)
1035 mbedtls_cipher_type_t cipher_alg;
1036 mbedtls_md_type_t md_alg;
1037 #endif
1039 p = key;
1040 end = p + keylen;
1042 if( pwdlen == 0 )
1043 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1046 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
1048 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1049 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1050 * encryptedData EncryptedData
1053 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1055 * EncryptedData ::= OCTET STRING
1057 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
1060 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1061 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1063 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1066 end = p + len;
1068 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1069 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1071 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1072 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1074 buf = p;
1077 * Decrypt EncryptedData with appropriate PBE
1079 #if defined(MBEDTLS_PKCS12_C)
1080 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
1082 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1083 cipher_alg, md_alg,
1084 pwd, pwdlen, p, len, buf ) ) != 0 )
1086 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1087 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1089 return( ret );
1092 decrypted = 1;
1094 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
1096 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1097 MBEDTLS_PKCS12_PBE_DECRYPT,
1098 pwd, pwdlen,
1099 p, len, buf ) ) != 0 )
1101 return( ret );
1104 // Best guess for password mismatch when using RC4. If first tag is
1105 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
1107 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1108 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1110 decrypted = 1;
1112 else
1113 #endif /* MBEDTLS_PKCS12_C */
1114 #if defined(MBEDTLS_PKCS5_C)
1115 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
1117 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1118 p, len, buf ) ) != 0 )
1120 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1121 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1123 return( ret );
1126 decrypted = 1;
1128 else
1129 #endif /* MBEDTLS_PKCS5_C */
1131 ((void) pwd);
1134 if( decrypted == 0 )
1135 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1137 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1139 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1142 * Parse a private key
1144 int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
1145 const unsigned char *key, size_t keylen,
1146 const unsigned char *pwd, size_t pwdlen )
1148 int ret;
1149 const mbedtls_pk_info_t *pk_info;
1151 #if defined(MBEDTLS_PEM_PARSE_C)
1152 size_t len;
1153 mbedtls_pem_context pem;
1155 mbedtls_pem_init( &pem );
1157 #if defined(MBEDTLS_RSA_C)
1158 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1159 if( keylen == 0 || key[keylen - 1] != '\0' )
1160 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1161 else
1162 ret = mbedtls_pem_read_buffer( &pem,
1163 "-----BEGIN RSA PRIVATE KEY-----",
1164 "-----END RSA PRIVATE KEY-----",
1165 key, pwd, pwdlen, &len );
1167 if( ret == 0 )
1169 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
1170 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1171 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
1172 pem.buf, pem.buflen ) ) != 0 )
1174 mbedtls_pk_free( pk );
1177 mbedtls_pem_free( &pem );
1178 return( ret );
1180 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1181 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1182 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1183 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1184 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1185 return( ret );
1186 #endif /* MBEDTLS_RSA_C */
1188 #if defined(MBEDTLS_ECP_C)
1189 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1190 if( keylen == 0 || key[keylen - 1] != '\0' )
1191 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1192 else
1193 ret = mbedtls_pem_read_buffer( &pem,
1194 "-----BEGIN EC PRIVATE KEY-----",
1195 "-----END EC PRIVATE KEY-----",
1196 key, pwd, pwdlen, &len );
1197 if( ret == 0 )
1199 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
1201 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1202 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1203 pem.buf, pem.buflen ) ) != 0 )
1205 mbedtls_pk_free( pk );
1208 mbedtls_pem_free( &pem );
1209 return( ret );
1211 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1212 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1213 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1214 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1215 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1216 return( ret );
1217 #endif /* MBEDTLS_ECP_C */
1219 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1220 if( keylen == 0 || key[keylen - 1] != '\0' )
1221 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1222 else
1223 ret = mbedtls_pem_read_buffer( &pem,
1224 "-----BEGIN PRIVATE KEY-----",
1225 "-----END PRIVATE KEY-----",
1226 key, NULL, 0, &len );
1227 if( ret == 0 )
1229 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1230 pem.buf, pem.buflen ) ) != 0 )
1232 mbedtls_pk_free( pk );
1235 mbedtls_pem_free( &pem );
1236 return( ret );
1238 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1239 return( ret );
1241 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1242 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1243 if( keylen == 0 || key[keylen - 1] != '\0' )
1244 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1245 else
1246 ret = mbedtls_pem_read_buffer( &pem,
1247 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1248 "-----END ENCRYPTED PRIVATE KEY-----",
1249 key, NULL, 0, &len );
1250 if( ret == 0 )
1252 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1253 pem.buf, pem.buflen,
1254 pwd, pwdlen ) ) != 0 )
1256 mbedtls_pk_free( pk );
1259 mbedtls_pem_free( &pem );
1260 return( ret );
1262 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1263 return( ret );
1264 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1265 #else
1266 ((void) pwd);
1267 ((void) pwdlen);
1268 #endif /* MBEDTLS_PEM_PARSE_C */
1271 * At this point we only know it's not a PEM formatted key. Could be any
1272 * of the known DER encoded private key formats
1274 * We try the different DER format parsers to see if one passes without
1275 * error
1277 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1279 unsigned char *key_copy;
1281 if( keylen == 0 )
1282 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1284 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1285 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1287 memcpy( key_copy, key, keylen );
1289 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1290 pwd, pwdlen );
1292 mbedtls_platform_zeroize( key_copy, keylen );
1293 mbedtls_free( key_copy );
1296 if( ret == 0 )
1297 return( 0 );
1299 mbedtls_pk_free( pk );
1301 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
1303 return( ret );
1305 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1307 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1308 return( 0 );
1310 mbedtls_pk_free( pk );
1312 #if defined(MBEDTLS_RSA_C)
1314 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
1315 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1316 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
1317 key, keylen ) ) != 0 )
1319 mbedtls_pk_free( pk );
1321 else
1323 return( 0 );
1326 #endif /* MBEDTLS_RSA_C */
1328 #if defined(MBEDTLS_ECP_C)
1330 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
1331 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1332 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1333 key, keylen ) ) != 0 )
1335 mbedtls_pk_free( pk );
1337 else
1339 return( 0 );
1342 #endif /* MBEDTLS_ECP_C */
1344 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1348 * Parse a public key
1350 int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
1351 const unsigned char *key, size_t keylen )
1353 int ret;
1354 unsigned char *p;
1355 #if defined(MBEDTLS_RSA_C)
1356 const mbedtls_pk_info_t *pk_info;
1357 #endif
1358 #if defined(MBEDTLS_PEM_PARSE_C)
1359 size_t len;
1360 mbedtls_pem_context pem;
1362 mbedtls_pem_init( &pem );
1363 #if defined(MBEDTLS_RSA_C)
1364 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1365 if( keylen == 0 || key[keylen - 1] != '\0' )
1366 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1367 else
1368 ret = mbedtls_pem_read_buffer( &pem,
1369 "-----BEGIN RSA PUBLIC KEY-----",
1370 "-----END RSA PUBLIC KEY-----",
1371 key, NULL, 0, &len );
1373 if( ret == 0 )
1375 p = pem.buf;
1376 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1377 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1379 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1380 return( ret );
1382 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1383 mbedtls_pk_free( ctx );
1385 mbedtls_pem_free( &pem );
1386 return( ret );
1388 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1390 mbedtls_pem_free( &pem );
1391 return( ret );
1393 #endif /* MBEDTLS_RSA_C */
1395 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1396 if( keylen == 0 || key[keylen - 1] != '\0' )
1397 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1398 else
1399 ret = mbedtls_pem_read_buffer( &pem,
1400 "-----BEGIN PUBLIC KEY-----",
1401 "-----END PUBLIC KEY-----",
1402 key, NULL, 0, &len );
1404 if( ret == 0 )
1407 * Was PEM encoded
1409 p = pem.buf;
1411 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1412 mbedtls_pem_free( &pem );
1413 return( ret );
1415 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1417 mbedtls_pem_free( &pem );
1418 return( ret );
1420 mbedtls_pem_free( &pem );
1421 #endif /* MBEDTLS_PEM_PARSE_C */
1423 #if defined(MBEDTLS_RSA_C)
1424 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1425 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1427 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1428 return( ret );
1430 p = (unsigned char *)key;
1431 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
1432 if( ret == 0 )
1434 return( ret );
1436 mbedtls_pk_free( ctx );
1437 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
1439 return( ret );
1441 #endif /* MBEDTLS_RSA_C */
1442 p = (unsigned char *) key;
1444 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
1446 return( ret );
1449 #endif /* MBEDTLS_PK_PARSE_C */