Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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