-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcryptapi.c
executable file
·4604 lines (4113 loc) · 146 KB
/
cryptapi.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
/****************************************************************************
* *
* cryptlib External API Interface *
* Copyright Peter Gutmann 1997-2013 *
* *
****************************************************************************/
/* NSA motto: In God we trust... all others we monitor.
-- Stanley Miller */
#include "crypt.h"
#if defined( INC_ALL )
#include "rpc.h"
#else
#include "misc/rpc.h"
#endif /* Compiler-specific includes */
/* Handlers for the various commands */
#ifdef USE_CERTIFICATES
static int cmdCertCheck( COMMAND_INFO *cmd )
{
assert( cmd->type == COMMAND_CERTCHECK );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 2 );
assert( cmd->noStrArgs == 0 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( !isHandleRangeValid( cmd->arg[ 1 ] ) && \
( cmd->arg[ 1 ] != CRYPT_UNUSED ) )
return( CRYPT_ARGERROR_NUM1 );
return( krnlSendMessage( cmd->arg[ 0 ], MESSAGE_CRT_SIGCHECK, NULL,
cmd->arg[ 1 ] ) );
}
#ifdef USE_KEYSETS
static int cmdCertMgmt( COMMAND_INFO *cmd )
{
MESSAGE_CERTMGMT_INFO certMgmtInfo;
int status;
assert( cmd->type == COMMAND_CERTMGMT );
assert( cmd->flags == COMMAND_FLAG_NONE || \
cmd->flags == COMMAND_FLAG_RET_NONE );
assert( cmd->noArgs == 4 );
assert( cmd->noStrArgs == 0 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 1 ] < CRYPT_CERTACTION_FIRST_USER || \
cmd->arg[ 1 ] > CRYPT_CERTACTION_LAST_USER )
return( CRYPT_ARGERROR_VALUE );
if( !isHandleRangeValid( cmd->arg[ 2 ] ) && \
!( ( cmd->arg[ 1 ] == CRYPT_CERTACTION_EXPIRE_CERT || \
cmd->arg[ 1 ] == CRYPT_CERTACTION_CLEANUP ) && \
cmd->arg[ 2 ] == CRYPT_UNUSED ) )
return( CRYPT_ARGERROR_NUM1 );
if( !isHandleRangeValid( cmd->arg[ 3 ] ) && \
!( ( cmd->arg[ 1 ] == CRYPT_CERTACTION_ISSUE_CRL || \
cmd->arg[ 1 ] == CRYPT_CERTACTION_EXPIRE_CERT || \
cmd->arg[ 1 ] == CRYPT_CERTACTION_CLEANUP ) && \
cmd->arg[ 3 ] == CRYPT_UNUSED ) )
return( CRYPT_ARGERROR_NUM2 );
setMessageCertMgmtInfo( &certMgmtInfo, cmd->arg[ 2 ], cmd->arg[ 3 ] );
if( cmd->flags == COMMAND_FLAG_RET_NONE )
{
/* If we aren't interested in the return value, set the crypt handle
to CRYPT_UNUSED to indicate that there's no need to return the
created certificate object */
certMgmtInfo.cryptCert = CRYPT_UNUSED;
}
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_KEY_CERTMGMT,
&certMgmtInfo, cmd->arg[ 1 ] );
if( cryptStatusOK( status ) && cmd->flags != COMMAND_FLAG_RET_NONE )
cmd->arg[ 0 ] = certMgmtInfo.cryptCert;
return( status );
}
#endif /* USE_KEYSETS */
static int cmdCertSign( COMMAND_INFO *cmd )
{
assert( cmd->type == COMMAND_CERTSIGN );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 2 );
assert( cmd->noStrArgs == 0 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( !isHandleRangeValid( cmd->arg[ 1 ] ) )
return( CRYPT_ARGERROR_NUM1 );
return( krnlSendMessage( cmd->arg[ 0 ], MESSAGE_CRT_SIGN, NULL,
cmd->arg[ 1 ] ) );
}
#endif /* USE_CERTIFICATES */
static int cmdCreateObject( COMMAND_INFO *cmd )
{
MESSAGE_CREATEOBJECT_INFO createInfo;
BOOLEAN bindToOwner = FALSE, hasStrArg = FALSE;
int owner DUMMY_INIT, status;
assert( cmd->type == COMMAND_CREATEOBJECT );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs >= 2 && cmd->noArgs <= 4 );
assert( cmd->noStrArgs >= 0 && cmd->noStrArgs <= 2 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) && \
cmd->arg[ 0 ] != SYSTEM_OBJECT_HANDLE )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 1 ] <= OBJECT_TYPE_NONE || \
cmd->arg[ 1 ] >= OBJECT_TYPE_LAST )
return( CRYPT_ERROR_FAILED ); /* Internal error */
switch( cmd->arg[ 1 ] )
{
case OBJECT_TYPE_CONTEXT:
assert( cmd->noArgs == 3 );
assert( cmd->noStrArgs == 0 );
if( cmd->arg[ 2 ] <= CRYPT_ALGO_NONE || \
cmd->arg[ 2 ] >= CRYPT_ALGO_LAST_EXTERNAL )
return( CRYPT_ARGERROR_NUM1 );
break;
case OBJECT_TYPE_CERTIFICATE:
assert( cmd->noArgs == 3 );
assert( cmd->noStrArgs == 0 );
if( cmd->arg[ 2 ] <= CRYPT_CERTTYPE_NONE || \
cmd->arg[ 2 ] >= CRYPT_CERTTYPE_LAST_EXTERNAL )
return( CRYPT_ARGERROR_NUM1 );
break;
case OBJECT_TYPE_DEVICE:
assert( cmd->noArgs == 3 );
assert( cmd->noStrArgs == 1 );
if( cmd->arg[ 2 ] <= CRYPT_DEVICE_NONE || \
cmd->arg[ 2 ] >= CRYPT_DEVICE_LAST )
return( CRYPT_ARGERROR_NUM1 );
if( cmd->arg[ 2 ] == CRYPT_DEVICE_PKCS11 || \
cmd->arg[ 2 ] == CRYPT_DEVICE_CRYPTOAPI )
{
if( cmd->strArgLen[ 0 ] < MIN_NAME_LENGTH || \
cmd->strArgLen[ 0 ] >= MAX_ATTRIBUTE_SIZE )
return( CRYPT_ARGERROR_STR1 );
hasStrArg = TRUE;
}
break;
case OBJECT_TYPE_KEYSET:
assert( cmd->noArgs == 4 );
assert( cmd->noStrArgs >= 0 && cmd->noStrArgs <= 1 );
if( cmd->arg[ 2 ] <= CRYPT_KEYSET_NONE || \
cmd->arg[ 2 ] >= CRYPT_KEYSET_LAST )
return( CRYPT_ARGERROR_NUM1 );
if( cmd->strArgLen[ 0 ] < MIN_NAME_LENGTH || \
cmd->strArgLen[ 0 ] >= MAX_ATTRIBUTE_SIZE )
return( CRYPT_ARGERROR_STR1 );
if( cmd->arg[ 3 ] < CRYPT_KEYOPT_NONE || \
cmd->arg[ 3 ] >= CRYPT_KEYOPT_LAST_EXTERNAL )
{
/* CRYPT_KEYOPT_NONE is a valid setting for this parameter */
return( CRYPT_ARGERROR_NUM2 );
}
hasStrArg = TRUE;
break;
case OBJECT_TYPE_ENVELOPE:
assert( cmd->noArgs == 3 );
assert( cmd->noStrArgs == 0 );
if( cmd->arg[ 2 ] <= CRYPT_FORMAT_NONE || \
cmd->arg[ 2 ] >= CRYPT_FORMAT_LAST_EXTERNAL )
return( CRYPT_ARGERROR_NUM1 );
break;
case OBJECT_TYPE_SESSION:
assert( cmd->noArgs == 3 );
assert( cmd->noStrArgs == 0 );
if( cmd->arg[ 2 ] <= CRYPT_SESSION_NONE || \
cmd->arg[ 2 ] >= CRYPT_SESSION_LAST )
return( CRYPT_ARGERROR_NUM1 );
break;
case OBJECT_TYPE_USER:
assert( cmd->noArgs == 2 );
assert( cmd->noStrArgs == 2 );
if( cmd->strArgLen[ 0 ] < MIN_NAME_LENGTH || \
cmd->strArgLen[ 0 ] >= CRYPT_MAX_TEXTSIZE )
return( CRYPT_ARGERROR_STR1 );
if( cmd->strArgLen[ 1 ] < MIN_NAME_LENGTH || \
cmd->strArgLen[ 1 ] >= CRYPT_MAX_TEXTSIZE )
return( CRYPT_ARGERROR_STR2 );
hasStrArg = TRUE;
break;
default:
retIntError();
}
/* If we're creating the object via a device, we should set the new
object owner to the device owner */
if( cmd->arg[ 0 ] != SYSTEM_OBJECT_HANDLE )
{
bindToOwner = TRUE;
owner = cmd->arg[ 0 ];
}
/* Create the object via the device. Since we're usually doing this via
the system object which is invisible to the user, we have to use an
internal message for this one case */
setMessageCreateObjectInfo( &createInfo, cmd->arg[ 2 ] );
if( cmd->noArgs == 4 )
createInfo.arg2 = cmd->arg[ 3 ];
if( hasStrArg )
{
createInfo.strArg1 = cmd->strArg[ 0 ];
createInfo.strArgLen1 = cmd->strArgLen[ 0 ];
if( cmd->noStrArgs > 1 )
{
createInfo.strArg2 = cmd->strArg[ 1 ];
createInfo.strArgLen2 = cmd->strArgLen[ 1 ];
}
}
if( cmd->arg[ 0 ] == SYSTEM_OBJECT_HANDLE )
{
status = krnlSendMessage( SYSTEM_OBJECT_HANDLE,
IMESSAGE_DEV_CREATEOBJECT, &createInfo,
cmd->arg[ 1 ] );
}
else
{
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_DEV_CREATEOBJECT,
&createInfo, cmd->arg[ 1 ] );
}
if( cryptStatusError( status ) )
return( status );
/* If the device used to create the object is bound to a thread, bind
the created object to the thread as well. If this fails, we don't
return the object to the caller since it would be returned in a
potentially unbound state */
if( bindToOwner )
{
int ownerID;
status = krnlSendMessage( owner, IMESSAGE_GETATTRIBUTE, &ownerID,
CRYPT_PROPERTY_OWNER );
if( cryptStatusOK( status ) )
status = krnlSendMessage( createInfo.cryptHandle,
IMESSAGE_SETATTRIBUTE, &ownerID,
CRYPT_PROPERTY_OWNER );
if( cryptStatusError( status ) && status != CRYPT_ERROR_NOTINITED )
{
krnlSendNotifier( createInfo.cryptHandle, IMESSAGE_DECREFCOUNT );
return( status );
}
}
/* Make the newly-created object externally visible if necessary. This
is only required when we're creating the object via the system
handle, which requires an internal message that leaves the object
internal */
if( cmd->arg[ 0 ] == SYSTEM_OBJECT_HANDLE )
{
krnlSendMessage( createInfo.cryptHandle, IMESSAGE_SETATTRIBUTE,
MESSAGE_VALUE_FALSE, CRYPT_IATTRIBUTE_INTERNAL );
}
cmd->arg[ 0 ] = createInfo.cryptHandle;
return( CRYPT_OK );
}
#ifdef USE_CERTIFICATES
static int cmdCreateObjectIndirect( COMMAND_INFO *cmd )
{
MESSAGE_CREATEOBJECT_INFO createInfo;
int status;
assert( cmd->type == COMMAND_CREATEOBJECT_INDIRECT );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 2 );
assert( cmd->noStrArgs == 1 );
/* Perform basic server-side error checking */
if( cmd->arg[ 0 ] != SYSTEM_OBJECT_HANDLE )
return( CRYPT_ERROR_FAILED ); /* Internal error */
if( cmd->arg[ 1 ] != OBJECT_TYPE_CERTIFICATE )
return( CRYPT_ERROR_FAILED ); /* Internal error */
if( cmd->strArgLen[ 0 ] < MIN_CERTSIZE || \
cmd->strArgLen[ 0 ] >= MAX_BUFFER_SIZE )
return( CRYPT_ARGERROR_STR1 );
/* Create the object via the device. Since we're usually doing this via
the system object which is invisible to the user, we have to use an
internal message for this one case */
setMessageCreateObjectIndirectInfo( &createInfo, cmd->strArg[ 0 ],
cmd->strArgLen[ 0 ],
CRYPT_CERTTYPE_NONE );
if( cmd->arg[ 0 ] == SYSTEM_OBJECT_HANDLE )
{
status = krnlSendMessage( SYSTEM_OBJECT_HANDLE,
IMESSAGE_DEV_CREATEOBJECT_INDIRECT,
&createInfo, OBJECT_TYPE_CERTIFICATE );
}
else
{
status = krnlSendMessage( cmd->arg[ 0 ],
MESSAGE_DEV_CREATEOBJECT_INDIRECT,
&createInfo, OBJECT_TYPE_CERTIFICATE );
}
if( cryptStatusError( status ) )
return( status );
/* Make the newly-created object externally visible */
krnlSendMessage( createInfo.cryptHandle, IMESSAGE_SETATTRIBUTE,
MESSAGE_VALUE_FALSE, CRYPT_IATTRIBUTE_INTERNAL );
cmd->arg[ 0 ] = createInfo.cryptHandle;
return( status );
}
#endif /* USE_CERTIFICATES */
static int cmdDecrypt( COMMAND_INFO *cmd )
{
int algorithm, mode = CRYPT_MODE_NONE, status; /* int vs.enum */
assert( cmd->type == COMMAND_DECRYPT );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 1 );
assert( cmd->noStrArgs == 1 );
/* Perform basic server-side error checking */
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&algorithm, CRYPT_CTXINFO_ALGO );
if( cryptStatusError( status ) )
return( status );
if( algorithm <= CRYPT_ALGO_LAST_CONVENTIONAL )
{
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&mode, CRYPT_CTXINFO_MODE );
if( cryptStatusError( status ) )
return( status );
}
else
{
if( algorithm <= CRYPT_ALGO_LAST_PKC )
{
int blockSize;
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&blockSize, CRYPT_CTXINFO_KEYSIZE );
if( cryptStatusOK( status ) && cmd->strArgLen[ 0 ] != blockSize )
status = CRYPT_ARGERROR_NUM1;
if( cryptStatusError( status ) )
return( status );
}
else
{
/* We shouldn't be invoking decrypt on a hash or MAC object */
return( CRYPT_ARGERROR_OBJECT );
}
}
if( cmd->strArgLen[ 0 ] <= 0 )
return( CRYPT_ARGERROR_NUM1 );
if( mode == CRYPT_MODE_ECB || mode == CRYPT_MODE_CBC )
{
int blockSize;
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&blockSize, CRYPT_CTXINFO_BLOCKSIZE );
if( cryptStatusOK( status ) && cmd->strArgLen[ 0 ] % blockSize )
status = CRYPT_ARGERROR_NUM1;
if( cryptStatusError( status ) )
return( status );
}
/* Make sure that the IV has been set */
if( needsIV( mode ) && !isStreamCipher( algorithm ) )
{
MESSAGE_DATA msgData;
setMessageData( &msgData, NULL, 0 );
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE_S,
&msgData, CRYPT_CTXINFO_IV );
if( cryptStatusError( status ) )
return( status );
}
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_CTX_DECRYPT,
cmd->strArgLen[ 0 ] ? cmd->strArg[ 0 ] : "",
cmd->strArgLen[ 0 ] );
return( status );
}
static int cmdDeleteAttribute( COMMAND_INFO *cmd )
{
assert( cmd->type == COMMAND_DELETEATTRIBUTE );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 2 );
assert( cmd->noStrArgs == 0 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) && \
cmd->arg[ 0 ] != DEFAULTUSER_OBJECT_HANDLE )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 0 ] == DEFAULTUSER_OBJECT_HANDLE )
{
if( cmd->arg[ 1 ] <= CRYPT_OPTION_FIRST || \
cmd->arg[ 1 ] >= CRYPT_OPTION_LAST )
return( CRYPT_ARGERROR_NUM1 );
}
else
{
if( cmd->arg[ 1 ] <= CRYPT_ATTRIBUTE_NONE || \
cmd->arg[ 1 ] >= CRYPT_ATTRIBUTE_LAST )
return( CRYPT_ARGERROR_NUM1 );
}
/* Delete the attribute. Since we're usually doing this via the default
user object which is invisible to the user, we have to use an internal
message for this one case */
if( cmd->arg[ 0 ] == DEFAULTUSER_OBJECT_HANDLE )
{
return( krnlSendMessage( DEFAULTUSER_OBJECT_HANDLE,
IMESSAGE_DELETEATTRIBUTE, NULL,
cmd->arg[ 1 ] ) );
}
return( krnlSendMessage( cmd->arg[ 0 ], MESSAGE_DELETEATTRIBUTE, NULL,
cmd->arg[ 1 ] ) );
}
#ifdef USE_KEYSETS
static int cmdDeleteKey( COMMAND_INFO *cmd )
{
MESSAGE_KEYMGMT_INFO deletekeyInfo;
int itemType = KEYMGMT_ITEM_PUBLICKEY;
assert( cmd->type == COMMAND_DELETEKEY );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs >= 2 && cmd->noArgs <= 3 );
assert( cmd->noStrArgs == 1 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 1 ] <= CRYPT_KEYID_NONE || \
cmd->arg[ 1 ] >= CRYPT_KEYID_LAST_EXTERNAL )
return( CRYPT_ARGERROR_NUM1 );
if( cmd->arg[ 2 ] )
{
/* It's a special-case object being fetched from a CA store */
if( cmd->arg[ 2 ] == CRYPT_CERTTYPE_REQUEST_CERT )
itemType = KEYMGMT_ITEM_REQUEST;
else
{
if( cmd->arg[ 2 ] == CRYPT_CERTTYPE_PKIUSER )
itemType = KEYMGMT_ITEM_PKIUSER;
else
return( CRYPT_ARGERROR_NUM2 );
}
}
if( cmd->strArgLen[ 0 ] < MIN_NAME_LENGTH || \
cmd->strArgLen[ 0 ] >= MAX_ATTRIBUTE_SIZE )
return( CRYPT_ARGERROR_STR1 );
/* Delete the key from the keyset. Unless the user has explicitly
specified a CA item to delete, we set the item type to delete to
public-key since private-key keysets will interpret this correctly
to mean they should also delete the associated private key */
setMessageKeymgmtInfo( &deletekeyInfo, cmd->arg[ 1 ], cmd->strArg[ 0 ],
cmd->strArgLen[ 0 ], NULL, 0, KEYMGMT_FLAG_NONE );
return( krnlSendMessage( cmd->arg[ 0 ], MESSAGE_KEY_DELETEKEY,
&deletekeyInfo, itemType ) );
}
#endif /* USE_KEYSETS */
static int cmdDestroyObject( COMMAND_INFO *cmd )
{
assert( cmd->type == COMMAND_DESTROYOBJECT );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 1 );
assert( cmd->noStrArgs == 0 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
/* Decrement the object's reference count, which may or may not actually
destroy it (the kernel marks it as internal so it appears destroyed
to the caller) */
return( krnlSendNotifier( cmd->arg[ 0 ], MESSAGE_DECREFCOUNT ) );
}
static int cmdEncrypt( COMMAND_INFO *cmd )
{
int algorithm, mode = CRYPT_MODE_NONE, status; /* int vs.enum */
assert( cmd->type == COMMAND_ENCRYPT );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 1 );
assert( cmd->noStrArgs == 1 );
/* Perform basic server-side error checking */
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&algorithm, CRYPT_CTXINFO_ALGO );
if( cryptStatusError( status ) )
return( status );
if( algorithm <= CRYPT_ALGO_LAST_CONVENTIONAL )
{
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&mode, CRYPT_CTXINFO_MODE );
if( cryptStatusError( status ) )
return( status );
}
else
{
if( algorithm <= CRYPT_ALGO_LAST_PKC )
{
int blockSize;
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&blockSize, CRYPT_CTXINFO_KEYSIZE );
if( cryptStatusOK( status ) && cmd->strArgLen[ 0 ] != blockSize )
status = CRYPT_ARGERROR_NUM1;
if( cryptStatusError( status ) )
return( status );
}
}
if( isHashAlgo( algorithm ) || isMacAlgo( algorithm ) )
{
/* For hash and MAC operations a length of zero is valid since this
is an indication to wrap up the hash operation */
if( cmd->strArgLen[ 0 ] < 0 )
return( CRYPT_ARGERROR_NUM1 );
}
else
{
if( cmd->strArgLen[ 0 ] <= 0 )
return( CRYPT_ARGERROR_NUM1 );
}
if( mode == CRYPT_MODE_ECB || mode == CRYPT_MODE_CBC )
{
int blockSize;
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&blockSize, CRYPT_CTXINFO_BLOCKSIZE );
if( cryptStatusOK( status ) && cmd->strArgLen[ 0 ] % blockSize )
status = CRYPT_ARGERROR_NUM1;
if( cryptStatusError( status ) )
return( status );
}
/* If there's no IV set, generate one ourselves */
if( needsIV( mode ) && !isStreamCipher( algorithm ) )
{
MESSAGE_DATA msgData;
setMessageData( &msgData, NULL, 0 );
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE_S,
&msgData, CRYPT_CTXINFO_IV );
if( cryptStatusError( status ) )
{
if( status == CRYPT_ERROR_NOTINITED )
krnlSendNotifier( cmd->arg[ 0 ], MESSAGE_CTX_GENIV );
else
return( status );
}
}
status = krnlSendMessage( cmd->arg[ 0 ],
( isHashAlgo( algorithm ) || \
isMacAlgo( algorithm ) ) ? \
MESSAGE_CTX_HASH : MESSAGE_CTX_ENCRYPT,
cmd->strArgLen[ 0 ] ? cmd->strArg[ 0 ] : "",
cmd->strArgLen[ 0 ] );
if( isHashAlgo( algorithm ) || isMacAlgo( algorithm ) )
{
/* There's no data to return since the hashing doesn't change it */
cmd->strArgLen[ 0 ] = 0;
}
return( status );
}
#ifdef USE_CERTIFICATES
static int cmdExportObject( COMMAND_INFO *cmd )
{
MESSAGE_DATA msgData;
int status;
assert( cmd->type == COMMAND_EXPORTOBJECT );
assert( cmd->flags == COMMAND_FLAG_NONE || \
cmd->flags == COMMAND_FLAG_RET_LENGTH );
assert( cmd->noArgs == 2 );
assert( ( cmd->flags == COMMAND_FLAG_NONE && cmd->noStrArgs == 1 ) || \
( cmd->flags == COMMAND_FLAG_RET_LENGTH && cmd->noStrArgs == 0 ) );
assert( cmd->flags == COMMAND_FLAG_RET_LENGTH || \
cmd->strArg[ 0 ] != NULL );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 1 ] <= CRYPT_CERTFORMAT_NONE || \
cmd->arg[ 1 ] >= CRYPT_CERTFORMAT_LAST_EXTERNAL )
{
/* At the moment the only object that we can export is a certificate,
so we make sure that the format type is valid for this */
return( CRYPT_ARGERROR_NUM1 );
}
/* Export the certificate */
if( cmd->flags == COMMAND_FLAG_RET_LENGTH )
{
setMessageData( &msgData, NULL, 0 );
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_CRT_EXPORT,
&msgData, cmd->arg[ 1 ] );
if( cryptStatusOK( status ) )
cmd->arg[ 0 ] = msgData.length;
}
else
{
setMessageData( &msgData, cmd->strArg[ 0 ], cmd->strArgLen[ 0 ] );
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_CRT_EXPORT,
&msgData, cmd->arg[ 1 ] );
if( cryptStatusOK( status ) )
cmd->strArgLen[ 0 ] = msgData.length;
}
/* If we try and export using a disallowed format (e.g. a
CRYPT_CERTFORMAT_CERTCHAIN from a certificate request) we'll get an
argument value error that we need to convert into something more
sensible. The error type to report is somewhat debatable since
either the format type or the object can be regarded as being wrong,
for example when exporting a certificate request as a certificate
chain the format is wrong but when exporting a data-only object as
anything the object is wrong. To handle this, we report an argument
value error as a numeric parameter error for cases where the format
is incorrect for the object type, and a permission error for cases
where the object can't be exported externally */
if( status == CRYPT_ARGERROR_VALUE )
{
int type;
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE, &type,
CRYPT_CERTINFO_CERTTYPE );
if( cryptStatusError( status ) )
return( CRYPT_ARGERROR_OBJECT );
status = ( type == CRYPT_CERTTYPE_CERTIFICATE || \
type == CRYPT_CERTTYPE_ATTRIBUTE_CERT || \
type == CRYPT_CERTTYPE_CERTCHAIN || \
type == CRYPT_CERTTYPE_CERTREQUEST || \
type == CRYPT_CERTTYPE_CRL ) ? \
CRYPT_ARGERROR_NUM1 : CRYPT_ERROR_PERMISSION;
}
return( status );
}
#endif /* USE_CERTIFICATES */
#if defined( USE_ENVELOPES ) || defined( USE_SESSIONS )
static int cmdFlushData( COMMAND_INFO *cmd )
{
MESSAGE_DATA msgData;
int status;
assert( cmd->type == COMMAND_FLUSHDATA );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 1 );
assert( cmd->noStrArgs == 0 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
/* Send the flush data command to the object */
setMessageData( &msgData, NULL, 0 );
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_ENV_PUSHDATA,
&msgData, 0 );
return( status );
}
#endif /* USE_ENVELOPES || USE_SESSIONS */
static int cmdGenKey( COMMAND_INFO *cmd )
{
assert( cmd->type == COMMAND_GENKEY );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 1 );
assert( cmd->noStrArgs == 0 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
return( krnlSendNotifier( cmd->arg[ 0 ], MESSAGE_CTX_GENKEY ) );
}
static int cmdGetAttribute( COMMAND_INFO *cmd )
{
MESSAGE_DATA msgData;
int status;
assert( cmd->type == COMMAND_GETATTRIBUTE );
assert( cmd->flags == COMMAND_FLAG_NONE || \
cmd->flags == COMMAND_FLAG_RET_LENGTH );
assert( cmd->noArgs >= 2 && cmd->noArgs <= 3 );
assert( ( cmd->flags == COMMAND_FLAG_NONE && cmd->noStrArgs == 1 ) || \
( ( cmd->noArgs == 2 || cmd->flags == COMMAND_FLAG_RET_LENGTH ) && \
cmd->noStrArgs == 0 ) );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) && \
cmd->arg[ 0 ] != DEFAULTUSER_OBJECT_HANDLE )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 0 ] == DEFAULTUSER_OBJECT_HANDLE )
{
if( cmd->arg[ 1 ] <= CRYPT_OPTION_FIRST || \
cmd->arg[ 1 ] >= CRYPT_OPTION_LAST )
return( CRYPT_ARGERROR_NUM1 );
}
else
{
if( cmd->arg[ 1 ] <= CRYPT_ATTRIBUTE_NONE || \
cmd->arg[ 1 ] >= CRYPT_ATTRIBUTE_LAST )
return( CRYPT_ARGERROR_NUM1 );
}
/* Get the attribute data from the object. If it's a config option,
we're usually doing this via the default user object which is
invisible to the user, so we have to use an internal message for this
one case.
This is further complicated by the fact that the kernel checks that
the destination memory is writable and either returns an error (for
an external message) or throws an exception (for the internal message
required to access the user object) if it isn't. Since the external
API doesn't allow the specification of the returned data length, it
uses a worst-case estimate which may be much larger than the actual
buffer size, which the kernel will refuse to write to. To handle
this we first read the actual length and then ask for only that much
data, which the caller should have made available for the output */
if( cmd->noArgs == 2 )
{
if( cmd->arg[ 0 ] == DEFAULTUSER_OBJECT_HANDLE )
{
return( krnlSendMessage( DEFAULTUSER_OBJECT_HANDLE,
IMESSAGE_GETATTRIBUTE, &cmd->arg[ 0 ],
cmd->arg[ 1 ] ) );
}
return( krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE,
&cmd->arg[ 0 ], cmd->arg[ 1 ] ) );
}
setMessageData( &msgData, NULL, 0 );
if( cmd->arg[ 0 ] == DEFAULTUSER_OBJECT_HANDLE )
{
status = krnlSendMessage( DEFAULTUSER_OBJECT_HANDLE,
IMESSAGE_GETATTRIBUTE_S, &msgData,
cmd->arg[ 1 ] );
}
else
{
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE_S,
&msgData, cmd->arg[ 1 ] );
}
if( cryptStatusError( status ) )
return( status );
if( cmd->flags == COMMAND_FLAG_RET_LENGTH )
{
cmd->arg[ 0 ] = msgData.length;
return( CRYPT_OK );
}
msgData.data = cmd->strArg[ 0 ];
if( cmd->arg[ 0 ] == DEFAULTUSER_OBJECT_HANDLE )
{
status = krnlSendMessage( DEFAULTUSER_OBJECT_HANDLE,
IMESSAGE_GETATTRIBUTE_S, &msgData,
cmd->arg[ 1 ] );
}
else
{
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE_S,
&msgData, cmd->arg[ 1 ] );
}
if( cryptStatusOK( status ) )
cmd->strArgLen[ 0 ] = msgData.length;
return( status );
}
#ifdef USE_KEYSETS
static int cmdGetKey( COMMAND_INFO *cmd )
{
MESSAGE_KEYMGMT_INFO getkeyInfo;
int messageType = ( cmd->arg[ 2 ] == CRYPT_KEYID_NONE ) ? \
MESSAGE_KEY_GETNEXTCERT : MESSAGE_KEY_GETKEY;
int owner, status;
assert( cmd->type == COMMAND_GETKEY );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 3 );
assert( cmd->noStrArgs >= 1 && cmd->noStrArgs <= 2 );
/* Perform basic server-side error checking. Because of keyset queries
we have to accept CRYPT_KEYID_NONE as well as obviously valid key
IDs. In addition if we find a missing ID we pass the request in as
a keyset query (this is matched to an implicit GetFirstCert performed
by setting the query attribute, this isn't really possible using the
external API) */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 1 ] <= KEYMGMT_ITEM_NONE || \
cmd->arg[ 1 ] >= KEYMGMT_ITEM_REVOCATIONINFO )
{
/* Item can only be a public key, private key, secret key, CA
request, or CA PKI user info */
return( CRYPT_ARGERROR_NUM1 );
}
if( cmd->arg[ 2 ] < CRYPT_KEYID_NONE || \
cmd->arg[ 2 ] >= CRYPT_KEYID_LAST_EXTERNAL )
return( CRYPT_ARGERROR_NUM2 );
if( cmd->arg[ 2 ] == CRYPT_KEYID_NONE )
{
if( cmd->arg[ 1 ] != KEYMGMT_ITEM_PUBLICKEY )
{
/* If we're doing a keyset query, it has to be for a
certificate */
return( CRYPT_ARGERROR_NUM1 );
}
if( cmd->strArgLen[ 0 ] )
return( CRYPT_ARGERROR_NUM1 );
}
else
{
if( cmd->strArgLen[ 0 ] < MIN_NAME_LENGTH || \
cmd->strArgLen[ 0 ] >= MAX_ATTRIBUTE_SIZE )
return( CRYPT_ARGERROR_STR1 );
}
/* Read the key from the keyset */
setMessageKeymgmtInfo( &getkeyInfo, cmd->arg[ 2 ],
cmd->strArgLen[ 0 ] ? cmd->strArg[ 0 ] : NULL,
cmd->strArgLen[ 0 ],
cmd->strArgLen[ 1 ] ? cmd->strArg[ 1 ] : NULL,
cmd->strArgLen[ 1 ], KEYMGMT_FLAG_NONE );
status = krnlSendMessage( cmd->arg[ 0 ], messageType, &getkeyInfo,
cmd->arg[ 1 ] );
if( cryptStatusError( status ) )
return( status );
/* If the keyset is bound to a thread, bind the key read from it to the
thread as well. If this fails, we don't return the imported key to
the caller since it would be returned in a potentially unbound state */
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_GETATTRIBUTE, &owner,
CRYPT_PROPERTY_OWNER );
if( cryptStatusOK( status ) )
{
status = krnlSendMessage( getkeyInfo.cryptHandle,
IMESSAGE_SETATTRIBUTE, &owner,
CRYPT_PROPERTY_OWNER );
}
if( cryptStatusError( status ) && status != CRYPT_ERROR_NOTINITED )
{
krnlSendNotifier( getkeyInfo.cryptHandle, IMESSAGE_DECREFCOUNT );
return( status );
}
cmd->arg[ 0 ] = getkeyInfo.cryptHandle;
return( CRYPT_OK );
}
#endif /* USE_KEYSETS */
#if defined( USE_ENVELOPES ) || defined( USE_SESSIONS )
static int cmdPopData( COMMAND_INFO *cmd )
{
MESSAGE_DATA msgData;
int status;
assert( cmd->type == COMMAND_POPDATA );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 2 );
assert( cmd->noStrArgs == 1 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 1 ] < 1 )
return( CRYPT_ARGERROR_NUM1 );
/* Get the data from the object. We always copy out the byte count value
because it's valid even if an error occurs */
setMessageData( &msgData, cmd->strArg[ 0 ], cmd->arg[ 1 ] );
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_ENV_POPDATA,
&msgData, 0 );
cmd->strArgLen[ 0 ] = msgData.length;
return( status );
}
static int cmdPushData( COMMAND_INFO *cmd )
{
MESSAGE_DATA msgData;
int status;
assert( cmd->type == COMMAND_PUSHDATA );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 1 );
assert( cmd->noStrArgs == 1 );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->strArgLen[ 0 ] < 0 )
return( CRYPT_ARGERROR_NUM1 );
/* Send the data to the object. We always copy out the byte count value
because it's valid even if an error occurs */
setMessageData( &msgData, cmd->strArgLen[ 0 ] ? cmd->strArg[ 0 ] : NULL,
cmd->strArgLen[ 0 ] );
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_ENV_PUSHDATA, &msgData,
0 );
cmd->arg[ 0 ] = msgData.length;
return( status );
}
#endif /* USE_ENVELOPES || USE_SESSIONS */
static int cmdQueryCapability( COMMAND_INFO *cmd )
{
CRYPT_QUERY_INFO queryInfo;
int status;
assert( cmd->type == COMMAND_QUERYCAPABILITY );
assert( cmd->flags == COMMAND_FLAG_NONE || \
cmd->flags == COMMAND_FLAG_RET_LENGTH );
assert( cmd->noArgs == 2 );
assert( ( cmd->flags == COMMAND_FLAG_NONE && cmd->noStrArgs == 1 ) || \
( cmd->flags == COMMAND_FLAG_RET_LENGTH && cmd->noStrArgs == 0 ) );
assert( cmd->flags == COMMAND_FLAG_RET_LENGTH || \
cmd->strArg[ 0 ] != NULL );
/* Perform basic server-side error checking */
if( !isHandleRangeValid( cmd->arg[ 0 ] ) && \
cmd->arg[ 0 ] != SYSTEM_OBJECT_HANDLE )
return( CRYPT_ARGERROR_OBJECT );
if( cmd->arg[ 1 ] < CRYPT_ALGO_NONE || \
cmd->arg[ 1 ] >= CRYPT_ALGO_LAST_EXTERNAL )
return( CRYPT_ARGERROR_NUM1 );
/* Query the device for information on the given algorithm and mode.
Since we're usually doing this via the system object which is
invisible to the user, we have to use an internal message for this
one case */
if( cmd->arg[ 0 ] == SYSTEM_OBJECT_HANDLE )
{
status = krnlSendMessage( SYSTEM_OBJECT_HANDLE,
IMESSAGE_DEV_QUERYCAPABILITY, &queryInfo,
cmd->arg[ 1 ] );
}
else
{
status = krnlSendMessage( cmd->arg[ 0 ], MESSAGE_DEV_QUERYCAPABILITY,
&queryInfo, cmd->arg[ 1 ] );
}
if( cryptStatusOK( status ) )
{
/* Return either the length or the full capability into on what the
caller has asked for */
if( cmd->flags == COMMAND_FLAG_RET_LENGTH )
cmd->arg[ 0 ] = sizeof( CRYPT_QUERY_INFO );
else
{
memcpy( cmd->strArg[ 0 ], &queryInfo,
sizeof( CRYPT_QUERY_INFO ) );
cmd->strArgLen[ 0 ] = sizeof( CRYPT_QUERY_INFO );
}
}
return( status );
}
#ifdef USE_RPCAPI
static int cmdServerQuery( COMMAND_INFO *cmd )
{
int value;
assert( cmd->type == COMMAND_SERVERQUERY );
assert( cmd->flags == COMMAND_FLAG_NONE );
assert( cmd->noArgs == 0 );
assert( cmd->noStrArgs == 0 );
/* Return information about the server */
krnlSendMessage( DEFAULTUSER_OBJECT_HANDLE, IMESSAGE_GETATTRIBUTE,
&value, CRYPT_OPTION_INFO_MAJORVERSION );
krnlSendMessage( DEFAULTUSER_OBJECT_HANDLE, IMESSAGE_GETATTRIBUTE,