-
Notifications
You must be signed in to change notification settings - Fork 446
Swap pycrypto* for pyca/cryptography #385
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
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
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 |
|---|---|---|
|
|
@@ -19,25 +19,13 @@ | |
|
|
||
| from future.backports.urllib.parse import urlencode | ||
|
|
||
| # from Crypto.PublicKey.RSA import importKey | ||
| # from Crypto.Signature import PKCS1_v1_5 | ||
| # from Crypto.Util.asn1 import DerSequence | ||
| # from Crypto.PublicKey import RSA | ||
| # from Crypto.Hash import SHA | ||
| # from Crypto.Hash import SHA224 | ||
| # from Crypto.Hash import SHA256 | ||
| # from Crypto.Hash import SHA384 | ||
| # from Crypto.Hash import SHA512 | ||
|
|
||
| from Cryptodome.PublicKey.RSA import importKey | ||
| from Cryptodome.Signature import PKCS1_v1_5 | ||
| from Cryptodome.Util.asn1 import DerSequence | ||
| from Cryptodome.PublicKey import RSA | ||
| from Cryptodome.Hash import SHA | ||
| from Cryptodome.Hash import SHA224 | ||
| from Cryptodome.Hash import SHA256 | ||
| from Cryptodome.Hash import SHA384 | ||
| from Cryptodome.Hash import SHA512 | ||
| from cryptography.exceptions import InvalidSignature | ||
| from cryptography.hazmat.backends import default_backend | ||
| from cryptography.hazmat.primitives import hashes | ||
| from cryptography.hazmat.primitives.asymmetric import rsa | ||
| from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 | ||
| from cryptography.hazmat.primitives.serialization import load_pem_private_key | ||
| from cryptography.x509 import load_pem_x509_certificate | ||
|
|
||
| from tempfile import NamedTemporaryFile | ||
| from subprocess import Popen | ||
|
|
@@ -87,6 +75,8 @@ | |
| PREFIX1 = "<?xml version='1.0' encoding='UTF-8'?>" | ||
| PREFIX2 = '<?xml version="1.0" encoding="UTF-8"?>' | ||
|
|
||
| backend = default_backend() | ||
|
|
||
|
|
||
| class SigverError(SAMLError): | ||
| pass | ||
|
|
@@ -406,18 +396,10 @@ def active_cert(key): | |
| """ | ||
| try: | ||
| cert_str = pem_format(key) | ||
| try: | ||
| certificate = importKey(cert_str) | ||
| not_before = to_time(str(certificate.get_not_before())) | ||
| not_after = to_time(str(certificate.get_not_after())) | ||
| assert not_before < utc_now() | ||
| assert not_after > utc_now() | ||
| return True | ||
| except: | ||
| cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_str) | ||
| assert cert.has_expired() == 0 | ||
| assert not OpenSSLWrapper().certificate_not_valid_yet(cert) | ||
| return True | ||
| cert = crypto.load_certificate(crypto.FILETYPE_PEM, cert_str) | ||
| assert cert.has_expired() == 0 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. assertion in production code might be optimized out.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm simply replacing an existing assertion. I expect @rohe already knows that about this code. |
||
| assert not OpenSSLWrapper().certificate_not_valid_yet(cert) | ||
| return True | ||
| except AssertionError: | ||
| return False | ||
| except AttributeError: | ||
|
|
@@ -555,19 +537,8 @@ def rsa_eq(key1, key2): | |
|
|
||
|
|
||
| def extract_rsa_key_from_x509_cert(pem): | ||
| # Convert from PEM to DER | ||
| der = ssl.PEM_cert_to_DER_cert(pem.decode('ascii')) | ||
|
|
||
| # Extract subjectPublicKeyInfo field from X.509 certificate (see RFC3280) | ||
| cert = DerSequence() | ||
| cert.decode(der) | ||
| tbsCertificate = DerSequence() | ||
| tbsCertificate.decode(cert[0]) | ||
| subjectPublicKeyInfo = tbsCertificate[6] | ||
|
|
||
| # Initialize RSA key | ||
| rsa_key = RSA.importKey(subjectPublicKeyInfo) | ||
| return rsa_key | ||
| cert = load_pem_x509_certificate(pem, backend) | ||
| return cert.public_key() | ||
|
|
||
|
|
||
| def pem_format(key): | ||
|
|
@@ -576,7 +547,7 @@ def pem_format(key): | |
|
|
||
|
|
||
| def import_rsa_key_from_file(filename): | ||
| return RSA.importKey(read_file(filename, 'r')) | ||
| return load_pem_private_key(read_file(filename, 'rb'), None, backend) | ||
|
|
||
|
|
||
| def parse_xmlsec_output(output): | ||
|
|
@@ -622,25 +593,28 @@ def sign(self, msg, key=None): | |
| if key is None: | ||
| key = self.key | ||
|
|
||
| h = self.digest.new(msg) | ||
| signer = PKCS1_v1_5.new(key) | ||
| return signer.sign(h) | ||
| return key.sign(msg, PKCS1v15(), self.digest) | ||
|
|
||
| def verify(self, msg, sig, key=None): | ||
| if key is None: | ||
| key = self.key | ||
|
|
||
| h = self.digest.new(msg) | ||
| verifier = PKCS1_v1_5.new(key) | ||
| return verifier.verify(h, sig) | ||
| try: | ||
| if isinstance(key, rsa.RSAPrivateKey): | ||
| key = key.public_key() | ||
|
|
||
| key.verify(sig, msg, PKCS1v15(), self.digest) | ||
| return True | ||
| except InvalidSignature: | ||
| return False | ||
|
|
||
|
|
||
| SIGNER_ALGS = { | ||
| SIG_RSA_SHA1: RSASigner(SHA), | ||
| SIG_RSA_SHA224: RSASigner(SHA224), | ||
| SIG_RSA_SHA256: RSASigner(SHA256), | ||
| SIG_RSA_SHA384: RSASigner(SHA384), | ||
| SIG_RSA_SHA512: RSASigner(SHA512), | ||
| SIG_RSA_SHA1: RSASigner(hashes.SHA1()), | ||
| SIG_RSA_SHA224: RSASigner(hashes.SHA224()), | ||
| SIG_RSA_SHA256: RSASigner(hashes.SHA256()), | ||
| SIG_RSA_SHA384: RSASigner(hashes.SHA384()), | ||
| SIG_RSA_SHA512: RSASigner(hashes.SHA512()), | ||
| } | ||
|
|
||
| REQ_ORDER = ["SAMLRequest", "RelayState", "SigAlg"] | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what if the certificate contains umlauts e.g. "ä"? Are they encoded in a special format?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cert_stris a PEM formatted string here, which is guaranteed to be ascii. The requirement for this is becauseload_pem_x509_certificatetakes bytes, not text.