-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkcs11.rs
1227 lines (1096 loc) · 36.1 KB
/
pkcs11.rs
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
/* -*- Mode: rust; rust-indent-offset: 4 -*- */
/* 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/. */
#![allow(non_snake_case)]
use pkcs11_bindings::*;
use std::slice;
use std::collections::btree_map::Entry;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::{Mutex, MutexGuard};
use crate::internal::{get_attribute, search};
use crate::internal::{ObjectHandle, Query, SearchResult};
use crate::version::*;
const BUILTINS_VERSION: CK_VERSION = CK_VERSION {
major: NSS_BUILTINS_LIBRARY_VERSION_MAJOR,
minor: NSS_BUILTINS_LIBRARY_VERSION_MINOR,
};
const FIRMWARE_VERSION: CK_VERSION = CK_VERSION {
major: NSS_BUILTINS_FIRMWARE_VERSION_MAJOR,
minor: NSS_BUILTINS_FIRMWARE_VERSION_MINOR,
};
const CRYPTOKI_VERSION: CK_VERSION = CK_VERSION {
major: NSS_BUILTINS_CRYPTOKI_VERSION_MAJOR,
minor: NSS_BUILTINS_CRYPTOKI_VERSION_MINOR,
};
const HARDWARE_VERSION: CK_VERSION = CK_VERSION {
major: NSS_BUILTINS_HARDWARE_VERSION_MAJOR,
minor: NSS_BUILTINS_HARDWARE_VERSION_MINOR,
};
const MANUFACTURER_ID_BYTES: &[u8; 32] = b"Mozilla Foundation ";
const LIBRARY_DESCRIPTION_BYTES: &[u8; 32] = b"NSS Builtin Object Cryptoki Modu";
const SLOT_COUNT: CK_ULONG = 1;
const SLOT_ID_ROOTS: CK_SLOT_ID = 1;
const SLOT_DESCRIPTION_ROOTS_BYTES: &[u8; 64] =
b"NSS Builtin Objects ";
const TOKEN_LABEL_ROOTS_BYTES: &[u8; 32] = b"Builtin Object Token ";
const TOKEN_MODEL_BYTES: &[u8; 16] = b"1 ";
const TOKEN_SERIAL_NUMBER_BYTES: &[u8; 16] = b"1 ";
const TOKEN_UTC_TIME: &[u8; 16] = b" ";
#[derive(Debug)]
struct PK11Error(CK_RV);
// The token assigns session handles using a counter. It would make sense to use a 64 bit counter,
// as there would then be no risk of exhausting the session handle space. However,
// CK_SESSION_HANDLE is defined as a C unsigned long, which is a u32 on some platforms.
//
// We start the counter at 1 since PKCS#11 reserves 0 to signal an invalid handle
//
type SessionHandle = u32;
static NEXT_HANDLE: AtomicU32 = AtomicU32::new(1);
// The token needs to keep track of which sessions are open.
//
type SessionSet = BTreeSet<SessionHandle>;
static OPEN_SESSIONS: Mutex<Option<SessionSet>> = Mutex::new(None);
// Helper functions for accessing OPEN_SESSIONS
//
type SessionSetGuard = MutexGuard<'static, Option<SessionSet>>;
fn get_open_sessions_guard() -> Result<SessionSetGuard, PK11Error> {
OPEN_SESSIONS
.lock()
.map_err(|_| PK11Error(CKR_DEVICE_ERROR))
}
fn get_open_sessions(guard: &mut SessionSetGuard) -> Result<&mut SessionSet, PK11Error> {
let sessions = guard
.as_mut()
.ok_or(PK11Error(CKR_CRYPTOKI_NOT_INITIALIZED))?;
Ok(sessions)
}
// The token needs to cache search results until the client reads them or closes the session.
//
type SearchCache = BTreeMap<SessionHandle, SearchResult>;
static SEARCHES: Mutex<Option<SearchCache>> = Mutex::new(None);
// Helper functions for accessing SEARCHES
//
type SearchCacheGuard = MutexGuard<'static, Option<SearchCache>>;
fn get_search_cache_guard() -> Result<SearchCacheGuard, PK11Error> {
SEARCHES.lock().map_err(|_| PK11Error(CKR_DEVICE_ERROR))
}
fn get_search_cache(guard: &mut SearchCacheGuard) -> Result<&mut SearchCache, PK11Error> {
let searches = guard
.as_mut()
.ok_or(PK11Error(CKR_CRYPTOKI_NOT_INITIALIZED))?;
Ok(searches)
}
fn validate_session(handle: SessionHandle) -> Result<(), PK11Error> {
let mut guard = get_open_sessions_guard()?;
let sessions = get_open_sessions(&mut guard)?;
if sessions.contains(&handle) {
return Ok(());
}
if handle < NEXT_HANDLE.load(Ordering::SeqCst) {
Err(PK11Error(CKR_SESSION_CLOSED))
} else {
// Possible that NEXT_HANDLE wrapped and we should return CKR_SESSION_CLOSED.
// But this is best-effort.
Err(PK11Error(CKR_SESSION_HANDLE_INVALID))
}
}
// The internal implementation of C_Initialize
fn initialize() -> Result<(), PK11Error> {
{
let mut search_cache_guard = get_search_cache_guard()?;
if (*search_cache_guard).is_some() {
return Err(PK11Error(CKR_CRYPTOKI_ALREADY_INITIALIZED));
}
*search_cache_guard = Some(SearchCache::default());
}
{
let mut session_guard = get_open_sessions_guard()?;
if (*session_guard).is_some() {
return Err(PK11Error(CKR_CRYPTOKI_ALREADY_INITIALIZED));
}
*session_guard = Some(SessionSet::default());
}
Ok(())
}
// The internal implementation of C_Finalize
fn finalize() -> Result<(), PK11Error> {
{
let mut guard = get_search_cache_guard()?;
// Try to access the search cache to ensure we're initialized.
// Returns CKR_CRYPTOKI_NOT_INITIALIZED if we're not.
let _ = get_search_cache(&mut guard)?;
*guard = None;
}
let mut guard = get_open_sessions_guard()?;
let _ = get_open_sessions(&mut guard)?;
*guard = None;
Ok(())
}
// Internal implementation of C_OpenSession
fn open_session() -> Result<SessionHandle, PK11Error> {
let mut handle = NEXT_HANDLE.fetch_add(1, Ordering::SeqCst);
if handle == 0 {
// skip handle 0 if the addition wraps
handle = NEXT_HANDLE.fetch_add(1, Ordering::SeqCst);
}
let mut guard = get_open_sessions_guard()?;
let sessions = get_open_sessions(&mut guard)?;
while !sessions.insert(handle) {
// this only executes if NEXT_HANDLE wraps while sessions with
// small handles are still open.
handle = NEXT_HANDLE.fetch_add(1, Ordering::SeqCst);
}
Ok(handle)
}
// Internal implementation of C_CloseSession
fn close_session(session: SessionHandle) -> Result<(), PK11Error> {
{
let mut guard = get_search_cache_guard()?;
let searches = get_search_cache(&mut guard)?;
searches.remove(&session);
}
{
let mut guard = get_open_sessions_guard()?;
let sessions = get_open_sessions(&mut guard)?;
if sessions.remove(&session) {
Ok(())
} else if session < NEXT_HANDLE.load(Ordering::SeqCst) {
Err(PK11Error(CKR_SESSION_CLOSED))
} else {
Err(PK11Error(CKR_SESSION_HANDLE_INVALID))
}
}
}
// Internal implementation of C_CloseAllSessions
fn close_all_sessions() -> Result<(), PK11Error> {
{
let mut guard = get_search_cache_guard()?;
let searches = get_search_cache(&mut guard)?;
searches.clear();
}
{
let mut guard = get_open_sessions_guard()?;
let sessions = get_open_sessions(&mut guard)?;
sessions.clear();
}
Ok(())
}
// Internal implementation of C_FindObjectsInit
fn find_objects_init(session: SessionHandle, query: &Query) -> Result<usize, PK11Error> {
validate_session(session)?;
let results = search(query);
let count = results.len();
let mut guard = get_search_cache_guard()?;
let searches = get_search_cache(&mut guard)?;
match searches.entry(session) {
Entry::Occupied(_) => Err(PK11Error(CKR_OPERATION_ACTIVE)),
Entry::Vacant(v) => {
v.insert(results);
Ok(count)
}
}
}
// Internal implementation of C_FindObjects
fn find_objects(session: SessionHandle, out: &mut [CK_OBJECT_HANDLE]) -> Result<usize, PK11Error> {
validate_session(session)?;
let mut guard = get_search_cache_guard()?;
let searches = get_search_cache(&mut guard)?;
if let Some(objects) = searches.get_mut(&session) {
for (i, out_i) in out.iter_mut().enumerate() {
match objects.pop() {
Some(object) => *out_i = object.into(),
None => return Ok(i),
}
}
Ok(out.len())
} else {
Ok(0)
}
}
// Internal implementation of C_FindObjectsFinal
fn find_objects_final(session: SessionHandle) -> Result<(), PK11Error> {
validate_session(session)?;
let mut guard = get_search_cache_guard()?;
let searches = get_search_cache(&mut guard)?;
searches.remove(&session);
Ok(())
}
extern "C" fn C_Initialize(_pInitArgs: CK_VOID_PTR) -> CK_RV {
match initialize() {
Ok(_) => CKR_OK,
Err(PK11Error(e)) => e,
}
}
extern "C" fn C_Finalize(pReserved: CK_VOID_PTR) -> CK_RV {
if !pReserved.is_null() {
return CKR_ARGUMENTS_BAD;
}
match finalize() {
Ok(_) => CKR_OK,
Err(PK11Error(e)) => e,
}
}
extern "C" fn C_GetInfo(pInfo: CK_INFO_PTR) -> CK_RV {
if pInfo.is_null() {
return CKR_ARGUMENTS_BAD;
}
unsafe {
*pInfo = CK_INFO {
cryptokiVersion: CRYPTOKI_VERSION,
manufacturerID: *MANUFACTURER_ID_BYTES,
flags: 0,
libraryDescription: *LIBRARY_DESCRIPTION_BYTES,
libraryVersion: BUILTINS_VERSION,
};
}
CKR_OK
}
extern "C" fn C_GetSlotList(
_tokenPresent: CK_BBOOL,
pSlotList: CK_SLOT_ID_PTR,
pulCount: CK_ULONG_PTR,
) -> CK_RV {
if pulCount.is_null() {
return CKR_ARGUMENTS_BAD;
}
if !pSlotList.is_null() {
if unsafe { *pulCount } < SLOT_COUNT {
return CKR_BUFFER_TOO_SMALL;
}
unsafe {
*pSlotList = SLOT_ID_ROOTS;
}
}
unsafe {
*pulCount = SLOT_COUNT;
}
CKR_OK
}
extern "C" fn C_GetSlotInfo(slotID: CK_SLOT_ID, pInfo: CK_SLOT_INFO_PTR) -> CK_RV {
if (slotID != SLOT_ID_ROOTS) || pInfo.is_null() {
return CKR_ARGUMENTS_BAD;
}
unsafe {
*pInfo = CK_SLOT_INFO {
slotDescription: *SLOT_DESCRIPTION_ROOTS_BYTES,
manufacturerID: *MANUFACTURER_ID_BYTES,
flags: CKF_TOKEN_PRESENT,
hardwareVersion: HARDWARE_VERSION,
firmwareVersion: FIRMWARE_VERSION,
};
}
CKR_OK
}
extern "C" fn C_GetTokenInfo(slotID: CK_SLOT_ID, pInfo: CK_TOKEN_INFO_PTR) -> CK_RV {
if (slotID != SLOT_ID_ROOTS) || pInfo.is_null() {
return CKR_ARGUMENTS_BAD;
}
unsafe {
*pInfo = CK_TOKEN_INFO {
label: *TOKEN_LABEL_ROOTS_BYTES,
manufacturerID: *MANUFACTURER_ID_BYTES,
model: *TOKEN_MODEL_BYTES,
serialNumber: *TOKEN_SERIAL_NUMBER_BYTES,
flags: CKF_WRITE_PROTECTED,
ulMaxSessionCount: CK_UNAVAILABLE_INFORMATION,
ulSessionCount: 0,
ulMaxRwSessionCount: CK_UNAVAILABLE_INFORMATION,
ulRwSessionCount: 0,
ulMaxPinLen: CK_UNAVAILABLE_INFORMATION,
ulMinPinLen: CK_UNAVAILABLE_INFORMATION,
ulTotalPublicMemory: CK_UNAVAILABLE_INFORMATION,
ulFreePublicMemory: CK_UNAVAILABLE_INFORMATION,
ulTotalPrivateMemory: CK_UNAVAILABLE_INFORMATION,
ulFreePrivateMemory: CK_UNAVAILABLE_INFORMATION,
hardwareVersion: HARDWARE_VERSION,
firmwareVersion: FIRMWARE_VERSION,
utcTime: *TOKEN_UTC_TIME,
};
}
CKR_OK
}
extern "C" fn C_GetMechanismList(
slotID: CK_SLOT_ID,
_pMechanismList: CK_MECHANISM_TYPE_PTR,
pulCount: CK_ULONG_PTR,
) -> CK_RV {
if slotID != SLOT_ID_ROOTS || pulCount.is_null() {
return CKR_ARGUMENTS_BAD;
}
unsafe {
*pulCount = 0;
}
CKR_OK
}
extern "C" fn C_GetMechanismInfo(
_slotID: CK_SLOT_ID,
_type: CK_MECHANISM_TYPE,
_pInfo: CK_MECHANISM_INFO_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_InitToken(
_slotID: CK_SLOT_ID,
_pPin: CK_UTF8CHAR_PTR,
_ulPinLen: CK_ULONG,
_pLabel: CK_UTF8CHAR_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_InitPIN(
_hSession: CK_SESSION_HANDLE,
_pPin: CK_UTF8CHAR_PTR,
_ulPinLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SetPIN(
_hSession: CK_SESSION_HANDLE,
_pOldPin: CK_UTF8CHAR_PTR,
_ulOldLen: CK_ULONG,
_pNewPin: CK_UTF8CHAR_PTR,
_ulNewLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_OpenSession(
slotID: CK_SLOT_ID,
flags: CK_FLAGS,
_pApplication: CK_VOID_PTR,
_Notify: CK_NOTIFY,
phSession: CK_SESSION_HANDLE_PTR,
) -> CK_RV {
if slotID != SLOT_ID_ROOTS || phSession.is_null() {
return CKR_ARGUMENTS_BAD;
}
// [pkcs11-base-v3.0, Section 5.6.1]
// For legacy reasons, the CKF_SERIAL_SESSION bit MUST always be set; if a call to
// C_OpenSession does not have this bit set, the call should return unsuccessfully with the
// error code CKR_SESSION_PARALLEL_NOT_SUPPORTED.
if flags & CKF_SERIAL_SESSION == 0 {
return CKR_SESSION_PARALLEL_NOT_SUPPORTED;
}
let session_id = match open_session() {
Ok(session_id) => session_id as CK_SESSION_HANDLE,
Err(PK11Error(e)) => return e,
};
unsafe { *phSession = session_id };
CKR_OK
}
extern "C" fn C_CloseSession(hSession: CK_SESSION_HANDLE) -> CK_RV {
let session: SessionHandle = match hSession.try_into() {
Ok(session) => session,
Err(_) => return CKR_SESSION_HANDLE_INVALID,
};
match close_session(session) {
Ok(_) => CKR_OK,
Err(PK11Error(e)) => e,
}
}
extern "C" fn C_CloseAllSessions(slotID: CK_SLOT_ID) -> CK_RV {
if slotID != SLOT_ID_ROOTS {
return CKR_ARGUMENTS_BAD;
}
match close_all_sessions() {
Ok(_) => CKR_OK,
Err(PK11Error(e)) => e,
}
}
extern "C" fn C_GetSessionInfo(_hSession: CK_SESSION_HANDLE, _pInfo: CK_SESSION_INFO_PTR) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_GetOperationState(
_hSession: CK_SESSION_HANDLE,
_pOperationState: CK_BYTE_PTR,
_pulOperationStateLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SetOperationState(
_hSession: CK_SESSION_HANDLE,
_pOperationState: CK_BYTE_PTR,
_ulOperationStateLen: CK_ULONG,
_hEncryptionKey: CK_OBJECT_HANDLE,
_hAuthenticationKey: CK_OBJECT_HANDLE,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_Login(
_hSession: CK_SESSION_HANDLE,
_userType: CK_USER_TYPE,
_pPin: CK_UTF8CHAR_PTR,
_ulPinLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_Logout(_hSession: CK_SESSION_HANDLE) -> CK_RV {
CKR_OK
}
extern "C" fn C_CreateObject(
_hSession: CK_SESSION_HANDLE,
_pTemplate: CK_ATTRIBUTE_PTR,
_ulCount: CK_ULONG,
_phObject: CK_OBJECT_HANDLE_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_CopyObject(
_hSession: CK_SESSION_HANDLE,
_hObject: CK_OBJECT_HANDLE,
_pTemplate: CK_ATTRIBUTE_PTR,
_ulCount: CK_ULONG,
_phNewObject: CK_OBJECT_HANDLE_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DestroyObject(_hSession: CK_SESSION_HANDLE, _hObject: CK_OBJECT_HANDLE) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_GetObjectSize(
_hSession: CK_SESSION_HANDLE,
_hObject: CK_OBJECT_HANDLE,
_pulSize: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_GetAttributeValue(
_hSession: CK_SESSION_HANDLE,
hObject: CK_OBJECT_HANDLE,
pTemplate: CK_ATTRIBUTE_PTR,
ulCount: CK_ULONG,
) -> CK_RV {
if pTemplate.is_null() {
return CKR_ARGUMENTS_BAD;
}
let count: usize = match ulCount.try_into() {
Ok(count) => count,
Err(_) => return CKR_ARGUMENTS_BAD,
};
// C_GetAttributeValue has a session handle parameter because PKCS#11 objects can have
// session-bound lifetimes and access controls. We don't have any session objects, and all of
// our token objects are public. So there's no good reason to validate the session handle.
//
//let session: SessionHandle = match hSession.try_into() {
// Ok(session) => session,
// Err(_) => return CKR_SESSION_HANDLE_INVALID,
//};
//
//if let Err(PK11Error(e)) = validate_session(session) {
// return e;
//}
let handle: ObjectHandle = match hObject.try_into() {
Ok(handle) => handle,
Err(_) => return CKR_OBJECT_HANDLE_INVALID,
};
let attrs: &mut [CK_ATTRIBUTE] = unsafe { slice::from_raw_parts_mut(pTemplate, count) };
let mut rv = CKR_OK;
// Handle requests with null pValue fields
for attr in attrs.iter_mut().filter(|x| x.pValue.is_null()) {
attr.ulValueLen = match get_attribute(attr.type_, &handle) {
None => {
// [pkcs11-base-v3.0, Section 5.7.5]
// 2. [...] if the specified value for the object is invalid (the object does not possess
// such an attribute), then the ulValueLen field in that triple is modified to hold the
// value CK_UNAVAILABLE_INFORMATION.
rv = CKR_ATTRIBUTE_TYPE_INVALID;
CK_UNAVAILABLE_INFORMATION
}
Some(attr) => {
// [pkcs11-base-v3.0, Section 5.7.5]
// 3. [...] if the pValue field has the value NULL_PTR, then the ulValueLen field is modified
// to hold the exact length of the specified attribute for the object.
attr.len() as CK_ULONG
}
}
}
// Handle requests with non-null pValue fields
for attr in attrs.iter_mut().filter(|x| !x.pValue.is_null()) {
let dst_len: usize = match attr.ulValueLen.try_into() {
Ok(dst_len) => dst_len,
Err(_) => return CKR_ARGUMENTS_BAD,
};
attr.ulValueLen = match get_attribute(attr.type_, &handle) {
None => {
// [pkcs11-base-v3.0, Section 5.7.5]
// 2. [...] if the specified value for the object is invalid (the object does not possess
// such an attribute), then the ulValueLen field in that triple is modified to hold the
// value CK_UNAVAILABLE_INFORMATION.
rv = CKR_ATTRIBUTE_TYPE_INVALID;
CK_UNAVAILABLE_INFORMATION
}
Some(src) if dst_len >= src.len() => {
// [pkcs11-base-v3.0, Section 5.7.5]
// 4. [...] if the length specified in ulValueLen is large enough to hold the value
// of the specified attribute for the object, then that attribute is copied into
// the buffer located at pValue, and the ulValueLen field is modified to hold
// the exact length of the attribute.
let dst: &mut [u8] =
unsafe { slice::from_raw_parts_mut(attr.pValue as *mut u8, dst_len) };
dst[..src.len()].copy_from_slice(src);
src.len() as CK_ULONG
}
_ => {
// [pkcs11-base-v3.0, Section 5.7.5]
// 5. Otherwise, the ulValueLen field is modified to hold the value
// CK_UNAVAILABLE_INFORMATION.
rv = CKR_BUFFER_TOO_SMALL;
CK_UNAVAILABLE_INFORMATION
}
};
}
// [pkcs11-base-v3.0, Section 5.7.5]
// If case 2 applies to any of the requested attributes, then the call should return the value
// CKR_ATTRIBUTE_TYPE_INVALID. If case 5 applies to any of the requested attributes, then the
// call should return the value CKR_BUFFER_TOO_SMALL. As usual, if more than one of these
// error codes is applicable, Cryptoki may return any of them. Only if none of them applies to
// any of the requested attributes will CKR_OK be returned.
rv
}
extern "C" fn C_SetAttributeValue(
_hSession: CK_SESSION_HANDLE,
_hObject: CK_OBJECT_HANDLE,
_pTemplate: CK_ATTRIBUTE_PTR,
_ulCount: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_FindObjectsInit(
hSession: CK_SESSION_HANDLE,
pTemplate: CK_ATTRIBUTE_PTR,
ulCount: CK_ULONG,
) -> CK_RV {
if pTemplate.is_null() {
return CKR_ARGUMENTS_BAD;
}
let count: usize = match ulCount.try_into() {
Ok(count) => count,
Err(_) => return CKR_ARGUMENTS_BAD,
};
let session: SessionHandle = match hSession.try_into() {
Ok(session) => session,
Err(_) => return CKR_SESSION_HANDLE_INVALID,
};
let raw_attrs: &[CK_ATTRIBUTE] = unsafe { slice::from_raw_parts_mut(pTemplate, count) };
let mut query: Vec<(CK_ATTRIBUTE_TYPE, &[u8])> = Vec::with_capacity(raw_attrs.len());
for attr in raw_attrs {
match usize::try_from(attr.ulValueLen) {
Ok(len) => query.push((attr.type_, unsafe {
slice::from_raw_parts_mut(attr.pValue as *mut u8, len)
})),
Err(_) => return CKR_ARGUMENTS_BAD,
}
}
match find_objects_init(session, &query) {
Ok(_) => CKR_OK,
Err(PK11Error(e)) => e,
}
}
extern "C" fn C_FindObjects(
hSession: CK_SESSION_HANDLE,
phObject: CK_OBJECT_HANDLE_PTR,
ulMaxObjectCount: CK_ULONG,
pulObjectCount: CK_ULONG_PTR,
) -> CK_RV {
if phObject.is_null() || pulObjectCount.is_null() {
return CKR_ARGUMENTS_BAD;
}
let max_object_count: usize = match ulMaxObjectCount.try_into() {
Ok(max_object_count) => max_object_count,
Err(_) => return CKR_ARGUMENTS_BAD,
};
let session: SessionHandle = match hSession.try_into() {
Ok(session) => session,
Err(_) => return CKR_SESSION_HANDLE_INVALID,
};
let out: &mut [CK_OBJECT_HANDLE] =
unsafe { slice::from_raw_parts_mut(phObject, max_object_count) };
match find_objects(session, out) {
Ok(num_found) => {
unsafe { *pulObjectCount = num_found as CK_ULONG };
CKR_OK
}
Err(PK11Error(e)) => e,
}
}
extern "C" fn C_FindObjectsFinal(hSession: CK_SESSION_HANDLE) -> CK_RV {
let session: SessionHandle = match hSession.try_into() {
Ok(session) => session,
Err(_) => return CKR_SESSION_HANDLE_INVALID,
};
match find_objects_final(session) {
Ok(()) => CKR_OK,
Err(PK11Error(e)) => e,
}
}
extern "C" fn C_EncryptInit(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hKey: CK_OBJECT_HANDLE,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_Encrypt(
_hSession: CK_SESSION_HANDLE,
_pData: CK_BYTE_PTR,
_ulDataLen: CK_ULONG,
_pEncryptedData: CK_BYTE_PTR,
_pulEncryptedDataLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_EncryptUpdate(
_hSession: CK_SESSION_HANDLE,
_pPart: CK_BYTE_PTR,
_ulPartLen: CK_ULONG,
_pEncryptedPart: CK_BYTE_PTR,
_pulEncryptedPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_EncryptFinal(
_hSession: CK_SESSION_HANDLE,
_pLastEncryptedPart: CK_BYTE_PTR,
_pulLastEncryptedPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DecryptInit(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hKey: CK_OBJECT_HANDLE,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_Decrypt(
_hSession: CK_SESSION_HANDLE,
_pEncryptedData: CK_BYTE_PTR,
_ulEncryptedDataLen: CK_ULONG,
_pData: CK_BYTE_PTR,
_pulDataLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DecryptUpdate(
_hSession: CK_SESSION_HANDLE,
_pEncryptedPart: CK_BYTE_PTR,
_ulEncryptedPartLen: CK_ULONG,
_pPart: CK_BYTE_PTR,
_pulPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DecryptFinal(
_hSession: CK_SESSION_HANDLE,
_pLastPart: CK_BYTE_PTR,
_pulLastPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DigestInit(_hSession: CK_SESSION_HANDLE, _pMechanism: CK_MECHANISM_PTR) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_Digest(
_hSession: CK_SESSION_HANDLE,
_pData: CK_BYTE_PTR,
_ulDataLen: CK_ULONG,
_pDigest: CK_BYTE_PTR,
_pulDigestLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DigestUpdate(
_hSession: CK_SESSION_HANDLE,
_pPart: CK_BYTE_PTR,
_ulPartLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DigestKey(_hSession: CK_SESSION_HANDLE, _hKey: CK_OBJECT_HANDLE) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DigestFinal(
_hSession: CK_SESSION_HANDLE,
_pDigest: CK_BYTE_PTR,
_pulDigestLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SignInit(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hKey: CK_OBJECT_HANDLE,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_Sign(
_hSession: CK_SESSION_HANDLE,
_pData: CK_BYTE_PTR,
_ulDataLen: CK_ULONG,
_pSignature: CK_BYTE_PTR,
_pulSignatureLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SignUpdate(
_hSession: CK_SESSION_HANDLE,
_pPart: CK_BYTE_PTR,
_ulPartLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SignFinal(
_hSession: CK_SESSION_HANDLE,
_pSignature: CK_BYTE_PTR,
_pulSignatureLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SignRecoverInit(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hKey: CK_OBJECT_HANDLE,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SignRecover(
_hSession: CK_SESSION_HANDLE,
_pData: CK_BYTE_PTR,
_ulDataLen: CK_ULONG,
_pSignature: CK_BYTE_PTR,
_pulSignatureLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_VerifyInit(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hKey: CK_OBJECT_HANDLE,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_Verify(
_hSession: CK_SESSION_HANDLE,
_pData: CK_BYTE_PTR,
_ulDataLen: CK_ULONG,
_pSignature: CK_BYTE_PTR,
_ulSignatureLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_VerifyUpdate(
_hSession: CK_SESSION_HANDLE,
_pPart: CK_BYTE_PTR,
_ulPartLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_VerifyFinal(
_hSession: CK_SESSION_HANDLE,
_pSignature: CK_BYTE_PTR,
_ulSignatureLen: CK_ULONG,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_VerifyRecoverInit(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hKey: CK_OBJECT_HANDLE,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_VerifyRecover(
_hSession: CK_SESSION_HANDLE,
_pSignature: CK_BYTE_PTR,
_ulSignatureLen: CK_ULONG,
_pData: CK_BYTE_PTR,
_pulDataLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DigestEncryptUpdate(
_hSession: CK_SESSION_HANDLE,
_pPart: CK_BYTE_PTR,
_ulPartLen: CK_ULONG,
_pEncryptedPart: CK_BYTE_PTR,
_pulEncryptedPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DecryptDigestUpdate(
_hSession: CK_SESSION_HANDLE,
_pEncryptedPart: CK_BYTE_PTR,
_ulEncryptedPartLen: CK_ULONG,
_pPart: CK_BYTE_PTR,
_pulPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_SignEncryptUpdate(
_hSession: CK_SESSION_HANDLE,
_pPart: CK_BYTE_PTR,
_ulPartLen: CK_ULONG,
_pEncryptedPart: CK_BYTE_PTR,
_pulEncryptedPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_DecryptVerifyUpdate(
_hSession: CK_SESSION_HANDLE,
_pEncryptedPart: CK_BYTE_PTR,
_ulEncryptedPartLen: CK_ULONG,
_pPart: CK_BYTE_PTR,
_pulPartLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_GenerateKey(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_pTemplate: CK_ATTRIBUTE_PTR,
_ulCount: CK_ULONG,
_phKey: CK_OBJECT_HANDLE_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_GenerateKeyPair(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_pPublicKeyTemplate: CK_ATTRIBUTE_PTR,
_ulPublicKeyAttributeCount: CK_ULONG,
_pPrivateKeyTemplate: CK_ATTRIBUTE_PTR,
_ulPrivateKeyAttributeCount: CK_ULONG,
_phPublicKey: CK_OBJECT_HANDLE_PTR,
_phPrivateKey: CK_OBJECT_HANDLE_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_WrapKey(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hWrappingKey: CK_OBJECT_HANDLE,
_hKey: CK_OBJECT_HANDLE,
_pWrappedKey: CK_BYTE_PTR,
_pulWrappedKeyLen: CK_ULONG_PTR,
) -> CK_RV {
CKR_FUNCTION_NOT_SUPPORTED
}
extern "C" fn C_UnwrapKey(
_hSession: CK_SESSION_HANDLE,
_pMechanism: CK_MECHANISM_PTR,
_hUnwrappingKey: CK_OBJECT_HANDLE,
_pWrappedKey: CK_BYTE_PTR,
_ulWrappedKeyLen: CK_ULONG,
_pTemplate: CK_ATTRIBUTE_PTR,