blob: 03d597293c1ded3521700766015f6e69269c613e [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020022#include "common.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/pk.h"
27#include "mbedtls/asn1.h"
28#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050029#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000030#include "mbedtls/error.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <string.h>
33
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
52
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020055#else
56#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020057#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020059#endif
60
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050061/* Parameter validation macros based on platform_util.h */
62#define PK_VALIDATE_RET( cond ) \
63 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
64#define PK_VALIDATE( cond ) \
65 MBEDTLS_INTERNAL_VALIDATE( cond )
66
Gilles Peskine832f3492017-11-30 11:42:12 +010067#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020068/*
69 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020070 *
71 * The file is expected to contain either PEM or DER encoded data.
72 * A terminating null byte is always appended. It is included in the announced
73 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020076{
77 FILE *f;
78 long size;
79
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050080 PK_VALIDATE_RET( path != NULL );
81 PK_VALIDATE_RET( buf != NULL );
82 PK_VALIDATE_RET( n != NULL );
83
Paul Bakker1a7550a2013-09-15 13:01:22 +020084 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
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500108 mbedtls_platform_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{
Janos Follath24eed8d2019-11-22 13:21:35 +0000130 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131 size_t n;
132 unsigned char *buf;
133
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 PK_VALIDATE_RET( ctx != NULL );
135 PK_VALIDATE_RET( path != NULL );
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200138 return( ret );
139
140 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144 (const unsigned char *) pwd, strlen( pwd ) );
145
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500146 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148
149 return( ret );
150}
151
152/*
153 * Load and parse a public key
154 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200156{
Janos Follath24eed8d2019-11-22 13:21:35 +0000157 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200158 size_t n;
159 unsigned char *buf;
160
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 PK_VALIDATE_RET( ctx != NULL );
162 PK_VALIDATE_RET( path != NULL );
163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200165 return( ret );
166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200168
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500169 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171
172 return( ret );
173}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_ECP_C)
177/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200178 *
179 * ECParameters ::= CHOICE {
180 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100181 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200182 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200183 * }
184 */
185static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200187{
Janos Follath24eed8d2019-11-22 13:21:35 +0000188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200189
Sanne Woudab2b29d52017-08-21 15:58:12 +0100190 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100191 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
192 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100193
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100194 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200195 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 if( params->tag != MBEDTLS_ASN1_OID
197#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
198 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100199#endif
200 )
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 +
203 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100204 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100209 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210
211 params->p = *p;
212 *p += params->len;
213
214 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
216 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200217
218 return( 0 );
219}
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200222/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100223 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
224 * WARNING: the resulting group should only be used with
225 * pk_group_id_from_specified(), since its base point may not be set correctly
226 * if it was encoded compressed.
227 *
228 * SpecifiedECDomain ::= SEQUENCE {
229 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
230 * fieldID FieldID {{FieldTypes}},
231 * curve Curve,
232 * base ECPoint,
233 * order INTEGER,
234 * cofactor INTEGER OPTIONAL,
235 * hash HashAlgorithm OPTIONAL,
236 * ...
237 * }
238 *
239 * We only support prime-field as field type, and ignore hash and cofactor.
240 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100242{
Janos Follath24eed8d2019-11-22 13:21:35 +0000243 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100244 unsigned char *p = params->p;
245 const unsigned char * const end = params->p + params->len;
246 const unsigned char *end_field, *end_curve;
247 size_t len;
248 int ver;
249
250 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
252 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100253
254 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100256
257 /*
258 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
259 * fieldType FIELD-ID.&id({IOSet}),
260 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
261 * }
262 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
264 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100265 return( ret );
266
267 end_field = p + len;
268
269 /*
270 * FIELD-ID ::= TYPE-IDENTIFIER
271 * FieldTypes FIELD-ID ::= {
272 * { Prime-p IDENTIFIED BY prime-field } |
273 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
274 * }
275 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100278 return( ret );
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
281 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284 }
285
286 p += len;
287
288 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
290 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100291
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200292 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100293
294 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
296 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100297
298 /*
299 * Curve ::= SEQUENCE {
300 * a FieldElement,
301 * b FieldElement,
302 * seed BIT STRING OPTIONAL
303 * -- Shall be present if used in SpecifiedECDomain
304 * -- with version equal to ecdpVer2 or ecdpVer3
305 * }
306 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
308 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100309 return( ret );
310
311 end_curve = p + len;
312
313 /*
314 * FieldElement ::= OCTET STRING
315 * containing an integer in the case of a prime field
316 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
318 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100321 }
322
323 p += len;
324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
326 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100329 }
330
331 p += len;
332
333 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100335 p += len;
336
337 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
339 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340
341 /*
342 * ECPoint ::= OCTET STRING
343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
345 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100348 ( const unsigned char *) p, len ) ) != 0 )
349 {
350 /*
351 * If we can't read the point because it's compressed, cheat by
352 * reading only the X coordinate and the parity bit of Y.
353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100355 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 len != mbedtls_mpi_size( &grp->P ) + 1 ||
357 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
358 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
359 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100362 }
363 }
364
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100365 p += len;
366
367 /*
368 * order INTEGER
369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
371 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100372
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200373 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100374
375 /*
376 * Allow optional elements by purposefully not enforcing p == end here.
377 */
378
379 return( 0 );
380}
381
382/*
383 * Find the group id associated with an (almost filled) group as generated by
384 * pk_group_from_specified(), or return an error if unknown.
385 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386static 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 +0100387{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100388 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ecp_group ref;
390 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200394 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100395 {
396 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200398 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100399
400 /* Compare to the group we were given, starting with easy tests */
401 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
403 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
404 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
405 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
406 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
407 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100408 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409 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 +0100410 {
411 break;
412 }
413
414 }
415
416cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100418
419 *grp_id = *id;
420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
422 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100423
424 return( ret );
425}
426
427/*
428 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
429 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
431 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100432{
Janos Follath24eed8d2019-11-22 13:21:35 +0000433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200434 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100437
438 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
439 goto cleanup;
440
441 ret = pk_group_id_from_group( &grp, grp_id );
442
443cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100445
446 return( ret );
447}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100449
450/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200451 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100452 *
453 * ECParameters ::= CHOICE {
454 * namedCurve OBJECT IDENTIFIER
455 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
456 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200457 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200459{
Janos Follath24eed8d2019-11-22 13:21:35 +0000460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
466 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100467 }
468 else
469 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100471 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
472 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100473#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100475#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100476 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200477
478 /*
479 * grp may already be initilialized; if so, make sure IDs match
480 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
482 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200483
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200484 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200485 return( ret );
486
487 return( 0 );
488}
489
490/*
491 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100492 *
493 * The caller is responsible for clearing the structure upon failure if
494 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200496 */
497static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200499{
Janos Follath24eed8d2019-11-22 13:21:35 +0000500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100503 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200504 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200506 }
507
508 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200510 */
511 *p = (unsigned char *) end;
512
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100513 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200518/*
519 * RSAPublicKey ::= SEQUENCE {
520 * modulus INTEGER, -- n
521 * publicExponent INTEGER -- e
522 * }
523 */
524static int pk_get_rsapubkey( unsigned char **p,
525 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527{
Janos Follath24eed8d2019-11-22 13:21:35 +0000528 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200529 size_t len;
530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
532 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
533 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200534
535 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
537 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200538
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100539 /* Import N */
540 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200542
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100543 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
544 NULL, 0, NULL, 0 ) ) != 0 )
545 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
546
547 *p += len;
548
549 /* Import E */
550 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
551 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
552
553 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
554 NULL, 0, *p, len ) ) != 0 )
555 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
556
557 *p += len;
558
Hanno Becker895c5ab2018-01-05 08:08:09 +0000559 if( mbedtls_rsa_complete( rsa ) != 0 ||
560 mbedtls_rsa_check_pubkey( rsa ) != 0 )
561 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100562 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000563 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100564
Paul Bakker1a7550a2013-09-15 13:01:22 +0200565 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
567 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200568
Paul Bakker1a7550a2013-09-15 13:01:22 +0200569 return( 0 );
570}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200572
573/* Get a PK algorithm identifier
574 *
575 * AlgorithmIdentifier ::= SEQUENCE {
576 * algorithm OBJECT IDENTIFIER,
577 * parameters ANY DEFINED BY algorithm OPTIONAL }
578 */
579static int pk_get_pk_alg( unsigned char **p,
580 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200582{
Janos Follath24eed8d2019-11-22 13:21:35 +0000583 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586 memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
589 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
592 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200593
594 /*
595 * No parameters with RSA (only for EC)
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 if( *pk_alg == MBEDTLS_PK_RSA &&
598 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200599 params->len != 0 ) )
600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200602 }
603
604 return( 0 );
605}
606
607/*
608 * SubjectPublicKeyInfo ::= SEQUENCE {
609 * algorithm AlgorithmIdentifier,
610 * subjectPublicKey BIT STRING }
611 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
613 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200614{
Janos Follath24eed8d2019-11-22 13:21:35 +0000615 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200616 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_asn1_buf alg_params;
618 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
619 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200620
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500621 PK_VALIDATE_RET( p != NULL );
622 PK_VALIDATE_RET( *p != NULL );
623 PK_VALIDATE_RET( end != NULL );
624 PK_VALIDATE_RET( pk != NULL );
625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
627 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200630 }
631
632 end = *p + len;
633
634 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
635 return( ret );
636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
638 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639
640 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
642 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200644 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
645 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200646
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200647 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200648 return( ret );
649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650#if defined(MBEDTLS_RSA_C)
651 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200652 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200653 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200654 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655#endif /* MBEDTLS_RSA_C */
656#if defined(MBEDTLS_ECP_C)
657 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200660 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200662 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_ECP_C */
664 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200665
666 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
668 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200669
670 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200672
673 return( ret );
674}
675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200677/*
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100678 * Wrapper around mbedtls_asn1_get_mpi() that rejects zero.
679 *
680 * The value zero is:
681 * - never a valid value for an RSA parameter
682 * - interpreted as "omitted, please reconstruct" by mbedtls_rsa_complete().
683 *
684 * Since values can't be omitted in PKCS#1, passing a zero value to
685 * rsa_complete() would be incorrect, so reject zero values early.
686 */
687static int asn1_get_nonzero_mpi( unsigned char **p,
688 const unsigned char *end,
689 mbedtls_mpi *X )
690{
691 int ret;
692
693 ret = mbedtls_asn1_get_mpi( p, end, X );
694 if( ret != 0 )
695 return( ret );
696
697 if( mbedtls_mpi_cmp_int( X, 0 ) == 0 )
698 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
699
700 return( 0 );
701}
702
703/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200704 * Parse a PKCS#1 encoded private RSA key
705 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200707 const unsigned char *key,
708 size_t keylen )
709{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100710 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200711 size_t len;
712 unsigned char *p, *end;
713
Hanno Beckerefa14e82017-10-11 19:45:19 +0100714 mbedtls_mpi T;
715 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100716
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717 p = (unsigned char *) key;
718 end = p + keylen;
719
720 /*
721 * This function parses the RSAPrivateKey (PKCS#1)
722 *
723 * RSAPrivateKey ::= SEQUENCE {
724 * version Version,
725 * modulus INTEGER, -- n
726 * publicExponent INTEGER, -- e
727 * privateExponent INTEGER, -- d
728 * prime1 INTEGER, -- p
729 * prime2 INTEGER, -- q
730 * exponent1 INTEGER, -- d mod (p-1)
731 * exponent2 INTEGER, -- d mod (q-1)
732 * coefficient INTEGER, -- (inverse of q) mod p
733 * otherPrimeInfos OtherPrimeInfos OPTIONAL
734 * }
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
737 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200740 }
741
742 end = p + len;
743
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100744 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747 }
748
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100749 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200752 }
753
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100754 /* Import N */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100755 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
756 ( ret = mbedtls_rsa_import( rsa, &T, NULL, NULL,
757 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100758 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200759
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100760 /* Import E */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100761 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
762 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
763 NULL, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100764 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100765
766 /* Import D */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100767 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
768 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, NULL,
769 &T, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100770 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100771
772 /* Import P */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100773 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
774 ( ret = mbedtls_rsa_import( rsa, NULL, &T, NULL,
775 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100776 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100777
778 /* Import Q */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100779 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
780 ( ret = mbedtls_rsa_import( rsa, NULL, NULL, &T,
781 NULL, NULL ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100782 goto cleanup;
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100783
Manuel Pégourié-Gonnardbbb5a0a2020-02-18 10:22:54 +0100784#if !defined(MBEDTLS_RSA_NO_CRT) && !defined(MBEDTLS_RSA_ALT)
Jack Lloyd8c2631b2020-01-23 17:23:52 -0500785 /*
786 * The RSA CRT parameters DP, DQ and QP are nominally redundant, in
787 * that they can be easily recomputed from D, P and Q. However by
788 * parsing them from the PKCS1 structure it is possible to avoid
789 * recalculating them which both reduces the overhead of loading
790 * RSA private keys into memory and also avoids side channels which
791 * can arise when computing those values, since all of D, P, and Q
792 * are secret. See https://eprint.iacr.org/2020/055 for a
793 * description of one such attack.
794 */
795
Jack Lloyd80cc8112020-01-22 17:34:29 -0500796 /* Import DP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100797 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
798 ( ret = mbedtls_mpi_copy( &rsa->DP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500799 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500800
801 /* Import DQ */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100802 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
803 ( ret = mbedtls_mpi_copy( &rsa->DQ, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500804 goto cleanup;
Jack Lloyd80cc8112020-01-22 17:34:29 -0500805
806 /* Import QP */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100807 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
808 ( ret = mbedtls_mpi_copy( &rsa->QP, &T ) ) != 0 )
Jack Lloyd2e9eef42020-01-28 14:43:52 -0500809 goto cleanup;
810
Jack Lloyd60239752020-01-27 17:53:36 -0500811#else
812 /* Verify existance of the CRT params */
Manuel Pégourié-Gonnarda04a2c32020-02-18 10:12:14 +0100813 if( ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
814 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 ||
815 ( ret = asn1_get_nonzero_mpi( &p, end, &T ) ) != 0 )
Jack Lloyd60239752020-01-27 17:53:36 -0500816 goto cleanup;
817#endif
Jack Lloyd80cc8112020-01-22 17:34:29 -0500818
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100819 /* rsa_complete() doesn't complete anything with the default
820 * implementation but is still called:
821 * - for the benefit of alternative implementation that may want to
822 * pre-compute stuff beyond what's provided (eg Montgomery factors)
823 * - as is also sanity-checks the key
824 *
825 * Furthermore, we also check the public part for consistency with
826 * mbedtls_pk_parse_pubkey(), as it includes size minima for example.
827 */
828 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ||
829 ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
830 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100831 goto cleanup;
Manuel Pégourié-Gonnardc4226792020-02-14 11:28:47 +0100832 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100833
Paul Bakker1a7550a2013-09-15 13:01:22 +0200834 if( p != end )
835 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100836 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
837 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200838 }
839
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100840cleanup:
841
Hanno Beckerefa14e82017-10-11 19:45:19 +0100842 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100843
844 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200845 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100846 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100847 if( ( ret & 0xff80 ) == 0 )
848 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
849 else
850 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200852 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200853 }
854
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100855 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200856}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200857#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200859#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860/*
861 * Parse a SEC1 encoded private EC key
862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200863static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200864 const unsigned char *key,
865 size_t keylen )
866{
Janos Follath24eed8d2019-11-22 13:21:35 +0000867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100868 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200869 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200870 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200871 unsigned char *p = (unsigned char *) key;
872 unsigned char *end = p + keylen;
873 unsigned char *end2;
874
875 /*
876 * RFC 5915, or SEC1 Appendix C.4
877 *
878 * ECPrivateKey ::= SEQUENCE {
879 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
880 * privateKey OCTET STRING,
881 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
882 * publicKey [1] BIT STRING OPTIONAL
883 * }
884 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200885 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
886 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200888 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200889 }
890
891 end = p + len;
892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200893 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
894 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200895
896 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200897 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200899 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
900 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200902 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200904 mbedtls_ecp_keypair_free( eck );
905 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200906 }
907
908 p += len;
909
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200910 pubkey_done = 0;
911 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200912 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200913 /*
914 * Is 'parameters' present?
915 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200916 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
917 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200918 {
919 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
920 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
921 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200922 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200923 return( ret );
924 }
925 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200926 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200928 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100930 }
Jethro Beekmand2df9362018-02-16 13:11:04 -0800931 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200932
Jethro Beekmand2df9362018-02-16 13:11:04 -0800933 if( p != end )
934 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200935 /*
936 * Is 'publickey' present? If not, or if we can't read it (eg because it
937 * is compressed), create it from the private key.
938 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200939 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
940 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200941 {
942 end2 = p + len;
943
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200944 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
945 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200946
947 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200948 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
949 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200950
951 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
952 pubkey_done = 1;
953 else
954 {
955 /*
956 * The only acceptable failure mode of pk_get_ecpubkey() above
957 * is if the point format is not recognized.
958 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200959 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
960 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200961 }
962 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200963 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200964 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +0200965 mbedtls_ecp_keypair_free( eck );
966 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +0200967 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200968 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100969
970 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100972 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 mbedtls_ecp_keypair_free( eck );
975 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +0200976 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200981 return( ret );
982 }
983
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200984 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200985}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200987
988/*
989 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +0100990 *
991 * Notes:
992 *
993 * - This function does not own the key buffer. It is the
994 * responsibility of the caller to take care of zeroizing
995 * and freeing it after use.
996 *
997 * - The function is responsible for freeing the provided
998 * PK context on failure.
999 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000 */
1001static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001003 const unsigned char* key,
1004 size_t keylen )
1005{
1006 int ret, version;
1007 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 unsigned char *p = (unsigned char *) key;
1010 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
1012 const mbedtls_pk_info_t *pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001013
1014 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001015 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001016 *
1017 * PrivateKeyInfo ::= SEQUENCE {
1018 * version Version,
1019 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1020 * privateKey PrivateKey,
1021 * attributes [0] IMPLICIT Attributes OPTIONAL }
1022 *
1023 * Version ::= INTEGER
1024 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1025 * PrivateKey ::= OCTET STRING
1026 *
1027 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1028 */
1029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1031 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001034 }
1035
1036 end = p + len;
1037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1039 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001040
1041 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043
1044 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001045 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1048 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001049
1050 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1052 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
1055 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001056
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001057 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001058 return( ret );
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_RSA_C)
1061 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001063 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *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_RSA_C */
1070#if defined(MBEDTLS_ECP_C)
1071 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001073 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1074 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001077 return( ret );
1078 }
1079 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#endif /* MBEDTLS_ECP_C */
1081 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001082
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001083 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001084}
1085
1086/*
1087 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001088 *
1089 * To save space, the decryption happens in-place on the given key buffer.
1090 * Also, while this function may modify the keybuffer, it doesn't own it,
1091 * and instead it is the responsibility of the caller to zeroize and properly
1092 * free it after use.
1093 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001096static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001097 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001098 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001099 const unsigned char *pwd, size_t pwdlen )
1100{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001101 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001102 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001103 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001104 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001105 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1106#if defined(MBEDTLS_PKCS12_C)
1107 mbedtls_cipher_type_t cipher_alg;
1108 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001109#endif
1110
Hanno Becker2aa80a72017-09-07 15:28:45 +01001111 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001112 end = p + keylen;
1113
1114 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001116
1117 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001118 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119 *
1120 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1121 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1122 * encryptedData EncryptedData
1123 * }
1124 *
1125 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1126 *
1127 * EncryptedData ::= OCTET STRING
1128 *
1129 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001130 *
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,
1133 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001135 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001136 }
1137
1138 end = p + len;
1139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001140 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1141 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1144 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001145
Hanno Beckerfab35692017-08-25 13:38:26 +01001146 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001147
1148 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001149 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001151#if defined(MBEDTLS_PKCS12_C)
1152 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001154 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001155 cipher_alg, md_alg,
1156 pwd, pwdlen, p, len, buf ) ) != 0 )
1157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001158 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1159 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001160
1161 return( ret );
1162 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001163
1164 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001165 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001166 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001168 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1169 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001170 pwd, pwdlen,
1171 p, len, buf ) ) != 0 )
1172 {
1173 return( ret );
1174 }
1175
1176 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001177 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001178 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1180 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001181
1182 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001183 }
1184 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001185#endif /* MBEDTLS_PKCS12_C */
1186#if defined(MBEDTLS_PKCS5_C)
1187 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001189 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190 p, len, buf ) ) != 0 )
1191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1193 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194
1195 return( ret );
1196 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001197
1198 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 }
1200 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001202 {
1203 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001204 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001206 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001207 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001208
Paul Bakker1a7550a2013-09-15 13:01:22 +02001209 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001211#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001212
1213/*
1214 * Parse a private key
1215 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001217 const unsigned char *key, size_t keylen,
1218 const unsigned char *pwd, size_t pwdlen )
1219{
Janos Follath24eed8d2019-11-22 13:21:35 +00001220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221 const mbedtls_pk_info_t *pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001222#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001225#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001227 PK_VALIDATE_RET( pk != NULL );
1228 if( keylen == 0 )
1229 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1230 PK_VALIDATE_RET( key != NULL );
1231
1232#if defined(MBEDTLS_PEM_PARSE_C)
1233 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001235#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001236 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001237 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001238 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1239 else
1240 ret = mbedtls_pem_read_buffer( &pem,
1241 "-----BEGIN RSA PRIVATE KEY-----",
1242 "-----END RSA PRIVATE KEY-----",
1243 key, pwd, pwdlen, &len );
1244
Paul Bakker1a7550a2013-09-15 13:01:22 +02001245 if( ret == 0 )
1246 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001247 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001248 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001250 pem.buf, pem.buflen ) ) != 0 )
1251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001253 }
1254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001255 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256 return( ret );
1257 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1259 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1260 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1261 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1262 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001263 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001267 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001268 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001269 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1270 else
1271 ret = mbedtls_pem_read_buffer( &pem,
1272 "-----BEGIN EC PRIVATE KEY-----",
1273 "-----END EC PRIVATE KEY-----",
1274 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001275 if( ret == 0 )
1276 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001277 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001278
Hanno Beckerfab35692017-08-25 13:38:26 +01001279 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001281 pem.buf, pem.buflen ) ) != 0 )
1282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001284 }
1285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001287 return( ret );
1288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1290 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1291 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1292 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1293 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_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001296
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001297 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001298 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001299 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1300 else
1301 ret = mbedtls_pem_read_buffer( &pem,
1302 "-----BEGIN PRIVATE KEY-----",
1303 "-----END PRIVATE KEY-----",
1304 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 if( ret == 0 )
1306 {
1307 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1308 pem.buf, pem.buflen ) ) != 0 )
1309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001311 }
1312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001314 return( ret );
1315 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001316 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 return( ret );
1318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001320 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001321 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001322 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1323 else
1324 ret = mbedtls_pem_read_buffer( &pem,
1325 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1326 "-----END ENCRYPTED PRIVATE KEY-----",
1327 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001328 if( ret == 0 )
1329 {
1330 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1331 pem.buf, pem.buflen,
1332 pwd, pwdlen ) ) != 0 )
1333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 }
1336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001338 return( ret );
1339 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001341 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001343#else
1344 ((void) pwd);
1345 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001346#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001347
1348 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001349 * At this point we only know it's not a PEM formatted key. Could be any
1350 * of the known DER encoded private key formats
1351 *
1352 * We try the different DER format parsers to see if one passes without
1353 * error
1354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001355#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001356 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001357 unsigned char *key_copy;
1358
1359 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1360 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1361
1362 memcpy( key_copy, key, keylen );
1363
1364 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1365 pwd, pwdlen );
1366
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001367 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001368 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369 }
1370
Hanno Beckerfab35692017-08-25 13:38:26 +01001371 if( ret == 0 )
1372 return( 0 );
1373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001374 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001375 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001377 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001378 {
1379 return( ret );
1380 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
1383 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
1384 return( 0 );
1385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001387 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390
Hanno Becker9be19262017-09-08 12:39:44 +01001391 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001392 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1393 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001394 {
1395 return( 0 );
1396 }
1397
Hanno Becker780f0a42018-10-10 11:23:33 +01001398 mbedtls_pk_free( pk );
1399 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001400#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001402#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001403 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001404 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1405 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1406 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001407 {
1408 return( 0 );
1409 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001410 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#endif /* MBEDTLS_ECP_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412
Hanno Becker780f0a42018-10-10 11:23:33 +01001413 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1414 * it is ok to leave the PK context initialized but not
1415 * freed: It is the caller's responsibility to call pk_init()
1416 * before calling this function, and to call pk_free()
1417 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1418 * isn't, this leads to mbedtls_pk_free() being called
1419 * twice, once here and once by the caller, but this is
1420 * also ok and in line with the mbedtls_pk_free() calls
1421 * on failed PEM parsing attempts. */
1422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001424}
1425
1426/*
1427 * Parse a public key
1428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001429int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001430 const unsigned char *key, size_t keylen )
1431{
Janos Follath24eed8d2019-11-22 13:21:35 +00001432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001433 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001434#if defined(MBEDTLS_RSA_C)
1435 const mbedtls_pk_info_t *pk_info;
1436#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001438 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 mbedtls_pem_context pem;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001440#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001441
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001442 PK_VALIDATE_RET( ctx != NULL );
1443 if( keylen == 0 )
1444 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1445 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1446
1447#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001449#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001450 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001451 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001452 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1453 else
1454 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001455 "-----BEGIN RSA PUBLIC KEY-----",
1456 "-----END RSA PUBLIC KEY-----",
1457 key, NULL, 0, &len );
1458
1459 if( ret == 0 )
1460 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001461 p = pem.buf;
1462 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1463 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001464
Ron Eldor84df1ae2017-10-16 17:11:52 +03001465 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1466 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001467
Ron Eldor84df1ae2017-10-16 17:11:52 +03001468 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1469 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001470
Ron Eldord0c56de2017-10-10 17:03:08 +03001471 mbedtls_pem_free( &pem );
1472 return( ret );
1473 }
1474 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1475 {
1476 mbedtls_pem_free( &pem );
1477 return( ret );
1478 }
1479#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001480
1481 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001482 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001483 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1484 else
1485 ret = mbedtls_pem_read_buffer( &pem,
1486 "-----BEGIN PUBLIC KEY-----",
1487 "-----END PUBLIC KEY-----",
1488 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001489
1490 if( ret == 0 )
1491 {
1492 /*
1493 * Was PEM encoded
1494 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001495 p = pem.buf;
1496
1497 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1498 mbedtls_pem_free( &pem );
1499 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001500 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001504 return( ret );
1505 }
Ron Eldor5472d432017-10-17 09:49:00 +03001506 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001508
1509#if defined(MBEDTLS_RSA_C)
1510 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
1511 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1512
1513 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1514 return( ret );
1515
Ron Eldor9566ff72018-02-07 18:59:41 +02001516 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001517 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001518 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001519 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001520 return( ret );
1521 }
1522 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001523 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001524 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001525 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001526 }
1527#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001528 p = (unsigned char *) key;
1529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001530 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531
Paul Bakker1a7550a2013-09-15 13:01:22 +02001532 return( ret );
1533}
1534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001535#endif /* MBEDTLS_PK_PARSE_C */