2013-09-15 13:01:22 +02:00
|
|
|
/*
|
|
|
|
* Public Key layer for parsing key files and structures
|
|
|
|
*
|
2015-07-27 11:11:48 +02:00
|
|
|
* Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-15 13:01:22 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-09-15 13:01:22 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-15 13:01:22 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-09-15 13:01:22 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* This file is part of mbed TLS (https://tls.mbed.org)
|
2013-09-15 13:01:22 +02:00
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
#include MBEDTLS_CONFIG_FILE
|
2014-04-29 12:39:06 +02:00
|
|
|
#endif
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PK_PARSE_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/rsa.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecp.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECDSA_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/ecdsa.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS5_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pkcs5.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS12_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pkcs12.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PLATFORM_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-09-15 13:01:22 +02:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2015-05-26 16:04:06 +02:00
|
|
|
#define mbedtls_calloc calloc
|
2015-04-08 12:49:31 +02:00
|
|
|
#define mbedtls_free free
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2014-06-13 17:20:13 +02:00
|
|
|
/* Implementation that should never be optimized out by the compiler */
|
2015-04-08 12:49:31 +02:00
|
|
|
static void mbedtls_zeroize( void *v, size_t n ) {
|
2014-06-13 17:20:13 +02:00
|
|
|
volatile unsigned char *p = v; while( n-- ) *p++ = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-15 13:01:22 +02:00
|
|
|
/*
|
|
|
|
* Load all data from a file into a given buffer.
|
2015-05-12 11:20:10 +02:00
|
|
|
*
|
|
|
|
* The file is expected to contain either PEM or DER encoded data.
|
|
|
|
* A terminating null byte is always appended. It is included in the announced
|
|
|
|
* length only if the data looks like it is PEM encoded.
|
2013-09-15 13:01:22 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
FILE *f;
|
|
|
|
long size;
|
|
|
|
|
|
|
|
if( ( f = fopen( path, "rb" ) ) == NULL )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
fseek( f, 0, SEEK_END );
|
|
|
|
if( ( size = ftell( f ) ) == -1 )
|
|
|
|
{
|
|
|
|
fclose( f );
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
fseek( f, 0, SEEK_SET );
|
|
|
|
|
|
|
|
*n = (size_t) size;
|
|
|
|
|
|
|
|
if( *n + 1 == 0 ||
|
2015-05-26 16:04:06 +02:00
|
|
|
( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
fclose( f );
|
2015-05-28 09:33:39 +02:00
|
|
|
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( fread( *buf, 1, *n, f ) != *n )
|
|
|
|
{
|
|
|
|
fclose( f );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( *buf );
|
|
|
|
return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose( f );
|
|
|
|
|
|
|
|
(*buf)[*n] = '\0';
|
|
|
|
|
2015-05-12 11:20:10 +02:00
|
|
|
if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
|
|
|
|
++*n;
|
|
|
|
|
2013-09-15 13:01:22 +02:00
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load and parse a private key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
|
2013-09-15 13:01:22 +02:00
|
|
|
const char *path, const char *pwd )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
if( pwd == NULL )
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
|
2013-09-15 13:01:22 +02:00
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_pk_parse_key( ctx, buf, n,
|
2013-09-15 13:01:22 +02:00
|
|
|
(const unsigned char *) pwd, strlen( pwd ) );
|
|
|
|
|
2015-05-12 11:20:10 +02:00
|
|
|
mbedtls_zeroize( buf, n );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( buf );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Load and parse a public key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_pk_parse_public_key( ctx, buf, n );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-05-12 11:20:10 +02:00
|
|
|
mbedtls_zeroize( buf, n );
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_free( buf );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
|
2013-09-15 13:01:22 +02:00
|
|
|
*
|
|
|
|
* ECParameters ::= CHOICE {
|
|
|
|
* namedCurve OBJECT IDENTIFIER
|
2014-03-14 17:58:42 +01:00
|
|
|
* specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
|
2013-09-15 13:01:22 +02:00
|
|
|
* -- implicitCurve NULL
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf *params )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-03-14 17:58:42 +01:00
|
|
|
/* Tag may be either OID or SEQUENCE */
|
2013-09-15 13:01:22 +02:00
|
|
|
params->tag = **p;
|
2015-04-08 12:49:31 +02:00
|
|
|
if( params->tag != MBEDTLS_ASN1_OID
|
|
|
|
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
|
|
|
|
&& params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
|
2014-03-19 16:39:52 +01:00
|
|
|
#endif
|
|
|
|
)
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
|
2014-03-14 17:58:42 +01:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, ¶ms->len, params->tag ) ) != 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-14 17:58:42 +01:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
params->p = *p;
|
|
|
|
*p += params->len;
|
|
|
|
|
|
|
|
if( *p != end )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
|
2014-03-14 17:58:42 +01:00
|
|
|
/*
|
|
|
|
* Parse a SpecifiedECDomain (SEC 1 C.2) and (mostly) fill the group with it.
|
|
|
|
* WARNING: the resulting group should only be used with
|
|
|
|
* pk_group_id_from_specified(), since its base point may not be set correctly
|
|
|
|
* if it was encoded compressed.
|
|
|
|
*
|
|
|
|
* SpecifiedECDomain ::= SEQUENCE {
|
|
|
|
* version SpecifiedECDomainVersion(ecdpVer1 | ecdpVer2 | ecdpVer3, ...),
|
|
|
|
* fieldID FieldID {{FieldTypes}},
|
|
|
|
* curve Curve,
|
|
|
|
* base ECPoint,
|
|
|
|
* order INTEGER,
|
|
|
|
* cofactor INTEGER OPTIONAL,
|
|
|
|
* hash HashAlgorithm OPTIONAL,
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* We only support prime-field as field type, and ignore hash and cofactor.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p = params->p;
|
|
|
|
const unsigned char * const end = params->p + params->len;
|
|
|
|
const unsigned char *end_field, *end_curve;
|
|
|
|
size_t len;
|
|
|
|
int ver;
|
|
|
|
|
|
|
|
/* SpecifiedECDomainVersion ::= INTEGER { 1, 2, 3 } */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
if( ver < 1 || ver > 3 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
|
|
|
|
* fieldType FIELD-ID.&id({IOSet}),
|
|
|
|
* parameters FIELD-ID.&Type({IOSet}{@fieldType})
|
|
|
|
* }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
end_field = p + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FIELD-ID ::= TYPE-IDENTIFIER
|
|
|
|
* FieldTypes FIELD-ID ::= {
|
|
|
|
* { Prime-p IDENTIFIED BY prime-field } |
|
|
|
|
* { Characteristic-two IDENTIFIED BY characteristic-two-field }
|
|
|
|
* }
|
|
|
|
* prime-field OBJECT IDENTIFIER ::= { id-fieldType 1 }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
|
|
|
|
memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
|
2014-03-14 17:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p += len;
|
|
|
|
|
|
|
|
/* Prime-p ::= INTEGER -- Field of size p. */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
2015-06-18 16:47:17 +02:00
|
|
|
grp->pbits = mbedtls_mpi_bitlen( &grp->P );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
if( p != end_field )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curve ::= SEQUENCE {
|
|
|
|
* a FieldElement,
|
|
|
|
* b FieldElement,
|
|
|
|
* seed BIT STRING OPTIONAL
|
|
|
|
* -- Shall be present if used in SpecifiedECDomain
|
|
|
|
* -- with version equal to ecdpVer2 or ecdpVer3
|
|
|
|
* }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
end_curve = p + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* FieldElement ::= OCTET STRING
|
|
|
|
* containing an integer in the case of a prime field
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
|
|
|
|
( ret = mbedtls_mpi_read_binary( &grp->A, p, len ) ) != 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-14 17:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p += len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 ||
|
|
|
|
( ret = mbedtls_mpi_read_binary( &grp->B, p, len ) ) != 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-14 17:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
p += len;
|
|
|
|
|
|
|
|
/* Ignore seed BIT STRING OPTIONAL */
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
|
2014-03-14 17:58:42 +01:00
|
|
|
p += len;
|
|
|
|
|
|
|
|
if( p != end_curve )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* ECPoint ::= OCTET STRING
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
|
2014-03-19 16:18:38 +01:00
|
|
|
( const unsigned char *) p, len ) ) != 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If we can't read the point because it's compressed, cheat by
|
|
|
|
* reading only the X coordinate and the parity bit of Y.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
|
2014-03-19 16:18:38 +01:00
|
|
|
( p[0] != 0x02 && p[0] != 0x03 ) ||
|
2015-04-08 12:49:31 +02:00
|
|
|
len != mbedtls_mpi_size( &grp->P ) + 1 ||
|
|
|
|
mbedtls_mpi_read_binary( &grp->G.X, p + 1, len - 1 ) != 0 ||
|
|
|
|
mbedtls_mpi_lset( &grp->G.Y, p[0] - 2 ) != 0 ||
|
|
|
|
mbedtls_mpi_lset( &grp->G.Z, 1 ) != 0 )
|
2014-03-19 16:18:38 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
2014-03-19 16:18:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-14 17:58:42 +01:00
|
|
|
p += len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* order INTEGER
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
2015-06-18 16:47:17 +02:00
|
|
|
grp->nbits = mbedtls_mpi_bitlen( &grp->N );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allow optional elements by purposefully not enforcing p == end here.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the group id associated with an (almost filled) group as generated by
|
|
|
|
* pk_group_from_specified(), or return an error if unknown.
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
2014-03-27 14:59:42 +01:00
|
|
|
int ret = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group ref;
|
|
|
|
const mbedtls_ecp_group_id *id;
|
2014-03-14 17:58:42 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &ref );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
|
|
|
/* Load the group associated to that id */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &ref );
|
2015-05-11 18:46:47 +02:00
|
|
|
MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
/* Compare to the group we were given, starting with easy tests */
|
|
|
|
if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_cmp_mpi( &grp->P, &ref.P ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &grp->A, &ref.A ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &grp->B, &ref.B ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &grp->N, &ref.N ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &grp->G.X, &ref.G.X ) == 0 &&
|
|
|
|
mbedtls_mpi_cmp_mpi( &grp->G.Z, &ref.G.Z ) == 0 &&
|
2014-03-14 17:58:42 +01:00
|
|
|
/* For Y we may only know the parity bit, so compare only that */
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &ref );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
*grp_id = *id;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
|
|
|
|
ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
|
|
|
|
mbedtls_ecp_group_id *grp_id )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group grp;
|
2014-03-14 17:58:42 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_init( &grp );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = pk_group_id_from_group( &grp, grp_id );
|
|
|
|
|
|
|
|
cleanup:
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_free( &grp );
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
|
2014-03-14 17:58:42 +01:00
|
|
|
|
2013-09-15 13:01:22 +02:00
|
|
|
/*
|
|
|
|
* Use EC parameters to initialise an EC group
|
2014-03-14 17:58:42 +01:00
|
|
|
*
|
|
|
|
* ECParameters ::= CHOICE {
|
|
|
|
* namedCurve OBJECT IDENTIFIER
|
|
|
|
* specifiedCurve SpecifiedECDomain -- = SEQUENCE { ... }
|
|
|
|
* -- implicitCurve NULL
|
2013-09-15 13:01:22 +02:00
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_group_id grp_id;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( params->tag == MBEDTLS_ASN1_OID )
|
2014-03-14 17:58:42 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
|
2014-03-14 17:58:42 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
|
2014-03-14 17:58:42 +01:00
|
|
|
if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
|
|
|
|
return( ret );
|
2014-03-19 16:39:52 +01:00
|
|
|
#else
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
2014-03-19 16:39:52 +01:00
|
|
|
#endif
|
2014-03-14 17:58:42 +01:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* grp may already be initilialized; if so, make sure IDs match
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-05-11 18:46:47 +02:00
|
|
|
if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* EC public key is an EC point
|
2014-03-19 16:18:38 +01:00
|
|
|
*
|
|
|
|
* The caller is responsible for clearing the structure upon failure if
|
|
|
|
* desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
|
2015-04-08 12:49:31 +02:00
|
|
|
* return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
|
2013-09-15 13:01:22 +02:00
|
|
|
*/
|
|
|
|
static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair *key )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
|
2014-03-19 16:18:38 +01:00
|
|
|
(const unsigned char *) *p, end - *p ) ) == 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-04-08 12:49:31 +02:00
|
|
|
* We know mbedtls_ecp_point_read_binary consumed all bytes or failed
|
2013-09-15 13:01:22 +02:00
|
|
|
*/
|
|
|
|
*p = (unsigned char *) end;
|
|
|
|
|
2014-03-19 16:18:38 +01:00
|
|
|
return( ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
/*
|
|
|
|
* RSAPublicKey ::= SEQUENCE {
|
|
|
|
* modulus INTEGER, -- n
|
|
|
|
* publicExponent INTEGER -- e
|
|
|
|
* }
|
|
|
|
*/
|
|
|
|
static int pk_get_rsapubkey( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_context *rsa )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( *p + len != end )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_mpi( p, end, &rsa->N ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( p, end, &rsa->E ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( *p != end )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
rsa->len = mbedtls_mpi_size( &rsa->N );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/* Get a PK algorithm identifier
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*/
|
|
|
|
static int pk_get_pk_alg( unsigned char **p,
|
|
|
|
const unsigned char *end,
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf alg_oid;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
memset( params, 0, sizeof(mbedtls_asn1_buf) );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* No parameters with RSA (only for EC)
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( *pk_alg == MBEDTLS_PK_RSA &&
|
|
|
|
( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
|
2013-09-15 13:01:22 +02:00
|
|
|
params->len != 0 ) )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_INVALID_ALG );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SubjectPublicKeyInfo ::= SEQUENCE {
|
|
|
|
* algorithm AlgorithmIdentifier,
|
|
|
|
* subjectPublicKey BIT STRING }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_pk_context *pk )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf alg_params;
|
|
|
|
mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
|
|
|
|
const mbedtls_pk_info_t *pk_info;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = *p + len;
|
|
|
|
|
|
|
|
if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( *p + len != end )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
if( pk_alg == MBEDTLS_PK_RSA )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
|
2013-09-15 13:01:22 +02:00
|
|
|
} else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
|
2013-09-15 13:01:22 +02:00
|
|
|
if( ret == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
|
2013-09-15 13:01:22 +02:00
|
|
|
} else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( ret == 0 && *p != end )
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( ret != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
/*
|
|
|
|
* Parse a PKCS#1 encoded private RSA key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
|
2013-09-15 13:01:22 +02:00
|
|
|
const unsigned char *key,
|
|
|
|
size_t keylen )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *p, *end;
|
|
|
|
|
|
|
|
p = (unsigned char *) key;
|
|
|
|
end = p + keylen;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function parses the RSAPrivateKey (PKCS#1)
|
|
|
|
*
|
|
|
|
* RSAPrivateKey ::= SEQUENCE {
|
|
|
|
* version Version,
|
|
|
|
* modulus INTEGER, -- n
|
|
|
|
* publicExponent INTEGER, -- e
|
|
|
|
* privateExponent INTEGER, -- d
|
|
|
|
* prime1 INTEGER, -- p
|
|
|
|
* prime2 INTEGER, -- q
|
|
|
|
* exponent1 INTEGER, -- d mod (p-1)
|
|
|
|
* exponent2 INTEGER, -- d mod (q-1)
|
|
|
|
* coefficient INTEGER, -- (inverse of q) mod p
|
|
|
|
* otherPrimeInfos OtherPrimeInfos OPTIONAL
|
|
|
|
* }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if( rsa->ver != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->N ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->E ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->D ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->P ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->Q ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DP ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->DQ ) ) != 0 ||
|
|
|
|
( ret = mbedtls_asn1_get_mpi( &p, end, &rsa->QP ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
rsa->len = mbedtls_mpi_size( &rsa->N );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( p != end )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_free( rsa );
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_rsa_check_privkey( rsa ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_rsa_free( rsa );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
/*
|
|
|
|
* Parse a SEC1 encoded private EC key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
|
2013-09-15 13:01:22 +02:00
|
|
|
const unsigned char *key,
|
|
|
|
size_t keylen )
|
|
|
|
{
|
|
|
|
int ret;
|
2014-03-14 17:58:42 +01:00
|
|
|
int version, pubkey_done;
|
2013-09-15 13:01:22 +02:00
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf params;
|
2013-09-15 13:01:22 +02:00
|
|
|
unsigned char *p = (unsigned char *) key;
|
|
|
|
unsigned char *end = p + keylen;
|
|
|
|
unsigned char *end2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RFC 5915, or SEC1 Appendix C.4
|
|
|
|
*
|
|
|
|
* ECPrivateKey ::= SEQUENCE {
|
|
|
|
* version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
|
|
|
|
* privateKey OCTET STRING,
|
|
|
|
* parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
|
|
|
|
* publicKey [1] BIT STRING OPTIONAL
|
|
|
|
* }
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( version != 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair_free( eck );
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p += len;
|
|
|
|
|
2015-04-14 11:18:04 +02:00
|
|
|
pubkey_done = 0;
|
|
|
|
if( p != end )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-14 11:18:04 +02:00
|
|
|
/*
|
|
|
|
* Is 'parameters' present?
|
|
|
|
*/
|
2015-04-15 10:50:34 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-14 11:18:04 +02:00
|
|
|
if( ( ret = pk_get_ecparams( &p, p + len, ¶ms) ) != 0 ||
|
|
|
|
( ret = pk_use_ecparams( ¶ms, &eck->grp ) ) != 0 )
|
|
|
|
{
|
2015-04-15 10:50:34 +02:00
|
|
|
mbedtls_ecp_keypair_free( eck );
|
2015-04-14 11:18:04 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
2015-04-15 10:50:34 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2015-04-14 11:18:04 +02:00
|
|
|
{
|
2015-04-15 10:50:34 +02:00
|
|
|
mbedtls_ecp_keypair_free( eck );
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2015-04-14 11:18:04 +02:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-14 11:18:04 +02:00
|
|
|
/*
|
|
|
|
* Is 'publickey' present? If not, or if we can't read it (eg because it
|
|
|
|
* is compressed), create it from the private key.
|
|
|
|
*/
|
2015-04-15 10:50:34 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
|
2014-03-19 16:18:38 +01:00
|
|
|
{
|
2015-04-14 11:18:04 +02:00
|
|
|
end2 = p + len;
|
|
|
|
|
2015-04-15 10:50:34 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2015-04-14 11:18:04 +02:00
|
|
|
|
|
|
|
if( p + len != end2 )
|
2015-04-15 10:50:34 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
|
2015-04-14 11:18:04 +02:00
|
|
|
|
|
|
|
if( ( ret = pk_get_ecpubkey( &p, end2, eck ) ) == 0 )
|
|
|
|
pubkey_done = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The only acceptable failure mode of pk_get_ecpubkey() above
|
|
|
|
* is if the point format is not recognized.
|
|
|
|
*/
|
2015-04-15 10:50:34 +02:00
|
|
|
if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
2015-04-14 11:18:04 +02:00
|
|
|
}
|
|
|
|
}
|
2015-04-15 10:50:34 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
|
2015-04-14 11:18:04 +02:00
|
|
|
{
|
2015-04-15 10:50:34 +02:00
|
|
|
mbedtls_ecp_keypair_free( eck );
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2014-03-19 16:18:38 +01:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
2014-03-14 17:58:42 +01:00
|
|
|
|
|
|
|
if( ! pubkey_done &&
|
2015-04-08 12:49:31 +02:00
|
|
|
( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
|
2014-03-14 17:58:42 +01:00
|
|
|
NULL, NULL ) ) != 0 )
|
2013-09-18 16:13:02 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair_free( eck );
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-18 16:13:02 +02:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_ecp_keypair_free( eck );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse an unencrypted PKCS#8 encoded private key
|
|
|
|
*/
|
|
|
|
static int pk_parse_key_pkcs8_unencrypted_der(
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context *pk,
|
2013-09-15 13:01:22 +02:00
|
|
|
const unsigned char* key,
|
|
|
|
size_t keylen )
|
|
|
|
{
|
|
|
|
int ret, version;
|
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf params;
|
2013-09-15 13:01:22 +02:00
|
|
|
unsigned char *p = (unsigned char *) key;
|
|
|
|
unsigned char *end = p + keylen;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
|
|
|
|
const mbedtls_pk_info_t *pk_info;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function parses the PrivatKeyInfo object (PKCS#8 v1.2 = RFC 5208)
|
|
|
|
*
|
|
|
|
* PrivateKeyInfo ::= SEQUENCE {
|
|
|
|
* version Version,
|
|
|
|
* privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
|
|
|
|
* privateKey PrivateKey,
|
|
|
|
* attributes [0] IMPLICIT Attributes OPTIONAL }
|
|
|
|
*
|
|
|
|
* Version ::= INTEGER
|
|
|
|
* PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
* PrivateKey ::= OCTET STRING
|
|
|
|
*
|
|
|
|
* The PrivateKey OCTET STRING is a SEC1 ECPrivateKey
|
|
|
|
*/
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( version != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, ¶ms ) ) != 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( len < 1 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-05-14 19:41:36 +02:00
|
|
|
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
if( pk_alg == MBEDTLS_PK_RSA )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), p, len ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
} else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
if( pk_alg == MBEDTLS_PK_ECKEY || pk_alg == MBEDTLS_PK_ECKEY_DH )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = pk_use_ecparams( ¶ms, &mbedtls_pk_ec( *pk )->grp ) ) != 0 ||
|
|
|
|
( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), p, len ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
} else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2014-06-17 14:06:49 +02:00
|
|
|
return( 0 );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse an encrypted PKCS#8 encoded private key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
static int pk_parse_key_pkcs8_encrypted_der(
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context *pk,
|
2017-08-25 14:38:26 +02:00
|
|
|
unsigned char *key, size_t keylen,
|
2013-09-15 13:01:22 +02:00
|
|
|
const unsigned char *pwd, size_t pwdlen )
|
|
|
|
{
|
2014-04-17 17:19:56 +02:00
|
|
|
int ret, decrypted = 0;
|
2013-09-15 13:01:22 +02:00
|
|
|
size_t len;
|
2017-08-25 14:38:26 +02:00
|
|
|
unsigned char *buf;
|
2013-09-15 13:01:22 +02:00
|
|
|
unsigned char *p, *end;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_asn1_buf pbe_alg_oid, pbe_params;
|
|
|
|
#if defined(MBEDTLS_PKCS12_C)
|
|
|
|
mbedtls_cipher_type_t cipher_alg;
|
|
|
|
mbedtls_md_type_t md_alg;
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
p = (unsigned char *) key;
|
|
|
|
end = p + keylen;
|
|
|
|
|
|
|
|
if( pwdlen == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function parses the EncryptedPrivatKeyInfo object (PKCS#8)
|
|
|
|
*
|
|
|
|
* EncryptedPrivateKeyInfo ::= SEQUENCE {
|
|
|
|
* encryptionAlgorithm EncryptionAlgorithmIdentifier,
|
|
|
|
* encryptedData EncryptedData
|
|
|
|
* }
|
|
|
|
*
|
|
|
|
* EncryptionAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* EncryptedData ::= OCTET STRING
|
|
|
|
*
|
|
|
|
* The EncryptedData OCTET STRING is a PKCS#8 PrivateKeyInfo
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_alg( &p, end, &pbe_alg_oid, &pbe_params ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
|
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2017-08-25 14:38:26 +02:00
|
|
|
buf = p;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decrypt EncryptedData with appropriate PDE
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS12_C)
|
|
|
|
if( mbedtls_oid_get_pkcs12_pbe_alg( &pbe_alg_oid, &md_alg, &cipher_alg ) == 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_pkcs12_pbe( &pbe_params, MBEDTLS_PKCS12_PBE_DECRYPT,
|
2013-09-15 13:01:22 +02:00
|
|
|
cipher_alg, md_alg,
|
|
|
|
pwd, pwdlen, p, len, buf ) ) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret == MBEDTLS_ERR_PKCS12_PASSWORD_MISMATCH )
|
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2014-04-17 17:19:56 +02:00
|
|
|
|
|
|
|
decrypted = 1;
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS12_PBE_SHA1_RC4_128, &pbe_alg_oid ) == 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_pkcs12_pbe_sha1_rc4_128( &pbe_params,
|
|
|
|
MBEDTLS_PKCS12_PBE_DECRYPT,
|
2013-09-15 13:01:22 +02:00
|
|
|
pwd, pwdlen,
|
|
|
|
p, len, buf ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Best guess for password mismatch when using RC4. If first tag is
|
2015-04-08 12:49:31 +02:00
|
|
|
// not MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE
|
2013-09-15 13:01:22 +02:00
|
|
|
//
|
2015-04-08 12:49:31 +02:00
|
|
|
if( *buf != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) )
|
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
|
2014-04-17 17:19:56 +02:00
|
|
|
|
|
|
|
decrypted = 1;
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PKCS12_C */
|
|
|
|
#if defined(MBEDTLS_PKCS5_C)
|
|
|
|
if( MBEDTLS_OID_CMP( MBEDTLS_OID_PKCS5_PBES2, &pbe_alg_oid ) == 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( ret = mbedtls_pkcs5_pbes2( &pbe_params, MBEDTLS_PKCS5_DECRYPT, pwd, pwdlen,
|
2013-09-15 13:01:22 +02:00
|
|
|
p, len, buf ) ) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret == MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH )
|
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
2014-04-17 17:19:56 +02:00
|
|
|
|
|
|
|
decrypted = 1;
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PKCS5_C */
|
2013-09-18 17:18:34 +02:00
|
|
|
{
|
|
|
|
((void) pwd);
|
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2014-04-17 17:19:56 +02:00
|
|
|
if( decrypted == 0 )
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
|
2014-04-17 17:19:56 +02:00
|
|
|
|
2013-09-15 13:01:22 +02:00
|
|
|
return( pk_parse_key_pkcs8_unencrypted_der( pk, buf, len ) );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a private key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pk_parse_key( mbedtls_pk_context *pk,
|
2013-09-15 13:01:22 +02:00
|
|
|
const unsigned char *key, size_t keylen,
|
|
|
|
const unsigned char *pwd, size_t pwdlen )
|
|
|
|
{
|
|
|
|
int ret;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_pk_info_t *pk_info;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_context pem;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_init( &pem );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
2015-05-12 11:20:10 +02:00
|
|
|
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
|
2015-05-12 12:43:54 +02:00
|
|
|
if( keylen == 0 || key[keylen - 1] != '\0' )
|
2015-05-12 11:20:10 +02:00
|
|
|
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
else
|
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN RSA PRIVATE KEY-----",
|
|
|
|
"-----END RSA PRIVATE KEY-----",
|
|
|
|
key, pwd, pwdlen, &len );
|
|
|
|
|
2013-09-15 13:01:22 +02:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
|
2017-08-25 14:54:04 +02:00
|
|
|
{
|
|
|
|
mbedtls_pem_free( &pem );
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2017-08-25 14:54:04 +02:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2017-08-25 14:38:26 +02:00
|
|
|
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
|
2015-04-08 12:49:31 +02:00
|
|
|
( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ),
|
2013-09-15 13:01:22 +02:00
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
|
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
|
|
|
|
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
|
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
|
|
|
|
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_RSA_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
2015-05-12 11:20:10 +02:00
|
|
|
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
|
2015-05-12 12:43:54 +02:00
|
|
|
if( keylen == 0 || key[keylen - 1] != '\0' )
|
2015-05-12 11:20:10 +02:00
|
|
|
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
else
|
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN EC PRIVATE KEY-----",
|
|
|
|
"-----END EC PRIVATE KEY-----",
|
|
|
|
key, pwd, pwdlen, &len );
|
2013-09-15 13:01:22 +02:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == NULL )
|
2017-08-25 14:54:04 +02:00
|
|
|
{
|
|
|
|
mbedtls_pem_free( &pem );
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2017-08-25 14:54:04 +02:00
|
|
|
}
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2017-08-25 14:38:26 +02:00
|
|
|
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
|
2015-04-08 12:49:31 +02:00
|
|
|
( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ),
|
2013-09-15 13:01:22 +02:00
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_MISMATCH )
|
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_MISMATCH );
|
|
|
|
else if( ret == MBEDTLS_ERR_PEM_PASSWORD_REQUIRED )
|
|
|
|
return( MBEDTLS_ERR_PK_PASSWORD_REQUIRED );
|
|
|
|
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-05-12 11:20:10 +02:00
|
|
|
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
|
2015-05-12 12:43:54 +02:00
|
|
|
if( keylen == 0 || key[keylen - 1] != '\0' )
|
2015-05-12 11:20:10 +02:00
|
|
|
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
else
|
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN PRIVATE KEY-----",
|
|
|
|
"-----END PRIVATE KEY-----",
|
|
|
|
key, NULL, 0, &len );
|
2013-09-15 13:01:22 +02:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk,
|
|
|
|
pem.buf, pem.buflen ) ) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
|
2015-05-12 11:20:10 +02:00
|
|
|
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
|
2015-05-12 12:43:54 +02:00
|
|
|
if( keylen == 0 || key[keylen - 1] != '\0' )
|
2015-05-12 11:20:10 +02:00
|
|
|
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
else
|
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN ENCRYPTED PRIVATE KEY-----",
|
|
|
|
"-----END ENCRYPTED PRIVATE KEY-----",
|
|
|
|
key, NULL, 0, &len );
|
2013-09-15 13:01:22 +02:00
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
if( ( ret = pk_parse_key_pkcs8_encrypted_der( pk,
|
|
|
|
pem.buf, pem.buflen,
|
|
|
|
pwd, pwdlen ) ) != 0 )
|
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
#else
|
2015-10-02 14:32:17 +02:00
|
|
|
((void) ret);
|
2013-09-15 13:01:22 +02:00
|
|
|
((void) pwd);
|
|
|
|
((void) pwdlen);
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
/*
|
2016-11-06 13:45:15 +01:00
|
|
|
* At this point we only know it's not a PEM formatted key. Could be any
|
|
|
|
* of the known DER encoded private key formats
|
|
|
|
*
|
|
|
|
* We try the different DER format parsers to see if one passes without
|
|
|
|
* error
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PKCS12_C) || defined(MBEDTLS_PKCS5_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2017-08-25 14:38:26 +02:00
|
|
|
unsigned char *key_copy;
|
|
|
|
|
|
|
|
if( ( key_copy = mbedtls_calloc( 1, keylen ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
|
|
|
|
|
|
|
|
memcpy( key_copy, key, keylen );
|
|
|
|
|
|
|
|
ret = pk_parse_key_pkcs8_encrypted_der( pk, key_copy, keylen,
|
|
|
|
pwd, pwdlen );
|
|
|
|
|
|
|
|
mbedtls_zeroize( key_copy, keylen );
|
|
|
|
mbedtls_free( key_copy );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-25 14:38:26 +02:00
|
|
|
if( ret == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
if( ret == MBEDTLS_ERR_PK_PASSWORD_MISMATCH )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PKCS12_C || MBEDTLS_PKCS5_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( ( ret = pk_parse_key_pkcs8_unencrypted_der( pk, key, keylen ) ) == 0 )
|
|
|
|
return( 0 );
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_RSA_C)
|
|
|
|
if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_RSA ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2017-08-25 14:38:26 +02:00
|
|
|
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
|
2015-04-08 12:49:31 +02:00
|
|
|
( ret = pk_parse_key_pkcs1_der( mbedtls_pk_rsa( *pk ), key, keylen ) ) == 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
|
|
|
#endif /* MBEDTLS_RSA_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_ECP_C)
|
|
|
|
if( ( pk_info = mbedtls_pk_info_from_type( MBEDTLS_PK_ECKEY ) ) == NULL )
|
|
|
|
return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2017-08-25 14:38:26 +02:00
|
|
|
if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 ||
|
2015-04-08 12:49:31 +02:00
|
|
|
( ret = pk_parse_key_sec1_der( mbedtls_pk_ec( *pk ), key, keylen ) ) == 0 )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_free( pk );
|
|
|
|
#endif /* MBEDTLS_ECP_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
|
2013-09-15 13:01:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a public key
|
|
|
|
*/
|
2015-04-08 12:49:31 +02:00
|
|
|
int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
|
2013-09-15 13:01:22 +02:00
|
|
|
const unsigned char *key, size_t keylen )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *p;
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2013-09-15 13:01:22 +02:00
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_context pem;
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_init( &pem );
|
2015-05-12 11:20:10 +02:00
|
|
|
|
|
|
|
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
|
2015-05-12 12:43:54 +02:00
|
|
|
if( keylen == 0 || key[keylen - 1] != '\0' )
|
2015-05-12 11:20:10 +02:00
|
|
|
ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
|
|
|
|
else
|
|
|
|
ret = mbedtls_pem_read_buffer( &pem,
|
|
|
|
"-----BEGIN PUBLIC KEY-----",
|
|
|
|
"-----END PUBLIC KEY-----",
|
|
|
|
key, NULL, 0, &len );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
|
|
|
if( ret == 0 )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Was PEM encoded
|
|
|
|
*/
|
|
|
|
key = pem.buf;
|
|
|
|
keylen = pem.buflen;
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
|
2013-09-15 13:01:22 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-15 13:01:22 +02:00
|
|
|
return( ret );
|
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C */
|
2013-09-15 13:01:22 +02:00
|
|
|
p = (unsigned char *) key;
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
ret = mbedtls_pk_parse_subpubkey( &p, p + keylen, ctx );
|
2013-09-15 13:01:22 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
|
|
|
mbedtls_pem_free( &pem );
|
2013-09-15 13:01:22 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PK_PARSE_C */
|