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