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