blob: 4562f6547a8158c743877edc83f7889eef62fb11 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +02001/*
2 * Public Key layer for parsing key files and structures
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker1a7550a2013-09-15 13:01:22 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PK_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pk.h"
31#include "mbedtls/asn1.h"
32#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050033#include "mbedtls/platform_util.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/rsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/ecdsa.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020045#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000047#include "mbedtls/pem.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020048#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/pkcs5.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if defined(MBEDTLS_PKCS12_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/pkcs12.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020054#endif
Jarno Lamsa42b83db2019-04-16 16:48:22 +030055#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker496b83f2019-08-20 13:33:49 +010056#include "tinycrypt/ecc.h"
Jarno Lamsa42b83db2019-04-16 16:48:22 +030057#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/platform.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +020061#else
62#include <stdlib.h>
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020063#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#define mbedtls_free free
Paul Bakker1a7550a2013-09-15 13:01:22 +020065#endif
66
Gilles Peskinee97dc602018-12-19 00:51:38 +010067/* Parameter validation macros based on platform_util.h */
68#define PK_VALIDATE_RET( cond ) \
69 MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
70#define PK_VALIDATE( cond ) \
71 MBEDTLS_INTERNAL_VALIDATE( cond )
72
Gilles Peskine832f3492017-11-30 11:42:12 +010073#if defined(MBEDTLS_FS_IO)
Paul Bakker1a7550a2013-09-15 13:01:22 +020074/*
75 * Load all data from a file into a given buffer.
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +020076 *
77 * The file is expected to contain either PEM or DER encoded data.
78 * A terminating null byte is always appended. It is included in the announced
79 * length only if the data looks like it is PEM encoded.
Paul Bakker1a7550a2013-09-15 13:01:22 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
Paul Bakker1a7550a2013-09-15 13:01:22 +020082{
83 FILE *f;
84 long size;
85
Gilles Peskinee97dc602018-12-19 00:51:38 +010086 PK_VALIDATE_RET( path != NULL );
87 PK_VALIDATE_RET( buf != NULL );
88 PK_VALIDATE_RET( n != NULL );
89
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 if( ( f = fopen( path, "rb" ) ) == NULL )
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_END );
94 if( ( size = ftell( f ) ) == -1 )
95 {
96 fclose( f );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +020098 }
99 fseek( f, 0, SEEK_SET );
100
101 *n = (size_t) size;
102
103 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200104 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105 {
106 fclose( f );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200107 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108 }
109
110 if( fread( *buf, 1, *n, f ) != *n )
111 {
112 fclose( f );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100113
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500114 mbedtls_platform_zeroize( *buf, *n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_free( *buf );
Andres Amaya Garcia1f2666f2017-06-26 10:36:20 +0100116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118 }
119
120 fclose( f );
121
122 (*buf)[*n] = '\0';
123
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +0200124 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
125 ++*n;
126
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127 return( 0 );
128}
129
130/*
131 * Load and parse a private key
132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134 const char *path, const char *pwd )
135{
136 int ret;
137 size_t n;
138 unsigned char *buf;
139
Gilles Peskinee97dc602018-12-19 00:51:38 +0100140 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100141 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200144 return( ret );
145
146 if( pwd == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200148 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 ret = mbedtls_pk_parse_key( ctx, buf, n,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200150 (const unsigned char *) pwd, strlen( pwd ) );
151
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500152 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200154
155 return( ret );
156}
157
158/*
159 * Load and parse a public key
160 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200162{
163 int ret;
164 size_t n;
165 unsigned char *buf;
166
Gilles Peskinee97dc602018-12-19 00:51:38 +0100167 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine8c71b3e2018-12-19 17:37:02 +0100168 PK_VALIDATE_RET( path != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +0100169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200171 return( ret );
172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200174
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500175 mbedtls_platform_zeroize( buf, n );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 mbedtls_free( buf );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200177
178 return( ret );
179}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#endif /* MBEDTLS_FS_IO */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200181
Jarno Lamsab1760922019-04-18 15:58:34 +0300182#if defined(MBEDTLS_USE_TINYCRYPT)
183static int pk_use_ecparams( const mbedtls_asn1_buf *params )
184{
Hanno Beckerdfb949b2019-08-23 15:22:25 +0100185 mbedtls_uecc_group_id grp_id;
Jarno Lamsab1760922019-04-18 15:58:34 +0300186
187 if( params->tag == MBEDTLS_ASN1_OID )
188 {
189 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
190 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
191 }
192 else
193 {
194 // Only P-256 is supported
195 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
196 }
197
198 return( 0 );
199}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100200#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +0300201
202#if defined(MBEDTLS_ECP_C) || \
203 defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
Paul Bakker1a7550a2013-09-15 13:01:22 +0200205 *
206 * ECParameters ::= CHOICE {
207 * namedCurve OBJECT IDENTIFIER
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100208 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200209 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200210 * }
211 */
212static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200214{
215 int ret;
216
Sanne Woudab2b29d52017-08-21 15:58:12 +0100217 if ( end - *p < 1 )
Sanne Wouda7b2e85d2017-08-30 21:10:42 +0100218 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
219 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Sanne Woudab2b29d52017-08-21 15:58:12 +0100220
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100221 /* Tag may be either OID or SEQUENCE */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200222 params->tag = **p;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223 if( params->tag != MBEDTLS_ASN1_OID
224#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
225 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100226#endif
227 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
230 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100231 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100236 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200237
238 params->p = *p;
239 *p += params->len;
240
241 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
243 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200244
245 return( 0 );
246}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100247#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200248
Hanno Becker3bef6432019-08-21 11:47:37 +0100249#if !defined(MBEDTLS_USE_TINYCRYPT)
250
Jarno Lamsab1760922019-04-18 15:58:34 +0300251#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200253/*
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100254 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
255 * WARNING: the resulting group should only be used with
256 * pk_group_id_from_specified(), since its base point may not be set correctly
257 * if it was encoded compressed.
258 *
259 * SpecifiedECDomain ::= SEQUENCE {
260 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
261 * fieldID FieldID {{FieldTypes}},
262 * curve Curve,
263 * base ECPoint,
264 * order INTEGER,
265 * cofactor INTEGER OPTIONAL,
266 * hash HashAlgorithm OPTIONAL,
267 * ...
268 * }
269 *
270 * We only support prime-field as field type, and ignore hash and cofactor.
271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100273{
274 int ret;
275 unsigned char *p = params->p;
276 const unsigned char * const end = params->p + params->len;
277 const unsigned char *end_field, *end_curve;
278 size_t len;
279 int ver;
280
281 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
283 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100284
285 if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100287
288 /*
289 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
290 * fieldType FIELD-ID.&id({IOSet}),
291 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
292 * }
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
295 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100296 return( ret );
297
298 end_field = p + len;
299
300 /*
301 * FIELD-ID ::= TYPE-IDENTIFIER
302 * FieldTypes FIELD-ID ::= {
303 * { Prime-p IDENTIFIED BY prime-field } |
304 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
305 * }
306 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100309 return( ret );
310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
Teppo Järvelin61f412e2019-10-03 12:25:22 +0300312 mbedtls_platform_memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, 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_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100315 }
316
317 p += len;
318
319 /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
321 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100322
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200323 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100324
325 if( p != end_field )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
327 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100328
329 /*
330 * Curve ::= SEQUENCE {
331 * a FieldElement,
332 * b FieldElement,
333 * seed BIT STRING OPTIONAL
334 * -- Shall be present if used in SpecifiedECDomain
335 * -- with version equal to ecdpVer2 or ecdpVer3
336 * }
337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
339 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100340 return( ret );
341
342 end_curve = p + len;
343
344 /*
345 * FieldElement ::= OCTET STRING
346 * containing an integer in the case of a prime field
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
349 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100352 }
353
354 p += len;
355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
357 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100360 }
361
362 p += len;
363
364 /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100366 p += len;
367
368 if( p != end_curve )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
370 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100371
372 /*
373 * ECPoint ::= OCTET STRING
374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
376 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100379 ( const unsigned char *) p, len ) ) != 0 )
380 {
381 /*
382 * If we can't read the point because it's compressed, cheat by
383 * reading only the X coordinate and the parity bit of Y.
384 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100386 ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 len != mbedtls_mpi_size( &grp->P ) + 1 ||
388 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
389 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
390 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100393 }
394 }
395
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100396 p += len;
397
398 /*
399 * order INTEGER
400 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
402 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100403
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200404 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100405
406 /*
407 * Allow optional elements by purposefully not enforcing p == end here.
408 */
409
410 return( 0 );
411}
412
413/*
414 * Find the group id associated with an (almost filled) group as generated by
415 * pk_group_from_specified(), or return an error if unknown.
416 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200417static 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 +0100418{
Manuel Pégourié-Gonnard5b8c4092014-03-27 14:59:42 +0100419 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200420 mbedtls_ecp_group ref;
421 const mbedtls_ecp_group_id *id;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200423 mbedtls_ecp_group_init( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100426 {
427 /* Load the group associated to that id */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200429 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100430
431 /* Compare to the group we were given, starting with easy tests */
432 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200433 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
434 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
435 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
436 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
437 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
438 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100439 /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440 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 +0100441 {
442 break;
443 }
444
445 }
446
447cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 mbedtls_ecp_group_free( &ref );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100449
450 *grp_id = *id;
451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
453 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100454
455 return( ret );
456}
457
458/*
459 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
460 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200461static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
462 mbedtls_ecp_group_id *grp_id )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100463{
464 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 mbedtls_ecp_group grp;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467 mbedtls_ecp_group_init( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100468
469 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
470 goto cleanup;
471
472 ret = pk_group_id_from_group( &grp, grp_id );
473
474cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 mbedtls_ecp_group_free( &grp );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100476
477 return( ret );
478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100480
481/*
Paul Bakker1a7550a2013-09-15 13:01:22 +0200482 * Use EC parameters to initialise an EC group
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100483 *
484 * ECParameters ::= CHOICE {
485 * namedCurve OBJECT IDENTIFIER
486 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
487 * -- implicitCurve NULL
Paul Bakker1a7550a2013-09-15 13:01:22 +0200488 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200489static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200490{
491 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_ecp_group_id grp_id;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 if( params->tag == MBEDTLS_ASN1_OID )
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
497 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100498 }
499 else
500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100502 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
503 return( ret );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100504#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard6fac3512014-03-19 16:39:52 +0100506#endif
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +0100507 }
Paul Bakker1a7550a2013-09-15 13:01:22 +0200508
509 /*
510 * grp may already be initilialized; if so, make sure IDs match
511 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
513 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200514
Manuel Pégourié-Gonnarde3a062b2015-05-11 18:46:47 +0200515 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200516 return( ret );
517
518 return( 0 );
519}
520
521/*
522 * EC public key is an EC point
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100523 *
524 * The caller is responsible for clearing the structure upon failure if
525 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
Paul Bakker1a7550a2013-09-15 13:01:22 +0200527 */
528static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_ecp_keypair *key )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200530{
531 int ret;
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100534 (const unsigned char *) *p, end - *p ) ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200535 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200537 }
538
539 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
Paul Bakker1a7550a2013-09-15 13:01:22 +0200541 */
542 *p = (unsigned char *) end;
543
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +0100544 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200545}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#endif /* MBEDTLS_ECP_C */
Hanno Becker3bef6432019-08-21 11:47:37 +0100547#endif /* !MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200548
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300549#if defined(MBEDTLS_USE_TINYCRYPT)
550/*
551 * Import a point from unsigned binary data (SEC1 2.3.4)
552 */
Hanno Becker7e38c372019-08-20 17:01:50 +0100553static int uecc_public_key_read_binary( mbedtls_uecc_keypair *uecc_keypair,
554 const unsigned char *buf, size_t ilen )
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300555{
Hanno Becker68d54782019-08-20 13:19:09 +0100556 if( ilen != 2 * NUM_ECC_BYTES + 1 )
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300557 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
558
Hanno Beckerad353f22019-08-20 13:04:30 +0100559 /* We are not handling the point at infinity. */
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300560
561 if( buf[0] != 0x04 )
562 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
563
Teppo Järvelin91d79382019-10-02 09:09:31 +0300564 mbedtls_platform_memcpy( uecc_keypair->public_key, buf + 1, 2 * NUM_ECC_BYTES );
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300565
566 return( 0 );
567}
568
569static int pk_get_ueccpubkey( unsigned char **p,
570 const unsigned char *end,
571 uint8_t *pk_context)
572{
Hanno Becker7e38c372019-08-20 17:01:50 +0100573 mbedtls_uecc_keypair *uecc_keypair = (mbedtls_uecc_keypair *) pk_context;
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300574 int ret;
575
Hanno Becker7e38c372019-08-20 17:01:50 +0100576 ret = uecc_public_key_read_binary( uecc_keypair,
577 (const unsigned char *) *p, end - *p );
Jarno Lamsa42b83db2019-04-16 16:48:22 +0300578
579 /*
580 * We know uecc_public_key_read_binary consumed all bytes or failed
581 */
582 *p = (unsigned char *) end;
583
584 return( ret );
585}
586#endif /* MBEDTLS_USE_TINYCRYPT */
587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200589/*
590 * RSAPublicKey ::= SEQUENCE {
591 * modulus INTEGER, -- n
592 * publicExponent INTEGER -- e
593 * }
594 */
595static int pk_get_rsapubkey( unsigned char **p,
596 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 mbedtls_rsa_context *rsa )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200598{
599 int ret;
600 size_t len;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
603 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
604 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200605
606 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
608 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200609
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100610 /* Import N */
611 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200613
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100614 if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0,
615 NULL, 0, NULL, 0 ) ) != 0 )
616 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
617
618 *p += len;
619
620 /* Import E */
621 if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 )
622 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
623
624 if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
625 NULL, 0, *p, len ) ) != 0 )
626 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
627
628 *p += len;
629
Hanno Becker895c5ab2018-01-05 08:08:09 +0000630 if( mbedtls_rsa_complete( rsa ) != 0 ||
631 mbedtls_rsa_check_pubkey( rsa ) != 0 )
632 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100633 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
Hanno Becker895c5ab2018-01-05 08:08:09 +0000634 }
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100635
Paul Bakker1a7550a2013-09-15 13:01:22 +0200636 if( *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
638 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200639
Paul Bakker1a7550a2013-09-15 13:01:22 +0200640 return( 0 );
641}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200643
644/* Get a PK algorithm identifier
645 *
646 * AlgorithmIdentifier ::= SEQUENCE {
647 * algorithm OBJECT IDENTIFIER,
648 * parameters ANY DEFINED BY algorithm OPTIONAL }
649 */
650static int pk_get_pk_alg( unsigned char **p,
651 const unsigned char *end,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200652 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200653{
654 int ret;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655 mbedtls_asn1_buf alg_oid;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200656
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200657 mbedtls_platform_memset( params, 0, sizeof(mbedtls_asn1_buf) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
660 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
663 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200664
665 /*
666 * No parameters with RSA (only for EC)
667 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200668 if( *pk_alg == MBEDTLS_PK_RSA &&
669 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
Paul Bakker1a7550a2013-09-15 13:01:22 +0200670 params->len != 0 ) )
671 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200672 return( MBEDTLS_ERR_PK_INVALID_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200673 }
674
675 return( 0 );
676}
677
678/*
679 * SubjectPublicKeyInfo ::= SEQUENCE {
680 * algorithm AlgorithmIdentifier,
681 * subjectPublicKey BIT STRING }
682 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
684 mbedtls_pk_context *pk )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200685{
686 int ret;
687 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_asn1_buf alg_params;
689 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200690 mbedtls_pk_handle_t pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200691
Gilles Peskinee97dc602018-12-19 00:51:38 +0100692 PK_VALIDATE_RET( p != NULL );
693 PK_VALIDATE_RET( *p != NULL );
694 PK_VALIDATE_RET( end != NULL );
695 PK_VALIDATE_RET( pk != NULL );
696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
698 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200701 }
702
703 end = *p + len;
704
705 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
706 return( ret );
707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
709 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200710
711 if( *p + len != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
713 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200714
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +0200715 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200717
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200718 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200719 return( ret );
720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200721#if defined(MBEDTLS_RSA_C)
722 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200723 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200725 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200726#endif /* MBEDTLS_RSA_C */
Hanno Becker8cf2f5e2019-08-21 11:51:53 +0100727#if defined(MBEDTLS_USE_TINYCRYPT)
728 if( pk_alg == MBEDTLS_PK_ECKEY )
729 {
730 ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx );
731 } else
732#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_ECP_C)
734 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200737 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200738 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200739 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740#endif /* MBEDTLS_ECP_C */
Hanno Becker8cf2f5e2019-08-21 11:51:53 +0100741#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200743
744 if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
746 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200747
748 if( ret != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200750
751 return( ret );
752}
753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200755/*
756 * Parse a PKCS#1 encoded private RSA key
757 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200758static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
Paul Bakker1a7550a2013-09-15 13:01:22 +0200759 const unsigned char *key,
760 size_t keylen )
761{
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100762 int ret, version;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200763 size_t len;
764 unsigned char *p, *end;
765
Hanno Beckerefa14e82017-10-11 19:45:19 +0100766 mbedtls_mpi T;
767 mbedtls_mpi_init( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100768
Paul Bakker1a7550a2013-09-15 13:01:22 +0200769 p = (unsigned char *) key;
770 end = p + keylen;
771
772 /*
773 * This function parses the RSAPrivateKey (PKCS#1)
774 *
775 * RSAPrivateKey ::= SEQUENCE {
776 * version Version,
777 * modulus INTEGER, -- n
778 * publicExponent INTEGER, -- e
779 * privateExponent INTEGER, -- d
780 * prime1 INTEGER, -- p
781 * prime2 INTEGER, -- q
782 * exponent1 INTEGER, -- d mod (p-1)
783 * exponent2 INTEGER, -- d mod (q-1)
784 * coefficient INTEGER, -- (inverse of q) mod p
785 * otherPrimeInfos OtherPrimeInfos OPTIONAL
786 * }
787 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
789 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200790 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200792 }
793
794 end = p + len;
795
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100796 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200799 }
800
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100801 if( version != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200804 }
805
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100806 /* Import N */
807 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
808 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
809 ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0,
810 NULL, 0, NULL, 0 ) ) != 0 )
811 goto cleanup;
812 p += len;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200813
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100814 /* Import E */
815 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
816 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
817 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
818 NULL, 0, p, len ) ) != 0 )
819 goto cleanup;
820 p += len;
821
822 /* Import D */
823 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
824 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
825 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0,
826 p, len, NULL, 0 ) ) != 0 )
827 goto cleanup;
828 p += len;
829
830 /* Import P */
831 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
832 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
833 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0,
834 NULL, 0, NULL, 0 ) ) != 0 )
835 goto cleanup;
836 p += len;
837
838 /* Import Q */
839 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
840 MBEDTLS_ASN1_INTEGER ) ) != 0 ||
841 ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len,
842 NULL, 0, NULL, 0 ) ) != 0 )
843 goto cleanup;
844 p += len;
845
846 /* Complete the RSA private key */
Hanno Becker7f25f852017-10-10 16:56:22 +0100847 if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100848 goto cleanup;
849
850 /* Check optional parameters */
Hanno Beckerefa14e82017-10-11 19:45:19 +0100851 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
852 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ||
853 ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 )
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100854 goto cleanup;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200855
856 if( p != end )
857 {
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100858 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
859 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200860 }
861
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100862cleanup:
863
Hanno Beckerefa14e82017-10-11 19:45:19 +0100864 mbedtls_mpi_free( &T );
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100865
866 if( ret != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +0200867 {
Hanno Beckerefa14e82017-10-11 19:45:19 +0100868 /* Wrap error code if it's coming from a lower level */
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100869 if( ( ret & 0xff80 ) == 0 )
870 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret;
871 else
872 ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200874 mbedtls_rsa_free( rsa );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200875 }
876
Hanno Beckerd58c5b22017-08-22 14:33:21 +0100877 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +0200878}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200879#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +0200880
Jarno Lamsab1760922019-04-18 15:58:34 +0300881#if defined(MBEDTLS_USE_TINYCRYPT)
882static int pk_parse_key_sec1_der( mbedtls_uecc_keypair *keypair,
883 const unsigned char *key,
884 size_t keylen)
885{
886 int ret;
887 int version, pubkey_done;
888 size_t len;
889 mbedtls_asn1_buf params;
890 unsigned char *p = (unsigned char *) key;
891 unsigned char *end = p + keylen;
892 unsigned char *end2;
893
894 /*
895 * RFC 5915, or SEC1 Appendix C.4
896 *
897 * ECPrivateKey ::= SEQUENCE {
898 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
899 * privateKey OCTET STRING,
900 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
901 * publicKey [1] BIT STRING OPTIONAL
902 * }
903 */
904 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
905 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
906 {
907 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
908 }
909
910 end = p + len;
911
912 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
913 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
914
915 if( version != 1 )
916 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
917
918 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
919 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
920
Teppo Järvelin91d79382019-10-02 09:09:31 +0300921 mbedtls_platform_memcpy( keypair->private_key, p, len );
Jarno Lamsab1760922019-04-18 15:58:34 +0300922
923 p += len;
924
925 pubkey_done = 0;
926 if( p != end )
927 {
928 /*
929 * Is 'parameters' present?
930 */
931 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
932 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
933 {
934 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
935 ( ret = pk_use_ecparams( &params ) ) != 0 )
936 {
937 return( ret );
938 }
939 }
940 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
941 {
942 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
943 }
944 }
945
946 if( p != end )
947 {
948 /*
949 * Is 'publickey' present? If not, or if we can't read it (eg because it
950 * is compressed), create it from the private key.
951 */
952 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
953 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
954 {
955 end2 = p + len;
956
957 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
958 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
959
960 if( p + len != end2 )
961 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
962 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
963
Hanno Becker7e38c372019-08-20 17:01:50 +0100964 if( ( ret = uecc_public_key_read_binary( keypair,
Jarno Lamsab1760922019-04-18 15:58:34 +0300965 (const unsigned char *) p, end2 - p ) ) == 0 )
Hanno Beckerad353f22019-08-20 13:04:30 +0100966 {
Jarno Lamsab1760922019-04-18 15:58:34 +0300967 pubkey_done = 1;
Hanno Beckerad353f22019-08-20 13:04:30 +0100968 }
Jarno Lamsab1760922019-04-18 15:58:34 +0300969 else
970 {
971 /*
Hanno Becker683d84a2019-09-04 16:10:46 +0100972 * The only acceptable failure mode of
973 * uecc_public_key_read_binary() above
Jarno Lamsab1760922019-04-18 15:58:34 +0300974 * is if the point format is not recognized.
975 */
976 if( ret != MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE )
977 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
978 }
979 }
980 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
981 {
982 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
983 }
984 }
985
Hanno Beckerd45f3832019-08-20 14:21:40 +0100986 if( !pubkey_done )
987 {
988 ret = uECC_compute_public_key( keypair->private_key,
989 keypair->public_key,
990 uECC_secp256r1() );
991 if( ret == 0 )
992 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
993 }
Jarno Lamsab1760922019-04-18 15:58:34 +0300994
995 return( 0 );
996}
Hanno Beckeraebffdd2019-08-21 12:13:44 +0100997#else /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsab1760922019-04-18 15:58:34 +0300998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#if defined(MBEDTLS_ECP_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001000/*
1001 * Parse a SEC1 encoded private EC key
1002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001003static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001004 const unsigned char *key,
1005 size_t keylen )
1006{
1007 int ret;
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001008 int version, pubkey_done;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001009 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001011 unsigned char *p = (unsigned char *) key;
1012 unsigned char *end = p + keylen;
1013 unsigned char *end2;
1014
1015 /*
1016 * RFC 5915, or SEC1 Appendix C.4
1017 *
1018 * ECPrivateKey ::= SEQUENCE {
1019 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1020 * privateKey OCTET STRING,
1021 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1022 * publicKey [1] BIT STRING OPTIONAL
1023 * }
1024 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001025 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1026 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001029 }
1030
1031 end = p + len;
1032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1034 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001035
1036 if( version != 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1040 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001043 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001044 mbedtls_ecp_keypair_free( eck );
1045 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001046 }
1047
1048 p += len;
1049
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001050 pubkey_done = 0;
1051 if( p != end )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001052 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001053 /*
1054 * Is 'parameters' present?
1055 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001056 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1057 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001058 {
1059 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
1060 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
1061 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001062 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001063 return( ret );
1064 }
1065 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001066 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001068 mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001069 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard5246ee52014-03-19 16:18:38 +01001070 }
Jethro Beekmand2df9362018-02-16 13:11:04 -08001071 }
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001072
Jethro Beekmand2df9362018-02-16 13:11:04 -08001073 if( p != end )
1074 {
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001075 /*
1076 * Is 'publickey' present? If not, or if we can't read it (eg because it
1077 * is compressed), create it from the private key.
1078 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001079 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1080 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001081 {
1082 end2 = p + len;
1083
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001084 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
1085 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001086
1087 if( p + len != end2 )
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001088 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1089 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001090
1091 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
1092 pubkey_done = 1;
1093 else
1094 {
1095 /*
1096 * The only acceptable failure mode of pk_get_ecpubkey() above
1097 * is if the point format is not recognized.
1098 */
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001099 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
1100 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001101 }
1102 }
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001103 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001104 {
Manuel Pégourié-Gonnarde1e58712015-04-15 10:50:34 +02001105 mbedtls_ecp_keypair_free( eck );
1106 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnard924cd102015-04-14 11:18:04 +02001107 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001108 }
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001109
1110 if( ! pubkey_done &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
Manuel Pégourié-Gonnardeab20d22014-03-14 17:58:42 +01001112 NULL, NULL ) ) != 0 )
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001113 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001114 mbedtls_ecp_keypair_free( eck );
1115 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Manuel Pégourié-Gonnardff29f9c2013-09-18 16:13:02 +02001116 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001118 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001120 mbedtls_ecp_keypair_free( eck );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001121 return( ret );
1122 }
1123
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001124 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001125}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001126#endif /* MBEDTLS_ECP_C */
Jarno Lamsab1760922019-04-18 15:58:34 +03001127#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001128
1129/*
1130 * Parse an unencrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001131 *
1132 * Notes:
1133 *
1134 * - This function does not own the key buffer. It is the
1135 * responsibility of the caller to take care of zeroizing
1136 * and freeing it after use.
1137 *
1138 * - The function is responsible for freeing the provided
1139 * PK context on failure.
1140 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001141 */
1142static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001143 mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001144 const unsigned char* key,
1145 size_t keylen )
1146{
1147 int ret, version;
1148 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001149 mbedtls_asn1_buf params;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001150 unsigned char *p = (unsigned char *) key;
1151 unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001152 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001153 mbedtls_pk_handle_t pk_info;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001154
1155 /*
Hanno Becker9c6cb382017-09-05 10:08:01 +01001156 * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001157 *
1158 * PrivateKeyInfo ::= SEQUENCE {
1159 * version Version,
1160 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
1161 * privateKey PrivateKey,
1162 * attributes [0] IMPLICIT Attributes OPTIONAL }
1163 *
1164 * Version ::= INTEGER
1165 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
1166 * PrivateKey ::= OCTET STRING
1167 *
1168 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
1169 */
1170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001171 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1172 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001175 }
1176
1177 end = p + len;
1178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001179 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
1180 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001181
1182 if( version != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001183 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001184
1185 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001186 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001188 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1189 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001190
1191 if( len < 1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001192 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
1193 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001194
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001195 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == MBEDTLS_PK_INVALID_HANDLE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001197
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +02001198 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001199 return( ret );
1200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201#if defined(MBEDTLS_RSA_C)
1202 if( pk_alg == MBEDTLS_PK_RSA )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001204 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001206 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001207 return( ret );
1208 }
1209 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210#endif /* MBEDTLS_RSA_C */
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001211#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Becker75f8d322019-08-20 14:31:50 +01001212 if( pk_alg == MBEDTLS_PK_ECKEY )
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001213 {
1214 if( ( ret = pk_use_ecparams( &params ) ) != 0 ||
Hanno Beckerda779712019-08-21 13:22:59 +01001215 ( ret = pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk ), p, len ) ) != 0)
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001216 {
1217 return( ret );
1218 }
Hanno Beckere65697c2019-08-20 14:31:00 +01001219 } else
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001220#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001221#if defined(MBEDTLS_ECP_C)
1222 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001223 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001224 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
1225 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001228 return( ret );
1229 }
1230 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001231#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001232#endif /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001233 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001234
Paul Bakkerd8bb8262014-06-17 14:06:49 +02001235 return( 0 );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001236}
1237
1238/*
1239 * Parse an encrypted PKCS#8 encoded private key
Hanno Beckerb4274212017-09-29 19:18:51 +01001240 *
1241 * To save space, the decryption happens in-place on the given key buffer.
1242 * Also, while this function may modify the keybuffer, it doesn't own it,
1243 * and instead it is the responsibility of the caller to zeroize and properly
1244 * free it after use.
1245 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001246 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001248static int pk_parse_key_pkcs8_encrypted_der(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 mbedtls_pk_context *pk,
Hanno Beckerfab35692017-08-25 13:38:26 +01001250 unsigned char *key, size_t keylen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001251 const unsigned char *pwd, size_t pwdlen )
1252{
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001253 int ret, decrypted = 0;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001254 size_t len;
Hanno Beckerfab35692017-08-25 13:38:26 +01001255 unsigned char *buf;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001256 unsigned char *p, *end;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
1258#if defined(MBEDTLS_PKCS12_C)
1259 mbedtls_cipher_type_t cipher_alg;
1260 mbedtls_md_type_t md_alg;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001261#endif
1262
Hanno Becker2aa80a72017-09-07 15:28:45 +01001263 p = key;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001264 end = p + keylen;
1265
1266 if( pwdlen == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001268
1269 /*
Hanno Beckerf04111f2017-09-29 19:18:42 +01001270 * This function parses the EncryptedPrivateKeyInfo object (PKCS#8)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001271 *
1272 * EncryptedPrivateKeyInfo ::= SEQUENCE {
1273 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
1274 * encryptedData EncryptedData
1275 * }
1276 *
1277 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
1278 *
1279 * EncryptedData ::= OCTET STRING
1280 *
1281 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
Hanno Beckerb8d16572017-09-07 15:29:01 +01001282 *
Paul Bakker1a7550a2013-09-15 13:01:22 +02001283 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001284 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
1285 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001288 }
1289
1290 end = p + len;
1291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
1293 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
1296 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001297
Hanno Beckerfab35692017-08-25 13:38:26 +01001298 buf = p;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001299
1300 /*
Hanno Beckerb8d16572017-09-07 15:29:01 +01001301 * Decrypt EncryptedData with appropriate PBE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_PKCS12_C)
1304 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001307 cipher_alg, md_alg,
1308 pwd, pwdlen, p, len, buf ) ) != 0 )
1309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
1311 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001312
1313 return( ret );
1314 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001315
1316 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001317 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
1321 MBEDTLS_PKCS12_PBE_DECRYPT,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001322 pwd, pwdlen,
1323 p, len, buf ) ) != 0 )
1324 {
1325 return( ret );
1326 }
1327
1328 // Best guess for password mismatch when using RC4. If first tag is
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001329 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
Paul Bakker1a7550a2013-09-15 13:01:22 +02001330 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001331 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
1332 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001333
1334 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001335 }
1336 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#endif /* MBEDTLS_PKCS12_C */
1338#if defined(MBEDTLS_PKCS5_C)
1339 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001341 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001342 p, len, buf ) ) != 0 )
1343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
1345 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001346
1347 return( ret );
1348 }
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001349
1350 decrypted = 1;
Paul Bakker1a7550a2013-09-15 13:01:22 +02001351 }
1352 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#endif /* MBEDTLS_PKCS5_C */
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001354 {
1355 ((void) pwd);
Manuel Pégourié-Gonnard1032c1d2013-09-18 17:18:34 +02001356 }
Paul Bakker1a7550a2013-09-15 13:01:22 +02001357
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001358 if( decrypted == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
Paul Bakkerf4cf80b2014-04-17 17:19:56 +02001360
Paul Bakker1a7550a2013-09-15 13:01:22 +02001361 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
1362}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001364
1365/*
1366 * Parse a private key
1367 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001368int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001369 const unsigned char *key, size_t keylen,
1370 const unsigned char *pwd, size_t pwdlen )
1371{
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001372#if defined(MBEDTLS_PKCS12_C) || \
1373 defined(MBEDTLS_PKCS5_C) || \
1374 defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001375 int ret;
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001376#endif
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001377 mbedtls_pk_handle_t pk_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001379 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001380 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001381#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001382
Gilles Peskinee97dc602018-12-19 00:51:38 +01001383 PK_VALIDATE_RET( pk != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001384 if( keylen == 0 )
1385 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
1386 PK_VALIDATE_RET( key != NULL );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001387
1388#if defined(MBEDTLS_PEM_PARSE_C)
1389 mbedtls_pem_init( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001392 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001393 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001394 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1395 else
1396 ret = mbedtls_pem_read_buffer( &pem,
1397 "-----BEGIN RSA PRIVATE KEY-----",
1398 "-----END RSA PRIVATE KEY-----",
1399 key, pwd, pwdlen, &len );
1400
Paul Bakker1a7550a2013-09-15 13:01:22 +02001401 if( ret == 0 )
1402 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001403 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Beckerfab35692017-08-25 13:38:26 +01001404 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001406 pem.buf, pem.buflen ) ) != 0 )
1407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001409 }
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001412 return( ret );
1413 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1415 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1416 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1417 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1418 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001419 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001421
Hanno Beckerd336f722019-08-21 11:46:11 +01001422#if defined(MBEDTLS_ECP_C) || defined(MBEDTLS_USE_TINYCRYPT)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001423 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001424 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001425 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1426 else
1427 ret = mbedtls_pem_read_buffer( &pem,
1428 "-----BEGIN EC PRIVATE KEY-----",
1429 "-----END EC PRIVATE KEY-----",
1430 key, pwd, pwdlen, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001431 if( ret == 0 )
1432 {
Hanno Becker66a0f832017-09-08 12:39:21 +01001433 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001434
Hanno Beckerd336f722019-08-21 11:46:11 +01001435#if defined(MBEDTLS_USE_TINYCRYPT)
1436 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Hanno Beckerda779712019-08-21 13:22:59 +01001437 ( ret = pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk ),
Hanno Beckerd336f722019-08-21 11:46:11 +01001438 pem.buf, pem.buflen ) ) != 0 )
1439#else /* MBEDTLS_USE_TINYCRYPT */
Hanno Beckerfab35692017-08-25 13:38:26 +01001440 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
Paul Bakker1a7550a2013-09-15 13:01:22 +02001442 pem.buf, pem.buflen ) ) != 0 )
Hanno Beckerd336f722019-08-21 11:46:11 +01001443#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001444 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001445 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001446 }
1447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001449 return( ret );
1450 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001451 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
1452 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
1453 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
1454 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
1455 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001456 return( ret );
Hanno Beckerd336f722019-08-21 11:46:11 +01001457#endif /* MBEDTLS_ECP_C || MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001458
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001459 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001460 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001461 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1462 else
1463 ret = mbedtls_pem_read_buffer( &pem,
1464 "-----BEGIN PRIVATE KEY-----",
1465 "-----END PRIVATE KEY-----",
1466 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001467 if( ret == 0 )
1468 {
1469 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
1470 pem.buf, pem.buflen ) ) != 0 )
1471 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001472 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001473 }
1474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001475 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001476 return( ret );
1477 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001478 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001479 return( ret );
1480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001481#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001482 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001483 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001484 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1485 else
1486 ret = mbedtls_pem_read_buffer( &pem,
1487 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
1488 "-----END ENCRYPTED PRIVATE KEY-----",
1489 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001490 if( ret == 0 )
1491 {
1492 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
1493 pem.buf, pem.buflen,
1494 pwd, pwdlen ) ) != 0 )
1495 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001496 mbedtls_pk_free( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001497 }
1498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001499 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001500 return( ret );
1501 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001502 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001503 return( ret );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001504#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001505#else
1506 ((void) pwd);
1507 ((void) pwdlen);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_PEM_PARSE_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001509
1510 /*
Brian J Murray2adecba2016-11-06 04:45:15 -08001511 * At this point we only know it's not a PEM formatted key. Could be any
1512 * of the known DER encoded private key formats
1513 *
1514 * We try the different DER format parsers to see if one passes without
1515 * error
1516 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001518 {
Hanno Beckerfab35692017-08-25 13:38:26 +01001519 unsigned char *key_copy;
1520
1521 if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
1522 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
1523
Teppo Järvelin91d79382019-10-02 09:09:31 +03001524 mbedtls_platform_memcpy( key_copy, key, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001525
1526 ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
1527 pwd, pwdlen );
1528
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001529 mbedtls_platform_zeroize( key_copy, keylen );
Hanno Beckerfab35692017-08-25 13:38:26 +01001530 mbedtls_free( key_copy );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001531 }
1532
Hanno Beckerfab35692017-08-25 13:38:26 +01001533 if( ret == 0 )
1534 return( 0 );
1535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001537 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001540 {
1541 return( ret );
1542 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001544
Manuel Pégourié-Gonnarde83b2c22019-06-18 11:31:59 +02001545 if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001546 return( 0 );
1547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001548 mbedtls_pk_free( pk );
Hanno Becker780f0a42018-10-10 11:23:33 +01001549 mbedtls_pk_init( pk );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#if defined(MBEDTLS_RSA_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001552
Hanno Becker9be19262017-09-08 12:39:44 +01001553 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA );
Hanno Becker780f0a42018-10-10 11:23:33 +01001554 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1555 pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001556 {
1557 return( 0 );
1558 }
1559
Hanno Becker780f0a42018-10-10 11:23:33 +01001560 mbedtls_pk_free( pk );
1561 mbedtls_pk_init( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001563
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001564#if defined(MBEDTLS_USE_TINYCRYPT)
Hanno Beckeradf11e12019-08-21 13:03:44 +01001565 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001566 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
Hanno Beckerda779712019-08-21 13:22:59 +01001567 pk_parse_key_sec1_der( mbedtls_pk_uecc( *pk),
Jarno Lamsa9c9e77a2019-04-18 16:13:19 +03001568 key, keylen) == 0)
1569 {
1570 return( 0 );
1571 }
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001572#else /* MBEDTLS_USE_TINYCRYPT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_ECP_C)
Hanno Becker9be19262017-09-08 12:39:44 +01001574 pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY );
Hanno Becker780f0a42018-10-10 11:23:33 +01001575 if( mbedtls_pk_setup( pk, pk_info ) == 0 &&
1576 pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
1577 key, keylen ) == 0 )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001578 {
1579 return( 0 );
1580 }
Hanno Becker780f0a42018-10-10 11:23:33 +01001581 mbedtls_pk_free( pk );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_ECP_C */
Hanno Beckeraebffdd2019-08-21 12:13:44 +01001583#endif /* MBEDTLS_USE_TINYCRYPT */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001584
Hanno Becker780f0a42018-10-10 11:23:33 +01001585 /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't,
1586 * it is ok to leave the PK context initialized but not
1587 * freed: It is the caller's responsibility to call pk_init()
1588 * before calling this function, and to call pk_free()
1589 * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C
1590 * isn't, this leads to mbedtls_pk_free() being called
1591 * twice, once here and once by the caller, but this is
1592 * also ok and in line with the mbedtls_pk_free() calls
1593 * on failed PEM parsing attempts. */
1594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001596}
1597
1598/*
1599 * Parse a public key
1600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
Paul Bakker1a7550a2013-09-15 13:01:22 +02001602 const unsigned char *key, size_t keylen )
1603{
1604 int ret;
1605 unsigned char *p;
Ron Eldor5472d432017-10-17 09:49:00 +03001606#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001607 mbedtls_pk_handle_t pk_info;
Ron Eldor5472d432017-10-17 09:49:00 +03001608#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_PEM_PARSE_C)
Paul Bakker1a7550a2013-09-15 13:01:22 +02001610 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 mbedtls_pem_context pem;
Gilles Peskinee97dc602018-12-19 00:51:38 +01001612#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +02001613
Gilles Peskinee97dc602018-12-19 00:51:38 +01001614 PK_VALIDATE_RET( ctx != NULL );
Gilles Peskine159171b2018-12-19 17:03:28 +01001615 if( keylen == 0 )
1616 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
Gilles Peskinee97dc602018-12-19 00:51:38 +01001617 PK_VALIDATE_RET( key != NULL || keylen == 0 );
1618
1619#if defined(MBEDTLS_PEM_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 mbedtls_pem_init( &pem );
Ron Eldord0c56de2017-10-10 17:03:08 +03001621#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001622 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001623 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001624 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1625 else
1626 ret = mbedtls_pem_read_buffer( &pem,
Ron Eldord0c56de2017-10-10 17:03:08 +03001627 "-----BEGIN RSA PUBLIC KEY-----",
1628 "-----END RSA PUBLIC KEY-----",
1629 key, NULL, 0, &len );
1630
1631 if( ret == 0 )
1632 {
Ron Eldor84df1ae2017-10-16 17:11:52 +03001633 p = pem.buf;
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001634 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == MBEDTLS_PK_INVALID_HANDLE )
Ron Eldor84df1ae2017-10-16 17:11:52 +03001635 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
Ron Eldord0c56de2017-10-10 17:03:08 +03001636
Ron Eldor84df1ae2017-10-16 17:11:52 +03001637 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1638 return( ret );
Ron Eldord0c56de2017-10-10 17:03:08 +03001639
Ron Eldor84df1ae2017-10-16 17:11:52 +03001640 if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 )
1641 mbedtls_pk_free( ctx );
Ron Eldor3f2da842017-10-17 15:50:30 +03001642
Ron Eldord0c56de2017-10-10 17:03:08 +03001643 mbedtls_pem_free( &pem );
1644 return( ret );
1645 }
1646 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
1647 {
1648 mbedtls_pem_free( &pem );
1649 return( ret );
1650 }
1651#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001652
1653 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
Gilles Peskine159171b2018-12-19 17:03:28 +01001654 if( key[keylen - 1] != '\0' )
Manuel Pégourié-Gonnard43b37cb2015-05-12 11:20:10 +02001655 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
1656 else
1657 ret = mbedtls_pem_read_buffer( &pem,
1658 "-----BEGIN PUBLIC KEY-----",
1659 "-----END PUBLIC KEY-----",
1660 key, NULL, 0, &len );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001661
1662 if( ret == 0 )
1663 {
1664 /*
1665 * Was PEM encoded
1666 */
Ron Eldor40b14a82017-10-16 19:30:00 +03001667 p = pem.buf;
1668
1669 ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx );
1670 mbedtls_pem_free( &pem );
1671 return( ret );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001672 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
Paul Bakker1a7550a2013-09-15 13:01:22 +02001674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 mbedtls_pem_free( &pem );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001676 return( ret );
1677 }
Ron Eldor5472d432017-10-17 09:49:00 +03001678 mbedtls_pem_free( &pem );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#endif /* MBEDTLS_PEM_PARSE_C */
Ron Eldor40b14a82017-10-16 19:30:00 +03001680
1681#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard020d9ba2019-09-19 10:45:14 +02001682 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == MBEDTLS_PK_INVALID_HANDLE )
Ron Eldor40b14a82017-10-16 19:30:00 +03001683 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
1684
1685 if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 )
1686 return( ret );
1687
Ron Eldor9566ff72018-02-07 18:59:41 +02001688 p = (unsigned char *)key;
Ron Eldor40b14a82017-10-16 19:30:00 +03001689 ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) );
Ron Eldor9566ff72018-02-07 18:59:41 +02001690 if( ret == 0 )
Ron Eldor40b14a82017-10-16 19:30:00 +03001691 {
Ron Eldor40b14a82017-10-16 19:30:00 +03001692 return( ret );
1693 }
1694 mbedtls_pk_free( ctx );
Ron Eldor9566ff72018-02-07 18:59:41 +02001695 if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
Ron Eldor40b14a82017-10-16 19:30:00 +03001696 {
Ron Eldor9566ff72018-02-07 18:59:41 +02001697 return( ret );
Ron Eldor40b14a82017-10-16 19:30:00 +03001698 }
1699#endif /* MBEDTLS_RSA_C */
Paul Bakker1a7550a2013-09-15 13:01:22 +02001700 p = (unsigned char *) key;
1701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
Paul Bakker1a7550a2013-09-15 13:01:22 +02001703
Paul Bakker1a7550a2013-09-15 13:01:22 +02001704 return( ret );
1705}
1706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001707#endif /* MBEDTLS_PK_PARSE_C */