blob: dbef18a3ee128fb5a673ce8c64b6899bd072fc14 [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
981 //TODO: Do we need to support derived public keys with uecc?
982
983 return( 0 );
984}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100985#else /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +0300986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988/*
989 * Parse a SEC1 encoded private EC key
990 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 const unsigned char *key,
993 size_t keylen )
994{
995 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100996 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999 unsigned char *p = (unsigned char *) key;
1000 unsigned char *end = p + keylen;
1001 unsigned char *end2;
1002
1003 /*
1004 * RFC 5915, or SEC1 Appendix C.4
1005 *
1006 * ECPrivateKey ::= SEQUENCE {
1007 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1008 * privateKey OCTET STRING,
1009 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1010 * publicKey [1] BIT STRING OPTIONAL
1011 * }
1012 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1014 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 }
1018
1019 end = p + len;
1020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001021 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1022 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023
1024 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1028 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_ecp_keypair_free( eck );
1033 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034 }
1035
1036 p += len;
1037
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001038 pubkey_done = 0;
1039 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001041 /*
1042 * Is 'parameters' present?
1043 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001044 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1045 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001046 {
1047 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
1048 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
1049 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001050 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001051 return( ret );
1052 }
1053 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001054 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001058 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001059 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001060
Jethro Beekmand2df9362018-02-16 13:11:04 -08001061 if( p != end )
1062 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001063 /*
1064 * Is 'publickey' present? If not, or if we can't read it (eg because it
1065 * is compressed), create it from the private key.
1066 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001067 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1068 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001069 {
1070 end2 = p + len;
1071
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001072 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1073 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001074
1075 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001076 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1077 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001078
1079 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
1080 pubkey_done = 1;
1081 else
1082 {
1083 /*
1084 * The only acceptable failure mode of pk_get_ecpubkey() above
1085 * is if the point format is not recognized.
1086 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001087 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1088 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001089 }
1090 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001091 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001092 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001093 mbedtls_ecp_keypair_free( eck );
1094 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001095 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001097
1098 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001100 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 mbedtls_ecp_keypair_free( eck );
1103 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001104 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109 return( ret );
1110 }
1111
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001112 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114#endif /* MBEDTLS_ECP_C */
Jarno Lamsab1760922019-04-18 15:58:34 +03001115#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116
1117/*
1118 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001119 *
1120 * Notes:
1121 *
1122 * - This function does not own the key buffer. It is the
1123 * responsibility of the caller to take care of zeroizing
1124 * and freeing it after use.
1125 *
1126 * - The function is responsible for freeing the provided
1127 * PK context on failure.
1128 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129 */
1130static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 const unsigned char* key,
1133 size_t keylen )
1134{
1135 int ret, version;
1136 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001137 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 unsigned char *p = (unsigned char *) key;
1139 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1141 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142
1143 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001144 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145 *
1146 * PrivateKeyInfo ::= SEQUENCE {
1147 * version Version,
1148 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1149 * privateKey PrivateKey,
1150 * attributes [0] IMPLICIT Attributes OPTIONAL }
1151 *
1152 * Version ::= INTEGER
1153 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1154 * PrivateKey ::= OCTET STRING
1155 *
1156 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1157 */
1158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1160 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 }
1164
1165 end = p + len;
1166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1168 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169
1170 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172
1173 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1177 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178
1179 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1181 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1184 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001186 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187 return( ret );
1188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189#if defined(MBEDTLS_RSA_C)
1190 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195 return( ret );
1196 }
1197 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198#endif /* MBEDTLS_RSA_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001199#if defined(MBEDTLS_USE_TINYCRYPT)
1200 if( pk_alg == MBEDTLS_PK_ECDSA)
1201 {
1202 if( ( ret = pk_use_ecparams( &params ) ) != 0 ||
1203 ( ret = pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk ), p, len ) ) != 0)
1204 {
1205 return( ret );
1206 }
1207 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001208#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#if defined(MBEDTLS_ECP_C)
1210 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1213 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216 return( ret );
1217 }
1218 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001220#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001223 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001224}
1225
1226/*
1227 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001228 *
1229 * To save space, the decryption happens in-place on the given key buffer.
1230 * Also, while this function may modify the keybuffer, it doesn't own it,
1231 * and instead it is the responsibility of the caller to zeroize and properly
1232 * free it after use.
1233 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001238 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239 const unsigned char *pwd, size_t pwdlen )
1240{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001241 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001243 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1246#if defined(MBEDTLS_PKCS12_C)
1247 mbedtls_cipher_type_t cipher_alg;
1248 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249#endif
1250
Hanno Becker2aa80a72017-09-07 15:28:45 +01001251 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252 end = p + keylen;
1253
1254 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256
1257 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001258 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259 *
1260 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1261 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1262 * encryptedData EncryptedData
1263 * }
1264 *
1265 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1266 *
1267 * EncryptedData ::= OCTET STRING
1268 *
1269 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001270 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1273 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 }
1277
1278 end = p + len;
1279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1281 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1284 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285
Hanno Beckerfab35692017-08-25 13:38:26 +01001286 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287
1288 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001289 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_PKCS12_C)
1292 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001295 cipher_alg, md_alg,
1296 pwd, pwdlen, p, len, buf ) ) != 0 )
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1299 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300
1301 return( ret );
1302 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001303
1304 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1309 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 pwd, pwdlen,
1311 p, len, buf ) ) != 0 )
1312 {
1313 return( ret );
1314 }
1315
1316 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1320 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001321
1322 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001323 }
1324 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_PKCS12_C */
1326#if defined(MBEDTLS_PKCS5_C)
1327 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 p, len, buf ) ) != 0 )
1331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1333 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334
1335 return( ret );
1336 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001337
1338 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339 }
1340 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001342 {
1343 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001344 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001346 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001348
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1350}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352
1353/*
1354 * Parse a private key
1355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357 const unsigned char *key, size_t keylen,
1358 const unsigned char *pwd, size_t pwdlen )
1359{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001360#if defined(MBEDTLS_PKCS12_C) || \
1361 defined(MBEDTLS_PKCS5_C) || \
1362 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001369#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370
Gilles Peskinee97dc602018-12-19 00:51:38 +01001371 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001372 if( keylen == 0 )
1373 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1374 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001375
1376#if defined(MBEDTLS_PEM_PARSE_C)
1377 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001380 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001381 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001382 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1383 else
1384 ret = mbedtls_pem_read_buffer( &pem,
1385 "-----BEGIN RSA PRIVATE KEY-----",
1386 "-----END RSA PRIVATE KEY-----",
1387 key, pwd, pwdlen, &len );
1388
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389 if( ret == 0 )
1390 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001391 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001392 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001393 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394 pem.buf, pem.buflen ) ) != 0 )
1395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397 }
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400 return( ret );
1401 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1403 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1404 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1405 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1406 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001411 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001412 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001413 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1414 else
1415 ret = mbedtls_pem_read_buffer( &pem,
1416 "-----BEGIN EC PRIVATE KEY-----",
1417 "-----END EC PRIVATE KEY-----",
1418 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419 if( ret == 0 )
1420 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001421 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422
Hanno Beckerfab35692017-08-25 13:38:26 +01001423 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425 pem.buf, pem.buflen ) ) != 0 )
1426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428 }
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431 return( ret );
1432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1434 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1435 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1436 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1437 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001441 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001442 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001443 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1444 else
1445 ret = mbedtls_pem_read_buffer( &pem,
1446 "-----BEGIN PRIVATE KEY-----",
1447 "-----END PRIVATE KEY-----",
1448 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 if( ret == 0 )
1450 {
1451 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1452 pem.buf, pem.buflen ) ) != 0 )
1453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455 }
1456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 return( ret );
1459 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461 return( ret );
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001464 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001465 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001466 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1467 else
1468 ret = mbedtls_pem_read_buffer( &pem,
1469 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1470 "-----END ENCRYPTED PRIVATE KEY-----",
1471 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001472 if( ret == 0 )
1473 {
1474 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1475 pem.buf, pem.buflen,
1476 pwd, pwdlen ) ) != 0 )
1477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479 }
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 return( ret );
1483 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001487#else
1488 ((void) pwd);
1489 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001490#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491
1492 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001493 * At this point we only know it's not a PEM formatted key. Could be any
1494 * of the known DER encoded private key formats
1495 *
1496 * We try the different DER format parsers to see if one passes without
1497 * error
1498 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001500 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001501 unsigned char *key_copy;
1502
1503 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1504 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1505
1506 memcpy( key_copy, key, keylen );
1507
1508 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1509 pwd, pwdlen );
1510
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001511 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001512 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001513 }
1514
Hanno Beckerfab35692017-08-25 13:38:26 +01001515 if( ret == 0 )
1516 return( 0 );
1517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001518 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001519 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001522 {
1523 return( ret );
1524 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001526
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001527 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528 return( 0 );
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001531 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534
Hanno Becker9be19262017-09-08 12:39:44 +01001535 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001536 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1537 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538 {
1539 return( 0 );
1540 }
1541
Hanno Becker780f0a42018-10-10 11:23:33 +01001542 mbedtls_pk_free( pk );
1543 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001545
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001546#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +01001547 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001548 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1549 pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk),
1550 key, keylen) == 0)
1551 {
1552 return( 0 );
1553 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001554#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001556 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001557 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1558 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1559 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560 {
1561 return( 0 );
1562 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001563 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001565#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566
Hanno Becker780f0a42018-10-10 11:23:33 +01001567 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1568 * it is ok to leave the PK context initialized but not
1569 * freed: It is the caller's responsibility to call pk_init()
1570 * before calling this function, and to call pk_free()
1571 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1572 * isn't, this leads to mbedtls_pk_free() being called
1573 * twice, once here and once by the caller, but this is
1574 * also ok and in line with the mbedtls_pk_free() calls
1575 * on failed PEM parsing attempts. */
1576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578}
1579
1580/*
1581 * Parse a public key
1582 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584 const unsigned char *key, size_t keylen )
1585{
1586 int ret;
1587 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001588#if defined(MBEDTLS_RSA_C)
1589 const mbedtls_pk_info_t *pk_info;
1590#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001592 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001594#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001595
Gilles Peskinee97dc602018-12-19 00:51:38 +01001596 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001597 if( keylen == 0 )
1598 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001599 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1600
1601#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001603#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001604 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001605 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001606 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1607 else
1608 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001609 "-----BEGIN RSA PUBLIC KEY-----",
1610 "-----END RSA PUBLIC KEY-----",
1611 key, NULL, 0, &len );
1612
1613 if( ret == 0 )
1614 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001615 p = pem.buf;
1616 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1617 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001618
Ron Eldor84df1ae2017-10-16 17:11:52 +03001619 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1620 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001621
Ron Eldor84df1ae2017-10-16 17:11:52 +03001622 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1623 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001624
Ron Eldord0c56de2017-10-10 17:03:08 +03001625 mbedtls_pem_free( &pem );
1626 return( ret );
1627 }
1628 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1629 {
1630 mbedtls_pem_free( &pem );
1631 return( ret );
1632 }
1633#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001634
1635 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001636 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001637 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1638 else
1639 ret = mbedtls_pem_read_buffer( &pem,
1640 "-----BEGIN PUBLIC KEY-----",
1641 "-----END PUBLIC KEY-----",
1642 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001643
1644 if( ret == 0 )
1645 {
1646 /*
1647 * Was PEM encoded
1648 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001649 p = pem.buf;
1650
1651 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1652 mbedtls_pem_free( &pem );
1653 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001654 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001655 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001658 return( ret );
1659 }
Ron Eldor5472d432017-10-17 09:49:00 +03001660 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001662
1663#if defined(MBEDTLS_RSA_C)
1664 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1665 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1666
1667 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1668 return( ret );
1669
Ron Eldor9566ff72018-02-07 18:59:41 +02001670 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001671 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001672 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001673 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001674 return( ret );
1675 }
1676 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001677 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001678 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001679 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001680 }
1681#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001682 p = (unsigned char *) key;
1683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685
Paul Bakker1a7550a2013-09-15 13:01:22 +02001686 return( ret );
1687}
1688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689#endif /* MBEDTLS_PK_PARSE_C */