blob: 2311986f7fa24d87d04f94eb62b65d5d95e4322e [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker1a7550a2013-09-15 13:01:22 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk.h"
31#include "mbedtls/asn1.h"
32#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000034#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020035
Rich Evans00ab4702015-02-06 13:43:58 +000036#include <string.h>
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020049#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020052#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020055#endif
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020059#else
60#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020061#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020063#endif
64
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050065/* Parameter validation macros based on platform_util.h */
66#define PK_VALIDATE_RET( cond ) \
67 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
68#define PK_VALIDATE( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE( cond )
70
Gilles Peskine832f3492017-11-30 11:42:12 +010071#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020072/*
73 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020074 *
75 * The file is expected to contain either PEM or DER encoded data.
76 * A terminating null byte is always appended. It is included in the announced
77 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020080{
81 FILE *f;
82 long size;
83
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050084 PK_VALIDATE_RET( path != NULL );
85 PK_VALIDATE_RET( buf != NULL );
86 PK_VALIDATE_RET( n != NULL );
87
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020090
91 fseek( f, 0, SEEK_END );
92 if( ( size = ftell( f ) ) == -1 )
93 {
94 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020096 }
97 fseek( f, 0, SEEK_SET );
98
99 *n = (size_t) size;
100
101 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200102 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 {
104 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200105 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106 }
107
108 if( fread( *buf, 1, *n, f ) != *n )
109 {
110 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100111
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500112 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116 }
117
118 fclose( f );
119
120 (*buf)[*n] = '\0';
121
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200122 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
123 ++*n;
124
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125 return( 0 );
126}
127
128/*
129 * Load and parse a private key
130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132 const char *path, const char *pwd )
133{
Janos Follath24eed8d2019-11-22 13:21:35 +0000134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135 size_t n;
136 unsigned char *buf;
137
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 PK_VALIDATE_RET( ctx != NULL );
139 PK_VALIDATE_RET( path != NULL );
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142 return( ret );
143
144 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148 (const unsigned char *) pwd, strlen( pwd ) );
149
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500150 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152
153 return( ret );
154}
155
156/*
157 * Load and parse a public key
158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160{
Janos Follath24eed8d2019-11-22 13:21:35 +0000161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162 size_t n;
163 unsigned char *buf;
164
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500165 PK_VALIDATE_RET( ctx != NULL );
166 PK_VALIDATE_RET( path != NULL );
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169 return( ret );
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500173 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175
176 return( ret );
177}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_ECP_C)
181/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182 *
183 * ECParameters ::= CHOICE {
184 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100185 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187 * }
188 */
189static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200191{
Janos Follath24eed8d2019-11-22 13:21:35 +0000192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200193
Sanne Woudab2b29d52017-08-21 15:58:12 +0100194 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100195 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
196 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100197
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( params->tag != MBEDTLS_ASN1_OID
201#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
202 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100203#endif
204 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
207 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100208 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214
215 params->p = *p;
216 *p += params->len;
217
218 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
220 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200221
222 return( 0 );
223}
224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200226/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100227 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
228 * WARNING: the resulting group should only be used with
229 * pk_group_id_from_specified(), since its base point may not be set correctly
230 * if it was encoded compressed.
231 *
232 * SpecifiedECDomain ::= SEQUENCE {
233 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
234 * fieldID FieldID {{FieldTypes}},
235 * curve Curve,
236 * base ECPoint,
237 * order INTEGER,
238 * cofactor INTEGER OPTIONAL,
239 * hash HashAlgorithm OPTIONAL,
240 * ...
241 * }
242 *
243 * We only support prime-field as field type, and ignore hash and cofactor.
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100246{
Janos Follath24eed8d2019-11-22 13:21:35 +0000247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248 unsigned char *p = params->p;
249 const unsigned char * const end = params->p + params->len;
250 const unsigned char *end_field, *end_curve;
251 size_t len;
252 int ver;
253
254 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
256 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100257
258 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100260
261 /*
262 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
263 * fieldType FIELD-ID.&id({IOSet}),
264 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
265 * }
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
268 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100269 return( ret );
270
271 end_field = p + len;
272
273 /*
274 * FIELD-ID ::= TYPE-IDENTIFIER
275 * FieldTypes FIELD-ID ::= {
276 * { Prime-p IDENTIFIED BY prime-field } |
277 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
278 * }
279 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
280 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100282 return( ret );
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
285 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288 }
289
290 p += len;
291
292 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
294 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100295
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200296 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100297
298 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
300 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100301
302 /*
303 * Curve ::= SEQUENCE {
304 * a FieldElement,
305 * b FieldElement,
306 * seed BIT STRING OPTIONAL
307 * -- Shall be present if used in SpecifiedECDomain
308 * -- with version equal to ecdpVer2 or ecdpVer3
309 * }
310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
312 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100313 return( ret );
314
315 end_curve = p + len;
316
317 /*
318 * FieldElement ::= OCTET STRING
319 * containing an integer in the case of a prime field
320 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
322 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325 }
326
327 p += len;
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
330 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100333 }
334
335 p += len;
336
337 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100339 p += len;
340
341 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
343 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100344
345 /*
346 * ECPoint ::= OCTET STRING
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
349 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100352 ( const unsigned char *) p, len ) ) != 0 )
353 {
354 /*
355 * If we can't read the point because it's compressed, cheat by
356 * reading only the X coordinate and the parity bit of Y.
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100359 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 len != mbedtls_mpi_size( &grp->P ) + 1 ||
361 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
362 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
363 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100366 }
367 }
368
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100369 p += len;
370
371 /*
372 * order INTEGER
373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
375 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200377 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100378
379 /*
380 * Allow optional elements by purposefully not enforcing p == end here.
381 */
382
383 return( 0 );
384}
385
386/*
387 * Find the group id associated with an (almost filled) group as generated by
388 * pk_group_from_specified(), or return an error if unknown.
389 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100392 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_ecp_group ref;
394 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100399 {
400 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200402 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403
404 /* Compare to the group we were given, starting with easy tests */
405 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
407 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
408 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
409 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
410 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
411 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 {
415 break;
416 }
417
418 }
419
420cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
423 *grp_id = *id;
424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
426 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100427
428 return( ret );
429}
430
431/*
432 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
433 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
435 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100436{
Janos Follath24eed8d2019-11-22 13:21:35 +0000437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441
442 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
443 goto cleanup;
444
445 ret = pk_group_id_from_group( &grp, grp_id );
446
447cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100449
450 return( ret );
451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453
454/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200455 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456 *
457 * ECParameters ::= CHOICE {
458 * namedCurve OBJECT IDENTIFIER
459 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
460 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200463{
Janos Follath24eed8d2019-11-22 13:21:35 +0000464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
470 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100471 }
472 else
473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100475 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
476 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100477#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100479#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100480 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481
482 /*
483 * grp may already be initilialized; if so, make sure IDs match
484 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
486 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200488 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200489 return( ret );
490
491 return( 0 );
492}
493
494/*
495 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100496 *
497 * The caller is responsible for clearing the structure upon failure if
498 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200500 */
501static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200503{
Janos Follath24eed8d2019-11-22 13:21:35 +0000504 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100507 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510 }
511
512 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514 */
515 *p = (unsigned char *) end;
516
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100517 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200522/*
523 * RSAPublicKey ::= SEQUENCE {
524 * modulus INTEGER, -- n
525 * publicExponent INTEGER -- e
526 * }
527 */
528static int pk_get_rsapubkey( unsigned char **p,
529 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531{
Janos Follath24eed8d2019-11-22 13:21:35 +0000532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533 size_t len;
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
536 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
537 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200538
539 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
541 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200542
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100543 /* Import N */
544 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200546
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100547 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
548 NULL, 0, NULL, 0 ) ) != 0 )
549 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
550
551 *p += len;
552
553 /* Import E */
554 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
555 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
556
557 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
558 NULL, 0, *p, len ) ) != 0 )
559 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
560
561 *p += len;
562
Hanno Becker895c5ab2018-01-05 08:08:09 +0000563 if( mbedtls_rsa_complete( rsa ) != 0 ||
564 mbedtls_rsa_check_pubkey( rsa ) != 0 )
565 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100566 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000567 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100568
Paul Bakker1a7550a2013-09-15 13:01:22 +0200569 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
571 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200572
Paul Bakker1a7550a2013-09-15 13:01:22 +0200573 return( 0 );
574}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200576
577/* Get a PK algorithm identifier
578 *
579 * AlgorithmIdentifier ::= SEQUENCE {
580 * algorithm OBJECT IDENTIFIER,
581 * parameters ANY DEFINED BY algorithm OPTIONAL }
582 */
583static int pk_get_pk_alg( unsigned char **p,
584 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200586{
Janos Follath24eed8d2019-11-22 13:21:35 +0000587 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
593 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
596 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200597
598 /*
599 * No parameters with RSA (only for EC)
600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 if( *pk_alg == MBEDTLS_PK_RSA &&
602 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200603 params->len != 0 ) )
604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200606 }
607
608 return( 0 );
609}
610
611/*
612 * SubjectPublicKeyInfo ::= SEQUENCE {
613 * algorithm AlgorithmIdentifier,
614 * subjectPublicKey BIT STRING }
615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
617 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200618{
Janos Follath24eed8d2019-11-22 13:21:35 +0000619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 mbedtls_asn1_buf alg_params;
622 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
623 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200624
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500625 PK_VALIDATE_RET( p != NULL );
626 PK_VALIDATE_RET( *p != NULL );
627 PK_VALIDATE_RET( end != NULL );
628 PK_VALIDATE_RET( pk != NULL );
629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
631 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200634 }
635
636 end = *p + len;
637
638 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
639 return( ret );
640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
642 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643
644 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
646 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
649 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200651 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652 return( ret );
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#if defined(MBEDTLS_RSA_C)
655 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_RSA_C */
660#if defined(MBEDTLS_ECP_C)
661 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667#endif /* MBEDTLS_ECP_C */
668 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200669
670 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
672 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200673
674 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676
677 return( ret );
678}
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200681/*
682 * Parse a PKCS#1 encoded private RSA key
683 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685 const unsigned char *key,
686 size_t keylen )
687{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100688 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200689 size_t len;
690 unsigned char *p, *end;
691
Hanno Beckerefa14e82017-10-11 19:45:19 +0100692 mbedtls_mpi T;
693 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100694
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695 p = (unsigned char *) key;
696 end = p + keylen;
697
698 /*
699 * This function parses the RSAPrivateKey (PKCS#1)
700 *
701 * RSAPrivateKey ::= SEQUENCE {
702 * version Version,
703 * modulus INTEGER, -- n
704 * publicExponent INTEGER, -- e
705 * privateExponent INTEGER, -- d
706 * prime1 INTEGER, -- p
707 * prime2 INTEGER, -- q
708 * exponent1 INTEGER, -- d mod (p-1)
709 * exponent2 INTEGER, -- d mod (q-1)
710 * coefficient INTEGER, -- (inverse of q) mod p
711 * otherPrimeInfos OtherPrimeInfos OPTIONAL
712 * }
713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
715 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200718 }
719
720 end = p + len;
721
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100722 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 }
726
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100727 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200730 }
731
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100732 /* Import N */
733 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
734 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
735 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
736 NULL, 0, NULL, 0 ) ) != 0 )
737 goto cleanup;
738 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100740 /* Import E */
741 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
742 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
743 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
744 NULL, 0, p, len ) ) != 0 )
745 goto cleanup;
746 p += len;
747
748 /* Import D */
749 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
750 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
751 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
752 p, len, NULL, 0 ) ) != 0 )
753 goto cleanup;
754 p += len;
755
756 /* Import P */
757 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
758 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
759 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
760 NULL, 0, NULL, 0 ) ) != 0 )
761 goto cleanup;
762 p += len;
763
764 /* Import Q */
765 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
766 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
767 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
768 NULL, 0, NULL, 0 ) ) != 0 )
769 goto cleanup;
770 p += len;
771
Jack Lloyd80cc8112020-01-22 17:34:29 -0500772 /* Import DP */
773 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
774 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
775 ( ret = mbedtls_mpi_read_binary( &rsa->DP, p, len ) ) != 0 )
776 goto cleanup;
777 p += len;
778
779 /* Import DQ */
780 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
781 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
782 ( ret = mbedtls_mpi_read_binary( &rsa->DQ, p, len ) ) != 0 )
783 goto cleanup;
784 p += len;
785
786 /* Import QP */
787 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
788 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
789 ( ret = mbedtls_mpi_read_binary( &rsa->QP, p, len ) ) != 0 )
790 goto cleanup;
791 p += len;
792
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100793 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100794 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100795 goto cleanup;
796
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797 if( p != end )
798 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100799 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
800 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801 }
802
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100803cleanup:
804
Hanno Beckerefa14e82017-10-11 19:45:19 +0100805 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100806
807 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200808 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100809 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100810 if( ( ret & 0xff80 ) == 0 )
811 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
812 else
813 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200815 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816 }
817
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200820#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200823/*
824 * Parse a SEC1 encoded private EC key
825 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827 const unsigned char *key,
828 size_t keylen )
829{
Janos Follath24eed8d2019-11-22 13:21:35 +0000830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100831 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200832 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834 unsigned char *p = (unsigned char *) key;
835 unsigned char *end = p + keylen;
836 unsigned char *end2;
837
838 /*
839 * RFC 5915, or SEC1 Appendix C.4
840 *
841 * ECPrivateKey ::= SEQUENCE {
842 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
843 * privateKey OCTET STRING,
844 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
845 * publicKey [1] BIT STRING OPTIONAL
846 * }
847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
849 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852 }
853
854 end = p + len;
855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
857 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858
859 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
863 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 mbedtls_ecp_keypair_free( eck );
868 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869 }
870
871 p += len;
872
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200873 pubkey_done = 0;
874 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200876 /*
877 * Is 'parameters' present?
878 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200879 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
880 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200881 {
882 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
883 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
884 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200885 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200886 return( ret );
887 }
888 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200889 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100893 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800894 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200895
Jethro Beekmand2df9362018-02-16 13:11:04 -0800896 if( p != end )
897 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200898 /*
899 * Is 'publickey' present? If not, or if we can't read it (eg because it
900 * is compressed), create it from the private key.
901 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200902 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
903 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200904 {
905 end2 = p + len;
906
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200907 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
908 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200909
910 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200911 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
912 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200913
914 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
915 pubkey_done = 1;
916 else
917 {
918 /*
919 * The only acceptable failure mode of pk_get_ecpubkey() above
920 * is if the point format is not recognized.
921 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200922 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
923 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200924 }
925 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200926 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200927 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200928 mbedtls_ecp_keypair_free( eck );
929 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200930 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100932
933 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100935 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 mbedtls_ecp_keypair_free( eck );
938 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200939 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944 return( ret );
945 }
946
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200947 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950
951/*
952 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100953 *
954 * Notes:
955 *
956 * - This function does not own the key buffer. It is the
957 * responsibility of the caller to take care of zeroizing
958 * and freeing it after use.
959 *
960 * - The function is responsible for freeing the provided
961 * PK context on failure.
962 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963 */
964static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966 const unsigned char* key,
967 size_t keylen )
968{
969 int ret, version;
970 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972 unsigned char *p = (unsigned char *) key;
973 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
975 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976
977 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +0100978 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979 *
980 * PrivateKeyInfo ::= SEQUENCE {
981 * version Version,
982 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
983 * privateKey PrivateKey,
984 * attributes [0] IMPLICIT Attributes OPTIONAL }
985 *
986 * Version ::= INTEGER
987 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
988 * PrivateKey ::= OCTET STRING
989 *
990 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
991 */
992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
994 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997 }
998
999 end = p + len;
1000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1002 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003
1004 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006
1007 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1011 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012
1013 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1015 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1018 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001020 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001021 return( ret );
1022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_RSA_C)
1024 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029 return( ret );
1030 }
1031 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#endif /* MBEDTLS_RSA_C */
1033#if defined(MBEDTLS_ECP_C)
1034 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1037 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040 return( ret );
1041 }
1042 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#endif /* MBEDTLS_ECP_C */
1044 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001046 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047}
1048
1049/*
1050 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001051 *
1052 * To save space, the decryption happens in-place on the given key buffer.
1053 * Also, while this function may modify the keybuffer, it doesn't own it,
1054 * and instead it is the responsibility of the caller to zeroize and properly
1055 * free it after use.
1056 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001061 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062 const unsigned char *pwd, size_t pwdlen )
1063{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001064 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001066 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1069#if defined(MBEDTLS_PKCS12_C)
1070 mbedtls_cipher_type_t cipher_alg;
1071 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001072#endif
1073
Hanno Becker2aa80a72017-09-07 15:28:45 +01001074 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 end = p + keylen;
1076
1077 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001079
1080 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001081 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082 *
1083 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1084 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1085 * encryptedData EncryptedData
1086 * }
1087 *
1088 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1089 *
1090 * EncryptedData ::= OCTET STRING
1091 *
1092 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001093 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1096 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001099 }
1100
1101 end = p + len;
1102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1104 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1107 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108
Hanno Beckerfab35692017-08-25 13:38:26 +01001109 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110
1111 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001112 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#if defined(MBEDTLS_PKCS12_C)
1115 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 cipher_alg, md_alg,
1119 pwd, pwdlen, p, len, buf ) ) != 0 )
1120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1122 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123
1124 return( ret );
1125 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001126
1127 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1132 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133 pwd, pwdlen,
1134 p, len, buf ) ) != 0 )
1135 {
1136 return( ret );
1137 }
1138
1139 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1143 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001144
1145 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146 }
1147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#endif /* MBEDTLS_PKCS12_C */
1149#if defined(MBEDTLS_PKCS5_C)
1150 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 p, len, buf ) ) != 0 )
1154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1156 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157
1158 return( ret );
1159 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001160
1161 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 }
1163 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001165 {
1166 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001167 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001169 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001171
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
1176/*
1177 * Parse a private key
1178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180 const unsigned char *key, size_t keylen,
1181 const unsigned char *pwd, size_t pwdlen )
1182{
Janos Follath24eed8d2019-11-22 13:21:35 +00001183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001188#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001190 PK_VALIDATE_RET( pk != NULL );
1191 if( keylen == 0 )
1192 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1193 PK_VALIDATE_RET( key != NULL );
1194
1195#if defined(MBEDTLS_PEM_PARSE_C)
1196 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001199 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001200 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001201 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1202 else
1203 ret = mbedtls_pem_read_buffer( &pem,
1204 "-----BEGIN RSA PRIVATE KEY-----",
1205 "-----END RSA PRIVATE KEY-----",
1206 key, pwd, pwdlen, &len );
1207
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208 if( ret == 0 )
1209 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001210 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001211 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 pem.buf, pem.buflen ) ) != 0 )
1214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216 }
1217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219 return( ret );
1220 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1222 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1223 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1224 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1225 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001230 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001231 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001232 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1233 else
1234 ret = mbedtls_pem_read_buffer( &pem,
1235 "-----BEGIN EC PRIVATE KEY-----",
1236 "-----END EC PRIVATE KEY-----",
1237 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238 if( ret == 0 )
1239 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001240 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001241
Hanno Beckerfab35692017-08-25 13:38:26 +01001242 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244 pem.buf, pem.buflen ) ) != 0 )
1245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 }
1248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 return( ret );
1251 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1253 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1254 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1255 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1256 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001260 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001261 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001262 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1263 else
1264 ret = mbedtls_pem_read_buffer( &pem,
1265 "-----BEGIN PRIVATE KEY-----",
1266 "-----END PRIVATE KEY-----",
1267 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268 if( ret == 0 )
1269 {
1270 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1271 pem.buf, pem.buflen ) ) != 0 )
1272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 }
1275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 return( ret );
1278 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280 return( ret );
1281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001283 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001284 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001285 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1286 else
1287 ret = mbedtls_pem_read_buffer( &pem,
1288 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1289 "-----END ENCRYPTED PRIVATE KEY-----",
1290 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291 if( ret == 0 )
1292 {
1293 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1294 pem.buf, pem.buflen,
1295 pwd, pwdlen ) ) != 0 )
1296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298 }
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301 return( ret );
1302 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306#else
1307 ((void) pwd);
1308 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310
1311 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001312 * At this point we only know it's not a PEM formatted key. Could be any
1313 * of the known DER encoded private key formats
1314 *
1315 * We try the different DER format parsers to see if one passes without
1316 * error
1317 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001320 unsigned char *key_copy;
1321
1322 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1323 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1324
1325 memcpy( key_copy, key, keylen );
1326
1327 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1328 pwd, pwdlen );
1329
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001330 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001331 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332 }
1333
Hanno Beckerfab35692017-08-25 13:38:26 +01001334 if( ret == 0 )
1335 return( 0 );
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001338 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341 {
1342 return( ret );
1343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
1346 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1347 return( 0 );
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001350 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Hanno Becker9be19262017-09-08 12:39:44 +01001354 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001355 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1356 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 {
1358 return( 0 );
1359 }
1360
Hanno Becker780f0a42018-10-10 11:23:33 +01001361 mbedtls_pk_free( pk );
1362 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001366 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001367 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1368 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1369 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370 {
1371 return( 0 );
1372 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001373 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375
Hanno Becker780f0a42018-10-10 11:23:33 +01001376 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1377 * it is ok to leave the PK context initialized but not
1378 * freed: It is the caller's responsibility to call pk_init()
1379 * before calling this function, and to call pk_free()
1380 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1381 * isn't, this leads to mbedtls_pk_free() being called
1382 * twice, once here and once by the caller, but this is
1383 * also ok and in line with the mbedtls_pk_free() calls
1384 * on failed PEM parsing attempts. */
1385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387}
1388
1389/*
1390 * Parse a public key
1391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 const unsigned char *key, size_t keylen )
1394{
Janos Follath24eed8d2019-11-22 13:21:35 +00001395 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001397#if defined(MBEDTLS_RSA_C)
1398 const mbedtls_pk_info_t *pk_info;
1399#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001403#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001405 PK_VALIDATE_RET( ctx != NULL );
1406 if( keylen == 0 )
1407 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1408 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1409
1410#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001412#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001413 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001414 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001415 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1416 else
1417 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001418 "-----BEGIN RSA PUBLIC KEY-----",
1419 "-----END RSA PUBLIC KEY-----",
1420 key, NULL, 0, &len );
1421
1422 if( ret == 0 )
1423 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001424 p = pem.buf;
1425 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1426 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001427
Ron Eldor84df1ae2017-10-16 17:11:52 +03001428 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1429 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001430
Ron Eldor84df1ae2017-10-16 17:11:52 +03001431 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1432 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001433
Ron Eldord0c56de2017-10-10 17:03:08 +03001434 mbedtls_pem_free( &pem );
1435 return( ret );
1436 }
1437 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1438 {
1439 mbedtls_pem_free( &pem );
1440 return( ret );
1441 }
1442#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001443
1444 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001445 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001446 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1447 else
1448 ret = mbedtls_pem_read_buffer( &pem,
1449 "-----BEGIN PUBLIC KEY-----",
1450 "-----END PUBLIC KEY-----",
1451 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452
1453 if( ret == 0 )
1454 {
1455 /*
1456 * Was PEM encoded
1457 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001458 p = pem.buf;
1459
1460 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1461 mbedtls_pem_free( &pem );
1462 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467 return( ret );
1468 }
Ron Eldor5472d432017-10-17 09:49:00 +03001469 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001471
1472#if defined(MBEDTLS_RSA_C)
1473 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1474 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1475
1476 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1477 return( ret );
1478
Ron Eldor9566ff72018-02-07 18:59:41 +02001479 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001480 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001481 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001482 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001483 return( ret );
1484 }
1485 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001486 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001487 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001488 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001489 }
1490#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491 p = (unsigned char *) key;
1492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495 return( ret );
1496}
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_PK_PARSE_C */