-
Notifications
You must be signed in to change notification settings - Fork 322
feat: Implement ES256 for JWT verification #340
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
68703f6
feat: Implement EC256 for JWT verification
theacodes ea08a7a
Fix name
theacodes ffc4f9c
resolve conflict with master
arithmetic1728 7930fda
update dependency for docs
arithmetic1728 d4b7d80
fix exports/doc, add es256 jwt test
arithmetic1728 e6b906b
fix test names
arithmetic1728 bc0fc97
Merge branch 'master' into add-ec256-crypto
bshaffer fc22ec3
add doc
arithmetic1728 c9b6e57
Merge branch 'master' into add-ec256-crypto
arithmetic1728 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 hidden or 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,7 @@ | ||
google.auth.crypt.es256 module | ||
============================== | ||
|
||
.. automodule:: google.auth.crypt.es256 | ||
:members: | ||
:inherited-members: | ||
:show-inheritance: |
This file contains hidden or 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 |
---|---|---|
|
@@ -12,4 +12,5 @@ Submodules | |
.. toctree:: | ||
|
||
google.auth.crypt.base | ||
google.auth.crypt.es256 | ||
google.auth.crypt.rsa |
This file contains hidden or 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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
cryptography | ||
sphinx-docstring-typing | ||
urllib3 | ||
requests | ||
|
This file contains hidden or 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 hidden or 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 hidden or 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,145 @@ | ||
# 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. | ||
|
||
"""ECDSA (ES256) verifier and signer that use the ``cryptography`` library. | ||
""" | ||
|
||
import cryptography.exceptions | ||
from cryptography.hazmat import backends | ||
from cryptography.hazmat.primitives import hashes | ||
from cryptography.hazmat.primitives import serialization | ||
from cryptography.hazmat.primitives.asymmetric import ec | ||
from cryptography.hazmat.primitives.asymmetric import padding | ||
import cryptography.x509 | ||
import pkg_resources | ||
|
||
from google.auth import _helpers | ||
from google.auth.crypt import base | ||
|
||
_IMPORT_ERROR_MSG = ( | ||
"cryptography>=1.4.0 is required to use cryptography-based ECDSA " "algorithms" | ||
) | ||
|
||
try: # pragma: NO COVER | ||
release = pkg_resources.get_distribution("cryptography").parsed_version | ||
if release < pkg_resources.parse_version("1.4.0"): | ||
raise ImportError(_IMPORT_ERROR_MSG) | ||
except pkg_resources.DistributionNotFound: # pragma: NO COVER | ||
raise ImportError(_IMPORT_ERROR_MSG) | ||
|
||
|
||
_CERTIFICATE_MARKER = b"-----BEGIN CERTIFICATE-----" | ||
_BACKEND = backends.default_backend() | ||
_PADDING = padding.PKCS1v15() | ||
|
||
|
||
class ES256Verifier(base.Verifier): | ||
"""Verifies ECDSA cryptographic signatures using public keys. | ||
|
||
Args: | ||
public_key ( | ||
cryptography.hazmat.primitives.asymmetric.ec.ECDSAPublicKey): | ||
The public key used to verify signatures. | ||
""" | ||
|
||
def __init__(self, public_key): | ||
self._pubkey = public_key | ||
|
||
@_helpers.copy_docstring(base.Verifier) | ||
def verify(self, message, signature): | ||
message = _helpers.to_bytes(message) | ||
try: | ||
self._pubkey.verify(signature, message, ec.ECDSA(hashes.SHA256())) | ||
return True | ||
except (ValueError, cryptography.exceptions.InvalidSignature): | ||
return False | ||
|
||
@classmethod | ||
def from_string(cls, public_key): | ||
"""Construct an Verifier instance from a public key or public | ||
certificate string. | ||
|
||
Args: | ||
public_key (Union[str, bytes]): The public key in PEM format or the | ||
x509 public key certificate. | ||
|
||
Returns: | ||
Verifier: The constructed verifier. | ||
|
||
Raises: | ||
ValueError: If the public key can't be parsed. | ||
""" | ||
public_key_data = _helpers.to_bytes(public_key) | ||
|
||
if _CERTIFICATE_MARKER in public_key_data: | ||
cert = cryptography.x509.load_pem_x509_certificate( | ||
public_key_data, _BACKEND | ||
) | ||
pubkey = cert.public_key() | ||
|
||
else: | ||
pubkey = serialization.load_pem_public_key(public_key_data, _BACKEND) | ||
|
||
return cls(pubkey) | ||
|
||
|
||
class ES256Signer(base.Signer, base.FromServiceAccountMixin): | ||
"""Signs messages with an ECDSA private key. | ||
|
||
Args: | ||
private_key ( | ||
cryptography.hazmat.primitives.asymmetric.ec.ECDSAPrivateKey): | ||
The private key to sign with. | ||
key_id (str): Optional key ID used to identify this private key. This | ||
can be useful to associate the private key with its associated | ||
public key or certificate. | ||
""" | ||
|
||
def __init__(self, private_key, key_id=None): | ||
self._key = private_key | ||
self._key_id = key_id | ||
|
||
@property | ||
@_helpers.copy_docstring(base.Signer) | ||
def key_id(self): | ||
return self._key_id | ||
|
||
@_helpers.copy_docstring(base.Signer) | ||
def sign(self, message): | ||
message = _helpers.to_bytes(message) | ||
return self._key.sign(message, ec.ECDSA(hashes.SHA256())) | ||
|
||
@classmethod | ||
def from_string(cls, key, key_id=None): | ||
"""Construct a RSASigner from a private key in PEM format. | ||
|
||
Args: | ||
key (Union[bytes, str]): Private key in PEM format. | ||
key_id (str): An optional key id used to identify the private key. | ||
|
||
Returns: | ||
google.auth.crypt._cryptography_rsa.RSASigner: The | ||
constructed signer. | ||
|
||
Raises: | ||
ValueError: If ``key`` is not ``bytes`` or ``str`` (unicode). | ||
UnicodeDecodeError: If ``key`` is ``bytes`` but cannot be decoded | ||
into a UTF-8 ``str``. | ||
ValueError: If ``cryptography`` "Could not deserialize key data." | ||
""" | ||
key = _helpers.to_bytes(key) | ||
private_key = serialization.load_pem_private_key( | ||
key, password=None, backend=_BACKEND | ||
) | ||
return cls(private_key, key_id=key_id) |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.