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