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

Skip to content

Commit beeb512

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 07ff92a commit beeb512

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
@@ -168,8 +168,9 @@ def test_random(self):
168168
self.assertRaises(ValueError, ssl.RAND_bytes, -5)
169169
self.assertRaises(ValueError, ssl.RAND_pseudo_bytes, -5)
170170

171-
self.assertRaises(TypeError, ssl.RAND_egd, 1)
172-
self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1)
171+
if hasattr(ssl, 'RAND_egd'):
172+
self.assertRaises(TypeError, ssl.RAND_egd, 1)
173+
self.assertRaises(TypeError, ssl.RAND_egd, 'foo', 1)
173174
ssl.RAND_add("this is a random string", 75.0)
174175

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

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ Core and Builtins
191191
Library
192192
-------
193193

194+
- Issue #21356: Make ssl.RAND_egd() optional to support LibreSSL. The
195+
availability of the function is checked during the compilation. Patch written
196+
by Bernard Spil.
197+
194198
- Issue #22915: SAX parser now supports files opened with file descriptor or
195199
bytes path.
196200

Modules/_ssl.c

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

3716+
#ifdef HAVE_RAND_EGD
37163717
static PyObject *
37173718
PySSL_RAND_egd(PyObject *self, PyObject *args)
37183719
{
@@ -3740,6 +3741,7 @@ PyDoc_STRVAR(PySSL_RAND_egd_doc,
37403741
Queries the entropy gather daemon (EGD) on the socket named by 'path'.\n\
37413742
Returns number of bytes read. Raises SSLError if connection to EGD\n\
37423743
fails or if it does not provide enough data to seed PRNG.");
3744+
#endif /* HAVE_RAND_EGD */
37433745

37443746
#endif /* HAVE_OPENSSL_RAND */
37453747

@@ -4135,8 +4137,10 @@ static PyMethodDef PySSL_methods[] = {
41354137
PySSL_RAND_bytes_doc},
41364138
{"RAND_pseudo_bytes", PySSL_RAND_pseudo_bytes, METH_VARARGS,
41374139
PySSL_RAND_pseudo_bytes_doc},
4140+
#ifdef HAVE_RAND_EGD
41384141
{"RAND_egd", PySSL_RAND_egd, METH_VARARGS,
41394142
PySSL_RAND_egd_doc},
4143+
#endif
41404144
{"RAND_status", (PyCFunction)PySSL_RAND_status, METH_NOARGS,
41414145
PySSL_RAND_status_doc},
41424146
#endif

configure

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

90479047
fi
90489048
# Dynamic linking for HP-UX
9049+
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for RAND_egd in -lcrypto" >&5
9050+
$as_echo_n "checking for RAND_egd in -lcrypto... " >&6; }
9051+
if ${ac_cv_lib_crypto_RAND_egd+:} false; then :
9052+
$as_echo_n "(cached) " >&6
9053+
else
9054+
ac_check_lib_save_LIBS=$LIBS
9055+
LIBS="-lcrypto $LIBS"
9056+
cat confdefs.h - <<_ACEOF >conftest.$ac_ext
9057+
/* end confdefs.h. */
9058+
9059+
/* Override any GCC internal prototype to avoid an error.
9060+
Use char because int might match the return type of a GCC
9061+
builtin and then its argument prototype would still apply. */
9062+
#ifdef __cplusplus
9063+
extern "C"
9064+
#endif
9065+
char RAND_egd ();
9066+
int
9067+
main ()
9068+
{
9069+
return RAND_egd ();
9070+
;
9071+
return 0;
9072+
}
9073+
_ACEOF
9074+
if ac_fn_c_try_link "$LINENO"; then :
9075+
ac_cv_lib_crypto_RAND_egd=yes
9076+
else
9077+
ac_cv_lib_crypto_RAND_egd=no
9078+
fi
9079+
rm -f core conftest.err conftest.$ac_objext \
9080+
conftest$ac_exeext conftest.$ac_ext
9081+
LIBS=$ac_check_lib_save_LIBS
9082+
fi
9083+
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_cv_lib_crypto_RAND_egd" >&5
9084+
$as_echo "$ac_cv_lib_crypto_RAND_egd" >&6; }
9085+
if test "x$ac_cv_lib_crypto_RAND_egd" = xyes; then :
9086+
9087+
$as_echo "#define HAVE_RAND_EGD 1" >>confdefs.h
9088+
9089+
fi
9090+
90499091

90509092
# only check for sem_init if thread support is requested
90519093
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
@@ -2293,6 +2293,9 @@ AC_MSG_RESULT($SHLIBS)
22932293
AC_CHECK_LIB(sendfile, sendfile)
22942294
AC_CHECK_LIB(dl, dlopen) # Dynamic linking for SunOS/Solaris and SYSV
22952295
AC_CHECK_LIB(dld, shl_load) # Dynamic linking for HP-UX
2296+
AC_CHECK_LIB(crypto, RAND_egd,
2297+
AC_DEFINE(HAVE_RAND_EGD, 1,
2298+
[Define if the libcrypto has RAND_egd]))
22962299

22972300
# only check for sem_init if thread support is requested
22982301
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)