blob: b9826378d4a91aa561d5073500910364ab8efd61 [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
Gilles Peskine832f3492017-11-30 11:42:12 +010053#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020054/*
55 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020056 *
57 * The file is expected to contain either PEM or DER encoded data.
58 * A terminating null byte is always appended. It is included in the announced
59 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020062{
63 FILE *f;
64 long size;
65
66 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020068
Gilles Peskineda0913b2022-06-30 17:03:40 +020069 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
70 mbedtls_setbuf( f, NULL );
71
Paul Bakker1a7550a2013-09-15 13:01:22 +020072 fseek( f, 0, SEEK_END );
73 if( ( size = ftell( f ) ) == -1 )
74 {
75 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020077 }
78 fseek( f, 0, SEEK_SET );
79
80 *n = (size_t) size;
81
82 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020083 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +020084 {
85 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +020086 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +020087 }
88
89 if( fread( *buf, 1, *n, f ) != *n )
90 {
91 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010092
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050093 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +010095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 }
98
99 fclose( f );
100
101 (*buf)[*n] = '\0';
102
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200103 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
104 ++*n;
105
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106 return( 0 );
107}
108
109/*
110 * Load and parse a private key
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200113 const char *path, const char *pwd,
114 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200115{
Janos Follath24eed8d2019-11-22 13:21:35 +0000116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117 size_t n;
118 unsigned char *buf;
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121 return( ret );
122
123 if( pwd == NULL )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200124 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0, f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 ret = mbedtls_pk_parse_key( ctx, buf, n,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200127 (const unsigned char *) pwd, strlen( pwd ), f_rng, p_rng );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500129 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131
132 return( ret );
133}
134
135/*
136 * Load and parse a public key
137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139{
Janos Follath24eed8d2019-11-22 13:21:35 +0000140 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 size_t n;
142 unsigned char *buf;
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145 return( ret );
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500149 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
152 return( ret );
153}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_ECP_C)
157/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158 *
159 * ECParameters ::= CHOICE {
160 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100161 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200163 * }
164 */
165static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200167{
Janos Follath24eed8d2019-11-22 13:21:35 +0000168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Sanne Woudab2b29d52017-08-21 15:58:12 +0100170 if ( end - *p < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100171 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
172 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100173
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100174 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 if( params->tag != MBEDTLS_ASN1_OID
177#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
178 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100179#endif
180 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100181 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100182 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
183 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100184 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100187 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100188 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100189 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190
191 params->p = *p;
192 *p += params->len;
193
194 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100195 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
196 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197
198 return( 0 );
199}
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200202/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100203 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
204 * WARNING: the resulting group should only be used with
205 * pk_group_id_from_specified(), since its base point may not be set correctly
206 * if it was encoded compressed.
207 *
208 * SpecifiedECDomain ::= SEQUENCE {
209 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
210 * fieldID FieldID {{FieldTypes}},
211 * curve Curve,
212 * base ECPoint,
213 * order INTEGER,
214 * cofactor INTEGER OPTIONAL,
215 * hash HashAlgorithm OPTIONAL,
216 * ...
217 * }
218 *
219 * We only support prime-field as field type, and ignore hash and cofactor.
220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100222{
Janos Follath24eed8d2019-11-22 13:21:35 +0000223 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100224 unsigned char *p = params->p;
225 const unsigned char * const end = params->p + params->len;
226 const unsigned char *end_field, *end_curve;
227 size_t len;
228 int ver;
229
230 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100232 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100233
234 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236
237 /*
238 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
239 * fieldType FIELD-ID.&id({IOSet}),
240 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
241 * }
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
244 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245 return( ret );
246
247 end_field = p + len;
248
249 /*
250 * FIELD-ID ::= TYPE-IDENTIFIER
251 * FieldTypes FIELD-ID ::= {
252 * { Prime-p IDENTIFIED BY prime-field } |
253 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
254 * }
255 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100258 return( ret );
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
261 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100264 }
265
266 p += len;
267
268 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100270 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100271
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200272 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273
274 if( p != end_field )
Chris Jones9f7a6932021-04-14 12:12:09 +0100275 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
276 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277
278 /*
279 * Curve ::= SEQUENCE {
280 * a FieldElement,
281 * b FieldElement,
282 * seed BIT STRING OPTIONAL
283 * -- Shall be present if used in SpecifiedECDomain
284 * -- with version equal to ecdpVer2 or ecdpVer3
285 * }
286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
288 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100289 return( ret );
290
291 end_curve = p + len;
292
293 /*
294 * FieldElement ::= OCTET STRING
295 * containing an integer in the case of a prime field
296 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
298 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100299 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100300 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100301 }
302
303 p += len;
304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
306 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100307 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100308 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100309 }
310
311 p += len;
312
313 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 p += len;
316
317 if( p != end_curve )
Chris Jones9f7a6932021-04-14 12:12:09 +0100318 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
319 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100320
321 /*
322 * ECPoint ::= OCTET STRING
323 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100325 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100328 ( const unsigned char *) p, len ) ) != 0 )
329 {
330 /*
331 * If we can't read the point because it's compressed, cheat by
332 * reading only the X coordinate and the parity bit of Y.
333 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100335 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 len != mbedtls_mpi_size( &grp->P ) + 1 ||
337 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
338 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
339 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100342 }
343 }
344
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100345 p += len;
346
347 /*
348 * order INTEGER
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100351 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100352
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200353 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100354
355 /*
356 * Allow optional elements by purposefully not enforcing p == end here.
357 */
358
359 return( 0 );
360}
361
362/*
363 * Find the group id associated with an (almost filled) group as generated by
364 * pk_group_from_specified(), or return an error if unknown.
365 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366static 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 +0100367{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100368 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 mbedtls_ecp_group ref;
370 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100375 {
376 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200378 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100379
380 /* Compare to the group we were given, starting with easy tests */
381 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
383 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
384 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
385 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
386 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
387 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100388 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 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 +0100390 {
391 break;
392 }
393
394 }
395
396cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398
399 *grp_id = *id;
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
402 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403
404 return( ret );
405}
406
407/*
408 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
409 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
411 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412{
Janos Follath24eed8d2019-11-22 13:21:35 +0000413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
418 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
419 goto cleanup;
420
421 ret = pk_group_id_from_group( &grp, grp_id );
422
423cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100425
426 return( ret );
427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
430/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200431 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100432 *
433 * ECParameters ::= CHOICE {
434 * namedCurve OBJECT IDENTIFIER
435 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
436 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200437 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200439{
Janos Follath24eed8d2019-11-22 13:21:35 +0000440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
446 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100447 }
448 else
449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
452 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100453#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100455#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200457
458 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800459 * grp may already be initialized; if so, make sure IDs match
Paul Bakker1a7550a2013-09-15 13:01:22 +0200460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
462 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200463
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200464 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200465 return( ret );
466
467 return( 0 );
468}
469
470/*
471 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100472 *
473 * The caller is responsible for clearing the structure upon failure if
474 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200476 */
477static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479{
Janos Follath24eed8d2019-11-22 13:21:35 +0000480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100483 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200485 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486 }
487
488 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490 */
491 *p = (unsigned char *) end;
492
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100493 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200494}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498/*
499 * RSAPublicKey ::= SEQUENCE {
500 * modulus INTEGER, -- n
501 * publicExponent INTEGER -- e
502 * }
503 */
504static int pk_get_rsapubkey( unsigned char **p,
505 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200506 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507{
Janos Follath24eed8d2019-11-22 13:21:35 +0000508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509 size_t len;
510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
512 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100513 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514
515 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100516 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
517 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100519 /* Import N */
520 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100521 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200522
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100523 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
524 NULL, 0, NULL, 0 ) ) != 0 )
525 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
526
527 *p += len;
528
529 /* Import E */
530 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100531 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100532
533 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
534 NULL, 0, *p, len ) ) != 0 )
535 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
536
537 *p += len;
538
Hanno Becker895c5ab2018-01-05 08:08:09 +0000539 if( mbedtls_rsa_complete( rsa ) != 0 ||
540 mbedtls_rsa_check_pubkey( rsa ) != 0 )
541 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100542 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000543 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100544
Paul Bakker1a7550a2013-09-15 13:01:22 +0200545 if( *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100546 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
547 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200548
Paul Bakker1a7550a2013-09-15 13:01:22 +0200549 return( 0 );
550}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200552
553/* Get a PK algorithm identifier
554 *
555 * AlgorithmIdentifier ::= SEQUENCE {
556 * algorithm OBJECT IDENTIFIER,
557 * parameters ANY DEFINED BY algorithm OPTIONAL }
558 */
559static int pk_get_pk_alg( unsigned char **p,
560 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200562{
Janos Follath24eed8d2019-11-22 13:21:35 +0000563 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100569 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_ALG, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
572 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200573
574 /*
575 * No parameters with RSA (only for EC)
576 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 if( *pk_alg == MBEDTLS_PK_RSA &&
578 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200579 params->len != 0 ) )
580 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200582 }
583
584 return( 0 );
585}
586
587/*
588 * SubjectPublicKeyInfo ::= SEQUENCE {
589 * algorithm AlgorithmIdentifier,
590 * subjectPublicKey BIT STRING }
591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
593 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200594{
Janos Follath24eed8d2019-11-22 13:21:35 +0000595 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200596 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_asn1_buf alg_params;
598 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
599 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
602 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200603 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100604 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200605 }
606
607 end = *p + len;
608
609 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
610 return( ret );
611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100613 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200614
615 if( *p + len != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100616 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
617 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
620 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200621
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200622 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623 return( ret );
624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#if defined(MBEDTLS_RSA_C)
626 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200629 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630#endif /* MBEDTLS_RSA_C */
631#if defined(MBEDTLS_ECP_C)
632 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200637 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_ECP_C */
639 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640
641 if( ret == 0 && *p != end )
Chris Jones9f7a6932021-04-14 12:12:09 +0100642 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
643 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644
645 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200646 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647
648 return( ret );
649}
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100653 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
654 *
655 * The value zero is:
656 * - never a valid value for an RSA parameter
657 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
658 *
659 * Since values can't be omitted in PKCS#1, passing a zero value to
660 * rsa_complete() would be incorrect, so reject zero values early.
661 */
662static int asn1_get_nonzero_mpi( unsigned char **p,
663 const unsigned char *end,
664 mbedtls_mpi *X )
665{
666 int ret;
667
668 ret = mbedtls_asn1_get_mpi( p, end, X );
669 if( ret != 0 )
670 return( ret );
671
672 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
673 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
674
675 return( 0 );
676}
677
678/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200679 * Parse a PKCS#1 encoded private RSA key
680 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682 const unsigned char *key,
683 size_t keylen )
684{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100685 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200686 size_t len;
687 unsigned char *p, *end;
688
Hanno Beckerefa14e82017-10-11 19:45:19 +0100689 mbedtls_mpi T;
690 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100691
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692 p = (unsigned char *) key;
693 end = p + keylen;
694
695 /*
696 * This function parses the RSAPrivateKey (PKCS#1)
697 *
698 * RSAPrivateKey ::= SEQUENCE {
699 * version Version,
700 * modulus INTEGER, -- n
701 * publicExponent INTEGER, -- e
702 * privateExponent INTEGER, -- d
703 * prime1 INTEGER, -- p
704 * prime2 INTEGER, -- q
705 * exponent1 INTEGER, -- d mod (p-1)
706 * exponent2 INTEGER, -- d mod (q-1)
707 * coefficient INTEGER, -- (inverse of q) mod p
708 * otherPrimeInfos OtherPrimeInfos OPTIONAL
709 * }
710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
712 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100714 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715 }
716
717 end = p + len;
718
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100719 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100721 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200722 }
723
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100724 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 }
728
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100729 /* Import N */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100730 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
731 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
732 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100733 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200734
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100735 /* Import E */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100736 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
737 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
738 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100739 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100740
741 /* Import D */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100742 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
743 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
744 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100745 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100746
747 /* Import P */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100748 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
749 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
750 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100751 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100752
753 /* Import Q */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100754 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
755 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
756 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100758
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100759#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500760 /*
761 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
762 * that they can be easily recomputed from D, P and Q. However by
763 * parsing them from the PKCS1 structure it is possible to avoid
764 * recalculating them which both reduces the overhead of loading
765 * RSA private keys into memory and also avoids side channels which
766 * can arise when computing those values, since all of D, P, and Q
767 * are secret. See https://eprint.iacr.org/2020/055 for a
768 * description of one such attack.
769 */
770
Jack Lloyd80cc8112020-01-22 17:34:29 -0500771 /* Import DP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100772 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
773 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500774 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500775
776 /* Import DQ */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100777 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
778 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500779 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500780
781 /* Import QP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100782 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
783 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500784 goto cleanup;
785
Jack Lloyd60239752020-01-27 17:53:36 -0500786#else
Shaun Case8b0ecbc2021-12-20 21:14:10 -0800787 /* Verify existence of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100788 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
789 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
790 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500791 goto cleanup;
792#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500793
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100794 /* rsa_complete() doesn't complete anything with the default
795 * implementation but is still called:
796 * - for the benefit of alternative implementation that may want to
797 * pre-compute stuff beyond what's provided (eg Montgomery factors)
798 * - as is also sanity-checks the key
799 *
800 * Furthermore, we also check the public part for consistency with
801 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
802 */
803 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
804 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
805 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100806 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100807 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100808
Paul Bakker1a7550a2013-09-15 13:01:22 +0200809 if( p != end )
810 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100811 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
812 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813 }
814
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100815cleanup:
816
Hanno Beckerefa14e82017-10-11 19:45:19 +0100817 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100818
819 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100821 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100822 if( ( ret & 0xff80 ) == 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100823 ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100824 else
825 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200827 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200828 }
829
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100830 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200835/*
836 * Parse a SEC1 encoded private EC key
837 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200838static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200839 const unsigned char *key, size_t keylen,
840 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841{
Janos Follath24eed8d2019-11-22 13:21:35 +0000842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100843 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844 size_t len;
Leonid Rozenboima3008e72022-04-21 17:28:18 -0700845 mbedtls_asn1_buf params = { 0, 0, NULL };
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846 unsigned char *p = (unsigned char *) key;
847 unsigned char *end = p + keylen;
848 unsigned char *end2;
849
850 /*
851 * RFC 5915, or SEC1 Appendix C.4
852 *
853 * ECPrivateKey ::= SEQUENCE {
854 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
855 * privateKey OCTET STRING,
856 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
857 * publicKey [1] BIT STRING OPTIONAL
858 * }
859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
861 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862 {
Chris Jones9f7a6932021-04-14 12:12:09 +0100863 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864 }
865
866 end = p + len;
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100869 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870
871 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100875 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100880 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 }
882
883 p += len;
884
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200885 pubkey_done = 0;
886 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200888 /*
889 * Is 'parameters' present?
890 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200891 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
892 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200893 {
894 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
895 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
896 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200897 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200898 return( ret );
899 }
900 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200901 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200903 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100904 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100905 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800906 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200907
Jethro Beekmand2df9362018-02-16 13:11:04 -0800908 if( p != end )
909 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200910 /*
911 * Is 'publickey' present? If not, or if we can't read it (eg because it
912 * is compressed), create it from the private key.
913 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200914 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
915 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200916 {
917 end2 = p + len;
918
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200919 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100920 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200921
922 if( p + len != end2 )
Chris Jones9f7a6932021-04-14 12:12:09 +0100923 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
924 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200925
926 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
927 pubkey_done = 1;
928 else
929 {
930 /*
931 * The only acceptable failure mode of pk_get_ecpubkey() above
932 * is if the point format is not recognized.
933 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200934 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
935 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200936 }
937 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200938 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200939 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200940 mbedtls_ecp_keypair_free( eck );
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 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200943 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100944
945 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200947 f_rng, p_rng ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_ecp_keypair_free( eck );
Chris Jones9f7a6932021-04-14 12:12:09 +0100950 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200951 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956 return( ret );
957 }
958
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200959 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200962
963/*
964 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100965 *
966 * Notes:
967 *
968 * - This function does not own the key buffer. It is the
969 * responsibility of the caller to take care of zeroizing
970 * and freeing it after use.
971 *
972 * - The function is responsible for freeing the provided
973 * PK context on failure.
974 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975 */
976static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200977 mbedtls_pk_context *pk,
978 const unsigned char* key, size_t keylen,
979 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980{
981 int ret, version;
982 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984 unsigned char *p = (unsigned char *) key;
985 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
987 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988
Manuel Pégourié-Gonnard609ab642021-06-16 14:29:11 +0200989#if !defined(MBEDTLS_ECP_C)
990 (void) f_rng;
991 (void) p_rng;
992#endif
993
Paul Bakker1a7550a2013-09-15 13:01:22 +0200994 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +0100995 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996 *
997 * PrivateKeyInfo ::= SEQUENCE {
998 * version Version,
999 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1000 * privateKey PrivateKey,
1001 * attributes [0] IMPLICIT Attributes OPTIONAL }
1002 *
1003 * Version ::= INTEGER
1004 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1005 * PrivateKey ::= OCTET STRING
1006 *
1007 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1008 */
1009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1011 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001013 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 }
1015
1016 end = p + len;
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001019 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020
1021 if( version != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001022 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_VERSION, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023
1024 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Chris Jonesfdb588b2021-04-14 18:15:24 +01001025 {
Chris Jones4d01c5b2021-04-28 14:12:07 +01001026 return( ret );
Chris Jonesfdb588b2021-04-14 18:15:24 +01001027 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001030 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031
1032 if( len < 1 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001033 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT,
1034 MBEDTLS_ERR_ASN1_OUT_OF_DATA ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1037 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001039 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040 return( ret );
1041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_RSA_C)
1043 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048 return( ret );
1049 }
1050 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#endif /* MBEDTLS_RSA_C */
1052#if defined(MBEDTLS_ECP_C)
1053 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001056 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059 return( ret );
1060 }
1061 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#endif /* MBEDTLS_ECP_C */
1063 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001065 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066}
1067
1068/*
1069 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001070 *
1071 * To save space, the decryption happens in-place on the given key buffer.
1072 * Also, while this function may modify the keybuffer, it doesn't own it,
1073 * and instead it is the responsibility of the caller to zeroize and properly
1074 * free it after use.
1075 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001079 mbedtls_pk_context *pk,
1080 unsigned char *key, size_t keylen,
1081 const unsigned char *pwd, size_t pwdlen,
1082 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001084 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001086 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001087 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1089#if defined(MBEDTLS_PKCS12_C)
1090 mbedtls_cipher_type_t cipher_alg;
1091 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001092#endif
1093
Hanno Becker2aa80a72017-09-07 15:28:45 +01001094 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095 end = p + keylen;
1096
1097 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001099
1100 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001101 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102 *
1103 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1104 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1105 * encryptedData EncryptedData
1106 * }
1107 *
1108 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1109 *
1110 * EncryptedData ::= OCTET STRING
1111 *
1112 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001113 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1116 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117 {
Chris Jones9f7a6932021-04-14 12:12:09 +01001118 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119 }
1120
1121 end = p + len;
1122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001124 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
Chris Jones9f7a6932021-04-14 12:12:09 +01001127 return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT, ret ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128
Hanno Beckerfab35692017-08-25 13:38:26 +01001129 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130
1131 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001132 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_PKCS12_C)
1135 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 cipher_alg, md_alg,
1139 pwd, pwdlen, p, len, buf ) ) != 0 )
1140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1142 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143
1144 return( ret );
1145 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001146
1147 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150#endif /* MBEDTLS_PKCS12_C */
1151#if defined(MBEDTLS_PKCS5_C)
1152 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155 p, len, buf ) ) != 0 )
1156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1158 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159
1160 return( ret );
1161 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001162
1163 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 }
1165 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001167 {
1168 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001169 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001171 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001173
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001174 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len, f_rng, p_rng ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177
1178/*
1179 * Parse a private key
1180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182 const unsigned char *key, size_t keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001183 const unsigned char *pwd, size_t pwdlen,
1184 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185{
Janos Follath24eed8d2019-11-22 13:21:35 +00001186 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001191#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001193 if( keylen == 0 )
1194 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001195
1196#if defined(MBEDTLS_PEM_PARSE_C)
1197 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001200 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001201 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001202 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1203 else
1204 ret = mbedtls_pem_read_buffer( &pem,
1205 "-----BEGIN RSA PRIVATE KEY-----",
1206 "-----END RSA PRIVATE KEY-----",
1207 key, pwd, pwdlen, &len );
1208
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209 if( ret == 0 )
1210 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001211 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001212 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214 pem.buf, pem.buflen ) ) != 0 )
1215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 }
1218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220 return( ret );
1221 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1223 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1224 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1225 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1226 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001231 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001232 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001233 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1234 else
1235 ret = mbedtls_pem_read_buffer( &pem,
1236 "-----BEGIN EC PRIVATE KEY-----",
1237 "-----END EC PRIVATE KEY-----",
1238 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239 if( ret == 0 )
1240 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001241 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242
Hanno Beckerfab35692017-08-25 13:38:26 +01001243 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001245 pem.buf, pem.buflen,
1246 f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249 }
1250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252 return( ret );
1253 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1255 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1256 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1257 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1258 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001262 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001263 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001264 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1265 else
1266 ret = mbedtls_pem_read_buffer( &pem,
1267 "-----BEGIN PRIVATE KEY-----",
1268 "-----END PRIVATE KEY-----",
1269 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270 if( ret == 0 )
1271 {
1272 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001273 pem.buf, pem.buflen, f_rng, p_rng ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 }
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279 return( ret );
1280 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282 return( ret );
1283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
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 ENCRYPTED PRIVATE KEY-----",
1291 "-----END ENCRYPTED PRIVATE KEY-----",
1292 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 if( ret == 0 )
1294 {
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001295 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, pem.buf, pem.buflen,
1296 pwd, pwdlen, 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 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307#else
1308 ((void) pwd);
1309 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311
1312 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001313 * At this point we only know it's not a PEM formatted key. Could be any
1314 * of the known DER encoded private key formats
1315 *
1316 * We try the different DER format parsers to see if one passes without
1317 * error
1318 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Gilles Peskine0ca21952021-12-10 17:36:37 +01001320 if( pwdlen != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001322 unsigned char *key_copy;
1323
1324 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1325 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1326
1327 memcpy( key_copy, key, keylen );
1328
1329 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001330 pwd, pwdlen, f_rng, p_rng );
Hanno Beckerfab35692017-08-25 13:38:26 +01001331
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001332 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001333 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334 }
1335
Hanno Beckerfab35692017-08-25 13:38:26 +01001336 if( ret == 0 )
1337 return( 0 );
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001340 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343 {
1344 return( ret );
1345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347
Kenneth Soerensene28d49b2019-01-03 12:39:29 +01001348 ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen, f_rng, p_rng );
1349 if( ret == 0 )
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001350 {
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 return( 0 );
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001352 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001355 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358
Hanno Becker9be19262017-09-08 12:39:44 +01001359 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001360 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1361 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362 {
1363 return( 0 );
1364 }
1365
Hanno Becker780f0a42018-10-10 11:23:33 +01001366 mbedtls_pk_free( pk );
1367 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001371 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001372 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1373 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +02001374 key, keylen, f_rng, p_rng ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 {
1376 return( 0 );
1377 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001378 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380
Hanno Becker780f0a42018-10-10 11:23:33 +01001381 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1382 * it is ok to leave the PK context initialized but not
1383 * freed: It is the caller's responsibility to call pk_init()
1384 * before calling this function, and to call pk_free()
1385 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1386 * isn't, this leads to mbedtls_pk_free() being called
1387 * twice, once here and once by the caller, but this is
1388 * also ok and in line with the mbedtls_pk_free() calls
1389 * on failed PEM parsing attempts. */
1390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392}
1393
1394/*
1395 * Parse a public key
1396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398 const unsigned char *key, size_t keylen )
1399{
Janos Follath24eed8d2019-11-22 13:21:35 +00001400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001402#if defined(MBEDTLS_RSA_C)
1403 const mbedtls_pk_info_t *pk_info;
1404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001408#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001410 if( keylen == 0 )
1411 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001412
1413#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001415#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001416 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001417 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001418 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1419 else
1420 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001421 "-----BEGIN RSA PUBLIC KEY-----",
1422 "-----END RSA PUBLIC KEY-----",
1423 key, NULL, 0, &len );
1424
1425 if( ret == 0 )
1426 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001427 p = pem.buf;
Andrzej Kurek92d74172022-06-28 10:29:42 -04001428 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
Paul Elliott072d2b02022-05-13 17:08:36 +01001429 {
1430 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001431 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Elliott072d2b02022-05-13 17:08:36 +01001432 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001433
Paul Elliott072d2b02022-05-13 17:08:36 +01001434 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1435 {
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001436 mbedtls_pem_free( &pem );
Ron Eldor84df1ae2017-10-16 17:11:52 +03001437 return( ret );
Leonid Rozenboim116f50c2022-04-21 13:05:10 -07001438 }
Ron Eldord0c56de2017-10-10 17:03:08 +03001439
Ron Eldor84df1ae2017-10-16 17:11:52 +03001440 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1441 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001442
Ron Eldord0c56de2017-10-10 17:03:08 +03001443 mbedtls_pem_free( &pem );
1444 return( ret );
1445 }
1446 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1447 {
1448 mbedtls_pem_free( &pem );
1449 return( ret );
1450 }
1451#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452
1453 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001454 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1456 else
1457 ret = mbedtls_pem_read_buffer( &pem,
1458 "-----BEGIN PUBLIC KEY-----",
1459 "-----END PUBLIC KEY-----",
1460 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461
1462 if( ret == 0 )
1463 {
1464 /*
1465 * Was PEM encoded
1466 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001467 p = pem.buf;
1468
1469 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1470 mbedtls_pem_free( &pem );
1471 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 return( ret );
1477 }
Ron Eldor5472d432017-10-17 09:49:00 +03001478 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001480
1481#if defined(MBEDTLS_RSA_C)
1482 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1483 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1484
1485 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1486 return( ret );
1487
Ron Eldor9566ff72018-02-07 18:59:41 +02001488 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001489 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001490 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001491 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001492 return( ret );
1493 }
1494 mbedtls_pk_free( ctx );
Chris Jones9f7a6932021-04-14 12:12:09 +01001495 if( ret != ( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_PK_INVALID_PUBKEY,
1496 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001497 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001498 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001499 }
1500#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501 p = (unsigned char *) key;
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505 return( ret );
1506}
1507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_PK_PARSE_C */