blob: 2a9a55862712ca73d76321d1162dc14061272627 [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
Gilles Peskine832f3492017-11-30 11:42:12 +010059#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020060/*
61 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020062 *
63 * The file is expected to contain either PEM or DER encoded data.
64 * A terminating null byte is always appended. It is included in the announced
65 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020068{
69 FILE *f;
70 long size;
71
72 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020074
Gilles Peskineda0913b2022-06-30 17:03:40 +020075 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
76 mbedtls_setbuf( f, NULL );
77
Paul Bakker1a7550a2013-09-15 13:01:22 +020078 fseek( f, 0, SEEK_END );
79 if( ( size = ftell( f ) ) == -1 )
80 {
81 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020083 }
84 fseek( f, 0, SEEK_SET );
85
86 *n = (size_t) size;
87
88 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020089 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 {
91 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020092 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +020093 }
94
95 if( fread( *buf, 1, *n, f ) != *n )
96 {
97 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010098
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050099 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 }
104
105 fclose( f );
106
107 (*buf)[*n] = '\0';
108
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200109 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
110 ++*n;
111
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112 return( 0 );
113}
114
115/*
116 * Load and parse a private key
117 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200119 const char *path, const char *pwd,
120 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121{
Janos Follath24eed8d2019-11-22 13:21:35 +0000122 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123 size_t n;
124 unsigned char *buf;
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127 return( ret );
128
129 if( pwd == NULL )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200130 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0, f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 ret = mbedtls_pk_parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200133 (const unsigned char *) pwd, strlen( pwd ), f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500135 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137
138 return( ret );
139}
140
141/*
142 * Load and parse a public key
143 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145{
Janos Follath24eed8d2019-11-22 13:21:35 +0000146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147 size_t n;
148 unsigned char *buf;
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151 return( ret );
152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500155 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200157
158 return( ret );
159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#if defined(MBEDTLS_ECP_C)
163/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164 *
165 * ECParameters ::= CHOICE {
166 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100167 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169 * }
170 */
171static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173{
Janos Follath24eed8d2019-11-22 13:21:35 +0000174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175
Sanne Woudab2b29d52017-08-21 15:58:12 +0100176 if ( end - *p < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100177 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
178 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100179
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100180 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 if( params->tag != MBEDTLS_ASN1_OID
183#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
184 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100185#endif
186 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100187 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100188 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
189 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100190 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100193 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100194 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100195 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200196
197 params->p = *p;
198 *p += params->len;
199
200 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100201 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
202 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200203
204 return( 0 );
205}
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200208/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100209 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
210 * WARNING: the resulting group should only be used with
211 * pk_group_id_from_specified(), since its base point may not be set correctly
212 * if it was encoded compressed.
213 *
214 * SpecifiedECDomain ::= SEQUENCE {
215 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
216 * fieldID FieldID {{FieldTypes}},
217 * curve Curve,
218 * base ECPoint,
219 * order INTEGER,
220 * cofactor INTEGER OPTIONAL,
221 * hash HashAlgorithm OPTIONAL,
222 * ...
223 * }
224 *
225 * We only support prime-field as field type, and ignore hash and cofactor.
226 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100228{
Janos Follath24eed8d2019-11-22 13:21:35 +0000229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100230 unsigned char *p = params->p;
231 const unsigned char * const end = params->p + params->len;
232 const unsigned char *end_field, *end_curve;
233 size_t len;
234 int ver;
235
236 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100238 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100239
240 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100242
243 /*
244 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
245 * fieldType FIELD-ID.&id({IOSet}),
246 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
247 * }
248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
250 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100251 return( ret );
252
253 end_field = p + len;
254
255 /*
256 * FIELD-ID ::= TYPE-IDENTIFIER
257 * FieldTypes FIELD-ID ::= {
258 * { Prime-p IDENTIFIED BY prime-field } |
259 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
260 * }
261 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100264 return( ret );
265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
267 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100270 }
271
272 p += len;
273
274 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100276 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200278 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100279
280 if( p != end_field )
Chris Jones9f7a6932021-04-14 12:12:09 +0100281 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
282 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100283
284 /*
285 * Curve ::= SEQUENCE {
286 * a FieldElement,
287 * b FieldElement,
288 * seed BIT STRING OPTIONAL
289 * -- Shall be present if used in SpecifiedECDomain
290 * -- with version equal to ecdpVer2 or ecdpVer3
291 * }
292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
294 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100295 return( ret );
296
297 end_curve = p + len;
298
299 /*
300 * FieldElement ::= OCTET STRING
301 * containing an integer in the case of a prime field
302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
304 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100305 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100306 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100307 }
308
309 p += len;
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
312 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100313 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100314 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 }
316
317 p += len;
318
319 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 p += len;
322
323 if( p != end_curve )
Chris Jones9f7a6932021-04-14 12:12:09 +0100324 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
325 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100326
327 /*
328 * ECPoint ::= OCTET STRING
329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100331 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100334 ( const unsigned char *) p, len ) ) != 0 )
335 {
336 /*
337 * If we can't read the point because it's compressed, cheat by
338 * reading only the X coordinate and the parity bit of Y.
339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100341 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 len != mbedtls_mpi_size( &grp->P ) + 1 ||
343 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
344 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
345 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100348 }
349 }
350
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100351 p += len;
352
353 /*
354 * order INTEGER
355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100357 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100358
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200359 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360
361 /*
362 * Allow optional elements by purposefully not enforcing p == end here.
363 */
364
365 return( 0 );
366}
367
368/*
369 * Find the group id associated with an (almost filled) group as generated by
370 * pk_group_from_specified(), or return an error if unknown.
371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372static 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 +0100373{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100374 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_ecp_group ref;
376 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100381 {
382 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200384 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100385
386 /* Compare to the group we were given, starting with easy tests */
387 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
389 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
390 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
391 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
392 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
393 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 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 +0100396 {
397 break;
398 }
399
400 }
401
402cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404
405 *grp_id = *id;
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
408 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100409
410 return( ret );
411}
412
413/*
414 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
417 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418{
Janos Follath24eed8d2019-11-22 13:21:35 +0000419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100423
424 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
425 goto cleanup;
426
427 ret = pk_group_id_from_group( &grp, grp_id );
428
429cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
432 return( ret );
433}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
436/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200437 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438 *
439 * ECParameters ::= CHOICE {
440 * namedCurve OBJECT IDENTIFIER
441 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
442 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200443 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200445{
Janos Follath24eed8d2019-11-22 13:21:35 +0000446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
452 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453 }
454 else
455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100457 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
458 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100459#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100461#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200463
464 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800465 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200466 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
468 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200469
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200470 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200471 return( ret );
472
473 return( 0 );
474}
475
476/*
477 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100478 *
479 * The caller is responsible for clearing the structure upon failure if
480 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482 */
483static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200485{
Janos Follath24eed8d2019-11-22 13:21:35 +0000486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100489 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200492 }
493
494 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496 */
497 *p = (unsigned char *) end;
498
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100499 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200500}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504/*
505 * RSAPublicKey ::= SEQUENCE {
506 * modulus INTEGER, -- n
507 * publicExponent INTEGER -- e
508 * }
509 */
510static int pk_get_rsapubkey( unsigned char **p,
511 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513{
Janos Follath24eed8d2019-11-22 13:21:35 +0000514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200515 size_t len;
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
518 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100519 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200520
521 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100522 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
523 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200524
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100525 /* Import N */
526 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100527 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100529 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
530 NULL, 0, NULL, 0 ) ) != 0 )
531 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
532
533 *p += len;
534
535 /* Import E */
536 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100537 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100538
539 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
540 NULL, 0, *p, len ) ) != 0 )
541 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
542
543 *p += len;
544
Hanno Becker895c5ab2018-01-05 08:08:09 +0000545 if( mbedtls_rsa_complete( rsa ) != 0 ||
546 mbedtls_rsa_check_pubkey( rsa ) != 0 )
547 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100548 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000549 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100550
Paul Bakker1a7550a2013-09-15 13:01:22 +0200551 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100552 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
553 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200554
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555 return( 0 );
556}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200558
559/* Get a PK algorithm identifier
560 *
561 * AlgorithmIdentifier ::= SEQUENCE {
562 * algorithm OBJECT IDENTIFIER,
563 * parameters ANY DEFINED BY algorithm OPTIONAL }
564 */
565static int pk_get_pk_alg( unsigned char **p,
566 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568{
Janos Follath24eed8d2019-11-22 13:21:35 +0000569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100575 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_ALG, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
578 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200579
580 /*
581 * No parameters with RSA (only for EC)
582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 if( *pk_alg == MBEDTLS_PK_RSA &&
584 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585 params->len != 0 ) )
586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200588 }
589
590 return( 0 );
591}
592
593/*
594 * SubjectPublicKeyInfo ::= SEQUENCE {
595 * algorithm AlgorithmIdentifier,
596 * subjectPublicKey BIT STRING }
597 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
599 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600{
Janos Follath24eed8d2019-11-22 13:21:35 +0000601 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200602 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_asn1_buf alg_params;
604 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
605 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
608 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200609 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100610 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200611 }
612
613 end = *p + len;
614
615 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
616 return( ret );
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100619 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620
621 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100622 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
623 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
626 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200628 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200629 return( ret );
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_RSA_C)
632 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_RSA_C */
637#if defined(MBEDTLS_ECP_C)
638 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200641 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_ECP_C */
645 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646
647 if( ret == 0 && *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100648 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
649 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650
651 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200653
654 return( ret );
655}
656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100659 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
660 *
661 * The value zero is:
662 * - never a valid value for an RSA parameter
663 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
664 *
665 * Since values can't be omitted in PKCS#1, passing a zero value to
666 * rsa_complete() would be incorrect, so reject zero values early.
667 */
668static int asn1_get_nonzero_mpi( unsigned char **p,
669 const unsigned char *end,
670 mbedtls_mpi *X )
671{
672 int ret;
673
674 ret = mbedtls_asn1_get_mpi( p, end, X );
675 if( ret != 0 )
676 return( ret );
677
678 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
679 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
680
681 return( 0 );
682}
683
684/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685 * Parse a PKCS#1 encoded private RSA key
686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688 const unsigned char *key,
689 size_t keylen )
690{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100691 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692 size_t len;
693 unsigned char *p, *end;
694
Hanno Beckerefa14e82017-10-11 19:45:19 +0100695 mbedtls_mpi T;
696 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100697
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698 p = (unsigned char *) key;
699 end = p + keylen;
700
701 /*
702 * This function parses the RSAPrivateKey (PKCS#1)
703 *
704 * RSAPrivateKey ::= SEQUENCE {
705 * version Version,
706 * modulus INTEGER, -- n
707 * publicExponent INTEGER, -- e
708 * privateExponent INTEGER, -- d
709 * prime1 INTEGER, -- p
710 * prime2 INTEGER, -- q
711 * exponent1 INTEGER, -- d mod (p-1)
712 * exponent2 INTEGER, -- d mod (q-1)
713 * coefficient INTEGER, -- (inverse of q) mod p
714 * otherPrimeInfos OtherPrimeInfos OPTIONAL
715 * }
716 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
718 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100720 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721 }
722
723 end = p + len;
724
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100725 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200726 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100727 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728 }
729
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100730 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733 }
734
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100735 /* Import N */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100736 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
737 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
738 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100739 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100741 /* Import E */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100742 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
743 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
744 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100745 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100746
747 /* Import D */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100748 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
749 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
750 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100751 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100752
753 /* Import P */
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, NULL, &T, NULL,
756 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100758
759 /* Import Q */
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, &T,
762 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100764
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100765#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500766 /*
767 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
768 * that they can be easily recomputed from D, P and Q. However by
769 * parsing them from the PKCS1 structure it is possible to avoid
770 * recalculating them which both reduces the overhead of loading
771 * RSA private keys into memory and also avoids side channels which
772 * can arise when computing those values, since all of D, P, and Q
773 * are secret. See https://eprint.iacr.org/2020/055 for a
774 * description of one such attack.
775 */
776
Jack Lloyd80cc8112020-01-22 17:34:29 -0500777 /* Import DP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100778 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
779 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500780 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500781
782 /* Import DQ */
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->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500785 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500786
787 /* Import QP */
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->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500790 goto cleanup;
791
Jack Lloyd60239752020-01-27 17:53:36 -0500792#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800793 /* Verify existence of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100794 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
795 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
796 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500797 goto cleanup;
798#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500799
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100800 /* rsa_complete() doesn't complete anything with the default
801 * implementation but is still called:
802 * - for the benefit of alternative implementation that may want to
803 * pre-compute stuff beyond what's provided (eg Montgomery factors)
804 * - as is also sanity-checks the key
805 *
806 * Furthermore, we also check the public part for consistency with
807 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
808 */
809 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
810 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
811 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100812 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100813 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814
Paul Bakker1a7550a2013-09-15 13:01:22 +0200815 if( p != end )
816 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100817 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
818 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819 }
820
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100821cleanup:
822
Hanno Beckerefa14e82017-10-11 19:45:19 +0100823 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100824
825 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100827 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100828 if( ( ret & 0xff80 ) == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100829 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100830 else
831 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200833 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834 }
835
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100836 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841/*
842 * Parse a SEC1 encoded private EC key
843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200845 const unsigned char *key, size_t keylen,
846 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200847{
Janos Follath24eed8d2019-11-22 13:21:35 +0000848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100849 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700851 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852 unsigned char *p = (unsigned char *) key;
853 unsigned char *end = p + keylen;
854 unsigned char *end2;
855
856 /*
857 * RFC 5915, or SEC1 Appendix C.4
858 *
859 * ECPrivateKey ::= SEQUENCE {
860 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
861 * privateKey OCTET STRING,
862 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
863 * publicKey [1] BIT STRING OPTIONAL
864 * }
865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
867 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100869 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870 }
871
872 end = p + len;
873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100875 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876
877 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100881 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100886 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 }
888
889 p += len;
890
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200891 pubkey_done = 0;
892 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200894 /*
895 * Is 'parameters' present?
896 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200897 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
898 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200899 {
900 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
901 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
902 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200903 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200904 return( ret );
905 }
906 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200907 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100910 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100911 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800912 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200913
Jethro Beekmand2df9362018-02-16 13:11:04 -0800914 if( p != end )
915 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200916 /*
917 * Is 'publickey' present? If not, or if we can't read it (eg because it
918 * is compressed), create it from the private key.
919 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200920 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
921 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200922 {
923 end2 = p + len;
924
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200925 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100926 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200927
928 if( p + len != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100929 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
930 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200931
932 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
933 pubkey_done = 1;
934 else
935 {
936 /*
937 * The only acceptable failure mode of pk_get_ecpubkey() above
938 * is if the point format is not recognized.
939 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200940 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
941 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200942 }
943 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200944 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200945 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200946 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100947 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200948 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200949 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100950
951 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200952 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200953 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100956 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200957 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200962 return( ret );
963 }
964
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200965 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200968
969/*
970 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100971 *
972 * Notes:
973 *
974 * - This function does not own the key buffer. It is the
975 * responsibility of the caller to take care of zeroizing
976 * and freeing it after use.
977 *
978 * - The function is responsible for freeing the provided
979 * PK context on failure.
980 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 */
982static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200983 mbedtls_pk_context *pk,
984 const unsigned char* key, size_t keylen,
985 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986{
987 int ret, version;
988 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990 unsigned char *p = (unsigned char *) key;
991 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
993 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200994
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +0200995#if !defined(MBEDTLS_ECP_C)
996 (void) f_rng;
997 (void) p_rng;
998#endif
999
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001001 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001002 *
1003 * PrivateKeyInfo ::= SEQUENCE {
1004 * version Version,
1005 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1006 * privateKey PrivateKey,
1007 * attributes [0] IMPLICIT Attributes OPTIONAL }
1008 *
1009 * Version ::= INTEGER
1010 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1011 * PrivateKey ::= OCTET STRING
1012 *
1013 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1014 */
1015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1017 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001019 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 }
1021
1022 end = p + len;
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001025 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026
1027 if( version != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001028 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029
1030 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Chris Jonesfdb588b2021-04-14 18:15:24 +01001031 {
Chris Jones4d01c5b2021-04-28 14:12:07 +01001032 return( ret );
Chris Jonesfdb588b2021-04-14 18:15:24 +01001033 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001036 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037
1038 if( len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001039 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1040 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1043 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001045 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 return( ret );
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_RSA_C)
1049 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054 return( ret );
1055 }
1056 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057#endif /* MBEDTLS_RSA_C */
1058#if defined(MBEDTLS_ECP_C)
1059 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001062 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065 return( ret );
1066 }
1067 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#endif /* MBEDTLS_ECP_C */
1069 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001071 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001072}
1073
1074/*
1075 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001076 *
1077 * To save space, the decryption happens in-place on the given key buffer.
1078 * Also, while this function may modify the keybuffer, it doesn't own it,
1079 * and instead it is the responsibility of the caller to zeroize and properly
1080 * free it after use.
1081 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001085 mbedtls_pk_context *pk,
1086 unsigned char *key, size_t keylen,
1087 const unsigned char *pwd, size_t pwdlen,
1088 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001089{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001090 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001092 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1095#if defined(MBEDTLS_PKCS12_C)
1096 mbedtls_cipher_type_t cipher_alg;
1097 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098#endif
1099
Hanno Becker2aa80a72017-09-07 15:28:45 +01001100 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001101 end = p + keylen;
1102
1103 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105
1106 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001107 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108 *
1109 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1110 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1111 * encryptedData EncryptedData
1112 * }
1113 *
1114 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1115 *
1116 * EncryptedData ::= OCTET STRING
1117 *
1118 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001119 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001124 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125 }
1126
1127 end = p + len;
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001130 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001133 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134
Hanno Beckerfab35692017-08-25 13:38:26 +01001135 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136
1137 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001138 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_PKCS12_C)
1141 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144 cipher_alg, md_alg,
1145 pwd, pwdlen, p, len, buf ) ) != 0 )
1146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1148 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149
1150 return( ret );
1151 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001152
1153 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156#endif /* MBEDTLS_PKCS12_C */
1157#if defined(MBEDTLS_PKCS5_C)
1158 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 p, len, buf ) ) != 0 )
1162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1164 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165
1166 return( ret );
1167 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001168
1169 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 }
1171 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001173 {
1174 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001175 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001177 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001179
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001180 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len, f_rng, p_rng ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183
1184/*
1185 * Parse a private key
1186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188 const unsigned char *key, size_t keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001189 const unsigned char *pwd, size_t pwdlen,
1190 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191{
Janos Follath24eed8d2019-11-22 13:21:35 +00001192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001197#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001199 if( keylen == 0 )
1200 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001201
1202#if defined(MBEDTLS_PEM_PARSE_C)
1203 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001206 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001207 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001208 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1209 else
1210 ret = mbedtls_pem_read_buffer( &pem,
1211 "-----BEGIN RSA PRIVATE KEY-----",
1212 "-----END RSA PRIVATE KEY-----",
1213 key, pwd, pwdlen, &len );
1214
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 if( ret == 0 )
1216 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001217 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001218 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220 pem.buf, pem.buflen ) ) != 0 )
1221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223 }
1224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226 return( ret );
1227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1229 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1230 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1231 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1232 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001233 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001237 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001238 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001239 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1240 else
1241 ret = mbedtls_pem_read_buffer( &pem,
1242 "-----BEGIN EC PRIVATE KEY-----",
1243 "-----END EC PRIVATE KEY-----",
1244 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 if( ret == 0 )
1246 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001247 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248
Hanno Beckerfab35692017-08-25 13:38:26 +01001249 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001251 pem.buf, pem.buflen,
1252 f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255 }
1256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 return( ret );
1259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1261 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1262 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1263 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1264 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001268 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001269 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001270 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1271 else
1272 ret = mbedtls_pem_read_buffer( &pem,
1273 "-----BEGIN PRIVATE KEY-----",
1274 "-----END PRIVATE KEY-----",
1275 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 if( ret == 0 )
1277 {
1278 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001279 pem.buf, pem.buflen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282 }
1283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 return( ret );
1286 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 return( ret );
1289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001291 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001292 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001293 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1294 else
1295 ret = mbedtls_pem_read_buffer( &pem,
1296 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1297 "-----END ENCRYPTED PRIVATE KEY-----",
1298 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 if( ret == 0 )
1300 {
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001301 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, pem.buf, pem.buflen,
1302 pwd, pwdlen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 }
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 return( ret );
1309 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313#else
1314 ((void) pwd);
1315 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317
1318 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001319 * At this point we only know it's not a PEM formatted key. Could be any
1320 * of the known DER encoded private key formats
1321 *
1322 * We try the different DER format parsers to see if one passes without
1323 * error
1324 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine0ca21952021-12-10 17:36:37 +01001326 if( pwdlen != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001328 unsigned char *key_copy;
1329
1330 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1331 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1332
1333 memcpy( key_copy, key, keylen );
1334
1335 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001336 pwd, pwdlen, f_rng, p_rng );
Hanno Beckerfab35692017-08-25 13:38:26 +01001337
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001338 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001339 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340 }
1341
Hanno Beckerfab35692017-08-25 13:38:26 +01001342 if( ret == 0 )
1343 return( 0 );
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001346 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349 {
1350 return( ret );
1351 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Kenneth Soerensene28d49b2019-01-03 12:39:29 +01001354 ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen, f_rng, p_rng );
1355 if( ret == 0 )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001356 {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 return( 0 );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001358 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001361 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
Hanno Becker9be19262017-09-08 12:39:44 +01001365 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001366 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1367 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368 {
1369 return( 0 );
1370 }
1371
Hanno Becker780f0a42018-10-10 11:23:33 +01001372 mbedtls_pk_free( pk );
1373 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001377 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001378 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1379 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001380 key, keylen, f_rng, p_rng ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381 {
1382 return( 0 );
1383 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001384 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386
Hanno Becker780f0a42018-10-10 11:23:33 +01001387 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1388 * it is ok to leave the PK context initialized but not
1389 * freed: It is the caller's responsibility to call pk_init()
1390 * before calling this function, and to call pk_free()
1391 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1392 * isn't, this leads to mbedtls_pk_free() being called
1393 * twice, once here and once by the caller, but this is
1394 * also ok and in line with the mbedtls_pk_free() calls
1395 * on failed PEM parsing attempts. */
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398}
1399
1400/*
1401 * Parse a public key
1402 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404 const unsigned char *key, size_t keylen )
1405{
Janos Follath24eed8d2019-11-22 13:21:35 +00001406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001408#if defined(MBEDTLS_RSA_C)
1409 const mbedtls_pk_info_t *pk_info;
1410#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001414#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001416 if( keylen == 0 )
1417 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001418
1419#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001421#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001422 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001423 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001424 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1425 else
1426 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001427 "-----BEGIN RSA PUBLIC KEY-----",
1428 "-----END RSA PUBLIC KEY-----",
1429 key, NULL, 0, &len );
1430
1431 if( ret == 0 )
1432 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001433 p = pem.buf;
Andrzej Kurek92d74172022-06-28 10:29:42 -04001434 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
Paul Elliott072d2b02022-05-13 17:08:36 +01001435 {
1436 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001437 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Elliott072d2b02022-05-13 17:08:36 +01001438 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001439
Paul Elliott072d2b02022-05-13 17:08:36 +01001440 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1441 {
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001442 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001443 return( ret );
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001444 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001445
Ron Eldor84df1ae2017-10-16 17:11:52 +03001446 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1447 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001448
Ron Eldord0c56de2017-10-10 17:03:08 +03001449 mbedtls_pem_free( &pem );
1450 return( ret );
1451 }
1452 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1453 {
1454 mbedtls_pem_free( &pem );
1455 return( ret );
1456 }
1457#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001458
1459 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001460 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001461 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1462 else
1463 ret = mbedtls_pem_read_buffer( &pem,
1464 "-----BEGIN PUBLIC KEY-----",
1465 "-----END PUBLIC KEY-----",
1466 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467
1468 if( ret == 0 )
1469 {
1470 /*
1471 * Was PEM encoded
1472 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001473 p = pem.buf;
1474
1475 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1476 mbedtls_pem_free( &pem );
1477 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 return( ret );
1483 }
Ron Eldor5472d432017-10-17 09:49:00 +03001484 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001486
1487#if defined(MBEDTLS_RSA_C)
1488 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1489 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1490
1491 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1492 return( ret );
1493
Ron Eldor9566ff72018-02-07 18:59:41 +02001494 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001495 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001496 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001497 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001498 return( ret );
1499 }
1500 mbedtls_pk_free( ctx );
Chris Jones9f7a6932021-04-14 12:12:09 +01001501 if( ret != ( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
1502 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001503 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001504 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001505 }
1506#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 p = (unsigned char *) key;
1508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001510
Paul Bakker1a7550a2013-09-15 13:01:22 +02001511 return( ret );
1512}
1513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001514#endif /* MBEDTLS_PK_PARSE_C */