Skip to content

Commit 7c90667

Browse files
committed
Issue #21356: Make ssl.RAND_egd() optional to support LibreSSL. The
availability of the function is checked during the compilation. Patch written by Bernard Spil.
1 parent 7ba8cdc commit 7c90667

9 files changed

Lines changed: 78 additions & 7 deletions

File tree

Doc/library/ssl.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ Random generation
299299
See http://egd.sourceforge.net/ or http://prngd.sourceforge.net/ for sources
300300
of entropy-gathering daemons.
301301

302+
Availability: not available with LibreSSL.
303+
302304
.. function:: RAND_add(bytes, entropy)
303305

304306
Mixes the given *bytes* into the SSL pseudo-random number generator. The

Lib/socket.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ def ssl(sock, keyfile=None, certfile=None):
6767
from _ssl import SSLError as sslerror
6868
from _ssl import \
6969
RAND_add, \
70-
RAND_egd, \
7170
RAND_status, \
7271
SSL_ERROR_ZERO_RETURN, \
7372
SSL_ERROR_WANT_READ, \
@@ -78,6 +77,11 @@ def ssl(sock, keyfile=None, certfile=None):
7877
SSL_ERROR_WANT_CONNECT, \
7978
SSL_ERROR_EOF, \
8079
SSL_ERROR_INVALID_ERROR_CODE
80+
try:
81+
from _ssl import RAND_egd
82+
except ImportError:
83+
# LibreSSL does not provide RAND_egd
84+
pass
8185

8286
import os, sys, warnings
8387

Lib/ssl.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@
106106
from _ssl import (VERIFY_DEFAULT, VERIFY_CRL_CHECK_LEAF, VERIFY_CRL_CHECK_CHAIN,
107107
VERIFY_X509_STRICT)
108108
from _ssl import txt2obj as _txt2obj, nid2obj as _nid2obj
109-
from _ssl import RAND_status, RAND_egd, RAND_add
109+
from _ssl import RAND_status, RAND_add
110+
try:
111+
from _ssl import RAND_egd
112+
except ImportError:
113+
# LibreSSL does not provide RAND_egd
114+
pass
110115

111116
def _import_symbols(prefix):
112117
for n in dir(_ssl):

Lib/test/test_ssl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ def test_random(self):
169169
sys.stdout.write("\n RAND_status is %d (%s)\n"
170170
% (v, (v and "sufficient randomness") or
171171
"insufficient randomness"))
172-
self.assertRaises(TypeError, ssl.RAND_egd, 1)
173-
self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1)
172+
if hasattr(ssl, 'RAND_egd'):
173+
self.assertRaises(TypeError, ssl.RAND_egd, 1)
174+
self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1)
174175
ssl.RAND_add("this is a random string", 75.0)
175176

176177
def test_parse_cert(self):

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Core and Builtins
1515
Library
1616
-------
1717

18+
- Issue #21356: Make ssl.RAND_egd() optional to support LibreSSL. The
19+
availability of the function is checked during the compilation. Patch written
20+
by Bernard Spil.
21+
1822
- Backport the context argument to ftplib.FTP_TLS.
1923

2024
- Issue #23111: Maximize compatibility in protocol versions of ftplib.FTP_TLS.

Modules/_ssl.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3301,6 +3301,11 @@ Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
33013301
It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
33023302
using the ssl() function.");
33033303

3304+
#endif /* HAVE_OPENSSL_RAND */
3305+
3306+
3307+
#ifdef HAVE_RAND_EGD
3308+
33043309
static PyObject *
33053310
PySSL_RAND_egd(PyObject *self, PyObject *arg)
33063311
{
@@ -3327,7 +3332,7 @@ Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
33273332
Returns number of bytes read. Raises SSLError if connection to EGD\n\
33283333
fails or if it does not provide enough data to seed PRNG.");
33293334

3330-
#endif /* HAVE_OPENSSL_RAND */
3335+
#endif /* HAVE_RAND_EGD */
33313336

33323337

33333338
PyDoc_STRVAR(PySSL_get_default_verify_paths_doc,
@@ -3720,10 +3725,12 @@ static PyMethodDef PySSL_methods[] = {
37203725
#ifdef HAVE_OPENSSL_RAND
37213726
{"RAND_add", PySSL_RAND_add, METH_VARARGS,
37223727
PySSL_RAND_add_doc},
3723-
{"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
3724-
PySSL_RAND_egd_doc},
37253728
{"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
37263729
PySSL_RAND_status_doc},
3730+
#endif
3731+
#ifdef HAVE_RAND_EGD
3732+
{"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
3733+
PySSL_RAND_egd_doc},
37273734
#endif
37283735
{"get_default_verify_paths", (PyCFunction)PySSL_get_default_verify_paths,
37293736
METH_NOARGS, PySSL_get_default_verify_paths_doc},

configure

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8551,6 +8551,48 @@ _ACEOF
85518551
85528552
fi
85538553
# Dynamic linking for HP-UX
8554+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for RAND_egd in -lcrypto" >&5
8555+
$as_echo_n "checking for RAND_egd in -lcrypto... " >&6; }
8556+
if ${ac_cv_lib_crypto_RAND_egd+:} false; then :
8557+
$as_echo_n "(cached) " >&6
8558+
else
8559+
ac_check_lib_save_LIBS=$LIBS
8560+
LIBS="-lcrypto $LIBS"
8561+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
8562+
/* end confdefs.h. */
8563+
8564+
/* Override any GCC internal prototype to avoid an error.
8565+
Use char because int might match the return type of a GCC
8566+
builtin and then its argument prototype would still apply. */
8567+
#ifdef __cplusplus
8568+
extern "C"
8569+
#endif
8570+
char RAND_egd ();
8571+
int
8572+
main ()
8573+
{
8574+
return RAND_egd ();
8575+
;
8576+
return 0;
8577+
}
8578+
_ACEOF
8579+
if ac_fn_c_try_link "$LINENO"; then :
8580+
ac_cv_lib_crypto_RAND_egd=yes
8581+
else
8582+
ac_cv_lib_crypto_RAND_egd=no
8583+
fi
8584+
rm -f core conftest.err conftest.$ac_objext \
8585+
conftest$ac_exeext conftest.$ac_ext
8586+
LIBS=$ac_check_lib_save_LIBS
8587+
fi
8588+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_crypto_RAND_egd" >&5
8589+
$as_echo "$ac_cv_lib_crypto_RAND_egd" >&6; }
8590+
if test "x$ac_cv_lib_crypto_RAND_egd" = xyes; then :
8591+
8592+
$as_echo "#define HAVE_RAND_EGD 1" >>confdefs.h
8593+
8594+
fi
8595+
85548596
85558597
# only check for sem_init if thread support is requested
85568598
if test "$with_threads" = "yes" -o -z "$with_threads"; then

configure.ac

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,9 @@ AC_MSG_RESULT($SHLIBS)
22212221
# checks for libraries
22222222
AC_CHECK_LIB(dl, dlopen) # Dynamic linking for SunOS/Solaris and SYSV
22232223
AC_CHECK_LIB(dld, shl_load) # Dynamic linking for HP-UX
2224+
AC_CHECK_LIB(crypto, RAND_egd,
2225+
AC_DEFINE(HAVE_RAND_EGD, 1,
2226+
[Define if the libcrypto has RAND_egd]))
22242227

22252228
# only check for sem_init if thread support is requested
22262229
if test "$with_threads" = "yes" -o -z "$with_threads"; then

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@
547547
/* Define to 1 if you have the `putenv' function. */
548548
#undef HAVE_PUTENV
549549

550+
/* Define if the libcrypto has RAND_egd */
551+
#undef HAVE_RAND_EGD
552+
550553
/* Define to 1 if you have the `readlink' function. */
551554
#undef HAVE_READLINK
552555

0 commit comments

Comments
 (0)