python-pkcs11 0.8.0__cp313-cp313-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pkcs11/constants.py ADDED
@@ -0,0 +1,514 @@
1
+ """
2
+ PKCS#11 constants.
3
+
4
+ See the Python :mod:`enum` documentation for more information on how to
5
+ use these classes.
6
+ """
7
+
8
+ from enum import IntEnum, IntFlag, unique
9
+
10
+ DEFAULT = object()
11
+ """Sentinel value used in templates.
12
+
13
+ Not all pkcs11 attribute sets are accepted by HSMs.
14
+ Use this value to remove the attribute from the template
15
+ sent to the HSM or to use the HSM default value.
16
+ """
17
+
18
+
19
+ @unique
20
+ class UserType(IntEnum):
21
+ """PKCS#11 user types."""
22
+
23
+ NOBODY = 999
24
+ """
25
+ Not officially in the PKCS#11 spec. Used to represent a session that is not
26
+ logged in.
27
+ """
28
+ SO = 0
29
+ """Security officer."""
30
+ USER = 1
31
+
32
+
33
+ class ObjectClass(IntEnum):
34
+ """
35
+ PKCS#11 :class:`Object` class.
36
+
37
+ This is the type of object we have.
38
+ """
39
+
40
+ DATA = 0x00000000
41
+ CERTIFICATE = 0x00000001
42
+ """See :class:`pkcs11.Certificate`."""
43
+ PUBLIC_KEY = 0x00000002
44
+ """See :class:`pkcs11.PublicKey`."""
45
+ PRIVATE_KEY = 0x00000003
46
+ """See :class:`pkcs11.PrivateKey`."""
47
+ SECRET_KEY = 0x00000004
48
+ """See :class:`pkcs11.SecretKey`."""
49
+ HW_FEATURE = 0x00000005
50
+ DOMAIN_PARAMETERS = 0x00000006
51
+ """See :class:`pkcs11.DomainParameters`."""
52
+ MECHANISM = 0x00000007
53
+ OTP_KEY = 0x00000008
54
+ PROFILE = 0x00000009
55
+
56
+ _VENDOR_DEFINED = 0x80000000
57
+
58
+ def __repr__(self):
59
+ return "<ObjectClass.%s>" % self.name
60
+
61
+
62
+ _ARRAY_ATTRIBUTE = 0x40000000
63
+ """Attribute consists of an array of values."""
64
+
65
+
66
+ class Attribute(IntEnum):
67
+ """
68
+ PKCS#11 object attributes.
69
+
70
+ Not all attributes are relevant to all objects.
71
+ Relevant attributes for each object type are given in `PKCS #11
72
+ <http://docs.oasis-open.org/pkcs11/pkcs11-curr/v2.40/pkcs11-curr-v2.40.html>`_.
73
+ """
74
+
75
+ CLASS = 0x00000000
76
+ """Object type (:class:`ObjectClass`)."""
77
+ TOKEN = 0x00000001
78
+ """
79
+ If True object will be stored to token.
80
+ Otherwise has session lifetime (:class:`bool`).
81
+ """
82
+ PRIVATE = 0x00000002
83
+ """
84
+ True if user must be authenticated to access this object (:class:`bool`).
85
+ """
86
+ LABEL = 0x00000003
87
+ """Object label (:class:`str`)."""
88
+ APPLICATION = 0x00000010
89
+ VALUE = 0x00000011
90
+ """
91
+ Object value. Usually represents a secret or private key.
92
+ For certificates this is the complete certificate in the certificate's
93
+ native format (e.g. BER-encoded X.509 or WTLS encoding).
94
+
95
+ May be `SENSITIVE` (:class:`bytes`).
96
+ """
97
+ OBJECT_ID = 0x00000012
98
+ CERTIFICATE_TYPE = 0x00000080
99
+ """
100
+ Certificate type (:class:`CertificateType`).
101
+ """
102
+ ISSUER = 0x00000081
103
+ """
104
+ Certificate issuer in certificate's native format
105
+ (e.g. X.509 DER-encoding or WTLS encoding) (:class:`bytes`).
106
+ """
107
+ SERIAL_NUMBER = 0x00000082
108
+ """
109
+ Certificate serial number in certificate's native format
110
+ (e.g. X.509 DER-encoding) (:class:`bytes`).
111
+ """
112
+ AC_ISSUER = 0x00000083
113
+ """
114
+ Attribute Certificate Issuer. Different from `ISSUER` because the
115
+ encoding is different (:class:`bytes`).
116
+ """
117
+ OWNER = 0x00000084
118
+ """
119
+ Attribute Certificate Owner. Different from `SUBJECT` because the
120
+ encoding is different (:class:`bytes`).
121
+ """
122
+ ATTR_TYPES = 0x00000085
123
+ """
124
+ BER-encoding of a sequence of object identifier values corresponding to the
125
+ attribute types contained in the certificate. When present, this field
126
+ offers an opportunity for applications to search for a particular attribute
127
+ certificate without fetching and parsing the certificate itself.
128
+ """
129
+ TRUSTED = 0x00000086
130
+ """
131
+ This key can be used to wrap keys with `WRAP_WITH_TRUSTED` set;
132
+ or this certificate can be trusted.
133
+ (:class:`bool`).
134
+ """
135
+ CERTIFICATE_CATEGORY = 0x00000087
136
+ """
137
+ Certificate category (:class:`CertificateCategory`).
138
+ """
139
+ JAVA_MIDP_SECURITY_DOMAIN = 0x00000088
140
+ URL = 0x00000089
141
+ """URL where the complete certificate can be obtained."""
142
+ HASH_OF_SUBJECT_PUBLIC_KEY = 0x0000008A
143
+ """Hash of the certificate subject's public key."""
144
+ HASH_OF_ISSUER_PUBLIC_KEY = 0x0000008B
145
+ """Hash of the certificate issuer's public key."""
146
+ CHECK_VALUE = 0x00000090
147
+ """`VALUE` checksum. Key Check Value (:class:`bytes`)."""
148
+
149
+ KEY_TYPE = 0x00000100
150
+ """Key type (:class:`KeyType`)."""
151
+ SUBJECT = 0x00000101
152
+ """
153
+ Certificate subject in certificate's native format
154
+ (e.g. X.509 DER-encoding or WTLS encoding) (:class:`bytes`).
155
+ """
156
+ ID = 0x00000102
157
+ """Key ID (bytes)."""
158
+ SENSITIVE = 0x00000103
159
+ """
160
+ Sensitive attributes cannot be retrieved from the HSM
161
+ (e.g. `VALUE` or `PRIVATE_EXPONENT`) (:class:`bool`).
162
+ """
163
+ ENCRYPT = 0x00000104
164
+ """Key supports encryption (:class:`bool`)."""
165
+ DECRYPT = 0x00000105
166
+ """Key supports decryption (:class:`bool`)."""
167
+ WRAP = 0x00000106
168
+ """Key supports wrapping (:class:`bool`)."""
169
+ UNWRAP = 0x00000107
170
+ """Key supports unwrapping (:class:`bool`)."""
171
+ SIGN = 0x00000108
172
+ """Key supports signing (:class:`bool`)."""
173
+ SIGN_RECOVER = 0x00000109
174
+ VERIFY = 0x0000010A
175
+ """Key supports signature verification (:class:`bool`)."""
176
+ VERIFY_RECOVER = 0x0000010B
177
+ DERIVE = 0x0000010C
178
+ """Key supports key derivation (:class:`bool`)."""
179
+ START_DATE = 0x00000110
180
+ """Start date for the object's validity (:class:`datetime.date`)."""
181
+ END_DATE = 0x00000111
182
+ """End date for the object's validity (:class:`datetime.date`)."""
183
+ MODULUS = 0x00000120
184
+ """RSA private key modulus (n) (`biginteger` as :class:`bytes`)."""
185
+ MODULUS_BITS = 0x00000121
186
+ """
187
+ RSA private key modulus length. Use this for private key generation
188
+ (:class:`int`).
189
+ """
190
+ PUBLIC_EXPONENT = 0x00000122
191
+ """
192
+ RSA public exponent (e) (`biginteger` as :class:`bytes`).
193
+
194
+ Default is b'\1\0\1' (65537).
195
+ """
196
+ PRIVATE_EXPONENT = 0x00000123
197
+ """RSA private exponent (d) (`biginteger` as :class:`bytes`)."""
198
+ PRIME_1 = 0x00000124
199
+ """
200
+ RSA private key prime #1 (p). May not be stored.
201
+ (`biginteger` as :class:`bytes`).
202
+ """
203
+ PRIME_2 = 0x00000125
204
+ """
205
+ RSA private key prime #2 (q). May not be stored.
206
+ (`biginteger` as :class:`bytes`).
207
+ """
208
+ EXPONENT_1 = 0x00000126
209
+ """
210
+ RSA private key exponent #1 (d mod p-1). May not be stored.
211
+ (`biginteger` as :class:`bytes`).
212
+ """
213
+ EXPONENT_2 = 0x00000127
214
+ """
215
+ RSA private key exponent #2 (d mod q-1). May not be stored.
216
+ (`biginteger` as :class:`bytes`).
217
+ """
218
+ COEFFICIENT = 0x00000128
219
+ """
220
+ RSA private key CRT coefficient (q^-1 mod p). May not be stored.
221
+ (`biginteger` as :class:`bytes`).
222
+ """
223
+ PRIME = 0x00000130
224
+ """
225
+ Prime number 'q' (used for DH).
226
+ (`biginteger` as :class:`bytes`).
227
+ """
228
+ SUBPRIME = 0x00000131
229
+ """
230
+ Subprime number 'q' (used for DH).
231
+ (`biginteger` as :class:`bytes`).
232
+ """
233
+ BASE = 0x00000132
234
+ """
235
+ Base number 'g' (used for DH).
236
+ (`biginteger` as :class:`bytes`).
237
+ """
238
+
239
+ PRIME_BITS = 0x00000133
240
+ SUBPRIME_BITS = 0x00000134
241
+
242
+ VALUE_BITS = 0x00000160
243
+ VALUE_LEN = 0x00000161
244
+ """
245
+ `VALUE` length in bytes. Use this for secret key generation
246
+ (:class:`int`).
247
+ """
248
+ EXTRACTABLE = 0x00000162
249
+ """Key can be extracted wrapped."""
250
+ LOCAL = 0x00000163
251
+ """True if generated on the token, False if imported."""
252
+ NEVER_EXTRACTABLE = 0x00000164
253
+ """`EXTRACTABLE` has always been False."""
254
+ ALWAYS_SENSITIVE = 0x00000165
255
+ """`SENSITIVE` has always been True."""
256
+ KEY_GEN_MECHANISM = 0x00000166
257
+ """Key generation mechanism (:class:`pkcs11.mechanisms.Mechanism`)."""
258
+
259
+ MODIFIABLE = 0x00000170
260
+ """Object can be modified (:class:`bool`)."""
261
+ COPYABLE = 0x00000171
262
+ """Object can be copied (:class:`bool`)."""
263
+
264
+ EC_PARAMS = 0x00000180
265
+ """
266
+ DER-encoded ANSI X9.62 Elliptic-Curve domain parameters (:class:`bytes`).
267
+
268
+ These can packed using :mod:`pkcs11.util.ec.encode_named_curve_parameters`:
269
+
270
+ ::
271
+
272
+ from pkcs11.util.ec import encode_named_curve_parameters
273
+
274
+ ecParams = encode_named_curve_parameters('secp256r1')
275
+
276
+ Or output by OpenSSL:
277
+
278
+ ::
279
+
280
+ openssl ecparam -outform der -name <curve name> | base64
281
+
282
+ """
283
+
284
+ EC_POINT = 0x00000181
285
+ """
286
+ DER-encoded ANSI X9.62 Public key for :attr:`KeyType.EC` (:class:`bytes`).
287
+ """
288
+
289
+ SECONDARY_AUTH = 0x00000200
290
+ AUTH_PIN_FLAGS = 0x00000201
291
+
292
+ ALWAYS_AUTHENTICATE = 0x00000202
293
+ """
294
+ User has to provide pin with each use (sign or decrypt) (:class:`bool`).
295
+ """
296
+
297
+ WRAP_WITH_TRUSTED = 0x00000210
298
+ """Key can only be wrapped with a `TRUSTED` key."""
299
+ WRAP_TEMPLATE = _ARRAY_ATTRIBUTE | 0x00000211
300
+ UNWRAP_TEMPLATE = _ARRAY_ATTRIBUTE | 0x00000212
301
+ DERIVE_TEMPLATE = _ARRAY_ATTRIBUTE | 0x00000213
302
+
303
+ OTP_FORMAT = 0x00000220
304
+ OTP_LENGTH = 0x00000221
305
+ OTP_TIME_INTERVAL = 0x00000222
306
+ OTP_USER_FRIENDLY_MODE = 0x00000223
307
+ OTP_CHALLENGE_REQUIREMENT = 0x00000224
308
+ OTP_TIME_REQUIREMENT = 0x00000225
309
+ OTP_COUNTER_REQUIREMENT = 0x00000226
310
+ OTP_PIN_REQUIREMENT = 0x00000227
311
+ OTP_COUNTER = 0x0000022E
312
+ OTP_TIME = 0x0000022F
313
+ OTP_USER_IDENTIFIER = 0x0000022A
314
+ OTP_SERVICE_IDENTIFIER = 0x0000022B
315
+ OTP_SERVICE_LOGO = 0x0000022C
316
+ OTP_SERVICE_LOGO_TYPE = 0x0000022D
317
+
318
+ GOSTR3410_PARAMS = 0x00000250
319
+ GOSTR3411_PARAMS = 0x00000251
320
+ GOST28147_PARAMS = 0x00000252
321
+
322
+ HW_FEATURE_TYPE = 0x00000300
323
+ RESET_ON_INIT = 0x00000301
324
+ HAS_RESET = 0x00000302
325
+
326
+ PIXEL_X = 0x00000400
327
+ PIXEL_Y = 0x00000401
328
+ RESOLUTION = 0x00000402
329
+ CHAR_ROWS = 0x00000403
330
+ CHAR_COLUMNS = 0x00000404
331
+ COLOR = 0x00000405
332
+ BITS_PER_PIXEL = 0x00000406
333
+ CHAR_SETS = 0x00000480
334
+ ENCODING_METHODS = 0x00000481
335
+ MIME_TYPES = 0x00000482
336
+ MECHANISM_TYPE = 0x00000500
337
+ REQUIRED_CMS_ATTRIBUTES = 0x00000501
338
+ DEFAULT_CMS_ATTRIBUTES = 0x00000502
339
+ SUPPORTED_CMS_ATTRIBUTES = 0x00000503
340
+ ALLOWED_MECHANISMS = _ARRAY_ATTRIBUTE | 0x00000600
341
+
342
+ _VENDOR_DEFINED = 0x80000000
343
+
344
+ def __repr__(self):
345
+ return "<Attribute.%s>" % self.name
346
+
347
+
348
+ class CertificateType(IntEnum):
349
+ """
350
+ Certificate type of a :class:`pkcs11.Certificate`.
351
+ """
352
+
353
+ X_509 = 0x00000000
354
+ X_509_ATTR_CERT = 0x00000001
355
+ WTLS = 0x00000002
356
+ _VENDOR_DEFINED = 0x80000000
357
+
358
+
359
+ @unique
360
+ class MechanismFlag(IntFlag):
361
+ """
362
+ Describes the capabilities of a :class:`pkcs11.mechanisms.Mechanism`
363
+ or :class:`pkcs11.Object`.
364
+
365
+ Some objects and mechanisms are symmetric (i.e. can be used for encryption
366
+ and decryption), some are asymmetric (e.g. public key cryptography).
367
+ """
368
+
369
+ HW = 0x00000001
370
+ """Mechanism is performed in hardware."""
371
+
372
+ ENCRYPT = 0x00000100
373
+ """Can be used for encryption."""
374
+ DECRYPT = 0x00000200
375
+ """Can be used for decryption."""
376
+ DIGEST = 0x00000400
377
+ """Can make a message digest (hash)."""
378
+ SIGN = 0x00000800
379
+ """Can calculate digital signature."""
380
+ SIGN_RECOVER = 0x00001000
381
+ VERIFY = 0x00002000
382
+ """Can verify digital signature."""
383
+ VERIFY_RECOVER = 0x00004000
384
+ GENERATE = 0x00008000
385
+ """Can generate key/object."""
386
+ GENERATE_KEY_PAIR = 0x00010000
387
+ """Can generate key pair."""
388
+ WRAP = 0x00020000
389
+ """Can wrap a key for export."""
390
+ UNWRAP = 0x00040000
391
+ """Can unwrap a key for import."""
392
+ DERIVE = 0x00080000
393
+ """Can derive a key from another key."""
394
+
395
+ EC_F_P = 0x00100000
396
+ EC_F_2M = 0x00200000
397
+ EC_ECPARAMETERS = 0x00400000
398
+ EC_NAMEDCURVE = 0x00800000
399
+ EC_UNCOMPRESS = 0x01000000
400
+ EC_COMPRESS = 0x02000000
401
+
402
+ EXTENSION = 0x80000000
403
+
404
+
405
+ @unique
406
+ class SlotFlag(IntFlag):
407
+ """:class:`pkcs11.Slot` flags."""
408
+
409
+ TOKEN_PRESENT = 0x00000001
410
+ """
411
+ A token is present in the slot
412
+ (N.B. some hardware known not to set this for soft-tokens.)
413
+ """
414
+ REMOVABLE_DEVICE = 0x00000002
415
+ """Removable devices."""
416
+ HW_SLOT = 0x00000004
417
+ """Hardware slot."""
418
+
419
+
420
+ @unique
421
+ class TokenFlag(IntFlag):
422
+ """:class:`pkcs11.Token` flags."""
423
+
424
+ RNG = 0x00000001
425
+ """Has random number generator."""
426
+ WRITE_PROTECTED = 0x00000002
427
+ """Token is write protected."""
428
+ LOGIN_REQUIRED = 0x00000004
429
+ """User must login."""
430
+ USER_PIN_INITIALIZED = 0x00000008
431
+ """Normal user's pin is set."""
432
+
433
+ RESTORE_KEY_NOT_NEEDED = 0x00000020
434
+ """
435
+ If it is set, that means that *every* time the state of cryptographic
436
+ operations of a session is successfully saved, all keys needed to continue
437
+ those operations are stored in the state.
438
+ """
439
+
440
+ CLOCK_ON_TOKEN = 0x00000040
441
+ """
442
+ If it is set, that means that the token has some sort of clock. The time
443
+ on that clock is returned in the token info structure.
444
+ """
445
+
446
+ PROTECTED_AUTHENTICATION_PATH = 0x00000100
447
+ """
448
+ If it is set, that means that there is some way for the user to login
449
+ without sending a PIN through the Cryptoki library itself.
450
+ """
451
+
452
+ DUAL_CRYPTO_OPERATIONS = 0x00000200
453
+ """
454
+ If it is true, that means that a single session with the token can perform
455
+ dual simultaneous cryptographic operations (digest and encrypt; decrypt and
456
+ digest; sign and encrypt; and decrypt and sign).
457
+ """
458
+
459
+ TOKEN_INITIALIZED = 0x00000400
460
+ """
461
+ If it is true, the token has been initialized using C_InitializeToken or an
462
+ equivalent mechanism outside the scope of PKCS #11. Calling
463
+ C_InitializeToken when this flag is set will cause the token to be
464
+ reinitialized.
465
+ """
466
+
467
+ USER_PIN_COUNT_LOW = 0x00010000
468
+ """
469
+ If it is true, an incorrect user login PIN has been entered at least once
470
+ since the last successful authentication.
471
+ """
472
+
473
+ USER_PIN_FINAL_TRY = 0x00020000
474
+ """
475
+ If it is true, supplying an incorrect user PIN will it to become locked.
476
+ """
477
+
478
+ USER_PIN_LOCKED = 0x00040000
479
+ """
480
+ If it is true, the user PIN has been locked. User login to the token is not
481
+ possible.
482
+ """
483
+
484
+ USER_PIN_TO_BE_CHANGED = 0x00080000
485
+ """
486
+ If it is true, the user PIN value is the default value set by token
487
+ initialization or manufacturing, or the PIN has been expired by the card.
488
+ """
489
+
490
+ SO_PIN_COUNT_LOW = 0x00100000
491
+ """
492
+ If it is true, an incorrect SO (security officer) login PIN has been
493
+ entered at least once since the last successful authentication.
494
+ """
495
+
496
+ SO_PIN_FINAL_TRY = 0x00200000
497
+ """
498
+ If it is true, supplying an incorrect SO (security officer) PIN will it to
499
+ become locked.
500
+ """
501
+
502
+ SO_PIN_LOCKED = 0x00400000
503
+ """
504
+ If it is true, the SO (security officer) PIN has been locked. SO login to
505
+ the token is not possible.
506
+ """
507
+
508
+ SO_PIN_TO_BE_CHANGED = 0x00800000
509
+ """
510
+ If it is true, the SO PIN value is the default value set by token
511
+ initialization or manufacturing, or the PIN has been expired by the card.
512
+ """
513
+
514
+ ERROR_STATE = 0x01000000
pkcs11/defaults.py ADDED
@@ -0,0 +1,197 @@
1
+ """
2
+ Default mappings for various key types and mechanisms.
3
+
4
+ None of this is provided for in PKCS#11 and its correctness should not be
5
+ assumed.
6
+ """
7
+
8
+ from datetime import datetime
9
+ from struct import Struct
10
+
11
+ from .constants import (
12
+ Attribute,
13
+ CertificateType,
14
+ MechanismFlag,
15
+ ObjectClass,
16
+ )
17
+ from .mechanisms import MGF, KeyType, Mechanism
18
+
19
+ DEFAULT_GENERATE_MECHANISMS = {
20
+ KeyType.AES: Mechanism.AES_KEY_GEN,
21
+ KeyType.DES2: Mechanism.DES2_KEY_GEN,
22
+ KeyType.DES3: Mechanism.DES3_KEY_GEN,
23
+ KeyType.DH: Mechanism.DH_PKCS_KEY_PAIR_GEN,
24
+ KeyType.DSA: Mechanism.DSA_KEY_PAIR_GEN,
25
+ KeyType.EC: Mechanism.EC_KEY_PAIR_GEN,
26
+ KeyType.RSA: Mechanism.RSA_PKCS_KEY_PAIR_GEN,
27
+ KeyType.X9_42_DH: Mechanism.X9_42_DH_KEY_PAIR_GEN,
28
+ KeyType.EC_EDWARDS: Mechanism.EC_EDWARDS_KEY_PAIR_GEN,
29
+ KeyType.GENERIC_SECRET: Mechanism.GENERIC_SECRET_KEY_GEN,
30
+ }
31
+ """
32
+ Default mechanisms for generating keys.
33
+ """
34
+
35
+ _ENCRYPTION = MechanismFlag.ENCRYPT | MechanismFlag.DECRYPT
36
+ _SIGNING = MechanismFlag.SIGN | MechanismFlag.VERIFY
37
+ _WRAPPING = MechanismFlag.WRAP | MechanismFlag.UNWRAP
38
+
39
+ DEFAULT_KEY_CAPABILITIES = {
40
+ KeyType.AES: _ENCRYPTION | _SIGNING | _WRAPPING,
41
+ KeyType.DES2: _ENCRYPTION | _SIGNING | _WRAPPING,
42
+ KeyType.DES3: _ENCRYPTION | _SIGNING | _WRAPPING,
43
+ KeyType.DH: MechanismFlag.DERIVE,
44
+ KeyType.DSA: _SIGNING,
45
+ KeyType.EC: _SIGNING | MechanismFlag.DERIVE,
46
+ KeyType.RSA: _ENCRYPTION | _SIGNING | _WRAPPING,
47
+ KeyType.GENERIC_SECRET: 0,
48
+ KeyType.EC_EDWARDS: _SIGNING,
49
+ }
50
+ """
51
+ Default capabilities for generating keys.
52
+ """
53
+
54
+ DEFAULT_ENCRYPT_MECHANISMS = {
55
+ KeyType.AES: Mechanism.AES_CBC_PAD,
56
+ KeyType.DES2: Mechanism.DES3_CBC_PAD,
57
+ KeyType.DES3: Mechanism.DES3_CBC_PAD,
58
+ KeyType.RSA: Mechanism.RSA_PKCS_OAEP,
59
+ }
60
+ """
61
+ Default mechanisms for encrypt/decrypt.
62
+ """
63
+
64
+ DEFAULT_SIGN_MECHANISMS = {
65
+ KeyType.AES: Mechanism.AES_MAC,
66
+ KeyType.DES2: Mechanism.DES3_MAC,
67
+ KeyType.DES3: Mechanism.DES3_MAC,
68
+ KeyType.DSA: Mechanism.DSA_SHA512,
69
+ KeyType.EC: Mechanism.ECDSA_SHA512,
70
+ KeyType.RSA: Mechanism.SHA512_RSA_PKCS,
71
+ KeyType.EC_EDWARDS: Mechanism.EDDSA,
72
+ }
73
+ """
74
+ Default mechanisms for sign/verify.
75
+ """
76
+
77
+ DEFAULT_WRAP_MECHANISMS = {
78
+ KeyType.AES: Mechanism.AES_KEY_WRAP,
79
+ KeyType.DES2: Mechanism.DES3_ECB,
80
+ KeyType.DES3: Mechanism.DES3_ECB,
81
+ KeyType.RSA: Mechanism.RSA_PKCS_OAEP,
82
+ }
83
+ """
84
+ Default mechanism for wrap/unwrap.
85
+ """
86
+
87
+ DEFAULT_DERIVE_MECHANISMS = {
88
+ KeyType.DH: Mechanism.DH_PKCS_DERIVE,
89
+ KeyType.EC: Mechanism.ECDH1_DERIVE,
90
+ KeyType.X9_42_DH: Mechanism.X9_42_DH_DERIVE,
91
+ }
92
+ """
93
+ Default mechanisms for key derivation
94
+ """
95
+
96
+ DEFAULT_PARAM_GENERATE_MECHANISMS = {
97
+ KeyType.DH: Mechanism.DH_PKCS_PARAMETER_GEN,
98
+ KeyType.DSA: Mechanism.DSA_PARAMETER_GEN,
99
+ KeyType.X9_42_DH: Mechanism.X9_42_DH_PARAMETER_GEN,
100
+ }
101
+ """
102
+ Default mechanisms for domain parameter generation
103
+ """
104
+
105
+
106
+ DEFAULT_MECHANISM_PARAMS = {
107
+ Mechanism.RSA_PKCS_OAEP: (Mechanism.SHA_1, MGF.SHA1, None),
108
+ Mechanism.RSA_PKCS_PSS: (Mechanism.SHA_1, MGF.SHA1, 20),
109
+ }
110
+ """
111
+ Default mechanism parameters
112
+ """
113
+
114
+
115
+ # (Pack Function, Unpack Function) functions
116
+ _bool = (Struct("?").pack, lambda v: Struct("?").unpack(v)[0])
117
+ _ulong = (Struct("L").pack, lambda v: Struct("L").unpack(v)[0])
118
+ _str = (lambda s: s.encode("utf-8"), lambda b: b.decode("utf-8"))
119
+ _date = (
120
+ lambda s: s.strftime("%Y%m%d").encode("ascii"),
121
+ lambda s: datetime.strptime(s.decode("ascii"), "%Y%m%d").date(),
122
+ )
123
+ _bytes = (bytes, bytes)
124
+ # The PKCS#11 biginteger type is an array of bytes in network byte order.
125
+ # If you have an int type, wrap it in biginteger()
126
+ _biginteger = _bytes
127
+
128
+
129
+ def _enum(type_):
130
+ """Factory to pack/unpack intos into IntEnums."""
131
+ pack, unpack = _ulong
132
+
133
+ return (lambda v: pack(int(v)), lambda v: type_(unpack(v)))
134
+
135
+
136
+ ATTRIBUTE_TYPES = {
137
+ Attribute.ALWAYS_AUTHENTICATE: _bool,
138
+ Attribute.ALWAYS_SENSITIVE: _bool,
139
+ Attribute.APPLICATION: _str,
140
+ Attribute.BASE: _biginteger,
141
+ Attribute.CERTIFICATE_TYPE: _enum(CertificateType),
142
+ Attribute.CHECK_VALUE: _bytes,
143
+ Attribute.CLASS: _enum(ObjectClass),
144
+ Attribute.COEFFICIENT: _biginteger,
145
+ Attribute.DECRYPT: _bool,
146
+ Attribute.DERIVE: _bool,
147
+ Attribute.EC_PARAMS: _bytes,
148
+ Attribute.EC_POINT: _bytes,
149
+ Attribute.ENCRYPT: _bool,
150
+ Attribute.END_DATE: _date,
151
+ Attribute.EXPONENT_1: _biginteger,
152
+ Attribute.EXPONENT_2: _biginteger,
153
+ Attribute.EXTRACTABLE: _bool,
154
+ Attribute.HASH_OF_ISSUER_PUBLIC_KEY: _bytes,
155
+ Attribute.HASH_OF_SUBJECT_PUBLIC_KEY: _bytes,
156
+ Attribute.ID: _bytes,
157
+ Attribute.ISSUER: _bytes,
158
+ Attribute.KEY_GEN_MECHANISM: _enum(Mechanism),
159
+ Attribute.KEY_TYPE: _enum(KeyType),
160
+ Attribute.LABEL: _str,
161
+ Attribute.LOCAL: _bool,
162
+ Attribute.MODIFIABLE: _bool,
163
+ Attribute.COPYABLE: _bool,
164
+ Attribute.MODULUS: _biginteger,
165
+ Attribute.MODULUS_BITS: _ulong,
166
+ Attribute.NEVER_EXTRACTABLE: _bool,
167
+ Attribute.OBJECT_ID: _bytes,
168
+ Attribute.PRIME: _biginteger,
169
+ Attribute.PRIME_BITS: _ulong,
170
+ Attribute.PRIME_1: _biginteger,
171
+ Attribute.PRIME_2: _biginteger,
172
+ Attribute.PRIVATE: _bool,
173
+ Attribute.PRIVATE_EXPONENT: _biginteger,
174
+ Attribute.PUBLIC_EXPONENT: _biginteger,
175
+ Attribute.SENSITIVE: _bool,
176
+ Attribute.SERIAL_NUMBER: _bytes,
177
+ Attribute.SIGN: _bool,
178
+ Attribute.SIGN_RECOVER: _bool,
179
+ Attribute.START_DATE: _date,
180
+ Attribute.SUBJECT: _bytes,
181
+ Attribute.SUBPRIME: _biginteger,
182
+ Attribute.SUBPRIME_BITS: _ulong,
183
+ Attribute.TOKEN: _bool,
184
+ Attribute.TRUSTED: _bool,
185
+ Attribute.UNWRAP: _bool,
186
+ Attribute.URL: _str,
187
+ Attribute.VALUE: _biginteger,
188
+ Attribute.VALUE_BITS: _ulong,
189
+ Attribute.VALUE_LEN: _ulong,
190
+ Attribute.VERIFY: _bool,
191
+ Attribute.VERIFY_RECOVER: _bool,
192
+ Attribute.WRAP: _bool,
193
+ Attribute.WRAP_WITH_TRUSTED: _bool,
194
+ }
195
+ """
196
+ Map of attributes to (serialize, deserialize) functions.
197
+ """