Software /
code /
prosody
Annotate
util-src/crand.c @ 13652:a08065207ef0
net.server_epoll: Call :shutdown() on TLS sockets when supported
Comment from Matthew:
This fixes a potential issue where the Prosody process gets blocked on sockets
waiting for them to close. Unlike non-TLS sockets, closing a TLS socket sends
layer 7 data, and this can cause problems for sockets which are in the process
of being cleaned up.
This depends on LuaSec changes which are not yet upstream.
From Martijn's original email:
So first my analysis of luasec. in ssl.c the socket is put into blocking
mode right before calling SSL_shutdown() inside meth_destroy(). My best
guess to why this is is because meth_destroy is linked to the __close
and __gc methods, which can't exactly be called multiple times and
luasec does want to make sure that a tls session is shutdown as clean
as possible.
I can't say I disagree with this reasoning and don't want to change this
behaviour. My solution to this without changing the current behaviour is
to introduce a shutdown() method. I am aware that this overlaps in a
conflicting way with tcp's shutdown method, but it stays close to the
OpenSSL name. This method calls SSL_shutdown() in the current
(non)blocking mode of the underlying socket and returns a boolean
whether or not the shutdown is completed (matching SSL_shutdown()'s 0
or 1 return values), and returns the familiar ssl_ioerror() strings on
error with a false for completion. This error can then be used to
determine if we have wantread/wantwrite to finalize things. Once
meth_shutdown() has been called once a shutdown flag will be set, which
indicates to meth_destroy() that the SSL_shutdown() has been handled
by the application and it shouldn't be needed to set the socket to
blocking mode. I've left the SSL_shutdown() call in the
LSEC_STATE_CONNECTED to prevent TOCTOU if the application reaches a
timeout for the shutdown code, which might allow SSL_shutdown() to
clean up anyway at the last possible moment.
Another thing I've changed to luasec is the call to socket_setblocking()
right before calling close(2) in socket_destroy() in usocket.c.
According to the latest POSIX[0]:
Note that the requirement for close() on a socket to block for up to
the current linger interval is not conditional on the O_NONBLOCK
setting.
Which I read to mean that removing O_NONBLOCK on the socket before close
doesn't impact the behaviour and only causes noise in system call
tracers. I didn't touch the windows bits of this, since I don't do
windows.
For the prosody side of things I've made the TLS shutdown bits resemble
interface:onwritable(), and put it under a combined guard of self._tls
and self.conn.shutdown. The self._tls bit is there to prevent getting
stuck on this condition, and self.conn.shutdown is there to prevent the
code being called by instances where the patched luasec isn't deployed.
The destroy() method can be called from various places and is read by
me as the "we give up" error path. To accommodate for these unexpected
entrypoints I've added a single call to self.conn:shutdown() to prevent
the socket being put into blocking mode. I have no expectations that
there is any other use here. Same as previous, the self.conn.shutdown
check is there to make sure it's not called on unpatched luasec
deployments and self._tls is there to make sure we don't call shutdown()
on tcp sockets.
I wouldn't recommend logging of the conn:shutdown() error inside
close(), since a lot of clients simply close the connection before
SSL_shutdown() is done.
author | Martijn van Duren <martijn@openbsd.org> |
---|---|
date | Thu, 06 Feb 2025 15:04:38 +0000 |
parent | 12976:a187600ec7d6 |
rev | line source |
---|---|
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 /* Prosody IM |
7829
1fb477d19fdd
util.crand: Update copyright header
Kim Alvefur <zash@zash.se>
parents:
7826
diff
changeset
|
2 -- Copyright (C) 2008-2017 Matthew Wild |
1fb477d19fdd
util.crand: Update copyright header
Kim Alvefur <zash@zash.se>
parents:
7826
diff
changeset
|
3 -- Copyright (C) 2008-2017 Waqas Hussain |
1fb477d19fdd
util.crand: Update copyright header
Kim Alvefur <zash@zash.se>
parents:
7826
diff
changeset
|
4 -- Copyright (C) 2016-2017 Kim Alvefur |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- This project is MIT/X11 licensed. Please see the |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 -- COPYING file in the source package for more information. |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 -- |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 */ |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 /* |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 * crand.c |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 * C PRNG interface |
7830
b9910a507250
util.crand: Add comment describing purpose of module
Kim Alvefur <zash@zash.se>
parents:
7829
diff
changeset
|
14 * |
b9910a507250
util.crand: Add comment describing purpose of module
Kim Alvefur <zash@zash.se>
parents:
7829
diff
changeset
|
15 * The purpose of this module is to provide access to a PRNG in |
b9910a507250
util.crand: Add comment describing purpose of module
Kim Alvefur <zash@zash.se>
parents:
7829
diff
changeset
|
16 * environments without /dev/urandom |
b9910a507250
util.crand: Add comment describing purpose of module
Kim Alvefur <zash@zash.se>
parents:
7829
diff
changeset
|
17 * |
b9910a507250
util.crand: Add comment describing purpose of module
Kim Alvefur <zash@zash.se>
parents:
7829
diff
changeset
|
18 * Caution! This has not been extensively tested. |
b9910a507250
util.crand: Add comment describing purpose of module
Kim Alvefur <zash@zash.se>
parents:
7829
diff
changeset
|
19 * |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 */ |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 |
7931
b619b85e01aa
util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents:
7918
diff
changeset
|
22 #define _DEFAULT_SOURCE |
b619b85e01aa
util.pposix, configure: Move _GNU_SOURCE macro into source files
Kim Alvefur <zash@zash.se>
parents:
7918
diff
changeset
|
23 |
8422
6eecf82ccd8e
util.crand: Always include stdlib to be sure __GLIBC_PREREQ is defined
Kim Alvefur <zash@zash.se>
parents:
8127
diff
changeset
|
24 #include <stdlib.h> |
8423
65c82a7d0537
util.crand: Reorder includes so system ones are first
Kim Alvefur <zash@zash.se>
parents:
8422
diff
changeset
|
25 #include <string.h> |
65c82a7d0537
util.crand: Reorder includes so system ones are first
Kim Alvefur <zash@zash.se>
parents:
8422
diff
changeset
|
26 #include <errno.h> |
8422
6eecf82ccd8e
util.crand: Always include stdlib to be sure __GLIBC_PREREQ is defined
Kim Alvefur <zash@zash.se>
parents:
8127
diff
changeset
|
27 |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 #include "lualib.h" |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 #include "lauxlib.h" |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 #if defined(WITH_GETRANDOM) |
7934
485b9e66fedf
util.crand: Only include getrandom shiv with glibc older than 2.25
Kim Alvefur <zash@zash.se>
parents:
7933
diff
changeset
|
32 |
8127
3e443d6791e6
util.crand: Build with musl/non-glibc (can't have undefined argument-taking macros?)
Kim Alvefur <zash@zash.se>
parents:
7969
diff
changeset
|
33 #ifndef __GLIBC_PREREQ |
8425
91c220f43826
util.crand: Add comments about defines
Kim Alvefur <zash@zash.se>
parents:
8424
diff
changeset
|
34 /* Not compiled with glibc at all */ |
8127
3e443d6791e6
util.crand: Build with musl/non-glibc (can't have undefined argument-taking macros?)
Kim Alvefur <zash@zash.se>
parents:
7969
diff
changeset
|
35 #define __GLIBC_PREREQ(a,b) 0 |
3e443d6791e6
util.crand: Build with musl/non-glibc (can't have undefined argument-taking macros?)
Kim Alvefur <zash@zash.se>
parents:
7969
diff
changeset
|
36 #endif |
3e443d6791e6
util.crand: Build with musl/non-glibc (can't have undefined argument-taking macros?)
Kim Alvefur <zash@zash.se>
parents:
7969
diff
changeset
|
37 |
7934
485b9e66fedf
util.crand: Only include getrandom shiv with glibc older than 2.25
Kim Alvefur <zash@zash.se>
parents:
7933
diff
changeset
|
38 #if ! __GLIBC_PREREQ(2,25) |
8425
91c220f43826
util.crand: Add comments about defines
Kim Alvefur <zash@zash.se>
parents:
8424
diff
changeset
|
39 /* Not compiled with a glibc that provides getrandom() */ |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 #include <unistd.h> |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 #include <sys/syscall.h> |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 #ifndef SYS_getrandom |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 #error getrandom() requires Linux 3.17 or later |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 #endif |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 |
7934
485b9e66fedf
util.crand: Only include getrandom shiv with glibc older than 2.25
Kim Alvefur <zash@zash.se>
parents:
7933
diff
changeset
|
47 /* This wasn't present before glibc 2.25 */ |
12470
80f3123053e2
util.crand: Reduce scope here too
Kim Alvefur <zash@zash.se>
parents:
8451
diff
changeset
|
48 static int getrandom(void *buf, size_t buflen, unsigned int flags) { |
7933
c91ec7689424
util.crand: Change argumen names to match some man page
Kim Alvefur <zash@zash.se>
parents:
7932
diff
changeset
|
49 return syscall(SYS_getrandom, buf, buflen, flags); |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 } |
7934
485b9e66fedf
util.crand: Only include getrandom shiv with glibc older than 2.25
Kim Alvefur <zash@zash.se>
parents:
7933
diff
changeset
|
51 #else |
485b9e66fedf
util.crand: Only include getrandom shiv with glibc older than 2.25
Kim Alvefur <zash@zash.se>
parents:
7933
diff
changeset
|
52 #include <sys/random.h> |
485b9e66fedf
util.crand: Only include getrandom shiv with glibc older than 2.25
Kim Alvefur <zash@zash.se>
parents:
7933
diff
changeset
|
53 #endif |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 #elif defined(WITH_OPENSSL) |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 #include <openssl/rand.h> |
8448
f516a52f19e8
util.crand: Make it possible to use arc4random on Linux (needs libbsd)
Kim Alvefur <zash@zash.se>
parents:
8447
diff
changeset
|
57 #elif defined(WITH_ARC4RANDOM) |
f516a52f19e8
util.crand: Make it possible to use arc4random on Linux (needs libbsd)
Kim Alvefur <zash@zash.se>
parents:
8447
diff
changeset
|
58 #ifdef __linux__ |
f516a52f19e8
util.crand: Make it possible to use arc4random on Linux (needs libbsd)
Kim Alvefur <zash@zash.se>
parents:
8447
diff
changeset
|
59 #include <bsd/stdlib.h> |
f516a52f19e8
util.crand: Make it possible to use arc4random on Linux (needs libbsd)
Kim Alvefur <zash@zash.se>
parents:
8447
diff
changeset
|
60 #endif |
f516a52f19e8
util.crand: Make it possible to use arc4random on Linux (needs libbsd)
Kim Alvefur <zash@zash.se>
parents:
8447
diff
changeset
|
61 #else |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 #error util.crand compiled without a random source |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 #endif |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 |
8447
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
65 #ifndef SMALLBUFSIZ |
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
66 #define SMALLBUFSIZ 32 |
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
67 #endif |
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
68 |
12470
80f3123053e2
util.crand: Reduce scope here too
Kim Alvefur <zash@zash.se>
parents:
8451
diff
changeset
|
69 static int Lrandom(lua_State *L) { |
8447
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
70 char smallbuf[SMALLBUFSIZ]; |
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
71 char *buf = &smallbuf[0]; |
8449
b572a708fd41
util.crand: Throw an error for sizes smaller than 1 byte
Kim Alvefur <zash@zash.se>
parents:
8448
diff
changeset
|
72 const lua_Integer l = luaL_checkinteger(L, 1); |
b572a708fd41
util.crand: Throw an error for sizes smaller than 1 byte
Kim Alvefur <zash@zash.se>
parents:
8448
diff
changeset
|
73 const size_t len = l; |
b572a708fd41
util.crand: Throw an error for sizes smaller than 1 byte
Kim Alvefur <zash@zash.se>
parents:
8448
diff
changeset
|
74 luaL_argcheck(L, l >= 0, 1, "must be > 0"); |
8447
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
75 |
8451
770f79a9635c
util.crand: Return early if a zero bytes are requested
Kim Alvefur <zash@zash.se>
parents:
8449
diff
changeset
|
76 if(len == 0) { |
770f79a9635c
util.crand: Return early if a zero bytes are requested
Kim Alvefur <zash@zash.se>
parents:
8449
diff
changeset
|
77 lua_pushliteral(L, ""); |
770f79a9635c
util.crand: Return early if a zero bytes are requested
Kim Alvefur <zash@zash.se>
parents:
8449
diff
changeset
|
78 return 1; |
770f79a9635c
util.crand: Return early if a zero bytes are requested
Kim Alvefur <zash@zash.se>
parents:
8449
diff
changeset
|
79 } |
770f79a9635c
util.crand: Return early if a zero bytes are requested
Kim Alvefur <zash@zash.se>
parents:
8449
diff
changeset
|
80 |
8447
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
81 if(len > SMALLBUFSIZ) { |
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
82 buf = lua_newuserdata(L, len); |
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
83 } |
200f4f1b7833
util.crand: Use a small buffer on the stack for small pieces of random, should be faster
Kim Alvefur <zash@zash.se>
parents:
8446
diff
changeset
|
84 |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 #if defined(WITH_GETRANDOM) |
7932 | 86 /* |
87 * This acts like a read from /dev/urandom with the exception that it | |
88 * *does* block if the entropy pool is not yet initialized. | |
89 */ | |
8443
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
90 int left = len; |
8444
adb079840714
util.crand: Only keep return value of getrandom() as the others don't return partial results
Kim Alvefur <zash@zash.se>
parents:
8443
diff
changeset
|
91 char *p = buf; |
8443
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
92 |
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
93 do { |
8444
adb079840714
util.crand: Only keep return value of getrandom() as the others don't return partial results
Kim Alvefur <zash@zash.se>
parents:
8443
diff
changeset
|
94 int ret = getrandom(p, left, 0); |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 |
8443
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
96 if(ret < 0) { |
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
97 lua_pushstring(L, strerror(errno)); |
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
98 return lua_error(L); |
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
99 } |
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
100 |
8444
adb079840714
util.crand: Only keep return value of getrandom() as the others don't return partial results
Kim Alvefur <zash@zash.se>
parents:
8443
diff
changeset
|
101 p += ret; |
8443
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
102 left -= ret; |
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
103 } while(left > 0); |
980885ba062c
util.crand: Try getrandom() again until buffer is filled
Kim Alvefur <zash@zash.se>
parents:
8425
diff
changeset
|
104 |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 #elif defined(WITH_ARC4RANDOM) |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 arc4random_buf(buf, len); |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
107 #elif defined(WITH_OPENSSL) |
8424 | 108 |
7915
e3d3ebd417f4
util.crand: Throw error if OpenSSLs RNG is not seeded
Kim Alvefur <zash@zash.se>
parents:
7832
diff
changeset
|
109 if(!RAND_status()) { |
e3d3ebd417f4
util.crand: Throw error if OpenSSLs RNG is not seeded
Kim Alvefur <zash@zash.se>
parents:
7832
diff
changeset
|
110 lua_pushliteral(L, "OpenSSL PRNG not seeded"); |
7918
12e5a54907b6
util.crand: Clarify that lua_error does not return
Kim Alvefur <zash@zash.se>
parents:
7915
diff
changeset
|
111 return lua_error(L); |
7915
e3d3ebd417f4
util.crand: Throw error if OpenSSLs RNG is not seeded
Kim Alvefur <zash@zash.se>
parents:
7832
diff
changeset
|
112 } |
e3d3ebd417f4
util.crand: Throw error if OpenSSLs RNG is not seeded
Kim Alvefur <zash@zash.se>
parents:
7832
diff
changeset
|
113 |
8445
2d3a3d12ec87
util.crand: Silence signedness warning
Kim Alvefur <zash@zash.se>
parents:
8444
diff
changeset
|
114 if(RAND_bytes((unsigned char *)buf, len) != 1) { |
7832 | 115 /* TODO ERR_get_error() */ |
7826 | 116 lua_pushstring(L, "RAND_bytes() failed"); |
117 return lua_error(L); | |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 } |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 #endif |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 |
8444
adb079840714
util.crand: Only keep return value of getrandom() as the others don't return partial results
Kim Alvefur <zash@zash.se>
parents:
8443
diff
changeset
|
122 lua_pushlstring(L, buf, len); |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 return 1; |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 } |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 |
12976
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
126 int luaopen_prosody_util_crand(lua_State *L) { |
7818
54669df178c2
util-src: Make C modules assert that the Lua runtime matches what it was compiled for
Kim Alvefur <zash@zash.se>
parents:
7439
diff
changeset
|
127 luaL_checkversion(L); |
7915
e3d3ebd417f4
util.crand: Throw error if OpenSSLs RNG is not seeded
Kim Alvefur <zash@zash.se>
parents:
7832
diff
changeset
|
128 |
7969
1c6a07606309
util-src: Specify size of various tables to be allocated
Kim Alvefur <zash@zash.se>
parents:
7934
diff
changeset
|
129 lua_createtable(L, 0, 2); |
7187
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
130 lua_pushcfunction(L, Lrandom); |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
131 lua_setfield(L, -2, "bytes"); |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
132 |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
133 #if defined(WITH_GETRANDOM) |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 lua_pushstring(L, "Linux"); |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 #elif defined(WITH_ARC4RANDOM) |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 lua_pushstring(L, "arc4random()"); |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 #elif defined(WITH_OPENSSL) |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
138 lua_pushstring(L, "OpenSSL"); |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
139 #endif |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 lua_setfield(L, -2, "_source"); |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 return 1; |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 } |
3d2c2f0809ee
util.crand: C binding to one of OpenSSL, Linux getrandom() or OpenBSD arc4random() CSPRNG
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
144 |
12976
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
145 int luaopen_util_crand(lua_State *L) { |
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
146 return luaopen_prosody_util_crand(L); |
a187600ec7d6
util: Add compat for prosody module name change to C sources
Kim Alvefur <zash@zash.se>
parents:
12575
diff
changeset
|
147 } |