/
pkcs11u.c
2117 lines (1893 loc) · 60.9 KB
/
pkcs11u.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* Internal PKCS #11 functions. Should only be called by pkcs11.c
*/
#include "pkcs11.h"
#include "pkcs11i.h"
#include "lowkeyi.h"
#include "secasn1.h"
#include "blapi.h"
#include "secerr.h"
#include "prnetdb.h" /* for PR_ntohl */
#include "sftkdb.h"
#include "softoken.h"
/*
* ******************** Error mapping *******************************
*/
/*
* map all the SEC_ERROR_xxx error codes that may be returned by freebl
* functions to CKR_xxx. return CKR_DEVICE_ERROR by default for backward
* compatibility.
*/
CK_RV
sftk_MapCryptError(int error)
{
switch (error) {
case SEC_ERROR_INVALID_ARGS:
case SEC_ERROR_BAD_DATA: /* MP_RANGE gets mapped to this */
return CKR_ARGUMENTS_BAD;
case SEC_ERROR_INPUT_LEN:
return CKR_DATA_LEN_RANGE;
case SEC_ERROR_OUTPUT_LEN:
return CKR_BUFFER_TOO_SMALL;
case SEC_ERROR_LIBRARY_FAILURE:
return CKR_GENERAL_ERROR;
case SEC_ERROR_NO_MEMORY:
return CKR_HOST_MEMORY;
case SEC_ERROR_BAD_SIGNATURE:
return CKR_SIGNATURE_INVALID;
case SEC_ERROR_INVALID_KEY:
return CKR_KEY_SIZE_RANGE;
case SEC_ERROR_BAD_KEY: /* an EC public key that fails validation */
return CKR_KEY_SIZE_RANGE; /* the closest error code */
case SEC_ERROR_UNSUPPORTED_EC_POINT_FORM:
return CKR_TEMPLATE_INCONSISTENT;
case SEC_ERROR_UNSUPPORTED_KEYALG:
return CKR_MECHANISM_INVALID;
case SEC_ERROR_UNSUPPORTED_ELLIPTIC_CURVE:
return CKR_DOMAIN_PARAMS_INVALID;
/* key pair generation failed after max number of attempts */
case SEC_ERROR_NEED_RANDOM:
return CKR_FUNCTION_FAILED;
}
return CKR_DEVICE_ERROR;
}
/*
* functions which adjust the mapping based on different contexts
* (Decrypt or Verify).
*/
/* used by Decrypt and UnwrapKey (indirectly) and Decrypt message */
CK_RV
sftk_MapDecryptError(int error)
{
switch (error) {
/* usually a padding error, or aead tag mismatch */
case SEC_ERROR_BAD_DATA:
return CKR_ENCRYPTED_DATA_INVALID;
default:
return sftk_MapCryptError(error);
}
}
/*
* return CKR_SIGNATURE_INVALID instead of CKR_DEVICE_ERROR by default for
* backward compatibilty.
*/
CK_RV
sftk_MapVerifyError(int error)
{
CK_RV crv = sftk_MapCryptError(error);
if (crv == CKR_DEVICE_ERROR)
crv = CKR_SIGNATURE_INVALID;
return crv;
}
/*
* ******************** Attribute Utilities *******************************
*/
/*
* create a new attribute with type, value, and length. Space is allocated
* to hold value.
*/
static SFTKAttribute *
sftk_NewAttribute(SFTKObject *object,
CK_ATTRIBUTE_TYPE type, const void *value, CK_ULONG len)
{
SFTKAttribute *attribute;
SFTKSessionObject *so = sftk_narrowToSessionObject(object);
int index;
if (so == NULL) {
/* allocate new attribute in a buffer */
PORT_Assert(0);
return NULL;
}
/*
* We attempt to keep down contention on Malloc and Arena locks by
* limiting the number of these calls on high traversed paths. This
* is done for attributes by 'allocating' them from a pool already
* allocated by the parent object.
*/
PZ_Lock(so->attributeLock);
index = so->nextAttr++;
PZ_Unlock(so->attributeLock);
PORT_Assert(index < MAX_OBJS_ATTRS);
if (index >= MAX_OBJS_ATTRS)
return NULL;
attribute = &so->attrList[index];
attribute->attrib.type = type;
attribute->freeAttr = PR_FALSE;
attribute->freeData = PR_FALSE;
if (value) {
if (len <= ATTR_SPACE) {
attribute->attrib.pValue = attribute->space;
} else {
attribute->attrib.pValue = PORT_Alloc(len);
attribute->freeData = PR_TRUE;
}
if (attribute->attrib.pValue == NULL) {
return NULL;
}
PORT_Memcpy(attribute->attrib.pValue, value, len);
attribute->attrib.ulValueLen = len;
} else {
attribute->attrib.pValue = NULL;
attribute->attrib.ulValueLen = 0;
}
attribute->attrib.type = type;
attribute->handle = type;
attribute->next = attribute->prev = NULL;
return attribute;
}
/*
* Free up all the memory associated with an attribute. Reference count
* must be zero to call this.
*/
static void
sftk_DestroyAttribute(SFTKAttribute *attribute)
{
if (attribute->freeData) {
if (attribute->attrib.pValue) {
/* clear out the data in the attribute value... it may have been
* sensitive data */
PORT_Memset(attribute->attrib.pValue, 0,
attribute->attrib.ulValueLen);
}
PORT_Free(attribute->attrib.pValue);
}
PORT_Free(attribute);
}
/*
* release a reference to an attribute structure
*/
void
sftk_FreeAttribute(SFTKAttribute *attribute)
{
if (attribute && attribute->freeAttr) {
sftk_DestroyAttribute(attribute);
return;
}
}
static SFTKAttribute *
sftk_FindTokenAttribute(SFTKTokenObject *object, CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *myattribute = NULL;
SFTKDBHandle *dbHandle = NULL;
CK_RV crv = CKR_HOST_MEMORY;
myattribute = (SFTKAttribute *)PORT_Alloc(sizeof(SFTKAttribute));
if (myattribute == NULL) {
goto loser;
}
dbHandle = sftk_getDBForTokenObject(object->obj.slot, object->obj.handle);
myattribute->handle = type;
myattribute->attrib.type = type;
myattribute->attrib.pValue = myattribute->space;
myattribute->attrib.ulValueLen = ATTR_SPACE;
myattribute->next = myattribute->prev = NULL;
myattribute->freeAttr = PR_TRUE;
myattribute->freeData = PR_FALSE;
crv = sftkdb_GetAttributeValue(dbHandle, object->obj.handle,
&myattribute->attrib, 1);
/* attribute is bigger than our attribute space buffer, malloc it */
if (crv == CKR_BUFFER_TOO_SMALL) {
myattribute->attrib.pValue = NULL;
crv = sftkdb_GetAttributeValue(dbHandle, object->obj.handle,
&myattribute->attrib, 1);
if (crv != CKR_OK) {
goto loser;
}
myattribute->attrib.pValue = PORT_Alloc(myattribute->attrib.ulValueLen);
if (myattribute->attrib.pValue == NULL) {
crv = CKR_HOST_MEMORY;
goto loser;
}
myattribute->freeData = PR_TRUE;
crv = sftkdb_GetAttributeValue(dbHandle, object->obj.handle,
&myattribute->attrib, 1);
}
loser:
if (dbHandle) {
sftk_freeDB(dbHandle);
}
if (crv != CKR_OK) {
if (myattribute) {
myattribute->attrib.ulValueLen = 0;
sftk_FreeAttribute(myattribute);
myattribute = NULL;
}
}
return myattribute;
}
/*
* look up and attribute structure from a type and Object structure.
* The returned attribute is referenced and needs to be freed when
* it is no longer needed.
*/
SFTKAttribute *
sftk_FindAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *attribute;
SFTKSessionObject *sessObject = sftk_narrowToSessionObject(object);
if (sessObject == NULL) {
return sftk_FindTokenAttribute(sftk_narrowToTokenObject(object), type);
}
PZ_Lock(sessObject->attributeLock);
sftkqueue_find(attribute, type, sessObject->head, sessObject->hashSize);
PZ_Unlock(sessObject->attributeLock);
return (attribute);
}
/*
* Take a buffer and it's length and return it's true size in bits;
*/
unsigned int
sftk_GetLengthInBits(unsigned char *buf, unsigned int bufLen)
{
unsigned int size = bufLen * 8;
unsigned int i;
/* Get the real length in bytes */
for (i = 0; i < bufLen; i++) {
unsigned char c = *buf++;
if (c != 0) {
unsigned char m;
for (m = 0x80; m > 0; m = m >> 1) {
if ((c & m) != 0) {
break;
}
size--;
}
break;
}
size -= 8;
}
return size;
}
/*
* Constrain a big num attribute. to size and padding
* minLength means length of the object must be greater than equal to minLength
* maxLength means length of the object must be less than equal to maxLength
* minMultiple means that object length mod minMultiple must equal 0.
* all input sizes are in bits.
* if any constraint is '0' that constraint is not checked.
*/
CK_RV
sftk_ConstrainAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type,
int minLength, int maxLength, int minMultiple)
{
SFTKAttribute *attribute;
int size;
unsigned char *ptr;
attribute = sftk_FindAttribute(object, type);
if (!attribute) {
return CKR_TEMPLATE_INCOMPLETE;
}
ptr = (unsigned char *)attribute->attrib.pValue;
if (ptr == NULL) {
sftk_FreeAttribute(attribute);
return CKR_ATTRIBUTE_VALUE_INVALID;
}
size = sftk_GetLengthInBits(ptr, attribute->attrib.ulValueLen);
sftk_FreeAttribute(attribute);
if ((minLength != 0) && (size < minLength)) {
return CKR_ATTRIBUTE_VALUE_INVALID;
}
if ((maxLength != 0) && (size > maxLength)) {
return CKR_ATTRIBUTE_VALUE_INVALID;
}
if ((minMultiple != 0) && ((size % minMultiple) != 0)) {
return CKR_ATTRIBUTE_VALUE_INVALID;
}
return CKR_OK;
}
PRBool
sftk_hasAttributeToken(SFTKTokenObject *object, CK_ATTRIBUTE_TYPE type)
{
CK_ATTRIBUTE template;
CK_RV crv;
SFTKDBHandle *dbHandle;
dbHandle = sftk_getDBForTokenObject(object->obj.slot, object->obj.handle);
template.type = type;
template.pValue = NULL;
template.ulValueLen = 0;
crv = sftkdb_GetAttributeValue(dbHandle, object->obj.handle, &template, 1);
sftk_freeDB(dbHandle);
/* attribute is bigger than our attribute space buffer, malloc it */
return (crv == CKR_OK) ? PR_TRUE : PR_FALSE;
}
/*
* return true if object has attribute
*/
PRBool
sftk_hasAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *attribute;
SFTKSessionObject *sessObject = sftk_narrowToSessionObject(object);
if (sessObject == NULL) {
return sftk_hasAttributeToken(sftk_narrowToTokenObject(object), type);
}
PZ_Lock(sessObject->attributeLock);
sftkqueue_find(attribute, type, sessObject->head, sessObject->hashSize);
PZ_Unlock(sessObject->attributeLock);
return (PRBool)(attribute != NULL);
}
/*
* add an attribute to an object
*/
static void
sftk_AddAttribute(SFTKObject *object, SFTKAttribute *attribute)
{
SFTKSessionObject *sessObject = sftk_narrowToSessionObject(object);
if (sessObject == NULL)
return;
PZ_Lock(sessObject->attributeLock);
sftkqueue_add(attribute, attribute->handle,
sessObject->head, sessObject->hashSize);
PZ_Unlock(sessObject->attributeLock);
}
/*
* copy an unsigned attribute into a SECItem. Secitem is allocated in
* the specified arena.
*/
CK_RV
sftk_Attribute2SSecItem(PLArenaPool *arena, SECItem *item, SFTKObject *object,
CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *attribute;
item->data = NULL;
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL)
return CKR_TEMPLATE_INCOMPLETE;
(void)SECITEM_AllocItem(arena, item, attribute->attrib.ulValueLen);
if (item->data == NULL) {
sftk_FreeAttribute(attribute);
return CKR_HOST_MEMORY;
}
PORT_Memcpy(item->data, attribute->attrib.pValue, item->len);
sftk_FreeAttribute(attribute);
return CKR_OK;
}
/*
* fetch multiple attributes into SECItems. Secitem data is allocated in
* the specified arena.
*/
CK_RV
sftk_MultipleAttribute2SecItem(PLArenaPool *arena, SFTKObject *object,
SFTKItemTemplate *itemTemplate, int itemTemplateCount)
{
CK_RV crv = CKR_OK;
CK_ATTRIBUTE templateSpace[SFTK_MAX_ITEM_TEMPLATE];
CK_ATTRIBUTE *template;
SFTKTokenObject *tokObject;
SFTKDBHandle *dbHandle = NULL;
int i;
tokObject = sftk_narrowToTokenObject(object);
/* session objects, just loop through the list */
if (tokObject == NULL) {
for (i = 0; i < itemTemplateCount; i++) {
crv = sftk_Attribute2SecItem(arena, itemTemplate[i].item, object,
itemTemplate[i].type);
if (crv != CKR_OK) {
return crv;
}
}
return CKR_OK;
}
/* don't do any work if none is required */
if (itemTemplateCount == 0) {
return CKR_OK;
}
/* don't allocate the template unless we need it */
if (itemTemplateCount > SFTK_MAX_ITEM_TEMPLATE) {
template = PORT_NewArray(CK_ATTRIBUTE, itemTemplateCount);
} else {
template = templateSpace;
}
if (template == NULL) {
crv = CKR_HOST_MEMORY;
goto loser;
}
dbHandle = sftk_getDBForTokenObject(object->slot, object->handle);
if (dbHandle == NULL) {
crv = CKR_OBJECT_HANDLE_INVALID;
goto loser;
}
/* set up the PKCS #11 template */
for (i = 0; i < itemTemplateCount; i++) {
template[i].type = itemTemplate[i].type;
template[i].pValue = NULL;
template[i].ulValueLen = 0;
}
/* fetch the attribute lengths */
crv = sftkdb_GetAttributeValue(dbHandle, object->handle,
template, itemTemplateCount);
if (crv != CKR_OK) {
goto loser;
}
/* allocate space for the attributes */
for (i = 0; i < itemTemplateCount; i++) {
template[i].pValue = PORT_ArenaAlloc(arena, template[i].ulValueLen);
if (template[i].pValue == NULL) {
crv = CKR_HOST_MEMORY;
goto loser;
}
}
/* fetch the attributes */
crv = sftkdb_GetAttributeValue(dbHandle, object->handle,
template, itemTemplateCount);
if (crv != CKR_OK) {
goto loser;
}
/* Fill in the items */
for (i = 0; i < itemTemplateCount; i++) {
itemTemplate[i].item->data = template[i].pValue;
itemTemplate[i].item->len = template[i].ulValueLen;
}
loser:
if (template != templateSpace) {
PORT_Free(template);
}
if (dbHandle) {
sftk_freeDB(dbHandle);
}
return crv;
}
/*
* delete an attribute from an object
*/
static void
sftk_DeleteAttribute(SFTKObject *object, SFTKAttribute *attribute)
{
SFTKSessionObject *sessObject = sftk_narrowToSessionObject(object);
if (sessObject == NULL) {
return;
}
PZ_Lock(sessObject->attributeLock);
if (sftkqueue_is_queued(attribute, attribute->handle,
sessObject->head, sessObject->hashSize)) {
sftkqueue_delete(attribute, attribute->handle,
sessObject->head, sessObject->hashSize);
}
PZ_Unlock(sessObject->attributeLock);
}
/*
* this is only valid for CK_BBOOL type attributes. Return the state
* of that attribute.
*/
PRBool
sftk_isTrue(SFTKObject *object, CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *attribute;
PRBool tok = PR_FALSE;
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL) {
return PR_FALSE;
}
tok = (PRBool)(*(CK_BBOOL *)attribute->attrib.pValue);
sftk_FreeAttribute(attribute);
return tok;
}
/*
* force an attribute to null.
* this is for sensitive keys which are stored in the database, we don't
* want to keep this info around in memory in the clear.
*/
void
sftk_nullAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *attribute;
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL)
return;
if (attribute->attrib.pValue != NULL) {
PORT_Memset(attribute->attrib.pValue, 0, attribute->attrib.ulValueLen);
if (attribute->freeData) {
PORT_Free(attribute->attrib.pValue);
}
attribute->freeData = PR_FALSE;
attribute->attrib.pValue = NULL;
attribute->attrib.ulValueLen = 0;
}
sftk_FreeAttribute(attribute);
}
static CK_RV
sftk_forceTokenAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type,
const void *value, unsigned int len)
{
CK_ATTRIBUTE attribute;
SFTKDBHandle *dbHandle = NULL;
SFTKTokenObject *to = sftk_narrowToTokenObject(object);
CK_RV crv;
PORT_Assert(to);
if (to == NULL) {
return CKR_DEVICE_ERROR;
}
dbHandle = sftk_getDBForTokenObject(object->slot, object->handle);
attribute.type = type;
attribute.pValue = (void *)value;
attribute.ulValueLen = len;
crv = sftkdb_SetAttributeValue(dbHandle, object, &attribute, 1);
sftk_freeDB(dbHandle);
return crv;
}
/*
* force an attribute to a specifc value.
*/
CK_RV
sftk_forceAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type,
const void *value, unsigned int len)
{
SFTKAttribute *attribute;
void *att_val = NULL;
PRBool freeData = PR_FALSE;
PORT_Assert(object);
PORT_Assert(object->refCount);
PORT_Assert(object->slot);
if (!object ||
!object->refCount ||
!object->slot) {
return CKR_DEVICE_ERROR;
}
if (sftk_isToken(object->handle)) {
return sftk_forceTokenAttribute(object, type, value, len);
}
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL)
return sftk_AddAttributeType(object, type, value, len);
if (value) {
if (len <= ATTR_SPACE) {
att_val = attribute->space;
} else {
att_val = PORT_Alloc(len);
freeData = PR_TRUE;
}
if (att_val == NULL) {
return CKR_HOST_MEMORY;
}
if (attribute->attrib.pValue == att_val) {
PORT_Memset(attribute->attrib.pValue, 0,
attribute->attrib.ulValueLen);
}
PORT_Memcpy(att_val, value, len);
}
if (attribute->attrib.pValue != NULL) {
if (attribute->attrib.pValue != att_val) {
PORT_Memset(attribute->attrib.pValue, 0,
attribute->attrib.ulValueLen);
}
if (attribute->freeData) {
PORT_Free(attribute->attrib.pValue);
}
attribute->freeData = PR_FALSE;
attribute->attrib.pValue = NULL;
attribute->attrib.ulValueLen = 0;
}
if (att_val) {
attribute->attrib.pValue = att_val;
attribute->attrib.ulValueLen = len;
attribute->freeData = freeData;
}
sftk_FreeAttribute(attribute);
return CKR_OK;
}
/*
* return a null terminated string from attribute 'type'. This string
* is allocated and needs to be freed with PORT_Free() When complete.
*/
char *
sftk_getString(SFTKObject *object, CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *attribute;
char *label = NULL;
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL)
return NULL;
if (attribute->attrib.pValue != NULL) {
label = (char *)PORT_Alloc(attribute->attrib.ulValueLen + 1);
if (label == NULL) {
sftk_FreeAttribute(attribute);
return NULL;
}
PORT_Memcpy(label, attribute->attrib.pValue,
attribute->attrib.ulValueLen);
label[attribute->attrib.ulValueLen] = 0;
}
sftk_FreeAttribute(attribute);
return label;
}
/*
* decode when a particular attribute may be modified
* SFTK_NEVER: This attribute must be set at object creation time and
* can never be modified.
* SFTK_ONCOPY: This attribute may be modified only when you copy the
* object.
* SFTK_SENSITIVE: The CKA_SENSITIVE attribute can only be changed from
* CK_FALSE to CK_TRUE.
* SFTK_ALWAYS: This attribute can always be modified.
* Some attributes vary their modification type based on the class of the
* object.
*/
SFTKModifyType
sftk_modifyType(CK_ATTRIBUTE_TYPE type, CK_OBJECT_CLASS inClass)
{
/* if we don't know about it, user user defined, always allow modify */
SFTKModifyType mtype = SFTK_ALWAYS;
switch (type) {
/* NEVER */
case CKA_CLASS:
case CKA_CERTIFICATE_TYPE:
case CKA_KEY_TYPE:
case CKA_MODULUS:
case CKA_MODULUS_BITS:
case CKA_PUBLIC_EXPONENT:
case CKA_PRIVATE_EXPONENT:
case CKA_PRIME:
case CKA_SUBPRIME:
case CKA_BASE:
case CKA_PRIME_1:
case CKA_PRIME_2:
case CKA_EXPONENT_1:
case CKA_EXPONENT_2:
case CKA_COEFFICIENT:
case CKA_VALUE_LEN:
case CKA_ALWAYS_SENSITIVE:
case CKA_NEVER_EXTRACTABLE:
case CKA_NSS_DB:
mtype = SFTK_NEVER;
break;
/* ONCOPY */
case CKA_TOKEN:
case CKA_PRIVATE:
case CKA_MODIFIABLE:
mtype = SFTK_ONCOPY;
break;
/* SENSITIVE */
case CKA_SENSITIVE:
case CKA_EXTRACTABLE:
mtype = SFTK_SENSITIVE;
break;
/* ALWAYS */
case CKA_LABEL:
case CKA_APPLICATION:
case CKA_ID:
case CKA_SERIAL_NUMBER:
case CKA_START_DATE:
case CKA_END_DATE:
case CKA_DERIVE:
case CKA_ENCRYPT:
case CKA_DECRYPT:
case CKA_SIGN:
case CKA_VERIFY:
case CKA_SIGN_RECOVER:
case CKA_VERIFY_RECOVER:
case CKA_WRAP:
case CKA_UNWRAP:
mtype = SFTK_ALWAYS;
break;
/* DEPENDS ON CLASS */
case CKA_VALUE:
mtype = (inClass == CKO_DATA) ? SFTK_ALWAYS : SFTK_NEVER;
break;
case CKA_SUBJECT:
mtype = (inClass == CKO_CERTIFICATE) ? SFTK_NEVER : SFTK_ALWAYS;
break;
default:
break;
}
return mtype;
}
/* decode if a particular attribute is sensitive (cannot be read
* back to the user of if the object is set to SENSITIVE) */
PRBool
sftk_isSensitive(CK_ATTRIBUTE_TYPE type, CK_OBJECT_CLASS inClass)
{
switch (type) {
/* ALWAYS */
case CKA_PRIVATE_EXPONENT:
case CKA_PRIME_1:
case CKA_PRIME_2:
case CKA_EXPONENT_1:
case CKA_EXPONENT_2:
case CKA_COEFFICIENT:
return PR_TRUE;
/* DEPENDS ON CLASS */
case CKA_VALUE:
/* PRIVATE and SECRET KEYS have SENSITIVE values */
return (PRBool)((inClass == CKO_PRIVATE_KEY) || (inClass == CKO_SECRET_KEY));
default:
break;
}
return PR_FALSE;
}
/*
* copy an attribute into a SECItem. Secitem is allocated in the specified
* arena.
*/
CK_RV
sftk_Attribute2SecItem(PLArenaPool *arena, SECItem *item, SFTKObject *object,
CK_ATTRIBUTE_TYPE type)
{
int len;
SFTKAttribute *attribute;
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL)
return CKR_TEMPLATE_INCOMPLETE;
len = attribute->attrib.ulValueLen;
if (arena) {
item->data = (unsigned char *)PORT_ArenaAlloc(arena, len);
} else {
item->data = (unsigned char *)PORT_Alloc(len);
}
if (item->data == NULL) {
sftk_FreeAttribute(attribute);
return CKR_HOST_MEMORY;
}
item->len = len;
PORT_Memcpy(item->data, attribute->attrib.pValue, len);
sftk_FreeAttribute(attribute);
return CKR_OK;
}
CK_RV
sftk_GetULongAttribute(SFTKObject *object, CK_ATTRIBUTE_TYPE type,
CK_ULONG *longData)
{
SFTKAttribute *attribute;
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL)
return CKR_TEMPLATE_INCOMPLETE;
if (attribute->attrib.ulValueLen != sizeof(CK_ULONG)) {
return CKR_ATTRIBUTE_VALUE_INVALID;
}
*longData = *(CK_ULONG *)attribute->attrib.pValue;
sftk_FreeAttribute(attribute);
return CKR_OK;
}
void
sftk_DeleteAttributeType(SFTKObject *object, CK_ATTRIBUTE_TYPE type)
{
SFTKAttribute *attribute;
attribute = sftk_FindAttribute(object, type);
if (attribute == NULL)
return;
sftk_DeleteAttribute(object, attribute);
sftk_FreeAttribute(attribute);
}
CK_RV
sftk_AddAttributeType(SFTKObject *object, CK_ATTRIBUTE_TYPE type,
const void *valPtr, CK_ULONG length)
{
SFTKAttribute *attribute;
attribute = sftk_NewAttribute(object, type, valPtr, length);
if (attribute == NULL) {
return CKR_HOST_MEMORY;
}
sftk_AddAttribute(object, attribute);
return CKR_OK;
}
/*
* ******************** Object Utilities *******************************
*/
/* must be called holding sftk_tokenKeyLock(slot) */
static SECItem *
sftk_lookupTokenKeyByHandle(SFTKSlot *slot, CK_OBJECT_HANDLE handle)
{
return (SECItem *)PL_HashTableLookup(slot->tokObjHashTable, (void *)handle);
}
/*
* use the refLock. This operations should be very rare, so the added
* contention on the ref lock should be lower than the overhead of adding
* a new lock. We use separate functions for this just in case I'm wrong.
*/
static void
sftk_tokenKeyLock(SFTKSlot *slot)
{
SKIP_AFTER_FORK(PZ_Lock(slot->objectLock));
}
static void
sftk_tokenKeyUnlock(SFTKSlot *slot)
{
SKIP_AFTER_FORK(PZ_Unlock(slot->objectLock));
}
static PRIntn
sftk_freeHashItem(PLHashEntry *entry, PRIntn index, void *arg)
{
SECItem *item = (SECItem *)entry->value;
SECITEM_FreeItem(item, PR_TRUE);
return HT_ENUMERATE_NEXT;
}
CK_RV
SFTK_ClearTokenKeyHashTable(SFTKSlot *slot)
{
sftk_tokenKeyLock(slot);
PORT_Assert(!slot->present);
PL_HashTableEnumerateEntries(slot->tokObjHashTable, sftk_freeHashItem, NULL);
sftk_tokenKeyUnlock(slot);
return CKR_OK;
}
/* allocation hooks that allow us to recycle old object structures */
static SFTKObjectFreeList sessionObjectList = { NULL, NULL, 0 };
static SFTKObjectFreeList tokenObjectList = { NULL, NULL, 0 };
SFTKObject *
sftk_GetObjectFromList(PRBool *hasLocks, PRBool optimizeSpace,
SFTKObjectFreeList *list, unsigned int hashSize, PRBool isSessionObject)
{
SFTKObject *object;
int size = 0;
if (!optimizeSpace) {
PZ_Lock(list->lock);
object = list->head;
if (object) {
list->head = object->next;
list->count--;
}
PZ_Unlock(list->lock);
if (object) {
object->next = object->prev = NULL;
*hasLocks = PR_TRUE;
return object;
}
}
size = isSessionObject ? sizeof(SFTKSessionObject) + hashSize * sizeof(SFTKAttribute *) : sizeof(SFTKTokenObject);
object = (SFTKObject *)PORT_ZAlloc(size);
if (isSessionObject && object) {
((SFTKSessionObject *)object)->hashSize = hashSize;
}
*hasLocks = PR_FALSE;
return object;
}
static void
sftk_PutObjectToList(SFTKObject *object, SFTKObjectFreeList *list,
PRBool isSessionObject)
{
/* the code below is equivalent to :
* optimizeSpace = isSessionObject ? object->optimizeSpace : PR_FALSE;
* just faster.
*/
PRBool optimizeSpace = isSessionObject &&
((SFTKSessionObject *)object)->optimizeSpace;
if (object->refLock && !optimizeSpace && (list->count < MAX_OBJECT_LIST_SIZE)) {
PZ_Lock(list->lock);
object->next = list->head;
list->head = object;
list->count++;
PZ_Unlock(list->lock);
return;
}
if (isSessionObject) {
SFTKSessionObject *so = (SFTKSessionObject *)object;
PZ_DestroyLock(so->attributeLock);
so->attributeLock = NULL;
}
if (object->refLock) {
PZ_DestroyLock(object->refLock);
object->refLock = NULL;
}
PORT_Free(object);
}
static SFTKObject *
sftk_freeObjectData(SFTKObject *object)
{
SFTKObject *next = object->next;
PORT_Free(object);
return next;
}