blob: 989aa4e29752dbd6d433a801c02bf852c47f48bc [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
Jack Lloydb10fd062020-01-29 13:09:55 -0500787#if !defined(MBEDTLS_RSA_NO_CRT)
788 /*
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
822 /* Complete the RSA private key */
823 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100824 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200825
826 if( p != end )
827 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100828 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
829 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830 }
831
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100832cleanup:
833
Hanno Beckerefa14e82017-10-11 19:45:19 +0100834 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100835
836 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200837 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100838 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100839 if( ( ret & 0xff80 ) == 0 )
840 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
841 else
842 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845 }
846
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100847 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200851#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200852/*
853 * Parse a SEC1 encoded private EC key
854 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200855static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856 const unsigned char *key,
857 size_t keylen )
858{
859 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100860 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200861 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200863 unsigned char *p = (unsigned char *) key;
864 unsigned char *end = p + keylen;
865 unsigned char *end2;
866
867 /*
868 * RFC 5915, or SEC1 Appendix C.4
869 *
870 * ECPrivateKey ::= SEQUENCE {
871 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
872 * privateKey OCTET STRING,
873 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
874 * publicKey [1] BIT STRING OPTIONAL
875 * }
876 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
878 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200881 }
882
883 end = p + len;
884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
886 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887
888 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200889 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
892 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200894 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200896 mbedtls_ecp_keypair_free( eck );
897 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898 }
899
900 p += len;
901
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200902 pubkey_done = 0;
903 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200904 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200905 /*
906 * Is 'parameters' present?
907 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200908 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
909 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200910 {
911 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
912 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
913 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200914 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200915 return( ret );
916 }
917 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200918 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200921 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100922 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800923 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200924
Jethro Beekmand2df9362018-02-16 13:11:04 -0800925 if( p != end )
926 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200927 /*
928 * Is 'publickey' present? If not, or if we can't read it (eg because it
929 * is compressed), create it from the private key.
930 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200931 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
932 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200933 {
934 end2 = p + len;
935
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200936 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
937 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200938
939 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200940 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
941 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200942
943 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
944 pubkey_done = 1;
945 else
946 {
947 /*
948 * The only acceptable failure mode of pk_get_ecpubkey() above
949 * is if the point format is not recognized.
950 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200951 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
952 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200953 }
954 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200955 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200956 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200957 mbedtls_ecp_keypair_free( eck );
958 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200959 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200960 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100961
962 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100964 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 mbedtls_ecp_keypair_free( eck );
967 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200968 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200971 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200973 return( ret );
974 }
975
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200976 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979
980/*
981 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100982 *
983 * Notes:
984 *
985 * - This function does not own the key buffer. It is the
986 * responsibility of the caller to take care of zeroizing
987 * and freeing it after use.
988 *
989 * - The function is responsible for freeing the provided
990 * PK context on failure.
991 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 */
993static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200995 const unsigned char* key,
996 size_t keylen )
997{
998 int ret, version;
999 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001001 unsigned char *p = (unsigned char *) key;
1002 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1004 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005
1006 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001007 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008 *
1009 * PrivateKeyInfo ::= SEQUENCE {
1010 * version Version,
1011 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1012 * privateKey PrivateKey,
1013 * attributes [0] IMPLICIT Attributes OPTIONAL }
1014 *
1015 * Version ::= INTEGER
1016 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1017 * PrivateKey ::= OCTET STRING
1018 *
1019 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1020 */
1021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1023 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001026 }
1027
1028 end = p + len;
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1031 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032
1033 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035
1036 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1040 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
1042 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1044 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001046 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1047 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001048
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001049 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001050 return( ret );
1051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#if defined(MBEDTLS_RSA_C)
1053 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058 return( ret );
1059 }
1060 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001061#endif /* MBEDTLS_RSA_C */
1062#if defined(MBEDTLS_ECP_C)
1063 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1066 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *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_ECP_C */
1073 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001074
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001075 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001076}
1077
1078/*
1079 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001080 *
1081 * To save space, the decryption happens in-place on the given key buffer.
1082 * Also, while this function may modify the keybuffer, it doesn't own it,
1083 * and instead it is the responsibility of the caller to zeroize and properly
1084 * free it after use.
1085 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001086 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001087#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001090 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091 const unsigned char *pwd, size_t pwdlen )
1092{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001093 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001095 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1098#if defined(MBEDTLS_PKCS12_C)
1099 mbedtls_cipher_type_t cipher_alg;
1100 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001101#endif
1102
Hanno Becker2aa80a72017-09-07 15:28:45 +01001103 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 end = p + keylen;
1105
1106 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001107 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108
1109 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001110 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001111 *
1112 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1113 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1114 * encryptedData EncryptedData
1115 * }
1116 *
1117 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1118 *
1119 * EncryptedData ::= OCTET STRING
1120 *
1121 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001122 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1125 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128 }
1129
1130 end = p + len;
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1133 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
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, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1136 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137
Hanno Beckerfab35692017-08-25 13:38:26 +01001138 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139
1140 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001141 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143#if defined(MBEDTLS_PKCS12_C)
1144 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001146 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 cipher_alg, md_alg,
1148 pwd, pwdlen, p, len, buf ) ) != 0 )
1149 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001150 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1151 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001152
1153 return( ret );
1154 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001155
1156 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1161 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001162 pwd, pwdlen,
1163 p, len, buf ) ) != 0 )
1164 {
1165 return( ret );
1166 }
1167
1168 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001169 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1172 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001173
1174 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175 }
1176 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177#endif /* MBEDTLS_PKCS12_C */
1178#if defined(MBEDTLS_PKCS5_C)
1179 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001182 p, len, buf ) ) != 0 )
1183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1185 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001186
1187 return( ret );
1188 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001189
1190 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001191 }
1192 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001194 {
1195 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001196 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001198 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001199 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001200
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1202}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001203#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001204
1205/*
1206 * Parse a private key
1207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209 const unsigned char *key, size_t keylen,
1210 const unsigned char *pwd, size_t pwdlen )
1211{
1212 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001217#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218
Gilles Peskinee97dc602018-12-19 00:51:38 +01001219 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001220 if( keylen == 0 )
1221 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1222 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001223
1224#if defined(MBEDTLS_PEM_PARSE_C)
1225 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001228 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001229 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001230 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1231 else
1232 ret = mbedtls_pem_read_buffer( &pem,
1233 "-----BEGIN RSA PRIVATE KEY-----",
1234 "-----END RSA PRIVATE KEY-----",
1235 key, pwd, pwdlen, &len );
1236
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237 if( ret == 0 )
1238 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001239 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001240 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001242 pem.buf, pem.buflen ) ) != 0 )
1243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 }
1246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248 return( ret );
1249 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1251 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1252 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1253 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1254 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001255 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001259 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001260 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001261 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1262 else
1263 ret = mbedtls_pem_read_buffer( &pem,
1264 "-----BEGIN EC PRIVATE KEY-----",
1265 "-----END EC PRIVATE KEY-----",
1266 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267 if( ret == 0 )
1268 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001269 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270
Hanno Beckerfab35692017-08-25 13:38:26 +01001271 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001273 pem.buf, pem.buflen ) ) != 0 )
1274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001276 }
1277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279 return( ret );
1280 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1282 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1283 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1284 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1285 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001289 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001290 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001291 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1292 else
1293 ret = mbedtls_pem_read_buffer( &pem,
1294 "-----BEGIN PRIVATE KEY-----",
1295 "-----END PRIVATE KEY-----",
1296 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 if( ret == 0 )
1298 {
1299 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1300 pem.buf, pem.buflen ) ) != 0 )
1301 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001303 }
1304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001306 return( ret );
1307 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 return( ret );
1310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001312 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001313 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001314 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1315 else
1316 ret = mbedtls_pem_read_buffer( &pem,
1317 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1318 "-----END ENCRYPTED PRIVATE KEY-----",
1319 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001320 if( ret == 0 )
1321 {
1322 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1323 pem.buf, pem.buflen,
1324 pwd, pwdlen ) ) != 0 )
1325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001326 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001327 }
1328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 return( ret );
1331 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001333 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335#else
1336 ((void) pwd);
1337 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339
1340 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001341 * At this point we only know it's not a PEM formatted key. Could be any
1342 * of the known DER encoded private key formats
1343 *
1344 * We try the different DER format parsers to see if one passes without
1345 * error
1346 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001347#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001348 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001349 unsigned char *key_copy;
1350
1351 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1352 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1353
1354 memcpy( key_copy, key, keylen );
1355
1356 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1357 pwd, pwdlen );
1358
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001359 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001360 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361 }
1362
Hanno Beckerfab35692017-08-25 13:38:26 +01001363 if( ret == 0 )
1364 return( 0 );
1365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001367 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001370 {
1371 return( ret );
1372 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374
1375 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1376 return( 0 );
1377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001379 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Hanno Becker9be19262017-09-08 12:39:44 +01001383 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001384 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1385 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386 {
1387 return( 0 );
1388 }
1389
Hanno Becker780f0a42018-10-10 11:23:33 +01001390 mbedtls_pk_free( pk );
1391 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001395 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001396 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1397 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1398 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001399 {
1400 return( 0 );
1401 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001402 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
Hanno Becker780f0a42018-10-10 11:23:33 +01001405 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1406 * it is ok to leave the PK context initialized but not
1407 * freed: It is the caller's responsibility to call pk_init()
1408 * before calling this function, and to call pk_free()
1409 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1410 * isn't, this leads to mbedtls_pk_free() being called
1411 * twice, once here and once by the caller, but this is
1412 * also ok and in line with the mbedtls_pk_free() calls
1413 * on failed PEM parsing attempts. */
1414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416}
1417
1418/*
1419 * Parse a public key
1420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422 const unsigned char *key, size_t keylen )
1423{
1424 int ret;
1425 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001426#if defined(MBEDTLS_RSA_C)
1427 const mbedtls_pk_info_t *pk_info;
1428#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001432#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433
Gilles Peskinee97dc602018-12-19 00:51:38 +01001434 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001435 if( keylen == 0 )
1436 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001437 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1438
1439#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001441#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001442 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001443 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001444 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1445 else
1446 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001447 "-----BEGIN RSA PUBLIC KEY-----",
1448 "-----END RSA PUBLIC KEY-----",
1449 key, NULL, 0, &len );
1450
1451 if( ret == 0 )
1452 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001453 p = pem.buf;
1454 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1455 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001456
Ron Eldor84df1ae2017-10-16 17:11:52 +03001457 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1458 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001459
Ron Eldor84df1ae2017-10-16 17:11:52 +03001460 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1461 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001462
Ron Eldord0c56de2017-10-10 17:03:08 +03001463 mbedtls_pem_free( &pem );
1464 return( ret );
1465 }
1466 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1467 {
1468 mbedtls_pem_free( &pem );
1469 return( ret );
1470 }
1471#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001472
1473 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001474 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001475 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1476 else
1477 ret = mbedtls_pem_read_buffer( &pem,
1478 "-----BEGIN PUBLIC KEY-----",
1479 "-----END PUBLIC KEY-----",
1480 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001481
1482 if( ret == 0 )
1483 {
1484 /*
1485 * Was PEM encoded
1486 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001487 p = pem.buf;
1488
1489 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1490 mbedtls_pem_free( &pem );
1491 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001492 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001494 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001496 return( ret );
1497 }
Ron Eldor5472d432017-10-17 09:49:00 +03001498 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001500
1501#if defined(MBEDTLS_RSA_C)
1502 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1503 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1504
1505 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1506 return( ret );
1507
Ron Eldor9566ff72018-02-07 18:59:41 +02001508 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001509 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001510 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001511 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001512 return( ret );
1513 }
1514 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001515 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001516 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001517 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001518 }
1519#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001520 p = (unsigned char *) key;
1521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001523
Paul Bakker1a7550a2013-09-15 13:01:22 +02001524 return( ret );
1525}
1526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001527#endif /* MBEDTLS_PK_PARSE_C */