Skip to main content

kryoptic_lib/pkcs11/
mod.rs

1// Copyright 2025 Simo Sorce
2// See LICENSE.txt file for terms
3
4//! This module is automatically generated by `bindgen` from the
5//! `PKCS #11 Headers` and includes all constants and objects
6//! defined in the `PKCS #11 API` version selected for the build
7
8#![allow(non_upper_case_globals)]
9#![allow(non_camel_case_types)]
10#![allow(non_snake_case)]
11#![allow(dead_code)]
12
13use std::borrow::Cow;
14
15use crate::error::Result;
16use crate::misc::{bytes_to_slice, struct_to_slice};
17
18include!(concat!(env!("OUT_DIR"), "/pkcs11_bindings.rs"));
19
20// types that need different mutability than bindgen provides
21pub type CK_FUNCTION_LIST_PTR = *const CK_FUNCTION_LIST;
22pub type CK_FUNCTION_LIST_3_0_PTR = *const CK_FUNCTION_LIST_3_0;
23pub type CK_FUNCTION_LIST_3_2_PTR = *const CK_FUNCTION_LIST_3_2;
24// this is wrongly converted on 32b architecture to too large value
25// which can not be represented in CK_ULONG.
26pub const CK_UNAVAILABLE_INFORMATION: CK_ULONG = CK_ULONG::MAX;
27#[repr(C)]
28#[derive(Debug, Copy, Clone)]
29pub struct CK_INTERFACE {
30    pub pInterfaceName: *const CK_CHAR,
31    pub pFunctionList: *const ::std::os::raw::c_void,
32    pub flags: CK_FLAGS,
33}
34unsafe impl Sync for CK_INTERFACE {}
35unsafe impl Send for CK_INTERFACE {}
36
37pub mod vendor;
38
39macro_rules! const_to_elem {
40    ($name:ident) => {
41        ($name, stringify!($name))
42    };
43}
44
45/// Static map with error num -> error string pairs
46static ERRORMAP: [(CK_ULONG, &str); 104] = [
47    const_to_elem!(CKR_OK),
48    const_to_elem!(CKR_CANCEL),
49    const_to_elem!(CKR_HOST_MEMORY),
50    const_to_elem!(CKR_SLOT_ID_INVALID),
51    const_to_elem!(CKR_GENERAL_ERROR),
52    const_to_elem!(CKR_FUNCTION_FAILED),
53    const_to_elem!(CKR_ARGUMENTS_BAD),
54    const_to_elem!(CKR_NO_EVENT),
55    const_to_elem!(CKR_NEED_TO_CREATE_THREADS),
56    const_to_elem!(CKR_CANT_LOCK),
57    const_to_elem!(CKR_ATTRIBUTE_READ_ONLY),
58    const_to_elem!(CKR_ATTRIBUTE_SENSITIVE),
59    const_to_elem!(CKR_ATTRIBUTE_TYPE_INVALID),
60    const_to_elem!(CKR_ATTRIBUTE_VALUE_INVALID),
61    const_to_elem!(CKR_ACTION_PROHIBITED),
62    const_to_elem!(CKR_DATA_INVALID),
63    const_to_elem!(CKR_DATA_LEN_RANGE),
64    const_to_elem!(CKR_DEVICE_ERROR),
65    const_to_elem!(CKR_DEVICE_MEMORY),
66    const_to_elem!(CKR_DEVICE_REMOVED),
67    const_to_elem!(CKR_ENCRYPTED_DATA_INVALID),
68    const_to_elem!(CKR_ENCRYPTED_DATA_LEN_RANGE),
69    const_to_elem!(CKR_AEAD_DECRYPT_FAILED),
70    const_to_elem!(CKR_FUNCTION_CANCELED),
71    const_to_elem!(CKR_FUNCTION_NOT_PARALLEL),
72    const_to_elem!(CKR_FUNCTION_NOT_SUPPORTED),
73    const_to_elem!(CKR_KEY_HANDLE_INVALID),
74    const_to_elem!(CKR_KEY_SIZE_RANGE),
75    const_to_elem!(CKR_KEY_TYPE_INCONSISTENT),
76    const_to_elem!(CKR_KEY_NOT_NEEDED),
77    const_to_elem!(CKR_KEY_CHANGED),
78    const_to_elem!(CKR_KEY_NEEDED),
79    const_to_elem!(CKR_KEY_INDIGESTIBLE),
80    const_to_elem!(CKR_KEY_FUNCTION_NOT_PERMITTED),
81    const_to_elem!(CKR_KEY_NOT_WRAPPABLE),
82    const_to_elem!(CKR_KEY_UNEXTRACTABLE),
83    const_to_elem!(CKR_MECHANISM_INVALID),
84    const_to_elem!(CKR_MECHANISM_PARAM_INVALID),
85    const_to_elem!(CKR_OBJECT_HANDLE_INVALID),
86    const_to_elem!(CKR_OPERATION_ACTIVE),
87    const_to_elem!(CKR_OPERATION_NOT_INITIALIZED),
88    const_to_elem!(CKR_PIN_INCORRECT),
89    const_to_elem!(CKR_PIN_INVALID),
90    const_to_elem!(CKR_PIN_LEN_RANGE),
91    const_to_elem!(CKR_PIN_EXPIRED),
92    const_to_elem!(CKR_PIN_LOCKED),
93    const_to_elem!(CKR_SESSION_CLOSED),
94    const_to_elem!(CKR_SESSION_COUNT),
95    const_to_elem!(CKR_SESSION_HANDLE_INVALID),
96    const_to_elem!(CKR_SESSION_PARALLEL_NOT_SUPPORTED),
97    const_to_elem!(CKR_SESSION_READ_ONLY),
98    const_to_elem!(CKR_SESSION_EXISTS),
99    const_to_elem!(CKR_SESSION_READ_ONLY_EXISTS),
100    const_to_elem!(CKR_SESSION_READ_WRITE_SO_EXISTS),
101    const_to_elem!(CKR_SIGNATURE_INVALID),
102    const_to_elem!(CKR_SIGNATURE_LEN_RANGE),
103    const_to_elem!(CKR_TEMPLATE_INCOMPLETE),
104    const_to_elem!(CKR_TEMPLATE_INCONSISTENT),
105    const_to_elem!(CKR_TOKEN_NOT_PRESENT),
106    const_to_elem!(CKR_TOKEN_NOT_RECOGNIZED),
107    const_to_elem!(CKR_TOKEN_WRITE_PROTECTED),
108    const_to_elem!(CKR_UNWRAPPING_KEY_HANDLE_INVALID),
109    const_to_elem!(CKR_UNWRAPPING_KEY_SIZE_RANGE),
110    const_to_elem!(CKR_UNWRAPPING_KEY_TYPE_INCONSISTENT),
111    const_to_elem!(CKR_USER_ALREADY_LOGGED_IN),
112    const_to_elem!(CKR_USER_NOT_LOGGED_IN),
113    const_to_elem!(CKR_USER_PIN_NOT_INITIALIZED),
114    const_to_elem!(CKR_USER_TYPE_INVALID),
115    const_to_elem!(CKR_USER_ANOTHER_ALREADY_LOGGED_IN),
116    const_to_elem!(CKR_USER_TOO_MANY_TYPES),
117    const_to_elem!(CKR_WRAPPED_KEY_INVALID),
118    const_to_elem!(CKR_WRAPPED_KEY_LEN_RANGE),
119    const_to_elem!(CKR_WRAPPING_KEY_HANDLE_INVALID),
120    const_to_elem!(CKR_WRAPPING_KEY_SIZE_RANGE),
121    const_to_elem!(CKR_WRAPPING_KEY_TYPE_INCONSISTENT),
122    const_to_elem!(CKR_RANDOM_SEED_NOT_SUPPORTED),
123    const_to_elem!(CKR_RANDOM_NO_RNG),
124    const_to_elem!(CKR_DOMAIN_PARAMS_INVALID),
125    const_to_elem!(CKR_CURVE_NOT_SUPPORTED),
126    const_to_elem!(CKR_BUFFER_TOO_SMALL),
127    const_to_elem!(CKR_SAVED_STATE_INVALID),
128    const_to_elem!(CKR_INFORMATION_SENSITIVE),
129    const_to_elem!(CKR_STATE_UNSAVEABLE),
130    const_to_elem!(CKR_CRYPTOKI_NOT_INITIALIZED),
131    const_to_elem!(CKR_CRYPTOKI_ALREADY_INITIALIZED),
132    const_to_elem!(CKR_MUTEX_BAD),
133    const_to_elem!(CKR_MUTEX_NOT_LOCKED),
134    const_to_elem!(CKR_NEW_PIN_MODE),
135    const_to_elem!(CKR_NEXT_OTP),
136    const_to_elem!(CKR_EXCEEDED_MAX_ITERATIONS),
137    const_to_elem!(CKR_FIPS_SELF_TEST_FAILED),
138    const_to_elem!(CKR_LIBRARY_LOAD_FAILED),
139    const_to_elem!(CKR_PIN_TOO_WEAK),
140    const_to_elem!(CKR_PUBLIC_KEY_INVALID),
141    const_to_elem!(CKR_FUNCTION_REJECTED),
142    const_to_elem!(CKR_TOKEN_RESOURCE_EXCEEDED),
143    const_to_elem!(CKR_OPERATION_CANCEL_FAILED),
144    const_to_elem!(CKR_KEY_EXHAUSTED),
145    const_to_elem!(CKR_PENDING),
146    const_to_elem!(CKR_SESSION_ASYNC_NOT_SUPPORTED),
147    const_to_elem!(CKR_SEED_RANDOM_REQUIRED),
148    const_to_elem!(CKR_OPERATION_NOT_VALIDATED),
149    const_to_elem!(CKR_TOKEN_NOT_INITIALIZED),
150    const_to_elem!(CKR_PARAMETER_SET_NOT_SUPPORTED),
151];
152
153/// Function to return an error string from a CK_RV error.
154///
155/// It never fails and returns "Unknown Error XX" where XX
156/// is an unknown error number (in hexadecimal notation).
157pub fn ckrv_to_string(ckrv: CK_ULONG) -> String {
158    for e in ERRORMAP {
159        if e.0 == ckrv {
160            return format!("{}({})", e.1, e.0);
161        }
162    }
163    return format!("Unknown Error {:x}", ckrv);
164}
165
166/// Static map with mech num -> mech name pairs
167static MECHMAP: [(CK_ULONG, &str); 470] = [
168    const_to_elem!(CKM_AES_KEY_GEN),
169    const_to_elem!(CKM_RSA_PKCS_KEY_PAIR_GEN),
170    const_to_elem!(CKM_RSA_PKCS),
171    const_to_elem!(CKM_RSA_9796),
172    const_to_elem!(CKM_RSA_X_509),
173    const_to_elem!(CKM_MD2_RSA_PKCS),
174    const_to_elem!(CKM_MD5_RSA_PKCS),
175    const_to_elem!(CKM_SHA1_RSA_PKCS),
176    const_to_elem!(CKM_RIPEMD128_RSA_PKCS),
177    const_to_elem!(CKM_RIPEMD160_RSA_PKCS),
178    const_to_elem!(CKM_RSA_PKCS_OAEP),
179    const_to_elem!(CKM_RSA_X9_31_KEY_PAIR_GEN),
180    const_to_elem!(CKM_RSA_X9_31),
181    const_to_elem!(CKM_SHA1_RSA_X9_31),
182    const_to_elem!(CKM_RSA_PKCS_PSS),
183    const_to_elem!(CKM_SHA1_RSA_PKCS_PSS),
184    const_to_elem!(CKM_ML_KEM_KEY_PAIR_GEN),
185    const_to_elem!(CKM_DSA_KEY_PAIR_GEN),
186    const_to_elem!(CKM_DSA),
187    const_to_elem!(CKM_DSA_SHA1),
188    const_to_elem!(CKM_DSA_SHA224),
189    const_to_elem!(CKM_DSA_SHA256),
190    const_to_elem!(CKM_DSA_SHA384),
191    const_to_elem!(CKM_DSA_SHA512),
192    const_to_elem!(CKM_ML_KEM),
193    const_to_elem!(CKM_DSA_SHA3_224),
194    const_to_elem!(CKM_DSA_SHA3_256),
195    const_to_elem!(CKM_DSA_SHA3_384),
196    const_to_elem!(CKM_DSA_SHA3_512),
197    const_to_elem!(CKM_ML_DSA_KEY_PAIR_GEN),
198    const_to_elem!(CKM_ML_DSA),
199    const_to_elem!(CKM_HASH_ML_DSA),
200    const_to_elem!(CKM_DH_PKCS_KEY_PAIR_GEN),
201    const_to_elem!(CKM_DH_PKCS_DERIVE),
202    const_to_elem!(CKM_HASH_ML_DSA_SHA224),
203    const_to_elem!(CKM_HASH_ML_DSA_SHA256),
204    const_to_elem!(CKM_HASH_ML_DSA_SHA384),
205    const_to_elem!(CKM_HASH_ML_DSA_SHA512),
206    const_to_elem!(CKM_HASH_ML_DSA_SHA3_224),
207    const_to_elem!(CKM_HASH_ML_DSA_SHA3_256),
208    const_to_elem!(CKM_HASH_ML_DSA_SHA3_384),
209    const_to_elem!(CKM_HASH_ML_DSA_SHA3_512),
210    const_to_elem!(CKM_HASH_ML_DSA_SHAKE128),
211    const_to_elem!(CKM_HASH_ML_DSA_SHAKE256),
212    const_to_elem!(CKM_SLH_DSA_KEY_PAIR_GEN),
213    const_to_elem!(CKM_SLH_DSA),
214    const_to_elem!(CKM_X9_42_DH_KEY_PAIR_GEN),
215    const_to_elem!(CKM_X9_42_DH_DERIVE),
216    const_to_elem!(CKM_X9_42_DH_HYBRID_DERIVE),
217    const_to_elem!(CKM_X9_42_MQV_DERIVE),
218    const_to_elem!(CKM_HASH_SLH_DSA),
219    const_to_elem!(CKM_HASH_SLH_DSA_SHA224),
220    const_to_elem!(CKM_HASH_SLH_DSA_SHA256),
221    const_to_elem!(CKM_HASH_SLH_DSA_SHA384),
222    const_to_elem!(CKM_HASH_SLH_DSA_SHA512),
223    const_to_elem!(CKM_HASH_SLH_DSA_SHA3_224),
224    const_to_elem!(CKM_HASH_SLH_DSA_SHA3_256),
225    const_to_elem!(CKM_HASH_SLH_DSA_SHA3_384),
226    const_to_elem!(CKM_HASH_SLH_DSA_SHA3_512),
227    const_to_elem!(CKM_HASH_SLH_DSA_SHAKE128),
228    const_to_elem!(CKM_HASH_SLH_DSA_SHAKE256),
229    const_to_elem!(CKM_SHA256_RSA_PKCS),
230    const_to_elem!(CKM_SHA384_RSA_PKCS),
231    const_to_elem!(CKM_SHA512_RSA_PKCS),
232    const_to_elem!(CKM_SHA256_RSA_PKCS_PSS),
233    const_to_elem!(CKM_SHA384_RSA_PKCS_PSS),
234    const_to_elem!(CKM_SHA512_RSA_PKCS_PSS),
235    const_to_elem!(CKM_SHA224_RSA_PKCS),
236    const_to_elem!(CKM_SHA224_RSA_PKCS_PSS),
237    const_to_elem!(CKM_SHA512_224),
238    const_to_elem!(CKM_SHA512_224_HMAC),
239    const_to_elem!(CKM_SHA512_224_HMAC_GENERAL),
240    const_to_elem!(CKM_SHA512_224_KEY_DERIVATION),
241    const_to_elem!(CKM_SHA512_256),
242    const_to_elem!(CKM_SHA512_256_HMAC),
243    const_to_elem!(CKM_SHA512_256_HMAC_GENERAL),
244    const_to_elem!(CKM_SHA512_256_KEY_DERIVATION),
245    const_to_elem!(CKM_SHA512_T),
246    const_to_elem!(CKM_SHA512_T_HMAC),
247    const_to_elem!(CKM_SHA512_T_HMAC_GENERAL),
248    const_to_elem!(CKM_SHA512_T_KEY_DERIVATION),
249    const_to_elem!(CKM_TLS12_EXTENDED_MASTER_KEY_DERIVE),
250    const_to_elem!(CKM_TLS12_EXTENDED_MASTER_KEY_DERIVE_DH),
251    const_to_elem!(CKM_SHA3_256_RSA_PKCS),
252    const_to_elem!(CKM_SHA3_384_RSA_PKCS),
253    const_to_elem!(CKM_SHA3_512_RSA_PKCS),
254    const_to_elem!(CKM_SHA3_256_RSA_PKCS_PSS),
255    const_to_elem!(CKM_SHA3_384_RSA_PKCS_PSS),
256    const_to_elem!(CKM_SHA3_512_RSA_PKCS_PSS),
257    const_to_elem!(CKM_SHA3_224_RSA_PKCS),
258    const_to_elem!(CKM_SHA3_224_RSA_PKCS_PSS),
259    const_to_elem!(CKM_RC2_KEY_GEN),
260    const_to_elem!(CKM_RC2_ECB),
261    const_to_elem!(CKM_RC2_CBC),
262    const_to_elem!(CKM_RC2_MAC),
263    const_to_elem!(CKM_RC2_MAC_GENERAL),
264    const_to_elem!(CKM_RC2_CBC_PAD),
265    const_to_elem!(CKM_RC4_KEY_GEN),
266    const_to_elem!(CKM_RC4),
267    const_to_elem!(CKM_DES_KEY_GEN),
268    const_to_elem!(CKM_DES_ECB),
269    const_to_elem!(CKM_DES_CBC),
270    const_to_elem!(CKM_DES_MAC),
271    const_to_elem!(CKM_DES_MAC_GENERAL),
272    const_to_elem!(CKM_DES_CBC_PAD),
273    const_to_elem!(CKM_DES2_KEY_GEN),
274    const_to_elem!(CKM_DES3_KEY_GEN),
275    const_to_elem!(CKM_DES3_ECB),
276    const_to_elem!(CKM_DES3_CBC),
277    const_to_elem!(CKM_DES3_MAC),
278    const_to_elem!(CKM_DES3_MAC_GENERAL),
279    const_to_elem!(CKM_DES3_CBC_PAD),
280    const_to_elem!(CKM_DES3_CMAC_GENERAL),
281    const_to_elem!(CKM_DES3_CMAC),
282    const_to_elem!(CKM_CDMF_KEY_GEN),
283    const_to_elem!(CKM_CDMF_ECB),
284    const_to_elem!(CKM_CDMF_CBC),
285    const_to_elem!(CKM_CDMF_MAC),
286    const_to_elem!(CKM_CDMF_MAC_GENERAL),
287    const_to_elem!(CKM_CDMF_CBC_PAD),
288    const_to_elem!(CKM_DES_OFB64),
289    const_to_elem!(CKM_DES_OFB8),
290    const_to_elem!(CKM_DES_CFB64),
291    const_to_elem!(CKM_DES_CFB8),
292    const_to_elem!(CKM_MD2),
293    const_to_elem!(CKM_MD2_HMAC),
294    const_to_elem!(CKM_MD2_HMAC_GENERAL),
295    const_to_elem!(CKM_MD5),
296    const_to_elem!(CKM_MD5_HMAC),
297    const_to_elem!(CKM_MD5_HMAC_GENERAL),
298    const_to_elem!(CKM_SHA_1),
299    const_to_elem!(CKM_SHA_1_HMAC),
300    const_to_elem!(CKM_SHA_1_HMAC_GENERAL),
301    const_to_elem!(CKM_RIPEMD128),
302    const_to_elem!(CKM_RIPEMD128_HMAC),
303    const_to_elem!(CKM_RIPEMD128_HMAC_GENERAL),
304    const_to_elem!(CKM_RIPEMD160),
305    const_to_elem!(CKM_RIPEMD160_HMAC),
306    const_to_elem!(CKM_RIPEMD160_HMAC_GENERAL),
307    const_to_elem!(CKM_SHA256),
308    const_to_elem!(CKM_SHA256_HMAC),
309    const_to_elem!(CKM_SHA256_HMAC_GENERAL),
310    const_to_elem!(CKM_SHA224),
311    const_to_elem!(CKM_SHA224_HMAC),
312    const_to_elem!(CKM_SHA224_HMAC_GENERAL),
313    const_to_elem!(CKM_SHA384),
314    const_to_elem!(CKM_SHA384_HMAC),
315    const_to_elem!(CKM_SHA384_HMAC_GENERAL),
316    const_to_elem!(CKM_SHA512),
317    const_to_elem!(CKM_SHA512_HMAC),
318    const_to_elem!(CKM_SHA512_HMAC_GENERAL),
319    const_to_elem!(CKM_SECURID_KEY_GEN),
320    const_to_elem!(CKM_SECURID),
321    const_to_elem!(CKM_HOTP_KEY_GEN),
322    const_to_elem!(CKM_HOTP),
323    const_to_elem!(CKM_ACTI),
324    const_to_elem!(CKM_ACTI_KEY_GEN),
325    const_to_elem!(CKM_SHA3_256),
326    const_to_elem!(CKM_SHA3_256_HMAC),
327    const_to_elem!(CKM_SHA3_256_HMAC_GENERAL),
328    const_to_elem!(CKM_SHA3_256_KEY_GEN),
329    const_to_elem!(CKM_SHA3_224),
330    const_to_elem!(CKM_SHA3_224_HMAC),
331    const_to_elem!(CKM_SHA3_224_HMAC_GENERAL),
332    const_to_elem!(CKM_SHA3_224_KEY_GEN),
333    const_to_elem!(CKM_SHA3_384),
334    const_to_elem!(CKM_SHA3_384_HMAC),
335    const_to_elem!(CKM_SHA3_384_HMAC_GENERAL),
336    const_to_elem!(CKM_SHA3_384_KEY_GEN),
337    const_to_elem!(CKM_SHA3_512),
338    const_to_elem!(CKM_SHA3_512_HMAC),
339    const_to_elem!(CKM_SHA3_512_HMAC_GENERAL),
340    const_to_elem!(CKM_SHA3_512_KEY_GEN),
341    const_to_elem!(CKM_CAST_KEY_GEN),
342    const_to_elem!(CKM_CAST_ECB),
343    const_to_elem!(CKM_CAST_CBC),
344    const_to_elem!(CKM_CAST_MAC),
345    const_to_elem!(CKM_CAST_MAC_GENERAL),
346    const_to_elem!(CKM_CAST_CBC_PAD),
347    const_to_elem!(CKM_CAST3_KEY_GEN),
348    const_to_elem!(CKM_CAST3_ECB),
349    const_to_elem!(CKM_CAST3_CBC),
350    const_to_elem!(CKM_CAST3_MAC),
351    const_to_elem!(CKM_CAST3_MAC_GENERAL),
352    const_to_elem!(CKM_CAST3_CBC_PAD),
353    const_to_elem!(CKM_CAST128_KEY_GEN),
354    const_to_elem!(CKM_CAST128_ECB),
355    const_to_elem!(CKM_CAST128_MAC),
356    const_to_elem!(CKM_CAST128_CBC),
357    const_to_elem!(CKM_CAST128_MAC_GENERAL),
358    const_to_elem!(CKM_CAST128_CBC_PAD),
359    const_to_elem!(CKM_RC5_KEY_GEN),
360    const_to_elem!(CKM_RC5_ECB),
361    const_to_elem!(CKM_RC5_CBC),
362    const_to_elem!(CKM_RC5_MAC),
363    const_to_elem!(CKM_RC5_MAC_GENERAL),
364    const_to_elem!(CKM_RC5_CBC_PAD),
365    const_to_elem!(CKM_IDEA_KEY_GEN),
366    const_to_elem!(CKM_IDEA_ECB),
367    const_to_elem!(CKM_IDEA_CBC),
368    const_to_elem!(CKM_IDEA_MAC),
369    const_to_elem!(CKM_IDEA_MAC_GENERAL),
370    const_to_elem!(CKM_IDEA_CBC_PAD),
371    const_to_elem!(CKM_GENERIC_SECRET_KEY_GEN),
372    const_to_elem!(CKM_CONCATENATE_BASE_AND_KEY),
373    const_to_elem!(CKM_CONCATENATE_BASE_AND_DATA),
374    const_to_elem!(CKM_CONCATENATE_DATA_AND_BASE),
375    const_to_elem!(CKM_XOR_BASE_AND_DATA),
376    const_to_elem!(CKM_EXTRACT_KEY_FROM_KEY),
377    const_to_elem!(CKM_SSL3_PRE_MASTER_KEY_GEN),
378    const_to_elem!(CKM_SSL3_MASTER_KEY_DERIVE),
379    const_to_elem!(CKM_SSL3_KEY_AND_MAC_DERIVE),
380    const_to_elem!(CKM_SSL3_MASTER_KEY_DERIVE_DH),
381    const_to_elem!(CKM_TLS_PRE_MASTER_KEY_GEN),
382    const_to_elem!(CKM_TLS_MASTER_KEY_DERIVE),
383    const_to_elem!(CKM_TLS_KEY_AND_MAC_DERIVE),
384    const_to_elem!(CKM_TLS_MASTER_KEY_DERIVE_DH),
385    const_to_elem!(CKM_TLS_PRF),
386    const_to_elem!(CKM_SSL3_MD5_MAC),
387    const_to_elem!(CKM_SSL3_SHA1_MAC),
388    const_to_elem!(CKM_MD5_KEY_DERIVATION),
389    const_to_elem!(CKM_MD2_KEY_DERIVATION),
390    const_to_elem!(CKM_SHA1_KEY_DERIVATION),
391    const_to_elem!(CKM_SHA256_KEY_DERIVATION),
392    const_to_elem!(CKM_SHA384_KEY_DERIVATION),
393    const_to_elem!(CKM_SHA512_KEY_DERIVATION),
394    const_to_elem!(CKM_SHA224_KEY_DERIVATION),
395    const_to_elem!(CKM_SHA3_256_KEY_DERIVATION),
396    const_to_elem!(CKM_SHA3_256_KEY_DERIVE),
397    const_to_elem!(CKM_SHA3_224_KEY_DERIVATION),
398    const_to_elem!(CKM_SHA3_224_KEY_DERIVE),
399    const_to_elem!(CKM_SHA3_384_KEY_DERIVATION),
400    const_to_elem!(CKM_SHA3_384_KEY_DERIVE),
401    const_to_elem!(CKM_SHA3_512_KEY_DERIVATION),
402    const_to_elem!(CKM_SHA3_512_KEY_DERIVE),
403    const_to_elem!(CKM_SHAKE_128_KEY_DERIVATION),
404    const_to_elem!(CKM_SHAKE_128_KEY_DERIVE),
405    const_to_elem!(CKM_SHAKE_256_KEY_DERIVATION),
406    const_to_elem!(CKM_SHAKE_256_KEY_DERIVE),
407    const_to_elem!(CKM_PBE_MD2_DES_CBC),
408    const_to_elem!(CKM_PBE_MD5_DES_CBC),
409    const_to_elem!(CKM_PBE_MD5_CAST_CBC),
410    const_to_elem!(CKM_PBE_MD5_CAST3_CBC),
411    const_to_elem!(CKM_PBE_MD5_CAST128_CBC),
412    const_to_elem!(CKM_PBE_SHA1_CAST128_CBC),
413    const_to_elem!(CKM_PBE_SHA1_RC4_128),
414    const_to_elem!(CKM_PBE_SHA1_RC4_40),
415    const_to_elem!(CKM_PBE_SHA1_DES3_EDE_CBC),
416    const_to_elem!(CKM_PBE_SHA1_DES2_EDE_CBC),
417    const_to_elem!(CKM_PBE_SHA1_RC2_128_CBC),
418    const_to_elem!(CKM_PBE_SHA1_RC2_40_CBC),
419    const_to_elem!(CKM_PKCS5_PBKD2),
420    const_to_elem!(CKM_PBA_SHA1_WITH_SHA1_HMAC),
421    const_to_elem!(CKM_WTLS_PRE_MASTER_KEY_GEN),
422    const_to_elem!(CKM_WTLS_MASTER_KEY_DERIVE),
423    const_to_elem!(CKM_WTLS_MASTER_KEY_DERIVE_DH_ECC),
424    const_to_elem!(CKM_WTLS_PRF),
425    const_to_elem!(CKM_WTLS_SERVER_KEY_AND_MAC_DERIVE),
426    const_to_elem!(CKM_WTLS_CLIENT_KEY_AND_MAC_DERIVE),
427    const_to_elem!(CKM_TLS10_MAC_SERVER),
428    const_to_elem!(CKM_TLS10_MAC_CLIENT),
429    const_to_elem!(CKM_TLS12_MAC),
430    const_to_elem!(CKM_TLS12_KDF),
431    const_to_elem!(CKM_TLS12_MASTER_KEY_DERIVE),
432    const_to_elem!(CKM_TLS12_KEY_AND_MAC_DERIVE),
433    const_to_elem!(CKM_TLS12_MASTER_KEY_DERIVE_DH),
434    const_to_elem!(CKM_TLS12_KEY_SAFE_DERIVE),
435    const_to_elem!(CKM_TLS_MAC),
436    const_to_elem!(CKM_TLS_KDF),
437    const_to_elem!(CKM_KEY_WRAP_LYNKS),
438    const_to_elem!(CKM_KEY_WRAP_SET_OAEP),
439    const_to_elem!(CKM_CMS_SIG),
440    const_to_elem!(CKM_KIP_DERIVE),
441    const_to_elem!(CKM_KIP_WRAP),
442    const_to_elem!(CKM_KIP_MAC),
443    const_to_elem!(CKM_CAMELLIA_KEY_GEN),
444    const_to_elem!(CKM_CAMELLIA_ECB),
445    const_to_elem!(CKM_CAMELLIA_CBC),
446    const_to_elem!(CKM_CAMELLIA_MAC),
447    const_to_elem!(CKM_CAMELLIA_MAC_GENERAL),
448    const_to_elem!(CKM_CAMELLIA_CBC_PAD),
449    const_to_elem!(CKM_CAMELLIA_ECB_ENCRYPT_DATA),
450    const_to_elem!(CKM_CAMELLIA_CBC_ENCRYPT_DATA),
451    const_to_elem!(CKM_CAMELLIA_CTR),
452    const_to_elem!(CKM_ARIA_KEY_GEN),
453    const_to_elem!(CKM_ARIA_ECB),
454    const_to_elem!(CKM_ARIA_CBC),
455    const_to_elem!(CKM_ARIA_MAC),
456    const_to_elem!(CKM_ARIA_MAC_GENERAL),
457    const_to_elem!(CKM_ARIA_CBC_PAD),
458    const_to_elem!(CKM_ARIA_ECB_ENCRYPT_DATA),
459    const_to_elem!(CKM_ARIA_CBC_ENCRYPT_DATA),
460    const_to_elem!(CKM_SEED_KEY_GEN),
461    const_to_elem!(CKM_SEED_ECB),
462    const_to_elem!(CKM_SEED_CBC),
463    const_to_elem!(CKM_SEED_MAC),
464    const_to_elem!(CKM_SEED_MAC_GENERAL),
465    const_to_elem!(CKM_SEED_CBC_PAD),
466    const_to_elem!(CKM_SEED_ECB_ENCRYPT_DATA),
467    const_to_elem!(CKM_SEED_CBC_ENCRYPT_DATA),
468    const_to_elem!(CKM_SKIPJACK_KEY_GEN),
469    const_to_elem!(CKM_SKIPJACK_ECB64),
470    const_to_elem!(CKM_SKIPJACK_CBC64),
471    const_to_elem!(CKM_SKIPJACK_OFB64),
472    const_to_elem!(CKM_SKIPJACK_CFB64),
473    const_to_elem!(CKM_SKIPJACK_CFB32),
474    const_to_elem!(CKM_SKIPJACK_CFB16),
475    const_to_elem!(CKM_SKIPJACK_CFB8),
476    const_to_elem!(CKM_SKIPJACK_WRAP),
477    const_to_elem!(CKM_SKIPJACK_PRIVATE_WRAP),
478    const_to_elem!(CKM_SKIPJACK_RELAYX),
479    const_to_elem!(CKM_KEA_KEY_PAIR_GEN),
480    const_to_elem!(CKM_KEA_KEY_DERIVE),
481    const_to_elem!(CKM_KEA_DERIVE),
482    const_to_elem!(CKM_FORTEZZA_TIMESTAMP),
483    const_to_elem!(CKM_BATON_KEY_GEN),
484    const_to_elem!(CKM_BATON_ECB128),
485    const_to_elem!(CKM_BATON_ECB96),
486    const_to_elem!(CKM_BATON_CBC128),
487    const_to_elem!(CKM_BATON_COUNTER),
488    const_to_elem!(CKM_BATON_SHUFFLE),
489    const_to_elem!(CKM_BATON_WRAP),
490    const_to_elem!(CKM_EC_KEY_PAIR_GEN),
491    const_to_elem!(CKM_ECDSA),
492    const_to_elem!(CKM_ECDSA_SHA1),
493    const_to_elem!(CKM_ECDSA_SHA224),
494    const_to_elem!(CKM_ECDSA_SHA256),
495    const_to_elem!(CKM_ECDSA_SHA384),
496    const_to_elem!(CKM_ECDSA_SHA512),
497    const_to_elem!(CKM_EC_KEY_PAIR_GEN_W_EXTRA_BITS),
498    const_to_elem!(CKM_ECDH1_DERIVE),
499    const_to_elem!(CKM_ECDH1_COFACTOR_DERIVE),
500    const_to_elem!(CKM_ECMQV_DERIVE),
501    const_to_elem!(CKM_ECDH_AES_KEY_WRAP),
502    const_to_elem!(CKM_RSA_AES_KEY_WRAP),
503    const_to_elem!(CKM_JUNIPER_KEY_GEN),
504    const_to_elem!(CKM_JUNIPER_ECB128),
505    const_to_elem!(CKM_JUNIPER_CBC128),
506    const_to_elem!(CKM_JUNIPER_COUNTER),
507    const_to_elem!(CKM_JUNIPER_SHUFFLE),
508    const_to_elem!(CKM_JUNIPER_WRAP),
509    const_to_elem!(CKM_FASTHASH),
510    const_to_elem!(CKM_AES_XTS),
511    const_to_elem!(CKM_AES_XTS_KEY_GEN),
512    const_to_elem!(CKM_AES_KEY_GEN),
513    const_to_elem!(CKM_AES_ECB),
514    const_to_elem!(CKM_AES_CBC),
515    const_to_elem!(CKM_AES_MAC),
516    const_to_elem!(CKM_AES_MAC_GENERAL),
517    const_to_elem!(CKM_AES_CBC_PAD),
518    const_to_elem!(CKM_AES_CTR),
519    const_to_elem!(CKM_AES_GCM),
520    const_to_elem!(CKM_AES_CCM),
521    const_to_elem!(CKM_AES_CTS),
522    const_to_elem!(CKM_AES_CMAC),
523    const_to_elem!(CKM_AES_CMAC_GENERAL),
524    const_to_elem!(CKM_AES_XCBC_MAC),
525    const_to_elem!(CKM_AES_XCBC_MAC_96),
526    const_to_elem!(CKM_AES_GMAC),
527    const_to_elem!(CKM_BLOWFISH_KEY_GEN),
528    const_to_elem!(CKM_BLOWFISH_CBC),
529    const_to_elem!(CKM_TWOFISH_KEY_GEN),
530    const_to_elem!(CKM_TWOFISH_CBC),
531    const_to_elem!(CKM_BLOWFISH_CBC_PAD),
532    const_to_elem!(CKM_TWOFISH_CBC_PAD),
533    const_to_elem!(CKM_DES_ECB_ENCRYPT_DATA),
534    const_to_elem!(CKM_DES_CBC_ENCRYPT_DATA),
535    const_to_elem!(CKM_DES3_ECB_ENCRYPT_DATA),
536    const_to_elem!(CKM_DES3_CBC_ENCRYPT_DATA),
537    const_to_elem!(CKM_AES_ECB_ENCRYPT_DATA),
538    const_to_elem!(CKM_AES_CBC_ENCRYPT_DATA),
539    const_to_elem!(CKM_GOSTR3410_KEY_PAIR_GEN),
540    const_to_elem!(CKM_GOSTR3410),
541    const_to_elem!(CKM_GOSTR3410_WITH_GOSTR3411),
542    const_to_elem!(CKM_GOSTR3410_KEY_WRAP),
543    const_to_elem!(CKM_GOSTR3410_DERIVE),
544    const_to_elem!(CKM_GOSTR3411),
545    const_to_elem!(CKM_GOSTR3411_HMAC),
546    const_to_elem!(CKM_GOST28147_KEY_GEN),
547    const_to_elem!(CKM_GOST28147_ECB),
548    const_to_elem!(CKM_GOST28147),
549    const_to_elem!(CKM_GOST28147_MAC),
550    const_to_elem!(CKM_GOST28147_KEY_WRAP),
551    const_to_elem!(CKM_CHACHA20_KEY_GEN),
552    const_to_elem!(CKM_CHACHA20),
553    const_to_elem!(CKM_POLY1305_KEY_GEN),
554    const_to_elem!(CKM_POLY1305),
555    const_to_elem!(CKM_DSA_PARAMETER_GEN),
556    const_to_elem!(CKM_DH_PKCS_PARAMETER_GEN),
557    const_to_elem!(CKM_X9_42_DH_PARAMETER_GEN),
558    const_to_elem!(CKM_DSA_PROBABILISTIC_PARAMETER_GEN),
559    const_to_elem!(CKM_DSA_SHAWE_TAYLOR_PARAMETER_GEN),
560    const_to_elem!(CKM_DSA_FIPS_G_GEN),
561    const_to_elem!(CKM_AES_OFB),
562    const_to_elem!(CKM_AES_CFB64),
563    const_to_elem!(CKM_AES_CFB8),
564    const_to_elem!(CKM_AES_CFB128),
565    const_to_elem!(CKM_AES_CFB1),
566    const_to_elem!(CKM_AES_KEY_WRAP),
567    const_to_elem!(CKM_AES_KEY_WRAP_PAD),
568    const_to_elem!(CKM_AES_KEY_WRAP_KWP),
569    const_to_elem!(CKM_AES_KEY_WRAP_PKCS7),
570    const_to_elem!(CKM_RSA_PKCS_TPM_1_1),
571    const_to_elem!(CKM_RSA_PKCS_OAEP_TPM_1_1),
572    const_to_elem!(CKM_SHA_1_KEY_GEN),
573    const_to_elem!(CKM_SHA224_KEY_GEN),
574    const_to_elem!(CKM_SHA256_KEY_GEN),
575    const_to_elem!(CKM_SHA384_KEY_GEN),
576    const_to_elem!(CKM_SHA512_KEY_GEN),
577    const_to_elem!(CKM_SHA512_224_KEY_GEN),
578    const_to_elem!(CKM_SHA512_256_KEY_GEN),
579    const_to_elem!(CKM_SHA512_T_KEY_GEN),
580    const_to_elem!(CKM_NULL),
581    const_to_elem!(CKM_BLAKE2B_160),
582    const_to_elem!(CKM_BLAKE2B_160_HMAC),
583    const_to_elem!(CKM_BLAKE2B_160_HMAC_GENERAL),
584    const_to_elem!(CKM_BLAKE2B_160_KEY_DERIVE),
585    const_to_elem!(CKM_BLAKE2B_160_KEY_GEN),
586    const_to_elem!(CKM_BLAKE2B_256),
587    const_to_elem!(CKM_BLAKE2B_256_HMAC),
588    const_to_elem!(CKM_BLAKE2B_256_HMAC_GENERAL),
589    const_to_elem!(CKM_BLAKE2B_256_KEY_DERIVE),
590    const_to_elem!(CKM_BLAKE2B_256_KEY_GEN),
591    const_to_elem!(CKM_BLAKE2B_384),
592    const_to_elem!(CKM_BLAKE2B_384_HMAC),
593    const_to_elem!(CKM_BLAKE2B_384_HMAC_GENERAL),
594    const_to_elem!(CKM_BLAKE2B_384_KEY_DERIVE),
595    const_to_elem!(CKM_BLAKE2B_384_KEY_GEN),
596    const_to_elem!(CKM_BLAKE2B_512),
597    const_to_elem!(CKM_BLAKE2B_512_HMAC),
598    const_to_elem!(CKM_BLAKE2B_512_HMAC_GENERAL),
599    const_to_elem!(CKM_BLAKE2B_512_KEY_DERIVE),
600    const_to_elem!(CKM_BLAKE2B_512_KEY_GEN),
601    const_to_elem!(CKM_SALSA20),
602    const_to_elem!(CKM_CHACHA20_POLY1305),
603    const_to_elem!(CKM_SALSA20_POLY1305),
604    const_to_elem!(CKM_X3DH_INITIALIZE),
605    const_to_elem!(CKM_X3DH_RESPOND),
606    const_to_elem!(CKM_X2RATCHET_INITIALIZE),
607    const_to_elem!(CKM_X2RATCHET_RESPOND),
608    const_to_elem!(CKM_X2RATCHET_ENCRYPT),
609    const_to_elem!(CKM_X2RATCHET_DECRYPT),
610    const_to_elem!(CKM_XEDDSA),
611    const_to_elem!(CKM_HKDF_DERIVE),
612    const_to_elem!(CKM_HKDF_DATA),
613    const_to_elem!(CKM_HKDF_KEY_GEN),
614    const_to_elem!(CKM_SALSA20_KEY_GEN),
615    const_to_elem!(CKM_ECDSA_SHA3_224),
616    const_to_elem!(CKM_ECDSA_SHA3_256),
617    const_to_elem!(CKM_ECDSA_SHA3_384),
618    const_to_elem!(CKM_ECDSA_SHA3_512),
619    const_to_elem!(CKM_EC_EDWARDS_KEY_PAIR_GEN),
620    const_to_elem!(CKM_EC_MONTGOMERY_KEY_PAIR_GEN),
621    const_to_elem!(CKM_EDDSA),
622    const_to_elem!(CKM_SP800_108_COUNTER_KDF),
623    const_to_elem!(CKM_SP800_108_FEEDBACK_KDF),
624    const_to_elem!(CKM_SP800_108_DOUBLE_PIPELINE_KDF),
625    const_to_elem!(CKM_IKE2_PRF_PLUS_DERIVE),
626    const_to_elem!(CKM_IKE_PRF_DERIVE),
627    const_to_elem!(CKM_IKE1_PRF_DERIVE),
628    const_to_elem!(CKM_IKE1_EXTENDED_DERIVE),
629    const_to_elem!(CKM_HSS_KEY_PAIR_GEN),
630    const_to_elem!(CKM_HSS),
631    const_to_elem!(CKM_XMSS_KEY_PAIR_GEN),
632    const_to_elem!(CKM_XMSSMT_KEY_PAIR_GEN),
633    const_to_elem!(CKM_XMSS),
634    const_to_elem!(CKM_XMSSMT),
635    const_to_elem!(CKM_ECDH_X_AES_KEY_WRAP),
636    const_to_elem!(CKM_ECDH_COF_AES_KEY_WRAP),
637    const_to_elem!(CKM_PUB_KEY_FROM_PRIV_KEY),
638];
639
640/// Static map with attribute num -> attribute name pairs
641static ATTRMAP: &[(CK_ULONG, &str)] = &[
642    const_to_elem!(CKA_CLASS),
643    const_to_elem!(CKA_TOKEN),
644    const_to_elem!(CKA_PRIVATE),
645    const_to_elem!(CKA_LABEL),
646    const_to_elem!(CKA_UNIQUE_ID),
647    const_to_elem!(CKA_APPLICATION),
648    const_to_elem!(CKA_VALUE),
649    const_to_elem!(CKA_OBJECT_ID),
650    const_to_elem!(CKA_CERTIFICATE_TYPE),
651    const_to_elem!(CKA_ISSUER),
652    const_to_elem!(CKA_SERIAL_NUMBER),
653    const_to_elem!(CKA_AC_ISSUER),
654    const_to_elem!(CKA_OWNER),
655    const_to_elem!(CKA_ATTR_TYPES),
656    const_to_elem!(CKA_TRUSTED),
657    const_to_elem!(CKA_CERTIFICATE_CATEGORY),
658    const_to_elem!(CKA_JAVA_MIDP_SECURITY_DOMAIN),
659    const_to_elem!(CKA_URL),
660    const_to_elem!(CKA_HASH_OF_SUBJECT_PUBLIC_KEY),
661    const_to_elem!(CKA_HASH_OF_ISSUER_PUBLIC_KEY),
662    const_to_elem!(CKA_NAME_HASH_ALGORITHM),
663    const_to_elem!(CKA_CHECK_VALUE),
664    const_to_elem!(CKA_KEY_TYPE),
665    const_to_elem!(CKA_SUBJECT),
666    const_to_elem!(CKA_ID),
667    const_to_elem!(CKA_SENSITIVE),
668    const_to_elem!(CKA_ENCRYPT),
669    const_to_elem!(CKA_DECRYPT),
670    const_to_elem!(CKA_WRAP),
671    const_to_elem!(CKA_UNWRAP),
672    const_to_elem!(CKA_SIGN),
673    const_to_elem!(CKA_SIGN_RECOVER),
674    const_to_elem!(CKA_VERIFY),
675    const_to_elem!(CKA_VERIFY_RECOVER),
676    const_to_elem!(CKA_DERIVE),
677    const_to_elem!(CKA_START_DATE),
678    const_to_elem!(CKA_END_DATE),
679    const_to_elem!(CKA_MODULUS),
680    const_to_elem!(CKA_MODULUS_BITS),
681    const_to_elem!(CKA_PUBLIC_EXPONENT),
682    const_to_elem!(CKA_PRIVATE_EXPONENT),
683    const_to_elem!(CKA_PRIME_1),
684    const_to_elem!(CKA_PRIME_2),
685    const_to_elem!(CKA_EXPONENT_1),
686    const_to_elem!(CKA_EXPONENT_2),
687    const_to_elem!(CKA_COEFFICIENT),
688    const_to_elem!(CKA_PUBLIC_KEY_INFO),
689    const_to_elem!(CKA_PRIME),
690    const_to_elem!(CKA_SUBPRIME),
691    const_to_elem!(CKA_BASE),
692    const_to_elem!(CKA_PRIME_BITS),
693    const_to_elem!(CKA_SUBPRIME_BITS),
694    const_to_elem!(CKA_SUB_PRIME_BITS),
695    const_to_elem!(CKA_VALUE_BITS),
696    const_to_elem!(CKA_VALUE_LEN),
697    const_to_elem!(CKA_EXTRACTABLE),
698    const_to_elem!(CKA_LOCAL),
699    const_to_elem!(CKA_NEVER_EXTRACTABLE),
700    const_to_elem!(CKA_ALWAYS_SENSITIVE),
701    const_to_elem!(CKA_KEY_GEN_MECHANISM),
702    const_to_elem!(CKA_MODIFIABLE),
703    const_to_elem!(CKA_COPYABLE),
704    const_to_elem!(CKA_DESTROYABLE),
705    const_to_elem!(CKA_EC_PARAMS),
706    const_to_elem!(CKA_EC_POINT),
707    const_to_elem!(CKA_ALWAYS_AUTHENTICATE),
708    const_to_elem!(CKA_WRAP_WITH_TRUSTED),
709    const_to_elem!(CKA_OTP_FORMAT),
710    const_to_elem!(CKA_OTP_LENGTH),
711    const_to_elem!(CKA_OTP_TIME_INTERVAL),
712    const_to_elem!(CKA_OTP_USER_FRIENDLY_MODE),
713    const_to_elem!(CKA_OTP_CHALLENGE_REQUIREMENT),
714    const_to_elem!(CKA_OTP_TIME_REQUIREMENT),
715    const_to_elem!(CKA_OTP_COUNTER_REQUIREMENT),
716    const_to_elem!(CKA_OTP_PIN_REQUIREMENT),
717    const_to_elem!(CKA_OTP_COUNTER),
718    const_to_elem!(CKA_OTP_TIME),
719    const_to_elem!(CKA_OTP_USER_IDENTIFIER),
720    const_to_elem!(CKA_OTP_SERVICE_IDENTIFIER),
721    const_to_elem!(CKA_OTP_SERVICE_LOGO),
722    const_to_elem!(CKA_OTP_SERVICE_LOGO_TYPE),
723    const_to_elem!(CKA_GOSTR3410_PARAMS),
724    const_to_elem!(CKA_GOSTR3411_PARAMS),
725    const_to_elem!(CKA_GOST28147_PARAMS),
726    const_to_elem!(CKA_HW_FEATURE_TYPE),
727    const_to_elem!(CKA_RESET_ON_INIT),
728    const_to_elem!(CKA_HAS_RESET),
729    const_to_elem!(CKA_PIXEL_X),
730    const_to_elem!(CKA_PIXEL_Y),
731    const_to_elem!(CKA_RESOLUTION),
732    const_to_elem!(CKA_CHAR_ROWS),
733    const_to_elem!(CKA_CHAR_COLUMNS),
734    const_to_elem!(CKA_COLOR),
735    const_to_elem!(CKA_BITS_PER_PIXEL),
736    const_to_elem!(CKA_CHAR_SETS),
737    const_to_elem!(CKA_ENCODING_METHODS),
738    const_to_elem!(CKA_MIME_TYPES),
739    const_to_elem!(CKA_MECHANISM_TYPE),
740    const_to_elem!(CKA_REQUIRED_CMS_ATTRIBUTES),
741    const_to_elem!(CKA_DEFAULT_CMS_ATTRIBUTES),
742    const_to_elem!(CKA_SUPPORTED_CMS_ATTRIBUTES),
743    const_to_elem!(CKA_PROFILE_ID),
744    const_to_elem!(CKA_X2RATCHET_BAG),
745    const_to_elem!(CKA_X2RATCHET_BAGSIZE),
746    const_to_elem!(CKA_X2RATCHET_BOBS1STMSG),
747    const_to_elem!(CKA_X2RATCHET_CKR),
748    const_to_elem!(CKA_X2RATCHET_CKS),
749    const_to_elem!(CKA_X2RATCHET_DHP),
750    const_to_elem!(CKA_X2RATCHET_DHR),
751    const_to_elem!(CKA_X2RATCHET_DHS),
752    const_to_elem!(CKA_X2RATCHET_HKR),
753    const_to_elem!(CKA_X2RATCHET_HKS),
754    const_to_elem!(CKA_X2RATCHET_ISALICE),
755    const_to_elem!(CKA_X2RATCHET_NHKR),
756    const_to_elem!(CKA_X2RATCHET_NHKS),
757    const_to_elem!(CKA_X2RATCHET_NR),
758    const_to_elem!(CKA_X2RATCHET_NS),
759    const_to_elem!(CKA_X2RATCHET_PNS),
760    const_to_elem!(CKA_X2RATCHET_RK),
761    const_to_elem!(CKA_HSS_LEVELS),
762    const_to_elem!(CKA_HSS_LMS_TYPE),
763    const_to_elem!(CKA_HSS_LMOTS_TYPE),
764    const_to_elem!(CKA_HSS_LMS_TYPES),
765    const_to_elem!(CKA_HSS_LMOTS_TYPES),
766    const_to_elem!(CKA_HSS_KEYS_REMAINING),
767    const_to_elem!(CKA_PARAMETER_SET),
768    const_to_elem!(CKA_OBJECT_VALIDATION_FLAGS),
769    const_to_elem!(CKA_VALIDATION_TYPE),
770    const_to_elem!(CKA_VALIDATION_VERSION),
771    const_to_elem!(CKA_VALIDATION_LEVEL),
772    const_to_elem!(CKA_VALIDATION_MODULE_ID),
773    const_to_elem!(CKA_VALIDATION_FLAG),
774    const_to_elem!(CKA_VALIDATION_AUTHORITY_TYPE),
775    const_to_elem!(CKA_VALIDATION_COUNTRY),
776    const_to_elem!(CKA_VALIDATION_CERTIFICATE_IDENTIFIER),
777    const_to_elem!(CKA_VALIDATION_CERTIFICATE_URI),
778    const_to_elem!(CKA_VALIDATION_VENDOR_URI),
779    const_to_elem!(CKA_VALIDATION_PROFILE),
780    const_to_elem!(CKA_ENCAPSULATE_TEMPLATE),
781    const_to_elem!(CKA_DECAPSULATE_TEMPLATE),
782    const_to_elem!(CKA_TRUST_SERVER_AUTH),
783    const_to_elem!(CKA_TRUST_CLIENT_AUTH),
784    const_to_elem!(CKA_TRUST_CODE_SIGNING),
785    const_to_elem!(CKA_TRUST_EMAIL_PROTECTION),
786    const_to_elem!(CKA_TRUST_IPSEC_IKE),
787    const_to_elem!(CKA_TRUST_TIME_STAMPING),
788    const_to_elem!(CKA_TRUST_OCSP_SIGNING),
789    const_to_elem!(CKA_ENCAPSULATE),
790    const_to_elem!(CKA_DECAPSULATE),
791    const_to_elem!(CKA_HASH_OF_CERTIFICATE),
792    const_to_elem!(CKA_PUBLIC_CRC64_VALUE),
793    const_to_elem!(CKA_SEED),
794    const_to_elem!(CKA_WRAP_TEMPLATE),
795    const_to_elem!(CKA_UNWRAP_TEMPLATE),
796    const_to_elem!(CKA_DERIVE_TEMPLATE),
797    const_to_elem!(CKA_ALLOWED_MECHANISMS),
798    const_to_elem!(CKA_VENDOR_DEFINED),
799];
800
801/// Static map with object class num -> object class name pairs
802static OBJMAP: &[(CK_ULONG, &str)] = &[
803    const_to_elem!(CKO_DATA),
804    const_to_elem!(CKO_CERTIFICATE),
805    const_to_elem!(CKO_PUBLIC_KEY),
806    const_to_elem!(CKO_PRIVATE_KEY),
807    const_to_elem!(CKO_SECRET_KEY),
808    const_to_elem!(CKO_HW_FEATURE),
809    const_to_elem!(CKO_DOMAIN_PARAMETERS),
810    const_to_elem!(CKO_MECHANISM),
811    const_to_elem!(CKO_OTP_KEY),
812    const_to_elem!(CKO_PROFILE),
813    const_to_elem!(CKO_VALIDATION),
814    const_to_elem!(CKO_TRUST),
815    const_to_elem!(CKO_VENDOR_DEFINED),
816];
817
818pub fn name_to_mech(name: &str) -> Result<CK_MECHANISM_TYPE> {
819    for e in MECHMAP {
820        if e.1 == name {
821            return Ok(e.0);
822        }
823    }
824    Err(CKR_ARGUMENTS_BAD)?
825}
826
827pub fn name_to_attr(name: &str) -> Result<CK_ATTRIBUTE_TYPE> {
828    for e in ATTRMAP {
829        if e.1 == name {
830            return Ok(e.0);
831        }
832    }
833    Err(CKR_ARGUMENTS_BAD)?
834}
835
836pub fn name_to_obj(name: &str) -> Result<CK_OBJECT_CLASS> {
837    for e in OBJMAP {
838        if e.1 == name {
839            return Ok(e.0);
840        }
841    }
842    Err(CKR_ARGUMENTS_BAD)?
843}
844
845/// Convenience helper to copy a pointer+length obtained via FFI into a
846/// valid Vector of bytes.
847macro_rules! bytes_to_vec {
848    ($ptr:expr, $len:expr) => {{
849        let ptr = $ptr as *const u8;
850        let size = usize::try_from($len).unwrap();
851        if ptr == std::ptr::null_mut() || size == 0 {
852            Vec::new()
853        } else {
854            let mut v = Vec::<u8>::with_capacity(size);
855            unsafe {
856                std::ptr::copy_nonoverlapping(ptr, v.as_mut_ptr(), size);
857                v.set_len(size);
858            }
859            v
860        }
861    }};
862}
863
864/// Date digits separator
865const ASCII_DASH: u8 = b'-';
866/// Smallest ASCII value for a date digit
867const MIN_ASCII_DIGIT: u8 = b'0';
868/// Largest ASCII value for a date digit
869const MAX_ASCII_DIGIT: u8 = b'9';
870
871/// Converts a vector of bytes into a CK_DATE structure with some validation
872///
873/// The data is checked to ensure only ASCII values of numbers are present,
874/// but there is no validation that the resulting date is in any way valid.
875pub fn vec_to_date_validate(val: Vec<u8>) -> Result<CK_DATE> {
876    if val.len() != 8 {
877        return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
878    }
879    for n in val.iter() {
880        if *n < MIN_ASCII_DIGIT || *n > MAX_ASCII_DIGIT {
881            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
882        }
883    }
884    Ok(CK_DATE {
885        year: [val[0], val[1], val[2], val[3]],
886        month: [val[4], val[5]],
887        day: [val[6], val[7]],
888    })
889}
890
891/// Parses a string as a date
892///
893/// Returns a CK_DATE on success
894pub fn string_to_ck_date(date: &str) -> Result<CK_DATE> {
895    let s = date.as_bytes().to_vec();
896    if s.len() != 10 {
897        return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
898    }
899    if s[4] != ASCII_DASH || s[7] != ASCII_DASH {
900        return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
901    }
902    let mut buf = vec![0; 8];
903    buf[0] = s[0];
904    buf[1] = s[1];
905    buf[2] = s[2];
906    buf[3] = s[3];
907    buf[4] = s[5];
908    buf[5] = s[6];
909    buf[6] = s[8];
910    buf[7] = s[9];
911    vec_to_date_validate(buf)
912}
913
914impl CK_ATTRIBUTE {
915    /// Returns the internal data memory buffer as a CK_ULONG
916    ///
917    /// Errors out if the data size does not match the size of a CK_ULONG
918    pub fn to_ulong(&self) -> Result<CK_ULONG> {
919        if self.ulValueLen != std::mem::size_of::<CK_ULONG>() as CK_ULONG {
920            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
921        }
922        Ok(unsafe { *(self.pValue as CK_ULONG_PTR) })
923    }
924
925    /// Returns the internal data memory buffer as a bool
926    ///
927    /// Errors out if the data size does not match the size of a CK_BBOOL
928    pub fn to_bool(self) -> Result<bool> {
929        if self.ulValueLen != std::mem::size_of::<CK_BBOOL>() as CK_ULONG {
930            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
931        }
932        let val: CK_BBOOL = unsafe { *(self.pValue as CK_BBOOL_PTR) };
933        if val == 0 {
934            Ok(false)
935        } else {
936            Ok(true)
937        }
938    }
939
940    /// Returns the internal data memory buffer as a String
941    ///
942    /// Errors out if the data size does not match or the buffer is
943    /// not parseable as a UTF8 string.
944    pub fn to_string(&self) -> Result<String> {
945        if self.ulValueLen == 0 {
946            return Ok(String::new());
947        }
948        if self.pValue.is_null() {
949            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
950        }
951        let buf: &[u8] = bytes_to_slice(
952            self.pValue as *const u8,
953            usize::try_from(self.ulValueLen)?,
954        );
955        match std::str::from_utf8(buf) {
956            Ok(s) => Ok(s.to_string()),
957            Err(_) => Err(CKR_ATTRIBUTE_VALUE_INVALID)?,
958        }
959    }
960
961    /// Returns the internal data memory buffer as a slice
962    ///
963    /// Errors out if the internal data pointer is null
964    pub fn to_slice(&self) -> Result<&[u8]> {
965        if self.ulValueLen == 0 {
966            return Ok(&[]);
967        }
968        if self.pValue.is_null() {
969            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
970        }
971        Ok(bytes_to_slice(
972            self.pValue as *const u8,
973            usize::try_from(self.ulValueLen)?,
974        ))
975    }
976
977    /// Returns a copy of the internal buffer as an vector
978    ///
979    /// Returns an empty vector if the internal buffer pointer is null
980    pub fn to_buf(&self) -> Result<Vec<u8>> {
981        Ok(bytes_to_vec!(self.pValue, self.ulValueLen))
982    }
983
984    /// Returns the internal buffer as a CK_DATE
985    ///
986    /// Errors out if parsing the buffer as a date fails
987    pub fn to_date(&self) -> Result<CK_DATE> {
988        if self.ulValueLen == 0 {
989            /* set 0000-00-00 */
990            return Ok(CK_DATE {
991                year: [b'0', b'0', b'0', b'0'],
992                month: [b'0', b'0'],
993                day: [b'0', b'0'],
994            });
995        }
996        if self.pValue.is_null() {
997            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
998        }
999        if self.ulValueLen != 8 {
1000            return Err(CKR_ATTRIBUTE_VALUE_INVALID)?;
1001        }
1002        vec_to_date_validate(bytes_to_vec!(self.pValue, self.ulValueLen))
1003    }
1004}
1005
1006impl CK_MECHANISM {
1007    /// Convenience function to cast mechanism parameters passed as a
1008    /// pointer/length into the structure they represent.
1009    ///
1010    /// A length check on `ulParameterLen` is performed to validate that
1011    /// the correct parameter structure is being casted.
1012    ///
1013    /// No other validation is performed, this is an UNSAFE operation
1014    /// that requires further content validation.
1015    pub fn get_parameters<T: Copy + Clone>(&self) -> Result<T> {
1016        let Ok(len) = usize::try_from(self.ulParameterLen) else {
1017            return Err(CKR_ARGUMENTS_BAD)?;
1018        };
1019        if len != std::mem::size_of::<T>() {
1020            return Err(CKR_ARGUMENTS_BAD)?;
1021        }
1022        Ok(unsafe { std::ptr::read_unaligned(self.pParameter as *const T) })
1023    }
1024
1025    pub fn from_ptr(mechptr: CK_MECHANISM_PTR) -> CK_MECHANISM {
1026        unsafe { std::ptr::read_unaligned(mechptr as *const CK_MECHANISM) }
1027    }
1028}
1029
1030impl CK_OTP_PARAM {
1031    /// Returns the internal data memory buffer as a CK_ULONG
1032    ///
1033    /// Errors out if the data size does not match the size of a CK_ULONG
1034    pub fn to_ulong(&self) -> Result<CK_ULONG> {
1035        if self.ulValueLen != std::mem::size_of::<CK_ULONG>() as CK_ULONG {
1036            return Err(CKR_MECHANISM_PARAM_INVALID)?;
1037        }
1038        Ok(unsafe { *(self.pValue as CK_ULONG_PTR) })
1039    }
1040
1041    /// Returns the internal data memory buffer as a bool
1042    ///
1043    /// Errors out if the data size does not match the size of a CK_BBOOL
1044    pub fn to_bool(self) -> Result<bool> {
1045        if self.ulValueLen != std::mem::size_of::<CK_BBOOL>() as CK_ULONG {
1046            return Err(CKR_MECHANISM_PARAM_INVALID)?;
1047        }
1048        let val: CK_BBOOL = unsafe { *(self.pValue as CK_BBOOL_PTR) };
1049        if val == 0 {
1050            Ok(false)
1051        } else {
1052            Ok(true)
1053        }
1054    }
1055
1056    /// Returns the internal data memory buffer as a slice
1057    ///
1058    /// Errors out if the internal data pointer is null
1059    pub fn to_slice(&self) -> Result<&[u8]> {
1060        if self.ulValueLen == 0 {
1061            return Ok(&[]);
1062        }
1063        if self.pValue.is_null() {
1064            return Err(CKR_MECHANISM_PARAM_INVALID)?;
1065        }
1066        Ok(bytes_to_slice(
1067            self.pValue as *const u8,
1068            usize::try_from(self.ulValueLen)?,
1069        ))
1070    }
1071
1072    /// Returns a copy of the internal buffer as an vector
1073    ///
1074    /// Returns an empty vector if the internal buffer pointer is null
1075    pub fn to_buf(&self) -> Result<Vec<u8>> {
1076        Ok(bytes_to_vec!(self.pValue, self.ulValueLen))
1077    }
1078}
1079
1080impl CK_OTP_PARAMS {
1081    /// Returns the internal data memory buffer as a slice
1082    ///
1083    /// Errors out if the internal data pointer is null
1084    pub fn to_slice(&self) -> Result<Cow<'_, [CK_OTP_PARAM]>> {
1085        if self.ulCount == 0 {
1086            return Ok(Cow::Borrowed(&[]));
1087        }
1088        if self.pParams.is_null() {
1089            return Err(CKR_MECHANISM_PARAM_INVALID)?;
1090        }
1091        struct_to_slice(self.pParams, usize::try_from(self.ulCount)?)
1092    }
1093}