Browse Source

fix a potential BOF

auth
clowwindy 10 years ago
parent
commit
81223902d0
  1. 12
      shadowsocks/crypto/ctypes_openssl.py

12
shadowsocks/crypto/ctypes_openssl.py

@ -26,6 +26,8 @@ __all__ = ['ciphers']
loaded = False loaded = False
buf_size = 2048
def load_openssl(): def load_openssl():
global loaded, libcrypto, CDLL, c_char_p, c_int, c_long, byref,\ global loaded, libcrypto, CDLL, c_char_p, c_int, c_long, byref,\
@ -50,7 +52,7 @@ def load_openssl():
libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,) libcrypto.EVP_CIPHER_CTX_cleanup.argtypes = (c_void_p,)
libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p,) libcrypto.EVP_CIPHER_CTX_free.argtypes = (c_void_p,)
buf = create_string_buffer(65536) buf = create_string_buffer(buf_size)
loaded = True loaded = True
@ -87,10 +89,14 @@ class CtypesCrypto(object):
raise Exception('can not initialize cipher context') raise Exception('can not initialize cipher context')
def update(self, data): def update(self, data):
global buf_size, buf
cipher_out_len = c_long(0) cipher_out_len = c_long(0)
l = len(data)
if buf_size < l:
buf_size = l * 2
buf = create_string_buffer(buf_size)
libcrypto.EVP_CipherUpdate(self._ctx, byref(buf), libcrypto.EVP_CipherUpdate(self._ctx, byref(buf),
byref(cipher_out_len), c_char_p(data), byref(cipher_out_len), c_char_p(data), l)
len(data))
# buf is copied to a str object when we access buf.raw # buf is copied to a str object when we access buf.raw
return buf.raw[:cipher_out_len.value] return buf.raw[:cipher_out_len.value]

Loading…
Cancel
Save