pkparse.c 38.3 KB
Newer Older
1 2 3
/*
 *  Public Key layer for parsing key files and structures
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
4
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
5
 *
6
 *  This file is part of mbed TLS (https://tls.mbed.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
23
#if !defined(MBEDTLS_CONFIG_FILE)
24
#include "mbedtls/config.h"
25
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
26
#include MBEDTLS_CONFIG_FILE
27
#endif
28

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
29
#if defined(MBEDTLS_PK_PARSE_C)
30

31 32 33
#include "mbedtls/pk.h"
#include "mbedtls/asn1.h"
#include "mbedtls/oid.h"
34

35 36
#include <string.h>

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
37
#if defined(MBEDTLS_RSA_C)
38
#include "mbedtls/rsa.h"
39
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
40
#if defined(MBEDTLS_ECP_C)
41
#include "mbedtls/ecp.h"
42
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
43
#if defined(MBEDTLS_ECDSA_C)
44
#include "mbedtls/ecdsa.h"
45
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
46
#if defined(MBEDTLS_PEM_PARSE_C)
47
#include "mbedtls/pem.h"
48
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
49
#if defined(MBEDTLS_PKCS5_C)
50
#include "mbedtls/pkcs5.h"
51
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
52
#if defined(MBEDTLS_PKCS12_C)
53
#include "mbedtls/pkcs12.h"
54 55
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
56
#if defined(MBEDTLS_PLATFORM_C)
57
#include "mbedtls/platform.h"
58 59
#else
#include <stdlib.h>
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
60 61
#define mbedtls_malloc     malloc
#define mbedtls_free       free
62 63
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
64
#if defined(MBEDTLS_FS_IO)
65
/* Implementation that should never be optimized out by the compiler */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
66
static void mbedtls_zeroize( void *v, size_t n ) {
67 68 69
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
}

70 71
/*
 * Load all data from a file into a given buffer.
72 73 74 75
 *
 * 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.
76
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
77
int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n )
78 79 80 81 82
{
    FILE *f;
    long size;

    if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
83
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
84 85 86 87 88

    fseek( f, 0, SEEK_END );
    if( ( size = ftell( f ) ) == -1 )
    {
        fclose( f );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
89
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
90 91 92 93 94 95
    }
    fseek( f, 0, SEEK_SET );

    *n = (size_t) size;

    if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
96
        ( *buf = mbedtls_malloc( *n + 1 ) ) == NULL )
97 98
    {
        fclose( f );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
99
        return( MBEDTLS_ERR_PK_MALLOC_FAILED );
100 101 102 103 104
    }

    if( fread( *buf, 1, *n, f ) != *n )
    {
        fclose( f );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
105 106
        mbedtls_free( *buf );
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
107 108 109 110 111 112
    }

    fclose( f );

    (*buf)[*n] = '\0';

113 114 115
    if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
        ++*n;

116 117 118 119 120 121
    return( 0 );
}

/*
 * Load and parse a private key
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
122
int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
123 124 125 126 127 128
                      const char *path, const char *pwd )
{
    int ret;
    size_t n;
    unsigned char *buf;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
129
    if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
130 131 132
        return( ret );

    if( pwd == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
133
        ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
134
    else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
135
        ret = mbedtls_pk_parse_key( ctx, buf, n,
136 137
                (const unsigned char *) pwd, strlen( pwd ) );

138
    mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
139
    mbedtls_free( buf );
140 141 142 143 144 145 146

    return( ret );
}

/*
 * Load and parse a public key
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
147
int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
148 149 150 151 152
{
    int ret;
    size_t n;
    unsigned char *buf;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
153
    if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
154 155
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
156
    ret = mbedtls_pk_parse_public_key( ctx, buf, n );
157

158
    mbedtls_zeroize( buf, n );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
159
    mbedtls_free( buf );
160 161 162

    return( ret );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
163
#endif /* MBEDTLS_FS_IO */
164

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
165 166
#if defined(MBEDTLS_ECP_C)
/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
167 168 169
 *
 * ECParameters ::= CHOICE {
 *   namedCurve         OBJECT IDENTIFIER
170
 *   specifiedCurve     SpecifiedECDomain -- = SEQUENCE { ... }
171 172 173 174
 *   -- implicitCurve   NULL
 * }
 */
static int pk_get_ecparams( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
175
                            mbedtls_asn1_buf *params )
176 177 178
{
    int ret;

179
    /* Tag may be either OID or SEQUENCE */
180
    params->tag = **p;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
181 182 183
    if( params->tag != MBEDTLS_ASN1_OID
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
            && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
184 185
#endif
            )
186
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
187 188
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
189
    }
190

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
191
    if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
192
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
193
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
194
    }
195 196 197 198 199

    params->p = *p;
    *p += params->len;

    if( *p != end )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
200 201
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
202 203 204 205

    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
206
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
/*
 * 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.
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
226
static int pk_group_from_specified( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
227 228 229 230 231 232 233 234 235
{
    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 } */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
236 237
    if( ( ret = mbedtls_asn1_get_int( &p, end, &ver ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
238 239

    if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
240
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
241 242 243 244 245 246 247

    /*
     * FieldID { FIELD-ID:IOSet } ::= SEQUENCE { -- Finite field
     *       fieldType FIELD-ID.&id({IOSet}),
     *       parameters FIELD-ID.&Type({IOSet}{@fieldType})
     * }
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
248 249
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
250 251 252 253 254 255 256 257 258 259 260 261
        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 }
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
262
    if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
263 264
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
265 266
    if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
        memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
267
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
268
        return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
269 270 271 272 273
    }

    p += len;

    /* Prime-p ::= INTEGER -- Field of size p. */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
274 275
    if( ( ret = mbedtls_asn1_get_mpi( &p, end_field, &grp->P ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
276

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
277
    grp->pbits = mbedtls_mpi_msb( &grp->P );
278 279

    if( p != end_field )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
280 281
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
282 283 284 285 286 287 288 289 290 291

    /*
     * Curve ::= SEQUENCE {
     *       a FieldElement,
     *       b FieldElement,
     *       seed BIT STRING OPTIONAL
     *       -- Shall be present if used in SpecifiedECDomain
     *       -- with version equal to ecdpVer2 or ecdpVer3
     * }
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
292 293
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
294 295 296 297 298 299 300 301
        return( ret );

    end_curve = p + len;

    /*
     * FieldElement ::= OCTET STRING
     * containing an integer in the case of a prime field
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
302 303
    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 )
304
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
305
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
306 307 308 309
    }

    p += len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
310 311
    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 )
312
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
313
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
314 315 316 317 318
    }

    p += len;

    /* Ignore seed BIT STRING OPTIONAL */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
319
    if( ( ret = mbedtls_asn1_get_tag( &p, end_curve, &len, MBEDTLS_ASN1_BIT_STRING ) ) == 0 )
320 321 322
        p += len;

    if( p != end_curve )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
323 324
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
325 326 327 328

    /*
     * ECPoint ::= OCTET STRING
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
329 330
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
331

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
332
    if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
333 334 335 336 337 338
                                      ( 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.
         */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
339
        if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
340
            ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
341 342 343 344
            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 )
345
        {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
346
            return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
347 348 349
        }
    }

350 351 352 353 354
    p += len;

    /*
     * order INTEGER
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
355 356
    if( ( ret = mbedtls_asn1_get_mpi( &p, end, &grp->N ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
357

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
358
    grp->nbits = mbedtls_mpi_msb( &grp->N );
359 360 361 362 363 364 365 366 367 368 369 370

    /*
     * 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.
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
371
static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
372
{
373
    int ret = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
374 375
    mbedtls_ecp_group ref;
    const mbedtls_ecp_group_id *id;
376

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
377
    mbedtls_ecp_group_init( &ref );
378

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
379
    for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
380 381
    {
        /* Load the group associated to that id */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
382
        mbedtls_ecp_group_free( &ref );
383
        MBEDTLS_MPI_CHK( mbedtls_ecp_group_load( &ref, *id ) );
384 385 386

        /* Compare to the group we were given, starting with easy tests */
        if( grp->pbits == ref.pbits && grp->nbits == ref.nbits &&
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
387 388 389 390 391 392
            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 &&
393
            /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
394
            mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
395 396 397 398 399 400 401
        {
            break;
        }

    }

cleanup:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
402
    mbedtls_ecp_group_free( &ref );
403 404 405

    *grp_id = *id;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
406 407
    if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
        ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
408 409 410 411 412 413 414

    return( ret );
}

/*
 * Parse a SpecifiedECDomain (SEC 1 C.2) and find the associated group ID
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
415 416
static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
                                       mbedtls_ecp_group_id *grp_id )
417 418
{
    int ret;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
419
    mbedtls_ecp_group grp;
420

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
421
    mbedtls_ecp_group_init( &grp );
422 423 424 425 426 427 428

    if( ( ret = pk_group_from_specified( params, &grp ) ) != 0 )
        goto cleanup;

    ret = pk_group_id_from_group( &grp, grp_id );

cleanup:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
429
    mbedtls_ecp_group_free( &grp );
430 431 432

    return( ret );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
433
#endif /* MBEDTLS_PK_PARSE_EC_EXTENDED */
434

435 436
/*
 * Use EC parameters to initialise an EC group
437 438 439 440 441
 *
 * ECParameters ::= CHOICE {
 *   namedCurve         OBJECT IDENTIFIER
 *   specifiedCurve     SpecifiedECDomain -- = SEQUENCE { ... }
 *   -- implicitCurve   NULL
442
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
443
static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
444 445
{
    int ret;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
446
    mbedtls_ecp_group_id grp_id;
447

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
448
    if( params->tag == MBEDTLS_ASN1_OID )
449
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
450 451
        if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
            return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
452 453 454
    }
    else
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
455
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
456 457
        if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
            return( ret );
458
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
459
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
460
#endif
461
    }
462 463 464 465

    /*
     * grp may already be initilialized; if so, make sure IDs match
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
466 467
    if( grp->id != MBEDTLS_ECP_DP_NONE && grp->id != grp_id )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
468

469
    if( ( ret = mbedtls_ecp_group_load( grp, grp_id ) ) != 0 )
470 471 472 473 474 475 476
        return( ret );

    return( 0 );
}

/*
 * EC public key is an EC point
477 478 479
 *
 * The caller is responsible for clearing the structure upon failure if
 * desired. Take care to pass along the possible ECP_FEATURE_UNAVAILABLE
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
480
 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
481 482
 */
static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
483
                            mbedtls_ecp_keypair *key )
484 485 486
{
    int ret;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
487
    if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
488
                    (const unsigned char *) *p, end - *p ) ) == 0 )
489
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
490
        ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
491 492 493
    }

    /*
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
494
     * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
495 496 497
     */
    *p = (unsigned char *) end;

498
    return( ret );
499
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
500
#endif /* MBEDTLS_ECP_C */
501

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
502
#if defined(MBEDTLS_RSA_C)
503 504 505 506 507 508 509 510
/*
 *  RSAPublicKey ::= SEQUENCE {
 *      modulus           INTEGER,  -- n
 *      publicExponent    INTEGER   -- e
 *  }
 */
static int pk_get_rsapubkey( unsigned char **p,
                             const unsigned char *end,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
511
                             mbedtls_rsa_context *rsa )
512 513 514 515
{
    int ret;
    size_t len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
516 517 518
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
519 520

    if( *p + len != end )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
521 522
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
523

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
524 525 526
    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 );
527 528

    if( *p != end )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
529 530
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
531

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
532 533
    if( ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
534

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
535
    rsa->len = mbedtls_mpi_size( &rsa->N );
536 537 538

    return( 0 );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
539
#endif /* MBEDTLS_RSA_C */
540 541 542 543 544 545 546 547 548

/* 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,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
549
                          mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
550 551
{
    int ret;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
552
    mbedtls_asn1_buf alg_oid;
553

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
554
    memset( params, 0, sizeof(mbedtls_asn1_buf) );
555

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
556 557
    if( ( ret = mbedtls_asn1_get_alg( p, end, &alg_oid, params ) ) != 0 )
        return( MBEDTLS_ERR_PK_INVALID_ALG + ret );
558

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
559 560
    if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
561 562 563 564

    /*
     * No parameters with RSA (only for EC)
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
565 566
    if( *pk_alg == MBEDTLS_PK_RSA &&
            ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
567 568
                params->len != 0 ) )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
569
        return( MBEDTLS_ERR_PK_INVALID_ALG );
570 571 572 573 574 575 576 577 578 579
    }

    return( 0 );
}

/*
 *  SubjectPublicKeyInfo  ::=  SEQUENCE  {
 *       algorithm            AlgorithmIdentifier,
 *       subjectPublicKey     BIT STRING }
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
580 581
int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
                        mbedtls_pk_context *pk )
582 583 584
{
    int ret;
    size_t len;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
585 586 587
    mbedtls_asn1_buf alg_params;
    mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
    const mbedtls_pk_info_t *pk_info;
588

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
589 590
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
591
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
592
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
593 594 595 596 597 598 599
    }

    end = *p + len;

    if( ( ret = pk_get_pk_alg( p, end, &pk_alg, &alg_params ) ) != 0 )
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
600 601
    if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
602 603

    if( *p + len != end )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
604 605
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
606

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
607 608
    if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
609

610
    if( ( ret = mbedtls_pk_setup( pk, pk_info ) ) != 0 )
611 612
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
613 614
#if defined(MBEDTLS_RSA_C)
    if( pk_alg == MBEDTLS_PK_RSA )
615
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
616
        ret = pk_get_rsapubkey( p, end, mbedtls_pk_rsa( *pk ) );
617
    } else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
618 619 620
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECP_C)
    if( pk_alg == MBEDTLS_PK_ECKEY_DH || pk_alg == MBEDTLS_PK_ECKEY )
621
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
622
        ret = pk_use_ecparams( &alg_params, &mbedtls_pk_ec( *pk )->grp );
623
        if( ret == 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
624
            ret = pk_get_ecpubkey( p, end, mbedtls_pk_ec( *pk ) );
625
    } else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
626 627
#endif /* MBEDTLS_ECP_C */
        ret = MBEDTLS_ERR_PK_UNKNOWN_PK_ALG;
628 629

    if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
630 631
        ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
              MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
632 633

    if( ret != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
634
        mbedtls_pk_free( pk );
635 636 637 638

    return( ret );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
639
#if defined(MBEDTLS_RSA_C)
640 641 642
/*
 * Parse a PKCS#1 encoded private RSA key
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
643
static int pk_parse_key_pkcs1_der( mbedtls_rsa_context *rsa,
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
                                   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
     *  }
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
670 671
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
672
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
673
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
674 675 676 677
    }

    end = p + len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
678
    if( ( ret = mbedtls_asn1_get_int( &p, end, &rsa->ver ) ) != 0 )
679
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
680
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
681 682 683 684
    }

    if( rsa->ver != 0 )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
685
        return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
686 687
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
688 689 690 691 692 693 694 695
    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 )
696
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
697 698
        mbedtls_rsa_free( rsa );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
699 700
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
701
    rsa->len = mbedtls_mpi_size( &rsa->N );
702 703 704

    if( p != end )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
705 706 707
        mbedtls_rsa_free( rsa );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
708 709
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
710
    if( ( ret = mbedtls_rsa_check_privkey( rsa ) ) != 0 )
711
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
712
        mbedtls_rsa_free( rsa );
713 714 715 716 717
        return( ret );
    }

    return( 0 );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
718
#endif /* MBEDTLS_RSA_C */
719

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
720
#if defined(MBEDTLS_ECP_C)
721 722 723
/*
 * Parse a SEC1 encoded private EC key
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
724
static int pk_parse_key_sec1_der( mbedtls_ecp_keypair *eck,
725 726 727 728
                                  const unsigned char *key,
                                  size_t keylen )
{
    int ret;
729
    int version, pubkey_done;
730
    size_t len;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
731
    mbedtls_asn1_buf params;
732 733 734 735 736 737 738 739 740 741 742 743 744 745
    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
     *    }
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
746 747
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
748
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
749
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
750 751 752 753
    }

    end = p + len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
754 755
    if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
756 757

    if( version != 1 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
758
        return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION );
759

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
760 761
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
762

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
763
    if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
764
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
765 766
        mbedtls_ecp_keypair_free( eck );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
767 768 769 770
    }

    p += len;

771 772
    pubkey_done = 0;
    if( p != end )
773
    {
774 775 776
        /*
         * Is 'parameters' present?
         */
777 778
        if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
                        MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) == 0 )
779
        {
780 781 782
            if( ( ret = pk_get_ecparams( &p, p + len, &params) ) != 0 ||
                ( ret = pk_use_ecparams( &params, &eck->grp )  ) != 0 )
            {
783
                mbedtls_ecp_keypair_free( eck );
784 785
                return( ret );
            }
786
        }
787
        else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
788
        {
789
            mbedtls_ecp_keypair_free( eck );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
790
            return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
791
        }
792

793 794 795 796
        /*
         * Is 'publickey' present? If not, or if we can't read it (eg because it
         * is compressed), create it from the private key.
         */
797 798
        if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
                        MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
799
        {
800 801
            end2 = p + len;

802 803
            if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
                return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
804 805

            if( p + len != end2 )
806 807
                return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                        MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
808 809 810 811 812 813 814 815 816

            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.
                 */
817 818
                if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
                    return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
819 820
            }
        }
821
        else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
822
        {
823 824
            mbedtls_ecp_keypair_free( eck );
            return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
825
        }
826
    }
827 828

    if( ! pubkey_done &&
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
829
        ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
830
                                                      NULL, NULL ) ) != 0 )
831
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
832 833
        mbedtls_ecp_keypair_free( eck );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
834
    }
835

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
836
    if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
837
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
838
        mbedtls_ecp_keypair_free( eck );
839 840 841
        return( ret );
    }

842
    return( 0 );
843
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
844
#endif /* MBEDTLS_ECP_C */
845 846 847 848 849

/*
 * Parse an unencrypted PKCS#8 encoded private key
 */
static int pk_parse_key_pkcs8_unencrypted_der(
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
850
                                    mbedtls_pk_context *pk,
851 852 853 854 855
                                    const unsigned char* key,
                                    size_t keylen )
{
    int ret, version;
    size_t len;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
856
    mbedtls_asn1_buf params;
857 858
    unsigned char *p = (unsigned char *) key;
    unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
859 860
    mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
    const mbedtls_pk_info_t *pk_info;
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

    /*
     * 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
     */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
878 879
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
880
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
881
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
882 883 884 885
    }

    end = p + len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
886 887
    if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
888 889

    if( version != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
890
        return( MBEDTLS_ERR_PK_KEY_INVALID_VERSION + ret );
891 892

    if( ( ret = pk_get_pk_alg( &p, end, &pk_alg, &params ) ) != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
893
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
894

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
895 896
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
897 898

    if( len < 1 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
899 900
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_OUT_OF_DATA );
901