Expose OpenSSL version info and handle missing SSLv2_METHOD support
diff --git a/OpenSSL/ssl/context.c b/OpenSSL/ssl/context.c
index ea7847f..16268d8 100644
--- a/OpenSSL/ssl/context.c
+++ b/OpenSSL/ssl/context.c
@@ -237,6 +237,15 @@
return;
}
+/*
+ * More recent builds of OpenSSL may have SSLv2 completely disabled.
+ */
+#ifdef OPENSSL_NO_SSL2
+#define SSLv2_METHOD_TEXT ""
+#else
+#define SSLv2_METHOD_TEXT "SSLv2_METHOD, "
+#endif
+
static char ssl_Context_doc[] = "\n\
Context(method) -> Context instance\n\
@@ -244,10 +253,12 @@
OpenSSL.SSL.Context instances define the parameters for setting up new SSL\n\
connections.\n\
\n\
-@param method: One of SSLv2_METHOD, SSLv3_METHOD, SSLv23_METHOD, or\n\
+@param method: One of " SSLv2_METHOD_TEXT "SSLv3_METHOD, SSLv23_METHOD, or\n\
TLSv1_METHOD.\n\
";
+#undef SSLv2_METHOD_TEXT
+
static char ssl_Context_load_verify_locations_doc[] = "\n\
Let SSL know where we can find trusted certificates for the certificate\n\
chain\n\
@@ -1107,11 +1118,16 @@
*/
static ssl_ContextObj*
ssl_Context_init(ssl_ContextObj *self, int i_method) {
- SSL_METHOD *method;
+ const SSL_METHOD *method;
switch (i_method) {
case ssl_SSLv2_METHOD:
+#ifdef OPENSSL_NO_SSL2
+ PyErr_SetString(PyExc_ValueError, "SSLv2_METHOD not supported by this version of OpenSSL");
+ return NULL;
+#else
method = SSLv2_method();
+#endif
break;
case ssl_SSLv23_METHOD:
method = SSLv23_method();
diff --git a/OpenSSL/ssl/ssl.c b/OpenSSL/ssl/ssl.c
index 50651a9..7a827a0 100644
--- a/OpenSSL/ssl/ssl.c
+++ b/OpenSSL/ssl/ssl.c
@@ -50,9 +50,30 @@
*ssl_WantX509LookupError, /* ... */
*ssl_SysCallError; /* Uses (errno,errstr) */
+static char ssl_SSLeay_version_doc[] = "\n\
+Return a string describing the version of OpenSSL in use.\n\
+\n\
+@param type: One of the SSLEAY_ constants defined in this module.\n\
+";
+
+static PyObject *
+ssl_SSLeay_version(PyObject *spam, PyObject *args) {
+ int t;
+ const char *version;
+
+ if (!PyArg_ParseTuple(args, "i:SSLeay_version", &t)) {
+ return NULL;
+ }
+
+ version = SSLeay_version(t);
+ return PyBytes_FromStringAndSize(version, strlen(version));
+}
+
+
/* Methods in the OpenSSL.SSL module */
static PyMethodDef ssl_methods[] = {
+ { "SSLeay_version", ssl_SSLeay_version, METH_VARARGS, ssl_SSLeay_version_doc },
{ NULL, NULL }
};
@@ -232,6 +253,16 @@
PyModule_AddIntConstant(module, "SSL_CB_HANDSHAKE_START", SSL_CB_HANDSHAKE_START);
PyModule_AddIntConstant(module, "SSL_CB_HANDSHAKE_DONE", SSL_CB_HANDSHAKE_DONE);
+ /* Version information indicators, used with SSLeay_version */
+ PyModule_AddIntConstant(module, "SSLEAY_VERSION", SSLEAY_VERSION);
+ PyModule_AddIntConstant(module, "SSLEAY_CFLAGS", SSLEAY_CFLAGS);
+ PyModule_AddIntConstant(module, "SSLEAY_BUILT_ON", SSLEAY_BUILT_ON);
+ PyModule_AddIntConstant(module, "SSLEAY_PLATFORM", SSLEAY_PLATFORM);
+ PyModule_AddIntConstant(module, "SSLEAY_DIR", SSLEAY_DIR);
+
+ /* Straight up version number */
+ PyModule_AddIntConstant(module, "OPENSSL_VERSION_NUMBER", OPENSSL_VERSION_NUMBER);
+
if (!init_ssl_context(module))
goto error;
if (!init_ssl_connection(module))