blob: e8a3e86ab5225b0a904e1100eb5ee20baf04d09b [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker1a7550a2013-09-15 13:01:22 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk.h"
31#include "mbedtls/asn1.h"
32#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +030055#if defined(MBEDTLS_USE_TINYCRYPT)
56#include "mbedtls/ecc.h"
57#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020061#else
62#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020063#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020065#endif
66
Gilles Peskinee97dc602018-12-19 00:51:38 +010067/* Parameter validation macros based on platform_util.h */
68#define PK_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
70#define PK_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
72
Gilles Peskine832f3492017-11-30 11:42:12 +010073#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020074/*
75 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020076 *
77 * The file is expected to contain either PEM or DER encoded data.
78 * A terminating null byte is always appended. It is included in the announced
79 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020082{
83 FILE *f;
84 long size;
85
Gilles Peskinee97dc602018-12-19 00:51:38 +010086 PK_VALIDATE_RET( path != NULL );
87 PK_VALIDATE_RET( buf != NULL );
88 PK_VALIDATE_RET( n != NULL );
89
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020092
93 fseek( f, 0, SEEK_END );
94 if( ( size = ftell( f ) ) == -1 )
95 {
96 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 }
99 fseek( f, 0, SEEK_SET );
100
101 *n = (size_t) size;
102
103 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200104 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 {
106 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200107 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108 }
109
110 if( fread( *buf, 1, *n, f ) != *n )
111 {
112 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100113
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500114 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118 }
119
120 fclose( f );
121
122 (*buf)[*n] = '\0';
123
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200124 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
125 ++*n;
126
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127 return( 0 );
128}
129
130/*
131 * Load and parse a private key
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134 const char *path, const char *pwd )
135{
136 int ret;
137 size_t n;
138 unsigned char *buf;
139
Gilles Peskinee97dc602018-12-19 00:51:38 +0100140 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100141 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144 return( ret );
145
146 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150 (const unsigned char *) pwd, strlen( pwd ) );
151
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500152 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154
155 return( ret );
156}
157
158/*
159 * Load and parse a public key
160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162{
163 int ret;
164 size_t n;
165 unsigned char *buf;
166
Gilles Peskinee97dc602018-12-19 00:51:38 +0100167 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100168 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171 return( ret );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500175 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177
178 return( ret );
179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182#if defined(MBEDTLS_ECP_C)
183/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184 *
185 * ECParameters ::= CHOICE {
186 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100187 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200188 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200189 * }
190 */
191static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200193{
194 int ret;
195
Sanne Woudab2b29d52017-08-21 15:58:12 +0100196 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100197 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
198 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100199
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100200 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200201 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( params->tag != MBEDTLS_ASN1_OID
203#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
204 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100205#endif
206 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
209 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100210 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100215 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200216
217 params->p = *p;
218 *p += params->len;
219
220 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
222 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200223
224 return( 0 );
225}
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200228/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100229 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
230 * WARNING: the resulting group should only be used with
231 * pk_group_id_from_specified(), since its base point may not be set correctly
232 * if it was encoded compressed.
233 *
234 * SpecifiedECDomain ::= SEQUENCE {
235 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
236 * fieldID FieldID {{FieldTypes}},
237 * curve Curve,
238 * base ECPoint,
239 * order INTEGER,
240 * cofactor INTEGER OPTIONAL,
241 * hash HashAlgorithm OPTIONAL,
242 * ...
243 * }
244 *
245 * We only support prime-field as field type, and ignore hash and cofactor.
246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100248{
249 int ret;
250 unsigned char *p = params->p;
251 const unsigned char * const end = params->p + params->len;
252 const unsigned char *end_field, *end_curve;
253 size_t len;
254 int ver;
255
256 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
258 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100262
263 /*
264 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
265 * fieldType FIELD-ID.&id({IOSet}),
266 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
267 * }
268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
270 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100271 return( ret );
272
273 end_field = p + len;
274
275 /*
276 * FIELD-ID ::= TYPE-IDENTIFIER
277 * FieldTypes FIELD-ID ::= {
278 * { Prime-p IDENTIFIED BY prime-field } |
279 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
280 * }
281 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284 return( ret );
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
287 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100290 }
291
292 p += len;
293
294 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
296 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100297
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200298 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100299
300 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
302 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303
304 /*
305 * Curve ::= SEQUENCE {
306 * a FieldElement,
307 * b FieldElement,
308 * seed BIT STRING OPTIONAL
309 * -- Shall be present if used in SpecifiedECDomain
310 * -- with version equal to ecdpVer2 or ecdpVer3
311 * }
312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
314 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 return( ret );
316
317 end_curve = p + len;
318
319 /*
320 * FieldElement ::= OCTET STRING
321 * containing an integer in the case of a prime field
322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
324 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327 }
328
329 p += len;
330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
332 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100335 }
336
337 p += len;
338
339 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100341 p += len;
342
343 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
345 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100346
347 /*
348 * ECPoint ::= OCTET STRING
349 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
351 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100354 ( const unsigned char *) p, len ) ) != 0 )
355 {
356 /*
357 * If we can't read the point because it's compressed, cheat by
358 * reading only the X coordinate and the parity bit of Y.
359 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100361 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 len != mbedtls_mpi_size( &grp->P ) + 1 ||
363 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
364 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
365 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200367 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100368 }
369 }
370
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371 p += len;
372
373 /*
374 * order INTEGER
375 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
377 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100378
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200379 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100380
381 /*
382 * Allow optional elements by purposefully not enforcing p == end here.
383 */
384
385 return( 0 );
386}
387
388/*
389 * Find the group id associated with an (almost filled) group as generated by
390 * pk_group_from_specified(), or return an error if unknown.
391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392static 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 +0100393{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100394 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_ecp_group ref;
396 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100401 {
402 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200404 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
406 /* Compare to the group we were given, starting with easy tests */
407 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
409 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
410 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
411 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
412 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
413 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 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 +0100416 {
417 break;
418 }
419
420 }
421
422cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
425 *grp_id = *id;
426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
428 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
430 return( ret );
431}
432
433/*
434 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
437 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438{
439 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100443
444 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
445 goto cleanup;
446
447 ret = pk_group_id_from_group( &grp, grp_id );
448
449cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451
452 return( ret );
453}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455
456/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200457 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100458 *
459 * ECParameters ::= CHOICE {
460 * namedCurve OBJECT IDENTIFIER
461 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
462 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200463 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200465{
466 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100470 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
472 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100473 }
474 else
475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100477 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
478 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100479#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100481#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100482 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200483
484 /*
485 * grp may already be initilialized; if so, make sure IDs match
486 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
488 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200489
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200490 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200491 return( ret );
492
493 return( 0 );
494}
495
496/*
497 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100498 *
499 * The caller is responsible for clearing the structure upon failure if
500 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502 */
503static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200505{
506 int ret;
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100509 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200512 }
513
514 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516 */
517 *p = (unsigned char *) end;
518
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100519 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200522
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300523#if defined(MBEDTLS_USE_TINYCRYPT)
524/*
525 * Import a point from unsigned binary data (SEC1 2.3.4)
526 */
527static int uecc_public_key_read_binary( uint8_t **pt,
528 const unsigned char *buf, size_t ilen )
529{
530
531 if( ilen < 1 )
532 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
533
534 //We are not handling the infinity point right now
535
536 if( buf[0] != 0x04 )
537 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
538
539 if( ilen != 2 * NUM_ECC_BYTES + 1 )
540 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
541
542 *pt = (uint8_t *) buf + 1;
543
544 return( 0 );
545}
546
547static int pk_get_ueccpubkey( unsigned char **p,
548 const unsigned char *end,
549 uint8_t *pk_context)
550{
551 int ret;
552
553 ret = uecc_public_key_read_binary( &pk_context,
554 (const unsigned char *) *p, end - *p );
555
556 /*
557 * We know uecc_public_key_read_binary consumed all bytes or failed
558 */
559 *p = (unsigned char *) end;
560
561 return( ret );
562}
563#endif /* MBEDTLS_USE_TINYCRYPT */
564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200566/*
567 * RSAPublicKey ::= SEQUENCE {
568 * modulus INTEGER, -- n
569 * publicExponent INTEGER -- e
570 * }
571 */
572static int pk_get_rsapubkey( unsigned char **p,
573 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200575{
576 int ret;
577 size_t len;
578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
580 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
581 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200582
583 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
585 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200586
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100587 /* Import N */
588 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200590
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100591 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
592 NULL, 0, NULL, 0 ) ) != 0 )
593 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
594
595 *p += len;
596
597 /* Import E */
598 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
599 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
600
601 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
602 NULL, 0, *p, len ) ) != 0 )
603 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
604
605 *p += len;
606
Hanno Becker895c5ab2018-01-05 08:08:09 +0000607 if( mbedtls_rsa_complete( rsa ) != 0 ||
608 mbedtls_rsa_check_pubkey( rsa ) != 0 )
609 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100610 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000611 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100612
Paul Bakker1a7550a2013-09-15 13:01:22 +0200613 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
615 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200616
Paul Bakker1a7550a2013-09-15 13:01:22 +0200617 return( 0 );
618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620
621/* Get a PK algorithm identifier
622 *
623 * AlgorithmIdentifier ::= SEQUENCE {
624 * algorithm OBJECT IDENTIFIER,
625 * parameters ANY DEFINED BY algorithm OPTIONAL }
626 */
627static int pk_get_pk_alg( unsigned char **p,
628 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630{
631 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
637 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
640 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200641
642 /*
643 * No parameters with RSA (only for EC)
644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 if( *pk_alg == MBEDTLS_PK_RSA &&
646 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647 params->len != 0 ) )
648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650 }
651
652 return( 0 );
653}
654
655/*
656 * SubjectPublicKeyInfo ::= SEQUENCE {
657 * algorithm AlgorithmIdentifier,
658 * subjectPublicKey BIT STRING }
659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
661 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662{
663 int ret;
664 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 mbedtls_asn1_buf alg_params;
666 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
667 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668
Gilles Peskinee97dc602018-12-19 00:51:38 +0100669 PK_VALIDATE_RET( p != NULL );
670 PK_VALIDATE_RET( *p != NULL );
671 PK_VALIDATE_RET( end != NULL );
672 PK_VALIDATE_RET( pk != NULL );
673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
675 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200678 }
679
680 end = *p + len;
681
682 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
683 return( ret );
684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
686 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200687
688 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
690 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
693 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200694
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200695 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200696 return( ret );
697
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300698#if defined(MBEDTLS_USE_TINYCRYPT)
699 if( pk_alg == MBEDTLS_PK_ECDSA )
700 {
701 ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx );
702 }
703#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_RSA_C)
705 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200707 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200708 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709#endif /* MBEDTLS_RSA_C */
710#if defined(MBEDTLS_ECP_C)
711 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200712 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200716 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200717#endif /* MBEDTLS_ECP_C */
718 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719
720 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
722 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723
724 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200726
727 return( ret );
728}
729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731/*
732 * Parse a PKCS#1 encoded private RSA key
733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735 const unsigned char *key,
736 size_t keylen )
737{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100738 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739 size_t len;
740 unsigned char *p, *end;
741
Hanno Beckerefa14e82017-10-11 19:45:19 +0100742 mbedtls_mpi T;
743 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100744
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745 p = (unsigned char *) key;
746 end = p + keylen;
747
748 /*
749 * This function parses the RSAPrivateKey (PKCS#1)
750 *
751 * RSAPrivateKey ::= SEQUENCE {
752 * version Version,
753 * modulus INTEGER, -- n
754 * publicExponent INTEGER, -- e
755 * privateExponent INTEGER, -- d
756 * prime1 INTEGER, -- p
757 * prime2 INTEGER, -- q
758 * exponent1 INTEGER, -- d mod (p-1)
759 * exponent2 INTEGER, -- d mod (q-1)
760 * coefficient INTEGER, -- (inverse of q) mod p
761 * otherPrimeInfos OtherPrimeInfos OPTIONAL
762 * }
763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768 }
769
770 end = p + len;
771
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775 }
776
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100777 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780 }
781
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100782 /* Import N */
783 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
784 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
785 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
786 NULL, 0, NULL, 0 ) ) != 0 )
787 goto cleanup;
788 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100790 /* Import E */
791 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
792 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
793 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
794 NULL, 0, p, len ) ) != 0 )
795 goto cleanup;
796 p += len;
797
798 /* Import D */
799 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
800 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
801 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
802 p, len, NULL, 0 ) ) != 0 )
803 goto cleanup;
804 p += len;
805
806 /* Import P */
807 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
808 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
809 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
810 NULL, 0, NULL, 0 ) ) != 0 )
811 goto cleanup;
812 p += len;
813
814 /* Import Q */
815 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
816 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
817 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
818 NULL, 0, NULL, 0 ) ) != 0 )
819 goto cleanup;
820 p += len;
821
822 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100823 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100824 goto cleanup;
825
826 /* Check optional parameters */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100827 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
828 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
829 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100830 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831
832 if( p != end )
833 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
835 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836 }
837
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100838cleanup:
839
Hanno Beckerefa14e82017-10-11 19:45:19 +0100840 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100841
842 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200843 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100844 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845 if( ( ret & 0xff80 ) == 0 )
846 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
847 else
848 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200850 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200851 }
852
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100853 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858/*
859 * Parse a SEC1 encoded private EC key
860 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862 const unsigned char *key,
863 size_t keylen )
864{
865 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100866 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869 unsigned char *p = (unsigned char *) key;
870 unsigned char *end = p + keylen;
871 unsigned char *end2;
872
873 /*
874 * RFC 5915, or SEC1 Appendix C.4
875 *
876 * ECPrivateKey ::= SEQUENCE {
877 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
878 * privateKey OCTET STRING,
879 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
880 * publicKey [1] BIT STRING OPTIONAL
881 * }
882 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200883 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
884 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 }
888
889 end = p + len;
890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
892 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893
894 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200895 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200896
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
898 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 mbedtls_ecp_keypair_free( eck );
903 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904 }
905
906 p += len;
907
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200908 pubkey_done = 0;
909 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200910 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200911 /*
912 * Is 'parameters' present?
913 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200914 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
915 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200916 {
917 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
918 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
919 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200920 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200921 return( ret );
922 }
923 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200924 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200926 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100928 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800929 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200930
Jethro Beekmand2df9362018-02-16 13:11:04 -0800931 if( p != end )
932 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200933 /*
934 * Is 'publickey' present? If not, or if we can't read it (eg because it
935 * is compressed), create it from the private key.
936 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200937 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
938 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200939 {
940 end2 = p + len;
941
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200942 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
943 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200944
945 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200946 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
947 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200948
949 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
950 pubkey_done = 1;
951 else
952 {
953 /*
954 * The only acceptable failure mode of pk_get_ecpubkey() above
955 * is if the point format is not recognized.
956 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200957 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
958 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200959 }
960 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200961 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200962 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200963 mbedtls_ecp_keypair_free( eck );
964 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200965 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100967
968 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100970 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_ecp_keypair_free( eck );
973 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200974 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979 return( ret );
980 }
981
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200982 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200983}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985
986/*
987 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100988 *
989 * Notes:
990 *
991 * - This function does not own the key buffer. It is the
992 * responsibility of the caller to take care of zeroizing
993 * and freeing it after use.
994 *
995 * - The function is responsible for freeing the provided
996 * PK context on failure.
997 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 */
999static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001 const unsigned char* key,
1002 size_t keylen )
1003{
1004 int ret, version;
1005 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 unsigned char *p = (unsigned char *) key;
1008 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001009 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1010 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011
1012 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001013 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 *
1015 * PrivateKeyInfo ::= SEQUENCE {
1016 * version Version,
1017 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1018 * privateKey PrivateKey,
1019 * attributes [0] IMPLICIT Attributes OPTIONAL }
1020 *
1021 * Version ::= INTEGER
1022 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1023 * PrivateKey ::= OCTET STRING
1024 *
1025 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1026 */
1027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1029 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032 }
1033
1034 end = p + len;
1035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1037 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
1039 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
1042 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1046 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047
1048 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1050 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1053 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001055 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056 return( ret );
1057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#if defined(MBEDTLS_RSA_C)
1059 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064 return( ret );
1065 }
1066 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067#endif /* MBEDTLS_RSA_C */
1068#if defined(MBEDTLS_ECP_C)
1069 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001071 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1072 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001073 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 return( ret );
1076 }
1077 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078#endif /* MBEDTLS_ECP_C */
1079 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001081 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082}
1083
1084/*
1085 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001086 *
1087 * To save space, the decryption happens in-place on the given key buffer.
1088 * Also, while this function may modify the keybuffer, it doesn't own it,
1089 * and instead it is the responsibility of the caller to zeroize and properly
1090 * free it after use.
1091 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001092 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001096 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097 const unsigned char *pwd, size_t pwdlen )
1098{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001099 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001101 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1104#if defined(MBEDTLS_PKCS12_C)
1105 mbedtls_cipher_type_t cipher_alg;
1106 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107#endif
1108
Hanno Becker2aa80a72017-09-07 15:28:45 +01001109 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110 end = p + keylen;
1111
1112 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114
1115 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001116 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117 *
1118 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1119 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1120 * encryptedData EncryptedData
1121 * }
1122 *
1123 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1124 *
1125 * EncryptedData ::= OCTET STRING
1126 *
1127 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001128 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1131 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134 }
1135
1136 end = p + len;
1137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1139 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1142 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143
Hanno Beckerfab35692017-08-25 13:38:26 +01001144 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145
1146 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001147 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149#if defined(MBEDTLS_PKCS12_C)
1150 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 cipher_alg, md_alg,
1154 pwd, pwdlen, p, len, buf ) ) != 0 )
1155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1157 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158
1159 return( ret );
1160 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001161
1162 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001164 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1167 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 pwd, pwdlen,
1169 p, len, buf ) ) != 0 )
1170 {
1171 return( ret );
1172 }
1173
1174 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1178 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001179
1180 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 }
1182 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183#endif /* MBEDTLS_PKCS12_C */
1184#if defined(MBEDTLS_PKCS5_C)
1185 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001187 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188 p, len, buf ) ) != 0 )
1189 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1191 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192
1193 return( ret );
1194 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001195
1196 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 }
1198 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001200 {
1201 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001202 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001204 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001206
Paul Bakker1a7550a2013-09-15 13:01:22 +02001207 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1208}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210
1211/*
1212 * Parse a private key
1213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 const unsigned char *key, size_t keylen,
1216 const unsigned char *pwd, size_t pwdlen )
1217{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001218#if defined(MBEDTLS_PKCS12_C) || \
1219 defined(MBEDTLS_PKCS5_C) || \
1220 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001225 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001226 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001227#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
Gilles Peskinee97dc602018-12-19 00:51:38 +01001229 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001230 if( keylen == 0 )
1231 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1232 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001233
1234#if defined(MBEDTLS_PEM_PARSE_C)
1235 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001238 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001239 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001240 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1241 else
1242 ret = mbedtls_pem_read_buffer( &pem,
1243 "-----BEGIN RSA PRIVATE KEY-----",
1244 "-----END RSA PRIVATE KEY-----",
1245 key, pwd, pwdlen, &len );
1246
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 if( ret == 0 )
1248 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001249 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001250 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001252 pem.buf, pem.buflen ) ) != 0 )
1253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255 }
1256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 return( ret );
1259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1261 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1262 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1263 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1264 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001269 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001270 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001271 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1272 else
1273 ret = mbedtls_pem_read_buffer( &pem,
1274 "-----BEGIN EC PRIVATE KEY-----",
1275 "-----END EC PRIVATE KEY-----",
1276 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 if( ret == 0 )
1278 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001279 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280
Hanno Beckerfab35692017-08-25 13:38:26 +01001281 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 pem.buf, pem.buflen ) ) != 0 )
1284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 }
1287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001289 return( ret );
1290 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1292 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1293 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1294 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1295 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001299 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001300 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001301 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1302 else
1303 ret = mbedtls_pem_read_buffer( &pem,
1304 "-----BEGIN PRIVATE KEY-----",
1305 "-----END PRIVATE KEY-----",
1306 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307 if( ret == 0 )
1308 {
1309 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1310 pem.buf, pem.buflen ) ) != 0 )
1311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 }
1314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316 return( ret );
1317 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 return( ret );
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001322 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001323 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001324 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1325 else
1326 ret = mbedtls_pem_read_buffer( &pem,
1327 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1328 "-----END ENCRYPTED PRIVATE KEY-----",
1329 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 if( ret == 0 )
1331 {
1332 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1333 pem.buf, pem.buflen,
1334 pwd, pwdlen ) ) != 0 )
1335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001336 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001337 }
1338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340 return( ret );
1341 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345#else
1346 ((void) pwd);
1347 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
1350 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001351 * At this point we only know it's not a PEM formatted key. Could be any
1352 * of the known DER encoded private key formats
1353 *
1354 * We try the different DER format parsers to see if one passes without
1355 * error
1356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001359 unsigned char *key_copy;
1360
1361 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1362 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1363
1364 memcpy( key_copy, key, keylen );
1365
1366 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1367 pwd, pwdlen );
1368
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001369 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001370 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371 }
1372
Hanno Beckerfab35692017-08-25 13:38:26 +01001373 if( ret == 0 )
1374 return( 0 );
1375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001377 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 {
1381 return( ret );
1382 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001385 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386 return( 0 );
1387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001389 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392
Hanno Becker9be19262017-09-08 12:39:44 +01001393 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001394 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1395 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001396 {
1397 return( 0 );
1398 }
1399
Hanno Becker780f0a42018-10-10 11:23:33 +01001400 mbedtls_pk_free( pk );
1401 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001405 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001406 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1407 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1408 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 {
1410 return( 0 );
1411 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001412 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Hanno Becker780f0a42018-10-10 11:23:33 +01001415 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1416 * it is ok to leave the PK context initialized but not
1417 * freed: It is the caller's responsibility to call pk_init()
1418 * before calling this function, and to call pk_free()
1419 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1420 * isn't, this leads to mbedtls_pk_free() being called
1421 * twice, once here and once by the caller, but this is
1422 * also ok and in line with the mbedtls_pk_free() calls
1423 * on failed PEM parsing attempts. */
1424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001426}
1427
1428/*
1429 * Parse a public key
1430 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001432 const unsigned char *key, size_t keylen )
1433{
1434 int ret;
1435 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001436#if defined(MBEDTLS_RSA_C)
1437 const mbedtls_pk_info_t *pk_info;
1438#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001442#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443
Gilles Peskinee97dc602018-12-19 00:51:38 +01001444 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001445 if( keylen == 0 )
1446 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001447 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1448
1449#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001451#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001453 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001454 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1455 else
1456 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001457 "-----BEGIN RSA PUBLIC KEY-----",
1458 "-----END RSA PUBLIC KEY-----",
1459 key, NULL, 0, &len );
1460
1461 if( ret == 0 )
1462 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001463 p = pem.buf;
1464 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1465 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001466
Ron Eldor84df1ae2017-10-16 17:11:52 +03001467 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1468 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001469
Ron Eldor84df1ae2017-10-16 17:11:52 +03001470 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1471 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001472
Ron Eldord0c56de2017-10-10 17:03:08 +03001473 mbedtls_pem_free( &pem );
1474 return( ret );
1475 }
1476 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1477 {
1478 mbedtls_pem_free( &pem );
1479 return( ret );
1480 }
1481#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001482
1483 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001484 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001485 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1486 else
1487 ret = mbedtls_pem_read_buffer( &pem,
1488 "-----BEGIN PUBLIC KEY-----",
1489 "-----END PUBLIC KEY-----",
1490 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001491
1492 if( ret == 0 )
1493 {
1494 /*
1495 * Was PEM encoded
1496 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001497 p = pem.buf;
1498
1499 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1500 mbedtls_pem_free( &pem );
1501 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504 {
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 }
Ron Eldor5472d432017-10-17 09:49:00 +03001508 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001509#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001510
1511#if defined(MBEDTLS_RSA_C)
1512 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1513 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1514
1515 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1516 return( ret );
1517
Ron Eldor9566ff72018-02-07 18:59:41 +02001518 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001519 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001520 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001521 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001522 return( ret );
1523 }
1524 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001525 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001526 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001527 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001528 }
1529#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 p = (unsigned char *) key;
1531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001532 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001533
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534 return( ret );
1535}
1536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_PK_PARSE_C */