[AMSTREAM] Sync with Wine Staging 3.9. CORE-14656
[reactos.git] / dll / 3rdparty / mbedtls / pkparse.c
1 /*
2 * Public Key layer for parsing key files and structures
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
6 *
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.
11 *
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.
16 *
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.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29
30 #if defined(MBEDTLS_PK_PARSE_C)
31
32 #include "mbedtls/pk.h"
33 #include "mbedtls/asn1.h"
34 #include "mbedtls/oid.h"
35
36 #include <string.h>
37
38 #if defined(MBEDTLS_RSA_C)
39 #include "mbedtls/rsa.h"
40 #endif
41 #if defined(MBEDTLS_ECP_C)
42 #include "mbedtls/ecp.h"
43 #endif
44 #if defined(MBEDTLS_ECDSA_C)
45 #include "mbedtls/ecdsa.h"
46 #endif
47 #if defined(MBEDTLS_PEM_PARSE_C)
48 #include "mbedtls/pem.h"
49 #endif
50 #if defined(MBEDTLS_PKCS5_C)
51 #include "mbedtls/pkcs5.h"
52 #endif
53 #if defined(MBEDTLS_PKCS12_C)
54 #include "mbedtls/pkcs12.h"
55 #endif
56
57 #if defined(MBEDTLS_PLATFORM_C)
58 #include "mbedtls/platform.h"
59 #else
60 #include <stdlib.h>
61 #define mbedtls_calloc calloc
62 #define mbedtls_free free
63 #endif
64
65 #if defined(MBEDTLS_FS_IO)
66 /* Implementation that should never be optimized out by the compiler */
67 static void mbedtls_zeroize( void *v, size_t n ) {
68 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
69 }
70
71 /*
72 * Load all data from a file into a given buffer.
73 *
74 * The file is expected to contain either PEM or DER encoded data.
75 * A terminating null byte is always appended. It is included in the announced
76 * length only if the data looks like it is PEM encoded.
77 */
78 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
79 {
80 FILE *f;
81 long size;
82
83 if( ( f = fopen( path, "rb" ) ) == NULL )
84 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
85
86 fseek( f, 0, SEEK_END );
87 if( ( size = ftell( f ) ) == -1 )
88 {
89 fclose( f );
90 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
91 }
92 fseek( f, 0, SEEK_SET );
93
94 *n = (size_t) size;
95
96 if( *n + 1 == 0 ||
97 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
98 {
99 fclose( f );
100 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
101 }
102
103 if( fread( *buf, 1, *n, f ) != *n )
104 {
105 fclose( f );
106 mbedtls_free( *buf );
107 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
108 }
109
110 fclose( f );
111
112 (*buf)[*n] = '\0';
113
114 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
115 ++*n;
116
117 return( 0 );
118 }
119
120 /*
121 * Load and parse a private key
122 */
123 int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
124 const char *path, const char *pwd )
125 {
126 int ret;
127 size_t n;
128 unsigned char *buf;
129
130 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
131 return( ret );
132
133 if( pwd == NULL )
134 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
135 else
136 ret = mbedtls_pk_parse_key( ctx, buf, n,
137 (const unsigned char *) pwd, strlen( pwd ) );
138
139 mbedtls_zeroize( buf, n );
140 mbedtls_free( buf );
141
142 return( ret );
143 }
144
145 /*
146 * Load and parse a public key
147 */
148 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
149 {
150 int ret;
151 size_t n;
152 unsigned char *buf;
153
154 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
155 return( ret );
156
157 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
158
159 mbedtls_zeroize( buf, n );
160 mbedtls_free( buf );
161
162 return( ret );
163 }
164 #endif /* MBEDTLS_FS_IO */
165
166 #if defined(MBEDTLS_ECP_C)
167 /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
168 *
169 * ECParameters ::= CHOICE {
170 * namedCurve OBJECT IDENTIFIER
171 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
172 * -- implicitCurve NULL
173 * }
174 */
175 static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
176 mbedtls_asn1_buf *params )
177 {
178 int ret;
179
180 /* Tag may be either OID or SEQUENCE */
181 params->tag = **p;
182 if( params->tag != MBEDTLS_ASN1_OID
183 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
184 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
185 #endif
186 )
187 {
188 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
189 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
190 }
191
192 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
193 {
194 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
195 }
196
197 params->p = *p;
198 *p += params->len;
199
200 if( *p != end )
201 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
202 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
203
204 return( 0 );
205 }
206
207 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
208 /*
209 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
210 * WARNING: the resulting group should only be used with
211 * pk_group_id_from_specified(), since its base point may not be set correctly
212 * if it was encoded compressed.
213 *
214 * SpecifiedECDomain ::= SEQUENCE {
215 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
216 * fieldID FieldID {{FieldTypes}},
217 * curve Curve,
218 * base ECPoint,
219 * order INTEGER,
220 * cofactor INTEGER OPTIONAL,
221 * hash HashAlgorithm OPTIONAL,
222 * ...
223 * }
224 *
225 * We only support prime-field as field type, and ignore hash and cofactor.
226 */
227 static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
228 {
229 int ret;
230 unsigned char *p = params->p;
231 const unsigned char * const end = params->p + params->len;
232 const unsigned char *end_field, *end_curve;
233 size_t len;
234 int ver;
235
236 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
237 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
238 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
239
240 if( ver < 1 || ver > 3 )
241 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
242
243 /*
244 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
245 * fieldType FIELD-ID.&id({IOSet}),
246 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
247 * }
248 */
249 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
250 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
251 return( ret );
252
253 end_field = p + len;
254
255 /*
256 * FIELD-ID ::= TYPE-IDENTIFIER
257 * FieldTypes FIELD-ID ::= {
258 * { Prime-p IDENTIFIED BY prime-field } |
259 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
260 * }
261 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
262 */
263 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
264 return( ret );
265
266 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
267 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
268 {
269 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
270 }
271
272 p += len;
273
274 /* Prime-p ::= INTEGER -- Field of size p. */
275 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
276 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
277
278 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
279
280 if( p != end_field )
281 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
282 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
283
284 /*
285 * Curve ::= SEQUENCE {
286 * a FieldElement,
287 * b FieldElement,
288 * seed BIT STRING OPTIONAL
289 * -- Shall be present if used in SpecifiedECDomain
290 * -- with version equal to ecdpVer2 or ecdpVer3
291 * }
292 */
293 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
294 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
295 return( ret );
296
297 end_curve = p + len;
298
299 /*
300 * FieldElement ::= OCTET STRING
301 * containing an integer in the case of a prime field
302 */
303 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
304 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
305 {
306 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
307 }
308
309 p += len;
310
311 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
312 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
313 {
314 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
315 }
316
317 p += len;
318
319 /* Ignore seed BIT STRING OPTIONAL */
320 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
321 p += len;
322
323 if( p != end_curve )
324 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
325 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
326
327 /*
328 * ECPoint ::= OCTET STRING
329 */
330 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
331 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
332
333 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
334 ( const unsigned char *) p, len ) ) != 0 )
335 {
336 /*
337 * If we can't read the point because it's compressed, cheat by
338 * reading only the X coordinate and the parity bit of Y.
339 */
340 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
341 ( p[0] != 0x02 && p[0] != 0x03 ) ||
342 len != mbedtls_mpi_size( &grp->P ) + 1 ||
343 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
344 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
345 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
346 {
347 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
348 }
349 }
350
351 p += len;
352
353 /*
354 * order INTEGER
355 */
356 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
357 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
358
359 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
360
361 /*
362 * Allow optional elements by purposefully not enforcing p == end here.
363 */
364
365 return( 0 );
366 }
367
368 /*
369 * Find the group id associated with an (almost filled) group as generated by
370 * pk_group_from_specified(), or return an error if unknown.
371 */
372 static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
373 {
374 int ret = 0;
375 mbedtls_ecp_group ref;
376 const mbedtls_ecp_group_id *id;
377
378 mbedtls_ecp_group_init( &ref );
379
380 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
381 {
382 /* Load the group associated to that id */
383 mbedtls_ecp_group_free( &ref );
384 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
385
386 /* Compare to the group we were given, starting with easy tests */
387 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
388 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
389 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
390 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
391 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
392 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
393 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
394 /* For Y we may only know the parity bit, so compare only that */
395 mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
396 {
397 break;
398 }
399
400 }
401
402 cleanup:
403 mbedtls_ecp_group_free( &ref );
404
405 *grp_id = *id;
406
407 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
408 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
409
410 return( ret );
411 }
412
413 /*
414 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
415 */
416 static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
417 mbedtls_ecp_group_id *grp_id )
418 {
419 int ret;
420 mbedtls_ecp_group grp;
421
422 mbedtls_ecp_group_init( &grp );
423
424 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
425 goto cleanup;
426
427 ret = pk_group_id_from_group( &grp, grp_id );
428
429 cleanup:
430 mbedtls_ecp_group_free( &grp );
431
432 return( ret );
433 }
434 #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
435
436 /*
437 * Use EC parameters to initialise an EC group
438 *
439 * ECParameters ::= CHOICE {
440 * namedCurve OBJECT IDENTIFIER
441 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
442 * -- implicitCurve NULL
443 */
444 static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
445 {
446 int ret;
447 mbedtls_ecp_group_id grp_id;
448
449 if( params->tag == MBEDTLS_ASN1_OID )
450 {
451 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
452 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
453 }
454 else
455 {
456 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
457 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
458 return( ret );
459 #else
460 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
461 #endif
462 }
463
464 /*
465 * grp may already be initilialized; if so, make sure IDs match
466 */
467 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
468 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
469
470 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
471 return( ret );
472
473 return( 0 );
474 }
475
476 /*
477 * EC public key is an EC point
478 *
479 * The caller is responsible for clearing the structure upon failure if
480 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
481 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
482 */
483 static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
484 mbedtls_ecp_keypair *key )
485 {
486 int ret;
487
488 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
489 (const unsigned char *) *p, end - *p ) ) == 0 )
490 {
491 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
492 }
493
494 /*
495 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
496 */
497 *p = (unsigned char *) end;
498
499 return( ret );
500 }
501 #endif /* MBEDTLS_ECP_C */
502
503 #if defined(MBEDTLS_RSA_C)
504 /*
505 * RSAPublicKey ::= SEQUENCE {
506 * modulus INTEGER, -- n
507 * publicExponent INTEGER -- e
508 * }
509 */
510 static int pk_get_rsapubkey( unsigned char **p,
511 const unsigned char *end,
512 mbedtls_rsa_context *rsa )
513 {
514 int ret;
515 size_t len;
516
517 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
518 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
519 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
520
521 if( *p + len != end )
522 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
523 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
524
525 if( ( ret = mbedtls_asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
526 ( ret = mbedtls_asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
527 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
528
529 if( *p != end )
530 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
531 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
532
533 if( ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
534 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
535
536 rsa->len = mbedtls_mpi_size( &rsa->N );
537
538 return( 0 );
539 }
540 #endif /* MBEDTLS_RSA_C */
541
542 /* Get a PK algorithm identifier
543 *
544 * AlgorithmIdentifier ::= SEQUENCE {
545 * algorithm OBJECT IDENTIFIER,
546 * parameters ANY DEFINED BY algorithm OPTIONAL }
547 */
548 static int pk_get_pk_alg( unsigned char **p,
549 const unsigned char *end,
550 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
551 {
552 int ret;
553 mbedtls_asn1_buf alg_oid;
554
555 memset( params, 0, sizeof(mbedtls_asn1_buf) );
556
557 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
558 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
559
560 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
561 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
562
563 /*
564 * No parameters with RSA (only for EC)
565 */
566 if( *pk_alg == MBEDTLS_PK_RSA &&
567 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
568 params->len != 0 ) )
569 {
570 return( MBEDTLS_ERR_PK_INVALID_ALG );
571 }
572
573 return( 0 );
574 }
575
576 /*
577 * SubjectPublicKeyInfo ::= SEQUENCE {
578 * algorithm AlgorithmIdentifier,
579 * subjectPublicKey BIT STRING }
580 */
581 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
582 mbedtls_pk_context *pk )
583 {
584 int ret;
585 size_t len;
586 mbedtls_asn1_buf alg_params;
587 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
588 const mbedtls_pk_info_t *pk_info;
589
590 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
591 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
592 {
593 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
594 }
595
596 end = *p + len;
597
598 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
599 return( ret );
600
601 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
602 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
603
604 if( *p + len != end )
605 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
606 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
607
608 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
609 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
610
611 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
612 return( ret );
613
614 #if defined(MBEDTLS_RSA_C)
615 if( pk_alg == MBEDTLS_PK_RSA )
616 {
617 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
618 } else
619 #endif /* MBEDTLS_RSA_C */
620 #if defined(MBEDTLS_ECP_C)
621 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
622 {
623 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
624 if( ret == 0 )
625 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
626 } else
627 #endif /* MBEDTLS_ECP_C */
628 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
629
630 if( ret == 0 && *p != end )
631 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
632 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
633
634 if( ret != 0 )
635 mbedtls_pk_free( pk );
636
637 return( ret );
638 }
639
640 #if defined(MBEDTLS_RSA_C)
641 /*
642 * Parse a PKCS#1 encoded private RSA key
643 */
644 static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
645 const unsigned char *key,
646 size_t keylen )
647 {
648 int ret;
649 size_t len;
650 unsigned char *p, *end;
651
652 p = (unsigned char *) key;
653 end = p + keylen;
654
655 /*
656 * This function parses the RSAPrivateKey (PKCS#1)
657 *
658 * RSAPrivateKey ::= SEQUENCE {
659 * version Version,
660 * modulus INTEGER, -- n
661 * publicExponent INTEGER, -- e
662 * privateExponent INTEGER, -- d
663 * prime1 INTEGER, -- p
664 * prime2 INTEGER, -- q
665 * exponent1 INTEGER, -- d mod (p-1)
666 * exponent2 INTEGER, -- d mod (q-1)
667 * coefficient INTEGER, -- (inverse of q) mod p
668 * otherPrimeInfos OtherPrimeInfos OPTIONAL
669 * }
670 */
671 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
672 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
673 {
674 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
675 }
676
677 end = p + len;
678
679 if( ( ret = mbedtls_asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
680 {
681 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
682 }
683
684 if( rsa->ver != 0 )
685 {
686 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
687 }
688
689 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
690 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
691 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
692 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
693 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
694 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
695 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
696 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
697 {
698 mbedtls_rsa_free( rsa );
699 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
700 }
701
702 rsa->len = mbedtls_mpi_size( &rsa->N );
703
704 if( p != end )
705 {
706 mbedtls_rsa_free( rsa );
707 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
708 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
709 }
710
711 if( ( ret = mbedtls_rsa_check_privkey( rsa ) ) != 0 )
712 {
713 mbedtls_rsa_free( rsa );
714 return( ret );
715 }
716
717 return( 0 );
718 }
719 #endif /* MBEDTLS_RSA_C */
720
721 #if defined(MBEDTLS_ECP_C)
722 /*
723 * Parse a SEC1 encoded private EC key
724 */
725 static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
726 const unsigned char *key,
727 size_t keylen )
728 {
729 int ret;
730 int version, pubkey_done;
731 size_t len;
732 mbedtls_asn1_buf params;
733 unsigned char *p = (unsigned char *) key;
734 unsigned char *end = p + keylen;
735 unsigned char *end2;
736
737 /*
738 * RFC 5915, or SEC1 Appendix C.4
739 *
740 * ECPrivateKey ::= SEQUENCE {
741 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
742 * privateKey OCTET STRING,
743 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
744 * publicKey [1] BIT STRING OPTIONAL
745 * }
746 */
747 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
748 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
749 {
750 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
751 }
752
753 end = p + len;
754
755 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
756 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
757
758 if( version != 1 )
759 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
760
761 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
762 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
763
764 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
765 {
766 mbedtls_ecp_keypair_free( eck );
767 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
768 }
769
770 p += len;
771
772 pubkey_done = 0;
773 if( p != end )
774 {
775 /*
776 * Is 'parameters' present?
777 */
778 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
779 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
780 {
781 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
782 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
783 {
784 mbedtls_ecp_keypair_free( eck );
785 return( ret );
786 }
787 }
788 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
789 {
790 mbedtls_ecp_keypair_free( eck );
791 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
792 }
793
794 /*
795 * Is 'publickey' present? If not, or if we can't read it (eg because it
796 * is compressed), create it from the private key.
797 */
798 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
799 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
800 {
801 end2 = p + len;
802
803 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
804 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
805
806 if( p + len != end2 )
807 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
808 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
809
810 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
811 pubkey_done = 1;
812 else
813 {
814 /*
815 * The only acceptable failure mode of pk_get_ecpubkey() above
816 * is if the point format is not recognized.
817 */
818 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
819 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
820 }
821 }
822 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
823 {
824 mbedtls_ecp_keypair_free( eck );
825 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
826 }
827 }
828
829 if( ! pubkey_done &&
830 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
831 NULL, NULL ) ) != 0 )
832 {
833 mbedtls_ecp_keypair_free( eck );
834 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
835 }
836
837 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
838 {
839 mbedtls_ecp_keypair_free( eck );
840 return( ret );
841 }
842
843 return( 0 );
844 }
845 #endif /* MBEDTLS_ECP_C */
846
847 /*
848 * Parse an unencrypted PKCS#8 encoded private key
849 */
850 static int pk_parse_key_pkcs8_unencrypted_der(
851 mbedtls_pk_context *pk,
852 const unsigned char* key,
853 size_t keylen )
854 {
855 int ret, version;
856 size_t len;
857 mbedtls_asn1_buf params;
858 unsigned char *p = (unsigned char *) key;
859 unsigned char *end = p + keylen;
860 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
861 const mbedtls_pk_info_t *pk_info;
862
863 /*
864 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
865 *
866 * PrivateKeyInfo ::= SEQUENCE {
867 * version Version,
868 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
869 * privateKey PrivateKey,
870 * attributes [0] IMPLICIT Attributes OPTIONAL }
871 *
872 * Version ::= INTEGER
873 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
874 * PrivateKey ::= OCTET STRING
875 *
876 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
877 */
878
879 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
880 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
881 {
882 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
883 }
884
885 end = p + len;
886
887 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
888 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
889
890 if( version != 0 )
891 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
892
893 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
894 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
895
896 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
897 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
898
899 if( len < 1 )
900 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
901 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
902
903 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
904 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
905
906 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
907 return( ret );
908
909 #if defined(MBEDTLS_RSA_C)
910 if( pk_alg == MBEDTLS_PK_RSA )
911 {
912 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
913 {
914 mbedtls_pk_free( pk );
915 return( ret );
916 }
917 } else
918 #endif /* MBEDTLS_RSA_C */
919 #if defined(MBEDTLS_ECP_C)
920 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
921 {
922 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
923 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
924 {
925 mbedtls_pk_free( pk );
926 return( ret );
927 }
928 } else
929 #endif /* MBEDTLS_ECP_C */
930 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
931
932 return( 0 );
933 }
934
935 /*
936 * Parse an encrypted PKCS#8 encoded private key
937 */
938 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
939 static int pk_parse_key_pkcs8_encrypted_der(
940 mbedtls_pk_context *pk,
941 const unsigned char *key, size_t keylen,
942 const unsigned char *pwd, size_t pwdlen )
943 {
944 int ret, decrypted = 0;
945 size_t len;
946 unsigned char buf[2048];
947 unsigned char *p, *end;
948 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
949 #if defined(MBEDTLS_PKCS12_C)
950 mbedtls_cipher_type_t cipher_alg;
951 mbedtls_md_type_t md_alg;
952 #endif
953
954 memset( buf, 0, sizeof( buf ) );
955
956 p = (unsigned char *) key;
957 end = p + keylen;
958
959 if( pwdlen == 0 )
960 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
961
962 /*
963 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
964 *
965 * EncryptedPrivateKeyInfo ::= SEQUENCE {
966 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
967 * encryptedData EncryptedData
968 * }
969 *
970 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
971 *
972 * EncryptedData ::= OCTET STRING
973 *
974 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
975 */
976 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
977 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
978 {
979 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
980 }
981
982 end = p + len;
983
984 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
985 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
986
987 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
988 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
989
990 if( len > sizeof( buf ) )
991 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
992
993 /*
994 * Decrypt EncryptedData with appropriate PDE
995 */
996 #if defined(MBEDTLS_PKCS12_C)
997 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
998 {
999 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
1000 cipher_alg, md_alg,
1001 pwd, pwdlen, p, len, buf ) ) != 0 )
1002 {
1003 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1004 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1005
1006 return( ret );
1007 }
1008
1009 decrypted = 1;
1010 }
1011 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
1012 {
1013 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1014 MBEDTLS_PKCS12_PBE_DECRYPT,
1015 pwd, pwdlen,
1016 p, len, buf ) ) != 0 )
1017 {
1018 return( ret );
1019 }
1020
1021 // Best guess for password mismatch when using RC4. If first tag is
1022 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
1023 //
1024 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1025 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1026
1027 decrypted = 1;
1028 }
1029 else
1030 #endif /* MBEDTLS_PKCS12_C */
1031 #if defined(MBEDTLS_PKCS5_C)
1032 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
1033 {
1034 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
1035 p, len, buf ) ) != 0 )
1036 {
1037 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1038 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1039
1040 return( ret );
1041 }
1042
1043 decrypted = 1;
1044 }
1045 else
1046 #endif /* MBEDTLS_PKCS5_C */
1047 {
1048 ((void) pwd);
1049 }
1050
1051 if( decrypted == 0 )
1052 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
1053
1054 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1055 }
1056 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1057
1058 /*
1059 * Parse a private key
1060 */
1061 int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
1062 const unsigned char *key, size_t keylen,
1063 const unsigned char *pwd, size_t pwdlen )
1064 {
1065 int ret;
1066 const mbedtls_pk_info_t *pk_info;
1067
1068 #if defined(MBEDTLS_PEM_PARSE_C)
1069 size_t len;
1070 mbedtls_pem_context pem;
1071
1072 mbedtls_pem_init( &pem );
1073
1074 #if defined(MBEDTLS_RSA_C)
1075 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1076 if( keylen == 0 || key[keylen - 1] != '\0' )
1077 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1078 else
1079 ret = mbedtls_pem_read_buffer( &pem,
1080 "-----BEGIN RSA PRIVATE KEY-----",
1081 "-----END RSA PRIVATE KEY-----",
1082 key, pwd, pwdlen, &len );
1083
1084 if( ret == 0 )
1085 {
1086 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1087 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1088
1089 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1090 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
1091 pem.buf, pem.buflen ) ) != 0 )
1092 {
1093 mbedtls_pk_free( pk );
1094 }
1095
1096 mbedtls_pem_free( &pem );
1097 return( ret );
1098 }
1099 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1100 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1101 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1102 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1103 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1104 return( ret );
1105 #endif /* MBEDTLS_RSA_C */
1106
1107 #if defined(MBEDTLS_ECP_C)
1108 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1109 if( keylen == 0 || key[keylen - 1] != '\0' )
1110 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1111 else
1112 ret = mbedtls_pem_read_buffer( &pem,
1113 "-----BEGIN EC PRIVATE KEY-----",
1114 "-----END EC PRIVATE KEY-----",
1115 key, pwd, pwdlen, &len );
1116 if( ret == 0 )
1117 {
1118 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == NULL )
1119 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1120
1121 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1122 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1123 pem.buf, pem.buflen ) ) != 0 )
1124 {
1125 mbedtls_pk_free( pk );
1126 }
1127
1128 mbedtls_pem_free( &pem );
1129 return( ret );
1130 }
1131 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1132 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1133 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1134 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1135 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1136 return( ret );
1137 #endif /* MBEDTLS_ECP_C */
1138
1139 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1140 if( keylen == 0 || key[keylen - 1] != '\0' )
1141 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1142 else
1143 ret = mbedtls_pem_read_buffer( &pem,
1144 "-----BEGIN PRIVATE KEY-----",
1145 "-----END PRIVATE KEY-----",
1146 key, NULL, 0, &len );
1147 if( ret == 0 )
1148 {
1149 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1150 pem.buf, pem.buflen ) ) != 0 )
1151 {
1152 mbedtls_pk_free( pk );
1153 }
1154
1155 mbedtls_pem_free( &pem );
1156 return( ret );
1157 }
1158 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1159 return( ret );
1160
1161 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1162 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1163 if( keylen == 0 || key[keylen - 1] != '\0' )
1164 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1165 else
1166 ret = mbedtls_pem_read_buffer( &pem,
1167 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1168 "-----END ENCRYPTED PRIVATE KEY-----",
1169 key, NULL, 0, &len );
1170 if( ret == 0 )
1171 {
1172 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1173 pem.buf, pem.buflen,
1174 pwd, pwdlen ) ) != 0 )
1175 {
1176 mbedtls_pk_free( pk );
1177 }
1178
1179 mbedtls_pem_free( &pem );
1180 return( ret );
1181 }
1182 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1183 return( ret );
1184 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1185 #else
1186 ((void) ret);
1187 ((void) pwd);
1188 ((void) pwdlen);
1189 #endif /* MBEDTLS_PEM_PARSE_C */
1190
1191 /*
1192 * At this point we only know it's not a PEM formatted key. Could be any
1193 * of the known DER encoded private key formats
1194 *
1195 * We try the different DER format parsers to see if one passes without
1196 * error
1197 */
1198 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
1199 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, key, keylen,
1200 pwd, pwdlen ) ) == 0 )
1201 {
1202 return( 0 );
1203 }
1204
1205 mbedtls_pk_free( pk );
1206
1207 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
1208 {
1209 return( ret );
1210 }
1211 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
1212
1213 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1214 return( 0 );
1215
1216 mbedtls_pk_free( pk );
1217
1218 #if defined(MBEDTLS_RSA_C)
1219 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1220 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1221
1222 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1223 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) ) == 0 )
1224 {
1225 return( 0 );
1226 }
1227
1228 mbedtls_pk_free( pk );
1229 #endif /* MBEDTLS_RSA_C */
1230
1231 #if defined(MBEDTLS_ECP_C)
1232 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == NULL )
1233 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1234
1235 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
1236 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), key, keylen ) ) == 0 )
1237 {
1238 return( 0 );
1239 }
1240
1241 mbedtls_pk_free( pk );
1242 #endif /* MBEDTLS_ECP_C */
1243
1244 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1245 }
1246
1247 /*
1248 * Parse a public key
1249 */
1250 int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
1251 const unsigned char *key, size_t keylen )
1252 {
1253 int ret;
1254 unsigned char *p;
1255 #if defined(MBEDTLS_PEM_PARSE_C)
1256 size_t len;
1257 mbedtls_pem_context pem;
1258
1259 mbedtls_pem_init( &pem );
1260
1261 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
1262 if( keylen == 0 || key[keylen - 1] != '\0' )
1263 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1264 else
1265 ret = mbedtls_pem_read_buffer( &pem,
1266 "-----BEGIN PUBLIC KEY-----",
1267 "-----END PUBLIC KEY-----",
1268 key, NULL, 0, &len );
1269
1270 if( ret == 0 )
1271 {
1272 /*
1273 * Was PEM encoded
1274 */
1275 key = pem.buf;
1276 keylen = pem.buflen;
1277 }
1278 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1279 {
1280 mbedtls_pem_free( &pem );
1281 return( ret );
1282 }
1283 #endif /* MBEDTLS_PEM_PARSE_C */
1284 p = (unsigned char *) key;
1285
1286 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
1287
1288 #if defined(MBEDTLS_PEM_PARSE_C)
1289 mbedtls_pem_free( &pem );
1290 #endif
1291
1292 return( ret );
1293 }
1294
1295 #endif /* MBEDTLS_PK_PARSE_C */