Skip to content

Commit 0e6e405

Browse files
committed
Python3 fixes, native feature detection, cleanup
1 parent 444d871 commit 0e6e405

4 files changed

Lines changed: 31 additions & 10 deletions

File tree

setup.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import os
2525
import sys
26-
import pip
2726
from setuptools import setup
2827
from setuptools.command.build_ext import build_ext
2928

@@ -71,6 +70,7 @@ def build_extension(self, ext):
7170
package_dir={"":package_dir},
7271

7372
zip_safe=False,
73+
cffi_modules=["./src/wolfssl/_build_ffi.py:ffi"],
7474

7575
keywords="wolfssl, wolfcrypt, security, cryptography",
7676
classifiers=[
@@ -87,7 +87,6 @@ def build_extension(self, ext):
8787
],
8888

8989
setup_requires=["cffi"],
90-
cffi_modules=["./src/wolfssl/_build_ffi.py:ffi"],
9190
install_requires=["cffi"],
9291
test_suite="tests",
9392
tests_require=["tox", "pytest"],

src/wolfssl/__init__.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ def set_ciphers(self, ciphers):
249249
be selected (because compile-time options or other configuration
250250
forbids use of all the specified ciphers), an SSLError will be raised.
251251
"""
252+
cipherBytes = t2b(ciphers)
252253
ret = _lib.wolfSSL_CTX_set_cipher_list(self.native_object,
253-
t2b(ciphers))
254+
_ffi.new("char[]", cipherBytes))
254255

255256
if ret != _SSL_SUCCESS:
256257
raise SSLError("Unable to set cipher list")
@@ -259,8 +260,11 @@ def use_sni(self, server_hostname):
259260
"""
260261
Sets the SNI hostname, wraps native wolfSSL_CTX_UseSNI()
261262
"""
263+
264+
sni = t2b(server_hostname)
265+
262266
ret = _lib.wolfSSL_CTX_UseSNI(self.native_object, 0,
263-
server_hostname, len(server_hostname))
267+
sni, len(sni))
264268

265269
if ret != _SSL_SUCCESS:
266270
raise SSLError("Unable to set wolfSSL CTX SNI")
@@ -421,8 +425,10 @@ def __init__(self, sock=None, keyfile=None, certfile=None,
421425
# match domain name / host name if set in context
422426
if server_hostname is not None:
423427
if self._context.check_hostname:
428+
429+
sni = _ffi.new("char[]", server_hostname.encode("utf-8"))
424430
_lib.wolfSSL_check_domain_name(self.native_object,
425-
server_hostname)
431+
sni)
426432

427433
if connected:
428434
try:
@@ -468,8 +474,11 @@ def use_sni(self, server_hostname):
468474
"""
469475
Sets the SNI hostname, wraps native wolfSSL_UseSNI()
470476
"""
477+
478+
sni = t2b(server_hostname)
479+
471480
ret = _lib.wolfSSL_UseSNI(self.native_object, 0,
472-
server_hostname, len(server_hostname))
481+
sni, len(sni))
473482

474483
if ret != _SSL_SUCCESS:
475484
raise SSLError("Unable to set wolfSSL SNI")

src/wolfssl/_build_ffi.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@
2626
from cffi import FFI
2727
from wolfssl._build_wolfssl import wolfssl_inc_path, wolfssl_lib_path
2828

29+
# open <wolfssl/options.h> header to parse for #define's
30+
# This will throw a FileNotFoundError if not able to find options.h
31+
optionsHeader = wolfssl_inc_path() + "/wolfssl/options.h"
32+
optionsHeaderStr = open(optionsHeader, 'r').read()
33+
34+
# require HAVE_SNI (--enable-sni) in native lib
35+
if '#define HAVE_SNI' not in optionsHeaderStr:
36+
raise SystemExit("wolfSSL needs to be compiled with --enable-sni")
37+
38+
# require OPENSSL_EXTRA (--enable-opensslextra) in native lib
39+
if '#define OPENSSL_EXTRA' not in optionsHeaderStr:
40+
raise SystemExit("wolfSSL needs to be compiled with --enable-opensslextra")
41+
2942
ffi = FFI()
3043

3144
ffi.set_source(
@@ -119,8 +132,8 @@
119132
int wolfSSL_UseSNI(void*, unsigned char, const void*, unsigned short);
120133
int wolfSSL_check_domain_name(void*, const char*);
121134
int wolfSSL_get_alert_history(void*, WOLFSSL_ALERT_HISTORY*);
122-
char* wolfSSL_alert_type_string_long(int);
123-
char* wolfSSL_alert_desc_string_long(int);
135+
const char* wolfSSL_alert_type_string_long(int);
136+
const char* wolfSSL_alert_desc_string_long(int);
124137
125138
/**
126139
* WOLFSSL_X509 functions
@@ -132,4 +145,4 @@
132145
)
133146

134147
if __name__ == "__main__":
135-
ffi.compile(verbose=1)
148+
ffi.compile(verbose=True)

src/wolfssl/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
def t2b(string):
3434
"""
35-
Converts text to bynary.
35+
Converts text to binary.
3636
"""
3737
if isinstance(string, _BINARY_TYPE):
3838
return string

0 commit comments

Comments
 (0)