mbed os with nrf51 internal bandgap enabled to read battery level

Dependents:   BLE_file_test BLE_Blink ExternalEncoder

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
elessair 0:f269e3021894 1 /*
elessair 0:f269e3021894 2 * Public Key layer for parsing key files and structures
elessair 0:f269e3021894 3 *
elessair 0:f269e3021894 4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
elessair 0:f269e3021894 5 * SPDX-License-Identifier: Apache-2.0
elessair 0:f269e3021894 6 *
elessair 0:f269e3021894 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
elessair 0:f269e3021894 8 * not use this file except in compliance with the License.
elessair 0:f269e3021894 9 * You may obtain a copy of the License at
elessair 0:f269e3021894 10 *
elessair 0:f269e3021894 11 * http://www.apache.org/licenses/LICENSE-2.0
elessair 0:f269e3021894 12 *
elessair 0:f269e3021894 13 * Unless required by applicable law or agreed to in writing, software
elessair 0:f269e3021894 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
elessair 0:f269e3021894 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
elessair 0:f269e3021894 16 * See the License for the specific language governing permissions and
elessair 0:f269e3021894 17 * limitations under the License.
elessair 0:f269e3021894 18 *
elessair 0:f269e3021894 19 * This file is part of mbed TLS (https://tls.mbed.org)
elessair 0:f269e3021894 20 */
elessair 0:f269e3021894 21
elessair 0:f269e3021894 22 #if !defined(MBEDTLS_CONFIG_FILE)
elessair 0:f269e3021894 23 #include "mbedtls/config.h"
elessair 0:f269e3021894 24 #else
elessair 0:f269e3021894 25 #include MBEDTLS_CONFIG_FILE
elessair 0:f269e3021894 26 #endif
elessair 0:f269e3021894 27
elessair 0:f269e3021894 28 #if defined(MBEDTLS_PK_PARSE_C)
elessair 0:f269e3021894 29
elessair 0:f269e3021894 30 #include "mbedtls/pk.h"
elessair 0:f269e3021894 31 #include "mbedtls/asn1.h"
elessair 0:f269e3021894 32 #include "mbedtls/oid.h"
elessair 0:f269e3021894 33
elessair 0:f269e3021894 34 #include <string.h>
elessair 0:f269e3021894 35
elessair 0:f269e3021894 36 #if defined(MBEDTLS_RSA_C)
elessair 0:f269e3021894 37 #include "mbedtls/rsa.h"
elessair 0:f269e3021894 38 #endif
elessair 0:f269e3021894 39 #if defined(MBEDTLS_ECP_C)
elessair 0:f269e3021894 40 #include "mbedtls/ecp.h"
elessair 0:f269e3021894 41 #endif
elessair 0:f269e3021894 42 #if defined(MBEDTLS_ECDSA_C)
elessair 0:f269e3021894 43 #include "mbedtls/ecdsa.h"
elessair 0:f269e3021894 44 #endif
elessair 0:f269e3021894 45 #if defined(MBEDTLS_PEM_PARSE_C)
elessair 0:f269e3021894 46 #include "mbedtls/pem.h"
elessair 0:f269e3021894 47 #endif
elessair 0:f269e3021894 48 #if defined(MBEDTLS_PKCS5_C)
elessair 0:f269e3021894 49 #include "mbedtls/pkcs5.h"
elessair 0:f269e3021894 50 #endif
elessair 0:f269e3021894 51 #if defined(MBEDTLS_PKCS12_C)
elessair 0:f269e3021894 52 #include "mbedtls/pkcs12.h"
elessair 0:f269e3021894 53 #endif
elessair 0:f269e3021894 54
elessair 0:f269e3021894 55 #if defined(MBEDTLS_PLATFORM_C)
elessair 0:f269e3021894 56 #include "mbedtls/platform.h"
elessair 0:f269e3021894 57 #else
elessair 0:f269e3021894 58 #include <stdlib.h>
elessair 0:f269e3021894 59 #define mbedtls_calloc calloc
elessair 0:f269e3021894 60 #define mbedtls_free free
elessair 0:f269e3021894 61 #endif
elessair 0:f269e3021894 62
elessair 0:f269e3021894 63 #if defined(MBEDTLS_FS_IO)
elessair 0:f269e3021894 64 /* Implementation that should never be optimized out by the compiler */
elessair 0:f269e3021894 65 static void mbedtls_zeroize( void *v, size_t n ) {
elessair 0:f269e3021894 66 volatile unsigned char *p = v; while( n-- ) *p++ = 0;
elessair 0:f269e3021894 67 }
elessair 0:f269e3021894 68
elessair 0:f269e3021894 69 /*
elessair 0:f269e3021894 70 * Load all data from a file into a given buffer.
elessair 0:f269e3021894 71 *
elessair 0:f269e3021894 72 * The file is expected to contain either PEM or DER encoded data.
elessair 0:f269e3021894 73 * A terminating null byte is always appended. It is included in the announced
elessair 0:f269e3021894 74 * length only if the data looks like it is PEM encoded.
elessair 0:f269e3021894 75 */
elessair 0:f269e3021894 76 int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
elessair 0:f269e3021894 77 {
elessair 0:f269e3021894 78 FILE *f;
elessair 0:f269e3021894 79 long size;
elessair 0:f269e3021894 80
elessair 0:f269e3021894 81 if( ( f = fopen( path, "rb" ) ) == NULL )
elessair 0:f269e3021894 82 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
elessair 0:f269e3021894 83
elessair 0:f269e3021894 84 fseek( f, 0, SEEK_END );
elessair 0:f269e3021894 85 if( ( size = ftell( f ) ) == -1 )
elessair 0:f269e3021894 86 {
elessair 0:f269e3021894 87 fclose( f );
elessair 0:f269e3021894 88 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
elessair 0:f269e3021894 89 }
elessair 0:f269e3021894 90 fseek( f, 0, SEEK_SET );
elessair 0:f269e3021894 91
elessair 0:f269e3021894 92 *n = (size_t) size;
elessair 0:f269e3021894 93
elessair 0:f269e3021894 94 if( *n + 1 == 0 ||
elessair 0:f269e3021894 95 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
elessair 0:f269e3021894 96 {
elessair 0:f269e3021894 97 fclose( f );
elessair 0:f269e3021894 98 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
elessair 0:f269e3021894 99 }
elessair 0:f269e3021894 100
elessair 0:f269e3021894 101 if( fread( *buf, 1, *n, f ) != *n )
elessair 0:f269e3021894 102 {
elessair 0:f269e3021894 103 fclose( f );
elessair 0:f269e3021894 104 mbedtls_free( *buf );
elessair 0:f269e3021894 105 return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
elessair 0:f269e3021894 106 }
elessair 0:f269e3021894 107
elessair 0:f269e3021894 108 fclose( f );
elessair 0:f269e3021894 109
elessair 0:f269e3021894 110 (*buf)[*n] = '\0';
elessair 0:f269e3021894 111
elessair 0:f269e3021894 112 if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
elessair 0:f269e3021894 113 ++*n;
elessair 0:f269e3021894 114
elessair 0:f269e3021894 115 return( 0 );
elessair 0:f269e3021894 116 }
elessair 0:f269e3021894 117
elessair 0:f269e3021894 118 /*
elessair 0:f269e3021894 119 * Load and parse a private key
elessair 0:f269e3021894 120 */
elessair 0:f269e3021894 121 int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
elessair 0:f269e3021894 122 const char *path, const char *pwd )
elessair 0:f269e3021894 123 {
elessair 0:f269e3021894 124 int ret;
elessair 0:f269e3021894 125 size_t n;
elessair 0:f269e3021894 126 unsigned char *buf;
elessair 0:f269e3021894 127
elessair 0:f269e3021894 128 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
elessair 0:f269e3021894 129 return( ret );
elessair 0:f269e3021894 130
elessair 0:f269e3021894 131 if( pwd == NULL )
elessair 0:f269e3021894 132 ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
elessair 0:f269e3021894 133 else
elessair 0:f269e3021894 134 ret = mbedtls_pk_parse_key( ctx, buf, n,
elessair 0:f269e3021894 135 (const unsigned char *) pwd, strlen( pwd ) );
elessair 0:f269e3021894 136
elessair 0:f269e3021894 137 mbedtls_zeroize( buf, n );
elessair 0:f269e3021894 138 mbedtls_free( buf );
elessair 0:f269e3021894 139
elessair 0:f269e3021894 140 return( ret );
elessair 0:f269e3021894 141 }
elessair 0:f269e3021894 142
elessair 0:f269e3021894 143 /*
elessair 0:f269e3021894 144 * Load and parse a public key
elessair 0:f269e3021894 145 */
elessair 0:f269e3021894 146 int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
elessair 0:f269e3021894 147 {
elessair 0:f269e3021894 148 int ret;
elessair 0:f269e3021894 149 size_t n;
elessair 0:f269e3021894 150 unsigned char *buf;
elessair 0:f269e3021894 151
elessair 0:f269e3021894 152 if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
elessair 0:f269e3021894 153 return( ret );
elessair 0:f269e3021894 154
elessair 0:f269e3021894 155 ret = mbedtls_pk_parse_public_key( ctx, buf, n );
elessair 0:f269e3021894 156
elessair 0:f269e3021894 157 mbedtls_zeroize( buf, n );
elessair 0:f269e3021894 158 mbedtls_free( buf );
elessair 0:f269e3021894 159
elessair 0:f269e3021894 160 return( ret );
elessair 0:f269e3021894 161 }
elessair 0:f269e3021894 162 #endif /* MBEDTLS_FS_IO */
elessair 0:f269e3021894 163
elessair 0:f269e3021894 164 #if defined(MBEDTLS_ECP_C)
elessair 0:f269e3021894 165 /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
elessair 0:f269e3021894 166 *
elessair 0:f269e3021894 167 * ECParameters ::= CHOICE {
elessair 0:f269e3021894 168 * namedCurve OBJECT IDENTIFIER
elessair 0:f269e3021894 169 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
elessair 0:f269e3021894 170 * -- implicitCurve NULL
elessair 0:f269e3021894 171 * }
elessair 0:f269e3021894 172 */
elessair 0:f269e3021894 173 static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
elessair 0:f269e3021894 174 mbedtls_asn1_buf *params )
elessair 0:f269e3021894 175 {
elessair 0:f269e3021894 176 int ret;
elessair 0:f269e3021894 177
elessair 0:f269e3021894 178 /* Tag may be either OID or SEQUENCE */
elessair 0:f269e3021894 179 params->tag = **p;
elessair 0:f269e3021894 180 if( params->tag != MBEDTLS_ASN1_OID
elessair 0:f269e3021894 181 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
elessair 0:f269e3021894 182 && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
elessair 0:f269e3021894 183 #endif
elessair 0:f269e3021894 184 )
elessair 0:f269e3021894 185 {
elessair 0:f269e3021894 186 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
elessair 0:f269e3021894 187 MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
elessair 0:f269e3021894 188 }
elessair 0:f269e3021894 189
elessair 0:f269e3021894 190 if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
elessair 0:f269e3021894 191 {
elessair 0:f269e3021894 192 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 193 }
elessair 0:f269e3021894 194
elessair 0:f269e3021894 195 params->p = *p;
elessair 0:f269e3021894 196 *p += params->len;
elessair 0:f269e3021894 197
elessair 0:f269e3021894 198 if( *p != end )
elessair 0:f269e3021894 199 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
elessair 0:f269e3021894 200 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 201
elessair 0:f269e3021894 202 return( 0 );
elessair 0:f269e3021894 203 }
elessair 0:f269e3021894 204
elessair 0:f269e3021894 205 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
elessair 0:f269e3021894 206 /*
elessair 0:f269e3021894 207 * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
elessair 0:f269e3021894 208 * WARNING: the resulting group should only be used with
elessair 0:f269e3021894 209 * pk_group_id_from_specified(), since its base point may not be set correctly
elessair 0:f269e3021894 210 * if it was encoded compressed.
elessair 0:f269e3021894 211 *
elessair 0:f269e3021894 212 * SpecifiedECDomain ::= SEQUENCE {
elessair 0:f269e3021894 213 * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
elessair 0:f269e3021894 214 * fieldID FieldID {{FieldTypes}},
elessair 0:f269e3021894 215 * curve Curve,
elessair 0:f269e3021894 216 * base ECPoint,
elessair 0:f269e3021894 217 * order INTEGER,
elessair 0:f269e3021894 218 * cofactor INTEGER OPTIONAL,
elessair 0:f269e3021894 219 * hash HashAlgorithm OPTIONAL,
elessair 0:f269e3021894 220 * ...
elessair 0:f269e3021894 221 * }
elessair 0:f269e3021894 222 *
elessair 0:f269e3021894 223 * We only support prime-field as field type, and ignore hash and cofactor.
elessair 0:f269e3021894 224 */
elessair 0:f269e3021894 225 static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
elessair 0:f269e3021894 226 {
elessair 0:f269e3021894 227 int ret;
elessair 0:f269e3021894 228 unsigned char *p = params->p;
elessair 0:f269e3021894 229 const unsigned char * const end = params->p + params->len;
elessair 0:f269e3021894 230 const unsigned char *end_field, *end_curve;
elessair 0:f269e3021894 231 size_t len;
elessair 0:f269e3021894 232 int ver;
elessair 0:f269e3021894 233
elessair 0:f269e3021894 234 /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
elessair 0:f269e3021894 235 if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
elessair 0:f269e3021894 236 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 237
elessair 0:f269e3021894 238 if( ver < 1 || ver > 3 )
elessair 0:f269e3021894 239 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
elessair 0:f269e3021894 240
elessair 0:f269e3021894 241 /*
elessair 0:f269e3021894 242 * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
elessair 0:f269e3021894 243 * fieldType FIELD-ID.&id({IOSet}),
elessair 0:f269e3021894 244 * parameters FIELD-ID.&Type({IOSet}{@fieldType})
elessair 0:f269e3021894 245 * }
elessair 0:f269e3021894 246 */
elessair 0:f269e3021894 247 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 248 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 249 return( ret );
elessair 0:f269e3021894 250
elessair 0:f269e3021894 251 end_field = p + len;
elessair 0:f269e3021894 252
elessair 0:f269e3021894 253 /*
elessair 0:f269e3021894 254 * FIELD-ID ::= TYPE-IDENTIFIER
elessair 0:f269e3021894 255 * FieldTypes FIELD-ID ::= {
elessair 0:f269e3021894 256 * { Prime-p IDENTIFIED BY prime-field } |
elessair 0:f269e3021894 257 * { Characteristic-two IDENTIFIED BY characteristic-two-field }
elessair 0:f269e3021894 258 * }
elessair 0:f269e3021894 259 * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
elessair 0:f269e3021894 260 */
elessair 0:f269e3021894 261 if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
elessair 0:f269e3021894 262 return( ret );
elessair 0:f269e3021894 263
elessair 0:f269e3021894 264 if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
elessair 0:f269e3021894 265 memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
elessair 0:f269e3021894 266 {
elessair 0:f269e3021894 267 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
elessair 0:f269e3021894 268 }
elessair 0:f269e3021894 269
elessair 0:f269e3021894 270 p += len;
elessair 0:f269e3021894 271
elessair 0:f269e3021894 272 /* Prime-p ::= INTEGER -- Field of size p. */
elessair 0:f269e3021894 273 if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
elessair 0:f269e3021894 274 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 275
elessair 0:f269e3021894 276 grp->pbits = mbedtls_mpi_bitlen( &grp->P );
elessair 0:f269e3021894 277
elessair 0:f269e3021894 278 if( p != end_field )
elessair 0:f269e3021894 279 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
elessair 0:f269e3021894 280 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 281
elessair 0:f269e3021894 282 /*
elessair 0:f269e3021894 283 * Curve ::= SEQUENCE {
elessair 0:f269e3021894 284 * a FieldElement,
elessair 0:f269e3021894 285 * b FieldElement,
elessair 0:f269e3021894 286 * seed BIT STRING OPTIONAL
elessair 0:f269e3021894 287 * -- Shall be present if used in SpecifiedECDomain
elessair 0:f269e3021894 288 * -- with version equal to ecdpVer2 or ecdpVer3
elessair 0:f269e3021894 289 * }
elessair 0:f269e3021894 290 */
elessair 0:f269e3021894 291 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 292 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 293 return( ret );
elessair 0:f269e3021894 294
elessair 0:f269e3021894 295 end_curve = p + len;
elessair 0:f269e3021894 296
elessair 0:f269e3021894 297 /*
elessair 0:f269e3021894 298 * FieldElement ::= OCTET STRING
elessair 0:f269e3021894 299 * containing an integer in the case of a prime field
elessair 0:f269e3021894 300 */
elessair 0:f269e3021894 301 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
elessair 0:f269e3021894 302 ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
elessair 0:f269e3021894 303 {
elessair 0:f269e3021894 304 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 305 }
elessair 0:f269e3021894 306
elessair 0:f269e3021894 307 p += len;
elessair 0:f269e3021894 308
elessair 0:f269e3021894 309 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
elessair 0:f269e3021894 310 ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
elessair 0:f269e3021894 311 {
elessair 0:f269e3021894 312 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 313 }
elessair 0:f269e3021894 314
elessair 0:f269e3021894 315 p += len;
elessair 0:f269e3021894 316
elessair 0:f269e3021894 317 /* Ignore seed BIT STRING OPTIONAL */
elessair 0:f269e3021894 318 if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
elessair 0:f269e3021894 319 p += len;
elessair 0:f269e3021894 320
elessair 0:f269e3021894 321 if( p != end_curve )
elessair 0:f269e3021894 322 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
elessair 0:f269e3021894 323 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 324
elessair 0:f269e3021894 325 /*
elessair 0:f269e3021894 326 * ECPoint ::= OCTET STRING
elessair 0:f269e3021894 327 */
elessair 0:f269e3021894 328 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
elessair 0:f269e3021894 329 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 330
elessair 0:f269e3021894 331 if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
elessair 0:f269e3021894 332 ( const unsigned char *) p, len ) ) != 0 )
elessair 0:f269e3021894 333 {
elessair 0:f269e3021894 334 /*
elessair 0:f269e3021894 335 * If we can't read the point because it's compressed, cheat by
elessair 0:f269e3021894 336 * reading only the X coordinate and the parity bit of Y.
elessair 0:f269e3021894 337 */
elessair 0:f269e3021894 338 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
elessair 0:f269e3021894 339 ( p[0] != 0x02 && p[0] != 0x03 ) ||
elessair 0:f269e3021894 340 len != mbedtls_mpi_size( &grp->P ) + 1 ||
elessair 0:f269e3021894 341 mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
elessair 0:f269e3021894 342 mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
elessair 0:f269e3021894 343 mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
elessair 0:f269e3021894 344 {
elessair 0:f269e3021894 345 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
elessair 0:f269e3021894 346 }
elessair 0:f269e3021894 347 }
elessair 0:f269e3021894 348
elessair 0:f269e3021894 349 p += len;
elessair 0:f269e3021894 350
elessair 0:f269e3021894 351 /*
elessair 0:f269e3021894 352 * order INTEGER
elessair 0:f269e3021894 353 */
elessair 0:f269e3021894 354 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
elessair 0:f269e3021894 355 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 356
elessair 0:f269e3021894 357 grp->nbits = mbedtls_mpi_bitlen( &grp->N );
elessair 0:f269e3021894 358
elessair 0:f269e3021894 359 /*
elessair 0:f269e3021894 360 * Allow optional elements by purposefully not enforcing p == end here.
elessair 0:f269e3021894 361 */
elessair 0:f269e3021894 362
elessair 0:f269e3021894 363 return( 0 );
elessair 0:f269e3021894 364 }
elessair 0:f269e3021894 365
elessair 0:f269e3021894 366 /*
elessair 0:f269e3021894 367 * Find the group id associated with an (almost filled) group as generated by
elessair 0:f269e3021894 368 * pk_group_from_specified(), or return an error if unknown.
elessair 0:f269e3021894 369 */
elessair 0:f269e3021894 370 static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
elessair 0:f269e3021894 371 {
elessair 0:f269e3021894 372 int ret = 0;
elessair 0:f269e3021894 373 mbedtls_ecp_group ref;
elessair 0:f269e3021894 374 const mbedtls_ecp_group_id *id;
elessair 0:f269e3021894 375
elessair 0:f269e3021894 376 mbedtls_ecp_group_init( &ref );
elessair 0:f269e3021894 377
elessair 0:f269e3021894 378 for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
elessair 0:f269e3021894 379 {
elessair 0:f269e3021894 380 /* Load the group associated to that id */
elessair 0:f269e3021894 381 mbedtls_ecp_group_free( &ref );
elessair 0:f269e3021894 382 MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
elessair 0:f269e3021894 383
elessair 0:f269e3021894 384 /* Compare to the group we were given, starting with easy tests */
elessair 0:f269e3021894 385 if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
elessair 0:f269e3021894 386 mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
elessair 0:f269e3021894 387 mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
elessair 0:f269e3021894 388 mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
elessair 0:f269e3021894 389 mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
elessair 0:f269e3021894 390 mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
elessair 0:f269e3021894 391 mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
elessair 0:f269e3021894 392 /* For Y we may only know the parity bit, so compare only that */
elessair 0:f269e3021894 393 mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
elessair 0:f269e3021894 394 {
elessair 0:f269e3021894 395 break;
elessair 0:f269e3021894 396 }
elessair 0:f269e3021894 397
elessair 0:f269e3021894 398 }
elessair 0:f269e3021894 399
elessair 0:f269e3021894 400 cleanup:
elessair 0:f269e3021894 401 mbedtls_ecp_group_free( &ref );
elessair 0:f269e3021894 402
elessair 0:f269e3021894 403 *grp_id = *id;
elessair 0:f269e3021894 404
elessair 0:f269e3021894 405 if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
elessair 0:f269e3021894 406 ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
elessair 0:f269e3021894 407
elessair 0:f269e3021894 408 return( ret );
elessair 0:f269e3021894 409 }
elessair 0:f269e3021894 410
elessair 0:f269e3021894 411 /*
elessair 0:f269e3021894 412 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
elessair 0:f269e3021894 413 */
elessair 0:f269e3021894 414 static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
elessair 0:f269e3021894 415 mbedtls_ecp_group_id *grp_id )
elessair 0:f269e3021894 416 {
elessair 0:f269e3021894 417 int ret;
elessair 0:f269e3021894 418 mbedtls_ecp_group grp;
elessair 0:f269e3021894 419
elessair 0:f269e3021894 420 mbedtls_ecp_group_init( &grp );
elessair 0:f269e3021894 421
elessair 0:f269e3021894 422 if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
elessair 0:f269e3021894 423 goto cleanup;
elessair 0:f269e3021894 424
elessair 0:f269e3021894 425 ret = pk_group_id_from_group( &grp, grp_id );
elessair 0:f269e3021894 426
elessair 0:f269e3021894 427 cleanup:
elessair 0:f269e3021894 428 mbedtls_ecp_group_free( &grp );
elessair 0:f269e3021894 429
elessair 0:f269e3021894 430 return( ret );
elessair 0:f269e3021894 431 }
elessair 0:f269e3021894 432 #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
elessair 0:f269e3021894 433
elessair 0:f269e3021894 434 /*
elessair 0:f269e3021894 435 * Use EC parameters to initialise an EC group
elessair 0:f269e3021894 436 *
elessair 0:f269e3021894 437 * ECParameters ::= CHOICE {
elessair 0:f269e3021894 438 * namedCurve OBJECT IDENTIFIER
elessair 0:f269e3021894 439 * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
elessair 0:f269e3021894 440 * -- implicitCurve NULL
elessair 0:f269e3021894 441 */
elessair 0:f269e3021894 442 static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
elessair 0:f269e3021894 443 {
elessair 0:f269e3021894 444 int ret;
elessair 0:f269e3021894 445 mbedtls_ecp_group_id grp_id;
elessair 0:f269e3021894 446
elessair 0:f269e3021894 447 if( params->tag == MBEDTLS_ASN1_OID )
elessair 0:f269e3021894 448 {
elessair 0:f269e3021894 449 if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
elessair 0:f269e3021894 450 return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
elessair 0:f269e3021894 451 }
elessair 0:f269e3021894 452 else
elessair 0:f269e3021894 453 {
elessair 0:f269e3021894 454 #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
elessair 0:f269e3021894 455 if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
elessair 0:f269e3021894 456 return( ret );
elessair 0:f269e3021894 457 #else
elessair 0:f269e3021894 458 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
elessair 0:f269e3021894 459 #endif
elessair 0:f269e3021894 460 }
elessair 0:f269e3021894 461
elessair 0:f269e3021894 462 /*
elessair 0:f269e3021894 463 * grp may already be initilialized; if so, make sure IDs match
elessair 0:f269e3021894 464 */
elessair 0:f269e3021894 465 if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
elessair 0:f269e3021894 466 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
elessair 0:f269e3021894 467
elessair 0:f269e3021894 468 if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
elessair 0:f269e3021894 469 return( ret );
elessair 0:f269e3021894 470
elessair 0:f269e3021894 471 return( 0 );
elessair 0:f269e3021894 472 }
elessair 0:f269e3021894 473
elessair 0:f269e3021894 474 /*
elessair 0:f269e3021894 475 * EC public key is an EC point
elessair 0:f269e3021894 476 *
elessair 0:f269e3021894 477 * The caller is responsible for clearing the structure upon failure if
elessair 0:f269e3021894 478 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
elessair 0:f269e3021894 479 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
elessair 0:f269e3021894 480 */
elessair 0:f269e3021894 481 static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
elessair 0:f269e3021894 482 mbedtls_ecp_keypair *key )
elessair 0:f269e3021894 483 {
elessair 0:f269e3021894 484 int ret;
elessair 0:f269e3021894 485
elessair 0:f269e3021894 486 if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
elessair 0:f269e3021894 487 (const unsigned char *) *p, end - *p ) ) == 0 )
elessair 0:f269e3021894 488 {
elessair 0:f269e3021894 489 ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
elessair 0:f269e3021894 490 }
elessair 0:f269e3021894 491
elessair 0:f269e3021894 492 /*
elessair 0:f269e3021894 493 * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
elessair 0:f269e3021894 494 */
elessair 0:f269e3021894 495 *p = (unsigned char *) end;
elessair 0:f269e3021894 496
elessair 0:f269e3021894 497 return( ret );
elessair 0:f269e3021894 498 }
elessair 0:f269e3021894 499 #endif /* MBEDTLS_ECP_C */
elessair 0:f269e3021894 500
elessair 0:f269e3021894 501 #if defined(MBEDTLS_RSA_C)
elessair 0:f269e3021894 502 /*
elessair 0:f269e3021894 503 * RSAPublicKey ::= SEQUENCE {
elessair 0:f269e3021894 504 * modulus INTEGER, -- n
elessair 0:f269e3021894 505 * publicExponent INTEGER -- e
elessair 0:f269e3021894 506 * }
elessair 0:f269e3021894 507 */
elessair 0:f269e3021894 508 static int pk_get_rsapubkey( unsigned char **p,
elessair 0:f269e3021894 509 const unsigned char *end,
elessair 0:f269e3021894 510 mbedtls_rsa_context *rsa )
elessair 0:f269e3021894 511 {
elessair 0:f269e3021894 512 int ret;
elessair 0:f269e3021894 513 size_t len;
elessair 0:f269e3021894 514
elessair 0:f269e3021894 515 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
elessair 0:f269e3021894 516 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 517 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
elessair 0:f269e3021894 518
elessair 0:f269e3021894 519 if( *p + len != end )
elessair 0:f269e3021894 520 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
elessair 0:f269e3021894 521 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 522
elessair 0:f269e3021894 523 if( ( ret = mbedtls_asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
elessair 0:f269e3021894 524 ( ret = mbedtls_asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
elessair 0:f269e3021894 525 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
elessair 0:f269e3021894 526
elessair 0:f269e3021894 527 if( *p != end )
elessair 0:f269e3021894 528 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
elessair 0:f269e3021894 529 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 530
elessair 0:f269e3021894 531 if( ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
elessair 0:f269e3021894 532 return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
elessair 0:f269e3021894 533
elessair 0:f269e3021894 534 rsa->len = mbedtls_mpi_size( &rsa->N );
elessair 0:f269e3021894 535
elessair 0:f269e3021894 536 return( 0 );
elessair 0:f269e3021894 537 }
elessair 0:f269e3021894 538 #endif /* MBEDTLS_RSA_C */
elessair 0:f269e3021894 539
elessair 0:f269e3021894 540 /* Get a PK algorithm identifier
elessair 0:f269e3021894 541 *
elessair 0:f269e3021894 542 * AlgorithmIdentifier ::= SEQUENCE {
elessair 0:f269e3021894 543 * algorithm OBJECT IDENTIFIER,
elessair 0:f269e3021894 544 * parameters ANY DEFINED BY algorithm OPTIONAL }
elessair 0:f269e3021894 545 */
elessair 0:f269e3021894 546 static int pk_get_pk_alg( unsigned char **p,
elessair 0:f269e3021894 547 const unsigned char *end,
elessair 0:f269e3021894 548 mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
elessair 0:f269e3021894 549 {
elessair 0:f269e3021894 550 int ret;
elessair 0:f269e3021894 551 mbedtls_asn1_buf alg_oid;
elessair 0:f269e3021894 552
elessair 0:f269e3021894 553 memset( params, 0, sizeof(mbedtls_asn1_buf) );
elessair 0:f269e3021894 554
elessair 0:f269e3021894 555 if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
elessair 0:f269e3021894 556 return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
elessair 0:f269e3021894 557
elessair 0:f269e3021894 558 if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
elessair 0:f269e3021894 559 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 560
elessair 0:f269e3021894 561 /*
elessair 0:f269e3021894 562 * No parameters with RSA (only for EC)
elessair 0:f269e3021894 563 */
elessair 0:f269e3021894 564 if( *pk_alg == MBEDTLS_PK_RSA &&
elessair 0:f269e3021894 565 ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
elessair 0:f269e3021894 566 params->len != 0 ) )
elessair 0:f269e3021894 567 {
elessair 0:f269e3021894 568 return( MBEDTLS_ERR_PK_INVALID_ALG );
elessair 0:f269e3021894 569 }
elessair 0:f269e3021894 570
elessair 0:f269e3021894 571 return( 0 );
elessair 0:f269e3021894 572 }
elessair 0:f269e3021894 573
elessair 0:f269e3021894 574 /*
elessair 0:f269e3021894 575 * SubjectPublicKeyInfo ::= SEQUENCE {
elessair 0:f269e3021894 576 * algorithm AlgorithmIdentifier,
elessair 0:f269e3021894 577 * subjectPublicKey BIT STRING }
elessair 0:f269e3021894 578 */
elessair 0:f269e3021894 579 int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
elessair 0:f269e3021894 580 mbedtls_pk_context *pk )
elessair 0:f269e3021894 581 {
elessair 0:f269e3021894 582 int ret;
elessair 0:f269e3021894 583 size_t len;
elessair 0:f269e3021894 584 mbedtls_asn1_buf alg_params;
elessair 0:f269e3021894 585 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
elessair 0:f269e3021894 586 const mbedtls_pk_info_t *pk_info;
elessair 0:f269e3021894 587
elessair 0:f269e3021894 588 if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
elessair 0:f269e3021894 589 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 590 {
elessair 0:f269e3021894 591 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 592 }
elessair 0:f269e3021894 593
elessair 0:f269e3021894 594 end = *p + len;
elessair 0:f269e3021894 595
elessair 0:f269e3021894 596 if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
elessair 0:f269e3021894 597 return( ret );
elessair 0:f269e3021894 598
elessair 0:f269e3021894 599 if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
elessair 0:f269e3021894 600 return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
elessair 0:f269e3021894 601
elessair 0:f269e3021894 602 if( *p + len != end )
elessair 0:f269e3021894 603 return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
elessair 0:f269e3021894 604 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 605
elessair 0:f269e3021894 606 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
elessair 0:f269e3021894 607 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 608
elessair 0:f269e3021894 609 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
elessair 0:f269e3021894 610 return( ret );
elessair 0:f269e3021894 611
elessair 0:f269e3021894 612 #if defined(MBEDTLS_RSA_C)
elessair 0:f269e3021894 613 if( pk_alg == MBEDTLS_PK_RSA )
elessair 0:f269e3021894 614 {
elessair 0:f269e3021894 615 ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
elessair 0:f269e3021894 616 } else
elessair 0:f269e3021894 617 #endif /* MBEDTLS_RSA_C */
elessair 0:f269e3021894 618 #if defined(MBEDTLS_ECP_C)
elessair 0:f269e3021894 619 if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
elessair 0:f269e3021894 620 {
elessair 0:f269e3021894 621 ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
elessair 0:f269e3021894 622 if( ret == 0 )
elessair 0:f269e3021894 623 ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
elessair 0:f269e3021894 624 } else
elessair 0:f269e3021894 625 #endif /* MBEDTLS_ECP_C */
elessair 0:f269e3021894 626 ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
elessair 0:f269e3021894 627
elessair 0:f269e3021894 628 if( ret == 0 && *p != end )
elessair 0:f269e3021894 629 ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
elessair 0:f269e3021894 630 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
elessair 0:f269e3021894 631
elessair 0:f269e3021894 632 if( ret != 0 )
elessair 0:f269e3021894 633 mbedtls_pk_free( pk );
elessair 0:f269e3021894 634
elessair 0:f269e3021894 635 return( ret );
elessair 0:f269e3021894 636 }
elessair 0:f269e3021894 637
elessair 0:f269e3021894 638 #if defined(MBEDTLS_RSA_C)
elessair 0:f269e3021894 639 /*
elessair 0:f269e3021894 640 * Parse a PKCS#1 encoded private RSA key
elessair 0:f269e3021894 641 */
elessair 0:f269e3021894 642 static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
elessair 0:f269e3021894 643 const unsigned char *key,
elessair 0:f269e3021894 644 size_t keylen )
elessair 0:f269e3021894 645 {
elessair 0:f269e3021894 646 int ret;
elessair 0:f269e3021894 647 size_t len;
elessair 0:f269e3021894 648 unsigned char *p, *end;
elessair 0:f269e3021894 649
elessair 0:f269e3021894 650 p = (unsigned char *) key;
elessair 0:f269e3021894 651 end = p + keylen;
elessair 0:f269e3021894 652
elessair 0:f269e3021894 653 /*
elessair 0:f269e3021894 654 * This function parses the RSAPrivateKey (PKCS#1)
elessair 0:f269e3021894 655 *
elessair 0:f269e3021894 656 * RSAPrivateKey ::= SEQUENCE {
elessair 0:f269e3021894 657 * version Version,
elessair 0:f269e3021894 658 * modulus INTEGER, -- n
elessair 0:f269e3021894 659 * publicExponent INTEGER, -- e
elessair 0:f269e3021894 660 * privateExponent INTEGER, -- d
elessair 0:f269e3021894 661 * prime1 INTEGER, -- p
elessair 0:f269e3021894 662 * prime2 INTEGER, -- q
elessair 0:f269e3021894 663 * exponent1 INTEGER, -- d mod (p-1)
elessair 0:f269e3021894 664 * exponent2 INTEGER, -- d mod (q-1)
elessair 0:f269e3021894 665 * coefficient INTEGER, -- (inverse of q) mod p
elessair 0:f269e3021894 666 * otherPrimeInfos OtherPrimeInfos OPTIONAL
elessair 0:f269e3021894 667 * }
elessair 0:f269e3021894 668 */
elessair 0:f269e3021894 669 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 670 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 671 {
elessair 0:f269e3021894 672 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 673 }
elessair 0:f269e3021894 674
elessair 0:f269e3021894 675 end = p + len;
elessair 0:f269e3021894 676
elessair 0:f269e3021894 677 if( ( ret = mbedtls_asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
elessair 0:f269e3021894 678 {
elessair 0:f269e3021894 679 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 680 }
elessair 0:f269e3021894 681
elessair 0:f269e3021894 682 if( rsa->ver != 0 )
elessair 0:f269e3021894 683 {
elessair 0:f269e3021894 684 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
elessair 0:f269e3021894 685 }
elessair 0:f269e3021894 686
elessair 0:f269e3021894 687 if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
elessair 0:f269e3021894 688 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
elessair 0:f269e3021894 689 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
elessair 0:f269e3021894 690 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
elessair 0:f269e3021894 691 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
elessair 0:f269e3021894 692 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
elessair 0:f269e3021894 693 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
elessair 0:f269e3021894 694 ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
elessair 0:f269e3021894 695 {
elessair 0:f269e3021894 696 mbedtls_rsa_free( rsa );
elessair 0:f269e3021894 697 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 698 }
elessair 0:f269e3021894 699
elessair 0:f269e3021894 700 rsa->len = mbedtls_mpi_size( &rsa->N );
elessair 0:f269e3021894 701
elessair 0:f269e3021894 702 if( p != end )
elessair 0:f269e3021894 703 {
elessair 0:f269e3021894 704 mbedtls_rsa_free( rsa );
elessair 0:f269e3021894 705 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
elessair 0:f269e3021894 706 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 707 }
elessair 0:f269e3021894 708
elessair 0:f269e3021894 709 if( ( ret = mbedtls_rsa_check_privkey( rsa ) ) != 0 )
elessair 0:f269e3021894 710 {
elessair 0:f269e3021894 711 mbedtls_rsa_free( rsa );
elessair 0:f269e3021894 712 return( ret );
elessair 0:f269e3021894 713 }
elessair 0:f269e3021894 714
elessair 0:f269e3021894 715 return( 0 );
elessair 0:f269e3021894 716 }
elessair 0:f269e3021894 717 #endif /* MBEDTLS_RSA_C */
elessair 0:f269e3021894 718
elessair 0:f269e3021894 719 #if defined(MBEDTLS_ECP_C)
elessair 0:f269e3021894 720 /*
elessair 0:f269e3021894 721 * Parse a SEC1 encoded private EC key
elessair 0:f269e3021894 722 */
elessair 0:f269e3021894 723 static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
elessair 0:f269e3021894 724 const unsigned char *key,
elessair 0:f269e3021894 725 size_t keylen )
elessair 0:f269e3021894 726 {
elessair 0:f269e3021894 727 int ret;
elessair 0:f269e3021894 728 int version, pubkey_done;
elessair 0:f269e3021894 729 size_t len;
elessair 0:f269e3021894 730 mbedtls_asn1_buf params;
elessair 0:f269e3021894 731 unsigned char *p = (unsigned char *) key;
elessair 0:f269e3021894 732 unsigned char *end = p + keylen;
elessair 0:f269e3021894 733 unsigned char *end2;
elessair 0:f269e3021894 734
elessair 0:f269e3021894 735 /*
elessair 0:f269e3021894 736 * RFC 5915, or SEC1 Appendix C.4
elessair 0:f269e3021894 737 *
elessair 0:f269e3021894 738 * ECPrivateKey ::= SEQUENCE {
elessair 0:f269e3021894 739 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
elessair 0:f269e3021894 740 * privateKey OCTET STRING,
elessair 0:f269e3021894 741 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
elessair 0:f269e3021894 742 * publicKey [1] BIT STRING OPTIONAL
elessair 0:f269e3021894 743 * }
elessair 0:f269e3021894 744 */
elessair 0:f269e3021894 745 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 746 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 747 {
elessair 0:f269e3021894 748 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 749 }
elessair 0:f269e3021894 750
elessair 0:f269e3021894 751 end = p + len;
elessair 0:f269e3021894 752
elessair 0:f269e3021894 753 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
elessair 0:f269e3021894 754 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 755
elessair 0:f269e3021894 756 if( version != 1 )
elessair 0:f269e3021894 757 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
elessair 0:f269e3021894 758
elessair 0:f269e3021894 759 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
elessair 0:f269e3021894 760 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 761
elessair 0:f269e3021894 762 if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
elessair 0:f269e3021894 763 {
elessair 0:f269e3021894 764 mbedtls_ecp_keypair_free( eck );
elessair 0:f269e3021894 765 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 766 }
elessair 0:f269e3021894 767
elessair 0:f269e3021894 768 p += len;
elessair 0:f269e3021894 769
elessair 0:f269e3021894 770 pubkey_done = 0;
elessair 0:f269e3021894 771 if( p != end )
elessair 0:f269e3021894 772 {
elessair 0:f269e3021894 773 /*
elessair 0:f269e3021894 774 * Is 'parameters' present?
elessair 0:f269e3021894 775 */
elessair 0:f269e3021894 776 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 777 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
elessair 0:f269e3021894 778 {
elessair 0:f269e3021894 779 if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
elessair 0:f269e3021894 780 ( ret = pk_use_ecparams( &params, &eck->grp ) ) != 0 )
elessair 0:f269e3021894 781 {
elessair 0:f269e3021894 782 mbedtls_ecp_keypair_free( eck );
elessair 0:f269e3021894 783 return( ret );
elessair 0:f269e3021894 784 }
elessair 0:f269e3021894 785 }
elessair 0:f269e3021894 786 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
elessair 0:f269e3021894 787 {
elessair 0:f269e3021894 788 mbedtls_ecp_keypair_free( eck );
elessair 0:f269e3021894 789 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 790 }
elessair 0:f269e3021894 791
elessair 0:f269e3021894 792 /*
elessair 0:f269e3021894 793 * Is 'publickey' present? If not, or if we can't read it (eg because it
elessair 0:f269e3021894 794 * is compressed), create it from the private key.
elessair 0:f269e3021894 795 */
elessair 0:f269e3021894 796 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 797 MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
elessair 0:f269e3021894 798 {
elessair 0:f269e3021894 799 end2 = p + len;
elessair 0:f269e3021894 800
elessair 0:f269e3021894 801 if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
elessair 0:f269e3021894 802 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 803
elessair 0:f269e3021894 804 if( p + len != end2 )
elessair 0:f269e3021894 805 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
elessair 0:f269e3021894 806 MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
elessair 0:f269e3021894 807
elessair 0:f269e3021894 808 if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
elessair 0:f269e3021894 809 pubkey_done = 1;
elessair 0:f269e3021894 810 else
elessair 0:f269e3021894 811 {
elessair 0:f269e3021894 812 /*
elessair 0:f269e3021894 813 * The only acceptable failure mode of pk_get_ecpubkey() above
elessair 0:f269e3021894 814 * is if the point format is not recognized.
elessair 0:f269e3021894 815 */
elessair 0:f269e3021894 816 if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
elessair 0:f269e3021894 817 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
elessair 0:f269e3021894 818 }
elessair 0:f269e3021894 819 }
elessair 0:f269e3021894 820 else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
elessair 0:f269e3021894 821 {
elessair 0:f269e3021894 822 mbedtls_ecp_keypair_free( eck );
elessair 0:f269e3021894 823 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 824 }
elessair 0:f269e3021894 825 }
elessair 0:f269e3021894 826
elessair 0:f269e3021894 827 if( ! pubkey_done &&
elessair 0:f269e3021894 828 ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
elessair 0:f269e3021894 829 NULL, NULL ) ) != 0 )
elessair 0:f269e3021894 830 {
elessair 0:f269e3021894 831 mbedtls_ecp_keypair_free( eck );
elessair 0:f269e3021894 832 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 833 }
elessair 0:f269e3021894 834
elessair 0:f269e3021894 835 if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
elessair 0:f269e3021894 836 {
elessair 0:f269e3021894 837 mbedtls_ecp_keypair_free( eck );
elessair 0:f269e3021894 838 return( ret );
elessair 0:f269e3021894 839 }
elessair 0:f269e3021894 840
elessair 0:f269e3021894 841 return( 0 );
elessair 0:f269e3021894 842 }
elessair 0:f269e3021894 843 #endif /* MBEDTLS_ECP_C */
elessair 0:f269e3021894 844
elessair 0:f269e3021894 845 /*
elessair 0:f269e3021894 846 * Parse an unencrypted PKCS#8 encoded private key
elessair 0:f269e3021894 847 */
elessair 0:f269e3021894 848 static int pk_parse_key_pkcs8_unencrypted_der(
elessair 0:f269e3021894 849 mbedtls_pk_context *pk,
elessair 0:f269e3021894 850 const unsigned char* key,
elessair 0:f269e3021894 851 size_t keylen )
elessair 0:f269e3021894 852 {
elessair 0:f269e3021894 853 int ret, version;
elessair 0:f269e3021894 854 size_t len;
elessair 0:f269e3021894 855 mbedtls_asn1_buf params;
elessair 0:f269e3021894 856 unsigned char *p = (unsigned char *) key;
elessair 0:f269e3021894 857 unsigned char *end = p + keylen;
elessair 0:f269e3021894 858 mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
elessair 0:f269e3021894 859 const mbedtls_pk_info_t *pk_info;
elessair 0:f269e3021894 860
elessair 0:f269e3021894 861 /*
elessair 0:f269e3021894 862 * This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
elessair 0:f269e3021894 863 *
elessair 0:f269e3021894 864 * PrivateKeyInfo ::= SEQUENCE {
elessair 0:f269e3021894 865 * version Version,
elessair 0:f269e3021894 866 * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
elessair 0:f269e3021894 867 * privateKey PrivateKey,
elessair 0:f269e3021894 868 * attributes [0] IMPLICIT Attributes OPTIONAL }
elessair 0:f269e3021894 869 *
elessair 0:f269e3021894 870 * Version ::= INTEGER
elessair 0:f269e3021894 871 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
elessair 0:f269e3021894 872 * PrivateKey ::= OCTET STRING
elessair 0:f269e3021894 873 *
elessair 0:f269e3021894 874 * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
elessair 0:f269e3021894 875 */
elessair 0:f269e3021894 876
elessair 0:f269e3021894 877 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 878 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 879 {
elessair 0:f269e3021894 880 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 881 }
elessair 0:f269e3021894 882
elessair 0:f269e3021894 883 end = p + len;
elessair 0:f269e3021894 884
elessair 0:f269e3021894 885 if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
elessair 0:f269e3021894 886 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 887
elessair 0:f269e3021894 888 if( version != 0 )
elessair 0:f269e3021894 889 return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
elessair 0:f269e3021894 890
elessair 0:f269e3021894 891 if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
elessair 0:f269e3021894 892 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 893
elessair 0:f269e3021894 894 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
elessair 0:f269e3021894 895 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 896
elessair 0:f269e3021894 897 if( len < 1 )
elessair 0:f269e3021894 898 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
elessair 0:f269e3021894 899 MBEDTLS_ERR_ASN1_OUT_OF_DATA );
elessair 0:f269e3021894 900
elessair 0:f269e3021894 901 if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
elessair 0:f269e3021894 902 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 903
elessair 0:f269e3021894 904 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
elessair 0:f269e3021894 905 return( ret );
elessair 0:f269e3021894 906
elessair 0:f269e3021894 907 #if defined(MBEDTLS_RSA_C)
elessair 0:f269e3021894 908 if( pk_alg == MBEDTLS_PK_RSA )
elessair 0:f269e3021894 909 {
elessair 0:f269e3021894 910 if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
elessair 0:f269e3021894 911 {
elessair 0:f269e3021894 912 mbedtls_pk_free( pk );
elessair 0:f269e3021894 913 return( ret );
elessair 0:f269e3021894 914 }
elessair 0:f269e3021894 915 } else
elessair 0:f269e3021894 916 #endif /* MBEDTLS_RSA_C */
elessair 0:f269e3021894 917 #if defined(MBEDTLS_ECP_C)
elessair 0:f269e3021894 918 if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
elessair 0:f269e3021894 919 {
elessair 0:f269e3021894 920 if( ( ret = pk_use_ecparams( &params, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
elessair 0:f269e3021894 921 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
elessair 0:f269e3021894 922 {
elessair 0:f269e3021894 923 mbedtls_pk_free( pk );
elessair 0:f269e3021894 924 return( ret );
elessair 0:f269e3021894 925 }
elessair 0:f269e3021894 926 } else
elessair 0:f269e3021894 927 #endif /* MBEDTLS_ECP_C */
elessair 0:f269e3021894 928 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 929
elessair 0:f269e3021894 930 return( 0 );
elessair 0:f269e3021894 931 }
elessair 0:f269e3021894 932
elessair 0:f269e3021894 933 /*
elessair 0:f269e3021894 934 * Parse an encrypted PKCS#8 encoded private key
elessair 0:f269e3021894 935 */
elessair 0:f269e3021894 936 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
elessair 0:f269e3021894 937 static int pk_parse_key_pkcs8_encrypted_der(
elessair 0:f269e3021894 938 mbedtls_pk_context *pk,
elessair 0:f269e3021894 939 const unsigned char *key, size_t keylen,
elessair 0:f269e3021894 940 const unsigned char *pwd, size_t pwdlen )
elessair 0:f269e3021894 941 {
elessair 0:f269e3021894 942 int ret, decrypted = 0;
elessair 0:f269e3021894 943 size_t len;
elessair 0:f269e3021894 944 unsigned char buf[2048];
elessair 0:f269e3021894 945 unsigned char *p, *end;
elessair 0:f269e3021894 946 mbedtls_asn1_buf pbe_alg_oid, pbe_params;
elessair 0:f269e3021894 947 #if defined(MBEDTLS_PKCS12_C)
elessair 0:f269e3021894 948 mbedtls_cipher_type_t cipher_alg;
elessair 0:f269e3021894 949 mbedtls_md_type_t md_alg;
elessair 0:f269e3021894 950 #endif
elessair 0:f269e3021894 951
elessair 0:f269e3021894 952 memset( buf, 0, sizeof( buf ) );
elessair 0:f269e3021894 953
elessair 0:f269e3021894 954 p = (unsigned char *) key;
elessair 0:f269e3021894 955 end = p + keylen;
elessair 0:f269e3021894 956
elessair 0:f269e3021894 957 if( pwdlen == 0 )
elessair 0:f269e3021894 958 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
elessair 0:f269e3021894 959
elessair 0:f269e3021894 960 /*
elessair 0:f269e3021894 961 * This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
elessair 0:f269e3021894 962 *
elessair 0:f269e3021894 963 * EncryptedPrivateKeyInfo ::= SEQUENCE {
elessair 0:f269e3021894 964 * encryptionAlgorithm EncryptionAlgorithmIdentifier,
elessair 0:f269e3021894 965 * encryptedData EncryptedData
elessair 0:f269e3021894 966 * }
elessair 0:f269e3021894 967 *
elessair 0:f269e3021894 968 * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
elessair 0:f269e3021894 969 *
elessair 0:f269e3021894 970 * EncryptedData ::= OCTET STRING
elessair 0:f269e3021894 971 *
elessair 0:f269e3021894 972 * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
elessair 0:f269e3021894 973 */
elessair 0:f269e3021894 974 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
elessair 0:f269e3021894 975 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
elessair 0:f269e3021894 976 {
elessair 0:f269e3021894 977 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 978 }
elessair 0:f269e3021894 979
elessair 0:f269e3021894 980 end = p + len;
elessair 0:f269e3021894 981
elessair 0:f269e3021894 982 if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
elessair 0:f269e3021894 983 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 984
elessair 0:f269e3021894 985 if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
elessair 0:f269e3021894 986 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
elessair 0:f269e3021894 987
elessair 0:f269e3021894 988 if( len > sizeof( buf ) )
elessair 0:f269e3021894 989 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
elessair 0:f269e3021894 990
elessair 0:f269e3021894 991 /*
elessair 0:f269e3021894 992 * Decrypt EncryptedData with appropriate PDE
elessair 0:f269e3021894 993 */
elessair 0:f269e3021894 994 #if defined(MBEDTLS_PKCS12_C)
elessair 0:f269e3021894 995 if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
elessair 0:f269e3021894 996 {
elessair 0:f269e3021894 997 if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
elessair 0:f269e3021894 998 cipher_alg, md_alg,
elessair 0:f269e3021894 999 pwd, pwdlen, p, len, buf ) ) != 0 )
elessair 0:f269e3021894 1000 {
elessair 0:f269e3021894 1001 if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
elessair 0:f269e3021894 1002 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
elessair 0:f269e3021894 1003
elessair 0:f269e3021894 1004 return( ret );
elessair 0:f269e3021894 1005 }
elessair 0:f269e3021894 1006
elessair 0:f269e3021894 1007 decrypted = 1;
elessair 0:f269e3021894 1008 }
elessair 0:f269e3021894 1009 else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
elessair 0:f269e3021894 1010 {
elessair 0:f269e3021894 1011 if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
elessair 0:f269e3021894 1012 MBEDTLS_PKCS12_PBE_DECRYPT,
elessair 0:f269e3021894 1013 pwd, pwdlen,
elessair 0:f269e3021894 1014 p, len, buf ) ) != 0 )
elessair 0:f269e3021894 1015 {
elessair 0:f269e3021894 1016 return( ret );
elessair 0:f269e3021894 1017 }
elessair 0:f269e3021894 1018
elessair 0:f269e3021894 1019 // Best guess for password mismatch when using RC4. If first tag is
elessair 0:f269e3021894 1020 // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
elessair 0:f269e3021894 1021 //
elessair 0:f269e3021894 1022 if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
elessair 0:f269e3021894 1023 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
elessair 0:f269e3021894 1024
elessair 0:f269e3021894 1025 decrypted = 1;
elessair 0:f269e3021894 1026 }
elessair 0:f269e3021894 1027 else
elessair 0:f269e3021894 1028 #endif /* MBEDTLS_PKCS12_C */
elessair 0:f269e3021894 1029 #if defined(MBEDTLS_PKCS5_C)
elessair 0:f269e3021894 1030 if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
elessair 0:f269e3021894 1031 {
elessair 0:f269e3021894 1032 if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
elessair 0:f269e3021894 1033 p, len, buf ) ) != 0 )
elessair 0:f269e3021894 1034 {
elessair 0:f269e3021894 1035 if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
elessair 0:f269e3021894 1036 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
elessair 0:f269e3021894 1037
elessair 0:f269e3021894 1038 return( ret );
elessair 0:f269e3021894 1039 }
elessair 0:f269e3021894 1040
elessair 0:f269e3021894 1041 decrypted = 1;
elessair 0:f269e3021894 1042 }
elessair 0:f269e3021894 1043 else
elessair 0:f269e3021894 1044 #endif /* MBEDTLS_PKCS5_C */
elessair 0:f269e3021894 1045 {
elessair 0:f269e3021894 1046 ((void) pwd);
elessair 0:f269e3021894 1047 }
elessair 0:f269e3021894 1048
elessair 0:f269e3021894 1049 if( decrypted == 0 )
elessair 0:f269e3021894 1050 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
elessair 0:f269e3021894 1051
elessair 0:f269e3021894 1052 return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
elessair 0:f269e3021894 1053 }
elessair 0:f269e3021894 1054 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
elessair 0:f269e3021894 1055
elessair 0:f269e3021894 1056 /*
elessair 0:f269e3021894 1057 * Parse a private key
elessair 0:f269e3021894 1058 */
elessair 0:f269e3021894 1059 int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
elessair 0:f269e3021894 1060 const unsigned char *key, size_t keylen,
elessair 0:f269e3021894 1061 const unsigned char *pwd, size_t pwdlen )
elessair 0:f269e3021894 1062 {
elessair 0:f269e3021894 1063 int ret;
elessair 0:f269e3021894 1064 const mbedtls_pk_info_t *pk_info;
elessair 0:f269e3021894 1065
elessair 0:f269e3021894 1066 #if defined(MBEDTLS_PEM_PARSE_C)
elessair 0:f269e3021894 1067 size_t len;
elessair 0:f269e3021894 1068 mbedtls_pem_context pem;
elessair 0:f269e3021894 1069
elessair 0:f269e3021894 1070 mbedtls_pem_init( &pem );
elessair 0:f269e3021894 1071
elessair 0:f269e3021894 1072 #if defined(MBEDTLS_RSA_C)
elessair 0:f269e3021894 1073 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
elessair 0:f269e3021894 1074 if( keylen == 0 || key[keylen - 1] != '\0' )
elessair 0:f269e3021894 1075 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
elessair 0:f269e3021894 1076 else
elessair 0:f269e3021894 1077 ret = mbedtls_pem_read_buffer( &pem,
elessair 0:f269e3021894 1078 "-----BEGIN RSA PRIVATE KEY-----",
elessair 0:f269e3021894 1079 "-----END RSA PRIVATE KEY-----",
elessair 0:f269e3021894 1080 key, pwd, pwdlen, &len );
elessair 0:f269e3021894 1081
elessair 0:f269e3021894 1082 if( ret == 0 )
elessair 0:f269e3021894 1083 {
elessair 0:f269e3021894 1084 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
elessair 0:f269e3021894 1085 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 1086
elessair 0:f269e3021894 1087 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
elessair 0:f269e3021894 1088 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
elessair 0:f269e3021894 1089 pem.buf, pem.buflen ) ) != 0 )
elessair 0:f269e3021894 1090 {
elessair 0:f269e3021894 1091 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1092 }
elessair 0:f269e3021894 1093
elessair 0:f269e3021894 1094 mbedtls_pem_free( &pem );
elessair 0:f269e3021894 1095 return( ret );
elessair 0:f269e3021894 1096 }
elessair 0:f269e3021894 1097 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
elessair 0:f269e3021894 1098 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
elessair 0:f269e3021894 1099 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
elessair 0:f269e3021894 1100 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
elessair 0:f269e3021894 1101 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
elessair 0:f269e3021894 1102 return( ret );
elessair 0:f269e3021894 1103 #endif /* MBEDTLS_RSA_C */
elessair 0:f269e3021894 1104
elessair 0:f269e3021894 1105 #if defined(MBEDTLS_ECP_C)
elessair 0:f269e3021894 1106 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
elessair 0:f269e3021894 1107 if( keylen == 0 || key[keylen - 1] != '\0' )
elessair 0:f269e3021894 1108 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
elessair 0:f269e3021894 1109 else
elessair 0:f269e3021894 1110 ret = mbedtls_pem_read_buffer( &pem,
elessair 0:f269e3021894 1111 "-----BEGIN EC PRIVATE KEY-----",
elessair 0:f269e3021894 1112 "-----END EC PRIVATE KEY-----",
elessair 0:f269e3021894 1113 key, pwd, pwdlen, &len );
elessair 0:f269e3021894 1114 if( ret == 0 )
elessair 0:f269e3021894 1115 {
elessair 0:f269e3021894 1116 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == NULL )
elessair 0:f269e3021894 1117 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 1118
elessair 0:f269e3021894 1119 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
elessair 0:f269e3021894 1120 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
elessair 0:f269e3021894 1121 pem.buf, pem.buflen ) ) != 0 )
elessair 0:f269e3021894 1122 {
elessair 0:f269e3021894 1123 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1124 }
elessair 0:f269e3021894 1125
elessair 0:f269e3021894 1126 mbedtls_pem_free( &pem );
elessair 0:f269e3021894 1127 return( ret );
elessair 0:f269e3021894 1128 }
elessair 0:f269e3021894 1129 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
elessair 0:f269e3021894 1130 return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
elessair 0:f269e3021894 1131 else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
elessair 0:f269e3021894 1132 return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
elessair 0:f269e3021894 1133 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
elessair 0:f269e3021894 1134 return( ret );
elessair 0:f269e3021894 1135 #endif /* MBEDTLS_ECP_C */
elessair 0:f269e3021894 1136
elessair 0:f269e3021894 1137 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
elessair 0:f269e3021894 1138 if( keylen == 0 || key[keylen - 1] != '\0' )
elessair 0:f269e3021894 1139 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
elessair 0:f269e3021894 1140 else
elessair 0:f269e3021894 1141 ret = mbedtls_pem_read_buffer( &pem,
elessair 0:f269e3021894 1142 "-----BEGIN PRIVATE KEY-----",
elessair 0:f269e3021894 1143 "-----END PRIVATE KEY-----",
elessair 0:f269e3021894 1144 key, NULL, 0, &len );
elessair 0:f269e3021894 1145 if( ret == 0 )
elessair 0:f269e3021894 1146 {
elessair 0:f269e3021894 1147 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
elessair 0:f269e3021894 1148 pem.buf, pem.buflen ) ) != 0 )
elessair 0:f269e3021894 1149 {
elessair 0:f269e3021894 1150 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1151 }
elessair 0:f269e3021894 1152
elessair 0:f269e3021894 1153 mbedtls_pem_free( &pem );
elessair 0:f269e3021894 1154 return( ret );
elessair 0:f269e3021894 1155 }
elessair 0:f269e3021894 1156 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
elessair 0:f269e3021894 1157 return( ret );
elessair 0:f269e3021894 1158
elessair 0:f269e3021894 1159 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
elessair 0:f269e3021894 1160 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
elessair 0:f269e3021894 1161 if( keylen == 0 || key[keylen - 1] != '\0' )
elessair 0:f269e3021894 1162 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
elessair 0:f269e3021894 1163 else
elessair 0:f269e3021894 1164 ret = mbedtls_pem_read_buffer( &pem,
elessair 0:f269e3021894 1165 "-----BEGIN ENCRYPTED PRIVATE KEY-----",
elessair 0:f269e3021894 1166 "-----END ENCRYPTED PRIVATE KEY-----",
elessair 0:f269e3021894 1167 key, NULL, 0, &len );
elessair 0:f269e3021894 1168 if( ret == 0 )
elessair 0:f269e3021894 1169 {
elessair 0:f269e3021894 1170 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
elessair 0:f269e3021894 1171 pem.buf, pem.buflen,
elessair 0:f269e3021894 1172 pwd, pwdlen ) ) != 0 )
elessair 0:f269e3021894 1173 {
elessair 0:f269e3021894 1174 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1175 }
elessair 0:f269e3021894 1176
elessair 0:f269e3021894 1177 mbedtls_pem_free( &pem );
elessair 0:f269e3021894 1178 return( ret );
elessair 0:f269e3021894 1179 }
elessair 0:f269e3021894 1180 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
elessair 0:f269e3021894 1181 return( ret );
elessair 0:f269e3021894 1182 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
elessair 0:f269e3021894 1183 #else
elessair 0:f269e3021894 1184 ((void) ret);
elessair 0:f269e3021894 1185 ((void) pwd);
elessair 0:f269e3021894 1186 ((void) pwdlen);
elessair 0:f269e3021894 1187 #endif /* MBEDTLS_PEM_PARSE_C */
elessair 0:f269e3021894 1188
elessair 0:f269e3021894 1189 /*
elessair 0:f269e3021894 1190 * At this point we only know it's not a PEM formatted key. Could be any
elessair 0:f269e3021894 1191 * of the known DER encoded private key formats
elessair 0:f269e3021894 1192 *
elessair 0:f269e3021894 1193 * We try the different DER format parsers to see if one passes without
elessair 0:f269e3021894 1194 * error
elessair 0:f269e3021894 1195 */
elessair 0:f269e3021894 1196 #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
elessair 0:f269e3021894 1197 if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, key, keylen,
elessair 0:f269e3021894 1198 pwd, pwdlen ) ) == 0 )
elessair 0:f269e3021894 1199 {
elessair 0:f269e3021894 1200 return( 0 );
elessair 0:f269e3021894 1201 }
elessair 0:f269e3021894 1202
elessair 0:f269e3021894 1203 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1204
elessair 0:f269e3021894 1205 if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
elessair 0:f269e3021894 1206 {
elessair 0:f269e3021894 1207 return( ret );
elessair 0:f269e3021894 1208 }
elessair 0:f269e3021894 1209 #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
elessair 0:f269e3021894 1210
elessair 0:f269e3021894 1211 if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
elessair 0:f269e3021894 1212 return( 0 );
elessair 0:f269e3021894 1213
elessair 0:f269e3021894 1214 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1215
elessair 0:f269e3021894 1216 #if defined(MBEDTLS_RSA_C)
elessair 0:f269e3021894 1217 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
elessair 0:f269e3021894 1218 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 1219
elessair 0:f269e3021894 1220 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
elessair 0:f269e3021894 1221 ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) ) == 0 )
elessair 0:f269e3021894 1222 {
elessair 0:f269e3021894 1223 return( 0 );
elessair 0:f269e3021894 1224 }
elessair 0:f269e3021894 1225
elessair 0:f269e3021894 1226 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1227 #endif /* MBEDTLS_RSA_C */
elessair 0:f269e3021894 1228
elessair 0:f269e3021894 1229 #if defined(MBEDTLS_ECP_C)
elessair 0:f269e3021894 1230 if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == NULL )
elessair 0:f269e3021894 1231 return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
elessair 0:f269e3021894 1232
elessair 0:f269e3021894 1233 if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
elessair 0:f269e3021894 1234 ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), key, keylen ) ) == 0 )
elessair 0:f269e3021894 1235 {
elessair 0:f269e3021894 1236 return( 0 );
elessair 0:f269e3021894 1237 }
elessair 0:f269e3021894 1238
elessair 0:f269e3021894 1239 mbedtls_pk_free( pk );
elessair 0:f269e3021894 1240 #endif /* MBEDTLS_ECP_C */
elessair 0:f269e3021894 1241
elessair 0:f269e3021894 1242 return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
elessair 0:f269e3021894 1243 }
elessair 0:f269e3021894 1244
elessair 0:f269e3021894 1245 /*
elessair 0:f269e3021894 1246 * Parse a public key
elessair 0:f269e3021894 1247 */
elessair 0:f269e3021894 1248 int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
elessair 0:f269e3021894 1249 const unsigned char *key, size_t keylen )
elessair 0:f269e3021894 1250 {
elessair 0:f269e3021894 1251 int ret;
elessair 0:f269e3021894 1252 unsigned char *p;
elessair 0:f269e3021894 1253 #if defined(MBEDTLS_PEM_PARSE_C)
elessair 0:f269e3021894 1254 size_t len;
elessair 0:f269e3021894 1255 mbedtls_pem_context pem;
elessair 0:f269e3021894 1256
elessair 0:f269e3021894 1257 mbedtls_pem_init( &pem );
elessair 0:f269e3021894 1258
elessair 0:f269e3021894 1259 /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
elessair 0:f269e3021894 1260 if( keylen == 0 || key[keylen - 1] != '\0' )
elessair 0:f269e3021894 1261 ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
elessair 0:f269e3021894 1262 else
elessair 0:f269e3021894 1263 ret = mbedtls_pem_read_buffer( &pem,
elessair 0:f269e3021894 1264 "-----BEGIN PUBLIC KEY-----",
elessair 0:f269e3021894 1265 "-----END PUBLIC KEY-----",
elessair 0:f269e3021894 1266 key, NULL, 0, &len );
elessair 0:f269e3021894 1267
elessair 0:f269e3021894 1268 if( ret == 0 )
elessair 0:f269e3021894 1269 {
elessair 0:f269e3021894 1270 /*
elessair 0:f269e3021894 1271 * Was PEM encoded
elessair 0:f269e3021894 1272 */
elessair 0:f269e3021894 1273 key = pem.buf;
elessair 0:f269e3021894 1274 keylen = pem.buflen;
elessair 0:f269e3021894 1275 }
elessair 0:f269e3021894 1276 else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
elessair 0:f269e3021894 1277 {
elessair 0:f269e3021894 1278 mbedtls_pem_free( &pem );
elessair 0:f269e3021894 1279 return( ret );
elessair 0:f269e3021894 1280 }
elessair 0:f269e3021894 1281 #endif /* MBEDTLS_PEM_PARSE_C */
elessair 0:f269e3021894 1282 p = (unsigned char *) key;
elessair 0:f269e3021894 1283
elessair 0:f269e3021894 1284 ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
elessair 0:f269e3021894 1285
elessair 0:f269e3021894 1286 #if defined(MBEDTLS_PEM_PARSE_C)
elessair 0:f269e3021894 1287 mbedtls_pem_free( &pem );
elessair 0:f269e3021894 1288 #endif
elessair 0:f269e3021894 1289
elessair 0:f269e3021894 1290 return( ret );
elessair 0:f269e3021894 1291 }
elessair 0:f269e3021894 1292
elessair 0:f269e3021894 1293 #endif /* MBEDTLS_PK_PARSE_C */