Modified mbed TLS headers for AES functionality only to reduce build size

Dependents:   BLE_Gateway_Linker_fix BLE_Gateway

Fork of mbedtls by sandbox

Committer:
electronichamsters
Date:
Mon Jul 10 04:00:25 2017 +0000
Revision:
5:f09f5ed830ca
Parent:
1:24750b9ad5ef
working gateway

Who changed what in which revision?

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