← Back to DAGDA | Support | Source: https://github.com/python/cpython/commit/e19dd3ced640e04538a33dc258243b8168391c86
Skip to content

Commit e19dd3c

Browse files
committed
Expose more OPENSSL_ variables
It might be a good idea to expose more BUILD variables so that an application can verify that the crypto and ssl libraries match the ones _ssl.c are using. This is probably not an issue on Linux and Windows where it's possible to actually find the path of the library. But if a platform doesn't have dladdr and one has to guess this might be useful.
1 parent 35ac3a7 commit e19dd3c

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎Lib/ssl.py‎

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

100100
import _ssl # if we can't import it, let the error propagate
101101

102-
from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION
102+
from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION, OPENSSL_CFLAGS, OPENSSL_BUILT_ON, OPENSSL_PLATFORM, OPENSSL_DIR
103103
from _ssl import _SSLContext, MemoryBIO, SSLSession
104104
from _ssl import (
105105
SSLError, SSLZeroReturnError, SSLWantReadError, SSLWantWriteError,

‎Modules/_ssl.c‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6195,6 +6195,22 @@ sslmodule_init_versioninfo(PyObject *m)
61956195
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
61966196
return -1;
61976197

6198+
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_CFLAGS));
6199+
if (r == NULL || PyModule_AddObject(m, "OPENSSL_CFLAGS", r))
6200+
return -1;
6201+
6202+
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_BUILT_ON));
6203+
if (r == NULL || PyModule_AddObject(m, "OPENSSL_BUILT_ON", r))
6204+
return -1;
6205+
6206+
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_PLATFORM));
6207+
if (r == NULL || PyModule_AddObject(m, "OPENSSL_PLATFORM", r))
6208+
return -1;
6209+
6210+
r = PyUnicode_FromString(OpenSSL_version(OPENSSL_DIR));
6211+
if (r == NULL || PyModule_AddObject(m, "OPENSSL_DIR", r))
6212+
return -1;
6213+
61986214
libver = OPENSSL_VERSION_NUMBER;
61996215
parse_openssl_version(libver, &major, &minor, &fix, &patch, &status);
62006216
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);

‎ssl_test.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
print("ssl.OPENSSL_VERSION_NUMBER", hex(ssl.OPENSSL_VERSION_NUMBER))
2525
print("ssl.OPENSSL_VERSION", ssl.OPENSSL_VERSION)
26+
print("ssl.OPENSSL_CFLAGS", ssl.OPENSSL_CFLAGS)
27+
print("ssl.OPENSSL_BUILT_ON", ssl.OPENSSL_BUILT_ON)
28+
print("ssl.OPENSSL_PLATFORM", ssl.OPENSSL_PLATFORM)
29+
print("ssl.OPENSSL_DIR", ssl.OPENSSL_DIR)
2630

2731
crypto_dll_path = ssl._ssl.CRYPTO_DLL_PATH
2832
print("CRYPTO_DLL_PATH:", crypto_dll_path)
@@ -63,6 +67,9 @@
6367

6468
print("ctypes OpenSSL_version_num:", hex(crypto_lib.OpenSSL_version_num()))
6569
print("ctypes OpenSSL_version:", crypto_lib.OpenSSL_version(0))
70+
assert ssl.OPENSSL_CFLAGS == crypto_lib.OpenSSL_version(1).decode('utf-8')
71+
assert ssl.OPENSSL_BUILT_ON == crypto_lib.OpenSSL_version(2).decode('utf-8')
72+
assert ssl.OPENSSL_PLATFORM == crypto_lib.OpenSSL_version(3).decode('utf-8')
6673

6774
ssl_lib.SSL_get_version.argtypes = (ctypes.c_void_p, )
6875
ssl_lib.SSL_get_version.restype = ctypes.c_char_p
@@ -129,6 +136,9 @@
129136

130137
print("cffi OpenSSL_version_num:", hex(crypto_lib.OpenSSL_version_num()))
131138
print("cffi OpenSSL_version(0):", crypto_ffi.string(crypto_lib.OpenSSL_version(0)))
139+
assert ssl.OPENSSL_CFLAGS == crypto_ffi.string(crypto_lib.OpenSSL_version(1)).decode('utf-8')
140+
assert ssl.OPENSSL_BUILT_ON == crypto_ffi.string(crypto_lib.OpenSSL_version(2)).decode('utf-8')
141+
assert ssl.OPENSSL_PLATFORM == crypto_ffi.string(crypto_lib.OpenSSL_version(3)).decode('utf-8')
132142

133143
ssl_ptr = ssl_ffi.cast('void *', ssl_addr)
134144
print("cffi SSL_get_version:", repr(ssl_ffi.string(ssl_lib.SSL_get_version(ssl_ptr))))

0 commit comments

Comments
 (0)