Debian Codegraph @

tools/srp_shared.c

raw symbols SCIP
1 /* librist. Copyright © 2020 SipRadius LLC. All right reserved.
2 * Author: Gijs Peskens <[email protected]>
3 * Author: Sergio Ammirata, Ph.D. <[email protected]>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8 #include "config.h"
9 #include "srp_shared.h"
10 #include <librist/librist_config.h>
11 #if HAVE_MBEDTLS
12 #include <mbedtls/base64.h>
13 #elif HAVE_NETTLE
14 #include <nettle/base64.h>
15 #endif
16 #include <stddef.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdint.h>
22 #include <sys/stat.h>
23
24 #ifdef _WIN32
25 #include <windows.h>
26 #endif
27
28 #if defined(_WIN32) && !defined(stat)
29 #define stat _stat
30 #endif
31
32 typedef enum {
33 IN_USERNAME = 0,
34 IN_VERIFIER,
35 IN_SALT,
36 IN_HASH_ALGO,
37 IN_HASH_VERSION,
38 } user_verifier_state_e;
39
40 #define READ_VERIFIER_LEN 1025
41 #define READ_SALT_LEN 1025
42 #define MAX_LINE_LEN (READ_VERIFIER_LEN + READ_SALT_LEN + 1)
43
44 int srp_base64_decode(char *string, size_t string_len, size_t *out_len, uint8_t **out) {
45 //Add padding if needed
46 if ((string_len % 4) != 0)
47 {
48 size_t needed_padding = 4 - (string_len % 4);
49 for (size_t i = 0; i < needed_padding; i++)
50 string[(string_len + i)] = '=';
51 string_len += needed_padding;
52 string[string_len] = '\0';
53 }
54 #if HAVE_MBEDTLS
55 size_t len = 0;
56
57 int ret =mbedtls_base64_decode(NULL, 0, &len, (unsigned char *)string, string_len);
58 if (ret != 0 && ret != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL)
59 return -1;
60
61 *out = malloc(len);
62 if (mbedtls_base64_decode(*out, len, out_len, (unsigned char *)string, string_len) != 0)
63 goto fail_decode;
64
65 return 0;
66 #elif HAVE_NETTLE
67 struct base64_decode_ctx ctx;
68 nettle_base64_decode_init(&ctx);
69 size_t len = BASE64_ENCODE_LENGTH(string_len);
70 *out = malloc(len);
71 if (nettle_base64_decode_update(&ctx, out_len, *out, string_len, string) != 1)
72 goto fail_decode;
73 if (nettle_base64_decode_final(&ctx) != 1)
74 goto fail_decode;
75 return 0;
76 #endif
77
78 fail_decode:
79 free(*out);
80 *out = NULL;
81 return -1;
82 }
83
84 int parse_line(const char *line, size_t line_len, uint8_t **decoded_verifier, size_t *decoded_verifier_len, uint8_t **decoded_salt, size_t *decoded_salt_len) {
85 char read = '\0';
86 user_verifier_state_e state = IN_VERIFIER;
87 int ret = -1;
88 size_t read_verifier_len = 0;
89 /* +8: room for the inline base64 padding (up to 3) + terminator writes */
90 char *read_verifier = calloc(READ_VERIFIER_LEN + 8, 1);
91 size_t read_salt_len = 0;
92 char *read_salt = calloc(READ_SALT_LEN + 8, 1);
93 if (!read_salt || !read_verifier)
94 goto out;
95 for (size_t i=0; i < line_len; i++) {
96 read = line[i];
97 if (read == ':')
98 {
99 if (state >= IN_SALT && read == '\n')
100 break;
101 if (state == IN_VERIFIER)
102 read_verifier[read_verifier_len+1] = '\0';
103 else if (state == IN_SALT)
104 {
105 read_salt[read_salt_len +1] = '\0';
106 }
107 state++;
108 } else if (state == IN_VERIFIER)
109 {
110 if (read_verifier_len == READ_VERIFIER_LEN)
111 return -1;
112 read_verifier[read_verifier_len] = read;
113 read_verifier_len++;
114 } else if (state == IN_SALT)
115 {
116 if (read_salt_len == READ_SALT_LEN)
117 return -1;
118 read_salt[read_salt_len] = read;
119 read_salt_len++;
120 }
121 }
122 ret = srp_base64_decode(read_verifier, read_verifier_len, decoded_verifier_len, decoded_verifier);
123 if (ret != 0)
124 goto out;
125 ret = srp_base64_decode(read_salt, read_salt_len, decoded_salt_len, decoded_salt);
126 if (ret != 0)
127 free(*decoded_verifier);
128 out:
129 free(read_verifier);
130 free(read_salt);
131 return ret;
132 }
133
134
135 void user_verifier_lookup(char * username,
136 librist_verifier_lookup_data_t *lookup_data,
137 int *hashversion,
138 uint64_t *generation,
139 void *user_data)
140 {
141 #if HAVE_NETTLE
142 struct base64_decode_ctx ctx;
143 nettle_base64_decode_init(&ctx);
144 #endif
145
146 if (user_data == NULL)
147 return;
148
149 char *srpfile = user_data;
150 if (!generation)
151 return;
152
153 #ifdef _WIN32
154 HANDLE hfile = CreateFile(
155 srpfile,
156 GENERIC_READ,
157 FILE_SHARE_READ,
158 NULL,
159 OPEN_EXISTING,
160 FILE_ATTRIBUTE_NORMAL,
161 NULL);
162 FILETIME mtime;
163
164 if (hfile == INVALID_HANDLE_VALUE)
165 return;
166
167 if (!GetFileTime(hfile, NULL, NULL, &mtime))
168 return;
169
170 CloseHandle(hfile);
171 *generation = ((uint64_t)mtime.dwHighDateTime << 32) | mtime.dwLowDateTime;
172 #else
173 struct stat buf;
174 if (stat(srpfile, &buf) != 0)
175 return;
176 #ifdef _DARWIN_C_SOURCE
177 *generation = ((uint64_t)buf.st_mtimespec.tv_sec << 32) | buf.st_mtimespec.tv_nsec;
178 #else
179 *generation = ((uint64_t)buf.st_mtim.tv_sec << 32) | buf.st_mtim.tv_nsec;
180 #endif
181 #endif
182
183 if (!lookup_data || !hashversion)
184 return;
185
186 FILE *fh = fopen(srpfile, "r");
187 if (!fh)
188 return;
189
190 size_t username_offset = 0;
191
192 char read_hashver[4] = {0}; /* 3 digits + NUL for atoi */
193 int read_hashver_len = 0;
194 user_verifier_state_e state = IN_USERNAME;
195 bool skipnextline = false;
196 int read = getc(fh);
197 //expected format: username:verifier:salt:3
198 int maxhashversion = *hashversion;
199 size_t username_len = strlen(username);
200 char *line_even = malloc(MAX_LINE_LEN);
201 size_t line_even_len =0;
202 bool line_one_done = false;
203 char *line_odd = malloc(MAX_LINE_LEN);
204 size_t line_odd_len = 0;
205
206 size_t line_len = 0;
207 while (read != EOF)
208 {
209 char *line = line_one_done? line_odd : line_even;
210 if (skipnextline)
211 {
212 if (read == '\n')
213 skipnextline = false;
214 }
215 else if (state >= IN_SALT && read == '\n') {
216 if (state >= IN_HASH_VERSION) {
217 state = IN_USERNAME;
218 username_offset = 0;
219 *hashversion = atoi(read_hashver);
220 if (line_one_done) {
221 line_odd_len = line_len;
222 } else {
223 line_even_len = line_len;
224 line_len = 0;
225 line_one_done = true;
226 if (*hashversion < maxhashversion) {
227 read = getc(fh);
228 continue;
229 }
230 }
231 }
232 break;
233 } else if (read == ':')
234 {
235 if (state == IN_VERIFIER) {
236 if (line_len >= MAX_LINE_LEN)
237 goto out;
238 line[line_len++] = read;
239 }
240 if (state == IN_USERNAME && username_offset != username_len) {
241 if (line_one_done) {
242 break;
243 }
244 skipnextline = true;
245 username_offset = 0;
246 continue;
247 }
248 state++;
249 }
250 else if (state == IN_USERNAME)
251 {
252 if (username_offset == username_len) {
253 username_offset = 0;
254 skipnextline = true;
255 continue;
256 }
257 if (username[username_offset] != read)
258 {
259 username_offset = 0;
260 skipnextline = true;
261 } else
262 username_offset++;
263 } else if (state >= IN_VERIFIER && state <= IN_SALT) {
264 if (line_len >= MAX_LINE_LEN) {
265 if (!line_one_done)
266 goto out;
267 break;
268 }
269 line[line_len++] = read;
270 } else if (state == IN_HASH_VERSION && read_hashver_len < 3) {
271 read_hashver[read_hashver_len] = read;
272 read_hashver_len++;
273 }
274 read = getc(fh);
275 }
276
277 *hashversion = atoi(read_hashver);
278 if (state < IN_SALT && !line_one_done)
279 goto out;
280
281 char *line = line_even;
282 line_len = line_even_len;
283 if (line_one_done && line_odd_len != 0) {
284 line = line_odd;
285 line_len = line_odd_len;
286 }
287
288 parse_line(line, line_len, &lookup_data->verifier, &lookup_data->verifier_len, &lookup_data->salt, &lookup_data->salt_len);
289 lookup_data->default_ng = true;
290
291 out:
292 free(line_even);
293 free(line_odd);
294 fclose(fh);
295 return;
296 }