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 char *read_verifier = calloc(READ_VERIFIER_LEN, 1);
90 size_t read_salt_len = 0;
91 char *read_salt = calloc(READ_SALT_LEN, 1);
92 if (!read_salt || !read_verifier)
93 goto out;
94 for (size_t i=0; i < line_len; i++) {
95 read = line[i];
96 if (read == ':')
97 {
98 if (state >= IN_SALT && read == '\n')
99 break;
100 if (state == IN_VERIFIER)
101 read_verifier[read_verifier_len+1] = '\0';
102 else if (state == IN_SALT)
103 {
104 read_salt[read_salt_len +1] = '\0';
105 }
106 state++;
107 } else if (state == IN_VERIFIER)
108 {
109 if (read_verifier_len == READ_VERIFIER_LEN)
110 return -1;
111 read_verifier[read_verifier_len] = read;
112 read_verifier_len++;
113 } else if (state == IN_SALT)
114 {
115 if (read_salt_len == READ_SALT_LEN)
116 return -1;
117 read_salt[read_salt_len] = read;
118 read_salt_len++;
119 }
120 }
121 ret = srp_base64_decode(read_verifier, read_verifier_len, decoded_verifier_len, decoded_verifier);
122 if (ret != 0)
123 goto out;
124 ret = srp_base64_decode(read_salt, read_salt_len, decoded_salt_len, decoded_salt);
125 if (ret != 0)
126 free(*decoded_verifier);
127 out:
128 free(read_verifier);
129 free(read_salt);
130 return ret;
131 }
132
133
134 void user_verifier_lookup(char * username,
135 librist_verifier_lookup_data_t *lookup_data,
136 int *hashversion,
137 uint64_t *generation,
138 void *user_data)
139 {
140 #if HAVE_NETTLE
141 struct base64_decode_ctx ctx;
142 nettle_base64_decode_init(&ctx);
143 #endif
144
145 if (user_data == NULL)
146 return;
147
148 char *srpfile = user_data;
149 if (!generation)
150 return;
151
152 #ifdef _WIN32
153 HANDLE hfile = CreateFile(
154 srpfile,
155 GENERIC_READ,
156 FILE_SHARE_READ,
157 NULL,
158 OPEN_EXISTING,
159 FILE_ATTRIBUTE_NORMAL,
160 NULL);
161 FILETIME mtime;
162
163 if (hfile == INVALID_HANDLE_VALUE)
164 return;
165
166 if (!GetFileTime(hfile, NULL, NULL, &mtime))
167 return;
168
169 CloseHandle(hfile);
170 *generation = ((uint64_t)mtime.dwHighDateTime << 32) | mtime.dwLowDateTime;
171 #else
172 struct stat buf;
173 if (stat(srpfile, &buf) != 0)
174 return;
175 #ifdef _DARWIN_C_SOURCE
176 *generation = ((uint64_t)buf.st_mtimespec.tv_sec << 32) | buf.st_mtimespec.tv_nsec;
177 #else
178 *generation = ((uint64_t)buf.st_mtim.tv_sec << 32) | buf.st_mtim.tv_nsec;
179 #endif
180 #endif
181
182 if (!lookup_data || !hashversion)
183 return;
184
185 FILE *fh = fopen(srpfile, "r");
186 if (!fh)
187 return;
188
189 size_t username_offset = 0;
190
191 char read_hashver[3] = {0};
192 int read_hashver_len = 0;
193 user_verifier_state_e state = IN_USERNAME;
194 bool skipnextline = false;
195 int read = getc(fh);
196 //expected format: username:verifier:salt:3
197 int maxhashversion = *hashversion;
198 size_t username_len = strlen(username);
199 char *line_even = malloc(MAX_LINE_LEN);
200 size_t line_even_len =0;
201 bool line_one_done = false;
202 char *line_odd = malloc(MAX_LINE_LEN);
203 size_t line_odd_len = 0;
204
205 size_t line_len = 0;
206 while (read != EOF)
207 {
208 char *line = line_one_done? line_odd : line_even;
209 if (skipnextline)
210 {
211 if (read == '\n')
212 skipnextline = false;
213 }
214 else if (state >= IN_SALT && read == '\n') {
215 if (state >= IN_HASH_VERSION) {
216 state = IN_USERNAME;
217 username_offset = 0;
218 *hashversion = atoi(read_hashver);
219 if (line_one_done) {
220 line_odd_len = line_len;
221 } else {
222 line_even_len = line_len;
223 line_len = 0;
224 line_one_done = true;
225 if (*hashversion < maxhashversion) {
226 read = getc(fh);
227 continue;
228 }
229 }
230 }
231 break;
232 } else if (read == ':')
233 {
234 if (state == IN_VERIFIER)
235 line[line_len++] = read;
236 if (state == IN_USERNAME && username_offset != username_len) {
237 if (line_one_done) {
238 break;
239 }
240 skipnextline = true;
241 username_offset = 0;
242 continue;
243 }
244 state++;
245 }
246 else if (state == IN_USERNAME)
247 {
248 if (username_offset == username_len) {
249 username_offset = 0;
250 skipnextline = true;
251 continue;
252 }
253 if (username[username_offset] != read)
254 {
255 username_offset = 0;
256 skipnextline = true;
257 } else
258 username_offset++;
259 } else if (state >= IN_VERIFIER && state <= IN_SALT) {
260 if (line_len >= MAX_LINE_LEN) {
261 if (!line_one_done)
262 goto out;
263 break;
264 }
265 line[line_len++] = read;
266 } else if (state == IN_HASH_VERSION && read_hashver_len < 3) {
267 read_hashver[read_hashver_len] = read;
268 read_hashver_len++;
269 }
270 read = getc(fh);
271 }
272
273 *hashversion = atoi(read_hashver);
274 if (state < IN_SALT && !line_one_done)
275 goto out;
276
277 char *line = line_even;
278 line_len = line_even_len;
279 if (line_one_done && line_odd_len != 0) {
280 line = line_odd;
281 line_len = line_odd_len;
282 }
283
284 parse_line(line, line_len, &lookup_data->verifier, &lookup_data->verifier_len, &lookup_data->salt, &lookup_data->salt_len);
285 lookup_data->default_ng = true;
286
287 out:
288 free(line_even);
289 free(line_odd);
290 fclose(fh);
291 return;
292 }