blob: d5004577a1d6ee367b484bf1bb17538b44eb5675 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker1a7550a2013-09-15 13:01:22 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk.h"
31#include "mbedtls/asn1.h"
32#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020058#else
59#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020060#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020062#endif
63
Gilles Peskinee97dc602018-12-19 00:51:38 +010064/* Parameter validation macros based on platform_util.h */
65#define PK_VALIDATE_RET( cond ) \
66 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
67#define PK_VALIDATE( cond ) \
68 MBEDTLS_INTERNAL_VALIDATE( cond )
69
Gilles Peskine832f3492017-11-30 11:42:12 +010070#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020071/*
72 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020073 *
74 * The file is expected to contain either PEM or DER encoded data.
75 * A terminating null byte is always appended. It is included in the announced
76 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020079{
80 FILE *f;
81 long size;
82
Gilles Peskinee97dc602018-12-19 00:51:38 +010083 PK_VALIDATE_RET( path != NULL );
84 PK_VALIDATE_RET( buf != NULL );
85 PK_VALIDATE_RET( n != NULL );
86
Paul Bakker1a7550a2013-09-15 13:01:22 +020087 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020089
90 fseek( f, 0, SEEK_END );
91 if( ( size = ftell( f ) ) == -1 )
92 {
93 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020095 }
96 fseek( f, 0, SEEK_SET );
97
98 *n = (size_t) size;
99
100 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200101 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102 {
103 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200104 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 }
106
107 if( fread( *buf, 1, *n, f ) != *n )
108 {
109 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100110
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500111 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200115 }
116
117 fclose( f );
118
119 (*buf)[*n] = '\0';
120
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200121 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
122 ++*n;
123
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124 return( 0 );
125}
126
127/*
128 * Load and parse a private key
129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131 const char *path, const char *pwd )
132{
133 int ret;
134 size_t n;
135 unsigned char *buf;
136
Gilles Peskinee97dc602018-12-19 00:51:38 +0100137 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100138 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 return( ret );
142
143 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200147 (const unsigned char *) pwd, strlen( pwd ) );
148
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500149 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200151
152 return( ret );
153}
154
155/*
156 * Load and parse a public key
157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159{
160 int ret;
161 size_t n;
162 unsigned char *buf;
163
Gilles Peskinee97dc602018-12-19 00:51:38 +0100164 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100165 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168 return( ret );
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500172 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174
175 return( ret );
176}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_ECP_C)
180/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181 *
182 * ECParameters ::= CHOICE {
183 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100184 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200185 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200186 * }
187 */
188static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200190{
191 int ret;
192
Sanne Woudab2b29d52017-08-21 15:58:12 +0100193 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100194 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
195 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100196
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100197 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200198 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 if( params->tag != MBEDTLS_ASN1_OID
200#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
201 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100202#endif
203 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
206 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100207 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100212 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200213
214 params->p = *p;
215 *p += params->len;
216
217 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
219 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200220
221 return( 0 );
222}
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200225/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100226 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
227 * WARNING: the resulting group should only be used with
228 * pk_group_id_from_specified(), since its base point may not be set correctly
229 * if it was encoded compressed.
230 *
231 * SpecifiedECDomain ::= SEQUENCE {
232 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
233 * fieldID FieldID {{FieldTypes}},
234 * curve Curve,
235 * base ECPoint,
236 * order INTEGER,
237 * cofactor INTEGER OPTIONAL,
238 * hash HashAlgorithm OPTIONAL,
239 * ...
240 * }
241 *
242 * We only support prime-field as field type, and ignore hash and cofactor.
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100245{
246 int ret;
247 unsigned char *p = params->p;
248 const unsigned char * const end = params->p + params->len;
249 const unsigned char *end_field, *end_curve;
250 size_t len;
251 int ver;
252
253 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
255 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100256
257 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259
260 /*
261 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
262 * fieldType FIELD-ID.&id({IOSet}),
263 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
264 * }
265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
267 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100268 return( ret );
269
270 end_field = p + len;
271
272 /*
273 * FIELD-ID ::= TYPE-IDENTIFIER
274 * FieldTypes FIELD-ID ::= {
275 * { Prime-p IDENTIFIED BY prime-field } |
276 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
277 * }
278 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
279 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100281 return( ret );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
284 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287 }
288
289 p += len;
290
291 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
293 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100294
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200295 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296
297 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
299 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100300
301 /*
302 * Curve ::= SEQUENCE {
303 * a FieldElement,
304 * b FieldElement,
305 * seed BIT STRING OPTIONAL
306 * -- Shall be present if used in SpecifiedECDomain
307 * -- with version equal to ecdpVer2 or ecdpVer3
308 * }
309 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
311 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100312 return( ret );
313
314 end_curve = p + len;
315
316 /*
317 * FieldElement ::= OCTET STRING
318 * containing an integer in the case of a prime field
319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
321 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324 }
325
326 p += len;
327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
329 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100332 }
333
334 p += len;
335
336 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100338 p += len;
339
340 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
342 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100343
344 /*
345 * ECPoint ::= OCTET STRING
346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
348 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100351 ( const unsigned char *) p, len ) ) != 0 )
352 {
353 /*
354 * If we can't read the point because it's compressed, cheat by
355 * reading only the X coordinate and the parity bit of Y.
356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100358 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 len != mbedtls_mpi_size( &grp->P ) + 1 ||
360 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
361 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
362 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100365 }
366 }
367
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368 p += len;
369
370 /*
371 * order INTEGER
372 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
374 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100375
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200376 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377
378 /*
379 * Allow optional elements by purposefully not enforcing p == end here.
380 */
381
382 return( 0 );
383}
384
385/*
386 * Find the group id associated with an (almost filled) group as generated by
387 * pk_group_from_specified(), or return an error if unknown.
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100390{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100391 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_ecp_group ref;
393 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100398 {
399 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200401 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402
403 /* Compare to the group we were given, starting with easy tests */
404 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
406 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
407 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
408 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
409 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
410 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100411 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100413 {
414 break;
415 }
416
417 }
418
419cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100421
422 *grp_id = *id;
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
425 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426
427 return( ret );
428}
429
430/*
431 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
432 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
434 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435{
436 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100440
441 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
442 goto cleanup;
443
444 ret = pk_group_id_from_group( &grp, grp_id );
445
446cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100448
449 return( ret );
450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452
453/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200454 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100455 *
456 * ECParameters ::= CHOICE {
457 * namedCurve OBJECT IDENTIFIER
458 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
459 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200462{
463 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200466 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
469 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100470 }
471 else
472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100474 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
475 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100476#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200477 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100478#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100479 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200480
481 /*
482 * grp may already be initilialized; if so, make sure IDs match
483 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
485 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200486
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200487 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 return( ret );
489
490 return( 0 );
491}
492
493/*
494 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100495 *
496 * The caller is responsible for clearing the structure upon failure if
497 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499 */
500static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200502{
503 int ret;
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100506 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200509 }
510
511 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200513 */
514 *p = (unsigned char *) end;
515
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100516 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200521/*
522 * RSAPublicKey ::= SEQUENCE {
523 * modulus INTEGER, -- n
524 * publicExponent INTEGER -- e
525 * }
526 */
527static int pk_get_rsapubkey( unsigned char **p,
528 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530{
531 int ret;
532 size_t len;
533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
535 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
536 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537
538 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
540 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100542 /* Import N */
543 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200545
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100546 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
547 NULL, 0, NULL, 0 ) ) != 0 )
548 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
549
550 *p += len;
551
552 /* Import E */
553 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
554 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
555
556 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
557 NULL, 0, *p, len ) ) != 0 )
558 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
559
560 *p += len;
561
Hanno Becker895c5ab2018-01-05 08:08:09 +0000562 if( mbedtls_rsa_complete( rsa ) != 0 ||
563 mbedtls_rsa_check_pubkey( rsa ) != 0 )
564 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100565 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000566 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100567
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
570 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200571
Paul Bakker1a7550a2013-09-15 13:01:22 +0200572 return( 0 );
573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200575
576/* Get a PK algorithm identifier
577 *
578 * AlgorithmIdentifier ::= SEQUENCE {
579 * algorithm OBJECT IDENTIFIER,
580 * parameters ANY DEFINED BY algorithm OPTIONAL }
581 */
582static int pk_get_pk_alg( unsigned char **p,
583 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585{
586 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200589 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
592 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200594 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
595 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200596
597 /*
598 * No parameters with RSA (only for EC)
599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 if( *pk_alg == MBEDTLS_PK_RSA &&
601 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200602 params->len != 0 ) )
603 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200605 }
606
607 return( 0 );
608}
609
610/*
611 * SubjectPublicKeyInfo ::= SEQUENCE {
612 * algorithm AlgorithmIdentifier,
613 * subjectPublicKey BIT STRING }
614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
616 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200617{
618 int ret;
619 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_asn1_buf alg_params;
621 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
622 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200623
Gilles Peskinee97dc602018-12-19 00:51:38 +0100624 PK_VALIDATE_RET( p != NULL );
625 PK_VALIDATE_RET( *p != NULL );
626 PK_VALIDATE_RET( end != NULL );
627 PK_VALIDATE_RET( pk != NULL );
628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
630 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200633 }
634
635 end = *p + len;
636
637 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
638 return( ret );
639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
641 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200642
643 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
645 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200647 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
648 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200649
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200650 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200651 return( ret );
652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653#if defined(MBEDTLS_RSA_C)
654 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200657 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658#endif /* MBEDTLS_RSA_C */
659#if defined(MBEDTLS_ECP_C)
660 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200663 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200664 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200665 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200666#endif /* MBEDTLS_ECP_C */
667 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200668
669 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
671 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200672
673 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200675
676 return( ret );
677}
678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200679#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200680/*
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100681 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
682 *
683 * The value zero is:
684 * - never a valid value for an RSA parameter
685 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
686 *
687 * Since values can't be omitted in PKCS#1, passing a zero value to
688 * rsa_complete() would be incorrect, so reject zero values early.
689 */
690static int asn1_get_nonzero_mpi( unsigned char **p,
691 const unsigned char *end,
692 mbedtls_mpi *X )
693{
694 int ret;
695
696 ret = mbedtls_asn1_get_mpi( p, end, X );
697 if( ret != 0 )
698 return( ret );
699
700 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
701 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
702
703 return( 0 );
704}
705
706/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200707 * Parse a PKCS#1 encoded private RSA key
708 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710 const unsigned char *key,
711 size_t keylen )
712{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100713 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714 size_t len;
715 unsigned char *p, *end;
716
Hanno Beckerefa14e82017-10-11 19:45:19 +0100717 mbedtls_mpi T;
718 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100719
Paul Bakker1a7550a2013-09-15 13:01:22 +0200720 p = (unsigned char *) key;
721 end = p + keylen;
722
723 /*
724 * This function parses the RSAPrivateKey (PKCS#1)
725 *
726 * RSAPrivateKey ::= SEQUENCE {
727 * version Version,
728 * modulus INTEGER, -- n
729 * publicExponent INTEGER, -- e
730 * privateExponent INTEGER, -- d
731 * prime1 INTEGER, -- p
732 * prime2 INTEGER, -- q
733 * exponent1 INTEGER, -- d mod (p-1)
734 * exponent2 INTEGER, -- d mod (q-1)
735 * coefficient INTEGER, -- (inverse of q) mod p
736 * otherPrimeInfos OtherPrimeInfos OPTIONAL
737 * }
738 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
740 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743 }
744
745 end = p + len;
746
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100747 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200748 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750 }
751
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100752 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200753 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755 }
756
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100757 /* Import N */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100758 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
759 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
760 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100761 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200762
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100763 /* Import E */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100764 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
765 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
766 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100767 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100768
769 /* Import D */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100770 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
771 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
772 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100773 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100774
775 /* Import P */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100776 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
777 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
778 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100779 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100780
781 /* Import Q */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100782 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
783 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
784 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100785 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100786
Manuel Pégourié-Gonnardd09fcde2020-02-18 10:22:54 +0100787#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloydb10fd062020-01-29 13:09:55 -0500788 /*
789 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
790 * that they can be easily recomputed from D, P and Q. However by
791 * parsing them from the PKCS1 structure it is possible to avoid
792 * recalculating them which both reduces the overhead of loading
793 * RSA private keys into memory and also avoids side channels which
794 * can arise when computing those values, since all of D, P, and Q
795 * are secret. See https://eprint.iacr.org/2020/055 for a
796 * description of one such attack.
797 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100798
Jack Lloydb10fd062020-01-29 13:09:55 -0500799 /* Import DP */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100800 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
801 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500802 goto cleanup;
803
804 /* Import DQ */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100805 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
806 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500807 goto cleanup;
808
809 /* Import QP */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100810 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
811 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500812 goto cleanup;
813
814#else
815 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnard9ab03052020-02-18 10:12:14 +0100816 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
817 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
818 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloydb10fd062020-01-29 13:09:55 -0500819 goto cleanup;
820#endif
821
Manuel Pégourié-Gonnard25bb8dc2020-02-14 11:28:47 +0100822 /* rsa_complete() doesn't complete anything with the default
823 * implementation but is still called:
824 * - for the benefit of alternative implementation that may want to
825 * pre-compute stuff beyond what's provided (eg Montgomery factors)
826 * - as is also sanity-checks the key
827 *
828 * Furthermore, we also check the public part for consistency with
829 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
830 */
831 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
832 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
833 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100834 goto cleanup;
Manuel Pégourié-Gonnard25bb8dc2020-02-14 11:28:47 +0100835 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200836
837 if( p != end )
838 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
840 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200841 }
842
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100843cleanup:
844
Hanno Beckerefa14e82017-10-11 19:45:19 +0100845 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100846
847 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100849 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100850 if( ( ret & 0xff80 ) == 0 )
851 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
852 else
853 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856 }
857
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100858 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200859}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863/*
864 * Parse a SEC1 encoded private EC key
865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200866static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867 const unsigned char *key,
868 size_t keylen )
869{
870 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100871 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200872 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200873 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200874 unsigned char *p = (unsigned char *) key;
875 unsigned char *end = p + keylen;
876 unsigned char *end2;
877
878 /*
879 * RFC 5915, or SEC1 Appendix C.4
880 *
881 * ECPrivateKey ::= SEQUENCE {
882 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
883 * privateKey OCTET STRING,
884 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
885 * publicKey [1] BIT STRING OPTIONAL
886 * }
887 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
889 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892 }
893
894 end = p + len;
895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
897 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898
899 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200900 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
903 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200905 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200907 mbedtls_ecp_keypair_free( eck );
908 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200909 }
910
911 p += len;
912
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200913 pubkey_done = 0;
914 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200915 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200916 /*
917 * Is 'parameters' present?
918 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200919 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
920 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200921 {
922 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
923 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
924 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200925 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200926 return( ret );
927 }
928 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200929 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200931 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200932 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100933 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800934 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200935
Jethro Beekmand2df9362018-02-16 13:11:04 -0800936 if( p != end )
937 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200938 /*
939 * Is 'publickey' present? If not, or if we can't read it (eg because it
940 * is compressed), create it from the private key.
941 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200942 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
943 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200944 {
945 end2 = p + len;
946
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200947 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
948 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200949
950 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200951 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
952 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200953
954 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
955 pubkey_done = 1;
956 else
957 {
958 /*
959 * The only acceptable failure mode of pk_get_ecpubkey() above
960 * is if the point format is not recognized.
961 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200962 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
963 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200964 }
965 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200966 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200967 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200968 mbedtls_ecp_keypair_free( eck );
969 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200970 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100972
973 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100975 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200977 mbedtls_ecp_keypair_free( eck );
978 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200979 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200981 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200984 return( ret );
985 }
986
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200987 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200988}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200990
991/*
992 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100993 *
994 * Notes:
995 *
996 * - This function does not own the key buffer. It is the
997 * responsibility of the caller to take care of zeroizing
998 * and freeing it after use.
999 *
1000 * - The function is responsible for freeing the provided
1001 * PK context on failure.
1002 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003 */
1004static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001005 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001006 const unsigned char* key,
1007 size_t keylen )
1008{
1009 int ret, version;
1010 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012 unsigned char *p = (unsigned char *) key;
1013 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1015 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016
1017 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001018 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001019 *
1020 * PrivateKeyInfo ::= SEQUENCE {
1021 * version Version,
1022 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1023 * privateKey PrivateKey,
1024 * attributes [0] IMPLICIT Attributes OPTIONAL }
1025 *
1026 * Version ::= INTEGER
1027 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1028 * PrivateKey ::= OCTET STRING
1029 *
1030 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1031 */
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1034 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001037 }
1038
1039 end = p + len;
1040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1042 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043
1044 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046
1047 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001050 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1051 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052
1053 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1055 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1058 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001059
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001060 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061 return( ret );
1062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063#if defined(MBEDTLS_RSA_C)
1064 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001066 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001069 return( ret );
1070 }
1071 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072#endif /* MBEDTLS_RSA_C */
1073#if defined(MBEDTLS_ECP_C)
1074 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1077 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *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_ECP_C */
1084 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001086 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001087}
1088
1089/*
1090 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001091 *
1092 * To save space, the decryption happens in-place on the given key buffer.
1093 * Also, while this function may modify the keybuffer, it doesn't own it,
1094 * and instead it is the responsibility of the caller to zeroize and properly
1095 * free it after use.
1096 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001099static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001100 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001101 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102 const unsigned char *pwd, size_t pwdlen )
1103{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001104 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001106 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001107 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001108 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1109#if defined(MBEDTLS_PKCS12_C)
1110 mbedtls_cipher_type_t cipher_alg;
1111 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112#endif
1113
Hanno Becker2aa80a72017-09-07 15:28:45 +01001114 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001115 end = p + keylen;
1116
1117 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119
1120 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001121 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001122 *
1123 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1124 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1125 * encryptedData EncryptedData
1126 * }
1127 *
1128 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1129 *
1130 * EncryptedData ::= OCTET STRING
1131 *
1132 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001133 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1136 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139 }
1140
1141 end = p + len;
1142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1144 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
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, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1147 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001148
Hanno Beckerfab35692017-08-25 13:38:26 +01001149 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150
1151 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001152 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154#if defined(MBEDTLS_PKCS12_C)
1155 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 cipher_alg, md_alg,
1159 pwd, pwdlen, p, len, buf ) ) != 0 )
1160 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001161 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1162 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001163
1164 return( ret );
1165 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001166
1167 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1172 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173 pwd, pwdlen,
1174 p, len, buf ) ) != 0 )
1175 {
1176 return( ret );
1177 }
1178
1179 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1183 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001184
1185 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186 }
1187 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#endif /* MBEDTLS_PKCS12_C */
1189#if defined(MBEDTLS_PKCS5_C)
1190 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001193 p, len, buf ) ) != 0 )
1194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1196 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
1198 return( ret );
1199 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001200
1201 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001202 }
1203 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001205 {
1206 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001207 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001208
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001209 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001211
Paul Bakker1a7550a2013-09-15 13:01:22 +02001212 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1213}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215
1216/*
1217 * Parse a private key
1218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001220 const unsigned char *key, size_t keylen,
1221 const unsigned char *pwd, size_t pwdlen )
1222{
1223 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001228#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001229
Gilles Peskinee97dc602018-12-19 00:51:38 +01001230 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001231 if( keylen == 0 )
1232 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1233 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001234
1235#if defined(MBEDTLS_PEM_PARSE_C)
1236 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001239 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001240 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001241 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1242 else
1243 ret = mbedtls_pem_read_buffer( &pem,
1244 "-----BEGIN RSA PRIVATE KEY-----",
1245 "-----END RSA PRIVATE KEY-----",
1246 key, pwd, pwdlen, &len );
1247
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248 if( ret == 0 )
1249 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001250 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001251 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253 pem.buf, pem.buflen ) ) != 0 )
1254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256 }
1257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001259 return( ret );
1260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1262 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1263 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1264 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1265 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001266 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001270 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001271 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001272 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1273 else
1274 ret = mbedtls_pem_read_buffer( &pem,
1275 "-----BEGIN EC PRIVATE KEY-----",
1276 "-----END EC PRIVATE KEY-----",
1277 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278 if( ret == 0 )
1279 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001280 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281
Hanno Beckerfab35692017-08-25 13:38:26 +01001282 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284 pem.buf, pem.buflen ) ) != 0 )
1285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287 }
1288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001290 return( ret );
1291 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1293 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1294 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1295 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1296 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001300 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001301 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001302 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1303 else
1304 ret = mbedtls_pem_read_buffer( &pem,
1305 "-----BEGIN PRIVATE KEY-----",
1306 "-----END PRIVATE KEY-----",
1307 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001308 if( ret == 0 )
1309 {
1310 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1311 pem.buf, pem.buflen ) ) != 0 )
1312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 }
1315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 return( ret );
1318 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320 return( ret );
1321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001323 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001324 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001325 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1326 else
1327 ret = mbedtls_pem_read_buffer( &pem,
1328 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1329 "-----END ENCRYPTED PRIVATE KEY-----",
1330 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001331 if( ret == 0 )
1332 {
1333 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1334 pem.buf, pem.buflen,
1335 pwd, pwdlen ) ) != 0 )
1336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 }
1339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341 return( ret );
1342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346#else
1347 ((void) pwd);
1348 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350
1351 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001352 * At this point we only know it's not a PEM formatted key. Could be any
1353 * of the known DER encoded private key formats
1354 *
1355 * We try the different DER format parsers to see if one passes without
1356 * error
1357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001359 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001360 unsigned char *key_copy;
1361
1362 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1363 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1364
1365 memcpy( key_copy, key, keylen );
1366
1367 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1368 pwd, pwdlen );
1369
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001370 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001371 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001372 }
1373
Hanno Beckerfab35692017-08-25 13:38:26 +01001374 if( ret == 0 )
1375 return( 0 );
1376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001378 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001381 {
1382 return( ret );
1383 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001385
1386 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1387 return( 0 );
1388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001390 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393
Hanno Becker9be19262017-09-08 12:39:44 +01001394 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001395 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1396 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001397 {
1398 return( 0 );
1399 }
1400
Hanno Becker780f0a42018-10-10 11:23:33 +01001401 mbedtls_pk_free( pk );
1402 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001406 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001407 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1408 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1409 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410 {
1411 return( 0 );
1412 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001413 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001415
Hanno Becker780f0a42018-10-10 11:23:33 +01001416 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1417 * it is ok to leave the PK context initialized but not
1418 * freed: It is the caller's responsibility to call pk_init()
1419 * before calling this function, and to call pk_free()
1420 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1421 * isn't, this leads to mbedtls_pk_free() being called
1422 * twice, once here and once by the caller, but this is
1423 * also ok and in line with the mbedtls_pk_free() calls
1424 * on failed PEM parsing attempts. */
1425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001427}
1428
1429/*
1430 * Parse a public key
1431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433 const unsigned char *key, size_t keylen )
1434{
1435 int ret;
1436 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001437#if defined(MBEDTLS_RSA_C)
1438 const mbedtls_pk_info_t *pk_info;
1439#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001442 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001443#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444
Gilles Peskinee97dc602018-12-19 00:51:38 +01001445 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001446 if( keylen == 0 )
1447 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001448 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1449
1450#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001452#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001453 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001454 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001455 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1456 else
1457 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001458 "-----BEGIN RSA PUBLIC KEY-----",
1459 "-----END RSA PUBLIC KEY-----",
1460 key, NULL, 0, &len );
1461
1462 if( ret == 0 )
1463 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001464 p = pem.buf;
1465 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1466 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001467
Ron Eldor84df1ae2017-10-16 17:11:52 +03001468 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1469 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001470
Ron Eldor84df1ae2017-10-16 17:11:52 +03001471 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1472 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001473
Ron Eldord0c56de2017-10-10 17:03:08 +03001474 mbedtls_pem_free( &pem );
1475 return( ret );
1476 }
1477 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1478 {
1479 mbedtls_pem_free( &pem );
1480 return( ret );
1481 }
1482#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001483
1484 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001485 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001486 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1487 else
1488 ret = mbedtls_pem_read_buffer( &pem,
1489 "-----BEGIN PUBLIC KEY-----",
1490 "-----END PUBLIC KEY-----",
1491 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492
1493 if( ret == 0 )
1494 {
1495 /*
1496 * Was PEM encoded
1497 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001498 p = pem.buf;
1499
1500 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1501 mbedtls_pem_free( &pem );
1502 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001506 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001507 return( ret );
1508 }
Ron Eldor5472d432017-10-17 09:49:00 +03001509 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001511
1512#if defined(MBEDTLS_RSA_C)
1513 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1514 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1515
1516 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1517 return( ret );
1518
Ron Eldor9566ff72018-02-07 18:59:41 +02001519 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001520 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001521 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001522 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001523 return( ret );
1524 }
1525 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001526 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001527 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001528 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001529 }
1530#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531 p = (unsigned char *) key;
1532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001534
Paul Bakker1a7550a2013-09-15 13:01:22 +02001535 return( ret );
1536}
1537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538#endif /* MBEDTLS_PK_PARSE_C */