blob: 7502fe7ffdc456befc65dc14aa65e715fe600b08 [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/*
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100667 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
668 *
669 * The value zero is:
670 * - never a valid value for an RSA parameter
671 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
672 *
673 * Since values can't be omitted in PKCS#1, passing a zero value to
674 * rsa_complete() would be incorrect, so reject zero values early.
675 */
676static int asn1_get_nonzero_mpi( unsigned char **p,
677 const unsigned char *end,
678 mbedtls_mpi *X )
679{
680 int ret;
681
682 ret = mbedtls_asn1_get_mpi( p, end, X );
683 if( ret != 0 )
684 return( ret );
685
686 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
687 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
688
689 return( 0 );
690}
691
692/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200693 * Parse a PKCS#1 encoded private RSA key
694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200696 const unsigned char *key,
697 size_t keylen )
698{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100699 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200700 size_t len;
701 unsigned char *p, *end;
702
Hanno Beckerefa14e82017-10-11 19:45:19 +0100703 mbedtls_mpi T;
704 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100705
Paul Bakker1a7550a2013-09-15 13:01:22 +0200706 p = (unsigned char *) key;
707 end = p + keylen;
708
709 /*
710 * This function parses the RSAPrivateKey (PKCS#1)
711 *
712 * RSAPrivateKey ::= SEQUENCE {
713 * version Version,
714 * modulus INTEGER, -- n
715 * publicExponent INTEGER, -- e
716 * privateExponent INTEGER, -- d
717 * prime1 INTEGER, -- p
718 * prime2 INTEGER, -- q
719 * exponent1 INTEGER, -- d mod (p-1)
720 * exponent2 INTEGER, -- d mod (q-1)
721 * coefficient INTEGER, -- (inverse of q) mod p
722 * otherPrimeInfos OtherPrimeInfos OPTIONAL
723 * }
724 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
726 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200729 }
730
731 end = p + len;
732
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100733 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200734 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200736 }
737
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100738 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200741 }
742
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100743 /* Import N */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100744 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
745 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
746 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100747 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200748
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100749 /* Import E */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100750 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
751 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
752 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100753 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100754
755 /* Import D */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100756 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
757 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
758 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100759 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760
761 /* Import P */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100762 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
763 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
764 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100765 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100766
767 /* Import Q */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100768 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
769 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
770 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100771 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100772
Manuel Pégourié-Gonnard609d79e2020-02-18 10:22:54 +0100773#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd100e1472020-01-29 13:13:04 -0500774 /*
775 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
776 * that they can be easily recomputed from D, P and Q. However by
777 * parsing them from the PKCS1 structure it is possible to avoid
778 * recalculating them which both reduces the overhead of loading
779 * RSA private keys into memory and also avoids side channels which
780 * can arise when computing those values, since all of D, P, and Q
781 * are secret. See https://eprint.iacr.org/2020/055 for a
782 * description of one such attack.
783 */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100784
Jack Lloyd100e1472020-01-29 13:13:04 -0500785 /* Import DP */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100786 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
787 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500788 goto cleanup;
789
790 /* Import DQ */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100791 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
792 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500793 goto cleanup;
794
795 /* Import QP */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100796 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
797 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500798 goto cleanup;
799
800#else
801 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnard8cc04912020-02-18 10:12:14 +0100802 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
803 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
804 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd100e1472020-01-29 13:13:04 -0500805 goto cleanup;
806#endif
807
Manuel Pégourié-Gonnard869e9662020-02-14 11:28:47 +0100808 /* rsa_complete() doesn't complete anything with the default
809 * implementation but is still called:
810 * - for the benefit of alternative implementation that may want to
811 * pre-compute stuff beyond what's provided (eg Montgomery factors)
812 * - as is also sanity-checks the key
813 *
814 * Furthermore, we also check the public part for consistency with
815 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
816 */
817 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
818 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
819 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100820 goto cleanup;
Manuel Pégourié-Gonnard869e9662020-02-14 11:28:47 +0100821 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200822
823 if( p != end )
824 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100825 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
826 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200827 }
828
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100829cleanup:
830
Hanno Beckerefa14e82017-10-11 19:45:19 +0100831 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100832
833 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100835 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100836 if( ( ret & 0xff80 ) == 0 )
837 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
838 else
839 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200841 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200842 }
843
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100844 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200846#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200847
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200848#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200849/*
850 * Parse a SEC1 encoded private EC key
851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 const unsigned char *key,
854 size_t keylen )
855{
856 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100857 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860 unsigned char *p = (unsigned char *) key;
861 unsigned char *end = p + keylen;
862 unsigned char *end2;
863
864 /*
865 * RFC 5915, or SEC1 Appendix C.4
866 *
867 * ECPrivateKey ::= SEQUENCE {
868 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
869 * privateKey OCTET STRING,
870 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
871 * publicKey [1] BIT STRING OPTIONAL
872 * }
873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
875 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200877 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878 }
879
880 end = p + len;
881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200882 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
883 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200884
885 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200886 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
889 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 mbedtls_ecp_keypair_free( eck );
894 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895 }
896
897 p += len;
898
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200899 pubkey_done = 0;
900 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200902 /*
903 * Is 'parameters' present?
904 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200905 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
906 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200907 {
908 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
909 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
910 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200911 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200912 return( ret );
913 }
914 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200915 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200916 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200917 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200918 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100919 }
Jethro Beekman004e3712018-02-16 13:11:04 -0800920 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200921
Jethro Beekman004e3712018-02-16 13:11:04 -0800922 if( p != end )
923 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200924 /*
925 * Is 'publickey' present? If not, or if we can't read it (eg because it
926 * is compressed), create it from the private key.
927 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200928 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
929 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200930 {
931 end2 = p + len;
932
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200933 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
934 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200935
936 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200937 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
938 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200939
940 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
941 pubkey_done = 1;
942 else
943 {
944 /*
945 * The only acceptable failure mode of pk_get_ecpubkey() above
946 * is if the point format is not recognized.
947 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200948 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
949 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200950 }
951 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200952 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200953 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200954 mbedtls_ecp_keypair_free( eck );
955 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200956 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200957 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100958
959 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100961 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 mbedtls_ecp_keypair_free( eck );
964 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200965 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200968 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200969 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200970 return( ret );
971 }
972
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200973 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200974}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200976
977/*
978 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100979 *
980 * Notes:
981 *
982 * - This function does not own the key buffer. It is the
983 * responsibility of the caller to take care of zeroizing
984 * and freeing it after use.
985 *
986 * - The function is responsible for freeing the provided
987 * PK context on failure.
988 *
Paul Bakker1a7550a2013-09-15 13:01:22 +0200989 */
990static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200992 const unsigned char* key,
993 size_t keylen )
994{
995 int ret, version;
996 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200997 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200998 unsigned char *p = (unsigned char *) key;
999 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001000 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1001 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001002
1003 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001004 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001005 *
1006 * PrivateKeyInfo ::= SEQUENCE {
1007 * version Version,
1008 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1009 * privateKey PrivateKey,
1010 * attributes [0] IMPLICIT Attributes OPTIONAL }
1011 *
1012 * Version ::= INTEGER
1013 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1014 * PrivateKey ::= OCTET STRING
1015 *
1016 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1017 */
1018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1020 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001023 }
1024
1025 end = p + len;
1026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1028 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029
1030 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032
1033 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001034 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1037 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
1039 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1041 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1044 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001045
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001046 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001047 return( ret );
1048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001049#if defined(MBEDTLS_RSA_C)
1050 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001055 return( ret );
1056 }
1057 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001058#endif /* MBEDTLS_RSA_C */
1059#if defined(MBEDTLS_ECP_C)
1060 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1063 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001066 return( ret );
1067 }
1068 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069#endif /* MBEDTLS_ECP_C */
1070 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001071
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001072 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001073}
1074
1075/*
1076 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001077 *
1078 * To save space, the decryption happens in-place on the given key buffer.
1079 * Also, while this function may modify the keybuffer, it doesn't own it,
1080 * and instead it is the responsibility of the caller to zeroize and properly
1081 * free it after use.
1082 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001083 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001084#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001085static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001087 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001088 const unsigned char *pwd, size_t pwdlen )
1089{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001090 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001091 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001092 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001093 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001094 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1095#if defined(MBEDTLS_PKCS12_C)
1096 mbedtls_cipher_type_t cipher_alg;
1097 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001098#endif
1099
Hanno Becker2aa80a72017-09-07 15:28:45 +01001100 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001101 end = p + keylen;
1102
1103 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001104 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001105
1106 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001107 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108 *
1109 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1110 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1111 * encryptedData EncryptedData
1112 * }
1113 *
1114 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1115 *
1116 * EncryptedData ::= OCTET STRING
1117 *
1118 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001119 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001121 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1122 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001124 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125 }
1126
1127 end = p + len;
1128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001129 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1130 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1133 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134
Hanno Beckerfab35692017-08-25 13:38:26 +01001135 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136
1137 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001138 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140#if defined(MBEDTLS_PKCS12_C)
1141 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144 cipher_alg, md_alg,
1145 pwd, pwdlen, p, len, buf ) ) != 0 )
1146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001147 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1148 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001149
1150 return( ret );
1151 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001152
1153 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001155 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001157 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1158 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001159 pwd, pwdlen,
1160 p, len, buf ) ) != 0 )
1161 {
1162 return( ret );
1163 }
1164
1165 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1169 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001170
1171 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001172 }
1173 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174#endif /* MBEDTLS_PKCS12_C */
1175#if defined(MBEDTLS_PKCS5_C)
1176 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001179 p, len, buf ) ) != 0 )
1180 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001181 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1182 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183
1184 return( ret );
1185 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001186
1187 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188 }
1189 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001190#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001191 {
1192 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001193 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001195 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001197
Paul Bakker1a7550a2013-09-15 13:01:22 +02001198 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1199}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001201
1202/*
1203 * Parse a private key
1204 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001205int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001206 const unsigned char *key, size_t keylen,
1207 const unsigned char *pwd, size_t pwdlen )
1208{
1209 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001212#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001213 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001219 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001220 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001221 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1222 else
1223 ret = mbedtls_pem_read_buffer( &pem,
1224 "-----BEGIN RSA PRIVATE KEY-----",
1225 "-----END RSA PRIVATE KEY-----",
1226 key, pwd, pwdlen, &len );
1227
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_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001231 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001232 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001233 pem.buf, pem.buflen ) ) != 0 )
1234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236 }
1237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001238 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001239 return( ret );
1240 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001241 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1242 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1243 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1244 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1245 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001246 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249#if defined(MBEDTLS_ECP_C)
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 EC PRIVATE KEY-----",
1256 "-----END EC PRIVATE KEY-----",
1257 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001258 if( ret == 0 )
1259 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001260 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261
Hanno Beckerfab35692017-08-25 13:38:26 +01001262 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264 pem.buf, pem.buflen ) ) != 0 )
1265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001267 }
1268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001270 return( ret );
1271 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1273 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1274 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1275 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1276 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001277 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001279
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001280 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001281 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001282 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1283 else
1284 ret = mbedtls_pem_read_buffer( &pem,
1285 "-----BEGIN PRIVATE KEY-----",
1286 "-----END PRIVATE KEY-----",
1287 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 if( ret == 0 )
1289 {
1290 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1291 pem.buf, pem.buflen ) ) != 0 )
1292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294 }
1295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297 return( ret );
1298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001299 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001300 return( ret );
1301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001302#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001303 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001304 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001305 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1306 else
1307 ret = mbedtls_pem_read_buffer( &pem,
1308 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1309 "-----END ENCRYPTED PRIVATE KEY-----",
1310 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311 if( ret == 0 )
1312 {
1313 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1314 pem.buf, pem.buflen,
1315 pwd, pwdlen ) ) != 0 )
1316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001317 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001318 }
1319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001321 return( ret );
1322 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001324 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001326#else
1327 ((void) pwd);
1328 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330
1331 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001332 * At this point we only know it's not a PEM formatted key. Could be any
1333 * of the known DER encoded private key formats
1334 *
1335 * We try the different DER format parsers to see if one passes without
1336 * error
1337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001339 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001340 unsigned char *key_copy;
1341
Andres Amaya Garciae9124b92018-01-23 20:03:52 +00001342 if( keylen == 0 )
1343 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1344
Hanno Beckerfab35692017-08-25 13:38:26 +01001345 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1346 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1347
1348 memcpy( key_copy, key, keylen );
1349
1350 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1351 pwd, pwdlen );
1352
1353 mbedtls_zeroize( key_copy, keylen );
1354 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001355 }
1356
Hanno Beckerfab35692017-08-25 13:38:26 +01001357 if( ret == 0 )
1358 return( 0 );
1359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001360 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001361 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364 {
1365 return( ret );
1366 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001368
1369 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1370 return( 0 );
1371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 mbedtls_pk_free( pk );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001373 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376
Hanno Becker9be19262017-09-08 12:39:44 +01001377 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001378 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1379 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001380 {
1381 return( 0 );
1382 }
1383
Hanno Becker5cc4f762018-10-10 11:23:33 +01001384 mbedtls_pk_free( pk );
1385 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001388#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001389 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker5cc4f762018-10-10 11:23:33 +01001390 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1391 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1392 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001393 {
1394 return( 0 );
1395 }
Hanno Becker5cc4f762018-10-10 11:23:33 +01001396 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001398
Hanno Becker5cc4f762018-10-10 11:23:33 +01001399 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1400 * it is ok to leave the PK context initialized but not
1401 * freed: It is the caller's responsibility to call pk_init()
1402 * before calling this function, and to call pk_free()
1403 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1404 * isn't, this leads to mbedtls_pk_free() being called
1405 * twice, once here and once by the caller, but this is
1406 * also ok and in line with the mbedtls_pk_free() calls
1407 * on failed PEM parsing attempts. */
1408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001410}
1411
1412/*
1413 * Parse a public key
1414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001415int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001416 const unsigned char *key, size_t keylen )
1417{
1418 int ret;
1419 unsigned char *p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422 mbedtls_pem_context pem;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424 mbedtls_pem_init( &pem );
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001425
1426 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Manuel Pégourié-Gonnard0ece0f92015-05-12 12:43:54 +02001427 if( keylen == 0 || key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001428 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1429 else
1430 ret = mbedtls_pem_read_buffer( &pem,
1431 "-----BEGIN PUBLIC KEY-----",
1432 "-----END PUBLIC KEY-----",
1433 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434
1435 if( ret == 0 )
1436 {
1437 /*
1438 * Was PEM encoded
1439 */
1440 key = pem.buf;
1441 keylen = pem.buflen;
1442 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001443 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446 return( ret );
1447 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 p = (unsigned char *) key;
1450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001452
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001453#if defined(MBEDTLS_PEM_PARSE_C)
1454 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001455#endif
1456
1457 return( ret );
1458}
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_PK_PARSE_C */