blob: 3bad0ce6dd755ad89a40cafae9e04760e10a8451 [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"
Paul Bakker1a7550a2013-09-15 13:01:22 +020033
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020041#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020047#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020050#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020053#endif
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020057#else
58#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020059#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020061#endif
62
Gilles Peskine832f3492017-11-30 11:42:12 +010063#if defined(MBEDTLS_FS_IO) || \
64 defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker34617722014-06-13 17:20:13 +020065/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static void mbedtls_zeroize( void *v, size_t n ) {
Paul Bakker34617722014-06-13 17:20:13 +020067 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
68}
Gilles Peskine832f3492017-11-30 11:42:12 +010069#endif
Paul Bakker34617722014-06-13 17:20:13 +020070
Gilles Peskine832f3492017-11-30 11:42:12 +010071#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020072/*
73 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020074 *
75 * The file is expected to contain either PEM or DER encoded data.
76 * A terminating null byte is always appended. It is included in the announced
77 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020080{
81 FILE *f;
82 long size;
83
84 if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020086
87 fseek( f, 0, SEEK_END );
88 if( ( size = ftell( f ) ) == -1 )
89 {
90 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020092 }
93 fseek( f, 0, SEEK_SET );
94
95 *n = (size_t) size;
96
97 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020098 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +020099 {
100 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200101 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102 }
103
104 if( fread( *buf, 1, *n, f ) != *n )
105 {
106 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100107
108 mbedtls_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112 }
113
114 fclose( f );
115
116 (*buf)[*n] = '\0';
117
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200118 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
119 ++*n;
120
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121 return( 0 );
122}
123
124/*
125 * Load and parse a private key
126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128 const char *path, const char *pwd )
129{
130 int ret;
131 size_t n;
132 unsigned char *buf;
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135 return( ret );
136
137 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200141 (const unsigned char *) pwd, strlen( pwd ) );
142
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200143 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200145
146 return( ret );
147}
148
149/*
150 * Load and parse a public key
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200153{
154 int ret;
155 size_t n;
156 unsigned char *buf;
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200159 return( ret );
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200163 mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165
166 return( ret );
167}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_ECP_C)
171/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200172 *
173 * ECParameters ::= CHOICE {
174 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100175 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200176 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177 * }
178 */
179static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181{
182 int ret;
183
Sanne Woudab2b29d52017-08-21 15:58:12 +0100184 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100185 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
186 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100187
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100188 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200189 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 if( params->tag != MBEDTLS_ASN1_OID
191#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
192 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100193#endif
194 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
197 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100198 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100203 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200204
205 params->p = *p;
206 *p += params->len;
207
208 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
210 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200211
212 return( 0 );
213}
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200216/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100217 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
218 * WARNING: the resulting group should only be used with
219 * pk_group_id_from_specified(), since its base point may not be set correctly
220 * if it was encoded compressed.
221 *
222 * SpecifiedECDomain ::= SEQUENCE {
223 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
224 * fieldID FieldID {{FieldTypes}},
225 * curve Curve,
226 * base ECPoint,
227 * order INTEGER,
228 * cofactor INTEGER OPTIONAL,
229 * hash HashAlgorithm OPTIONAL,
230 * ...
231 * }
232 *
233 * We only support prime-field as field type, and ignore hash and cofactor.
234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236{
237 int ret;
238 unsigned char *p = params->p;
239 const unsigned char * const end = params->p + params->len;
240 const unsigned char *end_field, *end_curve;
241 size_t len;
242 int ver;
243
244 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
246 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100247
248 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100250
251 /*
252 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
253 * fieldType FIELD-ID.&id({IOSet}),
254 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
255 * }
256 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
258 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100259 return( ret );
260
261 end_field = p + len;
262
263 /*
264 * FIELD-ID ::= TYPE-IDENTIFIER
265 * FieldTypes FIELD-ID ::= {
266 * { Prime-p IDENTIFIED BY prime-field } |
267 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
268 * }
269 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100272 return( ret );
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
275 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100278 }
279
280 p += len;
281
282 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
284 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100285
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200286 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
288 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
290 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100291
292 /*
293 * Curve ::= SEQUENCE {
294 * a FieldElement,
295 * b FieldElement,
296 * seed BIT STRING OPTIONAL
297 * -- Shall be present if used in SpecifiedECDomain
298 * -- with version equal to ecdpVer2 or ecdpVer3
299 * }
300 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
302 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100303 return( ret );
304
305 end_curve = p + len;
306
307 /*
308 * FieldElement ::= OCTET STRING
309 * containing an integer in the case of a prime field
310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
312 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 }
316
317 p += len;
318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
320 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100323 }
324
325 p += len;
326
327 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 p += len;
330
331 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
333 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100334
335 /*
336 * ECPoint ::= OCTET STRING
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
339 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100342 ( const unsigned char *) p, len ) ) != 0 )
343 {
344 /*
345 * If we can't read the point because it's compressed, cheat by
346 * reading only the X coordinate and the parity bit of Y.
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100349 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 len != mbedtls_mpi_size( &grp->P ) + 1 ||
351 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
352 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
353 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100356 }
357 }
358
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100359 p += len;
360
361 /*
362 * order INTEGER
363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
365 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100366
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200367 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100368
369 /*
370 * Allow optional elements by purposefully not enforcing p == end here.
371 */
372
373 return( 0 );
374}
375
376/*
377 * Find the group id associated with an (almost filled) group as generated by
378 * pk_group_from_specified(), or return an error if unknown.
379 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380static 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 +0100381{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100382 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_ecp_group ref;
384 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100389 {
390 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200392 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393
394 /* Compare to the group we were given, starting with easy tests */
395 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
397 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
398 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
399 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
400 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
401 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100402 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 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 +0100404 {
405 break;
406 }
407
408 }
409
410cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100412
413 *grp_id = *id;
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
416 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100417
418 return( ret );
419}
420
421/*
422 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
425 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426{
427 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100431
432 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
433 goto cleanup;
434
435 ret = pk_group_id_from_group( &grp, grp_id );
436
437cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439
440 return( ret );
441}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100443
444/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200445 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100446 *
447 * ECParameters ::= CHOICE {
448 * namedCurve OBJECT IDENTIFIER
449 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
450 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200451 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200453{
454 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200457 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
460 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100461 }
462 else
463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200464#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100465 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
466 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100467#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100469#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100470 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200471
472 /*
473 * grp may already be initilialized; if so, make sure IDs match
474 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
476 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200477
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200478 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200479 return( ret );
480
481 return( 0 );
482}
483
484/*
485 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100486 *
487 * The caller is responsible for clearing the structure upon failure if
488 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490 */
491static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200493{
494 int ret;
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100497 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200500 }
501
502 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504 */
505 *p = (unsigned char *) end;
506
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100507 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200512/*
513 * RSAPublicKey ::= SEQUENCE {
514 * modulus INTEGER, -- n
515 * publicExponent INTEGER -- e
516 * }
517 */
518static int pk_get_rsapubkey( unsigned char **p,
519 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200521{
522 int ret;
523 size_t len;
524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200525 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
526 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
527 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200528
529 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
531 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200532
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100533 /* Import N */
534 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200536
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100537 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
538 NULL, 0, NULL, 0 ) ) != 0 )
539 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
540
541 *p += len;
542
543 /* Import E */
544 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
545 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
546
547 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
548 NULL, 0, *p, len ) ) != 0 )
549 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
550
551 *p += len;
552
Hanno Becker895c5ab2018-01-05 08:08:09 +0000553 if( mbedtls_rsa_complete( rsa ) != 0 ||
554 mbedtls_rsa_check_pubkey( rsa ) != 0 )
555 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100556 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000557 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100558
Paul Bakker1a7550a2013-09-15 13:01:22 +0200559 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
561 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200562
Paul Bakker1a7550a2013-09-15 13:01:22 +0200563 return( 0 );
564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200566
567/* Get a PK algorithm identifier
568 *
569 * AlgorithmIdentifier ::= SEQUENCE {
570 * algorithm OBJECT IDENTIFIER,
571 * parameters ANY DEFINED BY algorithm OPTIONAL }
572 */
573static int pk_get_pk_alg( unsigned char **p,
574 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200576{
577 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
583 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
586 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200587
588 /*
589 * No parameters with RSA (only for EC)
590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( *pk_alg == MBEDTLS_PK_RSA &&
592 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593 params->len != 0 ) )
594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200595 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200596 }
597
598 return( 0 );
599}
600
601/*
602 * SubjectPublicKeyInfo ::= SEQUENCE {
603 * algorithm AlgorithmIdentifier,
604 * subjectPublicKey BIT STRING }
605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
607 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200608{
609 int ret;
610 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 mbedtls_asn1_buf alg_params;
612 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
613 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
616 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200619 }
620
621 end = *p + len;
622
623 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
624 return( ret );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
627 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200628
629 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200630 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
631 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
634 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200635
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200636 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200637 return( ret );
638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_RSA_C)
640 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644#endif /* MBEDTLS_RSA_C */
645#if defined(MBEDTLS_ECP_C)
646 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200647 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200649 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200651 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652#endif /* MBEDTLS_ECP_C */
653 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654
655 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200656 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
657 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658
659 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200660 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661
662 return( ret );
663}
664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200665#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200666/*
667 * Parse a PKCS#1 encoded private RSA key
668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200669static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200670 const unsigned char *key,
671 size_t keylen )
672{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100673 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200674 size_t len;
675 unsigned char *p, *end;
676
Hanno Beckerefa14e82017-10-11 19:45:19 +0100677 mbedtls_mpi T;
678 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100679
Paul Bakker1a7550a2013-09-15 13:01:22 +0200680 p = (unsigned char *) key;
681 end = p + keylen;
682
683 /*
684 * This function parses the RSAPrivateKey (PKCS#1)
685 *
686 * RSAPrivateKey ::= SEQUENCE {
687 * version Version,
688 * modulus INTEGER, -- n
689 * publicExponent INTEGER, -- e
690 * privateExponent INTEGER, -- d
691 * prime1 INTEGER, -- p
692 * prime2 INTEGER, -- q
693 * exponent1 INTEGER, -- d mod (p-1)
694 * exponent2 INTEGER, -- d mod (q-1)
695 * coefficient INTEGER, -- (inverse of q) mod p
696 * otherPrimeInfos OtherPrimeInfos OPTIONAL
697 * }
698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
700 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200701 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200702 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200703 }
704
705 end = p + len;
706
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100707 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200708 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710 }
711
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100712 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200713 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200715 }
716
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100717 /* Import N */
718 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
719 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
720 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
721 NULL, 0, NULL, 0 ) ) != 0 )
722 goto cleanup;
723 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200724
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100725 /* Import E */
726 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
727 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
728 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
729 NULL, 0, p, len ) ) != 0 )
730 goto cleanup;
731 p += len;
732
733 /* Import D */
734 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
735 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
736 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
737 p, len, NULL, 0 ) ) != 0 )
738 goto cleanup;
739 p += len;
740
741 /* Import P */
742 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
743 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
744 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
745 NULL, 0, NULL, 0 ) ) != 0 )
746 goto cleanup;
747 p += len;
748
749 /* Import Q */
750 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
751 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
752 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
753 NULL, 0, NULL, 0 ) ) != 0 )
754 goto cleanup;
755 p += len;
756
Jack Lloyd100e1472020-01-29 13:13:04 -0500757#if !defined(MBEDTLS_RSA_NO_CRT)
758 /*
759 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
760 * that they can be easily recomputed from D, P and Q. However by
761 * parsing them from the PKCS1 structure it is possible to avoid
762 * recalculating them which both reduces the overhead of loading
763 * RSA private keys into memory and also avoids side channels which
764 * can arise when computing those values, since all of D, P, and Q
765 * are secret. See https://eprint.iacr.org/2020/055 for a
766 * description of one such attack.
767 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100768
Jack Lloyd100e1472020-01-29 13:13:04 -0500769 /* Import DP */
770 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DP ) ) != 0)
771 goto cleanup;
772
773 /* Import DQ */
774 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0)
775 goto cleanup;
776
777 /* Import QP */
778 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->QP ) ) != 0)
779 goto cleanup;
780
781#else
782 /* Verify existance of the CRT params */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100783 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
784 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
785 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500786 goto cleanup;
787#endif
788
789 /* Complete the RSA private key */
790 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100791 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792
793 if( p != end )
794 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100795 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
796 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797 }
798
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100799cleanup:
800
Hanno Beckerefa14e82017-10-11 19:45:19 +0100801 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100802
803 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100805 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100806 if( ( ret & 0xff80 ) == 0 )
807 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
808 else
809 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200811 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200812 }
813
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200819/*
820 * Parse a SEC1 encoded private EC key
821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200823 const unsigned char *key,
824 size_t keylen )
825{
826 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100827 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200828 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200830 unsigned char *p = (unsigned char *) key;
831 unsigned char *end = p + keylen;
832 unsigned char *end2;
833
834 /*
835 * RFC 5915, or SEC1 Appendix C.4
836 *
837 * ECPrivateKey ::= SEQUENCE {
838 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
839 * privateKey OCTET STRING,
840 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
841 * publicKey [1] BIT STRING OPTIONAL
842 * }
843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200844 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
845 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200847 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200848 }
849
850 end = p + len;
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
853 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200854
855 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200856 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200857
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200858 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
859 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200861 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863 mbedtls_ecp_keypair_free( eck );
864 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200865 }
866
867 p += len;
868
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200869 pubkey_done = 0;
870 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200872 /*
873 * Is 'parameters' present?
874 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200875 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
876 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200877 {
878 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
879 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
880 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200881 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200882 return( ret );
883 }
884 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200885 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200887 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100889 }
Jethro Beekman004e3712018-02-16 13:11:04 -0800890 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200891
Jethro Beekman004e3712018-02-16 13:11:04 -0800892 if( p != end )
893 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200894 /*
895 * Is 'publickey' present? If not, or if we can't read it (eg because it
896 * is compressed), create it from the private key.
897 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200898 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
899 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200900 {
901 end2 = p + len;
902
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200903 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
904 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200905
906 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200907 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
908 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200909
910 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
911 pubkey_done = 1;
912 else
913 {
914 /*
915 * The only acceptable failure mode of pk_get_ecpubkey() above
916 * is if the point format is not recognized.
917 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200918 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
919 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200920 }
921 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200922 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200923 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200924 mbedtls_ecp_keypair_free( eck );
925 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200926 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200927 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100928
929 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100931 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200933 mbedtls_ecp_keypair_free( eck );
934 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200935 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200937 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200938 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200940 return( ret );
941 }
942
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200943 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200946
947/*
948 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100949 *
950 * Notes:
951 *
952 * - This function does not own the key buffer. It is the
953 * responsibility of the caller to take care of zeroizing
954 * and freeing it after use.
955 *
956 * - The function is responsible for freeing the provided
957 * PK context on failure.
958 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200959 */
960static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200961 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200962 const unsigned char* key,
963 size_t keylen )
964{
965 int ret, version;
966 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200968 unsigned char *p = (unsigned char *) key;
969 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
971 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200972
973 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +0100974 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200975 *
976 * PrivateKeyInfo ::= SEQUENCE {
977 * version Version,
978 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
979 * privateKey PrivateKey,
980 * attributes [0] IMPLICIT Attributes OPTIONAL }
981 *
982 * Version ::= INTEGER
983 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
984 * PrivateKey ::= OCTET STRING
985 *
986 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
987 */
988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200989 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
990 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200992 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200993 }
994
995 end = p + len;
996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
998 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200999
1000 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001002
1003 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001004 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001006 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1007 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001008
1009 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1011 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001013 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1014 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001015
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001016 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001017 return( ret );
1018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_RSA_C)
1020 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001024 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001025 return( ret );
1026 }
1027 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#endif /* MBEDTLS_RSA_C */
1029#if defined(MBEDTLS_ECP_C)
1030 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001031 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1033 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001035 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001036 return( ret );
1037 }
1038 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_ECP_C */
1040 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001042 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043}
1044
1045/*
1046 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001047 *
1048 * To save space, the decryption happens in-place on the given key buffer.
1049 * Also, while this function may modify the keybuffer, it doesn't own it,
1050 * and instead it is the responsibility of the caller to zeroize and properly
1051 * free it after use.
1052 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001053 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001056 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001057 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058 const unsigned char *pwd, size_t pwdlen )
1059{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001060 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001062 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001063 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001064 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1065#if defined(MBEDTLS_PKCS12_C)
1066 mbedtls_cipher_type_t cipher_alg;
1067 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001068#endif
1069
Hanno Becker2aa80a72017-09-07 15:28:45 +01001070 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001071 end = p + keylen;
1072
1073 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001074 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075
1076 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001077 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001078 *
1079 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1080 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1081 * encryptedData EncryptedData
1082 * }
1083 *
1084 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1085 *
1086 * EncryptedData ::= OCTET STRING
1087 *
1088 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001089 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001090 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1092 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001095 }
1096
1097 end = p + len;
1098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1100 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001102 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1103 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104
Hanno Beckerfab35692017-08-25 13:38:26 +01001105 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001106
1107 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001108 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001110#if defined(MBEDTLS_PKCS12_C)
1111 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001113 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001114 cipher_alg, md_alg,
1115 pwd, pwdlen, p, len, buf ) ) != 0 )
1116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001117 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1118 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119
1120 return( ret );
1121 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001122
1123 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001124 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001125 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001127 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1128 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001129 pwd, pwdlen,
1130 p, len, buf ) ) != 0 )
1131 {
1132 return( ret );
1133 }
1134
1135 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001137 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001138 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1139 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001140
1141 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142 }
1143 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001144#endif /* MBEDTLS_PKCS12_C */
1145#if defined(MBEDTLS_PKCS5_C)
1146 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001148 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149 p, len, buf ) ) != 0 )
1150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1152 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153
1154 return( ret );
1155 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001156
1157 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001158 }
1159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001160#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001161 {
1162 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001163 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001164
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001165 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001167
Paul Bakker1a7550a2013-09-15 13:01:22 +02001168 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1169}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001170#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001171
1172/*
1173 * Parse a private key
1174 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001175int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001176 const unsigned char *key, size_t keylen,
1177 const unsigned char *pwd, size_t pwdlen )
1178{
1179 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001180 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001182#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001184 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001189 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001190 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001191 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1192 else
1193 ret = mbedtls_pem_read_buffer( &pem,
1194 "-----BEGIN RSA PRIVATE KEY-----",
1195 "-----END RSA PRIVATE KEY-----",
1196 key, pwd, pwdlen, &len );
1197
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 if( ret == 0 )
1199 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001200 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001201 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203 pem.buf, pem.buflen ) ) != 0 )
1204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206 }
1207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001208 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209 return( ret );
1210 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1212 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1213 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1214 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1215 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001216 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001220 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001221 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001222 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1223 else
1224 ret = mbedtls_pem_read_buffer( &pem,
1225 "-----BEGIN EC PRIVATE KEY-----",
1226 "-----END EC PRIVATE KEY-----",
1227 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228 if( ret == 0 )
1229 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001230 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001231
Hanno Beckerfab35692017-08-25 13:38:26 +01001232 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001234 pem.buf, pem.buflen ) ) != 0 )
1235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001237 }
1238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001239 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001240 return( ret );
1241 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1243 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1244 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1245 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1246 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001247 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001249
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001250 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001251 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001252 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1253 else
1254 ret = mbedtls_pem_read_buffer( &pem,
1255 "-----BEGIN PRIVATE KEY-----",
1256 "-----END PRIVATE KEY-----",
1257 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 if( ret == 0 )
1259 {
1260 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1261 pem.buf, pem.buflen ) ) != 0 )
1262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264 }
1265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267 return( ret );
1268 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270 return( ret );
1271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001273 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001274 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001275 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1276 else
1277 ret = mbedtls_pem_read_buffer( &pem,
1278 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1279 "-----END ENCRYPTED PRIVATE KEY-----",
1280 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 if( ret == 0 )
1282 {
1283 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1284 pem.buf, pem.buflen,
1285 pwd, pwdlen ) ) != 0 )
1286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 }
1289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001290 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001291 return( ret );
1292 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296#else
1297 ((void) pwd);
1298 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300
1301 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001302 * At this point we only know it's not a PEM formatted key. Could be any
1303 * of the known DER encoded private key formats
1304 *
1305 * We try the different DER format parsers to see if one passes without
1306 * error
1307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001309 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001310 unsigned char *key_copy;
1311
Andres Amaya Garciae9124b92018-01-23 20:03:52 +00001312 if( keylen == 0 )
1313 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1314
Hanno Beckerfab35692017-08-25 13:38:26 +01001315 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1316 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1317
1318 memcpy( key_copy, key, keylen );
1319
1320 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1321 pwd, pwdlen );
1322
1323 mbedtls_zeroize( key_copy, keylen );
1324 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001325 }
1326
Hanno Beckerfab35692017-08-25 13:38:26 +01001327 if( ret == 0 )
1328 return( 0 );
1329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001331 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001333 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001334 {
1335 return( ret );
1336 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338
1339 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1340 return( 0 );
1341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001343 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001345#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346
Hanno Becker9be19262017-09-08 12:39:44 +01001347 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001348 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1349 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001350 {
1351 return( 0 );
1352 }
1353
Hanno Becker5cc4f762018-10-10 11:23:33 +01001354 mbedtls_pk_free( pk );
1355 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001359 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001360 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1361 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1362 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001363 {
1364 return( 0 );
1365 }
Hanno Becker5cc4f762018-10-10 11:23:33 +01001366 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368
Hanno Becker5cc4f762018-10-10 11:23:33 +01001369 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1370 * it is ok to leave the PK context initialized but not
1371 * freed: It is the caller's responsibility to call pk_init()
1372 * before calling this function, and to call pk_free()
1373 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1374 * isn't, this leads to mbedtls_pk_free() being called
1375 * twice, once here and once by the caller, but this is
1376 * also ok and in line with the mbedtls_pk_free() calls
1377 * on failed PEM parsing attempts. */
1378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380}
1381
1382/*
1383 * Parse a public key
1384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001386 const unsigned char *key, size_t keylen )
1387{
1388 int ret;
1389 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001391 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001395
1396 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001397 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001398 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1399 else
1400 ret = mbedtls_pem_read_buffer( &pem,
1401 "-----BEGIN PUBLIC KEY-----",
1402 "-----END PUBLIC KEY-----",
1403 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001404
1405 if( ret == 0 )
1406 {
1407 /*
1408 * Was PEM encoded
1409 */
1410 key = pem.buf;
1411 keylen = pem.buflen;
1412 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001413 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416 return( ret );
1417 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001418#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419 p = (unsigned char *) key;
1420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_PEM_PARSE_C)
1424 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001425#endif
1426
1427 return( ret );
1428}
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430#endif /* MBEDTLS_PK_PARSE_C */