This is a fork due to permission issues

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Fork of 6_songs-from-the-cloud by MakingMusicWorkshop

Committer:
maclobdell
Date:
Wed May 18 19:06:32 2016 +0000
Revision:
0:f7c60d3e7b8a
clean version

Who changed what in which revision?

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