-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Split crypt into a package to allow alternative implementations #189
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# Copyright 2016 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Cryptography helpers for verifying and signing messages. | ||
|
||
The simplest way to verify signatures is using :func:`verify_signature`:: | ||
|
||
cert = open('certs.pem').read() | ||
valid = crypt.verify_signature(message, signature, cert) | ||
|
||
If you're going to verify many messages with the same certificate, you can use | ||
:class:`RSAVerifier`:: | ||
|
||
cert = open('certs.pem').read() | ||
verifier = crypt.RSAVerifier.from_string(cert) | ||
valid = verifier.verify(message, signature) | ||
|
||
To sign messages use :class:`RSASigner` with a private key:: | ||
|
||
private_key = open('private_key.pem').read() | ||
signer = crypt.RSASigner(private_key) | ||
signature = signer.sign(message) | ||
""" | ||
|
||
import six | ||
|
||
from google.auth.crypt import base | ||
from google.auth.crypt import rsa | ||
|
||
|
||
__all__ = [ | ||
'RSASigner', | ||
'RSAVerifier', | ||
'Signer', | ||
'Verifier', | ||
] | ||
|
||
# Aliases to maintain the v1.0.0 interface, as the crypt module was split | ||
# into submodules. | ||
Signer = base.Signer | ||
Verifier = base.Verifier | ||
RSASigner = rsa.RSASigner | ||
RSAVerifier = rsa.RSAVerifier | ||
|
||
|
||
def verify_signature(message, signature, certs): | ||
"""Verify an RSA cryptographic signature. | ||
|
||
Checks that the provided ``signature`` was generated from ``bytes`` using | ||
the private key associated with the ``cert``. | ||
|
||
Args: | ||
message (Union[str, bytes]): The plaintext message. | ||
signature (Union[str, bytes]): The cryptographic signature to check. | ||
certs (Union[Sequence, str, bytes]): The certificate or certificates | ||
to use to check the signature. | ||
|
||
Returns: | ||
bool: True if the signature is valid, otherwise False. | ||
""" | ||
if isinstance(certs, (six.text_type, six.binary_type)): | ||
certs = [certs] | ||
|
||
for cert in certs: | ||
verifier = rsa.RSAVerifier.from_string(cert) | ||
if verifier.verify(message, signature): | ||
return True | ||
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# Copyright 2016 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Base classes for cryptographic signers and verifiers.""" | ||
|
||
import abc | ||
|
||
import six | ||
|
||
|
||
@six.add_metaclass(abc.ABCMeta) | ||
class Verifier(object): | ||
"""Abstract base class for crytographic signature verifiers.""" | ||
|
||
@abc.abstractmethod | ||
def verify(self, message, signature): | ||
"""Verifies a message against a cryptographic signature. | ||
|
||
Args: | ||
message (Union[str, bytes]): The message to verify. | ||
signature (Union[str, bytes]): The cryptography signature to check. | ||
|
||
Returns: | ||
bool: True if message was signed by the private key associated | ||
with the public key that this object was constructed with. | ||
""" | ||
# pylint: disable=missing-raises-doc,redundant-returns-doc | ||
# (pylint doesn't recognize that this is abstract) | ||
raise NotImplementedError('Verify must be implemented') | ||
|
||
|
||
@six.add_metaclass(abc.ABCMeta) | ||
class Signer(object): | ||
"""Abstract base class for cryptographic signers.""" | ||
|
||
@abc.abstractproperty | ||
def key_id(self): | ||
"""Optional[str]: The key ID used to identify this private key.""" | ||
raise NotImplementedError('Key id must be implemented') | ||
|
||
@abc.abstractmethod | ||
def sign(self, message): | ||
"""Signs a message. | ||
|
||
Args: | ||
message (Union[str, bytes]): The message to be signed. | ||
|
||
Returns: | ||
bytes: The signature of the message. | ||
""" | ||
# pylint: disable=missing-raises-doc,redundant-returns-doc | ||
# (pylint doesn't recognize that this is abstract) | ||
raise NotImplementedError('Sign must be implemented') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Copyright 2017 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""RSA cryptography signer and verifier.""" | ||
|
||
from google.auth.crypt import _python_rsa | ||
|
||
RSASigner = _python_rsa.RSASigner | ||
RSAVerifier = _python_rsa.RSAVerifier |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.