blob: fce5ad70aea317b81798840a5518cc1a38db52b4 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker1a7550a2013-09-15 13:01:22 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk.h"
31#include "mbedtls/asn1.h"
32#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +030055#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker496b83f2019-08-20 13:33:49 +010056#include "tinycrypt/ecc.h"
Jarno Lamsa42b83db2019-04-16 16:48:22 +030057#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020061#else
62#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020063#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020065#endif
66
Gilles Peskinee97dc602018-12-19 00:51:38 +010067/* Parameter validation macros based on platform_util.h */
68#define PK_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
70#define PK_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
72
Gilles Peskine832f3492017-11-30 11:42:12 +010073#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020074/*
75 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020076 *
77 * The file is expected to contain either PEM or DER encoded data.
78 * A terminating null byte is always appended. It is included in the announced
79 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020082{
83 FILE *f;
84 long size;
85
Gilles Peskinee97dc602018-12-19 00:51:38 +010086 PK_VALIDATE_RET( path != NULL );
87 PK_VALIDATE_RET( buf != NULL );
88 PK_VALIDATE_RET( n != NULL );
89
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020092
93 fseek( f, 0, SEEK_END );
94 if( ( size = ftell( f ) ) == -1 )
95 {
96 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 }
99 fseek( f, 0, SEEK_SET );
100
101 *n = (size_t) size;
102
103 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200104 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 {
106 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200107 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108 }
109
110 if( fread( *buf, 1, *n, f ) != *n )
111 {
112 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100113
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500114 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118 }
119
120 fclose( f );
121
122 (*buf)[*n] = '\0';
123
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200124 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
125 ++*n;
126
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127 return( 0 );
128}
129
130/*
131 * Load and parse a private key
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134 const char *path, const char *pwd )
135{
136 int ret;
137 size_t n;
138 unsigned char *buf;
139
Gilles Peskinee97dc602018-12-19 00:51:38 +0100140 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100141 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144 return( ret );
145
146 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150 (const unsigned char *) pwd, strlen( pwd ) );
151
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500152 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154
155 return( ret );
156}
157
158/*
159 * Load and parse a public key
160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162{
163 int ret;
164 size_t n;
165 unsigned char *buf;
166
Gilles Peskinee97dc602018-12-19 00:51:38 +0100167 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100168 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171 return( ret );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500175 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177
178 return( ret );
179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181
Jarno Lamsab1760922019-04-18 15:58:34 +0300182#if defined(MBEDTLS_USE_TINYCRYPT)
183static int pk_use_ecparams( const mbedtls_asn1_buf *params )
184{
185 uint32_t grp_id;
186
187 if( params->tag == MBEDTLS_ASN1_OID )
188 {
189 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
190 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
191 }
192 else
193 {
194 // Only P-256 is supported
195 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
196 }
197
198 return( 0 );
199}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100200#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +0300201
202#if defined(MBEDTLS_ECP_C) || \
203 defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200205 *
206 * ECParameters ::= CHOICE {
207 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100208 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210 * }
211 */
212static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214{
215 int ret;
216
Sanne Woudab2b29d52017-08-21 15:58:12 +0100217 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100218 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
219 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100220
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100221 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200222 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( params->tag != MBEDTLS_ASN1_OID
224#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
225 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100226#endif
227 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
230 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100231 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200237
238 params->p = *p;
239 *p += params->len;
240
241 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
243 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200244
245 return( 0 );
246}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100247#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200248
Jarno Lamsab1760922019-04-18 15:58:34 +0300249#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200251/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100252 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
253 * WARNING: the resulting group should only be used with
254 * pk_group_id_from_specified(), since its base point may not be set correctly
255 * if it was encoded compressed.
256 *
257 * SpecifiedECDomain ::= SEQUENCE {
258 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
259 * fieldID FieldID {{FieldTypes}},
260 * curve Curve,
261 * base ECPoint,
262 * order INTEGER,
263 * cofactor INTEGER OPTIONAL,
264 * hash HashAlgorithm OPTIONAL,
265 * ...
266 * }
267 *
268 * We only support prime-field as field type, and ignore hash and cofactor.
269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100271{
272 int ret;
273 unsigned char *p = params->p;
274 const unsigned char * const end = params->p + params->len;
275 const unsigned char *end_field, *end_curve;
276 size_t len;
277 int ver;
278
279 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
281 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100282
283 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
286 /*
287 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
288 * fieldType FIELD-ID.&id({IOSet}),
289 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
290 * }
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
293 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100294 return( ret );
295
296 end_field = p + len;
297
298 /*
299 * FIELD-ID ::= TYPE-IDENTIFIER
300 * FieldTypes FIELD-ID ::= {
301 * { Prime-p IDENTIFIED BY prime-field } |
302 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
303 * }
304 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100307 return( ret );
308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
310 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100313 }
314
315 p += len;
316
317 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
319 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100320
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200321 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322
323 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
325 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100326
327 /*
328 * Curve ::= SEQUENCE {
329 * a FieldElement,
330 * b FieldElement,
331 * seed BIT STRING OPTIONAL
332 * -- Shall be present if used in SpecifiedECDomain
333 * -- with version equal to ecdpVer2 or ecdpVer3
334 * }
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
337 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100338 return( ret );
339
340 end_curve = p + len;
341
342 /*
343 * FieldElement ::= OCTET STRING
344 * containing an integer in the case of a prime field
345 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
347 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100350 }
351
352 p += len;
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
355 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100358 }
359
360 p += len;
361
362 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100364 p += len;
365
366 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
368 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100369
370 /*
371 * ECPoint ::= OCTET STRING
372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
374 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100377 ( const unsigned char *) p, len ) ) != 0 )
378 {
379 /*
380 * If we can't read the point because it's compressed, cheat by
381 * reading only the X coordinate and the parity bit of Y.
382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100384 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 len != mbedtls_mpi_size( &grp->P ) + 1 ||
386 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
387 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
388 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100391 }
392 }
393
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394 p += len;
395
396 /*
397 * order INTEGER
398 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
400 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100401
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200402 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403
404 /*
405 * Allow optional elements by purposefully not enforcing p == end here.
406 */
407
408 return( 0 );
409}
410
411/*
412 * Find the group id associated with an (almost filled) group as generated by
413 * pk_group_from_specified(), or return an error if unknown.
414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415static 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 +0100416{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100417 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_ecp_group ref;
419 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424 {
425 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200427 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100428
429 /* Compare to the group we were given, starting with easy tests */
430 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
432 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
433 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
434 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
435 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
436 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 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 +0100439 {
440 break;
441 }
442
443 }
444
445cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100447
448 *grp_id = *id;
449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
451 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452
453 return( ret );
454}
455
456/*
457 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
460 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100461{
462 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
467 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
468 goto cleanup;
469
470 ret = pk_group_id_from_group( &grp, grp_id );
471
472cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100474
475 return( ret );
476}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100478
479/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200480 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100481 *
482 * ECParameters ::= CHOICE {
483 * namedCurve OBJECT IDENTIFIER
484 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
485 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488{
489 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
495 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100496 }
497 else
498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100500 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
501 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100502#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100504#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100505 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506
507 /*
508 * grp may already be initilialized; if so, make sure IDs match
509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
511 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200512
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200513 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514 return( ret );
515
516 return( 0 );
517}
518
519/*
520 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100521 *
522 * The caller is responsible for clearing the structure upon failure if
523 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525 */
526static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528{
529 int ret;
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100532 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200535 }
536
537 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200539 */
540 *p = (unsigned char *) end;
541
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100542 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200543}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200545
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300546#if defined(MBEDTLS_USE_TINYCRYPT)
547/*
548 * Import a point from unsigned binary data (SEC1 2.3.4)
549 */
Hanno Becker7e38c372019-08-20 17:01:50 +0100550static int uecc_public_key_read_binary( mbedtls_uecc_keypair *uecc_keypair,
551 const unsigned char *buf, size_t ilen )
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300552{
Hanno Becker68d54782019-08-20 13:19:09 +0100553 if( ilen != 2 * NUM_ECC_BYTES + 1 )
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300554 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
555
Hanno Beckerad353f22019-08-20 13:04:30 +0100556 /* We are not handling the point at infinity. */
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300557
558 if( buf[0] != 0x04 )
559 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
560
Hanno Becker7e38c372019-08-20 17:01:50 +0100561 memcpy( uecc_keypair->public_key, buf + 1, 2 * NUM_ECC_BYTES );
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300562
563 return( 0 );
564}
565
566static int pk_get_ueccpubkey( unsigned char **p,
567 const unsigned char *end,
568 uint8_t *pk_context)
569{
Hanno Becker7e38c372019-08-20 17:01:50 +0100570 mbedtls_uecc_keypair *uecc_keypair = (mbedtls_uecc_keypair *) pk_context;
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300571 int ret;
572
Hanno Becker7e38c372019-08-20 17:01:50 +0100573 ret = uecc_public_key_read_binary( uecc_keypair,
574 (const unsigned char *) *p, end - *p );
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300575
576 /*
577 * We know uecc_public_key_read_binary consumed all bytes or failed
578 */
579 *p = (unsigned char *) end;
580
581 return( ret );
582}
583#endif /* MBEDTLS_USE_TINYCRYPT */
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200586/*
587 * RSAPublicKey ::= SEQUENCE {
588 * modulus INTEGER, -- n
589 * publicExponent INTEGER -- e
590 * }
591 */
592static int pk_get_rsapubkey( unsigned char **p,
593 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200595{
596 int ret;
597 size_t len;
598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
600 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
601 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200602
603 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
605 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200606
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100607 /* Import N */
608 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200610
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100611 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
612 NULL, 0, NULL, 0 ) ) != 0 )
613 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
614
615 *p += len;
616
617 /* Import E */
618 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
619 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
620
621 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
622 NULL, 0, *p, len ) ) != 0 )
623 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
624
625 *p += len;
626
Hanno Becker895c5ab2018-01-05 08:08:09 +0000627 if( mbedtls_rsa_complete( rsa ) != 0 ||
628 mbedtls_rsa_check_pubkey( rsa ) != 0 )
629 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100630 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000631 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100632
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
635 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200636
Paul Bakker1a7550a2013-09-15 13:01:22 +0200637 return( 0 );
638}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640
641/* Get a PK algorithm identifier
642 *
643 * AlgorithmIdentifier ::= SEQUENCE {
644 * algorithm OBJECT IDENTIFIER,
645 * parameters ANY DEFINED BY algorithm OPTIONAL }
646 */
647static int pk_get_pk_alg( unsigned char **p,
648 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650{
651 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
657 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
660 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661
662 /*
663 * No parameters with RSA (only for EC)
664 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 if( *pk_alg == MBEDTLS_PK_RSA &&
666 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200667 params->len != 0 ) )
668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200670 }
671
672 return( 0 );
673}
674
675/*
676 * SubjectPublicKeyInfo ::= SEQUENCE {
677 * algorithm AlgorithmIdentifier,
678 * subjectPublicKey BIT STRING }
679 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
681 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682{
683 int ret;
684 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_asn1_buf alg_params;
686 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
687 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688
Gilles Peskinee97dc602018-12-19 00:51:38 +0100689 PK_VALIDATE_RET( p != NULL );
690 PK_VALIDATE_RET( *p != NULL );
691 PK_VALIDATE_RET( end != NULL );
692 PK_VALIDATE_RET( pk != NULL );
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
695 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200698 }
699
700 end = *p + len;
701
702 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
703 return( ret );
704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
706 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200707
708 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
710 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
713 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200715 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716 return( ret );
717
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300718#if defined(MBEDTLS_USE_TINYCRYPT)
719 if( pk_alg == MBEDTLS_PK_ECDSA )
720 {
721 ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx );
722 }
723#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724#if defined(MBEDTLS_RSA_C)
725 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200726 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200728 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200729#endif /* MBEDTLS_RSA_C */
730#if defined(MBEDTLS_ECP_C)
731 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200734 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200736 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200737#endif /* MBEDTLS_ECP_C */
738 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739
740 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
742 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743
744 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200746
747 return( ret );
748}
749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200751/*
752 * Parse a PKCS#1 encoded private RSA key
753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755 const unsigned char *key,
756 size_t keylen )
757{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100758 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200759 size_t len;
760 unsigned char *p, *end;
761
Hanno Beckerefa14e82017-10-11 19:45:19 +0100762 mbedtls_mpi T;
763 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100764
Paul Bakker1a7550a2013-09-15 13:01:22 +0200765 p = (unsigned char *) key;
766 end = p + keylen;
767
768 /*
769 * This function parses the RSAPrivateKey (PKCS#1)
770 *
771 * RSAPrivateKey ::= SEQUENCE {
772 * version Version,
773 * modulus INTEGER, -- n
774 * publicExponent INTEGER, -- e
775 * privateExponent INTEGER, -- d
776 * prime1 INTEGER, -- p
777 * prime2 INTEGER, -- q
778 * exponent1 INTEGER, -- d mod (p-1)
779 * exponent2 INTEGER, -- d mod (q-1)
780 * coefficient INTEGER, -- (inverse of q) mod p
781 * otherPrimeInfos OtherPrimeInfos OPTIONAL
782 * }
783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
785 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200788 }
789
790 end = p + len;
791
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100792 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795 }
796
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100797 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200798 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800 }
801
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802 /* Import N */
803 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
804 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
805 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
806 NULL, 0, NULL, 0 ) ) != 0 )
807 goto cleanup;
808 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200809
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100810 /* Import E */
811 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
812 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
813 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
814 NULL, 0, p, len ) ) != 0 )
815 goto cleanup;
816 p += len;
817
818 /* Import D */
819 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
820 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
821 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
822 p, len, NULL, 0 ) ) != 0 )
823 goto cleanup;
824 p += len;
825
826 /* Import P */
827 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
828 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
829 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
830 NULL, 0, NULL, 0 ) ) != 0 )
831 goto cleanup;
832 p += len;
833
834 /* Import Q */
835 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
836 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
837 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
838 NULL, 0, NULL, 0 ) ) != 0 )
839 goto cleanup;
840 p += len;
841
842 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100843 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100844 goto cleanup;
845
846 /* Check optional parameters */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100847 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
848 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
849 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100850 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851
852 if( p != end )
853 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100854 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
855 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856 }
857
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100858cleanup:
859
Hanno Beckerefa14e82017-10-11 19:45:19 +0100860 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100861
862 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100864 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100865 if( ( ret & 0xff80 ) == 0 )
866 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
867 else
868 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
869
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871 }
872
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100873 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876
Jarno Lamsab1760922019-04-18 15:58:34 +0300877#if defined(MBEDTLS_USE_TINYCRYPT)
878static int pk_parse_key_sec1_der( mbedtls_uecc_keypair *keypair,
879 const unsigned char *key,
880 size_t keylen)
881{
882 int ret;
883 int version, pubkey_done;
884 size_t len;
885 mbedtls_asn1_buf params;
886 unsigned char *p = (unsigned char *) key;
887 unsigned char *end = p + keylen;
888 unsigned char *end2;
889
890 /*
891 * RFC 5915, or SEC1 Appendix C.4
892 *
893 * ECPrivateKey ::= SEQUENCE {
894 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
895 * privateKey OCTET STRING,
896 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
897 * publicKey [1] BIT STRING OPTIONAL
898 * }
899 */
900 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
901 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
902 {
903 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
904 }
905
906 end = p + len;
907
908 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
909 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
910
911 if( version != 1 )
912 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
913
914 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
915 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
916
Hanno Beckerad353f22019-08-20 13:04:30 +0100917 memcpy( keypair->private_key, p, len );
Jarno Lamsab1760922019-04-18 15:58:34 +0300918
919 p += len;
920
921 pubkey_done = 0;
922 if( p != end )
923 {
924 /*
925 * Is 'parameters' present?
926 */
927 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
928 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
929 {
930 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
931 ( ret = pk_use_ecparams( &params ) ) != 0 )
932 {
933 return( ret );
934 }
935 }
936 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
937 {
938 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
939 }
940 }
941
942 if( p != end )
943 {
944 /*
945 * Is 'publickey' present? If not, or if we can't read it (eg because it
946 * is compressed), create it from the private key.
947 */
948 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
949 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
950 {
951 end2 = p + len;
952
953 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
954 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
955
956 if( p + len != end2 )
957 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
958 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
959
Hanno Becker7e38c372019-08-20 17:01:50 +0100960 if( ( ret = uecc_public_key_read_binary( keypair,
Jarno Lamsab1760922019-04-18 15:58:34 +0300961 (const unsigned char *) p, end2 - p ) ) == 0 )
Hanno Beckerad353f22019-08-20 13:04:30 +0100962 {
Jarno Lamsab1760922019-04-18 15:58:34 +0300963 pubkey_done = 1;
Hanno Beckerad353f22019-08-20 13:04:30 +0100964 }
Jarno Lamsab1760922019-04-18 15:58:34 +0300965 else
966 {
967 /*
968 * The only acceptable failure mode of pk_get_ecpubkey() above
969 * is if the point format is not recognized.
970 */
971 if( ret != MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE )
972 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
973 }
974 }
975 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
976 {
977 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
978 }
979 }
980
Hanno Beckerd45f3832019-08-20 14:21:40 +0100981 if( !pubkey_done )
982 {
983 ret = uECC_compute_public_key( keypair->private_key,
984 keypair->public_key,
985 uECC_secp256r1() );
986 if( ret == 0 )
987 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
988 }
Jarno Lamsab1760922019-04-18 15:58:34 +0300989
990 return( 0 );
991}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100992#else /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +0300993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995/*
996 * Parse a SEC1 encoded private EC key
997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999 const unsigned char *key,
1000 size_t keylen )
1001{
1002 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001003 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001004 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006 unsigned char *p = (unsigned char *) key;
1007 unsigned char *end = p + keylen;
1008 unsigned char *end2;
1009
1010 /*
1011 * RFC 5915, or SEC1 Appendix C.4
1012 *
1013 * ECPrivateKey ::= SEQUENCE {
1014 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1015 * privateKey OCTET STRING,
1016 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1017 * publicKey [1] BIT STRING OPTIONAL
1018 * }
1019 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1021 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 }
1025
1026 end = p + len;
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1029 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030
1031 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1035 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_ecp_keypair_free( eck );
1040 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041 }
1042
1043 p += len;
1044
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001045 pubkey_done = 0;
1046 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001048 /*
1049 * Is 'parameters' present?
1050 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001051 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1052 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001053 {
1054 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
1055 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
1056 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001057 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001058 return( ret );
1059 }
1060 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001061 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001065 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001066 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001067
Jethro Beekmand2df9362018-02-16 13:11:04 -08001068 if( p != end )
1069 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001070 /*
1071 * Is 'publickey' present? If not, or if we can't read it (eg because it
1072 * is compressed), create it from the private key.
1073 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001074 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1075 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001076 {
1077 end2 = p + len;
1078
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001079 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1080 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001081
1082 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001083 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1084 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001085
1086 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
1087 pubkey_done = 1;
1088 else
1089 {
1090 /*
1091 * The only acceptable failure mode of pk_get_ecpubkey() above
1092 * is if the point format is not recognized.
1093 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001094 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1095 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001096 }
1097 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001098 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001099 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001100 mbedtls_ecp_keypair_free( eck );
1101 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001102 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001103 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001104
1105 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001107 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 mbedtls_ecp_keypair_free( eck );
1110 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001111 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 return( ret );
1117 }
1118
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001119 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121#endif /* MBEDTLS_ECP_C */
Jarno Lamsab1760922019-04-18 15:58:34 +03001122#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123
1124/*
1125 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001126 *
1127 * Notes:
1128 *
1129 * - This function does not own the key buffer. It is the
1130 * responsibility of the caller to take care of zeroizing
1131 * and freeing it after use.
1132 *
1133 * - The function is responsible for freeing the provided
1134 * PK context on failure.
1135 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136 */
1137static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139 const unsigned char* key,
1140 size_t keylen )
1141{
1142 int ret, version;
1143 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145 unsigned char *p = (unsigned char *) key;
1146 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1148 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149
1150 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001151 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152 *
1153 * PrivateKeyInfo ::= SEQUENCE {
1154 * version Version,
1155 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1156 * privateKey PrivateKey,
1157 * attributes [0] IMPLICIT Attributes OPTIONAL }
1158 *
1159 * Version ::= INTEGER
1160 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1161 * PrivateKey ::= OCTET STRING
1162 *
1163 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1164 */
1165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1167 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 }
1171
1172 end = p + len;
1173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1175 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
1177 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179
1180 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1184 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
1186 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1188 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1191 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001193 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194 return( ret );
1195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196#if defined(MBEDTLS_RSA_C)
1197 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202 return( ret );
1203 }
1204 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205#endif /* MBEDTLS_RSA_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001206#if defined(MBEDTLS_USE_TINYCRYPT)
1207 if( pk_alg == MBEDTLS_PK_ECDSA)
1208 {
1209 if( ( ret = pk_use_ecparams( &params ) ) != 0 ||
1210 ( ret = pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk ), p, len ) ) != 0)
1211 {
1212 return( ret );
1213 }
1214 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001215#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216#if defined(MBEDTLS_ECP_C)
1217 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1220 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223 return( ret );
1224 }
1225 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001227#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001230 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231}
1232
1233/*
1234 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001235 *
1236 * To save space, the decryption happens in-place on the given key buffer.
1237 * Also, while this function may modify the keybuffer, it doesn't own it,
1238 * and instead it is the responsibility of the caller to zeroize and properly
1239 * free it after use.
1240 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001245 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001246 const unsigned char *pwd, size_t pwdlen )
1247{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001248 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001250 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1253#if defined(MBEDTLS_PKCS12_C)
1254 mbedtls_cipher_type_t cipher_alg;
1255 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256#endif
1257
Hanno Becker2aa80a72017-09-07 15:28:45 +01001258 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259 end = p + keylen;
1260
1261 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263
1264 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001265 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266 *
1267 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1268 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1269 * encryptedData EncryptedData
1270 * }
1271 *
1272 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1273 *
1274 * EncryptedData ::= OCTET STRING
1275 *
1276 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001277 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1280 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 }
1284
1285 end = p + len;
1286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1288 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1291 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292
Hanno Beckerfab35692017-08-25 13:38:26 +01001293 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294
1295 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001296 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#if defined(MBEDTLS_PKCS12_C)
1299 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 cipher_alg, md_alg,
1303 pwd, pwdlen, p, len, buf ) ) != 0 )
1304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1306 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307
1308 return( ret );
1309 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001310
1311 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1316 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 pwd, pwdlen,
1318 p, len, buf ) ) != 0 )
1319 {
1320 return( ret );
1321 }
1322
1323 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1327 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001328
1329 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 }
1331 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_PKCS12_C */
1333#if defined(MBEDTLS_PKCS5_C)
1334 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337 p, len, buf ) ) != 0 )
1338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1340 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
1342 return( ret );
1343 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001344
1345 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346 }
1347 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001349 {
1350 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001351 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001353 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001355
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1357}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359
1360/*
1361 * Parse a private key
1362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364 const unsigned char *key, size_t keylen,
1365 const unsigned char *pwd, size_t pwdlen )
1366{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001367#if defined(MBEDTLS_PKCS12_C) || \
1368 defined(MBEDTLS_PKCS5_C) || \
1369 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001376#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377
Gilles Peskinee97dc602018-12-19 00:51:38 +01001378 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001379 if( keylen == 0 )
1380 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1381 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001382
1383#if defined(MBEDTLS_PEM_PARSE_C)
1384 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001387 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001388 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001389 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1390 else
1391 ret = mbedtls_pem_read_buffer( &pem,
1392 "-----BEGIN RSA PRIVATE KEY-----",
1393 "-----END RSA PRIVATE KEY-----",
1394 key, pwd, pwdlen, &len );
1395
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396 if( ret == 0 )
1397 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001398 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001399 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 pem.buf, pem.buflen ) ) != 0 )
1402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404 }
1405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001406 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 return( ret );
1408 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1410 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1411 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1412 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1413 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001418 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001419 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001420 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1421 else
1422 ret = mbedtls_pem_read_buffer( &pem,
1423 "-----BEGIN EC PRIVATE KEY-----",
1424 "-----END EC PRIVATE KEY-----",
1425 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426 if( ret == 0 )
1427 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001428 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429
Hanno Beckerfab35692017-08-25 13:38:26 +01001430 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 pem.buf, pem.buflen ) ) != 0 )
1433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 }
1436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438 return( ret );
1439 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1441 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1442 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1443 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1444 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001445 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001448 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001449 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1451 else
1452 ret = mbedtls_pem_read_buffer( &pem,
1453 "-----BEGIN PRIVATE KEY-----",
1454 "-----END PRIVATE KEY-----",
1455 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456 if( ret == 0 )
1457 {
1458 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1459 pem.buf, pem.buflen ) ) != 0 )
1460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001461 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462 }
1463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001464 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001465 return( ret );
1466 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001468 return( ret );
1469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001471 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001472 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001473 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1474 else
1475 ret = mbedtls_pem_read_buffer( &pem,
1476 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1477 "-----END ENCRYPTED PRIVATE KEY-----",
1478 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479 if( ret == 0 )
1480 {
1481 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1482 pem.buf, pem.buflen,
1483 pwd, pwdlen ) ) != 0 )
1484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486 }
1487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001489 return( ret );
1490 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494#else
1495 ((void) pwd);
1496 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001498
1499 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001500 * At this point we only know it's not a PEM formatted key. Could be any
1501 * of the known DER encoded private key formats
1502 *
1503 * We try the different DER format parsers to see if one passes without
1504 * error
1505 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001508 unsigned char *key_copy;
1509
1510 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1511 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1512
1513 memcpy( key_copy, key, keylen );
1514
1515 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1516 pwd, pwdlen );
1517
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001518 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001519 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520 }
1521
Hanno Beckerfab35692017-08-25 13:38:26 +01001522 if( ret == 0 )
1523 return( 0 );
1524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001526 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529 {
1530 return( ret );
1531 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001534 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535 return( 0 );
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001538 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001540#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001541
Hanno Becker9be19262017-09-08 12:39:44 +01001542 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001543 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1544 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001545 {
1546 return( 0 );
1547 }
1548
Hanno Becker780f0a42018-10-10 11:23:33 +01001549 mbedtls_pk_free( pk );
1550 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001552
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001553#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +01001554 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001555 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1556 pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk),
1557 key, keylen) == 0)
1558 {
1559 return( 0 );
1560 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001561#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001563 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001564 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1565 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1566 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001567 {
1568 return( 0 );
1569 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001570 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001572#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001573
Hanno Becker780f0a42018-10-10 11:23:33 +01001574 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1575 * it is ok to leave the PK context initialized but not
1576 * freed: It is the caller's responsibility to call pk_init()
1577 * before calling this function, and to call pk_free()
1578 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1579 * isn't, this leads to mbedtls_pk_free() being called
1580 * twice, once here and once by the caller, but this is
1581 * also ok and in line with the mbedtls_pk_free() calls
1582 * on failed PEM parsing attempts. */
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001585}
1586
1587/*
1588 * Parse a public key
1589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001591 const unsigned char *key, size_t keylen )
1592{
1593 int ret;
1594 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001595#if defined(MBEDTLS_RSA_C)
1596 const mbedtls_pk_info_t *pk_info;
1597#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001598#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001599 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001601#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602
Gilles Peskinee97dc602018-12-19 00:51:38 +01001603 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001604 if( keylen == 0 )
1605 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001606 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1607
1608#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001610#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001611 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001612 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001613 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1614 else
1615 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001616 "-----BEGIN RSA PUBLIC KEY-----",
1617 "-----END RSA PUBLIC KEY-----",
1618 key, NULL, 0, &len );
1619
1620 if( ret == 0 )
1621 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001622 p = pem.buf;
1623 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1624 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001625
Ron Eldor84df1ae2017-10-16 17:11:52 +03001626 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1627 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001628
Ron Eldor84df1ae2017-10-16 17:11:52 +03001629 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1630 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001631
Ron Eldord0c56de2017-10-10 17:03:08 +03001632 mbedtls_pem_free( &pem );
1633 return( ret );
1634 }
1635 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1636 {
1637 mbedtls_pem_free( &pem );
1638 return( ret );
1639 }
1640#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001641
1642 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001643 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001644 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1645 else
1646 ret = mbedtls_pem_read_buffer( &pem,
1647 "-----BEGIN PUBLIC KEY-----",
1648 "-----END PUBLIC KEY-----",
1649 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001650
1651 if( ret == 0 )
1652 {
1653 /*
1654 * Was PEM encoded
1655 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001656 p = pem.buf;
1657
1658 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1659 mbedtls_pem_free( &pem );
1660 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001663 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001665 return( ret );
1666 }
Ron Eldor5472d432017-10-17 09:49:00 +03001667 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001669
1670#if defined(MBEDTLS_RSA_C)
1671 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1672 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1673
1674 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1675 return( ret );
1676
Ron Eldor9566ff72018-02-07 18:59:41 +02001677 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001678 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001679 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001680 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001681 return( ret );
1682 }
1683 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001684 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001685 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001686 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001687 }
1688#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689 p = (unsigned char *) key;
1690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001692
Paul Bakker1a7550a2013-09-15 13:01:22 +02001693 return( ret );
1694}
1695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_PK_PARSE_C */