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
|
"""
Default mappings for various key types and mechanisms.
None of this is provided for in PKCS#11 and its correctness should not be
assumed.
"""
from datetime import datetime
from struct import Struct
from .constants import (
Attribute,
CertificateType,
MechanismFlag,
ObjectClass,
)
from .mechanisms import Mechanism, KeyType, MGF
DEFAULT_GENERATE_MECHANISMS = {
KeyType.AES: Mechanism.AES_KEY_GEN,
KeyType.DES2: Mechanism.DES2_KEY_GEN,
KeyType.DES3: Mechanism.DES3_KEY_GEN,
KeyType.DH: Mechanism.DH_PKCS_KEY_PAIR_GEN,
KeyType.DSA: Mechanism.DSA_KEY_PAIR_GEN,
KeyType.EC: Mechanism.EC_KEY_PAIR_GEN,
KeyType.RSA: Mechanism.RSA_PKCS_KEY_PAIR_GEN,
KeyType.X9_42_DH: Mechanism.X9_42_DH_KEY_PAIR_GEN,
KeyType.EC_EDWARDS: Mechanism.EC_EDWARDS_KEY_PAIR_GEN,
}
"""
Default mechanisms for generating keys.
"""
_ENCRYPTION = MechanismFlag.ENCRYPT | MechanismFlag.DECRYPT
_SIGNING = MechanismFlag.SIGN | MechanismFlag.VERIFY
_WRAPPING = MechanismFlag.WRAP | MechanismFlag.UNWRAP
DEFAULT_KEY_CAPABILITIES = {
KeyType.AES: _ENCRYPTION | _SIGNING | _WRAPPING,
KeyType.DES2: _ENCRYPTION | _SIGNING | _WRAPPING,
KeyType.DES3: _ENCRYPTION | _SIGNING | _WRAPPING,
KeyType.DH: MechanismFlag.DERIVE,
KeyType.DSA: _SIGNING,
KeyType.EC: _SIGNING | MechanismFlag.DERIVE,
KeyType.RSA: _ENCRYPTION | _SIGNING | _WRAPPING,
KeyType.GENERIC_SECRET: 0,
KeyType.EC_EDWARDS: _SIGNING,
}
"""
Default capabilities for generating keys.
"""
DEFAULT_ENCRYPT_MECHANISMS = {
KeyType.AES: Mechanism.AES_CBC_PAD,
KeyType.DES2: Mechanism.DES3_CBC_PAD,
KeyType.DES3: Mechanism.DES3_CBC_PAD,
KeyType.RSA: Mechanism.RSA_PKCS_OAEP,
}
"""
Default mechanisms for encrypt/decrypt.
"""
DEFAULT_SIGN_MECHANISMS = {
KeyType.AES: Mechanism.AES_MAC,
KeyType.DES2: Mechanism.DES3_MAC,
KeyType.DES3: Mechanism.DES3_MAC,
KeyType.DSA: Mechanism.DSA_SHA512,
KeyType.EC: Mechanism.ECDSA_SHA512,
KeyType.RSA: Mechanism.SHA512_RSA_PKCS,
KeyType.EC_EDWARDS: Mechanism.EDDSA,
}
"""
Default mechanisms for sign/verify.
"""
DEFAULT_WRAP_MECHANISMS = {
KeyType.AES: Mechanism.AES_KEY_WRAP,
KeyType.DES2: Mechanism.DES3_ECB,
KeyType.DES3: Mechanism.DES3_ECB,
KeyType.RSA: Mechanism.RSA_PKCS_OAEP,
}
"""
Default mechanism for wrap/unwrap.
"""
DEFAULT_DERIVE_MECHANISMS = {
KeyType.DH: Mechanism.DH_PKCS_DERIVE,
KeyType.EC: Mechanism.ECDH1_DERIVE,
KeyType.X9_42_DH: Mechanism.X9_42_DH_DERIVE,
}
"""
Default mechanisms for key derivation
"""
DEFAULT_PARAM_GENERATE_MECHANISMS = {
KeyType.DH: Mechanism.DH_PKCS_PARAMETER_GEN,
KeyType.DSA: Mechanism.DSA_PARAMETER_GEN,
KeyType.X9_42_DH: Mechanism.X9_42_DH_PARAMETER_GEN,
}
"""
Default mechanisms for domain parameter generation
"""
DEFAULT_MECHANISM_PARAMS = {
Mechanism.RSA_PKCS_OAEP: (Mechanism.SHA_1, MGF.SHA1, None),
Mechanism.RSA_PKCS_PSS: (Mechanism.SHA_1, MGF.SHA1, 20),
}
"""
Default mechanism parameters
"""
# (Pack Function, Unpack Function) functions
_bool = (Struct('?').pack, lambda v: Struct('?').unpack(v)[0])
_ulong = (Struct('L').pack, lambda v: Struct('L').unpack(v)[0])
_str = (lambda s: s.encode('utf-8'), lambda b: b.decode('utf-8'))
_date = (lambda s: s.strftime('%Y%m%d').encode('ascii'),
lambda s: datetime.strptime(s.decode('ascii'), '%Y%m%d').date())
_bytes = (bytes, bytes)
# The PKCS#11 biginteger type is an array of bytes in network byte order.
# If you have an int type, wrap it in biginteger()
_biginteger = _bytes
def _enum(type_):
"""Factory to pack/unpack intos into IntEnums."""
pack, unpack = _ulong
return (lambda v: pack(int(v)),
lambda v: type_(unpack(v)))
ATTRIBUTE_TYPES = {
Attribute.ALWAYS_AUTHENTICATE: _bool,
Attribute.ALWAYS_SENSITIVE: _bool,
Attribute.APPLICATION: _str,
Attribute.BASE: _biginteger,
Attribute.CERTIFICATE_TYPE: _enum(CertificateType),
Attribute.CHECK_VALUE: _bytes,
Attribute.CLASS: _enum(ObjectClass),
Attribute.COEFFICIENT: _biginteger,
Attribute.DECRYPT: _bool,
Attribute.DERIVE: _bool,
Attribute.EC_PARAMS: _bytes,
Attribute.EC_POINT: _bytes,
Attribute.ENCRYPT: _bool,
Attribute.END_DATE: _date,
Attribute.EXPONENT_1: _biginteger,
Attribute.EXPONENT_2: _biginteger,
Attribute.EXTRACTABLE: _bool,
Attribute.HASH_OF_ISSUER_PUBLIC_KEY: _bytes,
Attribute.HASH_OF_SUBJECT_PUBLIC_KEY: _bytes,
Attribute.ID: _bytes,
Attribute.ISSUER: _bytes,
Attribute.KEY_GEN_MECHANISM: _enum(Mechanism),
Attribute.KEY_TYPE: _enum(KeyType),
Attribute.LABEL: _str,
Attribute.LOCAL: _bool,
Attribute.MODIFIABLE: _bool,
Attribute.COPYABLE: _bool,
Attribute.MODULUS: _biginteger,
Attribute.MODULUS_BITS: _ulong,
Attribute.NEVER_EXTRACTABLE: _bool,
Attribute.OBJECT_ID: _bytes,
Attribute.PRIME: _biginteger,
Attribute.PRIME_BITS: _ulong,
Attribute.PRIME_1: _biginteger,
Attribute.PRIME_2: _biginteger,
Attribute.PRIVATE: _bool,
Attribute.PRIVATE_EXPONENT: _biginteger,
Attribute.PUBLIC_EXPONENT: _biginteger,
Attribute.SENSITIVE: _bool,
Attribute.SERIAL_NUMBER: _bytes,
Attribute.SIGN: _bool,
Attribute.SIGN_RECOVER: _bool,
Attribute.START_DATE: _date,
Attribute.SUBJECT: _bytes,
Attribute.SUBPRIME: _biginteger,
Attribute.SUBPRIME_BITS: _ulong,
Attribute.TOKEN: _bool,
Attribute.TRUSTED: _bool,
Attribute.UNWRAP: _bool,
Attribute.URL: _str,
Attribute.VALUE: _biginteger,
Attribute.VALUE_BITS: _ulong,
Attribute.VALUE_LEN: _ulong,
Attribute.VERIFY: _bool,
Attribute.VERIFY_RECOVER: _bool,
Attribute.WRAP: _bool,
Attribute.WRAP_WITH_TRUSTED: _bool,
}
"""
Map of attributes to (serialize, deserialize) functions.
"""
|