aboutsummaryrefslogtreecommitdiff
path: root/library/pkcs11_client.c
blob: e1b7e6d2cf799e5530e45e7a114fa71337dbbb18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
 *  Generic wrapper for Cryptoki (PKCS#11) support
 *
 *  Copyright (C) 2017, ARM Limited, All Rights Reserved
 *  SPDX-License-Identifier: Apache-2.0
 *
 *  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
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  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.
 *
 *  This file is part of mbed TLS (https://tls.mbed.org)
 */

#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif

#if defined(MBEDTLS_PKCS11_CLIENT_C)

#include <stdint.h>
#include <string.h>

#include "mbedtls/pkcs11_client.h"

#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdlib.h>
#define mbedtls_calloc    calloc
#define mbedtls_free       free
#endif



#if defined(MBEDTLS_PK_C)
#include "mbedtls/pk.h"
#include "mbedtls/pk_info.h"

#if defined(MBEDTLS_ECDSA_C)
#include "mbedtls/asn1.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/bignum.h"
#include "mbedtls/ecdsa.h"
#include "mbedtls/ecp.h"
#include "mbedtls/oid.h"
#endif

#define ARRAY_LENGTH( a ) ( sizeof( a ) / sizeof( *( a ) ) )

typedef struct {
    mbedtls_pk_type_t key_type; /**< key type */
    CK_SESSION_HANDLE hSession; /**< session handle */
    CK_OBJECT_HANDLE hPublicKey; /**< public key handle (must not be null) */
    CK_OBJECT_HANDLE hPrivateKey; /**< private key handle (may be null) */
    uint16_t bit_length; /**< key length in bits */
} mbedtls_pk_pkcs11_context_t;

static int pkcs11_err_to_mbedtls_pk_err( CK_RV rv )
{
    switch( rv )
    {
    case CKR_OK:
        return( 0 );
    case CKR_HOST_MEMORY:
        return( MBEDTLS_ERR_PK_ALLOC_FAILED );
    case CKR_ARGUMENTS_BAD:
        return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
    case CKR_KEY_FUNCTION_NOT_PERMITTED:
        return( MBEDTLS_ERR_PK_NOT_PERMITTED );
    case CKR_MECHANISM_INVALID:
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
    case CKR_MECHANISM_PARAM_INVALID:
        return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
    case CKR_OBJECT_HANDLE_INVALID:
        return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
    case CKR_SIGNATURE_INVALID:
        return( MBEDTLS_ERR_PK_INVALID_SIGNATURE );
    case CKR_SIGNATURE_LEN_RANGE:
        return( MBEDTLS_ERR_PK_SIG_LEN_MISMATCH );
    case CKR_TEMPLATE_INCOMPLETE:
        return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
    case CKR_BUFFER_TOO_SMALL:
        return( MBEDTLS_ERR_PK_BUFFER_TOO_SMALL );
    default:
        return( MBEDTLS_ERR_PK_FILE_IO_ERROR );
    }
}

static size_t pkcs11_pk_get_bitlen( const void *ctx_arg )
{
    const mbedtls_pk_pkcs11_context_t *ctx = ctx_arg;
    return( ctx->bit_length );
}

static int pkcs11_pk_can_do( const void *ctx_arg, mbedtls_pk_type_t type )
{
    const mbedtls_pk_pkcs11_context_t *ctx = ctx_arg;
    return ctx->key_type == mbedtls_pk_representation_type( type );
}

static void *pkcs11_pk_alloc( )
{
    return( mbedtls_calloc( 1, sizeof( mbedtls_pk_pkcs11_context_t ) ) );
}

static void pkcs11_pk_free( void *ctx )
{
    mbedtls_free( ctx );
}

static size_t pkcs11_pk_signature_size( const void *ctx_arg )
{
    const mbedtls_pk_pkcs11_context_t *ctx = ctx_arg;
    switch( ctx->key_type )
    {
    case MBEDTLS_PK_RSA:
        return( ( ctx->bit_length + 7 ) / 8 );
    case MBEDTLS_PK_ECKEY:
        return( MBEDTLS_ECDSA_MAX_SIG_LEN( ctx->bit_length ) );
    default:
        return( 0 );
    }
}

static int pkcs11_sign( void *ctx_arg,
                        mbedtls_md_type_t md_alg,
                        const unsigned char *hash, size_t hash_len,
                        unsigned char *sig, size_t *sig_len,
                        int (*f_rng)(void *, unsigned char *, size_t),
                        void *p_rng )
{
    mbedtls_pk_pkcs11_context_t *ctx = ctx_arg;
    CK_RV rv;
    CK_MECHANISM mechanism = {0, NULL_PTR, 0};
    CK_ULONG ck_sig_len;
    (void)(md_alg);
    /* This function takes size_t arguments but the underlying layer
       takes unsigned long. Either type may be smaller than the other.
       Legitimate values won't overflow either type but we still need
       to check for overflow for robustness. */
    if( hash_len > (CK_ULONG)( -1 ) )
        return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
    (void) f_rng;
    (void) p_rng;

    switch( ctx->key_type )
    {
#if defined(MBEDTLS_ECDSA_C)
    case MBEDTLS_PK_ECKEY:
        ck_sig_len = MBEDTLS_ECDSA_MAX_SIG_LEN( ctx->bit_length );
        mechanism.mechanism = CKM_ECDSA;
        break;
#endif /* MBEDTLS_ECDSA_C */
    default:
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
    }

    rv = C_SignInit( ctx->hSession, &mechanism, ctx->hPrivateKey );
    if( rv != CKR_OK )
        goto exit;
    rv = C_Sign( ctx->hSession, (CK_BYTE_PTR) hash, hash_len,
                 sig, &ck_sig_len );
    if( rv != CKR_OK )
        goto exit;

    if( mechanism.mechanism == CKM_ECDSA )
    {
        /* The signature from the token contains r and s concatenated,
         * each in the form of a big-endian byte sequence, with r and s
         * having the same length as the base point.
         *
         * This library encodes ECDSA signatures in ASN.1 as documented
         * for mbedtls_ecdsa_write_signature:
         *   SEQUENCE {
         *     r INTEGER,
         *     s INTEGER
         *   }
         *
         * Perform the conversion using existing utility functions,
         * with temporary bignums.
         */
        uint16_t byte_len = ( ( ctx->bit_length + 7 ) / 8 );
        size_t sig_size = MBEDTLS_ECDSA_MAX_SIG_LEN( ctx->bit_length );
        mbedtls_mpi r, s;
        mbedtls_mpi_init( &r );
        mbedtls_mpi_init( &s );
        rv = CKR_OK;
        if( ck_sig_len != 2 * byte_len )
        {
            /* Bad data from the token */
            rv = CKR_GENERAL_ERROR;
            goto ecdsa_exit;
        }
        if( mbedtls_mpi_read_binary( &r, sig, byte_len ) != 0 ||
            mbedtls_mpi_read_binary( &s, sig + byte_len, byte_len ) != 0 )
        {
            rv = CKR_HOST_MEMORY;
            goto ecdsa_exit;
        }
        /* The signature buffer is guaranteed to have enough room for
           the encoded signature by the pk_sign interface. */
        if( mbedtls_ecdsa_signature_to_asn1( &r, &s, sig, sig_len, sig_size ) != 0 )
        {
            rv = CKR_GENERAL_ERROR;
            goto ecdsa_exit;
        }
    ecdsa_exit:
        mbedtls_mpi_free( &r );
        mbedtls_mpi_free( &s );
        if( rv != CKR_OK )
            goto exit;
    }
    else
    {
        *sig_len = ck_sig_len;
    }

exit:
    if( rv != CKR_OK )
        memset( sig, 0, ck_sig_len );
    return( pkcs11_err_to_mbedtls_pk_err( rv ) );
}

static int pkcs11_verify( void *ctx_arg,
                        mbedtls_md_type_t md_alg,
                        const unsigned char *hash, size_t hash_len,
                        const unsigned char *sig, size_t sig_len)
{
    mbedtls_pk_pkcs11_context_t *ctx = ctx_arg;
    CK_RV rv;
    CK_MECHANISM mechanism = {0, NULL_PTR, 0};
    unsigned char *decoded_sig = NULL;
    size_t decoded_sig_len;

    /* This function takes size_t arguments but the underlying layer
       takes unsigned long. Either type may be smaller than the other.
       Legitimate values won't overflow either type but we still need
       to check for overflow for robustness. */
    if( hash_len > (CK_ULONG)( -1 ) )
        return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );

    switch( ctx->key_type )
    {
#if defined(MBEDTLS_RSA_C)
    case MBEDTLS_PK_RSA:
        switch( md_alg )
        {
        case MBEDTLS_MD_MD5:
            mechanism.mechanism = CKM_MD5_RSA_PKCS;
            break;
        case MBEDTLS_MD_SHA1:
            mechanism.mechanism = CKM_SHA1_RSA_PKCS;
            break;
        case MBEDTLS_MD_SHA256:
            mechanism.mechanism = CKM_SHA256_RSA_PKCS;
            break;
        case MBEDTLS_MD_SHA384:
            mechanism.mechanism = CKM_SHA384_RSA_PKCS;
            break;
        case MBEDTLS_MD_SHA512:
            mechanism.mechanism = CKM_SHA512_RSA_PKCS;
            break;
        default:
            return( MBEDTLS_ERR_PK_INVALID_ALG );
        }
        break;
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECDSA_C)
    case MBEDTLS_PK_ECKEY:
        mechanism.mechanism = CKM_ECDSA;
        break;
#endif /* MBEDTLS_ECDSA_C */
    default:
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
    }
    if( mechanism.mechanism == CKM_ECDSA )
    {
        uint16_t byte_len = ( ( ctx->bit_length + 7 ) / 8 );
        decoded_sig = mbedtls_calloc( 1, 2 * byte_len );
        if( decoded_sig == NULL )
        {
            return( MBEDTLS_ERR_PK_ALLOC_FAILED );
        }
        if( mbedtls_ecdsa_signature_to_raw( sig, sig_len, byte_len,
                                    decoded_sig, 2 * byte_len,
                                    &decoded_sig_len ) != 0 )
        {
            rv = CKR_GENERAL_ERROR;
            goto exit;
        }
    }
    rv = C_VerifyInit( ctx->hSession, &mechanism, ctx->hPublicKey );
    if( rv != CKR_OK )
        goto exit;
    rv = C_Verify( ctx->hSession, (CK_BYTE_PTR) hash, hash_len,
           decoded_sig, decoded_sig_len );
    if( rv != CKR_OK )
        goto exit;

exit:
    mbedtls_free(decoded_sig);
    return( pkcs11_err_to_mbedtls_pk_err( rv ) );
}

static const mbedtls_pk_info_t mbedtls_pk_pkcs11_info =
    MBEDTLS_PK_OPAQUE_INFO_1( "pkcs11"
                              , pkcs11_pk_get_bitlen
                              , pkcs11_pk_can_do
                              , pkcs11_pk_signature_size
                              , pkcs11_verify
                              , pkcs11_sign
                              , NULL //pkcs11_decrypt
                              , NULL //pkcs11_encrypt
                              , NULL //check_pair_func
                              , pkcs11_pk_alloc
                              , pkcs11_pk_free
                              , NULL //debug_func
        );

int mbedtls_pkcs11_setup_pk( mbedtls_pk_context *ctx,
                             CK_SESSION_HANDLE hSession,
                             CK_OBJECT_HANDLE hPublicKey,
                             CK_OBJECT_HANDLE hPrivateKey )
{
    CK_OBJECT_CLASS public_key_class = -1, private_key_class = -1;
    CK_KEY_TYPE public_key_type = -1, private_key_type = -1;
    mbedtls_pk_type_t can_do;
    CK_ATTRIBUTE attributes[] = {
        {CKA_CLASS, &public_key_class, sizeof( public_key_class )},
        {CKA_KEY_TYPE, &public_key_type, sizeof( public_key_type )},
    };
    CK_RV rv;
    uint16_t key_size = 0;

    rv = C_GetAttributeValue( hSession, hPublicKey,
                              attributes, ARRAY_LENGTH( attributes ) );
    if( rv != CKR_OK )
        return( pkcs11_err_to_mbedtls_pk_err( rv ) );
    if( public_key_class != CKO_PUBLIC_KEY )
        return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );

    if( hPrivateKey != CK_INVALID_HANDLE )
    {
        attributes[0].pValue = &private_key_class;
        attributes[1].pValue = &private_key_type;
        rv = C_GetAttributeValue( hSession, hPrivateKey,
                                  attributes, ARRAY_LENGTH( attributes ) );
        if( rv != CKR_OK )
            return( pkcs11_err_to_mbedtls_pk_err( rv ) );
        if( private_key_class != CKO_PRIVATE_KEY )
            return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
        if( public_key_type != private_key_type )
            return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
    }

    switch( public_key_type ) {
#if defined(MBEDTLS_ECDSA_C)
    case CKK_ECDSA:
        can_do = MBEDTLS_PK_ECKEY;
        {
            unsigned char ecParams[MBEDTLS_OID_EC_GRP_MAX_SIZE];
            mbedtls_asn1_buf params_asn1;
            mbedtls_ecp_group_id grp_id;
            const mbedtls_ecp_curve_info *curve_info;
            attributes[0].type = CKA_EC_PARAMS;
            attributes[0].pValue = ecParams;
            attributes[0].ulValueLen = sizeof( ecParams );
            rv = C_GetAttributeValue( hSession, hPublicKey, attributes, 1 );
            if( rv != CKR_OK )
                return( pkcs11_err_to_mbedtls_pk_err( rv ) );
            params_asn1.tag = ecParams[0];
            params_asn1.len = ecParams[1];
            params_asn1.p = ecParams + 2;
            if( mbedtls_oid_get_ec_grp( &params_asn1, &grp_id ) != 0 )
                return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
            curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
            if( curve_info == NULL )
                return( MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE );
            key_size = curve_info->bit_size;
        }
        break;
#endif /* MBEDTLS_ECDSA_C */
    default:
        can_do = MBEDTLS_PK_OPAQUE;
        break;
    }

    {
        int ret = mbedtls_pk_setup( ctx, &mbedtls_pk_pkcs11_info );
        if( ret != 0 )
            return( MBEDTLS_ERR_PK_ALLOC_FAILED );
    }
    {
        mbedtls_pk_pkcs11_context_t *pkcs11_ctx = ctx->pk_ctx;
        pkcs11_ctx->key_type = can_do;
        pkcs11_ctx->bit_length = key_size;
        pkcs11_ctx->hSession = hSession;
        pkcs11_ctx->hPublicKey = hPublicKey;
        pkcs11_ctx->hPrivateKey = hPrivateKey;
    }
    return( 0 );
}

#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C)
static int mpi_to_ck( const mbedtls_mpi *mpi,
                      CK_ATTRIBUTE *attr, CK_ATTRIBUTE_TYPE at,
                      unsigned char **p, size_t len )
{
    if( mbedtls_mpi_write_binary( mpi, *p, len ) != 0 )
        return( 0 );
    attr->type = at;
    attr->pValue = *p;
    attr->ulValueLen = len;
    *p += len;
    return( 1 );
}
#define MPI_TO_CK( mpi, attr, at, p, len )                            \
    do                                                                \
    {                                                                 \
        if( !mpi_to_ck( ( mpi ), ( attr ), ( at ), ( p ), ( len ) ) ) \
        {                                                             \
            rv = CKR_ARGUMENTS_BAD;                                   \
            goto exit;                                                \
        }                                                             \
    }                                                                 \
    while( 0 )
#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) */

#define MBEDTLS_PKCS11_BOOL( x ) ( ( x ) ? CK_TRUE : CK_FALSE )

int mbedtls_pkcs11_import_pk( const mbedtls_pk_context *ctx,
                                 uint32_t flags,
                                 CK_SESSION_HANDLE hSession,
                                 CK_OBJECT_HANDLE *hPublicKey,
                                 CK_OBJECT_HANDLE *hPrivateKey )
{
    CK_OBJECT_CLASS cko_private_key = CKO_PRIVATE_KEY;
    CK_OBJECT_CLASS cko_public_key = CKO_PUBLIC_KEY;
    CK_KEY_TYPE ck_key_type;
    CK_BBOOL ck_sensitive = MBEDTLS_PKCS11_BOOL( flags & MBEDTLS_PKCS11_FLAG_SENSITIVE );
    CK_BBOOL ck_extractable = MBEDTLS_PKCS11_BOOL( flags & MBEDTLS_PKCS11_FLAG_EXTRACTABLE );
    CK_BBOOL ck_sign = MBEDTLS_PKCS11_BOOL( flags & MBEDTLS_PKCS11_FLAG_SIGN );
    CK_BBOOL ck_verify = MBEDTLS_PKCS11_BOOL( flags & MBEDTLS_PKCS11_FLAG_VERIFY );
    CK_BBOOL ck_decrypt = MBEDTLS_PKCS11_BOOL( flags & MBEDTLS_PKCS11_FLAG_DECRYPT );
    CK_BBOOL ck_encrypt = MBEDTLS_PKCS11_BOOL( flags & MBEDTLS_PKCS11_FLAG_ENCRYPT );
    CK_BBOOL ck_token = MBEDTLS_PKCS11_BOOL( flags & MBEDTLS_PKCS11_FLAG_TOKEN );
    CK_ATTRIBUTE public_attributes[] = {
        {CKA_CLASS, &cko_public_key, sizeof( cko_public_key )},
        {CKA_KEY_TYPE, &ck_key_type, sizeof( ck_key_type )},
        {CKA_TOKEN, &ck_token, sizeof( ck_token )},
        {CKA_ENCRYPT, &ck_encrypt, sizeof( ck_encrypt )},
        {CKA_VERIFY, &ck_verify, sizeof( ck_verify )},
#define COMMON_PUBLIC_ATTRIBUTES 5 // number of attributes above
        {-1, NULL, 0},
        {-1, NULL, 0},
    };
    CK_ATTRIBUTE private_attributes[] = {
        {CKA_CLASS, &cko_private_key, sizeof( cko_private_key )},
        {CKA_KEY_TYPE, &ck_key_type, sizeof( ck_key_type )},
        {CKA_TOKEN, &ck_token, sizeof( ck_token )},
        {CKA_DECRYPT, &ck_decrypt, sizeof( ck_decrypt )},
        {CKA_SIGN, &ck_sign, sizeof( ck_sign )},
        {CKA_SENSITIVE, &ck_sensitive, sizeof( ck_sensitive )},
        {CKA_EXTRACTABLE, &ck_extractable, sizeof( ck_extractable )},
#define COMMON_PRIVATE_ATTRIBUTES 7 // number of attributes above
        {-1, NULL, 0},
        {-1, NULL, 0},
        {-1, NULL, 0},
        {-1, NULL, 0},
        {-1, NULL, 0},
        {-1, NULL, 0},
        {-1, NULL, 0},
        {-1, NULL, 0},
    };
    CK_ATTRIBUTE *public_end = public_attributes + COMMON_PUBLIC_ATTRIBUTES;
    CK_ATTRIBUTE *private_end = private_attributes + COMMON_PRIVATE_ATTRIBUTES;
#undef COMMON_PUBLIC_ATTRIBUTES
#undef COMMON_PRIVATE_ATTRIBUTES
    unsigned char *data = NULL;
    CK_RV rv;

    if( hPublicKey != NULL )
        *hPublicKey = CK_INVALID_HANDLE;
    if( hPrivateKey != NULL )
        *hPrivateKey = CK_INVALID_HANDLE;

    /* Prepare the data-dependent key attributes */
    switch( mbedtls_pk_representation_type( mbedtls_pk_get_type( ctx ) ) )
    {
#if defined(MBEDTLS_ECDSA_C)
        case MBEDTLS_PK_ECKEY:
        {
            const mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *ctx );
            unsigned char *p;
            size_t curve_bytes = ( ec->grp.pbits + 7 ) / 8;
            size_t point_bytes = 4 + 2 * curve_bytes; // overapproximation
            int format = MBEDTLS_ECP_PF_UNCOMPRESSED;
            int ret;
            data = mbedtls_calloc( 1,
                                   MBEDTLS_OID_EC_GRP_MAX_SIZE +
                                   ( hPublicKey == NULL ? 0 : point_bytes ) +
                                   ( hPrivateKey == NULL ? 0 : curve_bytes ) );
            if( data == NULL )
            {
                rv = CKR_HOST_MEMORY;
                goto exit;
            }
            p = data;
            ck_key_type = CKK_ECDSA;
            /* Convert the group identifier */
            ret = mbedtls_ecp_ansi_write_group( &ec->grp, p,
                                                MBEDTLS_OID_EC_GRP_MAX_SIZE );
            if( ret < 0 )
            {
                rv = CKR_GENERAL_ERROR;
                goto exit;
            }
            public_end->type = CKA_EC_PARAMS;
            public_end->pValue = p;
            public_end->ulValueLen = ret;
            p += ret;
            *private_end++ = *public_end++;
            if( hPublicKey != NULL )
            {
                /* Convert the public point */
                ret = mbedtls_ecp_ansi_write_point( ec, format, p, point_bytes );
                if( ret < 0 )
                {
                    rv = CKR_GENERAL_ERROR;
                    goto exit;
                }
                public_end->type = CKA_EC_POINT;
                public_end->pValue = p;
                public_end->ulValueLen = ret;
                p += ret;
                public_end++;
            }
            if( hPrivateKey != NULL )
            {
                /* Convert the private value */
                MPI_TO_CK( &ec->d, private_end++, CKA_VALUE, &p, curve_bytes );
            }
        }
        break;
#endif /* MBEDTLS_ECDSA_C */
    default:
        return( MBEDTLS_ERR_PK_UNKNOWN_PK_ALG );
    }

    if( hPublicKey != NULL )
    {
        *hPublicKey = CK_INVALID_HANDLE;
        rv = C_CreateObject( hSession,
                             public_attributes,
                             public_end - public_attributes,
                             hPublicKey );
        if( rv != CKR_OK )
            goto exit;
    }

    if( hPrivateKey != NULL )
    {
        rv = C_CreateObject( hSession,
                             private_attributes,
                             private_end - private_attributes,
                             hPrivateKey );
        if( rv != CKR_OK )
            goto exit;
    }

exit:
    if( rv != CKR_OK )
    {
        /* In case an error happened, destroy any object that we
           created. In case C_DestroyObject failed, we report the original
           error, but *hPublicKey may contain a valid handle if
           creating the private key failed and then destroying the public key
           also failed (e.g. because the token disconnected). */
        if( hPublicKey != NULL && *hPublicKey != CK_INVALID_HANDLE )
        {
            if( C_DestroyObject( hSession, *hPublicKey ) == CKR_OK )
                *hPublicKey = CK_INVALID_HANDLE;
        }
        if( hPrivateKey != NULL && *hPrivateKey != CK_INVALID_HANDLE )
        {
            if( C_DestroyObject( hSession, *hPrivateKey ) == CKR_OK )
                *hPrivateKey = CK_INVALID_HANDLE;
        }
    }
    mbedtls_free( data );
    return( pkcs11_err_to_mbedtls_pk_err( rv ) );
}

#endif /* MBEDTLS_PK_C */



#endif /* MBEDTLS_PKCS11_CLIENT_C */