blob: 9194040a52184de9ee557ab77799c14f909fa05d [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
Gilles Peskine832f3492017-11-30 11:42:12 +010060#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020061/*
62 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020063 *
64 * The file is expected to contain either PEM or DER encoded data.
65 * A terminating null byte is always appended. It is included in the announced
66 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020069{
70 FILE *f;
71 long size;
72
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050073
Paul Bakker1a7550a2013-09-15 13:01:22 +020074 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020076
Gilles Peskineda0913b2022-06-30 17:03:40 +020077 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
78 mbedtls_setbuf( f, NULL );
79
Paul Bakker1a7550a2013-09-15 13:01:22 +020080 fseek( f, 0, SEEK_END );
81 if( ( size = ftell( f ) ) == -1 )
82 {
83 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020085 }
86 fseek( f, 0, SEEK_SET );
87
88 *n = (size_t) size;
89
90 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020091 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +020092 {
93 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020094 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +020095 }
96
97 if( fread( *buf, 1, *n, f ) != *n )
98 {
99 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100100
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500101 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
107 fclose( f );
108
109 (*buf)[*n] = '\0';
110
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200111 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
112 ++*n;
113
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114 return( 0 );
115}
116
117/*
118 * Load and parse a private key
119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200121 const char *path, const char *pwd,
122 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123{
Janos Follath24eed8d2019-11-22 13:21:35 +0000124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125 size_t n;
126 unsigned char *buf;
127
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128
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é-Gonnard84dea012021-06-15 11:29:26 +0200133 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0, f_rng, p_rng );
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,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200136 (const unsigned char *) pwd, strlen( pwd ), f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155 return( ret );
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500159 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
162 return( ret );
163}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166#if defined(MBEDTLS_ECP_C)
167/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 *
169 * ECParameters ::= CHOICE {
170 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100171 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173 * }
174 */
175static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177{
Janos Follath24eed8d2019-11-22 13:21:35 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179
Sanne Woudab2b29d52017-08-21 15:58:12 +0100180 if ( end - *p < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100181 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
182 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100183
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100184 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 if( params->tag != MBEDTLS_ASN1_OID
187#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
188 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100189#endif
190 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100191 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100192 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
193 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100194 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100197 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100198 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100199 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200200
201 params->p = *p;
202 *p += params->len;
203
204 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100205 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
206 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
208 return( 0 );
209}
210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
214 * WARNING: the resulting group should only be used with
215 * pk_group_id_from_specified(), since its base point may not be set correctly
216 * if it was encoded compressed.
217 *
218 * SpecifiedECDomain ::= SEQUENCE {
219 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
220 * fieldID FieldID {{FieldTypes}},
221 * curve Curve,
222 * base ECPoint,
223 * order INTEGER,
224 * cofactor INTEGER OPTIONAL,
225 * hash HashAlgorithm OPTIONAL,
226 * ...
227 * }
228 *
229 * We only support prime-field as field type, and ignore hash and cofactor.
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 unsigned char *p = params->p;
235 const unsigned char * const end = params->p + params->len;
236 const unsigned char *end_field, *end_curve;
237 size_t len;
238 int ver;
239
240 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100242 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100243
244 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100246
247 /*
248 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
249 * fieldType FIELD-ID.&id({IOSet}),
250 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
251 * }
252 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
254 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100255 return( ret );
256
257 end_field = p + len;
258
259 /*
260 * FIELD-ID ::= TYPE-IDENTIFIER
261 * FieldTypes FIELD-ID ::= {
262 * { Prime-p IDENTIFIED BY prime-field } |
263 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
264 * }
265 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100268 return( ret );
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
271 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100274 }
275
276 p += len;
277
278 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100280 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100281
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200282 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100283
284 if( p != end_field )
Chris Jones9f7a6932021-04-14 12:12:09 +0100285 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
286 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
288 /*
289 * Curve ::= SEQUENCE {
290 * a FieldElement,
291 * b FieldElement,
292 * seed BIT STRING OPTIONAL
293 * -- Shall be present if used in SpecifiedECDomain
294 * -- with version equal to ecdpVer2 or ecdpVer3
295 * }
296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
298 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100299 return( ret );
300
301 end_curve = p + len;
302
303 /*
304 * FieldElement ::= OCTET STRING
305 * containing an integer in the case of a prime field
306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
308 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100309 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100310 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311 }
312
313 p += len;
314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
316 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100319 }
320
321 p += len;
322
323 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325 p += len;
326
327 if( p != end_curve )
Chris Jones9f7a6932021-04-14 12:12:09 +0100328 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
329 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100330
331 /*
332 * ECPoint ::= OCTET STRING
333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100335 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100338 ( const unsigned char *) p, len ) ) != 0 )
339 {
340 /*
341 * If we can't read the point because it's compressed, cheat by
342 * reading only the X coordinate and the parity bit of Y.
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100345 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 len != mbedtls_mpi_size( &grp->P ) + 1 ||
347 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
348 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
349 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100352 }
353 }
354
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100355 p += len;
356
357 /*
358 * order INTEGER
359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100361 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100362
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200363 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100364
365 /*
366 * Allow optional elements by purposefully not enforcing p == end here.
367 */
368
369 return( 0 );
370}
371
372/*
373 * Find the group id associated with an (almost filled) group as generated by
374 * pk_group_from_specified(), or return an error if unknown.
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376static 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 +0100377{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100378 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 mbedtls_ecp_group ref;
380 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100385 {
386 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200388 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
390 /* Compare to the group we were given, starting with easy tests */
391 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
393 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
394 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
395 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
396 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
397 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 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 +0100400 {
401 break;
402 }
403
404 }
405
406cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100408
409 *grp_id = *id;
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
412 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100413
414 return( ret );
415}
416
417/*
418 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
421 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422{
Janos Follath24eed8d2019-11-22 13:21:35 +0000423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100427
428 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
429 goto cleanup;
430
431 ret = pk_group_id_from_group( &grp, grp_id );
432
433cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
436 return( ret );
437}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
440/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200441 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100442 *
443 * ECParameters ::= CHOICE {
444 * namedCurve OBJECT IDENTIFIER
445 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
446 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200447 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200449{
Janos Follath24eed8d2019-11-22 13:21:35 +0000450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
456 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100457 }
458 else
459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100461 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
462 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100463#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100465#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200467
468 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800469 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200470 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
472 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200473
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200474 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200475 return( ret );
476
477 return( 0 );
478}
479
480/*
481 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100482 *
483 * The caller is responsible for clearing the structure upon failure if
484 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486 */
487static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200489{
Janos Follath24eed8d2019-11-22 13:21:35 +0000490 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100493 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496 }
497
498 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200500 */
501 *p = (unsigned char *) end;
502
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100503 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508/*
509 * RSAPublicKey ::= SEQUENCE {
510 * modulus INTEGER, -- n
511 * publicExponent INTEGER -- e
512 * }
513 */
514static int pk_get_rsapubkey( unsigned char **p,
515 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517{
Janos Follath24eed8d2019-11-22 13:21:35 +0000518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200519 size_t len;
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
522 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100523 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200524
525 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100526 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
527 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100529 /* Import N */
530 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100531 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200532
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100533 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
534 NULL, 0, NULL, 0 ) ) != 0 )
535 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
536
537 *p += len;
538
539 /* Import E */
540 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100541 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100542
543 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
544 NULL, 0, *p, len ) ) != 0 )
545 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
546
547 *p += len;
548
Hanno Becker895c5ab2018-01-05 08:08:09 +0000549 if( mbedtls_rsa_complete( rsa ) != 0 ||
550 mbedtls_rsa_check_pubkey( rsa ) != 0 )
551 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100552 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000553 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100554
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100556 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
557 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200558
Paul Bakker1a7550a2013-09-15 13:01:22 +0200559 return( 0 );
560}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200562
563/* Get a PK algorithm identifier
564 *
565 * AlgorithmIdentifier ::= SEQUENCE {
566 * algorithm OBJECT IDENTIFIER,
567 * parameters ANY DEFINED BY algorithm OPTIONAL }
568 */
569static int pk_get_pk_alg( unsigned char **p,
570 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200572{
Janos Follath24eed8d2019-11-22 13:21:35 +0000573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100579 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_ALG, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
582 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200583
584 /*
585 * No parameters with RSA (only for EC)
586 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( *pk_alg == MBEDTLS_PK_RSA &&
588 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589 params->len != 0 ) )
590 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200592 }
593
594 return( 0 );
595}
596
597/*
598 * SubjectPublicKeyInfo ::= SEQUENCE {
599 * algorithm AlgorithmIdentifier,
600 * subjectPublicKey BIT STRING }
601 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
603 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200604{
Janos Follath24eed8d2019-11-22 13:21:35 +0000605 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200606 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_asn1_buf alg_params;
608 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
609 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200610
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
613 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200614 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100615 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200616 }
617
618 end = *p + len;
619
620 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
621 return( ret );
622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100624 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200625
626 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100627 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
628 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
631 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200632
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200633 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200634 return( ret );
635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#if defined(MBEDTLS_RSA_C)
637 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_RSA_C */
642#if defined(MBEDTLS_ECP_C)
643 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#endif /* MBEDTLS_ECP_C */
650 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200651
652 if( ret == 0 && *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100653 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
654 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200655
656 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658
659 return( ret );
660}
661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200663/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100664 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
665 *
666 * The value zero is:
667 * - never a valid value for an RSA parameter
668 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
669 *
670 * Since values can't be omitted in PKCS#1, passing a zero value to
671 * rsa_complete() would be incorrect, so reject zero values early.
672 */
673static int asn1_get_nonzero_mpi( unsigned char **p,
674 const unsigned char *end,
675 mbedtls_mpi *X )
676{
677 int ret;
678
679 ret = mbedtls_asn1_get_mpi( p, end, X );
680 if( ret != 0 )
681 return( ret );
682
683 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
684 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
685
686 return( 0 );
687}
688
689/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200690 * Parse a PKCS#1 encoded private RSA key
691 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693 const unsigned char *key,
694 size_t keylen )
695{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100696 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697 size_t len;
698 unsigned char *p, *end;
699
Hanno Beckerefa14e82017-10-11 19:45:19 +0100700 mbedtls_mpi T;
701 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100702
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703 p = (unsigned char *) key;
704 end = p + keylen;
705
706 /*
707 * This function parses the RSAPrivateKey (PKCS#1)
708 *
709 * RSAPrivateKey ::= SEQUENCE {
710 * version Version,
711 * modulus INTEGER, -- n
712 * publicExponent INTEGER, -- e
713 * privateExponent INTEGER, -- d
714 * prime1 INTEGER, -- p
715 * prime2 INTEGER, -- q
716 * exponent1 INTEGER, -- d mod (p-1)
717 * exponent2 INTEGER, -- d mod (q-1)
718 * coefficient INTEGER, -- (inverse of q) mod p
719 * otherPrimeInfos OtherPrimeInfos OPTIONAL
720 * }
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
723 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200724 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100725 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200726 }
727
728 end = p + len;
729
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100730 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100732 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733 }
734
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100735 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738 }
739
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100740 /* Import N */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100741 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
742 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
743 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100744 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100746 /* Import E */
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, NULL, NULL, NULL,
749 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100750 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100751
752 /* Import D */
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 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100756 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757
758 /* Import P */
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, &T, NULL,
761 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100762 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763
764 /* Import Q */
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, NULL, &T,
767 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100768 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100769
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100770#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500771 /*
772 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
773 * that they can be easily recomputed from D, P and Q. However by
774 * parsing them from the PKCS1 structure it is possible to avoid
775 * recalculating them which both reduces the overhead of loading
776 * RSA private keys into memory and also avoids side channels which
777 * can arise when computing those values, since all of D, P, and Q
778 * are secret. See https://eprint.iacr.org/2020/055 for a
779 * description of one such attack.
780 */
781
Jack Lloyd80cc8112020-01-22 17:34:29 -0500782 /* Import DP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100783 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
784 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500785 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500786
787 /* Import DQ */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100788 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
789 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500790 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500791
792 /* Import QP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100793 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
794 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500795 goto cleanup;
796
Jack Lloyd60239752020-01-27 17:53:36 -0500797#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800798 /* Verify existence of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100799 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
800 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
801 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500802 goto cleanup;
803#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500804
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100805 /* rsa_complete() doesn't complete anything with the default
806 * implementation but is still called:
807 * - for the benefit of alternative implementation that may want to
808 * pre-compute stuff beyond what's provided (eg Montgomery factors)
809 * - as is also sanity-checks the key
810 *
811 * Furthermore, we also check the public part for consistency with
812 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
813 */
814 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
815 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
816 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100817 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100818 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100819
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820 if( p != end )
821 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100822 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
823 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200824 }
825
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100826cleanup:
827
Hanno Beckerefa14e82017-10-11 19:45:19 +0100828 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100829
830 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100832 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100833 if( ( ret & 0xff80 ) == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100834 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100835 else
836 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839 }
840
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100841 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200845#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846/*
847 * Parse a SEC1 encoded private EC key
848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200850 const unsigned char *key, size_t keylen,
851 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852{
Janos Follath24eed8d2019-11-22 13:21:35 +0000853 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100854 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700856 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200857 unsigned char *p = (unsigned char *) key;
858 unsigned char *end = p + keylen;
859 unsigned char *end2;
860
861 /*
862 * RFC 5915, or SEC1 Appendix C.4
863 *
864 * ECPrivateKey ::= SEQUENCE {
865 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
866 * privateKey OCTET STRING,
867 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
868 * publicKey [1] BIT STRING OPTIONAL
869 * }
870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
872 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100874 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875 }
876
877 end = p + len;
878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
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 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100891 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892 }
893
894 p += len;
895
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200896 pubkey_done = 0;
897 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200899 /*
900 * Is 'parameters' present?
901 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200902 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
903 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200904 {
905 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
906 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
907 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200908 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200909 return( ret );
910 }
911 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200912 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100915 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100916 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800917 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200918
Jethro Beekmand2df9362018-02-16 13:11:04 -0800919 if( p != end )
920 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200921 /*
922 * Is 'publickey' present? If not, or if we can't read it (eg because it
923 * is compressed), create it from the private key.
924 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200925 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
926 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200927 {
928 end2 = p + len;
929
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200930 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100931 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200932
933 if( p + len != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100934 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
935 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200936
937 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
938 pubkey_done = 1;
939 else
940 {
941 /*
942 * The only acceptable failure mode of pk_get_ecpubkey() above
943 * is if the point format is not recognized.
944 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200945 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
946 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200947 }
948 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200949 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200950 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200951 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100952 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200953 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100955
956 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200958 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200959 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100961 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200962 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200967 return( ret );
968 }
969
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200970 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200973
974/*
975 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100976 *
977 * Notes:
978 *
979 * - This function does not own the key buffer. It is the
980 * responsibility of the caller to take care of zeroizing
981 * and freeing it after use.
982 *
983 * - The function is responsible for freeing the provided
984 * PK context on failure.
985 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986 */
987static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200988 mbedtls_pk_context *pk,
989 const unsigned char* key, size_t keylen,
990 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991{
992 int ret, version;
993 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 unsigned char *p = (unsigned char *) key;
996 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
998 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001000#if !defined(MBEDTLS_ECP_C)
1001 (void) f_rng;
1002 (void) p_rng;
1003#endif
1004
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001006 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 *
1008 * PrivateKeyInfo ::= SEQUENCE {
1009 * version Version,
1010 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1011 * privateKey PrivateKey,
1012 * attributes [0] IMPLICIT Attributes OPTIONAL }
1013 *
1014 * Version ::= INTEGER
1015 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1016 * PrivateKey ::= OCTET STRING
1017 *
1018 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1019 */
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1022 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001024 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 }
1026
1027 end = p + len;
1028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001030 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031
1032 if( version != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001033 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034
1035 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Chris Jonesfdb588b2021-04-14 18:15:24 +01001036 {
Chris Jones4d01c5b2021-04-28 14:12:07 +01001037 return( ret );
Chris Jonesfdb588b2021-04-14 18:15:24 +01001038 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001041 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001042
1043 if( len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001044 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1045 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1048 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001050 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051 return( ret );
1052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#if defined(MBEDTLS_RSA_C)
1054 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059 return( ret );
1060 }
1061 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_RSA_C */
1063#if defined(MBEDTLS_ECP_C)
1064 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001067 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001068 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070 return( ret );
1071 }
1072 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073#endif /* MBEDTLS_ECP_C */
1074 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001076 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001077}
1078
1079/*
1080 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001081 *
1082 * To save space, the decryption happens in-place on the given key buffer.
1083 * Also, while this function may modify the keybuffer, it doesn't own it,
1084 * and instead it is the responsibility of the caller to zeroize and properly
1085 * free it after use.
1086 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001087 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001089static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001090 mbedtls_pk_context *pk,
1091 unsigned char *key, size_t keylen,
1092 const unsigned char *pwd, size_t pwdlen,
1093 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094{
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 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#endif /* MBEDTLS_PKCS12_C */
1162#if defined(MBEDTLS_PKCS5_C)
1163 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 p, len, buf ) ) != 0 )
1167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1169 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170
1171 return( ret );
1172 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001173
1174 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175 }
1176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001178 {
1179 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001180 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001182 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001184
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001185 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len, f_rng, p_rng ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188
1189/*
1190 * Parse a private key
1191 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193 const unsigned char *key, size_t keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001194 const unsigned char *pwd, size_t pwdlen,
1195 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196{
Janos Follath24eed8d2019-11-22 13:21:35 +00001197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001202#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001204 if( keylen == 0 )
1205 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001206
1207#if defined(MBEDTLS_PEM_PARSE_C)
1208 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001211 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001212 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001213 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1214 else
1215 ret = mbedtls_pem_read_buffer( &pem,
1216 "-----BEGIN RSA PRIVATE KEY-----",
1217 "-----END RSA PRIVATE KEY-----",
1218 key, pwd, pwdlen, &len );
1219
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220 if( ret == 0 )
1221 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001222 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001223 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001225 pem.buf, pem.buflen ) ) != 0 )
1226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228 }
1229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231 return( ret );
1232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1234 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1235 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1236 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1237 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001242 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001243 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001244 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1245 else
1246 ret = mbedtls_pem_read_buffer( &pem,
1247 "-----BEGIN EC PRIVATE KEY-----",
1248 "-----END EC PRIVATE KEY-----",
1249 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 if( ret == 0 )
1251 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001252 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253
Hanno Beckerfab35692017-08-25 13:38:26 +01001254 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001256 pem.buf, pem.buflen,
1257 f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 }
1261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263 return( ret );
1264 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1266 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1267 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1268 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1269 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001272
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001273 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001274 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001275 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1276 else
1277 ret = mbedtls_pem_read_buffer( &pem,
1278 "-----BEGIN PRIVATE KEY-----",
1279 "-----END PRIVATE KEY-----",
1280 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 if( ret == 0 )
1282 {
1283 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001284 pem.buf, pem.buflen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287 }
1288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 return( ret );
1291 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 return( ret );
1294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001296 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001297 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001298 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1299 else
1300 ret = mbedtls_pem_read_buffer( &pem,
1301 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1302 "-----END ENCRYPTED PRIVATE KEY-----",
1303 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 if( ret == 0 )
1305 {
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001306 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, pem.buf, pem.buflen,
1307 pwd, pwdlen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 }
1311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 return( ret );
1314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318#else
1319 ((void) pwd);
1320 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322
1323 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001324 * At this point we only know it's not a PEM formatted key. Could be any
1325 * of the known DER encoded private key formats
1326 *
1327 * We try the different DER format parsers to see if one passes without
1328 * error
1329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine0ca21952021-12-10 17:36:37 +01001331 if( pwdlen != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001333 unsigned char *key_copy;
1334
1335 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1336 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1337
1338 memcpy( key_copy, key, keylen );
1339
1340 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001341 pwd, pwdlen, f_rng, p_rng );
Hanno Beckerfab35692017-08-25 13:38:26 +01001342
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001343 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001344 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345 }
1346
Hanno Beckerfab35692017-08-25 13:38:26 +01001347 if( ret == 0 )
1348 return( 0 );
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001351 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354 {
1355 return( ret );
1356 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Kenneth Soerensene28d49b2019-01-03 12:39:29 +01001359 ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen, f_rng, p_rng );
1360 if( ret == 0 )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001361 {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362 return( 0 );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001363 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001366 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369
Hanno Becker9be19262017-09-08 12:39:44 +01001370 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001371 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1372 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373 {
1374 return( 0 );
1375 }
1376
Hanno Becker780f0a42018-10-10 11:23:33 +01001377 mbedtls_pk_free( pk );
1378 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001382 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001383 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1384 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001385 key, keylen, f_rng, p_rng ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386 {
1387 return( 0 );
1388 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001389 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391
Hanno Becker780f0a42018-10-10 11:23:33 +01001392 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1393 * it is ok to leave the PK context initialized but not
1394 * freed: It is the caller's responsibility to call pk_init()
1395 * before calling this function, and to call pk_free()
1396 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1397 * isn't, this leads to mbedtls_pk_free() being called
1398 * twice, once here and once by the caller, but this is
1399 * also ok and in line with the mbedtls_pk_free() calls
1400 * on failed PEM parsing attempts. */
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403}
1404
1405/*
1406 * Parse a public key
1407 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 const unsigned char *key, size_t keylen )
1410{
Janos Follath24eed8d2019-11-22 13:21:35 +00001411 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001413#if defined(MBEDTLS_RSA_C)
1414 const mbedtls_pk_info_t *pk_info;
1415#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001417 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001419#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001421 if( keylen == 0 )
1422 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001423
1424#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001426#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001427 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001428 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001429 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1430 else
1431 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001432 "-----BEGIN RSA PUBLIC KEY-----",
1433 "-----END RSA PUBLIC KEY-----",
1434 key, NULL, 0, &len );
1435
1436 if( ret == 0 )
1437 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001438 p = pem.buf;
Andrzej Kurek92d74172022-06-28 10:29:42 -04001439 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
Paul Elliott072d2b02022-05-13 17:08:36 +01001440 {
1441 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001442 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Elliott072d2b02022-05-13 17:08:36 +01001443 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001444
Paul Elliott072d2b02022-05-13 17:08:36 +01001445 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1446 {
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001447 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001448 return( ret );
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001449 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001450
Ron Eldor84df1ae2017-10-16 17:11:52 +03001451 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1452 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001453
Ron Eldord0c56de2017-10-10 17:03:08 +03001454 mbedtls_pem_free( &pem );
1455 return( ret );
1456 }
1457 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1458 {
1459 mbedtls_pem_free( &pem );
1460 return( ret );
1461 }
1462#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001463
1464 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001465 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001466 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1467 else
1468 ret = mbedtls_pem_read_buffer( &pem,
1469 "-----BEGIN PUBLIC KEY-----",
1470 "-----END PUBLIC KEY-----",
1471 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472
1473 if( ret == 0 )
1474 {
1475 /*
1476 * Was PEM encoded
1477 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001478 p = pem.buf;
1479
1480 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1481 mbedtls_pem_free( &pem );
1482 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487 return( ret );
1488 }
Ron Eldor5472d432017-10-17 09:49:00 +03001489 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001491
1492#if defined(MBEDTLS_RSA_C)
1493 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1494 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1495
1496 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1497 return( ret );
1498
Ron Eldor9566ff72018-02-07 18:59:41 +02001499 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001500 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001501 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001502 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001503 return( ret );
1504 }
1505 mbedtls_pk_free( ctx );
Chris Jones9f7a6932021-04-14 12:12:09 +01001506 if( ret != ( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
1507 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001508 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001509 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001510 }
1511#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001512 p = (unsigned char *) key;
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515
Paul Bakker1a7550a2013-09-15 13:01:22 +02001516 return( ret );
1517}
1518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001519#endif /* MBEDTLS_PK_PARSE_C */