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" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 33 | #include "mbedtls/platform_util.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 34 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 35 | #include <string.h> |
| 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/rsa.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 39 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 41 | #include "mbedtls/ecp.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 42 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #if defined(MBEDTLS_ECDSA_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 44 | #include "mbedtls/ecdsa.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 45 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 47 | #include "mbedtls/pem.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 48 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if defined(MBEDTLS_PKCS5_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/pkcs5.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 51 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #if defined(MBEDTLS_PKCS12_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 53 | #include "mbedtls/pkcs12.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 54 | #endif |
Jarno Lamsa | 42b83db | 2019-04-16 16:48:22 +0300 | [diff] [blame] | 55 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 56 | #include "mbedtls/ecc.h" |
| 57 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/platform.h" |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 61 | #else |
| 62 | #include <stdlib.h> |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 63 | #define mbedtls_calloc calloc |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | #define mbedtls_free free |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 65 | #endif |
| 66 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 67 | /* Parameter validation macros based on platform_util.h */ |
| 68 | #define PK_VALIDATE_RET( cond ) \ |
| 69 | MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA ) |
| 70 | #define PK_VALIDATE( cond ) \ |
| 71 | MBEDTLS_INTERNAL_VALIDATE( cond ) |
| 72 | |
Gilles Peskine | 832f349 | 2017-11-30 11:42:12 +0100 | [diff] [blame] | 73 | #if defined(MBEDTLS_FS_IO) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 74 | /* |
| 75 | * Load all data from a file into a given buffer. |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 76 | * |
| 77 | * The file is expected to contain either PEM or DER encoded data. |
| 78 | * A terminating null byte is always appended. It is included in the announced |
| 79 | * length only if the data looks like it is PEM encoded. |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 80 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | 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] | 82 | { |
| 83 | FILE *f; |
| 84 | long size; |
| 85 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 86 | PK_VALIDATE_RET( path != NULL ); |
| 87 | PK_VALIDATE_RET( buf != NULL ); |
| 88 | PK_VALIDATE_RET( n != NULL ); |
| 89 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 90 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | return( MBEDTLS_ERR_PK_FILE_IO_ERROR ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 92 | |
| 93 | fseek( f, 0, SEEK_END ); |
| 94 | if( ( size = ftell( f ) ) == -1 ) |
| 95 | { |
| 96 | fclose( f ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | return( MBEDTLS_ERR_PK_FILE_IO_ERROR ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 98 | } |
| 99 | fseek( f, 0, SEEK_SET ); |
| 100 | |
| 101 | *n = (size_t) size; |
| 102 | |
| 103 | if( *n + 1 == 0 || |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 104 | ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 105 | { |
| 106 | fclose( f ); |
Manuel Pégourié-Gonnard | 6a8ca33 | 2015-05-28 09:33:39 +0200 | [diff] [blame] | 107 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | if( fread( *buf, 1, *n, f ) != *n ) |
| 111 | { |
| 112 | fclose( f ); |
Andres Amaya Garcia | 1f2666f | 2017-06-26 10:36:20 +0100 | [diff] [blame] | 113 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 114 | mbedtls_platform_zeroize( *buf, *n ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | mbedtls_free( *buf ); |
Andres Amaya Garcia | 1f2666f | 2017-06-26 10:36:20 +0100 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | return( MBEDTLS_ERR_PK_FILE_IO_ERROR ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | fclose( f ); |
| 121 | |
| 122 | (*buf)[*n] = '\0'; |
| 123 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 124 | if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL ) |
| 125 | ++*n; |
| 126 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 127 | return( 0 ); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Load and parse a private key |
| 132 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 134 | const char *path, const char *pwd ) |
| 135 | { |
| 136 | int ret; |
| 137 | size_t n; |
| 138 | unsigned char *buf; |
| 139 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 140 | PK_VALIDATE_RET( ctx != NULL ); |
Gilles Peskine | 8c71b3e | 2018-12-19 17:37:02 +0100 | [diff] [blame] | 141 | PK_VALIDATE_RET( path != NULL ); |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 144 | return( ret ); |
| 145 | |
| 146 | if( pwd == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 148 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | ret = mbedtls_pk_parse_key( ctx, buf, n, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 150 | (const unsigned char *) pwd, strlen( pwd ) ); |
| 151 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 152 | mbedtls_platform_zeroize( buf, n ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | mbedtls_free( buf ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 154 | |
| 155 | return( ret ); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Load and parse a public key |
| 160 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | 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] | 162 | { |
| 163 | int ret; |
| 164 | size_t n; |
| 165 | unsigned char *buf; |
| 166 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 167 | PK_VALIDATE_RET( ctx != NULL ); |
Gilles Peskine | 8c71b3e | 2018-12-19 17:37:02 +0100 | [diff] [blame] | 168 | PK_VALIDATE_RET( path != NULL ); |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 169 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 171 | return( ret ); |
| 172 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 173 | ret = mbedtls_pk_parse_public_key( ctx, buf, n ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 174 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 175 | mbedtls_platform_zeroize( buf, n ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | mbedtls_free( buf ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 177 | |
| 178 | return( ret ); |
| 179 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | #endif /* MBEDTLS_FS_IO */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 181 | |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 182 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 183 | static int pk_use_ecparams( const mbedtls_asn1_buf *params ) |
| 184 | { |
| 185 | uint32_t grp_id; |
| 186 | |
| 187 | if( params->tag == MBEDTLS_ASN1_OID ) |
| 188 | { |
| 189 | if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 ) |
| 190 | return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE ); |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | // Only P-256 is supported |
| 195 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
| 196 | } |
| 197 | |
| 198 | return( 0 ); |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | #if defined(MBEDTLS_ECP_C) || \ |
| 203 | defined(MBEDTLS_USE_TINYCRYPT) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | /* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 205 | * |
| 206 | * ECParameters ::= CHOICE { |
| 207 | * namedCurve OBJECT IDENTIFIER |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 208 | * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 209 | * -- implicitCurve NULL |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 210 | * } |
| 211 | */ |
| 212 | 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] | 213 | mbedtls_asn1_buf *params ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 214 | { |
| 215 | int ret; |
| 216 | |
Sanne Wouda | b2b29d5 | 2017-08-21 15:58:12 +0100 | [diff] [blame] | 217 | if ( end - *p < 1 ) |
Sanne Wouda | 7b2e85d | 2017-08-30 21:10:42 +0100 | [diff] [blame] | 218 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 219 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Sanne Wouda | b2b29d5 | 2017-08-21 15:58:12 +0100 | [diff] [blame] | 220 | |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 221 | /* Tag may be either OID or SEQUENCE */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 222 | params->tag = **p; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | if( params->tag != MBEDTLS_ASN1_OID |
| 224 | #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED) |
| 225 | && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) |
Manuel Pégourié-Gonnard | 6fac351 | 2014-03-19 16:39:52 +0100 | [diff] [blame] | 226 | #endif |
| 227 | ) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 228 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 230 | MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 231 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 232 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | 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] | 234 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 235 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 236 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 237 | |
| 238 | params->p = *p; |
| 239 | *p += params->len; |
| 240 | |
| 241 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 243 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 244 | |
| 245 | return( 0 ); |
| 246 | } |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 247 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 248 | |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 249 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 251 | /* |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 252 | * Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it. |
| 253 | * WARNING: the resulting group should only be used with |
| 254 | * pk_group_id_from_specified(), since its base point may not be set correctly |
| 255 | * if it was encoded compressed. |
| 256 | * |
| 257 | * SpecifiedECDomain ::= SEQUENCE { |
| 258 | * version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...), |
| 259 | * fieldID FieldID {{FieldTypes}}, |
| 260 | * curve Curve, |
| 261 | * base ECPoint, |
| 262 | * order INTEGER, |
| 263 | * cofactor INTEGER OPTIONAL, |
| 264 | * hash HashAlgorithm OPTIONAL, |
| 265 | * ... |
| 266 | * } |
| 267 | * |
| 268 | * We only support prime-field as field type, and ignore hash and cofactor. |
| 269 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | 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] | 271 | { |
| 272 | int ret; |
| 273 | unsigned char *p = params->p; |
| 274 | const unsigned char * const end = params->p + params->len; |
| 275 | const unsigned char *end_field, *end_curve; |
| 276 | size_t len; |
| 277 | int ver; |
| 278 | |
| 279 | /* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 ) |
| 281 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 282 | |
| 283 | if( ver < 1 || ver > 3 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 285 | |
| 286 | /* |
| 287 | * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field |
| 288 | * fieldType FIELD-ID.&id({IOSet}), |
| 289 | * parameters FIELD-ID.&Type({IOSet}{@fieldType}) |
| 290 | * } |
| 291 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 292 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 293 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 294 | return( ret ); |
| 295 | |
| 296 | end_field = p + len; |
| 297 | |
| 298 | /* |
| 299 | * FIELD-ID ::= TYPE-IDENTIFIER |
| 300 | * FieldTypes FIELD-ID ::= { |
| 301 | * { Prime-p IDENTIFIED BY prime-field } | |
| 302 | * { Characteristic-two IDENTIFIED BY characteristic-two-field } |
| 303 | * } |
| 304 | * prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 } |
| 305 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | 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] | 307 | return( ret ); |
| 308 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 309 | if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) || |
| 310 | 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] | 311 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | p += len; |
| 316 | |
| 317 | /* Prime-p ::= INTEGER -- Field of size p. */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 ) |
| 319 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 320 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 321 | grp->pbits = mbedtls_mpi_bitlen( &grp->P ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 322 | |
| 323 | if( p != end_field ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 324 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 325 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 326 | |
| 327 | /* |
| 328 | * Curve ::= SEQUENCE { |
| 329 | * a FieldElement, |
| 330 | * b FieldElement, |
| 331 | * seed BIT STRING OPTIONAL |
| 332 | * -- Shall be present if used in SpecifiedECDomain |
| 333 | * -- with version equal to ecdpVer2 or ecdpVer3 |
| 334 | * } |
| 335 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 337 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 338 | return( ret ); |
| 339 | |
| 340 | end_curve = p + len; |
| 341 | |
| 342 | /* |
| 343 | * FieldElement ::= OCTET STRING |
| 344 | * containing an integer in the case of a prime field |
| 345 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 || |
| 347 | ( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 ) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +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 + ret ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | p += len; |
| 353 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 354 | if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 || |
| 355 | ( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 ) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 356 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 357 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | p += len; |
| 361 | |
| 362 | /* Ignore seed BIT STRING OPTIONAL */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | 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] | 364 | p += len; |
| 365 | |
| 366 | if( p != end_curve ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 367 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 368 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 369 | |
| 370 | /* |
| 371 | * ECPoint ::= OCTET STRING |
| 372 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 373 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 374 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 375 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 376 | if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G, |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 377 | ( const unsigned char *) p, len ) ) != 0 ) |
| 378 | { |
| 379 | /* |
| 380 | * If we can't read the point because it's compressed, cheat by |
| 381 | * reading only the X coordinate and the parity bit of Y. |
| 382 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE || |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 384 | ( p[0] != 0x02 && p[0] != 0x03 ) || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 385 | len != mbedtls_mpi_size( &grp->P ) + 1 || |
| 386 | mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 || |
| 387 | mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 || |
| 388 | mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 ) |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 394 | p += len; |
| 395 | |
| 396 | /* |
| 397 | * order INTEGER |
| 398 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 ) |
| 400 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 401 | |
Manuel Pégourié-Gonnard | c0696c2 | 2015-06-18 16:47:17 +0200 | [diff] [blame] | 402 | grp->nbits = mbedtls_mpi_bitlen( &grp->N ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 403 | |
| 404 | /* |
| 405 | * Allow optional elements by purposefully not enforcing p == end here. |
| 406 | */ |
| 407 | |
| 408 | return( 0 ); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * Find the group id associated with an (almost filled) group as generated by |
| 413 | * pk_group_from_specified(), or return an error if unknown. |
| 414 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | 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] | 416 | { |
Manuel Pégourié-Gonnard | 5b8c409 | 2014-03-27 14:59:42 +0100 | [diff] [blame] | 417 | int ret = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 418 | mbedtls_ecp_group ref; |
| 419 | const mbedtls_ecp_group_id *id; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 420 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 421 | mbedtls_ecp_group_init( &ref ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | 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] | 424 | { |
| 425 | /* Load the group associated to that id */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | mbedtls_ecp_group_free( &ref ); |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 427 | MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 428 | |
| 429 | /* Compare to the group we were given, starting with easy tests */ |
| 430 | if( grp->pbits == ref.pbits && grp->nbits == ref.nbits && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 431 | mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 && |
| 432 | mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 && |
| 433 | mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 && |
| 434 | mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 && |
| 435 | mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 && |
| 436 | 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] | 437 | /* 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] | 438 | 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] | 439 | { |
| 440 | break; |
| 441 | } |
| 442 | |
| 443 | } |
| 444 | |
| 445 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 446 | mbedtls_ecp_group_free( &ref ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 447 | |
| 448 | *grp_id = *id; |
| 449 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 450 | if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE ) |
| 451 | ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 452 | |
| 453 | return( ret ); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID |
| 458 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | static int pk_group_id_from_specified( const mbedtls_asn1_buf *params, |
| 460 | mbedtls_ecp_group_id *grp_id ) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 461 | { |
| 462 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 463 | mbedtls_ecp_group grp; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 464 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 465 | mbedtls_ecp_group_init( &grp ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 466 | |
| 467 | if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 ) |
| 468 | goto cleanup; |
| 469 | |
| 470 | ret = pk_group_id_from_group( &grp, grp_id ); |
| 471 | |
| 472 | cleanup: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 473 | mbedtls_ecp_group_free( &grp ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 474 | |
| 475 | return( ret ); |
| 476 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 477 | #endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */ |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 478 | |
| 479 | /* |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 480 | * Use EC parameters to initialise an EC group |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 481 | * |
| 482 | * ECParameters ::= CHOICE { |
| 483 | * namedCurve OBJECT IDENTIFIER |
| 484 | * specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... } |
| 485 | * -- implicitCurve NULL |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 486 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 487 | 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] | 488 | { |
| 489 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 490 | mbedtls_ecp_group_id grp_id; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 491 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 492 | if( params->tag == MBEDTLS_ASN1_OID ) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 493 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 494 | if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 ) |
| 495 | return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE ); |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 496 | } |
| 497 | else |
| 498 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 499 | #if defined(MBEDTLS_PK_PARSE_EC_EXTENDED) |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 500 | if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 ) |
| 501 | return( ret ); |
Manuel Pégourié-Gonnard | 6fac351 | 2014-03-19 16:39:52 +0100 | [diff] [blame] | 502 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 503 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
Manuel Pégourié-Gonnard | 6fac351 | 2014-03-19 16:39:52 +0100 | [diff] [blame] | 504 | #endif |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 505 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 506 | |
| 507 | /* |
| 508 | * grp may already be initilialized; if so, make sure IDs match |
| 509 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 510 | if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id ) |
| 511 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 512 | |
Manuel Pégourié-Gonnard | e3a062b | 2015-05-11 18:46:47 +0200 | [diff] [blame] | 513 | if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 514 | return( ret ); |
| 515 | |
| 516 | return( 0 ); |
| 517 | } |
| 518 | |
| 519 | /* |
| 520 | * EC public key is an EC point |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 521 | * |
| 522 | * The caller is responsible for clearing the structure upon failure if |
| 523 | * 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] | 524 | * 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] | 525 | */ |
| 526 | 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] | 527 | mbedtls_ecp_keypair *key ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 528 | { |
| 529 | int ret; |
| 530 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 531 | 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] | 532 | (const unsigned char *) *p, end - *p ) ) == 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | * We know mbedtls_ecp_point_read_binary consumed all bytes or failed |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 539 | */ |
| 540 | *p = (unsigned char *) end; |
| 541 | |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 542 | return( ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 543 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 545 | |
Jarno Lamsa | 42b83db | 2019-04-16 16:48:22 +0300 | [diff] [blame] | 546 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 547 | /* |
| 548 | * Import a point from unsigned binary data (SEC1 2.3.4) |
| 549 | */ |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 550 | static int uecc_public_key_read_binary( uint8_t *pt, |
Jarno Lamsa | 42b83db | 2019-04-16 16:48:22 +0300 | [diff] [blame] | 551 | const unsigned char *buf, size_t ilen ) |
| 552 | { |
| 553 | |
| 554 | if( ilen < 1 ) |
| 555 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY ); |
| 556 | |
| 557 | //We are not handling the infinity point right now |
| 558 | |
| 559 | if( buf[0] != 0x04 ) |
| 560 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); |
| 561 | |
| 562 | if( ilen != 2 * NUM_ECC_BYTES + 1 ) |
| 563 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY ); |
| 564 | |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 565 | memcpy( pt, buf + 1, ilen - 1); |
Jarno Lamsa | 42b83db | 2019-04-16 16:48:22 +0300 | [diff] [blame] | 566 | |
| 567 | return( 0 ); |
| 568 | } |
| 569 | |
| 570 | static int pk_get_ueccpubkey( unsigned char **p, |
| 571 | const unsigned char *end, |
| 572 | uint8_t *pk_context) |
| 573 | { |
| 574 | int ret; |
| 575 | |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 576 | ret = uecc_public_key_read_binary( pk_context, |
Jarno Lamsa | 42b83db | 2019-04-16 16:48:22 +0300 | [diff] [blame] | 577 | (const unsigned char *) *p, end - *p ); |
| 578 | |
| 579 | /* |
| 580 | * We know uecc_public_key_read_binary consumed all bytes or failed |
| 581 | */ |
| 582 | *p = (unsigned char *) end; |
| 583 | |
| 584 | return( ret ); |
| 585 | } |
| 586 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
| 587 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 589 | /* |
| 590 | * RSAPublicKey ::= SEQUENCE { |
| 591 | * modulus INTEGER, -- n |
| 592 | * publicExponent INTEGER -- e |
| 593 | * } |
| 594 | */ |
| 595 | static int pk_get_rsapubkey( unsigned char **p, |
| 596 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 597 | mbedtls_rsa_context *rsa ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 598 | { |
| 599 | int ret; |
| 600 | size_t len; |
| 601 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 602 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 603 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 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 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 610 | /* Import N */ |
| 611 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 612 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 613 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 614 | if( ( ret = mbedtls_rsa_import_raw( rsa, *p, len, NULL, 0, NULL, 0, |
| 615 | NULL, 0, NULL, 0 ) ) != 0 ) |
| 616 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY ); |
| 617 | |
| 618 | *p += len; |
| 619 | |
| 620 | /* Import E */ |
| 621 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, MBEDTLS_ASN1_INTEGER ) ) != 0 ) |
| 622 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret ); |
| 623 | |
| 624 | if( ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0, |
| 625 | NULL, 0, *p, len ) ) != 0 ) |
| 626 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY ); |
| 627 | |
| 628 | *p += len; |
| 629 | |
Hanno Becker | 895c5ab | 2018-01-05 08:08:09 +0000 | [diff] [blame] | 630 | if( mbedtls_rsa_complete( rsa ) != 0 || |
| 631 | mbedtls_rsa_check_pubkey( rsa ) != 0 ) |
| 632 | { |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 633 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY ); |
Hanno Becker | 895c5ab | 2018-01-05 08:08:09 +0000 | [diff] [blame] | 634 | } |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 635 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 636 | if( *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 637 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY + |
| 638 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 639 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 640 | return( 0 ); |
| 641 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 642 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 643 | |
| 644 | /* Get a PK algorithm identifier |
| 645 | * |
| 646 | * AlgorithmIdentifier ::= SEQUENCE { |
| 647 | * algorithm OBJECT IDENTIFIER, |
| 648 | * parameters ANY DEFINED BY algorithm OPTIONAL } |
| 649 | */ |
| 650 | static int pk_get_pk_alg( unsigned char **p, |
| 651 | const unsigned char *end, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 652 | mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 653 | { |
| 654 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 655 | mbedtls_asn1_buf alg_oid; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 656 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 657 | memset( params, 0, sizeof(mbedtls_asn1_buf) ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 658 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 659 | if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 ) |
| 660 | return( MBEDTLS_ERR_PK_INVALID_ALG + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 661 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 662 | if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 ) |
| 663 | return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 664 | |
| 665 | /* |
| 666 | * No parameters with RSA (only for EC) |
| 667 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 668 | if( *pk_alg == MBEDTLS_PK_RSA && |
| 669 | ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) || |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 670 | params->len != 0 ) ) |
| 671 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 672 | return( MBEDTLS_ERR_PK_INVALID_ALG ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | return( 0 ); |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 680 | * algorithm AlgorithmIdentifier, |
| 681 | * subjectPublicKey BIT STRING } |
| 682 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 683 | int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end, |
| 684 | mbedtls_pk_context *pk ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 685 | { |
| 686 | int ret; |
| 687 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 688 | mbedtls_asn1_buf alg_params; |
| 689 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 690 | const mbedtls_pk_info_t *pk_info; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 691 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 692 | PK_VALIDATE_RET( p != NULL ); |
| 693 | PK_VALIDATE_RET( *p != NULL ); |
| 694 | PK_VALIDATE_RET( end != NULL ); |
| 695 | PK_VALIDATE_RET( pk != NULL ); |
| 696 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 697 | if( ( ret = mbedtls_asn1_get_tag( p, end, &len, |
| 698 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 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 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | end = *p + len; |
| 704 | |
| 705 | if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 ) |
| 706 | return( ret ); |
| 707 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 708 | if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 ) |
| 709 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 710 | |
| 711 | if( *p + len != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 712 | return( MBEDTLS_ERR_PK_INVALID_PUBKEY + |
| 713 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
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 | if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL ) |
| 716 | return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 717 | |
Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 718 | if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 719 | return( ret ); |
| 720 | |
Jarno Lamsa | 42b83db | 2019-04-16 16:48:22 +0300 | [diff] [blame] | 721 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 722 | if( pk_alg == MBEDTLS_PK_ECDSA ) |
| 723 | { |
| 724 | ret = pk_get_ueccpubkey( p, end, (uint8_t*) pk->pk_ctx ); |
| 725 | } |
| 726 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 727 | #if defined(MBEDTLS_RSA_C) |
| 728 | if( pk_alg == MBEDTLS_PK_RSA ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 729 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 730 | ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 731 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 732 | #endif /* MBEDTLS_RSA_C */ |
| 733 | #if defined(MBEDTLS_ECP_C) |
| 734 | if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 735 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 736 | ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 737 | if( ret == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 738 | ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 739 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 740 | #endif /* MBEDTLS_ECP_C */ |
| 741 | ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 742 | |
| 743 | if( ret == 0 && *p != end ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 744 | ret = MBEDTLS_ERR_PK_INVALID_PUBKEY |
| 745 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 746 | |
| 747 | if( ret != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 748 | mbedtls_pk_free( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 749 | |
| 750 | return( ret ); |
| 751 | } |
| 752 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 753 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 754 | /* |
| 755 | * Parse a PKCS#1 encoded private RSA key |
| 756 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 757 | static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 758 | const unsigned char *key, |
| 759 | size_t keylen ) |
| 760 | { |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 761 | int ret, version; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 762 | size_t len; |
| 763 | unsigned char *p, *end; |
| 764 | |
Hanno Becker | efa14e8 | 2017-10-11 19:45:19 +0100 | [diff] [blame] | 765 | mbedtls_mpi T; |
| 766 | mbedtls_mpi_init( &T ); |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 767 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 768 | p = (unsigned char *) key; |
| 769 | end = p + keylen; |
| 770 | |
| 771 | /* |
| 772 | * This function parses the RSAPrivateKey (PKCS#1) |
| 773 | * |
| 774 | * RSAPrivateKey ::= SEQUENCE { |
| 775 | * version Version, |
| 776 | * modulus INTEGER, -- n |
| 777 | * publicExponent INTEGER, -- e |
| 778 | * privateExponent INTEGER, -- d |
| 779 | * prime1 INTEGER, -- p |
| 780 | * prime2 INTEGER, -- q |
| 781 | * exponent1 INTEGER, -- d mod (p-1) |
| 782 | * exponent2 INTEGER, -- d mod (q-1) |
| 783 | * coefficient INTEGER, -- (inverse of q) mod p |
| 784 | * otherPrimeInfos OtherPrimeInfos OPTIONAL |
| 785 | * } |
| 786 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 787 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 788 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 789 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 790 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | end = p + len; |
| 794 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 795 | if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 796 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 797 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 798 | } |
| 799 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 800 | if( version != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 801 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 802 | return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 803 | } |
| 804 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 805 | /* Import N */ |
| 806 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 807 | MBEDTLS_ASN1_INTEGER ) ) != 0 || |
| 808 | ( ret = mbedtls_rsa_import_raw( rsa, p, len, NULL, 0, NULL, 0, |
| 809 | NULL, 0, NULL, 0 ) ) != 0 ) |
| 810 | goto cleanup; |
| 811 | p += len; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 812 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 813 | /* Import E */ |
| 814 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 815 | MBEDTLS_ASN1_INTEGER ) ) != 0 || |
| 816 | ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0, |
| 817 | NULL, 0, p, len ) ) != 0 ) |
| 818 | goto cleanup; |
| 819 | p += len; |
| 820 | |
| 821 | /* Import D */ |
| 822 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 823 | MBEDTLS_ASN1_INTEGER ) ) != 0 || |
| 824 | ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, NULL, 0, |
| 825 | p, len, NULL, 0 ) ) != 0 ) |
| 826 | goto cleanup; |
| 827 | p += len; |
| 828 | |
| 829 | /* Import P */ |
| 830 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 831 | MBEDTLS_ASN1_INTEGER ) ) != 0 || |
| 832 | ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, p, len, NULL, 0, |
| 833 | NULL, 0, NULL, 0 ) ) != 0 ) |
| 834 | goto cleanup; |
| 835 | p += len; |
| 836 | |
| 837 | /* Import Q */ |
| 838 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 839 | MBEDTLS_ASN1_INTEGER ) ) != 0 || |
| 840 | ( ret = mbedtls_rsa_import_raw( rsa, NULL, 0, NULL, 0, p, len, |
| 841 | NULL, 0, NULL, 0 ) ) != 0 ) |
| 842 | goto cleanup; |
| 843 | p += len; |
| 844 | |
| 845 | /* Complete the RSA private key */ |
Hanno Becker | 7f25f85 | 2017-10-10 16:56:22 +0100 | [diff] [blame] | 846 | if( ( ret = mbedtls_rsa_complete( rsa ) ) != 0 ) |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 847 | goto cleanup; |
| 848 | |
| 849 | /* Check optional parameters */ |
Hanno Becker | efa14e8 | 2017-10-11 19:45:19 +0100 | [diff] [blame] | 850 | if( ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 || |
| 851 | ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 || |
| 852 | ( ret = mbedtls_asn1_get_mpi( &p, end, &T ) ) != 0 ) |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 853 | goto cleanup; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 854 | |
| 855 | if( p != end ) |
| 856 | { |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 857 | ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 858 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 859 | } |
| 860 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 861 | cleanup: |
| 862 | |
Hanno Becker | efa14e8 | 2017-10-11 19:45:19 +0100 | [diff] [blame] | 863 | mbedtls_mpi_free( &T ); |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 864 | |
| 865 | if( ret != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 866 | { |
Hanno Becker | efa14e8 | 2017-10-11 19:45:19 +0100 | [diff] [blame] | 867 | /* Wrap error code if it's coming from a lower level */ |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 868 | if( ( ret & 0xff80 ) == 0 ) |
| 869 | ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret; |
| 870 | else |
| 871 | ret = MBEDTLS_ERR_PK_KEY_INVALID_FORMAT; |
| 872 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 873 | mbedtls_rsa_free( rsa ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 874 | } |
| 875 | |
Hanno Becker | d58c5b2 | 2017-08-22 14:33:21 +0100 | [diff] [blame] | 876 | return( ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 877 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 878 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 879 | |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 880 | #if defined(MBEDTLS_USE_TINYCRYPT) |
| 881 | static int pk_parse_key_sec1_der( mbedtls_uecc_keypair *keypair, |
| 882 | const unsigned char *key, |
| 883 | size_t keylen) |
| 884 | { |
| 885 | int ret; |
| 886 | int version, pubkey_done; |
| 887 | size_t len; |
| 888 | mbedtls_asn1_buf params; |
| 889 | unsigned char *p = (unsigned char *) key; |
| 890 | unsigned char *end = p + keylen; |
| 891 | unsigned char *end2; |
| 892 | |
| 893 | /* |
| 894 | * RFC 5915, or SEC1 Appendix C.4 |
| 895 | * |
| 896 | * ECPrivateKey ::= SEQUENCE { |
| 897 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 898 | * privateKey OCTET STRING, |
| 899 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 900 | * publicKey [1] BIT STRING OPTIONAL |
| 901 | * } |
| 902 | */ |
| 903 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 904 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
| 905 | { |
| 906 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
| 907 | } |
| 908 | |
| 909 | end = p + len; |
| 910 | |
| 911 | if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 ) |
| 912 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
| 913 | |
| 914 | if( version != 1 ) |
| 915 | return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION ); |
| 916 | |
| 917 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 918 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
| 919 | |
| 920 | memcpy(keypair->private_key, p, len); |
| 921 | |
| 922 | p += len; |
| 923 | |
| 924 | pubkey_done = 0; |
| 925 | if( p != end ) |
| 926 | { |
| 927 | /* |
| 928 | * Is 'parameters' present? |
| 929 | */ |
| 930 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 931 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 ) |
| 932 | { |
| 933 | if( ( ret = pk_get_ecparams( &p, p + len, ¶ms) ) != 0 || |
| 934 | ( ret = pk_use_ecparams( ¶ms ) ) != 0 ) |
| 935 | { |
| 936 | return( ret ); |
| 937 | } |
| 938 | } |
| 939 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 940 | { |
| 941 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | if( p != end ) |
| 946 | { |
| 947 | /* |
| 948 | * Is 'publickey' present? If not, or if we can't read it (eg because it |
| 949 | * is compressed), create it from the private key. |
| 950 | */ |
| 951 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 952 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 ) |
| 953 | { |
| 954 | end2 = p + len; |
| 955 | |
| 956 | if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 ) |
| 957 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
| 958 | |
| 959 | if( p + len != end2 ) |
| 960 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 961 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
| 962 | |
| 963 | if( ( ret = uecc_public_key_read_binary( keypair->public_key, |
| 964 | (const unsigned char *) p, end2 - p ) ) == 0 ) |
| 965 | pubkey_done = 1; |
| 966 | else |
| 967 | { |
| 968 | /* |
| 969 | * The only acceptable failure mode of pk_get_ecpubkey() above |
| 970 | * is if the point format is not recognized. |
| 971 | */ |
| 972 | if( ret != MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ) |
| 973 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
| 974 | } |
| 975 | } |
| 976 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
| 977 | { |
| 978 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
| 979 | } |
| 980 | } |
| 981 | |
| 982 | //TODO: Do we need to support derived public keys with uecc? |
| 983 | |
| 984 | return( 0 ); |
| 985 | } |
| 986 | #else |
| 987 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 988 | #if defined(MBEDTLS_ECP_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 989 | /* |
| 990 | * Parse a SEC1 encoded private EC key |
| 991 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 992 | static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 993 | const unsigned char *key, |
| 994 | size_t keylen ) |
| 995 | { |
| 996 | int ret; |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 997 | int version, pubkey_done; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 998 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 999 | mbedtls_asn1_buf params; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1000 | unsigned char *p = (unsigned char *) key; |
| 1001 | unsigned char *end = p + keylen; |
| 1002 | unsigned char *end2; |
| 1003 | |
| 1004 | /* |
| 1005 | * RFC 5915, or SEC1 Appendix C.4 |
| 1006 | * |
| 1007 | * ECPrivateKey ::= SEQUENCE { |
| 1008 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 1009 | * privateKey OCTET STRING, |
| 1010 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 1011 | * publicKey [1] BIT STRING OPTIONAL |
| 1012 | * } |
| 1013 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1014 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1015 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1016 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1017 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | end = p + len; |
| 1021 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1022 | if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 ) |
| 1023 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1024 | |
| 1025 | if( version != 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1026 | return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1027 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1028 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 1029 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1030 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1031 | if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1032 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1033 | mbedtls_ecp_keypair_free( eck ); |
| 1034 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | p += len; |
| 1038 | |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1039 | pubkey_done = 0; |
| 1040 | if( p != end ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1041 | { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1042 | /* |
| 1043 | * Is 'parameters' present? |
| 1044 | */ |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1045 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1046 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 ) |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1047 | { |
| 1048 | if( ( ret = pk_get_ecparams( &p, p + len, ¶ms) ) != 0 || |
| 1049 | ( ret = pk_use_ecparams( ¶ms, &eck->grp ) ) != 0 ) |
| 1050 | { |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1051 | mbedtls_ecp_keypair_free( eck ); |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1052 | return( ret ); |
| 1053 | } |
| 1054 | } |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1055 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1056 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1057 | mbedtls_ecp_keypair_free( eck ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1058 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | 5246ee5 | 2014-03-19 16:18:38 +0100 | [diff] [blame] | 1059 | } |
Jethro Beekman | d2df936 | 2018-02-16 13:11:04 -0800 | [diff] [blame] | 1060 | } |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1061 | |
Jethro Beekman | d2df936 | 2018-02-16 13:11:04 -0800 | [diff] [blame] | 1062 | if( p != end ) |
| 1063 | { |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1064 | /* |
| 1065 | * Is 'publickey' present? If not, or if we can't read it (eg because it |
| 1066 | * is compressed), create it from the private key. |
| 1067 | */ |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1068 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1069 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 ) |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1070 | { |
| 1071 | end2 = p + len; |
| 1072 | |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1073 | if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 ) |
| 1074 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1075 | |
| 1076 | if( p + len != end2 ) |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1077 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 1078 | MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ); |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1079 | |
| 1080 | if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 ) |
| 1081 | pubkey_done = 1; |
| 1082 | else |
| 1083 | { |
| 1084 | /* |
| 1085 | * The only acceptable failure mode of pk_get_ecpubkey() above |
| 1086 | * is if the point format is not recognized. |
| 1087 | */ |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1088 | if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ) |
| 1089 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1090 | } |
| 1091 | } |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1092 | else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1093 | { |
Manuel Pégourié-Gonnard | e1e5871 | 2015-04-15 10:50:34 +0200 | [diff] [blame] | 1094 | mbedtls_ecp_keypair_free( eck ); |
| 1095 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | 924cd10 | 2015-04-14 11:18:04 +0200 | [diff] [blame] | 1096 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1097 | } |
Manuel Pégourié-Gonnard | eab20d2 | 2014-03-14 17:58:42 +0100 | [diff] [blame] | 1098 | |
| 1099 | if( ! pubkey_done && |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1100 | ( 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] | 1101 | NULL, NULL ) ) != 0 ) |
Manuel Pégourié-Gonnard | ff29f9c | 2013-09-18 16:13:02 +0200 | [diff] [blame] | 1102 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1103 | mbedtls_ecp_keypair_free( eck ); |
| 1104 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Manuel Pégourié-Gonnard | ff29f9c | 2013-09-18 16:13:02 +0200 | [diff] [blame] | 1105 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1106 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1107 | if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 ) |
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 | mbedtls_ecp_keypair_free( eck ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1110 | return( ret ); |
| 1111 | } |
| 1112 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1113 | return( 0 ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1114 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1115 | #endif /* MBEDTLS_ECP_C */ |
Jarno Lamsa | b176092 | 2019-04-18 15:58:34 +0300 | [diff] [blame^] | 1116 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1117 | |
| 1118 | /* |
| 1119 | * Parse an unencrypted PKCS#8 encoded private key |
Hanno Becker | b427421 | 2017-09-29 19:18:51 +0100 | [diff] [blame] | 1120 | * |
| 1121 | * Notes: |
| 1122 | * |
| 1123 | * - This function does not own the key buffer. It is the |
| 1124 | * responsibility of the caller to take care of zeroizing |
| 1125 | * and freeing it after use. |
| 1126 | * |
| 1127 | * - The function is responsible for freeing the provided |
| 1128 | * PK context on failure. |
| 1129 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1130 | */ |
| 1131 | static int pk_parse_key_pkcs8_unencrypted_der( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1132 | mbedtls_pk_context *pk, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1133 | const unsigned char* key, |
| 1134 | size_t keylen ) |
| 1135 | { |
| 1136 | int ret, version; |
| 1137 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1138 | mbedtls_asn1_buf params; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1139 | unsigned char *p = (unsigned char *) key; |
| 1140 | unsigned char *end = p + keylen; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1141 | mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE; |
| 1142 | const mbedtls_pk_info_t *pk_info; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1143 | |
| 1144 | /* |
Hanno Becker | 9c6cb38 | 2017-09-05 10:08:01 +0100 | [diff] [blame] | 1145 | * This function parses the PrivateKeyInfo object (PKCS#8 v1.2 = RFC 5208) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1146 | * |
| 1147 | * PrivateKeyInfo ::= SEQUENCE { |
| 1148 | * version Version, |
| 1149 | * privateKeyAlgorithm PrivateKeyAlgorithmIdentifier, |
| 1150 | * privateKey PrivateKey, |
| 1151 | * attributes [0] IMPLICIT Attributes OPTIONAL } |
| 1152 | * |
| 1153 | * Version ::= INTEGER |
| 1154 | * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1155 | * PrivateKey ::= OCTET STRING |
| 1156 | * |
| 1157 | * The PrivateKey OCTET STRING is a SEC1 ECPrivateKey |
| 1158 | */ |
| 1159 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1160 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1161 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1162 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1163 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1164 | } |
| 1165 | |
| 1166 | end = p + len; |
| 1167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1168 | if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 ) |
| 1169 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1170 | |
| 1171 | if( version != 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1172 | return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1173 | |
| 1174 | 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] | 1175 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1176 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1177 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 1178 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1179 | |
| 1180 | if( len < 1 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1181 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + |
| 1182 | MBEDTLS_ERR_ASN1_OUT_OF_DATA ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1183 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1184 | if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL ) |
| 1185 | return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1186 | |
Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 1187 | if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1188 | return( ret ); |
| 1189 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1190 | #if defined(MBEDTLS_RSA_C) |
| 1191 | if( pk_alg == MBEDTLS_PK_RSA ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1192 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1193 | 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] | 1194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1195 | mbedtls_pk_free( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1196 | return( ret ); |
| 1197 | } |
| 1198 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1199 | #endif /* MBEDTLS_RSA_C */ |
| 1200 | #if defined(MBEDTLS_ECP_C) |
| 1201 | if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1202 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1203 | if( ( ret = pk_use_ecparams( ¶ms, &mbedtls_pk_ec( *pk )->grp ) ) != 0 || |
| 1204 | ( 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] | 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 | return( ret ); |
| 1208 | } |
| 1209 | } else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1210 | #endif /* MBEDTLS_ECP_C */ |
| 1211 | return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1212 | |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 1213 | return( 0 ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1214 | } |
| 1215 | |
| 1216 | /* |
| 1217 | * Parse an encrypted PKCS#8 encoded private key |
Hanno Becker | b427421 | 2017-09-29 19:18:51 +0100 | [diff] [blame] | 1218 | * |
| 1219 | * To save space, the decryption happens in-place on the given key buffer. |
| 1220 | * Also, while this function may modify the keybuffer, it doesn't own it, |
| 1221 | * and instead it is the responsibility of the caller to zeroize and properly |
| 1222 | * free it after use. |
| 1223 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1224 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1225 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1226 | static int pk_parse_key_pkcs8_encrypted_der( |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1227 | mbedtls_pk_context *pk, |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1228 | unsigned char *key, size_t keylen, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1229 | const unsigned char *pwd, size_t pwdlen ) |
| 1230 | { |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1231 | int ret, decrypted = 0; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1232 | size_t len; |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1233 | unsigned char *buf; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1234 | unsigned char *p, *end; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1235 | mbedtls_asn1_buf pbe_alg_oid, pbe_params; |
| 1236 | #if defined(MBEDTLS_PKCS12_C) |
| 1237 | mbedtls_cipher_type_t cipher_alg; |
| 1238 | mbedtls_md_type_t md_alg; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1239 | #endif |
| 1240 | |
Hanno Becker | 2aa80a7 | 2017-09-07 15:28:45 +0100 | [diff] [blame] | 1241 | p = key; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1242 | end = p + keylen; |
| 1243 | |
| 1244 | if( pwdlen == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1245 | return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1246 | |
| 1247 | /* |
Hanno Becker | f04111f | 2017-09-29 19:18:42 +0100 | [diff] [blame] | 1248 | * This function parses the EncryptedPrivateKeyInfo object (PKCS#8) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1249 | * |
| 1250 | * EncryptedPrivateKeyInfo ::= SEQUENCE { |
| 1251 | * encryptionAlgorithm EncryptionAlgorithmIdentifier, |
| 1252 | * encryptedData EncryptedData |
| 1253 | * } |
| 1254 | * |
| 1255 | * EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier |
| 1256 | * |
| 1257 | * EncryptedData ::= OCTET STRING |
| 1258 | * |
| 1259 | * The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo |
Hanno Becker | b8d1657 | 2017-09-07 15:29:01 +0100 | [diff] [blame] | 1260 | * |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1261 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1262 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, |
| 1263 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1264 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1265 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1266 | } |
| 1267 | |
| 1268 | end = p + len; |
| 1269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1270 | if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 ) |
| 1271 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1273 | if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ) |
| 1274 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1275 | |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1276 | buf = p; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1277 | |
| 1278 | /* |
Hanno Becker | b8d1657 | 2017-09-07 15:29:01 +0100 | [diff] [blame] | 1279 | * Decrypt EncryptedData with appropriate PBE |
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 | #if defined(MBEDTLS_PKCS12_C) |
| 1282 | 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] | 1283 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1284 | if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1285 | cipher_alg, md_alg, |
| 1286 | pwd, pwdlen, p, len, buf ) ) != 0 ) |
| 1287 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1288 | if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH ) |
| 1289 | return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1290 | |
| 1291 | return( ret ); |
| 1292 | } |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1293 | |
| 1294 | decrypted = 1; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1295 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1296 | 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] | 1297 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1298 | if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params, |
| 1299 | MBEDTLS_PKCS12_PBE_DECRYPT, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1300 | pwd, pwdlen, |
| 1301 | p, len, buf ) ) != 0 ) |
| 1302 | { |
| 1303 | return( ret ); |
| 1304 | } |
| 1305 | |
| 1306 | // 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] | 1307 | // not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1308 | // |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1309 | if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) |
| 1310 | return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH ); |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1311 | |
| 1312 | decrypted = 1; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1313 | } |
| 1314 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1315 | #endif /* MBEDTLS_PKCS12_C */ |
| 1316 | #if defined(MBEDTLS_PKCS5_C) |
| 1317 | if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1318 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1319 | if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1320 | p, len, buf ) ) != 0 ) |
| 1321 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1322 | if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH ) |
| 1323 | return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1324 | |
| 1325 | return( ret ); |
| 1326 | } |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1327 | |
| 1328 | decrypted = 1; |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1329 | } |
| 1330 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1331 | #endif /* MBEDTLS_PKCS5_C */ |
Manuel Pégourié-Gonnard | 1032c1d | 2013-09-18 17:18:34 +0200 | [diff] [blame] | 1332 | { |
| 1333 | ((void) pwd); |
Manuel Pégourié-Gonnard | 1032c1d | 2013-09-18 17:18:34 +0200 | [diff] [blame] | 1334 | } |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1335 | |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1336 | if( decrypted == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1337 | return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE ); |
Paul Bakker | f4cf80b | 2014-04-17 17:19:56 +0200 | [diff] [blame] | 1338 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1339 | return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) ); |
| 1340 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1341 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1342 | |
| 1343 | /* |
| 1344 | * Parse a private key |
| 1345 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1346 | int mbedtls_pk_parse_key( mbedtls_pk_context *pk, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1347 | const unsigned char *key, size_t keylen, |
| 1348 | const unsigned char *pwd, size_t pwdlen ) |
| 1349 | { |
Manuel Pégourié-Gonnard | e83b2c2 | 2019-06-18 11:31:59 +0200 | [diff] [blame] | 1350 | #if defined(MBEDTLS_PKCS12_C) || \ |
| 1351 | defined(MBEDTLS_PKCS5_C) || \ |
| 1352 | defined(MBEDTLS_PEM_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1353 | int ret; |
Manuel Pégourié-Gonnard | e83b2c2 | 2019-06-18 11:31:59 +0200 | [diff] [blame] | 1354 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1355 | const mbedtls_pk_info_t *pk_info; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1356 | #if defined(MBEDTLS_PEM_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1357 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1358 | mbedtls_pem_context pem; |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1359 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1360 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1361 | PK_VALIDATE_RET( pk != NULL ); |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1362 | if( keylen == 0 ) |
| 1363 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
| 1364 | PK_VALIDATE_RET( key != NULL ); |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1365 | |
| 1366 | #if defined(MBEDTLS_PEM_PARSE_C) |
| 1367 | mbedtls_pem_init( &pem ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1368 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1369 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1370 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1371 | if( key[keylen - 1] != '\0' ) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1372 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1373 | else |
| 1374 | ret = mbedtls_pem_read_buffer( &pem, |
| 1375 | "-----BEGIN RSA PRIVATE KEY-----", |
| 1376 | "-----END RSA PRIVATE KEY-----", |
| 1377 | key, pwd, pwdlen, &len ); |
| 1378 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1379 | if( ret == 0 ) |
| 1380 | { |
Hanno Becker | 66a0f83 | 2017-09-08 12:39:21 +0100 | [diff] [blame] | 1381 | pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ); |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1382 | if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1383 | ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1384 | pem.buf, pem.buflen ) ) != 0 ) |
| 1385 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1386 | mbedtls_pk_free( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1387 | } |
| 1388 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1389 | mbedtls_pem_free( &pem ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1390 | return( ret ); |
| 1391 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1392 | else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH ) |
| 1393 | return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH ); |
| 1394 | else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED ) |
| 1395 | return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED ); |
| 1396 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1397 | return( ret ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1398 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1399 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1400 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1401 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1402 | if( key[keylen - 1] != '\0' ) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1403 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1404 | else |
| 1405 | ret = mbedtls_pem_read_buffer( &pem, |
| 1406 | "-----BEGIN EC PRIVATE KEY-----", |
| 1407 | "-----END EC PRIVATE KEY-----", |
| 1408 | key, pwd, pwdlen, &len ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1409 | if( ret == 0 ) |
| 1410 | { |
Hanno Becker | 66a0f83 | 2017-09-08 12:39:21 +0100 | [diff] [blame] | 1411 | pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1412 | |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1413 | if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1414 | ( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1415 | pem.buf, pem.buflen ) ) != 0 ) |
| 1416 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1417 | mbedtls_pk_free( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1418 | } |
| 1419 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1420 | mbedtls_pem_free( &pem ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1421 | return( ret ); |
| 1422 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1423 | else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH ) |
| 1424 | return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH ); |
| 1425 | else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED ) |
| 1426 | return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED ); |
| 1427 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1428 | return( ret ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1429 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1430 | |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1431 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1432 | if( key[keylen - 1] != '\0' ) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1433 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1434 | else |
| 1435 | ret = mbedtls_pem_read_buffer( &pem, |
| 1436 | "-----BEGIN PRIVATE KEY-----", |
| 1437 | "-----END PRIVATE KEY-----", |
| 1438 | key, NULL, 0, &len ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1439 | if( ret == 0 ) |
| 1440 | { |
| 1441 | if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, |
| 1442 | pem.buf, pem.buflen ) ) != 0 ) |
| 1443 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1444 | mbedtls_pk_free( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1445 | } |
| 1446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1447 | mbedtls_pem_free( &pem ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1448 | return( ret ); |
| 1449 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1450 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1451 | return( ret ); |
| 1452 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1453 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1454 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1455 | if( key[keylen - 1] != '\0' ) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1456 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1457 | else |
| 1458 | ret = mbedtls_pem_read_buffer( &pem, |
| 1459 | "-----BEGIN ENCRYPTED PRIVATE KEY-----", |
| 1460 | "-----END ENCRYPTED PRIVATE KEY-----", |
| 1461 | key, NULL, 0, &len ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1462 | if( ret == 0 ) |
| 1463 | { |
| 1464 | if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk, |
| 1465 | pem.buf, pem.buflen, |
| 1466 | pwd, pwdlen ) ) != 0 ) |
| 1467 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1468 | mbedtls_pk_free( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1471 | mbedtls_pem_free( &pem ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1472 | return( ret ); |
| 1473 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1474 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1475 | return( ret ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1476 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1477 | #else |
| 1478 | ((void) pwd); |
| 1479 | ((void) pwdlen); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1480 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1481 | |
| 1482 | /* |
Brian J Murray | 2adecba | 2016-11-06 04:45:15 -0800 | [diff] [blame] | 1483 | * At this point we only know it's not a PEM formatted key. Could be any |
| 1484 | * of the known DER encoded private key formats |
| 1485 | * |
| 1486 | * We try the different DER format parsers to see if one passes without |
| 1487 | * error |
| 1488 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1489 | #if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1490 | { |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1491 | unsigned char *key_copy; |
| 1492 | |
| 1493 | if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL ) |
| 1494 | return( MBEDTLS_ERR_PK_ALLOC_FAILED ); |
| 1495 | |
| 1496 | memcpy( key_copy, key, keylen ); |
| 1497 | |
| 1498 | ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen, |
| 1499 | pwd, pwdlen ); |
| 1500 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 1501 | mbedtls_platform_zeroize( key_copy, keylen ); |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1502 | mbedtls_free( key_copy ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
Hanno Becker | fab3569 | 2017-08-25 13:38:26 +0100 | [diff] [blame] | 1505 | if( ret == 0 ) |
| 1506 | return( 0 ); |
| 1507 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1508 | mbedtls_pk_free( pk ); |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1509 | mbedtls_pk_init( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1510 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1511 | if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1512 | { |
| 1513 | return( ret ); |
| 1514 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1515 | #endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1516 | |
Manuel Pégourié-Gonnard | e83b2c2 | 2019-06-18 11:31:59 +0200 | [diff] [blame] | 1517 | if( pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) == 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1518 | return( 0 ); |
| 1519 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1520 | mbedtls_pk_free( pk ); |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1521 | mbedtls_pk_init( pk ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1522 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1523 | #if defined(MBEDTLS_RSA_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1524 | |
Hanno Becker | 9be1926 | 2017-09-08 12:39:44 +0100 | [diff] [blame] | 1525 | pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ); |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1526 | if( mbedtls_pk_setup( pk, pk_info ) == 0 && |
| 1527 | pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) == 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1528 | { |
| 1529 | return( 0 ); |
| 1530 | } |
| 1531 | |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1532 | mbedtls_pk_free( pk ); |
| 1533 | mbedtls_pk_init( pk ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1534 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1535 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1536 | #if defined(MBEDTLS_ECP_C) |
Hanno Becker | 9be1926 | 2017-09-08 12:39:44 +0100 | [diff] [blame] | 1537 | pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ); |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1538 | if( mbedtls_pk_setup( pk, pk_info ) == 0 && |
| 1539 | pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), |
| 1540 | key, keylen ) == 0 ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1541 | { |
| 1542 | return( 0 ); |
| 1543 | } |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1544 | mbedtls_pk_free( pk ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1545 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1546 | |
Hanno Becker | 780f0a4 | 2018-10-10 11:23:33 +0100 | [diff] [blame] | 1547 | /* If MBEDTLS_RSA_C is defined but MBEDTLS_ECP_C isn't, |
| 1548 | * it is ok to leave the PK context initialized but not |
| 1549 | * freed: It is the caller's responsibility to call pk_init() |
| 1550 | * before calling this function, and to call pk_free() |
| 1551 | * when it fails. If MBEDTLS_ECP_C is defined but MBEDTLS_RSA_C |
| 1552 | * isn't, this leads to mbedtls_pk_free() being called |
| 1553 | * twice, once here and once by the caller, but this is |
| 1554 | * also ok and in line with the mbedtls_pk_free() calls |
| 1555 | * on failed PEM parsing attempts. */ |
| 1556 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1557 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * Parse a public key |
| 1562 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1563 | int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx, |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1564 | const unsigned char *key, size_t keylen ) |
| 1565 | { |
| 1566 | int ret; |
| 1567 | unsigned char *p; |
Ron Eldor | 5472d43 | 2017-10-17 09:49:00 +0300 | [diff] [blame] | 1568 | #if defined(MBEDTLS_RSA_C) |
| 1569 | const mbedtls_pk_info_t *pk_info; |
| 1570 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1571 | #if defined(MBEDTLS_PEM_PARSE_C) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1572 | size_t len; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1573 | mbedtls_pem_context pem; |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1574 | #endif |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1575 | |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1576 | PK_VALIDATE_RET( ctx != NULL ); |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1577 | if( keylen == 0 ) |
| 1578 | return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT ); |
Gilles Peskine | e97dc60 | 2018-12-19 00:51:38 +0100 | [diff] [blame] | 1579 | PK_VALIDATE_RET( key != NULL || keylen == 0 ); |
| 1580 | |
| 1581 | #if defined(MBEDTLS_PEM_PARSE_C) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1582 | mbedtls_pem_init( &pem ); |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1583 | #if defined(MBEDTLS_RSA_C) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1584 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1585 | if( key[keylen - 1] != '\0' ) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1586 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1587 | else |
| 1588 | ret = mbedtls_pem_read_buffer( &pem, |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1589 | "-----BEGIN RSA PUBLIC KEY-----", |
| 1590 | "-----END RSA PUBLIC KEY-----", |
| 1591 | key, NULL, 0, &len ); |
| 1592 | |
| 1593 | if( ret == 0 ) |
| 1594 | { |
Ron Eldor | 84df1ae | 2017-10-16 17:11:52 +0300 | [diff] [blame] | 1595 | p = pem.buf; |
| 1596 | if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL ) |
| 1597 | return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG ); |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1598 | |
Ron Eldor | 84df1ae | 2017-10-16 17:11:52 +0300 | [diff] [blame] | 1599 | if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 ) |
| 1600 | return( ret ); |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1601 | |
Ron Eldor | 84df1ae | 2017-10-16 17:11:52 +0300 | [diff] [blame] | 1602 | if ( ( ret = pk_get_rsapubkey( &p, p + pem.buflen, mbedtls_pk_rsa( *ctx ) ) ) != 0 ) |
| 1603 | mbedtls_pk_free( ctx ); |
Ron Eldor | 3f2da84 | 2017-10-17 15:50:30 +0300 | [diff] [blame] | 1604 | |
Ron Eldor | d0c56de | 2017-10-10 17:03:08 +0300 | [diff] [blame] | 1605 | mbedtls_pem_free( &pem ); |
| 1606 | return( ret ); |
| 1607 | } |
| 1608 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
| 1609 | { |
| 1610 | mbedtls_pem_free( &pem ); |
| 1611 | return( ret ); |
| 1612 | } |
| 1613 | #endif /* MBEDTLS_RSA_C */ |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1614 | |
| 1615 | /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */ |
Gilles Peskine | 159171b | 2018-12-19 17:03:28 +0100 | [diff] [blame] | 1616 | if( key[keylen - 1] != '\0' ) |
Manuel Pégourié-Gonnard | 43b37cb | 2015-05-12 11:20:10 +0200 | [diff] [blame] | 1617 | ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT; |
| 1618 | else |
| 1619 | ret = mbedtls_pem_read_buffer( &pem, |
| 1620 | "-----BEGIN PUBLIC KEY-----", |
| 1621 | "-----END PUBLIC KEY-----", |
| 1622 | key, NULL, 0, &len ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1623 | |
| 1624 | if( ret == 0 ) |
| 1625 | { |
| 1626 | /* |
| 1627 | * Was PEM encoded |
| 1628 | */ |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1629 | p = pem.buf; |
| 1630 | |
| 1631 | ret = mbedtls_pk_parse_subpubkey( &p, p + pem.buflen, ctx ); |
| 1632 | mbedtls_pem_free( &pem ); |
| 1633 | return( ret ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1634 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1635 | else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT ) |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1636 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1637 | mbedtls_pem_free( &pem ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1638 | return( ret ); |
| 1639 | } |
Ron Eldor | 5472d43 | 2017-10-17 09:49:00 +0300 | [diff] [blame] | 1640 | mbedtls_pem_free( &pem ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1641 | #endif /* MBEDTLS_PEM_PARSE_C */ |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1642 | |
| 1643 | #if defined(MBEDTLS_RSA_C) |
| 1644 | if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL ) |
| 1645 | return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG ); |
| 1646 | |
| 1647 | if( ( ret = mbedtls_pk_setup( ctx, pk_info ) ) != 0 ) |
| 1648 | return( ret ); |
| 1649 | |
Ron Eldor | 9566ff7 | 2018-02-07 18:59:41 +0200 | [diff] [blame] | 1650 | p = (unsigned char *)key; |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1651 | ret = pk_get_rsapubkey( &p, p + keylen, mbedtls_pk_rsa( *ctx ) ); |
Ron Eldor | 9566ff7 | 2018-02-07 18:59:41 +0200 | [diff] [blame] | 1652 | if( ret == 0 ) |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1653 | { |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1654 | return( ret ); |
| 1655 | } |
| 1656 | mbedtls_pk_free( ctx ); |
Ron Eldor | 9566ff7 | 2018-02-07 18:59:41 +0200 | [diff] [blame] | 1657 | if( ret != ( MBEDTLS_ERR_PK_INVALID_PUBKEY + MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) ) |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1658 | { |
Ron Eldor | 9566ff7 | 2018-02-07 18:59:41 +0200 | [diff] [blame] | 1659 | return( ret ); |
Ron Eldor | 40b14a8 | 2017-10-16 19:30:00 +0300 | [diff] [blame] | 1660 | } |
| 1661 | #endif /* MBEDTLS_RSA_C */ |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1662 | p = (unsigned char *) key; |
| 1663 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1664 | ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx ); |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1665 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1666 | return( ret ); |
| 1667 | } |
| 1668 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 1669 | #endif /* MBEDTLS_PK_PARSE_C */ |