Skip to content

Commit 7d5a3bf

Browse files
reaperhulkalex
authored andcommitted
Hypothetically fixes pyOpenSSL tests (#805)
* fix openssl CLI testing for 1.1.1 * various 1.1.1 related fixes some of which are just admitting TLS 1.3 is fundamentally different and pinning the tests to TLS 1.2 * flake8 fixes * allow travis_infra env var through * fix twisted
1 parent 37e6022 commit 7d5a3bf

File tree

5 files changed

+52
-32
lines changed

5 files changed

+52
-32
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def find_meta(meta):
4949
read_file("README.rst") + "\n\n" +
5050
"Release Information\n" +
5151
"===================\n\n" +
52-
re.search("(\d{2}.\d.\d \(.*?\)\n.*?)\n\n\n----\n",
52+
re.search(r"(\d{2}.\d.\d \(.*?\)\n.*?)\n\n\n----\n",
5353
read_file("CHANGELOG.rst"), re.S).group(1) +
5454
"\n\n`Full changelog " +
5555
"<{uri}en/stable/changelog.html>`_.\n\n"

src/OpenSSL/SSL.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,19 @@ def set_cipher_list(self, cipher_list):
11851185
_openssl_assert(
11861186
_lib.SSL_CTX_set_cipher_list(self._context, cipher_list) == 1
11871187
)
1188+
# In OpenSSL 1.1.1 setting the cipher list will always return TLS 1.3
1189+
# ciphers even if you pass an invalid cipher. Applications (like
1190+
# Twisted) have tests that depend on an error being raised if an
1191+
# invalid cipher string is passed, but without the following check
1192+
# for the TLS 1.3 specific cipher suites it would never error.
1193+
tmpconn = Connection(self, None)
1194+
_openssl_assert(
1195+
tmpconn.get_cipher_list() != [
1196+
'TLS_AES_256_GCM_SHA384',
1197+
'TLS_CHACHA20_POLY1305_SHA256',
1198+
'TLS_AES_128_GCM_SHA256'
1199+
]
1200+
)
11881201

11891202
def set_client_ca_list(self, certificate_authorities):
11901203
"""

tests/test_crypto.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,20 +3156,20 @@ def test_export_pem(self):
31563156
representing a serial number, a revoked reason, and certificate issuer
31573157
information.
31583158
"""
3159-
crl = self._get_crl()
31603159
# PEM format
3161-
dumped_crl = crl.export(
3160+
dumped_crl = self._get_crl().export(
31623161
self.cert, self.pkey, days=20, digest=b"sha256"
31633162
)
3164-
text = _runopenssl(dumped_crl, b"crl", b"-noout", b"-text")
3165-
3166-
# These magic values are based on the way the CRL above was constructed
3167-
# and with what certificate it was exported.
3168-
text.index(b'Serial Number: 03AB')
3169-
text.index(b'Superseded')
3170-
text.index(
3171-
b'Issuer: /C=US/ST=IL/L=Chicago/O=Testing/CN=Testing Root CA'
3172-
)
3163+
crl = x509.load_pem_x509_crl(dumped_crl, backend)
3164+
revoked = crl.get_revoked_certificate_by_serial_number(0x03AB)
3165+
assert revoked is not None
3166+
assert crl.issuer == x509.Name([
3167+
x509.NameAttribute(x509.NameOID.COUNTRY_NAME, u"US"),
3168+
x509.NameAttribute(x509.NameOID.STATE_OR_PROVINCE_NAME, u"IL"),
3169+
x509.NameAttribute(x509.NameOID.LOCALITY_NAME, u"Chicago"),
3170+
x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, u"Testing"),
3171+
x509.NameAttribute(x509.NameOID.COMMON_NAME, u"Testing Root CA"),
3172+
])
31733173

31743174
def test_export_der(self):
31753175
"""
@@ -3180,17 +3180,19 @@ def test_export_der(self):
31803180
crl = self._get_crl()
31813181

31823182
# DER format
3183-
dumped_crl = crl.export(
3183+
dumped_crl = self._get_crl().export(
31843184
self.cert, self.pkey, FILETYPE_ASN1, digest=b"md5"
31853185
)
3186-
text = _runopenssl(
3187-
dumped_crl, b"crl", b"-noout", b"-text", b"-inform", b"DER"
3188-
)
3189-
text.index(b'Serial Number: 03AB')
3190-
text.index(b'Superseded')
3191-
text.index(
3192-
b'Issuer: /C=US/ST=IL/L=Chicago/O=Testing/CN=Testing Root CA'
3193-
)
3186+
crl = x509.load_der_x509_crl(dumped_crl, backend)
3187+
revoked = crl.get_revoked_certificate_by_serial_number(0x03AB)
3188+
assert revoked is not None
3189+
assert crl.issuer == x509.Name([
3190+
x509.NameAttribute(x509.NameOID.COUNTRY_NAME, u"US"),
3191+
x509.NameAttribute(x509.NameOID.STATE_OR_PROVINCE_NAME, u"IL"),
3192+
x509.NameAttribute(x509.NameOID.LOCALITY_NAME, u"Chicago"),
3193+
x509.NameAttribute(x509.NameOID.ORGANIZATION_NAME, u"Testing"),
3194+
x509.NameAttribute(x509.NameOID.COMMON_NAME, u"Testing Root CA"),
3195+
])
31943196

31953197
# Flaky because we compare the output of running commands which sometimes
31963198
# varies by 1 second
@@ -3207,7 +3209,8 @@ def test_export_text(self):
32073209
self.cert, self.pkey, FILETYPE_ASN1, digest=b"md5"
32083210
)
32093211
text = _runopenssl(
3210-
dumped_crl, b"crl", b"-noout", b"-text", b"-inform", b"DER"
3212+
dumped_crl, b"crl", b"-noout", b"-text", b"-inform", b"DER",
3213+
b"-nameopt", b""
32113214
)
32123215

32133216
# text format
@@ -3778,7 +3781,7 @@ def __init__(self):
37783781

37793782
class TestEllipticCurveEquality(EqualityTestsMixin):
37803783
"""
3781-
Tests `_EllipticCurve`\ 's implementation of ``==`` and ``!=``.
3784+
Tests `_EllipticCurve`'s implementation of ``==`` and ``!=``.
37823785
"""
37833786
curve_factory = EllipticCurveFactory()
37843787

tests/test_ssl.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,14 @@ def _create_certificate_chain():
216216
return [(cakey, cacert), (ikey, icert), (skey, scert)]
217217

218218

219-
def loopback_client_factory(socket):
220-
client = Connection(Context(SSLv23_METHOD), socket)
219+
def loopback_client_factory(socket, version=SSLv23_METHOD):
220+
client = Connection(Context(version), socket)
221221
client.set_connect_state()
222222
return client
223223

224224

225-
def loopback_server_factory(socket):
226-
ctx = Context(SSLv23_METHOD)
225+
def loopback_server_factory(socket, version=SSLv23_METHOD):
226+
ctx = Context(version)
227227
ctx.use_privatekey(load_privatekey(FILETYPE_PEM, server_key_pem))
228228
ctx.use_certificate(load_certificate(FILETYPE_PEM, server_cert_pem))
229229
server = Connection(ctx, socket)
@@ -1307,13 +1307,13 @@ def test_set_verify_callback_exception(self):
13071307
exception, verification fails and the exception is propagated to the
13081308
caller of `Connection.do_handshake`.
13091309
"""
1310-
serverContext = Context(TLSv1_METHOD)
1310+
serverContext = Context(TLSv1_2_METHOD)
13111311
serverContext.use_privatekey(
13121312
load_privatekey(FILETYPE_PEM, cleartextPrivateKeyPEM))
13131313
serverContext.use_certificate(
13141314
load_certificate(FILETYPE_PEM, cleartextCertificatePEM))
13151315

1316-
clientContext = Context(TLSv1_METHOD)
1316+
clientContext = Context(TLSv1_2_METHOD)
13171317

13181318
def verify_callback(*args):
13191319
raise Exception("silly verify failure")
@@ -2539,7 +2539,7 @@ def test_client_set_session(self):
25392539
"""
25402540
key = load_privatekey(FILETYPE_PEM, server_key_pem)
25412541
cert = load_certificate(FILETYPE_PEM, server_cert_pem)
2542-
ctx = Context(SSLv23_METHOD)
2542+
ctx = Context(TLSv1_2_METHOD)
25432543
ctx.use_privatekey(key)
25442544
ctx.use_certificate(cert)
25452545
ctx.set_session_id("unity-test")
@@ -3193,7 +3193,10 @@ def test_renegotiate(self):
31933193
"""
31943194
Go through a complete renegotiation cycle.
31953195
"""
3196-
server, client = loopback()
3196+
server, client = loopback(
3197+
lambda s: loopback_server_factory(s, TLSv1_2_METHOD),
3198+
lambda s: loopback_client_factory(s, TLSv1_2_METHOD),
3199+
)
31973200

31983201
server.send(b"hello world")
31993202

tox.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ deps =
2727
git+https://github.com/twisted/twisted
2828
idna
2929
service_identity
30+
bcrypt
3031
passenv = ARCHFLAGS CFLAGS LC_ALL LDFLAGS PATH LD_LIBRARY_PATH TERM
3132
commands =
3233
python -c "import OpenSSL.SSL; print(OpenSSL.SSL.SSLeay_version(OpenSSL.SSL.SSLEAY_VERSION))"
@@ -38,7 +39,7 @@ basepython=python3.5
3839
deps =
3940
pyasn1
4041
ndg-httpsclient
41-
passenv = ARCHFLAGS CFLAGS LC_ALL LDFLAGS PATH LD_LIBRARY_PATH TERM
42+
passenv = ARCHFLAGS CFLAGS LC_ALL LDFLAGS PATH LD_LIBRARY_PATH TERM TRAVIS_INFRA
4243
whitelist_externals =
4344
rm
4445
commands =

0 commit comments

Comments
 (0)