nkjnm

Dependencies:   MAX44000 nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Committer:
nexpaq
Date:
Sat Sep 17 16:32:05 2016 +0000
Revision:
1:55a6170b404f
checking in for sharing

Who changed what in which revision?

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