forked from wolfSSL/wolfPKCS11
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinternal.c
More file actions
13354 lines (12050 loc) · 397 KB
/
internal.c
File metadata and controls
13354 lines (12050 loc) · 397 KB
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
/* internal.c
*
* Copyright (C) 2006-2023 wolfSSL Inc.
*
* This file is part of wolfPKCS11.
*
* wolfPKCS11 is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfPKCS11 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <wolfpkcs11/config.h>
#endif
#include "wolfpkcs11/pkcs11.h"
#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#else
#include "user_settings.h"
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/version.h>
#include <wolfssl/wolfcrypt/pwdbased.h>
#include <wolfssl/wolfcrypt/asn.h>
#include <wolfssl/wolfcrypt/hash.h>
#include <wolfssl/wolfcrypt/hmac.h>
#include <wolfssl/wolfcrypt/ecc.h>
#include <wolfssl/wolfcrypt/rsa.h>
#include <wolfssl/wolfcrypt/asn_public.h>
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/cmac.h>
#include <wolfssl/wolfcrypt/kdf.h>
#if !defined(WOLFPKCS11_NO_STORE) && !defined(WOLFPKCS11_CUSTOM_STORE)
/* OS-specific includes for directory creation */
#if defined(_WIN32) || defined(_MSC_VER)
#include <direct.h>
#include <io.h>
#define MKDIR(path) _mkdir(path)
#else
#include <sys/stat.h>
#include <errno.h>
#define MKDIR(path) mkdir(path, 0700)
#endif
#endif
#include <wolfpkcs11/internal.h>
#include <wolfpkcs11/store.h>
#ifdef WOLFPKCS11_TPM
#include <wolftpm/tpm2_wrap.h>
#include <wolftpm/version.h>
#ifndef WOLFPKCS11_TPM_CUST_IO
#include <hal/tpm_io.h>
#ifndef TPM2_IOCB_CTX
#define TPM2_IOCB_CTX NULL
#endif
#endif
#endif
#ifdef WOLFSSL_MAXQ10XX_CRYPTO
#include <wolfpkcs11/port/maxim/MXQ_API.h>
#include <wolfssl/wolfcrypt/asn.h>
#define MAX_CERT_DATASIZE 2048
#define MAX_SIG_DATASIZE 64
#define ECC_KEYCOMPLEN 32
#endif /* WOLFSSL_MAXQ10XX_CRYPTO */
#if defined(WC_RSA_BLINDING) && (!defined(HAVE_FIPS) || \
(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
#define WOLFPKCS11_NEED_RSA_RNG
#endif
#if defined(WOLFPKCS11_TPM) && defined(WOLFSSL_MAXQ10XX_CRYPTO)
#error "wolfTPM and MAXQ10XX are incompatible with each other."
#endif
/* Helper to get size of struct field */
#define FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
/* Size of hash calculated from PIN. */
#define PIN_HASH_SZ 32
/* Size of seed used when calculating hash from PIN. */
#define PIN_SEED_SZ 16
/* Size of token's label. */
#define LABEL_SZ 32
/* Length of seed from global random to seed local random. */
#define RNG_SEED_SZ 32
/* Maximum size of storage for generated/derived DH key. */
#ifdef WOLFPKCS11_NSS
#define WP11_MAX_DH_KEY_SZ (8192/8)
#else
#define WP11_MAX_DH_KEY_SZ (4096/8)
#endif
/* Maximum size of storage for generated/derived symmetric key. */
#ifdef WOLFPKCS11_NSS
#define WP11_MAX_SYM_KEY_SZ (2048)
#elif !defined(NO_DH)
#define WP11_MAX_SYM_KEY_SZ (4096/8)
#elif defined(HAVE_ECC)
#define WP11_MAX_SYM_KEY_SZ ((521+7)/8)
#else
#define WP11_MAX_SYM_KEY_SZ 64
#endif
#ifndef WP11_MAX_CERT_SZ
#define WP11_MAX_CERT_SZ 4096
#endif
#define PKCS11_CHECK_VALUE_SIZE 3
/* Sizes for storage. */
#define WP11_MAX_IV_SZ 16
#define WP11_MAX_GCM_NONCE_SZ 16
#define WP11_MAX_GCM_TAG_SZ 16
#define WP11_MAX_GCM_TAG_BITS 128
/* wolfSSL ECC signatures are ASN.1 encoding - need to encode and decode. */
#define ASN_INTEGER 0x02
#define ASN_OCTET_STRING 0x04
#define ASN_OBJECT_ID 0x06
#define ASN_SEQUENCE 0x10
#define ASN_CONSTRUCTED 0x20
#define ASN_LONG_LENGTH 0x80
/* Create a session handle from slot id and session id. */
#define SESS_HANDLE(slot, s) (((slot) << 16) | (s))
/* Determine slot id from session handle. */
#define SESS_HANDLE_SLOT_ID(s) ((CK_SLOT_ID)((s) >> 16))
/* Determine session id from session handle. */
#define SESS_HANDLE_SESS_ID(s) ((s) & 0xffff)
/* Create an object handle from a onToken and object id. */
#define OBJ_HANDLE(on, i) (((on) << 28) | (i))
/* Determine whether object is onToken from object handle. */
#define OBJ_HANDLE_ON_TOKEN(h) ((int)((h) >> 28))
/* Determine object id from object handle. */
#define OBJ_HANDLE_OBJ_ID(h) ((h) & 0xfffffff)
#ifdef SINGLE_THREADED
/* Disable locking. */
typedef int WP11_Lock;
#define WP11_Lock_Init(l) ({ 0; })
#define WP11_Lock_Free(l)
#define WP11_Lock_LockRW(l) ({ 0; })
#define WP11_Lock_UnlockRW(l) ({ 0; })
#define WP11_Lock_LockRO(l) ({ 0; })
#define WP11_Lock_UnlockRO(l) ({ 0; })
#else
typedef struct WP11_Lock {
wolfSSL_Mutex read; /* Mutex for accessing count */
wolfSSL_Mutex write; /* Mutex for writing */
int cnt; /* Count of readers */
} WP11_Lock;
#endif
#ifdef DEBUG_WOLFPKCS11
int wolfpkcs11_debugging = 0;
WP11_API void wolfPKCS11_Debugging_On(void)
{
wolfpkcs11_debugging = 1;
WOLFPKCS11_MSG("debug logging enabled");
}
WP11_API void wolfPKCS11_Debugging_Off(void)
{
WOLFPKCS11_MSG("debug logging disabled");
wolfpkcs11_debugging = 0;
}
#endif
/* Symmetric key data. */
typedef struct WP11_Data {
byte data[WP11_MAX_SYM_KEY_SZ]; /* Key data */
word32 len; /* Length of key data in bytes */
} WP11_Data;
typedef struct WP11_GenericData {
byte *data;
word32 dataLen;
byte *application;
word32 applicationLen;
byte *objectId; /* CKA_OBJECT_ID, a DER encoded ID */
word32 objectIdLen;
} WP11_GenericData;
/* Certificate */
typedef struct WP11_Cert {
byte *data; /* Certificate data */
word32 len; /* Length of certificate data in bytes */
CK_CERTIFICATE_TYPE type;
} WP11_Cert;
typedef struct WP11_Trust {
byte sha1Hash[WC_SHA_DIGEST_SIZE];
byte md5Hash[WC_MD5_DIGEST_SIZE];
CK_ULONG serverAuth;
CK_ULONG clientAuth;
CK_ULONG emailProtection;
CK_ULONG codeSigning;
CK_BBOOL stepUpApproved;
} WP11_Trust;
#ifndef NO_DH
typedef struct WP11_DhKey {
byte key[WP11_MAX_DH_KEY_SZ]; /* Public or private key */
word32 len; /* Length of public key */
DhKey params; /* DH parameters object */
} WP11_DhKey;
#endif
/* When adding/modifying new members, add support in WP11_Object_Copy */
struct WP11_Object {
union {
#ifndef NO_RSA
RsaKey* rsaKey; /* RSA key object */
#endif
#ifdef HAVE_ECC
ecc_key* ecKey; /* EC key object */
#endif
#ifndef NO_DH
WP11_DhKey* dhKey; /* DH parameters object */
#endif
WP11_Data* symmKey; /* Symmetric key object */
WP11_GenericData genericData; /* Generic data object */
WP11_Cert cert; /* Certificate object */
#ifdef WOLFPKCS11_NSS
WP11_Trust trust; /* Trust object */
#endif
} data;
#ifdef WOLFPKCS11_TPM
WOLFTPM2_KEYBLOB* tpmKey;
#endif
#ifdef WOLFSSL_STM32U5_DHUK
unsigned char* dhukIv; /* IV used with wrapping and unwrapping AES key. */
int dhukIvLen;
#endif
CK_KEY_TYPE type; /* Key type of this object */
word32 size; /* Size of the key in bits or bytes */
#ifndef WOLFPKCS11_NO_STORE
unsigned char* keyData; /* Encoded key data */
int keyDataLen; /* Length of encoded key data */
byte iv[GCM_NONCE_MID_SZ]; /* IV/nonce for encrypt/decrypt */
byte encoded:1; /* Key isn't in decoded form */
#endif
WP11_Session* session; /* Session object belongs to */
WP11_Slot* slot; /* Slot object belongs to */
CK_OBJECT_HANDLE handle; /* Handle of this object */
CK_OBJECT_CLASS objClass; /* Object class */
CK_MECHANISM_TYPE keyGenMech; /* Key Gen mechanism created with */
byte onToken:1; /* Object on token or session */
byte local:1; /* Locally created object */
word32 opFlag; /* Flags of operations allowed */
char startDate[8]; /* Start date of usage */
char endDate[8]; /* End data of usage */
unsigned char* keyId; /* Key identifier */
int keyIdLen; /* Length of key identifier */
unsigned char* label; /* Object label */
int labelLen; /* Length of object label */
unsigned char* issuer; /* Certificate issuer */
int issuerLen; /* Length of certificate issuer */
unsigned char* serial; /* Certificate serial number */
int serialLen; /* Length of certificate serial number */
unsigned char* subject; /* Subject of the object */
int subjectLen; /* Length of subject */
#ifdef WOLFPKCS11_NSS
unsigned char* email; /* Certificate email (NSS extension) */
int emailLen; /* Length of certificate email */
#endif
word32 category; /* Category of certificate */
WP11_Lock* lock; /* Object specific lock */
int devId;
WP11_Object* next; /* Next object in linked list */
};
typedef struct WP11_Find {
int state; /* Whether operation is initialized */
CK_OBJECT_HANDLE found[WP11_FIND_MAX];
/* List of object handles found */
int count; /* Count of object handles */
int curr; /* Index of last object returned */
} WP11_Find;
#ifndef NO_RSA
#ifndef WC_NO_RSA_OAEP
typedef struct WP11_OaepParams {
enum wc_HashType hashType; /* Type of hash algorithm */
int mgf; /* Mask Generation Function */
byte* label; /* Label or AAD */
int labelSz; /* Size of label in bytes */
} WP11_OaepParams;
#endif
#ifdef WC_RSA_PSS
typedef struct WP11_PssParams {
enum wc_HashType hashType;
int mgf;
int saltLen;
} WP11_PssParams;
#endif
#endif
#ifndef NO_AES
#ifdef HAVE_AES_CBC
typedef struct WP11_CbcParams {
unsigned char iv[WP11_MAX_IV_SZ]; /* IV of CBC operation */
Aes aes; /* AES object from wolfCrypt */
unsigned char partial[AES_BLOCK_SIZE];
/* Partial block when streaming */
byte partialSz; /* Size of partial block data */
} WP11_CbcParams;
#endif
#ifdef HAVE_AESCTR
typedef struct WP11_CtrParams {
Aes aes; /* AES object from wolfCrypt */
} WP11_CtrParams;
#endif
#ifdef HAVE_AESGCM
typedef struct WP11_GcmParams {
unsigned char iv[WP11_MAX_GCM_NONCE_SZ];
/* IV/nonce data */
int ivSz; /* IV/nonce size in bytes */
unsigned char* aad; /* Additional Authentication Data */
int aadSz; /* AAD size in bytes */
int tagBits; /* Authentication tag size in bits */
unsigned char authTag[WP11_MAX_GCM_TAG_SZ];
/* Authentication tag calculated */
unsigned char* enc; /* Encrypted data - cached for decrypt */
int encSz; /* Size of encrypted data in bytes */
} WP11_GcmParams;
#endif
#ifdef HAVE_AESCCM
typedef struct WP11_CcmParams {
int dataSz; /* Data size in bytes */
unsigned char iv[WP11_MAX_GCM_NONCE_SZ];
/* IV/nonce data */
int ivSz; /* IV/nonce size in bytes */
unsigned char* aad; /* Additional Authentication Data */
int aadSz; /* AAD size in bytes */
int macSz; /* Size of MAC data in bytes */
} WP11_CcmParams;
#endif
#ifdef HAVE_AES_KEYWRAP
typedef struct WP11_KeyWrapParams {
Aes aes; /* AES object from wolfCrypt */
unsigned char iv[8]; /* IV/nonce data */
word32 ivSz; /* IV/nonce size in bytes */
} WP11_KeyWrapParams;
#endif
#ifdef HAVE_AESCTS
typedef struct WP11_CtsParams {
unsigned char iv[WP11_MAX_IV_SZ]; /* IV of CBC operation */
Aes aes; /* AES object from wolfCrypt */
} WP11_CtsParams;
#endif
#endif
#ifndef NO_HMAC
typedef struct WP11_Hmac {
Hmac hmac;
word32 hmacSz;
} WP11_Hmac;
#endif
typedef struct WP11_Digest {
wc_HashAlg hash;
enum wc_HashType hashType;
} WP11_Digest;
#ifdef HAVE_AESCMAC
typedef struct WP11_Cmac {
Cmac cmac;
byte sigLen;
} WP11_Cmac;
#endif
typedef struct WP11_TlsMacParams {
enum wc_MACAlgorithm mac;
word32 macSz;
CK_OBJECT_HANDLE key;
byte server:1;
byte isTlsPrf:1;
} WP11_TlsMacParams;
struct WP11_Session {
unsigned char inUse; /* Indicates session has been opened */
CK_SESSION_HANDLE handle; /* CryptoKi API session handle value */
CK_MECHANISM_TYPE mechanism; /* Op that is being performed */
CK_SLOT_ID slotId; /* Id of slot that session is on */
WP11_Slot* slot; /* Slot that session is on */
WP11_Object* object; /* Linked list of objects on session */
int objCnt; /* Count of objects in session */
WP11_Object* curr; /* Current object */
WP11_Find find; /* Find data */
int init; /* Which op is initialized */
union {
#ifndef NO_RSA
#ifndef WC_NO_RSA_OAEP
WP11_OaepParams oaep; /* RSA-OAEP parameters */
#endif
#ifdef WC_RSA_PSS
WP11_PssParams pss; /* RSA-PSS parameters */
#endif
#endif
#ifndef NO_AES
#ifdef HAVE_AES_CBC
WP11_CbcParams cbc; /* AES-CBC parameters */
#endif
#ifdef HAVE_AESCTR
WP11_CtrParams ctr; /* AES-CTR parameters */
#endif
#ifdef HAVE_AESGCM
WP11_GcmParams gcm; /* AES-GCM parameters */
#endif
#ifdef HAVE_AESCCM
WP11_CcmParams ccm; /* AES-CCM parameters */
#endif
#ifdef HAVE_AESCTS
WP11_CtsParams cts; /* AES-CTS parameters */
#endif
#ifdef HAVE_AES_KEYWRAP
WP11_KeyWrapParams kw; /* AES-KW parameters */
#endif
#endif
#ifndef NO_HMAC
WP11_Hmac hmac; /* HMAC parameters */
#endif
WP11_Digest digest; /* Digest parameters */
#ifdef HAVE_AESCMAC
WP11_Cmac cmac; /* CMAC parameters */
#endif
WP11_TlsMacParams tlsMac; /* TLS MAC parameters */
} params;
byte* data; /* Held data for one-shot mechs */
word32 dataSz; /* Size of held data */
int devId;
WP11_Session* next; /* Next session for slot */
};
typedef struct WP11_Token {
char label[LABEL_SZ]; /* Token label */
int state; /* Token initialize state */
byte soPin[PIN_HASH_SZ]; /* SO's PIN hashed with seed */
int soPinLen; /* Used to indicate PIN set */
byte soPinSeed[PIN_SEED_SZ]; /* Seed for calculating SO's PIN */
int soFailedLogin; /* Count of consecutive failed logins */
time_t soLastFailedLogin; /* Time of last login if it failed */
time_t soFailLoginTimeout; /* Timeout after max login fails */
byte userPin[PIN_HASH_SZ]; /* User's PIN hashed with seed */
int userPinLen; /* Used to indicate PIN set */
byte userPinSeed[PIN_SEED_SZ]; /* Seed for calculating user's PIN */
int userFailedLogin; /* Count of consecutive failed logins */
time_t userLastFailedLogin; /* Time of last login if it failed */
time_t userFailLoginTimeout; /* Timeout after max login fails */
#ifndef WOLFPKCS11_NO_STORE
byte seed[PIN_SEED_SZ]; /* Seed used to calculate key */
byte key[AES_256_KEY_SIZE]; /* Key to en/decrypt private data */
#endif
WC_RNG rng; /* Random number generator */
WP11_Lock rngLock; /* Lock for random access */
WP11_Lock lock; /* Lock for object access */
int loginState; /* Login state of the token */
WP11_Object* object; /* Linked list of token objects */
int objCnt; /* Count of objects on token */
int tokenFlags; /* Flags for token */
int nextObjId;
byte userPinEmpty:2; /* Indicates user PIN is empty
* 0 = not set
* 1 = empty
* 2 = not empty */
} WP11_Token;
struct WP11_Slot {
CK_SLOT_ID id; /* CryptoKi API slot id value */
WP11_Token token; /* Token information for slot */
WP11_Session* session; /* Linked list of sessions */
WP11_Lock lock; /* Lock for access to slot info */
int devId;
#ifdef WOLFPKCS11_TPM
WOLFTPM2_DEV tpmDev;
WOLFTPM2_KEY tpmSrk;
WOLFTPM2_SESSION tpmSession;
TpmCryptoDevCtx tpmCtx;
#endif
};
#if defined(HAVE_FIPS) && FIPS_VERSION_LT(6,0)
#define USE_LOCAL_CURVE_OID_LOOKUP
typedef struct WP11_Ecc_Curve
{
ecc_curve_id curve_id;
byte curve_oid[9];
CK_LONG curve_size;
} WP11_Ecc_Curve;
const WP11_Ecc_Curve DefinedCurves[] = {
{ ECC_SECP192R1, {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x01}, 8 },
{ ECC_PRIME192V2, {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x02}, 8 },
{ ECC_PRIME192V3, {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x03}, 8 },
{ ECC_PRIME239V1, {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x04}, 8 },
{ ECC_PRIME239V2, {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x05}, 8 },
{ ECC_PRIME239V3, {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x06}, 8 },
{ ECC_SECP256R1, {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07}, 8 },
{ ECC_SECP112R1, {0x2B,0x81,0x04,0x00,0x06}, 5 },
{ ECC_SECP112R2, {0x2B,0x81,0x04,0x00,0x07}, 5 },
{ ECC_SECP128R1, {0x2B,0x81,0x04,0x00,0x1C}, 5 },
{ ECC_SECP128R2, {0x2B,0x81,0x04,0x00,0x1D}, 5 },
{ ECC_SECP160R1, {0x2B,0x81,0x04,0x00,0x08}, 5 },
{ ECC_SECP160R2, {0x2B,0x81,0x04,0x00,0x1E}, 5 },
{ ECC_SECP224R1, {0x2B,0x81,0x04,0x00,0x21}, 5 },
{ ECC_SECP384R1, {0x2B,0x81,0x04,0x00,0x22}, 5 },
{ ECC_SECP521R1, {0x2B,0x81,0x04,0x00,0x23}, 5 },
{ ECC_SECP160K1, {0x2B,0x81,0x04,0x00,0x09}, 5 },
{ ECC_SECP192K1, {0x2B,0x81,0x04,0x00,0x1F}, 5 },
{ ECC_SECP224K1, {0x2B,0x81,0x04,0x00,0x20}, 5 },
{ ECC_SECP256K1, {0x2B,0x81,0x04,0x00,0x0A}, 5 },
{ ECC_BRAINPOOLP160R1, {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x01}, 9 },
{ ECC_BRAINPOOLP192R1, {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x03}, 9 },
{ ECC_BRAINPOOLP224R1, {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x05}, 9 },
{ ECC_BRAINPOOLP256R1, {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x07}, 9 },
{ ECC_BRAINPOOLP320R1, {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x09}, 9 },
{ ECC_BRAINPOOLP384R1, {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x0B}, 9 },
{ ECC_BRAINPOOLP512R1, {0x2B,0x24,0x03,0x03,0x02,0x08,0x01,0x01,0x0D}, 9 },
{ ECC_CURVE_MAX, { 0x0 }, 0 }
};
#endif
/* Number of slots. */
#define slotCnt 1
/* List of slot objects. */
static WP11_Slot slotList[1];
/* Global random used in random API, cryptographic operations and generating
* seed when creating new hash of PIN.
*/
static WC_RNG globalRandom;
/* Count of times library has had init called. */
static int libraryInitCount = 0;
/* Lock for globals including global random. */
static WP11_Lock globalLock;
#ifndef SINGLE_THREADED
/**
* Initialize a lock.
*
* @param lock [in] Lock object.
* @return BAD_MUTEX_E on failure.
* 0 on success.
*/
static int WP11_Lock_Init(WP11_Lock* lock)
{
int ret;
ret = wc_InitMutex(&lock->read);
if (ret == 0) {
ret = wc_InitMutex(&lock->write);
if (ret != 0)
wc_FreeMutex(&lock->read);
}
if (ret == 0)
lock->cnt = 0;
if (ret != 0)
ret = BAD_MUTEX_E;
return ret;
}
/**
* Free a lock.
*
* @param lock [in] Lock object.
*/
static void WP11_Lock_Free(WP11_Lock* lock)
{
wc_FreeMutex(&lock->write);
wc_FreeMutex(&lock->read);
}
/**
* Lock for read/write.
*
* @param lock [in] Lock object.
* @return BAD_MUTEX_E on failure.
* 0 on success.
*/
static int WP11_Lock_LockRW(WP11_Lock* lock)
{
int ret;
ret = wc_LockMutex(&lock->write);
#ifdef DEBUG_LOCK
fprintf(stderr, "LRW: %p - %d\n", &lock->write, lock->cnt);
#endif
return ret;
}
/**
* Unlock after read/write.
*
* @param lock [in] Lock object.
* @return BAD_MUTEX_E on failure.
* 0 on success.
*/
static int WP11_Lock_UnlockRW(WP11_Lock* lock)
{
int ret;
#ifdef DEBUG_LOCK
fprintf(stderr, "URW: %p - %d\n", &lock->write, lock->cnt);
#endif
ret = wc_UnLockMutex(&lock->write);
if (ret != 0)
ret = BAD_MUTEX_E;
return ret;
}
/**
* Lock for read-only.
*
* @param lock [in] Lock object.
* @return BAD_MUTEX_E on failure.
* 0 on success.
*/
static int WP11_Lock_LockRO(WP11_Lock* lock)
{
int ret;
ret = wc_LockMutex(&lock->read);
if (ret == 0) {
if (++lock->cnt == 1)
ret = wc_LockMutex(&lock->write);
#ifdef DEBUG_LOCK
fprintf(stderr, "LRO: %p - %d\n", &lock->write, lock->cnt);
#endif
}
if (ret == 0)
ret = wc_UnLockMutex(&lock->read);
if (ret != 0)
ret = BAD_MUTEX_E;
return ret;
}
/**
* Unlock after reading.
*
* @param lock [in] Lock object.
* @return BAD_MUTEX_E on failure.
* 0 on success.
*/
static int WP11_Lock_UnlockRO(WP11_Lock* lock)
{
int ret;
ret = wc_LockMutex(&lock->read);
if (ret == 0) {
if (--lock->cnt == 0)
ret = wc_UnLockMutex(&lock->write);
#ifdef DEBUG_LOCK
fprintf(stderr, "URO: %p - %d\n", &lock->write, lock->cnt);
#endif
}
if (ret == 0)
ret = wc_UnLockMutex(&lock->read);
if (ret != 0)
ret = BAD_MUTEX_E;
return ret;
}
#endif
static int Rng_New(WC_RNG* baseRng, WP11_Lock* lock, WC_RNG* rng)
{
int ret;
unsigned char seed[RNG_SEED_SZ];
WP11_Lock_LockRW(lock);
ret = wc_RNG_GenerateBlock(baseRng, seed, sizeof(seed));
WP11_Lock_UnlockRW(lock);
(void)lock;
if (ret == 0)
ret = wc_InitRngNonce_ex(rng, seed, sizeof(seed), NULL, INVALID_DEVID);
return ret;
}
static void Rng_Free(WC_RNG* rng)
{
wc_FreeRng(rng);
}
/**
* Allocate and initialize a new session.
*
* @param slot [in] Slot object session is connected with.
* @param handle [in] Session handle for the session object.
* @param session [out] New, initialized session object or NULL on error.
* @return MEMORY_E when dynamic memory allocation fails.
* 0 on success.
*/
static int wp11_Session_New(WP11_Slot* slot, CK_OBJECT_HANDLE handle,
WP11_Session** session)
{
int ret = 0;
WP11_Session* sess;
sess = (WP11_Session*)XMALLOC(sizeof(*sess), NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (sess == NULL)
ret = MEMORY_E;
if (ret == 0) {
XMEMSET(sess, 0, sizeof(*sess));
sess->slotId = slot->id;
sess->slot = slot;
sess->handle = handle;
sess->devId = slot->devId;
*session = sess;
}
return ret;
}
/**
* Check if the slot has an empty user PIN.
*
* @param slot [in] Slot object.
* @return 1 if the slot has an empty user PIN.
* 0 if the slot does not have an empty user PIN.
*/
int WP11_Slot_Has_Empty_Pin(WP11_Slot* slot)
{
if (slot == NULL)
return 0;
if (slot->token.tokenFlags & WP11_TOKEN_FLAG_USER_PIN_SET) {
switch (slot->token.userPinEmpty) {
case 1:
/* Empty user PIN */
return 1;
case 2:
/* Non-empty user PIN */
return 0;
default:
/* Cache result as WP11_Slot_CheckUserPin is very expensive */
if (WP11_Slot_CheckUserPin(slot, (char*)"", 0) == 0) {
/* Empty user PIN */
slot->token.userPinEmpty = 1;
return 1;
}
else {
/* Non-empty user PIN */
slot->token.userPinEmpty = 2;
return 0;
}
}
}
return 0;
}
/**
* Check if the slot has an SO PIN set.
*
* @param slot [in] Slot object.
* @return 1 if the slot has an SO PIN set.
* 0 if the slot does not have an SO PIN set.
*/
int WP11_Slot_SOPin_IsSet(WP11_Slot* slot)
{
if (slot == NULL)
return 0;
return slot->token.tokenFlags & WP11_TOKEN_FLAG_SO_PIN_SET;
}
/**
* Add a new session to the token in the slot.
*
* @param slot [in] Slot object.
* @param session [out] New, initialized session object or NULL on error.
* @return MEMORY_E when out of memory.
* 0 on success.
*/
static int wp11_Slot_AddSession(WP11_Slot* slot, WP11_Session** session)
{
int ret;
CK_OBJECT_HANDLE handle;
/* Calculate session handle value. */
if (slot->session != NULL)
handle = slot->session->handle + 1;
else
handle = SESS_HANDLE(slot->id, 1);
ret = wp11_Session_New(slot, handle, session);
if (ret == 0) {
/* Add to front of list */
(*session)->next = slot->session;
slot->session = *session;
}
return ret;
}
/**
* Finalize a session - clean-up but don't clear out.
*
* @param session [in] Session object.
*/
static void wp11_Session_Final(WP11_Session* session)
{
WP11_Object* obj;
if (session->inUse) {
/* Free objects in session. */
while ((obj = session->object) != NULL) {
/* ignore return value, logged in function */
(void)WP11_Session_RemoveObject(session, obj);
WP11_Object_Free(obj);
}
session->inUse = 0;
}
session->curr = NULL;
/* Finalize any find. */
WP11_Session_FindFinal(session);
#if !defined(NO_RSA) && !defined(WC_NO_RSA_OAEP)
if (session->mechanism == CKM_RSA_PKCS_OAEP &&
session->params.oaep.label != NULL) {
XFREE(session->params.oaep.label, NULL, DYNAMIC_TYPE_TMP_BUFFER);
session->params.oaep.label = NULL;
}
#endif
#ifndef NO_RSA
#ifdef HAVE_AES_CBC
if ((session->mechanism == CKM_AES_CBC ||
session->mechanism == CKM_AES_CBC_PAD) && session->init) {
wc_AesFree(&session->params.cbc.aes);
session->init = 0;
}
#endif
#ifdef HAVE_AESCTR
if (session->mechanism == CKM_AES_CTR && session->init) {
wc_AesFree(&session->params.ctr.aes);
session->init = 0;
}
#endif
#ifdef HAVE_AESGCM
if (session->mechanism == CKM_AES_GCM) {
if (session->params.gcm.aad != NULL) {
XFREE(session->params.gcm.aad, NULL, DYNAMIC_TYPE_TMP_BUFFER);
session->params.gcm.aad = NULL;
}
if (session->params.gcm.enc != NULL) {
XFREE(session->params.gcm.enc, NULL, DYNAMIC_TYPE_TMP_BUFFER);
session->params.gcm.enc = NULL;
}
}
#endif
#ifdef HAVE_AESCTS
if (session->mechanism == CKM_AES_CTS && session->init) {
wc_AesFree(&session->params.cts.aes);
session->init = 0;
}
#endif
#ifdef HAVE_AESCCM
if (session->mechanism == CKM_AES_CCM) {
if (session->params.ccm.aad != NULL) {
XFREE(session->params.ccm.aad, NULL, DYNAMIC_TYPE_TMP_BUFFER);
session->params.ccm.aad = NULL;
}
}
#endif
#endif
}
#ifndef WOLFPKCS11_NO_STORE
#ifndef WOLFPKCS11_CUSTOM_STORE
#ifdef WOLFPKCS11_TPM_STORE
/* determine which hierarchy to store in platform or owner */
#ifndef WOLFPKCS11_TPM_AUTH_TYPE
#define WOLFPKCS11_TPM_AUTH_TYPE 1 /* 1=TPM_RH_OWNER, 2=TPM_RH_PLATFORM */
#endif
#ifndef WOLFPKCS11_TPM_NV_BASE
#if WOLFPKCS11_TPM_AUTH_TYPE == 1
/* Owner Range: 0x1800000 -> 0x1c00000 */
#undef WOLFPKCS11_TPM_AUTH_TYPE
#define WOLFPKCS11_TPM_AUTH_TYPE TPM_RH_OWNER
#define WOLFPKCS11_TPM_NV_BASE TPM_20_OWNER_NV_SPACE
#else
/* Platform Range: 0x1400000 -> 0x1800000 */
#undef WOLFPKCS11_TPM_AUTH_TYPE
#define WOLFPKCS11_TPM_AUTH_TYPE TPM_RH_PLATFORM
#define WOLFPKCS11_TPM_NV_BASE TPM_20_PLATFORM_MFG_NV_SPACE
#endif
#endif
typedef struct WP11_TpmStore {
WOLFTPM2_DEV* dev;
WOLFTPM2_NV nv;
word32 offset;
} WP11_TpmStore;
static WP11_TpmStore tpmStores[1]; /* maximum of 1 open store */
/* Internal function to get maximum size for each store type */
static int wolfPKCS11_Store_GetMaxSize(int type, int variableSz)
{
int maxSz = 0;
switch (type) {
case WOLFPKCS11_STORE_TOKEN:
maxSz =
FIELD_SIZE(WP11_Token, label) +
sizeof(word32) + /* soPinLen */
FIELD_SIZE(WP11_Token, soPinSeed) +
FIELD_SIZE(WP11_Token, soFailedLogin) +
FIELD_SIZE(WP11_Token, soLastFailedLogin) +
FIELD_SIZE(WP11_Token, soFailLoginTimeout) +
sizeof(word32) + /* userPinLen */
FIELD_SIZE(WP11_Token, userPinSeed) +
FIELD_SIZE(WP11_Token, userFailedLogin) +
FIELD_SIZE(WP11_Token, userLastFailedLogin) +
FIELD_SIZE(WP11_Token, userFailLoginTimeout) +
FIELD_SIZE(WP11_Token, seed) +
FIELD_SIZE(WP11_Token, objCnt) +
FIELD_SIZE(WP11_Token, tokenFlags) +
FIELD_SIZE(WP11_Token, nextObjId) +
variableSz /* soPinLen + userPinLen + (objCnt * long) */
;
break;
case WOLFPKCS11_STORE_OBJECT:
maxSz =
FIELD_SIZE(WP11_Object, iv) +
FIELD_SIZE(WP11_Object, handle) +
FIELD_SIZE(WP11_Object, objClass) +
FIELD_SIZE(WP11_Object, keyGenMech) +
1 /* FIELD_SIZE(WP11_Object, onToken) */ +
1 /* FIELD_SIZE(WP11_Object, local) */ +
4 /* FIELD_SIZE(WP11_Object, flag) */ +
FIELD_SIZE(WP11_Object, opFlag) +
FIELD_SIZE(WP11_Object, startDate) +
FIELD_SIZE(WP11_Object, endDate) +
sizeof(word32) + /* keyIdLenSz */
sizeof(word32) + /* labelLen */
sizeof(word32) + /* issuerLen */
sizeof(word32) + /* serialLen */
sizeof(word32) + /* subjectLen */
FIELD_SIZE(WP11_Object, category) +
variableSz /* keyIdLen + labelLen + issuerLen + serialLen + subjectLen */
;
break;
case WOLFPKCS11_STORE_DATA:
maxSz =