-
Notifications
You must be signed in to change notification settings - Fork 17
/
pkcs11.c
7940 lines (7039 loc) · 273 KB
/
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
/*
* Amazon FreeRTOS PKCS#11 for A71CH V1.0.0
* Copyright (C) 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
* Copyright 2018 NXP
*
* 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
*/
#if defined(SSS_USE_FTR_FILE)
#include "fsl_sss_ftr.h"
#else
#include "fsl_sss_ftr_default.h"
#endif
#if (SSS_HAVE_SSCP || SSS_HAVE_APPLET_SE05X_IOT)
/* FreeRTOS includes. */
#if defined(USE_RTOS) && USE_RTOS == 1
#include "FreeRTOS.h"
#include "semphr.h"
#include "task.h"
#endif
#include "core_pkcs11.h"
/* mbedTLS includes. */
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#include "mbedtls/base64.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
#include "mbedtls/oid.h"
#include "mbedtls/pk.h"
#include "mbedtls/pk_internal.h"
#include "mbedtls/sha256.h"
#include "mbedtls/x509_crt.h"
#include "pkcs11_mbedtls_utils.h"
/*Other includes */
#include <nxEnsure.h>
#include <nxLog_App.h>
#include "HLSEAPI.h"
#include "ex_sss.h"
#if SSS_HAVE_ALT_A71CH
#include "ax_mbedtls.h"
#endif
#if SSS_HAVE_ALT_SSS
#include "sss_mbedtls.h"
#endif
#if SSS_HAVE_APPLET_SE05X_IOT
#include <fsl_sss_se05x_apis.h>
#include <se05x_APDU.h>
#include <se05x_const.h>
#include "se05x_APDU_apis.h"
#include "se05x_enums.h"
#endif
#if defined(PKCS11_LIBRARY)
#include <projdefs.h>
#if (__GNUC__ && !AX_EMBEDDED)
#include <errno.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
/* Only for base session with os */
static pthread_mutex_t gSessionlock;
static pthread_mutex_t gFilelock;
#endif
#endif
#if defined(USE_RTOS) && USE_RTOS == 1
static SemaphoreHandle_t xSemaphore;
#endif
/* C runtime includes. */
#include <PlugAndTrust_Pkg_Ver.h>
#include <fsl_sss_util_asn1_der.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Remove this later */
#include <ex_sss_boot.h>
extern ex_sss_boot_ctx_t *pex_sss_demo_boot_ctx;
extern ex_sss_cloud_ctx_t *pex_sss_demo_tls_ctx;
extern const char *g_port_name;
#undef DEBUG_PKCS11_PAL
#define PKCS11_TOKEN_LABEL \
{ \
'S', 'S', 'S', '_', 'P', 'K', 'C', 'S', '1', '1' \
}
#define PKCS11_MANUFACTURER \
{ \
'N', 'X', 'P' \
}
#define PKCS11_LIBRARY_VERSION \
(CK_VERSION) \
{ \
.major = PLUGANDTRUST_VER_MAJOR, .minor = PLUGANDTRUST_VER_MINOR, \
}
#define pkcs11SLOT_ID 1
#define pkcs11OBJECT_HANDLE_PUBLIC_KEY 1
#define pkcs11OBJECT_HANDLE_PRIVATE_KEY 2
#define pkcs11OBJECT_HANDLE_CERTIFICATE 3
#define pkcs11OBJECT_HANDLE_SYMMETRIC 4
#define SIZE_CLIENT_CERTIFICATE 500
#define SIZE_INTER_ROOT_CERTIFICATE 900
#define CKA_SSS_ID CKA_VENDOR_DEFINED + CKA_OBJECT_ID
/**
* @brief Definitions for parameter checking
*/
#define pkcs11CREATEOBJECT_MINIMUM_ATTRIBUTE_COUNT 2
/**
* Defines for the set key pair through create object
*/
#define pkcs11CREATEOBJECT_SET_KEYPAIR_LENGTH 138
#define pkcs11CREATEOBJECT_SET_PUBKEY_LENGTH 65
#define pkcs11CREATEOBJECT_PUBLIC_KEY_INDEX 32
#define pkcs11NO_OPERATION ((CK_MECHANISM_TYPE)0xFFFFFFFFF)
#define pkcs11INVALID_OBJECT_CLASS ((CK_OBJECT_CLASS)0x0FFFFFFF)
#define pkcs11INVALID_KEY_TYPE ((CK_KEY_TYPE)0x0FFFFFFF)
#if defined(USE_RTOS) && USE_RTOS == 1
#define LOCK_MUTEX_FOR_RTOS if (xSemaphoreTake(xSemaphore, portMAX_DELAY) == pdTRUE)
#else
#define LOCK_MUTEX_FOR_RTOS
#endif
#if defined(USE_RTOS) && USE_RTOS == 1
#define UNLOCK_MUTEX_FOR_RTOS_RET(RET) \
xSemaphoreGive(xSemaphore); \
return RET;
#else
#define UNLOCK_MUTEX_FOR_RTOS_RET(RET) return RET;
#endif
#if defined(USE_RTOS) && USE_RTOS == 1
#define UNLOCK_MUTEX_FOR_RTOS xSemaphoreGive(xSemaphore);
#else
#define UNLOCK_MUTEX_FOR_RTOS
#endif
#if defined(USE_RTOS) && USE_RTOS == 1
#define UNLOCK_MUTEX_FOR_RTOS_EXIT_ON_FAIL(CONDITION) \
if (!(CONDITION)) { \
NX_ENSURE_MESSAGE(#CONDITION); \
xSemaphoreGive(xSemaphore); \
goto exit; \
}
#else
#define UNLOCK_MUTEX_FOR_RTOS_EXIT_ON_FAIL(CONDITION) ENSURE_OR_GO_EXIT(CONDITION)
#endif
#define ASN1_SKIP_TO_NEXT_TAG(pTLV, taglen) \
{ \
if (taglen < 0x7F) { \
pTLV += taglen + 1 + 1 /* Length byte */; \
} \
else if (taglen < 0xFF) { \
pTLV += taglen + 1 + 2 /* Length bytes */; \
} \
else { \
pTLV += taglen + 1 + 3 /* Length bytes */; \
} \
}
/**
* Defines OpenSC NON_REPUDIATION attribute
*/
#define SC_VENDOR_DEFINED 0x4F534300 /* OSC */
// CKA_OPENSC_NON_REPUDIATION for OpenSC 0.17
#define CKA_OPENSC_NON_REPUDIATION_0_17 (CKA_VENDOR_DEFINED | 1UL)
// CKA_OPENSC_NON_REPUDIATION for OpenSC 0.21
#define CKA_OPENSC_NON_REPUDIATION_0_21 (CKA_VENDOR_DEFINED | SC_VENDOR_DEFINED | 1UL)
// uint8_t nist_header_start[] = { 0x30, 0x81, 0x87, 0x02, 0x01, 0x00, 0x30, 0x13,
// 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02,
// 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
// 0x03, 0x01, 0x07, 0x04, 0x6D, 0x30, 0x6B, 0x02,
// 0x01, 0x01, 0x04, 0x20};
// uint8_t nist_header_end[] = {0xA1, 0x44, 0x03, 0x42, 0x00 };
#define DEFAULT_POLICY_SYMM_KEY \
(POLICY_OBJ_ALLOW_DELETE | POLICY_OBJ_ALLOW_SIGN | POLICY_OBJ_ALLOW_VERIFY | POLICY_OBJ_ALLOW_ENC | \
POLICY_OBJ_ALLOW_DEC | POLICY_OBJ_ALLOW_KDF | POLICY_OBJ_ALLOW_WRAP | POLICY_OBJ_ALLOW_WRITE | \
POLICY_OBJ_ALLOW_GEN | POLICY_OBJ_ALLOW_DESFIRE_AUTHENTICATION | POLICY_OBJ_ALLOW_DESFIRE_DUMP_SESSION_KEYS | \
POLICY_OBJ_ALLOW_IMPORT_EXPORT)
#define DEFAULT_POLICY_ASYMM_KEY \
(POLICY_OBJ_ALLOW_DELETE | POLICY_OBJ_ALLOW_SIGN | POLICY_OBJ_ALLOW_VERIFY | POLICY_OBJ_ALLOW_ENC | \
POLICY_OBJ_ALLOW_DEC | POLICY_OBJ_ALLOW_KDF | POLICY_OBJ_ALLOW_WRAP | POLICY_OBJ_ALLOW_WRITE | \
POLICY_OBJ_ALLOW_GEN | POLICY_OBJ_ALLOW_KA | POLICY_OBJ_ALLOW_READ | POLICY_OBJ_ALLOW_IMPORT_EXPORT)
#define DEFAULT_POLICY_BIN_COUNT_PCR (POLICY_OBJ_ALLOW_DELETE | POLICY_OBJ_ALLOW_WRITE | POLICY_OBJ_ALLOW_READ)
#define DEFAULT_POLICY_USERID (POLICY_OBJ_ALLOW_DELETE | POLICY_OBJ_ALLOW_WRITE)
static int sessionCount = 0;
static bool cryptokiInitialized = false;
static bool mutex_initialised = false;
// #if (__GNUC__ && !AX_EMBEDDED)
// #endif
typedef int (*pfnMbedTlsSign)(void *ctx,
mbedtls_md_type_t md_alg,
const unsigned char *hash,
size_t hash_len,
unsigned char *sig,
size_t *sig_len,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng);
/**
* @brief Key structure.
*/
typedef struct P11Key
{
mbedtls_pk_context xMbedPkCtx;
mbedtls_x509_crt xMbedX509Cli;
mbedtls_pk_info_t xMbedPkInfo;
pfnMbedTlsSign pfnSavedMbedSign;
void *pvSavedMbedPkCtx;
uint16_t pem_len_client;
uint16_t pem_len_client_ca;
unsigned char *certificate_buf; //[2500];
} P11Key_t, *P11KeyPtr_t;
/**
* @brief Session structure.
*/
typedef struct P11Session
{
P11KeyPtr_t pxCurrentKey;
CK_ULONG ulState;
CK_BBOOL xOpened;
CK_MECHANISM_TYPE xOperationInProgress;
CK_BBOOL digestUpdateCalled;
CK_OBJECT_HANDLE xOperationKeyHandle;
CK_BBOOL xFindObjectInit;
CK_BBOOL xFindObjectComplete;
CK_OBJECT_CLASS xFindObjectClass;
uint32_t xFindObjectTotalFound;
CK_KEY_TYPE xFindObjectKeyType;
mbedtls_ctr_drbg_context xMbedDrbgCtx;
mbedtls_entropy_context xMbedEntropyContext;
mbedtls_pk_context xPublicKey;
mbedtls_sha256_context xSHA256Context;
CK_BBOOL labelPresent;
CK_BBOOL keyIdPresent;
uint32_t keyId;
char label[32];
size_t labelLen;
void *mechParameter;
CK_ULONG mechParameterLen;
#if SSS_HAVE_ALT_SSS
sss_digest_t digest_ctx;
#endif
} P11Session_t, *P11SessionPtr_t;
#if SSS_HAVE_SE05X_VER_GTE_06_00
/**
* SE05x Attribute.
*/
typedef struct _se05x_object_attribute
{
uint32_t obj_id; /**< Object identifier. */
uint8_t obj_type; /**< Object type. */
SE05x_SetIndicator_t auth_attr; /**< Authentication attribute. */
uint16_t auth_attempts; /**< Authentication attempts counter. Only for Authentication Object*/
uint16_t min_AEAD_tag_len; /**< Minimum tag length for AEAD operations. Only for non-Authentication Object*/
uint32_t auth_obj_id; /**< Authentication object identifier */
uint16_t max_attempts; /**< Maximum authentication attempts */
SE05x_Origin_t origin; /**< The origin of the Secure Object */
uint32_t version; /**< The Secure Object version. */
uint32_t policy_num; /**< Number of policy */
} se05x_object_attribute;
#endif
/*-----------------------------------------------------------*/
// static void prvFreeKey( P11KeyPtr_t pxKey );
// static int awsRetreieve_certificate(unsigned char *pbuf, uint8_t index, uint32_t *outbuflen);
// static int convertdertopemformat(const unsigned char * int_ca_cert_der, int len_der, unsigned char * int_ca_cert_pem, int dst_buf_len, U16 *outlength);
CK_RV ParseSignMechanism(P11SessionPtr_t pxSession, sss_algorithm_t *algorithm);
CK_RV ParseEncryptionMechanism(P11SessionPtr_t pxSession, sss_algorithm_t *algorithm);
CK_RV ParseDigestMechanism(P11SessionPtr_t pxSession, sss_algorithm_t *algorithm);
CK_RV AsymmetricEncrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData,
CK_ULONG_PTR pulEncryptedDataLen);
CK_RV AsymmetricDecrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pEncryptedData,
CK_ULONG ulEncryptedDataLen,
CK_BYTE_PTR pData,
CK_ULONG_PTR pulDataLen);
CK_RV SymmetricEncrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData,
CK_ULONG_PTR pulEncryptedDataLen);
CK_RV SymmetricDecrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData,
CK_ULONG_PTR pulEncryptedDataLen);
CK_RV GetAttributeParameterIndex(
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_ATTRIBUTE_TYPE type, CK_ULONG_PTR index);
CK_RV LabelToKeyId(unsigned char *label, size_t labelSize, uint32_t *keyId);
CK_RV CreateRawPrivateKey(CK_ATTRIBUTE_PTR pxTemplate, CK_ULONG ulCount, uint8_t *key, size_t *keyLen);
CK_RV CreateRawPublicKey(CK_ATTRIBUTE_PTR pxTemplate, CK_ULONG ulCount, uint8_t *key, size_t *keyLen);
CK_RV EcSignatureToRandS(uint8_t *signature, size_t *sigLen);
CK_RV EcRandSToSignature(uint8_t *rands, const size_t rands_len, uint8_t *output, size_t *outputLen);
CK_RV EcPublickeyGetEcParams(uint8_t *input, size_t *dataLen);
CK_RV parseCertificateGetAttribute(
uint32_t xObject, CK_ATTRIBUTE_TYPE attributeType, uint8_t *pData, CK_ULONG *ulAttrLength);
CK_BBOOL isX509Certificate(uint32_t xObject);
#if SSS_HAVE_ALT_A71CH
static U16 HLSE_Create_token(
uint32_t keyId, HLSE_OBJECT_TYPE objType, void *buff, unsigned long bufflen, HLSE_OBJECT_HANDLE handle_object);
static mbedtls_ecp_group_id curve_list[16] = {
0,
};
#endif
#if SSS_HAVE_APPLET_SE05X_IOT
static smStatus_t read_id_list(uint32_t *idlist, size_t *idlistlen);
static CK_RV read_object_size_with_lock(uint32_t keyId, uint16_t *keyLen);
static CK_RV read_object_size(uint32_t keyId, uint16_t *keyLen);
static uint8_t CheckIfKeyIdExists(uint32_t keyId, pSe05xSession_t session_ctx);
#endif
#if SSS_HAVE_ALT_SSS
static int convertpemtoder(const unsigned char *input, size_t ilen, unsigned char *output, size_t *olen);
static sss_status_t sss_create_token(sss_key_store_t *keystore,
sss_object_t *CreateObject,
U32 ObjectId,
sss_key_part_t KeyPart,
sss_cipher_type_t CipherType,
U8 *buffer,
U32 bufferLen,
U32 bitLen);
static mbedtls_ecp_group_id curve_list[16] = {
0,
};
#endif
#define ID_ECPUBLICKEY \
{ \
0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01 \
}
typedef struct _object_identifiers
{
uint32_t identifier[16];
size_t indentifier_len;
int groupId;
char *name;
} object_identifiers_t;
static object_identifiers_t object_identifiers[] = {
{{1, 2, 840, 10045, 3, 1, 1}, 7, MBEDTLS_ECP_DP_SECP192R1, "MBEDTLS_ECP_DP_SECP192R1"},
{{1, 3, 132, 0, 33}, 5, MBEDTLS_ECP_DP_SECP224R1, "MBEDTLS_ECP_DP_SECP224R1"},
{{1, 2, 840, 10045, 3, 1, 7}, 7, MBEDTLS_ECP_DP_SECP256R1, "MBEDTLS_ECP_DP_SECP256R1"},
{{1, 3, 132, 0, 34}, 5, MBEDTLS_ECP_DP_SECP384R1, "MBEDTLS_ECP_DP_SECP384R1"},
{{1, 3, 132, 0, 35}, 5, MBEDTLS_ECP_DP_SECP521R1, "MBEDTLS_ECP_DP_SECP521R1"},
{{1, 3, 36, 3, 3, 2, 8, 1, 1, 7}, 10, MBEDTLS_ECP_DP_BP256R1, "MBEDTLS_ECP_DP_BP256R1"},
{{1, 3, 24, 3, 3, 2, 8, 1, 1, 7}, 10, MBEDTLS_ECP_DP_BP256R1, "MBEDTLS_ECP_DP_BP256R1"},
{{1, 3, 36, 3, 3, 2, 8, 1, 1, 11}, 10, MBEDTLS_ECP_DP_BP384R1, "MBEDTLS_ECP_DP_BP384R1"},
{{1, 3, 36, 3, 3, 2, 8, 1, 1, 13}, 10, MBEDTLS_ECP_DP_BP512R1, "MBEDTLS_ECP_DP_BP512R1"},
{{1, 3, 132, 0, 31}, 5, MBEDTLS_ECP_DP_SECP192K1, "MBEDTLS_ECP_DP_SECP192K1"},
{{1, 3, 132, 0, 32}, 5, MBEDTLS_ECP_DP_SECP224K1, "MBEDTLS_ECP_DP_SECP224K1"},
{{1, 3, 132, 0, 10}, 5, MBEDTLS_ECP_DP_SECP256K1, "MBEDTLS_ECP_DP_SECP256K1"},
{
{
0,
},
0,
0,
},
};
#if SSS_HAVE_ALT
// int mbedtls_ssl_set_curve_list(mbedtls_ssl_config *conf, const char * pcLabelName)
int mbedtls_ssl_set_curve_list(mbedtls_ssl_config *conf, uint32_t keyIndex)
{
sss_status_t status = kStatus_SSS_Fail;
sss_object_t clientKey = {
0,
};
uint32_t objectId[16] = {
0,
};
uint8_t objectIdLen = sizeof(objectId);
int i = 0;
size_t compareLen = 0;
// uint32_t keyIndex = 0;
// LabelToKeyId((unsigned char*)pcLabelName, strlen(pcLabelName), &keyIndex);
// printf(" \n\n%s\n\n", __FUNCTION__);
LOCK_MUTEX_FOR_RTOS
{
status = sss_key_object_init(&clientKey, &pex_sss_demo_boot_ctx->ks);
if (status != kStatus_SSS_Success) {
UNLOCK_MUTEX_FOR_RTOS_RET(kStatus_SSS_Fail);
// return kStatus_SSS_Fail;
}
status = sss_key_object_get_handle(&clientKey, keyIndex);
if (status != kStatus_SSS_Success) {
UNLOCK_MUTEX_FOR_RTOS_RET(kStatus_SSS_Fail);
// return kStatus_SSS_Fail;
}
status = sss_util_asn1_get_oid_from_sssObj(&clientKey, objectId, &objectIdLen);
if (status != kStatus_SSS_Success) {
UNLOCK_MUTEX_FOR_RTOS_RET(1);
// return 1;
}
UNLOCK_MUTEX_FOR_RTOS
}
while (1) {
if (object_identifiers[i].indentifier_len == 0) {
break;
}
if (object_identifiers[i].indentifier_len != objectIdLen) {
i++;
continue;
}
if (objectIdLen * sizeof(uint32_t) > 64) {
compareLen = 64;
}
else {
compareLen = objectIdLen * sizeof(uint32_t);
}
if (0 == memcmp(object_identifiers[i].identifier, objectId, compareLen)) {
curve_list[0] = object_identifiers[i].groupId;
curve_list[1] = MBEDTLS_ECP_DP_NONE;
mbedtls_ssl_conf_curves(conf, curve_list);
return 0;
}
i++;
}
return 1;
}
#endif //SSS_HAVE_ALT
/**
* @brief Maps an opaque caller session handle into its internal state structure.
*/
static P11SessionPtr_t prvSessionPointerFromHandle(CK_SESSION_HANDLE xSession)
{
return (P11SessionPtr_t)xSession; /*lint !e923 Allow casting integer type to pointer for handle. */
}
/*-----------------------------------------------------------*/
/**
* @brief Initializes a key structure.
*/
/*-----------------------------------------------------------*/
/**
* @brief Load the default key and certificate from storage.
*/
CK_RV GetAttributeParameterIndex(
CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, CK_ATTRIBUTE_TYPE type, CK_ULONG_PTR index)
{
CK_RV xResult = CKR_ARGUMENTS_BAD;
CK_ULONG i = 0;
CK_BBOOL foundType = CK_FALSE;
for (i = 0; i < ulCount; i++) {
if (pTemplate[i].type == type) {
foundType = CK_TRUE;
xResult = CKR_OK;
break;
}
}
if (foundType) {
*index = i;
}
return xResult;
}
CK_RV AsymmetricEncrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData,
CK_ULONG_PTR pulEncryptedDataLen)
{
CK_RV xResult = CKR_OK;
sss_status_t status = kStatus_SSS_Fail;
uint8_t data[2048] = {0};
size_t dataLen = sizeof(data);
// sss_algorithm_t digest_algorithm = kAlgorithm_None;
sss_asymmetric_t asymmCtx;
sss_object_t sss_object = {0};
if (pxSessionObj->xOperationInProgress == CKM_RSA_PKCS) {
xResult = (2048 >= (ulDataLen + 11)) ? CKR_OK : CKR_MECHANISM_INVALID;
if (xResult != CKR_OK) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return xResult;
}
}
#if 0 //Check if hashing is performed in applet because we are not calculating hash in AKM
if(pxSessionObj->xOperationInProgress != CKM_RSA_PKCS) {
xResult = GetSSSAlgorithm(algorithm, &digest_algorithm);
if(xResult != CKR_OK) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return xResult;
}
sss_digest_t digestCtx;
status = sss_digest_context_init(&digestCtx,
&pex_sss_demo_boot_ctx->session,
digest_algorithm,
kMode_SSS_Digest);
if(status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return CKR_DEVICE_ERROR;
}
status = sss_digest_one_go(&digestCtx,
pData,
(size_t) ulDataLen,
&data[0],
&dataLen);
if(status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return CKR_DEVICE_ERROR;
}
sss_digest_context_free(&digestCtx);
}
else
#endif
{
memcpy(&data[0], pData, ulDataLen);
dataLen = ulDataLen;
}
LOCK_MUTEX_FOR_RTOS
{
status = sss_key_object_init(&sss_object, &pex_sss_demo_boot_ctx->ks);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_key_object_get_handle(&sss_object, pxSessionObj->xOperationKeyHandle);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_asymmetric_context_init(
&asymmCtx, &pex_sss_demo_boot_ctx->session, &sss_object, algorithm, kMode_SSS_Encrypt);
if (status != kStatus_SSS_Success) {
LOG_E(" sss_asymmetric_context_ sign init Failed...");
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_FUNCTION_FAILED)
// return CKR_FUNCTION_FAILED;
}
/* Do Encryption */
uint8_t encData[256] = {0};
size_t encDataLen = sizeof(encData);
status = sss_asymmetric_encrypt(&asymmCtx, &data[0], dataLen, &encData[0], &encDataLen);
if (status != kStatus_SSS_Success) {
LOG_E("sss_asymmetric_encrypt failed");
xResult = CKR_FUNCTION_FAILED;
}
if (xResult == CKR_OK) {
if (pEncryptedData) {
if (*pulEncryptedDataLen < encDataLen) {
xResult = CKR_BUFFER_TOO_SMALL;
}
else {
memcpy(pEncryptedData, &encData[0], encDataLen);
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
}
}
*pulEncryptedDataLen = encDataLen;
}
sss_asymmetric_context_free(&asymmCtx);
UNLOCK_MUTEX_FOR_RTOS
}
return xResult;
}
CK_RV AsymmetricDecrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pEncryptedData,
CK_ULONG ulEncryptedDataLen,
CK_BYTE_PTR pData,
CK_ULONG_PTR pulDataLen)
{
CK_RV xResult = CKR_OK;
sss_status_t status = kStatus_SSS_Fail;
uint8_t data[2048] = {0};
size_t dataLen = sizeof(data);
// sss_algorithm_t digest_algorithm = kAlgorithm_None;
sss_asymmetric_t asymmCtx;
sss_object_t sss_object = {0};
// if(pxSessionObj->xOperationInProgress == CKM_RSA_PKCS) {
// xResult = (2048 >= (ulEncryptedDataLen + 11)) ? CKR_OK : CKR_MECHANISM_INVALID;
// if(xResult != CKR_OK) {
// pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
// return xResult;
// }
// }
#if 0 // Not required for decryption. Not sure how to process Algorithm with hashing.
if(pxSessionObj->xOperationInProgress != CKM_RSA_PKCS /*&&
pxSessionObj->xOperationInProgress != CKM_RSA_PKCS_PSS*/) {
xResult = GetSSSAlgorithm(algorithm, &digest_algorithm);
if(xResult != CKR_OK) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return xResult;
}
sss_digest_t digestCtx;
status = sss_digest_context_init(&digestCtx,
&pex_sss_demo_boot_ctx->session,
digest_algorithm,
kMode_SSS_Digest);
if(status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return CKR_DEVICE_ERROR;
}
status = sss_digest_one_go(&digestCtx,
pEncryptedData,
(size_t) ulEncryptedDataLen,
&data[0],
&dataLen);
if(status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return CKR_DEVICE_ERROR;
}
sss_digest_context_free(&digestCtx);
}
else
#endif
{
memcpy(&data[0], pEncryptedData, ulEncryptedDataLen);
dataLen = ulEncryptedDataLen;
}
LOCK_MUTEX_FOR_RTOS
{
status = sss_key_object_init(&sss_object, &pex_sss_demo_boot_ctx->ks);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_key_object_get_handle(&sss_object, pxSessionObj->xOperationKeyHandle);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_asymmetric_context_init(
&asymmCtx, &pex_sss_demo_boot_ctx->session, &sss_object, algorithm, kMode_SSS_Decrypt);
if (status != kStatus_SSS_Success) {
LOG_E(" sss_asymmetric_context_ sign init Failed...");
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_FUNCTION_FAILED)
// return CKR_FUNCTION_FAILED;
}
/* Do Signing */
uint8_t signature[256] = {0};
size_t sigLen = sizeof(signature);
status = sss_asymmetric_decrypt(&asymmCtx, &data[0], dataLen, &signature[0], &sigLen);
// status = sss_asymmetric_sign_digest(
// &asymmCtx, &data[0], dataLen, &signature[0], &sigLen);
if (status != kStatus_SSS_Success) {
LOG_E("sss_asymmetric_decrypt failed");
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
xResult = CKR_FUNCTION_FAILED;
}
if (xResult == CKR_OK) {
if (pData) {
if (*pulDataLen < sigLen) {
xResult = CKR_BUFFER_TOO_SMALL;
}
else {
memcpy(pData, &signature[0], sigLen);
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
}
}
*pulDataLen = sigLen;
}
sss_asymmetric_context_free(&asymmCtx);
UNLOCK_MUTEX_FOR_RTOS
}
return xResult;
}
CK_RV SymmetricEncrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pData,
CK_ULONG ulDataLen,
CK_BYTE_PTR pEncryptedData,
CK_ULONG_PTR pulEncryptedDataLen)
{
CK_RV xResult = CKR_OK;
sss_status_t status = kStatus_SSS_Fail;
// uint8_t data[2048] = { 0 };
// size_t dataLen = sizeof(data);
sss_symmetric_t symmCtx;
sss_object_t symmObject;
// memcpy(&data[0], pData, ulDataLen);
// dataLen = ulDataLen;
uint8_t iv[16] = {0};
size_t ivLen = sizeof(iv);
uint8_t encData[256] = {0};
size_t encDataLen = sizeof(encData);
size_t tempOutBufLen = encDataLen;
uint8_t *pOut = &encData[0];
if (algorithm == kAlgorithm_SSS_AES_CBC) {
if (pxSessionObj->mechParameterLen != 0) {
memcpy(iv, pxSessionObj->mechParameter, ivLen);
}
}
#if 0
//PKCS11 Documentation not clear. Ignoring currently
if(algorithm == kAlgorithm_SSS_AES_CTR) {
if(!pxSessionObj->mechParameterLen) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return CKR_ARGUMENTS_BAD;
}
else {
CK_AES_CTR_PARAMS_PTR ctrParams = (CK_AES_CTR_PARAMS_PTR) pxSessionObj->mechParameter;
memcpy(&iv[0], &pxSessionObj->mechParameter.cb[0], sizeof(iv));
}
}
#endif
LOCK_MUTEX_FOR_RTOS
{
status = sss_key_object_init(&symmObject, &pex_sss_demo_boot_ctx->ks);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
// return CKR_DEVICE_ERROR;
}
status = sss_key_object_get_handle(&symmObject, pxSessionObj->xOperationKeyHandle);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
// return CKR_DEVICE_ERROR;
}
status = sss_symmetric_context_init(
&symmCtx, &pex_sss_demo_boot_ctx->session, &symmObject, algorithm, kMode_SSS_Encrypt);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
// return CKR_DEVICE_ERROR;
}
/*Do Encryption*/
status = sss_cipher_init(&symmCtx, iv, ivLen);
if (status != kStatus_SSS_Success) {
sss_symmetric_context_free(&symmCtx);
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_cipher_update(&symmCtx, (const uint8_t *)pData, (size_t)ulDataLen, pOut, &tempOutBufLen);
if (status != kStatus_SSS_Success) {
sss_symmetric_context_free(&symmCtx);
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
// return CKR_DEVICE_ERROR;
}
pOut = pOut + tempOutBufLen;
encDataLen = tempOutBufLen;
tempOutBufLen = sizeof(encData) - tempOutBufLen;
status = sss_cipher_finish(&symmCtx, NULL, 0, pOut, &tempOutBufLen);
if (status != kStatus_SSS_Success) {
sss_symmetric_context_free(&symmCtx);
LOG_E("sss_cipher_one_go failed");
xResult = CKR_FUNCTION_FAILED;
}
encDataLen = encDataLen + tempOutBufLen;
if (xResult == CKR_OK) {
if (pEncryptedData) {
if (*pulEncryptedDataLen < encDataLen) {
xResult = CKR_BUFFER_TOO_SMALL;
}
else {
memcpy(pEncryptedData, &encData[0], encDataLen);
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
}
}
*pulEncryptedDataLen = encDataLen;
}
sss_symmetric_context_free(&symmCtx);
UNLOCK_MUTEX_FOR_RTOS
}
return xResult;
}
CK_RV SymmetricDecrypt(P11SessionPtr_t pxSessionObj,
sss_algorithm_t algorithm,
CK_BYTE_PTR pEncryptedData,
CK_ULONG ulEncryptedDataLen,
CK_BYTE_PTR pData,
CK_ULONG_PTR pulDecryptedDataLen)
{
CK_RV xResult = CKR_OK;
sss_status_t status = kStatus_SSS_Fail;
// uint8_t data[2048] = { 0 };
// size_t dataLen = sizeof(data);
sss_symmetric_t symmCtx;
sss_object_t symmObject;
// status = sss_key_object_get_handle(&symmObject, CLIENT_SYMMETRIC_KEY_INDEX);
// if(status != kStatus_SSS_Success) {
// pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
// return CKR_DEVICE_ERROR;
// }
// memcpy(&data[0], pEncryptedData, ulEncryptedDataLen);
// dataLen = ulEncryptedDataLen;
uint8_t iv[16] = {0};
size_t ivLen = sizeof(iv);
uint8_t encData[256] = {0};
size_t encDataLen = sizeof(encData);
size_t tempOutBufLen = encDataLen;
uint8_t *pOut = &encData[0];
if (algorithm == kAlgorithm_SSS_AES_CBC) {
if (pxSessionObj->mechParameterLen != 0) {
memcpy(iv, pxSessionObj->mechParameter, ivLen);
}
}
#if 0
//PKCS11 Documentation not clear. Ignoring currently
if(algorithm == kAlgorithm_SSS_AES_CTR) {
if(!pxSessionObj->mechParameterLen) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
return CKR_ARGUMENTS_BAD;
}
else {
CK_AES_CTR_PARAMS_PTR ctrParams = (CK_AES_CTR_PARAMS_PTR) pxSessionObj->mechParameter;
memcpy(&iv[0], &pxSessionObj->mechParameter.cb[0], sizeof(iv));
}
}
#endif
LOCK_MUTEX_FOR_RTOS
{
status = sss_key_object_init(&symmObject, &pex_sss_demo_boot_ctx->ks);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_key_object_get_handle(&symmObject, pxSessionObj->xOperationKeyHandle);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_symmetric_context_init(
&symmCtx, &pex_sss_demo_boot_ctx->session, &symmObject, algorithm, kMode_SSS_Decrypt);
if (status != kStatus_SSS_Success) {
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
// return CKR_DEVICE_ERROR;
}
/*Do Decryption*/
status = sss_cipher_init(&symmCtx, iv, ivLen);
if (status != kStatus_SSS_Success) {
sss_symmetric_context_free(&symmCtx);
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
}
status = sss_cipher_update(
&symmCtx, (const uint8_t *)pEncryptedData, (size_t)ulEncryptedDataLen, pOut, &tempOutBufLen);
if (status != kStatus_SSS_Success) {
sss_symmetric_context_free(&symmCtx);
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
UNLOCK_MUTEX_FOR_RTOS_RET(CKR_DEVICE_ERROR)
// return CKR_DEVICE_ERROR;
}
pOut = pOut + tempOutBufLen;
encDataLen = tempOutBufLen;
tempOutBufLen = sizeof(encData) - tempOutBufLen;
status = sss_cipher_finish(&symmCtx, NULL, 0, pOut, &tempOutBufLen);
if (status != kStatus_SSS_Success) {
sss_symmetric_context_free(&symmCtx);
LOG_E("sss_cipher_one_go failed");
xResult = CKR_FUNCTION_FAILED;
}
encDataLen = encDataLen + tempOutBufLen;
if (xResult == CKR_OK) {
size_t i = 0;
while ((encData[encDataLen - 1 - i] == 0) && i < encDataLen) {
i++;
}
encDataLen = encDataLen - i;
if (pData) {
if (*pulDecryptedDataLen < encDataLen) {
xResult = CKR_BUFFER_TOO_SMALL;
}
else {
if (encDataLen > 0) {
memcpy(pData, &encData[0], encDataLen);
}
else {
LOG_E("memcpy failed");
}
pxSessionObj->xOperationInProgress = pkcs11NO_OPERATION;
}
}
*pulDecryptedDataLen = encDataLen;
}
sss_symmetric_context_free(&symmCtx);
UNLOCK_MUTEX_FOR_RTOS
}
return xResult;
}
CK_RV GetSSSAlgorithm(const sss_algorithm_t algorithm, sss_algorithm_t *digest_algo)
{
switch (algorithm) {