__init__.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2016 Google LLC
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Cryptography helpers for verifying and signing messages.
  15. The simplest way to verify signatures is using :func:`verify_signature`::
  16. cert = open('certs.pem').read()
  17. valid = crypt.verify_signature(message, signature, cert)
  18. If you're going to verify many messages with the same certificate, you can use
  19. :class:`RSAVerifier`::
  20. cert = open('certs.pem').read()
  21. verifier = crypt.RSAVerifier.from_string(cert)
  22. valid = verifier.verify(message, signature)
  23. To sign messages use :class:`RSASigner` with a private key::
  24. private_key = open('private_key.pem').read()
  25. signer = crypt.RSASigner.from_string(private_key)
  26. signature = signer.sign(message)
  27. The code above also works for :class:`ES256Signer` and :class:`ES256Verifier`.
  28. Note that these two classes are only available if your `cryptography` dependency
  29. version is at least 1.4.0.
  30. """
  31. from google.auth.crypt import base
  32. from google.auth.crypt import rsa
  33. try:
  34. from google.auth.crypt import es256
  35. except ImportError: # pragma: NO COVER
  36. es256 = None # type: ignore
  37. if es256 is not None: # pragma: NO COVER
  38. __all__ = [
  39. "ES256Signer",
  40. "ES256Verifier",
  41. "RSASigner",
  42. "RSAVerifier",
  43. "Signer",
  44. "Verifier",
  45. ]
  46. else: # pragma: NO COVER
  47. __all__ = ["RSASigner", "RSAVerifier", "Signer", "Verifier"]
  48. # Aliases to maintain the v1.0.0 interface, as the crypt module was split
  49. # into submodules.
  50. Signer = base.Signer
  51. Verifier = base.Verifier
  52. RSASigner = rsa.RSASigner
  53. RSAVerifier = rsa.RSAVerifier
  54. if es256 is not None: # pragma: NO COVER
  55. ES256Signer = es256.ES256Signer
  56. ES256Verifier = es256.ES256Verifier
  57. def verify_signature(message, signature, certs, verifier_cls=rsa.RSAVerifier):
  58. """Verify an RSA or ECDSA cryptographic signature.
  59. Checks that the provided ``signature`` was generated from ``bytes`` using
  60. the private key associated with the ``cert``.
  61. Args:
  62. message (Union[str, bytes]): The plaintext message.
  63. signature (Union[str, bytes]): The cryptographic signature to check.
  64. certs (Union[Sequence, str, bytes]): The certificate or certificates
  65. to use to check the signature.
  66. verifier_cls (Optional[~google.auth.crypt.base.Signer]): Which verifier
  67. class to use for verification. This can be used to select different
  68. algorithms, such as RSA or ECDSA. Default value is :class:`RSAVerifier`.
  69. Returns:
  70. bool: True if the signature is valid, otherwise False.
  71. """
  72. if isinstance(certs, (str, bytes)):
  73. certs = [certs]
  74. for cert in certs:
  75. verifier = verifier_cls.from_string(cert)
  76. if verifier.verify(message, signature):
  77. return True
  78. return False