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