blob: ac631d93d57040da53a012184e39913a921f851b [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 Lloyd60239752020-01-27 17:53:36 -0500772#if !defined(MBEDTLS_RSA_NO_CRT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500773 /*
774 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
775 * that they can be easily recomputed from D, P and Q. However by
776 * parsing them from the PKCS1 structure it is possible to avoid
777 * recalculating them which both reduces the overhead of loading
778 * RSA private keys into memory and also avoids side channels which
779 * can arise when computing those values, since all of D, P, and Q
780 * are secret. See https://eprint.iacr.org/2020/055 for a
781 * description of one such attack.
782 */
783
Jack Lloyd80cc8112020-01-22 17:34:29 -0500784 /* Import DP */
785 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
786 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
787 ( ret = mbedtls_mpi_read_binary( &rsa->DP, p, len ) ) != 0 )
788 goto cleanup;
789 p += len;
790
791 /* Import DQ */
792 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
793 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
794 ( ret = mbedtls_mpi_read_binary( &rsa->DQ, p, len ) ) != 0 )
795 goto cleanup;
796 p += len;
797
798 /* Import QP */
799 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
800 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
801 ( ret = mbedtls_mpi_read_binary( &rsa->QP, p, len ) ) != 0 )
802 goto cleanup;
803 p += len;
Jack Lloyd60239752020-01-27 17:53:36 -0500804#else
805 /* Verify existance of the CRT params */
806 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
807 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
808 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
809 goto cleanup;
810#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500811
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100812 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100813 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814 goto cleanup;
815
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816 if( p != end )
817 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
819 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820 }
821
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100822cleanup:
823
Hanno Beckerefa14e82017-10-11 19:45:19 +0100824 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100825
826 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100828 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100829 if( ( ret & 0xff80 ) == 0 )
830 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
831 else
832 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835 }
836
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100837 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200838}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200839#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842/*
843 * Parse a SEC1 encoded private EC key
844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846 const unsigned char *key,
847 size_t keylen )
848{
Janos Follath24eed8d2019-11-22 13:21:35 +0000849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100850 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 unsigned char *p = (unsigned char *) key;
854 unsigned char *end = p + keylen;
855 unsigned char *end2;
856
857 /*
858 * RFC 5915, or SEC1 Appendix C.4
859 *
860 * ECPrivateKey ::= SEQUENCE {
861 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
862 * privateKey OCTET STRING,
863 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
864 * publicKey [1] BIT STRING OPTIONAL
865 * }
866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200867 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
868 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871 }
872
873 end = p + len;
874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
876 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877
878 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
882 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_ecp_keypair_free( eck );
887 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888 }
889
890 p += len;
891
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200892 pubkey_done = 0;
893 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200895 /*
896 * Is 'parameters' present?
897 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200898 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
899 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200900 {
901 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
902 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
903 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200904 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200905 return( ret );
906 }
907 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200908 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100912 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800913 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200914
Jethro Beekmand2df9362018-02-16 13:11:04 -0800915 if( p != end )
916 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200917 /*
918 * Is 'publickey' present? If not, or if we can't read it (eg because it
919 * is compressed), create it from the private key.
920 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200921 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
922 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200923 {
924 end2 = p + len;
925
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200926 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
927 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200928
929 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200930 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
931 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200932
933 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
934 pubkey_done = 1;
935 else
936 {
937 /*
938 * The only acceptable failure mode of pk_get_ecpubkey() above
939 * is if the point format is not recognized.
940 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200941 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
942 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200943 }
944 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200945 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200946 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200947 mbedtls_ecp_keypair_free( eck );
948 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200949 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100951
952 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100954 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_ecp_keypair_free( eck );
957 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200958 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963 return( ret );
964 }
965
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200966 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200969
970/*
971 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100972 *
973 * Notes:
974 *
975 * - This function does not own the key buffer. It is the
976 * responsibility of the caller to take care of zeroizing
977 * and freeing it after use.
978 *
979 * - The function is responsible for freeing the provided
980 * PK context on failure.
981 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982 */
983static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985 const unsigned char* key,
986 size_t keylen )
987{
988 int ret, version;
989 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991 unsigned char *p = (unsigned char *) key;
992 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
994 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995
996 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +0100997 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 *
999 * PrivateKeyInfo ::= SEQUENCE {
1000 * version Version,
1001 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1002 * privateKey PrivateKey,
1003 * attributes [0] IMPLICIT Attributes OPTIONAL }
1004 *
1005 * Version ::= INTEGER
1006 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1007 * PrivateKey ::= OCTET STRING
1008 *
1009 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1010 */
1011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1013 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 }
1017
1018 end = p + len;
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1021 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022
1023 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025
1026 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1030 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031
1032 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1034 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1037 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001039 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040 return( ret );
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_RSA_C)
1043 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048 return( ret );
1049 }
1050 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_RSA_C */
1052#if defined(MBEDTLS_ECP_C)
1053 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1056 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059 return( ret );
1060 }
1061 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_ECP_C */
1063 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001065 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066}
1067
1068/*
1069 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001070 *
1071 * To save space, the decryption happens in-place on the given key buffer.
1072 * Also, while this function may modify the keybuffer, it doesn't own it,
1073 * and instead it is the responsibility of the caller to zeroize and properly
1074 * free it after use.
1075 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001080 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081 const unsigned char *pwd, size_t pwdlen )
1082{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001083 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001085 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1088#if defined(MBEDTLS_PKCS12_C)
1089 mbedtls_cipher_type_t cipher_alg;
1090 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091#endif
1092
Hanno Becker2aa80a72017-09-07 15:28:45 +01001093 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094 end = p + keylen;
1095
1096 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098
1099 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001100 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001101 *
1102 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1103 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1104 * encryptedData EncryptedData
1105 * }
1106 *
1107 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1108 *
1109 * EncryptedData ::= OCTET STRING
1110 *
1111 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001112 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1115 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 }
1119
1120 end = p + len;
1121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1123 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1126 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127
Hanno Beckerfab35692017-08-25 13:38:26 +01001128 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129
1130 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001131 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133#if defined(MBEDTLS_PKCS12_C)
1134 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 cipher_alg, md_alg,
1138 pwd, pwdlen, p, len, buf ) ) != 0 )
1139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1141 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142
1143 return( ret );
1144 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001145
1146 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1151 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152 pwd, pwdlen,
1153 p, len, buf ) ) != 0 )
1154 {
1155 return( ret );
1156 }
1157
1158 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1162 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001163
1164 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 }
1166 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#endif /* MBEDTLS_PKCS12_C */
1168#if defined(MBEDTLS_PKCS5_C)
1169 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 p, len, buf ) ) != 0 )
1173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1175 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
1177 return( ret );
1178 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001179
1180 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 }
1182 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001184 {
1185 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001186 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001188 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001190
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194
1195/*
1196 * Parse a private key
1197 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 const unsigned char *key, size_t keylen,
1200 const unsigned char *pwd, size_t pwdlen )
1201{
Janos Follath24eed8d2019-11-22 13:21:35 +00001202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001207#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001209 PK_VALIDATE_RET( pk != NULL );
1210 if( keylen == 0 )
1211 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1212 PK_VALIDATE_RET( key != NULL );
1213
1214#if defined(MBEDTLS_PEM_PARSE_C)
1215 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001218 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001219 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001220 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1221 else
1222 ret = mbedtls_pem_read_buffer( &pem,
1223 "-----BEGIN RSA PRIVATE KEY-----",
1224 "-----END RSA PRIVATE KEY-----",
1225 key, pwd, pwdlen, &len );
1226
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227 if( ret == 0 )
1228 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001229 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001230 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232 pem.buf, pem.buflen ) ) != 0 )
1233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235 }
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238 return( ret );
1239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1241 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1242 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1243 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1244 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001249 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001250 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001251 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1252 else
1253 ret = mbedtls_pem_read_buffer( &pem,
1254 "-----BEGIN EC PRIVATE KEY-----",
1255 "-----END EC PRIVATE KEY-----",
1256 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257 if( ret == 0 )
1258 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001259 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260
Hanno Beckerfab35692017-08-25 13:38:26 +01001261 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263 pem.buf, pem.buflen ) ) != 0 )
1264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266 }
1267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269 return( ret );
1270 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1272 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1273 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1274 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1275 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001279 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001280 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001281 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1282 else
1283 ret = mbedtls_pem_read_buffer( &pem,
1284 "-----BEGIN PRIVATE KEY-----",
1285 "-----END PRIVATE KEY-----",
1286 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287 if( ret == 0 )
1288 {
1289 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1290 pem.buf, pem.buflen ) ) != 0 )
1291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 }
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296 return( ret );
1297 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 return( ret );
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001302 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001303 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001304 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1305 else
1306 ret = mbedtls_pem_read_buffer( &pem,
1307 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1308 "-----END ENCRYPTED PRIVATE KEY-----",
1309 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 if( ret == 0 )
1311 {
1312 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1313 pem.buf, pem.buflen,
1314 pwd, pwdlen ) ) != 0 )
1315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 }
1318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320 return( ret );
1321 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325#else
1326 ((void) pwd);
1327 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329
1330 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001331 * At this point we only know it's not a PEM formatted key. Could be any
1332 * of the known DER encoded private key formats
1333 *
1334 * We try the different DER format parsers to see if one passes without
1335 * error
1336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001339 unsigned char *key_copy;
1340
1341 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1342 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1343
1344 memcpy( key_copy, key, keylen );
1345
1346 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1347 pwd, pwdlen );
1348
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001349 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001350 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 }
1352
Hanno Beckerfab35692017-08-25 13:38:26 +01001353 if( ret == 0 )
1354 return( 0 );
1355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001357 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 {
1361 return( ret );
1362 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
1365 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1366 return( 0 );
1367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001369 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372
Hanno Becker9be19262017-09-08 12:39:44 +01001373 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001374 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1375 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376 {
1377 return( 0 );
1378 }
1379
Hanno Becker780f0a42018-10-10 11:23:33 +01001380 mbedtls_pk_free( pk );
1381 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001385 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001386 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1387 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1388 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389 {
1390 return( 0 );
1391 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001392 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394
Hanno Becker780f0a42018-10-10 11:23:33 +01001395 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1396 * it is ok to leave the PK context initialized but not
1397 * freed: It is the caller's responsibility to call pk_init()
1398 * before calling this function, and to call pk_free()
1399 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1400 * isn't, this leads to mbedtls_pk_free() being called
1401 * twice, once here and once by the caller, but this is
1402 * also ok and in line with the mbedtls_pk_free() calls
1403 * on failed PEM parsing attempts. */
1404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406}
1407
1408/*
1409 * Parse a public key
1410 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 const unsigned char *key, size_t keylen )
1413{
Janos Follath24eed8d2019-11-22 13:21:35 +00001414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001416#if defined(MBEDTLS_RSA_C)
1417 const mbedtls_pk_info_t *pk_info;
1418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001422#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001424 PK_VALIDATE_RET( ctx != NULL );
1425 if( keylen == 0 )
1426 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1427 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1428
1429#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001431#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001432 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001433 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1435 else
1436 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001437 "-----BEGIN RSA PUBLIC KEY-----",
1438 "-----END RSA PUBLIC KEY-----",
1439 key, NULL, 0, &len );
1440
1441 if( ret == 0 )
1442 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001443 p = pem.buf;
1444 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1445 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001446
Ron Eldor84df1ae2017-10-16 17:11:52 +03001447 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1448 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001449
Ron Eldor84df1ae2017-10-16 17:11:52 +03001450 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1451 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001452
Ron Eldord0c56de2017-10-10 17:03:08 +03001453 mbedtls_pem_free( &pem );
1454 return( ret );
1455 }
1456 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1457 {
1458 mbedtls_pem_free( &pem );
1459 return( ret );
1460 }
1461#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001462
1463 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001464 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001465 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1466 else
1467 ret = mbedtls_pem_read_buffer( &pem,
1468 "-----BEGIN PUBLIC KEY-----",
1469 "-----END PUBLIC KEY-----",
1470 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471
1472 if( ret == 0 )
1473 {
1474 /*
1475 * Was PEM encoded
1476 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001477 p = pem.buf;
1478
1479 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1480 mbedtls_pem_free( &pem );
1481 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486 return( ret );
1487 }
Ron Eldor5472d432017-10-17 09:49:00 +03001488 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001490
1491#if defined(MBEDTLS_RSA_C)
1492 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1493 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1494
1495 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1496 return( ret );
1497
Ron Eldor9566ff72018-02-07 18:59:41 +02001498 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001499 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001500 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001501 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001502 return( ret );
1503 }
1504 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001505 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001506 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001507 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001508 }
1509#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510 p = (unsigned char *) key;
1511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001513
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514 return( ret );
1515}
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#endif /* MBEDTLS_PK_PARSE_C */