blob: fe6aaca338c0e3c32660b153681d3f9f4615d506 [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#else
54#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020057#endif
58
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050059/* Parameter validation macros based on platform_util.h */
60#define PK_VALIDATE_RET( cond ) \
61 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
62#define PK_VALIDATE( cond ) \
63 MBEDTLS_INTERNAL_VALIDATE( cond )
64
Gilles Peskine832f3492017-11-30 11:42:12 +010065#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020066/*
67 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020068 *
69 * The file is expected to contain either PEM or DER encoded data.
70 * A terminating null byte is always appended. It is included in the announced
71 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020074{
75 FILE *f;
76 long size;
77
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 PK_VALIDATE_RET( path != NULL );
79 PK_VALIDATE_RET( buf != NULL );
80 PK_VALIDATE_RET( n != NULL );
81
Paul Bakker1a7550a2013-09-15 13:01:22 +020082 if( ( f = fopen( path, "rb" ) ) == NULL )
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_END );
86 if( ( size = ftell( f ) ) == -1 )
87 {
88 fclose( f );
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_SET );
92
93 *n = (size_t) size;
94
95 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020096 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 {
98 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020099 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200100 }
101
102 if( fread( *buf, 1, *n, f ) != *n )
103 {
104 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100105
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500106 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110 }
111
112 fclose( f );
113
114 (*buf)[*n] = '\0';
115
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200116 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
117 ++*n;
118
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119 return( 0 );
120}
121
122/*
123 * Load and parse a private key
124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200126 const char *path, const char *pwd,
127 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128{
Janos Follath24eed8d2019-11-22 13:21:35 +0000129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130 size_t n;
131 unsigned char *buf;
132
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 PK_VALIDATE_RET( ctx != NULL );
134 PK_VALIDATE_RET( path != NULL );
135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137 return( ret );
138
139 if( pwd == NULL )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200140 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0, f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 ret = mbedtls_pk_parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200143 (const unsigned char *) pwd, strlen( pwd ), f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500145 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147
148 return( ret );
149}
150
151/*
152 * Load and parse a public key
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155{
Janos Follath24eed8d2019-11-22 13:21:35 +0000156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157 size_t n;
158 unsigned char *buf;
159
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500160 PK_VALIDATE_RET( ctx != NULL );
161 PK_VALIDATE_RET( path != NULL );
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164 return( ret );
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500168 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170
171 return( ret );
172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_ECP_C)
176/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177 *
178 * ECParameters ::= CHOICE {
179 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100180 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182 * }
183 */
184static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186{
Janos Follath24eed8d2019-11-22 13:21:35 +0000187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200188
Sanne Woudab2b29d52017-08-21 15:58:12 +0100189 if ( end - *p < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100190 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
191 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100192
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100193 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200194 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( params->tag != MBEDTLS_ASN1_OID
196#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
197 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100198#endif
199 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100200 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100201 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
202 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100206 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100207 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100208 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209
210 params->p = *p;
211 *p += params->len;
212
213 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100214 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
215 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200216
217 return( 0 );
218}
219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200221/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100222 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
223 * WARNING: the resulting group should only be used with
224 * pk_group_id_from_specified(), since its base point may not be set correctly
225 * if it was encoded compressed.
226 *
227 * SpecifiedECDomain ::= SEQUENCE {
228 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
229 * fieldID FieldID {{FieldTypes}},
230 * curve Curve,
231 * base ECPoint,
232 * order INTEGER,
233 * cofactor INTEGER OPTIONAL,
234 * hash HashAlgorithm OPTIONAL,
235 * ...
236 * }
237 *
238 * We only support prime-field as field type, and ignore hash and cofactor.
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100241{
Janos Follath24eed8d2019-11-22 13:21:35 +0000242 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100243 unsigned char *p = params->p;
244 const unsigned char * const end = params->p + params->len;
245 const unsigned char *end_field, *end_curve;
246 size_t len;
247 int ver;
248
249 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100251 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100252
253 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100255
256 /*
257 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
258 * fieldType FIELD-ID.&id({IOSet}),
259 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
260 * }
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
263 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100264 return( ret );
265
266 end_field = p + len;
267
268 /*
269 * FIELD-ID ::= TYPE-IDENTIFIER
270 * FieldTypes FIELD-ID ::= {
271 * { Prime-p IDENTIFIED BY prime-field } |
272 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
273 * }
274 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
275 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277 return( ret );
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
280 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100283 }
284
285 p += len;
286
287 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100289 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100290
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200291 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100292
293 if( p != end_field )
Chris Jones9f7a6932021-04-14 12:12:09 +0100294 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
295 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296
297 /*
298 * Curve ::= SEQUENCE {
299 * a FieldElement,
300 * b FieldElement,
301 * seed BIT STRING OPTIONAL
302 * -- Shall be present if used in SpecifiedECDomain
303 * -- with version equal to ecdpVer2 or ecdpVer3
304 * }
305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
307 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100308 return( ret );
309
310 end_curve = p + len;
311
312 /*
313 * FieldElement ::= OCTET STRING
314 * containing an integer in the case of a prime field
315 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
317 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100318 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100319 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100320 }
321
322 p += len;
323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
325 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100326 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100327 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328 }
329
330 p += len;
331
332 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334 p += len;
335
336 if( p != end_curve )
Chris Jones9f7a6932021-04-14 12:12:09 +0100337 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
338 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100339
340 /*
341 * ECPoint ::= OCTET STRING
342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100344 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100347 ( const unsigned char *) p, len ) ) != 0 )
348 {
349 /*
350 * If we can't read the point because it's compressed, cheat by
351 * reading only the X coordinate and the parity bit of Y.
352 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100354 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 len != mbedtls_mpi_size( &grp->P ) + 1 ||
356 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
357 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
358 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100361 }
362 }
363
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100364 p += len;
365
366 /*
367 * order INTEGER
368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100370 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200372 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100373
374 /*
375 * Allow optional elements by purposefully not enforcing p == end here.
376 */
377
378 return( 0 );
379}
380
381/*
382 * Find the group id associated with an (almost filled) group as generated by
383 * pk_group_from_specified(), or return an error if unknown.
384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385static 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 +0100386{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100387 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_ecp_group ref;
389 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394 {
395 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200397 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398
399 /* Compare to the group we were given, starting with easy tests */
400 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
402 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
403 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
404 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
405 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
406 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100407 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 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 +0100409 {
410 break;
411 }
412
413 }
414
415cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
418 *grp_id = *id;
419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
421 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
423 return( ret );
424}
425
426/*
427 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
430 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431{
Janos Follath24eed8d2019-11-22 13:21:35 +0000432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100436
437 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
438 goto cleanup;
439
440 ret = pk_group_id_from_group( &grp, grp_id );
441
442cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444
445 return( ret );
446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200450 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451 *
452 * ECParameters ::= CHOICE {
453 * namedCurve OBJECT IDENTIFIER
454 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
455 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200458{
Janos Follath24eed8d2019-11-22 13:21:35 +0000459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
465 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466 }
467 else
468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100470 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
471 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100472#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100474#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100475 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200476
477 /*
478 * grp may already be initilialized; if so, make sure IDs match
479 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
481 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200483 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200484 return( ret );
485
486 return( 0 );
487}
488
489/*
490 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100491 *
492 * The caller is responsible for clearing the structure upon failure if
493 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200495 */
496static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498{
Janos Follath24eed8d2019-11-22 13:21:35 +0000499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100502 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200503 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200505 }
506
507 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509 */
510 *p = (unsigned char *) end;
511
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100512 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517/*
518 * RSAPublicKey ::= SEQUENCE {
519 * modulus INTEGER, -- n
520 * publicExponent INTEGER -- e
521 * }
522 */
523static int pk_get_rsapubkey( unsigned char **p,
524 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200526{
Janos Follath24eed8d2019-11-22 13:21:35 +0000527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528 size_t len;
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
531 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100532 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533
534 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100535 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
536 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100538 /* Import N */
539 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100540 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100542 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
543 NULL, 0, NULL, 0 ) ) != 0 )
544 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
545
546 *p += len;
547
548 /* Import E */
549 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100550 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100551
552 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
553 NULL, 0, *p, len ) ) != 0 )
554 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
555
556 *p += len;
557
Hanno Becker895c5ab2018-01-05 08:08:09 +0000558 if( mbedtls_rsa_complete( rsa ) != 0 ||
559 mbedtls_rsa_check_pubkey( rsa ) != 0 )
560 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100561 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000562 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100563
Paul Bakker1a7550a2013-09-15 13:01:22 +0200564 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100565 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
566 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200567
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568 return( 0 );
569}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200571
572/* Get a PK algorithm identifier
573 *
574 * AlgorithmIdentifier ::= SEQUENCE {
575 * algorithm OBJECT IDENTIFIER,
576 * parameters ANY DEFINED BY algorithm OPTIONAL }
577 */
578static int pk_get_pk_alg( unsigned char **p,
579 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200581{
Janos Follath24eed8d2019-11-22 13:21:35 +0000582 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100588 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_ALG, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
591 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200592
593 /*
594 * No parameters with RSA (only for EC)
595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 if( *pk_alg == MBEDTLS_PK_RSA &&
597 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200598 params->len != 0 ) )
599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200601 }
602
603 return( 0 );
604}
605
606/*
607 * SubjectPublicKeyInfo ::= SEQUENCE {
608 * algorithm AlgorithmIdentifier,
609 * subjectPublicKey BIT STRING }
610 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
612 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200613{
Janos Follath24eed8d2019-11-22 13:21:35 +0000614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200615 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_asn1_buf alg_params;
617 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
618 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500620 PK_VALIDATE_RET( p != NULL );
621 PK_VALIDATE_RET( *p != NULL );
622 PK_VALIDATE_RET( end != NULL );
623 PK_VALIDATE_RET( pk != NULL );
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
626 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100628 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200629 }
630
631 end = *p + len;
632
633 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
634 return( ret );
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100637 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638
639 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100640 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
641 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
644 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200646 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647 return( ret );
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_RSA_C)
650 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200653 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654#endif /* MBEDTLS_RSA_C */
655#if defined(MBEDTLS_ECP_C)
656 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200659 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_ECP_C */
663 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664
665 if( ret == 0 && *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100666 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
667 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668
669 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200671
672 return( ret );
673}
674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100677 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
678 *
679 * The value zero is:
680 * - never a valid value for an RSA parameter
681 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
682 *
683 * Since values can't be omitted in PKCS#1, passing a zero value to
684 * rsa_complete() would be incorrect, so reject zero values early.
685 */
686static int asn1_get_nonzero_mpi( unsigned char **p,
687 const unsigned char *end,
688 mbedtls_mpi *X )
689{
690 int ret;
691
692 ret = mbedtls_asn1_get_mpi( p, end, X );
693 if( ret != 0 )
694 return( ret );
695
696 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
697 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
698
699 return( 0 );
700}
701
702/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703 * Parse a PKCS#1 encoded private RSA key
704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706 const unsigned char *key,
707 size_t keylen )
708{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100709 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710 size_t len;
711 unsigned char *p, *end;
712
Hanno Beckerefa14e82017-10-11 19:45:19 +0100713 mbedtls_mpi T;
714 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100715
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716 p = (unsigned char *) key;
717 end = p + keylen;
718
719 /*
720 * This function parses the RSAPrivateKey (PKCS#1)
721 *
722 * RSAPrivateKey ::= SEQUENCE {
723 * version Version,
724 * modulus INTEGER, -- n
725 * publicExponent INTEGER, -- e
726 * privateExponent INTEGER, -- d
727 * prime1 INTEGER, -- p
728 * prime2 INTEGER, -- q
729 * exponent1 INTEGER, -- d mod (p-1)
730 * exponent2 INTEGER, -- d mod (q-1)
731 * coefficient INTEGER, -- (inverse of q) mod p
732 * otherPrimeInfos OtherPrimeInfos OPTIONAL
733 * }
734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
736 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 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
741 end = p + len;
742
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100743 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200744 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100745 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200746 }
747
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100748 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200751 }
752
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100753 /* Import N */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100754 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
755 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
756 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100759 /* Import E */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100760 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
761 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
762 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100764
765 /* Import D */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100766 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
767 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
768 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100769 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100770
771 /* Import P */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100772 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
773 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
774 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100775 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100776
777 /* Import Q */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100778 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
779 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
780 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100781 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100782
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100783#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500784 /*
785 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
786 * that they can be easily recomputed from D, P and Q. However by
787 * parsing them from the PKCS1 structure it is possible to avoid
788 * recalculating them which both reduces the overhead of loading
789 * RSA private keys into memory and also avoids side channels which
790 * can arise when computing those values, since all of D, P, and Q
791 * are secret. See https://eprint.iacr.org/2020/055 for a
792 * description of one such attack.
793 */
794
Jack Lloyd80cc8112020-01-22 17:34:29 -0500795 /* Import DP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100796 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
797 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500798 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500799
800 /* Import DQ */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100801 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
802 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500803 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500804
805 /* Import QP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100806 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
807 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500808 goto cleanup;
809
Jack Lloyd60239752020-01-27 17:53:36 -0500810#else
811 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100812 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
813 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
814 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500815 goto cleanup;
816#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500817
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100818 /* rsa_complete() doesn't complete anything with the default
819 * implementation but is still called:
820 * - for the benefit of alternative implementation that may want to
821 * pre-compute stuff beyond what's provided (eg Montgomery factors)
822 * - as is also sanity-checks the key
823 *
824 * Furthermore, we also check the public part for consistency with
825 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
826 */
827 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
828 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
829 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100830 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100831 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100832
Paul Bakker1a7550a2013-09-15 13:01:22 +0200833 if( p != end )
834 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100835 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
836 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837 }
838
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839cleanup:
840
Hanno Beckerefa14e82017-10-11 19:45:19 +0100841 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100842
843 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100845 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846 if( ( ret & 0xff80 ) == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100847 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100848 else
849 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852 }
853
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100854 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859/*
860 * Parse a SEC1 encoded private EC key
861 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200863 const unsigned char *key, size_t keylen,
864 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865{
Janos Follath24eed8d2019-11-22 13:21:35 +0000866 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100867 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870 unsigned char *p = (unsigned char *) key;
871 unsigned char *end = p + keylen;
872 unsigned char *end2;
873
874 /*
875 * RFC 5915, or SEC1 Appendix C.4
876 *
877 * ECPrivateKey ::= SEQUENCE {
878 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
879 * privateKey OCTET STRING,
880 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
881 * publicKey [1] BIT STRING OPTIONAL
882 * }
883 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
885 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100887 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888 }
889
890 end = p + len;
891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200892 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100893 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894
895 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100899 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100904 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905 }
906
907 p += len;
908
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200909 pubkey_done = 0;
910 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200912 /*
913 * Is 'parameters' present?
914 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200915 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
916 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200917 {
918 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
919 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
920 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200921 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200922 return( ret );
923 }
924 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200925 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100928 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100929 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800930 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200931
Jethro Beekmand2df9362018-02-16 13:11:04 -0800932 if( p != end )
933 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200934 /*
935 * Is 'publickey' present? If not, or if we can't read it (eg because it
936 * is compressed), create it from the private key.
937 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200938 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
939 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200940 {
941 end2 = p + len;
942
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200943 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100944 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200945
946 if( p + len != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100947 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
948 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200949
950 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
951 pubkey_done = 1;
952 else
953 {
954 /*
955 * The only acceptable failure mode of pk_get_ecpubkey() above
956 * is if the point format is not recognized.
957 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200958 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
959 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200960 }
961 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200962 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200963 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200964 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100965 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200966 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100968
969 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200971 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100974 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200975 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200979 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980 return( ret );
981 }
982
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200983 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986
987/*
988 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100989 *
990 * Notes:
991 *
992 * - This function does not own the key buffer. It is the
993 * responsibility of the caller to take care of zeroizing
994 * and freeing it after use.
995 *
996 * - The function is responsible for freeing the provided
997 * PK context on failure.
998 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999 */
1000static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001001 mbedtls_pk_context *pk,
1002 const unsigned char* key, size_t keylen,
1003 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001004{
1005 int ret, version;
1006 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008 unsigned char *p = (unsigned char *) key;
1009 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1011 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001013#if !defined(MBEDTLS_ECP_C)
1014 (void) f_rng;
1015 (void) p_rng;
1016#endif
1017
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001019 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 *
1021 * PrivateKeyInfo ::= SEQUENCE {
1022 * version Version,
1023 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1024 * privateKey PrivateKey,
1025 * attributes [0] IMPLICIT Attributes OPTIONAL }
1026 *
1027 * Version ::= INTEGER
1028 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1029 * PrivateKey ::= OCTET STRING
1030 *
1031 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1032 */
1033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1035 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001037 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038 }
1039
1040 end = p + len;
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001043 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044
1045 if( version != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001046 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047
1048 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Chris Jonesfdb588b2021-04-14 18:15:24 +01001049 {
Chris Jones4d01c5b2021-04-28 14:12:07 +01001050 return( ret );
Chris Jonesfdb588b2021-04-14 18:15:24 +01001051 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001054 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055
1056 if( len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001057 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1058 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1061 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001063 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064 return( ret );
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066#if defined(MBEDTLS_RSA_C)
1067 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001072 return( ret );
1073 }
1074 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_RSA_C */
1076#if defined(MBEDTLS_ECP_C)
1077 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001080 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083 return( ret );
1084 }
1085 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#endif /* MBEDTLS_ECP_C */
1087 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001089 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090}
1091
1092/*
1093 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001094 *
1095 * To save space, the decryption happens in-place on the given key buffer.
1096 * Also, while this function may modify the keybuffer, it doesn't own it,
1097 * and instead it is the responsibility of the caller to zeroize and properly
1098 * free it after use.
1099 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001103 mbedtls_pk_context *pk,
1104 unsigned char *key, size_t keylen,
1105 const unsigned char *pwd, size_t pwdlen,
1106 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001108 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001110 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001111 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1113#if defined(MBEDTLS_PKCS12_C)
1114 mbedtls_cipher_type_t cipher_alg;
1115 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116#endif
1117
Hanno Becker2aa80a72017-09-07 15:28:45 +01001118 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119 end = p + keylen;
1120
1121 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001122 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123
1124 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001125 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126 *
1127 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1128 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1129 * encryptedData EncryptedData
1130 * }
1131 *
1132 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1133 *
1134 * EncryptedData ::= OCTET STRING
1135 *
1136 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001137 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1140 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001142 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143 }
1144
1145 end = p + len;
1146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001148 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001151 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152
Hanno Beckerfab35692017-08-25 13:38:26 +01001153 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154
1155 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001156 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#if defined(MBEDTLS_PKCS12_C)
1159 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 cipher_alg, md_alg,
1163 pwd, pwdlen, p, len, buf ) ) != 0 )
1164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1166 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167
1168 return( ret );
1169 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001170
1171 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_PKCS12_C */
1175#if defined(MBEDTLS_PKCS5_C)
1176 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 p, len, buf ) ) != 0 )
1180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1182 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183
1184 return( ret );
1185 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001186
1187 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188 }
1189 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001191 {
1192 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001193 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001195 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001197
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001198 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len, f_rng, p_rng ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201
1202/*
1203 * Parse a private key
1204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206 const unsigned char *key, size_t keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001207 const unsigned char *pwd, size_t pwdlen,
1208 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209{
Janos Follath24eed8d2019-11-22 13:21:35 +00001210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001215#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001217 PK_VALIDATE_RET( pk != NULL );
1218 if( keylen == 0 )
1219 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1220 PK_VALIDATE_RET( key != NULL );
1221
1222#if defined(MBEDTLS_PEM_PARSE_C)
1223 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001226 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001227 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001228 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1229 else
1230 ret = mbedtls_pem_read_buffer( &pem,
1231 "-----BEGIN RSA PRIVATE KEY-----",
1232 "-----END RSA PRIVATE KEY-----",
1233 key, pwd, pwdlen, &len );
1234
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235 if( ret == 0 )
1236 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001237 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001238 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240 pem.buf, pem.buflen ) ) != 0 )
1241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243 }
1244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001246 return( ret );
1247 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1249 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1250 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1251 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1252 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001257 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001258 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001259 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1260 else
1261 ret = mbedtls_pem_read_buffer( &pem,
1262 "-----BEGIN EC PRIVATE KEY-----",
1263 "-----END EC PRIVATE KEY-----",
1264 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 if( ret == 0 )
1266 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001267 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268
Hanno Beckerfab35692017-08-25 13:38:26 +01001269 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001271 pem.buf, pem.buflen,
1272 f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 }
1276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 return( ret );
1279 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1281 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1282 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1283 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1284 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001288 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001289 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001290 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1291 else
1292 ret = mbedtls_pem_read_buffer( &pem,
1293 "-----BEGIN PRIVATE KEY-----",
1294 "-----END PRIVATE KEY-----",
1295 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296 if( ret == 0 )
1297 {
1298 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001299 pem.buf, pem.buflen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 }
1303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 return( ret );
1306 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 return( ret );
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001311 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001312 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001313 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1314 else
1315 ret = mbedtls_pem_read_buffer( &pem,
1316 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1317 "-----END ENCRYPTED PRIVATE KEY-----",
1318 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 if( ret == 0 )
1320 {
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001321 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, pem.buf, pem.buflen,
1322 pwd, pwdlen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 }
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 return( ret );
1329 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333#else
1334 ((void) pwd);
1335 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337
1338 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001339 * At this point we only know it's not a PEM formatted key. Could be any
1340 * of the known DER encoded private key formats
1341 *
1342 * We try the different DER format parsers to see if one passes without
1343 * error
1344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001347 unsigned char *key_copy;
1348
1349 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1350 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1351
1352 memcpy( key_copy, key, keylen );
1353
1354 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001355 pwd, pwdlen, f_rng, p_rng );
Hanno Beckerfab35692017-08-25 13:38:26 +01001356
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001357 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001358 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359 }
1360
Hanno Beckerfab35692017-08-25 13:38:26 +01001361 if( ret == 0 )
1362 return( 0 );
1363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001365 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368 {
1369 return( ret );
1370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001373 if( ( ret = pk_parse_key_pkcs8_unencrypted_der(
1374 pk, key, keylen, f_rng, p_rng ) ) == 0 )
1375 {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376 return( 0 );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001377 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001380 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001383
Hanno Becker9be19262017-09-08 12:39:44 +01001384 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001385 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1386 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387 {
1388 return( 0 );
1389 }
1390
Hanno Becker780f0a42018-10-10 11:23:33 +01001391 mbedtls_pk_free( pk );
1392 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001396 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001397 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1398 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001399 key, keylen, f_rng, p_rng ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400 {
1401 return( 0 );
1402 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001403 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001405
Hanno Becker780f0a42018-10-10 11:23:33 +01001406 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1407 * it is ok to leave the PK context initialized but not
1408 * freed: It is the caller's responsibility to call pk_init()
1409 * before calling this function, and to call pk_free()
1410 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1411 * isn't, this leads to mbedtls_pk_free() being called
1412 * twice, once here and once by the caller, but this is
1413 * also ok and in line with the mbedtls_pk_free() calls
1414 * on failed PEM parsing attempts. */
1415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417}
1418
1419/*
1420 * Parse a public key
1421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423 const unsigned char *key, size_t keylen )
1424{
Janos Follath24eed8d2019-11-22 13:21:35 +00001425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001427#if defined(MBEDTLS_RSA_C)
1428 const mbedtls_pk_info_t *pk_info;
1429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001433#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001435 PK_VALIDATE_RET( ctx != NULL );
1436 if( keylen == 0 )
1437 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1438 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1439
1440#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001442#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001443 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001444 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001445 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1446 else
1447 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001448 "-----BEGIN RSA PUBLIC KEY-----",
1449 "-----END RSA PUBLIC KEY-----",
1450 key, NULL, 0, &len );
1451
1452 if( ret == 0 )
1453 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001454 p = pem.buf;
1455 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1456 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001457
Ron Eldor84df1ae2017-10-16 17:11:52 +03001458 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1459 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001460
Ron Eldor84df1ae2017-10-16 17:11:52 +03001461 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1462 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001463
Ron Eldord0c56de2017-10-10 17:03:08 +03001464 mbedtls_pem_free( &pem );
1465 return( ret );
1466 }
1467 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1468 {
1469 mbedtls_pem_free( &pem );
1470 return( ret );
1471 }
1472#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001473
1474 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001475 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001476 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1477 else
1478 ret = mbedtls_pem_read_buffer( &pem,
1479 "-----BEGIN PUBLIC KEY-----",
1480 "-----END PUBLIC KEY-----",
1481 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482
1483 if( ret == 0 )
1484 {
1485 /*
1486 * Was PEM encoded
1487 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001488 p = pem.buf;
1489
1490 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1491 mbedtls_pem_free( &pem );
1492 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001493 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 return( ret );
1498 }
Ron Eldor5472d432017-10-17 09:49:00 +03001499 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001501
1502#if defined(MBEDTLS_RSA_C)
1503 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1504 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1505
1506 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1507 return( ret );
1508
Ron Eldor9566ff72018-02-07 18:59:41 +02001509 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001510 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001511 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001512 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001513 return( ret );
1514 }
1515 mbedtls_pk_free( ctx );
Chris Jones9f7a6932021-04-14 12:12:09 +01001516 if( ret != ( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
1517 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001518 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001519 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001520 }
1521#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522 p = (unsigned char *) key;
1523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526 return( ret );
1527}
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_PK_PARSE_C */