-
Notifications
You must be signed in to change notification settings - Fork 635
/
Copy pathpkcs11_operations.c
1211 lines (1031 loc) · 42.4 KB
/
pkcs11_operations.c
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
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* AWS IoT Device SDK for Embedded C 202412.00
* Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/**
* @file pkcs11_operations.c
*
* @brief This file provides wrapper functions for PKCS11 operations.
*/
/* Standard includes. */
#include <errno.h>
#include <assert.h>
/* Config include. */
#include "demo_config.h"
/* Interface include. */
#include "pkcs11_operations.h"
/* PKCS #11 include. */
#include "core_pkcs11_config.h"
#include "core_pki_utils.h"
#include "mbedtls_utils.h"
/* MbedTLS include. */
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "entropy_poll.h"
#include "mbedtls/error.h"
#include "mbedtls/oid.h"
#include "mbedtls/pk.h"
#include "pk_wrap.h"
#include "mbedtls/sha256.h"
#include "mbedtls/x509_crt.h"
#include "mbedtls/x509_csr.h"
/**
* @brief Size of buffer in which to hold the certificate signing request (CSR).
*/
#define CLAIM_CERT_BUFFER_LENGTH 2048
/**
* @brief Size of buffer in which to hold the certificate signing request (CSR).
*/
#define CLAIM_PRIVATE_KEY_BUFFER_LENGTH 2048
/**
* @brief Represents string to be logged when mbedTLS returned error
* does not contain a high-level code.
*/
static const char * pNoHighLevelMbedTlsCodeStr = "<No-High-Level-Code>";
/**
* @brief Represents string to be logged when mbedTLS returned error
* does not contain a low-level code.
*/
static const char * pNoLowLevelMbedTlsCodeStr = "<No-Low-Level-Code>";
/**
* @brief Utility for converting the high-level code in an mbedTLS error to
* string, if the code-contains a high-level code; otherwise, using a default
* string.
*/
#define mbedtlsHighLevelCodeOrDefault( mbedTlsCode ) \
( mbedtls_high_level_strerr( mbedTlsCode ) != NULL ) \
? mbedtls_high_level_strerr( mbedTlsCode ) \
: pNoHighLevelMbedTlsCodeStr
/**
* @brief Utility for converting the level-level code in an mbedTLS error to
* string, if the code-contains a level-level code; otherwise, using a default
* string.
*/
#define mbedtlsLowLevelCodeOrDefault( mbedTlsCode ) \
( mbedtls_low_level_strerr( mbedTlsCode ) != NULL ) \
? mbedtls_low_level_strerr( mbedTlsCode ) \
: pNoLowLevelMbedTlsCodeStr
/* Length parameters for importing RSA-2048 private keys. */
#define MODULUS_LENGTH pkcs11RSA_2048_MODULUS_BITS / 8
#define E_LENGTH 3
#define D_LENGTH pkcs11RSA_2048_MODULUS_BITS / 8
#define PRIME_1_LENGTH 128
#define PRIME_2_LENGTH 128
#define EXPONENT_1_LENGTH 128
#define EXPONENT_2_LENGTH 128
#define COEFFICIENT_LENGTH 128
#define EC_PARAMS_LENGTH 10
#define EC_D_LENGTH 32
/**
* @brief Struct for holding parsed RSA-2048 private keys.
*/
typedef struct RsaParams_t
{
CK_BYTE modulus[ MODULUS_LENGTH + 1 ];
CK_BYTE e[ E_LENGTH + 1 ];
CK_BYTE d[ D_LENGTH + 1 ];
CK_BYTE prime1[ PRIME_1_LENGTH + 1 ];
CK_BYTE prime2[ PRIME_2_LENGTH + 1 ];
CK_BYTE exponent1[ EXPONENT_1_LENGTH + 1 ];
CK_BYTE exponent2[ EXPONENT_2_LENGTH + 1 ];
CK_BYTE coefficient[ COEFFICIENT_LENGTH + 1 ];
} RsaParams_t;
/**
* @brief Struct containing parameters needed by the signing callback.
*/
typedef struct SigningCallbackContext
{
CK_SESSION_HANDLE p11Session;
CK_OBJECT_HANDLE p11PrivateKey;
} SigningCallbackContext_t;
/**
* @brief Parameters for the signing callback. This needs to be global as
* MbedTLS passes the key context to the signing function, so we cannot pass
* our own.
*/
static SigningCallbackContext_t signingContext = { 0 };
/*-----------------------------------------------------------*/
/**
* @brief Reads a file into the given buffer.
*
* @param[in] path Path of the file.
* @param[out] pBuffer Buffer to read file contents into.
* @param[in] bufferLength Length of #pBuffer.
* @param[out] pOutWrittenLength Length of contents written to #pBuffer.
*/
static bool readFile( const char * path,
char * pBuffer,
size_t bufferLength,
size_t * pOutWrittenLength );
/**
* @brief Delete the specified crypto object from storage.
*
* @param[in] session The PKCS #11 session.
* @param[in] pkcsLabelsPtr The list of labels to remove.
* @param[in] pClass The list of corresponding classes.
* @param[in] count The length of #pkcsLabelsPtr and #pClass.
*/
static CK_RV destroyProvidedObjects( CK_SESSION_HANDLE session,
CK_BYTE_PTR * pkcsLabelsPtr,
CK_OBJECT_CLASS * pClass,
CK_ULONG count );
/**
* @brief Import the specified ECDSA private key into storage.
*
* @param[in] session The PKCS #11 session.
* @param[in] label The label to store the key.
* @param[in] mbedPkContext The private key to store.
*/
static CK_RV provisionPrivateECKey( CK_SESSION_HANDLE session,
const char * label,
mbedtls_pk_context * mbedPkContext );
/**
* @brief Import the specified RSA private key into storage.
*
* @param[in] session The PKCS #11 session.
* @param[in] label The label to store the key.
* @param[in] mbedPkContext The private key to store.
*/
static CK_RV provisionPrivateRSAKey( CK_SESSION_HANDLE session,
const char * label,
mbedtls_pk_context * mbedPkContext );
/**
* @brief Import the specified private key into storage.
*
* @param[in] session The PKCS #11 session.
* @param[in] privateKey The private key to store, in PEM format.
* @param[in] privateKeyLength The length of the key, including null terminator.
* @param[in] label The label to store the key.
*/
static CK_RV provisionPrivateKey( CK_SESSION_HANDLE session,
const char * privateKey,
size_t privateKeyLength,
const char * label );
/**
* @brief Import the specified X.509 client certificate into storage.
*
* @param[in] session The PKCS #11 session.
* @param[in] certificate The certificate to store, in PEM format.
* @param[in] certificateLength The length of the certificate, including the null terminator.
* @param[in] label The label to store the certificate.
*/
static CK_RV provisionCertificate( CK_SESSION_HANDLE session,
const char * certificate,
size_t certificateLength,
const char * label );
/**
* @brief Read the specified ECDSA public key into the MbedTLS ECDSA context.
*
* @param[in] p11Session The PKCS #11 session.
* @param[in] pEcdsaContext The context in which to store the key.
* @param[in] publicKey The public key to read.
*/
static int extractEcPublicKey( CK_SESSION_HANDLE p11Session,
mbedtls_ecdsa_context * pEcdsaContext,
CK_OBJECT_HANDLE publicKey );
/**
* @brief MbedTLS callback for signing using the provisioned private key. Used for
* signing the CSR.
*
* @param[in] pContext Unused.
* @param[in] mdAlg Unused.
* @param[in] pHash Data to sign.
* @param[in] hashLen Length of #pHash.
* @param[out] pSig The signature
* @param[in] sig_size Unused
* @param[out] pSigLen The length of the signature.
* @param[in] pRng Unused.
* @param[in] pRngContext Unused.
*/
static int32_t privateKeySigningCallback( mbedtls_pk_context * pContext,
mbedtls_md_type_t mdAlg,
const unsigned char * pHash,
size_t hashLen,
unsigned char * pSig,
size_t sig_size,
size_t * pSigLen,
int ( * pRng )( void *, unsigned char *, size_t ),
void * pRngContext );
/**
* @brief MbedTLS random generation callback to generate random values with
* PKCS #11.
*
* @param[in] pCtx Pointer to the PKCS #11 session handle.
* @param[out] pRandom Buffer to write random data to.
* @param[in] randomLength Length of random data to write.
*/
static int randomCallback( void * pCtx,
unsigned char * pRandom,
size_t randomLength );
/**
* @brief Generate a new ECDSA key pair using PKCS #11.
*
* @param[in] session The PKCS #11 session.
* @param[in] privateKeyLabel The label to store the private key.
* @param[in] publicKeyLabel The label to store the public key.
* @param[out] privateKeyHandlePtr The handle of the private key.
* @param[out] publicKeyHandlePtr The handle of the public key.
*/
static CK_RV generateKeyPairEC( CK_SESSION_HANDLE session,
const char * privateKeyLabel,
const char * publicKeyLabel,
CK_OBJECT_HANDLE_PTR privateKeyHandlePtr,
CK_OBJECT_HANDLE_PTR publicKeyHandlePtr );
/*-----------------------------------------------------------*/
static bool readFile( const char * path,
char * pBuffer,
size_t bufferLength,
size_t * pOutWrittenLength )
{
FILE * file;
size_t length = 0;
bool status = true;
/* Get the file descriptor for the CSR file. */
file = fopen( path, "rb" );
if( file == NULL )
{
LogError( ( "Error opening file at path: %s. Error: %s.",
path, strerror( errno ) ) );
status = false;
}
else
{
int result;
/* Seek to the end of the file, so that we can get the file size. */
result = fseek( file, 0L, SEEK_END );
if( result == -1 )
{
LogError( ( "Failed while moving to end of file. Path: %s. Error: %s.",
path, strerror( errno ) ) );
status = false;
}
else
{
long lenResult = -1;
/* Get the current position which is the file size. */
lenResult = ftell( file );
if( lenResult == -1 )
{
LogError( ( "Failed to get length of file. Path: %s. Error: %s.", path,
strerror( errno ) ) );
status = false;
}
else
{
length = ( size_t ) lenResult;
}
}
if( status == true )
{
if( length > bufferLength )
{
LogError( ( "Buffer too small for file. Buffer size: %ld. Required size: %ld.",
bufferLength, length ) );
status = false;
}
}
if( status == true )
{
/* Return to the beginning of the file. */
result = fseek( file, 0L, SEEK_SET );
if( result == -1 )
{
LogError( ( "Failed to move to beginning of file. Path: %s. Error: %s.",
path, strerror( errno ) ) );
status = false;
}
}
if( status == true )
{
size_t written = 0;
/* Read the CSR into our buffer. */
written = fread( pBuffer, 1, length, file );
if( written != length )
{
LogError( ( "Failed reading file. Path: %s. Error: %s.", path,
strerror( errno ) ) );
status = false;
}
else
{
*pOutWrittenLength = length;
}
}
fclose( file );
}
return status;
}
/*-----------------------------------------------------------*/
static CK_RV destroyProvidedObjects( CK_SESSION_HANDLE session,
CK_BYTE_PTR * pkcsLabelsPtr,
CK_OBJECT_CLASS * pClass,
CK_ULONG count )
{
CK_RV result;
CK_FUNCTION_LIST_PTR functionList;
CK_OBJECT_HANDLE objectHandle;
CK_BYTE * labelPtr;
CK_ULONG index = 0;
result = C_GetFunctionList( &functionList );
if( result != CKR_OK )
{
LogError( ( "Could not get a PKCS #11 function pointer." ) );
}
else
{
for( index = 0; index < count; index++ )
{
labelPtr = pkcsLabelsPtr[ index ];
result = xFindObjectWithLabelAndClass( session, ( char * ) labelPtr,
strlen( ( char * ) labelPtr ),
pClass[ index ], &objectHandle );
while( ( result == CKR_OK ) && ( objectHandle != CK_INVALID_HANDLE ) )
{
result = functionList->C_DestroyObject( session, objectHandle );
/* PKCS #11 allows a module to maintain multiple objects with the same
* label and type. The intent of this loop is to try to delete all of
* them. However, to avoid getting stuck, we won't try to find another
* object of the same label/type if the previous delete failed. */
if( result == CKR_OK )
{
result = xFindObjectWithLabelAndClass( session, ( char * ) labelPtr,
strlen( ( char * ) labelPtr ),
pClass[ index ], &objectHandle );
}
else
{
break;
}
}
}
}
return result;
}
/*-----------------------------------------------------------*/
static CK_RV provisionPrivateECKey( CK_SESSION_HANDLE session,
const char * label,
mbedtls_pk_context * mbedPkContext )
{
CK_RV result = CKR_OK;
CK_FUNCTION_LIST_PTR functionList = NULL;
CK_BYTE * DPtr = NULL; /* Private value D. */
CK_BYTE * ecParamsPtr = NULL; /* DER-encoding of an ANSI X9.62 Parameters value */
int mbedResult = 0;
CK_BBOOL trueObject = CK_TRUE;
CK_KEY_TYPE privateKeyType = CKK_EC;
CK_OBJECT_CLASS privateKeyClass = CKO_PRIVATE_KEY;
CK_OBJECT_HANDLE objectHandle = CK_INVALID_HANDLE;
mbedtls_ecp_keypair * keyPair = ( mbedtls_ecp_keypair * ) mbedPkContext->pk_ctx;
result = C_GetFunctionList( &functionList );
if( result != CKR_OK )
{
LogError( ( "Could not get a PKCS #11 function pointer." ) );
}
else
{
DPtr = ( CK_BYTE * ) malloc( EC_D_LENGTH );
if( DPtr == NULL )
{
result = CKR_HOST_MEMORY;
}
}
if( result == CKR_OK )
{
mbedResult = mbedtls_mpi_write_binary( &( keyPair->d ), DPtr, EC_D_LENGTH );
if( mbedResult != 0 )
{
LogError( ( "Failed to parse EC private key components." ) );
result = CKR_ATTRIBUTE_VALUE_INVALID;
}
}
if( result == CKR_OK )
{
if( keyPair->grp.id == MBEDTLS_ECP_DP_SECP256R1 )
{
ecParamsPtr = ( CK_BYTE * ) ( "\x06\x08" MBEDTLS_OID_EC_GRP_SECP256R1 );
}
else
{
result = CKR_CURVE_NOT_SUPPORTED;
}
}
if( result == CKR_OK )
{
CK_ATTRIBUTE privateKeyTemplate[] =
{
{ CKA_CLASS, NULL /* &privateKeyClass*/, sizeof( CK_OBJECT_CLASS ) },
{ CKA_KEY_TYPE, NULL /* &privateKeyType*/, sizeof( CK_KEY_TYPE ) },
{ CKA_LABEL, ( void * ) label, ( CK_ULONG ) strlen( label ) },
{ CKA_TOKEN, NULL /* &trueObject*/, sizeof( CK_BBOOL ) },
{ CKA_SIGN, NULL /* &trueObject*/, sizeof( CK_BBOOL ) },
{ CKA_EC_PARAMS, NULL /* ecParamsPtr*/, EC_PARAMS_LENGTH },
{ CKA_VALUE, NULL /* DPtr*/, EC_D_LENGTH }
};
/* Aggregate initializers must not use the address of an automatic variable. */
privateKeyTemplate[ 0 ].pValue = &privateKeyClass;
privateKeyTemplate[ 1 ].pValue = &privateKeyType;
privateKeyTemplate[ 3 ].pValue = &trueObject;
privateKeyTemplate[ 4 ].pValue = &trueObject;
privateKeyTemplate[ 5 ].pValue = ecParamsPtr;
privateKeyTemplate[ 6 ].pValue = DPtr;
result = functionList->C_CreateObject( session,
( CK_ATTRIBUTE_PTR ) &privateKeyTemplate,
sizeof( privateKeyTemplate ) / sizeof( CK_ATTRIBUTE ),
&objectHandle );
}
if( DPtr != NULL )
{
free( DPtr );
}
return result;
}
/*-----------------------------------------------------------*/
static CK_RV provisionPrivateRSAKey( CK_SESSION_HANDLE session,
const char * label,
mbedtls_pk_context * mbedPkContext )
{
CK_RV result = CKR_OK;
CK_FUNCTION_LIST_PTR functionList = NULL;
int mbedResult = 0;
CK_KEY_TYPE privateKeyType = CKK_RSA;
mbedtls_rsa_context * rsaContext = ( mbedtls_rsa_context * ) mbedPkContext->pk_ctx;
CK_OBJECT_CLASS privateKeyClass = CKO_PRIVATE_KEY;
RsaParams_t * rsaParams = NULL;
CK_BBOOL trueObject = CK_TRUE;
CK_OBJECT_HANDLE objectHandle = CK_INVALID_HANDLE;
result = C_GetFunctionList( &functionList );
if( result != CKR_OK )
{
LogError( ( "Could not get a PKCS #11 function pointer." ) );
}
else
{
rsaParams = ( RsaParams_t * ) malloc( sizeof( RsaParams_t ) );
if( rsaParams == NULL )
{
result = CKR_HOST_MEMORY;
}
}
if( result == CKR_OK )
{
memset( rsaParams, 0, sizeof( RsaParams_t ) );
mbedResult = mbedtls_rsa_export_raw( rsaContext,
rsaParams->modulus, MODULUS_LENGTH + 1,
rsaParams->prime1, PRIME_1_LENGTH + 1,
rsaParams->prime2, PRIME_2_LENGTH + 1,
rsaParams->d, D_LENGTH + 1,
rsaParams->e, E_LENGTH + 1 );
if( mbedResult != 0 )
{
LogError( ( "Failed to parse RSA private key components." ) );
result = CKR_ATTRIBUTE_VALUE_INVALID;
}
/* Export Exponent 1, Exponent 2, Coefficient. */
mbedResult |= mbedtls_mpi_write_binary( ( mbedtls_mpi const * ) &rsaContext->DP, rsaParams->exponent1, EXPONENT_1_LENGTH + 1 );
mbedResult |= mbedtls_mpi_write_binary( ( mbedtls_mpi const * ) &rsaContext->DQ, rsaParams->exponent2, EXPONENT_2_LENGTH + 1 );
mbedResult |= mbedtls_mpi_write_binary( ( mbedtls_mpi const * ) &rsaContext->QP, rsaParams->coefficient, COEFFICIENT_LENGTH + 1 );
if( mbedResult != 0 )
{
LogError( ( "Failed to parse RSA private key Chinese Remainder Theorem variables." ) );
result = CKR_ATTRIBUTE_VALUE_INVALID;
}
}
if( result == CKR_OK )
{
/* When importing the fields, the pointer is incremented by 1
* to remove the leading 0 padding (if it existed) and the original field
* length is used */
CK_ATTRIBUTE privateKeyTemplate[] =
{
{ CKA_CLASS, NULL /* &privateKeyClass */, sizeof( CK_OBJECT_CLASS ) },
{ CKA_KEY_TYPE, NULL /* &privateKeyType */, sizeof( CK_KEY_TYPE ) },
{ CKA_LABEL, ( void * ) label, ( CK_ULONG ) strlen( label ) },
{ CKA_TOKEN, NULL /* &trueObject */, sizeof( CK_BBOOL ) },
{ CKA_SIGN, NULL /* &trueObject */, sizeof( CK_BBOOL ) },
{ CKA_MODULUS, rsaParams->modulus + 1, MODULUS_LENGTH },
{ CKA_PRIVATE_EXPONENT, rsaParams->d + 1, D_LENGTH },
{ CKA_PUBLIC_EXPONENT, rsaParams->e + 1, E_LENGTH },
{ CKA_PRIME_1, rsaParams->prime1 + 1, PRIME_1_LENGTH },
{ CKA_PRIME_2, rsaParams->prime2 + 1, PRIME_2_LENGTH },
{ CKA_EXPONENT_1, rsaParams->exponent1 + 1, EXPONENT_1_LENGTH },
{ CKA_EXPONENT_2, rsaParams->exponent2 + 1, EXPONENT_2_LENGTH },
{ CKA_COEFFICIENT, rsaParams->coefficient + 1, COEFFICIENT_LENGTH }
};
/* Aggregate initializers must not use the address of an automatic variable. */
privateKeyTemplate[ 0 ].pValue = &privateKeyClass;
privateKeyTemplate[ 1 ].pValue = &privateKeyType;
privateKeyTemplate[ 3 ].pValue = &trueObject;
privateKeyTemplate[ 4 ].pValue = &trueObject;
result = functionList->C_CreateObject( session,
( CK_ATTRIBUTE_PTR ) &privateKeyTemplate,
sizeof( privateKeyTemplate ) / sizeof( CK_ATTRIBUTE ),
&objectHandle );
}
if( NULL != rsaParams )
{
free( rsaParams );
}
return result;
}
/*-----------------------------------------------------------*/
static CK_RV provisionPrivateKey( CK_SESSION_HANDLE session,
const char * privateKey,
size_t privateKeyLength,
const char * label )
{
CK_RV result = CKR_OK;
mbedtls_pk_type_t mbedKeyType = MBEDTLS_PK_NONE;
int mbedResult = 0;
mbedtls_pk_context mbedPkContext = { 0 };
mbedtls_ctr_drbg_context ctr_drbg = { 0 };
mbedtls_entropy_context entropy = { 0 };
mbedtls_pk_init( &mbedPkContext );
mbedtls_entropy_init( &entropy );
mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0 );
mbedResult = mbedtls_pk_parse_key( &mbedPkContext, ( const uint8_t * ) privateKey,
privateKeyLength, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg );
if( mbedResult != 0 )
{
LogError( ( "Unable to parse private key." ) );
result = CKR_ARGUMENTS_BAD;
}
/* Determine whether the key to be imported is RSA or EC. */
if( result == CKR_OK )
{
mbedKeyType = mbedtls_pk_get_type( &mbedPkContext );
if( mbedKeyType == MBEDTLS_PK_RSA )
{
result = provisionPrivateRSAKey( session, label, &mbedPkContext );
}
else if( ( mbedKeyType == MBEDTLS_PK_ECDSA ) ||
( mbedKeyType == MBEDTLS_PK_ECKEY ) ||
( mbedKeyType == MBEDTLS_PK_ECKEY_DH ) )
{
result = provisionPrivateECKey( session, label, &mbedPkContext );
}
else
{
LogError( ( "Invalid private key type provided. Only RSA-2048 and "
"EC P-256 keys are supported." ) );
result = CKR_ARGUMENTS_BAD;
}
}
mbedtls_pk_free( &mbedPkContext );
return result;
}
/*-----------------------------------------------------------*/
static CK_RV provisionCertificate( CK_SESSION_HANDLE session,
const char * certificate,
size_t certificateLength,
const char * label )
{
PKCS11_CertificateTemplate_t certificateTemplate;
CK_OBJECT_CLASS certificateClass = CKO_CERTIFICATE;
CK_CERTIFICATE_TYPE certificateType = CKC_X_509;
CK_FUNCTION_LIST_PTR functionList = NULL;
CK_RV result = CKR_OK;
uint8_t * derObject = NULL;
int32_t conversion = 0;
size_t derLen = 0;
CK_BBOOL tokenStorage = CK_TRUE;
CK_BYTE subject[] = "TestSubject";
CK_OBJECT_HANDLE objectHandle = CK_INVALID_HANDLE;
/* Initialize the client certificate template. */
certificateTemplate.xObjectClass.type = CKA_CLASS;
certificateTemplate.xObjectClass.pValue = &certificateClass;
certificateTemplate.xObjectClass.ulValueLen = sizeof( certificateClass );
certificateTemplate.xSubject.type = CKA_SUBJECT;
certificateTemplate.xSubject.pValue = subject;
certificateTemplate.xSubject.ulValueLen = strlen( ( const char * ) subject );
certificateTemplate.xValue.type = CKA_VALUE;
certificateTemplate.xValue.pValue = ( CK_VOID_PTR ) certificate;
certificateTemplate.xValue.ulValueLen = ( CK_ULONG ) certificateLength;
certificateTemplate.xLabel.type = CKA_LABEL;
certificateTemplate.xLabel.pValue = ( CK_VOID_PTR ) label;
certificateTemplate.xLabel.ulValueLen = strlen( label );
certificateTemplate.xCertificateType.type = CKA_CERTIFICATE_TYPE;
certificateTemplate.xCertificateType.pValue = &certificateType;
certificateTemplate.xCertificateType.ulValueLen = sizeof( CK_CERTIFICATE_TYPE );
certificateTemplate.xTokenObject.type = CKA_TOKEN;
certificateTemplate.xTokenObject.pValue = &tokenStorage;
certificateTemplate.xTokenObject.ulValueLen = sizeof( tokenStorage );
if( certificate == NULL )
{
LogError( ( "Certificate cannot be null." ) );
result = CKR_ATTRIBUTE_VALUE_INVALID;
}
if( result == CKR_OK )
{
result = C_GetFunctionList( &functionList );
if( result != CKR_OK )
{
LogError( ( "Could not get a PKCS #11 function pointer." ) );
}
}
if( result == CKR_OK )
{
/* Convert the certificate to DER format from PEM. The DER key should
* be about 3/4 the size of the PEM key, so mallocing the PEM key size
* is sufficient. */
derObject = ( uint8_t * ) malloc( certificateTemplate.xValue.ulValueLen );
derLen = certificateTemplate.xValue.ulValueLen;
if( derObject != NULL )
{
conversion = convert_pem_to_der( ( unsigned char * ) certificateTemplate.xValue.pValue,
certificateTemplate.xValue.ulValueLen,
derObject, &derLen );
if( 0 != conversion )
{
LogError( ( "Failed to convert provided certificate." ) );
result = CKR_ARGUMENTS_BAD;
}
}
else
{
LogError( ( "Failed to allocate buffer for converting certificate to DER." ) );
result = CKR_HOST_MEMORY;
}
}
if( result == CKR_OK )
{
/* Set the template pointers to refer to the DER converted objects. */
certificateTemplate.xValue.pValue = derObject;
certificateTemplate.xValue.ulValueLen = derLen;
/* Best effort clean-up of the existing object, if it exists. */
destroyProvidedObjects( session, ( CK_BYTE_PTR * ) &label, &certificateClass, 1 );
/* Create an object using the encoded client certificate. */
LogInfo( ( "Writing certificate into label \"%s\".", label ) );
result = functionList->C_CreateObject( session,
( CK_ATTRIBUTE_PTR ) &certificateTemplate,
sizeof( certificateTemplate ) / sizeof( CK_ATTRIBUTE ),
&objectHandle );
}
if( derObject != NULL )
{
free( derObject );
}
return result;
}
/*-----------------------------------------------------------*/
bool loadClaimCredentials( CK_SESSION_HANDLE p11Session,
const char * pClaimCertPath,
const char * pClaimCertLabel,
const char * pClaimPrivKeyPath,
const char * pClaimPrivKeyLabel )
{
bool status;
char claimCert[ CLAIM_CERT_BUFFER_LENGTH ] = { 0 };
size_t claimCertLength = 0;
char claimPrivateKey[ CLAIM_PRIVATE_KEY_BUFFER_LENGTH ] = { 0 };
size_t claimPrivateKeyLength = 0;
CK_RV ret;
assert( pClaimCertPath != NULL );
assert( pClaimCertLabel != NULL );
assert( pClaimPrivKeyPath != NULL );
assert( pClaimPrivKeyLabel != NULL );
status = readFile( pClaimCertPath, claimCert, CLAIM_CERT_BUFFER_LENGTH,
&claimCertLength );
if( status == true )
{
status = readFile( pClaimPrivKeyPath, claimPrivateKey,
CLAIM_PRIVATE_KEY_BUFFER_LENGTH, &claimPrivateKeyLength );
}
if( status == true )
{
ret = provisionPrivateKey( p11Session, claimPrivateKey,
claimPrivateKeyLength + 1, /* MbedTLS includes null character in length for PEM objects. */
pClaimPrivKeyLabel );
status = ( ret == CKR_OK );
}
if( status == true )
{
ret = provisionCertificate( p11Session, claimCert,
claimCertLength + 1, /* MbedTLS includes null character in length for PEM objects. */
pClaimCertLabel );
status = ( ret == CKR_OK );
}
return status;
}
/*-----------------------------------------------------------*/
static int extractEcPublicKey( CK_SESSION_HANDLE p11Session,
mbedtls_ecdsa_context * pEcdsaContext,
CK_OBJECT_HANDLE publicKey )
{
CK_ATTRIBUTE ecTemplate = { 0 };
int mbedtlsRet = -1;
CK_RV pkcs11ret = CKR_OK;
CK_BYTE ecPoint[ 67 ] = { 0 };
CK_FUNCTION_LIST_PTR pP11FunctionList;
mbedtls_ecdsa_init( pEcdsaContext );
mbedtls_ecp_group_init( &( pEcdsaContext->grp ) );
pkcs11ret = C_GetFunctionList( &pP11FunctionList );
if( pkcs11ret != CKR_OK )
{
LogError( ( "Could not get a PKCS #11 function pointer." ) );
}
else
{
ecTemplate.type = CKA_EC_POINT;
ecTemplate.pValue = ecPoint;
ecTemplate.ulValueLen = sizeof( ecPoint );
pkcs11ret = pP11FunctionList->C_GetAttributeValue( p11Session, publicKey, &ecTemplate, 1 );
if( pkcs11ret != CKR_OK )
{
LogError( ( "Failed to extract EC public key. Could not get attribute value. "
"C_GetAttributeValue failed with %lu.", pkcs11ret ) );
}
}
if( pkcs11ret == CKR_OK )
{
mbedtlsRet = mbedtls_ecp_group_load( &( pEcdsaContext->grp ), MBEDTLS_ECP_DP_SECP256R1 );
if( mbedtlsRet != 0 )
{
LogError( ( "Failed creating an EC key. "
"mbedtls_ecp_group_load failed: MbedTLS"
"error = %s : %s.",
mbedtlsHighLevelCodeOrDefault( mbedtlsRet ),
mbedtlsLowLevelCodeOrDefault( mbedtlsRet ) ) );
pkcs11ret = CKR_FUNCTION_FAILED;
}
else
{
mbedtlsRet = mbedtls_ecp_point_read_binary( &( pEcdsaContext->grp ), &( pEcdsaContext->Q ), &ecPoint[ 2 ], ecTemplate.ulValueLen - 2 );
if( mbedtlsRet != 0 )
{
LogError( ( "Failed creating an EC key. "
"mbedtls_ecp_group_load failed: MbedTLS"
"error = %s : %s.",
mbedtlsHighLevelCodeOrDefault( mbedtlsRet ),
mbedtlsLowLevelCodeOrDefault( mbedtlsRet ) ) );
pkcs11ret = CKR_FUNCTION_FAILED;
}
}
}
return mbedtlsRet;
}
/*-----------------------------------------------------------*/
static int32_t privateKeySigningCallback( mbedtls_pk_context * pContext,
mbedtls_md_type_t mdAlg,
const unsigned char * pHash,
size_t hashLen,
unsigned char * pSig,
size_t sig_size,
size_t * pSigLen,
int ( * pRng )( void *, unsigned char *, size_t ),
void * pRngContext )
{
CK_RV ret = CKR_OK;
int32_t result = 0;
CK_MECHANISM mech = { 0 };
CK_BYTE toBeSigned[ 256 ];
CK_ULONG toBeSignedLen = sizeof( toBeSigned );
CK_FUNCTION_LIST_PTR functionList = NULL;
/* Unreferenced parameters. */
( void ) ( pContext );
( void ) ( pRng );
( void ) ( pRngContext );
( void ) ( mdAlg );
( void ) ( sig_size );
/* Sanity check buffer length. */
if( hashLen > sizeof( toBeSigned ) )
{
ret = CKR_ARGUMENTS_BAD;
}
mech.mechanism = CKM_ECDSA;
memcpy( toBeSigned, pHash, hashLen );
toBeSignedLen = hashLen;
if( ret == CKR_OK )
{
ret = C_GetFunctionList( &functionList );
}
if( ret == CKR_OK )
{
ret = functionList->C_SignInit( signingContext.p11Session, &mech,
signingContext.p11PrivateKey );
}
if( ret == CKR_OK )
{
*pSigLen = sizeof( toBeSigned );
ret = functionList->C_Sign( signingContext.p11Session, toBeSigned,
toBeSignedLen, pSig, ( CK_ULONG_PTR ) pSigLen );
}
if( ret == CKR_OK )
{
/* PKCS #11 for P256 returns a 64-byte signature with 32 bytes for R and 32
* bytes for S. This must be converted to an ASN.1 encoded array. */
if( *pSigLen != pkcs11ECDSA_P256_SIGNATURE_LENGTH )
{
ret = CKR_FUNCTION_FAILED;
LogError( ( "Failed to sign message using PKCS #11. Expected signature "
"length of %lu, but received %lu.",
( unsigned long ) pkcs11ECDSA_P256_SIGNATURE_LENGTH,
( unsigned long ) *pSigLen ) );
}
if( ret == CKR_OK )
{
PKI_pkcs11SignatureTombedTLSSignature( pSig, pSigLen );
}
}
if( ret != CKR_OK )
{
LogError( ( "Failed to sign message using PKCS #11 with error code %lu.", ret ) );
result = -1;
}
return result;
}
/*-----------------------------------------------------------*/
static int randomCallback( void * pCtx,
unsigned char * pRandom,
size_t randomLength )
{
CK_SESSION_HANDLE * p11Session = ( CK_SESSION_HANDLE * ) pCtx;
CK_RV res;
CK_FUNCTION_LIST_PTR pP11FunctionList;
res = C_GetFunctionList( &pP11FunctionList );