diff options
| author | 2023-06-05 18:29:25 +0200 | |
|---|---|---|
| committer | 2023-06-05 18:29:25 +0200 | |
| commit | fafdbbf815ea44ca1813a4ab58547d487f6a3bfb (patch) | |
| tree | 139aeaf983b3b634f4b3bdfa57bb63876bb2cf68 | |
| parent | Some prototype tweaks. (diff) | |
| download | penes-snac2-fafdbbf815ea44ca1813a4ab58547d487f6a3bfb.tar.gz penes-snac2-fafdbbf815ea44ca1813a4ab58547d487f6a3bfb.tar.xz penes-snac2-fafdbbf815ea44ca1813a4ab58547d487f6a3bfb.zip | |
Replaced usage of random() with xs_rnd_buf().
| -rw-r--r-- | Makefile | 11 | ||||
| -rw-r--r-- | mastoapi.c | 14 | ||||
| -rw-r--r-- | snac.c | 5 | ||||
| -rw-r--r-- | utils.c | 6 | ||||
| -rw-r--r-- | xs_random.h | 87 | ||||
| -rw-r--r-- | xs_version.h | 2 |
6 files changed, 102 insertions, 23 deletions
| @@ -37,19 +37,20 @@ activitypub.o: activitypub.c xs.h xs_json.h xs_curl.h xs_mime.h \ | |||
| 37 | xs_openssl.h xs_regex.h xs_time.h xs_set.h snac.h | 37 | xs_openssl.h xs_regex.h xs_time.h xs_set.h snac.h |
| 38 | data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h xs_glob.h xs_set.h \ | 38 | data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h xs_glob.h xs_set.h \ |
| 39 | xs_time.h snac.h | 39 | xs_time.h snac.h |
| 40 | format.o: format.c xs.h xs_regex.h snac.h | 40 | format.o: format.c xs.h xs_regex.h xs_mime.h snac.h |
| 41 | html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \ | 41 | html.o: html.c xs.h xs_io.h xs_json.h xs_regex.h xs_set.h xs_openssl.h \ |
| 42 | xs_time.h xs_mime.h snac.h | 42 | xs_time.h xs_mime.h snac.h |
| 43 | http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \ | 43 | http.o: http.c xs.h xs_io.h xs_openssl.h xs_curl.h xs_time.h xs_json.h \ |
| 44 | snac.h | 44 | snac.h |
| 45 | httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_httpd.h xs_mime.h \ | 45 | httpd.o: httpd.c xs.h xs_io.h xs_json.h xs_socket.h xs_httpd.h xs_mime.h \ |
| 46 | xs_time.h snac.h | 46 | xs_time.h xs_openssl.h snac.h |
| 47 | main.o: main.c xs.h xs_io.h xs_json.h snac.h | 47 | main.o: main.c xs.h xs_io.h xs_json.h snac.h |
| 48 | mastoapi.o: mastoapi.c xs.h xs_openssl.h xs_json.h xs_io.h xs_time.h \ | 48 | mastoapi.o: mastoapi.c xs.h xs_openssl.h xs_json.h xs_io.h xs_time.h \ |
| 49 | xs_glob.h xs_set.h snac.h | 49 | xs_glob.h xs_set.h xs_random.h snac.h |
| 50 | snac.o: snac.c xs.h xs_io.h xs_unicode.h xs_json.h xs_curl.h xs_openssl.h \ | 50 | snac.o: snac.c xs.h xs_io.h xs_unicode.h xs_json.h xs_curl.h xs_openssl.h \ |
| 51 | xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ | 51 | xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ |
| 52 | snac.h | 52 | xs_random.h snac.h |
| 53 | upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h | 53 | upgrade.o: upgrade.c xs.h xs_io.h xs_json.h xs_glob.h snac.h |
| 54 | utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h snac.h | 54 | utils.o: utils.c xs.h xs_io.h xs_json.h xs_time.h xs_openssl.h \ |
| 55 | xs_random.h snac.h | ||
| 55 | webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h snac.h | 56 | webfinger.o: webfinger.c xs.h xs_json.h xs_curl.h snac.h |
| @@ -10,6 +10,7 @@ | |||
| 10 | #include "xs_time.h" | 10 | #include "xs_time.h" |
| 11 | #include "xs_glob.h" | 11 | #include "xs_glob.h" |
| 12 | #include "xs_set.h" | 12 | #include "xs_set.h" |
| 13 | #include "xs_random.h" | ||
| 13 | 14 | ||
| 14 | #include "snac.h" | 15 | #include "snac.h" |
| 15 | 16 | ||
| @@ -17,19 +18,8 @@ static xs_str *random_str(void) | |||
| 17 | /* just what is says in the tin */ | 18 | /* just what is says in the tin */ |
| 18 | { | 19 | { |
| 19 | unsigned int data[4] = {0}; | 20 | unsigned int data[4] = {0}; |
| 20 | FILE *f; | ||
| 21 | |||
| 22 | if ((f = fopen("/dev/random", "r")) != NULL) { | ||
| 23 | fread(data, sizeof(data), 1, f); | ||
| 24 | fclose(f); | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | data[0] = random() % 0xffffffff; | ||
| 28 | data[1] = random() % 0xffffffff; | ||
| 29 | data[2] = random() % 0xffffffff; | ||
| 30 | data[3] = random() % 0xffffffff; | ||
| 31 | } | ||
| 32 | 21 | ||
| 22 | xs_rnd_buf(data, sizeof(data)); | ||
| 33 | return xs_hex_enc((char *)data, sizeof(data)); | 23 | return xs_hex_enc((char *)data, sizeof(data)); |
| 34 | } | 24 | } |
| 35 | 25 | ||
| @@ -16,6 +16,7 @@ | |||
| 16 | #include "xs_set.h" | 16 | #include "xs_set.h" |
| 17 | #include "xs_time.h" | 17 | #include "xs_time.h" |
| 18 | #include "xs_glob.h" | 18 | #include "xs_glob.h" |
| 19 | #include "xs_random.h" | ||
| 19 | 20 | ||
| 20 | #include "snac.h" | 21 | #include "snac.h" |
| 21 | 22 | ||
| @@ -122,7 +123,9 @@ xs_str *hash_password(const char *uid, const char *passwd, const char *nonce) | |||
| 122 | xs *hash; | 123 | xs *hash; |
| 123 | 124 | ||
| 124 | if (nonce == NULL) { | 125 | if (nonce == NULL) { |
| 125 | d_nonce = xs_fmt("%08x", random()); | 126 | unsigned int r; |
| 127 | xs_rnd_buf(&r, sizeof(r)); | ||
| 128 | d_nonce = xs_fmt("%08x", r); | ||
| 126 | nonce = d_nonce; | 129 | nonce = d_nonce; |
| 127 | } | 130 | } |
| 128 | 131 | ||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include "xs_json.h" | 6 | #include "xs_json.h" |
| 7 | #include "xs_time.h" | 7 | #include "xs_time.h" |
| 8 | #include "xs_openssl.h" | 8 | #include "xs_openssl.h" |
| 9 | #include "xs_random.h" | ||
| 9 | 10 | ||
| 10 | #include "snac.h" | 11 | #include "snac.h" |
| 11 | 12 | ||
| @@ -204,10 +205,7 @@ void new_password(const char *uid, d_char **clear_pwd, d_char **hashed_pwd) | |||
| 204 | { | 205 | { |
| 205 | int rndbuf[3]; | 206 | int rndbuf[3]; |
| 206 | 207 | ||
| 207 | srandom(time(NULL) ^ getpid()); | 208 | xs_rnd_buf(rndbuf, sizeof(rndbuf)); |
| 208 | rndbuf[0] = random() & 0xffffffff; | ||
| 209 | rndbuf[1] = random() & 0xffffffff; | ||
| 210 | rndbuf[2] = random() & 0xffffffff; | ||
| 211 | 209 | ||
| 212 | *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf)); | 210 | *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf)); |
| 213 | *hashed_pwd = hash_password(uid, *clear_pwd, NULL); | 211 | *hashed_pwd = hash_password(uid, *clear_pwd, NULL); |
diff --git a/xs_random.h b/xs_random.h new file mode 100644 index 0000000..3566827 --- /dev/null +++ b/xs_random.h | |||
| @@ -0,0 +1,87 @@ | |||
| 1 | /* copyright (c) 2022 - 2023 grunfink / MIT license */ | ||
| 2 | |||
| 3 | #ifndef _XS_RANDOM_H | ||
| 4 | |||
| 5 | #define _XS_RANDOM_H | ||
| 6 | |||
| 7 | unsigned int xs_rnd_int32_d(unsigned int *seed); | ||
| 8 | void *xs_rnd_buf(void *buf, int size); | ||
| 9 | |||
| 10 | #ifdef XS_IMPLEMENTATION | ||
| 11 | |||
| 12 | #include <stdio.h> | ||
| 13 | #include <sys/time.h> | ||
| 14 | #include <unistd.h> | ||
| 15 | #include <stdlib.h> | ||
| 16 | |||
| 17 | unsigned int xs_rnd_int32_d(unsigned int *seed) | ||
| 18 | /* returns a deterministic random integer. If seed is NULL, uses a static one */ | ||
| 19 | { | ||
| 20 | static unsigned int s = 0; | ||
| 21 | |||
| 22 | if (seed == NULL) | ||
| 23 | seed = &s; | ||
| 24 | |||
| 25 | if (*seed == 0) { | ||
| 26 | struct timeval tv; | ||
| 27 | |||
| 28 | gettimeofday(&tv, NULL); | ||
| 29 | *seed = tv.tv_sec ^ tv.tv_usec ^ getpid(); | ||
| 30 | } | ||
| 31 | |||
| 32 | /* Linear congruential generator by Numerical Recipes */ | ||
| 33 | *seed = (*seed * 1664525) + 1013904223; | ||
| 34 | |||
| 35 | return *seed; | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | void *xs_rnd_buf(void *buf, int size) | ||
| 40 | /* fills buf with random data */ | ||
| 41 | { | ||
| 42 | #ifdef __OpenBSD__ | ||
| 43 | |||
| 44 | /* available since OpenBSD 2.2 */ | ||
| 45 | arc4random_buf(buf, size); | ||
| 46 | |||
| 47 | #else | ||
| 48 | |||
| 49 | FILE *f; | ||
| 50 | int done = 0; | ||
| 51 | |||
| 52 | if ((f = fopen("/dev/urandom", "r")) != NULL) { | ||
| 53 | /* fill with great random data from the system */ | ||
| 54 | if (fread(buf, size, 1, f) == 1) | ||
| 55 | done = 1; | ||
| 56 | |||
| 57 | fclose(f); | ||
| 58 | } | ||
| 59 | |||
| 60 | if (!done) { | ||
| 61 | /* fill the buffer with poor quality, deterministic data */ | ||
| 62 | unsigned int s = 0; | ||
| 63 | unsigned char *p = (unsigned char *)buf; | ||
| 64 | int n = size / sizeof(s); | ||
| 65 | |||
| 66 | /* fill with full integers */ | ||
| 67 | while (n--) { | ||
| 68 | xs_rnd_int32_d(&s); | ||
| 69 | p = memcpy(p, &s, sizeof(s)) + sizeof(s); | ||
| 70 | } | ||
| 71 | |||
| 72 | if ((n = size % sizeof(s))) { | ||
| 73 | /* fill the remaining */ | ||
| 74 | xs_rnd_int32_d(&s); | ||
| 75 | memcpy(p, &s, n); | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | #endif /* __OpenBSD__ */ | ||
| 80 | |||
| 81 | return buf; | ||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | #endif /* XS_IMPLEMENTATION */ | ||
| 86 | |||
| 87 | #endif /* XS_RANDOM_H */ | ||
diff --git a/xs_version.h b/xs_version.h index 7a793d1..b589fed 100644 --- a/xs_version.h +++ b/xs_version.h | |||
| @@ -1 +1 @@ | |||
| /* 3588cbb7859917f1c5965254f8a53c3349c773ea */ | /* 5c255b45c8cd5d6c01c983b03e635936db12da03 */ | ||