1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- """Cryptography helpers for verifying and signing messages .
15+ """Pure-Python RSA cryptography implementation .
1616
1717Uses the ``rsa``, ``pyasn1`` and ``pyasn1_modules`` packages
1818to parse PEM files storing PKCS#1 or PKCS#8 keys as well as
1919certificates. There is no support for p12 files.
20+ """
2021
21- The simplest way to verify signatures is using :func:`verify_signature`::
22-
23- cert = open('certs.pem').read()
24- valid = crypt.verify_signature(message, signature, cert)
25-
26- If you're going to verify many messages with the same certificate, you can use
27- :class:`RSAVerifier`::
28-
29- cert = open('certs.pem').read()
30- verifier = crypt.RSAVerifier.from_string(cert)
31- valid = verifier.verify(message, signature)
32-
33-
34- To sign messages use :class:`RSASigner` with a private key::
35-
36- private_key = open('private_key.pem').read()
37- signer = crypt.RSASigner(private_key)
38- signature = signer.sign(message)
22+ from __future__ import absolute_import
3923
40- """
41- import abc
4224import io
4325import json
4426
5032import six
5133
5234from google .auth import _helpers
35+ from google .auth .crypt import base
5336
5437_POW2 = (128 , 64 , 32 , 16 , 8 , 4 , 2 , 1 )
5538_CERTIFICATE_MARKER = b'-----BEGIN CERTIFICATE-----'
@@ -84,28 +67,7 @@ def _bit_list_to_bytes(bit_list):
8467 return bytes (byte_vals )
8568
8669
87- @six .add_metaclass (abc .ABCMeta )
88- class Verifier (object ):
89- """Abstract base class for crytographic signature verifiers."""
90-
91- @abc .abstractmethod
92- def verify (self , message , signature ):
93- """Verifies a message against a cryptographic signature.
94-
95- Args:
96- message (Union[str, bytes]): The message to verify.
97- signature (Union[str, bytes]): The cryptography signature to check.
98-
99- Returns:
100- bool: True if message was signed by the private key associated
101- with the public key that this object was constructed with.
102- """
103- # pylint: disable=missing-raises-doc,redundant-returns-doc
104- # (pylint doesn't recognize that this is abstract)
105- raise NotImplementedError ('Verify must be implemented' )
106-
107-
108- class RSAVerifier (Verifier ):
70+ class RSAVerifier (base .Verifier ):
10971 """Verifies RSA cryptographic signatures using public keys.
11072
11173 Args:
@@ -116,7 +78,7 @@ class RSAVerifier(Verifier):
11678 def __init__ (self , public_key ):
11779 self ._pubkey = public_key
11880
119- @_helpers .copy_docstring (Verifier )
81+ @_helpers .copy_docstring (base . Verifier )
12082 def verify (self , message , signature ):
12183 message = _helpers .to_bytes (message )
12284 try :
@@ -157,56 +119,7 @@ def from_string(cls, public_key):
157119 return cls (pubkey )
158120
159121
160- def verify_signature (message , signature , certs ):
161- """Verify an RSA cryptographic signature.
162-
163- Checks that the provided ``signature`` was generated from ``bytes`` using
164- the private key associated with the ``cert``.
165-
166- Args:
167- message (Union[str, bytes]): The plaintext message.
168- signature (Union[str, bytes]): The cryptographic signature to check.
169- certs (Union[Sequence, str, bytes]): The certificate or certificates
170- to use to check the signature.
171-
172- Returns:
173- bool: True if the signature is valid, otherwise False.
174- """
175- if isinstance (certs , (six .text_type , six .binary_type )):
176- certs = [certs ]
177-
178- for cert in certs :
179- verifier = RSAVerifier .from_string (cert )
180- if verifier .verify (message , signature ):
181- return True
182- return False
183-
184-
185- @six .add_metaclass (abc .ABCMeta )
186- class Signer (object ):
187- """Abstract base class for cryptographic signers."""
188-
189- @abc .abstractproperty
190- def key_id (self ):
191- """Optional[str]: The key ID used to identify this private key."""
192- raise NotImplementedError ('Key id must be implemented' )
193-
194- @abc .abstractmethod
195- def sign (self , message ):
196- """Signs a message.
197-
198- Args:
199- message (Union[str, bytes]): The message to be signed.
200-
201- Returns:
202- bytes: The signature of the message.
203- """
204- # pylint: disable=missing-raises-doc,redundant-returns-doc
205- # (pylint doesn't recognize that this is abstract)
206- raise NotImplementedError ('Sign must be implemented' )
207-
208-
209- class RSASigner (Signer ):
122+ class RSASigner (base .Signer ):
210123 """Signs messages with an RSA private key.
211124
212125 Args:
@@ -221,11 +134,11 @@ def __init__(self, private_key, key_id=None):
221134 self ._key_id = key_id
222135
223136 @property
224- @_helpers .copy_docstring (Signer )
137+ @_helpers .copy_docstring (base . Signer )
225138 def key_id (self ):
226139 return self ._key_id
227140
228- @_helpers .copy_docstring (Signer )
141+ @_helpers .copy_docstring (base . Signer )
229142 def sign (self , message ):
230143 message = _helpers .to_bytes (message )
231144 return rsa .pkcs1 .sign (message , self ._key , 'SHA-256' )
0 commit comments