blob: f54ed5466d9d59239e45067b729b88bb161af35d [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úti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
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 * **********
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"
Paul Bakker1a7550a2013-09-15 13:01:22 +020058
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <string.h>
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020063#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020066#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020072#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000074#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020075#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000077#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020078#endif
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000081#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020082#else
83#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020084#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020086#endif
87
Gilles Peskine832f3492017-11-30 11:42:12 +010088#if defined(MBEDTLS_FS_IO) || \
89 defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker34617722014-06-13 17:20:13 +020090/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020092 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
93}
Gilles Peskine832f3492017-11-30 11:42:12 +010094#endif
Paul Bakker34617722014-06-13 17:20:13 +020095
Gilles Peskine832f3492017-11-30 11:42:12 +010096#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020097/*
98 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020099 *
100 * The file is expected to contain either PEM or DER encoded data.
101 * A terminating null byte is always appended. It is included in the announced
102 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105{
106 FILE *f;
107 long size;
108
109 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200111
112 fseek( f, 0, SEEK_END );
113 if( ( size = ftell( f ) ) == -1 )
114 {
115 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117 }
118 fseek( f, 0, SEEK_SET );
119
120 *n = (size_t) size;
121
122 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200123 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124 {
125 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200126 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127 }
128
129 if( fread( *buf, 1, *n, f ) != *n )
130 {
131 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100132
133 mbedtls_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137 }
138
139 fclose( f );
140
141 (*buf)[*n] = '\0';
142
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200143 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
144 ++*n;
145
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146 return( 0 );
147}
148
149/*
150 * Load and parse a private key
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153 const char *path, const char *pwd )
154{
155 int ret;
156 size_t n;
157 unsigned char *buf;
158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200160 return( ret );
161
162 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200164 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200166 (const unsigned char *) pwd, strlen( pwd ) );
167
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200168 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200170
171 return( ret );
172}
173
174/*
175 * Load and parse a public key
176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178{
179 int ret;
180 size_t n;
181 unsigned char *buf;
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200184 return( ret );
185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200188 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190
191 return( ret );
192}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_ECP_C)
196/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200197 *
198 * ECParameters ::= CHOICE {
199 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100200 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200201 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200202 * }
203 */
204static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200206{
207 int ret;
208
Sanne Woudab2b29d52017-08-21 15:58:12 +0100209 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100210 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
211 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100212
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100213 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 if( params->tag != MBEDTLS_ASN1_OID
216#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
217 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100218#endif
219 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
222 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100223 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100228 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200229
230 params->p = *p;
231 *p += params->len;
232
233 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
235 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200236
237 return( 0 );
238}
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200241/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100242 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
243 * WARNING: the resulting group should only be used with
244 * pk_group_id_from_specified(), since its base point may not be set correctly
245 * if it was encoded compressed.
246 *
247 * SpecifiedECDomain ::= SEQUENCE {
248 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
249 * fieldID FieldID {{FieldTypes}},
250 * curve Curve,
251 * base ECPoint,
252 * order INTEGER,
253 * cofactor INTEGER OPTIONAL,
254 * hash HashAlgorithm OPTIONAL,
255 * ...
256 * }
257 *
258 * We only support prime-field as field type, and ignore hash and cofactor.
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100261{
262 int ret;
263 unsigned char *p = params->p;
264 const unsigned char * const end = params->p + params->len;
265 const unsigned char *end_field, *end_curve;
266 size_t len;
267 int ver;
268
269 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
271 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100272
273 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100275
276 /*
277 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
278 * fieldType FIELD-ID.&id({IOSet}),
279 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
280 * }
281 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
283 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284 return( ret );
285
286 end_field = p + len;
287
288 /*
289 * FIELD-ID ::= TYPE-IDENTIFIER
290 * FieldTypes FIELD-ID ::= {
291 * { Prime-p IDENTIFIED BY prime-field } |
292 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
293 * }
294 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100297 return( ret );
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
300 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303 }
304
305 p += len;
306
307 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
309 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100310
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200311 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312
313 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
315 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100316
317 /*
318 * Curve ::= SEQUENCE {
319 * a FieldElement,
320 * b FieldElement,
321 * seed BIT STRING OPTIONAL
322 * -- Shall be present if used in SpecifiedECDomain
323 * -- with version equal to ecdpVer2 or ecdpVer3
324 * }
325 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
327 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328 return( ret );
329
330 end_curve = p + len;
331
332 /*
333 * FieldElement ::= OCTET STRING
334 * containing an integer in the case of a prime field
335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
337 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340 }
341
342 p += len;
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
345 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100348 }
349
350 p += len;
351
352 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100354 p += len;
355
356 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
358 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100359
360 /*
361 * ECPoint ::= OCTET STRING
362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
364 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100367 ( const unsigned char *) p, len ) ) != 0 )
368 {
369 /*
370 * If we can't read the point because it's compressed, cheat by
371 * reading only the X coordinate and the parity bit of Y.
372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100374 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 len != mbedtls_mpi_size( &grp->P ) + 1 ||
376 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
377 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
378 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100381 }
382 }
383
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100384 p += len;
385
386 /*
387 * order INTEGER
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
390 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200392 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393
394 /*
395 * Allow optional elements by purposefully not enforcing p == end here.
396 */
397
398 return( 0 );
399}
400
401/*
402 * Find the group id associated with an (almost filled) group as generated by
403 * pk_group_from_specified(), or return an error if unknown.
404 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405static 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 +0100406{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100407 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_ecp_group ref;
409 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100414 {
415 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200417 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418
419 /* Compare to the group we were given, starting with easy tests */
420 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
422 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
423 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
424 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
425 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
426 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100427 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 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 +0100429 {
430 break;
431 }
432
433 }
434
435cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438 *grp_id = *id;
439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
441 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100442
443 return( ret );
444}
445
446/*
447 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
448 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
450 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100451{
452 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100456
457 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
458 goto cleanup;
459
460 ret = pk_group_id_from_group( &grp, grp_id );
461
462cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464
465 return( ret );
466}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468
469/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200470 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100471 *
472 * ECParameters ::= CHOICE {
473 * namedCurve OBJECT IDENTIFIER
474 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
475 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200476 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200478{
479 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
485 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100486 }
487 else
488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100490 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
491 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100492#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100494#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100495 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496
497 /*
498 * grp may already be initilialized; if so, make sure IDs match
499 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
501 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200503 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504 return( ret );
505
506 return( 0 );
507}
508
509/*
510 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100511 *
512 * The caller is responsible for clearing the structure upon failure if
513 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200515 */
516static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518{
519 int ret;
520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100522 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200525 }
526
527 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200529 */
530 *p = (unsigned char *) end;
531
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100532 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537/*
538 * RSAPublicKey ::= SEQUENCE {
539 * modulus INTEGER, -- n
540 * publicExponent INTEGER -- e
541 * }
542 */
543static int pk_get_rsapubkey( unsigned char **p,
544 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200546{
547 int ret;
548 size_t len;
549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
551 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
552 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200553
554 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
556 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200557
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100558 /* Import N */
559 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200561
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100562 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
563 NULL, 0, NULL, 0 ) ) != 0 )
564 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
565
566 *p += len;
567
568 /* Import E */
569 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
570 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
571
572 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
573 NULL, 0, *p, len ) ) != 0 )
574 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
575
576 *p += len;
577
Hanno Becker895c5ab2018-01-05 08:08:09 +0000578 if( mbedtls_rsa_complete( rsa ) != 0 ||
579 mbedtls_rsa_check_pubkey( rsa ) != 0 )
580 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100581 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000582 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100583
Paul Bakker1a7550a2013-09-15 13:01:22 +0200584 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
586 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200587
Paul Bakker1a7550a2013-09-15 13:01:22 +0200588 return( 0 );
589}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200590#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200591
592/* Get a PK algorithm identifier
593 *
594 * AlgorithmIdentifier ::= SEQUENCE {
595 * algorithm OBJECT IDENTIFIER,
596 * parameters ANY DEFINED BY algorithm OPTIONAL }
597 */
598static int pk_get_pk_alg( unsigned char **p,
599 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200601{
602 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
608 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200610 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
611 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200612
613 /*
614 * No parameters with RSA (only for EC)
615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 if( *pk_alg == MBEDTLS_PK_RSA &&
617 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200618 params->len != 0 ) )
619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200621 }
622
623 return( 0 );
624}
625
626/*
627 * SubjectPublicKeyInfo ::= SEQUENCE {
628 * algorithm AlgorithmIdentifier,
629 * subjectPublicKey BIT STRING }
630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
632 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633{
634 int ret;
635 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636 mbedtls_asn1_buf alg_params;
637 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
638 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
641 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200644 }
645
646 end = *p + len;
647
648 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
649 return( ret );
650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
652 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200653
654 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
656 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
659 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200661 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662 return( ret );
663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664#if defined(MBEDTLS_RSA_C)
665 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669#endif /* MBEDTLS_RSA_C */
670#if defined(MBEDTLS_ECP_C)
671 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200675 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200676 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677#endif /* MBEDTLS_ECP_C */
678 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200679
680 if( ret == 0 && *p != end )
Jens Reimann45a59582020-09-22 11:57:16 +0200681 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY +
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200683
684 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200686
687 return( ret );
688}
689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691/*
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100692 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
693 *
694 * The value zero is:
695 * - never a valid value for an RSA parameter
696 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
697 *
698 * Since values can't be omitted in PKCS#1, passing a zero value to
699 * rsa_complete() would be incorrect, so reject zero values early.
700 */
701static int asn1_get_nonzero_mpi( unsigned char **p,
702 const unsigned char *end,
703 mbedtls_mpi *X )
704{
705 int ret;
706
707 ret = mbedtls_asn1_get_mpi( p, end, X );
708 if( ret != 0 )
709 return( ret );
710
711 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
712 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
713
714 return( 0 );
715}
716
717/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200718 * Parse a PKCS#1 encoded private RSA key
719 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200721 const unsigned char *key,
722 size_t keylen )
723{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100724 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 size_t len;
726 unsigned char *p, *end;
727
Hanno Beckerefa14e82017-10-11 19:45:19 +0100728 mbedtls_mpi T;
729 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100730
Paul Bakker1a7550a2013-09-15 13:01:22 +0200731 p = (unsigned char *) key;
732 end = p + keylen;
733
734 /*
735 * This function parses the RSAPrivateKey (PKCS#1)
736 *
737 * RSAPrivateKey ::= SEQUENCE {
738 * version Version,
739 * modulus INTEGER, -- n
740 * publicExponent INTEGER, -- e
741 * privateExponent INTEGER, -- d
742 * prime1 INTEGER, -- p
743 * prime2 INTEGER, -- q
744 * exponent1 INTEGER, -- d mod (p-1)
745 * exponent2 INTEGER, -- d mod (q-1)
746 * coefficient INTEGER, -- (inverse of q) mod p
747 * otherPrimeInfos OtherPrimeInfos OPTIONAL
748 * }
749 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200750 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
751 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200753 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200754 }
755
756 end = p + len;
757
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100758 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200761 }
762
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200764 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200766 }
767
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100768 /* Import N */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100769 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
770 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
771 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200773
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100774 /* Import E */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100775 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
776 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
777 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100778 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100779
780 /* Import D */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100781 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
782 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
783 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100784 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100785
786 /* Import P */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100787 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
788 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
789 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100790 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100791
792 /* Import Q */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100793 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
794 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
795 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100796 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100797
Manuel Pégourié-Gonnard609d79e2020-02-18 10:22:54 +0100798#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd100e1472020-01-29 13:13:04 -0500799 /*
800 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
801 * that they can be easily recomputed from D, P and Q. However by
802 * parsing them from the PKCS1 structure it is possible to avoid
803 * recalculating them which both reduces the overhead of loading
804 * RSA private keys into memory and also avoids side channels which
805 * can arise when computing those values, since all of D, P, and Q
806 * are secret. See https://eprint.iacr.org/2020/055 for a
807 * description of one such attack.
808 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100809
Jack Lloyd100e1472020-01-29 13:13:04 -0500810 /* Import DP */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100811 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
812 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500813 goto cleanup;
814
815 /* Import DQ */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100816 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
817 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500818 goto cleanup;
819
820 /* Import QP */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100821 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
822 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500823 goto cleanup;
824
825#else
826 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100827 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
828 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
829 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500830 goto cleanup;
831#endif
832
Manuel Pégourié-Gonnard869e9662020-02-14 11:28:47 +0100833 /* rsa_complete() doesn't complete anything with the default
834 * implementation but is still called:
835 * - for the benefit of alternative implementation that may want to
836 * pre-compute stuff beyond what's provided (eg Montgomery factors)
837 * - as is also sanity-checks the key
838 *
839 * Furthermore, we also check the public part for consistency with
840 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
841 */
842 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
843 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
844 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100845 goto cleanup;
Manuel Pégourié-Gonnard869e9662020-02-14 11:28:47 +0100846 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200847
848 if( p != end )
849 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100850 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
851 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852 }
853
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100854cleanup:
855
Hanno Beckerefa14e82017-10-11 19:45:19 +0100856 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100857
858 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100860 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100861 if( ( ret & 0xff80 ) == 0 )
862 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
863 else
864 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867 }
868
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100869 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200870}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200871#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874/*
875 * Parse a SEC1 encoded private EC key
876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878 const unsigned char *key,
879 size_t keylen )
880{
881 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100882 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200883 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200884 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200885 unsigned char *p = (unsigned char *) key;
886 unsigned char *end = p + keylen;
887 unsigned char *end2;
888
889 /*
890 * RFC 5915, or SEC1 Appendix C.4
891 *
892 * ECPrivateKey ::= SEQUENCE {
893 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
894 * privateKey OCTET STRING,
895 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
896 * publicKey [1] BIT STRING OPTIONAL
897 * }
898 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
900 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903 }
904
905 end = p + len;
906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
908 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909
910 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200911 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
914 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200916 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 mbedtls_ecp_keypair_free( eck );
919 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200920 }
921
922 p += len;
923
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200924 pubkey_done = 0;
925 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200926 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200927 /*
928 * Is 'parameters' present?
929 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200930 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
931 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200932 {
933 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
934 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
935 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200936 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200937 return( ret );
938 }
939 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200940 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200943 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100944 }
Jethro Beekman004e3712018-02-16 13:11:04 -0800945 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200946
Jethro Beekman004e3712018-02-16 13:11:04 -0800947 if( p != end )
948 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200949 /*
950 * Is 'publickey' present? If not, or if we can't read it (eg because it
951 * is compressed), create it from the private key.
952 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200953 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
954 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200955 {
956 end2 = p + len;
957
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200958 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
959 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200960
961 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200962 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
963 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200964
965 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
966 pubkey_done = 1;
967 else
968 {
969 /*
970 * The only acceptable failure mode of pk_get_ecpubkey() above
971 * is if the point format is not recognized.
972 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200973 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
974 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200975 }
976 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200977 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200978 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200979 mbedtls_ecp_keypair_free( eck );
980 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200981 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100983
984 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100986 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 mbedtls_ecp_keypair_free( eck );
989 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200990 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 return( ret );
996 }
997
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200998 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001
1002/*
1003 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001004 *
1005 * Notes:
1006 *
1007 * - This function does not own the key buffer. It is the
1008 * responsibility of the caller to take care of zeroizing
1009 * and freeing it after use.
1010 *
1011 * - The function is responsible for freeing the provided
1012 * PK context on failure.
1013 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001014 */
1015static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 const unsigned char* key,
1018 size_t keylen )
1019{
1020 int ret, version;
1021 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 unsigned char *p = (unsigned char *) key;
1024 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1026 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027
1028 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001029 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001030 *
1031 * PrivateKeyInfo ::= SEQUENCE {
1032 * version Version,
1033 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1034 * privateKey PrivateKey,
1035 * attributes [0] IMPLICIT Attributes OPTIONAL }
1036 *
1037 * Version ::= INTEGER
1038 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1039 * PrivateKey ::= OCTET STRING
1040 *
1041 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1042 */
1043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1045 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048 }
1049
1050 end = p + len;
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1053 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054
1055 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001057
1058 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001059 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1062 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063
1064 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1066 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1069 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001070
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001071 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001072 return( ret );
1073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074#if defined(MBEDTLS_RSA_C)
1075 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001080 return( ret );
1081 }
1082 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#endif /* MBEDTLS_RSA_C */
1084#if defined(MBEDTLS_ECP_C)
1085 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1088 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001089 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091 return( ret );
1092 }
1093 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094#endif /* MBEDTLS_ECP_C */
1095 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001097 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098}
1099
1100/*
1101 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001102 *
1103 * To save space, the decryption happens in-place on the given key buffer.
1104 * Also, while this function may modify the keybuffer, it doesn't own it,
1105 * and instead it is the responsibility of the caller to zeroize and properly
1106 * free it after use.
1107 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001109#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001110static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001112 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001113 const unsigned char *pwd, size_t pwdlen )
1114{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001115 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001117 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001118 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001119 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1120#if defined(MBEDTLS_PKCS12_C)
1121 mbedtls_cipher_type_t cipher_alg;
1122 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123#endif
1124
Hanno Becker2aa80a72017-09-07 15:28:45 +01001125 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126 end = p + keylen;
1127
1128 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001130
1131 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001132 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001133 *
1134 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1135 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1136 * encryptedData EncryptedData
1137 * }
1138 *
1139 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1140 *
1141 * EncryptedData ::= OCTET STRING
1142 *
1143 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001144 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1147 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 }
1151
1152 end = p + len;
1153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1155 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1158 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159
Hanno Beckerfab35692017-08-25 13:38:26 +01001160 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001161
1162 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001163 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001165#if defined(MBEDTLS_PKCS12_C)
1166 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001169 cipher_alg, md_alg,
1170 pwd, pwdlen, p, len, buf ) ) != 0 )
1171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001172 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1173 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001174
1175 return( ret );
1176 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001177
1178 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1183 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184 pwd, pwdlen,
1185 p, len, buf ) ) != 0 )
1186 {
1187 return( ret );
1188 }
1189
1190 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001192 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1194 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001195
1196 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197 }
1198 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199#endif /* MBEDTLS_PKCS12_C */
1200#if defined(MBEDTLS_PKCS5_C)
1201 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204 p, len, buf ) ) != 0 )
1205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1207 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208
1209 return( ret );
1210 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001211
1212 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 }
1214 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001216 {
1217 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001218 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001219
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001220 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001222
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1224}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226
1227/*
1228 * Parse a private key
1229 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001230int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231 const unsigned char *key, size_t keylen,
1232 const unsigned char *pwd, size_t pwdlen )
1233{
1234 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001237#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001238 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001243#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001244 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001245 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001246 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1247 else
1248 ret = mbedtls_pem_read_buffer( &pem,
1249 "-----BEGIN RSA PRIVATE KEY-----",
1250 "-----END RSA PRIVATE KEY-----",
1251 key, pwd, pwdlen, &len );
1252
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253 if( ret == 0 )
1254 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001255 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001256 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 pem.buf, pem.buflen ) ) != 0 )
1259 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261 }
1262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264 return( ret );
1265 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1267 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1268 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1269 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1270 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001274#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001275 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001276 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001277 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1278 else
1279 ret = mbedtls_pem_read_buffer( &pem,
1280 "-----BEGIN EC PRIVATE KEY-----",
1281 "-----END EC PRIVATE KEY-----",
1282 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 if( ret == 0 )
1284 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001285 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286
Hanno Beckerfab35692017-08-25 13:38:26 +01001287 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001288 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001289 pem.buf, pem.buflen ) ) != 0 )
1290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001292 }
1293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001294 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001295 return( ret );
1296 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1298 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1299 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1300 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1301 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001304
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001305 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001306 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001307 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1308 else
1309 ret = mbedtls_pem_read_buffer( &pem,
1310 "-----BEGIN PRIVATE KEY-----",
1311 "-----END PRIVATE KEY-----",
1312 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001313 if( ret == 0 )
1314 {
1315 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1316 pem.buf, pem.buflen ) ) != 0 )
1317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 }
1320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 return( ret );
1323 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001324 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 return( ret );
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001328 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001329 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001330 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1331 else
1332 ret = mbedtls_pem_read_buffer( &pem,
1333 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1334 "-----END ENCRYPTED PRIVATE KEY-----",
1335 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001336 if( ret == 0 )
1337 {
1338 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1339 pem.buf, pem.buflen,
1340 pwd, pwdlen ) ) != 0 )
1341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343 }
1344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346 return( ret );
1347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001349 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001350#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351#else
1352 ((void) pwd);
1353 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355
1356 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001357 * At this point we only know it's not a PEM formatted key. Could be any
1358 * of the known DER encoded private key formats
1359 *
1360 * We try the different DER format parsers to see if one passes without
1361 * error
1362 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001365 unsigned char *key_copy;
1366
Andres Amaya Garciae9124b92018-01-23 20:03:52 +00001367 if( keylen == 0 )
1368 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1369
Hanno Beckerfab35692017-08-25 13:38:26 +01001370 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1371 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1372
1373 memcpy( key_copy, key, keylen );
1374
1375 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1376 pwd, pwdlen );
1377
1378 mbedtls_zeroize( key_copy, keylen );
1379 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 }
1381
Hanno Beckerfab35692017-08-25 13:38:26 +01001382 if( ret == 0 )
1383 return( 0 );
1384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001386 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001389 {
1390 return( ret );
1391 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393
1394 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1395 return( 0 );
1396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001398 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401
Hanno Becker9be19262017-09-08 12:39:44 +01001402 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001403 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1404 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001405 {
1406 return( 0 );
1407 }
1408
Hanno Becker5cc4f762018-10-10 11:23:33 +01001409 mbedtls_pk_free( pk );
1410 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001414 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001415 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1416 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1417 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001418 {
1419 return( 0 );
1420 }
Hanno Becker5cc4f762018-10-10 11:23:33 +01001421 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423
Hanno Becker5cc4f762018-10-10 11:23:33 +01001424 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1425 * it is ok to leave the PK context initialized but not
1426 * freed: It is the caller's responsibility to call pk_init()
1427 * before calling this function, and to call pk_free()
1428 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1429 * isn't, this leads to mbedtls_pk_free() being called
1430 * twice, once here and once by the caller, but this is
1431 * also ok and in line with the mbedtls_pk_free() calls
1432 * on failed PEM parsing attempts. */
1433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001435}
1436
1437/*
1438 * Parse a public key
1439 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 const unsigned char *key, size_t keylen )
1442{
1443 int ret;
1444 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001449 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450
1451 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001452 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1454 else
1455 ret = mbedtls_pem_read_buffer( &pem,
1456 "-----BEGIN PUBLIC KEY-----",
1457 "-----END PUBLIC KEY-----",
1458 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001459
1460 if( ret == 0 )
1461 {
1462 /*
1463 * Was PEM encoded
1464 */
1465 key = pem.buf;
1466 keylen = pem.buflen;
1467 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001470 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001471 return( ret );
1472 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001474 p = (unsigned char *) key;
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478#if defined(MBEDTLS_PEM_PARSE_C)
1479 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001480#endif
1481
1482 return( ret );
1483}
1484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001485#endif /* MBEDTLS_PK_PARSE_C */