blob: 624ca4c6715cbc3706f8c74b8ea6ba0d56e83264 [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
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker1a7550a2013-09-15 13:01:22 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/pk.h"
58#include "mbedtls/asn1.h"
59#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050060#include "mbedtls/platform_util.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020061
Rich Evans00ab4702015-02-06 13:43:58 +000062#include <string.h>
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020078#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000080#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020081#endif
82
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000084#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020085#else
86#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020087#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020089#endif
90
Gilles Peskinee97dc602018-12-19 00:51:38 +010091/* Parameter validation macros based on platform_util.h */
92#define PK_VALIDATE_RET( cond ) \
93 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
94#define PK_VALIDATE( cond ) \
95 MBEDTLS_INTERNAL_VALIDATE( cond )
96
Gilles Peskine832f3492017-11-30 11:42:12 +010097#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020098/*
99 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200100 *
101 * The file is expected to contain either PEM or DER encoded data.
102 * A terminating null byte is always appended. It is included in the announced
103 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200104 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106{
107 FILE *f;
108 long size;
109
Gilles Peskinee97dc602018-12-19 00:51:38 +0100110 PK_VALIDATE_RET( path != NULL );
111 PK_VALIDATE_RET( buf != NULL );
112 PK_VALIDATE_RET( n != NULL );
113
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116
117 fseek( f, 0, SEEK_END );
118 if( ( size = ftell( f ) ) == -1 )
119 {
120 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122 }
123 fseek( f, 0, SEEK_SET );
124
125 *n = (size_t) size;
126
127 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200128 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129 {
130 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200131 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132 }
133
134 if( fread( *buf, 1, *n, f ) != *n )
135 {
136 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100137
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500138 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142 }
143
144 fclose( f );
145
146 (*buf)[*n] = '\0';
147
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200148 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
149 ++*n;
150
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151 return( 0 );
152}
153
154/*
155 * Load and parse a private key
156 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158 const char *path, const char *pwd )
159{
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
170 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174 (const unsigned char *) pwd, strlen( pwd ) );
175
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500176 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178
179 return( ret );
180}
181
182/*
183 * Load and parse a public key
184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186{
187 int ret;
188 size_t n;
189 unsigned char *buf;
190
Gilles Peskinee97dc602018-12-19 00:51:38 +0100191 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100192 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195 return( ret );
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200198
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500199 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200201
202 return( ret );
203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_ECP_C)
207/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200208 *
209 * ECParameters ::= CHOICE {
210 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100211 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200212 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200213 * }
214 */
215static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200217{
218 int ret;
219
Sanne Woudab2b29d52017-08-21 15:58:12 +0100220 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100221 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
222 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100223
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100224 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200225 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 if( params->tag != MBEDTLS_ASN1_OID
227#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
228 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100229#endif
230 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
233 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100239 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200240
241 params->p = *p;
242 *p += params->len;
243
244 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
246 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200247
248 return( 0 );
249}
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200252/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100253 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
254 * WARNING: the resulting group should only be used with
255 * pk_group_id_from_specified(), since its base point may not be set correctly
256 * if it was encoded compressed.
257 *
258 * SpecifiedECDomain ::= SEQUENCE {
259 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
260 * fieldID FieldID {{FieldTypes}},
261 * curve Curve,
262 * base ECPoint,
263 * order INTEGER,
264 * cofactor INTEGER OPTIONAL,
265 * hash HashAlgorithm OPTIONAL,
266 * ...
267 * }
268 *
269 * We only support prime-field as field type, and ignore hash and cofactor.
270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100272{
273 int ret;
274 unsigned char *p = params->p;
275 const unsigned char * const end = params->p + params->len;
276 const unsigned char *end_field, *end_curve;
277 size_t len;
278 int ver;
279
280 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
282 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100283
284 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100286
287 /*
288 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
289 * fieldType FIELD-ID.&id({IOSet}),
290 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
291 * }
292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
294 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100295 return( ret );
296
297 end_field = p + len;
298
299 /*
300 * FIELD-ID ::= TYPE-IDENTIFIER
301 * FieldTypes FIELD-ID ::= {
302 * { Prime-p IDENTIFIED BY prime-field } |
303 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
304 * }
305 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100308 return( ret );
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
311 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100314 }
315
316 p += len;
317
318 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
320 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200322 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323
324 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
326 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327
328 /*
329 * Curve ::= SEQUENCE {
330 * a FieldElement,
331 * b FieldElement,
332 * seed BIT STRING OPTIONAL
333 * -- Shall be present if used in SpecifiedECDomain
334 * -- with version equal to ecdpVer2 or ecdpVer3
335 * }
336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
338 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100339 return( ret );
340
341 end_curve = p + len;
342
343 /*
344 * FieldElement ::= OCTET STRING
345 * containing an integer in the case of a prime field
346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
348 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100349 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100351 }
352
353 p += len;
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
356 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100359 }
360
361 p += len;
362
363 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100365 p += len;
366
367 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
369 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100370
371 /*
372 * ECPoint ::= OCTET STRING
373 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
375 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100378 ( const unsigned char *) p, len ) ) != 0 )
379 {
380 /*
381 * If we can't read the point because it's compressed, cheat by
382 * reading only the X coordinate and the parity bit of Y.
383 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100385 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 len != mbedtls_mpi_size( &grp->P ) + 1 ||
387 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
388 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
389 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100390 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100392 }
393 }
394
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395 p += len;
396
397 /*
398 * order INTEGER
399 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
401 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200403 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100404
405 /*
406 * Allow optional elements by purposefully not enforcing p == end here.
407 */
408
409 return( 0 );
410}
411
412/*
413 * Find the group id associated with an (almost filled) group as generated by
414 * pk_group_from_specified(), or return an error if unknown.
415 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416static 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 +0100417{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100418 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_ecp_group ref;
420 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100425 {
426 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200428 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
430 /* Compare to the group we were given, starting with easy tests */
431 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
433 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
434 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
435 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
436 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
437 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 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 +0100440 {
441 break;
442 }
443
444 }
445
446cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449 *grp_id = *id;
450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
452 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453
454 return( ret );
455}
456
457/*
458 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
459 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200460static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
461 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100462{
463 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467
468 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
469 goto cleanup;
470
471 ret = pk_group_id_from_group( &grp, grp_id );
472
473cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100475
476 return( ret );
477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200478#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100479
480/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100482 *
483 * ECParameters ::= CHOICE {
484 * namedCurve OBJECT IDENTIFIER
485 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
486 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200489{
490 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
496 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100497 }
498 else
499 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100501 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
502 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100503#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100505#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100506 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507
508 /*
509 * grp may already be initilialized; if so, make sure IDs match
510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
512 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200514 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200515 return( ret );
516
517 return( 0 );
518}
519
520/*
521 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100522 *
523 * The caller is responsible for clearing the structure upon failure if
524 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200526 */
527static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200529{
530 int ret;
531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100533 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536 }
537
538 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200540 */
541 *p = (unsigned char *) end;
542
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100543 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200544}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200548/*
549 * RSAPublicKey ::= SEQUENCE {
550 * modulus INTEGER, -- n
551 * publicExponent INTEGER -- e
552 * }
553 */
554static int pk_get_rsapubkey( unsigned char **p,
555 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557{
558 int ret;
559 size_t len;
560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
562 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
563 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200564
565 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
567 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100569 /* Import N */
570 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200572
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100573 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
574 NULL, 0, NULL, 0 ) ) != 0 )
575 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
576
577 *p += len;
578
579 /* Import E */
580 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
581 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
582
583 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
584 NULL, 0, *p, len ) ) != 0 )
585 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
586
587 *p += len;
588
Hanno Becker895c5ab2018-01-05 08:08:09 +0000589 if( mbedtls_rsa_complete( rsa ) != 0 ||
590 mbedtls_rsa_check_pubkey( rsa ) != 0 )
591 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100592 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000593 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100594
Paul Bakker1a7550a2013-09-15 13:01:22 +0200595 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
597 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200598
Paul Bakker1a7550a2013-09-15 13:01:22 +0200599 return( 0 );
600}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200602
603/* Get a PK algorithm identifier
604 *
605 * AlgorithmIdentifier ::= SEQUENCE {
606 * algorithm OBJECT IDENTIFIER,
607 * parameters ANY DEFINED BY algorithm OPTIONAL }
608 */
609static int pk_get_pk_alg( unsigned char **p,
610 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200612{
613 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
619 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
622 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623
624 /*
625 * No parameters with RSA (only for EC)
626 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 if( *pk_alg == MBEDTLS_PK_RSA &&
628 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200629 params->len != 0 ) )
630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200632 }
633
634 return( 0 );
635}
636
637/*
638 * SubjectPublicKeyInfo ::= SEQUENCE {
639 * algorithm AlgorithmIdentifier,
640 * subjectPublicKey BIT STRING }
641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
643 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644{
645 int ret;
646 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 mbedtls_asn1_buf alg_params;
648 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
649 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200650
Gilles Peskinee97dc602018-12-19 00:51:38 +0100651 PK_VALIDATE_RET( p != NULL );
652 PK_VALIDATE_RET( *p != NULL );
653 PK_VALIDATE_RET( end != NULL );
654 PK_VALIDATE_RET( pk != NULL );
655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
657 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660 }
661
662 end = *p + len;
663
664 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
665 return( ret );
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
668 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200669
670 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
672 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
675 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200677 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200678 return( ret );
679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680#if defined(MBEDTLS_RSA_C)
681 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200684 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* MBEDTLS_RSA_C */
686#if defined(MBEDTLS_ECP_C)
687 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200690 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200692 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693#endif /* MBEDTLS_ECP_C */
694 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200695
696 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
698 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200699
700 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200701 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200702
703 return( ret );
704}
705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200707/*
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100708 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
709 *
710 * The value zero is:
711 * - never a valid value for an RSA parameter
712 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
713 *
714 * Since values can't be omitted in PKCS#1, passing a zero value to
715 * rsa_complete() would be incorrect, so reject zero values early.
716 */
717static int asn1_get_nonzero_mpi( unsigned char **p,
718 const unsigned char *end,
719 mbedtls_mpi *X )
720{
721 int ret;
722
723 ret = mbedtls_asn1_get_mpi( p, end, X );
724 if( ret != 0 )
725 return( ret );
726
727 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
728 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
729
730 return( 0 );
731}
732
733/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200734 * Parse a PKCS#1 encoded private RSA key
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 const unsigned char *key,
738 size_t keylen )
739{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100740 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 size_t len;
742 unsigned char *p, *end;
743
Hanno Beckerefa14e82017-10-11 19:45:19 +0100744 mbedtls_mpi T;
745 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100746
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747 p = (unsigned char *) key;
748 end = p + keylen;
749
750 /*
751 * This function parses the RSAPrivateKey (PKCS#1)
752 *
753 * RSAPrivateKey ::= SEQUENCE {
754 * version Version,
755 * modulus INTEGER, -- n
756 * publicExponent INTEGER, -- e
757 * privateExponent INTEGER, -- d
758 * prime1 INTEGER, -- p
759 * prime2 INTEGER, -- q
760 * exponent1 INTEGER, -- d mod (p-1)
761 * exponent2 INTEGER, -- d mod (q-1)
762 * coefficient INTEGER, -- (inverse of q) mod p
763 * otherPrimeInfos OtherPrimeInfos OPTIONAL
764 * }
765 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
767 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200769 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200770 }
771
772 end = p + len;
773
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100774 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200777 }
778
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100779 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200782 }
783
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100784 /* Import N */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100785 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
786 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
787 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100788 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200789
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100790 /* Import E */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100791 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
792 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
793 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100794 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100795
796 /* Import D */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100797 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
798 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
799 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100800 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100801
802 /* Import P */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100803 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
804 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
805 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100806 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100807
808 /* Import Q */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100809 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
810 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
811 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100812 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100813
Manuel Pégourié-Gonnardd09fcde2020-02-18 10:22:54 +0100814#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloydb10fd062020-01-29 13:09:55 -0500815 /*
816 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
817 * that they can be easily recomputed from D, P and Q. However by
818 * parsing them from the PKCS1 structure it is possible to avoid
819 * recalculating them which both reduces the overhead of loading
820 * RSA private keys into memory and also avoids side channels which
821 * can arise when computing those values, since all of D, P, and Q
822 * are secret. See https://eprint.iacr.org/2020/055 for a
823 * description of one such attack.
824 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100825
Jack Lloydb10fd062020-01-29 13:09:55 -0500826 /* Import DP */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100827 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
828 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500829 goto cleanup;
830
831 /* Import DQ */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100832 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
833 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500834 goto cleanup;
835
836 /* Import QP */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100837 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
838 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500839 goto cleanup;
840
841#else
842 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100843 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
844 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
845 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500846 goto cleanup;
847#endif
848
Manuel Pégourié-Gonnard25bb8dc2020-02-14 11:28:47 +0100849 /* rsa_complete() doesn't complete anything with the default
850 * implementation but is still called:
851 * - for the benefit of alternative implementation that may want to
852 * pre-compute stuff beyond what's provided (eg Montgomery factors)
853 * - as is also sanity-checks the key
854 *
855 * Furthermore, we also check the public part for consistency with
856 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
857 */
858 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
859 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
860 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100861 goto cleanup;
Manuel Pégourié-Gonnard25bb8dc2020-02-14 11:28:47 +0100862 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863
864 if( p != end )
865 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100866 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
867 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200868 }
869
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100870cleanup:
871
Hanno Beckerefa14e82017-10-11 19:45:19 +0100872 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100873
874 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100876 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100877 if( ( ret & 0xff80 ) == 0 )
878 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
879 else
880 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883 }
884
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100885 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890/*
891 * Parse a SEC1 encoded private EC key
892 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200894 const unsigned char *key,
895 size_t keylen )
896{
897 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100898 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200899 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901 unsigned char *p = (unsigned char *) key;
902 unsigned char *end = p + keylen;
903 unsigned char *end2;
904
905 /*
906 * RFC 5915, or SEC1 Appendix C.4
907 *
908 * ECPrivateKey ::= SEQUENCE {
909 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
910 * privateKey OCTET STRING,
911 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
912 * publicKey [1] BIT STRING OPTIONAL
913 * }
914 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
916 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919 }
920
921 end = p + len;
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
924 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200925
926 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
930 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200934 mbedtls_ecp_keypair_free( eck );
935 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936 }
937
938 p += len;
939
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200940 pubkey_done = 0;
941 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200942 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200943 /*
944 * Is 'parameters' present?
945 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200946 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
947 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200948 {
949 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
950 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
951 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200952 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200953 return( ret );
954 }
955 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200956 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200957 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200958 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100960 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800961 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200962
Jethro Beekmand2df9362018-02-16 13:11:04 -0800963 if( p != end )
964 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200965 /*
966 * Is 'publickey' present? If not, or if we can't read it (eg because it
967 * is compressed), create it from the private key.
968 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200969 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
970 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200971 {
972 end2 = p + len;
973
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200974 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
975 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200976
977 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200978 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
979 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200980
981 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
982 pubkey_done = 1;
983 else
984 {
985 /*
986 * The only acceptable failure mode of pk_get_ecpubkey() above
987 * is if the point format is not recognized.
988 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200989 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
990 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200991 }
992 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200993 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200994 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200995 mbedtls_ecp_keypair_free( eck );
996 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200997 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100999
1000 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001002 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 mbedtls_ecp_keypair_free( eck );
1005 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001006 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011 return( ret );
1012 }
1013
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001014 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017
1018/*
1019 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001020 *
1021 * Notes:
1022 *
1023 * - This function does not own the key buffer. It is the
1024 * responsibility of the caller to take care of zeroizing
1025 * and freeing it after use.
1026 *
1027 * - The function is responsible for freeing the provided
1028 * PK context on failure.
1029 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 */
1031static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001033 const unsigned char* key,
1034 size_t keylen )
1035{
1036 int ret, version;
1037 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001039 unsigned char *p = (unsigned char *) key;
1040 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1042 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043
1044 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001045 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 *
1047 * PrivateKeyInfo ::= SEQUENCE {
1048 * version Version,
1049 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1050 * privateKey PrivateKey,
1051 * attributes [0] IMPLICIT Attributes OPTIONAL }
1052 *
1053 * Version ::= INTEGER
1054 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1055 * PrivateKey ::= OCTET STRING
1056 *
1057 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1058 */
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1061 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064 }
1065
1066 end = p + len;
1067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1069 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070
1071 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001073
1074 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1078 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001079
1080 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1082 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1085 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001087 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088 return( ret );
1089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#if defined(MBEDTLS_RSA_C)
1091 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 return( ret );
1097 }
1098 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#endif /* MBEDTLS_RSA_C */
1100#if defined(MBEDTLS_ECP_C)
1101 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001103 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1104 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001106 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107 return( ret );
1108 }
1109 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#endif /* MBEDTLS_ECP_C */
1111 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001113 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114}
1115
1116/*
1117 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001118 *
1119 * To save space, the decryption happens in-place on the given key buffer.
1120 * Also, while this function may modify the keybuffer, it doesn't own it,
1121 * and instead it is the responsibility of the caller to zeroize and properly
1122 * free it after use.
1123 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001128 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129 const unsigned char *pwd, size_t pwdlen )
1130{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001131 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001133 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1136#if defined(MBEDTLS_PKCS12_C)
1137 mbedtls_cipher_type_t cipher_alg;
1138 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139#endif
1140
Hanno Becker2aa80a72017-09-07 15:28:45 +01001141 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142 end = p + keylen;
1143
1144 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001145 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001146
1147 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001148 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149 *
1150 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1151 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1152 * encryptedData EncryptedData
1153 * }
1154 *
1155 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1156 *
1157 * EncryptedData ::= OCTET STRING
1158 *
1159 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001160 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001162 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1163 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 }
1167
1168 end = p + len;
1169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1171 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001173 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1174 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
Hanno Beckerfab35692017-08-25 13:38:26 +01001176 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177
1178 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001179 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181#if defined(MBEDTLS_PKCS12_C)
1182 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185 cipher_alg, md_alg,
1186 pwd, pwdlen, p, len, buf ) ) != 0 )
1187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1189 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190
1191 return( ret );
1192 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001193
1194 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1199 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001200 pwd, pwdlen,
1201 p, len, buf ) ) != 0 )
1202 {
1203 return( ret );
1204 }
1205
1206 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001209 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1210 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001211
1212 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 }
1214 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_PKCS12_C */
1216#if defined(MBEDTLS_PKCS5_C)
1217 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220 p, len, buf ) ) != 0 )
1221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1223 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001224
1225 return( ret );
1226 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001227
1228 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229 }
1230 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001232 {
1233 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001234 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001235
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001236 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001238
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1240}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242
1243/*
1244 * Parse a private key
1245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001246int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 const unsigned char *key, size_t keylen,
1248 const unsigned char *pwd, size_t pwdlen )
1249{
1250 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001255#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256
Gilles Peskinee97dc602018-12-19 00:51:38 +01001257 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001258 if( keylen == 0 )
1259 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1260 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001261
1262#if defined(MBEDTLS_PEM_PARSE_C)
1263 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001266 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001267 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001268 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1269 else
1270 ret = mbedtls_pem_read_buffer( &pem,
1271 "-----BEGIN RSA PRIVATE KEY-----",
1272 "-----END RSA PRIVATE KEY-----",
1273 key, pwd, pwdlen, &len );
1274
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 if( ret == 0 )
1276 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001277 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001278 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001280 pem.buf, pem.buflen ) ) != 0 )
1281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 }
1284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001285 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 return( ret );
1287 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1289 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1290 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1291 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1292 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001297 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001298 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001299 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1300 else
1301 ret = mbedtls_pem_read_buffer( &pem,
1302 "-----BEGIN EC PRIVATE KEY-----",
1303 "-----END EC PRIVATE KEY-----",
1304 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 if( ret == 0 )
1306 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001307 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308
Hanno Beckerfab35692017-08-25 13:38:26 +01001309 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311 pem.buf, pem.buflen ) ) != 0 )
1312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 }
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 return( ret );
1318 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1320 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1321 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1322 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1323 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001327 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001328 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001329 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1330 else
1331 ret = mbedtls_pem_read_buffer( &pem,
1332 "-----BEGIN PRIVATE KEY-----",
1333 "-----END PRIVATE KEY-----",
1334 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 if( ret == 0 )
1336 {
1337 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1338 pem.buf, pem.buflen ) ) != 0 )
1339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341 }
1342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344 return( ret );
1345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347 return( ret );
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001350 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001351 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001352 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1353 else
1354 ret = mbedtls_pem_read_buffer( &pem,
1355 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1356 "-----END ENCRYPTED PRIVATE KEY-----",
1357 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001358 if( ret == 0 )
1359 {
1360 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1361 pem.buf, pem.buflen,
1362 pwd, pwdlen ) ) != 0 )
1363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001365 }
1366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368 return( ret );
1369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001373#else
1374 ((void) pwd);
1375 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001377
1378 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001379 * At this point we only know it's not a PEM formatted key. Could be any
1380 * of the known DER encoded private key formats
1381 *
1382 * We try the different DER format parsers to see if one passes without
1383 * error
1384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001387 unsigned char *key_copy;
1388
1389 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1390 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1391
1392 memcpy( key_copy, key, keylen );
1393
1394 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1395 pwd, pwdlen );
1396
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001397 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001398 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 }
1400
Hanno Beckerfab35692017-08-25 13:38:26 +01001401 if( ret == 0 )
1402 return( 0 );
1403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001405 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001407 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001408 {
1409 return( ret );
1410 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412
1413 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1414 return( 0 );
1415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001417 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420
Hanno Becker9be19262017-09-08 12:39:44 +01001421 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001422 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1423 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424 {
1425 return( 0 );
1426 }
1427
Hanno Becker780f0a42018-10-10 11:23:33 +01001428 mbedtls_pk_free( pk );
1429 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001433 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001434 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1435 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1436 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437 {
1438 return( 0 );
1439 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001440 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442
Hanno Becker780f0a42018-10-10 11:23:33 +01001443 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1444 * it is ok to leave the PK context initialized but not
1445 * freed: It is the caller's responsibility to call pk_init()
1446 * before calling this function, and to call pk_free()
1447 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1448 * isn't, this leads to mbedtls_pk_free() being called
1449 * twice, once here and once by the caller, but this is
1450 * also ok and in line with the mbedtls_pk_free() calls
1451 * on failed PEM parsing attempts. */
1452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001454}
1455
1456/*
1457 * Parse a public key
1458 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001460 const unsigned char *key, size_t keylen )
1461{
1462 int ret;
1463 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001464#if defined(MBEDTLS_RSA_C)
1465 const mbedtls_pk_info_t *pk_info;
1466#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001468 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001469 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001470#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471
Gilles Peskinee97dc602018-12-19 00:51:38 +01001472 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001473 if( keylen == 0 )
1474 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001475 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1476
1477#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001479#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001480 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001481 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001482 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1483 else
1484 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001485 "-----BEGIN RSA PUBLIC KEY-----",
1486 "-----END RSA PUBLIC KEY-----",
1487 key, NULL, 0, &len );
1488
1489 if( ret == 0 )
1490 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001491 p = pem.buf;
1492 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1493 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001494
Ron Eldor84df1ae2017-10-16 17:11:52 +03001495 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1496 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001497
Ron Eldor84df1ae2017-10-16 17:11:52 +03001498 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1499 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001500
Ron Eldord0c56de2017-10-10 17:03:08 +03001501 mbedtls_pem_free( &pem );
1502 return( ret );
1503 }
1504 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1505 {
1506 mbedtls_pem_free( &pem );
1507 return( ret );
1508 }
1509#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001510
1511 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001512 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001513 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1514 else
1515 ret = mbedtls_pem_read_buffer( &pem,
1516 "-----BEGIN PUBLIC KEY-----",
1517 "-----END PUBLIC KEY-----",
1518 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001519
1520 if( ret == 0 )
1521 {
1522 /*
1523 * Was PEM encoded
1524 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001525 p = pem.buf;
1526
1527 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1528 mbedtls_pem_free( &pem );
1529 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534 return( ret );
1535 }
Ron Eldor5472d432017-10-17 09:49:00 +03001536 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001537#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001538
1539#if defined(MBEDTLS_RSA_C)
1540 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1541 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1542
1543 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1544 return( ret );
1545
Ron Eldor9566ff72018-02-07 18:59:41 +02001546 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001547 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001548 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001549 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001550 return( ret );
1551 }
1552 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001553 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001554 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001555 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001556 }
1557#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001558 p = (unsigned char *) key;
1559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001561
Paul Bakker1a7550a2013-09-15 13:01:22 +02001562 return( ret );
1563}
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#endif /* MBEDTLS_PK_PARSE_C */