blob: 6170d6d0125e1ef004b3e0b4d4caecbf735e1911 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
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 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020049#endif
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020052
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050053/* Parameter validation macros based on platform_util.h */
54#define PK_VALIDATE_RET( cond ) \
55 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
56#define PK_VALIDATE( cond ) \
57 MBEDTLS_INTERNAL_VALIDATE( cond )
58
Gilles Peskine832f3492017-11-30 11:42:12 +010059#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020060/*
61 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020062 *
63 * The file is expected to contain either PEM or DER encoded data.
64 * A terminating null byte is always appended. It is included in the announced
65 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020068{
69 FILE *f;
70 long size;
71
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072 PK_VALIDATE_RET( path != NULL );
73 PK_VALIDATE_RET( buf != NULL );
74 PK_VALIDATE_RET( n != NULL );
75
Paul Bakker1a7550a2013-09-15 13:01:22 +020076 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020078
79 fseek( f, 0, SEEK_END );
80 if( ( size = ftell( f ) ) == -1 )
81 {
82 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020084 }
85 fseek( f, 0, SEEK_SET );
86
87 *n = (size_t) size;
88
89 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020090 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +020091 {
92 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020093 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +020094 }
95
96 if( fread( *buf, 1, *n, f ) != *n )
97 {
98 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010099
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500100 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200104 }
105
106 fclose( f );
107
108 (*buf)[*n] = '\0';
109
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200110 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
111 ++*n;
112
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113 return( 0 );
114}
115
116/*
117 * Load and parse a private key
118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120 const char *path, const char *pwd )
121{
Janos Follath24eed8d2019-11-22 13:21:35 +0000122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123 size_t n;
124 unsigned char *buf;
125
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500126 PK_VALIDATE_RET( ctx != NULL );
127 PK_VALIDATE_RET( path != NULL );
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130 return( ret );
131
132 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200136 (const unsigned char *) pwd, strlen( pwd ) );
137
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500138 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140
141 return( ret );
142}
143
144/*
145 * Load and parse a public key
146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148{
Janos Follath24eed8d2019-11-22 13:21:35 +0000149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150 size_t n;
151 unsigned char *buf;
152
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 PK_VALIDATE_RET( ctx != NULL );
154 PK_VALIDATE_RET( path != NULL );
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157 return( ret );
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500161 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163
164 return( ret );
165}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#if defined(MBEDTLS_ECP_C)
169/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170 *
171 * ECParameters ::= CHOICE {
172 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100173 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175 * }
176 */
177static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179{
Janos Follath24eed8d2019-11-22 13:21:35 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181
Sanne Woudab2b29d52017-08-21 15:58:12 +0100182 if ( end - *p < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100183 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
184 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100185
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100186 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( params->tag != MBEDTLS_ASN1_OID
189#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
190 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100191#endif
192 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100193 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100194 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
195 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100199 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100200 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100201 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200202
203 params->p = *p;
204 *p += params->len;
205
206 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100207 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
208 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209
210 return( 0 );
211}
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100215 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
216 * WARNING: the resulting group should only be used with
217 * pk_group_id_from_specified(), since its base point may not be set correctly
218 * if it was encoded compressed.
219 *
220 * SpecifiedECDomain ::= SEQUENCE {
221 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
222 * fieldID FieldID {{FieldTypes}},
223 * curve Curve,
224 * base ECPoint,
225 * order INTEGER,
226 * cofactor INTEGER OPTIONAL,
227 * hash HashAlgorithm OPTIONAL,
228 * ...
229 * }
230 *
231 * We only support prime-field as field type, and ignore hash and cofactor.
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234{
Janos Follath24eed8d2019-11-22 13:21:35 +0000235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 unsigned char *p = params->p;
237 const unsigned char * const end = params->p + params->len;
238 const unsigned char *end_field, *end_curve;
239 size_t len;
240 int ver;
241
242 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100244 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245
246 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248
249 /*
250 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
251 * fieldType FIELD-ID.&id({IOSet}),
252 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
253 * }
254 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
256 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100257 return( ret );
258
259 end_field = p + len;
260
261 /*
262 * FIELD-ID ::= TYPE-IDENTIFIER
263 * FieldTypes FIELD-ID ::= {
264 * { Prime-p IDENTIFIED BY prime-field } |
265 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
266 * }
267 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100270 return( ret );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
273 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100276 }
277
278 p += len;
279
280 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100282 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100283
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200284 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
286 if( p != end_field )
Chris Jones9f7a6932021-04-14 12:12:09 +0100287 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
288 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100289
290 /*
291 * Curve ::= SEQUENCE {
292 * a FieldElement,
293 * b FieldElement,
294 * seed BIT STRING OPTIONAL
295 * -- Shall be present if used in SpecifiedECDomain
296 * -- with version equal to ecdpVer2 or ecdpVer3
297 * }
298 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
300 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100301 return( ret );
302
303 end_curve = p + len;
304
305 /*
306 * FieldElement ::= OCTET STRING
307 * containing an integer in the case of a prime field
308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
310 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100312 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100313 }
314
315 p += len;
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
318 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100319 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100320 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 }
322
323 p += len;
324
325 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327 p += len;
328
329 if( p != end_curve )
Chris Jones9f7a6932021-04-14 12:12:09 +0100330 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
331 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100332
333 /*
334 * ECPoint ::= OCTET STRING
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100337 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100340 ( const unsigned char *) p, len ) ) != 0 )
341 {
342 /*
343 * If we can't read the point because it's compressed, cheat by
344 * reading only the X coordinate and the parity bit of Y.
345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100347 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 len != mbedtls_mpi_size( &grp->P ) + 1 ||
349 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
350 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
351 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100354 }
355 }
356
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100357 p += len;
358
359 /*
360 * order INTEGER
361 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100363 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100364
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200365 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100366
367 /*
368 * Allow optional elements by purposefully not enforcing p == end here.
369 */
370
371 return( 0 );
372}
373
374/*
375 * Find the group id associated with an (almost filled) group as generated by
376 * pk_group_from_specified(), or return an error if unknown.
377 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378static 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 +0100379{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100380 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381 mbedtls_ecp_group ref;
382 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387 {
388 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200390 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391
392 /* Compare to the group we were given, starting with easy tests */
393 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
395 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
396 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
397 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
398 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
399 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100400 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 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 +0100402 {
403 break;
404 }
405
406 }
407
408cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410
411 *grp_id = *id;
412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
414 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100415
416 return( ret );
417}
418
419/*
420 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
423 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424{
Janos Follath24eed8d2019-11-22 13:21:35 +0000425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
430 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
431 goto cleanup;
432
433 ret = pk_group_id_from_group( &grp, grp_id );
434
435cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438 return( ret );
439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441
442/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200443 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444 *
445 * ECParameters ::= CHOICE {
446 * namedCurve OBJECT IDENTIFIER
447 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
448 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200451{
Janos Follath24eed8d2019-11-22 13:21:35 +0000452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
458 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100459 }
460 else
461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
464 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100465#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100467#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200469
470 /*
Shaun Case0e7791f2021-12-20 21:14:10 -0800471 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200472 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
474 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200475
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200476 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200477 return( ret );
478
479 return( 0 );
480}
481
482/*
483 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100484 *
485 * The caller is responsible for clearing the structure upon failure if
486 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 */
489static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491{
Janos Follath24eed8d2019-11-22 13:21:35 +0000492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100495 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498 }
499
500 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502 */
503 *p = (unsigned char *) end;
504
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100505 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510/*
511 * RSAPublicKey ::= SEQUENCE {
512 * modulus INTEGER, -- n
513 * publicExponent INTEGER -- e
514 * }
515 */
516static int pk_get_rsapubkey( unsigned char **p,
517 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200519{
Janos Follath24eed8d2019-11-22 13:21:35 +0000520 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200521 size_t len;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
524 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100525 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200526
527 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100528 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
529 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100531 /* Import N */
532 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100533 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200534
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100535 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
536 NULL, 0, NULL, 0 ) ) != 0 )
537 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
538
539 *p += len;
540
541 /* Import E */
542 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100543 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100544
545 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
546 NULL, 0, *p, len ) ) != 0 )
547 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
548
549 *p += len;
550
Hanno Becker895c5ab2018-01-05 08:08:09 +0000551 if( mbedtls_rsa_complete( rsa ) != 0 ||
552 mbedtls_rsa_check_pubkey( rsa ) != 0 )
553 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100554 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000555 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100556
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100558 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
559 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200560
Paul Bakker1a7550a2013-09-15 13:01:22 +0200561 return( 0 );
562}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200564
565/* Get a PK algorithm identifier
566 *
567 * AlgorithmIdentifier ::= SEQUENCE {
568 * algorithm OBJECT IDENTIFIER,
569 * parameters ANY DEFINED BY algorithm OPTIONAL }
570 */
571static int pk_get_pk_alg( unsigned char **p,
572 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200574{
Janos Follath24eed8d2019-11-22 13:21:35 +0000575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100581 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_ALG, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
584 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585
586 /*
587 * No parameters with RSA (only for EC)
588 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 if( *pk_alg == MBEDTLS_PK_RSA &&
590 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200591 params->len != 0 ) )
592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200594 }
595
596 return( 0 );
597}
598
599/*
600 * SubjectPublicKeyInfo ::= SEQUENCE {
601 * algorithm AlgorithmIdentifier,
602 * subjectPublicKey BIT STRING }
603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
605 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200606{
Janos Follath24eed8d2019-11-22 13:21:35 +0000607 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_asn1_buf alg_params;
610 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
611 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200612
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500613 PK_VALIDATE_RET( p != NULL );
614 PK_VALIDATE_RET( *p != NULL );
615 PK_VALIDATE_RET( end != NULL );
616 PK_VALIDATE_RET( pk != NULL );
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
619 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100621 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200622 }
623
624 end = *p + len;
625
626 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
627 return( ret );
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100630 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631
632 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100633 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
634 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
637 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200639 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640 return( ret );
641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#if defined(MBEDTLS_RSA_C)
643 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647#endif /* MBEDTLS_RSA_C */
648#if defined(MBEDTLS_ECP_C)
649 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#endif /* MBEDTLS_ECP_C */
656 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200657
658 if( ret == 0 && *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100659 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
660 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661
662 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664
665 return( ret );
666}
667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200669/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100670 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
671 *
672 * The value zero is:
673 * - never a valid value for an RSA parameter
674 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
675 *
676 * Since values can't be omitted in PKCS#1, passing a zero value to
677 * rsa_complete() would be incorrect, so reject zero values early.
678 */
679static int asn1_get_nonzero_mpi( unsigned char **p,
680 const unsigned char *end,
681 mbedtls_mpi *X )
682{
683 int ret;
684
685 ret = mbedtls_asn1_get_mpi( p, end, X );
686 if( ret != 0 )
687 return( ret );
688
689 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
690 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
691
692 return( 0 );
693}
694
695/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200696 * Parse a PKCS#1 encoded private RSA key
697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200699 const unsigned char *key,
700 size_t keylen )
701{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100702 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703 size_t len;
704 unsigned char *p, *end;
705
Hanno Beckerefa14e82017-10-11 19:45:19 +0100706 mbedtls_mpi T;
707 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100708
Paul Bakker1a7550a2013-09-15 13:01:22 +0200709 p = (unsigned char *) key;
710 end = p + keylen;
711
712 /*
713 * This function parses the RSAPrivateKey (PKCS#1)
714 *
715 * RSAPrivateKey ::= SEQUENCE {
716 * version Version,
717 * modulus INTEGER, -- n
718 * publicExponent INTEGER, -- e
719 * privateExponent INTEGER, -- d
720 * prime1 INTEGER, -- p
721 * prime2 INTEGER, -- q
722 * exponent1 INTEGER, -- d mod (p-1)
723 * exponent2 INTEGER, -- d mod (q-1)
724 * coefficient INTEGER, -- (inverse of q) mod p
725 * otherPrimeInfos OtherPrimeInfos OPTIONAL
726 * }
727 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
729 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200730 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100731 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200732 }
733
734 end = p + len;
735
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100736 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100738 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739 }
740
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100741 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200743 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200744 }
745
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100746 /* Import N */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100747 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
748 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
749 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100750 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200751
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100752 /* Import E */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100753 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
754 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
755 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100756 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757
758 /* Import D */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100759 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
760 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
761 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100762 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763
764 /* Import P */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100765 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
766 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
767 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100768 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100769
770 /* Import Q */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100771 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
772 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
773 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100774 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100775
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100776#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500777 /*
778 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
779 * that they can be easily recomputed from D, P and Q. However by
780 * parsing them from the PKCS1 structure it is possible to avoid
781 * recalculating them which both reduces the overhead of loading
782 * RSA private keys into memory and also avoids side channels which
783 * can arise when computing those values, since all of D, P, and Q
784 * are secret. See https://eprint.iacr.org/2020/055 for a
785 * description of one such attack.
786 */
787
Jack Lloyd80cc8112020-01-22 17:34:29 -0500788 /* Import DP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100789 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
790 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500791 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500792
793 /* Import DQ */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100794 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
795 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500796 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500797
798 /* Import QP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100799 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
800 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500801 goto cleanup;
802
Jack Lloyd60239752020-01-27 17:53:36 -0500803#else
Shaun Case0e7791f2021-12-20 21:14:10 -0800804 /* Verify existence of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100805 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
806 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
807 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500808 goto cleanup;
809#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500810
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100811 /* rsa_complete() doesn't complete anything with the default
812 * implementation but is still called:
813 * - for the benefit of alternative implementation that may want to
814 * pre-compute stuff beyond what's provided (eg Montgomery factors)
815 * - as is also sanity-checks the key
816 *
817 * Furthermore, we also check the public part for consistency with
818 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
819 */
820 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
821 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
822 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100823 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100824 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100825
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826 if( p != end )
827 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100828 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
829 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830 }
831
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100832cleanup:
833
Hanno Beckerefa14e82017-10-11 19:45:19 +0100834 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100835
836 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100838 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839 if( ( ret & 0xff80 ) == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100840 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100841 else
842 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845 }
846
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100847 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852/*
853 * Parse a SEC1 encoded private EC key
854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856 const unsigned char *key,
857 size_t keylen )
858{
Janos Follath24eed8d2019-11-22 13:21:35 +0000859 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100860 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863 unsigned char *p = (unsigned char *) key;
864 unsigned char *end = p + keylen;
865 unsigned char *end2;
866
867 /*
868 * RFC 5915, or SEC1 Appendix C.4
869 *
870 * ECPrivateKey ::= SEQUENCE {
871 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
872 * privateKey OCTET STRING,
873 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
874 * publicKey [1] BIT STRING OPTIONAL
875 * }
876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
878 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100880 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 }
882
883 end = p + len;
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100886 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887
888 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100892 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100897 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 }
899
900 p += len;
901
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200902 pubkey_done = 0;
903 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200905 /*
906 * Is 'parameters' present?
907 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200908 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
909 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200910 {
911 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
912 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
913 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200914 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200915 return( ret );
916 }
917 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200918 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100921 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100922 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800923 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200924
Jethro Beekmand2df9362018-02-16 13:11:04 -0800925 if( p != end )
926 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200927 /*
928 * Is 'publickey' present? If not, or if we can't read it (eg because it
929 * is compressed), create it from the private key.
930 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200931 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
932 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200933 {
934 end2 = p + len;
935
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200936 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100937 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200938
939 if( p + len != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100940 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
941 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200942
943 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
944 pubkey_done = 1;
945 else
946 {
947 /*
948 * The only acceptable failure mode of pk_get_ecpubkey() above
949 * is if the point format is not recognized.
950 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200951 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
952 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200953 }
954 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200955 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200956 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200957 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100958 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200959 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100961
962 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100964 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100967 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200968 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200973 return( ret );
974 }
975
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200976 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979
980/*
981 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100982 *
983 * Notes:
984 *
985 * - This function does not own the key buffer. It is the
986 * responsibility of the caller to take care of zeroizing
987 * and freeing it after use.
988 *
989 * - The function is responsible for freeing the provided
990 * PK context on failure.
991 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 */
993static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 const unsigned char* key,
996 size_t keylen )
997{
998 int ret, version;
999 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001 unsigned char *p = (unsigned char *) key;
1002 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1004 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005
1006 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001007 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008 *
1009 * PrivateKeyInfo ::= SEQUENCE {
1010 * version Version,
1011 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1012 * privateKey PrivateKey,
1013 * attributes [0] IMPLICIT Attributes OPTIONAL }
1014 *
1015 * Version ::= INTEGER
1016 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1017 * PrivateKey ::= OCTET STRING
1018 *
1019 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1020 */
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1023 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001025 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026 }
1027
1028 end = p + len;
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001031 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032
1033 if( version != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001034 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035
1036 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Chris Jonesfdb588b2021-04-14 18:15:24 +01001037 {
Chris Jones4d01c5b2021-04-28 14:12:07 +01001038 return( ret );
Chris Jonesfdb588b2021-04-14 18:15:24 +01001039 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001042 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043
1044 if( len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001045 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1046 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1049 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001051 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052 return( ret );
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_RSA_C)
1055 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060 return( ret );
1061 }
1062 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#endif /* MBEDTLS_RSA_C */
1064#if defined(MBEDTLS_ECP_C)
1065 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1068 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001071 return( ret );
1072 }
1073 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#endif /* MBEDTLS_ECP_C */
1075 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001077 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078}
1079
1080/*
1081 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001082 *
1083 * To save space, the decryption happens in-place on the given key buffer.
1084 * Also, while this function may modify the keybuffer, it doesn't own it,
1085 * and instead it is the responsibility of the caller to zeroize and properly
1086 * free it after use.
1087 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001092 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093 const unsigned char *pwd, size_t pwdlen )
1094{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001095 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001097 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1100#if defined(MBEDTLS_PKCS12_C)
1101 mbedtls_cipher_type_t cipher_alg;
1102 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001103#endif
1104
Hanno Becker2aa80a72017-09-07 15:28:45 +01001105 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001106 end = p + keylen;
1107
1108 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110
1111 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001112 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 *
1114 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1115 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1116 * encryptedData EncryptedData
1117 * }
1118 *
1119 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1120 *
1121 * EncryptedData ::= OCTET STRING
1122 *
1123 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001124 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1127 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001129 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130 }
1131
1132 end = p + len;
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001135 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001138 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139
Hanno Beckerfab35692017-08-25 13:38:26 +01001140 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141
1142 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001143 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145#if defined(MBEDTLS_PKCS12_C)
1146 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149 cipher_alg, md_alg,
1150 pwd, pwdlen, p, len, buf ) ) != 0 )
1151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1153 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154
1155 return( ret );
1156 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001157
1158 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1163 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 pwd, pwdlen,
1165 p, len, buf ) ) != 0 )
1166 {
1167 return( ret );
1168 }
1169
1170 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1174 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001175
1176 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 }
1178 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#endif /* MBEDTLS_PKCS12_C */
1180#if defined(MBEDTLS_PKCS5_C)
1181 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184 p, len, buf ) ) != 0 )
1185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1187 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188
1189 return( ret );
1190 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001191
1192 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193 }
1194 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001196 {
1197 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001200 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001202
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1204}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206
1207/*
1208 * Parse a private key
1209 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211 const unsigned char *key, size_t keylen,
1212 const unsigned char *pwd, size_t pwdlen )
1213{
Janos Follath24eed8d2019-11-22 13:21:35 +00001214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001219#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001221 PK_VALIDATE_RET( pk != NULL );
1222 if( keylen == 0 )
1223 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1224 PK_VALIDATE_RET( key != NULL );
1225
1226#if defined(MBEDTLS_PEM_PARSE_C)
1227 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#if defined(MBEDTLS_RSA_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 RSA PRIVATE KEY-----",
1236 "-----END RSA PRIVATE KEY-----",
1237 key, pwd, pwdlen, &len );
1238
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239 if( ret == 0 )
1240 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001241 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
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_pkcs1_der( mbedtls_pk_rsa( *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_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001261 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001262 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001263 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1264 else
1265 ret = mbedtls_pem_read_buffer( &pem,
1266 "-----BEGIN EC PRIVATE KEY-----",
1267 "-----END EC PRIVATE KEY-----",
1268 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269 if( ret == 0 )
1270 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001271 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001272
Hanno Beckerfab35692017-08-25 13:38:26 +01001273 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 pem.buf, pem.buflen ) ) != 0 )
1276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 }
1279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 return( ret );
1282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1284 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1285 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1286 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1287 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001291 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001292 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001293 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1294 else
1295 ret = mbedtls_pem_read_buffer( &pem,
1296 "-----BEGIN PRIVATE KEY-----",
1297 "-----END PRIVATE KEY-----",
1298 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 if( ret == 0 )
1300 {
1301 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1302 pem.buf, pem.buflen ) ) != 0 )
1303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 }
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 return( ret );
1309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311 return( ret );
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001314 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001315 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001316 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1317 else
1318 ret = mbedtls_pem_read_buffer( &pem,
1319 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1320 "-----END ENCRYPTED PRIVATE KEY-----",
1321 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 if( ret == 0 )
1323 {
1324 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1325 pem.buf, pem.buflen,
1326 pwd, pwdlen ) ) != 0 )
1327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329 }
1330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332 return( ret );
1333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337#else
1338 ((void) pwd);
1339 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
1342 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001343 * At this point we only know it's not a PEM formatted key. Could be any
1344 * of the known DER encoded private key formats
1345 *
1346 * We try the different DER format parsers to see if one passes without
1347 * error
1348 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001351 unsigned char *key_copy;
1352
1353 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1354 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1355
1356 memcpy( key_copy, key, keylen );
1357
1358 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1359 pwd, pwdlen );
1360
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001361 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001362 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363 }
1364
Hanno Beckerfab35692017-08-25 13:38:26 +01001365 if( ret == 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( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372 {
1373 return( ret );
1374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376
Kenneth Soerensenc4e950e2019-01-03 12:39:29 +01001377 ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen );
1378 if( ret == 0 )
1379 {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 return( 0 );
Kenneth Soerensenc4e950e2019-01-03 12:39:29 +01001381 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001384 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
Hanno Becker9be19262017-09-08 12:39:44 +01001388 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001389 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1390 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391 {
1392 return( 0 );
1393 }
1394
Hanno Becker780f0a42018-10-10 11:23:33 +01001395 mbedtls_pk_free( pk );
1396 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001400 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001401 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1402 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1403 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404 {
1405 return( 0 );
1406 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001407 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409
Hanno Becker780f0a42018-10-10 11:23:33 +01001410 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1411 * it is ok to leave the PK context initialized but not
1412 * freed: It is the caller's responsibility to call pk_init()
1413 * before calling this function, and to call pk_free()
1414 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1415 * isn't, this leads to mbedtls_pk_free() being called
1416 * twice, once here and once by the caller, but this is
1417 * also ok and in line with the mbedtls_pk_free() calls
1418 * on failed PEM parsing attempts. */
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421}
1422
1423/*
1424 * Parse a public key
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427 const unsigned char *key, size_t keylen )
1428{
Janos Follath24eed8d2019-11-22 13:21:35 +00001429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001431#if defined(MBEDTLS_RSA_C)
1432 const mbedtls_pk_info_t *pk_info;
1433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001437#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001439 PK_VALIDATE_RET( ctx != NULL );
1440 if( keylen == 0 )
1441 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1442 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1443
1444#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001446#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001447 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001448 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001449 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1450 else
1451 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001452 "-----BEGIN RSA PUBLIC KEY-----",
1453 "-----END RSA PUBLIC KEY-----",
1454 key, NULL, 0, &len );
1455
1456 if( ret == 0 )
1457 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001458 p = pem.buf;
1459 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
Paul Elliottb39b4992022-05-13 17:08:36 +01001460 {
1461 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001462 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Elliottb39b4992022-05-13 17:08:36 +01001463 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001464
Ron Eldor84df1ae2017-10-16 17:11:52 +03001465 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
Paul Elliottb39b4992022-05-13 17:08:36 +01001466 {
Leonid Rozenboim763ee3c2022-04-21 13:05:10 -07001467 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001468 return( ret );
Leonid Rozenboim763ee3c2022-04-21 13:05:10 -07001469 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001470
Ron Eldor84df1ae2017-10-16 17:11:52 +03001471 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1472 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001473
Ron Eldord0c56de2017-10-10 17:03:08 +03001474 mbedtls_pem_free( &pem );
1475 return( ret );
1476 }
1477 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1478 {
1479 mbedtls_pem_free( &pem );
1480 return( ret );
1481 }
1482#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001483
1484 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001485 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001486 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1487 else
1488 ret = mbedtls_pem_read_buffer( &pem,
1489 "-----BEGIN PUBLIC KEY-----",
1490 "-----END PUBLIC KEY-----",
1491 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
1493 if( ret == 0 )
1494 {
1495 /*
1496 * Was PEM encoded
1497 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001498 p = pem.buf;
1499
1500 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1501 mbedtls_pem_free( &pem );
1502 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 return( ret );
1508 }
Ron Eldor5472d432017-10-17 09:49:00 +03001509 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001511
1512#if defined(MBEDTLS_RSA_C)
1513 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1514 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1515
1516 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1517 return( ret );
1518
Ron Eldor9566ff72018-02-07 18:59:41 +02001519 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001520 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001521 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001522 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001523 return( ret );
1524 }
1525 mbedtls_pk_free( ctx );
Chris Jones9f7a6932021-04-14 12:12:09 +01001526 if( ret != ( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
1527 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001528 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001529 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001530 }
1531#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532 p = (unsigned char *) key;
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536 return( ret );
1537}
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#endif /* MBEDTLS_PK_PARSE_C */