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