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