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