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

Skip to content

Commit fcfed19

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 9d01717 commit fcfed19

7 files changed

Lines changed: 65 additions & 3 deletions

File tree

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, RAND_bytes, RAND_pseudo_bytes
109+
from _ssl import RAND_status, RAND_add, RAND_bytes, RAND_pseudo_bytes
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
@@ -154,8 +154,9 @@ def test_random(self):
154154
self.assertRaises(ValueError, ssl.RAND_bytes, -5)
155155
self.assertRaises(ValueError, ssl.RAND_pseudo_bytes, -5)
156156

157-
self.assertRaises(TypeError, ssl.RAND_egd, 1)
158-
self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1)
157+
if hasattr(ssl, 'RAND_egd'):
158+
self.assertRaises(TypeError, ssl.RAND_egd, 1)
159+
self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1)
159160
ssl.RAND_add("this is a random string", 75.0)
160161

161162
@unittest.skipUnless(os.name == 'posix', 'requires posix')

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ Core and Builtins
4444
Library
4545
-------
4646

47+
- Issue #21356: Make ssl.RAND_egd() optional to support LibreSSL. The
48+
availability of the function is checked during the compilation. Patch written
49+
by Bernard Spil.
50+
4751
- Issue #20896, #22935: The :func:`ssl.get_server_certificate` function now
4852
uses the :data:`~ssl.PROTOCOL_SSLv23` protocol by default, not
4953
:data:`~ssl.PROTOCOL_SSLv3`, for maximum compatibility and support platforms

Modules/_ssl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3335,6 +3335,7 @@ Returns 1 if the OpenSSL PRNG has been seeded with enough data and 0 if not.\n\
33353335
It is necessary to seed the PRNG with RAND_add() on some platforms before\n\
33363336
using the ssl() function.");
33373337

3338+
#ifdef HAVE_RAND_EGD
33383339
static PyObject *
33393340
PySSL_RAND_egd(PyObject *self, PyObject *args)
33403341
{
@@ -3362,6 +3363,7 @@ PyDoc_STRVAR(PySSL_RAND_egd_doc,
33623363
Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
33633364
Returns number of bytes read. Raises SSLError if connection to EGD\n\
33643365
fails or if it does not provide enough data to seed PRNG.");
3366+
#endif /* HAVE_RAND_EGD */
33653367

33663368
#endif /* HAVE_OPENSSL_RAND */
33673369

@@ -3757,8 +3759,10 @@ static PyMethodDef PySSL_methods[] = {
37573759
PySSL_RAND_bytes_doc},
37583760
{"RAND_pseudo_bytes", PySSL_RAND_pseudo_bytes, METH_VARARGS,
37593761
PySSL_RAND_pseudo_bytes_doc},
3762+
#ifdef HAVE_RAND_EGD
37603763
{"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
37613764
PySSL_RAND_egd_doc},
3765+
#endif
37623766
{"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
37633767
PySSL_RAND_status_doc},
37643768
#endif

configure

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8913,6 +8913,48 @@ _ACEOF
89138913

89148914
fi
89158915
# Dynamic linking for HP-UX
8916+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for RAND_egd in -lcrypto" >&5
8917+
$as_echo_n "checking for RAND_egd in -lcrypto... " >&6; }
8918+
if ${ac_cv_lib_crypto_RAND_egd+:} false; then :
8919+
$as_echo_n "(cached) " >&6
8920+
else
8921+
ac_check_lib_save_LIBS=$LIBS
8922+
LIBS="-lcrypto $LIBS"
8923+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
8924+
/* end confdefs.h. */
8925+
8926+
/* Override any GCC internal prototype to avoid an error.
8927+
Use char because int might match the return type of a GCC
8928+
builtin and then its argument prototype would still apply. */
8929+
#ifdef __cplusplus
8930+
extern "C"
8931+
#endif
8932+
char RAND_egd ();
8933+
int
8934+
main ()
8935+
{
8936+
return RAND_egd ();
8937+
;
8938+
return 0;
8939+
}
8940+
_ACEOF
8941+
if ac_fn_c_try_link "$LINENO"; then :
8942+
ac_cv_lib_crypto_RAND_egd=yes
8943+
else
8944+
ac_cv_lib_crypto_RAND_egd=no
8945+
fi
8946+
rm -f core conftest.err conftest.$ac_objext \
8947+
conftest$ac_exeext conftest.$ac_ext
8948+
LIBS=$ac_check_lib_save_LIBS
8949+
fi
8950+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_crypto_RAND_egd" >&5
8951+
$as_echo "$ac_cv_lib_crypto_RAND_egd" >&6; }
8952+
if test "x$ac_cv_lib_crypto_RAND_egd" = xyes; then :
8953+
8954+
$as_echo "#define HAVE_RAND_EGD 1" >>confdefs.h
8955+
8956+
fi
8957+
89168958

89178959
# only check for sem_init if thread support is requested
89188960
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
@@ -2238,6 +2238,9 @@ AC_MSG_RESULT($SHLIBS)
22382238
AC_CHECK_LIB(sendfile, sendfile)
22392239
AC_CHECK_LIB(dl, dlopen) # Dynamic linking for SunOS/Solaris and SYSV
22402240
AC_CHECK_LIB(dld, shl_load) # Dynamic linking for HP-UX
2241+
AC_CHECK_LIB(crypto, RAND_egd,
2242+
AC_DEFINE(HAVE_RAND_EGD, 1,
2243+
[Define if the libcrypto has RAND_egd]))
22412244

22422245
# only check for sem_init if thread support is requested
22432246
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
@@ -675,6 +675,9 @@
675675
/* Define to 1 if you have the `pwrite' function. */
676676
#undef HAVE_PWRITE
677677

678+
/* Define if the libcrypto has RAND_egd */
679+
#undef HAVE_RAND_EGD
680+
678681
/* Define to 1 if you have the `readlink' function. */
679682
#undef HAVE_READLINK
680683

0 commit comments

Comments
 (0)