blob: 086807d836379d27636e928fbb867a129da259b3 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
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 * **********
Paul Bakker1a7550a2013-09-15 13:01:22 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/pk.h"
56#include "mbedtls/asn1.h"
57#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050058#include "mbedtls/platform_util.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020059
Rich Evans00ab4702015-02-06 13:43:58 +000060#include <string.h>
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020064#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020070#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000072#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020073#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000075#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020076#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000078#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020079#endif
80
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000082#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020083#else
84#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020085#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020087#endif
88
Gilles Peskinee97dc602018-12-19 00:51:38 +010089/* Parameter validation macros based on platform_util.h */
90#define PK_VALIDATE_RET( cond ) \
91 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
92#define PK_VALIDATE( cond ) \
93 MBEDTLS_INTERNAL_VALIDATE( cond )
94
Gilles Peskine832f3492017-11-30 11:42:12 +010095#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020096/*
97 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020098 *
99 * The file is expected to contain either PEM or DER encoded data.
100 * A terminating null byte is always appended. It is included in the announced
101 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200104{
105 FILE *f;
106 long size;
107
Gilles Peskinee97dc602018-12-19 00:51:38 +0100108 PK_VALIDATE_RET( path != NULL );
109 PK_VALIDATE_RET( buf != NULL );
110 PK_VALIDATE_RET( n != NULL );
111
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114
115 fseek( f, 0, SEEK_END );
116 if( ( size = ftell( f ) ) == -1 )
117 {
118 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200120 }
121 fseek( f, 0, SEEK_SET );
122
123 *n = (size_t) size;
124
125 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200126 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127 {
128 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200129 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130 }
131
132 if( fread( *buf, 1, *n, f ) != *n )
133 {
134 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100135
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500136 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140 }
141
142 fclose( f );
143
144 (*buf)[*n] = '\0';
145
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200146 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
147 ++*n;
148
Paul Bakker1a7550a2013-09-15 13:01:22 +0200149 return( 0 );
150}
151
152/*
153 * Load and parse a private key
154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156 const char *path, const char *pwd )
157{
158 int ret;
159 size_t n;
160 unsigned char *buf;
161
Gilles Peskinee97dc602018-12-19 00:51:38 +0100162 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100163 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166 return( ret );
167
168 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 (const unsigned char *) pwd, strlen( pwd ) );
173
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500174 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176
177 return( ret );
178}
179
180/*
181 * Load and parse a public key
182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184{
185 int ret;
186 size_t n;
187 unsigned char *buf;
188
Gilles Peskinee97dc602018-12-19 00:51:38 +0100189 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100190 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200193 return( ret );
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200196
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500197 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
200 return( ret );
201}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_ECP_C)
205/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206 *
207 * ECParameters ::= CHOICE {
208 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100209 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200211 * }
212 */
213static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200215{
216 int ret;
217
Sanne Woudab2b29d52017-08-21 15:58:12 +0100218 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100219 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
220 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100221
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100222 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200223 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 if( params->tag != MBEDTLS_ASN1_OID
225#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
226 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100227#endif
228 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
231 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100232 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100237 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200238
239 params->p = *p;
240 *p += params->len;
241
242 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
244 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200245
246 return( 0 );
247}
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200250/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100251 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
252 * WARNING: the resulting group should only be used with
253 * pk_group_id_from_specified(), since its base point may not be set correctly
254 * if it was encoded compressed.
255 *
256 * SpecifiedECDomain ::= SEQUENCE {
257 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
258 * fieldID FieldID {{FieldTypes}},
259 * curve Curve,
260 * base ECPoint,
261 * order INTEGER,
262 * cofactor INTEGER OPTIONAL,
263 * hash HashAlgorithm OPTIONAL,
264 * ...
265 * }
266 *
267 * We only support prime-field as field type, and ignore hash and cofactor.
268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100270{
271 int ret;
272 unsigned char *p = params->p;
273 const unsigned char * const end = params->p + params->len;
274 const unsigned char *end_field, *end_curve;
275 size_t len;
276 int ver;
277
278 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
280 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100281
282 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284
285 /*
286 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
287 * fieldType FIELD-ID.&id({IOSet}),
288 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
289 * }
290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
292 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100293 return( ret );
294
295 end_field = p + len;
296
297 /*
298 * FIELD-ID ::= TYPE-IDENTIFIER
299 * FieldTypes FIELD-ID ::= {
300 * { Prime-p IDENTIFIED BY prime-field } |
301 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
302 * }
303 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100306 return( ret );
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
309 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312 }
313
314 p += len;
315
316 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
318 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100319
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200320 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321
322 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
324 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100325
326 /*
327 * Curve ::= SEQUENCE {
328 * a FieldElement,
329 * b FieldElement,
330 * seed BIT STRING OPTIONAL
331 * -- Shall be present if used in SpecifiedECDomain
332 * -- with version equal to ecdpVer2 or ecdpVer3
333 * }
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
336 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100337 return( ret );
338
339 end_curve = p + len;
340
341 /*
342 * FieldElement ::= OCTET STRING
343 * containing an integer in the case of a prime field
344 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
346 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100349 }
350
351 p += len;
352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
354 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100357 }
358
359 p += len;
360
361 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100363 p += len;
364
365 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
367 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
369 /*
370 * ECPoint ::= OCTET STRING
371 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
373 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100376 ( const unsigned char *) p, len ) ) != 0 )
377 {
378 /*
379 * If we can't read the point because it's compressed, cheat by
380 * reading only the X coordinate and the parity bit of Y.
381 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200382 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100383 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 len != mbedtls_mpi_size( &grp->P ) + 1 ||
385 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
386 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
387 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100390 }
391 }
392
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393 p += len;
394
395 /*
396 * order INTEGER
397 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
399 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100400
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200401 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402
403 /*
404 * Allow optional elements by purposefully not enforcing p == end here.
405 */
406
407 return( 0 );
408}
409
410/*
411 * Find the group id associated with an (almost filled) group as generated by
412 * pk_group_from_specified(), or return an error if unknown.
413 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414static 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 +0100415{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100416 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_ecp_group ref;
418 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100423 {
424 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200426 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100427
428 /* Compare to the group we were given, starting with easy tests */
429 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
431 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
432 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
433 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
434 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
435 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100436 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 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 +0100438 {
439 break;
440 }
441
442 }
443
444cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200445 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446
447 *grp_id = *id;
448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
450 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451
452 return( ret );
453}
454
455/*
456 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
459 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100460{
461 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100465
466 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
467 goto cleanup;
468
469 ret = pk_group_id_from_group( &grp, grp_id );
470
471cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100473
474 return( ret );
475}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100477
478/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100480 *
481 * ECParameters ::= CHOICE {
482 * namedCurve OBJECT IDENTIFIER
483 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
484 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200485 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200487{
488 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
494 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100495 }
496 else
497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100499 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
500 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100501#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100503#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100504 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200505
506 /*
507 * grp may already be initilialized; if so, make sure IDs match
508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
510 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200511
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200512 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513 return( ret );
514
515 return( 0 );
516}
517
518/*
519 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100520 *
521 * The caller is responsible for clearing the structure upon failure if
522 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200524 */
525static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527{
528 int ret;
529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100531 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200534 }
535
536 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200537 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200538 */
539 *p = (unsigned char *) end;
540
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100541 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200542}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200543#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200546/*
547 * RSAPublicKey ::= SEQUENCE {
548 * modulus INTEGER, -- n
549 * publicExponent INTEGER -- e
550 * }
551 */
552static int pk_get_rsapubkey( unsigned char **p,
553 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200555{
556 int ret;
557 size_t len;
558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
560 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
561 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200562
563 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200564 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
565 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200566
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100567 /* Import N */
568 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200570
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100571 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
572 NULL, 0, NULL, 0 ) ) != 0 )
573 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
574
575 *p += len;
576
577 /* Import E */
578 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
579 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
580
581 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
582 NULL, 0, *p, len ) ) != 0 )
583 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
584
585 *p += len;
586
Hanno Becker895c5ab2018-01-05 08:08:09 +0000587 if( mbedtls_rsa_complete( rsa ) != 0 ||
588 mbedtls_rsa_check_pubkey( rsa ) != 0 )
589 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100590 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000591 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100592
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
595 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200596
Paul Bakker1a7550a2013-09-15 13:01:22 +0200597 return( 0 );
598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200600
601/* Get a PK algorithm identifier
602 *
603 * AlgorithmIdentifier ::= SEQUENCE {
604 * algorithm OBJECT IDENTIFIER,
605 * parameters ANY DEFINED BY algorithm OPTIONAL }
606 */
607static int pk_get_pk_alg( unsigned char **p,
608 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200610{
611 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
617 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
620 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200621
622 /*
623 * No parameters with RSA (only for EC)
624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 if( *pk_alg == MBEDTLS_PK_RSA &&
626 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200627 params->len != 0 ) )
628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630 }
631
632 return( 0 );
633}
634
635/*
636 * SubjectPublicKeyInfo ::= SEQUENCE {
637 * algorithm AlgorithmIdentifier,
638 * subjectPublicKey BIT STRING }
639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
641 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642{
643 int ret;
644 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_asn1_buf alg_params;
646 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
647 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648
Gilles Peskinee97dc602018-12-19 00:51:38 +0100649 PK_VALIDATE_RET( p != NULL );
650 PK_VALIDATE_RET( *p != NULL );
651 PK_VALIDATE_RET( end != NULL );
652 PK_VALIDATE_RET( pk != NULL );
653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200654 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
655 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200657 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658 }
659
660 end = *p + len;
661
662 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
663 return( ret );
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
666 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200667
668 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
670 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
673 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200675 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676 return( ret );
677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200678#if defined(MBEDTLS_RSA_C)
679 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200681 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200682 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683#endif /* MBEDTLS_RSA_C */
684#if defined(MBEDTLS_ECP_C)
685 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200688 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200690 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200691#endif /* MBEDTLS_ECP_C */
692 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693
694 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
696 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200697
698 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700
701 return( ret );
702}
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200705/*
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100706 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
707 *
708 * The value zero is:
709 * - never a valid value for an RSA parameter
710 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
711 *
712 * Since values can't be omitted in PKCS#1, passing a zero value to
713 * rsa_complete() would be incorrect, so reject zero values early.
714 */
715static int asn1_get_nonzero_mpi( unsigned char **p,
716 const unsigned char *end,
717 mbedtls_mpi *X )
718{
719 int ret;
720
721 ret = mbedtls_asn1_get_mpi( p, end, X );
722 if( ret != 0 )
723 return( ret );
724
725 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
726 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
727
728 return( 0 );
729}
730
731/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200732 * Parse a PKCS#1 encoded private RSA key
733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200734static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735 const unsigned char *key,
736 size_t keylen )
737{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100738 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739 size_t len;
740 unsigned char *p, *end;
741
Hanno Beckerefa14e82017-10-11 19:45:19 +0100742 mbedtls_mpi T;
743 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100744
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745 p = (unsigned char *) key;
746 end = p + keylen;
747
748 /*
749 * This function parses the RSAPrivateKey (PKCS#1)
750 *
751 * RSAPrivateKey ::= SEQUENCE {
752 * version Version,
753 * modulus INTEGER, -- n
754 * publicExponent INTEGER, -- e
755 * privateExponent INTEGER, -- d
756 * prime1 INTEGER, -- p
757 * prime2 INTEGER, -- q
758 * exponent1 INTEGER, -- d mod (p-1)
759 * exponent2 INTEGER, -- d mod (q-1)
760 * coefficient INTEGER, -- (inverse of q) mod p
761 * otherPrimeInfos OtherPrimeInfos OPTIONAL
762 * }
763 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200764 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
765 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200768 }
769
770 end = p + len;
771
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200775 }
776
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100777 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200780 }
781
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100782 /* Import N */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100783 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
784 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
785 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100786 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200787
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100788 /* Import E */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100789 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
790 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
791 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100792 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100793
794 /* Import D */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100795 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
796 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
797 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100798 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100799
800 /* Import P */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100801 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
802 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
803 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100804 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100805
806 /* Import Q */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100807 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
808 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
809 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100810 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100811
Manuel Pégourié-Gonnardd09fcde2020-02-18 10:22:54 +0100812#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloydb10fd062020-01-29 13:09:55 -0500813 /*
814 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
815 * that they can be easily recomputed from D, P and Q. However by
816 * parsing them from the PKCS1 structure it is possible to avoid
817 * recalculating them which both reduces the overhead of loading
818 * RSA private keys into memory and also avoids side channels which
819 * can arise when computing those values, since all of D, P, and Q
820 * are secret. See https://eprint.iacr.org/2020/055 for a
821 * description of one such attack.
822 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100823
Jack Lloydb10fd062020-01-29 13:09:55 -0500824 /* Import DP */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100825 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
826 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500827 goto cleanup;
828
829 /* Import DQ */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100830 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
831 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500832 goto cleanup;
833
834 /* Import QP */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100835 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
836 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500837 goto cleanup;
838
839#else
840 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100841 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
842 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
843 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500844 goto cleanup;
845#endif
846
Manuel Pégourié-Gonnard25bb8dc2020-02-14 11:28:47 +0100847 /* rsa_complete() doesn't complete anything with the default
848 * implementation but is still called:
849 * - for the benefit of alternative implementation that may want to
850 * pre-compute stuff beyond what's provided (eg Montgomery factors)
851 * - as is also sanity-checks the key
852 *
853 * Furthermore, we also check the public part for consistency with
854 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
855 */
856 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
857 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
858 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100859 goto cleanup;
Manuel Pégourié-Gonnard25bb8dc2020-02-14 11:28:47 +0100860 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861
862 if( p != end )
863 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100864 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
865 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200866 }
867
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100868cleanup:
869
Hanno Beckerefa14e82017-10-11 19:45:19 +0100870 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100871
872 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200873 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100874 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100875 if( ( ret & 0xff80 ) == 0 )
876 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
877 else
878 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 }
882
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100883 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200888/*
889 * Parse a SEC1 encoded private EC key
890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892 const unsigned char *key,
893 size_t keylen )
894{
895 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100896 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200897 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200899 unsigned char *p = (unsigned char *) key;
900 unsigned char *end = p + keylen;
901 unsigned char *end2;
902
903 /*
904 * RFC 5915, or SEC1 Appendix C.4
905 *
906 * ECPrivateKey ::= SEQUENCE {
907 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
908 * privateKey OCTET STRING,
909 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
910 * publicKey [1] BIT STRING OPTIONAL
911 * }
912 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
914 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917 }
918
919 end = p + len;
920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
922 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200923
924 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200927 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
928 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 mbedtls_ecp_keypair_free( eck );
933 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200934 }
935
936 p += len;
937
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200938 pubkey_done = 0;
939 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200941 /*
942 * Is 'parameters' present?
943 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200944 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
945 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200946 {
947 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
948 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
949 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200950 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200951 return( ret );
952 }
953 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200954 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200955 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200956 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100958 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800959 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200960
Jethro Beekmand2df9362018-02-16 13:11:04 -0800961 if( p != end )
962 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200963 /*
964 * Is 'publickey' present? If not, or if we can't read it (eg because it
965 * is compressed), create it from the private key.
966 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200967 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
968 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200969 {
970 end2 = p + len;
971
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200972 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
973 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200974
975 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200976 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
977 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200978
979 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
980 pubkey_done = 1;
981 else
982 {
983 /*
984 * The only acceptable failure mode of pk_get_ecpubkey() above
985 * is if the point format is not recognized.
986 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200987 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
988 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200989 }
990 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200991 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200992 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200993 mbedtls_ecp_keypair_free( eck );
994 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200995 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200996 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100997
998 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001000 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_ecp_keypair_free( eck );
1003 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001004 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 return( ret );
1010 }
1011
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001012 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015
1016/*
1017 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001018 *
1019 * Notes:
1020 *
1021 * - This function does not own the key buffer. It is the
1022 * responsibility of the caller to take care of zeroizing
1023 * and freeing it after use.
1024 *
1025 * - The function is responsible for freeing the provided
1026 * PK context on failure.
1027 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001028 */
1029static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031 const unsigned char* key,
1032 size_t keylen )
1033{
1034 int ret, version;
1035 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 unsigned char *p = (unsigned char *) key;
1038 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1040 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
1042 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001043 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001044 *
1045 * PrivateKeyInfo ::= SEQUENCE {
1046 * version Version,
1047 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1048 * privateKey PrivateKey,
1049 * attributes [0] IMPLICIT Attributes OPTIONAL }
1050 *
1051 * Version ::= INTEGER
1052 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1053 * PrivateKey ::= OCTET STRING
1054 *
1055 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1056 */
1057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1059 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060 {
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 }
1063
1064 end = p + len;
1065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1067 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001068
1069 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001071
1072 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1076 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001077
1078 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1080 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1083 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001085 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 return( ret );
1087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_RSA_C)
1089 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094 return( ret );
1095 }
1096 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097#endif /* MBEDTLS_RSA_C */
1098#if defined(MBEDTLS_ECP_C)
1099 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1102 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105 return( ret );
1106 }
1107 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108#endif /* MBEDTLS_ECP_C */
1109 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001111 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112}
1113
1114/*
1115 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001116 *
1117 * To save space, the decryption happens in-place on the given key buffer.
1118 * Also, while this function may modify the keybuffer, it doesn't own it,
1119 * and instead it is the responsibility of the caller to zeroize and properly
1120 * free it after use.
1121 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001123#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001126 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001127 const unsigned char *pwd, size_t pwdlen )
1128{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001129 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001131 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001132 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001133 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1134#if defined(MBEDTLS_PKCS12_C)
1135 mbedtls_cipher_type_t cipher_alg;
1136 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137#endif
1138
Hanno Becker2aa80a72017-09-07 15:28:45 +01001139 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001140 end = p + keylen;
1141
1142 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144
1145 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001146 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 *
1148 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1149 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1150 * encryptedData EncryptedData
1151 * }
1152 *
1153 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1154 *
1155 * EncryptedData ::= OCTET STRING
1156 *
1157 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001158 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1161 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001163 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 }
1165
1166 end = p + len;
1167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1169 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1172 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173
Hanno Beckerfab35692017-08-25 13:38:26 +01001174 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175
1176 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001177 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179#if defined(MBEDTLS_PKCS12_C)
1180 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 cipher_alg, md_alg,
1184 pwd, pwdlen, p, len, buf ) ) != 0 )
1185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1187 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188
1189 return( ret );
1190 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001191
1192 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1197 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 pwd, pwdlen,
1199 p, len, buf ) ) != 0 )
1200 {
1201 return( ret );
1202 }
1203
1204 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1208 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001209
1210 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211 }
1212 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213#endif /* MBEDTLS_PKCS12_C */
1214#if defined(MBEDTLS_PKCS5_C)
1215 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218 p, len, buf ) ) != 0 )
1219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001220 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1221 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001222
1223 return( ret );
1224 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001225
1226 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001227 }
1228 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001230 {
1231 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001232 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001233
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001234 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001236
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240
1241/*
1242 * Parse a private key
1243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 const unsigned char *key, size_t keylen,
1246 const unsigned char *pwd, size_t pwdlen )
1247{
1248 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001253#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254
Gilles Peskinee97dc602018-12-19 00:51:38 +01001255 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001256 if( keylen == 0 )
1257 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1258 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001259
1260#if defined(MBEDTLS_PEM_PARSE_C)
1261 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001264 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001265 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001266 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1267 else
1268 ret = mbedtls_pem_read_buffer( &pem,
1269 "-----BEGIN RSA PRIVATE KEY-----",
1270 "-----END RSA PRIVATE KEY-----",
1271 key, pwd, pwdlen, &len );
1272
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273 if( ret == 0 )
1274 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001275 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001276 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 pem.buf, pem.buflen ) ) != 0 )
1279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 }
1282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284 return( ret );
1285 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1287 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1288 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1289 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1290 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001295 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001296 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001297 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1298 else
1299 ret = mbedtls_pem_read_buffer( &pem,
1300 "-----BEGIN EC PRIVATE KEY-----",
1301 "-----END EC PRIVATE KEY-----",
1302 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 if( ret == 0 )
1304 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001305 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306
Hanno Beckerfab35692017-08-25 13:38:26 +01001307 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 pem.buf, pem.buflen ) ) != 0 )
1310 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312 }
1313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001315 return( ret );
1316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1318 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1319 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1320 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1321 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001325 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001326 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001327 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1328 else
1329 ret = mbedtls_pem_read_buffer( &pem,
1330 "-----BEGIN PRIVATE KEY-----",
1331 "-----END PRIVATE KEY-----",
1332 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333 if( ret == 0 )
1334 {
1335 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1336 pem.buf, pem.buflen ) ) != 0 )
1337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339 }
1340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 return( ret );
1343 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001345 return( ret );
1346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001348 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001349 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001350 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1351 else
1352 ret = mbedtls_pem_read_buffer( &pem,
1353 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1354 "-----END ENCRYPTED PRIVATE KEY-----",
1355 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 if( ret == 0 )
1357 {
1358 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1359 pem.buf, pem.buflen,
1360 pwd, pwdlen ) ) != 0 )
1361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363 }
1364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001365 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001366 return( ret );
1367 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001371#else
1372 ((void) pwd);
1373 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375
1376 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001377 * At this point we only know it's not a PEM formatted key. Could be any
1378 * of the known DER encoded private key formats
1379 *
1380 * We try the different DER format parsers to see if one passes without
1381 * error
1382 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001384 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001385 unsigned char *key_copy;
1386
1387 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1388 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1389
1390 memcpy( key_copy, key, keylen );
1391
1392 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1393 pwd, pwdlen );
1394
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001395 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001396 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397 }
1398
Hanno Beckerfab35692017-08-25 13:38:26 +01001399 if( ret == 0 )
1400 return( 0 );
1401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001403 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406 {
1407 return( ret );
1408 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410
1411 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1412 return( 0 );
1413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001415 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418
Hanno Becker9be19262017-09-08 12:39:44 +01001419 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001420 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1421 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422 {
1423 return( 0 );
1424 }
1425
Hanno Becker780f0a42018-10-10 11:23:33 +01001426 mbedtls_pk_free( pk );
1427 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001431 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001432 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1433 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1434 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435 {
1436 return( 0 );
1437 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001438 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001440
Hanno Becker780f0a42018-10-10 11:23:33 +01001441 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1442 * it is ok to leave the PK context initialized but not
1443 * freed: It is the caller's responsibility to call pk_init()
1444 * before calling this function, and to call pk_free()
1445 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1446 * isn't, this leads to mbedtls_pk_free() being called
1447 * twice, once here and once by the caller, but this is
1448 * also ok and in line with the mbedtls_pk_free() calls
1449 * on failed PEM parsing attempts. */
1450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452}
1453
1454/*
1455 * Parse a public key
1456 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458 const unsigned char *key, size_t keylen )
1459{
1460 int ret;
1461 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001462#if defined(MBEDTLS_RSA_C)
1463 const mbedtls_pk_info_t *pk_info;
1464#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001465#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001466 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001467 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001468#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469
Gilles Peskinee97dc602018-12-19 00:51:38 +01001470 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001471 if( keylen == 0 )
1472 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001473 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1474
1475#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001477#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001478 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001479 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001480 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1481 else
1482 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001483 "-----BEGIN RSA PUBLIC KEY-----",
1484 "-----END RSA PUBLIC KEY-----",
1485 key, NULL, 0, &len );
1486
1487 if( ret == 0 )
1488 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001489 p = pem.buf;
1490 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1491 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001492
Ron Eldor84df1ae2017-10-16 17:11:52 +03001493 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1494 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001495
Ron Eldor84df1ae2017-10-16 17:11:52 +03001496 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1497 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001498
Ron Eldord0c56de2017-10-10 17:03:08 +03001499 mbedtls_pem_free( &pem );
1500 return( ret );
1501 }
1502 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1503 {
1504 mbedtls_pem_free( &pem );
1505 return( ret );
1506 }
1507#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001508
1509 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001510 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001511 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1512 else
1513 ret = mbedtls_pem_read_buffer( &pem,
1514 "-----BEGIN PUBLIC KEY-----",
1515 "-----END PUBLIC KEY-----",
1516 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001517
1518 if( ret == 0 )
1519 {
1520 /*
1521 * Was PEM encoded
1522 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001523 p = pem.buf;
1524
1525 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1526 mbedtls_pem_free( &pem );
1527 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532 return( ret );
1533 }
Ron Eldor5472d432017-10-17 09:49:00 +03001534 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001536
1537#if defined(MBEDTLS_RSA_C)
1538 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1539 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1540
1541 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1542 return( ret );
1543
Ron Eldor9566ff72018-02-07 18:59:41 +02001544 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001545 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001546 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001547 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001548 return( ret );
1549 }
1550 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001551 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001552 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001553 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001554 }
1555#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001556 p = (unsigned char *) key;
1557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001559
Paul Bakker1a7550a2013-09-15 13:01:22 +02001560 return( ret );
1561}
1562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#endif /* MBEDTLS_PK_PARSE_C */