Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04f6a32

Browse files
committed
Merged revisions 79812 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r79812 | antoine.pitrou | 2010-04-05 23:35:07 +0200 (lun., 05 avril 2010) | 5 lines Issue #8321: Give access to OpenSSL version numbers from the `ssl` module, using the new attributes `ssl.OPENSSL_VERSION`, `ssl.OPENSSL_VERSION_INFO` and `ssl.OPENSSL_VERSION_NUMBER`. ........
1 parent c50846a commit 04f6a32

5 files changed

Lines changed: 93 additions & 1 deletion

File tree

Doc/library/ssl.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,36 @@ Functions, Constants, and Exceptions
237237
modern version, and probably the best choice for maximum protection, if both
238238
sides can speak it.
239239

240+
.. data:: OPENSSL_VERSION
241+
242+
The version string of the OpenSSL library loaded by the interpreter::
243+
244+
>>> ssl.OPENSSL_VERSION
245+
'OpenSSL 0.9.8k 25 Mar 2009'
246+
247+
.. versionadded:: 2.7
248+
249+
.. data:: OPENSSL_VERSION_INFO
250+
251+
A tuple of five integers representing version information about the
252+
OpenSSL library::
253+
254+
>>> ssl.OPENSSL_VERSION_INFO
255+
(0, 9, 8, 11, 15)
256+
257+
.. versionadded:: 2.7
258+
259+
.. data:: OPENSSL_VERSION_NUMBER
260+
261+
The raw version number of the OpenSSL library, as a single integer::
262+
263+
>>> ssl.OPENSSL_VERSION_NUMBER
264+
9470143L
265+
>>> hex(ssl.OPENSSL_VERSION_NUMBER)
266+
'0x9080bfL'
267+
268+
.. versionadded:: 2.7
269+
240270

241271
SSLSocket Objects
242272
-----------------

Lib/ssl.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858

5959
import _ssl # if we can't import it, let the error propagate
6060

61+
from _ssl import OPENSSL_VERSION_NUMBER, OPENSSL_VERSION_INFO, OPENSSL_VERSION
6162
from _ssl import SSLError
6263
from _ssl import CERT_NONE, CERT_OPTIONAL, CERT_REQUIRED
6364
from _ssl import (PROTOCOL_SSLv2, PROTOCOL_SSLv3, PROTOCOL_SSLv23,

Lib/test/test_ssl.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,34 @@ def testDERtoPEM(self):
9494
if (d1 != d2):
9595
raise support.TestFailed("PEM-to-DER or DER-to-PEM translation failed")
9696

97+
def test_openssl_version(self):
98+
n = ssl.OPENSSL_VERSION_NUMBER
99+
t = ssl.OPENSSL_VERSION_INFO
100+
s = ssl.OPENSSL_VERSION
101+
self.assertIsInstance(n, int)
102+
self.assertIsInstance(t, tuple)
103+
self.assertIsInstance(s, str)
104+
# Some sanity checks follow
105+
# >= 0.9
106+
self.assertGreaterEqual(n, 0x900000)
107+
# < 2.0
108+
self.assertLess(n, 0x20000000)
109+
major, minor, fix, patch, status = t
110+
self.assertGreaterEqual(major, 0)
111+
self.assertLess(major, 2)
112+
self.assertGreaterEqual(minor, 0)
113+
self.assertLess(minor, 256)
114+
self.assertGreaterEqual(fix, 0)
115+
self.assertLess(fix, 256)
116+
self.assertGreaterEqual(patch, 0)
117+
self.assertLessEqual(patch, 26)
118+
self.assertGreaterEqual(status, 0)
119+
self.assertLessEqual(status, 15)
120+
# Version string as returned by OpenSSL, the format might change
121+
self.assertTrue(s.startswith("OpenSSL {:d}.{:d}.{:d}".format(major, minor, fix)),
122+
(s, t))
123+
124+
97125
class NetworkedTests(unittest.TestCase):
98126

99127
def testConnect(self):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,10 @@ C-API
303303
Library
304304
-------
305305

306+
- Issue #8321: Give access to OpenSSL version numbers from the `ssl` module,
307+
using the new attributes `ssl.OPENSSL_VERSION`, `ssl.OPENSSL_VERSION_INFO`
308+
and `ssl.OPENSSL_VERSION_NUMBER`.
309+
306310
- Add functools.total_ordering() and functools.cmp_to_key().
307311

308312
- Issue #8257: The Decimal construct now accepts a float instance

Modules/_ssl.c

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1636,7 +1636,9 @@ static struct PyModuleDef _sslmodule = {
16361636
PyMODINIT_FUNC
16371637
PyInit__ssl(void)
16381638
{
1639-
PyObject *m, *d;
1639+
PyObject *m, *d, *r;
1640+
unsigned long libver;
1641+
unsigned int major, minor, fix, patch, status;
16401642
PySocketModule_APIObject *socket_api;
16411643

16421644
if (PyType_Ready(&PySSL_Type) < 0)
@@ -1710,5 +1712,32 @@ PyInit__ssl(void)
17101712
PY_SSL_VERSION_SSL23);
17111713
PyModule_AddIntConstant(m, "PROTOCOL_TLSv1",
17121714
PY_SSL_VERSION_TLS1);
1715+
1716+
/* OpenSSL version */
1717+
/* SSLeay() gives us the version of the library linked against,
1718+
which could be different from the headers version.
1719+
*/
1720+
libver = SSLeay();
1721+
r = PyLong_FromUnsignedLong(libver);
1722+
if (r == NULL)
1723+
return NULL;
1724+
if (PyModule_AddObject(m, "OPENSSL_VERSION_NUMBER", r))
1725+
return NULL;
1726+
status = libver & 0xF;
1727+
libver >>= 4;
1728+
patch = libver & 0xFF;
1729+
libver >>= 8;
1730+
fix = libver & 0xFF;
1731+
libver >>= 8;
1732+
minor = libver & 0xFF;
1733+
libver >>= 8;
1734+
major = libver & 0xFF;
1735+
r = Py_BuildValue("IIIII", major, minor, fix, patch, status);
1736+
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION_INFO", r))
1737+
return NULL;
1738+
r = PyUnicode_FromString(SSLeay_version(SSLEAY_VERSION));
1739+
if (r == NULL || PyModule_AddObject(m, "OPENSSL_VERSION", r))
1740+
return NULL;
1741+
17131742
return m;
17141743
}

0 commit comments

Comments
 (0)