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