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