blob: 4ec63e4bbc3cd415d5e507610b0a6ee239369ba7 [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
771 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100772 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100773 goto cleanup;
774
775 /* Check optional parameters */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100776 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
777 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
778 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100779 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780
781 if( p != end )
782 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100783 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
784 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200785 }
786
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100787cleanup:
788
Hanno Beckerefa14e82017-10-11 19:45:19 +0100789 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100790
791 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100793 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100794 if( ( ret & 0xff80 ) == 0 )
795 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
796 else
797 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200800 }
801
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200803}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200804#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200807/*
808 * Parse a SEC1 encoded private EC key
809 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200811 const unsigned char *key,
812 size_t keylen )
813{
814 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100815 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200816 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200818 unsigned char *p = (unsigned char *) key;
819 unsigned char *end = p + keylen;
820 unsigned char *end2;
821
822 /*
823 * RFC 5915, or SEC1 Appendix C.4
824 *
825 * ECPrivateKey ::= SEQUENCE {
826 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
827 * privateKey OCTET STRING,
828 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
829 * publicKey [1] BIT STRING OPTIONAL
830 * }
831 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200832 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
833 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200835 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836 }
837
838 end = p + len;
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
841 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842
843 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
847 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851 mbedtls_ecp_keypair_free( eck );
852 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 }
854
855 p += len;
856
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200857 pubkey_done = 0;
858 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200860 /*
861 * Is 'parameters' present?
862 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200863 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
864 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200865 {
866 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
867 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
868 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200869 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200870 return( ret );
871 }
872 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200873 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200876 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100877 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800878 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200879
Jethro Beekmand2df9362018-02-16 13:11:04 -0800880 if( p != end )
881 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200882 /*
883 * Is 'publickey' present? If not, or if we can't read it (eg because it
884 * is compressed), create it from the private key.
885 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200886 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
887 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200888 {
889 end2 = p + len;
890
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200891 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
892 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200893
894 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200895 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
896 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200897
898 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
899 pubkey_done = 1;
900 else
901 {
902 /*
903 * The only acceptable failure mode of pk_get_ecpubkey() above
904 * is if the point format is not recognized.
905 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200906 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
907 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200908 }
909 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200910 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200911 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200912 mbedtls_ecp_keypair_free( eck );
913 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200914 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100916
917 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100919 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 mbedtls_ecp_keypair_free( eck );
922 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200923 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928 return( ret );
929 }
930
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200931 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200932}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200934
935/*
936 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100937 *
938 * Notes:
939 *
940 * - This function does not own the key buffer. It is the
941 * responsibility of the caller to take care of zeroizing
942 * and freeing it after use.
943 *
944 * - The function is responsible for freeing the provided
945 * PK context on failure.
946 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200947 */
948static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200949 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200950 const unsigned char* key,
951 size_t keylen )
952{
953 int ret, version;
954 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200956 unsigned char *p = (unsigned char *) key;
957 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
959 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960
961 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +0100962 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200963 *
964 * PrivateKeyInfo ::= SEQUENCE {
965 * version Version,
966 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
967 * privateKey PrivateKey,
968 * attributes [0] IMPLICIT Attributes OPTIONAL }
969 *
970 * Version ::= INTEGER
971 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
972 * PrivateKey ::= OCTET STRING
973 *
974 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
975 */
976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
978 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 }
982
983 end = p + len;
984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
986 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987
988 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990
991 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
995 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996
997 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
999 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1002 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001004 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 return( ret );
1006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_RSA_C)
1008 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001012 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013 return( ret );
1014 }
1015 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_RSA_C */
1017#if defined(MBEDTLS_ECP_C)
1018 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1021 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 return( ret );
1025 }
1026 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#endif /* MBEDTLS_ECP_C */
1028 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001030 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031}
1032
1033/*
1034 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001035 *
1036 * To save space, the decryption happens in-place on the given key buffer.
1037 * Also, while this function may modify the keybuffer, it doesn't own it,
1038 * and instead it is the responsibility of the caller to zeroize and properly
1039 * free it after use.
1040 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001045 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 const unsigned char *pwd, size_t pwdlen )
1047{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001048 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001050 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1053#if defined(MBEDTLS_PKCS12_C)
1054 mbedtls_cipher_type_t cipher_alg;
1055 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056#endif
1057
Hanno Becker2aa80a72017-09-07 15:28:45 +01001058 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059 end = p + keylen;
1060
1061 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063
1064 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001065 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066 *
1067 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1068 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1069 * encryptedData EncryptedData
1070 * }
1071 *
1072 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1073 *
1074 * EncryptedData ::= OCTET STRING
1075 *
1076 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001077 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1080 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083 }
1084
1085 end = p + len;
1086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1088 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1091 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001092
Hanno Beckerfab35692017-08-25 13:38:26 +01001093 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094
1095 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001096 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_PKCS12_C)
1099 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102 cipher_alg, md_alg,
1103 pwd, pwdlen, p, len, buf ) ) != 0 )
1104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1106 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107
1108 return( ret );
1109 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001110
1111 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1116 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117 pwd, pwdlen,
1118 p, len, buf ) ) != 0 )
1119 {
1120 return( ret );
1121 }
1122
1123 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1127 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001128
1129 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130 }
1131 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#endif /* MBEDTLS_PKCS12_C */
1133#if defined(MBEDTLS_PKCS5_C)
1134 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 p, len, buf ) ) != 0 )
1138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001139 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1140 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141
1142 return( ret );
1143 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001144
1145 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146 }
1147 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001149 {
1150 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001151 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001153 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001155
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159
1160/*
1161 * Parse a private key
1162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 const unsigned char *key, size_t keylen,
1165 const unsigned char *pwd, size_t pwdlen )
1166{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001167#if defined(MBEDTLS_PKCS12_C) || \
1168 defined(MBEDTLS_PKCS5_C) || \
1169 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001171#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001176#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177
Gilles Peskinee97dc602018-12-19 00:51:38 +01001178 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001179 if( keylen == 0 )
1180 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1181 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001182
1183#if defined(MBEDTLS_PEM_PARSE_C)
1184 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001187 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001188 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001189 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1190 else
1191 ret = mbedtls_pem_read_buffer( &pem,
1192 "-----BEGIN RSA PRIVATE KEY-----",
1193 "-----END RSA PRIVATE KEY-----",
1194 key, pwd, pwdlen, &len );
1195
Paul Bakker1a7550a2013-09-15 13:01:22 +02001196 if( ret == 0 )
1197 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001198 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001199 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201 pem.buf, pem.buflen ) ) != 0 )
1202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204 }
1205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001207 return( ret );
1208 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1210 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1211 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1212 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1213 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001214 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001218 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001219 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001220 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1221 else
1222 ret = mbedtls_pem_read_buffer( &pem,
1223 "-----BEGIN EC PRIVATE KEY-----",
1224 "-----END EC PRIVATE KEY-----",
1225 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226 if( ret == 0 )
1227 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001228 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
Hanno Beckerfab35692017-08-25 13:38:26 +01001230 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001232 pem.buf, pem.buflen ) ) != 0 )
1233 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001234 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235 }
1236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238 return( ret );
1239 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001240 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1241 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1242 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1243 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1244 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001248 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001249 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001250 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1251 else
1252 ret = mbedtls_pem_read_buffer( &pem,
1253 "-----BEGIN PRIVATE KEY-----",
1254 "-----END PRIVATE KEY-----",
1255 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256 if( ret == 0 )
1257 {
1258 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1259 pem.buf, pem.buflen ) ) != 0 )
1260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262 }
1263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265 return( ret );
1266 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268 return( ret );
1269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001271 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001272 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001273 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1274 else
1275 ret = mbedtls_pem_read_buffer( &pem,
1276 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1277 "-----END ENCRYPTED PRIVATE KEY-----",
1278 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279 if( ret == 0 )
1280 {
1281 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1282 pem.buf, pem.buflen,
1283 pwd, pwdlen ) ) != 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_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294#else
1295 ((void) pwd);
1296 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001298
1299 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001300 * At this point we only know it's not a PEM formatted key. Could be any
1301 * of the known DER encoded private key formats
1302 *
1303 * We try the different DER format parsers to see if one passes without
1304 * error
1305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001308 unsigned char *key_copy;
1309
1310 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1311 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1312
1313 memcpy( key_copy, key, keylen );
1314
1315 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1316 pwd, pwdlen );
1317
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001318 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001319 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320 }
1321
Hanno Beckerfab35692017-08-25 13:38:26 +01001322 if( ret == 0 )
1323 return( 0 );
1324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001326 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001329 {
1330 return( ret );
1331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001334 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 return( 0 );
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001338 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341
Hanno Becker9be19262017-09-08 12:39:44 +01001342 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001343 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1344 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345 {
1346 return( 0 );
1347 }
1348
Hanno Becker780f0a42018-10-10 11:23:33 +01001349 mbedtls_pk_free( pk );
1350 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001354 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001355 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1356 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1357 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358 {
1359 return( 0 );
1360 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001361 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363
Hanno Becker780f0a42018-10-10 11:23:33 +01001364 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1365 * it is ok to leave the PK context initialized but not
1366 * freed: It is the caller's responsibility to call pk_init()
1367 * before calling this function, and to call pk_free()
1368 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1369 * isn't, this leads to mbedtls_pk_free() being called
1370 * twice, once here and once by the caller, but this is
1371 * also ok and in line with the mbedtls_pk_free() calls
1372 * on failed PEM parsing attempts. */
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375}
1376
1377/*
1378 * Parse a public key
1379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381 const unsigned char *key, size_t keylen )
1382{
1383 int ret;
1384 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001385#if defined(MBEDTLS_RSA_C)
1386 const mbedtls_pk_info_t *pk_info;
1387#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001391#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001392
Gilles Peskinee97dc602018-12-19 00:51:38 +01001393 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001394 if( keylen == 0 )
1395 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001396 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1397
1398#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001400#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001401 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001402 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001403 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1404 else
1405 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001406 "-----BEGIN RSA PUBLIC KEY-----",
1407 "-----END RSA PUBLIC KEY-----",
1408 key, NULL, 0, &len );
1409
1410 if( ret == 0 )
1411 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001412 p = pem.buf;
1413 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1414 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001415
Ron Eldor84df1ae2017-10-16 17:11:52 +03001416 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1417 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001418
Ron Eldor84df1ae2017-10-16 17:11:52 +03001419 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1420 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001421
Ron Eldord0c56de2017-10-10 17:03:08 +03001422 mbedtls_pem_free( &pem );
1423 return( ret );
1424 }
1425 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1426 {
1427 mbedtls_pem_free( &pem );
1428 return( ret );
1429 }
1430#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001431
1432 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001433 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001434 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1435 else
1436 ret = mbedtls_pem_read_buffer( &pem,
1437 "-----BEGIN PUBLIC KEY-----",
1438 "-----END PUBLIC KEY-----",
1439 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440
1441 if( ret == 0 )
1442 {
1443 /*
1444 * Was PEM encoded
1445 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001446 p = pem.buf;
1447
1448 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1449 mbedtls_pem_free( &pem );
1450 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001451 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001454 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455 return( ret );
1456 }
Ron Eldor5472d432017-10-17 09:49:00 +03001457 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001459
1460#if defined(MBEDTLS_RSA_C)
1461 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1462 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1463
1464 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1465 return( ret );
1466
Ron Eldor9566ff72018-02-07 18:59:41 +02001467 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001468 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001469 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001470 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001471 return( ret );
1472 }
1473 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001474 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001475 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001476 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001477 }
1478#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479 p = (unsigned char *) key;
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482
Paul Bakker1a7550a2013-09-15 13:01:22 +02001483 return( ret );
1484}
1485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001486#endif /* MBEDTLS_PK_PARSE_C */