blob: 688082b56775b3751934e976143a507bf3d9e935 [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
Hanno Becker7e38c372019-08-20 17:01:50 +0100576 ret = uecc_public_key_read_binary( uecc_keypair,
577 (const unsigned char *) *p, end - *p );
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300578
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é-Gonnard7a346b82019-10-02 14:47:01 +0200657 mbedtls_platform_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;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200690 mbedtls_pk_handle_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é-Gonnard020d9ba2019-09-19 10:45:14 +0200715 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#if defined(MBEDTLS_RSA_C)
722 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_RSA_C */
Hanno Becker8cf2f5e2019-08-21 11:51:53 +0100727#if defined(MBEDTLS_USE_TINYCRYPT)
728 if( pk_alg == MBEDTLS_PK_ECKEY )
729 {
730 ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx );
731 } else
732#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#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 */
Hanno Becker8cf2f5e2019-08-21 11:51:53 +0100741#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743
744 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
746 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747
748 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750
751 return( ret );
752}
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755/*
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400756 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
757 *
758 * The value zero is:
759 * - never a valid value for an RSA parameter
760 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
761 *
762 * Since values can't be omitted in PKCS#1, passing a zero value to
763 * rsa_complete() would be incorrect, so reject zero values early.
764 */
765static int asn1_get_nonzero_mpi( unsigned char **p,
766 const unsigned char *end,
767 mbedtls_mpi *X )
768{
769 int ret;
770
771 ret = mbedtls_asn1_get_mpi( p, end, X );
772 if( ret != 0 )
773 return( ret );
774
775 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
776 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
777
778 return( 0 );
779}
780
781/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782 * Parse a PKCS#1 encoded private RSA key
783 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785 const unsigned char *key,
786 size_t keylen )
787{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100788 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789 size_t len;
790 unsigned char *p, *end;
791
Hanno Beckerefa14e82017-10-11 19:45:19 +0100792 mbedtls_mpi T;
793 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100794
Paul Bakker1a7550a2013-09-15 13:01:22 +0200795 p = (unsigned char *) key;
796 end = p + keylen;
797
798 /*
799 * This function parses the RSAPrivateKey (PKCS#1)
800 *
801 * RSAPrivateKey ::= SEQUENCE {
802 * version Version,
803 * modulus INTEGER, -- n
804 * publicExponent INTEGER, -- e
805 * privateExponent INTEGER, -- d
806 * prime1 INTEGER, -- p
807 * prime2 INTEGER, -- q
808 * exponent1 INTEGER, -- d mod (p-1)
809 * exponent2 INTEGER, -- d mod (q-1)
810 * coefficient INTEGER, -- (inverse of q) mod p
811 * otherPrimeInfos OtherPrimeInfos OPTIONAL
812 * }
813 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
815 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818 }
819
820 end = p + len;
821
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100822 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200824 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200825 }
826
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100827 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830 }
831
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100832 /* Import N */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400833 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
834 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
835 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100836 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100838 /* Import E */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400839 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
840 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
841 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100842 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100843
844 /* Import D */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400845 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
846 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
847 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100848 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100849
850 /* Import P */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400851 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
852 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
853 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100854 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100855
856 /* Import Q */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400857 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
858 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
859 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100860 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100861
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400862#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd32b6e692020-01-29 13:09:55 -0500863 /*
864 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
865 * that they can be easily recomputed from D, P and Q. However by
866 * parsing them from the PKCS1 structure it is possible to avoid
867 * recalculating them which both reduces the overhead of loading
868 * RSA private keys into memory and also avoids side channels which
869 * can arise when computing those values, since all of D, P, and Q
870 * are secret. See https://eprint.iacr.org/2020/055 for a
871 * description of one such attack.
872 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100873
Jack Lloyd32b6e692020-01-29 13:09:55 -0500874 /* Import DP */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400875 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
876 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500877 goto cleanup;
878
879 /* Import DQ */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400880 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
881 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500882 goto cleanup;
883
884 /* Import QP */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400885 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
886 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500887 goto cleanup;
888
889#else
890 /* Verify existance of the CRT params */
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400891 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
892 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
893 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd32b6e692020-01-29 13:09:55 -0500894 goto cleanup;
895#endif
896
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400897 /* rsa_complete() doesn't complete anything with the default
898 * implementation but is still called:
899 * - for the benefit of alternative implementation that may want to
900 * pre-compute stuff beyond what's provided (eg Montgomery factors)
901 * - as is also sanity-checks the key
902 *
903 * Furthermore, we also check the public part for consistency with
904 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
905 */
906 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
907 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
908 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100909 goto cleanup;
Andrzej Kurek825ebd42020-05-18 11:47:25 -0400910 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911
912 if( p != end )
913 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100914 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
915 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916 }
917
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100918cleanup:
919
Hanno Beckerefa14e82017-10-11 19:45:19 +0100920 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100921
922 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100924 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100925 if( ( ret & 0xff80 ) == 0 )
926 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
927 else
928 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931 }
932
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100933 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200934}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Jarno Lamsab1760922019-04-18 15:58:34 +0300937#if defined(MBEDTLS_USE_TINYCRYPT)
938static int pk_parse_key_sec1_der( mbedtls_uecc_keypair *keypair,
939 const unsigned char *key,
940 size_t keylen)
941{
942 int ret;
943 int version, pubkey_done;
944 size_t len;
945 mbedtls_asn1_buf params;
946 unsigned char *p = (unsigned char *) key;
947 unsigned char *end = p + keylen;
948 unsigned char *end2;
949
950 /*
951 * RFC 5915, or SEC1 Appendix C.4
952 *
953 * ECPrivateKey ::= SEQUENCE {
954 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
955 * privateKey OCTET STRING,
956 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
957 * publicKey [1] BIT STRING OPTIONAL
958 * }
959 */
960 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
961 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
962 {
963 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
964 }
965
966 end = p + len;
967
968 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
969 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
970
971 if( version != 1 )
972 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
973
974 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
975 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
976
Teppo Järvelin91d79382019-10-02 09:09:31 +0300977 mbedtls_platform_memcpy( keypair->private_key, p, len );
Jarno Lamsab1760922019-04-18 15:58:34 +0300978
979 p += len;
980
981 pubkey_done = 0;
982 if( p != end )
983 {
984 /*
985 * Is 'parameters' present?
986 */
987 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
988 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
989 {
990 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
991 ( ret = pk_use_ecparams( &params ) ) != 0 )
992 {
993 return( ret );
994 }
995 }
996 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
997 {
998 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
999 }
1000 }
1001
1002 if( p != end )
1003 {
1004 /*
1005 * Is 'publickey' present? If not, or if we can't read it (eg because it
1006 * is compressed), create it from the private key.
1007 */
1008 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1009 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
1010 {
1011 end2 = p + len;
1012
1013 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1014 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1015
1016 if( p + len != end2 )
1017 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1018 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
1019
Hanno Becker7e38c372019-08-20 17:01:50 +01001020 if( ( ret = uecc_public_key_read_binary( keypair,
Jarno Lamsab1760922019-04-18 15:58:34 +03001021 (const unsigned char *) p, end2 - p ) ) == 0 )
Hanno Beckerad353f22019-08-20 13:04:30 +01001022 {
Jarno Lamsab1760922019-04-18 15:58:34 +03001023 pubkey_done = 1;
Hanno Beckerad353f22019-08-20 13:04:30 +01001024 }
Jarno Lamsab1760922019-04-18 15:58:34 +03001025 else
1026 {
1027 /*
Hanno Becker683d84a2019-09-04 16:10:46 +01001028 * The only acceptable failure mode of
1029 * uecc_public_key_read_binary() above
Jarno Lamsab1760922019-04-18 15:58:34 +03001030 * is if the point format is not recognized.
1031 */
1032 if( ret != MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE )
1033 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1034 }
1035 }
1036 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
1037 {
1038 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
1039 }
1040 }
1041
Hanno Beckerd45f3832019-08-20 14:21:40 +01001042 if( !pubkey_done )
1043 {
1044 ret = uECC_compute_public_key( keypair->private_key,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +01001045 keypair->public_key );
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +01001046 if( ret == UECC_FAULT_DETECTED )
1047 return( MBEDTLS_ERR_PLATFORM_FAULT_DETECTED );
1048 if( ret != UECC_SUCCESS )
Hanno Beckerd45f3832019-08-20 14:21:40 +01001049 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1050 }
Jarno Lamsab1760922019-04-18 15:58:34 +03001051
1052 return( 0 );
1053}
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001054#else /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +03001055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057/*
1058 * Parse a SEC1 encoded private EC key
1059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061 const unsigned char *key,
1062 size_t keylen )
1063{
1064 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001065 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001068 unsigned char *p = (unsigned char *) key;
1069 unsigned char *end = p + keylen;
1070 unsigned char *end2;
1071
1072 /*
1073 * RFC 5915, or SEC1 Appendix C.4
1074 *
1075 * ECPrivateKey ::= SEQUENCE {
1076 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1077 * privateKey OCTET STRING,
1078 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1079 * publicKey [1] BIT STRING OPTIONAL
1080 * }
1081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1083 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 }
1087
1088 end = p + len;
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1091 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001092
1093 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1097 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 mbedtls_ecp_keypair_free( eck );
1102 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001103 }
1104
1105 p += len;
1106
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001107 pubkey_done = 0;
1108 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001110 /*
1111 * Is 'parameters' present?
1112 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001113 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1114 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001115 {
1116 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
1117 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
1118 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001119 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001120 return( ret );
1121 }
1122 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001123 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001127 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001128 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001129
Jethro Beekmand2df9362018-02-16 13:11:04 -08001130 if( p != end )
1131 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001132 /*
1133 * Is 'publickey' present? If not, or if we can't read it (eg because it
1134 * is compressed), create it from the private key.
1135 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001136 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1137 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001138 {
1139 end2 = p + len;
1140
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001141 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1142 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001143
1144 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001145 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1146 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001147
1148 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
1149 pubkey_done = 1;
1150 else
1151 {
1152 /*
1153 * The only acceptable failure mode of pk_get_ecpubkey() above
1154 * is if the point format is not recognized.
1155 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001156 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1157 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001158 }
1159 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001160 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001161 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001162 mbedtls_ecp_keypair_free( eck );
1163 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001164 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001166
1167 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001169 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 mbedtls_ecp_keypair_free( eck );
1172 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001173 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 return( ret );
1179 }
1180
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001181 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#endif /* MBEDTLS_ECP_C */
Jarno Lamsab1760922019-04-18 15:58:34 +03001184#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
1186/*
1187 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001188 *
1189 * Notes:
1190 *
1191 * - This function does not own the key buffer. It is the
1192 * responsibility of the caller to take care of zeroizing
1193 * and freeing it after use.
1194 *
1195 * - The function is responsible for freeing the provided
1196 * PK context on failure.
1197 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 */
1199static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201 const unsigned char* key,
1202 size_t keylen )
1203{
1204 int ret, version;
1205 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001207 unsigned char *p = (unsigned char *) key;
1208 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001210 mbedtls_pk_handle_t pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211
1212 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001213 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214 *
1215 * PrivateKeyInfo ::= SEQUENCE {
1216 * version Version,
1217 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1218 * privateKey PrivateKey,
1219 * attributes [0] IMPLICIT Attributes OPTIONAL }
1220 *
1221 * Version ::= INTEGER
1222 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1223 * PrivateKey ::= OCTET STRING
1224 *
1225 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1226 */
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1229 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232 }
1233
1234 end = p + len;
1235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1237 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238
1239 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001241
1242 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1246 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247
1248 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1250 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001252 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001255 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256 return( ret );
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_RSA_C)
1259 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264 return( ret );
1265 }
1266 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_RSA_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001268#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker75f8d322019-08-20 14:31:50 +01001269 if( pk_alg == MBEDTLS_PK_ECKEY )
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001270 {
1271 if( ( ret = pk_use_ecparams( &params ) ) != 0 ||
Hanno Beckerda779712019-08-21 13:22:59 +01001272 ( ret = pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk ), p, len ) ) != 0)
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001273 {
1274 return( ret );
1275 }
Hanno Beckere65697c2019-08-20 14:31:00 +01001276 } else
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001277#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#if defined(MBEDTLS_ECP_C)
1279 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1282 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 return( ret );
1286 }
1287 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001289#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001292 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293}
1294
1295/*
1296 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001297 *
1298 * To save space, the decryption happens in-place on the given key buffer.
1299 * Also, while this function may modify the keybuffer, it doesn't own it,
1300 * and instead it is the responsibility of the caller to zeroize and properly
1301 * free it after use.
1302 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001304#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001307 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 const unsigned char *pwd, size_t pwdlen )
1309{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001310 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001312 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1315#if defined(MBEDTLS_PKCS12_C)
1316 mbedtls_cipher_type_t cipher_alg;
1317 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318#endif
1319
Hanno Becker2aa80a72017-09-07 15:28:45 +01001320 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 end = p + keylen;
1322
1323 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325
1326 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001327 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 *
1329 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1330 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1331 * encryptedData EncryptedData
1332 * }
1333 *
1334 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1335 *
1336 * EncryptedData ::= OCTET STRING
1337 *
1338 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001339 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1342 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345 }
1346
1347 end = p + len;
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1350 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1353 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001354
Hanno Beckerfab35692017-08-25 13:38:26 +01001355 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356
1357 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001358 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360#if defined(MBEDTLS_PKCS12_C)
1361 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364 cipher_alg, md_alg,
1365 pwd, pwdlen, p, len, buf ) ) != 0 )
1366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1368 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369
1370 return( ret );
1371 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001372
1373 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1378 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379 pwd, pwdlen,
1380 p, len, buf ) ) != 0 )
1381 {
1382 return( ret );
1383 }
1384
1385 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1389 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001390
1391 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392 }
1393 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#endif /* MBEDTLS_PKCS12_C */
1395#if defined(MBEDTLS_PKCS5_C)
1396 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 p, len, buf ) ) != 0 )
1400 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1402 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403
1404 return( ret );
1405 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001406
1407 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408 }
1409 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001411 {
1412 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001413 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001415 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001417
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1419}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421
1422/*
1423 * Parse a private key
1424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426 const unsigned char *key, size_t keylen,
1427 const unsigned char *pwd, size_t pwdlen )
1428{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001429#if defined(MBEDTLS_PKCS12_C) || \
1430 defined(MBEDTLS_PKCS5_C) || \
1431 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001433#endif
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001434 mbedtls_pk_handle_t pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001435#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001436 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001438#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001439
Gilles Peskinee97dc602018-12-19 00:51:38 +01001440 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001441 if( keylen == 0 )
1442 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1443 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001444
1445#if defined(MBEDTLS_PEM_PARSE_C)
1446 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001449 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001450 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001451 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1452 else
1453 ret = mbedtls_pem_read_buffer( &pem,
1454 "-----BEGIN RSA PRIVATE KEY-----",
1455 "-----END RSA PRIVATE KEY-----",
1456 key, pwd, pwdlen, &len );
1457
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 if( ret == 0 )
1459 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001460 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001461 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001463 pem.buf, pem.buflen ) ) != 0 )
1464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001466 }
1467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 return( ret );
1470 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1472 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1473 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1474 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1475 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001478
Hanno Beckerd336f722019-08-21 11:46:11 +01001479#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001480 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001481 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001482 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1483 else
1484 ret = mbedtls_pem_read_buffer( &pem,
1485 "-----BEGIN EC PRIVATE KEY-----",
1486 "-----END EC PRIVATE KEY-----",
1487 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001488 if( ret == 0 )
1489 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001490 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491
Hanno Beckerd336f722019-08-21 11:46:11 +01001492#if defined(MBEDTLS_USE_TINYCRYPT)
1493 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Hanno Beckerda779712019-08-21 13:22:59 +01001494 ( ret = pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk ),
Hanno Beckerd336f722019-08-21 11:46:11 +01001495 pem.buf, pem.buflen ) ) != 0 )
1496#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Beckerfab35692017-08-25 13:38:26 +01001497 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001499 pem.buf, pem.buflen ) ) != 0 )
Hanno Beckerd336f722019-08-21 11:46:11 +01001500#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503 }
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001506 return( ret );
1507 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1509 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1510 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1511 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1512 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001513 return( ret );
Hanno Beckerd336f722019-08-21 11:46:11 +01001514#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001515
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001516 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001517 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001518 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1519 else
1520 ret = mbedtls_pem_read_buffer( &pem,
1521 "-----BEGIN PRIVATE KEY-----",
1522 "-----END PRIVATE KEY-----",
1523 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001524 if( ret == 0 )
1525 {
1526 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1527 pem.buf, pem.buflen ) ) != 0 )
1528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 }
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533 return( ret );
1534 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001536 return( ret );
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001539 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001540 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001541 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1542 else
1543 ret = mbedtls_pem_read_buffer( &pem,
1544 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1545 "-----END ENCRYPTED PRIVATE KEY-----",
1546 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001547 if( ret == 0 )
1548 {
1549 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1550 pem.buf, pem.buflen,
1551 pwd, pwdlen ) ) != 0 )
1552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001554 }
1555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001557 return( ret );
1558 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001562#else
1563 ((void) pwd);
1564 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001566
1567 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001568 * At this point we only know it's not a PEM formatted key. Could be any
1569 * of the known DER encoded private key formats
1570 *
1571 * We try the different DER format parsers to see if one passes without
1572 * error
1573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001575 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001576 unsigned char *key_copy;
1577
1578 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1579 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1580
Teppo Järvelin91d79382019-10-02 09:09:31 +03001581 mbedtls_platform_memcpy( key_copy, key, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001582
1583 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1584 pwd, pwdlen );
1585
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001586 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001587 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001588 }
1589
Hanno Beckerfab35692017-08-25 13:38:26 +01001590 if( ret == 0 )
1591 return( 0 );
1592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001594 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001597 {
1598 return( ret );
1599 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001601
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001602 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001603 return( 0 );
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001606 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001609
Hanno Becker9be19262017-09-08 12:39:44 +01001610 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001611 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1612 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001613 {
1614 return( 0 );
1615 }
1616
Hanno Becker780f0a42018-10-10 11:23:33 +01001617 mbedtls_pk_free( pk );
1618 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001620
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001621#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +01001622 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001623 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
Hanno Beckerda779712019-08-21 13:22:59 +01001624 pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk),
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001625 key, keylen) == 0)
1626 {
1627 return( 0 );
1628 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001629#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001630#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001631 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001632 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1633 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1634 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001635 {
1636 return( 0 );
1637 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001638 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001640#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001641
Hanno Becker780f0a42018-10-10 11:23:33 +01001642 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1643 * it is ok to leave the PK context initialized but not
1644 * freed: It is the caller's responsibility to call pk_init()
1645 * before calling this function, and to call pk_free()
1646 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1647 * isn't, this leads to mbedtls_pk_free() being called
1648 * twice, once here and once by the caller, but this is
1649 * also ok and in line with the mbedtls_pk_free() calls
1650 * on failed PEM parsing attempts. */
1651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001653}
1654
1655/*
1656 * Parse a public key
1657 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001659 const unsigned char *key, size_t keylen )
1660{
1661 int ret;
1662 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001663#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001664 mbedtls_pk_handle_t pk_info;
Ron Eldor5472d432017-10-17 09:49:00 +03001665#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001667 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001669#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001670
Gilles Peskinee97dc602018-12-19 00:51:38 +01001671 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001672 if( keylen == 0 )
1673 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001674 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1675
1676#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001677 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001678#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001679 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001680 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001681 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1682 else
1683 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001684 "-----BEGIN RSA PUBLIC KEY-----",
1685 "-----END RSA PUBLIC KEY-----",
1686 key, NULL, 0, &len );
1687
1688 if( ret == 0 )
1689 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001690 p = pem.buf;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001691 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == MBEDTLS_PK_INVALID_HANDLE )
Ron Eldor84df1ae2017-10-16 17:11:52 +03001692 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001693
Ron Eldor84df1ae2017-10-16 17:11:52 +03001694 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1695 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001696
Ron Eldor84df1ae2017-10-16 17:11:52 +03001697 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1698 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001699
Ron Eldord0c56de2017-10-10 17:03:08 +03001700 mbedtls_pem_free( &pem );
1701 return( ret );
1702 }
1703 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1704 {
1705 mbedtls_pem_free( &pem );
1706 return( ret );
1707 }
1708#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001709
1710 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001711 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001712 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1713 else
1714 ret = mbedtls_pem_read_buffer( &pem,
1715 "-----BEGIN PUBLIC KEY-----",
1716 "-----END PUBLIC KEY-----",
1717 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001718
1719 if( ret == 0 )
1720 {
1721 /*
1722 * Was PEM encoded
1723 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001724 p = pem.buf;
1725
1726 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1727 mbedtls_pem_free( &pem );
1728 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001729 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001731 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001733 return( ret );
1734 }
Ron Eldor5472d432017-10-17 09:49:00 +03001735 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001737
1738#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001739 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == MBEDTLS_PK_INVALID_HANDLE )
Ron Eldor40b14a82017-10-16 19:30:00 +03001740 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1741
1742 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1743 return( ret );
1744
Ron Eldor9566ff72018-02-07 18:59:41 +02001745 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001746 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001747 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001748 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001749 return( ret );
1750 }
1751 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001752 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001753 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001754 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001755 }
1756#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001757 p = (unsigned char *) key;
1758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001760
Paul Bakker1a7550a2013-09-15 13:01:22 +02001761 return( ret );
1762}
1763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001764#endif /* MBEDTLS_PK_PARSE_C */