|
| 1 | +from __future__ import division |
| 2 | + |
| 3 | +import math |
| 4 | + |
1 | 5 | import six |
2 | 6 |
|
3 | 7 | try: |
4 | 8 | from ecdsa import SigningKey as EcdsaSigningKey, VerifyingKey as EcdsaVerifyingKey |
5 | 9 | except ImportError: |
6 | | - SigningKey = VerifyingKey = None |
7 | | -from ecdsa.util import sigdecode_string, sigencode_string, sigdecode_der, sigencode_der |
| 10 | + EcdsaSigningKey = EcdsaVerifyingKey = None |
8 | 11 |
|
9 | 12 | from jose.backends.base import Key |
10 | 13 | from jose.utils import base64_to_long, long_to_base64 |
|
15 | 18 | from cryptography.hazmat.backends import default_backend |
16 | 19 | from cryptography.hazmat.primitives import hashes, serialization |
17 | 20 | from cryptography.hazmat.primitives.asymmetric import ec, rsa, padding |
| 21 | +from cryptography.hazmat.primitives.asymmetric.utils import decode_dss_signature, encode_dss_signature |
18 | 22 | from cryptography.hazmat.primitives.serialization import load_pem_private_key, load_pem_public_key |
| 23 | +from cryptography.utils import int_from_bytes, int_to_bytes |
19 | 24 | from cryptography.x509 import load_pem_x509_certificate |
20 | 25 |
|
21 | 26 |
|
@@ -94,15 +99,30 @@ def _process_jwk(self, jwk_dict): |
94 | 99 | else: |
95 | 100 | return public.public_key(self.cryptography_backend()) |
96 | 101 |
|
| 102 | + def _sig_component_length(self): |
| 103 | + """Determine the correct serialization length for an encoded signature component. |
| 104 | +
|
| 105 | + This is the number of bytes required to encode the maximum key value. |
| 106 | + """ |
| 107 | + return math.ceil(self.prepared_key.key_size / 8.0) |
| 108 | + |
97 | 109 | def _der_to_asn1(self, der_signature): |
98 | 110 | """Convert signature from DER encoding to ASN1 encoding.""" |
99 | | - order = (2 ** self.prepared_key.curve.key_size) - 1 |
100 | | - return sigencode_string(*sigdecode_der(der_signature, order), order=order) |
| 111 | + r, s = decode_dss_signature(der_signature) |
| 112 | + component_length = self._sig_component_length() |
| 113 | + return int_to_bytes(r, component_length) + int_to_bytes(s, component_length) |
101 | 114 |
|
102 | 115 | def _asn1_to_der(self, asn1_signature): |
103 | 116 | """Convert signature from ASN1 encoding to DER encoding.""" |
104 | | - order = (2 ** self.prepared_key.curve.key_size) - 1 |
105 | | - return sigencode_der(*sigdecode_string(asn1_signature, order), order=order) |
| 117 | + component_length = self._sig_component_length() |
| 118 | + if len(asn1_signature) != int(2 * component_length): |
| 119 | + raise ValueError("Invalid signature") |
| 120 | + |
| 121 | + r_bytes = asn1_signature[:component_length] |
| 122 | + s_bytes = asn1_signature[component_length:] |
| 123 | + r = int_from_bytes(r_bytes, "big") |
| 124 | + s = int_from_bytes(s_bytes, "big") |
| 125 | + return encode_dss_signature(r, s) |
106 | 126 |
|
107 | 127 | def sign(self, msg): |
108 | 128 | if self.hash_alg.digest_size * 8 > self.prepared_key.curve.key_size: |
|
0 commit comments