blob: 411fbaa1c7168adff369f76012abc3e2fc3b5860 [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{
Hanno Beckerdfb949b2019-08-23 15:22:25 +0100185 mbedtls_uecc_group_id grp_id;
Jarno Lamsab1760922019-04-18 15:58:34 +0300186
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
Hanno Becker3bef6432019-08-21 11:47:37 +0100249#if !defined(MBEDTLS_USE_TINYCRYPT)
250
Jarno Lamsab1760922019-04-18 15:58:34 +0300251#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200253/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100254 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
255 * WARNING: the resulting group should only be used with
256 * pk_group_id_from_specified(), since its base point may not be set correctly
257 * if it was encoded compressed.
258 *
259 * SpecifiedECDomain ::= SEQUENCE {
260 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
261 * fieldID FieldID {{FieldTypes}},
262 * curve Curve,
263 * base ECPoint,
264 * order INTEGER,
265 * cofactor INTEGER OPTIONAL,
266 * hash HashAlgorithm OPTIONAL,
267 * ...
268 * }
269 *
270 * We only support prime-field as field type, and ignore hash and cofactor.
271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273{
274 int ret;
275 unsigned char *p = params->p;
276 const unsigned char * const end = params->p + params->len;
277 const unsigned char *end_field, *end_curve;
278 size_t len;
279 int ver;
280
281 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
283 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284
285 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
288 /*
289 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
290 * fieldType FIELD-ID.&id({IOSet}),
291 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
292 * }
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
295 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296 return( ret );
297
298 end_field = p + len;
299
300 /*
301 * FIELD-ID ::= TYPE-IDENTIFIER
302 * FieldTypes FIELD-ID ::= {
303 * { Prime-p IDENTIFIED BY prime-field } |
304 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
305 * }
306 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100309 return( ret );
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300312 mbedtls_platform_memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 }
316
317 p += len;
318
319 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
321 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200323 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324
325 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328
329 /*
330 * Curve ::= SEQUENCE {
331 * a FieldElement,
332 * b FieldElement,
333 * seed BIT STRING OPTIONAL
334 * -- Shall be present if used in SpecifiedECDomain
335 * -- with version equal to ecdpVer2 or ecdpVer3
336 * }
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
339 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340 return( ret );
341
342 end_curve = p + len;
343
344 /*
345 * FieldElement ::= OCTET STRING
346 * containing an integer in the case of a prime field
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
349 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100352 }
353
354 p += len;
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
357 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360 }
361
362 p += len;
363
364 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100366 p += len;
367
368 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
370 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371
372 /*
373 * ECPoint ::= OCTET STRING
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
376 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100379 ( const unsigned char *) p, len ) ) != 0 )
380 {
381 /*
382 * If we can't read the point because it's compressed, cheat by
383 * reading only the X coordinate and the parity bit of Y.
384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100386 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 len != mbedtls_mpi_size( &grp->P ) + 1 ||
388 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
389 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
390 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100393 }
394 }
395
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396 p += len;
397
398 /*
399 * order INTEGER
400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
402 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200404 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
406 /*
407 * Allow optional elements by purposefully not enforcing p == end here.
408 */
409
410 return( 0 );
411}
412
413/*
414 * Find the group id associated with an (almost filled) group as generated by
415 * pk_group_from_specified(), or return an error if unknown.
416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417static 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 +0100418{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100419 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_ecp_group ref;
421 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426 {
427 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200429 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430
431 /* Compare to the group we were given, starting with easy tests */
432 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
434 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
435 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
436 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
437 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
438 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 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 +0100441 {
442 break;
443 }
444
445 }
446
447cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100449
450 *grp_id = *id;
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
453 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100454
455 return( ret );
456}
457
458/*
459 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
462 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463{
464 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468
469 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
470 goto cleanup;
471
472 ret = pk_group_id_from_group( &grp, grp_id );
473
474cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100476
477 return( ret );
478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100480
481/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100483 *
484 * ECParameters ::= CHOICE {
485 * namedCurve OBJECT IDENTIFIER
486 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
487 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490{
491 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
497 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100498 }
499 else
500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100502 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
503 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100504#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100506#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100507 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508
509 /*
510 * grp may already be initilialized; if so, make sure IDs match
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
513 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200515 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516 return( ret );
517
518 return( 0 );
519}
520
521/*
522 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100523 *
524 * The caller is responsible for clearing the structure upon failure if
525 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527 */
528static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530{
531 int ret;
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100534 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537 }
538
539 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541 */
542 *p = (unsigned char *) end;
543
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100544 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_ECP_C */
Hanno Becker3bef6432019-08-21 11:47:37 +0100547#endif /* !MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200548
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300549#if defined(MBEDTLS_USE_TINYCRYPT)
550/*
551 * Import a point from unsigned binary data (SEC1 2.3.4)
552 */
Hanno Becker7e38c372019-08-20 17:01:50 +0100553static int uecc_public_key_read_binary( mbedtls_uecc_keypair *uecc_keypair,
554 const unsigned char *buf, size_t ilen )
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300555{
Hanno Becker68d54782019-08-20 13:19:09 +0100556 if( ilen != 2 * NUM_ECC_BYTES + 1 )
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300557 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
558
Hanno Beckerad353f22019-08-20 13:04:30 +0100559 /* We are not handling the point at infinity. */
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300560
561 if( buf[0] != 0x04 )
562 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
563
Teppo Järvelin91d79382019-10-02 09:09:31 +0300564 mbedtls_platform_memcpy( uecc_keypair->public_key, buf + 1, 2 * NUM_ECC_BYTES );
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300565
566 return( 0 );
567}
568
569static int pk_get_ueccpubkey( unsigned char **p,
570 const unsigned char *end,
571 uint8_t *pk_context)
572{
Hanno Becker7e38c372019-08-20 17:01:50 +0100573 mbedtls_uecc_keypair *uecc_keypair = (mbedtls_uecc_keypair *) pk_context;
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300574 int ret;
575
Andrzej Kurek3ed65d22020-07-17 02:10:40 -0400576 if( ( ret = uecc_public_key_read_binary( uecc_keypair,
577 (const unsigned char *) *p, end - *p ) )
578 != 0 )
579 return ret;
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300580
581 /*
582 * We know uecc_public_key_read_binary consumed all bytes or failed
583 */
584 *p = (unsigned char *) end;
585
586 return( ret );
587}
588#endif /* MBEDTLS_USE_TINYCRYPT */
589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200591/*
592 * RSAPublicKey ::= SEQUENCE {
593 * modulus INTEGER, -- n
594 * publicExponent INTEGER -- e
595 * }
596 */
597static int pk_get_rsapubkey( unsigned char **p,
598 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600{
601 int ret;
602 size_t len;
603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
605 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
606 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200607
608 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
610 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200611
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100612 /* Import N */
613 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200615
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100616 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
617 NULL, 0, NULL, 0 ) ) != 0 )
618 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
619
620 *p += len;
621
622 /* Import E */
623 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
624 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
625
626 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
627 NULL, 0, *p, len ) ) != 0 )
628 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
629
630 *p += len;
631
Hanno Becker895c5ab2018-01-05 08:08:09 +0000632 if( mbedtls_rsa_complete( rsa ) != 0 ||
633 mbedtls_rsa_check_pubkey( rsa ) != 0 )
634 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100635 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000636 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100637
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
640 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200641
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642 return( 0 );
643}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200645
646/* Get a PK algorithm identifier
647 *
648 * AlgorithmIdentifier ::= SEQUENCE {
649 * algorithm OBJECT IDENTIFIER,
650 * parameters ANY DEFINED BY algorithm OPTIONAL }
651 */
652static int pk_get_pk_alg( unsigned char **p,
653 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200655{
656 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200659 mbedtls_platform_memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
662 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
665 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666
667 /*
668 * No parameters with RSA (only for EC)
669 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 if( *pk_alg == MBEDTLS_PK_RSA &&
671 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200672 params->len != 0 ) )
673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200675 }
676
677 return( 0 );
678}
679
680/*
681 * SubjectPublicKeyInfo ::= SEQUENCE {
682 * algorithm AlgorithmIdentifier,
683 * subjectPublicKey BIT STRING }
684 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
686 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200687{
688 int ret;
689 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 mbedtls_asn1_buf alg_params;
691 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200692 mbedtls_pk_handle_t pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693
Gilles Peskinee97dc602018-12-19 00:51:38 +0100694 PK_VALIDATE_RET( p != NULL );
695 PK_VALIDATE_RET( *p != NULL );
696 PK_VALIDATE_RET( end != NULL );
697 PK_VALIDATE_RET( pk != NULL );
698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
700 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703 }
704
705 end = *p + len;
706
707 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
708 return( ret );
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
711 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200712
713 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
715 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200717 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200720 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721 return( ret );
722
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 */
Hanno Becker8cf2f5e2019-08-21 11:51:53 +0100729#if defined(MBEDTLS_USE_TINYCRYPT)
730 if( pk_alg == MBEDTLS_PK_ECKEY )
731 {
732 ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx );
733 } else
734#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735#if defined(MBEDTLS_ECP_C)
736 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742#endif /* MBEDTLS_ECP_C */
Hanno Becker8cf2f5e2019-08-21 11:51:53 +0100743#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200744 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745
746 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200747 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
748 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200749
750 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752
753 return( ret );
754}
755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200757/*
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400758 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
759 *
760 * The value zero is:
761 * - never a valid value for an RSA parameter
762 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
763 *
764 * Since values can't be omitted in PKCS#1, passing a zero value to
765 * rsa_complete() would be incorrect, so reject zero values early.
766 */
767static int asn1_get_nonzero_mpi( unsigned char **p,
768 const unsigned char *end,
769 mbedtls_mpi *X )
770{
771 int ret;
772
773 ret = mbedtls_asn1_get_mpi( p, end, X );
774 if( ret != 0 )
775 return( ret );
776
777 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
778 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
779
780 return( 0 );
781}
782
783/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200784 * Parse a PKCS#1 encoded private RSA key
785 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787 const unsigned char *key,
788 size_t keylen )
789{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100790 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200791 size_t len;
792 unsigned char *p, *end;
793
Hanno Beckerefa14e82017-10-11 19:45:19 +0100794 mbedtls_mpi T;
795 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100796
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797 p = (unsigned char *) key;
798 end = p + keylen;
799
800 /*
801 * This function parses the RSAPrivateKey (PKCS#1)
802 *
803 * RSAPrivateKey ::= SEQUENCE {
804 * version Version,
805 * modulus INTEGER, -- n
806 * publicExponent INTEGER, -- e
807 * privateExponent INTEGER, -- d
808 * prime1 INTEGER, -- p
809 * prime2 INTEGER, -- q
810 * exponent1 INTEGER, -- d mod (p-1)
811 * exponent2 INTEGER, -- d mod (q-1)
812 * coefficient INTEGER, -- (inverse of q) mod p
813 * otherPrimeInfos OtherPrimeInfos OPTIONAL
814 * }
815 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
817 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200820 }
821
822 end = p + len;
823
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100824 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200826 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827 }
828
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100829 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200831 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200832 }
833
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834 /* Import N */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400835 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
836 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
837 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100838 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200839
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100840 /* Import E */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400841 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
842 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
843 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100844 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845
846 /* Import D */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400847 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
848 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
849 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100850 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100851
852 /* Import P */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400853 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
854 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
855 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100856 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100857
858 /* Import Q */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400859 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
860 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
861 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100862 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100863
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400864#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd32b6e692020-01-29 13:09:55 -0500865 /*
866 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
867 * that they can be easily recomputed from D, P and Q. However by
868 * parsing them from the PKCS1 structure it is possible to avoid
869 * recalculating them which both reduces the overhead of loading
870 * RSA private keys into memory and also avoids side channels which
871 * can arise when computing those values, since all of D, P, and Q
872 * are secret. See https://eprint.iacr.org/2020/055 for a
873 * description of one such attack.
874 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100875
Jack Lloyd32b6e692020-01-29 13:09:55 -0500876 /* Import DP */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400877 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
878 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500879 goto cleanup;
880
881 /* Import DQ */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400882 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
883 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500884 goto cleanup;
885
886 /* Import QP */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400887 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
888 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500889 goto cleanup;
890
891#else
892 /* Verify existance of the CRT params */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400893 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
894 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
895 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500896 goto cleanup;
897#endif
898
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400899 /* rsa_complete() doesn't complete anything with the default
900 * implementation but is still called:
901 * - for the benefit of alternative implementation that may want to
902 * pre-compute stuff beyond what's provided (eg Montgomery factors)
903 * - as is also sanity-checks the key
904 *
905 * Furthermore, we also check the public part for consistency with
906 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
907 */
908 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
909 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
910 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100911 goto cleanup;
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400912 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200913
914 if( p != end )
915 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100916 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
917 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200918 }
919
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100920cleanup:
921
Hanno Beckerefa14e82017-10-11 19:45:19 +0100922 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100923
924 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100926 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100927 if( ( ret & 0xff80 ) == 0 )
928 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
929 else
930 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933 }
934
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100935 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200938
Jarno Lamsab1760922019-04-18 15:58:34 +0300939#if defined(MBEDTLS_USE_TINYCRYPT)
940static int pk_parse_key_sec1_der( mbedtls_uecc_keypair *keypair,
941 const unsigned char *key,
942 size_t keylen)
943{
944 int ret;
945 int version, pubkey_done;
946 size_t len;
947 mbedtls_asn1_buf params;
948 unsigned char *p = (unsigned char *) key;
949 unsigned char *end = p + keylen;
950 unsigned char *end2;
951
952 /*
953 * RFC 5915, or SEC1 Appendix C.4
954 *
955 * ECPrivateKey ::= SEQUENCE {
956 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
957 * privateKey OCTET STRING,
958 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
959 * publicKey [1] BIT STRING OPTIONAL
960 * }
961 */
962 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
963 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
964 {
965 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
966 }
967
968 end = p + len;
969
970 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
971 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
972
973 if( version != 1 )
974 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
975
976 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
977 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
978
Teppo Järvelin91d79382019-10-02 09:09:31 +0300979 mbedtls_platform_memcpy( keypair->private_key, p, len );
Jarno Lamsab1760922019-04-18 15:58:34 +0300980
981 p += len;
982
983 pubkey_done = 0;
984 if( p != end )
985 {
986 /*
987 * Is 'parameters' present?
988 */
989 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
990 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
991 {
992 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
993 ( ret = pk_use_ecparams( &params ) ) != 0 )
994 {
995 return( ret );
996 }
997 }
998 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
999 {
1000 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1001 }
1002 }
1003
1004 if( p != end )
1005 {
1006 /*
1007 * Is 'publickey' present? If not, or if we can't read it (eg because it
1008 * is compressed), create it from the private key.
1009 */
1010 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1011 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
1012 {
1013 end2 = p + len;
1014
1015 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1016 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1017
1018 if( p + len != end2 )
1019 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1020 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1021
Hanno Becker7e38c372019-08-20 17:01:50 +01001022 if( ( ret = uecc_public_key_read_binary( keypair,
Jarno Lamsab1760922019-04-18 15:58:34 +03001023 (const unsigned char *) p, end2 - p ) ) == 0 )
Hanno Beckerad353f22019-08-20 13:04:30 +01001024 {
Jarno Lamsab1760922019-04-18 15:58:34 +03001025 pubkey_done = 1;
Hanno Beckerad353f22019-08-20 13:04:30 +01001026 }
Jarno Lamsab1760922019-04-18 15:58:34 +03001027 else
1028 {
1029 /*
Hanno Becker683d84a2019-09-04 16:10:46 +01001030 * The only acceptable failure mode of
1031 * uecc_public_key_read_binary() above
Jarno Lamsab1760922019-04-18 15:58:34 +03001032 * is if the point format is not recognized.
1033 */
1034 if( ret != MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE )
1035 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1036 }
1037 }
1038 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
1039 {
1040 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1041 }
1042 }
1043
Hanno Beckerd45f3832019-08-20 14:21:40 +01001044 if( !pubkey_done )
1045 {
1046 ret = uECC_compute_public_key( keypair->private_key,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +01001047 keypair->public_key );
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001048 if( ret == UECC_FAULT_DETECTED )
1049 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1050 if( ret != UECC_SUCCESS )
Hanno Beckerd45f3832019-08-20 14:21:40 +01001051 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1052 }
Jarno Lamsab1760922019-04-18 15:58:34 +03001053
1054 return( 0 );
1055}
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001056#else /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +03001057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059/*
1060 * Parse a SEC1 encoded private EC key
1061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063 const unsigned char *key,
1064 size_t keylen )
1065{
1066 int ret;
Andrzej Kurek3ed65d22020-07-17 02:10:40 -04001067 int version, pubkey_done = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001068 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070 unsigned char *p = (unsigned char *) key;
1071 unsigned char *end = p + keylen;
1072 unsigned char *end2;
1073
1074 /*
1075 * RFC 5915, or SEC1 Appendix C.4
1076 *
1077 * ECPrivateKey ::= SEQUENCE {
1078 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1079 * privateKey OCTET STRING,
1080 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1081 * publicKey [1] BIT STRING OPTIONAL
1082 * }
1083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1085 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088 }
1089
1090 end = p + len;
1091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1093 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094
1095 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1099 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_ecp_keypair_free( eck );
1104 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105 }
1106
1107 p += len;
1108
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001109 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001111 /*
1112 * Is 'parameters' present?
1113 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001114 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1115 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001116 {
1117 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
1118 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
1119 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001120 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001121 return( ret );
1122 }
1123 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001124 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001128 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001129 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001130
Jethro Beekmand2df9362018-02-16 13:11:04 -08001131 if( p != end )
1132 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001133 /*
1134 * Is 'publickey' present? If not, or if we can't read it (eg because it
1135 * is compressed), create it from the private key.
1136 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001137 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1138 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001139 {
1140 end2 = p + len;
1141
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001142 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1143 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001144
1145 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001146 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1147 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001148
1149 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
1150 pubkey_done = 1;
1151 else
1152 {
1153 /*
1154 * The only acceptable failure mode of pk_get_ecpubkey() above
1155 * is if the point format is not recognized.
1156 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001157 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1158 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001159 }
1160 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001161 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001162 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001163 mbedtls_ecp_keypair_free( eck );
1164 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001165 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001167
1168 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001170 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 mbedtls_ecp_keypair_free( eck );
1173 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001174 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001176 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 return( ret );
1180 }
1181
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001182 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#endif /* MBEDTLS_ECP_C */
Jarno Lamsab1760922019-04-18 15:58:34 +03001185#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186
1187/*
1188 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001189 *
1190 * Notes:
1191 *
1192 * - This function does not own the key buffer. It is the
1193 * responsibility of the caller to take care of zeroizing
1194 * and freeing it after use.
1195 *
1196 * - The function is responsible for freeing the provided
1197 * PK context on failure.
1198 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 */
1200static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202 const unsigned char* key,
1203 size_t keylen )
1204{
1205 int ret, version;
1206 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208 unsigned char *p = (unsigned char *) key;
1209 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001211 mbedtls_pk_handle_t pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001212
1213 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001214 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 *
1216 * PrivateKeyInfo ::= SEQUENCE {
1217 * version Version,
1218 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1219 * privateKey PrivateKey,
1220 * attributes [0] IMPLICIT Attributes OPTIONAL }
1221 *
1222 * Version ::= INTEGER
1223 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1224 * PrivateKey ::= OCTET STRING
1225 *
1226 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1227 */
1228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1230 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001233 }
1234
1235 end = p + len;
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1238 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239
1240 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242
1243 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1247 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248
1249 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1251 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001253 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001256 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257 return( ret );
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_RSA_C)
1260 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 return( ret );
1266 }
1267 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#endif /* MBEDTLS_RSA_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001269#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker75f8d322019-08-20 14:31:50 +01001270 if( pk_alg == MBEDTLS_PK_ECKEY )
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001271 {
1272 if( ( ret = pk_use_ecparams( &params ) ) != 0 ||
Hanno Beckerda779712019-08-21 13:22:59 +01001273 ( ret = pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk ), p, len ) ) != 0)
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001274 {
1275 return( ret );
1276 }
Hanno Beckere65697c2019-08-20 14:31:00 +01001277 } else
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001278#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279#if defined(MBEDTLS_ECP_C)
1280 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1283 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 return( ret );
1287 }
1288 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001290#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001293 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294}
1295
1296/*
1297 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001298 *
1299 * To save space, the decryption happens in-place on the given key buffer.
1300 * Also, while this function may modify the keybuffer, it doesn't own it,
1301 * and instead it is the responsibility of the caller to zeroize and properly
1302 * free it after use.
1303 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001308 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 const unsigned char *pwd, size_t pwdlen )
1310{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001311 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001313 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1316#if defined(MBEDTLS_PKCS12_C)
1317 mbedtls_cipher_type_t cipher_alg;
1318 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319#endif
1320
Hanno Becker2aa80a72017-09-07 15:28:45 +01001321 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 end = p + keylen;
1323
1324 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326
1327 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001328 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329 *
1330 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1331 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1332 * encryptedData EncryptedData
1333 * }
1334 *
1335 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1336 *
1337 * EncryptedData ::= OCTET STRING
1338 *
1339 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001340 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1343 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346 }
1347
1348 end = p + len;
1349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1351 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1354 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355
Hanno Beckerfab35692017-08-25 13:38:26 +01001356 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
1358 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001359 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001360 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_PKCS12_C)
1362 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365 cipher_alg, md_alg,
1366 pwd, pwdlen, p, len, buf ) ) != 0 )
1367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1369 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370
1371 return( ret );
1372 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001373
1374 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1379 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 pwd, pwdlen,
1381 p, len, buf ) ) != 0 )
1382 {
1383 return( ret );
1384 }
1385
1386 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1390 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001391
1392 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 }
1394 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_PKCS12_C */
1396#if defined(MBEDTLS_PKCS5_C)
1397 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001400 p, len, buf ) ) != 0 )
1401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1403 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
1405 return( ret );
1406 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001407
1408 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 }
1410 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001412 {
1413 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001414 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001416 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001418
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422
1423/*
1424 * Parse a private key
1425 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427 const unsigned char *key, size_t keylen,
1428 const unsigned char *pwd, size_t pwdlen )
1429{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001430#if defined(MBEDTLS_PKCS12_C) || \
1431 defined(MBEDTLS_PKCS5_C) || \
1432 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001434#endif
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001435 mbedtls_pk_handle_t pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001439#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440
Gilles Peskinee97dc602018-12-19 00:51:38 +01001441 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001442 if( keylen == 0 )
1443 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1444 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001445
1446#if defined(MBEDTLS_PEM_PARSE_C)
1447 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001451 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1453 else
1454 ret = mbedtls_pem_read_buffer( &pem,
1455 "-----BEGIN RSA PRIVATE KEY-----",
1456 "-----END RSA PRIVATE KEY-----",
1457 key, pwd, pwdlen, &len );
1458
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459 if( ret == 0 )
1460 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001461 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001462 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001464 pem.buf, pem.buflen ) ) != 0 )
1465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001466 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467 }
1468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001470 return( ret );
1471 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1473 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1474 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1475 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1476 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479
Hanno Beckerd336f722019-08-21 11:46:11 +01001480#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001481 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001482 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001483 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1484 else
1485 ret = mbedtls_pem_read_buffer( &pem,
1486 "-----BEGIN EC PRIVATE KEY-----",
1487 "-----END EC PRIVATE KEY-----",
1488 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001489 if( ret == 0 )
1490 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001491 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
Hanno Beckerd336f722019-08-21 11:46:11 +01001493#if defined(MBEDTLS_USE_TINYCRYPT)
1494 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Hanno Beckerda779712019-08-21 13:22:59 +01001495 ( ret = pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk ),
Hanno Beckerd336f722019-08-21 11:46:11 +01001496 pem.buf, pem.buflen ) ) != 0 )
1497#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Beckerfab35692017-08-25 13:38:26 +01001498 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001500 pem.buf, pem.buflen ) ) != 0 )
Hanno Beckerd336f722019-08-21 11:46:11 +01001501#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504 }
1505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 return( ret );
1508 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1510 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1511 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1512 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1513 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001514 return( ret );
Hanno Beckerd336f722019-08-21 11:46:11 +01001515#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001516
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001517 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001518 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001519 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1520 else
1521 ret = mbedtls_pem_read_buffer( &pem,
1522 "-----BEGIN PRIVATE KEY-----",
1523 "-----END PRIVATE KEY-----",
1524 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001525 if( ret == 0 )
1526 {
1527 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1528 pem.buf, pem.buflen ) ) != 0 )
1529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531 }
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534 return( ret );
1535 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001537 return( ret );
1538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001540 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001541 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001542 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1543 else
1544 ret = mbedtls_pem_read_buffer( &pem,
1545 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1546 "-----END ENCRYPTED PRIVATE KEY-----",
1547 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001548 if( ret == 0 )
1549 {
1550 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1551 pem.buf, pem.buflen,
1552 pwd, pwdlen ) ) != 0 )
1553 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001555 }
1556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558 return( ret );
1559 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563#else
1564 ((void) pwd);
1565 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001567
1568 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001569 * At this point we only know it's not a PEM formatted key. Could be any
1570 * of the known DER encoded private key formats
1571 *
1572 * We try the different DER format parsers to see if one passes without
1573 * error
1574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001576 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001577 unsigned char *key_copy;
1578
1579 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1580 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1581
Teppo Järvelin91d79382019-10-02 09:09:31 +03001582 mbedtls_platform_memcpy( key_copy, key, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001583
1584 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1585 pwd, pwdlen );
1586
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001587 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001588 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001589 }
1590
Hanno Beckerfab35692017-08-25 13:38:26 +01001591 if( ret == 0 )
1592 return( 0 );
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001595 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001598 {
1599 return( ret );
1600 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001603 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001604 return( 0 );
1605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001607 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001610
Hanno Becker9be19262017-09-08 12:39:44 +01001611 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001612 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1613 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001614 {
1615 return( 0 );
1616 }
1617
Hanno Becker780f0a42018-10-10 11:23:33 +01001618 mbedtls_pk_free( pk );
1619 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001621
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001622#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +01001623 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001624 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
Hanno Beckerda779712019-08-21 13:22:59 +01001625 pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk),
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001626 key, keylen) == 0)
1627 {
1628 return( 0 );
1629 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001630#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001631#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001632 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001633 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1634 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1635 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001636 {
1637 return( 0 );
1638 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001639 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001641#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001642
Hanno Becker780f0a42018-10-10 11:23:33 +01001643 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1644 * it is ok to leave the PK context initialized but not
1645 * freed: It is the caller's responsibility to call pk_init()
1646 * before calling this function, and to call pk_free()
1647 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1648 * isn't, this leads to mbedtls_pk_free() being called
1649 * twice, once here and once by the caller, but this is
1650 * also ok and in line with the mbedtls_pk_free() calls
1651 * on failed PEM parsing attempts. */
1652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001653 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001654}
1655
1656/*
1657 * Parse a public key
1658 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001660 const unsigned char *key, size_t keylen )
1661{
1662 int ret;
1663 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001664#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001665 mbedtls_pk_handle_t pk_info;
Ron Eldor5472d432017-10-17 09:49:00 +03001666#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001668 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001669 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001670#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001671
Gilles Peskinee97dc602018-12-19 00:51:38 +01001672 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001673 if( keylen == 0 )
1674 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001675 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1676
1677#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001679#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001680 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001681 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001682 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1683 else
1684 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001685 "-----BEGIN RSA PUBLIC KEY-----",
1686 "-----END RSA PUBLIC KEY-----",
1687 key, NULL, 0, &len );
1688
1689 if( ret == 0 )
1690 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001691 p = pem.buf;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001692 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == MBEDTLS_PK_INVALID_HANDLE )
Ron Eldor84df1ae2017-10-16 17:11:52 +03001693 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001694
Ron Eldor84df1ae2017-10-16 17:11:52 +03001695 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1696 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001697
Ron Eldor84df1ae2017-10-16 17:11:52 +03001698 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1699 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001700
Ron Eldord0c56de2017-10-10 17:03:08 +03001701 mbedtls_pem_free( &pem );
1702 return( ret );
1703 }
1704 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1705 {
1706 mbedtls_pem_free( &pem );
1707 return( ret );
1708 }
1709#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001710
1711 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001712 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001713 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1714 else
1715 ret = mbedtls_pem_read_buffer( &pem,
1716 "-----BEGIN PUBLIC KEY-----",
1717 "-----END PUBLIC KEY-----",
1718 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001719
1720 if( ret == 0 )
1721 {
1722 /*
1723 * Was PEM encoded
1724 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001725 p = pem.buf;
1726
1727 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1728 mbedtls_pem_free( &pem );
1729 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001730 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001732 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001734 return( ret );
1735 }
Ron Eldor5472d432017-10-17 09:49:00 +03001736 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001738
1739#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001740 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == MBEDTLS_PK_INVALID_HANDLE )
Ron Eldor40b14a82017-10-16 19:30:00 +03001741 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1742
1743 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1744 return( ret );
1745
Ron Eldor9566ff72018-02-07 18:59:41 +02001746 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001747 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001748 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001749 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001750 return( ret );
1751 }
1752 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001753 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001754 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001755 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001756 }
1757#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001758 p = (unsigned char *) key;
1759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001760 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001761
Paul Bakker1a7550a2013-09-15 13:01:22 +02001762 return( ret );
1763}
1764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001765#endif /* MBEDTLS_PK_PARSE_C */