/
pk11obj.c
2267 lines (2047 loc) · 67.4 KB
/
pk11obj.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/. */
/*
* This file manages object type indepentent functions.
*/
#include <limits.h>
#include <stddef.h>
#include "seccomon.h"
#include "secmod.h"
#include "secmodi.h"
#include "secmodti.h"
#include "pkcs11.h"
#include "pkcs11t.h"
#include "pk11func.h"
#include "keyhi.h"
#include "secitem.h"
#include "secerr.h"
#include "sslerr.h"
#define PK11_SEARCH_CHUNKSIZE 10
/*
* Build a block big enough to hold the data
*/
SECItem *
PK11_BlockData(SECItem *data, unsigned long size)
{
SECItem *newData;
if (size == 0u)
return NULL;
newData = (SECItem *)PORT_Alloc(sizeof(SECItem));
if (newData == NULL)
return NULL;
newData->len = (data->len + (size - 1)) / size;
newData->len *= size;
newData->data = (unsigned char *)PORT_ZAlloc(newData->len);
if (newData->data == NULL) {
PORT_Free(newData);
return NULL;
}
PORT_Memset(newData->data, newData->len - data->len, newData->len);
PORT_Memcpy(newData->data, data->data, data->len);
return newData;
}
SECStatus
PK11_DestroyObject(PK11SlotInfo *slot, CK_OBJECT_HANDLE object)
{
CK_RV crv;
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_DestroyObject(slot->session, object);
PK11_ExitSlotMonitor(slot);
if (crv != CKR_OK) {
return SECFailure;
}
return SECSuccess;
}
SECStatus
PK11_DestroyTokenObject(PK11SlotInfo *slot, CK_OBJECT_HANDLE object)
{
CK_RV crv;
SECStatus rv = SECSuccess;
CK_SESSION_HANDLE rwsession;
rwsession = PK11_GetRWSession(slot);
if (rwsession == CK_INVALID_HANDLE) {
PORT_SetError(SEC_ERROR_BAD_DATA);
return SECFailure;
}
crv = PK11_GETTAB(slot)->C_DestroyObject(rwsession, object);
if (crv != CKR_OK) {
rv = SECFailure;
PORT_SetError(PK11_MapError(crv));
}
PK11_RestoreROSession(slot, rwsession);
return rv;
}
/*
* Read in a single attribute into a SECItem. Allocate space for it with
* PORT_Alloc unless an arena is supplied. In the latter case use the arena
* to allocate the space.
*
* PK11_ReadAttribute sets the 'data' and 'len' fields of the SECItem but
* does not modify its 'type' field.
*/
SECStatus
PK11_ReadAttribute(PK11SlotInfo *slot, CK_OBJECT_HANDLE id,
CK_ATTRIBUTE_TYPE type, PLArenaPool *arena, SECItem *result)
{
CK_ATTRIBUTE attr = { 0, NULL, 0 };
CK_RV crv;
attr.type = type;
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_GetAttributeValue(slot->session, id, &attr, 1);
if (crv != CKR_OK) {
PK11_ExitSlotMonitor(slot);
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
if (arena) {
attr.pValue = PORT_ArenaAlloc(arena, attr.ulValueLen);
} else {
attr.pValue = PORT_Alloc(attr.ulValueLen);
}
if (attr.pValue == NULL) {
PK11_ExitSlotMonitor(slot);
return SECFailure;
}
crv = PK11_GETTAB(slot)->C_GetAttributeValue(slot->session, id, &attr, 1);
PK11_ExitSlotMonitor(slot);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
if (!arena)
PORT_Free(attr.pValue);
return SECFailure;
}
result->data = (unsigned char *)attr.pValue;
result->len = attr.ulValueLen;
return SECSuccess;
}
/*
* Read in a single attribute into As a Ulong.
*/
CK_ULONG
PK11_ReadULongAttribute(PK11SlotInfo *slot, CK_OBJECT_HANDLE id,
CK_ATTRIBUTE_TYPE type)
{
CK_ATTRIBUTE attr;
CK_ULONG value = CK_UNAVAILABLE_INFORMATION;
CK_RV crv;
PK11_SETATTRS(&attr, type, &value, sizeof(value));
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_GetAttributeValue(slot->session, id, &attr, 1);
PK11_ExitSlotMonitor(slot);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
}
return value;
}
/*
* check to see if a bool has been set.
*/
CK_BBOOL
pk11_HasAttributeSet_Lock(PK11SlotInfo *slot, CK_OBJECT_HANDLE id,
CK_ATTRIBUTE_TYPE type, PRBool haslock)
{
CK_BBOOL ckvalue = CK_FALSE;
CK_ATTRIBUTE theTemplate;
CK_RV crv;
/* Prepare to retrieve the attribute. */
PK11_SETATTRS(&theTemplate, type, &ckvalue, sizeof(CK_BBOOL));
/* Retrieve attribute value. */
if (!haslock)
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_GetAttributeValue(slot->session, id,
&theTemplate, 1);
if (!haslock)
PK11_ExitSlotMonitor(slot);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return CK_FALSE;
}
return ckvalue;
}
CK_BBOOL
PK11_HasAttributeSet(PK11SlotInfo *slot, CK_OBJECT_HANDLE id,
CK_ATTRIBUTE_TYPE type, PRBool haslock)
{
PR_ASSERT(haslock == PR_FALSE);
return pk11_HasAttributeSet_Lock(slot, id, type, PR_FALSE);
}
/*
* returns a full list of attributes. Allocate space for them. If an arena is
* provided, allocate space out of the arena.
*/
CK_RV
PK11_GetAttributes(PLArenaPool *arena, PK11SlotInfo *slot,
CK_OBJECT_HANDLE obj, CK_ATTRIBUTE *attr, int count)
{
int i;
/* make pedantic happy... note that it's only used arena != NULL */
void *mark = NULL;
CK_RV crv;
if (slot->session == CK_INVALID_HANDLE)
return CKR_SESSION_HANDLE_INVALID;
/*
* first get all the lengths of the parameters.
*/
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_GetAttributeValue(slot->session, obj, attr, count);
if (crv != CKR_OK) {
PK11_ExitSlotMonitor(slot);
return crv;
}
if (arena) {
mark = PORT_ArenaMark(arena);
if (mark == NULL)
return CKR_HOST_MEMORY;
}
/*
* now allocate space to store the results.
*/
for (i = 0; i < count; i++) {
if (attr[i].ulValueLen == 0)
continue;
if (arena) {
attr[i].pValue = PORT_ArenaAlloc(arena, attr[i].ulValueLen);
if (attr[i].pValue == NULL) {
/* arena failures, just release the mark */
PORT_ArenaRelease(arena, mark);
PK11_ExitSlotMonitor(slot);
return CKR_HOST_MEMORY;
}
} else {
attr[i].pValue = PORT_Alloc(attr[i].ulValueLen);
if (attr[i].pValue == NULL) {
/* Separate malloc failures, loop to release what we have
* so far */
int j;
for (j = 0; j < i; j++) {
PORT_Free(attr[j].pValue);
/* don't give the caller pointers to freed memory */
attr[j].pValue = NULL;
}
PK11_ExitSlotMonitor(slot);
return CKR_HOST_MEMORY;
}
}
}
/*
* finally get the results.
*/
crv = PK11_GETTAB(slot)->C_GetAttributeValue(slot->session, obj, attr, count);
PK11_ExitSlotMonitor(slot);
if (crv != CKR_OK) {
if (arena) {
PORT_ArenaRelease(arena, mark);
} else {
for (i = 0; i < count; i++) {
PORT_Free(attr[i].pValue);
/* don't give the caller pointers to freed memory */
attr[i].pValue = NULL;
}
}
} else if (arena && mark) {
PORT_ArenaUnmark(arena, mark);
}
return crv;
}
PRBool
PK11_IsPermObject(PK11SlotInfo *slot, CK_OBJECT_HANDLE handle)
{
return (PRBool)PK11_HasAttributeSet(slot, handle, CKA_TOKEN, PR_FALSE);
}
char *
PK11_GetObjectNickname(PK11SlotInfo *slot, CK_OBJECT_HANDLE id)
{
char *nickname = NULL;
SECItem result;
SECStatus rv;
rv = PK11_ReadAttribute(slot, id, CKA_LABEL, NULL, &result);
if (rv != SECSuccess) {
return NULL;
}
nickname = PORT_ZAlloc(result.len + 1);
if (nickname == NULL) {
PORT_Free(result.data);
return NULL;
}
PORT_Memcpy(nickname, result.data, result.len);
PORT_Free(result.data);
return nickname;
}
SECStatus
PK11_SetObjectNickname(PK11SlotInfo *slot, CK_OBJECT_HANDLE id,
const char *nickname)
{
int len = PORT_Strlen(nickname);
CK_ATTRIBUTE setTemplate;
CK_RV crv;
CK_SESSION_HANDLE rwsession;
if (len < 0) {
return SECFailure;
}
PK11_SETATTRS(&setTemplate, CKA_LABEL, (CK_CHAR *)nickname, len);
rwsession = PK11_GetRWSession(slot);
if (rwsession == CK_INVALID_HANDLE) {
PORT_SetError(SEC_ERROR_BAD_DATA);
return SECFailure;
}
crv = PK11_GETTAB(slot)->C_SetAttributeValue(rwsession, id,
&setTemplate, 1);
PK11_RestoreROSession(slot, rwsession);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
return SECSuccess;
}
/*
* strip leading zero's from key material
*/
void
pk11_SignedToUnsigned(CK_ATTRIBUTE *attrib)
{
char *ptr = (char *)attrib->pValue;
unsigned long len = attrib->ulValueLen;
while ((len > 1) && (*ptr == 0)) {
len--;
ptr++;
}
attrib->pValue = ptr;
attrib->ulValueLen = len;
}
/*
* get a new session on a slot. If we run out of session, use the slot's
* 'exclusive' session. In this case owner becomes false.
*/
CK_SESSION_HANDLE
pk11_GetNewSession(PK11SlotInfo *slot, PRBool *owner)
{
CK_SESSION_HANDLE session;
*owner = PR_TRUE;
if (!slot->isThreadSafe)
PK11_EnterSlotMonitor(slot);
if (PK11_GETTAB(slot)->C_OpenSession(slot->slotID, CKF_SERIAL_SESSION,
slot, pk11_notify, &session) != CKR_OK) {
*owner = PR_FALSE;
session = slot->session;
}
if (!slot->isThreadSafe)
PK11_ExitSlotMonitor(slot);
return session;
}
void
pk11_CloseSession(PK11SlotInfo *slot, CK_SESSION_HANDLE session, PRBool owner)
{
if (!owner)
return;
if (!slot->isThreadSafe)
PK11_EnterSlotMonitor(slot);
(void)PK11_GETTAB(slot)->C_CloseSession(session);
if (!slot->isThreadSafe)
PK11_ExitSlotMonitor(slot);
}
SECStatus
PK11_CreateNewObject(PK11SlotInfo *slot, CK_SESSION_HANDLE session,
const CK_ATTRIBUTE *theTemplate, int count,
PRBool token, CK_OBJECT_HANDLE *objectID)
{
CK_SESSION_HANDLE rwsession;
CK_RV crv;
SECStatus rv = SECSuccess;
rwsession = session;
if (token) {
rwsession = PK11_GetRWSession(slot);
} else if (rwsession == CK_INVALID_HANDLE) {
rwsession = slot->session;
if (rwsession != CK_INVALID_HANDLE)
PK11_EnterSlotMonitor(slot);
}
if (rwsession == CK_INVALID_HANDLE) {
PORT_SetError(SEC_ERROR_BAD_DATA);
return SECFailure;
}
crv = PK11_GETTAB(slot)->C_CreateObject(rwsession,
/* cast away const :-( */ (CK_ATTRIBUTE_PTR)theTemplate,
count, objectID);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
rv = SECFailure;
}
if (token) {
PK11_RestoreROSession(slot, rwsession);
} else if (session == CK_INVALID_HANDLE) {
PK11_ExitSlotMonitor(slot);
}
return rv;
}
/* This function may add a maximum of 9 attributes. */
unsigned int
pk11_OpFlagsToAttributes(CK_FLAGS flags, CK_ATTRIBUTE *attrs, CK_BBOOL *ckTrue)
{
const static CK_ATTRIBUTE_TYPE attrTypes[12] = {
CKA_ENCRYPT, CKA_DECRYPT, 0 /* DIGEST */, CKA_SIGN,
CKA_SIGN_RECOVER, CKA_VERIFY, CKA_VERIFY_RECOVER, 0 /* GEN */,
0 /* GEN PAIR */, CKA_WRAP, CKA_UNWRAP, CKA_DERIVE
};
const CK_ATTRIBUTE_TYPE *pType = attrTypes;
CK_ATTRIBUTE *attr = attrs;
CK_FLAGS test = CKF_ENCRYPT;
PR_ASSERT(!(flags & ~CKF_KEY_OPERATION_FLAGS));
flags &= CKF_KEY_OPERATION_FLAGS;
for (; flags && test <= CKF_DERIVE; test <<= 1, ++pType) {
if (test & flags) {
flags ^= test;
PR_ASSERT(*pType);
PK11_SETATTRS(attr, *pType, ckTrue, sizeof *ckTrue);
++attr;
}
}
return (attr - attrs);
}
/*
* Check for conflicting flags, for example, if both PK11_ATTR_PRIVATE
* and PK11_ATTR_PUBLIC are set.
*/
PRBool
pk11_BadAttrFlags(PK11AttrFlags attrFlags)
{
PK11AttrFlags trueFlags = attrFlags & 0x55555555;
PK11AttrFlags falseFlags = (attrFlags >> 1) & 0x55555555;
return ((trueFlags & falseFlags) != 0);
}
/*
* This function may add a maximum of 5 attributes.
* The caller must make sure the attribute flags don't have conflicts.
*/
unsigned int
pk11_AttrFlagsToAttributes(PK11AttrFlags attrFlags, CK_ATTRIBUTE *attrs,
CK_BBOOL *ckTrue, CK_BBOOL *ckFalse)
{
const static CK_ATTRIBUTE_TYPE attrTypes[5] = {
CKA_TOKEN, CKA_PRIVATE, CKA_MODIFIABLE, CKA_SENSITIVE,
CKA_EXTRACTABLE
};
const CK_ATTRIBUTE_TYPE *pType = attrTypes;
CK_ATTRIBUTE *attr = attrs;
PK11AttrFlags test = PK11_ATTR_TOKEN;
PR_ASSERT(!pk11_BadAttrFlags(attrFlags));
/* we test two related bitflags in each iteration */
for (; attrFlags && test <= PK11_ATTR_EXTRACTABLE; test <<= 2, ++pType) {
if (test & attrFlags) {
attrFlags ^= test;
PK11_SETATTRS(attr, *pType, ckTrue, sizeof *ckTrue);
++attr;
} else if ((test << 1) & attrFlags) {
attrFlags ^= (test << 1);
PK11_SETATTRS(attr, *pType, ckFalse, sizeof *ckFalse);
++attr;
}
}
return (attr - attrs);
}
/*
* Some non-compliant PKCS #11 vendors do not give us the modulus, so actually
* set up a signature to get the signaure length.
*/
static int
pk11_backupGetSignLength(SECKEYPrivateKey *key)
{
PK11SlotInfo *slot = key->pkcs11Slot;
CK_MECHANISM mech = { 0, NULL, 0 };
PRBool owner = PR_TRUE;
CK_SESSION_HANDLE session;
CK_ULONG len;
CK_RV crv;
unsigned char h_data[20] = { 0 };
unsigned char buf[20]; /* obviously to small */
CK_ULONG smallLen = sizeof(buf);
mech.mechanism = PK11_MapSignKeyType(key->keyType);
session = pk11_GetNewSession(slot, &owner);
if (!owner || !(slot->isThreadSafe))
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_SignInit(session, &mech, key->pkcs11ID);
if (crv != CKR_OK) {
if (!owner || !(slot->isThreadSafe))
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PORT_SetError(PK11_MapError(crv));
return -1;
}
len = 0;
crv = PK11_GETTAB(slot)->C_Sign(session, h_data, sizeof(h_data),
NULL, &len);
/* now call C_Sign with too small a buffer to clear the session state */
(void)PK11_GETTAB(slot)->C_Sign(session, h_data, sizeof(h_data), buf, &smallLen);
if (!owner || !(slot->isThreadSafe))
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return -1;
}
return len;
}
/*
* get the length of a signature object based on the key
*/
int
PK11_SignatureLen(SECKEYPrivateKey *key)
{
int val;
SECItem attributeItem = { siBuffer, NULL, 0 };
SECStatus rv;
int length;
switch (key->keyType) {
case rsaKey:
val = PK11_GetPrivateModulusLen(key);
if (val == -1) {
return pk11_backupGetSignLength(key);
}
return (unsigned long)val;
case fortezzaKey:
return 40;
case dsaKey:
rv = PK11_ReadAttribute(key->pkcs11Slot, key->pkcs11ID, CKA_SUBPRIME,
NULL, &attributeItem);
if (rv == SECSuccess) {
length = attributeItem.len;
if ((length > 0) && attributeItem.data[0] == 0) {
length--;
}
PORT_Free(attributeItem.data);
return length * 2;
}
return pk11_backupGetSignLength(key);
case ecKey:
rv = PK11_ReadAttribute(key->pkcs11Slot, key->pkcs11ID, CKA_EC_PARAMS,
NULL, &attributeItem);
if (rv == SECSuccess) {
length = SECKEY_ECParamsToBasePointOrderLen(&attributeItem);
PORT_Free(attributeItem.data);
if (length != 0) {
length = ((length + 7) / 8) * 2;
return length;
}
}
return pk11_backupGetSignLength(key);
default:
break;
}
PORT_SetError(SEC_ERROR_INVALID_KEY);
return 0;
}
/*
* copy a key (or any other object) on a token
*/
CK_OBJECT_HANDLE
PK11_CopyKey(PK11SlotInfo *slot, CK_OBJECT_HANDLE srcObject)
{
CK_OBJECT_HANDLE destObject;
CK_RV crv;
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_CopyObject(slot->session, srcObject, NULL, 0,
&destObject);
PK11_ExitSlotMonitor(slot);
if (crv == CKR_OK)
return destObject;
PORT_SetError(PK11_MapError(crv));
return CK_INVALID_HANDLE;
}
PRBool
pk11_FindAttrInTemplate(CK_ATTRIBUTE *attr, unsigned int numAttrs,
CK_ATTRIBUTE_TYPE target)
{
for (; numAttrs > 0; ++attr, --numAttrs) {
if (attr->type == target)
return PR_TRUE;
}
return PR_FALSE;
}
/*
* Recover the Signed data. We need this because our old verify can't
* figure out which hash algorithm to use until we decryptted this.
*/
SECStatus
PK11_VerifyRecover(SECKEYPublicKey *key, const SECItem *sig,
SECItem *dsig, void *wincx)
{
PK11SlotInfo *slot = key->pkcs11Slot;
CK_OBJECT_HANDLE id = key->pkcs11ID;
CK_MECHANISM mech = { 0, NULL, 0 };
PRBool owner = PR_TRUE;
CK_SESSION_HANDLE session;
CK_ULONG len;
CK_RV crv;
mech.mechanism = PK11_MapSignKeyType(key->keyType);
if (slot == NULL) {
slot = PK11_GetBestSlotWithAttributes(mech.mechanism,
CKF_VERIFY_RECOVER, 0, wincx);
if (slot == NULL) {
PORT_SetError(SEC_ERROR_NO_MODULE);
return SECFailure;
}
id = PK11_ImportPublicKey(slot, key, PR_FALSE);
} else {
PK11_ReferenceSlot(slot);
}
if (id == CK_INVALID_HANDLE) {
PK11_FreeSlot(slot);
PORT_SetError(SEC_ERROR_BAD_KEY);
return SECFailure;
}
session = pk11_GetNewSession(slot, &owner);
if (!owner || !(slot->isThreadSafe))
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_VerifyRecoverInit(session, &mech, id);
if (crv != CKR_OK) {
if (!owner || !(slot->isThreadSafe))
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PORT_SetError(PK11_MapError(crv));
PK11_FreeSlot(slot);
return SECFailure;
}
len = dsig->len;
crv = PK11_GETTAB(slot)->C_VerifyRecover(session, sig->data,
sig->len, dsig->data, &len);
if (!owner || !(slot->isThreadSafe))
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
dsig->len = len;
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
PK11_FreeSlot(slot);
return SECFailure;
}
PK11_FreeSlot(slot);
return SECSuccess;
}
/*
* verify a signature from its hash.
*/
SECStatus
PK11_Verify(SECKEYPublicKey *key, const SECItem *sig, const SECItem *hash,
void *wincx)
{
CK_MECHANISM_TYPE mech = PK11_MapSignKeyType(key->keyType);
return PK11_VerifyWithMechanism(key, mech, NULL, sig, hash, wincx);
}
/*
* Verify a signature from its hash using the given algorithm.
*/
SECStatus
PK11_VerifyWithMechanism(SECKEYPublicKey *key, CK_MECHANISM_TYPE mechanism,
const SECItem *param, const SECItem *sig,
const SECItem *hash, void *wincx)
{
PK11SlotInfo *slot = key->pkcs11Slot;
CK_OBJECT_HANDLE id = key->pkcs11ID;
CK_MECHANISM mech = { 0, NULL, 0 };
PRBool owner = PR_TRUE;
CK_SESSION_HANDLE session;
CK_RV crv;
mech.mechanism = mechanism;
if (param) {
mech.pParameter = param->data;
mech.ulParameterLen = param->len;
}
if (slot == NULL) {
unsigned int length = 0;
if ((mech.mechanism == CKM_DSA) &&
/* 129 is 1024 bits translated to bytes and
* padded with an optional '0' to maintain a
* positive sign */
(key->u.dsa.params.prime.len > 129)) {
/* we need to get a slot that not only can do DSA, but can do DSA2
* key lengths */
length = key->u.dsa.params.prime.len;
if (key->u.dsa.params.prime.data[0] == 0) {
length--;
}
/* convert keysize to bits for slot lookup */
length *= 8;
}
slot = PK11_GetBestSlotWithAttributes(mech.mechanism,
CKF_VERIFY, length, wincx);
if (slot == NULL) {
PORT_SetError(SEC_ERROR_NO_MODULE);
return SECFailure;
}
id = PK11_ImportPublicKey(slot, key, PR_FALSE);
} else {
PK11_ReferenceSlot(slot);
}
if (id == CK_INVALID_HANDLE) {
PK11_FreeSlot(slot);
PORT_SetError(SEC_ERROR_BAD_KEY);
return SECFailure;
}
session = pk11_GetNewSession(slot, &owner);
if (!owner || !(slot->isThreadSafe))
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_VerifyInit(session, &mech, id);
if (crv != CKR_OK) {
if (!owner || !(slot->isThreadSafe))
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PK11_FreeSlot(slot);
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
crv = PK11_GETTAB(slot)->C_Verify(session, hash->data,
hash->len, sig->data, sig->len);
if (!owner || !(slot->isThreadSafe))
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PK11_FreeSlot(slot);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
return SECSuccess;
}
/*
* sign a hash. The algorithm is determined by the key.
*/
SECStatus
PK11_Sign(SECKEYPrivateKey *key, SECItem *sig, const SECItem *hash)
{
CK_MECHANISM_TYPE mech = PK11_MapSignKeyType(key->keyType);
return PK11_SignWithMechanism(key, mech, NULL, sig, hash);
}
/*
* Sign a hash using the given algorithm.
*/
SECStatus
PK11_SignWithMechanism(SECKEYPrivateKey *key, CK_MECHANISM_TYPE mechanism,
const SECItem *param, SECItem *sig, const SECItem *hash)
{
PK11SlotInfo *slot = key->pkcs11Slot;
CK_MECHANISM mech = { 0, NULL, 0 };
PRBool owner = PR_TRUE;
CK_SESSION_HANDLE session;
PRBool haslock = PR_FALSE;
CK_ULONG len;
CK_RV crv;
mech.mechanism = mechanism;
if (param) {
mech.pParameter = param->data;
mech.ulParameterLen = param->len;
}
if (SECKEY_HAS_ATTRIBUTE_SET(key, CKA_PRIVATE)) {
PK11_HandlePasswordCheck(slot, key->wincx);
}
session = pk11_GetNewSession(slot, &owner);
haslock = (!owner || !(slot->isThreadSafe));
if (haslock)
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_SignInit(session, &mech, key->pkcs11ID);
if (crv != CKR_OK) {
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
/* PKCS11 2.20 says if CKA_ALWAYS_AUTHENTICATE then
* do C_Login with CKU_CONTEXT_SPECIFIC
* between C_SignInit and C_Sign */
if (SECKEY_HAS_ATTRIBUTE_SET_LOCK(key, CKA_ALWAYS_AUTHENTICATE, haslock)) {
PK11_DoPassword(slot, session, PR_FALSE, key->wincx, haslock, PR_TRUE);
}
len = sig->len;
crv = PK11_GETTAB(slot)->C_Sign(session, hash->data,
hash->len, sig->data, &len);
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
sig->len = len;
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
return SECSuccess;
}
/*
* sign data with a MAC key.
*/
SECStatus
PK11_SignWithSymKey(PK11SymKey *symKey, CK_MECHANISM_TYPE mechanism,
SECItem *param, SECItem *sig, const SECItem *data)
{
PK11SlotInfo *slot = symKey->slot;
CK_MECHANISM mech = { 0, NULL, 0 };
PRBool owner = PR_TRUE;
CK_SESSION_HANDLE session;
PRBool haslock = PR_FALSE;
CK_ULONG len;
CK_RV crv;
mech.mechanism = mechanism;
if (param) {
mech.pParameter = param->data;
mech.ulParameterLen = param->len;
}
session = pk11_GetNewSession(slot, &owner);
haslock = (!owner || !(slot->isThreadSafe));
if (haslock)
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_SignInit(session, &mech, symKey->objectID);
if (crv != CKR_OK) {
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
len = sig->len;
crv = PK11_GETTAB(slot)->C_Sign(session, data->data,
data->len, sig->data, &len);
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
sig->len = len;
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
return SECSuccess;
}
SECStatus
PK11_Decrypt(PK11SymKey *symKey,
CK_MECHANISM_TYPE mechanism, SECItem *param,
unsigned char *out, unsigned int *outLen,
unsigned int maxLen,
const unsigned char *enc, unsigned encLen)
{
PK11SlotInfo *slot = symKey->slot;
CK_MECHANISM mech = { 0, NULL, 0 };
CK_ULONG len = maxLen;
PRBool owner = PR_TRUE;
CK_SESSION_HANDLE session;
PRBool haslock = PR_FALSE;
CK_RV crv;
mech.mechanism = mechanism;
if (param) {
mech.pParameter = param->data;
mech.ulParameterLen = param->len;
}
session = pk11_GetNewSession(slot, &owner);
haslock = (!owner || !slot->isThreadSafe);
if (haslock)
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_DecryptInit(session, &mech, symKey->objectID);
if (crv != CKR_OK) {
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
crv = PK11_GETTAB(slot)->C_Decrypt(session, (unsigned char *)enc, encLen,
out, &len);
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
*outLen = len;
return SECSuccess;
}
SECStatus
PK11_Encrypt(PK11SymKey *symKey,
CK_MECHANISM_TYPE mechanism, SECItem *param,
unsigned char *out, unsigned int *outLen,
unsigned int maxLen,
const unsigned char *data, unsigned int dataLen)
{
PK11SlotInfo *slot = symKey->slot;
CK_MECHANISM mech = { 0, NULL, 0 };
CK_ULONG len = maxLen;
PRBool owner = PR_TRUE;
CK_SESSION_HANDLE session;
PRBool haslock = PR_FALSE;
CK_RV crv;
mech.mechanism = mechanism;
if (param) {
mech.pParameter = param->data;
mech.ulParameterLen = param->len;
}
session = pk11_GetNewSession(slot, &owner);
haslock = (!owner || !slot->isThreadSafe);
if (haslock)
PK11_EnterSlotMonitor(slot);
crv = PK11_GETTAB(slot)->C_EncryptInit(session, &mech, symKey->objectID);
if (crv != CKR_OK) {
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
crv = PK11_GETTAB(slot)->C_Encrypt(session, (unsigned char *)data,
dataLen, out, &len);
if (haslock)
PK11_ExitSlotMonitor(slot);
pk11_CloseSession(slot, session, owner);
if (crv != CKR_OK) {
PORT_SetError(PK11_MapError(crv));
return SECFailure;
}
*outLen = len;
return SECSuccess;
}
static SECStatus
pk11_PrivDecryptRaw(SECKEYPrivateKey *key,
unsigned char *data, unsigned *outLen, unsigned int maxLen,
const unsigned char *enc, unsigned encLen,
CK_MECHANISM_PTR mech)
{
PK11SlotInfo *slot = key->pkcs11Slot;
CK_ULONG out = maxLen;