This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
iot_test_pkcs11.c
2615 lines (2154 loc) · 118 KB
/
iot_test_pkcs11.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
/*
* FreeRTOS PKCS #11 V2.2.0
* Copyright (C) 2020 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.
*
* http://aws.amazon.com/freertos
* http://www.FreeRTOS.org
*/
/* Standard includes. */
#include <stdlib.h>
#include <string.h>
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "FreeRTOSIPConfig.h"
#include "task.h"
#include "event_groups.h"
#include "core_pki_utils.h"
/* Crypto includes. */
#include "iot_crypto.h"
#include "aws_clientcredential.h"
#include "iot_default_root_certificates.h"
#include "core_pkcs11_config.h"
#include "core_pkcs11.h"
#include "aws_dev_mode_key_provisioning.h"
#include "core_test_pkcs11_config.h"
#include "mbedtls/x509_crt.h"
#if ( pkcs11testRSA_KEY_SUPPORT == 0 ) && ( pkcs11testEC_KEY_SUPPORT == 0 )
#error "RSA or Elliptic curve keys (or both) must be supported."
#endif
#if ( pkcs11testIMPORT_PRIVATE_KEY_SUPPORT == 0 ) && ( pkcs11testGENERATE_KEYPAIR_SUPPORT == 0 ) && ( pkcs11testPREPROVISIONED_SUPPORT == 0 )
#error "The device must have some mechanism configured to provision the PKCS #11 stack."
#endif
#include "iot_test_pkcs11_globals.h"
#include "core_pkcs11_config.h"
/* Test includes. */
#include "unity_fixture.h"
#include "unity.h"
/* mbedTLS includes. */
#include "mbedtls/sha256.h"
#include "mbedtls/pk.h"
#include "mbedtls/pk_internal.h"
#include "mbedtls/oid.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy_poll.h"
typedef enum
{
eNone, /* Device is not provisioned. All credentials have been destroyed. */
eRsaTest, /* Provisioned using the RSA test credentials located in this file. */
eEllipticCurveTest, /* Provisioned using EC test credentials located in this file. */
eClientCredential, /* Provisioned using the credentials in aws_clientcredential_keys. */
eGeneratedEc, /* Provisioned using elliptic curve generated on device. Private key unknown. No corresponding certificate. */
eGeneratedRsa,
eDeliberatelyInvalid, /* Provisioned using credentials that are meant to trigger an error condition. */
eStateUnknown /* State of the credentials is unknown. */
} CredentialsProvisioned_t;
/* PKCS #11 Globals.
* These are used to reduce setup and tear down calls, and to
* prevent memory leaks in the case of TEST_PROTECT() actions being triggered. */
CK_SESSION_HANDLE xGlobalSession = 0;
CK_FUNCTION_LIST_PTR pxGlobalFunctionList = NULL_PTR;
CK_SLOT_ID xGlobalSlotId = 0;
CK_MECHANISM_TYPE xGlobalMechanismType = 0;
/* Model Based Tests (MBT) test group variables. */
CK_OBJECT_HANDLE xGlobalPublicKeyHandle = 0;
CK_OBJECT_HANDLE xGlobalPrivateKeyHandle = 0;
CK_BBOOL xGlobalCkTrue = CK_TRUE;
CK_BBOOL xGlobalCkFalse = CK_FALSE;
CredentialsProvisioned_t xCurrentCredentials = eStateUnknown;
/* PKCS #11 Global Data Containers. */
CK_BYTE rsaHashedMessage[ pkcs11SHA256_DIGEST_LENGTH ] = { 0 };
CK_BYTE ecdsaSignature[ pkcs11RSA_2048_SIGNATURE_LENGTH ] = { 0x00 };
CK_BYTE ecdsaHashedMessage[ pkcs11SHA256_DIGEST_LENGTH ] = { 0xab };
/* The StartFinish test group is for General Purpose,
* Session, Slot, and Token management functions.
* These tests do not require provisioning. */
TEST_GROUP( Full_PKCS11_StartFinish );
/* Tests for determing the capabilities of the PKCS #11 module. */
TEST_GROUP( Full_PKCS11_Capabilities );
/* The NoKey test group is for test of cryptographic functionality
* that do not require keys. Covers digesting and randomness.
* These tests do not require provisioning. */
TEST_GROUP( Full_PKCS11_NoObject );
/* The RSA test group is for tests that require RSA keys. */
TEST_GROUP( Full_PKCS11_RSA );
/* The EC test group is for tests that require elliptic curve keys. */
TEST_GROUP( Full_PKCS11_EC );
/* #define PKCS11_TEST_MEMORY_LEAK */
#ifdef PKCS11_TEST_MEMORY_LEAK
BaseType_t xHeapBefore;
BaseType_t xHeapAfter;
#endif
TEST_SETUP( Full_PKCS11_StartFinish )
{
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapBefore = xPortGetFreeHeapSize();
#endif
}
TEST_TEAR_DOWN( Full_PKCS11_StartFinish )
{
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapAfter = xPortGetFreeHeapSize();
configPRINTF( ( "Heap before %d, Heap After %d, Difference %d \r\n", xHeapBefore, xHeapAfter, ( xHeapAfter - xHeapBefore ) ) );
#endif
}
TEST_GROUP_RUNNER( Full_PKCS11_StartFinish )
{
RUN_TEST_CASE( Full_PKCS11_StartFinish, AFQP_StartFinish_FirstTest );
RUN_TEST_CASE( Full_PKCS11_StartFinish, AFQP_GetFunctionList );
RUN_TEST_CASE( Full_PKCS11_StartFinish, AFQP_InitializeFinalize );
RUN_TEST_CASE( Full_PKCS11_StartFinish, AFQP_GetSlotList );
RUN_TEST_CASE( Full_PKCS11_StartFinish, AFQP_OpenSessionCloseSession );
prvAfterRunningTests_NoObject();
}
TEST_SETUP( Full_PKCS11_Capabilities )
{
CK_RV xResult;
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to initialize PKCS #11 module." );
xResult = xInitializePkcs11Session( &xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to open PKCS #11 session." );
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapBefore = xPortGetFreeHeapSize();
#endif
}
TEST_TEAR_DOWN( Full_PKCS11_Capabilities )
{
CK_RV xResult;
xResult = pxGlobalFunctionList->C_CloseSession( xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to close session." );
xResult = pxGlobalFunctionList->C_Finalize( NULL );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to finalize session." );
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapAfter = xPortGetFreeHeapSize();
configPRINTF( ( "Heap before %d, Heap After %d, Difference %d \r\n", xHeapBefore, xHeapAfter, ( xHeapAfter - xHeapBefore ) ) );
#endif
}
TEST_GROUP_RUNNER( Full_PKCS11_Capabilities )
{
prvBeforeRunningTests();
RUN_TEST_CASE( Full_PKCS11_Capabilities, AFQP_Capabilities );
prvAfterRunningTests_NoObject();
}
TEST_SETUP( Full_PKCS11_NoObject )
{
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapBefore = xPortGetFreeHeapSize();
#endif
CK_RV xResult;
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to initialize PKCS #11 module." );
xResult = xInitializePkcs11Session( &xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to open PKCS #11 session." );
}
TEST_TEAR_DOWN( Full_PKCS11_NoObject )
{
CK_RV xResult;
xResult = pxGlobalFunctionList->C_CloseSession( xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to close session." );
xResult = pxGlobalFunctionList->C_Finalize( NULL );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to finalize session." );
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapAfter = xPortGetFreeHeapSize();
configPRINTF( ( "Heap before %d, Heap After %d, Difference %d \r\n", xHeapBefore, xHeapAfter, ( xHeapAfter - xHeapBefore ) ) );
#endif
}
TEST_GROUP_RUNNER( Full_PKCS11_NoObject )
{
prvBeforeRunningTests();
RUN_TEST_CASE( Full_PKCS11_NoObject, AFQP_Digest );
RUN_TEST_CASE( Full_PKCS11_NoObject, AFQP_Digest_ErrorConditions );
RUN_TEST_CASE( Full_PKCS11_NoObject, AFQP_GenerateRandom );
RUN_TEST_CASE( Full_PKCS11_NoObject, AFQP_GenerateRandomMultiThread );
prvAfterRunningTests_NoObject();
}
TEST_SETUP( Full_PKCS11_RSA )
{
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapBefore = xPortGetFreeHeapSize();
#endif
CK_RV xResult;
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to initialize PKCS #11 module." );
xResult = xInitializePkcs11Session( &xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to open PKCS #11 session." );
}
TEST_TEAR_DOWN( Full_PKCS11_RSA )
{
CK_RV xResult;
xResult = pxGlobalFunctionList->C_CloseSession( xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to close session." );
xResult = pxGlobalFunctionList->C_Finalize( NULL );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to finalize session." );
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapAfter = xPortGetFreeHeapSize();
configPRINTF( ( "Heap before %d, Heap After %d, Difference %d \r\n", xHeapBefore, xHeapAfter, ( xHeapAfter - xHeapBefore ) ) );
#endif
}
TEST_GROUP_RUNNER( Full_PKCS11_RSA )
{
#if ( pkcs11testRSA_KEY_SUPPORT == 1 )
prvBeforeRunningTests();
#if ( pkcs11testIMPORT_PRIVATE_KEY_SUPPORT == 1 )
RUN_TEST_CASE( Full_PKCS11_RSA, AFQP_CreateObject );
#endif
/* Generating RSA keys is not currently supported.
#if ( pkcs11testGENERATE_KEYPAIR_SUPPORT == 1 )
* RUN_TEST_CASE( Full_PKCS11_RSA, AFQP_GenerateKeyPair );
#endif
*/
RUN_TEST_CASE( Full_PKCS11_RSA, AFQP_FindObject );
RUN_TEST_CASE( Full_PKCS11_RSA, AFQP_FindObjectMultiThread );
RUN_TEST_CASE( Full_PKCS11_RSA, AFQP_GetAttributeValue );
RUN_TEST_CASE( Full_PKCS11_RSA, AFQP_Sign );
#if ( pkcs11testPREPROVISIONED_SUPPORT != 1 )
/* Always destroy objects last. */
RUN_TEST_CASE( Full_PKCS11_RSA, AFQP_DestroyObject );
#endif
#if ( pkcs11testIMPORT_PRIVATE_KEY_SUPPORT == 1 )
/* This will re-import credentials if supported, it is expected that
* secure elements do not destroy the "real" credentials and the test
* suite will not try to re-provision a secure element if they are
* destroyed, so it is best to use a separate slot for throwaway test
* credentials.
*/
prvAfterRunningTests_Object();
#endif
#endif /* if ( pkcs11testRSA_KEY_SUPPORT == 1 ) */
}
TEST_SETUP( Full_PKCS11_EC )
{
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapBefore = xPortGetFreeHeapSize();
#endif
CK_RV xResult;
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to initialize PKCS #11 module." );
xResult = xInitializePkcs11Session( &xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to open PKCS #11 session." );
}
TEST_TEAR_DOWN( Full_PKCS11_EC )
{
CK_RV xResult;
xResult = pxGlobalFunctionList->C_CloseSession( xGlobalSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to close session." );
xResult = pxGlobalFunctionList->C_Finalize( NULL );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to finalize session." );
#ifdef PKCS11_TEST_MEMORY_LEAK
/* Give the print buffer time to empty */
vTaskDelay( 500 );
xHeapAfter = xPortGetFreeHeapSize();
configPRINTF( ( "Heap before %d, Heap After %d, Difference %d \r\n", xHeapBefore, xHeapAfter, ( xHeapAfter - xHeapBefore ) ) );
#endif
}
TEST_GROUP_RUNNER( Full_PKCS11_EC )
{
#if ( pkcs11testEC_KEY_SUPPORT == 1 )
prvBeforeRunningTests();
#if ( pkcs11testGENERATE_KEYPAIR_SUPPORT == 1 )
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_GenerateKeyPair );
#endif
/* Always run AFQP_CreateObject after AFQP_GenerateKeyPair if the port
* supports both. Ports that support object importing have extra checks
* as the contents of the private key and public key are well known.
*/
#if ( pkcs11testIMPORT_PRIVATE_KEY_SUPPORT == 1 )
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_CreateObject );
#endif
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_FindObject );
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_GetAttributeValue );
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_Sign );
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_Verify );
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_FindObjectMultiThread );
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_GetAttributeValueMultiThread );
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_SignVerifyMultiThread );
#if ( pkcs11testPREPROVISIONED_SUPPORT != 1 )
RUN_TEST_CASE( Full_PKCS11_EC, AFQP_DestroyObject );
#endif
#if ( pkcs11testIMPORT_PRIVATE_KEY_SUPPORT == 1 )
/* This will re-import credentials if supported, it is expected that
* secure elements do not destroy the "real" credentials and the test
* suite will not try to re-provision a secure element if they are
* destroyed, so it is best to use a separate slot for throwaway test
* credentials.
*/
prvAfterRunningTests_Object();
#endif
#endif /* if ( pkcs11testEC_KEY_SUPPORT == 1 ) */
}
/* Data structure to store results of multi-thread tests. */
typedef struct MultithreadTaskParams
{
BaseType_t xTaskNumber;
CK_RV xTestResult;
void * pvTaskData;
} MultithreadTaskParams_t;
/* Event group used to synchronize tasks. */
static EventGroupHandle_t xSyncEventGroup;
static MultithreadTaskParams_t xGlobalTaskParams[ pkcs11testMULTI_THREAD_TASK_COUNT ];
/*-----------------------------------------------------------*/
/* Multitask loop configuration. */
/*-----------------------------------------------------------*/
/* Stack size of each task. This can be configured in core_test_pkcs11_config.h. */
#ifndef pkcs11testMULTI_TASK_STACK_SIZE
#define pkcs11testMULTI_TASK_STACK_SIZE ( configMINIMAL_STACK_SIZE * 6 )
#endif
/* Priority of each task. This can be configured in core_test_pkcs11_config.h. */
#ifndef pkcs11testMULTI_TASK_PRIORITY
#define pkcs11testMULTI_TASK_PRIORITY ( tskIDLE_PRIORITY )
#endif
/* Specifies bits for all tasks to the event group. */
#define pkcs11testALL_BITS ( ( 1 << pkcs11testMULTI_THREAD_TASK_COUNT ) - 1 )
static CK_RV prvDestroyTestCredentials( void )
{
CK_RV xResult = CKR_OK;
CK_RV xDestroyResult = CKR_OK;
CK_OBJECT_HANDLE xObject = CK_INVALID_HANDLE;
CK_ULONG ulLabelCount = 0;
CK_BYTE * pxPkcsLabels[] =
{
( CK_BYTE * ) pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS,
( CK_BYTE * ) pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS,
( CK_BYTE * ) pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS,
#if ( pkcs11configJITP_CODEVERIFY_ROOT_CERT_SUPPORTED == 1 )
( CK_BYTE * ) pkcs11testLABEL_CODE_VERIFICATION_KEY,
( CK_BYTE * ) pkcs11testLABEL_JITP_CERTIFICATE,
( CK_BYTE * ) pkcs11testLABEL_ROOT_CERTIFICATE
#endif
};
CK_OBJECT_CLASS xClass[] =
{
CKO_CERTIFICATE,
CKO_PRIVATE_KEY,
CKO_PUBLIC_KEY,
#if ( pkcs11configJITP_CODEVERIFY_ROOT_CERT_SUPPORTED == 1 )
CKO_PUBLIC_KEY,
CKO_CERTIFICATE,
CKO_CERTIFICATE
#endif
};
xDestroyResult = xDestroyProvidedObjects( xGlobalSession,
pxPkcsLabels,
xClass,
sizeof( xClass ) / sizeof( CK_OBJECT_CLASS ) );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to destroy credentials." );
for( ulLabelCount = 0;
ulLabelCount < sizeof( xClass ) / sizeof( CK_OBJECT_CLASS );
ulLabelCount++ )
{
xResult = xFindObjectWithLabelAndClass( xGlobalSession,
( char * ) pxPkcsLabels[ ulLabelCount ],
strlen( ( char * ) pxPkcsLabels[ ulLabelCount ] ),
xClass[ ulLabelCount ],
&xObject );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Found an object after deleting it.\r\n" );
TEST_ASSERT_EQUAL_MESSAGE( CK_INVALID_HANDLE, xObject, "Object found after it was destroyed.\r\n" );
}
return xDestroyResult;
}
CK_RV prvBeforeRunningTests( void )
{
CK_RV xResult;
/* Initialize the function list */
xResult = C_GetFunctionList( &pxGlobalFunctionList );
if( xResult == CKR_OK )
{
/* Close the last session if it was not closed already. */
pxGlobalFunctionList->C_Finalize( NULL );
}
return xResult;
}
/* If no changes to PKCS #11 objects have been made during the test,
* just make sure that the PKCS #11 module is initialized and in a good state.
*/
void prvAfterRunningTests_NoObject( void )
{
xInitializePKCS11();
}
/* If these tests may have manipulated the PKCS #11 objects
* (private key, public keys and/or certificates), run this routine afterwards
* to make sure that credentials are in a good state for the other test groups. */
void prvAfterRunningTests_Object( void )
{
/* Check if the test label is the same as the run-time label. */
/* Only reprovision a device that supports importing private keys. */
#if ( pkcs11testIMPORT_PRIVATE_KEY_SUPPORT == 1 )
/* If labels are the same, then we are assuming that this device does not
* have a secure element. */
if( ( 0 == strcmp( pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS, pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) ) &&
( 0 == strcmp( pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS, pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS ) ) )
{
/* Delete the old device private key and certificate, if that
* operation is supported by this port. Replace
* them with known-good AWS IoT credentials. */
xDestroyDefaultCryptoObjects( xGlobalSession );
/* Re-provision the device with default certs
* so that subsequent tests are not changed. */
vDevModeKeyProvisioning();
xCurrentCredentials = eClientCredential;
}
else
{
/* If the labels are different, then test credentials
* and application credentials are stored in separate
* slots which were not modified, so nothing special
* needs to be done. */
}
#endif /* if ( pkcs11testIMPORT_PRIVATE_KEY_SUPPORT == 1 ) */
}
static void prvMultiThreadHelper( void * pvTaskFxnPtr )
{
BaseType_t xTaskNumber;
/* Create the event group used to synchronize tasks. */
xSyncEventGroup = xEventGroupCreate();
if( xSyncEventGroup != NULL )
{
/* Create the sign verify loop tasks. */
for( xTaskNumber = 0; xTaskNumber < pkcs11testMULTI_THREAD_TASK_COUNT; xTaskNumber++ )
{
xGlobalTaskParams[ xTaskNumber ].xTaskNumber = xTaskNumber;
xGlobalTaskParams[ xTaskNumber ].xTestResult = 0xFFFFFFFF; /* Initialize to a non-zero value. */
xTaskCreate( ( TaskFunction_t ) pvTaskFxnPtr, /* Task code. */
"Multithread", /* All tasks have same name, but are distinguished by task number. */
pkcs11testMULTI_TASK_STACK_SIZE, /* Task stack size. */
&( xGlobalTaskParams[ xTaskNumber ] ), /* Where the task writes its result. */
pkcs11testMULTI_TASK_PRIORITY, /* Task priority. */
NULL ); /* Task handle (not used). */
}
/* Wait for all tasks to finish. */
if( xEventGroupWaitBits( xSyncEventGroup,
pkcs11testALL_BITS,
pdFALSE,
pdTRUE,
pkcs11testEVENT_GROUP_TIMEOUT_MS ) != pkcs11testALL_BITS )
{
configPRINTF( ( "Timed out waiting for tasks to finish in multi thread test.\r\n" ) );
}
vEventGroupDelete( xSyncEventGroup );
}
/* Check the tasks' results. */
if( TEST_PROTECT() )
{
for( xTaskNumber = 0; xTaskNumber < pkcs11testMULTI_THREAD_TASK_COUNT; xTaskNumber++ )
{
if( xGlobalTaskParams[ xTaskNumber ].xTestResult != 0 )
{
configPRINTF( ( "Multi thread task %d returned failure.\r\n",
xGlobalTaskParams[ xTaskNumber ].xTaskNumber ) );
TEST_FAIL();
}
}
}
}
/* Assumes that device is already provisioned at time of calling. */
static void prvFindObjectTest( CK_OBJECT_HANDLE_PTR pxPrivateKeyHandle,
CK_OBJECT_HANDLE_PTR pxCertificateHandle,
CK_OBJECT_HANDLE_PTR pxPublicKeyHandle )
{
CK_RV xResult;
CK_OBJECT_HANDLE xTestObjectHandle = CK_INVALID_HANDLE;
/* Happy Path - Find a previously created object. */
xResult = xFindObjectWithLabelAndClass( xGlobalSession,
pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) - 1,
CKO_PRIVATE_KEY,
pxPrivateKeyHandle );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to find private key after closing and reopening a session." );
TEST_ASSERT_NOT_EQUAL_MESSAGE( CK_INVALID_HANDLE, *pxPrivateKeyHandle, "Invalid object handle found for private key." );
/* TODO: Add the code sign key and root ca. */
xResult = xFindObjectWithLabelAndClass( xGlobalSession,
pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS ) - 1,
CKO_PUBLIC_KEY,
pxPublicKeyHandle );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to find public key after closing and reopening a session." );
TEST_ASSERT_NOT_EQUAL_MESSAGE( CK_INVALID_HANDLE, *pxPublicKeyHandle, "Invalid object handle found for public key key." );
xResult = xFindObjectWithLabelAndClass( xGlobalSession,
pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS ) - 1,
CKO_CERTIFICATE,
pxCertificateHandle );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to find certificate after closing and reopening a session." );
TEST_ASSERT_NOT_EQUAL_MESSAGE( CK_INVALID_HANDLE, *pxCertificateHandle, "Invalid object handle found for certificate." );
/* Try to find an object that has never been created. */
xResult = xFindObjectWithLabelAndClass( xGlobalSession,
( char * ) "This label doesn't exist",
sizeof( "This label doesn't exist" ) - 1,
CKO_PUBLIC_KEY,
&xTestObjectHandle );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Incorrect error code finding object that doesn't exist" );
TEST_ASSERT_EQUAL_MESSAGE( CK_INVALID_HANDLE, xTestObjectHandle, "Incorrect error code finding object that doesn't exist" );
}
TEST( Full_PKCS11_StartFinish, AFQP_StartFinish_FirstTest )
{
CK_RV xResult;
/* Finalize the PKCS #11 module to get it in a known state.
* Set up the PKCS #11 function list pointer. */
xResult = prvBeforeRunningTests();
/* prvBeforeRunningTests finalizes the PKCS #11 modules so that tests will start
* in a known state. It is OK if the module was not previously initialized. */
if( xResult == CKR_CRYPTOKI_NOT_INITIALIZED )
{
xResult = CKR_OK;
}
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Setup for the PKCS #11 routine failed. Test module will start in an unknown state." );
}
/*-----------------------------------------------------------*/
TEST( Full_PKCS11_StartFinish, AFQP_GetFunctionList )
{
CK_FUNCTION_LIST_PTR pxFunctionList = NULL;
TEST_ASSERT_EQUAL( CKR_ARGUMENTS_BAD, C_GetFunctionList( NULL ) );
TEST_ASSERT_EQUAL( CKR_OK, C_GetFunctionList( &pxFunctionList ) );
/* Ensure that pxFunctionList was changed by C_GetFunctionList. */
TEST_ASSERT_NOT_EQUAL( NULL, pxFunctionList );
}
TEST( Full_PKCS11_StartFinish, AFQP_InitializeFinalize )
{
CK_FUNCTION_LIST_PTR pxFunctionList = NULL;
CK_RV xResult;
xResult = C_GetFunctionList( &pxFunctionList );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to get function list." );
TEST_ASSERT_NOT_EQUAL_MESSAGE( NULL, pxFunctionList, "Invalid function list pointer." );
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to initialize PKCS #11 module." );
if( TEST_PROTECT() )
{
/* Call initialize a second time. Since this call may be made many times,
* it is important that PKCS #11 implementations be tolerant of multiple calls. */
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_CRYPTOKI_ALREADY_INITIALIZED, xResult, "Second PKCS #11 module initialization. " );
/* C_Finalize should fail if pReserved isn't NULL. */
xResult = pxFunctionList->C_Finalize( ( CK_VOID_PTR ) 0x1234 );
TEST_ASSERT_EQUAL_MESSAGE( CKR_ARGUMENTS_BAD, xResult, "Negative Test: Finalize with invalid argument" );
}
xResult = pxFunctionList->C_Finalize( NULL );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Finalize failed" );
/* Call Finalize a second time. Since finalize may be called multiple times,
* it is important that the PKCS #11 module is tolerant of multiple calls. */
xResult = pxFunctionList->C_Finalize( NULL );
TEST_ASSERT_EQUAL_MESSAGE( CKR_CRYPTOKI_NOT_INITIALIZED, xResult, "Second PKCS #11 finalization failed" );
}
TEST( Full_PKCS11_StartFinish, AFQP_GetSlotList )
{
CK_RV xResult;
CK_SLOT_ID * pxSlotId = NULL;
CK_ULONG xSlotCount = 0;
CK_ULONG xExtraSlotCount = 0;
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to initialize PKCS #11 module" );
if( TEST_PROTECT() )
{
/* The Happy Path. */
/* When a NULL slot pointer is passed in,
* the number of slots should be updated. */
xResult = pxGlobalFunctionList->C_GetSlotList( CK_TRUE, NULL, &xSlotCount );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to get slot count" );
TEST_ASSERT_GREATER_THAN_MESSAGE( 0, xSlotCount, "Slot count incorrectly updated" );
/* Allocate memory to receive the list of slots, plus one extra. */
pxSlotId = pvPortMalloc( sizeof( CK_SLOT_ID ) * ( xSlotCount + 1 ) );
TEST_ASSERT_NOT_EQUAL_MESSAGE( NULL, pxSlotId, "Failed malloc memory for slot list" );
/* Call C_GetSlotList again to receive all slots with tokens present. */
xResult = pxGlobalFunctionList->C_GetSlotList( CK_TRUE, pxSlotId, &xSlotCount );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to get slot count" );
/* Note: Being able to use the slot to open a session will be tested
* in the C_OpenSession tests. */
/* Off the happy path. */
xExtraSlotCount = xSlotCount + 1;
/* Make sure that number of slots returned is updated when extra buffer room exists. */
xResult = pxGlobalFunctionList->C_GetSlotList( CK_TRUE, pxSlotId, &xExtraSlotCount );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to get slot list" );
TEST_ASSERT_EQUAL_MESSAGE( xSlotCount, xExtraSlotCount, "Failed to update the number of slots" );
/* Claim that the buffer to receive slots is too small. */
xSlotCount = 0;
xResult = pxGlobalFunctionList->C_GetSlotList( CK_TRUE, pxSlotId, &xSlotCount );
TEST_ASSERT_EQUAL_MESSAGE( CKR_BUFFER_TOO_SMALL, xResult, "Negative Test: Improper handling of too-small slot buffer" );
}
if( pxSlotId != NULL )
{
vPortFree( pxSlotId );
}
xResult = pxGlobalFunctionList->C_Finalize( NULL );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Finalize failed" );
}
extern CK_RV xGetSlotList( CK_SLOT_ID ** ppxSlotId,
CK_ULONG * pxSlotCount );
TEST( Full_PKCS11_StartFinish, AFQP_OpenSessionCloseSession )
{
CK_SLOT_ID_PTR pxSlotId = NULL;
CK_SLOT_ID xSlotId = 0;
CK_ULONG xSlotCount = 0;
CK_SESSION_HANDLE xSession = 0;
CK_BBOOL xSessionOpen = CK_FALSE;
CK_RV xResult = CKR_OK;
xResult = xInitializePKCS11();
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to initialize PKCS #11 module" );
if( TEST_PROTECT() )
{
xResult = xGetSlotList( &pxSlotId,
&xSlotCount );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to get slot list" );
xSlotId = pxSlotId[ pkcs11testSLOT_NUMBER ];
vPortFree( pxSlotId ); /* xGetSlotList allocates memory. */
TEST_ASSERT_GREATER_THAN( 0, xSlotCount );
xResult = pxGlobalFunctionList->C_OpenSession( xSlotId,
CKF_SERIAL_SESSION, /* This flag is mandatory for PKCS #11 legacy reasons. */
NULL, /* Application defined pointer. */
NULL, /* Callback function. */
&xSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to open session" );
xSessionOpen = CK_TRUE;
}
if( xSessionOpen )
{
xResult = pxGlobalFunctionList->C_CloseSession( xSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to close session." );
}
pxGlobalFunctionList->C_Finalize( NULL );
/* Negative tests */
/* Try to open a session without having initialized the module. */
xResult = pxGlobalFunctionList->C_OpenSession( xSlotId,
CKF_SERIAL_SESSION, /* This flag is mandatory for PKCS #11 legacy reasons. */
NULL, /* Application defined pointer. */
NULL, /* Callback function. */
&xSession );
TEST_ASSERT_EQUAL_MESSAGE( CKR_CRYPTOKI_NOT_INITIALIZED, xResult, "Negative Test: Opened a session before initializing module." );
}
/*--------------------------------------------------------*/
/*-------------- Capabilities Tests --------------------- */
/*--------------------------------------------------------*/
TEST( Full_PKCS11_Capabilities, AFQP_Capabilities )
{
CK_RV xResult = 0;
CK_ULONG xSlotCount = 0;
CK_SLOT_ID_PTR pxSlotId = NULL;
CK_MECHANISM_INFO MechanismInfo = { 0 };
CK_BBOOL xSupportsKeyGen = CK_FALSE;
/* Determine the number of slots. */
xResult = pxGlobalFunctionList->C_GetSlotList( CK_TRUE, NULL, &xSlotCount );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to get slot count" );
/* Allocate memory to receive the list of slots, plus one extra. */
pxSlotId = pvPortMalloc( sizeof( CK_SLOT_ID ) * xSlotCount );
TEST_ASSERT_NOT_EQUAL_MESSAGE( NULL, pxSlotId, "Failed malloc memory for slot list" );
/* Call C_GetSlotList again to receive all slots with tokens present. */
xResult = pxGlobalFunctionList->C_GetSlotList( CK_TRUE, pxSlotId, &xSlotCount );
TEST_ASSERT_EQUAL_MESSAGE( CKR_OK, xResult, "Failed to get slot count" );
/* Check for RSA PKCS #1 signing support. */
xResult = pxGlobalFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], CKM_RSA_PKCS, &MechanismInfo );
TEST_ASSERT_TRUE( CKR_OK == xResult || CKR_MECHANISM_INVALID == xResult );
if( CKR_OK == xResult )
{
TEST_ASSERT_TRUE( 0 != ( CKF_SIGN & MechanismInfo.flags ) );
TEST_ASSERT_TRUE( MechanismInfo.ulMaxKeySize >= pkcs11RSA_2048_MODULUS_BITS &&
MechanismInfo.ulMinKeySize <= pkcs11RSA_2048_MODULUS_BITS );
/* Check for pre-padded signature verification support. This is required
* for round-trip testing. */
xResult = pxGlobalFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], CKM_RSA_X_509, &MechanismInfo );
TEST_ASSERT_TRUE( CKR_OK == xResult );
TEST_ASSERT_TRUE( 0 != ( CKF_VERIFY & MechanismInfo.flags ) );
TEST_ASSERT_TRUE( MechanismInfo.ulMaxKeySize >= pkcs11RSA_2048_MODULUS_BITS &&
MechanismInfo.ulMinKeySize <= pkcs11RSA_2048_MODULUS_BITS );
/* Check consistency with static configuration. */
#if ( 0 == pkcs11testRSA_KEY_SUPPORT )
TEST_FAIL_MESSAGE( "Static and runtime configuration for key generation support are inconsistent." );
#endif
configPRINTF( ( "The PKCS #11 module supports RSA signing.\r\n" ) );
}
/* Check for ECDSA support, if applicable. */
xResult = pxGlobalFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], CKM_ECDSA, &MechanismInfo );
TEST_ASSERT_TRUE( CKR_OK == xResult || CKR_MECHANISM_INVALID == xResult );
if( CKR_OK == xResult )
{
TEST_ASSERT_TRUE( 0 != ( ( CKF_SIGN | CKF_VERIFY ) & MechanismInfo.flags ) );
TEST_ASSERT_TRUE( MechanismInfo.ulMaxKeySize >= pkcs11ECDSA_P256_KEY_BITS &&
MechanismInfo.ulMinKeySize <= pkcs11ECDSA_P256_KEY_BITS );
/* Check consistency with static configuration. */
#if ( 0 == pkcs11testEC_KEY_SUPPORT )
TEST_FAIL_MESSAGE( "Static and runtime configuration for key generation support are inconsistent." );
#endif
configPRINTF( ( "The PKCS #11 module supports ECDSA.\r\n" ) );
}
#if ( pkcs11testPREPROVISIONED_SUPPORT != 1 )
/* Check for elliptic-curve key generation support. */
xResult = pxGlobalFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], CKM_EC_KEY_PAIR_GEN, &MechanismInfo );
TEST_ASSERT_TRUE( CKR_OK == xResult || CKR_MECHANISM_INVALID == xResult );
if( CKR_OK == xResult )
{
TEST_ASSERT_TRUE( 0 != ( CKF_GENERATE_KEY_PAIR & MechanismInfo.flags ) );
TEST_ASSERT_TRUE( MechanismInfo.ulMaxKeySize >= pkcs11ECDSA_P256_KEY_BITS &&
MechanismInfo.ulMinKeySize <= pkcs11ECDSA_P256_KEY_BITS );
xSupportsKeyGen = CK_TRUE;
configPRINTF( ( "The PKCS #11 module supports elliptic-curve key generation.\r\n" ) );
}
/* Check for consistency between static configuration and runtime key
* generation settings. */
if( CK_TRUE == xSupportsKeyGen )
{
#if ( 0 == pkcs11testGENERATE_KEYPAIR_SUPPORT )
TEST_FAIL_MESSAGE( "Static and runtime configuration for key generation support are inconsistent." );
#endif
}
else
{
#if ( 1 == pkcs11testGENERATE_KEYPAIR_SUPPORT )
TEST_FAIL_MESSAGE( "Static and runtime configuration for key generation support are inconsistent." );
#endif
}
#endif /* if ( pkcs11testPREPROVISIONED_SUPPORT != 1 ) */
/* SHA-256 support is required. */
xResult = pxGlobalFunctionList->C_GetMechanismInfo( pxSlotId[ 0 ], CKM_SHA256, &MechanismInfo );
TEST_ASSERT_TRUE( CKR_OK == xResult );
TEST_ASSERT_TRUE( 0 != ( CKF_DIGEST & MechanismInfo.flags ) );
vPortFree( pxSlotId );
/* Report on static configuration for key import support. */
#if ( 1 == pkcs11testIMPORT_PRIVATE_KEY_SUPPORT )
configPRINTF( ( "The PKCS #11 module supports private key import.\r\n" ) );
#endif
}
/*--------------------------------------------------------*/
/*-------------- No Object Tests ------------------------ */
/*--------------------------------------------------------*/
static CK_BYTE x896BitInput[] = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
static CK_BYTE xSha256HashOf896BitInput[] =
{
0xcf, 0x5b, 0x16, 0xa7, 0x78, 0xaf, 0x83, 0x80, 0x03, 0x6c, 0xe5, 0x9e, 0x7b, 0x04, 0x92, 0x37,
0x0b, 0x24, 0x9b, 0x11, 0xe8, 0xf0, 0x7a, 0x51, 0xaf, 0xac, 0x45, 0x03, 0x7a, 0xfe, 0xe9, 0xd1
};
TEST( Full_PKCS11_NoObject, AFQP_Digest )
{
CK_RV xResult = 0;
CK_MECHANISM xDigestMechanism;
CK_BYTE xDigestResult[ pkcs11SHA256_DIGEST_LENGTH ] = { 0 };
CK_ULONG xDigestLength = 0;
/* Hash with SHA256 */
xDigestMechanism.mechanism = CKM_SHA256;
xResult = pxGlobalFunctionList->C_DigestInit( xGlobalSession, &xDigestMechanism );
TEST_ASSERT_EQUAL( CKR_OK, xResult );
/* Subtract one because this hash was performed on the characters without the null terminator. */
xResult = pxGlobalFunctionList->C_DigestUpdate( xGlobalSession, x896BitInput, sizeof( x896BitInput ) - 1 );
TEST_ASSERT_EQUAL( CKR_OK, xResult );
/* Call C_DigestFinal on a NULL buffer to get the buffer length required. */
xResult = pxGlobalFunctionList->C_DigestFinal( xGlobalSession, NULL, &xDigestLength );
TEST_ASSERT_EQUAL( CKR_OK, xResult );
TEST_ASSERT_EQUAL( pkcs11SHA256_DIGEST_LENGTH, xDigestLength );
xResult = pxGlobalFunctionList->C_DigestFinal( xGlobalSession, xDigestResult, &xDigestLength );
TEST_ASSERT_EQUAL( CKR_OK, xResult );
TEST_ASSERT_EQUAL_INT8_ARRAY( xSha256HashOf896BitInput, xDigestResult, pkcs11SHA256_DIGEST_LENGTH );
}
TEST( Full_PKCS11_NoObject, AFQP_Digest_ErrorConditions )
{
CK_RV xResult = 0;
CK_MECHANISM xDigestMechanism;
CK_BYTE xDigestResult[ pkcs11SHA256_DIGEST_LENGTH ] = { 0 };
CK_ULONG xDigestLength = 0;
/* Make sure that no NULL pointers in functions to be called in this test. */
TEST_ASSERT_NOT_EQUAL( NULL, pxGlobalFunctionList->C_DigestInit );
TEST_ASSERT_NOT_EQUAL( NULL, pxGlobalFunctionList->C_DigestUpdate );
TEST_ASSERT_NOT_EQUAL( NULL, pxGlobalFunctionList->C_DigestFinal );
/* Invalid hash mechanism. */
xDigestMechanism.mechanism = 0x253; /*253 doesn't correspond to anything. */ /*CKM_MD5; */
xResult = pxGlobalFunctionList->C_DigestInit( xGlobalSession, &xDigestMechanism );
TEST_ASSERT_EQUAL( CKR_MECHANISM_INVALID, xResult );
/* Null Session. */
xDigestMechanism.mechanism = CKM_SHA256;
xResult = pxGlobalFunctionList->C_DigestInit( ( CK_SESSION_HANDLE ) NULL, &xDigestMechanism );
TEST_ASSERT_EQUAL( CKR_SESSION_HANDLE_INVALID, xResult );
/* Make sure that digest update fails if DigestInit did not succeed. */
xResult = pxGlobalFunctionList->C_DigestUpdate( xGlobalSession, x896BitInput, sizeof( x896BitInput ) - 1 );
TEST_ASSERT_EQUAL( CKR_OPERATION_NOT_INITIALIZED, xResult );
/* Initialize the session properly. */
xResult = pxGlobalFunctionList->C_DigestInit( xGlobalSession, &xDigestMechanism );
TEST_ASSERT_EQUAL( CKR_OK, xResult );
/* Try to update digest with a NULL session handle. */
xResult = pxGlobalFunctionList->C_DigestUpdate( ( CK_SESSION_HANDLE ) NULL, x896BitInput, sizeof( x896BitInput ) - 1 );
TEST_ASSERT_EQUAL( CKR_SESSION_HANDLE_INVALID, xResult );