/
sftkdb.c
2989 lines (2722 loc) · 95.4 KB
/
sftkdb.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/. */
/*
* The following code handles the storage of PKCS 11 modules used by the
* NSS. For the rest of NSS, only one kind of database handle exists:
*
* SFTKDBHandle
*
* There is one SFTKDBHandle for the each key database and one for each cert
* database. These databases are opened as associated pairs, one pair per
* slot. SFTKDBHandles are reference counted objects.
*
* Each SFTKDBHandle points to a low level database handle (SDB). This handle
* represents the underlying physical database. These objects are not
* reference counted, an are 'owned' by their respective SFTKDBHandles.
*
*
*/
#include "sftkdb.h"
#include "sftkdbti.h"
#include "pkcs11t.h"
#include "pkcs11i.h"
#include "sdb.h"
#include "prprf.h"
#include "pratom.h"
#include "lgglue.h"
#include "utilpars.h"
#include "secerr.h"
#include "softoken.h"
#if defined(_WIN32)
#include <windows.h>
#endif
/*
* We want all databases to have the same binary representation independent of
* endianness or length of the host architecture. In general PKCS #11 attributes
* are endian/length independent except those attributes that pass CK_ULONG.
*
* The following functions fixes up the CK_ULONG type attributes so that the data
* base sees a machine independent view. CK_ULONGs are stored as 4 byte network
* byte order values (big endian).
*/
#define BBP 8
PRBool
sftkdb_isULONGAttribute(CK_ATTRIBUTE_TYPE type)
{
switch (type) {
case CKA_CERTIFICATE_CATEGORY:
case CKA_CERTIFICATE_TYPE:
case CKA_CLASS:
case CKA_JAVA_MIDP_SECURITY_DOMAIN:
case CKA_KEY_GEN_MECHANISM:
case CKA_KEY_TYPE:
case CKA_MECHANISM_TYPE:
case CKA_MODULUS_BITS:
case CKA_PRIME_BITS:
case CKA_SUBPRIME_BITS:
case CKA_VALUE_BITS:
case CKA_VALUE_LEN:
case CKA_TRUST_DIGITAL_SIGNATURE:
case CKA_TRUST_NON_REPUDIATION:
case CKA_TRUST_KEY_ENCIPHERMENT:
case CKA_TRUST_DATA_ENCIPHERMENT:
case CKA_TRUST_KEY_AGREEMENT:
case CKA_TRUST_KEY_CERT_SIGN:
case CKA_TRUST_CRL_SIGN:
case CKA_TRUST_SERVER_AUTH:
case CKA_TRUST_CLIENT_AUTH:
case CKA_TRUST_CODE_SIGNING:
case CKA_TRUST_EMAIL_PROTECTION:
case CKA_TRUST_IPSEC_END_SYSTEM:
case CKA_TRUST_IPSEC_TUNNEL:
case CKA_TRUST_IPSEC_USER:
case CKA_TRUST_TIME_STAMPING:
case CKA_TRUST_STEP_UP_APPROVED:
return PR_TRUE;
default:
break;
}
return PR_FALSE;
}
/* are the attributes private? */
static PRBool
sftkdb_isPrivateAttribute(CK_ATTRIBUTE_TYPE type)
{
switch (type) {
case CKA_VALUE:
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;
default:
break;
}
return PR_FALSE;
}
/* These attributes must be authenticated with an hmac. */
static PRBool
sftkdb_isAuthenticatedAttribute(CK_ATTRIBUTE_TYPE type)
{
switch (type) {
case CKA_MODULUS:
case CKA_PUBLIC_EXPONENT:
case CKA_CERT_SHA1_HASH:
case CKA_CERT_MD5_HASH:
case CKA_TRUST_SERVER_AUTH:
case CKA_TRUST_CLIENT_AUTH:
case CKA_TRUST_EMAIL_PROTECTION:
case CKA_TRUST_CODE_SIGNING:
case CKA_TRUST_STEP_UP_APPROVED:
case CKA_NSS_OVERRIDE_EXTENSIONS:
return PR_TRUE;
default:
break;
}
return PR_FALSE;
}
/*
* convert a native ULONG to a database ulong. Database ulong's
* are all 4 byte big endian values.
*/
void
sftk_ULong2SDBULong(unsigned char *data, CK_ULONG value)
{
int i;
for (i = 0; i < SDB_ULONG_SIZE; i++) {
data[i] = (value >> (SDB_ULONG_SIZE - 1 - i) * BBP) & 0xff;
}
}
/*
* convert a database ulong back to a native ULONG. (reverse of the above
* function.
*/
static CK_ULONG
sftk_SDBULong2ULong(unsigned char *data)
{
int i;
CK_ULONG value = 0;
for (i = 0; i < SDB_ULONG_SIZE; i++) {
value |= (((CK_ULONG)data[i]) << (SDB_ULONG_SIZE - 1 - i) * BBP);
}
return value;
}
/*
* fix up the input templates. Our fixed up ints are stored in data and must
* be freed by the caller. The new template must also be freed. If there are no
* CK_ULONG attributes, the orignal template is passed in as is.
*/
static CK_ATTRIBUTE *
sftkdb_fixupTemplateIn(const CK_ATTRIBUTE *template, int count,
unsigned char **dataOut)
{
int i;
int ulongCount = 0;
unsigned char *data;
CK_ATTRIBUTE *ntemplate;
*dataOut = NULL;
/* first count the number of CK_ULONG attributes */
for (i = 0; i < count; i++) {
/* Don't 'fixup' NULL values */
if (!template[i].pValue) {
continue;
}
if (template[i].ulValueLen == sizeof(CK_ULONG)) {
if (sftkdb_isULONGAttribute(template[i].type)) {
ulongCount++;
}
}
}
/* no attributes to fixup, just call on through */
if (ulongCount == 0) {
return (CK_ATTRIBUTE *)template;
}
/* allocate space for new ULONGS */
data = (unsigned char *)PORT_Alloc(SDB_ULONG_SIZE * ulongCount);
if (!data) {
return NULL;
}
/* allocate new template */
ntemplate = PORT_NewArray(CK_ATTRIBUTE, count);
if (!ntemplate) {
PORT_Free(data);
return NULL;
}
*dataOut = data;
/* copy the old template, fixup the actual ulongs */
for (i = 0; i < count; i++) {
ntemplate[i] = template[i];
/* Don't 'fixup' NULL values */
if (!template[i].pValue) {
continue;
}
if (template[i].ulValueLen == sizeof(CK_ULONG)) {
if (sftkdb_isULONGAttribute(template[i].type)) {
CK_ULONG value = *(CK_ULONG *)template[i].pValue;
sftk_ULong2SDBULong(data, value);
ntemplate[i].pValue = data;
ntemplate[i].ulValueLen = SDB_ULONG_SIZE;
data += SDB_ULONG_SIZE;
}
}
}
return ntemplate;
}
static const char SFTKDB_META_SIG_TEMPLATE[] = "sig_%s_%08x_%08x";
/*
* return a string describing the database type (key or cert)
*/
const char *
sftkdb_TypeString(SFTKDBHandle *handle)
{
return (handle->type == SFTK_KEYDB_TYPE) ? "key" : "cert";
}
/*
* Some attributes are signed with an Hmac and a pbe key generated from
* the password. This signature is stored indexed by object handle and
* attribute type in the meta data table in the key database.
*
* Signature entries are indexed by the string
* sig_[cert/key]_{ObjectID}_{Attribute}
*
* This function fetches that pkcs5 signature. Caller supplies a SECItem
* pre-allocated to the appropriate size if the SECItem is too small the
* function will fail with CKR_BUFFER_TOO_SMALL.
*/
static CK_RV
sftkdb_getRawAttributeSignature(SFTKDBHandle *handle, SDB *db,
CK_OBJECT_HANDLE objectID,
CK_ATTRIBUTE_TYPE type,
SECItem *signText)
{
char id[30];
CK_RV crv;
sprintf(id, SFTKDB_META_SIG_TEMPLATE,
sftkdb_TypeString(handle),
(unsigned int)objectID, (unsigned int)type);
crv = (*db->sdb_GetMetaData)(db, id, signText, NULL);
return crv;
}
CK_RV
sftkdb_GetAttributeSignature(SFTKDBHandle *handle, SFTKDBHandle *keyHandle,
CK_OBJECT_HANDLE objectID, CK_ATTRIBUTE_TYPE type,
SECItem *signText)
{
SDB *db = SFTK_GET_SDB(keyHandle);
return sftkdb_getRawAttributeSignature(handle, db, objectID, type, signText);
}
CK_RV
sftkdb_DestroyAttributeSignature(SFTKDBHandle *handle, SDB *db,
CK_OBJECT_HANDLE objectID,
CK_ATTRIBUTE_TYPE type)
{
char id[30];
CK_RV crv;
sprintf(id, SFTKDB_META_SIG_TEMPLATE,
sftkdb_TypeString(handle),
(unsigned int)objectID, (unsigned int)type);
crv = (*db->sdb_DestroyMetaData)(db, id);
return crv;
}
/*
* Some attributes are signed with an Hmac and a pbe key generated from
* the password. This signature is stored indexed by object handle and
* attribute type in the meta data table in the key database.
*
* Signature entries are indexed by the string
* sig_[cert/key]_{ObjectID}_{Attribute}
*
* This function stores that pkcs5 signature.
*/
CK_RV
sftkdb_PutAttributeSignature(SFTKDBHandle *handle, SDB *keyTarget,
CK_OBJECT_HANDLE objectID, CK_ATTRIBUTE_TYPE type,
SECItem *signText)
{
char id[30];
CK_RV crv;
sprintf(id, SFTKDB_META_SIG_TEMPLATE,
sftkdb_TypeString(handle),
(unsigned int)objectID, (unsigned int)type);
crv = (*keyTarget->sdb_PutMetaData)(keyTarget, id, signText, NULL);
return crv;
}
/*
* fix up returned data. NOTE: sftkdb_fixupTemplateIn has already allocated
* separate data sections for the database ULONG values.
*/
static CK_RV
sftkdb_fixupTemplateOut(CK_ATTRIBUTE *template, CK_OBJECT_HANDLE objectID,
CK_ATTRIBUTE *ntemplate, int count, SFTKDBHandle *handle)
{
int i;
CK_RV crv = CKR_OK;
SFTKDBHandle *keyHandle;
PRBool checkSig = PR_TRUE;
PRBool checkEnc = PR_TRUE;
PORT_Assert(handle);
/* find the key handle */
keyHandle = handle;
if (handle->type != SFTK_KEYDB_TYPE) {
checkEnc = PR_FALSE;
keyHandle = handle->peerDB;
}
if ((keyHandle == NULL) ||
((SFTK_GET_SDB(keyHandle)->sdb_flags & SDB_HAS_META) == 0) ||
(keyHandle->passwordKey.data == NULL)) {
checkSig = PR_FALSE;
}
for (i = 0; i < count; i++) {
CK_ULONG length = template[i].ulValueLen;
template[i].ulValueLen = ntemplate[i].ulValueLen;
/* fixup ulongs */
if (ntemplate[i].ulValueLen == SDB_ULONG_SIZE) {
if (sftkdb_isULONGAttribute(template[i].type)) {
if (template[i].pValue) {
CK_ULONG value;
value = sftk_SDBULong2ULong(ntemplate[i].pValue);
if (length < sizeof(CK_ULONG)) {
template[i].ulValueLen = -1;
crv = CKR_BUFFER_TOO_SMALL;
continue;
}
PORT_Memcpy(template[i].pValue, &value, sizeof(CK_ULONG));
}
template[i].ulValueLen = sizeof(CK_ULONG);
}
}
/* if no data was retrieved, no need to process encrypted or signed
* attributes */
if ((template[i].pValue == NULL) || (template[i].ulValueLen == -1)) {
continue;
}
/* fixup private attributes */
if (checkEnc && sftkdb_isPrivateAttribute(ntemplate[i].type)) {
/* we have a private attribute */
/* This code depends on the fact that the cipherText is bigger
* than the plain text */
SECItem cipherText;
SECItem *plainText;
SECStatus rv;
cipherText.data = ntemplate[i].pValue;
cipherText.len = ntemplate[i].ulValueLen;
PZ_Lock(handle->passwordLock);
if (handle->passwordKey.data == NULL) {
PZ_Unlock(handle->passwordLock);
template[i].ulValueLen = -1;
crv = CKR_USER_NOT_LOGGED_IN;
continue;
}
rv = sftkdb_DecryptAttribute(handle,
&handle->passwordKey,
objectID,
ntemplate[i].type,
&cipherText, &plainText);
PZ_Unlock(handle->passwordLock);
if (rv != SECSuccess) {
PORT_Memset(template[i].pValue, 0, template[i].ulValueLen);
template[i].ulValueLen = -1;
crv = CKR_GENERAL_ERROR;
continue;
}
PORT_Assert(template[i].ulValueLen >= plainText->len);
if (template[i].ulValueLen < plainText->len) {
SECITEM_FreeItem(plainText, PR_TRUE);
PORT_Memset(template[i].pValue, 0, template[i].ulValueLen);
template[i].ulValueLen = -1;
crv = CKR_GENERAL_ERROR;
continue;
}
/* copy the plain text back into the template */
PORT_Memcpy(template[i].pValue, plainText->data, plainText->len);
template[i].ulValueLen = plainText->len;
SECITEM_FreeItem(plainText, PR_TRUE);
}
/* make sure signed attributes are valid */
if (checkSig && sftkdb_isAuthenticatedAttribute(ntemplate[i].type)) {
SECStatus rv;
CK_RV local_crv;
SECItem signText;
SECItem plainText;
unsigned char signData[SDB_MAX_META_DATA_LEN];
signText.data = signData;
signText.len = sizeof(signData);
/* Use a local variable so that we don't clobber any already
* set error. This function returns either CKR_OK or the last
* found error in the template */
local_crv = sftkdb_GetAttributeSignature(handle, keyHandle,
objectID,
ntemplate[i].type,
&signText);
if (local_crv != CKR_OK) {
PORT_Memset(template[i].pValue, 0, template[i].ulValueLen);
template[i].ulValueLen = -1;
crv = local_crv;
continue;
}
plainText.data = ntemplate[i].pValue;
plainText.len = ntemplate[i].ulValueLen;
/*
* we do a second check holding the lock just in case the user
* loggout while we were trying to get the signature.
*/
PZ_Lock(keyHandle->passwordLock);
if (keyHandle->passwordKey.data == NULL) {
/* if we are no longer logged in, no use checking the other
* Signatures either. */
checkSig = PR_FALSE;
PZ_Unlock(keyHandle->passwordLock);
continue;
}
rv = sftkdb_VerifyAttribute(keyHandle,
&keyHandle->passwordKey,
objectID, ntemplate[i].type,
&plainText, &signText);
PZ_Unlock(keyHandle->passwordLock);
if (rv != SECSuccess) {
PORT_Memset(template[i].pValue, 0, template[i].ulValueLen);
template[i].ulValueLen = -1;
crv = CKR_SIGNATURE_INVALID; /* better error code? */
}
/* This Attribute is fine */
}
}
return crv;
}
/*
* Some attributes are signed with an HMAC and a pbe key generated from
* the password. This signature is stored indexed by object handle and
*
* Those attributes are:
* 1) Trust object hashes and trust values.
* 2) public key values.
*
* Certs themselves are considered properly authenticated by virtue of their
* signature, or their matching hash with the trust object.
*
* These signature is only checked for objects coming from shared databases.
* Older dbm style databases have such no signature checks. HMACs are also
* only checked when the token is logged in, as it requires a pbe generated
* from the password.
*
* Tokens which have no key database (and therefore no master password) do not
* have any stored signature values. Signature values are stored in the key
* database, since the signature data is tightly coupled to the key database
* password.
*
* This function takes a template of attributes that were either created or
* modified. These attributes are checked to see if the need to be signed.
* If they do, then this function signs the attributes and writes them
* to the meta data store.
*
* This function can fail if there are attributes that must be signed, but
* the token is not logged in.
*
* The caller is expected to abort any transaction he was in in the
* event of a failure of this function.
*/
static CK_RV
sftk_signTemplate(PLArenaPool *arena, SFTKDBHandle *handle,
PRBool mayBeUpdateDB,
CK_OBJECT_HANDLE objectID, const CK_ATTRIBUTE *template,
CK_ULONG count)
{
unsigned int i;
CK_RV crv;
SFTKDBHandle *keyHandle = handle;
SDB *keyTarget = NULL;
PRBool usingPeerDB = PR_FALSE;
PRBool inPeerDBTransaction = PR_FALSE;
PORT_Assert(handle);
if (handle->type != SFTK_KEYDB_TYPE) {
keyHandle = handle->peerDB;
usingPeerDB = PR_TRUE;
}
/* no key DB defined? then no need to sign anything */
if (keyHandle == NULL) {
crv = CKR_OK;
goto loser;
}
/* When we are in a middle of an update, we have an update database set,
* but we want to write to the real database. The bool mayBeUpdateDB is
* set to TRUE if it's possible that we want to write an update database
* rather than a primary */
keyTarget = (mayBeUpdateDB && keyHandle->update) ? keyHandle->update : keyHandle->db;
/* skip the the database does not support meta data */
if ((keyTarget->sdb_flags & SDB_HAS_META) == 0) {
crv = CKR_OK;
goto loser;
}
/* If we had to switch databases, we need to initialize a transaction. */
if (usingPeerDB) {
crv = (*keyTarget->sdb_Begin)(keyTarget);
if (crv != CKR_OK) {
goto loser;
}
inPeerDBTransaction = PR_TRUE;
}
for (i = 0; i < count; i++) {
if (sftkdb_isAuthenticatedAttribute(template[i].type)) {
SECStatus rv;
SECItem *signText;
SECItem plainText;
plainText.data = template[i].pValue;
plainText.len = template[i].ulValueLen;
PZ_Lock(keyHandle->passwordLock);
if (keyHandle->passwordKey.data == NULL) {
PZ_Unlock(keyHandle->passwordLock);
crv = CKR_USER_NOT_LOGGED_IN;
goto loser;
}
rv = sftkdb_SignAttribute(arena, keyHandle, keyTarget,
&keyHandle->passwordKey,
keyHandle->defaultIterationCount,
objectID, template[i].type,
&plainText, &signText);
PZ_Unlock(keyHandle->passwordLock);
if (rv != SECSuccess) {
crv = CKR_GENERAL_ERROR; /* better error code here? */
goto loser;
}
crv = sftkdb_PutAttributeSignature(handle, keyTarget, objectID,
template[i].type, signText);
if (crv != CKR_OK) {
goto loser;
}
}
}
crv = CKR_OK;
/* If necessary, commit the transaction */
if (inPeerDBTransaction) {
crv = (*keyTarget->sdb_Commit)(keyTarget);
if (crv != CKR_OK) {
goto loser;
}
inPeerDBTransaction = PR_FALSE;
}
loser:
if (inPeerDBTransaction) {
/* The transaction must have failed. Abort. */
(*keyTarget->sdb_Abort)(keyTarget);
PORT_Assert(crv != CKR_OK);
if (crv == CKR_OK)
crv = CKR_GENERAL_ERROR;
}
return crv;
}
static CK_RV
sftkdb_CreateObject(PLArenaPool *arena, SFTKDBHandle *handle,
SDB *db, CK_OBJECT_HANDLE *objectID,
CK_ATTRIBUTE *template, CK_ULONG count)
{
CK_RV crv;
crv = (*db->sdb_CreateObject)(db, objectID, template, count);
if (crv != CKR_OK) {
goto loser;
}
crv = sftk_signTemplate(arena, handle, (db == handle->update),
*objectID, template, count);
loser:
return crv;
}
static CK_RV
sftkdb_fixupSignatures(SFTKDBHandle *handle,
SDB *db, CK_OBJECT_HANDLE oldID, CK_OBJECT_HANDLE newID,
CK_ATTRIBUTE *ptemplate, CK_ULONG max_attributes)
{
unsigned int i;
CK_RV crv = CKR_OK;
/* if we don't have a meta table, we didn't write any signature objects */
if ((db->sdb_flags & SDB_HAS_META) == 0) {
return CKR_OK;
}
for (i = 0; i < max_attributes; i++) {
CK_ATTRIBUTE *att = &ptemplate[i];
CK_ATTRIBUTE_TYPE type = att->type;
if (sftkdb_isPrivateAttribute(type)) {
/* move the signature from one object handle to another and delete
* the old entry */
SECItem signature;
unsigned char signData[SDB_MAX_META_DATA_LEN];
signature.data = signData;
signature.len = sizeof(signData);
crv = sftkdb_getRawAttributeSignature(handle, db, oldID, type,
&signature);
if (crv != CKR_OK) {
/* NOTE: if we ever change our default write from AES_CBC
* to AES_KW, We'll need to change this to a continue as
* we won't need the integrity record for AES_KW */
break;
}
crv = sftkdb_PutAttributeSignature(handle, db, newID, type,
&signature);
if (crv != CKR_OK) {
break;
}
/* now get rid of the old one */
crv = sftkdb_DestroyAttributeSignature(handle, db, oldID, type);
if (crv != CKR_OK) {
break;
}
}
}
return crv;
}
CK_ATTRIBUTE *
sftk_ExtractTemplate(PLArenaPool *arena, SFTKObject *object,
SFTKDBHandle *handle, CK_OBJECT_HANDLE objectID,
SDB *db, CK_ULONG *pcount, CK_RV *crv)
{
unsigned int count;
CK_ATTRIBUTE *template;
unsigned int i, templateIndex;
SFTKSessionObject *sessObject = sftk_narrowToSessionObject(object);
PRBool doEnc = PR_TRUE;
*crv = CKR_OK;
if (sessObject == NULL) {
*crv = CKR_GENERAL_ERROR; /* internal programming error */
return NULL;
}
PORT_Assert(handle);
/* find the key handle */
if (handle->type != SFTK_KEYDB_TYPE) {
doEnc = PR_FALSE;
}
PZ_Lock(sessObject->attributeLock);
count = 0;
for (i = 0; i < sessObject->hashSize; i++) {
SFTKAttribute *attr;
for (attr = sessObject->head[i]; attr; attr = attr->next) {
count++;
}
}
template = PORT_ArenaNewArray(arena, CK_ATTRIBUTE, count);
if (template == NULL) {
PZ_Unlock(sessObject->attributeLock);
*crv = CKR_HOST_MEMORY;
return NULL;
}
templateIndex = 0;
for (i = 0; i < sessObject->hashSize; i++) {
SFTKAttribute *attr;
for (attr = sessObject->head[i]; attr; attr = attr->next) {
CK_ATTRIBUTE *tp = &template[templateIndex++];
/* copy the attribute */
*tp = attr->attrib;
/* fixup ULONG s */
if ((tp->ulValueLen == sizeof(CK_ULONG)) &&
(sftkdb_isULONGAttribute(tp->type))) {
CK_ULONG value = *(CK_ULONG *)tp->pValue;
unsigned char *data;
tp->pValue = PORT_ArenaAlloc(arena, SDB_ULONG_SIZE);
data = (unsigned char *)tp->pValue;
if (data == NULL) {
*crv = CKR_HOST_MEMORY;
break;
}
sftk_ULong2SDBULong(data, value);
tp->ulValueLen = SDB_ULONG_SIZE;
}
/* encrypt private attributes */
if (doEnc && sftkdb_isPrivateAttribute(tp->type)) {
/* we have a private attribute */
SECItem *cipherText;
SECItem plainText;
SECStatus rv;
plainText.data = tp->pValue;
plainText.len = tp->ulValueLen;
PZ_Lock(handle->passwordLock);
if (handle->passwordKey.data == NULL) {
PZ_Unlock(handle->passwordLock);
*crv = CKR_USER_NOT_LOGGED_IN;
break;
}
rv = sftkdb_EncryptAttribute(arena, handle, db,
&handle->passwordKey,
handle->defaultIterationCount,
objectID,
tp->type,
&plainText, &cipherText);
PZ_Unlock(handle->passwordLock);
if (rv == SECSuccess) {
tp->pValue = cipherText->data;
tp->ulValueLen = cipherText->len;
} else {
*crv = CKR_GENERAL_ERROR; /* better error code here? */
break;
}
PORT_Memset(plainText.data, 0, plainText.len);
}
}
}
PORT_Assert(templateIndex <= count);
PZ_Unlock(sessObject->attributeLock);
if (*crv != CKR_OK) {
return NULL;
}
if (pcount) {
*pcount = count;
}
return template;
}
/*
* return a pointer to the attribute in the give template.
* The return value is not const, as the caller may modify
* the given attribute value, but such modifications will
* modify the actual value in the template.
*/
static CK_ATTRIBUTE *
sftkdb_getAttributeFromTemplate(CK_ATTRIBUTE_TYPE attribute,
CK_ATTRIBUTE *ptemplate, CK_ULONG len)
{
CK_ULONG i;
for (i = 0; i < len; i++) {
if (attribute == ptemplate[i].type) {
return &ptemplate[i];
}
}
return NULL;
}
static const CK_ATTRIBUTE *
sftkdb_getAttributeFromConstTemplate(CK_ATTRIBUTE_TYPE attribute,
const CK_ATTRIBUTE *ptemplate, CK_ULONG len)
{
CK_ULONG i;
for (i = 0; i < len; i++) {
if (attribute == ptemplate[i].type) {
return &ptemplate[i];
}
}
return NULL;
}
/*
* fetch a template which identifies 'unique' entries based on object type
*/
static CK_RV
sftkdb_getFindTemplate(CK_OBJECT_CLASS objectType, unsigned char *objTypeData,
CK_ATTRIBUTE *findTemplate, CK_ULONG *findCount,
CK_ATTRIBUTE *ptemplate, int len)
{
CK_ATTRIBUTE *attr;
CK_ULONG count = 1;
sftk_ULong2SDBULong(objTypeData, objectType);
findTemplate[0].type = CKA_CLASS;
findTemplate[0].pValue = objTypeData;
findTemplate[0].ulValueLen = SDB_ULONG_SIZE;
switch (objectType) {
case CKO_CERTIFICATE:
case CKO_NSS_TRUST:
attr = sftkdb_getAttributeFromTemplate(CKA_ISSUER, ptemplate, len);
if (attr == NULL) {
return CKR_TEMPLATE_INCOMPLETE;
}
findTemplate[1] = *attr;
attr = sftkdb_getAttributeFromTemplate(CKA_SERIAL_NUMBER,
ptemplate, len);
if (attr == NULL) {
return CKR_TEMPLATE_INCOMPLETE;
}
findTemplate[2] = *attr;
count = 3;
break;
case CKO_PRIVATE_KEY:
case CKO_PUBLIC_KEY:
case CKO_SECRET_KEY:
attr = sftkdb_getAttributeFromTemplate(CKA_ID, ptemplate, len);
if (attr == NULL) {
return CKR_TEMPLATE_INCOMPLETE;
}
if (attr->ulValueLen == 0) {
/* key is too generic to determine that it's unique, usually
* happens in the key gen case */
return CKR_OBJECT_HANDLE_INVALID;
}
findTemplate[1] = *attr;
count = 2;
break;
case CKO_NSS_CRL:
attr = sftkdb_getAttributeFromTemplate(CKA_SUBJECT, ptemplate, len);
if (attr == NULL) {
return CKR_TEMPLATE_INCOMPLETE;
}
findTemplate[1] = *attr;
count = 2;
break;
case CKO_NSS_SMIME:
attr = sftkdb_getAttributeFromTemplate(CKA_SUBJECT, ptemplate, len);
if (attr == NULL) {
return CKR_TEMPLATE_INCOMPLETE;
}
findTemplate[1] = *attr;
attr = sftkdb_getAttributeFromTemplate(CKA_NSS_EMAIL, ptemplate, len);
if (attr == NULL) {
return CKR_TEMPLATE_INCOMPLETE;
}
findTemplate[2] = *attr;
count = 3;
break;
default:
attr = sftkdb_getAttributeFromTemplate(CKA_VALUE, ptemplate, len);
if (attr == NULL) {
return CKR_TEMPLATE_INCOMPLETE;
}
findTemplate[1] = *attr;
count = 2;
break;
}
*findCount = count;
return CKR_OK;
}
/*
* look to see if this object already exists and return its object ID if
* it does.
*/
static CK_RV
sftkdb_lookupObject(SDB *db, CK_OBJECT_CLASS objectType,
CK_OBJECT_HANDLE *id, CK_ATTRIBUTE *ptemplate, CK_ULONG len)
{
CK_ATTRIBUTE findTemplate[3];
CK_ULONG count = 1;
CK_ULONG objCount = 0;
SDBFind *find = NULL;
unsigned char objTypeData[SDB_ULONG_SIZE];
CK_RV crv;
*id = CK_INVALID_HANDLE;
if (objectType == CKO_NSS_CRL) {
return CKR_OK;
}
crv = sftkdb_getFindTemplate(objectType, objTypeData,
findTemplate, &count, ptemplate, len);
if (crv == CKR_OBJECT_HANDLE_INVALID) {
/* key is too generic to determine that it's unique, usually
* happens in the key gen case, tell the caller to go ahead
* and just create it */
return CKR_OK;
}
if (crv != CKR_OK) {
return crv;
}
/* use the raw find, so we get the correct database */
crv = (*db->sdb_FindObjectsInit)(db, findTemplate, count, &find);
if (crv != CKR_OK) {
return crv;
}
(*db->sdb_FindObjects)(db, find, id, 1, &objCount);
(*db->sdb_FindObjectsFinal)(db, find);
if (objCount == 0) {
*id = CK_INVALID_HANDLE;
}
return CKR_OK;
}
/*
* check to see if this template conflicts with others in our current database.
*/
static CK_RV
sftkdb_checkConflicts(SDB *db, CK_OBJECT_CLASS objectType,
const CK_ATTRIBUTE *ptemplate, CK_ULONG len,
CK_OBJECT_HANDLE sourceID)
{
CK_ATTRIBUTE findTemplate[2];
unsigned char objTypeData[SDB_ULONG_SIZE];
/* we may need to allocate some temporaries. Keep track of what was
* allocated so we can free it in the end */
unsigned char *temp1 = NULL;
unsigned char *temp2 = NULL;
CK_ULONG objCount = 0;
SDBFind *find = NULL;
CK_OBJECT_HANDLE id;
const CK_ATTRIBUTE *attr, *attr2;
CK_RV crv;
CK_ATTRIBUTE subject;
/* Currently the only conflict is with nicknames pointing to the same
* subject when creating or modifying a certificate. */
/* If the object is not a cert, no problem. */
if (objectType != CKO_CERTIFICATE) {
return CKR_OK;
}
/* if not setting a nickname then there's still no problem */
attr = sftkdb_getAttributeFromConstTemplate(CKA_LABEL, ptemplate, len);
if ((attr == NULL) || (attr->ulValueLen == 0)) {
return CKR_OK;
}
/* fetch the subject of the source. For creation and merge, this should
* be found in the template */
attr2 = sftkdb_getAttributeFromConstTemplate(CKA_SUBJECT, ptemplate, len);
if (sourceID == CK_INVALID_HANDLE) {
if ((attr2 == NULL) || ((CK_LONG)attr2->ulValueLen < 0)) {
crv = CKR_TEMPLATE_INCOMPLETE;
goto done;
}
} else if ((attr2 == NULL) || ((CK_LONG)attr2->ulValueLen <= 0)) {
/* sourceID is set if we are trying to modify an existing entry instead
* of creating a new one. In this case the subject may not be (probably
* isn't) in the template, we have to read it from the database */
subject.type = CKA_SUBJECT;
subject.pValue = NULL;
subject.ulValueLen = 0;
crv = (*db->sdb_GetAttributeValue)(db, sourceID, &subject, 1);
if (crv != CKR_OK) {
goto done;
}
if ((CK_LONG)subject.ulValueLen < 0) {
crv = CKR_DEVICE_ERROR; /* closest pkcs11 error to corrupted DB */
goto done;
}
temp1 = subject.pValue = PORT_Alloc(++subject.ulValueLen);
if (temp1 == NULL) {
crv = CKR_HOST_MEMORY;
goto done;
}