|
|
554a6e |
diff -uNr a/oauth2client/_pure_python_crypt.py b/oauth2client/_pure_python_crypt.py
|
|
|
554a6e |
--- a/oauth2client/_pure_python_crypt.py 2016-10-14 19:53:53.000000000 +0200
|
|
|
554a6e |
+++ b/oauth2client/_pure_python_crypt.py 2018-06-21 15:40:25.216478384 +0200
|
|
|
554a6e |
@@ -23,7 +23,10 @@
|
|
|
554a6e |
from pyasn1_modules import pem
|
|
|
554a6e |
from pyasn1_modules.rfc2459 import Certificate
|
|
|
554a6e |
from pyasn1_modules.rfc5208 import PrivateKeyInfo
|
|
|
554a6e |
-import rsa
|
|
|
554a6e |
+from cryptography.hazmat.primitives import serialization, hashes
|
|
|
554a6e |
+from cryptography.hazmat.primitives.asymmetric import padding
|
|
|
554a6e |
+from cryptography import x509
|
|
|
554a6e |
+from cryptography.hazmat.backends import default_backend
|
|
|
554a6e |
import six
|
|
|
554a6e |
|
|
|
554a6e |
from oauth2client import _helpers
|
|
|
554a6e |
@@ -70,7 +73,8 @@
|
|
|
554a6e |
"""
|
|
|
554a6e |
|
|
|
554a6e |
def __init__(self, pubkey):
|
|
|
554a6e |
- self._pubkey = pubkey
|
|
|
554a6e |
+ self._pubkey = serialization.load_pem_public_key(pubkey,
|
|
|
554a6e |
+ backend=default_backend())
|
|
|
554a6e |
|
|
|
554a6e |
def verify(self, message, signature):
|
|
|
554a6e |
"""Verifies a message against a signature.
|
|
|
554a6e |
@@ -87,8 +91,9 @@
|
|
|
554a6e |
"""
|
|
|
554a6e |
message = _helpers._to_bytes(message, encoding='utf-8')
|
|
|
554a6e |
try:
|
|
|
554a6e |
- return rsa.pkcs1.verify(message, signature, self._pubkey)
|
|
|
554a6e |
- except (ValueError, rsa.pkcs1.VerificationError):
|
|
|
554a6e |
+ return self._pubkey.verify(signature, message, padding.PKCS1v15(),
|
|
|
554a6e |
+ hashes.SHA256())
|
|
|
554a6e |
+ except (ValueError, TypeError, InvalidSignature):
|
|
|
554a6e |
return False
|
|
|
554a6e |
|
|
|
554a6e |
@classmethod
|
|
|
554a6e |
@@ -112,16 +117,18 @@
|
|
|
554a6e |
"""
|
|
|
554a6e |
key_pem = _helpers._to_bytes(key_pem)
|
|
|
554a6e |
if is_x509_cert:
|
|
|
554a6e |
- der = rsa.pem.load_pem(key_pem, 'CERTIFICATE')
|
|
|
554a6e |
+ der = x509.load_pem_x509_certificate(pem_data, default_backend())
|
|
|
554a6e |
asn1_cert, remaining = decoder.decode(der, asn1Spec=Certificate())
|
|
|
554a6e |
if remaining != b'':
|
|
|
554a6e |
raise ValueError('Unused bytes', remaining)
|
|
|
554a6e |
|
|
|
554a6e |
cert_info = asn1_cert['tbsCertificate']['subjectPublicKeyInfo']
|
|
|
554a6e |
key_bytes = _bit_list_to_bytes(cert_info['subjectPublicKey'])
|
|
|
554a6e |
- pubkey = rsa.PublicKey.load_pkcs1(key_bytes, 'DER')
|
|
|
554a6e |
+ pubkey = serialization.load_der_public_key(decoded_key,
|
|
|
554a6e |
+ backend=default_backend())
|
|
|
554a6e |
else:
|
|
|
554a6e |
- pubkey = rsa.PublicKey.load_pkcs1(key_pem, 'PEM')
|
|
|
554a6e |
+ pubkey = serialization.load_pem_public_key(decoded_key,
|
|
|
554a6e |
+ backend=default_backend())
|
|
|
554a6e |
return cls(pubkey)
|
|
|
554a6e |
|
|
|
554a6e |
|
|
|
554a6e |
@@ -134,6 +141,8 @@
|
|
|
554a6e |
|
|
|
554a6e |
def __init__(self, pkey):
|
|
|
554a6e |
self._key = pkey
|
|
|
554a6e |
+ self._pubkey = serialization.load_pem_private_key(pkey,
|
|
|
554a6e |
+ backend=default_backend())
|
|
|
554a6e |
|
|
|
554a6e |
def sign(self, message):
|
|
|
554a6e |
"""Signs a message.
|
|
|
554a6e |
@@ -145,7 +154,7 @@
|
|
|
554a6e |
string, The signature of the message for the given key.
|
|
|
554a6e |
"""
|
|
|
554a6e |
message = _helpers._to_bytes(message, encoding='utf-8')
|
|
|
554a6e |
- return rsa.pkcs1.sign(message, self._key, 'SHA-256')
|
|
|
554a6e |
+ return self._key.sign(message, padding.PKCS1v15(), hashes.SHA256())
|
|
|
554a6e |
|
|
|
554a6e |
@classmethod
|
|
|
554a6e |
def from_string(cls, key, password='notasecret'):
|
|
|
554a6e |
@@ -168,16 +177,19 @@
|
|
|
554a6e |
six.StringIO(key), _PKCS1_MARKER, _PKCS8_MARKER)
|
|
|
554a6e |
|
|
|
554a6e |
if marker_id == 0:
|
|
|
554a6e |
- pkey = rsa.key.PrivateKey.load_pkcs1(key_bytes,
|
|
|
554a6e |
- format='DER')
|
|
|
554a6e |
+ pkey = serialization.load_der_private_key(
|
|
|
554a6e |
+ key_bytes, password=None,
|
|
|
554a6e |
+ backend=default_backend())
|
|
|
554a6e |
+
|
|
|
554a6e |
elif marker_id == 1:
|
|
|
554a6e |
key_info, remaining = decoder.decode(
|
|
|
554a6e |
key_bytes, asn1Spec=_PKCS8_SPEC)
|
|
|
554a6e |
if remaining != b'':
|
|
|
554a6e |
raise ValueError('Unused bytes', remaining)
|
|
|
554a6e |
pkey_info = key_info.getComponentByName('privateKey')
|
|
|
554a6e |
- pkey = rsa.key.PrivateKey.load_pkcs1(pkey_info.asOctets(),
|
|
|
554a6e |
- format='DER')
|
|
|
554a6e |
+ pkey = serialization.load_der_private_key(
|
|
|
554a6e |
+ pkey_info.asOctets(), password=None,
|
|
|
554a6e |
+ backend=default_backend())
|
|
|
554a6e |
else:
|
|
|
554a6e |
raise ValueError('No key could be detected.')
|
|
|
554a6e |
|