Skip to content
This repository was archived by the owner on Apr 13, 2024. It is now read-only.

Commit 619a13c

Browse files
committed
Make PSS salt configurable
In order to be able to decode the PSS message, the salt length need to be known.
1 parent 1e01f5c commit 619a13c

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

httpsig/sign.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111

1212
DEFAULT_SIGN_ALGORITHM = "hs2019"
13+
DEFAULT_SALT_LENGTH = 20
1314

1415

1516
class Signer(object):
@@ -19,9 +20,11 @@ class Signer(object):
1920
2021
Password-protected keyfiles are not supported.
2122
"""
22-
def __init__(self, secret, algorithm=None, sign_algorithm=None):
23+
def __init__(self, secret, algorithm=None, sign_algorithm=None, salt_length=None):
2324
if algorithm is None:
2425
algorithm = DEFAULT_SIGN_ALGORITHM
26+
if salt_length is None:
27+
salt_length = DEFAULT_SALT_LENGTH
2528

2629
assert algorithm in ALGORITHMS, "Unknown algorithm"
2730
assert sign_algorithm is None or sign_algorithm in SIGN_ALGORITHMS, "Unsupported digital signature algorithm"
@@ -58,7 +61,7 @@ def __init__(self, secret, algorithm=None, sign_algorithm=None):
5861
elif self.sign_algorithm == "PSS":
5962
try:
6063
rsa_key = RSA.importKey(secret)
61-
self._rsa = PKCS1_PSS.new(rsa_key)
64+
self._rsa = PKCS1_PSS.new(rsa_key, saltLen=salt_length)
6265
self._hash = HASHES[self.hash_algorithm]
6366
except ValueError:
6467
raise HttpSigException("Invalid key.")
@@ -100,18 +103,19 @@ class HeaderSigner(Signer):
100103
to use
101104
:arg secret: a PEM-encoded RSA private key or an HMAC secret (must
102105
match the algorithm)
103-
:arg algorithm: one of the seven specified algorithms
104-
:arg sign_algorithm: required for 'hs2019' algorithm. Sign algorithm for the secret
105-
:arg headers: a list of http headers to be included in the signing
106+
:param algorithm: one of the seven specified algorithms
107+
:param sign_algorithm: required for 'hs2019' algorithm. Sign algorithm for the secret
108+
:param sign_algorithm: Custom salt length for 'hs2019' and 'PSS' sign algorithm.
109+
:param headers: a list of http headers to be included in the signing
106110
string, defaulting to ['date'].
107-
:arg sign_header: header used to include signature, defaulting to
111+
:param sign_header: header used to include signature, defaulting to
108112
'authorization'.
109113
"""
110-
def __init__(self, key_id, secret, algorithm=None, sign_algorithm=None, headers=None, sign_header='authorization'):
114+
def __init__(self, key_id, secret, algorithm=None, sign_algorithm=None, salt_length=None, headers=None, sign_header='authorization'):
111115
if algorithm is None:
112116
algorithm = DEFAULT_SIGN_ALGORITHM
113117

114-
super(HeaderSigner, self).__init__(secret=secret, algorithm=algorithm, sign_algorithm=sign_algorithm)
118+
super(HeaderSigner, self).__init__(secret=secret, algorithm=algorithm, sign_algorithm=sign_algorithm, salt_length=salt_length)
115119
self.headers = headers or ['date']
116120
self.signature_template = build_signature_template(
117121
key_id, algorithm, headers, sign_header)

httpsig/verify.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class HeaderVerifier(Verifier):
5252
"""
5353

5454
def __init__(self, headers, secret, required_headers=None, method=None,
55-
path=None, host=None, sign_header='authorization', sign_algorithm=None):
55+
path=None, host=None, sign_header='authorization', sign_algorithm=None, salt_length=None):
5656
"""
5757
Instantiate a HeaderVerifier object.
5858
@@ -73,6 +73,7 @@ def __init__(self, headers, secret, required_headers=None, method=None,
7373
Default is 'authorization'.
7474
:param sign_algorithm: Required for 'hs2019' algorithm, specifies the
7575
digital signature algorithm (derived from keyId) to use.
76+
:param sign_algorithm: Custom salt length for 'hs2019' and 'PSS' sign algorithm.
7677
"""
7778
required_headers = required_headers or ['date']
7879
self.headers = CaseInsensitiveDict(headers)
@@ -93,11 +94,11 @@ def __init__(self, headers, secret, required_headers=None, method=None,
9394

9495
if self.auth_dict['algorithm'] != DEFAULT_SIGN_ALGORITHM:
9596
print("Algorithm: {} is deprecated please update to {}".format(self.auth_dict['algorithm'], DEFAULT_SIGN_ALGORITHM))
96-
elif self.auth_dict['algorithm'] == DEFAULT_SIGN_ALGORITHM and self.sign_algorithm is None:
97+
elif self.auth_dict['algorithm'] == DEFAULT_SIGN_ALGORITHM and sign_algorithm is None:
9798
raise HttpSigException("Required sign algorithm for {} algorithm not set".format(DEFAULT_SIGN_ALGORITHM))
9899

99100
super(HeaderVerifier, self).__init__(
100-
secret, algorithm=self.auth_dict['algorithm'], sign_algorithm=sign_algorithm)
101+
secret, algorithm=self.auth_dict['algorithm'], sign_algorithm=sign_algorithm, salt_length=salt_length)
101102

102103
def verify(self):
103104
"""

0 commit comments

Comments
 (0)