blob: 9a5e1b88c575a67b095308147c292721a85da944 [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úti4e9f7122020-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úti4e9f7122020-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"
Paul Bakker1a7550a2013-09-15 13:01:22 +020060
Rich Evans00ab4702015-02-06 13:43:58 +000061#include <string.h>
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020065#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020068#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000070#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020071#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000073#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020074#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000076#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000079#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020080#endif
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000083#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020084#else
85#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020086#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020088#endif
89
Gilles Peskine832f3492017-11-30 11:42:12 +010090#if defined(MBEDTLS_FS_IO) || \
91 defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker34617722014-06-13 17:20:13 +020092/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020094 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
95}
Gilles Peskine832f3492017-11-30 11:42:12 +010096#endif
Paul Bakker34617722014-06-13 17:20:13 +020097
Gilles Peskine832f3492017-11-30 11:42:12 +010098#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020099/*
100 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200101 *
102 * The file is expected to contain either PEM or DER encoded data.
103 * A terminating null byte is always appended. It is included in the announced
104 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107{
108 FILE *f;
109 long size;
110
111 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113
114 fseek( f, 0, SEEK_END );
115 if( ( size = ftell( f ) ) == -1 )
116 {
117 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200119 }
120 fseek( f, 0, SEEK_SET );
121
122 *n = (size_t) size;
123
124 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200125 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126 {
127 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200128 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129 }
130
131 if( fread( *buf, 1, *n, f ) != *n )
132 {
133 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100134
135 mbedtls_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139 }
140
141 fclose( f );
142
143 (*buf)[*n] = '\0';
144
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200145 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
146 ++*n;
147
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148 return( 0 );
149}
150
151/*
152 * Load and parse a private key
153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200155 const char *path, const char *pwd )
156{
157 int ret;
158 size_t n;
159 unsigned char *buf;
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162 return( ret );
163
164 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 (const unsigned char *) pwd, strlen( pwd ) );
169
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200170 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172
173 return( ret );
174}
175
176/*
177 * Load and parse a public key
178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200180{
181 int ret;
182 size_t n;
183 unsigned char *buf;
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 return( ret );
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200189
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200190 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200192
193 return( ret );
194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_ECP_C)
198/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199 *
200 * ECParameters ::= CHOICE {
201 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100202 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200203 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200204 * }
205 */
206static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200208{
209 int ret;
210
Sanne Woudab2b29d52017-08-21 15:58:12 +0100211 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100212 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
213 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100214
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100215 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200216 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 if( params->tag != MBEDTLS_ASN1_OID
218#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
219 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100220#endif
221 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
224 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100225 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100230 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200231
232 params->p = *p;
233 *p += params->len;
234
235 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
237 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200238
239 return( 0 );
240}
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200243/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
245 * WARNING: the resulting group should only be used with
246 * pk_group_id_from_specified(), since its base point may not be set correctly
247 * if it was encoded compressed.
248 *
249 * SpecifiedECDomain ::= SEQUENCE {
250 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
251 * fieldID FieldID {{FieldTypes}},
252 * curve Curve,
253 * base ECPoint,
254 * order INTEGER,
255 * cofactor INTEGER OPTIONAL,
256 * hash HashAlgorithm OPTIONAL,
257 * ...
258 * }
259 *
260 * We only support prime-field as field type, and ignore hash and cofactor.
261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100263{
264 int ret;
265 unsigned char *p = params->p;
266 const unsigned char * const end = params->p + params->len;
267 const unsigned char *end_field, *end_curve;
268 size_t len;
269 int ver;
270
271 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
273 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100274
275 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100277
278 /*
279 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
280 * fieldType FIELD-ID.&id({IOSet}),
281 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
282 * }
283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
285 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100286 return( ret );
287
288 end_field = p + len;
289
290 /*
291 * FIELD-ID ::= TYPE-IDENTIFIER
292 * FieldTypes FIELD-ID ::= {
293 * { Prime-p IDENTIFIED BY prime-field } |
294 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
295 * }
296 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100299 return( ret );
300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
302 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100305 }
306
307 p += len;
308
309 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
311 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200313 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100314
315 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
317 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100318
319 /*
320 * Curve ::= SEQUENCE {
321 * a FieldElement,
322 * b FieldElement,
323 * seed BIT STRING OPTIONAL
324 * -- Shall be present if used in SpecifiedECDomain
325 * -- with version equal to ecdpVer2 or ecdpVer3
326 * }
327 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
329 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100330 return( ret );
331
332 end_curve = p + len;
333
334 /*
335 * FieldElement ::= OCTET STRING
336 * containing an integer in the case of a prime field
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
339 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100342 }
343
344 p += len;
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
347 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100350 }
351
352 p += len;
353
354 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100356 p += len;
357
358 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
360 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100361
362 /*
363 * ECPoint ::= OCTET STRING
364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
366 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100369 ( const unsigned char *) p, len ) ) != 0 )
370 {
371 /*
372 * If we can't read the point because it's compressed, cheat by
373 * reading only the X coordinate and the parity bit of Y.
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100376 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 len != mbedtls_mpi_size( &grp->P ) + 1 ||
378 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
379 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
380 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100383 }
384 }
385
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100386 p += len;
387
388 /*
389 * order INTEGER
390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
392 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200394 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395
396 /*
397 * Allow optional elements by purposefully not enforcing p == end here.
398 */
399
400 return( 0 );
401}
402
403/*
404 * Find the group id associated with an (almost filled) group as generated by
405 * pk_group_from_specified(), or return an error if unknown.
406 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407static 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 +0100408{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100409 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_ecp_group ref;
411 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100416 {
417 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200419 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100420
421 /* Compare to the group we were given, starting with easy tests */
422 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
424 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
425 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
426 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
427 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
428 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 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 +0100431 {
432 break;
433 }
434
435 }
436
437cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
440 *grp_id = *id;
441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
443 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100444
445 return( ret );
446}
447
448/*
449 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
450 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
452 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100453{
454 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100458
459 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
460 goto cleanup;
461
462 ret = pk_group_id_from_group( &grp, grp_id );
463
464cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
467 return( ret );
468}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100470
471/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200472 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100473 *
474 * ECParameters ::= CHOICE {
475 * namedCurve OBJECT IDENTIFIER
476 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
477 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200478 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200480{
481 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
487 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100488 }
489 else
490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
493 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100494#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100496#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100497 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498
499 /*
500 * grp may already be initilialized; if so, make sure IDs match
501 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
503 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200505 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506 return( ret );
507
508 return( 0 );
509}
510
511/*
512 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100513 *
514 * The caller is responsible for clearing the structure upon failure if
515 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200516 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517 */
518static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200520{
521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100524 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527 }
528
529 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200531 */
532 *p = (unsigned char *) end;
533
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100534 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200535}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200539/*
540 * RSAPublicKey ::= SEQUENCE {
541 * modulus INTEGER, -- n
542 * publicExponent INTEGER -- e
543 * }
544 */
545static int pk_get_rsapubkey( unsigned char **p,
546 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200548{
549 int ret;
550 size_t len;
551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200552 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
553 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
554 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555
556 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200557 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
558 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200559
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100560 /* Import N */
561 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200563
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100564 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
565 NULL, 0, NULL, 0 ) ) != 0 )
566 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
567
568 *p += len;
569
570 /* Import E */
571 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
572 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
573
574 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
575 NULL, 0, *p, len ) ) != 0 )
576 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
577
578 *p += len;
579
Hanno Becker895c5ab2018-01-05 08:08:09 +0000580 if( mbedtls_rsa_complete( rsa ) != 0 ||
581 mbedtls_rsa_check_pubkey( rsa ) != 0 )
582 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100583 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000584 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100585
Paul Bakker1a7550a2013-09-15 13:01:22 +0200586 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
588 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589
Paul Bakker1a7550a2013-09-15 13:01:22 +0200590 return( 0 );
591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593
594/* Get a PK algorithm identifier
595 *
596 * AlgorithmIdentifier ::= SEQUENCE {
597 * algorithm OBJECT IDENTIFIER,
598 * parameters ANY DEFINED BY algorithm OPTIONAL }
599 */
600static int pk_get_pk_alg( unsigned char **p,
601 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200603{
604 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
610 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
613 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200614
615 /*
616 * No parameters with RSA (only for EC)
617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 if( *pk_alg == MBEDTLS_PK_RSA &&
619 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620 params->len != 0 ) )
621 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623 }
624
625 return( 0 );
626}
627
628/*
629 * SubjectPublicKeyInfo ::= SEQUENCE {
630 * algorithm AlgorithmIdentifier,
631 * subjectPublicKey BIT STRING }
632 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
634 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635{
636 int ret;
637 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200638 mbedtls_asn1_buf alg_params;
639 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
640 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
643 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646 }
647
648 end = *p + len;
649
650 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
651 return( ret );
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
654 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200655
656 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
658 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
661 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200663 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664 return( ret );
665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#if defined(MBEDTLS_RSA_C)
667 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200670 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_RSA_C */
672#if defined(MBEDTLS_ECP_C)
673 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200678 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#endif /* MBEDTLS_ECP_C */
680 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200681
682 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
684 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685
686 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688
689 return( ret );
690}
691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200692#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693/*
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100694 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
695 *
696 * The value zero is:
697 * - never a valid value for an RSA parameter
698 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
699 *
700 * Since values can't be omitted in PKCS#1, passing a zero value to
701 * rsa_complete() would be incorrect, so reject zero values early.
702 */
703static int asn1_get_nonzero_mpi( unsigned char **p,
704 const unsigned char *end,
705 mbedtls_mpi *X )
706{
707 int ret;
708
709 ret = mbedtls_asn1_get_mpi( p, end, X );
710 if( ret != 0 )
711 return( ret );
712
713 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
714 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
715
716 return( 0 );
717}
718
719/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720 * Parse a PKCS#1 encoded private RSA key
721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200722static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723 const unsigned char *key,
724 size_t keylen )
725{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100726 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 size_t len;
728 unsigned char *p, *end;
729
Hanno Beckerefa14e82017-10-11 19:45:19 +0100730 mbedtls_mpi T;
731 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100732
Paul Bakker1a7550a2013-09-15 13:01:22 +0200733 p = (unsigned char *) key;
734 end = p + keylen;
735
736 /*
737 * This function parses the RSAPrivateKey (PKCS#1)
738 *
739 * RSAPrivateKey ::= SEQUENCE {
740 * version Version,
741 * modulus INTEGER, -- n
742 * publicExponent INTEGER, -- e
743 * privateExponent INTEGER, -- d
744 * prime1 INTEGER, -- p
745 * prime2 INTEGER, -- q
746 * exponent1 INTEGER, -- d mod (p-1)
747 * exponent2 INTEGER, -- d mod (q-1)
748 * coefficient INTEGER, -- (inverse of q) mod p
749 * otherPrimeInfos OtherPrimeInfos OPTIONAL
750 * }
751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200752 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
753 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200755 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200756 }
757
758 end = p + len;
759
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200763 }
764
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100765 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768 }
769
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100770 /* Import N */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100771 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
772 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
773 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100774 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100776 /* Import E */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100777 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
778 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
779 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100780 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100781
782 /* Import D */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100783 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
784 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
785 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100786 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100787
788 /* Import P */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100789 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
790 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
791 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100792 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100793
794 /* Import Q */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100795 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
796 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
797 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100798 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100799
Manuel Pégourié-Gonnard609d79e2020-02-18 10:22:54 +0100800#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd100e1472020-01-29 13:13:04 -0500801 /*
802 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
803 * that they can be easily recomputed from D, P and Q. However by
804 * parsing them from the PKCS1 structure it is possible to avoid
805 * recalculating them which both reduces the overhead of loading
806 * RSA private keys into memory and also avoids side channels which
807 * can arise when computing those values, since all of D, P, and Q
808 * are secret. See https://eprint.iacr.org/2020/055 for a
809 * description of one such attack.
810 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100811
Jack Lloyd100e1472020-01-29 13:13:04 -0500812 /* Import DP */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100813 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
814 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500815 goto cleanup;
816
817 /* Import DQ */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100818 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
819 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500820 goto cleanup;
821
822 /* Import QP */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100823 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
824 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500825 goto cleanup;
826
827#else
828 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100829 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
830 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
831 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500832 goto cleanup;
833#endif
834
Manuel Pégourié-Gonnard869e9662020-02-14 11:28:47 +0100835 /* rsa_complete() doesn't complete anything with the default
836 * implementation but is still called:
837 * - for the benefit of alternative implementation that may want to
838 * pre-compute stuff beyond what's provided (eg Montgomery factors)
839 * - as is also sanity-checks the key
840 *
841 * Furthermore, we also check the public part for consistency with
842 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
843 */
844 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
845 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
846 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100847 goto cleanup;
Manuel Pégourié-Gonnard869e9662020-02-14 11:28:47 +0100848 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849
850 if( p != end )
851 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100852 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
853 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854 }
855
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100856cleanup:
857
Hanno Beckerefa14e82017-10-11 19:45:19 +0100858 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100859
860 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100862 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100863 if( ( ret & 0xff80 ) == 0 )
864 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
865 else
866 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200868 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869 }
870
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100871 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200872}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200875#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876/*
877 * Parse a SEC1 encoded private EC key
878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200880 const unsigned char *key,
881 size_t keylen )
882{
883 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100884 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 unsigned char *p = (unsigned char *) key;
888 unsigned char *end = p + keylen;
889 unsigned char *end2;
890
891 /*
892 * RFC 5915, or SEC1 Appendix C.4
893 *
894 * ECPrivateKey ::= SEQUENCE {
895 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
896 * privateKey OCTET STRING,
897 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
898 * publicKey [1] BIT STRING OPTIONAL
899 * }
900 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200901 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
902 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200905 }
906
907 end = p + len;
908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
910 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200911
912 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200915 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
916 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_ecp_keypair_free( eck );
921 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200922 }
923
924 p += len;
925
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200926 pubkey_done = 0;
927 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200928 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200929 /*
930 * Is 'parameters' present?
931 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200932 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
933 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200934 {
935 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
936 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
937 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200938 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200939 return( ret );
940 }
941 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200942 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200943 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200944 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100946 }
Jethro Beekman004e3712018-02-16 13:11:04 -0800947 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200948
Jethro Beekman004e3712018-02-16 13:11:04 -0800949 if( p != end )
950 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200951 /*
952 * Is 'publickey' present? If not, or if we can't read it (eg because it
953 * is compressed), create it from the private key.
954 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200955 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
956 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200957 {
958 end2 = p + len;
959
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200960 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
961 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200962
963 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200964 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
965 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200966
967 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
968 pubkey_done = 1;
969 else
970 {
971 /*
972 * The only acceptable failure mode of pk_get_ecpubkey() above
973 * is if the point format is not recognized.
974 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200975 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
976 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200977 }
978 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200979 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200980 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200981 mbedtls_ecp_keypair_free( eck );
982 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200983 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100985
986 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100988 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200989 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200990 mbedtls_ecp_keypair_free( eck );
991 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200992 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200997 return( ret );
998 }
999
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001000 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003
1004/*
1005 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001006 *
1007 * Notes:
1008 *
1009 * - This function does not own the key buffer. It is the
1010 * responsibility of the caller to take care of zeroizing
1011 * and freeing it after use.
1012 *
1013 * - The function is responsible for freeing the provided
1014 * PK context on failure.
1015 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 */
1017static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019 const unsigned char* key,
1020 size_t keylen )
1021{
1022 int ret, version;
1023 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 unsigned char *p = (unsigned char *) key;
1026 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1028 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029
1030 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001031 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032 *
1033 * PrivateKeyInfo ::= SEQUENCE {
1034 * version Version,
1035 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1036 * privateKey PrivateKey,
1037 * attributes [0] IMPLICIT Attributes OPTIONAL }
1038 *
1039 * Version ::= INTEGER
1040 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1041 * PrivateKey ::= OCTET STRING
1042 *
1043 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1044 */
1045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1047 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050 }
1051
1052 end = p + len;
1053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1055 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056
1057 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059
1060 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1064 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065
1066 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001067 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1068 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1071 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001072
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001073 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001074 return( ret );
1075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076#if defined(MBEDTLS_RSA_C)
1077 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082 return( ret );
1083 }
1084 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#endif /* MBEDTLS_RSA_C */
1086#if defined(MBEDTLS_ECP_C)
1087 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1090 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001092 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093 return( ret );
1094 }
1095 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_ECP_C */
1097 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001099 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100}
1101
1102/*
1103 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001104 *
1105 * To save space, the decryption happens in-place on the given key buffer.
1106 * Also, while this function may modify the keybuffer, it doesn't own it,
1107 * and instead it is the responsibility of the caller to zeroize and properly
1108 * free it after use.
1109 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001114 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001115 const unsigned char *pwd, size_t pwdlen )
1116{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001117 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001119 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1122#if defined(MBEDTLS_PKCS12_C)
1123 mbedtls_cipher_type_t cipher_alg;
1124 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125#endif
1126
Hanno Becker2aa80a72017-09-07 15:28:45 +01001127 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 end = p + keylen;
1129
1130 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132
1133 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001134 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001135 *
1136 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1137 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1138 * encryptedData EncryptedData
1139 * }
1140 *
1141 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1142 *
1143 * EncryptedData ::= OCTET STRING
1144 *
1145 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001146 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1149 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152 }
1153
1154 end = p + len;
1155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001156 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1157 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001159 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1160 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161
Hanno Beckerfab35692017-08-25 13:38:26 +01001162 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163
1164 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001165 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001167#if defined(MBEDTLS_PKCS12_C)
1168 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001171 cipher_alg, md_alg,
1172 pwd, pwdlen, p, len, buf ) ) != 0 )
1173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1175 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176
1177 return( ret );
1178 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001179
1180 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1185 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 pwd, pwdlen,
1187 p, len, buf ) ) != 0 )
1188 {
1189 return( ret );
1190 }
1191
1192 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1196 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001197
1198 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 }
1200 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#endif /* MBEDTLS_PKCS12_C */
1202#if defined(MBEDTLS_PKCS5_C)
1203 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206 p, len, buf ) ) != 0 )
1207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1209 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001210
1211 return( ret );
1212 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001213
1214 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 }
1216 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001218 {
1219 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001220 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001221
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001222 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001223 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001224
Paul Bakker1a7550a2013-09-15 13:01:22 +02001225 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228
1229/*
1230 * Parse a private key
1231 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001233 const unsigned char *key, size_t keylen,
1234 const unsigned char *pwd, size_t pwdlen )
1235{
1236 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001246 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001247 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001248 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1249 else
1250 ret = mbedtls_pem_read_buffer( &pem,
1251 "-----BEGIN RSA PRIVATE KEY-----",
1252 "-----END RSA PRIVATE KEY-----",
1253 key, pwd, pwdlen, &len );
1254
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255 if( ret == 0 )
1256 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001257 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001258 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001260 pem.buf, pem.buflen ) ) != 0 )
1261 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263 }
1264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266 return( ret );
1267 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1269 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1270 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1271 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1272 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001277 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001278 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001279 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1280 else
1281 ret = mbedtls_pem_read_buffer( &pem,
1282 "-----BEGIN EC PRIVATE KEY-----",
1283 "-----END EC PRIVATE KEY-----",
1284 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001285 if( ret == 0 )
1286 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001287 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288
Hanno Beckerfab35692017-08-25 13:38:26 +01001289 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291 pem.buf, pem.buflen ) ) != 0 )
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 }
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 return( ret );
1298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1300 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1301 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1302 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1303 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001307 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001308 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001309 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1310 else
1311 ret = mbedtls_pem_read_buffer( &pem,
1312 "-----BEGIN PRIVATE KEY-----",
1313 "-----END PRIVATE KEY-----",
1314 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001315 if( ret == 0 )
1316 {
1317 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1318 pem.buf, pem.buflen ) ) != 0 )
1319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 }
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324 return( ret );
1325 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327 return( ret );
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001330 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001331 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001332 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1333 else
1334 ret = mbedtls_pem_read_buffer( &pem,
1335 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1336 "-----END ENCRYPTED PRIVATE KEY-----",
1337 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 if( ret == 0 )
1339 {
1340 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1341 pem.buf, pem.buflen,
1342 pwd, pwdlen ) ) != 0 )
1343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345 }
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348 return( ret );
1349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001352#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001353#else
1354 ((void) pwd);
1355 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
1358 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001359 * At this point we only know it's not a PEM formatted key. Could be any
1360 * of the known DER encoded private key formats
1361 *
1362 * We try the different DER format parsers to see if one passes without
1363 * error
1364 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001367 unsigned char *key_copy;
1368
Andres Amaya Garciae9124b92018-01-23 20:03:52 +00001369 if( keylen == 0 )
1370 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1371
Hanno Beckerfab35692017-08-25 13:38:26 +01001372 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1373 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1374
1375 memcpy( key_copy, key, keylen );
1376
1377 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1378 pwd, pwdlen );
1379
1380 mbedtls_zeroize( key_copy, keylen );
1381 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382 }
1383
Hanno Beckerfab35692017-08-25 13:38:26 +01001384 if( ret == 0 )
1385 return( 0 );
1386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001388 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391 {
1392 return( ret );
1393 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001395
1396 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1397 return( 0 );
1398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001399 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001400 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001403
Hanno Becker9be19262017-09-08 12:39:44 +01001404 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001405 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1406 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 {
1408 return( 0 );
1409 }
1410
Hanno Becker5cc4f762018-10-10 11:23:33 +01001411 mbedtls_pk_free( pk );
1412 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001416 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001417 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1418 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1419 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001420 {
1421 return( 0 );
1422 }
Hanno Becker5cc4f762018-10-10 11:23:33 +01001423 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425
Hanno Becker5cc4f762018-10-10 11:23:33 +01001426 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1427 * it is ok to leave the PK context initialized but not
1428 * freed: It is the caller's responsibility to call pk_init()
1429 * before calling this function, and to call pk_free()
1430 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1431 * isn't, this leads to mbedtls_pk_free() being called
1432 * twice, once here and once by the caller, but this is
1433 * also ok and in line with the mbedtls_pk_free() calls
1434 * on failed PEM parsing attempts. */
1435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001437}
1438
1439/*
1440 * Parse a public key
1441 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001443 const unsigned char *key, size_t keylen )
1444{
1445 int ret;
1446 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452
1453 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001454 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1456 else
1457 ret = mbedtls_pem_read_buffer( &pem,
1458 "-----BEGIN PUBLIC KEY-----",
1459 "-----END PUBLIC KEY-----",
1460 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001461
1462 if( ret == 0 )
1463 {
1464 /*
1465 * Was PEM encoded
1466 */
1467 key = pem.buf;
1468 keylen = pem.buflen;
1469 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473 return( ret );
1474 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 p = (unsigned char *) key;
1477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_PEM_PARSE_C)
1481 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001482#endif
1483
1484 return( ret );
1485}
1486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_PK_PARSE_C */