|
| 1 | +import base64 |
| 2 | +import six |
| 3 | + |
| 4 | +from Cryptodome.Hash import HMAC |
| 5 | +from Cryptodome.PublicKey import RSA |
| 6 | +from Cryptodome.Signature import PKCS1_v1_5 |
| 7 | + |
| 8 | +from .utils import * |
| 9 | + |
| 10 | + |
| 11 | +DEFAULT_SIGN_ALGORITHM = "hmac-sha256" |
| 12 | + |
| 13 | + |
| 14 | +class Signer(object): |
| 15 | + """ |
| 16 | + When using an RSA algo, the secret is a PEM-encoded private key. |
| 17 | + When using an HMAC algo, the secret is the HMAC signing secret. |
| 18 | +
|
| 19 | + Password-protected keyfiles are not supported. |
| 20 | + """ |
| 21 | + def __init__(self, secret, algorithm=None): |
| 22 | + if algorithm is None: |
| 23 | + algorithm = DEFAULT_SIGN_ALGORITHM |
| 24 | + |
| 25 | + assert algorithm in ALGORITHMS, "Unknown algorithm" |
| 26 | + if isinstance(secret, six.string_types): secret = secret.encode("ascii") |
| 27 | + |
| 28 | + self._rsa = None |
| 29 | + self._hash = None |
| 30 | + self.sign_algorithm, self.hash_algorithm = algorithm.split('-') |
| 31 | + |
| 32 | + if self.sign_algorithm == 'rsa': |
| 33 | + try: |
| 34 | + rsa_key = RSA.importKey(secret) |
| 35 | + self._rsa = PKCS1_v1_5.new(rsa_key) |
| 36 | + self._hash = HASHES[self.hash_algorithm] |
| 37 | + except ValueError: |
| 38 | + raise HttpSigException("Invalid key.") |
| 39 | + |
| 40 | + elif self.sign_algorithm == 'hmac': |
| 41 | + self._hash = HMAC.new(secret, digestmod=HASHES[self.hash_algorithm]) |
| 42 | + |
| 43 | + @property |
| 44 | + def algorithm(self): |
| 45 | + return '%s-%s' % (self.sign_algorithm, self.hash_algorithm) |
| 46 | + |
| 47 | + def _sign_rsa(self, data): |
| 48 | + if isinstance(data, six.string_types): data = data.encode("ascii") |
| 49 | + h = self._hash.new() |
| 50 | + h.update(data) |
| 51 | + return self._rsa.sign(h) |
| 52 | + |
| 53 | + def _sign_hmac(self, data): |
| 54 | + if isinstance(data, six.string_types): data = data.encode("ascii") |
| 55 | + hmac = self._hash.copy() |
| 56 | + hmac.update(data) |
| 57 | + return hmac.digest() |
| 58 | + |
| 59 | + def _sign(self, data): |
| 60 | + if isinstance(data, six.string_types): data = data.encode("ascii") |
| 61 | + signed = None |
| 62 | + if self._rsa: |
| 63 | + signed = self._sign_rsa(data) |
| 64 | + elif self._hash: |
| 65 | + signed = self._sign_hmac(data) |
| 66 | + if not signed: |
| 67 | + raise SystemError('No valid encryptor found.') |
| 68 | + return base64.b64encode(signed).decode("ascii") |
| 69 | + |
| 70 | + |
| 71 | +class HeaderSigner(Signer): |
| 72 | + ''' |
| 73 | + Generic object that will sign headers as a dictionary using the http-signature scheme. |
| 74 | + https://github.com/joyent/node-http-signature/blob/master/http_signing.md |
| 75 | +
|
| 76 | + :arg key_id: the mandatory label indicating to the server which secret to use |
| 77 | + :arg secret: a PEM-encoded RSA private key or an HMAC secret (must match the algorithm) |
| 78 | + :arg algorithm: one of the six specified algorithms |
| 79 | + :arg headers: a list of http headers to be included in the signing string, defaulting to ['date']. |
| 80 | + ''' |
| 81 | + def __init__(self, key_id, secret, algorithm=None, headers=None): |
| 82 | + if algorithm is None: |
| 83 | + algorithm = DEFAULT_SIGN_ALGORITHM |
| 84 | + |
| 85 | + super(HeaderSigner, self).__init__(secret=secret, algorithm=algorithm) |
| 86 | + self.headers = headers or ['date'] |
| 87 | + self.signature_template = build_signature_template(key_id, algorithm, headers) |
| 88 | + |
| 89 | + def sign(self, headers, host=None, method=None, path=None): |
| 90 | + """ |
| 91 | + Add Signature Authorization header to case-insensitive header dict. |
| 92 | +
|
| 93 | + headers is a case-insensitive dict of mutable headers. |
| 94 | + host is a override for the 'host' header (defaults to value in headers). |
| 95 | + method is the HTTP method (required when using '(request-target)'). |
| 96 | + path is the HTTP path (required when using '(request-target)'). |
| 97 | + """ |
| 98 | + headers = CaseInsensitiveDict(headers) |
| 99 | + required_headers = self.headers or ['date'] |
| 100 | + signable = generate_message(required_headers, headers, host, method, path) |
| 101 | + |
| 102 | + signature = self._sign(signable) |
| 103 | + headers['authorization'] = self.signature_template % signature |
| 104 | + |
| 105 | + return headers |
| 106 | + |
0 commit comments