Skip to content

Commit 9dbdda0

Browse files
committed
Update cupsGetRand and cupsOAuthMakeBase64Random to use OpenSSL/GNUTLS random functions.
1 parent b37b6ac commit 9dbdda0

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

‎CHANGES.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ v3.0.4 - YYYY-MM-DD
2323
- Updated `ippeveprinter` to add a date/time stamp when logging to a file.
2424
- Updated dateTime parsing in IPP files.
2525
- Updated the HTTP implementation to reduce stack usage.
26+
- Updated `cupsGetRand` and `cupsOAuthMakeBase64Random` to use better random
27+
data, when available.
2628
- Now explicitly validate IPP attribute names as keyword strings.
2729
- Fixed `httpGetDateString` with bad date/time values (Issue #167)
2830
- Fixed a potential buffer overflow in `cupsDNSSDAssembleFullName` on Windows

‎cups/oauth.c‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1726,9 +1726,18 @@ cupsOAuthMakeBase64Random(size_t len) // I - Number of bytes
17261726
len = sizeof(bytes);
17271727

17281728
// Fill out random bytes and convert it to Base64URL...
1729-
for (i = 0; i < len; i ++)
1730-
bytes[i] = (char)cupsGetRand();
1729+
#ifdef HAVE_OPENSSL
1730+
if (RAND_bytes((unsigned char *)bytes, (int)len) < 0)
1731+
# else
1732+
if (gnutls_rnd(GNUTLS_RND_NONCE, bytes, len) != 0)
1733+
# endif // HAVE_OPENSSL
1734+
{
1735+
// TLS library random number generator failed, use the backup generator...
1736+
for (i = 0; i < len; i ++)
1737+
bytes[i] = (char)cupsGetRand();
1738+
}
17311739

1740+
// Base64URL-encode the bytes...
17321741
httpEncode64(base64url, sizeof(base64url), bytes, len, /*url*/true);
17331742

17341743
// Copy and return the random string...

‎cups/rand.c‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ cupsGetRand(void)
4040
return (arc4random());
4141

4242
#else
43-
// Use a Mersenne twister random number generator seeded from /dev/urandom...
4443
unsigned i, // Looping var
4544
temp; // Temporary value
4645
static bool first_time = true; // First time we ran?
@@ -50,6 +49,17 @@ cupsGetRand(void)
5049
// Mutex to control access to state
5150

5251

52+
// Try getting random data from the TLS library...
53+
# ifdef HAVE_OPENSSL
54+
if (RAND_bytes(&i, (int)sizeof(i)) > 0)
55+
return (i);
56+
57+
# else
58+
if (gnutls_rnd(GNUTLS_RND_NONCE, &i, sizeof(i)) == 0)
59+
return (i);
60+
# endif // HAVE_OPENSSL
61+
62+
// Fall back to a Mersenne twister random number generator seeded from /dev/urandom...
5363
pthread_mutex_lock(&mt_mutex);
5464

5565
if (first_time)

0 commit comments

Comments
 (0)