@@ -139,6 +139,7 @@ def from_string(key, password='notasecret'):
139
139
from Crypto .PublicKey import RSA
140
140
from Crypto .Hash import SHA256
141
141
from Crypto .Signature import PKCS1_v1_5
142
+ from Crypto .Util .asn1 import DerSequence
142
143
143
144
144
145
class PyCryptoVerifier (object ):
@@ -167,7 +168,10 @@ def verify(self, message, signature):
167
168
return PKCS1_v1_5 .new (self ._pubkey ).verify (
168
169
SHA256 .new (message ), signature )
169
170
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
171
175
172
176
@staticmethod
173
177
def from_string (key_pem , is_x509_cert ):
@@ -180,14 +184,15 @@ def from_string(key_pem, is_x509_cert):
180
184
181
185
Returns:
182
186
Verifier instance.
183
-
184
- Raises:
185
- NotImplementedError if is_x509_cert is true.
186
187
"""
187
188
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 ])
191
196
else :
192
197
pubkey = RSA .importKey (key_pem )
193
198
return PyCryptoVerifier (pubkey )
0 commit comments