blob: 7c8cd804b08ad13de11da06780945ddafdc8ce7a [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 */
Jarno Lamsab1760922019-04-18 15:58:34 +0300550static int uecc_public_key_read_binary( uint8_t *pt,
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300551 const unsigned char *buf, size_t ilen )
552{
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
Jarno Lamsab1760922019-04-18 15:58:34 +0300561 memcpy( pt, buf + 1, ilen - 1);
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{
570 int ret;
571
Jarno Lamsab1760922019-04-18 15:58:34 +0300572 ret = uecc_public_key_read_binary( pk_context,
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300573 (const unsigned char *) *p, end - *p );
574
575 /*
576 * We know uecc_public_key_read_binary consumed all bytes or failed
577 */
578 *p = (unsigned char *) end;
579
580 return( ret );
581}
582#endif /* MBEDTLS_USE_TINYCRYPT */
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585/*
586 * RSAPublicKey ::= SEQUENCE {
587 * modulus INTEGER, -- n
588 * publicExponent INTEGER -- e
589 * }
590 */
591static int pk_get_rsapubkey( unsigned char **p,
592 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200594{
595 int ret;
596 size_t len;
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
599 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
600 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200601
602 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
604 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200605
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100606 /* Import N */
607 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200609
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100610 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
611 NULL, 0, NULL, 0 ) ) != 0 )
612 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
613
614 *p += len;
615
616 /* Import E */
617 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
618 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
619
620 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
621 NULL, 0, *p, len ) ) != 0 )
622 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
623
624 *p += len;
625
Hanno Becker895c5ab2018-01-05 08:08:09 +0000626 if( mbedtls_rsa_complete( rsa ) != 0 ||
627 mbedtls_rsa_check_pubkey( rsa ) != 0 )
628 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100629 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000630 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100631
Paul Bakker1a7550a2013-09-15 13:01:22 +0200632 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
634 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635
Paul Bakker1a7550a2013-09-15 13:01:22 +0200636 return( 0 );
637}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639
640/* Get a PK algorithm identifier
641 *
642 * AlgorithmIdentifier ::= SEQUENCE {
643 * algorithm OBJECT IDENTIFIER,
644 * parameters ANY DEFINED BY algorithm OPTIONAL }
645 */
646static int pk_get_pk_alg( unsigned char **p,
647 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200649{
650 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
656 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
659 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660
661 /*
662 * No parameters with RSA (only for EC)
663 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( *pk_alg == MBEDTLS_PK_RSA &&
665 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666 params->len != 0 ) )
667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200669 }
670
671 return( 0 );
672}
673
674/*
675 * SubjectPublicKeyInfo ::= SEQUENCE {
676 * algorithm AlgorithmIdentifier,
677 * subjectPublicKey BIT STRING }
678 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
680 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200681{
682 int ret;
683 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 mbedtls_asn1_buf alg_params;
685 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
686 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200687
Gilles Peskinee97dc602018-12-19 00:51:38 +0100688 PK_VALIDATE_RET( p != NULL );
689 PK_VALIDATE_RET( *p != NULL );
690 PK_VALIDATE_RET( end != NULL );
691 PK_VALIDATE_RET( pk != NULL );
692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
694 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200696 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697 }
698
699 end = *p + len;
700
701 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
702 return( ret );
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
705 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706
707 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
709 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
712 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200714 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715 return( ret );
716
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300717#if defined(MBEDTLS_USE_TINYCRYPT)
718 if( pk_alg == MBEDTLS_PK_ECDSA )
719 {
720 ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx );
721 }
722#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723#if defined(MBEDTLS_RSA_C)
724 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#endif /* MBEDTLS_RSA_C */
729#if defined(MBEDTLS_ECP_C)
730 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736#endif /* MBEDTLS_ECP_C */
737 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738
739 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
741 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200742
743 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745
746 return( ret );
747}
748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750/*
751 * Parse a PKCS#1 encoded private RSA key
752 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754 const unsigned char *key,
755 size_t keylen )
756{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758 size_t len;
759 unsigned char *p, *end;
760
Hanno Beckerefa14e82017-10-11 19:45:19 +0100761 mbedtls_mpi T;
762 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764 p = (unsigned char *) key;
765 end = p + keylen;
766
767 /*
768 * This function parses the RSAPrivateKey (PKCS#1)
769 *
770 * RSAPrivateKey ::= SEQUENCE {
771 * version Version,
772 * modulus INTEGER, -- n
773 * publicExponent INTEGER, -- e
774 * privateExponent INTEGER, -- d
775 * prime1 INTEGER, -- p
776 * prime2 INTEGER, -- q
777 * exponent1 INTEGER, -- d mod (p-1)
778 * exponent2 INTEGER, -- d mod (q-1)
779 * coefficient INTEGER, -- (inverse of q) mod p
780 * otherPrimeInfos OtherPrimeInfos OPTIONAL
781 * }
782 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
784 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787 }
788
789 end = p + len;
790
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100791 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200794 }
795
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100796 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200799 }
800
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100801 /* Import N */
802 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
803 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
804 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
805 NULL, 0, NULL, 0 ) ) != 0 )
806 goto cleanup;
807 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200808
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809 /* Import E */
810 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
811 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
812 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
813 NULL, 0, p, len ) ) != 0 )
814 goto cleanup;
815 p += len;
816
817 /* Import D */
818 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
819 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
820 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
821 p, len, NULL, 0 ) ) != 0 )
822 goto cleanup;
823 p += len;
824
825 /* Import P */
826 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
827 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
828 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
829 NULL, 0, NULL, 0 ) ) != 0 )
830 goto cleanup;
831 p += len;
832
833 /* Import Q */
834 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
835 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
836 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
837 NULL, 0, NULL, 0 ) ) != 0 )
838 goto cleanup;
839 p += len;
840
841 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100842 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100843 goto cleanup;
844
845 /* Check optional parameters */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100846 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
847 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
848 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100849 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
851 if( p != end )
852 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100853 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
854 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855 }
856
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100857cleanup:
858
Hanno Beckerefa14e82017-10-11 19:45:19 +0100859 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100860
861 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100863 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100864 if( ( ret & 0xff80 ) == 0 )
865 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
866 else
867 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200869 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870 }
871
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100872 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875
Jarno Lamsab1760922019-04-18 15:58:34 +0300876#if defined(MBEDTLS_USE_TINYCRYPT)
877static int pk_parse_key_sec1_der( mbedtls_uecc_keypair *keypair,
878 const unsigned char *key,
879 size_t keylen)
880{
881 int ret;
882 int version, pubkey_done;
883 size_t len;
884 mbedtls_asn1_buf params;
885 unsigned char *p = (unsigned char *) key;
886 unsigned char *end = p + keylen;
887 unsigned char *end2;
888
889 /*
890 * RFC 5915, or SEC1 Appendix C.4
891 *
892 * ECPrivateKey ::= SEQUENCE {
893 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
894 * privateKey OCTET STRING,
895 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
896 * publicKey [1] BIT STRING OPTIONAL
897 * }
898 */
899 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
900 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
901 {
902 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
903 }
904
905 end = p + len;
906
907 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
908 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
909
910 if( version != 1 )
911 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
912
913 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
914 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
915
Hanno Beckerad353f22019-08-20 13:04:30 +0100916 memcpy( keypair->private_key, p, len );
Jarno Lamsab1760922019-04-18 15:58:34 +0300917
918 p += len;
919
920 pubkey_done = 0;
921 if( p != end )
922 {
923 /*
924 * Is 'parameters' present?
925 */
926 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
927 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
928 {
929 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
930 ( ret = pk_use_ecparams( &params ) ) != 0 )
931 {
932 return( ret );
933 }
934 }
935 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
936 {
937 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
938 }
939 }
940
941 if( p != end )
942 {
943 /*
944 * Is 'publickey' present? If not, or if we can't read it (eg because it
945 * is compressed), create it from the private key.
946 */
947 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
948 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
949 {
950 end2 = p + len;
951
952 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
953 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
954
955 if( p + len != end2 )
956 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
957 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
958
Hanno Beckerad353f22019-08-20 13:04:30 +0100959 if( ( ret = uecc_public_key_read_binary( keypair->public_key,
Jarno Lamsab1760922019-04-18 15:58:34 +0300960 (const unsigned char *) p, end2 - p ) ) == 0 )
Hanno Beckerad353f22019-08-20 13:04:30 +0100961 {
Jarno Lamsab1760922019-04-18 15:58:34 +0300962 pubkey_done = 1;
Hanno Beckerad353f22019-08-20 13:04:30 +0100963 }
Jarno Lamsab1760922019-04-18 15:58:34 +0300964 else
965 {
966 /*
967 * The only acceptable failure mode of pk_get_ecpubkey() above
968 * is if the point format is not recognized.
969 */
970 if( ret != MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE )
971 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
972 }
973 }
974 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
975 {
976 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
977 }
978 }
979
980 //TODO: Do we need to support derived public keys with uecc?
981
982 return( 0 );
983}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100984#else /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +0300985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987/*
988 * Parse a SEC1 encoded private EC key
989 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991 const unsigned char *key,
992 size_t keylen )
993{
994 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100995 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 unsigned char *p = (unsigned char *) key;
999 unsigned char *end = p + keylen;
1000 unsigned char *end2;
1001
1002 /*
1003 * RFC 5915, or SEC1 Appendix C.4
1004 *
1005 * ECPrivateKey ::= SEQUENCE {
1006 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1007 * privateKey OCTET STRING,
1008 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1009 * publicKey [1] BIT STRING OPTIONAL
1010 * }
1011 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1013 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 }
1017
1018 end = p + len;
1019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1021 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022
1023 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001026 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1027 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001029 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 mbedtls_ecp_keypair_free( eck );
1032 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033 }
1034
1035 p += len;
1036
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001037 pubkey_done = 0;
1038 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001040 /*
1041 * Is 'parameters' present?
1042 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001043 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1044 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001045 {
1046 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
1047 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
1048 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001049 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001050 return( ret );
1051 }
1052 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001053 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001057 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001058 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001059
Jethro Beekmand2df9362018-02-16 13:11:04 -08001060 if( p != end )
1061 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001062 /*
1063 * Is 'publickey' present? If not, or if we can't read it (eg because it
1064 * is compressed), create it from the private key.
1065 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001066 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1067 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001068 {
1069 end2 = p + len;
1070
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001071 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1072 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001073
1074 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001075 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1076 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001077
1078 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
1079 pubkey_done = 1;
1080 else
1081 {
1082 /*
1083 * The only acceptable failure mode of pk_get_ecpubkey() above
1084 * is if the point format is not recognized.
1085 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001086 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1087 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001088 }
1089 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001090 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001091 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001092 mbedtls_ecp_keypair_free( eck );
1093 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001094 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001096
1097 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001099 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_ecp_keypair_free( eck );
1102 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001103 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001106 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108 return( ret );
1109 }
1110
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001111 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113#endif /* MBEDTLS_ECP_C */
Jarno Lamsab1760922019-04-18 15:58:34 +03001114#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001115
1116/*
1117 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001118 *
1119 * Notes:
1120 *
1121 * - This function does not own the key buffer. It is the
1122 * responsibility of the caller to take care of zeroizing
1123 * and freeing it after use.
1124 *
1125 * - The function is responsible for freeing the provided
1126 * PK context on failure.
1127 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 */
1129static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131 const unsigned char* key,
1132 size_t keylen )
1133{
1134 int ret, version;
1135 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 unsigned char *p = (unsigned char *) key;
1138 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1140 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141
1142 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001143 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144 *
1145 * PrivateKeyInfo ::= SEQUENCE {
1146 * version Version,
1147 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1148 * privateKey PrivateKey,
1149 * attributes [0] IMPLICIT Attributes OPTIONAL }
1150 *
1151 * Version ::= INTEGER
1152 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1153 * PrivateKey ::= OCTET STRING
1154 *
1155 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1156 */
1157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1159 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 }
1163
1164 end = p + len;
1165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1167 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168
1169 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001171
1172 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1176 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177
1178 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1180 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1183 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001185 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 return( ret );
1187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_RSA_C)
1189 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194 return( ret );
1195 }
1196 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197#endif /* MBEDTLS_RSA_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001198#if defined(MBEDTLS_USE_TINYCRYPT)
1199 if( pk_alg == MBEDTLS_PK_ECDSA)
1200 {
1201 if( ( ret = pk_use_ecparams( &params ) ) != 0 ||
1202 ( ret = pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk ), p, len ) ) != 0)
1203 {
1204 return( ret );
1205 }
1206 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001207#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_ECP_C)
1209 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1212 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 return( ret );
1216 }
1217 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001219#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001222 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223}
1224
1225/*
1226 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001227 *
1228 * To save space, the decryption happens in-place on the given key buffer.
1229 * Also, while this function may modify the keybuffer, it doesn't own it,
1230 * and instead it is the responsibility of the caller to zeroize and properly
1231 * free it after use.
1232 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001233 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001237 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238 const unsigned char *pwd, size_t pwdlen )
1239{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001240 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001241 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001242 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001243 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1245#if defined(MBEDTLS_PKCS12_C)
1246 mbedtls_cipher_type_t cipher_alg;
1247 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248#endif
1249
Hanno Becker2aa80a72017-09-07 15:28:45 +01001250 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251 end = p + keylen;
1252
1253 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255
1256 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001257 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 *
1259 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1260 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1261 * encryptedData EncryptedData
1262 * }
1263 *
1264 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1265 *
1266 * EncryptedData ::= OCTET STRING
1267 *
1268 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001269 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1272 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 }
1276
1277 end = p + len;
1278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1280 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1283 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284
Hanno Beckerfab35692017-08-25 13:38:26 +01001285 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
1287 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001288 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290#if defined(MBEDTLS_PKCS12_C)
1291 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 cipher_alg, md_alg,
1295 pwd, pwdlen, p, len, buf ) ) != 0 )
1296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1298 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299
1300 return( ret );
1301 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001302
1303 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1308 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 pwd, pwdlen,
1310 p, len, buf ) ) != 0 )
1311 {
1312 return( ret );
1313 }
1314
1315 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1319 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001320
1321 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 }
1323 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324#endif /* MBEDTLS_PKCS12_C */
1325#if defined(MBEDTLS_PKCS5_C)
1326 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329 p, len, buf ) ) != 0 )
1330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1332 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333
1334 return( ret );
1335 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001336
1337 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 }
1339 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001341 {
1342 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001343 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001345 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001347
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1349}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351
1352/*
1353 * Parse a private key
1354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 const unsigned char *key, size_t keylen,
1357 const unsigned char *pwd, size_t pwdlen )
1358{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001359#if defined(MBEDTLS_PKCS12_C) || \
1360 defined(MBEDTLS_PKCS5_C) || \
1361 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001363#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001368#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369
Gilles Peskinee97dc602018-12-19 00:51:38 +01001370 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001371 if( keylen == 0 )
1372 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1373 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001374
1375#if defined(MBEDTLS_PEM_PARSE_C)
1376 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001379 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001380 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001381 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1382 else
1383 ret = mbedtls_pem_read_buffer( &pem,
1384 "-----BEGIN RSA PRIVATE KEY-----",
1385 "-----END RSA PRIVATE KEY-----",
1386 key, pwd, pwdlen, &len );
1387
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388 if( ret == 0 )
1389 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001390 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001391 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 pem.buf, pem.buflen ) ) != 0 )
1394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396 }
1397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 return( ret );
1400 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1402 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1403 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1404 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1405 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001410 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001411 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001412 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1413 else
1414 ret = mbedtls_pem_read_buffer( &pem,
1415 "-----BEGIN EC PRIVATE KEY-----",
1416 "-----END EC PRIVATE KEY-----",
1417 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 if( ret == 0 )
1419 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001420 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421
Hanno Beckerfab35692017-08-25 13:38:26 +01001422 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424 pem.buf, pem.buflen ) ) != 0 )
1425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427 }
1428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 return( ret );
1431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1433 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1434 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1435 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1436 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001440 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001441 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001442 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1443 else
1444 ret = mbedtls_pem_read_buffer( &pem,
1445 "-----BEGIN PRIVATE KEY-----",
1446 "-----END PRIVATE KEY-----",
1447 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448 if( ret == 0 )
1449 {
1450 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1451 pem.buf, pem.buflen ) ) != 0 )
1452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454 }
1455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001456 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001457 return( ret );
1458 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460 return( ret );
1461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001463 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001464 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001465 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1466 else
1467 ret = mbedtls_pem_read_buffer( &pem,
1468 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1469 "-----END ENCRYPTED PRIVATE KEY-----",
1470 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471 if( ret == 0 )
1472 {
1473 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1474 pem.buf, pem.buflen,
1475 pwd, pwdlen ) ) != 0 )
1476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478 }
1479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481 return( ret );
1482 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001484 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001486#else
1487 ((void) pwd);
1488 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490
1491 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001492 * At this point we only know it's not a PEM formatted key. Could be any
1493 * of the known DER encoded private key formats
1494 *
1495 * We try the different DER format parsers to see if one passes without
1496 * error
1497 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001500 unsigned char *key_copy;
1501
1502 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1503 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1504
1505 memcpy( key_copy, key, keylen );
1506
1507 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1508 pwd, pwdlen );
1509
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001510 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001511 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001512 }
1513
Hanno Beckerfab35692017-08-25 13:38:26 +01001514 if( ret == 0 )
1515 return( 0 );
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001518 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001521 {
1522 return( ret );
1523 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001526 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001527 return( 0 );
1528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001530 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Hanno Becker9be19262017-09-08 12:39:44 +01001534 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001535 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1536 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537 {
1538 return( 0 );
1539 }
1540
Hanno Becker780f0a42018-10-10 11:23:33 +01001541 mbedtls_pk_free( pk );
1542 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001545#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +01001546 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001547 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1548 pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk),
1549 key, keylen) == 0)
1550 {
1551 return( 0 );
1552 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001553#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001555 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001556 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1557 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1558 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001559 {
1560 return( 0 );
1561 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001562 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001564#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001565
Hanno Becker780f0a42018-10-10 11:23:33 +01001566 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1567 * it is ok to leave the PK context initialized but not
1568 * freed: It is the caller's responsibility to call pk_init()
1569 * before calling this function, and to call pk_free()
1570 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1571 * isn't, this leads to mbedtls_pk_free() being called
1572 * twice, once here and once by the caller, but this is
1573 * also ok and in line with the mbedtls_pk_free() calls
1574 * on failed PEM parsing attempts. */
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001577}
1578
1579/*
1580 * Parse a public key
1581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001583 const unsigned char *key, size_t keylen )
1584{
1585 int ret;
1586 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001587#if defined(MBEDTLS_RSA_C)
1588 const mbedtls_pk_info_t *pk_info;
1589#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001591 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001593#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001594
Gilles Peskinee97dc602018-12-19 00:51:38 +01001595 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001596 if( keylen == 0 )
1597 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001598 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1599
1600#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001602#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001603 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001604 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001605 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1606 else
1607 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001608 "-----BEGIN RSA PUBLIC KEY-----",
1609 "-----END RSA PUBLIC KEY-----",
1610 key, NULL, 0, &len );
1611
1612 if( ret == 0 )
1613 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001614 p = pem.buf;
1615 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1616 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001617
Ron Eldor84df1ae2017-10-16 17:11:52 +03001618 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1619 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001620
Ron Eldor84df1ae2017-10-16 17:11:52 +03001621 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1622 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001623
Ron Eldord0c56de2017-10-10 17:03:08 +03001624 mbedtls_pem_free( &pem );
1625 return( ret );
1626 }
1627 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1628 {
1629 mbedtls_pem_free( &pem );
1630 return( ret );
1631 }
1632#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001633
1634 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001635 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001636 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1637 else
1638 ret = mbedtls_pem_read_buffer( &pem,
1639 "-----BEGIN PUBLIC KEY-----",
1640 "-----END PUBLIC KEY-----",
1641 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001642
1643 if( ret == 0 )
1644 {
1645 /*
1646 * Was PEM encoded
1647 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001648 p = pem.buf;
1649
1650 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1651 mbedtls_pem_free( &pem );
1652 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001657 return( ret );
1658 }
Ron Eldor5472d432017-10-17 09:49:00 +03001659 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001661
1662#if defined(MBEDTLS_RSA_C)
1663 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1664 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1665
1666 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1667 return( ret );
1668
Ron Eldor9566ff72018-02-07 18:59:41 +02001669 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001670 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001671 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001672 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001673 return( ret );
1674 }
1675 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001676 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001677 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001678 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001679 }
1680#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001681 p = (unsigned char *) key;
1682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001684
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685 return( ret );
1686}
1687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#endif /* MBEDTLS_PK_PARSE_C */