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_pkcs11_trustx.c
3240 lines (2782 loc) · 106 KB
/
iot_pkcs11_trustx.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
/**
* MIT License
*
* Copyright (c) 2018 Infineon Technologies AG
*
* 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
*
* OPTIGA(TM) Trust X - based PKCS#11 V1.0.0
*/
/**
* @file aws_pkcs11_trustx.c
* @brief OPTIGA(TM) Trust X -based PKCS#11 implementation for software keys. This
* file deviates from the FreeRTOS style standard for some function names and
* data types in order to maintain compliance with the PKCS #11 standard.
*/
/* FreeRTOS includes. */
#include "FreeRTOS.h"
#include "semphr.h"
#include "core_pkcs11_config.h"
#ifdef FREERTOS_ENABLE_UNIT_TESTS
#include "core_test_pkcs11_config.h"
#endif
#include "iot_crypto.h"
#include "core_pkcs11.h"
#include "core_pkcs11_pal.h"
/* OPTIGA(TM) Trust X Includes */
#include "optiga/optiga_crypt.h"
#include "optiga/optiga_util.h"
#include "optiga/pal/pal_os_event.h"
#include "optiga/ifx_i2c/ifx_i2c_config.h"
#include "ecdsa_utils.h"
/* C runtime includes. */
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define PKCS11_PRINT( X ) configPRINTF( X )
#define PKCS11_WARNING_PRINT( X ) /* configPRINTF( X ) */
#define pkcs11NO_OPERATION ( ( CK_MECHANISM_TYPE ) 0xFFFFFFFFF )
/* The size of the buffer malloc'ed for the exported public key in C_GenerateKeyPair */
#define pkcs11KEY_GEN_MAX_DER_SIZE 200
#define MAX_PUBLIC_KEY_SIZE 100
typedef struct P11Object_t
{
CK_OBJECT_HANDLE xHandle;
/* If Label is associated with a private key slot. */
/* This flag is used to mark it active/non-active, as we can't remove the private key */
CK_BYTE xLabel[ pkcs11configMAX_LABEL_LENGTH + 1 ]; /* Plus 1 for the null terminator. */
} P11Object_t;
typedef struct P11ObjectList_t
{
SemaphoreHandle_t xMutex; /* Mutex that protects write operations to the xObjects array. */
P11Object_t xObjects[ pkcs11configMAX_NUM_OBJECTS ];
} P11ObjectList_t;
/* PKCS #11 Object */
typedef struct P11Struct_t
{
CK_BBOOL xIsInitialized;
P11ObjectList_t xObjectList;
uint16_t xCertOid;
uint16_t xPrivKeyOid;
} P11Struct_t, * P11Context_t;
static P11Struct_t xP11Context;
typedef struct OptigaSha256Ctx_t
{
uint8_t pubHashCtxBuf[ 130 ];
optiga_hash_context_t xHashCtx;
} OptigaSha256Ctx_t;
/**
* @brief Session structure.
*/
typedef struct P11Session
{
CK_ULONG ulState;
CK_BBOOL xOpened;
CK_MECHANISM_TYPE xOperationInProgress;
CK_BYTE * pxFindObjectLabel; /* Pointer to the label for the search in progress. Should be NULL if no search in progress. */
uint8_t xFindObjectLabelLength;
CK_MECHANISM_TYPE xVerifyMechanism;
uint16_t usVerifyKeyOid;
CK_MECHANISM_TYPE xSignMechanism;
uint16_t usSignKeyOid;
OptigaSha256Ctx_t xSHA256Context;
} P11Session_t, * P11SessionPtr_t;
/**
* @brief Cryptoki module attribute definitions.
*/
#define pkcs11SLOT_ID 1
/**
* @brief Helper definitions.
*/
#define pkcs11CREATE_OBJECT_MIN_ATTRIBUTE_COUNT 3
#define pkcs11GENERATE_KEY_PAIR_KEYTYPE_ATTRIBUTE_INDEX 0
#define pkcs11GENERATE_KEY_PAIR_ECPARAMS_ATTRIBUTE_INDEX 1
#define PKCS11_MODULE_IS_INITIALIZED ( ( xP11Context.xIsInitialized == CK_TRUE ) ? 1 : 0 )
#define PKCS11_SESSION_IS_OPEN( xSessionHandle ) ( ( ( ( P11SessionPtr_t ) xSessionHandle )->xOpened ) == CK_TRUE ? CKR_OK : CKR_SESSION_CLOSED )
#define PKCS11_SESSION_IS_VALID( xSessionHandle ) ( ( ( P11SessionPtr_t ) xSessionHandle != NULL ) ? PKCS11_SESSION_IS_OPEN( xSessionHandle ) : CKR_SESSION_HANDLE_INVALID )
#define PKCS11_SESSION_VALID_AND_MODULE_INITIALIZED( xSessionHandle ) ( PKCS11_MODULE_IS_INITIALIZED ? PKCS11_SESSION_IS_VALID( xSessionHandle ) : CKR_CRYPTOKI_NOT_INITIALIZED )
/*-----------------------------------------------------------*/
#define pkcs11OBJECT_CERTIFICATE_MAX_SIZE 1728
enum eObjectHandles
{
eInvalidHandle = 0, /* According to PKCS #11 spec, 0 is never a valid object handle. */
eAwsDevicePrivateKey = 1,
eAwsTestPrivateKey,
eAwsDevicePublicKey,
eAwsTestPublicKey,
eAwsDeviceCertificate,
eAwsTestCertificate,
eAwsCodeSigningKey
};
/* The communication context `ifx_i2c_context_0` is declared in the header file ifx_i2c_config.h */
optiga_comms_t optiga_comms = { ( void * ) &ifx_i2c_context_0, NULL, NULL, OPTIGA_COMMS_SUCCESS };
/*-----------------------------------------------------------*/
static uint8_t prvAppendOptigaCertTags( uint16_t xCertWithoutTagsLength,
uint8_t * pxCertTags,
uint16_t xCertTagsLength )
{
char t1[ 3 ], t2[ 3 ], t3[ 3 ];
int xCalc = xCertWithoutTagsLength,
xCalc1 = 0,
xCalc2 = 0;
uint8_t ret = 0;
do
{
if( ( pxCertTags == NULL ) || ( xCertWithoutTagsLength == 0 ) ||
( xCertTagsLength != 9 ) )
{
break;
}
if( xCalc > 0xFF )
{
xCalc1 = xCalc >> 8;
xCalc = xCalc % 0x100;
if( xCalc1 > 0xFF )
{
xCalc2 = xCalc1 >> 8;
xCalc1 = xCalc1 % 0x100;
}
}
t3[ 0 ] = xCalc2;
t3[ 1 ] = xCalc1;
t3[ 2 ] = xCalc;
xCalc = xCertWithoutTagsLength + 3;
if( xCalc > 0xFF )
{
xCalc1 = xCalc >> 8;
xCalc = xCalc % 0x100;
if( xCalc1 > 0xFF )
{
xCalc2 = xCalc1 >> 8;
xCalc1 = xCalc1 % 0x100;
}
}
t2[ 0 ] = xCalc2;
t2[ 1 ] = xCalc1;
t2[ 2 ] = xCalc;
xCalc = xCertWithoutTagsLength + 6;
if( xCalc > 0xFF )
{
xCalc1 = xCalc >> 8;
xCalc = xCalc % 0x100;
if( xCalc1 > 0xFF )
{
xCalc2 = xCalc1 >> 8;
xCalc1 = xCalc1 % 0x100;
}
}
t1[ 0 ] = 0xC0;
t1[ 1 ] = xCalc1;
t1[ 2 ] = xCalc;
for( int i = 0; i < 3; i++ )
{
pxCertTags[ i ] = t1[ i ];
}
for( int i = 0; i < 3; i++ )
{
pxCertTags[ i + 3 ] = t2[ i ];
}
for( int i = 0; i < 3; i++ )
{
pxCertTags[ i + 6 ] = t3[ i ];
}
ret = 1;
} while( 0 );
return ret;
}
static int32_t prvUploadCertificate( char * pucLabel,
uint8_t * pucData,
uint32_t ulDataSize )
{
long lOptigaOid = 0;
const uint8_t xTagsLength = 9;
uint8_t pxCertTags[ xTagsLength ];
optiga_lib_status_t xReturn;
char * xEnd = NULL;
/**
* Write a certificate to a given cert object (e.g. E0E8)
* using optiga_util_write_data.
*
* Use Erase and Write (OPTIGA_UTIL_ERASE_AND_WRITE) option,
* to clear the remaining data in the object
*/
lOptigaOid = strtol( pucLabel, &xEnd, 16 );
if( ( 0 != lOptigaOid ) && ( USHRT_MAX > lOptigaOid ) && ( USHRT_MAX > ulDataSize ) )
{
/* Certificates on OPTIGA Trust SE are stored with certitficate identifiers -> tags, */
/* which are 9 bytes long */
if( prvAppendOptigaCertTags( ulDataSize, pxCertTags, xTagsLength ) )
{
xReturn = optiga_util_write_data( ( uint16_t ) lOptigaOid,
OPTIGA_UTIL_ERASE_AND_WRITE,
0,
pxCertTags,
9 );
if( OPTIGA_LIB_SUCCESS == xReturn )
{
xReturn = optiga_util_write_data( ( uint16_t ) lOptigaOid,
OPTIGA_UTIL_WRITE_ONLY,
xTagsLength,
pucData,
ulDataSize );
}
}
}
return xReturn;
}
static int32_t prvUploadPublicKey( char * pucLabel,
uint8_t * pucData,
uint32_t ulDataSize )
{
long lOptigaOid = 0;
optiga_lib_status_t xReturn;
char * xEnd = NULL;
/**
* Write a public key to an arbitrary data object
* Note: You might need to lock the data object here. see optiga_util_write_metadata()
*
* Use Erase and Write (OPTIGA_UTIL_ERASE_AND_WRITE) option,
* to clear the remaining data in the object
*/
lOptigaOid = strtol( pucLabel, &xEnd, 16 );
if( ( 0 != lOptigaOid ) && ( USHRT_MAX >= lOptigaOid ) && ( USHRT_MAX >= ulDataSize ) )
{
xReturn = optiga_util_write_data( ( uint16_t ) lOptigaOid,
OPTIGA_UTIL_ERASE_AND_WRITE,
0,
pucData,
ulDataSize );
}
return xReturn;
}
/**
* @brief Saves an object in non-volatile storage.
*
* Port-specific file write for cryptographic information.
*
* @param[in] pxLabel The label of the object to be stored.
* @param[in] pucData The object data to be saved
* @param[in] pulDataSize Size (in bytes) of object data.
*
* @return The object handle if successful.
* eInvalidHandle = 0 if unsuccessful.
*/
CK_OBJECT_HANDLE PKCS11_PAL_SaveObject( CK_ATTRIBUTE_PTR pxLabel,
CK_BYTE_PTR pucData,
CK_ULONG ulDataSize )
{
CK_OBJECT_HANDLE xHandle = eInvalidHandle;
long lOptigaOid = 0;
uint8_t bOffset = 0;
char * xEnd = NULL;
optiga_lib_status_t xReturn;
if( ulDataSize <= pkcs11OBJECT_CERTIFICATE_MAX_SIZE )
{
/* Translate from the PKCS#11 label to local storage file name. */
if( 0 == memcmp( pxLabel->pValue,
&pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS,
sizeof( pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS ) ) )
{
if( prvUploadCertificate( pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS, pucData, ulDataSize ) == OPTIGA_LIB_SUCCESS )
{
xHandle = eAwsDeviceCertificate;
}
}
#if FREERTOS_ENABLE_UNIT_TESTS
else if( 0 == memcmp( pxLabel->pValue,
&pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS ) ) )
{
if( prvUploadCertificate( pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS, pucData, ulDataSize ) == OPTIGA_LIB_SUCCESS )
{
xHandle = eAwsTestCertificate;
}
}
#endif
else if( 0 == memcmp( pxLabel->pValue,
&pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS,
sizeof( pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) ) )
{
/* This operation isn't supported for the OPTIGA(TM) Trust X due to a security considerations
* You can only generate a keypair and export a private component if you like */
/* We do assign a handle though, as the AWS can#t handle the lables without having a handle*/
xHandle = eAwsDevicePrivateKey;
}
#if FREERTOS_ENABLE_UNIT_TESTS
else if( 0 == memcmp( pxLabel->pValue,
&pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) ) )
{
xHandle = eAwsTestPrivateKey;
}
#endif
else if( 0 == memcmp( pxLabel->pValue,
&pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS,
sizeof( pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS ) ) )
{
if( prvUploadPublicKey( pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS, pucData, ulDataSize ) == OPTIGA_LIB_SUCCESS )
{
xHandle = eAwsDevicePublicKey;
}
}
#if FREERTOS_ENABLE_UNIT_TESTS
else if( 0 == memcmp( pxLabel->pValue,
&pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS ) ) )
{
if( prvUploadPublicKey( pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS, pucData, ulDataSize ) == OPTIGA_LIB_SUCCESS )
{
xHandle = eAwsTestPublicKey;
}
}
#endif
else if( 0 == memcmp( pxLabel->pValue,
&pkcs11configLABEL_CODE_VERIFICATION_KEY,
sizeof( pkcs11configLABEL_CODE_VERIFICATION_KEY ) ) )
{
/**
* Write a Code Verification Key/Certificate to an Trust Anchor data object
* Note: You might need to lock the data object here. see optiga_util_write_metadata()
*
* Use Erase and Write (OPTIGA_UTIL_ERASE_AND_WRITE) option,
* to clear the remaining data in the object
*/
lOptigaOid = strtol( pkcs11configLABEL_CODE_VERIFICATION_KEY, &xEnd, 16 );
if( ( 0 != lOptigaOid ) && ( USHRT_MAX > lOptigaOid ) && ( USHRT_MAX > ulDataSize ) )
{
xReturn = optiga_util_write_data( ( uint16_t ) lOptigaOid,
OPTIGA_UTIL_ERASE_AND_WRITE,
bOffset,
pucData,
ulDataSize );
if( OPTIGA_LIB_SUCCESS == xReturn )
{
xHandle = eAwsCodeSigningKey;
}
}
}
}
return xHandle;
}
/*-----------------------------------------------------------*/
/**
* @brief Translates a PKCS #11 label into an object handle.
*
* Port-specific object handle retrieval.
*
*
* @param[in] pxLabel Pointer to the label of the object
* who's handle should be found.
* @param[in] usLength The length of the label, in bytes.
*
* @return The object handle if operation was successful.
* Returns eInvalidHandle if unsuccessful.
*/
CK_OBJECT_HANDLE PKCS11_PAL_FindObject( CK_BYTE_PTR pLabel,
CK_ULONG usLength )
{
CK_OBJECT_HANDLE xHandle = eInvalidHandle;
/* Translate from the PKCS#11 label to local storage file name. */
if( 0 == memcmp( pLabel,
&pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS,
sizeof( pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS ) ) )
{
xHandle = eAwsDeviceCertificate;
}
#if FREERTOS_ENABLE_UNIT_TESTS
else if( 0 == memcmp( pLabel,
&pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS ) ) )
{
xHandle = eAwsTestCertificate;
}
#endif
else if( 0 == memcmp( pLabel,
&pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS,
sizeof( pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) ) )
{
/* This operation isn't supported for the OPTIGA(TM) Trust X due to a security considerations
* You can only generate a keypair and export a private component if you like */
/* We do assign a handle though, as the AWS can#t handle the lables without having a handle*/
xHandle = eAwsDevicePrivateKey;
}
#if FREERTOS_ENABLE_UNIT_TESTS
else if( 0 == memcmp( pLabel,
&pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS ) ) )
{
xHandle = eAwsTestPrivateKey;
}
#endif
else if( 0 == memcmp( pLabel,
&pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS,
sizeof( pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS ) ) )
{
xHandle = eAwsDevicePublicKey;
}
#if FREERTOS_ENABLE_UNIT_TESTS
else if( 0 == memcmp( pLabel,
&pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS,
sizeof( pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS ) ) )
{
xHandle = eAwsTestPublicKey;
}
#endif
else if( 0 == memcmp( pLabel,
&pkcs11configLABEL_CODE_VERIFICATION_KEY,
sizeof( pkcs11configLABEL_CODE_VERIFICATION_KEY ) ) )
{
xHandle = eAwsCodeSigningKey;
}
return xHandle;
}
/*-----------------------------------------------------------*/
/**
* @brief Gets the value of an object in storage, by handle.
*
* Port-specific file access for cryptographic information.
*
* This call dynamically allocates the buffer which object value
* data is copied into. PKCS11_PAL_GetObjectValueCleanup()
* should be called after each use to free the dynamically allocated
* buffer.
*
* @sa PKCS11_PAL_GetObjectValueCleanup
*
* @param[in] pcFileName The name of the file to be read.
* @param[out] ppucData Pointer to buffer for file data.
* @param[out] pulDataSize Size (in bytes) of data located in file.
* @param[out] pIsPrivate Boolean indicating if value is private (CK_TRUE)
* or exportable (CK_FALSE)
*
* @return CKR_OK if operation was successful. CKR_KEY_HANDLE_INVALID if
* no such object handle was found, CKR_DEVICE_MEMORY if memory for
* buffer could not be allocated, CKR_FUNCTION_FAILED for device driver
* error.
*/
CK_RV PKCS11_PAL_GetObjectValue( CK_OBJECT_HANDLE xHandle,
CK_BYTE_PTR * ppucData,
CK_ULONG_PTR pulDataSize,
CK_BBOOL * pIsPrivate )
{
BaseType_t ulReturn = CKR_OK;
optiga_lib_status_t xReturn;
long lOptigaOid = 0;
char * xEnd = NULL;
uint8_t xOffset = 0;
*pIsPrivate = CK_FALSE;
switch( xHandle )
{
case eAwsDeviceCertificate:
lOptigaOid = strtol( pkcs11configLABEL_DEVICE_CERTIFICATE_FOR_TLS, &xEnd, 16 );
xOffset = 9;
break;
#if FREERTOS_ENABLE_UNIT_TESTS
case eAwsTestCertificate:
lOptigaOid = strtol( pkcs11testLABEL_DEVICE_CERTIFICATE_FOR_TLS, &xEnd, 16 );
xOffset = 9;
break;
#endif
case eAwsDevicePublicKey:
lOptigaOid = strtol( pkcs11configLABEL_DEVICE_PUBLIC_KEY_FOR_TLS, &xEnd, 16 );
break;
#if FREERTOS_ENABLE_UNIT_TESTS
case eAwsTestPublicKey:
lOptigaOid = strtol( pkcs11testLABEL_DEVICE_PUBLIC_KEY_FOR_TLS, &xEnd, 16 );
break;
#endif
case eAwsCodeSigningKey:
lOptigaOid = strtol( pkcs11configLABEL_CODE_VERIFICATION_KEY, &xEnd, 16 );
break;
case eAwsDevicePrivateKey:
case eAwsTestPrivateKey:
/*
* This operation isn't supported for the OPTIGA(TM) Trust X due to a security considerations
* You can only generate a keypair and export a private component if you like
*/
default:
ulReturn = CKR_KEY_HANDLE_INVALID;
break;
}
/* We need to allocate a buffer for a certificate/certificate chain */
/* This data is later should be freed with PKCS11_PAL_GetObjectValueCleanup */
*ppucData = pvPortMalloc( pkcs11OBJECT_CERTIFICATE_MAX_SIZE );
if( NULL != *ppucData )
{
*pulDataSize = pkcs11OBJECT_CERTIFICATE_MAX_SIZE;
if( ( 0 != lOptigaOid ) && ( USHRT_MAX > lOptigaOid ) && ( NULL != pulDataSize ) )
{
xReturn = optiga_util_read_data( lOptigaOid, xOffset, *ppucData, ( uint16_t * ) pulDataSize );
if( OPTIGA_LIB_SUCCESS != xReturn )
{
*ppucData = NULL;
*pulDataSize = 0;
ulReturn = CKR_KEY_HANDLE_INVALID;
}
}
}
return ulReturn;
}
/*-----------------------------------------------------------*/
/**
* @brief Cleanup after PKCS11_GetObjectValue().
*
* @param[in] pucData The buffer to free.
* (*ppucData from PKCS11_PAL_GetObjectValue())
* @param[in] ulDataSize The length of the buffer to free.
* (*pulDataSize from PKCS11_PAL_GetObjectValue())
*/
void PKCS11_PAL_GetObjectValueCleanup( CK_BYTE_PTR pucData,
CK_ULONG ulDataSize )
{
/* Unused parameters. */
vPortFree( pucData );
/* Since no buffer was allocated on heap, there is no cleanup
* to be done. */
}
/**
* @brief Maps an opaque caller session handle into its internal state structure.
*/
P11SessionPtr_t prvSessionPointerFromHandle( CK_SESSION_HANDLE xSession )
{
return ( P11SessionPtr_t ) xSession; /*lint !e923 Allow casting integer type to pointer for handle. */
}
/*
* PKCS#11 module implementation.
*/
/**
* @brief PKCS#11 interface functions implemented by this Cryptoki module.
*/
static CK_FUNCTION_LIST prvP11FunctionList =
{
{ CRYPTOKI_VERSION_MAJOR, CRYPTOKI_VERSION_MINOR },
C_Initialize,
C_Finalize,
NULL, /*C_GetInfo */
C_GetFunctionList,
C_GetSlotList,
NULL, /*C_GetSlotInfo*/
C_GetTokenInfo,
NULL, /*C_GetMechanismList*/
C_GetMechanismInfo, /*C_GetMechanismInfo */
C_InitToken,
NULL, /*C_InitPIN*/
NULL, /*C_SetPIN*/
C_OpenSession,
C_CloseSession,
NULL, /*C_CloseAllSessions*/
NULL, /*C_GetSessionInfo*/
NULL, /*C_GetOperationState*/
NULL, /*C_SetOperationState*/
C_Login, /*C_Login*/
NULL, /*C_Logout*/
C_CreateObject,
NULL, /*C_CopyObject*/
C_DestroyObject,
NULL, /*C_GetObjectSize*/
C_GetAttributeValue,
NULL, /*C_SetAttributeValue*/
C_FindObjectsInit,
C_FindObjects,
C_FindObjectsFinal,
NULL, /*C_EncryptInit*/
NULL, /*C_Encrypt*/
NULL, /*C_EncryptUpdate*/
NULL, /*C_EncryptFinal*/
NULL, /*C_DecryptInit*/
NULL, /*C_Decrypt*/
NULL, /*C_DecryptUpdate*/
NULL, /*C_DecryptFinal*/
C_DigestInit,
NULL, /*C_Digest*/
C_DigestUpdate,
NULL, /* C_DigestKey*/
C_DigestFinal,
C_SignInit,
C_Sign,
NULL, /*C_SignUpdate*/
NULL, /*C_SignFinal*/
NULL, /*C_SignRecoverInit*/
NULL, /*C_SignRecover*/
C_VerifyInit,
C_Verify,
NULL, /*C_VerifyUpdate*/
NULL, /*C_VerifyFinal*/
NULL, /*C_VerifyRecoverInit*/
NULL, /*C_VerifyRecover*/
NULL, /*C_DigestEncryptUpdate*/
NULL, /*C_DecryptDigestUpdate*/
NULL, /*C_SignEncryptUpdate*/
NULL, /*C_DecryptVerifyUpdate*/
NULL, /*C_GenerateKey*/
C_GenerateKeyPair,
NULL, /*C_WrapKey*/
NULL, /*C_UnwrapKey*/
NULL, /*C_DeriveKey*/
NULL, /*C_SeedRandom*/
C_GenerateRandom,
NULL, /*C_GetFunctionStatus*/
NULL, /*C_CancelFunction*/
NULL /*C_WaitForSlotEvent*/
};
/*-----------------------------------------------------------*/
/*-----------------------------------------------------------*/
CK_RV prvOPTIGATrustX_Initialize( void )
{
CK_RV xResult = CKR_OK;
uint16_t optiga_oid;
if( xP11Context.xIsInitialized == CK_TRUE )
{
xResult = CKR_CRYPTOKI_ALREADY_INITIALIZED;
}
if( xResult == CKR_OK )
{
memset( &xP11Context, 0, sizeof( xP11Context ) );
xP11Context.xObjectList.xMutex = xSemaphoreCreateMutex();
/* CRYPTO_Init(); */
pal_os_event_init();
xResult = optiga_util_open_application( &optiga_comms );
/*status = example_authenticate_chip(); */
if( OPTIGA_LIB_SUCCESS == xResult )
{
xP11Context.xIsInitialized = CK_TRUE;
xResult = CKR_OK;
optiga_oid = eCURRENT_LIMITATION;
/* Maximum Power, Minimum Current limitation */
uint8_t current_limit = 15;
optiga_util_write_data( optiga_oid, OPTIGA_UTIL_WRITE_ONLY,
0, /*offset */
¤t_limit,
1 );
}
else
{
xResult = CKR_FUNCTION_FAILED;
}
}
return xResult;
}
CK_RV prvGetObjectClass( CK_ATTRIBUTE_PTR pxTemplate,
CK_ULONG ulCount,
CK_OBJECT_CLASS * pxClass )
{
CK_RV xResult = CKR_TEMPLATE_INCOMPLETE;
uint32_t ulIndex = 0;
/* Search template for class attribute. */
for( ulIndex = 0; ulIndex < ulCount; ulIndex++ )
{
CK_ATTRIBUTE xAttribute = pxTemplate[ ulIndex ];
if( xAttribute.type == CKA_CLASS )
{
memcpy( pxClass, xAttribute.pValue, sizeof( CK_OBJECT_CLASS ) );
xResult = CKR_OK;
break;
}
}
return xResult;
}
/*-----------------------------------------------------------------------*/
/* Functions for maintaining the PKCS #11 module's label-handle lookups. */
/*-----------------------------------------------------------------------*/
/**
* @brief Searches the PKCS #11 module's object list for label and provides handle.
*
* @param[in] pcLabel Array containing label.
* @param[in] xLableLength Length of the label, in bytes.
* @param[out] pxPalHandle Pointer to the PAL handle to be provided.
* CK_INVALID_HANDLE if no object found.
* @param[out] pxAppHandle Pointer to the application handle to be provided.
* CK_INVALID_HANDLE if no object found.
*/
void prvFindObjectInListByLabel( uint8_t * pcLabel,
size_t xLabelLength,
CK_OBJECT_HANDLE_PTR pxPalHandle,
CK_OBJECT_HANDLE_PTR pxAppHandle )
{
uint8_t ucIndex;
*pxPalHandle = CK_INVALID_HANDLE;
*pxAppHandle = CK_INVALID_HANDLE;
for( ucIndex = 0; ucIndex < pkcs11configMAX_NUM_OBJECTS; ucIndex++ )
{
if( 0 == memcmp( pcLabel, xP11Context.xObjectList.xObjects[ ucIndex ].xLabel, xLabelLength ) )
{
*pxPalHandle = xP11Context.xObjectList.xObjects[ ucIndex ].xHandle;
*pxAppHandle = ucIndex + 1; /* Zero is not a valid handle, so let's offset by 1. */
break;
}
}
}
/**
* @brief Searches the PKCS #11 module's object list for handle and provides label info.
*
* @param[in] xAppHandle The handle of the object being searched for, used by the application.
* @param[out] xPalHandle The handle of the object being used by the PAL.
* @param[out] ppcLabel Pointer to an array containing label. NULL if object not found.
* @param[out] pxLabelLength Pointer to label length (includes a string null terminator).
* 0 if no object found.
*/
void prvFindObjectInListByHandle( CK_OBJECT_HANDLE xAppHandle,
CK_OBJECT_HANDLE_PTR pxPalHandle,
uint8_t ** ppcLabel,
size_t * pxLabelLength )
{
int lIndex = xAppHandle - 1;
*ppcLabel = NULL;
*pxLabelLength = 0;
if( pkcs11configMAX_NUM_OBJECTS >= lIndex )
{
if( xP11Context.xObjectList.xObjects[ lIndex ].xHandle != CK_INVALID_HANDLE )
{
*ppcLabel = xP11Context.xObjectList.xObjects[ lIndex ].xLabel;
*pxLabelLength = strlen( ( const char * ) xP11Context.xObjectList.xObjects[ lIndex ].xLabel ) + 1;
*pxPalHandle = xP11Context.xObjectList.xObjects[ lIndex ].xHandle;
}
}
else
{
*ppcLabel = NULL;
*pxLabelLength = 0;
pxPalHandle = NULL;
}
}
CK_RV prvDeleteObjectFromList( CK_OBJECT_HANDLE xAppHandle )
{
CK_RV xResult = CKR_OK;
BaseType_t xGotSemaphore;
int lIndex = xAppHandle - 1;
xGotSemaphore = xSemaphoreTake( xP11Context.xObjectList.xMutex, portMAX_DELAY );
if( xGotSemaphore == pdTRUE )
{
if( xP11Context.xObjectList.xObjects[ lIndex ].xHandle != CK_INVALID_HANDLE )
{
memset( &xP11Context.xObjectList.xObjects[ lIndex ], 0, sizeof( P11Object_t ) );
}
else
{
xResult = CKR_OBJECT_HANDLE_INVALID;
}
xSemaphoreGive( xP11Context.xObjectList.xMutex );
}
else
{
xResult = CKR_CANT_LOCK;
}
return xResult;
}
CK_RV prvAddObjectToList( CK_OBJECT_HANDLE xPalHandle,
CK_OBJECT_HANDLE_PTR pxAppHandle,
uint8_t * pcLabel,
size_t xLabelLength )
{
CK_RV xResult = CKR_OK;
BaseType_t xGotSemaphore;
CK_BBOOL xObjectFound = CK_FALSE;
int lInsertIndex = -1;
int lSearchIndex = pkcs11configMAX_NUM_OBJECTS - 1;
xGotSemaphore = xSemaphoreTake( xP11Context.xObjectList.xMutex, portMAX_DELAY );
if( xGotSemaphore == pdTRUE )
{
for( lSearchIndex = pkcs11configMAX_NUM_OBJECTS - 1; lSearchIndex >= 0; lSearchIndex-- )
{
if( xP11Context.xObjectList.xObjects[ lSearchIndex ].xHandle == xPalHandle )
{
/* Object already exists in list. */
xObjectFound = CK_TRUE;
break;
}
else if( xP11Context.xObjectList.xObjects[ lSearchIndex ].xHandle == CK_INVALID_HANDLE )
{
lInsertIndex = lSearchIndex;
}
}
if( xObjectFound == CK_FALSE )
{
if( lInsertIndex != -1 )
{
if( xLabelLength < pkcs11configMAX_LABEL_LENGTH )
{
xP11Context.xObjectList.xObjects[ lInsertIndex ].xHandle = xPalHandle;
memcpy( xP11Context.xObjectList.xObjects[ lInsertIndex ].xLabel, pcLabel, xLabelLength );
*pxAppHandle = lInsertIndex + 1;
}
else
{
xResult = CKR_DATA_LEN_RANGE;
}
}
}
xSemaphoreGive( xP11Context.xObjectList.xMutex );
}
else
{
xResult = CKR_CANT_LOCK;
}
return xResult;
}
#if ( pkcs11configPAL_DESTROY_SUPPORTED != 1 )
CK_RV PKCS11_PAL_DestroyObject( CK_OBJECT_HANDLE xAppHandle )
{
uint8_t * pcLabel = NULL;
char * pcTempLabel = NULL;
size_t xLabelLength = 0;
uint32_t ulObjectLength = 0;
CK_RV xResult = CKR_OK;
CK_BBOOL xFreeMemory = CK_FALSE;
CK_BYTE_PTR pxObject = NULL;
CK_OBJECT_HANDLE xPalHandle;
CK_OBJECT_HANDLE xAppHandle2;
CK_LONG lOptigaOid = 0;
char * xEnd = NULL;
prvFindObjectInListByHandle( xAppHandle, &xPalHandle, &pcLabel, &xLabelLength );
if( pcLabel != NULL )
{
if( ( 0 == memcmp( pcLabel, pkcs11configLABEL_DEVICE_PRIVATE_KEY_FOR_TLS, xLabelLength ) )
#if FREERTOS_ENABLE_UNIT_TESTS
||
( 0 == memcmp( pcLabel, pkcs11testLABEL_DEVICE_PRIVATE_KEY_FOR_TLS, xLabelLength ) )
#endif
)
{
prvFindObjectInListByLabel( ( uint8_t * ) pcLabel, strlen( ( char * ) pcLabel ), &xPalHandle, &xAppHandle2 );
if( xPalHandle != CK_INVALID_HANDLE )
{
xResult = prvDeleteObjectFromList( xAppHandle2 );
}
lOptigaOid = strtol( ( char * ) pcLabel, &xEnd, 16 );
CK_BYTE pucDumbData[ 68 ];
uint16_t ucDumbDataLength = 68;
if( OPTIGA_LIB_SUCCESS != optiga_crypt_ecc_generate_keypair( OPTIGA_ECC_NIST_P_256,