11"""
22Key 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
109from . import biginteger
1110from ..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
4646def 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
5959def 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
9288def 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 ()
0 commit comments