Skip to content

Commit fd6edb2

Browse files
committed
Support X509 pem cert and signature verification using PyCrypto.
1 parent 74264d2 commit fd6edb2

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

oauth2client/crypt.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ def from_string(key, password='notasecret'):
139139
from Crypto.PublicKey import RSA
140140
from Crypto.Hash import SHA256
141141
from Crypto.Signature import PKCS1_v1_5
142+
from Crypto.Util.asn1 import DerSequence
142143

143144

144145
class PyCryptoVerifier(object):
@@ -167,7 +168,10 @@ def verify(self, message, signature):
167168
return PKCS1_v1_5.new(self._pubkey).verify(
168169
SHA256.new(message), signature)
169170
except:
170-
return False
171+
lsignature = long(signature.encode('hex'), 16)
172+
hexsig = '%064x' % self._pubkey.encrypt(lsignature, '')[0]
173+
local_hash = SHA256.new(message).hexdigest()
174+
return hexsig[-64:] == local_hash
171175

172176
@staticmethod
173177
def from_string(key_pem, is_x509_cert):
@@ -180,14 +184,15 @@ def from_string(key_pem, is_x509_cert):
180184
181185
Returns:
182186
Verifier instance.
183-
184-
Raises:
185-
NotImplementedError if is_x509_cert is true.
186187
"""
187188
if is_x509_cert:
188-
raise NotImplementedError(
189-
'X509 certs are not supported by the PyCrypto library. '
190-
'Try using PyOpenSSL if native code is an option.')
189+
pemLines = key_pem.replace(' ', '').split()
190+
certDer = _urlsafe_b64decode(''.join(pemLines[1:-1]))
191+
certSeq = DerSequence()
192+
certSeq.decode(certDer)
193+
tbsSeq = DerSequence()
194+
tbsSeq.decode(certSeq[0])
195+
pubkey = RSA.importKey(tbsSeq[6])
191196
else:
192197
pubkey = RSA.importKey(key_pem)
193198
return PyCryptoVerifier(pubkey)

0 commit comments

Comments
 (0)