1
#pragma clang diagnostic ignored "-Wunknown-warning-option"
2
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
3
#pragma clang diagnostic ignored "-Wsign-conversion"
5
#pragma GCC diagnostic ignored "-Wconversion"
7
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
8
#pragma clang diagnostic ignored "-Wsign-conversion"
10
#pragma GCC diagnostic ignored "-Wconversion"
12
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
13
#pragma clang diagnostic ignored "-Wsign-conversion"
15
* Public Key layer for parsing key files and structures
17
* Copyright The Mbed TLS Contributors
18
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20
* This file is provided under the Apache License 2.0, or the
21
* GNU General Public License v2.0 or later.
26
* Licensed under the Apache License, Version 2.0 (the "License"); you may
27
* not use this file except in compliance with the License.
28
* You may obtain a copy of the License at
30
* http://www.apache.org/licenses/LICENSE-2.0
32
* Unless required by applicable law or agreed to in writing, software
33
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
* See the License for the specific language governing permissions and
36
* limitations under the License.
41
* GNU General Public License v2.0 or later:
43
* This program is free software; you can redistribute it and/or modify
44
* it under the terms of the GNU General Public License as published by
45
* the Free Software Foundation; either version 2 of the License, or
46
* (at your option) any later version.
48
* This program is distributed in the hope that it will be useful,
49
* but WITHOUT ANY WARRANTY; without even the implied warranty of
50
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51
* GNU General Public License for more details.
53
* You should have received a copy of the GNU General Public License along
54
* with this program; if not, write to the Free Software Foundation, Inc.,
55
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
60
#if !defined(MBEDTLS_CONFIG_FILE)
61
#include "mbedtls/config.h"
63
#include MBEDTLS_CONFIG_FILE
66
#if defined(MBEDTLS_PK_PARSE_C)
68
#include "mbedtls/pk.h"
69
#include "mbedtls/asn1.h"
70
#include "mbedtls/oid.h"
71
#include "mbedtls/platform_util.h"
75
#if defined(MBEDTLS_RSA_C)
76
#include "mbedtls/rsa.h"
78
#if defined(MBEDTLS_ECP_C)
79
#include "mbedtls/ecp.h"
81
#if defined(MBEDTLS_ECDSA_C)
82
#include "mbedtls/ecdsa.h"
84
#if defined(MBEDTLS_PEM_PARSE_C)
85
#include "mbedtls/pem.h"
87
#if defined(MBEDTLS_PKCS5_C)
88
#include "mbedtls/pkcs5.h"
90
#if defined(MBEDTLS_PKCS12_C)
91
#include "mbedtls/pkcs12.h"
94
#if defined(MBEDTLS_PLATFORM_C)
95
#include "mbedtls/platform.h"
98
#define mbedtls_calloc calloc
99
#define mbedtls_free free
102
/* Parameter validation macros based on platform_util.h */
103
#define PK_VALIDATE_RET( cond ) \
104
MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
105
#define PK_VALIDATE( cond ) \
106
MBEDTLS_INTERNAL_VALIDATE( cond )
108
#if defined(MBEDTLS_FS_IO)
110
* Load all data from a file into a given buffer.
112
* The file is expected to contain either PEM or DER encoded data.
113
* A terminating null byte is always appended. It is included in the announced
114
* length only if the data looks like it is PEM encoded.
116
int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
121
PK_VALIDATE_RET( path != NULL );
122
PK_VALIDATE_RET( buf != NULL );
123
PK_VALIDATE_RET( n != NULL );
125
if( ( f = fopen( path, "rb" ) ) == NULL )
126
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
128
fseek( f, 0, SEEK_END );
129
if( ( size = ftell( f ) ) == -1 )
132
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
134
fseek( f, 0, SEEK_SET );
139
( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
142
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
145
if( fread( *buf, 1, *n, f ) != *n )
149
mbedtls_platform_zeroize( *buf, *n );
150
mbedtls_free( *buf );
152
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
159
if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
166
* Load and parse a private key
168
int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
169
const char *path, const char *pwd )
175
PK_VALIDATE_RET( ctx != NULL );
176
PK_VALIDATE_RET( path != NULL );
178
if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
182
ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
184
ret = mbedtls_pk_parse_key( ctx, buf, n,
185
(const unsigned char *) pwd, strlen( pwd ) );
187
mbedtls_platform_zeroize( buf, n );
194
* Load and parse a public key
196
int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
202
PK_VALIDATE_RET( ctx != NULL );
203
PK_VALIDATE_RET( path != NULL );
205
if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
208
ret = mbedtls_pk_parse_public_key( ctx, buf, n );
210
mbedtls_platform_zeroize( buf, n );
215
#endif /* MBEDTLS_FS_IO */
217
#if defined(MBEDTLS_ECP_C)
218
/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
220
* ECParameters ::= CHOICE {
221
* namedCurve OBJECT IDENTIFIER
222
* specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
223
* -- implicitCurve NULL
226
static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
227
mbedtls_asn1_buf *params )
232
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
233
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
235
/* Tag may be either OID or SEQUENCE */
237
if( params->tag != MBEDTLS_ASN1_OID
238
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
239
&& params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
243
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
244
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
247
if( ( ret = mbedtls_asn1_get_tag( p, end, ¶ms->len, params->tag ) ) != 0 )
249
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
256
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
257
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
262
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
264
* Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
265
* WARNING: the resulting group should only be used with
266
* pk_group_id_from_specified(), since its base point may not be set correctly
267
* if it was encoded compressed.
269
* SpecifiedECDomain ::= SEQUENCE {
270
* version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
271
* fieldID FieldID {{FieldTypes}},
275
* cofactor INTEGER OPTIONAL,
276
* hash HashAlgorithm OPTIONAL,
280
* We only support prime-field as field type, and ignore hash and cofactor.
282
static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
285
unsigned char *p = params->p;
286
const unsigned char * const end = params->p + params->len;
287
const unsigned char *end_field, *end_curve;
291
/* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
292
if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
293
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
295
if( ver < 1 || ver > 3 )
296
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
299
* FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
300
* fieldType FIELD-ID.&id({IOSet}),
301
* parameters FIELD-ID.&Type({IOSet}{@fieldType})
304
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
305
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
311
* FIELD-ID ::= TYPE-IDENTIFIER
312
* FieldTypes FIELD-ID ::= {
313
* { Prime-p IDENTIFIED BY prime-field } |
314
* { Characteristic-two IDENTIFIED BY characteristic-two-field }
316
* prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
318
if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
321
if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
322
memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
324
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
329
/* Prime-p ::= INTEGER -- Field of size p. */
330
if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
331
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
333
grp->pbits = mbedtls_mpi_bitlen( &grp->P );
336
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
337
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
340
* Curve ::= SEQUENCE {
343
* seed BIT STRING OPTIONAL
344
* -- Shall be present if used in SpecifiedECDomain
345
* -- with version equal to ecdpVer2 or ecdpVer3
348
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
349
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
355
* FieldElement ::= OCTET STRING
356
* containing an integer in the case of a prime field
358
if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
359
( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
361
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
366
if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
367
( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
369
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
374
/* Ignore seed BIT STRING OPTIONAL */
375
if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
379
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
380
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
383
* ECPoint ::= OCTET STRING
385
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
386
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
388
if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
389
( const unsigned char *) p, len ) ) != 0 )
392
* If we can't read the point because it's compressed, cheat by
393
* reading only the X coordinate and the parity bit of Y.
395
if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
396
( p[0] != 0x02 && p[0] != 0x03 ) ||
397
len != mbedtls_mpi_size( &grp->P ) + 1 ||
398
mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
399
mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
400
mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
402
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
411
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
412
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
414
grp->nbits = mbedtls_mpi_bitlen( &grp->N );
417
* Allow optional elements by purposefully not enforcing p == end here.
424
* Find the group id associated with an (almost filled) group as generated by
425
* pk_group_from_specified(), or return an error if unknown.
427
static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
430
mbedtls_ecp_group ref;
431
const mbedtls_ecp_group_id *id;
433
mbedtls_ecp_group_init( &ref );
435
for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
437
/* Load the group associated to that id */
438
mbedtls_ecp_group_free( &ref );
439
MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
441
/* Compare to the group we were given, starting with easy tests */
442
if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
443
mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
444
mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
445
mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
446
mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
447
mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
448
mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
449
/* For Y we may only know the parity bit, so compare only that */
450
mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
458
mbedtls_ecp_group_free( &ref );
462
if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
463
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
469
* Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
471
static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
472
mbedtls_ecp_group_id *grp_id )
475
mbedtls_ecp_group grp;
477
mbedtls_ecp_group_init( &grp );
479
if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
482
ret = pk_group_id_from_group( &grp, grp_id );
485
mbedtls_ecp_group_free( &grp );
489
#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
492
* Use EC parameters to initialise an EC group
494
* ECParameters ::= CHOICE {
495
* namedCurve OBJECT IDENTIFIER
496
* specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
497
* -- implicitCurve NULL
499
static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
502
mbedtls_ecp_group_id grp_id;
504
if( params->tag == MBEDTLS_ASN1_OID )
506
if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
507
return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
511
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
512
if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
515
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
520
* grp may already be initilialized; if so, make sure IDs match
522
if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
523
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
525
if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
532
* EC public key is an EC point
534
* The caller is responsible for clearing the structure upon failure if
535
* desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
536
* return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
538
static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
539
mbedtls_ecp_keypair *key )
543
if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
544
(const unsigned char *) *p, end - *p ) ) == 0 )
546
ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
550
* We know mbedtls_ecp_point_read_binary consumed all bytes or failed
552
*p = (unsigned char *) end;
556
#endif /* MBEDTLS_ECP_C */
558
#if defined(MBEDTLS_RSA_C)
560
* RSAPublicKey ::= SEQUENCE {
561
* modulus INTEGER, -- n
562
* publicExponent INTEGER -- e
565
static int pk_get_rsapubkey( unsigned char **p,
566
const unsigned char *end,
567
mbedtls_rsa_context *rsa )
572
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
573
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
574
return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
576
if( *p + len != end )
577
return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
578
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
581
if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
582
return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
584
if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
585
NULL, 0, NULL, 0 ) ) != 0 )
586
return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
591
if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
592
return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
594
if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
595
NULL, 0, *p, len ) ) != 0 )
596
return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
600
if( mbedtls_rsa_complete( rsa ) != 0 ||
601
mbedtls_rsa_check_pubkey( rsa ) != 0 )
603
return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
607
return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
608
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
612
#endif /* MBEDTLS_RSA_C */
614
/* Get a PK algorithm identifier
616
* AlgorithmIdentifier ::= SEQUENCE {
617
* algorithm OBJECT IDENTIFIER,
618
* parameters ANY DEFINED BY algorithm OPTIONAL }
620
static int pk_get_pk_alg( unsigned char **p,
621
const unsigned char *end,
622
mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
625
mbedtls_asn1_buf alg_oid;
627
memset( params, 0, sizeof(mbedtls_asn1_buf) );
629
if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
630
return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
632
if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
633
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
636
* No parameters with RSA (only for EC)
638
if( *pk_alg == MBEDTLS_PK_RSA &&
639
( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
642
return( MBEDTLS_ERR_PK_INVALID_ALG );
649
* SubjectPublicKeyInfo ::= SEQUENCE {
650
* algorithm AlgorithmIdentifier,
651
* subjectPublicKey BIT STRING }
653
int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
654
mbedtls_pk_context *pk )
658
mbedtls_asn1_buf alg_params;
659
mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
660
const mbedtls_pk_info_t *pk_info;
662
PK_VALIDATE_RET( p != NULL );
663
PK_VALIDATE_RET( *p != NULL );
664
PK_VALIDATE_RET( end != NULL );
665
PK_VALIDATE_RET( pk != NULL );
667
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
668
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
670
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
675
if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
678
if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
679
return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
681
if( *p + len != end )
682
return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
683
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
685
if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
686
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
688
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
691
#if defined(MBEDTLS_RSA_C)
692
if( pk_alg == MBEDTLS_PK_RSA )
694
ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
696
#endif /* MBEDTLS_RSA_C */
697
#if defined(MBEDTLS_ECP_C)
698
if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
700
ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
702
ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
704
#endif /* MBEDTLS_ECP_C */
705
ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
707
if( ret == 0 && *p != end )
708
ret = MBEDTLS_ERR_PK_INVALID_PUBKEY +
709
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
712
mbedtls_pk_free( pk );
717
#if defined(MBEDTLS_RSA_C)
719
* Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
722
* - never a valid value for an RSA parameter
723
* - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
725
* Since values can't be omitted in PKCS#1, passing a zero value to
726
* rsa_complete() would be incorrect, so reject zero values early.
728
static int asn1_get_nonzero_mpi( unsigned char **p,
729
const unsigned char *end,
734
ret = mbedtls_asn1_get_mpi( p, end, X );
738
if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
739
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
745
* Parse a PKCS#1 encoded private RSA key
747
static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
748
const unsigned char *key,
753
unsigned char *p, *end;
756
mbedtls_mpi_init( &T );
758
p = (unsigned char *) key;
762
* This function parses the RSAPrivateKey (PKCS#1)
764
* RSAPrivateKey ::= SEQUENCE {
766
* modulus INTEGER, -- n
767
* publicExponent INTEGER, -- e
768
* privateExponent INTEGER, -- d
769
* prime1 INTEGER, -- p
770
* prime2 INTEGER, -- q
771
* exponent1 INTEGER, -- d mod (p-1)
772
* exponent2 INTEGER, -- d mod (q-1)
773
* coefficient INTEGER, -- (inverse of q) mod p
774
* otherPrimeInfos OtherPrimeInfos OPTIONAL
777
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
778
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
780
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
785
if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
787
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
792
return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
796
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
797
( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
798
NULL, NULL ) ) != 0 )
802
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
803
( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
808
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
809
( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
814
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
815
( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
816
NULL, NULL ) ) != 0 )
820
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
821
( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
822
NULL, NULL ) ) != 0 )
825
#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
827
* The RSA CRT parameters DP, DQ and QP are nominally redundant, in
828
* that they can be easily recomputed from D, P and Q. However by
829
* parsing them from the PKCS1 structure it is possible to avoid
830
* recalculating them which both reduces the overhead of loading
831
* RSA private keys into memory and also avoids side channels which
832
* can arise when computing those values, since all of D, P, and Q
833
* are secret. See https://eprint.iacr.org/2020/055 for a
834
* description of one such attack.
838
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
839
( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
843
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
844
( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
848
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
849
( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
853
/* Verify existance of the CRT params */
854
if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
855
( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
856
( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
860
/* rsa_complete() doesn't complete anything with the default
861
* implementation but is still called:
862
* - for the benefit of alternative implementation that may want to
863
* pre-compute stuff beyond what's provided (eg Montgomery factors)
864
* - as is also sanity-checks the key
866
* Furthermore, we also check the public part for consistency with
867
* mbedtls_pk_parse_pubkey(), as it includes size minima for example.
869
if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
870
( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
877
ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
878
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
883
mbedtls_mpi_free( &T );
887
/* Wrap error code if it's coming from a lower level */
888
if( ( ret & 0xff80 ) == 0 )
889
ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
891
ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
893
mbedtls_rsa_free( rsa );
898
#endif /* MBEDTLS_RSA_C */
900
#if defined(MBEDTLS_ECP_C)
902
* Parse a SEC1 encoded private EC key
904
static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
905
const unsigned char *key,
909
int version, pubkey_done;
911
mbedtls_asn1_buf params;
912
unsigned char *p = (unsigned char *) key;
913
unsigned char *end = p + keylen;
917
* RFC 5915, or SEC1 Appendix C.4
919
* ECPrivateKey ::= SEQUENCE {
920
* version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
921
* privateKey OCTET STRING,
922
* parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
923
* publicKey [1] BIT STRING OPTIONAL
926
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
927
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
929
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
934
if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
935
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
938
return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
940
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
941
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
943
if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
945
mbedtls_ecp_keypair_free( eck );
946
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
955
* Is 'parameters' present?
957
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
958
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
960
if( ( ret = pk_get_ecparams( &p, p + len, ¶ms) ) != 0 ||
961
( ret = pk_use_ecparams( ¶ms, &eck->grp ) ) != 0 )
963
mbedtls_ecp_keypair_free( eck );
967
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
969
mbedtls_ecp_keypair_free( eck );
970
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
977
* Is 'publickey' present? If not, or if we can't read it (eg because it
978
* is compressed), create it from the private key.
980
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
981
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
985
if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
986
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
988
if( p + len != end2 )
989
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
990
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
992
if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
997
* The only acceptable failure mode of pk_get_ecpubkey() above
998
* is if the point format is not recognized.
1000
if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1001
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1004
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
1006
mbedtls_ecp_keypair_free( eck );
1007
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1011
if( ! pubkey_done &&
1012
( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
1013
NULL, NULL ) ) != 0 )
1015
mbedtls_ecp_keypair_free( eck );
1016
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1019
if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
1021
mbedtls_ecp_keypair_free( eck );
1027
#endif /* MBEDTLS_ECP_C */
1030
* Parse an unencrypted PKCS#8 encoded private key
1034
* - This function does not own the key buffer. It is the
1035
* responsibility of the caller to take care of zeroizing
1036
* and freeing it after use.
1038
* - The function is responsible for freeing the provided
1039
* PK context on failure.
1042
static int pk_parse_key_pkcs8_unencrypted_der(
1043
mbedtls_pk_context *pk,
1044
const unsigned char* key,
1049
mbedtls_asn1_buf params;
1050
unsigned char *p = (unsigned char *) key;
1051
unsigned char *end = p + keylen;
1052
mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1053
const mbedtls_pk_info_t *pk_info;
1056
* This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
1058
* PrivateKeyInfo ::= SEQUENCE {
1060
* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1061
* privateKey PrivateKey,
1062
* attributes [0] IMPLICIT Attributes OPTIONAL }
1064
* Version ::= INTEGER
1065
* PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1066
* PrivateKey ::= OCTET STRING
1068
* The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1071
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1072
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1074
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1079
if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1080
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1083
return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
1085
if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, ¶ms ) ) != 0 )
1088
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1089
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1092
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1093
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
1095
if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1096
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1098
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
1101
#if defined(MBEDTLS_RSA_C)
1102
if( pk_alg == MBEDTLS_PK_RSA )
1104
if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
1106
mbedtls_pk_free( pk );
1110
#endif /* MBEDTLS_RSA_C */
1111
#if defined(MBEDTLS_ECP_C)
1112
if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
1114
if( ( ret = pk_use_ecparams( ¶ms, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1115
( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
1117
mbedtls_pk_free( pk );
1121
#endif /* MBEDTLS_ECP_C */
1122
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1128
* Parse an encrypted PKCS#8 encoded private key
1130
* To save space, the decryption happens in-place on the given key buffer.
1131
* Also, while this function may modify the keybuffer, it doesn't own it,
1132
* and instead it is the responsibility of the caller to zeroize and properly
1133
* free it after use.
1136
#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1137
static int pk_parse_key_pkcs8_encrypted_der(
1138
mbedtls_pk_context *pk,
1139
unsigned char *key, size_t keylen,
1140
const unsigned char *pwd, size_t pwdlen )
1142
int ret, decrypted = 0;
1145
unsigned char *p, *end;
1146
mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1147
#if defined(MBEDTLS_PKCS12_C)
1148
mbedtls_cipher_type_t cipher_alg;
1149
mbedtls_md_type_t md_alg;
1156
return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1159
* This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
1161
* EncryptedPrivateKeyInfo ::= SEQUENCE {
1162
* encryptionAlgorithm EncryptionAlgorithmIdentifier,
1163
* encryptedData EncryptedData
1166
* EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1168
* EncryptedData ::= OCTET STRING
1170
* The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
1173
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1174
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
1176
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1181
if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1182
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1184
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1185
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1190
* Decrypt EncryptedData with appropriate PBE
1192
#if defined(MBEDTLS_PKCS12_C)
1193
if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
1195
if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1197
pwd, pwdlen, p, len, buf ) ) != 0 )
1199
if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1200
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1207
else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
1209
if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1210
MBEDTLS_PKCS12_PBE_DECRYPT,
1212
p, len, buf ) ) != 0 )
1217
// Best guess for password mismatch when using RC4. If first tag is
1218
// not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
1220
if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1221
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1226
#endif /* MBEDTLS_PKCS12_C */
1227
#if defined(MBEDTLS_PKCS5_C)
1228
if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
1230
if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1231
p, len, buf ) ) != 0 )
1233
if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1234
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1242
#endif /* MBEDTLS_PKCS5_C */
1247
if( decrypted == 0 )
1248
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1250
return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1252
#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1255
* Parse a private key
1257
int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
1258
const unsigned char *key, size_t keylen,
1259
const unsigned char *pwd, size_t pwdlen )
1262
const mbedtls_pk_info_t *pk_info;
1263
#if defined(MBEDTLS_PEM_PARSE_C)
1265
mbedtls_pem_context pem;
1268
PK_VALIDATE_RET( pk != NULL );
1270
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1271
PK_VALIDATE_RET( key != NULL );
1273
#if defined(MBEDTLS_PEM_PARSE_C)
1274
mbedtls_pem_init( &pem );
1276
#if defined(MBEDTLS_RSA_C)
1277
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1278
if( key[keylen - 1] != '\0' )
1279
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1281
ret = mbedtls_pem_read_buffer( &pem,
1282
"-----BEGIN RSA PRIVATE KEY-----",
1283
"-----END RSA PRIVATE KEY-----",
1284
key, pwd, pwdlen, &len );
1288
pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
1289
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1290
( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
1291
pem.buf, pem.buflen ) ) != 0 )
1293
mbedtls_pk_free( pk );
1296
mbedtls_pem_free( &pem );
1299
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1300
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1301
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1302
return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1303
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1305
#endif /* MBEDTLS_RSA_C */
1307
#if defined(MBEDTLS_ECP_C)
1308
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1309
if( key[keylen - 1] != '\0' )
1310
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1312
ret = mbedtls_pem_read_buffer( &pem,
1313
"-----BEGIN EC PRIVATE KEY-----",
1314
"-----END EC PRIVATE KEY-----",
1315
key, pwd, pwdlen, &len );
1318
pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
1320
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1321
( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1322
pem.buf, pem.buflen ) ) != 0 )
1324
mbedtls_pk_free( pk );
1327
mbedtls_pem_free( &pem );
1330
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1331
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1332
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1333
return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1334
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1336
#endif /* MBEDTLS_ECP_C */
1338
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1339
if( key[keylen - 1] != '\0' )
1340
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1342
ret = mbedtls_pem_read_buffer( &pem,
1343
"-----BEGIN PRIVATE KEY-----",
1344
"-----END PRIVATE KEY-----",
1345
key, NULL, 0, &len );
1348
if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1349
pem.buf, pem.buflen ) ) != 0 )
1351
mbedtls_pk_free( pk );
1354
mbedtls_pem_free( &pem );
1357
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1360
#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1361
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1362
if( key[keylen - 1] != '\0' )
1363
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1365
ret = mbedtls_pem_read_buffer( &pem,
1366
"-----BEGIN ENCRYPTED PRIVATE KEY-----",
1367
"-----END ENCRYPTED PRIVATE KEY-----",
1368
key, NULL, 0, &len );
1371
if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1372
pem.buf, pem.buflen,
1373
pwd, pwdlen ) ) != 0 )
1375
mbedtls_pk_free( pk );
1378
mbedtls_pem_free( &pem );
1381
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1383
#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1387
#endif /* MBEDTLS_PEM_PARSE_C */
1390
* At this point we only know it's not a PEM formatted key. Could be any
1391
* of the known DER encoded private key formats
1393
* We try the different DER format parsers to see if one passes without
1396
#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1398
unsigned char *key_copy;
1400
if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1401
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1403
memcpy( key_copy, key, keylen );
1405
ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1408
mbedtls_platform_zeroize( key_copy, keylen );
1409
mbedtls_free( key_copy );
1415
mbedtls_pk_free( pk );
1416
mbedtls_pk_init( pk );
1418
if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
1422
#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1424
if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1427
mbedtls_pk_free( pk );
1428
mbedtls_pk_init( pk );
1430
#if defined(MBEDTLS_RSA_C)
1432
pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
1433
if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1434
pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
1439
mbedtls_pk_free( pk );
1440
mbedtls_pk_init( pk );
1441
#endif /* MBEDTLS_RSA_C */
1443
#if defined(MBEDTLS_ECP_C)
1444
pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
1445
if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1446
pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1447
key, keylen ) == 0 )
1451
mbedtls_pk_free( pk );
1452
#endif /* MBEDTLS_ECP_C */
1454
/* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1455
* it is ok to leave the PK context initialized but not
1456
* freed: It is the caller's responsibility to call pk_init()
1457
* before calling this function, and to call pk_free()
1458
* when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1459
* isn't, this leads to mbedtls_pk_free() being called
1460
* twice, once here and once by the caller, but this is
1461
* also ok and in line with the mbedtls_pk_free() calls
1462
* on failed PEM parsing attempts. */
1464
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1468
* Parse a public key
1470
int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
1471
const unsigned char *key, size_t keylen )
1475
#if defined(MBEDTLS_RSA_C)
1476
const mbedtls_pk_info_t *pk_info;
1478
#if defined(MBEDTLS_PEM_PARSE_C)
1480
mbedtls_pem_context pem;
1483
PK_VALIDATE_RET( ctx != NULL );
1485
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1486
PK_VALIDATE_RET( key != NULL || keylen == 0 );
1488
#if defined(MBEDTLS_PEM_PARSE_C)
1489
mbedtls_pem_init( &pem );
1490
#if defined(MBEDTLS_RSA_C)
1491
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1492
if( key[keylen - 1] != '\0' )
1493
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1495
ret = mbedtls_pem_read_buffer( &pem,
1496
"-----BEGIN RSA PUBLIC KEY-----",
1497
"-----END RSA PUBLIC KEY-----",
1498
key, NULL, 0, &len );
1503
if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1504
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1506
if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1509
if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1510
mbedtls_pk_free( ctx );
1512
mbedtls_pem_free( &pem );
1515
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1517
mbedtls_pem_free( &pem );
1520
#endif /* MBEDTLS_RSA_C */
1522
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1523
if( key[keylen - 1] != '\0' )
1524
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1526
ret = mbedtls_pem_read_buffer( &pem,
1527
"-----BEGIN PUBLIC KEY-----",
1528
"-----END PUBLIC KEY-----",
1529
key, NULL, 0, &len );
1538
ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1539
mbedtls_pem_free( &pem );
1542
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1544
mbedtls_pem_free( &pem );
1547
mbedtls_pem_free( &pem );
1548
#endif /* MBEDTLS_PEM_PARSE_C */
1550
#if defined(MBEDTLS_RSA_C)
1551
if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1552
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1554
if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1557
p = (unsigned char *)key;
1558
ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
1563
mbedtls_pk_free( ctx );
1564
if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
1568
#endif /* MBEDTLS_RSA_C */
1569
p = (unsigned char *) key;
1571
ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
1576
#endif /* MBEDTLS_PK_PARSE_C */