Skip to content

Commit 886ff57

Browse files
author
Danielle Madeley
committed
Finish off port to asn1crypto
1 parent 2cb3e65 commit 886ff57

File tree

9 files changed

+121
-135
lines changed

9 files changed

+121
-135
lines changed

dev-requirements.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ Cython
22
setuptools_scm
33

44
# Used for tests
5-
pyasn1
6-
pyasn1-modules
75
oscrypto
86
cryptography
97

dev-requirements.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ jinja2==2.9.6 # via sphinx
1818
markupsafe==1.0 # via jinja2
1919
mccabe==0.6.1 # via flake8
2020
oscrypto==0.18.0
21-
pyasn1-modules==0.0.9
22-
pyasn1==0.2.3
2321
pycodestyle==2.3.1 # via flake8
2422
pycparser==2.18 # via cffi
2523
pyflakes==1.5.0 # via flake8

pkcs11/util/dh.py

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,47 @@
11
"""
22
Key handling utilities for Diffie-Hellman keys.
3-
4-
These utilities depend on :mod:`pyasn1` and :mod:`pyasn1_modules`.
53
"""
64

7-
from pyasn1.codec.der import encoder, decoder
8-
from pyasn1_modules.rfc3279 import DomainParameters, DHPublicKey
5+
from asn1crypto.algos import DHParameters
6+
from asn1crypto.core import Integer
97

108
from . import biginteger
119
from ..constants import Attribute
1210
from ..exceptions import AttributeTypeInvalid
1311

1412

15-
def decode_x9_42_dh_domain_parameters(der):
13+
def decode_dh_domain_parameters(der):
1614
"""
17-
Decode RFC3279 (X9.42) DER-encoded Diffie-Hellman domain parameters.
15+
Decode DER-encoded Diffie-Hellman domain parameters.
1816
1917
:param bytes der: DER-encoded parameters
2018
:rtype: dict(Attribute,*)
2119
"""
2220

23-
params, _ = decoder.decode(der, asn1Spec=DomainParameters())
21+
params = DHParameters.load(der)
2422

2523
return {
2624
Attribute.BASE: biginteger(params['g']),
2725
Attribute.PRIME: biginteger(params['p']),
28-
Attribute.SUBPRIME: biginteger(params['q']),
2926
}
3027

3128

32-
def encode_x9_42_dh_domain_parameters(obj):
29+
def encode_dh_domain_parameters(obj):
3330
"""
34-
Encode DH domain parameters into RFC 3279 (X9.42) DER-encoded format.
31+
Encode DH domain parameters into DER-encoded format.
3532
3633
Calculates the subprime if it isn't available.
3734
3835
:param DomainParameters obj: domain parameters
3936
:rtype: bytes
4037
"""
4138

42-
asn1 = DomainParameters()
43-
asn1['g'] = int.from_bytes(obj[Attribute.BASE], byteorder='big')
44-
asn1['p'] = int.from_bytes(obj[Attribute.PRIME], byteorder='big')
45-
46-
try:
47-
asn1['q'] = int.from_bytes(obj[Attribute.SUBPRIME], byteorder='big')
48-
except AttributeTypeInvalid:
49-
# If we don't have the subprime, calculate it.
50-
asn1['q'] = (asn1['p'] - 1) // 2
39+
asn1 = DHParameters({
40+
'g': int.from_bytes(obj[Attribute.BASE], byteorder='big'),
41+
'p': int.from_bytes(obj[Attribute.PRIME], byteorder='big'),
42+
})
5143

52-
return encoder.encode(asn1)
44+
return asn1.dump()
5345

5446

5547
def encode_dh_public_key(key):
@@ -60,9 +52,9 @@ def encode_dh_public_key(key):
6052
:rtype: bytes
6153
"""
6254

63-
asn1 = DHPublicKey(int.from_bytes(key[Attribute.VALUE], byteorder='big'))
55+
asn1 = Integer(int.from_bytes(key[Attribute.VALUE], byteorder='big'))
6456

65-
return encoder.encode(asn1)
57+
return asn1.dump()
6658

6759

6860
def decode_dh_public_key(der):
@@ -75,5 +67,5 @@ def decode_dh_public_key(der):
7567
:rtype: bytes
7668
"""
7769

78-
asn1, _ = decoder.decode(der, asn1Spec=DHPublicKey())
70+
asn1 = Integer.load(der)
7971
return biginteger(asn1)

pkcs11/util/dsa.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
"""
22
Key handling utilities for DSA keys, domain parameters and signatures..
3-
4-
These utilities depend on :mod:`pyasn1` and :mod:`pyasn1_modules`.
53
"""
64

7-
from pyasn1.codec.der import encoder, decoder
8-
from pyasn1_modules.rfc3279 import Dss_Parms, Dss_Sig_Value, DSAPublicKey
5+
from asn1crypto.core import Integer
6+
from asn1crypto.keys import DSAParams
7+
from asn1crypto.algos import DSASignature
98

109
from . import biginteger
1110
from ..constants import Attribute
@@ -19,7 +18,7 @@ def decode_dsa_domain_parameters(der):
1918
:rtype: dict(Attribute,*)
2019
"""
2120

22-
params, _ = decoder.decode(der, asn1Spec=Dss_Parms())
21+
params = DSAParams.load(der)
2322

2423
return {
2524
Attribute.BASE: biginteger(params['g']),
@@ -35,12 +34,13 @@ def encode_dsa_domain_parameters(obj):
3534
:param DomainParameters obj: domain parameters
3635
:rtype: bytes
3736
"""
38-
asn1 = Dss_Parms()
39-
asn1['g'] = int.from_bytes(obj[Attribute.BASE], byteorder='big')
40-
asn1['p'] = int.from_bytes(obj[Attribute.PRIME], byteorder='big')
41-
asn1['q'] = int.from_bytes(obj[Attribute.SUBPRIME], byteorder='big')
37+
asn1 = DSAParams({
38+
'g': int.from_bytes(obj[Attribute.BASE], byteorder='big'),
39+
'p': int.from_bytes(obj[Attribute.PRIME], byteorder='big'),
40+
'q': int.from_bytes(obj[Attribute.SUBPRIME], byteorder='big'),
41+
})
4242

43-
return encoder.encode(asn1)
43+
return asn1.dump()
4444

4545

4646
def encode_dsa_public_key(key):
@@ -51,9 +51,9 @@ def encode_dsa_public_key(key):
5151
:rtype: bytes
5252
"""
5353

54-
asn1 = DSAPublicKey(int.from_bytes(key[Attribute.VALUE], byteorder='big'))
54+
asn1 = Integer(int.from_bytes(key[Attribute.VALUE], byteorder='big'))
5555

56-
return encoder.encode(asn1)
56+
return asn1.dump()
5757

5858

5959
def decode_dsa_public_key(der):
@@ -66,7 +66,8 @@ def decode_dsa_public_key(der):
6666
:rtype: bytes
6767
"""
6868

69-
asn1, _ = decoder.decode(der, asn1Spec=DSAPublicKey())
69+
asn1 = Integer.load(der)
70+
7071
return biginteger(asn1)
7172

7273

@@ -79,14 +80,9 @@ def encode_dsa_signature(signature):
7980
:rtype: bytes
8081
"""
8182

82-
part = len(signature) // 2
83-
r, s = signature[:part], signature[part:]
83+
asn1 = DSASignature.from_p1363(signature)
8484

85-
asn1 = Dss_Sig_Value()
86-
asn1['r'] = int.from_bytes(r, byteorder='big')
87-
asn1['s'] = int.from_bytes(s, byteorder='big')
88-
89-
return encoder.encode(asn1)
85+
return asn1.dump()
9086

9187

9288
def decode_dsa_signature(der):
@@ -98,13 +94,6 @@ def decode_dsa_signature(der):
9894
:rtype bytes:
9995
"""
10096

101-
asn1, _ = decoder.decode(der, asn1Spec=Dss_Sig_Value())
102-
103-
r = int(asn1['r'])
104-
s = int(asn1['s'])
97+
asn1 = DSASignature.load(der)
10598

106-
# r and s are both 20 bytes
107-
return b''.join((
108-
r.to_bytes(20, byteorder='big'),
109-
s.to_bytes(20, byteorder='big'),
110-
))
99+
return asn1.to_p1363()

pkcs11/util/rsa.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from asn1crypto.keys import RSAPrivateKey, RSAPublicKey
66

7+
from . import biginteger
78
from ..constants import Attribute, ObjectClass, MechanismFlag
89
from ..mechanisms import KeyType
910
from ..defaults import DEFAULT_KEY_CAPABILITIES
@@ -26,14 +27,14 @@ def decode_rsa_private_key(der, capabilities=None):
2627
return {
2728
Attribute.CLASS: ObjectClass.PRIVATE_KEY,
2829
Attribute.KEY_TYPE: KeyType.RSA,
29-
Attribute.MODULUS: key['modulus'],
30-
Attribute.PUBLIC_EXPONENT: key['public_exponent'],
31-
Attribute.PRIVATE_EXPONENT: key['private_exponent'],
32-
Attribute.PRIME_1: key['prime1'],
33-
Attribute.PRIME_2: key['prime2'],
34-
Attribute.EXPONENT_1: key['exponent1'],
35-
Attribute.EXPONENT_2: key['exponent2'],
36-
Attribute.COEFFICIENT: key['coefficient'],
30+
Attribute.MODULUS: biginteger(key['modulus']),
31+
Attribute.PUBLIC_EXPONENT: biginteger(key['public_exponent']),
32+
Attribute.PRIVATE_EXPONENT: biginteger(key['private_exponent']),
33+
Attribute.PRIME_1: biginteger(key['prime1']),
34+
Attribute.PRIME_2: biginteger(key['prime2']),
35+
Attribute.EXPONENT_1: biginteger(key['exponent1']),
36+
Attribute.EXPONENT_2: biginteger(key['exponent2']),
37+
Attribute.COEFFICIENT: biginteger(key['coefficient']),
3738
Attribute.DECRYPT: MechanismFlag.DECRYPT in capabilities,
3839
Attribute.SIGN: MechanismFlag.SIGN in capabilities,
3940
Attribute.UNWRAP: MechanismFlag.UNWRAP in capabilities,
@@ -57,8 +58,8 @@ def decode_rsa_public_key(der, capabilities=None):
5758
return {
5859
Attribute.CLASS: ObjectClass.PUBLIC_KEY,
5960
Attribute.KEY_TYPE: KeyType.RSA,
60-
Attribute.MODULUS: key['modulus'],
61-
Attribute.PUBLIC_EXPONENT: key['public_exponent'],
61+
Attribute.MODULUS: biginteger(key['modulus']),
62+
Attribute.PUBLIC_EXPONENT: biginteger(key['public_exponent']),
6263
Attribute.ENCRYPT: MechanismFlag.ENCRYPT in capabilities,
6364
Attribute.VERIFY: MechanismFlag.VERIFY in capabilities,
6465
Attribute.WRAP: MechanismFlag.WRAP in capabilities,

pkcs11/util/x509.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"""
22
Certificate handling utilities for X.509 (SSL) certificates.
3-
4-
These utilities depend on :mod:`pyasn1` and :mod:`pyasn1_modules`.
53
"""
64

75
from datetime import datetime

tests/test_dh.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
from pkcs11 import Attribute, KeyType, DomainParameters, Mechanism
88
from pkcs11.util.dh import (
9-
decode_x9_42_dh_domain_parameters,
10-
encode_x9_42_dh_domain_parameters,
9+
decode_dh_domain_parameters,
10+
encode_dh_domain_parameters,
1111
encode_dh_public_key,
1212
)
1313

@@ -105,11 +105,10 @@ def test_load_params(self):
105105
""")
106106

107107
params = self.session.create_domain_parameters(
108-
KeyType.X9_42_DH,
109-
decode_x9_42_dh_domain_parameters(PARAMS),
108+
KeyType.DH,
109+
decode_dh_domain_parameters(PARAMS),
110110
local=True)
111111
self.assertIsInstance(params, DomainParameters)
112-
self.assertEqual(len(params[Attribute.SUBPRIME]) * 8, 224)
113112
self.assertEqual(params[Attribute.PRIME][:4],
114113
b'\xAD\x10\x7E\x1E')
115114

@@ -119,7 +118,7 @@ def test_generate_params(self):
119118
self.assertIsInstance(params, DomainParameters)
120119
self.assertEqual(params[Attribute.PRIME_BITS], 512)
121120
self.assertEqual(len(params[Attribute.PRIME]) * 8, 512)
122-
encode_x9_42_dh_domain_parameters(params)
121+
encode_dh_domain_parameters(params)
123122

124123
# Test encoding the public key
125124
public, _ = params.generate_keypair()

tests/test_public_key_external.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ def test_rsa(self):
3636

3737
@requires(Mechanism.ECDSA_SHA1)
3838
def test_ecdsa(self):
39-
from pyasn1_modules.rfc3279 import prime256v1
40-
4139
# A key we generated earlier
4240
self.session.create_domain_parameters(KeyType.EC, {
43-
Attribute.EC_PARAMS: encode_named_curve_parameters(prime256v1),
41+
Attribute.EC_PARAMS: encode_named_curve_parameters('secp256r1'),
4442
}, local=True)\
4543
.generate_keypair()
4644

@@ -61,11 +59,9 @@ def test_ecdsa(self):
6159

6260
@requires(Mechanism.ECDH1_DERIVE)
6361
def test_ecdh(self):
64-
from pyasn1_modules.rfc3279 import prime256v1
65-
6662
# A key we generated earlier
6763
self.session.create_domain_parameters(KeyType.EC, {
68-
Attribute.EC_PARAMS: encode_named_curve_parameters(prime256v1),
64+
Attribute.EC_PARAMS: encode_named_curve_parameters('secp256r1'),
6965
}, local=True)\
7066
.generate_keypair()
7167

0 commit comments

Comments
 (0)