diff options
-rw-r--r-- | CHANGES | 7 | ||||
-rw-r--r-- | M2Crypto/EVP.py | 6 | ||||
-rw-r--r-- | M2Crypto/SMIME.py | 31 | ||||
-rw-r--r-- | M2Crypto/SSL/Session.py | 5 | ||||
-rw-r--r-- | tests/test_evp.py | 10 | ||||
-rw-r--r-- | tests/test_smime.py | 19 | ||||
-rw-r--r-- | tests/test_ssl.py | 7 |
7 files changed, 61 insertions, 24 deletions
@@ -8,6 +8,13 @@ Pavel Shramov - Reverted a change done in 0.17 to m2urllib2 which changed urls to include host when it should stay as it was +- SMIME.text_crlf and text_crlf_bio were always raising TypeError; fixed +- EVP.load_key and load_key_bio fixed to raise EVP.EVPError and BIO.BIOError + instead of str (str exceptions not allowed in Python 2.6 and later) +- SSL.Session.load_session fixed to raise SSL.SSLError instead of str +- SMIME.load_pkcs7, load_pkcs7_bio, smime_load_pkcs7, smime_load_pkcs7_bio, + text_crlf, text_crlf_bio fixed to raise BIO.BIOError, SMIME.PKCS7_Error and + SMIME.SMIME_Error as appropriate instead of str 0.19.1 - 2008-10-12 ------------------- diff --git a/M2Crypto/EVP.py b/M2Crypto/EVP.py index c9fb91a..5c7f558 100644 --- a/M2Crypto/EVP.py +++ b/M2Crypto/EVP.py @@ -359,11 +359,11 @@ def load_key(file, callback=util.passphrase_callback): """ bio = m2.bio_new_file(file, 'r') if bio is None: - raise Err.get_error() + raise BIO.BIOError(Err.get_error()) cptr = m2.pkey_read_pem(bio, callback) m2.bio_free(bio) if cptr is None: - raise Err.get_error() + raise EVPError(Err.get_error()) return PKey(cptr, 1) def load_key_bio(bio, callback=util.passphrase_callback): @@ -382,7 +382,7 @@ def load_key_bio(bio, callback=util.passphrase_callback): """ cptr = m2.pkey_read_pem(bio._ptr(), callback) if cptr is None: - raise Err.get_error() + raise EVPError(Err.get_error()) return PKey(cptr, 1) def load_key_string(string, callback=util.passphrase_callback): diff --git a/M2Crypto/SMIME.py b/M2Crypto/SMIME.py index 738e683..556cd8f 100644 --- a/M2Crypto/SMIME.py +++ b/M2Crypto/SMIME.py @@ -63,7 +63,7 @@ class PKCS7: def load_pkcs7(p7file): bio = m2.bio_new_file(p7file, 'r') if bio is None: - raise Err.get_error() + raise BIO.BIOError(Err.get_error()) try: p7_ptr = m2.pkcs7_read_bio(bio) @@ -71,21 +71,21 @@ def load_pkcs7(p7file): m2.bio_free(bio) if p7_ptr is None: - raise Err.get_error() + raise PKCS7_Error(Err.get_error()) return PKCS7(p7_ptr, 1) def load_pkcs7_bio(p7_bio): p7_ptr = m2.pkcs7_read_bio(p7_bio._ptr()) if p7_ptr is None: - raise Err.get_error() + raise PKCS7_Error(Err.get_error()) return PKCS7(p7_ptr, 1) def smime_load_pkcs7(p7file): bio = m2.bio_new_file(p7file, 'r') if bio is None: - raise Err.get_error() + raise BIO.BIOError(Err.get_error()) try: p7_ptr, bio_ptr = m2.smime_read_pkcs7(bio) @@ -93,7 +93,7 @@ def smime_load_pkcs7(p7file): m2.bio_free(bio) if p7_ptr is None: - raise Err.get_error() + raise SMIME_Error(Err.get_error()) if bio_ptr is None: return PKCS7(p7_ptr, 1), None else: @@ -103,7 +103,7 @@ def smime_load_pkcs7(p7file): def smime_load_pkcs7_bio(p7_bio): p7_ptr, bio_ptr = m2.smime_read_pkcs7(p7_bio._ptr()) if p7_ptr is None: - raise Err.get_error() + raise SMIME_Error(Err.get_error()) if bio_ptr is None: return PKCS7(p7_ptr, 1), None else: @@ -175,7 +175,7 @@ class SMIME: raise SMIME_Error, 'no recipient certs: use set_x509_stack()' pkcs7 = m2.pkcs7_encrypt(self.x509_stack._ptr(), data_bio._ptr(), self.cipher._ptr(), flags) if pkcs7 is None: - raise SMIME_Error, Err.get_error() + raise SMIME_Error(Err.get_error()) return PKCS7(pkcs7, 1) def decrypt(self, pkcs7, flags=0): @@ -185,7 +185,7 @@ class SMIME: raise SMIME_Error, 'no certificate: load_key() used incorrectly?' blob = m2.pkcs7_decrypt(pkcs7._ptr(), self.pkey._ptr(), self.x509._ptr(), flags) if blob is None: - raise SMIME_Error, Err.get_error() + raise SMIME_Error(Err.get_error()) return blob def sign(self, data_bio, flags=0): @@ -195,13 +195,13 @@ class SMIME: pkcs7 = m2.pkcs7_sign1(self.x509._ptr(), self.pkey._ptr(), self.x509_stack._ptr(), data_bio._ptr(), flags) if pkcs7 is None: - raise SMIME_Error, Err.get_error() + raise SMIME_Error(Err.get_error()) return PKCS7(pkcs7, 1) else: pkcs7 = m2.pkcs7_sign0(self.x509._ptr(), self.pkey._ptr(), data_bio._ptr(), flags) if pkcs7 is None: - raise SMIME_Error, Err.get_error() + raise SMIME_Error(Err.get_error()) return PKCS7(pkcs7, 1) def verify(self, pkcs7, data_bio=None, flags=0): @@ -216,7 +216,7 @@ class SMIME: else: blob = m2.pkcs7_verify1(p7, self.x509_stack._ptr(), self.x509_store._ptr(), data_bio._ptr(), flags) if blob is None: - raise SMIME_Error, Err.get_error() + raise SMIME_Error(Err.get_error()) return blob def write(self, out_bio, pkcs7, data_bio=None, flags=0): @@ -230,17 +230,16 @@ class SMIME: def text_crlf(text): bio_in = BIO.MemoryBuffer(text) bio_out = BIO.MemoryBuffer() - if m2.smime_crlf_copy(bio_in, bio_out): + if m2.smime_crlf_copy(bio_in._ptr(), bio_out._ptr()): return bio_out.read() else: - raise Err.get_error() + raise SMIME_Error(Err.get_error()) def text_crlf_bio(bio_in): bio_out = BIO.MemoryBuffer() - m2.smime_crlf_copy(bio_in, bio_out) - if m2.smime_crlf_copy(bio_in, bio_out): + if m2.smime_crlf_copy(bio_in._ptr(), bio_out._ptr()): return bio_out else: - raise Err.get_error() + raise SMIME_Error(Err.get_error()) diff --git a/M2Crypto/SSL/Session.py b/M2Crypto/SSL/Session.py index 4ea33cf..1edf5b0 100644 --- a/M2Crypto/SSL/Session.py +++ b/M2Crypto/SSL/Session.py @@ -53,7 +53,6 @@ def load_session(pemfile): cptr = m2.ssl_session_read_pem(f.bio_ptr()) f.close() if cptr is None: - raise Err.get_error() + from M2Crypto.SSL import SSLError + raise SSLError(Err.get_error()) return Session(cptr, 1) - - diff --git a/tests/test_evp.py b/tests/test_evp.py index a4234f2..6dd2bdc 100644 --- a/tests/test_evp.py +++ b/tests/test_evp.py @@ -10,7 +10,7 @@ Author: Heikki Toivonen import unittest import cStringIO, sha from binascii import hexlify, unhexlify -from M2Crypto import EVP, RSA, util, Rand, m2 +from M2Crypto import EVP, RSA, util, Rand, m2, BIO from M2Crypto.util import h2b class EVPTestCase(unittest.TestCase): @@ -161,6 +161,14 @@ class EVPTestCase(unittest.TestCase): pubkey.verify_update('test message not') assert pubkey.verify_final(sig) == 0 + def test_load_bad(self): + self.assertRaises(BIO.BIOError, EVP.load_key, + 'thisdoesnotexist-dfgh56789') + self.assertRaises(EVP.EVPError, EVP.load_key, + 'tests/signer.pem') # not a key + self.assertRaises(EVP.EVPError, EVP.load_key_bio, + BIO.MemoryBuffer('no a key')) + class CipherTestCase(unittest.TestCase): def cipher_filter(self, cipher, inf, outf): diff --git a/tests/test_smime.py b/tests/test_smime.py index f79abed..a1f8b91 100644 --- a/tests/test_smime.py +++ b/tests/test_smime.py @@ -6,7 +6,7 @@ Copyright (C) 2006 Open Source Applications Foundation. All Rights Reserved. """ import unittest -from M2Crypto import SMIME, BIO, Rand, X509 +from M2Crypto import SMIME, BIO, Rand, X509, EVP class SMIMETestCase(unittest.TestCase): cleartext = 'some text to manipulate' @@ -16,6 +16,23 @@ class SMIMETestCase(unittest.TestCase): self.signed = self.test_sign() self.encrypted = self.test_encrypt() + def test_load_bad(self): + s = SMIME.SMIME() + self.assertRaises(EVP.EVPError, s.load_key, + 'tests/signer.pem', + 'tests/signer.pem') + + self.assertRaises(BIO.BIOError, SMIME.load_pkcs7, 'nosuchfile-dfg456') + self.assertRaises(SMIME.PKCS7_Error, SMIME.load_pkcs7, 'tests/signer.pem') + self.assertRaises(SMIME.PKCS7_Error, SMIME.load_pkcs7_bio, BIO.MemoryBuffer('no pkcs7')) + + self.assertRaises(SMIME.SMIME_Error, SMIME.smime_load_pkcs7, 'tests/signer.pem') + self.assertRaises(SMIME.SMIME_Error, SMIME.smime_load_pkcs7_bio, BIO.MemoryBuffer('no pkcs7')) + + def test_crlf(self): + self.assertEqual(SMIME.text_crlf('foobar'), 'Content-Type: text/plain\r\n\r\nfoobar') + self.assertEqual(SMIME.text_crlf_bio(BIO.MemoryBuffer('foobar')).read(), 'Content-Type: text/plain\r\n\r\nfoobar') + def test_sign(self): buf = BIO.MemoryBuffer(self.cleartext) s = SMIME.SMIME() diff --git a/tests/test_ssl.py b/tests/test_ssl.py index dc6d57e..489184c 100644 --- a/tests/test_ssl.py +++ b/tests/test_ssl.py @@ -1006,10 +1006,17 @@ class ContextTestCase(unittest.TestCase): assert isinstance(store, X509.X509_Store) +class SessionTestCase(unittest.TestCase): + def test_session_load_bad(self): + self.assertRaises(SSL.SSLError, SSL.Session.load_session, + 'tests/signer.pem') + + def suite(): suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(CheckerTestCase)) suite.addTest(unittest.makeSuite(ContextTestCase)) + suite.addTest(unittest.makeSuite(SessionTestCase)) suite.addTest(unittest.makeSuite(PassSSLClientTestCase)) suite.addTest(unittest.makeSuite(HttpslibSSLClientTestCase)) suite.addTest(unittest.makeSuite(UrllibSSLClientTestCase)) |