blob: 9b6516511a9838e022ae4eebdb352ffa19871fd8 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020049#endif
50
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000051#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020052
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050053/* Parameter validation macros based on platform_util.h */
54#define PK_VALIDATE_RET( cond ) \
55 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
56#define PK_VALIDATE( cond ) \
57 MBEDTLS_INTERNAL_VALIDATE( cond )
58
Gilles Peskine832f3492017-11-30 11:42:12 +010059#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020060/*
61 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020062 *
63 * The file is expected to contain either PEM or DER encoded data.
64 * A terminating null byte is always appended. It is included in the announced
65 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020066 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020068{
69 FILE *f;
70 long size;
71
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050072 PK_VALIDATE_RET( path != NULL );
73 PK_VALIDATE_RET( buf != NULL );
74 PK_VALIDATE_RET( n != NULL );
75
Paul Bakker1a7550a2013-09-15 13:01:22 +020076 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020078
Gilles Peskineda0913b2022-06-30 17:03:40 +020079 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
80 mbedtls_setbuf( f, NULL );
81
Paul Bakker1a7550a2013-09-15 13:01:22 +020082 fseek( f, 0, SEEK_END );
83 if( ( size = ftell( f ) ) == -1 )
84 {
85 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020087 }
88 fseek( f, 0, SEEK_SET );
89
90 *n = (size_t) size;
91
92 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020093 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +020094 {
95 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020096 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 }
98
99 if( fread( *buf, 1, *n, f ) != *n )
100 {
101 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100102
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500103 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107 }
108
109 fclose( f );
110
111 (*buf)[*n] = '\0';
112
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200113 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
114 ++*n;
115
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116 return( 0 );
117}
118
119/*
120 * Load and parse a private key
121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200123 const char *path, const char *pwd,
124 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125{
Janos Follath24eed8d2019-11-22 13:21:35 +0000126 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127 size_t n;
128 unsigned char *buf;
129
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500130 PK_VALIDATE_RET( ctx != NULL );
131 PK_VALIDATE_RET( path != NULL );
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134 return( ret );
135
136 if( pwd == NULL )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200137 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0, f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200138 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 ret = mbedtls_pk_parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200140 (const unsigned char *) pwd, strlen( pwd ), f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500142 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144
145 return( ret );
146}
147
148/*
149 * Load and parse a public key
150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200152{
Janos Follath24eed8d2019-11-22 13:21:35 +0000153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154 size_t n;
155 unsigned char *buf;
156
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500157 PK_VALIDATE_RET( ctx != NULL );
158 PK_VALIDATE_RET( path != NULL );
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200161 return( ret );
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500165 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167
168 return( ret );
169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_ECP_C)
173/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174 *
175 * ECParameters ::= CHOICE {
176 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100177 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200179 * }
180 */
181static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200183{
Janos Follath24eed8d2019-11-22 13:21:35 +0000184 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185
Sanne Woudab2b29d52017-08-21 15:58:12 +0100186 if ( end - *p < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100187 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
188 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100189
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100190 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200191 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( params->tag != MBEDTLS_ASN1_OID
193#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
194 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100195#endif
196 )
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,
199 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100200 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100203 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100204 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100205 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206
207 params->p = *p;
208 *p += params->len;
209
210 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100211 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
212 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200213
214 return( 0 );
215}
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200218/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100219 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
220 * WARNING: the resulting group should only be used with
221 * pk_group_id_from_specified(), since its base point may not be set correctly
222 * if it was encoded compressed.
223 *
224 * SpecifiedECDomain ::= SEQUENCE {
225 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
226 * fieldID FieldID {{FieldTypes}},
227 * curve Curve,
228 * base ECPoint,
229 * order INTEGER,
230 * cofactor INTEGER OPTIONAL,
231 * hash HashAlgorithm OPTIONAL,
232 * ...
233 * }
234 *
235 * We only support prime-field as field type, and ignore hash and cofactor.
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100238{
Janos Follath24eed8d2019-11-22 13:21:35 +0000239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100240 unsigned char *p = params->p;
241 const unsigned char * const end = params->p + params->len;
242 const unsigned char *end_field, *end_curve;
243 size_t len;
244 int ver;
245
246 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100248 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100249
250 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100252
253 /*
254 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
255 * fieldType FIELD-ID.&id({IOSet}),
256 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
257 * }
258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
260 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100261 return( ret );
262
263 end_field = p + len;
264
265 /*
266 * FIELD-ID ::= TYPE-IDENTIFIER
267 * FieldTypes FIELD-ID ::= {
268 * { Prime-p IDENTIFIED BY prime-field } |
269 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
270 * }
271 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100274 return( ret );
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
277 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100280 }
281
282 p += len;
283
284 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100286 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200288 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100289
290 if( p != end_field )
Chris Jones9f7a6932021-04-14 12:12:09 +0100291 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
292 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100293
294 /*
295 * Curve ::= SEQUENCE {
296 * a FieldElement,
297 * b FieldElement,
298 * seed BIT STRING OPTIONAL
299 * -- Shall be present if used in SpecifiedECDomain
300 * -- with version equal to ecdpVer2 or ecdpVer3
301 * }
302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
304 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100305 return( ret );
306
307 end_curve = p + len;
308
309 /*
310 * FieldElement ::= OCTET STRING
311 * containing an integer in the case of a prime field
312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
314 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100316 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100317 }
318
319 p += len;
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
322 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100324 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325 }
326
327 p += len;
328
329 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331 p += len;
332
333 if( p != end_curve )
Chris Jones9f7a6932021-04-14 12:12:09 +0100334 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
335 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100336
337 /*
338 * ECPoint ::= OCTET STRING
339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100341 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100344 ( const unsigned char *) p, len ) ) != 0 )
345 {
346 /*
347 * If we can't read the point because it's compressed, cheat by
348 * reading only the X coordinate and the parity bit of Y.
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100351 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 len != mbedtls_mpi_size( &grp->P ) + 1 ||
353 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
354 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
355 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100358 }
359 }
360
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100361 p += len;
362
363 /*
364 * order INTEGER
365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100367 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200369 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
371 /*
372 * Allow optional elements by purposefully not enforcing p == end here.
373 */
374
375 return( 0 );
376}
377
378/*
379 * Find the group id associated with an (almost filled) group as generated by
380 * pk_group_from_specified(), or return an error if unknown.
381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382static 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 +0100383{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100384 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 mbedtls_ecp_group ref;
386 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391 {
392 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200394 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395
396 /* Compare to the group we were given, starting with easy tests */
397 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
399 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
400 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
401 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
402 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
403 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 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 +0100406 {
407 break;
408 }
409
410 }
411
412cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414
415 *grp_id = *id;
416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
418 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419
420 return( ret );
421}
422
423/*
424 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
427 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428{
Janos Follath24eed8d2019-11-22 13:21:35 +0000429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100433
434 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
435 goto cleanup;
436
437 ret = pk_group_id_from_group( &grp, grp_id );
438
439cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441
442 return( ret );
443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100445
446/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200447 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448 *
449 * ECParameters ::= CHOICE {
450 * namedCurve OBJECT IDENTIFIER
451 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
452 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200453 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200455{
Janos Follath24eed8d2019-11-22 13:21:35 +0000456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
462 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463 }
464 else
465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
468 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100469#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100471#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100472 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200473
474 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800475 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
478 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200480 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481 return( ret );
482
483 return( 0 );
484}
485
486/*
487 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100488 *
489 * The caller is responsible for clearing the structure upon failure if
490 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200492 */
493static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200495{
Janos Follath24eed8d2019-11-22 13:21:35 +0000496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100499 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502 }
503
504 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506 */
507 *p = (unsigned char *) end;
508
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100509 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514/*
515 * RSAPublicKey ::= SEQUENCE {
516 * modulus INTEGER, -- n
517 * publicExponent INTEGER -- e
518 * }
519 */
520static int pk_get_rsapubkey( unsigned char **p,
521 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200523{
Janos Follath24eed8d2019-11-22 13:21:35 +0000524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525 size_t len;
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
528 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100529 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530
531 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100532 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
533 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200534
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100535 /* Import N */
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 ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200538
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100539 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
540 NULL, 0, NULL, 0 ) ) != 0 )
541 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
542
543 *p += len;
544
545 /* Import E */
546 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100547 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100548
549 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
550 NULL, 0, *p, len ) ) != 0 )
551 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
552
553 *p += len;
554
Hanno Becker895c5ab2018-01-05 08:08:09 +0000555 if( mbedtls_rsa_complete( rsa ) != 0 ||
556 mbedtls_rsa_check_pubkey( rsa ) != 0 )
557 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100558 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000559 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100560
Paul Bakker1a7550a2013-09-15 13:01:22 +0200561 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100562 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
563 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200564
Paul Bakker1a7550a2013-09-15 13:01:22 +0200565 return( 0 );
566}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568
569/* Get a PK algorithm identifier
570 *
571 * AlgorithmIdentifier ::= SEQUENCE {
572 * algorithm OBJECT IDENTIFIER,
573 * parameters ANY DEFINED BY algorithm OPTIONAL }
574 */
575static int pk_get_pk_alg( unsigned char **p,
576 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200578{
Janos Follath24eed8d2019-11-22 13:21:35 +0000579 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100585 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_ALG, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
588 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589
590 /*
591 * No parameters with RSA (only for EC)
592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( *pk_alg == MBEDTLS_PK_RSA &&
594 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200595 params->len != 0 ) )
596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200598 }
599
600 return( 0 );
601}
602
603/*
604 * SubjectPublicKeyInfo ::= SEQUENCE {
605 * algorithm AlgorithmIdentifier,
606 * subjectPublicKey BIT STRING }
607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
609 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200610{
Janos Follath24eed8d2019-11-22 13:21:35 +0000611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200612 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_asn1_buf alg_params;
614 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
615 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200616
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500617 PK_VALIDATE_RET( p != NULL );
618 PK_VALIDATE_RET( *p != NULL );
619 PK_VALIDATE_RET( end != NULL );
620 PK_VALIDATE_RET( pk != NULL );
621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
623 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200624 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100625 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200626 }
627
628 end = *p + len;
629
630 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
631 return( ret );
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100634 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635
636 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100637 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
638 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
641 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200643 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644 return( ret );
645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646#if defined(MBEDTLS_RSA_C)
647 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#endif /* MBEDTLS_RSA_C */
652#if defined(MBEDTLS_ECP_C)
653 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659#endif /* MBEDTLS_ECP_C */
660 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661
662 if( ret == 0 && *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100663 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
664 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200665
666 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668
669 return( ret );
670}
671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200673/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100674 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
675 *
676 * The value zero is:
677 * - never a valid value for an RSA parameter
678 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
679 *
680 * Since values can't be omitted in PKCS#1, passing a zero value to
681 * rsa_complete() would be incorrect, so reject zero values early.
682 */
683static int asn1_get_nonzero_mpi( unsigned char **p,
684 const unsigned char *end,
685 mbedtls_mpi *X )
686{
687 int ret;
688
689 ret = mbedtls_asn1_get_mpi( p, end, X );
690 if( ret != 0 )
691 return( ret );
692
693 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
694 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
695
696 return( 0 );
697}
698
699/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700 * Parse a PKCS#1 encoded private RSA key
701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703 const unsigned char *key,
704 size_t keylen )
705{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100706 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200707 size_t len;
708 unsigned char *p, *end;
709
Hanno Beckerefa14e82017-10-11 19:45:19 +0100710 mbedtls_mpi T;
711 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100712
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713 p = (unsigned char *) key;
714 end = p + keylen;
715
716 /*
717 * This function parses the RSAPrivateKey (PKCS#1)
718 *
719 * RSAPrivateKey ::= SEQUENCE {
720 * version Version,
721 * modulus INTEGER, -- n
722 * publicExponent INTEGER, -- e
723 * privateExponent INTEGER, -- d
724 * prime1 INTEGER, -- p
725 * prime2 INTEGER, -- q
726 * exponent1 INTEGER, -- d mod (p-1)
727 * exponent2 INTEGER, -- d mod (q-1)
728 * coefficient INTEGER, -- (inverse of q) mod p
729 * otherPrimeInfos OtherPrimeInfos OPTIONAL
730 * }
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
733 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200734 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100735 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200736 }
737
738 end = p + len;
739
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100740 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100742 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743 }
744
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100745 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200748 }
749
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100750 /* Import N */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100751 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
752 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
753 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100754 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100756 /* Import E */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100757 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
758 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
759 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100761
762 /* Import D */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100763 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
764 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
765 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100766 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100767
768 /* Import P */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100769 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
770 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
771 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100773
774 /* Import Q */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100775 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
776 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
777 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100778 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100779
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100780#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500781 /*
782 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
783 * that they can be easily recomputed from D, P and Q. However by
784 * parsing them from the PKCS1 structure it is possible to avoid
785 * recalculating them which both reduces the overhead of loading
786 * RSA private keys into memory and also avoids side channels which
787 * can arise when computing those values, since all of D, P, and Q
788 * are secret. See https://eprint.iacr.org/2020/055 for a
789 * description of one such attack.
790 */
791
Jack Lloyd80cc8112020-01-22 17:34:29 -0500792 /* Import DP */
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->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500795 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500796
797 /* Import DQ */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100798 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
799 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500800 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500801
802 /* Import QP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100803 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
804 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500805 goto cleanup;
806
Jack Lloyd60239752020-01-27 17:53:36 -0500807#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800808 /* Verify existence of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100809 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
810 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
811 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500812 goto cleanup;
813#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500814
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100815 /* rsa_complete() doesn't complete anything with the default
816 * implementation but is still called:
817 * - for the benefit of alternative implementation that may want to
818 * pre-compute stuff beyond what's provided (eg Montgomery factors)
819 * - as is also sanity-checks the key
820 *
821 * Furthermore, we also check the public part for consistency with
822 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
823 */
824 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
825 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
826 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100827 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100828 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100829
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830 if( p != end )
831 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100832 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
833 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834 }
835
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100836cleanup:
837
Hanno Beckerefa14e82017-10-11 19:45:19 +0100838 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839
840 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100842 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100843 if( ( ret & 0xff80 ) == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100844 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845 else
846 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849 }
850
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100851 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200853#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856/*
857 * Parse a SEC1 encoded private EC key
858 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200860 const unsigned char *key, size_t keylen,
861 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862{
Janos Follath24eed8d2019-11-22 13:21:35 +0000863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100864 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700866 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867 unsigned char *p = (unsigned char *) key;
868 unsigned char *end = p + keylen;
869 unsigned char *end2;
870
871 /*
872 * RFC 5915, or SEC1 Appendix C.4
873 *
874 * ECPrivateKey ::= SEQUENCE {
875 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
876 * privateKey OCTET STRING,
877 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
878 * publicKey [1] BIT STRING OPTIONAL
879 * }
880 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200881 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
882 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100884 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 }
886
887 end = p + len;
888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100890 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200891
892 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100896 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100901 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902 }
903
904 p += len;
905
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200906 pubkey_done = 0;
907 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200909 /*
910 * Is 'parameters' present?
911 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200912 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
913 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200914 {
915 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
916 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
917 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200918 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200919 return( ret );
920 }
921 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200922 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200924 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100925 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100926 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800927 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200928
Jethro Beekmand2df9362018-02-16 13:11:04 -0800929 if( p != end )
930 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200931 /*
932 * Is 'publickey' present? If not, or if we can't read it (eg because it
933 * is compressed), create it from the private key.
934 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200935 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
936 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200937 {
938 end2 = p + len;
939
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200940 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100941 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200942
943 if( p + len != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100944 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
945 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200946
947 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
948 pubkey_done = 1;
949 else
950 {
951 /*
952 * The only acceptable failure mode of pk_get_ecpubkey() above
953 * is if the point format is not recognized.
954 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200955 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
956 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200957 }
958 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200959 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200960 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200961 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100962 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200963 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200964 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100965
966 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200968 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100971 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200972 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977 return( ret );
978 }
979
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200980 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200983
984/*
985 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100986 *
987 * Notes:
988 *
989 * - This function does not own the key buffer. It is the
990 * responsibility of the caller to take care of zeroizing
991 * and freeing it after use.
992 *
993 * - The function is responsible for freeing the provided
994 * PK context on failure.
995 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996 */
997static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200998 mbedtls_pk_context *pk,
999 const unsigned char* key, size_t keylen,
1000 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001{
1002 int ret, version;
1003 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 unsigned char *p = (unsigned char *) key;
1006 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1008 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001010#if !defined(MBEDTLS_ECP_C)
1011 (void) f_rng;
1012 (void) p_rng;
1013#endif
1014
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001016 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 *
1018 * PrivateKeyInfo ::= SEQUENCE {
1019 * version Version,
1020 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1021 * privateKey PrivateKey,
1022 * attributes [0] IMPLICIT Attributes OPTIONAL }
1023 *
1024 * Version ::= INTEGER
1025 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1026 * PrivateKey ::= OCTET STRING
1027 *
1028 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1029 */
1030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1032 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001034 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 }
1036
1037 end = p + len;
1038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001040 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
1042 if( version != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001043 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044
1045 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Chris Jonesfdb588b2021-04-14 18:15:24 +01001046 {
Chris Jones4d01c5b2021-04-28 14:12:07 +01001047 return( ret );
Chris Jonesfdb588b2021-04-14 18:15:24 +01001048 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001051 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052
1053 if( len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001054 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1055 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1058 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001060 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061 return( ret );
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_RSA_C)
1064 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069 return( ret );
1070 }
1071 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_RSA_C */
1073#if defined(MBEDTLS_ECP_C)
1074 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001077 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080 return( ret );
1081 }
1082 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#endif /* MBEDTLS_ECP_C */
1084 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001086 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001087}
1088
1089/*
1090 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001091 *
1092 * To save space, the decryption happens in-place on the given key buffer.
1093 * Also, while this function may modify the keybuffer, it doesn't own it,
1094 * and instead it is the responsibility of the caller to zeroize and properly
1095 * free it after use.
1096 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001099static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001100 mbedtls_pk_context *pk,
1101 unsigned char *key, size_t keylen,
1102 const unsigned char *pwd, size_t pwdlen,
1103 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001105 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001106 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001107 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1110#if defined(MBEDTLS_PKCS12_C)
1111 mbedtls_cipher_type_t cipher_alg;
1112 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113#endif
1114
Hanno Becker2aa80a72017-09-07 15:28:45 +01001115 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 end = p + keylen;
1117
1118 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120
1121 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001122 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 *
1124 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1125 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1126 * encryptedData EncryptedData
1127 * }
1128 *
1129 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1130 *
1131 * EncryptedData ::= OCTET STRING
1132 *
1133 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001134 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1137 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001139 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140 }
1141
1142 end = p + len;
1143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001145 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001148 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149
Hanno Beckerfab35692017-08-25 13:38:26 +01001150 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151
1152 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001153 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155#if defined(MBEDTLS_PKCS12_C)
1156 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 cipher_alg, md_alg,
1160 pwd, pwdlen, p, len, buf ) ) != 0 )
1161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1163 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164
1165 return( ret );
1166 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001167
1168 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171#endif /* MBEDTLS_PKCS12_C */
1172#if defined(MBEDTLS_PKCS5_C)
1173 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 p, len, buf ) ) != 0 )
1177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1179 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180
1181 return( ret );
1182 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001183
1184 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185 }
1186 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001188 {
1189 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001190 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001192 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001194
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001195 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len, f_rng, p_rng ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198
1199/*
1200 * Parse a private key
1201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203 const unsigned char *key, size_t keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001204 const unsigned char *pwd, size_t pwdlen,
1205 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206{
Janos Follath24eed8d2019-11-22 13:21:35 +00001207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001212#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001214 PK_VALIDATE_RET( pk != NULL );
1215 if( keylen == 0 )
1216 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1217 PK_VALIDATE_RET( key != NULL );
1218
1219#if defined(MBEDTLS_PEM_PARSE_C)
1220 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001223 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001224 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001225 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1226 else
1227 ret = mbedtls_pem_read_buffer( &pem,
1228 "-----BEGIN RSA PRIVATE KEY-----",
1229 "-----END RSA PRIVATE KEY-----",
1230 key, pwd, pwdlen, &len );
1231
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232 if( ret == 0 )
1233 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001234 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001235 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237 pem.buf, pem.buflen ) ) != 0 )
1238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240 }
1241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243 return( ret );
1244 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1246 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1247 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1248 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1249 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001254 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001255 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001256 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1257 else
1258 ret = mbedtls_pem_read_buffer( &pem,
1259 "-----BEGIN EC PRIVATE KEY-----",
1260 "-----END EC PRIVATE KEY-----",
1261 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 if( ret == 0 )
1263 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001264 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265
Hanno Beckerfab35692017-08-25 13:38:26 +01001266 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001268 pem.buf, pem.buflen,
1269 f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001272 }
1273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 return( ret );
1276 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1278 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1279 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1280 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1281 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001285 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001286 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001287 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1288 else
1289 ret = mbedtls_pem_read_buffer( &pem,
1290 "-----BEGIN PRIVATE KEY-----",
1291 "-----END PRIVATE KEY-----",
1292 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 if( ret == 0 )
1294 {
1295 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001296 pem.buf, pem.buflen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 }
1300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 return( ret );
1303 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 return( ret );
1306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001308 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001309 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001310 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1311 else
1312 ret = mbedtls_pem_read_buffer( &pem,
1313 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1314 "-----END ENCRYPTED PRIVATE KEY-----",
1315 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 if( ret == 0 )
1317 {
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001318 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, pem.buf, pem.buflen,
1319 pwd, pwdlen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 }
1323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 return( ret );
1326 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330#else
1331 ((void) pwd);
1332 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334
1335 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001336 * At this point we only know it's not a PEM formatted key. Could be any
1337 * of the known DER encoded private key formats
1338 *
1339 * We try the different DER format parsers to see if one passes without
1340 * error
1341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine0ca21952021-12-10 17:36:37 +01001343 if( pwdlen != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001345 unsigned char *key_copy;
1346
1347 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1348 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1349
1350 memcpy( key_copy, key, keylen );
1351
1352 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001353 pwd, pwdlen, f_rng, p_rng );
Hanno Beckerfab35692017-08-25 13:38:26 +01001354
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001355 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001356 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 }
1358
Hanno Beckerfab35692017-08-25 13:38:26 +01001359 if( ret == 0 )
1360 return( 0 );
1361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001363 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366 {
1367 return( ret );
1368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370
Kenneth Soerensene28d49b2019-01-03 12:39:29 +01001371 ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen, f_rng, p_rng );
1372 if( ret == 0 )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001373 {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 return( 0 );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001375 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001378 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Hanno Becker9be19262017-09-08 12:39:44 +01001382 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001383 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1384 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385 {
1386 return( 0 );
1387 }
1388
Hanno Becker780f0a42018-10-10 11:23:33 +01001389 mbedtls_pk_free( pk );
1390 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001394 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001395 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1396 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001397 key, keylen, f_rng, p_rng ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398 {
1399 return( 0 );
1400 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001401 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403
Hanno Becker780f0a42018-10-10 11:23:33 +01001404 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1405 * it is ok to leave the PK context initialized but not
1406 * freed: It is the caller's responsibility to call pk_init()
1407 * before calling this function, and to call pk_free()
1408 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1409 * isn't, this leads to mbedtls_pk_free() being called
1410 * twice, once here and once by the caller, but this is
1411 * also ok and in line with the mbedtls_pk_free() calls
1412 * on failed PEM parsing attempts. */
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415}
1416
1417/*
1418 * Parse a public key
1419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421 const unsigned char *key, size_t keylen )
1422{
Janos Follath24eed8d2019-11-22 13:21:35 +00001423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001425#if defined(MBEDTLS_RSA_C)
1426 const mbedtls_pk_info_t *pk_info;
1427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001431#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001433 PK_VALIDATE_RET( ctx != NULL );
1434 if( keylen == 0 )
1435 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1436 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1437
1438#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001440#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001441 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001442 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001443 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1444 else
1445 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001446 "-----BEGIN RSA PUBLIC KEY-----",
1447 "-----END RSA PUBLIC KEY-----",
1448 key, NULL, 0, &len );
1449
1450 if( ret == 0 )
1451 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001452 p = pem.buf;
Andrzej Kurek92d74172022-06-28 10:29:42 -04001453 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
Paul Elliott072d2b02022-05-13 17:08:36 +01001454 {
1455 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001456 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Elliott072d2b02022-05-13 17:08:36 +01001457 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001458
Paul Elliott072d2b02022-05-13 17:08:36 +01001459 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1460 {
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001461 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001462 return( ret );
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001463 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001464
Ron Eldor84df1ae2017-10-16 17:11:52 +03001465 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1466 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001467
Ron Eldord0c56de2017-10-10 17:03:08 +03001468 mbedtls_pem_free( &pem );
1469 return( ret );
1470 }
1471 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1472 {
1473 mbedtls_pem_free( &pem );
1474 return( ret );
1475 }
1476#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001477
1478 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001479 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001480 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1481 else
1482 ret = mbedtls_pem_read_buffer( &pem,
1483 "-----BEGIN PUBLIC KEY-----",
1484 "-----END PUBLIC KEY-----",
1485 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486
1487 if( ret == 0 )
1488 {
1489 /*
1490 * Was PEM encoded
1491 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001492 p = pem.buf;
1493
1494 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1495 mbedtls_pem_free( &pem );
1496 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501 return( ret );
1502 }
Ron Eldor5472d432017-10-17 09:49:00 +03001503 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001505
1506#if defined(MBEDTLS_RSA_C)
1507 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1508 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1509
1510 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1511 return( ret );
1512
Ron Eldor9566ff72018-02-07 18:59:41 +02001513 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001514 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001515 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001516 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001517 return( ret );
1518 }
1519 mbedtls_pk_free( ctx );
Chris Jones9f7a6932021-04-14 12:12:09 +01001520 if( ret != ( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
1521 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001522 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001523 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001524 }
1525#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526 p = (unsigned char *) key;
1527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 return( ret );
1531}
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#endif /* MBEDTLS_PK_PARSE_C */