blob: 73d59a6bbe9828fdd9e4796519060d33a5a3feb3 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/pk.h"
25#include "mbedtls/asn1.h"
26#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050027#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000028#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Rich Evans00ab4702015-02-06 13:43:58 +000030#include <string.h>
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020040#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020046#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020049#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#else
54#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020055#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020057#endif
58
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050059/* Parameter validation macros based on platform_util.h */
60#define PK_VALIDATE_RET( cond ) \
61 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
62#define PK_VALIDATE( cond ) \
63 MBEDTLS_INTERNAL_VALIDATE( cond )
64
Gilles Peskine832f3492017-11-30 11:42:12 +010065#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020066/*
67 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020068 *
69 * The file is expected to contain either PEM or DER encoded data.
70 * A terminating null byte is always appended. It is included in the announced
71 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020072 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020074{
75 FILE *f;
76 long size;
77
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050078 PK_VALIDATE_RET( path != NULL );
79 PK_VALIDATE_RET( buf != NULL );
80 PK_VALIDATE_RET( n != NULL );
81
Paul Bakker1a7550a2013-09-15 13:01:22 +020082 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020084
Gilles Peskineda0913b2022-06-30 17:03:40 +020085 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
86 mbedtls_setbuf( f, NULL );
87
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 fseek( f, 0, SEEK_END );
89 if( ( size = ftell( f ) ) == -1 )
90 {
91 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020093 }
94 fseek( f, 0, SEEK_SET );
95
96 *n = (size_t) size;
97
98 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020099 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200100 {
101 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200102 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 }
104
105 if( fread( *buf, 1, *n, f ) != *n )
106 {
107 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100108
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500109 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113 }
114
115 fclose( f );
116
117 (*buf)[*n] = '\0';
118
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200119 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
120 ++*n;
121
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122 return( 0 );
123}
124
125/*
126 * Load and parse a private key
127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200129 const char *path, const char *pwd,
130 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131{
Janos Follath24eed8d2019-11-22 13:21:35 +0000132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133 size_t n;
134 unsigned char *buf;
135
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 PK_VALIDATE_RET( ctx != NULL );
137 PK_VALIDATE_RET( path != NULL );
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140 return( ret );
141
142 if( pwd == NULL )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200143 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0, f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 ret = mbedtls_pk_parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200146 (const unsigned char *) pwd, strlen( pwd ), f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500148 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150
151 return( ret );
152}
153
154/*
155 * Load and parse a public key
156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158{
Janos Follath24eed8d2019-11-22 13:21:35 +0000159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160 size_t n;
161 unsigned char *buf;
162
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500163 PK_VALIDATE_RET( ctx != NULL );
164 PK_VALIDATE_RET( path != NULL );
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167 return( ret );
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500171 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200173
174 return( ret );
175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#if defined(MBEDTLS_ECP_C)
179/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200180 *
181 * ECParameters ::= CHOICE {
182 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100183 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185 * }
186 */
187static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200189{
Janos Follath24eed8d2019-11-22 13:21:35 +0000190 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200191
Sanne Woudab2b29d52017-08-21 15:58:12 +0100192 if ( end - *p < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100193 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
194 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100195
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100196 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 if( params->tag != MBEDTLS_ASN1_OID
199#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
200 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100201#endif
202 )
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,
205 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100206 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100209 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100210 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100211 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212
213 params->p = *p;
214 *p += params->len;
215
216 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100217 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
218 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200219
220 return( 0 );
221}
222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200224/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100225 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
226 * WARNING: the resulting group should only be used with
227 * pk_group_id_from_specified(), since its base point may not be set correctly
228 * if it was encoded compressed.
229 *
230 * SpecifiedECDomain ::= SEQUENCE {
231 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
232 * fieldID FieldID {{FieldTypes}},
233 * curve Curve,
234 * base ECPoint,
235 * order INTEGER,
236 * cofactor INTEGER OPTIONAL,
237 * hash HashAlgorithm OPTIONAL,
238 * ...
239 * }
240 *
241 * We only support prime-field as field type, and ignore hash and cofactor.
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244{
Janos Follath24eed8d2019-11-22 13:21:35 +0000245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100246 unsigned char *p = params->p;
247 const unsigned char * const end = params->p + params->len;
248 const unsigned char *end_field, *end_curve;
249 size_t len;
250 int ver;
251
252 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100254 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100255
256 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100258
259 /*
260 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
261 * fieldType FIELD-ID.&id({IOSet}),
262 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
263 * }
264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
266 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100267 return( ret );
268
269 end_field = p + len;
270
271 /*
272 * FIELD-ID ::= TYPE-IDENTIFIER
273 * FieldTypes FIELD-ID ::= {
274 * { Prime-p IDENTIFIED BY prime-field } |
275 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
276 * }
277 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100280 return( ret );
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
283 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100286 }
287
288 p += len;
289
290 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100292 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100293
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200294 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100295
296 if( p != end_field )
Chris Jones9f7a6932021-04-14 12:12:09 +0100297 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
298 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100299
300 /*
301 * Curve ::= SEQUENCE {
302 * a FieldElement,
303 * b FieldElement,
304 * seed BIT STRING OPTIONAL
305 * -- Shall be present if used in SpecifiedECDomain
306 * -- with version equal to ecdpVer2 or ecdpVer3
307 * }
308 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
310 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311 return( ret );
312
313 end_curve = p + len;
314
315 /*
316 * FieldElement ::= OCTET STRING
317 * containing an integer in the case of a prime field
318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
320 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100322 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 }
324
325 p += len;
326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
328 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100330 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100331 }
332
333 p += len;
334
335 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100337 p += len;
338
339 if( p != end_curve )
Chris Jones9f7a6932021-04-14 12:12:09 +0100340 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
341 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100342
343 /*
344 * ECPoint ::= OCTET STRING
345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100347 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100350 ( const unsigned char *) p, len ) ) != 0 )
351 {
352 /*
353 * If we can't read the point because it's compressed, cheat by
354 * reading only the X coordinate and the parity bit of Y.
355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100357 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 len != mbedtls_mpi_size( &grp->P ) + 1 ||
359 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
360 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
361 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100364 }
365 }
366
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100367 p += len;
368
369 /*
370 * order INTEGER
371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100373 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100374
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200375 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376
377 /*
378 * Allow optional elements by purposefully not enforcing p == end here.
379 */
380
381 return( 0 );
382}
383
384/*
385 * Find the group id associated with an (almost filled) group as generated by
386 * pk_group_from_specified(), or return an error if unknown.
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388static 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 +0100389{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100390 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_ecp_group ref;
392 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100397 {
398 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200400 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100401
402 /* Compare to the group we were given, starting with easy tests */
403 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
405 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
406 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
407 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
408 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
409 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 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 +0100412 {
413 break;
414 }
415
416 }
417
418cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100420
421 *grp_id = *id;
422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
424 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100425
426 return( ret );
427}
428
429/*
430 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
433 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100434{
Janos Follath24eed8d2019-11-22 13:21:35 +0000435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
440 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
441 goto cleanup;
442
443 ret = pk_group_id_from_group( &grp, grp_id );
444
445cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100447
448 return( ret );
449}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451
452/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200453 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100454 *
455 * ECParameters ::= CHOICE {
456 * namedCurve OBJECT IDENTIFIER
457 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
458 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200461{
Janos Follath24eed8d2019-11-22 13:21:35 +0000462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
468 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100469 }
470 else
471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100473 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
474 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100475#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100477#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100478 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479
480 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800481 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200483 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
484 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200485
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200486 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487 return( ret );
488
489 return( 0 );
490}
491
492/*
493 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100494 *
495 * The caller is responsible for clearing the structure upon failure if
496 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498 */
499static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200501{
Janos Follath24eed8d2019-11-22 13:21:35 +0000502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100505 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508 }
509
510 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200512 */
513 *p = (unsigned char *) end;
514
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100515 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200520/*
521 * RSAPublicKey ::= SEQUENCE {
522 * modulus INTEGER, -- n
523 * publicExponent INTEGER -- e
524 * }
525 */
526static int pk_get_rsapubkey( unsigned char **p,
527 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200529{
Janos Follath24eed8d2019-11-22 13:21:35 +0000530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531 size_t len;
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
534 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100535 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536
537 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100538 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
539 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200540
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100541 /* Import N */
542 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100543 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200544
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100545 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
546 NULL, 0, NULL, 0 ) ) != 0 )
547 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
548
549 *p += len;
550
551 /* Import E */
552 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100553 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100554
555 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
556 NULL, 0, *p, len ) ) != 0 )
557 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
558
559 *p += len;
560
Hanno Becker895c5ab2018-01-05 08:08:09 +0000561 if( mbedtls_rsa_complete( rsa ) != 0 ||
562 mbedtls_rsa_check_pubkey( rsa ) != 0 )
563 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100564 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000565 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100566
Paul Bakker1a7550a2013-09-15 13:01:22 +0200567 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100568 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
569 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200570
Paul Bakker1a7550a2013-09-15 13:01:22 +0200571 return( 0 );
572}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200573#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200574
575/* Get a PK algorithm identifier
576 *
577 * AlgorithmIdentifier ::= SEQUENCE {
578 * algorithm OBJECT IDENTIFIER,
579 * parameters ANY DEFINED BY algorithm OPTIONAL }
580 */
581static int pk_get_pk_alg( unsigned char **p,
582 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200584{
Janos Follath24eed8d2019-11-22 13:21:35 +0000585 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100591 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_ALG, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
594 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200595
596 /*
597 * No parameters with RSA (only for EC)
598 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 if( *pk_alg == MBEDTLS_PK_RSA &&
600 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200601 params->len != 0 ) )
602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200604 }
605
606 return( 0 );
607}
608
609/*
610 * SubjectPublicKeyInfo ::= SEQUENCE {
611 * algorithm AlgorithmIdentifier,
612 * subjectPublicKey BIT STRING }
613 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
615 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200616{
Janos Follath24eed8d2019-11-22 13:21:35 +0000617 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200618 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 mbedtls_asn1_buf alg_params;
620 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
621 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200622
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500623 PK_VALIDATE_RET( p != NULL );
624 PK_VALIDATE_RET( *p != NULL );
625 PK_VALIDATE_RET( end != NULL );
626 PK_VALIDATE_RET( pk != NULL );
627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
629 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100631 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200632 }
633
634 end = *p + len;
635
636 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
637 return( ret );
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100640 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200641
642 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100643 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
644 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
647 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200649 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650 return( ret );
651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#if defined(MBEDTLS_RSA_C)
653 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657#endif /* MBEDTLS_RSA_C */
658#if defined(MBEDTLS_ECP_C)
659 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#endif /* MBEDTLS_ECP_C */
666 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200667
668 if( ret == 0 && *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100669 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
670 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200671
672 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674
675 return( ret );
676}
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200679/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100680 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
681 *
682 * The value zero is:
683 * - never a valid value for an RSA parameter
684 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
685 *
686 * Since values can't be omitted in PKCS#1, passing a zero value to
687 * rsa_complete() would be incorrect, so reject zero values early.
688 */
689static int asn1_get_nonzero_mpi( unsigned char **p,
690 const unsigned char *end,
691 mbedtls_mpi *X )
692{
693 int ret;
694
695 ret = mbedtls_asn1_get_mpi( p, end, X );
696 if( ret != 0 )
697 return( ret );
698
699 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
700 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
701
702 return( 0 );
703}
704
705/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706 * Parse a PKCS#1 encoded private RSA key
707 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200709 const unsigned char *key,
710 size_t keylen )
711{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100712 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713 size_t len;
714 unsigned char *p, *end;
715
Hanno Beckerefa14e82017-10-11 19:45:19 +0100716 mbedtls_mpi T;
717 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100718
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719 p = (unsigned char *) key;
720 end = p + keylen;
721
722 /*
723 * This function parses the RSAPrivateKey (PKCS#1)
724 *
725 * RSAPrivateKey ::= SEQUENCE {
726 * version Version,
727 * modulus INTEGER, -- n
728 * publicExponent INTEGER, -- e
729 * privateExponent INTEGER, -- d
730 * prime1 INTEGER, -- p
731 * prime2 INTEGER, -- q
732 * exponent1 INTEGER, -- d mod (p-1)
733 * exponent2 INTEGER, -- d mod (q-1)
734 * coefficient INTEGER, -- (inverse of q) mod p
735 * otherPrimeInfos OtherPrimeInfos OPTIONAL
736 * }
737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
739 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100741 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200742 }
743
744 end = p + len;
745
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100746 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100748 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200749 }
750
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100751 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754 }
755
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100756 /* Import N */
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, &T, NULL, NULL,
759 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100762 /* Import E */
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 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100766 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100767
768 /* Import D */
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, NULL, NULL,
771 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100773
774 /* Import P */
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, &T, NULL,
777 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100778 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100779
780 /* Import Q */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100781 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
782 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
783 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100784 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100785
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100786#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500787 /*
788 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
789 * that they can be easily recomputed from D, P and Q. However by
790 * parsing them from the PKCS1 structure it is possible to avoid
791 * recalculating them which both reduces the overhead of loading
792 * RSA private keys into memory and also avoids side channels which
793 * can arise when computing those values, since all of D, P, and Q
794 * are secret. See https://eprint.iacr.org/2020/055 for a
795 * description of one such attack.
796 */
797
Jack Lloyd80cc8112020-01-22 17:34:29 -0500798 /* Import DP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100799 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
800 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500801 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500802
803 /* Import DQ */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100804 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
805 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500806 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500807
808 /* Import QP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100809 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
810 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500811 goto cleanup;
812
Jack Lloyd60239752020-01-27 17:53:36 -0500813#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800814 /* Verify existence of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100815 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
816 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
817 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500818 goto cleanup;
819#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500820
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100821 /* rsa_complete() doesn't complete anything with the default
822 * implementation but is still called:
823 * - for the benefit of alternative implementation that may want to
824 * pre-compute stuff beyond what's provided (eg Montgomery factors)
825 * - as is also sanity-checks the key
826 *
827 * Furthermore, we also check the public part for consistency with
828 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
829 */
830 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
831 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
832 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100833 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100834 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100835
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836 if( p != end )
837 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100838 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
839 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200840 }
841
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100842cleanup:
843
Hanno Beckerefa14e82017-10-11 19:45:19 +0100844 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845
846 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200847 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100848 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100849 if( ( ret & 0xff80 ) == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100850 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100851 else
852 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200854 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855 }
856
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100857 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862/*
863 * Parse a SEC1 encoded private EC key
864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200865static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200866 const unsigned char *key, size_t keylen,
867 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868{
Janos Follath24eed8d2019-11-22 13:21:35 +0000869 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100870 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700872 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873 unsigned char *p = (unsigned char *) key;
874 unsigned char *end = p + keylen;
875 unsigned char *end2;
876
877 /*
878 * RFC 5915, or SEC1 Appendix C.4
879 *
880 * ECPrivateKey ::= SEQUENCE {
881 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
882 * privateKey OCTET STRING,
883 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
884 * publicKey [1] BIT STRING OPTIONAL
885 * }
886 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
888 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889 {
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
893 end = p + len;
894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 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
898 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100902 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200906 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100907 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200908 }
909
910 p += len;
911
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200912 pubkey_done = 0;
913 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200915 /*
916 * Is 'parameters' present?
917 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200918 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
919 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200920 {
921 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
922 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
923 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200924 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200925 return( ret );
926 }
927 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200928 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100931 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100932 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800933 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200934
Jethro Beekmand2df9362018-02-16 13:11:04 -0800935 if( p != end )
936 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200937 /*
938 * Is 'publickey' present? If not, or if we can't read it (eg because it
939 * is compressed), create it from the private key.
940 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200941 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
942 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200943 {
944 end2 = p + len;
945
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200946 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
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
949 if( p + len != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100950 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
951 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200952
953 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
954 pubkey_done = 1;
955 else
956 {
957 /*
958 * The only acceptable failure mode of pk_get_ecpubkey() above
959 * is if the point format is not recognized.
960 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200961 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
962 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200963 }
964 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200965 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200966 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200967 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100968 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200969 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200970 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100971
972 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200974 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100977 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200978 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200983 return( ret );
984 }
985
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200986 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200989
990/*
991 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100992 *
993 * Notes:
994 *
995 * - This function does not own the key buffer. It is the
996 * responsibility of the caller to take care of zeroizing
997 * and freeing it after use.
998 *
999 * - The function is responsible for freeing the provided
1000 * PK context on failure.
1001 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001002 */
1003static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001004 mbedtls_pk_context *pk,
1005 const unsigned char* key, size_t keylen,
1006 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007{
1008 int ret, version;
1009 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011 unsigned char *p = (unsigned char *) key;
1012 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1014 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +02001016#if !defined(MBEDTLS_ECP_C)
1017 (void) f_rng;
1018 (void) p_rng;
1019#endif
1020
Paul Bakker1a7550a2013-09-15 13:01:22 +02001021 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001022 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 *
1024 * PrivateKeyInfo ::= SEQUENCE {
1025 * version Version,
1026 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1027 * privateKey PrivateKey,
1028 * attributes [0] IMPLICIT Attributes OPTIONAL }
1029 *
1030 * Version ::= INTEGER
1031 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1032 * PrivateKey ::= OCTET STRING
1033 *
1034 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1035 */
1036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1038 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039 {
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
1043 end = p + len;
1044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001046 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047
1048 if( version != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001049 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050
1051 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Chris Jonesfdb588b2021-04-14 18:15:24 +01001052 {
Chris Jones4d01c5b2021-04-28 14:12:07 +01001053 return( ret );
Chris Jonesfdb588b2021-04-14 18:15:24 +01001054 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001057 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058
1059 if( len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001060 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1061 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1064 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001066 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067 return( ret );
1068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#if defined(MBEDTLS_RSA_C)
1070 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 return( ret );
1076 }
1077 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#endif /* MBEDTLS_RSA_C */
1079#if defined(MBEDTLS_ECP_C)
1080 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001083 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 return( ret );
1087 }
1088 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#endif /* MBEDTLS_ECP_C */
1090 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001092 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093}
1094
1095/*
1096 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001097 *
1098 * To save space, the decryption happens in-place on the given key buffer.
1099 * Also, while this function may modify the keybuffer, it doesn't own it,
1100 * and instead it is the responsibility of the caller to zeroize and properly
1101 * free it after use.
1102 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001106 mbedtls_pk_context *pk,
1107 unsigned char *key, size_t keylen,
1108 const unsigned char *pwd, size_t pwdlen,
1109 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001111 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001113 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1116#if defined(MBEDTLS_PKCS12_C)
1117 mbedtls_cipher_type_t cipher_alg;
1118 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119#endif
1120
Hanno Becker2aa80a72017-09-07 15:28:45 +01001121 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001122 end = p + keylen;
1123
1124 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126
1127 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001128 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129 *
1130 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1131 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1132 * encryptedData EncryptedData
1133 * }
1134 *
1135 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1136 *
1137 * EncryptedData ::= OCTET STRING
1138 *
1139 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001140 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001142 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1143 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144 {
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 }
1147
1148 end = p + len;
1149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001151 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001153 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001154 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155
Hanno Beckerfab35692017-08-25 13:38:26 +01001156 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157
1158 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001159 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161#if defined(MBEDTLS_PKCS12_C)
1162 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 cipher_alg, md_alg,
1166 pwd, pwdlen, p, len, buf ) ) != 0 )
1167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1169 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170
1171 return( ret );
1172 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001173
1174 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_PKCS12_C */
1178#if defined(MBEDTLS_PKCS5_C)
1179 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182 p, len, buf ) ) != 0 )
1183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1185 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186
1187 return( ret );
1188 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001189
1190 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191 }
1192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001194 {
1195 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001198 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001200
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001201 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len, f_rng, p_rng ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
1205/*
1206 * Parse a private key
1207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209 const unsigned char *key, size_t keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001210 const unsigned char *pwd, size_t pwdlen,
1211 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001212{
Janos Follath24eed8d2019-11-22 13:21:35 +00001213 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001218#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001220 PK_VALIDATE_RET( pk != NULL );
1221 if( keylen == 0 )
1222 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1223 PK_VALIDATE_RET( key != NULL );
1224
1225#if defined(MBEDTLS_PEM_PARSE_C)
1226 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001229 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001230 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001231 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1232 else
1233 ret = mbedtls_pem_read_buffer( &pem,
1234 "-----BEGIN RSA PRIVATE KEY-----",
1235 "-----END RSA PRIVATE KEY-----",
1236 key, pwd, pwdlen, &len );
1237
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238 if( ret == 0 )
1239 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001240 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001241 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243 pem.buf, pem.buflen ) ) != 0 )
1244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001246 }
1247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249 return( ret );
1250 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1252 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1253 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1254 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1255 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001260 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001261 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001262 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1263 else
1264 ret = mbedtls_pem_read_buffer( &pem,
1265 "-----BEGIN EC PRIVATE KEY-----",
1266 "-----END EC PRIVATE KEY-----",
1267 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268 if( ret == 0 )
1269 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001270 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271
Hanno Beckerfab35692017-08-25 13:38:26 +01001272 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001274 pem.buf, pem.buflen,
1275 f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 }
1279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 return( ret );
1282 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1284 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1285 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1286 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1287 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001291 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001292 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001293 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1294 else
1295 ret = mbedtls_pem_read_buffer( &pem,
1296 "-----BEGIN PRIVATE KEY-----",
1297 "-----END PRIVATE KEY-----",
1298 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299 if( ret == 0 )
1300 {
1301 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001302 pem.buf, pem.buflen, 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 );
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001314 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001315 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001316 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1317 else
1318 ret = mbedtls_pem_read_buffer( &pem,
1319 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1320 "-----END ENCRYPTED PRIVATE KEY-----",
1321 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 if( ret == 0 )
1323 {
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001324 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, pem.buf, pem.buflen,
1325 pwd, pwdlen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 }
1329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331 return( ret );
1332 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001336#else
1337 ((void) pwd);
1338 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340
1341 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001342 * At this point we only know it's not a PEM formatted key. Could be any
1343 * of the known DER encoded private key formats
1344 *
1345 * We try the different DER format parsers to see if one passes without
1346 * error
1347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine0ca21952021-12-10 17:36:37 +01001349 if( pwdlen != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001351 unsigned char *key_copy;
1352
1353 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1354 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1355
1356 memcpy( key_copy, key, keylen );
1357
1358 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001359 pwd, pwdlen, f_rng, p_rng );
Hanno Beckerfab35692017-08-25 13:38:26 +01001360
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001361 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001362 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363 }
1364
Hanno Beckerfab35692017-08-25 13:38:26 +01001365 if( ret == 0 )
1366 return( 0 );
1367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001369 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372 {
1373 return( ret );
1374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376
Kenneth Soerensene28d49b2019-01-03 12:39:29 +01001377 ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen, f_rng, p_rng );
1378 if( ret == 0 )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001379 {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 return( 0 );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001381 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001384 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
Hanno Becker9be19262017-09-08 12:39:44 +01001388 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001389 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1390 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391 {
1392 return( 0 );
1393 }
1394
Hanno Becker780f0a42018-10-10 11:23:33 +01001395 mbedtls_pk_free( pk );
1396 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001400 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001401 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1402 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001403 key, keylen, f_rng, p_rng ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404 {
1405 return( 0 );
1406 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001407 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409
Hanno Becker780f0a42018-10-10 11:23:33 +01001410 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1411 * it is ok to leave the PK context initialized but not
1412 * freed: It is the caller's responsibility to call pk_init()
1413 * before calling this function, and to call pk_free()
1414 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1415 * isn't, this leads to mbedtls_pk_free() being called
1416 * twice, once here and once by the caller, but this is
1417 * also ok and in line with the mbedtls_pk_free() calls
1418 * on failed PEM parsing attempts. */
1419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421}
1422
1423/*
1424 * Parse a public key
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427 const unsigned char *key, size_t keylen )
1428{
Janos Follath24eed8d2019-11-22 13:21:35 +00001429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001431#if defined(MBEDTLS_RSA_C)
1432 const mbedtls_pk_info_t *pk_info;
1433#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001437#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001439 PK_VALIDATE_RET( ctx != NULL );
1440 if( keylen == 0 )
1441 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1442 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1443
1444#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001446#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001447 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001448 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001449 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1450 else
1451 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001452 "-----BEGIN RSA PUBLIC KEY-----",
1453 "-----END RSA PUBLIC KEY-----",
1454 key, NULL, 0, &len );
1455
1456 if( ret == 0 )
1457 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001458 p = pem.buf;
Andrzej Kurek92d74172022-06-28 10:29:42 -04001459 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
Paul Elliott072d2b02022-05-13 17:08:36 +01001460 {
1461 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001462 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Elliott072d2b02022-05-13 17:08:36 +01001463 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001464
Paul Elliott072d2b02022-05-13 17:08:36 +01001465 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1466 {
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001467 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001468 return( ret );
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001469 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001470
Ron Eldor84df1ae2017-10-16 17:11:52 +03001471 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1472 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001473
Ron Eldord0c56de2017-10-10 17:03:08 +03001474 mbedtls_pem_free( &pem );
1475 return( ret );
1476 }
1477 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1478 {
1479 mbedtls_pem_free( &pem );
1480 return( ret );
1481 }
1482#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001483
1484 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001485 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001486 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1487 else
1488 ret = mbedtls_pem_read_buffer( &pem,
1489 "-----BEGIN PUBLIC KEY-----",
1490 "-----END PUBLIC KEY-----",
1491 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
1493 if( ret == 0 )
1494 {
1495 /*
1496 * Was PEM encoded
1497 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001498 p = pem.buf;
1499
1500 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1501 mbedtls_pem_free( &pem );
1502 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 return( ret );
1508 }
Ron Eldor5472d432017-10-17 09:49:00 +03001509 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001511
1512#if defined(MBEDTLS_RSA_C)
1513 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1514 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1515
1516 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1517 return( ret );
1518
Ron Eldor9566ff72018-02-07 18:59:41 +02001519 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001520 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001521 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001522 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001523 return( ret );
1524 }
1525 mbedtls_pk_free( ctx );
Chris Jones9f7a6932021-04-14 12:12:09 +01001526 if( ret != ( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
1527 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001528 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001529 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001530 }
1531#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532 p = (unsigned char *) key;
1533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536 return( ret );
1537}
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#endif /* MBEDTLS_PK_PARSE_C */