blob: 99464e0d8a3874c6a70a68d58662fb1b6e71579b [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)
56#include "mbedtls/ecc.h"
57#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}
200#endif
201
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}
Jarno Lamsab1760922019-04-18 15:58:34 +0300247#endif
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{
553
554 if( ilen < 1 )
555 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
556
Hanno Beckerad353f22019-08-20 13:04:30 +0100557 /* We are not handling the point at infinity. */
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300558
559 if( buf[0] != 0x04 )
560 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
561
562 if( ilen != 2 * NUM_ECC_BYTES + 1 )
563 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
564
Jarno Lamsab1760922019-04-18 15:58:34 +0300565 memcpy( pt, buf + 1, ilen - 1);
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300566
567 return( 0 );
568}
569
570static int pk_get_ueccpubkey( unsigned char **p,
571 const unsigned char *end,
572 uint8_t *pk_context)
573{
574 int ret;
575
Jarno Lamsab1760922019-04-18 15:58:34 +0300576 ret = uecc_public_key_read_binary( pk_context,
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300577 (const unsigned char *) *p, end - *p );
578
579 /*
580 * We know uecc_public_key_read_binary consumed all bytes or failed
581 */
582 *p = (unsigned char *) end;
583
584 return( ret );
585}
586#endif /* MBEDTLS_USE_TINYCRYPT */
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589/*
590 * RSAPublicKey ::= SEQUENCE {
591 * modulus INTEGER, -- n
592 * publicExponent INTEGER -- e
593 * }
594 */
595static int pk_get_rsapubkey( unsigned char **p,
596 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200598{
599 int ret;
600 size_t len;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
603 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
604 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200605
606 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
608 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200609
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100610 /* Import N */
611 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200613
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100614 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
615 NULL, 0, NULL, 0 ) ) != 0 )
616 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
617
618 *p += len;
619
620 /* Import E */
621 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
622 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
623
624 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
625 NULL, 0, *p, len ) ) != 0 )
626 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
627
628 *p += len;
629
Hanno Becker895c5ab2018-01-05 08:08:09 +0000630 if( mbedtls_rsa_complete( rsa ) != 0 ||
631 mbedtls_rsa_check_pubkey( rsa ) != 0 )
632 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100633 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000634 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100635
Paul Bakker1a7550a2013-09-15 13:01:22 +0200636 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
638 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640 return( 0 );
641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643
644/* Get a PK algorithm identifier
645 *
646 * AlgorithmIdentifier ::= SEQUENCE {
647 * algorithm OBJECT IDENTIFIER,
648 * parameters ANY DEFINED BY algorithm OPTIONAL }
649 */
650static int pk_get_pk_alg( unsigned char **p,
651 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200653{
654 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
660 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
663 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664
665 /*
666 * No parameters with RSA (only for EC)
667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 if( *pk_alg == MBEDTLS_PK_RSA &&
669 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200670 params->len != 0 ) )
671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200673 }
674
675 return( 0 );
676}
677
678/*
679 * SubjectPublicKeyInfo ::= SEQUENCE {
680 * algorithm AlgorithmIdentifier,
681 * subjectPublicKey BIT STRING }
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
684 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685{
686 int ret;
687 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_asn1_buf alg_params;
689 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
690 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691
Gilles Peskinee97dc602018-12-19 00:51:38 +0100692 PK_VALIDATE_RET( p != NULL );
693 PK_VALIDATE_RET( *p != NULL );
694 PK_VALIDATE_RET( end != NULL );
695 PK_VALIDATE_RET( pk != NULL );
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
698 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200701 }
702
703 end = *p + len;
704
705 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
706 return( ret );
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
709 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710
711 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
713 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
716 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200718 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719 return( ret );
720
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300721#if defined(MBEDTLS_USE_TINYCRYPT)
722 if( pk_alg == MBEDTLS_PK_ECDSA )
723 {
724 ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx );
725 }
726#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727#if defined(MBEDTLS_RSA_C)
728 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200729 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732#endif /* MBEDTLS_RSA_C */
733#if defined(MBEDTLS_ECP_C)
734 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_ECP_C */
741 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200742
743 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
745 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200746
747 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200749
750 return( ret );
751}
752
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754/*
755 * Parse a PKCS#1 encoded private RSA key
756 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200758 const unsigned char *key,
759 size_t keylen )
760{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100761 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200762 size_t len;
763 unsigned char *p, *end;
764
Hanno Beckerefa14e82017-10-11 19:45:19 +0100765 mbedtls_mpi T;
766 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100767
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768 p = (unsigned char *) key;
769 end = p + keylen;
770
771 /*
772 * This function parses the RSAPrivateKey (PKCS#1)
773 *
774 * RSAPrivateKey ::= SEQUENCE {
775 * version Version,
776 * modulus INTEGER, -- n
777 * publicExponent INTEGER, -- e
778 * privateExponent INTEGER, -- d
779 * prime1 INTEGER, -- p
780 * prime2 INTEGER, -- q
781 * exponent1 INTEGER, -- d mod (p-1)
782 * exponent2 INTEGER, -- d mod (q-1)
783 * coefficient INTEGER, -- (inverse of q) mod p
784 * otherPrimeInfos OtherPrimeInfos OPTIONAL
785 * }
786 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
788 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791 }
792
793 end = p + len;
794
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100795 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200798 }
799
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100800 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200801 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200803 }
804
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100805 /* Import N */
806 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
807 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
808 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
809 NULL, 0, NULL, 0 ) ) != 0 )
810 goto cleanup;
811 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100813 /* Import E */
814 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
815 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
816 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
817 NULL, 0, p, len ) ) != 0 )
818 goto cleanup;
819 p += len;
820
821 /* Import D */
822 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
823 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
824 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
825 p, len, NULL, 0 ) ) != 0 )
826 goto cleanup;
827 p += len;
828
829 /* Import P */
830 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
831 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
832 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
833 NULL, 0, NULL, 0 ) ) != 0 )
834 goto cleanup;
835 p += len;
836
837 /* Import Q */
838 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
839 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
840 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
841 NULL, 0, NULL, 0 ) ) != 0 )
842 goto cleanup;
843 p += len;
844
845 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100846 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100847 goto cleanup;
848
849 /* Check optional parameters */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100850 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
851 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
852 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100853 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
855 if( p != end )
856 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100857 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
858 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859 }
860
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100861cleanup:
862
Hanno Beckerefa14e82017-10-11 19:45:19 +0100863 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100864
865 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100867 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100868 if( ( ret & 0xff80 ) == 0 )
869 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
870 else
871 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874 }
875
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100876 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200877}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200878#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879
Jarno Lamsab1760922019-04-18 15:58:34 +0300880#if defined(MBEDTLS_USE_TINYCRYPT)
881static int pk_parse_key_sec1_der( mbedtls_uecc_keypair *keypair,
882 const unsigned char *key,
883 size_t keylen)
884{
885 int ret;
886 int version, pubkey_done;
887 size_t len;
888 mbedtls_asn1_buf params;
889 unsigned char *p = (unsigned char *) key;
890 unsigned char *end = p + keylen;
891 unsigned char *end2;
892
893 /*
894 * RFC 5915, or SEC1 Appendix C.4
895 *
896 * ECPrivateKey ::= SEQUENCE {
897 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
898 * privateKey OCTET STRING,
899 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
900 * publicKey [1] BIT STRING OPTIONAL
901 * }
902 */
903 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
904 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
905 {
906 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
907 }
908
909 end = p + len;
910
911 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
912 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
913
914 if( version != 1 )
915 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
916
917 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
918 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
919
Hanno Beckerad353f22019-08-20 13:04:30 +0100920 memcpy( keypair->private_key, p, len );
Jarno Lamsab1760922019-04-18 15:58:34 +0300921
922 p += len;
923
924 pubkey_done = 0;
925 if( p != end )
926 {
927 /*
928 * Is 'parameters' present?
929 */
930 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
931 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
932 {
933 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
934 ( ret = pk_use_ecparams( &params ) ) != 0 )
935 {
936 return( ret );
937 }
938 }
939 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
940 {
941 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
942 }
943 }
944
945 if( p != end )
946 {
947 /*
948 * Is 'publickey' present? If not, or if we can't read it (eg because it
949 * is compressed), create it from the private key.
950 */
951 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
952 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
953 {
954 end2 = p + len;
955
956 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
957 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
958
959 if( p + len != end2 )
960 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
961 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
962
Hanno Beckerad353f22019-08-20 13:04:30 +0100963 if( ( ret = uecc_public_key_read_binary( keypair->public_key,
Jarno Lamsab1760922019-04-18 15:58:34 +0300964 (const unsigned char *) p, end2 - p ) ) == 0 )
Hanno Beckerad353f22019-08-20 13:04:30 +0100965 {
Jarno Lamsab1760922019-04-18 15:58:34 +0300966 pubkey_done = 1;
Hanno Beckerad353f22019-08-20 13:04:30 +0100967 }
Jarno Lamsab1760922019-04-18 15:58:34 +0300968 else
969 {
970 /*
971 * The only acceptable failure mode of pk_get_ecpubkey() above
972 * is if the point format is not recognized.
973 */
974 if( ret != MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE )
975 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
976 }
977 }
978 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
979 {
980 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
981 }
982 }
983
984 //TODO: Do we need to support derived public keys with uecc?
985
986 return( 0 );
987}
988#else
989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991/*
992 * Parse a SEC1 encoded private EC key
993 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 const unsigned char *key,
996 size_t keylen )
997{
998 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100999 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001002 unsigned char *p = (unsigned char *) key;
1003 unsigned char *end = p + keylen;
1004 unsigned char *end2;
1005
1006 /*
1007 * RFC 5915, or SEC1 Appendix C.4
1008 *
1009 * ECPrivateKey ::= SEQUENCE {
1010 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1011 * privateKey OCTET STRING,
1012 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1013 * publicKey [1] BIT STRING OPTIONAL
1014 * }
1015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1017 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001020 }
1021
1022 end = p + len;
1023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1025 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026
1027 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1031 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_ecp_keypair_free( eck );
1036 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 }
1038
1039 p += len;
1040
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001041 pubkey_done = 0;
1042 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001044 /*
1045 * Is 'parameters' present?
1046 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001047 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1048 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001049 {
1050 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
1051 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
1052 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001053 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001054 return( ret );
1055 }
1056 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001057 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001061 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001062 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001063
Jethro Beekmand2df9362018-02-16 13:11:04 -08001064 if( p != end )
1065 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001066 /*
1067 * Is 'publickey' present? If not, or if we can't read it (eg because it
1068 * is compressed), create it from the private key.
1069 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001070 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1071 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001072 {
1073 end2 = p + len;
1074
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001075 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1076 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001077
1078 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001079 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1080 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001081
1082 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
1083 pubkey_done = 1;
1084 else
1085 {
1086 /*
1087 * The only acceptable failure mode of pk_get_ecpubkey() above
1088 * is if the point format is not recognized.
1089 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001090 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1091 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001092 }
1093 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001094 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001095 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001096 mbedtls_ecp_keypair_free( eck );
1097 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001098 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001099 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001100
1101 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001103 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_ecp_keypair_free( eck );
1106 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001107 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 return( ret );
1113 }
1114
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001115 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117#endif /* MBEDTLS_ECP_C */
Jarno Lamsab1760922019-04-18 15:58:34 +03001118#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119
1120/*
1121 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001122 *
1123 * Notes:
1124 *
1125 * - This function does not own the key buffer. It is the
1126 * responsibility of the caller to take care of zeroizing
1127 * and freeing it after use.
1128 *
1129 * - The function is responsible for freeing the provided
1130 * PK context on failure.
1131 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 */
1133static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135 const unsigned char* key,
1136 size_t keylen )
1137{
1138 int ret, version;
1139 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141 unsigned char *p = (unsigned char *) key;
1142 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1144 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145
1146 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001147 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 *
1149 * PrivateKeyInfo ::= SEQUENCE {
1150 * version Version,
1151 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1152 * privateKey PrivateKey,
1153 * attributes [0] IMPLICIT Attributes OPTIONAL }
1154 *
1155 * Version ::= INTEGER
1156 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1157 * PrivateKey ::= OCTET STRING
1158 *
1159 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1160 */
1161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1163 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 }
1167
1168 end = p + len;
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1171 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172
1173 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
1176 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1180 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
1182 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1184 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1187 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001189 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 return( ret );
1191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192#if defined(MBEDTLS_RSA_C)
1193 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 return( ret );
1199 }
1200 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#endif /* MBEDTLS_RSA_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001202#if defined(MBEDTLS_USE_TINYCRYPT)
1203 if( pk_alg == MBEDTLS_PK_ECDSA)
1204 {
1205 if( ( ret = pk_use_ecparams( &params ) ) != 0 ||
1206 ( ret = pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk ), p, len ) ) != 0)
1207 {
1208 return( ret );
1209 }
1210 }
1211#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_ECP_C)
1213 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1216 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219 return( ret );
1220 }
1221 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#endif /* MBEDTLS_ECP_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001225
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001226 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227}
1228
1229/*
1230 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001231 *
1232 * To save space, the decryption happens in-place on the given key buffer.
1233 * Also, while this function may modify the keybuffer, it doesn't own it,
1234 * and instead it is the responsibility of the caller to zeroize and properly
1235 * free it after use.
1236 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001241 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242 const unsigned char *pwd, size_t pwdlen )
1243{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001244 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001246 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1249#if defined(MBEDTLS_PKCS12_C)
1250 mbedtls_cipher_type_t cipher_alg;
1251 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252#endif
1253
Hanno Becker2aa80a72017-09-07 15:28:45 +01001254 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255 end = p + keylen;
1256
1257 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259
1260 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001261 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 *
1263 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1264 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1265 * encryptedData EncryptedData
1266 * }
1267 *
1268 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1269 *
1270 * EncryptedData ::= OCTET STRING
1271 *
1272 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001273 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1276 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279 }
1280
1281 end = p + len;
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1284 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1287 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288
Hanno Beckerfab35692017-08-25 13:38:26 +01001289 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290
1291 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001292 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_PKCS12_C)
1295 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298 cipher_alg, md_alg,
1299 pwd, pwdlen, p, len, buf ) ) != 0 )
1300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001301 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1302 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303
1304 return( ret );
1305 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001306
1307 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1312 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 pwd, pwdlen,
1314 p, len, buf ) ) != 0 )
1315 {
1316 return( ret );
1317 }
1318
1319 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1323 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001324
1325 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326 }
1327 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#endif /* MBEDTLS_PKCS12_C */
1329#if defined(MBEDTLS_PKCS5_C)
1330 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333 p, len, buf ) ) != 0 )
1334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1336 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337
1338 return( ret );
1339 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001340
1341 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 }
1343 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001345 {
1346 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001347 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001349 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001351
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355
1356/*
1357 * Parse a private key
1358 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 const unsigned char *key, size_t keylen,
1361 const unsigned char *pwd, size_t pwdlen )
1362{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001363#if defined(MBEDTLS_PKCS12_C) || \
1364 defined(MBEDTLS_PKCS5_C) || \
1365 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001367#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001372#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373
Gilles Peskinee97dc602018-12-19 00:51:38 +01001374 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001375 if( keylen == 0 )
1376 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1377 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001378
1379#if defined(MBEDTLS_PEM_PARSE_C)
1380 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001383 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001384 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001385 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1386 else
1387 ret = mbedtls_pem_read_buffer( &pem,
1388 "-----BEGIN RSA PRIVATE KEY-----",
1389 "-----END RSA PRIVATE KEY-----",
1390 key, pwd, pwdlen, &len );
1391
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392 if( ret == 0 )
1393 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001394 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001395 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397 pem.buf, pem.buflen ) ) != 0 )
1398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400 }
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403 return( ret );
1404 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1406 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1407 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1408 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1409 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001414 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001415 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001416 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1417 else
1418 ret = mbedtls_pem_read_buffer( &pem,
1419 "-----BEGIN EC PRIVATE KEY-----",
1420 "-----END EC PRIVATE KEY-----",
1421 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422 if( ret == 0 )
1423 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001424 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425
Hanno Beckerfab35692017-08-25 13:38:26 +01001426 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001428 pem.buf, pem.buflen ) ) != 0 )
1429 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431 }
1432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434 return( ret );
1435 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1437 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1438 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1439 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1440 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001444 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001445 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001446 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1447 else
1448 ret = mbedtls_pem_read_buffer( &pem,
1449 "-----BEGIN PRIVATE KEY-----",
1450 "-----END PRIVATE KEY-----",
1451 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452 if( ret == 0 )
1453 {
1454 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1455 pem.buf, pem.buflen ) ) != 0 )
1456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 }
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461 return( ret );
1462 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464 return( ret );
1465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001467 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001468 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001469 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1470 else
1471 ret = mbedtls_pem_read_buffer( &pem,
1472 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1473 "-----END ENCRYPTED PRIVATE KEY-----",
1474 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001475 if( ret == 0 )
1476 {
1477 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1478 pem.buf, pem.buflen,
1479 pwd, pwdlen ) ) != 0 )
1480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482 }
1483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001485 return( ret );
1486 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490#else
1491 ((void) pwd);
1492 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494
1495 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001496 * At this point we only know it's not a PEM formatted key. Could be any
1497 * of the known DER encoded private key formats
1498 *
1499 * We try the different DER format parsers to see if one passes without
1500 * error
1501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001504 unsigned char *key_copy;
1505
1506 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1507 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1508
1509 memcpy( key_copy, key, keylen );
1510
1511 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1512 pwd, pwdlen );
1513
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001514 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001515 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001516 }
1517
Hanno Beckerfab35692017-08-25 13:38:26 +01001518 if( ret == 0 )
1519 return( 0 );
1520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001521 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001522 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001524 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525 {
1526 return( ret );
1527 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001528#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001529
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001530 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531 return( 0 );
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001534 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537
Hanno Becker9be19262017-09-08 12:39:44 +01001538 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001539 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1540 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001541 {
1542 return( 0 );
1543 }
1544
Hanno Becker780f0a42018-10-10 11:23:33 +01001545 mbedtls_pk_free( pk );
1546 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001548
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001549#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +01001550 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001551 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1552 pk_parse_key_sec1_der( mbedtls_uecc_pk( *pk),
1553 key, keylen) == 0)
1554 {
1555 return( 0 );
1556 }
1557#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001559 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001560 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1561 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1562 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563 {
1564 return( 0 );
1565 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001566 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_ECP_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001568#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001569
Hanno Becker780f0a42018-10-10 11:23:33 +01001570 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1571 * it is ok to leave the PK context initialized but not
1572 * freed: It is the caller's responsibility to call pk_init()
1573 * before calling this function, and to call pk_free()
1574 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1575 * isn't, this leads to mbedtls_pk_free() being called
1576 * twice, once here and once by the caller, but this is
1577 * also ok and in line with the mbedtls_pk_free() calls
1578 * on failed PEM parsing attempts. */
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001581}
1582
1583/*
1584 * Parse a public key
1585 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001587 const unsigned char *key, size_t keylen )
1588{
1589 int ret;
1590 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001591#if defined(MBEDTLS_RSA_C)
1592 const mbedtls_pk_info_t *pk_info;
1593#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001595 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001597#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001598
Gilles Peskinee97dc602018-12-19 00:51:38 +01001599 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001600 if( keylen == 0 )
1601 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001602 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1603
1604#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001606#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001607 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001608 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001609 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1610 else
1611 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001612 "-----BEGIN RSA PUBLIC KEY-----",
1613 "-----END RSA PUBLIC KEY-----",
1614 key, NULL, 0, &len );
1615
1616 if( ret == 0 )
1617 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001618 p = pem.buf;
1619 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1620 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001621
Ron Eldor84df1ae2017-10-16 17:11:52 +03001622 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1623 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001624
Ron Eldor84df1ae2017-10-16 17:11:52 +03001625 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1626 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001627
Ron Eldord0c56de2017-10-10 17:03:08 +03001628 mbedtls_pem_free( &pem );
1629 return( ret );
1630 }
1631 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1632 {
1633 mbedtls_pem_free( &pem );
1634 return( ret );
1635 }
1636#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001637
1638 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001639 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001640 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1641 else
1642 ret = mbedtls_pem_read_buffer( &pem,
1643 "-----BEGIN PUBLIC KEY-----",
1644 "-----END PUBLIC KEY-----",
1645 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001646
1647 if( ret == 0 )
1648 {
1649 /*
1650 * Was PEM encoded
1651 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001652 p = pem.buf;
1653
1654 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1655 mbedtls_pem_free( &pem );
1656 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001657 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001660 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661 return( ret );
1662 }
Ron Eldor5472d432017-10-17 09:49:00 +03001663 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001664#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001665
1666#if defined(MBEDTLS_RSA_C)
1667 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1668 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1669
1670 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1671 return( ret );
1672
Ron Eldor9566ff72018-02-07 18:59:41 +02001673 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001674 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001675 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001676 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001677 return( ret );
1678 }
1679 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001680 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001681 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001682 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001683 }
1684#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001685 p = (unsigned char *) key;
1686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001688
Paul Bakker1a7550a2013-09-15 13:01:22 +02001689 return( ret );
1690}
1691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#endif /* MBEDTLS_PK_PARSE_C */