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

    if( ( f = fopen( path, "rb" ) ) == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
79
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
80 81 82 83 84

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

    *n = (size_t) size;

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

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

    fclose( f );

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

    return( 0 );
}

/*
 * Load and parse a private key
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
115
int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
116 117 118 119 120 121
                      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
122
    if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
123 124 125
        return( ret );

    if( pwd == NULL )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
126
        ret = mbedtls_pk_parse_key( ctx, buf, n, NULL, 0 );
127
    else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
128
        ret = mbedtls_pk_parse_key( ctx, buf, n,
129 130
                (const unsigned char *) pwd, strlen( pwd ) );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
131 132
    mbedtls_zeroize( buf, n + 1 );
    mbedtls_free( buf );
133 134 135 136 137 138 139

    return( ret );
}

/*
 * Load and parse a public key
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
140
int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path )
141 142 143 144 145
{
    int ret;
    size_t n;
    unsigned char *buf;

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

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
151 152
    mbedtls_zeroize( buf, n + 1 );
    mbedtls_free( buf );
153 154 155

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
158 159
#if defined(MBEDTLS_ECP_C)
/* Minimally parse an ECParameters buffer to and mbedtls_asn1_buf
160 161 162
 *
 * ECParameters ::= CHOICE {
 *   namedCurve         OBJECT IDENTIFIER
163
 *   specifiedCurve     SpecifiedECDomain -- = SEQUENCE { ... }
164 165 166 167
 *   -- 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
168
                            mbedtls_asn1_buf *params )
169 170 171
{
    int ret;

172
    /* Tag may be either OID or SEQUENCE */
173
    params->tag = **p;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
174 175 176
    if( params->tag != MBEDTLS_ASN1_OID
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
            && params->tag != ( MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE )
177 178
#endif
            )
179
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
180 181
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
182
    }
183

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
184
    if( ( ret = mbedtls_asn1_get_tag( p, end, &params->len, params->tag ) ) != 0 )
185
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
186
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
187
    }
188 189 190 191 192

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

    if( *p != end )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
193 194
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
195 196 197 198

    return( 0 );
}

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

    if( ver < 1 || ver > 3 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
233
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
234 235 236 237 238 239 240

    /*
     * 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
241 242
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
243 244 245 246 247 248 249 250 251 252 253 254
        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
255
    if( ( ret = mbedtls_asn1_get_tag( &p, end_field, &len, MBEDTLS_ASN1_OID ) ) != 0 )
256 257
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
258 259
    if( len != MBEDTLS_OID_SIZE( MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD ) ||
        memcmp( p, MBEDTLS_OID_ANSI_X9_62_PRIME_FIELD, len ) != 0 )
260
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
261
        return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
262 263 264 265 266
    }

    p += len;

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
270
    grp->pbits = mbedtls_mpi_msb( &grp->P );
271 272

    if( p != end_field )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
273 274
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
275 276 277 278 279 280 281 282 283 284

    /*
     * 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
285 286
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
287 288 289 290 291 292 293 294
        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
295 296
    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 )
297
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
298
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
299 300 301 302
    }

    p += len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
303 304
    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 )
305
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
306
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
307 308 309 310 311
    }

    p += len;

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

    if( p != end_curve )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
316 317
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
318 319 320 321

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
325
    if( ( ret = mbedtls_ecp_point_read_binary( grp, &grp->G,
326 327 328 329 330 331
                                      ( 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
332
        if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE ||
333
            ( p[0] != 0x02 && p[0] != 0x03 ) ||
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
334 335 336 337
            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 )
338
        {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
339
            return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
340 341 342
        }
    }

343 344 345 346 347
    p += len;

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
351
    grp->nbits = mbedtls_mpi_msb( &grp->N );
352 353 354 355 356 357 358 359 360 361 362 363

    /*
     * 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
364
static int pk_group_id_from_group( const mbedtls_ecp_group *grp, mbedtls_ecp_group_id *grp_id )
365
{
366
    int ret = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
367 368
    mbedtls_ecp_group ref;
    const mbedtls_ecp_group_id *id;
369

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
372
    for( id = mbedtls_ecp_grp_id_list(); *id != MBEDTLS_ECP_DP_NONE; id++ )
373 374
    {
        /* Load the group associated to that id */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
375 376
        mbedtls_ecp_group_free( &ref );
        MBEDTLS_MPI_CHK( mbedtls_ecp_use_known_dp( &ref, *id ) );
377 378 379

        /* 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
380 381 382 383 384 385
            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 &&
386
            /* For Y we may only know the parity bit, so compare only that */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
387
            mbedtls_mpi_get_bit( &grp->G.Y, 0 ) == mbedtls_mpi_get_bit( &ref.G.Y, 0 ) )
388 389 390 391 392 393 394
        {
            break;
        }

    }

cleanup:
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
395
    mbedtls_ecp_group_free( &ref );
396 397 398

    *grp_id = *id;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
399 400
    if( ret == 0 && *id == MBEDTLS_ECP_DP_NONE )
        ret = MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
401 402 403 404 405 406 407

    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
408 409
static int pk_group_id_from_specified( const mbedtls_asn1_buf *params,
                                       mbedtls_ecp_group_id *grp_id )
410 411
{
    int ret;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
412
    mbedtls_ecp_group grp;
413

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
414
    mbedtls_ecp_group_init( &grp );
415 416 417 418 419 420 421

    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
422
    mbedtls_ecp_group_free( &grp );
423 424 425

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

428 429
/*
 * Use EC parameters to initialise an EC group
430 431 432 433 434
 *
 * ECParameters ::= CHOICE {
 *   namedCurve         OBJECT IDENTIFIER
 *   specifiedCurve     SpecifiedECDomain -- = SEQUENCE { ... }
 *   -- implicitCurve   NULL
435
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
436
static int pk_use_ecparams( const mbedtls_asn1_buf *params, mbedtls_ecp_group *grp )
437 438
{
    int ret;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
439
    mbedtls_ecp_group_id grp_id;
440

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
441
    if( params->tag == MBEDTLS_ASN1_OID )
442
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
443 444
        if( mbedtls_oid_get_ec_grp( params, &grp_id ) != 0 )
            return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
445 446 447
    }
    else
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
448
#if defined(MBEDTLS_PK_PARSE_EC_EXTENDED)
449 450
        if( ( ret = pk_group_id_from_specified( params, &grp_id ) ) != 0 )
            return( ret );
451
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
452
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
453
#endif
454
    }
455 456 457 458

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
462
    if( ( ret = mbedtls_ecp_use_known_dp( grp, grp_id ) ) != 0 )
463 464 465 466 467 468 469
        return( ret );

    return( 0 );
}

/*
 * EC public key is an EC point
470 471 472
 *
 * 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
473
 * return code of mbedtls_ecp_point_read_binary() and leave p in a usable state.
474 475
 */
static int pk_get_ecpubkey( unsigned char **p, const unsigned char *end,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
476
                            mbedtls_ecp_keypair *key )
477 478 479
{
    int ret;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
480
    if( ( ret = mbedtls_ecp_point_read_binary( &key->grp, &key->Q,
481
                    (const unsigned char *) *p, end - *p ) ) == 0 )
482
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
483
        ret = mbedtls_ecp_check_pubkey( &key->grp, &key->Q );
484 485 486
    }

    /*
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
487
     * We know mbedtls_ecp_point_read_binary consumed all bytes or failed
488 489 490
     */
    *p = (unsigned char *) end;

491
    return( ret );
492
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
493
#endif /* MBEDTLS_ECP_C */
494

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
495
#if defined(MBEDTLS_RSA_C)
496 497 498 499 500 501 502 503
/*
 *  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
504
                             mbedtls_rsa_context *rsa )
505 506 507 508
{
    int ret;
    size_t len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
509 510 511
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
512 513

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
517 518 519
    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 );
520 521

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
525 526
    if( ( ret = mbedtls_rsa_check_pubkey( rsa ) ) != 0 )
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY );
527

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
528
    rsa->len = mbedtls_mpi_size( &rsa->N );
529 530 531

    return( 0 );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
532
#endif /* MBEDTLS_RSA_C */
533 534 535 536 537 538 539 540 541

/* 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
542
                          mbedtls_pk_type_t *pk_alg, mbedtls_asn1_buf *params )
543 544
{
    int ret;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
545
    mbedtls_asn1_buf alg_oid;
546

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

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
552 553
    if( mbedtls_oid_get_pk_alg( &alg_oid, pk_alg ) != 0 )
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
554 555 556 557

    /*
     * No parameters with RSA (only for EC)
     */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
558 559
    if( *pk_alg == MBEDTLS_PK_RSA &&
            ( ( params->tag != MBEDTLS_ASN1_NULL && params->tag != 0 ) ||
560 561
                params->len != 0 ) )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
562
        return( MBEDTLS_ERR_PK_INVALID_ALG );
563 564 565 566 567 568 569 570 571 572
    }

    return( 0 );
}

/*
 *  SubjectPublicKeyInfo  ::=  SEQUENCE  {
 *       algorithm            AlgorithmIdentifier,
 *       subjectPublicKey     BIT STRING }
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
573 574
int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
                        mbedtls_pk_context *pk )
575 576 577
{
    int ret;
    size_t len;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
578 579 580
    mbedtls_asn1_buf alg_params;
    mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
    const mbedtls_pk_info_t *pk_info;
581

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
582 583
    if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
                    MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
584
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
585
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
586 587 588 589 590 591 592
    }

    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
593 594
    if( ( ret = mbedtls_asn1_get_bitstring_null( p, end, &len ) ) != 0 )
        return( MBEDTLS_ERR_PK_INVALID_PUBKEY + ret );
595 596

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
600 601
    if( ( pk_info = mbedtls_pk_info_from_type( pk_alg ) ) == NULL )
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
602

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
603
    if( ( ret = mbedtls_pk_init_ctx( pk, pk_info ) ) != 0 )
604 605
        return( ret );

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

    if( ret == 0 && *p != end )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
623 624
        ret = MBEDTLS_ERR_PK_INVALID_PUBKEY
              MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
625 626

    if( ret != 0 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
627
        mbedtls_pk_free( pk );
628 629 630 631

    return( ret );
}

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

    end = p + len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
671
    if( ( ret = mbedtls_asn1_get_int( &p, end, &rsa->ver ) ) != 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
    }

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
681 682 683 684 685 686 687 688
    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 )
689
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
690 691
        mbedtls_rsa_free( rsa );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
692 693
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
694
    rsa->len = mbedtls_mpi_size( &rsa->N );
695 696 697

    if( p != end )
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
698 699 700
        mbedtls_rsa_free( rsa );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
701 702
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
703
    if( ( ret = mbedtls_rsa_check_privkey( rsa ) ) != 0 )
704
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
705
        mbedtls_rsa_free( rsa );
706 707 708 709 710
        return( ret );
    }

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

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

    end = p + len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
747 748
    if( ( ret = mbedtls_asn1_get_int( &p, end, &version ) ) != 0 )
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
749 750

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

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

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
756
    if( ( ret = mbedtls_mpi_read_binary( &eck->d, p, len ) ) != 0 )
757
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
758 759
        mbedtls_ecp_keypair_free( eck );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
760 761 762 763
    }

    p += len;

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

786 787 788 789
        /*
         * Is 'publickey' present? If not, or if we can't read it (eg because it
         * is compressed), create it from the private key.
         */
790 791
        if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
                        MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) ) == 0 )
792
        {
793 794
            end2 = p + len;

795 796
            if( ( ret = mbedtls_asn1_get_bitstring_null( &p, end2, &len ) ) != 0 )
                return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
797 798

            if( p + len != end2 )
799 800
                return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT +
                        MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
801 802 803 804 805 806 807 808 809

            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.
                 */
810 811
                if( ret != MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE )
                    return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT );
812 813
            }
        }
814
        else if( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
815
        {
816 817
            mbedtls_ecp_keypair_free( eck );
            return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
818
        }
819
    }
820 821

    if( ! pubkey_done &&
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
822
        ( ret = mbedtls_ecp_mul( &eck->grp, &eck->Q, &eck->d, &eck->grp.G,
823
                                                      NULL, NULL ) ) != 0 )
824
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
825 826
        mbedtls_ecp_keypair_free( eck );
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
827
    }
828

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
829
    if( ( ret = mbedtls_ecp_check_privkey( &eck->grp, &eck->d ) ) != 0 )
830
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
831
        mbedtls_ecp_keypair_free( eck );
832 833 834
        return( ret );
    }

835
    return( 0 );
836
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
837
#endif /* MBEDTLS_ECP_C */
838 839 840 841 842

/*
 * 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
843
                                    mbedtls_pk_context *pk,
844 845 846 847 848
                                    const unsigned char* key,
                                    size_t keylen )
{
    int ret, version;
    size_t len;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
849
    mbedtls_asn1_buf params;
850 851
    unsigned char *p = (unsigned char *) key;
    unsigned char *end = p + keylen;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
852 853
    mbedtls_pk_type_t pk_alg = MBEDTLS_PK_NONE;
    const mbedtls_pk_info_t *pk_info;
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

    /*
     * 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
871 872
    if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
            MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
873
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
874
        return( MBEDTLS_ERR_PK_KEY_INVALID_FORMAT + ret );
875 876 877 878
    }

    end = p + len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
879