Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add message digest BIO filters.

These BIO filters calculate message digests of data read or written
through the filter to the sink.
  • Loading branch information...
commit 75a5aa01d6a4d34b97fea5cfeda0c652383d2080 1 parent 342c26c
Aaron Iles authored
Showing with 62 additions and 7 deletions.
  1. +37 −1 tests/api/test_bio_filter.py
  2. +25 −6 tls/api/bio.py
View
38 tests/api/test_bio_filter.py
@@ -2,7 +2,7 @@
import ctypes
import unittest
-from tls.api import bio
+from tls.api import bio, digest
class BioFilter:
@@ -42,3 +42,39 @@ class TestZlibFilter(BioFilter, unittest.TestCase):
output = b'x\x9c\xf3p\xf5\xf1\xf1W\x08\xf7\x0f\xf2q\x01\x00\x12\x8b\x03\x1d'
method = bio.BIO_f_zlib()
+
+
+class HashFilter(BioFilter):
+
+ input = b'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'
+ output = input
+ method = bio.BIO_f_md()
+
+ def setUp(self):
+ BioFilter.setUp(self)
+ bio.BIO_set_md(self.filter, self.md)
+
+ def test_filter(self):
+ BioFilter.test_filter(self)
+ buf = bytes(digest.EVP_MD_size(self.md))
+ bio.BIO_gets(self.filter, buf, len(buf))
+ hash_value = ''.join('{0:02x}'.format(v) for v in buf)
+ self.assertEqual(hash_value, self.hash)
+
+
+class TestMD5Filter(HashFilter, unittest.TestCase):
+
+ hash = '8215ef0796a20bcaaae116d3876c664a'
+ md = digest.EVP_md5()
+
+
+class TestSHA1Filter(HashFilter, unittest.TestCase):
+
+ hash = '84983e441c3bd26ebaae4aa1f95129e5e54670f1'
+ md = digest.EVP_sha1()
+
+
+class TestSHA256Filter(HashFilter, unittest.TestCase):
+
+ hash = '248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1'
+ md = digest.EVP_sha256()
View
31 tls/api/bio.py
@@ -98,18 +98,32 @@
# BIO base64
prototype_func('BIO_f_base64', c_method_p, None)
+# BIO message digests
+prototype_func('BIO_f_md', c_method_p, None)
+
# BIO macros
-def _bio_ctrl_macro(symbol, errcheck=lambda r, f, a: r, words=1):
+def _bio_ctrl_macro(symbol, errcheck=lambda r, f, a: r, template='void', words=1):
"""Create function definition for BIO_ctrl calling macro."""
- template = """
-def BIO_{0}(bio, larg=0, parg=None):
- args = (bio, {1}, larg, parg)
+ void = """
+def BIO_{0}(bio):
+ args = (bio, {1}, 0, None)
+ return errcheck(BIO_ctrl(*args), BIO_ctrl, args)
+"""
+ larg = """
+def BIO_{0}(bio, larg):
+ args = (bio, {1}, larg, None)
return errcheck(BIO_ctrl(*args), BIO_ctrl, args)
"""
+ parg = """
+def BIO_{0}(bio, parg):
+ args = (bio, {1}, 0, parg)
+ return errcheck(BIO_ctrl(*args), BIO_ctrl, args)
+"""
+ templates = {'void': void, 'long': larg, 'pointer': parg}
name = '_'.join(s.lower() for s in symbol.split('_')[-words:])
constant = getattr(tls.api.constant, symbol)
- statement = template.format(name, constant)
+ statement = templates[template].format(name, constant)
namespace = dict(locals())
namespace.update(globals())
exec(statement, namespace, globals())
@@ -122,5 +136,10 @@ def BIO_{0}(bio, larg=0, parg=None):
_bio_ctrl_macro('BIO_CTRL_PENDING')
_bio_ctrl_macro('BIO_CTRL_WPENDING')
-_bio_ctrl_macro('BIO_C_FILE_SEEK', err_neg)
+_bio_ctrl_macro('BIO_C_FILE_SEEK', err_neg, template='long')
_bio_ctrl_macro('BIO_C_FILE_TELL', err_neg)
+
+_bio_ctrl_macro('BIO_C_GET_MD', template='pointer', words=2)
+_bio_ctrl_macro('BIO_C_SET_MD', template='pointer', words=2)
+_bio_ctrl_macro('BIO_C_GET_MD_CTX', template='pointer', words=3)
+_bio_ctrl_macro('BIO_C_SET_MD_CTX', template='pointer', words=3)
Please sign in to comment.
Something went wrong with that request. Please try again.