summaryrefslogtreecommitdiff
path: root/security/nss/lib/fortcrypt/swfort/swflib.c
blob: cc4647006969bfca970a7a36c03161a6ff976afa (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
/*
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape security libraries.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */
/*
 * implement the MACI calls as Software Fortezza Calls.
 * only do the ones Nescape Needs. This provides a single software slot,
 * with 100 key registers, and 50 backup Ra private registers. Since we only
 * create one session per slot, this implementation only uses one session.
 * One future enhancement may be to try to improve on this for better threading
 * support.
 */

#include "prtypes.h"
#include "prio.h"

#include "swforti.h"
#include "keytlow.h"
/* #include "dh.h" */
#include "blapi.h"
#include "maci.h"
/* #include "dsa.h" */
/* #include "hasht.h" */
#include "secitem.h"
#include "secrng.h"
#include "keylow.h"
#include "secder.h"

#ifdef XP_UNIX
#include <unistd.h>
#endif

#ifndef O_BINARY
#define O_BINARY 0
#endif


/* currently we only support one software token. In the future we can use the
 * session to determin which of many possible tokens we are talking about.
 * all the calls which need tokens take a pointer to the software token as a
 * target.
 */
static FORTSWToken *swtoken = NULL;

#define SOCKET_ID 1


/* can't change the pin on SW fortezza for now */
int
MACI_ChangePIN(HSESSION session, int PINType, CI_PIN CI_FAR pOldPIN,
						 CI_PIN CI_FAR pNewPin)
{
    return CI_INV_STATE;
}


/*
 * Check pin checks the pin, then logs the user in or out depending on if
 * the pin succedes. The General implementation would support both SSO and 
 * User mode our's only needs User mode. Pins are checked by whether or not
 * they can produce our valid Ks for this 'card'.
 */
int
MACI_CheckPIN(HSESSION session, int PINType, CI_PIN CI_FAR pin)
{
    FORTSkipjackKeyPtr Ks;
    FORTSWFile *config_file = NULL;
    FORTSkipjackKey seed;
    unsigned char pinArea[13];
    unsigned char *padPin = NULL;

    /* This SW module can only log in as USER */
    if (PINType != CI_USER_PIN) return CI_INV_TYPE;

    if (swtoken == NULL) return CI_NO_CARD;
    /* we can't check a pin if we haven't been initialized yet */
    if (swtoken->config_file == NULL) return CI_NO_CARD;
    config_file = swtoken->config_file;

    /* Make sure the pin value meets minimum lengths */
    if (PORT_Strlen((char *)pin) < 12) {
	PORT_Memset(pinArea, ' ', sizeof(pinArea));
	PORT_Memcpy(pinArea,pin,PORT_Strlen((char *)pin));
	pinArea[12] = 0;
	padPin = pinArea;
    }

    /* get the Ks by unwrapping it from the memphrase with the pbe generated
     * from the pin */
    Ks = fort_CalculateKMemPhrase(config_file, 
			&config_file->fortezzaPhrase, (char *)pin, NULL);

    if (Ks == 0) {
	Ks = fort_CalculateKMemPhrase(config_file, 
		&config_file->fortezzaPhrase, (char *)padPin, NULL);
	if (Ks == 0) {
	    PORT_Memset(pinArea, 0, sizeof(pinArea));
	    fort_Logout(swtoken);
	    return CI_FAIL;
	}
    }

    /* use Ks and hash to verify that pin is correct */
    if (! fort_CheckMemPhrase(config_file, &config_file->fortezzaPhrase, 
						(char *)pin, Ks) ) {
	if ((padPin == NULL) ||
    	   ! fort_CheckMemPhrase(config_file, &config_file->fortezzaPhrase, 
						(char *)padPin, Ks) )  {
	    PORT_Memset(pinArea, 0, sizeof(pinArea));
	    fort_Logout(swtoken);
	    return CI_FAIL;
	}
    }

    PORT_Memset(pinArea, 0, sizeof(pinArea));


    /* OK, add the random Seed value into the random number generator */
    fort_skipjackUnwrap(Ks,config_file->wrappedRandomSeed.len,
	config_file->wrappedRandomSeed.data,seed);
    RNG_RandomUpdate(seed,sizeof(seed));

    /* it is, go ahead and log in */
    swtoken->login = PR_TRUE;
    /* Ks is always stored in keyReg[0] when we log in */
    PORT_Memcpy(swtoken->keyReg[0].data, Ks, sizeof (FORTSkipjackKey));
    swtoken->keyReg[0].present = PR_TRUE;
    PORT_Memset(Ks, 0, sizeof(FORTSkipjackKey));
    PORT_Free(Ks);


    return CI_OK;
}

/*
 * close an open socket. Power_Down flag is set when we want to reset the
 * cards complete state.
 */
int
MACI_Close(HSESSION session, unsigned int flags, int socket)
{
    if (socket != SOCKET_ID) return CI_BAD_CARD;
    if (swtoken == NULL) return CI_BAD_CARD;

    if (flags == CI_POWER_DOWN_FLAG) {
	fort_Logout(swtoken);
    }
    return CI_OK;
}

/*
 * Decrypt keeps track of it's own IV.
 */
int
MACI_Decrypt(HSESSION session, unsigned int size, CI_DATA cipherIn,
				 			CI_DATA plainOut)
{
    int ret;
    unsigned char IV[SKIPJACK_BLOCK_SIZE];

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    if ((ret = fort_KeyOK(swtoken,swtoken->key,PR_TRUE)) != CI_OK)  return ret;

    /*fort_AddNoise();*/

    /* save the IV, before we potentially trash the new one when we decrypt.
     * (it's permissible to decrypt into the cipher text buffer by passing the
     * same buffers for both cipherIn and plainOut.
     */
    PORT_Memcpy(IV,swtoken->IV, sizeof(IV));
    fort_UpdateIV(cipherIn,size,swtoken->IV);
    return fort_skipjackDecrypt(swtoken->keyReg[swtoken->key].data,
						IV,size,cipherIn,plainOut);
}

/*
 * Clear a key from one of the key registers (indicated by index).
 * return an error if no key exists.
 */
int
MACI_DeleteKey(HSESSION session, int index)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;

    /* can't delete Ks */
    if (index == 0) return CI_INV_KEY_INDEX;

    if ((ret = fort_KeyOK(swtoken,index,PR_TRUE)) != CI_OK)  return ret;
    fort_ClearKey(&swtoken->keyReg[index]);
    return CI_OK;
}


/*
 * encrypt  some blocks of data and update the IV.
 */ 
int
MACI_Encrypt(HSESSION session, unsigned int size, CI_DATA plainIn,
				 			CI_DATA cipherOut)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    if ((ret = fort_KeyOK(swtoken,swtoken->key,PR_TRUE)) != CI_OK)  return ret;

    /*fort_AddNoise();*/

    ret = fort_skipjackEncrypt(swtoken->keyReg[swtoken->key].data,
				swtoken->IV,size,plainIn,cipherOut);
    fort_UpdateIV(cipherOut,size,swtoken->IV);

    return ret;

}

/*
 * create a new IV and encode it.
 */

static char *leafbits="THIS IS NOT LEAF";

int
MACI_GenerateIV(HSESSION Session, CI_IV CI_FAR pIV)
{
    int ret;

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    if ((ret = fort_KeyOK(swtoken,swtoken->key,PR_TRUE)) != CI_OK)  return ret;

    ret = fort_GenerateRandom(swtoken->IV,SKIPJACK_BLOCK_SIZE);
    if (ret != CI_OK) return ret;

    PORT_Memcpy(pIV,leafbits,SKIPJACK_LEAF_SIZE);
    PORT_Memcpy(&pIV[SKIPJACK_LEAF_SIZE],swtoken->IV,SKIPJACK_BLOCK_SIZE);
   
    return CI_OK;
}


/*
 * create a new Key
 */
int
MACI_GenerateMEK(HSESSION session, int index, int reserved)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    if ((ret = fort_KeyOK(swtoken,index,PR_FALSE)) != CI_OK)  return ret;

    ret = fort_GenerateRandom(swtoken->keyReg[index].data,
					sizeof (swtoken->keyReg[index].data));
    if (ret == CI_OK) swtoken->keyReg[index].present = PR_TRUE;

    return ret;
}

/*
 * build a new Ra/ra pair for a KEA exchange.
 */
int
MACI_GenerateRa(HSESSION session, CI_RA CI_FAR pRa)
{
    int ret;
    int counter;
    int RaLen,raLen;
    DSAPrivateKey *privKey = NULL;
    PQGParams params;
    SECStatus rv;
    int crv = CI_EXEC_FAIL;
    fortSlotEntry *certEntry = NULL;
    unsigned char *unsignedRa = NULL;
    unsigned char *unsignedra = NULL;
    fortKeyInformation *key_info = NULL;
	

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    /* make sure the personality is set */
    if (swtoken->certIndex == 0) return CI_INV_STATE;

    /* pick next Ra circular buffer */
    counter = swtoken->nextRa;
    swtoken->nextRa++;
    if (swtoken->nextRa >= MAX_RA_SLOTS) swtoken->nextRa = 0;

    /* now get the params for diffie -helman key gen */
    certEntry = fort_GetCertEntry(swtoken->config_file,swtoken->certIndex);
    if (certEntry == NULL) return CI_INV_CERT_INDEX;
    if (certEntry->exchangeKeyInformation) {
	key_info = certEntry->exchangeKeyInformation;
    } else {
	key_info = certEntry->signatureKeyInformation;
    }
    if (key_info == NULL) return CI_NO_X;
   
    /* Generate Diffie Helman key Pair -- but we use DSA key gen to do it */
    rv = SECITEM_CopyItem(NULL,&params.prime,&key_info->p);
    if (rv != SECSuccess) return CI_EXEC_FAIL;
    rv = SECITEM_CopyItem(NULL,&params.subPrime,&key_info->q);
    if (rv != SECSuccess) return CI_EXEC_FAIL;
    rv = SECITEM_CopyItem(NULL,&params.base,&key_info->g);
    if (rv != SECSuccess) return CI_EXEC_FAIL;

    /* KEA uses DSA like key generation with short DSA keys that have to
     * maintain a relationship to q */
    rv = DSA_NewKey(&params, &privKey);
    SECITEM_FreeItem(&params.prime,PR_FALSE);
    SECITEM_FreeItem(&params.subPrime,PR_FALSE);
    SECITEM_FreeItem(&params.base,PR_FALSE);
    if (rv != SECSuccess) return CI_EXEC_FAIL;
    
    /* save private key, public key, and param in Ra Circular buffer */
    unsignedRa = privKey->publicValue.data;
    RaLen      = privKey->publicValue.len;
    while ((unsignedRa[0] == 0) && (RaLen > CI_RA_SIZE)) {
	unsignedRa++;
	RaLen--;
    }
    if (RaLen > CI_RA_SIZE) goto loser;

    unsignedra = privKey->privateValue.data;
    raLen      = privKey->privateValue.len;
    while ((unsignedra[0] == 0) && (raLen > sizeof(fortRaPrivate))) {
	unsignedra++;
	raLen--;
    }

    if (raLen > sizeof(fortRaPrivate)) goto loser;

    PORT_Memset(swtoken->RaValues[counter].private, 0, sizeof(fortRaPrivate));
    PORT_Memcpy(
	&swtoken->RaValues[counter].private[sizeof(fortRaPrivate) - raLen],
							unsignedra, raLen);
    PORT_Memset(pRa, 0, CI_RA_SIZE);
    PORT_Memcpy(&pRa[CI_RA_SIZE-RaLen], unsignedRa, RaLen);
    PORT_Memcpy(swtoken->RaValues[counter].public, pRa, CI_RA_SIZE);
    crv = CI_OK;

loser:
    PORT_FreeArena(privKey->params.arena, PR_TRUE);

    return crv;
}


/*
 * return some random data.
 */
int
MACI_GenerateRandom(HSESSION session, CI_RANDOM CI_FAR random)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_FALSE)) != CI_OK) return ret;
    return fort_GenerateRandom(random,sizeof (CI_RANDOM));
}


static CI_RA Remail = {
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
};

/*
 * build a new Token exchange key using KEA.
 */
int
MACI_GenerateTEK(HSESSION hSession, int flags, int target, 
     CI_RA CI_FAR Ra, CI_RA CI_FAR Rb, unsigned int YSize, CI_Y CI_FAR pY )
{
    SECKEYLowPrivateKey *key 		= NULL;
    fortSlotEntry *      certEntry;
    unsigned char *      w		= NULL;
    SECItem              *q;
    SECStatus            rv;
    int                  ret,i;
    PRBool               email 		= PR_TRUE;
    SECItem              R;		/* public */
    SECItem              Y;		/* public */
    SECItem              r;		/* private */
    SECItem              x;		/* private */
    SECItem              wItem;		/* derived secret */
    fortRaPrivatePtr     ra;
    FORTSkipjackKey      cover_key;

    unsigned char pad[10] = { 0x72, 0xf1, 0xa8, 0x7e, 0x92,
			      0x82, 0x41, 0x98, 0xab, 0x0b };

    /* verify that everything is ok with the token, keys and certs */
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    /* make sure the personality is set */
    if (swtoken->certIndex == 0) return CI_INV_STATE;
    if ((ret = fort_KeyOK(swtoken,target,PR_FALSE)) != CI_OK)  return ret;

    /* get the cert from the entry, then look up the key from that cert */
    certEntry = fort_GetCertEntry(swtoken->config_file,swtoken->certIndex);
    if (certEntry == NULL) return CI_INV_CERT_INDEX;
    key = fort_GetPrivKey(swtoken,dhKey,certEntry);
    if (key == NULL) return CI_NO_X; 

    if (certEntry->exchangeKeyInformation) {
	q = &certEntry->exchangeKeyInformation->q;
    } else {
	q = &certEntry->signatureKeyInformation->q;
    }

    email = (PORT_Memcmp(Rb,Remail,sizeof(Rb)) == 0) ? PR_TRUE: PR_FALSE;


    /* load the common elements */
    Y.data = pY;
    Y.len = YSize;
    x.data = key->u.dh.privateValue.data;
    x.len = key->u.dh.privateValue.len;

    /* now initialize the rest of the values */
    if (flags == CI_INITIATOR_FLAG) {
	if (email) { 
	    R.data = Y.data;
	    R.len = Y.len;
	} else {
	    R.data = Rb;
	    R.len = sizeof(CI_RA);
	}
        ra = fort_LookupPrivR(swtoken,Ra);
	if (ra == NULL) {
	    ret = CI_EXEC_FAIL;
	    goto loser;
	}
	r.data = ra;
	r.len = sizeof(fortRaPrivate);
    } else {
	R.data = Ra;
	R.len = sizeof(CI_RA);
	if (email) {
	    r.data = x.data;
	    r.len = x.len;
	} else {
            ra = fort_LookupPrivR(swtoken,Rb);
	    if (ra == NULL) {
		ret = CI_EXEC_FAIL;
		goto loser;
	    }
	    r.data = ra;
	    r.len = sizeof(fortRaPrivate);
	}
    }


    if (!KEA_Verify(&Y,&key->u.dh.prime,q)) {
	ret = CI_EXEC_FAIL;
	goto loser;
    }
    if (!KEA_Verify(&R,&key->u.dh.prime,q)) {
	ret = CI_EXEC_FAIL;
	goto loser;
    }

    /* calculate the base key */
    rv = KEA_Derive(&key->u.dh.prime, &Y, &R, &r, &x, &wItem);
    if (rv != SECSuccess) {
	ret = CI_EXEC_FAIL;
	goto loser;
    }

    w = wItem.data;
    /* use the skipjack wrapping function to 'mix' the key up */
    for (i=0; i < sizeof(FORTSkipjackKey); i++) 
    	cover_key[i] = pad[i] ^ w[i];

    ret = fort_skipjackWrap(cover_key,sizeof(FORTSkipjackKey),
		&w[sizeof(FORTSkipjackKey)],swtoken->keyReg[target].data);
    if (ret != CI_OK) goto loser;

    swtoken->keyReg[target].present = PR_TRUE;

    ret = CI_OK;
loser:
    if (w) PORT_Free(w);
    if (key) SECKEY_LowDestroyPrivateKey(key);
    
    return ret;
}


/*
 * return the bytes of a certificate.
 */
int
MACI_GetCertificate(HSESSION hSession, int certIndex,
						 CI_CERTIFICATE CI_FAR cert)
{
    int len;
    int ret;
    fortSlotEntry *certEntry = NULL;

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;

    certEntry = fort_GetCertEntry(swtoken->config_file,certIndex);
    if (certEntry == NULL) return CI_INV_CERT_INDEX;

    len = certEntry->certificateData.dataEncryptedWithKs.len;
    PORT_Memset(cert,0,sizeof(CI_CERTIFICATE));
    PORT_Memcpy(cert, certEntry->certificateData.dataEncryptedWithKs.data,len);

    /* Ks is always stored in keyReg[0] when we log in */
    return fort_skipjackDecrypt(swtoken->keyReg[0].data,
	&certEntry->certificateData.dataIV.data[SKIPJACK_LEAF_SIZE],
						len,cert,cert);
}


/*
 * return out sofware configuration bytes. Those field not used by the PKCS #11
 * module may not be filled in exactly.
 */
#define NETSCAPE "Netscape Communications Corp    "
#define PRODUCT  "Netscape Software FORTEZZA Lib  "
#define SOFTWARE "Software FORTEZZA Implementation"

int
MACI_GetConfiguration(HSESSION hSession, CI_CONFIG_PTR config)
{
    config->LibraryVersion = 0x0100;
    config->ManufacturerVersion = 0x0100;
    PORT_Memcpy(config->ManufacturerName,NETSCAPE,sizeof(NETSCAPE));
    PORT_Memcpy(config->ProductName,PRODUCT,sizeof(PRODUCT));
    PORT_Memcpy(config->ProcessorType,SOFTWARE,sizeof(SOFTWARE));
    config->UserRAMSize = 0;
    config->LargestBlockSize = 0x10000;
    config->KeyRegisterCount = KEY_REGISTERS;
    config->CertificateCount = 
		swtoken ? fort_GetCertCount(swtoken->config_file): 0;
    config->CryptoCardFlag = 0;
    config->ICDVersion = 0;
    config->ManufacturerSWVer = 0x0100;
    config->DriverVersion = 0x0100;
    return CI_OK;
}

/*
 * return a list of all the personalities (up to the value 'EntryCount')
 */
int
MACI_GetPersonalityList(HSESSION hSession, int EntryCount, 
						CI_PERSON CI_FAR personList[])
{
    int count;
    int i,ret;
    FORTSWFile *config_file = NULL;
    unsigned char tmp[32];

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    config_file = swtoken->config_file;

    /* search for the index */
    count= fort_GetCertCount(config_file);

    /* don't return more than the user asked for */
    if (count > EntryCount) count = EntryCount;
    for (i=0; i < count ;i ++) {
	int len, dataLen;
	personList[i].CertificateIndex =
				config_file->slotEntries[i]->certIndex;
	len = config_file->slotEntries[i]->certificateLabel.
						dataEncryptedWithKs.len;
	if (len > sizeof(tmp)) len = sizeof(tmp);
        PORT_Memset(personList[i].CertLabel, ' ',
					sizeof(personList[i].CertLabel));
        PORT_Memcpy(tmp, 
	     config_file->slotEntries[i]->
			certificateLabel.dataEncryptedWithKs.data,
								len);
	/* Ks is always stored in keyReg[0] when we log in */
	ret = fort_skipjackDecrypt(swtoken->keyReg[0].data,
	 &config_file->slotEntries[i]->
			certificateLabel.dataIV.data[SKIPJACK_LEAF_SIZE],len,
			tmp,tmp);
	if (ret != CI_OK) return ret;
	dataLen = DER_GetInteger(&config_file->slotEntries[i]->
			certificateLabel.length);
	if (dataLen > sizeof(tmp)) dataLen = sizeof(tmp);
        PORT_Memcpy(personList[i].CertLabel, tmp, dataLen);
        personList[i].CertLabel[32] = 0;
        personList[i].CertLabel[33] = 0;
        personList[i].CertLabel[34] = 0;
        personList[i].CertLabel[35] = 0;
    }
    return CI_OK;
}


/*
 * get a new session ID. This function is only to make the interface happy,
 * the PKCS #11 module only uses one session per token.
 */
int
MACI_GetSessionID(HSESSION *session)
{
    *session = 1;
    return CI_OK;
}

/*
 * return the current card state.
 */
int
MACI_GetState(HSESSION hSession, CI_STATE_PTR state)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_FALSE)) != CI_OK) return ret;
    *state = fort_GetState(swtoken);
    return CI_OK;
}

/*
 * return the status. NOTE that KeyRegisterFlags and CertificateFlags are never
 * really used by the PKCS #11 module, so they are not implemented.
 */
int
MACI_GetStatus(HSESSION hSession, CI_STATUS_PTR status)
{
    int ret;
    FORTSWFile *config_file = NULL;

    if ((ret = fort_CardExists(swtoken,PR_FALSE)) != CI_OK) return ret;
    config_file = swtoken->config_file;
    status->CurrentSocket = 1;
    status->LockState = swtoken->lock;
    PORT_Memcpy(status->SerialNumber,
		config_file->serialID.data, config_file->serialID.len);
    status->CurrentState = fort_GetState(swtoken);
    status->DecryptionMode = CI_CBC64_MODE;
    status->EncryptionMode = CI_CBC64_MODE;
    status->CurrentPersonality = swtoken->certIndex;
    status->KeyRegisterCount = KEY_REGISTERS;
    /* our code doesn't use KeyRegisters, which is good, because there's not
     * enough of them .... */
    PORT_Memset(status->KeyRegisterFlags,0,sizeof(status->KeyRegisterFlags));
    status->CertificateCount = fort_GetCertCount(config_file);
    PORT_Memset(status->CertificateFlags,0,sizeof(status->CertificateFlags));
    PORT_Memset(status->Flags,0,sizeof(status->Flags));

    return CI_OK;
}

/*
 * add the time call because the PKCS #11 module calls it, but always pretend
 * the clock is bad, so it never uses the returned time.
 */
int
MACI_GetTime(HSESSION hSession, CI_TIME CI_FAR time)
{
   return CI_BAD_CLOCK;
}


/* This function is copied from NSPR so that the PKCS #11 module can be
 * independent of NSPR */
PRInt32 local_getFileInfo(const char *fn, PRFileInfo *info);

/*
 * initialize the SW module, and return the number of slots we support (1).
 */
int
MACI_Initialize(int CI_FAR *count)
{
    char *filename = NULL;
    SECItem file;
    FORTSignedSWFile *decode_file = NULL;
    PRFileInfo info;
    /*PRFileDesc *fd = NULL;*/
    int fd = -1;
    PRStatus err;
    int ret = CI_OK;
    int fcount;

    file.data = NULL;
    file.len = 0;

    *count = 1;

    /* allocate swtoken structure */
    swtoken = PORT_ZNew(FORTSWToken);
    if (swtoken == NULL) return CI_OUT_OF_MEMORY;

    filename = (char *)fort_LookupFORTEZZAInitFile();
    if (filename == NULL) {
	ret = CI_BAD_READ;
	goto failed;
    }
    
    fd = open(filename,O_RDONLY|O_BINARY,0);
    if (fd < 0) {
	ret = CI_BAD_READ;
	goto failed;
    }

    err = local_getFileInfo(filename,&info);
    if ((err != 0) || (info.size == 0)) {
	ret = CI_BAD_READ;
	goto failed;
    }

    file.data = PORT_ZAlloc(info.size);
    if (file.data == NULL) {
	ret = CI_OUT_OF_MEMORY;
	goto failed;
    }

    fcount = read(fd,file.data,info.size);
    close(fd);  fd = -1;
    if (fcount != (int)info.size) {
	ret = CI_BAD_READ;
	goto failed;
    }

    file.len = fcount;

    decode_file = FORT_GetSWFile(&file);
    if (decode_file == NULL) {
	ret = CI_BAD_READ;
	goto failed;
    }
    swtoken->config_file = &decode_file->file;

    RNG_SystemInfoForRNG();
    RNG_FileForRNG(filename);


failed:
    if (filename) PORT_Free(filename);
    if (fd != -1) close(fd);
    if (file.data) PORT_Free(file.data);
    if (ret != CI_OK) {
	if (decode_file) FORT_DestroySignedSWFile(decode_file);
	if (swtoken) PORT_Free(swtoken);
	swtoken = NULL;
    }

    return CI_OK;
}

/*
 * load an IV from an external source. We technically should check it with the
 * key we received.
 */
int
MACI_LoadIV(HSESSION session, CI_IV CI_FAR iv)
{
    int ret;

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    PORT_Memcpy(swtoken->IV,&iv[SKIPJACK_LEAF_SIZE],SKIPJACK_BLOCK_SIZE);
    return CI_OK;
}

/* implement token lock (should call PR_Monitor here) */
int
MACI_Lock(HSESSION session, int flags)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    swtoken->lock = 1;

    return CI_OK;
}

/* open a token. For software there isn't much to do that hasn't already been
 * done by initialize. */
int
MACI_Open(HSESSION session, unsigned int flags, int socket)
{
    if (socket != SOCKET_ID) return CI_NO_CARD;
    if (swtoken == NULL) return CI_NO_CARD;
    return CI_OK;
}

/*
 * Reset logs out the token...
 */
int
MACI_Reset(HSESSION session)
{
    if (swtoken) fort_Logout(swtoken);
    return CI_OK;
}

/*
 * restore and encrypt/decrypt state. NOTE: there is no error checking in this
 * or the save function.
 */
int
MACI_Restore(HSESSION session, int type, CI_SAVE_DATA CI_FAR data)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    PORT_Memcpy(swtoken->IV,data, sizeof (swtoken->IV));
    return CI_OK;
}

/*
 * save and encrypt/decrypt state. NOTE: there is no error checking in this
 * or the restore function.
 */
int
MACI_Save(HSESSION session, int type,CI_SAVE_DATA CI_FAR data)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    PORT_Memcpy(data,swtoken->IV, sizeof (swtoken->IV));
    return CI_OK;
}

/*
 * picks a token to operate against. In our case there can be only one.
 */
int
MACI_Select(HSESSION session, int socket)
{
    if (socket == SOCKET_ID) return CKR_OK;
    return CI_NO_CARD;
}

/*
 * set a register as the key to use for encrypt/decrypt operations.
 */
int
MACI_SetKey(HSESSION session, int index)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    if ((ret = fort_KeyOK(swtoken,index,PR_TRUE)) != CI_OK)  return ret;

    swtoken->key = index;
    return CI_OK;
}

/*
 * only CBC64 is supported. Keep setmode for compatibility */
int
MACI_SetMode(HSESSION session, int type, int mode)
{
    if (mode != CI_CBC64_MODE) return CI_INV_MODE;
    return CI_OK;
}

/* set the personality to use for sign/verify */
int
MACI_SetPersonality(HSESSION session, int cert)
{
    int ret;
    fortSlotEntry *certEntry = NULL;

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;

    certEntry = fort_GetCertEntry(swtoken->config_file,cert);
    if ((certEntry == NULL) ||
	   ((certEntry->exchangeKeyInformation == NULL) &&
			(certEntry->signatureKeyInformation == NULL)) )
						 return CI_INV_CERT_INDEX;
    swtoken->certIndex = cert;
    return CI_OK;
}


/* DSA sign some data */
int
MACI_Sign(HSESSION session, CI_HASHVALUE CI_FAR hash, CI_SIGNATURE CI_FAR sig)
{
    SECKEYLowPrivateKey *key 		= NULL;
    fortSlotEntry *      certEntry	= NULL;
    int                  ret 		= CI_OK;
    SECStatus            rv;
    SECItem              signItem;
    SECItem              hashItem;
    unsigned char        random[DSA_SUBPRIME_LEN];

    /* standard checks */
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    /* make sure the personality is set */
    if (swtoken->certIndex == 0) return CI_INV_STATE;

    /* get the current personality */
    certEntry = fort_GetCertEntry(swtoken->config_file,swtoken->certIndex);
    if (certEntry == NULL) return CI_INV_CERT_INDEX;

    /* extract the private key from the personality */
    ret = CI_OK;
    key = fort_GetPrivKey(swtoken,dsaKey,certEntry);
    if (key == NULL) {
	ret = CI_NO_X;
	goto loser;
    }

    /* create a random value for the signature */
    ret = fort_GenerateRandom(random, sizeof(random));
    if (ret != CI_OK) goto loser;

    /* Sign with that private key */
    signItem.data = sig;
    signItem.len  = DSA_SIGNATURE_LEN;

    hashItem.data = hash;
    hashItem.len  = SHA1_LENGTH;

    rv = DSA_SignDigestWithSeed(&key->u.dsa, &signItem, &hashItem, random);
    if (rv != SECSuccess) {
	ret = CI_EXEC_FAIL;
    }

    /* clean up */
loser:
    if (key != NULL) SECKEY_LowDestroyPrivateKey(key);

    return ret;
}

/*
 * clean up after ourselves.
 */
int
MACI_Terminate(HSESSION session)
{
    if (swtoken == NULL) return CI_OUT_OF_MEMORY;

    /* clear all the keys */
    fort_Logout(swtoken);
    
    FORT_DestroySWFile(swtoken->config_file);
    PORT_Free(swtoken);
    swtoken = NULL;
    return CI_OK;
}



/* implement token unlock (should call PR_Monitor here) */
int
MACI_Unlock(HSESSION session)
{
    int ret;
    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    swtoken->lock = 0;
    return CI_OK;
}

/*
 * unwrap a key into our software token. NOTE: this function does not
 * verify that the wrapping key is Ks or a TEK. This is because our higher
 * level software doesn't try to wrap MEKs with MEKs. If this API was exposed
 * generically, then we would have to worry about things like this.
 */
int
MACI_UnwrapKey(HSESSION session, int wrapKey, int target, 
						CI_KEY CI_FAR keyData)
{
    int ret = CI_OK;

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    if ((ret = fort_KeyOK(swtoken,target,PR_FALSE)) != CI_OK)  return ret;
    if ((ret = fort_KeyOK(swtoken,wrapKey,PR_TRUE)) != CI_OK)  return ret;
    ret = fort_skipjackUnwrap(swtoken->keyReg[wrapKey].data,
	sizeof(CI_KEY), keyData, swtoken->keyReg[target].data);
    if (ret != CI_OK) goto loser;

    swtoken->keyReg[target].present = PR_TRUE;

loser:
    return ret;
}

/*
 * Wrap a key out of our software token. NOTE: this function does not
 * verify that the wrapping key is Ks or a TEK, or that the source key is
 * a MEK. This is because our higher level software doesn't try to wrap MEKs 
 * with MEKs, or wrap out TEKS and Ks. If this API was exposed
 * generically, then we would have to worry about things like this.
 */
int
MACI_WrapKey(HSESSION session, int wrapKey, int source, CI_KEY CI_FAR keyData)
{
    int ret;

    if ((ret = fort_CardExists(swtoken,PR_TRUE)) != CI_OK) return ret;
    if ((ret = fort_KeyOK(swtoken,source,PR_TRUE)) != CI_OK)  return ret;
    if ((ret = fort_KeyOK(swtoken,wrapKey,PR_TRUE)) != CI_OK)  return ret;
    ret = fort_skipjackWrap(swtoken->keyReg[wrapKey].data,
	sizeof(CI_KEY), swtoken->keyReg[source].data,keyData);

    return ret;
}