/
types.rs
2643 lines (2281 loc) · 96.3 KB
/
types.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
// Copyright 2017 Marcus Heese
//
// Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#![allow(non_camel_case_types, non_snake_case)]
// Cryptoki's packed structs interfere with the Clone trait, so we implement Copy and use this
macro_rules! packed_clone {
($name:ty) => {
impl Clone for $name {
fn clone(&self) -> $name {
*self
}
}
};
}
// packed structs only needed on Windows, pkcs11.h mentions
macro_rules! cryptoki_aligned {
($decl:item) => {
#[cfg(windows)]
#[repr(packed, C)]
$decl
#[cfg(not(windows))]
#[repr(C)]
$decl
}
}
use num_bigint::BigUint;
use std::mem;
use std::ptr;
use std::slice;
use super::CkFrom;
use crate::errors::Error;
use crate::functions::*;
use crate::types::padding::*;
pub const CK_TRUE: CK_BBOOL = 1;
pub const CK_FALSE: CK_BBOOL = 0;
//// an unsigned 8-bit value
pub type CK_BYTE = u8;
pub type CK_BYTE_PTR = *mut CK_BYTE;
/// an unsigned 8-bit character
pub type CK_CHAR = CK_BYTE;
pub type CK_CHAR_PTR = *mut CK_CHAR;
/// an 8-bit UTF-8 character
pub type CK_UTF8CHAR = CK_BYTE;
pub type CK_UTF8CHAR_PTR = *mut CK_UTF8CHAR;
/// a BYTE-sized Boolean flag
pub type CK_BBOOL = CK_BYTE;
/// an unsigned value, at least 32 bits long
#[cfg(windows)]
pub type CK_ULONG = u32;
#[cfg(all(target_os = "linux", target_arch = "arm"))]
pub type CK_ULONG = u32;
#[cfg(all(not(windows), not(all(target_os = "linux", target_arch = "arm"))))]
pub type CK_ULONG = u64;
pub type CK_ULONG_PTR = *mut CK_ULONG;
/// a signed value, the same size as a CK_ULONG
#[cfg(windows)]
pub type CK_LONG = i32;
#[cfg(all(target_os = "linux", target_arch = "arm"))]
pub type CK_LONG = i32;
#[cfg(all(not(windows), not(all(target_os = "linux", target_arch = "arm"))))]
pub type CK_LONG = i64;
/// at least 32 bits; each bit is a Boolean flag
pub type CK_FLAGS = CK_ULONG;
/* some special values for certain CK_ULONG variables */
pub const CK_UNAVAILABLE_INFORMATION: CK_ULONG = !0;
pub const CK_EFFECTIVELY_INFINITE: CK_ULONG = 0;
#[derive(Debug)]
#[repr(u8)]
pub enum CK_VOID {
#[doc(hidden)]
__Variant1,
#[doc(hidden)]
__Variant2,
}
pub type CK_VOID_PTR = *mut CK_VOID;
/// Pointer to a CK_VOID_PTR-- i.e., pointer to pointer to void
pub type CK_VOID_PTR_PTR = *mut CK_VOID_PTR;
/// The following value is always invalid if used as a session
/// handle or object handle
pub const CK_INVALID_HANDLE: CK_ULONG = 0;
/* NOTE: The newtypes in this module are manual expansions of variants
* of [CK_UTF8CHAR; N].
* A future version of this library should replace these with PaddingStr<N>
* for const N, once Rust stablizes the constant generics feature.
*/
pub mod padding {
use super::{CK_CHAR, CK_UTF8CHAR};
use crate::str_from_blank_padded;
/// Encapsulates a blank-padded 16-byte UTF-8 string for conversion purposes.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct BlankPaddedString16(pub [CK_CHAR; 16]);
impl std::convert::From<BlankPaddedString16> for String {
fn from(field: BlankPaddedString16) -> String {
str_from_blank_padded(&field.0)
}
}
impl Default for BlankPaddedString16 {
fn default() -> Self {
Self { 0: [32; 16] }
}
}
impl std::fmt::Display for BlankPaddedString16 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", String::from(*self))
}
}
impl std::fmt::Debug for BlankPaddedString16 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "CK_CHAR \"{}\"", String::from_utf8_lossy(&self.0))
}
}
/// Encapsulates a blank-padded 16-byte UTF-8 string for conversion purposes.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct BlankPaddedUtf8String16(pub [CK_UTF8CHAR; 16]);
impl std::convert::From<BlankPaddedUtf8String16> for String {
fn from(field: BlankPaddedUtf8String16) -> String {
str_from_blank_padded(&field.0)
}
}
impl Default for BlankPaddedUtf8String16 {
fn default() -> Self {
Self { 0: [32; 16] }
}
}
impl std::fmt::Display for BlankPaddedUtf8String16 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", String::from(*self))
}
}
impl std::fmt::Debug for BlankPaddedUtf8String16 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "CK_UTF8CHAR \"{}\"", String::from_utf8_lossy(&self.0))
}
}
/// Encapsulates a blank-padded 32-byte UTF-8 string for conversion purposes.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct BlankPaddedUtf8String32(pub [CK_UTF8CHAR; 32]);
impl std::convert::From<BlankPaddedUtf8String32> for String {
fn from(field: BlankPaddedUtf8String32) -> String {
str_from_blank_padded(&field.0)
}
}
impl Default for BlankPaddedUtf8String32 {
fn default() -> Self {
Self { 0: [32; 32] }
}
}
impl std::fmt::Display for BlankPaddedUtf8String32 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", String::from(*self))
}
}
impl std::fmt::Debug for BlankPaddedUtf8String32 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "CK_UTF8CHAR \"{}\"", String::from_utf8_lossy(&self.0))
}
}
/// Encapsulates a blank-padded 64-byte UTF-8 string for conversion purposes.
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct BlankPaddedUtf8String64(pub [CK_UTF8CHAR; 64]);
impl std::convert::From<BlankPaddedUtf8String64> for String {
fn from(field: BlankPaddedUtf8String64) -> String {
str_from_blank_padded(&field.0)
}
}
impl Default for BlankPaddedUtf8String64 {
fn default() -> Self {
Self { 0: [32; 64] }
}
}
impl std::fmt::Display for BlankPaddedUtf8String64 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "{}", String::from(*self))
}
}
impl std::fmt::Debug for BlankPaddedUtf8String64 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
write!(f, "CK_UTF8CHAR \"{}\"", String::from_utf8_lossy(&self.0))
}
}
}
cryptoki_aligned! {
#[derive(Debug, Copy, Default)]
pub struct CK_VERSION {
pub major: CK_BYTE, /* integer portion of version number */
pub minor: CK_BYTE, /* 1/100ths portion of version number */
}
}
packed_clone!(CK_VERSION);
impl std::fmt::Display for CK_VERSION {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{}.{}", self.major, self.minor)
}
}
pub type CK_VERSION_PTR = *mut CK_VERSION;
cryptoki_aligned! {
#[derive(Debug, Copy, Default)]
pub struct CK_INFO {
/* manufacturerID and libraryDecription have been changed from
* CK_CHAR to CK_UTF8CHAR for v2.10 */
pub cryptokiVersion: CK_VERSION, /* Cryptoki interface ver */
pub manufacturerID: BlankPaddedUtf8String32, /* blank padded */
pub flags: CK_FLAGS, /* must be zero */
pub libraryDescription: BlankPaddedUtf8String32, /* blank padded */
pub libraryVersion: CK_VERSION, /* version of library */
}
}
packed_clone!(CK_INFO);
impl CK_INFO {
pub fn new() -> CK_INFO {
CK_INFO {
cryptokiVersion: Default::default(),
manufacturerID: Default::default(),
flags: 0,
libraryDescription: Default::default(),
libraryVersion: Default::default(),
}
}
}
pub type CK_INFO_PTR = *mut CK_INFO;
/// CK_NOTIFICATION enumerates the types of notifications that
/// Cryptoki provides to an application
pub type CK_NOTIFICATION = CK_ULONG;
pub const CKN_SURRENDER: CK_NOTIFICATION = 0;
pub const CKN_OTP_CHANGED: CK_NOTIFICATION = 1;
pub type CK_SLOT_ID = CK_ULONG;
pub type CK_SLOT_ID_PTR = *mut CK_SLOT_ID;
cryptoki_aligned! {
/// CK_SLOT_INFO provides information about a slot
#[derive(Default, Debug, Copy)]
pub struct CK_SLOT_INFO {
/// slotDescription and manufacturerID have been changed from
/// CK_CHAR to CK_UTF8CHAR for v2.10
pub slotDescription: BlankPaddedUtf8String64, /* blank padded */
pub manufacturerID: BlankPaddedUtf8String32, /* blank padded */
pub flags: CK_FLAGS,
/// version of hardware
pub hardwareVersion: CK_VERSION, /* version of hardware */
/// version of firmware
pub firmwareVersion: CK_VERSION, /* version of firmware */
}
}
packed_clone! {CK_SLOT_INFO}
/// a token is there
pub const CKF_TOKEN_PRESENT: CK_FLAGS = 0x00000001;
/// removable devices
pub const CKF_REMOVABLE_DEVICE: CK_FLAGS = 0x00000002;
/// hardware slot
pub const CKF_HW_SLOT: CK_FLAGS = 0x00000004;
pub type CK_SLOT_INFO_PTR = *mut CK_SLOT_INFO;
cryptoki_aligned! {
#[derive(Default, Debug, Copy)]
pub struct CK_TOKEN_INFO {
/* label, manufacturerID, and model have been changed from
* CK_CHAR to CK_UTF8CHAR for v2.10 */
pub label: BlankPaddedUtf8String32, /* blank padded */
pub manufacturerID: BlankPaddedUtf8String32, /* blank padded */
pub model: BlankPaddedUtf8String16, /* blank padded */
pub serialNumber: BlankPaddedString16, /* blank padded */
pub flags: CK_FLAGS, /* see below */
pub ulMaxSessionCount: CK_ULONG, /* max open sessions */
pub ulSessionCount: CK_ULONG, /* sess. now open */
pub ulMaxRwSessionCount: CK_ULONG, /* max R/W sessions */
pub ulRwSessionCount: CK_ULONG, /* R/W sess. now open */
pub ulMaxPinLen: CK_ULONG, /* in bytes */
pub ulMinPinLen: CK_ULONG, /* in bytes */
pub ulTotalPublicMemory: CK_ULONG, /* in bytes */
pub ulFreePublicMemory: CK_ULONG, /* in bytes */
pub ulTotalPrivateMemory: CK_ULONG, /* in bytes */
pub ulFreePrivateMemory: CK_ULONG, /* in bytes */
pub hardwareVersion: CK_VERSION, /* version of hardware */
pub firmwareVersion: CK_VERSION, /* version of firmware */
pub utcTime: [CK_CHAR; 16], /* time */
}
}
packed_clone!(CK_TOKEN_INFO);
/// has random # generator
pub const CKF_RNG: CK_FLAGS = 0x00000001;
/// token is write-protected
pub const CKF_WRITE_PROTECTED: CK_FLAGS = 0x00000002;
/// user must login
pub const CKF_LOGIN_REQUIRED: CK_FLAGS = 0x00000004;
/// normal user's PIN is set
pub const CKF_USER_PIN_INITIALIZED: CK_FLAGS = 0x00000008;
/// CKF_RESTORE_KEY_NOT_NEEDED. If it is set,
/// that means that *every* time the state of cryptographic
/// operations of a session is successfully saved, all keys
/// needed to continue those operations are stored in the state
pub const CKF_RESTORE_KEY_NOT_NEEDED: CK_FLAGS = 0x00000020;
/// CKF_CLOCK_ON_TOKEN. If it is set, that means
/// that the token has some sort of clock. The time on that
/// clock is returned in the token info structure
pub const CKF_CLOCK_ON_TOKEN: CK_FLAGS = 0x00000040;
/// CKF_PROTECTED_AUTHENTICATION_PATH. If it is
/// set, that means that there is some way for the user to login
/// without sending a PIN through the Cryptoki library itself
pub const CKF_PROTECTED_AUTHENTICATION_PATH: CK_FLAGS = 0x00000100;
/// CKF_DUAL_CRYPTO_OPERATIONS. If it is true,
/// that means that a single session with the token can perform
/// dual simultaneous cryptographic operations (digest and
/// encrypt; decrypt and digest; sign and encrypt; and decrypt
/// and sign)
pub const CKF_DUAL_CRYPTO_OPERATIONS: CK_FLAGS = 0x00000200;
/// CKF_TOKEN_INITIALIZED. If it is true, the
/// token has been initialized using C_InitializeToken or an
/// equivalent mechanism outside the scope of PKCS #11.
/// Calling C_InitializeToken when this flag is set will cause
/// the token to be reinitialized.
pub const CKF_TOKEN_INITIALIZED: CK_FLAGS = 0x00000400;
/// CKF_SECONDARY_AUTHENTICATION. If it is
/// true, the token supports secondary authentication for
/// private key objects.
pub const CKF_SECONDARY_AUTHENTICATION: CK_FLAGS = 0x00000800;
/// CKF_USER_PIN_COUNT_LOW. If it is true, an
/// incorrect user login PIN has been entered at least once
/// since the last successful authentication.
pub const CKF_USER_PIN_COUNT_LOW: CK_FLAGS = 0x00010000;
/// CKF_USER_PIN_FINAL_TRY. If it is true,
/// supplying an incorrect user PIN will it to become locked.
pub const CKF_USER_PIN_FINAL_TRY: CK_FLAGS = 0x00020000;
/// CKF_USER_PIN_LOCKED. If it is true, the
/// user PIN has been locked. User login to the token is not
/// possible.
pub const CKF_USER_PIN_LOCKED: CK_FLAGS = 0x00040000;
/// CKF_USER_PIN_TO_BE_CHANGED. If it is true,
/// the user PIN value is the default value set by token
/// initialization or manufacturing, or the PIN has been
/// expired by the card.
pub const CKF_USER_PIN_TO_BE_CHANGED: CK_FLAGS = 0x00080000;
/// CKF_SO_PIN_COUNT_LOW. If it is true, an
/// incorrect SO login PIN has been entered at least once since
/// the last successful authentication.
pub const CKF_SO_PIN_COUNT_LOW: CK_FLAGS = 0x00100000;
/// CKF_SO_PIN_FINAL_TRY. If it is true,
/// supplying an incorrect SO PIN will it to become locked.
pub const CKF_SO_PIN_FINAL_TRY: CK_FLAGS = 0x00200000;
/// CKF_SO_PIN_LOCKED. If it is true, the SO
/// PIN has been locked. SO login to the token is not possible.
pub const CKF_SO_PIN_LOCKED: CK_FLAGS = 0x00400000;
/// CKF_SO_PIN_TO_BE_CHANGED. If it is true,
/// the SO PIN value is the default value set by token
/// initialization or manufacturing, or the PIN has been
/// expired by the card.
pub const CKF_SO_PIN_TO_BE_CHANGED: CK_FLAGS = 0x00800000;
pub const CKF_ERROR_STATE: CK_FLAGS = 0x01000000;
pub type CK_TOKEN_INFO_PTR = *mut CK_TOKEN_INFO;
/// CK_SESSION_HANDLE is a Cryptoki-assigned value that
/// identifies a session
pub type CK_SESSION_HANDLE = CK_ULONG;
pub type CK_SESSION_HANDLE_PTR = *mut CK_SESSION_HANDLE;
/// CK_USER_TYPE enumerates the types of Cryptoki users
pub type CK_USER_TYPE = CK_ULONG;
/// Security Officer
pub const CKU_SO: CK_USER_TYPE = 0;
/// Normal user
pub const CKU_USER: CK_USER_TYPE = 1;
/// Context specific
pub const CKU_CONTEXT_SPECIFIC: CK_USER_TYPE = 2;
/// CK_STATE enumerates the session states
type CK_STATE = CK_ULONG;
pub const CKS_RO_PUBLIC_SESSION: CK_STATE = 0;
pub const CKS_RO_USER_FUNCTIONS: CK_STATE = 1;
pub const CKS_RW_PUBLIC_SESSION: CK_STATE = 2;
pub const CKS_RW_USER_FUNCTIONS: CK_STATE = 3;
pub const CKS_RW_SO_FUNCTIONS: CK_STATE = 4;
cryptoki_aligned! {
#[derive(Debug, Default, Copy)]
pub struct CK_SESSION_INFO {
pub slotID: CK_SLOT_ID,
pub state: CK_STATE,
pub flags: CK_FLAGS,
/// device-dependent error code
pub ulDeviceError: CK_ULONG,
}
}
packed_clone!(CK_SESSION_INFO);
/// session is r/w
pub const CKF_RW_SESSION: CK_FLAGS = 0x00000002;
/// no parallel
pub const CKF_SERIAL_SESSION: CK_FLAGS = 0x00000004;
pub type CK_SESSION_INFO_PTR = *mut CK_SESSION_INFO;
/// CK_OBJECT_HANDLE is a token-specific identifier for an
/// object
pub type CK_OBJECT_HANDLE = CK_ULONG;
pub type CK_OBJECT_HANDLE_PTR = *mut CK_OBJECT_HANDLE;
/// CK_OBJECT_CLASS is a value that identifies the classes (or
/// types) of objects that Cryptoki recognizes. It is defined
/// as follows:
pub type CK_OBJECT_CLASS = CK_ULONG;
/// The following classes of objects are defined:
pub const CKO_DATA: CK_OBJECT_CLASS = 0x00000000;
pub const CKO_CERTIFICATE: CK_OBJECT_CLASS = 0x00000001;
pub const CKO_PUBLIC_KEY: CK_OBJECT_CLASS = 0x00000002;
pub const CKO_PRIVATE_KEY: CK_OBJECT_CLASS = 0x00000003;
pub const CKO_SECRET_KEY: CK_OBJECT_CLASS = 0x00000004;
pub const CKO_HW_FEATURE: CK_OBJECT_CLASS = 0x00000005;
pub const CKO_DOMAIN_PARAMETERS: CK_OBJECT_CLASS = 0x00000006;
pub const CKO_MECHANISM: CK_OBJECT_CLASS = 0x00000007;
pub const CKO_OTP_KEY: CK_OBJECT_CLASS = 0x00000008;
pub const CKO_VENDOR_DEFINED: CK_OBJECT_CLASS = 0x80000000;
pub type CK_OBJECT_CLASS_PTR = *mut CK_OBJECT_CLASS;
/// CK_HW_FEATURE_TYPE is a value that identifies the hardware feature type
/// of an object with CK_OBJECT_CLASS equal to CKO_HW_FEATURE.
pub type CK_HW_FEATURE_TYPE = CK_ULONG;
/// The following hardware feature types are defined
pub const CKH_MONOTONIC_COUNTER: CK_HW_FEATURE_TYPE = 0x00000001;
pub const CKH_CLOCK: CK_HW_FEATURE_TYPE = 0x00000002;
pub const CKH_USER_INTERFACE: CK_HW_FEATURE_TYPE = 0x00000003;
pub const CKH_VENDOR_DEFINED: CK_HW_FEATURE_TYPE = 0x80000000;
/// CK_KEY_TYPE is a value that identifies a key type
pub type CK_KEY_TYPE = CK_ULONG;
/// the following key types are defined:
pub const CKK_RSA: CK_KEY_TYPE = 0x00000000;
pub const CKK_DSA: CK_KEY_TYPE = 0x00000001;
pub const CKK_DH: CK_KEY_TYPE = 0x00000002;
pub const CKK_ECDSA: CK_KEY_TYPE = CKK_EC;
pub const CKK_EC: CK_KEY_TYPE = 0x00000003;
pub const CKK_X9_42_DH: CK_KEY_TYPE = 0x00000004;
pub const CKK_KEA: CK_KEY_TYPE = 0x00000005;
pub const CKK_GENERIC_SECRET: CK_KEY_TYPE = 0x00000010;
pub const CKK_RC2: CK_KEY_TYPE = 0x00000011;
pub const CKK_RC4: CK_KEY_TYPE = 0x00000012;
pub const CKK_DES: CK_KEY_TYPE = 0x00000013;
pub const CKK_DES2: CK_KEY_TYPE = 0x00000014;
pub const CKK_DES3: CK_KEY_TYPE = 0x00000015;
pub const CKK_CAST: CK_KEY_TYPE = 0x00000016;
pub const CKK_CAST3: CK_KEY_TYPE = 0x00000017;
pub const CKK_CAST5: CK_KEY_TYPE = CKK_CAST128;
pub const CKK_CAST128: CK_KEY_TYPE = 0x00000018;
pub const CKK_RC5: CK_KEY_TYPE = 0x00000019;
pub const CKK_IDEA: CK_KEY_TYPE = 0x0000001A;
pub const CKK_SKIPJACK: CK_KEY_TYPE = 0x0000001B;
pub const CKK_BATON: CK_KEY_TYPE = 0x0000001C;
pub const CKK_JUNIPER: CK_KEY_TYPE = 0x0000001D;
pub const CKK_CDMF: CK_KEY_TYPE = 0x0000001E;
pub const CKK_AES: CK_KEY_TYPE = 0x0000001F;
pub const CKK_BLOWFISH: CK_KEY_TYPE = 0x00000020;
pub const CKK_TWOFISH: CK_KEY_TYPE = 0x00000021;
pub const CKK_SECURID: CK_KEY_TYPE = 0x00000022;
pub const CKK_HOTP: CK_KEY_TYPE = 0x00000023;
pub const CKK_ACTI: CK_KEY_TYPE = 0x00000024;
pub const CKK_CAMELLIA: CK_KEY_TYPE = 0x00000025;
pub const CKK_ARIA: CK_KEY_TYPE = 0x00000026;
pub const CKK_MD5_HMAC: CK_KEY_TYPE = 0x00000027;
pub const CKK_SHA_1_HMAC: CK_KEY_TYPE = 0x00000028;
pub const CKK_RIPEMD128_HMAC: CK_KEY_TYPE = 0x00000029;
pub const CKK_RIPEMD160_HMAC: CK_KEY_TYPE = 0x0000002A;
pub const CKK_SHA256_HMAC: CK_KEY_TYPE = 0x0000002B;
pub const CKK_SHA384_HMAC: CK_KEY_TYPE = 0x0000002C;
pub const CKK_SHA512_HMAC: CK_KEY_TYPE = 0x0000002D;
pub const CKK_SHA224_HMAC: CK_KEY_TYPE = 0x0000002E;
pub const CKK_SEED: CK_KEY_TYPE = 0x0000002F;
pub const CKK_GOSTR3410: CK_KEY_TYPE = 0x00000030;
pub const CKK_GOSTR3411: CK_KEY_TYPE = 0x00000031;
pub const CKK_GOST28147: CK_KEY_TYPE = 0x00000032;
pub const CKK_VENDOR_DEFINED: CK_KEY_TYPE = 0x80000000;
/// CK_CERTIFICATE_TYPE is a value that identifies a certificate
/// type
pub type CK_CERTIFICATE_TYPE = CK_ULONG;
pub const CK_CERTIFICATE_CATEGORY_UNSPECIFIED: CK_ULONG = 0;
pub const CK_CERTIFICATE_CATEGORY_TOKEN_USER: CK_ULONG = 1;
pub const CK_CERTIFICATE_CATEGORY_AUTHORITY: CK_ULONG = 2;
pub const CK_CERTIFICATE_CATEGORY_OTHER_ENTITY: CK_ULONG = 3;
pub const CK_SECURITY_DOMAIN_UNSPECIFIED: CK_ULONG = 0;
pub const CK_SECURITY_DOMAIN_MANUFACTURER: CK_ULONG = 1;
pub const CK_SECURITY_DOMAIN_OPERATOR: CK_ULONG = 2;
pub const CK_SECURITY_DOMAIN_THIRD_PARTY: CK_ULONG = 3;
/// The following certificate types are defined:
pub const CKC_X_509: CK_CERTIFICATE_TYPE = 0x00000000;
pub const CKC_X_509_ATTR_CERT: CK_CERTIFICATE_TYPE = 0x00000001;
pub const CKC_WTLS: CK_CERTIFICATE_TYPE = 0x00000002;
pub const CKC_VENDOR_DEFINED: CK_CERTIFICATE_TYPE = 0x80000000;
/// CK_ATTRIBUTE_TYPE is a value that identifies an attribute
/// type
pub type CK_ATTRIBUTE_TYPE = CK_ULONG;
/// The CKF_ARRAY_ATTRIBUTE flag identifies an attribute which
/// consists of an array of values.
pub const CKF_ARRAY_ATTRIBUTE: CK_FLAGS = 0x40000000;
/// The following OTP-related defines relate to the CKA_OTP_FORMAT attribute
pub const CK_OTP_FORMAT_DECIMAL: CK_ULONG = 0;
pub const CK_OTP_FORMAT_HEXADECIMAL: CK_ULONG = 1;
pub const CK_OTP_FORMAT_ALPHANUMERIC: CK_ULONG = 2;
pub const CK_OTP_FORMAT_BINARY: CK_ULONG = 3;
/// The following OTP-related defines relate to the CKA_OTP_..._REQUIREMENT
/// attributes
pub const CK_OTP_PARAM_IGNORED: CK_ULONG = 0;
pub const CK_OTP_PARAM_OPTIONAL: CK_ULONG = 1;
pub const CK_OTP_PARAM_MANDATORY: CK_ULONG = 2;
/// The following attribute types are defined:
pub const CKA_CLASS: CK_ATTRIBUTE_TYPE = 0x00000000;
pub const CKA_TOKEN: CK_ATTRIBUTE_TYPE = 0x00000001;
pub const CKA_PRIVATE: CK_ATTRIBUTE_TYPE = 0x00000002;
pub const CKA_LABEL: CK_ATTRIBUTE_TYPE = 0x00000003;
pub const CKA_APPLICATION: CK_ATTRIBUTE_TYPE = 0x00000010;
pub const CKA_VALUE: CK_ATTRIBUTE_TYPE = 0x00000011;
pub const CKA_OBJECT_ID: CK_ATTRIBUTE_TYPE = 0x00000012;
pub const CKA_CERTIFICATE_TYPE: CK_ATTRIBUTE_TYPE = 0x00000080;
pub const CKA_ISSUER: CK_ATTRIBUTE_TYPE = 0x00000081;
pub const CKA_SERIAL_NUMBER: CK_ATTRIBUTE_TYPE = 0x00000082;
pub const CKA_AC_ISSUER: CK_ATTRIBUTE_TYPE = 0x00000083;
pub const CKA_OWNER: CK_ATTRIBUTE_TYPE = 0x00000084;
pub const CKA_ATTR_TYPES: CK_ATTRIBUTE_TYPE = 0x00000085;
pub const CKA_TRUSTED: CK_ATTRIBUTE_TYPE = 0x00000086;
pub const CKA_CERTIFICATE_CATEGORY: CK_ATTRIBUTE_TYPE = 0x00000087;
pub const CKA_JAVA_MIDP_SECURITY_DOMAIN: CK_ATTRIBUTE_TYPE = 0x00000088;
pub const CKA_URL: CK_ATTRIBUTE_TYPE = 0x00000089;
pub const CKA_HASH_OF_SUBJECT_PUBLIC_KEY: CK_ATTRIBUTE_TYPE = 0x0000008A;
pub const CKA_HASH_OF_ISSUER_PUBLIC_KEY: CK_ATTRIBUTE_TYPE = 0x0000008B;
pub const CKA_NAME_HASH_ALGORITHM: CK_ATTRIBUTE_TYPE = 0x0000008C;
pub const CKA_CHECK_VALUE: CK_ATTRIBUTE_TYPE = 0x00000090;
pub const CKA_KEY_TYPE: CK_ATTRIBUTE_TYPE = 0x00000100;
pub const CKA_SUBJECT: CK_ATTRIBUTE_TYPE = 0x00000101;
pub const CKA_ID: CK_ATTRIBUTE_TYPE = 0x00000102;
pub const CKA_SENSITIVE: CK_ATTRIBUTE_TYPE = 0x00000103;
pub const CKA_ENCRYPT: CK_ATTRIBUTE_TYPE = 0x00000104;
pub const CKA_DECRYPT: CK_ATTRIBUTE_TYPE = 0x00000105;
pub const CKA_WRAP: CK_ATTRIBUTE_TYPE = 0x00000106;
pub const CKA_UNWRAP: CK_ATTRIBUTE_TYPE = 0x00000107;
pub const CKA_SIGN: CK_ATTRIBUTE_TYPE = 0x00000108;
pub const CKA_SIGN_RECOVER: CK_ATTRIBUTE_TYPE = 0x00000109;
pub const CKA_VERIFY: CK_ATTRIBUTE_TYPE = 0x0000010A;
pub const CKA_VERIFY_RECOVER: CK_ATTRIBUTE_TYPE = 0x0000010B;
pub const CKA_DERIVE: CK_ATTRIBUTE_TYPE = 0x0000010C;
pub const CKA_START_DATE: CK_ATTRIBUTE_TYPE = 0x00000110;
pub const CKA_END_DATE: CK_ATTRIBUTE_TYPE = 0x00000111;
pub const CKA_MODULUS: CK_ATTRIBUTE_TYPE = 0x00000120;
pub const CKA_MODULUS_BITS: CK_ATTRIBUTE_TYPE = 0x00000121;
pub const CKA_PUBLIC_EXPONENT: CK_ATTRIBUTE_TYPE = 0x00000122;
pub const CKA_PRIVATE_EXPONENT: CK_ATTRIBUTE_TYPE = 0x00000123;
pub const CKA_PRIME_1: CK_ATTRIBUTE_TYPE = 0x00000124;
pub const CKA_PRIME_2: CK_ATTRIBUTE_TYPE = 0x00000125;
pub const CKA_EXPONENT_1: CK_ATTRIBUTE_TYPE = 0x00000126;
pub const CKA_EXPONENT_2: CK_ATTRIBUTE_TYPE = 0x00000127;
pub const CKA_COEFFICIENT: CK_ATTRIBUTE_TYPE = 0x00000128;
pub const CKA_PUBLIC_KEY_INFO: CK_ATTRIBUTE_TYPE = 0x00000129;
pub const CKA_PRIME: CK_ATTRIBUTE_TYPE = 0x00000130;
pub const CKA_SUBPRIME: CK_ATTRIBUTE_TYPE = 0x00000131;
pub const CKA_BASE: CK_ATTRIBUTE_TYPE = 0x00000132;
pub const CKA_PRIME_BITS: CK_ATTRIBUTE_TYPE = 0x00000133;
pub const CKA_SUBPRIME_BITS: CK_ATTRIBUTE_TYPE = 0x00000134;
pub const CKA_SUB_PRIME_BITS: CK_ATTRIBUTE_TYPE = CKA_SUBPRIME_BITS;
pub const CKA_VALUE_BITS: CK_ATTRIBUTE_TYPE = 0x00000160;
pub const CKA_VALUE_LEN: CK_ATTRIBUTE_TYPE = 0x00000161;
pub const CKA_EXTRACTABLE: CK_ATTRIBUTE_TYPE = 0x00000162;
pub const CKA_LOCAL: CK_ATTRIBUTE_TYPE = 0x00000163;
pub const CKA_NEVER_EXTRACTABLE: CK_ATTRIBUTE_TYPE = 0x00000164;
pub const CKA_ALWAYS_SENSITIVE: CK_ATTRIBUTE_TYPE = 0x00000165;
pub const CKA_KEY_GEN_MECHANISM: CK_ATTRIBUTE_TYPE = 0x00000166;
pub const CKA_MODIFIABLE: CK_ATTRIBUTE_TYPE = 0x00000170;
pub const CKA_COPYABLE: CK_ATTRIBUTE_TYPE = 0x00000171;
pub const CKA_DESTROYABLE: CK_ATTRIBUTE_TYPE = 0x00000172;
pub const CKA_ECDSA_PARAMS: CK_ATTRIBUTE_TYPE = CKA_EC_PARAMS;
pub const CKA_EC_PARAMS: CK_ATTRIBUTE_TYPE = 0x00000180;
pub const CKA_EC_POINT: CK_ATTRIBUTE_TYPE = 0x00000181;
pub const CKA_SECONDARY_AUTH: CK_ATTRIBUTE_TYPE = 0x00000200; /* Deprecated */
pub const CKA_AUTH_PIN_FLAGS: CK_ATTRIBUTE_TYPE = 0x00000201; /* Deprecated */
pub const CKA_ALWAYS_AUTHENTICATE: CK_ATTRIBUTE_TYPE = 0x00000202;
pub const CKA_WRAP_WITH_TRUSTED: CK_ATTRIBUTE_TYPE = 0x00000210;
pub const CKA_WRAP_TEMPLATE: CK_ATTRIBUTE_TYPE = CKF_ARRAY_ATTRIBUTE | 0x00000211;
pub const CKA_UNWRAP_TEMPLATE: CK_ATTRIBUTE_TYPE = CKF_ARRAY_ATTRIBUTE | 0x00000212;
pub const CKA_DERIVE_TEMPLATE: CK_ATTRIBUTE_TYPE = CKF_ARRAY_ATTRIBUTE | 0x00000213;
pub const CKA_OTP_FORMAT: CK_ATTRIBUTE_TYPE = 0x00000220;
pub const CKA_OTP_LENGTH: CK_ATTRIBUTE_TYPE = 0x00000221;
pub const CKA_OTP_TIME_INTERVAL: CK_ATTRIBUTE_TYPE = 0x00000222;
pub const CKA_OTP_USER_FRIENDLY_MODE: CK_ATTRIBUTE_TYPE = 0x00000223;
pub const CKA_OTP_CHALLENGE_REQUIREMENT: CK_ATTRIBUTE_TYPE = 0x00000224;
pub const CKA_OTP_TIME_REQUIREMENT: CK_ATTRIBUTE_TYPE = 0x00000225;
pub const CKA_OTP_COUNTER_REQUIREMENT: CK_ATTRIBUTE_TYPE = 0x00000226;
pub const CKA_OTP_PIN_REQUIREMENT: CK_ATTRIBUTE_TYPE = 0x00000227;
pub const CKA_OTP_COUNTER: CK_ATTRIBUTE_TYPE = 0x0000022E;
pub const CKA_OTP_TIME: CK_ATTRIBUTE_TYPE = 0x0000022F;
pub const CKA_OTP_USER_IDENTIFIER: CK_ATTRIBUTE_TYPE = 0x0000022A;
pub const CKA_OTP_SERVICE_IDENTIFIER: CK_ATTRIBUTE_TYPE = 0x0000022B;
pub const CKA_OTP_SERVICE_LOGO: CK_ATTRIBUTE_TYPE = 0x0000022C;
pub const CKA_OTP_SERVICE_LOGO_TYPE: CK_ATTRIBUTE_TYPE = 0x0000022D;
pub const CKA_GOSTR3410_PARAMS: CK_ATTRIBUTE_TYPE = 0x00000250;
pub const CKA_GOSTR3411_PARAMS: CK_ATTRIBUTE_TYPE = 0x00000251;
pub const CKA_GOST28147_PARAMS: CK_ATTRIBUTE_TYPE = 0x00000252;
pub const CKA_HW_FEATURE_TYPE: CK_ATTRIBUTE_TYPE = 0x00000300;
pub const CKA_RESET_ON_INIT: CK_ATTRIBUTE_TYPE = 0x00000301;
pub const CKA_HAS_RESET: CK_ATTRIBUTE_TYPE = 0x00000302;
pub const CKA_PIXEL_X: CK_ATTRIBUTE_TYPE = 0x00000400;
pub const CKA_PIXEL_Y: CK_ATTRIBUTE_TYPE = 0x00000401;
pub const CKA_RESOLUTION: CK_ATTRIBUTE_TYPE = 0x00000402;
pub const CKA_CHAR_ROWS: CK_ATTRIBUTE_TYPE = 0x00000403;
pub const CKA_CHAR_COLUMNS: CK_ATTRIBUTE_TYPE = 0x00000404;
pub const CKA_COLOR: CK_ATTRIBUTE_TYPE = 0x00000405;
pub const CKA_BITS_PER_PIXEL: CK_ATTRIBUTE_TYPE = 0x00000406;
pub const CKA_CHAR_SETS: CK_ATTRIBUTE_TYPE = 0x00000480;
pub const CKA_ENCODING_METHODS: CK_ATTRIBUTE_TYPE = 0x00000481;
pub const CKA_MIME_TYPES: CK_ATTRIBUTE_TYPE = 0x00000482;
pub const CKA_MECHANISM_TYPE: CK_ATTRIBUTE_TYPE = 0x00000500;
pub const CKA_REQUIRED_CMS_ATTRIBUTES: CK_ATTRIBUTE_TYPE = 0x00000501;
pub const CKA_DEFAULT_CMS_ATTRIBUTES: CK_ATTRIBUTE_TYPE = 0x00000502;
pub const CKA_SUPPORTED_CMS_ATTRIBUTES: CK_ATTRIBUTE_TYPE = 0x00000503;
pub const CKA_ALLOWED_MECHANISMS: CK_ATTRIBUTE_TYPE = CKF_ARRAY_ATTRIBUTE | 0x00000600;
pub const CKA_VENDOR_DEFINED: CK_ATTRIBUTE_TYPE = 0x80000000;
cryptoki_aligned! {
/// CK_ATTRIBUTE is a structure that includes the type, length
/// and value of an attribute
#[derive(Copy)]
pub struct CK_ATTRIBUTE {
pub attrType: CK_ATTRIBUTE_TYPE,
pub pValue: CK_VOID_PTR,
/// in bytes
pub ulValueLen: CK_ULONG,
}
}
packed_clone!(CK_ATTRIBUTE);
pub type CK_ATTRIBUTE_PTR = *mut CK_ATTRIBUTE;
impl Default for CK_ATTRIBUTE {
fn default() -> Self {
Self {
attrType: CKA_VENDOR_DEFINED,
pValue: ptr::null_mut(),
ulValueLen: 0,
}
}
}
impl std::fmt::Debug for CK_ATTRIBUTE {
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
let attrType = format!("0x{:x}", { self.attrType });
let data = if self.is_value_unavailable() {
// That allows to still debug unavailable values
&[]
} else {
unsafe { slice::from_raw_parts(self.pValue as *const u8, self.ulValueLen as usize) }
};
fmt.debug_struct("CK_ATTRIBUTE")
.field("attrType", &attrType)
.field("pValue", &data)
.field("ulValueLen", &{ self.ulValueLen })
.finish()
}
}
impl CK_ATTRIBUTE {
pub fn new(attrType: CK_ATTRIBUTE_TYPE) -> Self {
Self {
attrType,
pValue: ptr::null_mut(),
ulValueLen: 0,
}
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn with_bool(mut self, b: &CK_BBOOL) -> Self {
self.pValue = b as *const CK_BBOOL as CK_VOID_PTR;
self.ulValueLen = 1;
self
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn set_bool(&mut self, b: &CK_BBOOL) {
self.pValue = b as *const CK_BBOOL as CK_VOID_PTR;
if self.ulValueLen == 0 {
self.ulValueLen = 1;
}
}
pub fn get_bool(&self) -> Result<bool, Error> {
self.available_value()?;
let data: CK_BBOOL = unsafe { mem::transmute_copy(&*self.pValue) };
Ok(CkFrom::from(data))
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn with_ck_ulong(mut self, val: &CK_ULONG) -> Self {
self.pValue = val as *const _ as CK_VOID_PTR;
self.ulValueLen = std::mem::size_of::<CK_ULONG>() as CK_ULONG;
self
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn set_ck_ulong(&mut self, val: &CK_ULONG) {
self.pValue = val as *const _ as CK_VOID_PTR;
if self.ulValueLen == 0 {
self.ulValueLen = std::mem::size_of::<CK_ULONG>() as CK_ULONG;
}
}
pub fn get_ck_ulong(&self) -> Result<CK_ULONG, Error> {
self.available_value()?;
Ok(unsafe { mem::transmute_copy(&*self.pValue) })
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn with_ck_long(mut self, val: &CK_LONG) -> Self {
self.pValue = val as *const _ as CK_VOID_PTR;
self.ulValueLen = std::mem::size_of::<CK_LONG>() as CK_ULONG;
self
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn set_ck_long(&mut self, val: &CK_LONG) {
self.pValue = val as *const _ as CK_VOID_PTR;
if self.ulValueLen == 0 {
self.ulValueLen = std::mem::size_of::<CK_LONG>() as CK_ULONG;
}
}
pub fn get_ck_long(&self) -> Result<CK_LONG, Error> {
self.available_value()?;
Ok(unsafe { mem::transmute_copy(&*self.pValue) })
}
pub fn with_biginteger(mut self, val: &[u8]) -> Self {
self.pValue = val.as_ptr() as CK_VOID_PTR;
self.ulValueLen = val.len() as CK_ULONG;
self
}
pub fn set_biginteger(&mut self, val: &[u8]) {
self.pValue = val.as_ptr() as CK_VOID_PTR;
if self.ulValueLen == 0 {
self.ulValueLen = val.len() as CK_ULONG;
}
}
pub fn get_biginteger(&self) -> Result<BigUint, Error> {
self.available_value()?;
let slice =
unsafe { slice::from_raw_parts(self.pValue as CK_BYTE_PTR, self.ulValueLen as usize) };
Ok(BigUint::from_bytes_le(slice))
}
pub fn with_bytes(mut self, val: &[CK_BYTE]) -> Self {
self.pValue = val.as_ptr() as CK_VOID_PTR;
self.ulValueLen = val.len() as CK_ULONG;
self
}
pub fn set_bytes(&mut self, val: &[CK_BYTE]) {
self.pValue = val.as_ptr() as CK_VOID_PTR;
if self.ulValueLen == 0 {
self.ulValueLen = val.len() as CK_ULONG;
}
}
pub fn get_bytes(&self) -> Result<Vec<CK_BYTE>, Error> {
self.available_value()?;
let slice =
unsafe { slice::from_raw_parts(self.pValue as CK_BYTE_PTR, self.ulValueLen as usize) };
Ok(Vec::from(slice))
}
pub fn with_string(mut self, str: &str) -> Self {
self.pValue = str.as_ptr() as CK_VOID_PTR;
self.ulValueLen = str.len() as CK_ULONG;
self
}
pub fn set_string(&mut self, str: &str) {
self.pValue = str.as_ptr() as CK_VOID_PTR;
if self.ulValueLen == 0 {
self.ulValueLen = str.len() as CK_ULONG;
}
}
pub fn get_string(&self) -> Result<String, Error> {
self.available_value()?;
let slice =
unsafe { slice::from_raw_parts(self.pValue as CK_BYTE_PTR, self.ulValueLen as usize) };
Ok(String::from_utf8_lossy(slice).into_owned())
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn with_date(mut self, date: &CK_DATE) -> Self {
self.pValue = (date as *const CK_DATE) as CK_VOID_PTR;
self.ulValueLen = mem::size_of::<CK_DATE>() as CK_ULONG;
self
}
#[allow(clippy::trivially_copy_pass_by_ref)]
pub fn set_date(&mut self, date: &CK_DATE) {
self.pValue = (date as *const CK_DATE) as CK_VOID_PTR;
if self.ulValueLen == 0 {
self.ulValueLen = mem::size_of::<CK_DATE>() as CK_ULONG;
}
}
pub fn get_date(&self) -> Result<CK_DATE, Error> {
self.available_value()?;
Ok(unsafe { mem::transmute_copy(&*self.pValue) })
}
/// Check if the value contained by this attribute is invalid or unavailable.
/// This function needs to be called to safely interpret the bytes the attribute contains with
/// `from_raw_parts`.
/// Same function that `is_value_unavailable`.
pub fn is_value_invalid(&self) -> bool {
self.is_value_unavailable()
}
/// Check if the value contained by this attribute is invalid or unavailable.
/// This function needs to be called to safely interpret the bytes the attribute contains with
/// `from_raw_parts`.
/// Same function that `is_value_invalid`.
pub fn is_value_unavailable(&self) -> bool {
// get_attribute_value can set the ulValueLen field of this attribute to
// CK_UNAVAILABLE_INFORMATION
self.ulValueLen == CK_UNAVAILABLE_INFORMATION
}
/// Check if the value contained by this attribute is invalid or unavailable in a faillible way.
/// Same as `valid_value`.
fn available_value(&self) -> Result<(), Error> {
if self.is_value_unavailable() {
Err(Error::UnavailableInformation)
} else {
Ok(())
}
}
// this works for C structs and primitives, but not for vectors, slices, strings
//pub fn set_ptr<T>(&mut self, val: &T) {
// self.pValue = (val as *const T) as CK_VOID_PTR;
//}
}
//trait CkAttributeFrom<T> {
// fn from_ck(T, CK_ATTRIBUTE_TYPE) -> Self;
//}
//
//trait CkAttributeInto<T> {
// fn into_attribute(self, CK_ATTRIBUTE_TYPE) -> CK_ATTRIBUTE;
//}
//
//impl<T> CkAttributeInto<T> for T where CK_ATTRIBUTE: CkAttributeFrom<T> {
// fn into_attribute(self, attrType: CK_ATTRIBUTE_TYPE) -> CK_ATTRIBUTE {
// CkAttributeFrom::from_ck(self, attrType)
// }
//}
//
//impl CkAttributeFrom<bool> for CK_ATTRIBUTE {
// fn from_ck(b: bool, attrType: CK_ATTRIBUTE_TYPE) -> CK_ATTRIBUTE {
// let val: CK_BBOOL = if b { 1 } else { 0 };
// let ret = Self {
// attrType: attrType,
// pValue: &val as *const u8 as *const CK_VOID,
// ulValueLen: 1,
// };
// println!("{:?}", ret);
// ret
// }
//}
cryptoki_aligned! {
/// CK_DATE is a structure that defines a date
#[derive(Debug, Default, Copy)]
pub struct CK_DATE {
/// the year ("1900" - "9999")
pub year: [CK_CHAR; 4],
/// the month ("01" - "12")
pub month: [CK_CHAR; 2],
/// the day ("01" - "31")
pub day: [CK_CHAR; 2],
}
}
packed_clone!(CK_DATE);
/// CK_MECHANISM_TYPE is a value that identifies a mechanism
/// type
pub type CK_MECHANISM_TYPE = CK_ULONG;
/// the following mechanism types are defined:
pub const CKM_RSA_PKCS_KEY_PAIR_GEN: CK_MECHANISM_TYPE = 0x00000000;