blob: 0ae24027fe60efc9efba5cfb14076e5d0df04563 [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
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020058#else
59#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020062#endif
63
Gilles Peskinee97dc602018-12-19 00:51:38 +010064/* Parameter validation macros based on platform_util.h */
65#define PK_VALIDATE_RET( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
67#define PK_VALIDATE( cond ) \
68 MBEDTLS_INTERNAL_VALIDATE( cond )
69
Gilles Peskine832f3492017-11-30 11:42:12 +010070#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020071/*
72 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020073 *
74 * The file is expected to contain either PEM or DER encoded data.
75 * A terminating null byte is always appended. It is included in the announced
76 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020079{
80 FILE *f;
81 long size;
82
Gilles Peskinee97dc602018-12-19 00:51:38 +010083 PK_VALIDATE_RET( path != NULL );
84 PK_VALIDATE_RET( buf != NULL );
85 PK_VALIDATE_RET( n != NULL );
86
Paul Bakker1a7550a2013-09-15 13:01:22 +020087 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020089
90 fseek( f, 0, SEEK_END );
91 if( ( size = ftell( f ) ) == -1 )
92 {
93 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020095 }
96 fseek( f, 0, SEEK_SET );
97
98 *n = (size_t) size;
99
100 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200101 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102 {
103 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200104 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
107 if( fread( *buf, 1, *n, f ) != *n )
108 {
109 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100110
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500111 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200115 }
116
117 fclose( f );
118
119 (*buf)[*n] = '\0';
120
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200121 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
122 ++*n;
123
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124 return( 0 );
125}
126
127/*
128 * Load and parse a private key
129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131 const char *path, const char *pwd )
132{
133 int ret;
134 size_t n;
135 unsigned char *buf;
136
Gilles Peskinee97dc602018-12-19 00:51:38 +0100137 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100138 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 return( ret );
142
143 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147 (const unsigned char *) pwd, strlen( pwd ) );
148
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500149 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
152 return( ret );
153}
154
155/*
156 * Load and parse a public key
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159{
160 int ret;
161 size_t n;
162 unsigned char *buf;
163
Gilles Peskinee97dc602018-12-19 00:51:38 +0100164 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100165 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 return( ret );
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500172 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174
175 return( ret );
176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_ECP_C)
180/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181 *
182 * ECParameters ::= CHOICE {
183 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100184 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 * }
187 */
188static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190{
191 int ret;
192
Sanne Woudab2b29d52017-08-21 15:58:12 +0100193 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100194 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
195 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100196
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100197 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200198 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( params->tag != MBEDTLS_ASN1_OID
200#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
201 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100202#endif
203 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
206 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100207 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100212 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200213
214 params->p = *p;
215 *p += params->len;
216
217 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
219 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200220
221 return( 0 );
222}
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200225/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100226 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
227 * WARNING: the resulting group should only be used with
228 * pk_group_id_from_specified(), since its base point may not be set correctly
229 * if it was encoded compressed.
230 *
231 * SpecifiedECDomain ::= SEQUENCE {
232 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
233 * fieldID FieldID {{FieldTypes}},
234 * curve Curve,
235 * base ECPoint,
236 * order INTEGER,
237 * cofactor INTEGER OPTIONAL,
238 * hash HashAlgorithm OPTIONAL,
239 * ...
240 * }
241 *
242 * We only support prime-field as field type, and ignore hash and cofactor.
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245{
246 int ret;
247 unsigned char *p = params->p;
248 const unsigned char * const end = params->p + params->len;
249 const unsigned char *end_field, *end_curve;
250 size_t len;
251 int ver;
252
253 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
255 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100256
257 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 /*
261 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
262 * fieldType FIELD-ID.&id({IOSet}),
263 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
264 * }
265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
267 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100268 return( ret );
269
270 end_field = p + len;
271
272 /*
273 * FIELD-ID ::= TYPE-IDENTIFIER
274 * FieldTypes FIELD-ID ::= {
275 * { Prime-p IDENTIFIED BY prime-field } |
276 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
277 * }
278 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100281 return( ret );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
284 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287 }
288
289 p += len;
290
291 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
293 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100294
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200295 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296
297 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
299 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100300
301 /*
302 * Curve ::= SEQUENCE {
303 * a FieldElement,
304 * b FieldElement,
305 * seed BIT STRING OPTIONAL
306 * -- Shall be present if used in SpecifiedECDomain
307 * -- with version equal to ecdpVer2 or ecdpVer3
308 * }
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
311 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312 return( ret );
313
314 end_curve = p + len;
315
316 /*
317 * FieldElement ::= OCTET STRING
318 * containing an integer in the case of a prime field
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
321 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324 }
325
326 p += len;
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
329 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100332 }
333
334 p += len;
335
336 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100338 p += len;
339
340 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100343
344 /*
345 * ECPoint ::= OCTET STRING
346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
348 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100351 ( const unsigned char *) p, len ) ) != 0 )
352 {
353 /*
354 * If we can't read the point because it's compressed, cheat by
355 * reading only the X coordinate and the parity bit of Y.
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100358 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 len != mbedtls_mpi_size( &grp->P ) + 1 ||
360 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
361 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
362 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100365 }
366 }
367
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368 p += len;
369
370 /*
371 * order INTEGER
372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
374 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100375
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200376 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377
378 /*
379 * Allow optional elements by purposefully not enforcing p == end here.
380 */
381
382 return( 0 );
383}
384
385/*
386 * Find the group id associated with an (almost filled) group as generated by
387 * pk_group_from_specified(), or return an error if unknown.
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389static 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 +0100390{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100391 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_ecp_group ref;
393 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398 {
399 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200401 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402
403 /* Compare to the group we were given, starting with easy tests */
404 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
406 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
407 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
408 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
409 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
410 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 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 +0100413 {
414 break;
415 }
416
417 }
418
419cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
422 *grp_id = *id;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
425 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426
427 return( ret );
428}
429
430/*
431 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
434 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435{
436 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440
441 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
442 goto cleanup;
443
444 ret = pk_group_id_from_group( &grp, grp_id );
445
446cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449 return( ret );
450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452
453/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200454 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455 *
456 * ECParameters ::= CHOICE {
457 * namedCurve OBJECT IDENTIFIER
458 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
459 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200462{
463 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
469 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100470 }
471 else
472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100474 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
475 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100476#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100478#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100479 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200480
481 /*
482 * grp may already be initilialized; if so, make sure IDs match
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
485 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200487 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 return( ret );
489
490 return( 0 );
491}
492
493/*
494 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100495 *
496 * The caller is responsible for clearing the structure upon failure if
497 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499 */
500static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502{
503 int ret;
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100506 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509 }
510
511 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513 */
514 *p = (unsigned char *) end;
515
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100516 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200521/*
522 * RSAPublicKey ::= SEQUENCE {
523 * modulus INTEGER, -- n
524 * publicExponent INTEGER -- e
525 * }
526 */
527static int pk_get_rsapubkey( unsigned char **p,
528 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530{
531 int ret;
532 size_t len;
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
535 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
536 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537
538 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
540 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100542 /* Import N */
543 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200545
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100546 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
547 NULL, 0, NULL, 0 ) ) != 0 )
548 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
549
550 *p += len;
551
552 /* Import E */
553 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
554 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
555
556 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
557 NULL, 0, *p, len ) ) != 0 )
558 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
559
560 *p += len;
561
Hanno Becker895c5ab2018-01-05 08:08:09 +0000562 if( mbedtls_rsa_complete( rsa ) != 0 ||
563 mbedtls_rsa_check_pubkey( rsa ) != 0 )
564 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100565 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000566 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100567
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
570 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200571
Paul Bakker1a7550a2013-09-15 13:01:22 +0200572 return( 0 );
573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200575
576/* Get a PK algorithm identifier
577 *
578 * AlgorithmIdentifier ::= SEQUENCE {
579 * algorithm OBJECT IDENTIFIER,
580 * parameters ANY DEFINED BY algorithm OPTIONAL }
581 */
582static int pk_get_pk_alg( unsigned char **p,
583 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585{
586 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
592 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
595 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200596
597 /*
598 * No parameters with RSA (only for EC)
599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 if( *pk_alg == MBEDTLS_PK_RSA &&
601 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200602 params->len != 0 ) )
603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200605 }
606
607 return( 0 );
608}
609
610/*
611 * SubjectPublicKeyInfo ::= SEQUENCE {
612 * algorithm AlgorithmIdentifier,
613 * subjectPublicKey BIT STRING }
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
616 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200617{
618 int ret;
619 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_asn1_buf alg_params;
621 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
622 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623
Gilles Peskinee97dc602018-12-19 00:51:38 +0100624 PK_VALIDATE_RET( p != NULL );
625 PK_VALIDATE_RET( *p != NULL );
626 PK_VALIDATE_RET( end != NULL );
627 PK_VALIDATE_RET( pk != NULL );
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
630 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633 }
634
635 end = *p + len;
636
637 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
638 return( ret );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
641 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642
643 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
645 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
648 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200649
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200650 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200651 return( ret );
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_RSA_C)
654 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200657 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_RSA_C */
659#if defined(MBEDTLS_ECP_C)
660 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200663 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200665 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#endif /* MBEDTLS_ECP_C */
667 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668
669 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
671 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200672
673 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200675
676 return( ret );
677}
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200680/*
681 * Parse a PKCS#1 encoded private RSA key
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200684 const unsigned char *key,
685 size_t keylen )
686{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100687 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688 size_t len;
689 unsigned char *p, *end;
690
Hanno Beckerefa14e82017-10-11 19:45:19 +0100691 mbedtls_mpi T;
692 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100693
Paul Bakker1a7550a2013-09-15 13:01:22 +0200694 p = (unsigned char *) key;
695 end = p + keylen;
696
697 /*
698 * This function parses the RSAPrivateKey (PKCS#1)
699 *
700 * RSAPrivateKey ::= SEQUENCE {
701 * version Version,
702 * modulus INTEGER, -- n
703 * publicExponent INTEGER, -- e
704 * privateExponent INTEGER, -- d
705 * prime1 INTEGER, -- p
706 * prime2 INTEGER, -- q
707 * exponent1 INTEGER, -- d mod (p-1)
708 * exponent2 INTEGER, -- d mod (q-1)
709 * coefficient INTEGER, -- (inverse of q) mod p
710 * otherPrimeInfos OtherPrimeInfos OPTIONAL
711 * }
712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
714 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717 }
718
719 end = p + len;
720
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100721 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200722 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200724 }
725
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100726 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200729 }
730
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100731 /* Import N */
732 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
733 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
734 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
735 NULL, 0, NULL, 0 ) ) != 0 )
736 goto cleanup;
737 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100739 /* Import E */
740 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
741 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
742 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
743 NULL, 0, p, len ) ) != 0 )
744 goto cleanup;
745 p += len;
746
747 /* Import D */
748 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
749 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
750 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
751 p, len, NULL, 0 ) ) != 0 )
752 goto cleanup;
753 p += len;
754
755 /* Import P */
756 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
757 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
758 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
759 NULL, 0, NULL, 0 ) ) != 0 )
760 goto cleanup;
761 p += len;
762
763 /* Import Q */
764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
765 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
766 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
767 NULL, 0, NULL, 0 ) ) != 0 )
768 goto cleanup;
769 p += len;
770
Jack Lloydb10fd062020-01-29 13:09:55 -0500771#if !defined(MBEDTLS_RSA_NO_CRT)
772 /*
773 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
774 * that they can be easily recomputed from D, P and Q. However by
775 * parsing them from the PKCS1 structure it is possible to avoid
776 * recalculating them which both reduces the overhead of loading
777 * RSA private keys into memory and also avoids side channels which
778 * can arise when computing those values, since all of D, P, and Q
779 * are secret. See https://eprint.iacr.org/2020/055 for a
780 * description of one such attack.
781 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100782
Jack Lloydb10fd062020-01-29 13:09:55 -0500783 /* Import DP */
784 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DP ) ) != 0)
785 goto cleanup;
786
787 /* Import DQ */
788 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0)
789 goto cleanup;
790
791 /* Import QP */
792 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->QP ) ) != 0)
793 goto cleanup;
794
795#else
796 /* Verify existance of the CRT params */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100797 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
798 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
799 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500800 goto cleanup;
801#endif
802
803 /* Complete the RSA private key */
804 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100805 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200806
807 if( p != end )
808 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
810 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200811 }
812
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100813cleanup:
814
Hanno Beckerefa14e82017-10-11 19:45:19 +0100815 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100816
817 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100819 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100820 if( ( ret & 0xff80 ) == 0 )
821 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
822 else
823 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200825 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200826 }
827
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100828 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200829}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200830#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200833/*
834 * Parse a SEC1 encoded private EC key
835 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200836static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837 const unsigned char *key,
838 size_t keylen )
839{
840 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100841 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200843 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200844 unsigned char *p = (unsigned char *) key;
845 unsigned char *end = p + keylen;
846 unsigned char *end2;
847
848 /*
849 * RFC 5915, or SEC1 Appendix C.4
850 *
851 * ECPrivateKey ::= SEQUENCE {
852 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
853 * privateKey OCTET STRING,
854 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
855 * publicKey [1] BIT STRING OPTIONAL
856 * }
857 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
859 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862 }
863
864 end = p + len;
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
867 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868
869 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200872 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
873 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 mbedtls_ecp_keypair_free( eck );
878 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879 }
880
881 p += len;
882
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200883 pubkey_done = 0;
884 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200886 /*
887 * Is 'parameters' present?
888 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200889 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
890 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200891 {
892 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
893 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
894 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200895 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200896 return( ret );
897 }
898 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200899 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100903 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800904 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200905
Jethro Beekmand2df9362018-02-16 13:11:04 -0800906 if( p != end )
907 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200908 /*
909 * Is 'publickey' present? If not, or if we can't read it (eg because it
910 * is compressed), create it from the private key.
911 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200912 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
913 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200914 {
915 end2 = p + len;
916
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200917 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
918 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200919
920 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200921 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
922 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200923
924 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
925 pubkey_done = 1;
926 else
927 {
928 /*
929 * The only acceptable failure mode of pk_get_ecpubkey() above
930 * is if the point format is not recognized.
931 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200932 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
933 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200934 }
935 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200936 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200937 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200938 mbedtls_ecp_keypair_free( eck );
939 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200940 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100942
943 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100945 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200946 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_ecp_keypair_free( eck );
948 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200949 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200952 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200953 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200954 return( ret );
955 }
956
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200957 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200958}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960
961/*
962 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100963 *
964 * Notes:
965 *
966 * - This function does not own the key buffer. It is the
967 * responsibility of the caller to take care of zeroizing
968 * and freeing it after use.
969 *
970 * - The function is responsible for freeing the provided
971 * PK context on failure.
972 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200973 */
974static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976 const unsigned char* key,
977 size_t keylen )
978{
979 int ret, version;
980 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982 unsigned char *p = (unsigned char *) key;
983 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200984 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
985 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200986
987 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +0100988 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200989 *
990 * PrivateKeyInfo ::= SEQUENCE {
991 * version Version,
992 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
993 * privateKey PrivateKey,
994 * attributes [0] IMPLICIT Attributes OPTIONAL }
995 *
996 * Version ::= INTEGER
997 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
998 * PrivateKey ::= OCTET STRING
999 *
1000 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1001 */
1002
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1004 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 }
1008
1009 end = p + len;
1010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1012 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013
1014 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016
1017 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1021 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022
1023 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1025 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1028 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001030 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031 return( ret );
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033#if defined(MBEDTLS_RSA_C)
1034 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039 return( ret );
1040 }
1041 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#endif /* MBEDTLS_RSA_C */
1043#if defined(MBEDTLS_ECP_C)
1044 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1047 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050 return( ret );
1051 }
1052 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001053#endif /* MBEDTLS_ECP_C */
1054 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001056 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057}
1058
1059/*
1060 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001061 *
1062 * To save space, the decryption happens in-place on the given key buffer.
1063 * Also, while this function may modify the keybuffer, it doesn't own it,
1064 * and instead it is the responsibility of the caller to zeroize and properly
1065 * free it after use.
1066 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001071 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001072 const unsigned char *pwd, size_t pwdlen )
1073{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001074 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001076 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001077 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1079#if defined(MBEDTLS_PKCS12_C)
1080 mbedtls_cipher_type_t cipher_alg;
1081 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082#endif
1083
Hanno Becker2aa80a72017-09-07 15:28:45 +01001084 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085 end = p + keylen;
1086
1087 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001089
1090 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001091 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001092 *
1093 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1094 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1095 * encryptedData EncryptedData
1096 * }
1097 *
1098 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1099 *
1100 * EncryptedData ::= OCTET STRING
1101 *
1102 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001103 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1106 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109 }
1110
1111 end = p + len;
1112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1114 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001116 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1117 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118
Hanno Beckerfab35692017-08-25 13:38:26 +01001119 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120
1121 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001122 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124#if defined(MBEDTLS_PKCS12_C)
1125 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 cipher_alg, md_alg,
1129 pwd, pwdlen, p, len, buf ) ) != 0 )
1130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1132 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133
1134 return( ret );
1135 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001136
1137 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001141 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1142 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001143 pwd, pwdlen,
1144 p, len, buf ) ) != 0 )
1145 {
1146 return( ret );
1147 }
1148
1149 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001151 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1153 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001154
1155 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 }
1157 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_PKCS12_C */
1159#if defined(MBEDTLS_PKCS5_C)
1160 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163 p, len, buf ) ) != 0 )
1164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1166 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167
1168 return( ret );
1169 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001170
1171 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 }
1173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001175 {
1176 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001177 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001179 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001181
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1183}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
1186/*
1187 * Parse a private key
1188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 const unsigned char *key, size_t keylen,
1191 const unsigned char *pwd, size_t pwdlen )
1192{
1193 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001198#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199
Gilles Peskinee97dc602018-12-19 00:51:38 +01001200 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001201 if( keylen == 0 )
1202 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1203 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001204
1205#if defined(MBEDTLS_PEM_PARSE_C)
1206 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001209 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001210 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001211 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1212 else
1213 ret = mbedtls_pem_read_buffer( &pem,
1214 "-----BEGIN RSA PRIVATE KEY-----",
1215 "-----END RSA PRIVATE KEY-----",
1216 key, pwd, pwdlen, &len );
1217
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218 if( ret == 0 )
1219 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001220 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001221 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223 pem.buf, pem.buflen ) ) != 0 )
1224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226 }
1227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229 return( ret );
1230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1232 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1233 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1234 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1235 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001240 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001241 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001242 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1243 else
1244 ret = mbedtls_pem_read_buffer( &pem,
1245 "-----BEGIN EC PRIVATE KEY-----",
1246 "-----END EC PRIVATE KEY-----",
1247 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248 if( ret == 0 )
1249 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001250 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251
Hanno Beckerfab35692017-08-25 13:38:26 +01001252 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254 pem.buf, pem.buflen ) ) != 0 )
1255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257 }
1258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 return( ret );
1261 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1263 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1264 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1265 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1266 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001269
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001270 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001271 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001272 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1273 else
1274 ret = mbedtls_pem_read_buffer( &pem,
1275 "-----BEGIN PRIVATE KEY-----",
1276 "-----END PRIVATE KEY-----",
1277 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 if( ret == 0 )
1279 {
1280 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1281 pem.buf, pem.buflen ) ) != 0 )
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284 }
1285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287 return( ret );
1288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 return( ret );
1291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001293 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001294 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001295 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1296 else
1297 ret = mbedtls_pem_read_buffer( &pem,
1298 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1299 "-----END ENCRYPTED PRIVATE KEY-----",
1300 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001301 if( ret == 0 )
1302 {
1303 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1304 pem.buf, pem.buflen,
1305 pwd, pwdlen ) ) != 0 )
1306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 }
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311 return( ret );
1312 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001316#else
1317 ((void) pwd);
1318 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320
1321 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001322 * At this point we only know it's not a PEM formatted key. Could be any
1323 * of the known DER encoded private key formats
1324 *
1325 * We try the different DER format parsers to see if one passes without
1326 * error
1327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001330 unsigned char *key_copy;
1331
1332 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1333 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1334
1335 memcpy( key_copy, key, keylen );
1336
1337 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1338 pwd, pwdlen );
1339
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001340 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001341 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 }
1343
Hanno Beckerfab35692017-08-25 13:38:26 +01001344 if( ret == 0 )
1345 return( 0 );
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001348 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 {
1352 return( ret );
1353 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355
1356 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1357 return( 0 );
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001360 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363
Hanno Becker9be19262017-09-08 12:39:44 +01001364 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001365 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1366 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001367 {
1368 return( 0 );
1369 }
1370
Hanno Becker780f0a42018-10-10 11:23:33 +01001371 mbedtls_pk_free( pk );
1372 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001376 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001377 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1378 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1379 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 {
1381 return( 0 );
1382 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001383 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
Hanno Becker780f0a42018-10-10 11:23:33 +01001386 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1387 * it is ok to leave the PK context initialized but not
1388 * freed: It is the caller's responsibility to call pk_init()
1389 * before calling this function, and to call pk_free()
1390 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1391 * isn't, this leads to mbedtls_pk_free() being called
1392 * twice, once here and once by the caller, but this is
1393 * also ok and in line with the mbedtls_pk_free() calls
1394 * on failed PEM parsing attempts. */
1395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397}
1398
1399/*
1400 * Parse a public key
1401 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403 const unsigned char *key, size_t keylen )
1404{
1405 int ret;
1406 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001407#if defined(MBEDTLS_RSA_C)
1408 const mbedtls_pk_info_t *pk_info;
1409#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001411 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001412 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001413#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Gilles Peskinee97dc602018-12-19 00:51:38 +01001415 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001416 if( keylen == 0 )
1417 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001418 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1419
1420#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001422#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001423 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001424 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001425 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1426 else
1427 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001428 "-----BEGIN RSA PUBLIC KEY-----",
1429 "-----END RSA PUBLIC KEY-----",
1430 key, NULL, 0, &len );
1431
1432 if( ret == 0 )
1433 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001434 p = pem.buf;
1435 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1436 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001437
Ron Eldor84df1ae2017-10-16 17:11:52 +03001438 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1439 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001440
Ron Eldor84df1ae2017-10-16 17:11:52 +03001441 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1442 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001443
Ron Eldord0c56de2017-10-10 17:03:08 +03001444 mbedtls_pem_free( &pem );
1445 return( ret );
1446 }
1447 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1448 {
1449 mbedtls_pem_free( &pem );
1450 return( ret );
1451 }
1452#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453
1454 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001455 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001456 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1457 else
1458 ret = mbedtls_pem_read_buffer( &pem,
1459 "-----BEGIN PUBLIC KEY-----",
1460 "-----END PUBLIC KEY-----",
1461 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001462
1463 if( ret == 0 )
1464 {
1465 /*
1466 * Was PEM encoded
1467 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001468 p = pem.buf;
1469
1470 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1471 mbedtls_pem_free( &pem );
1472 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001474 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477 return( ret );
1478 }
Ron Eldor5472d432017-10-17 09:49:00 +03001479 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001481
1482#if defined(MBEDTLS_RSA_C)
1483 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1484 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1485
1486 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1487 return( ret );
1488
Ron Eldor9566ff72018-02-07 18:59:41 +02001489 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001490 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001491 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001492 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001493 return( ret );
1494 }
1495 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001496 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001497 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001498 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001499 }
1500#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001501 p = (unsigned char *) key;
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505 return( ret );
1506}
1507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_PK_PARSE_C */