|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2024 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include "pwdstore.h" |
|
30 |
|
31 #include "utils.h" |
|
32 |
|
33 #include <stdio.h> |
|
34 #include <stdlib.h> |
|
35 #include <string.h> |
|
36 |
|
37 #include <cx/utils.h> |
|
38 #include <cx/hash_map.h> |
|
39 |
|
40 #ifdef _WIN32 |
|
41 #include <winsock.h> |
|
42 #pragma comment(lib, "Ws2_32.lib") |
|
43 #else |
|
44 #include <netinet/in.h> |
|
45 #endif |
|
46 |
|
47 |
|
48 static pwdstore_pwinput_func pw_input = (pwdstore_pwinput_func)pwdstore_default_pwinput; |
|
49 static void *pw_input_data = "Master password: "; |
|
50 |
|
51 char * pwdstore_default_pwinput(char *prompt) { |
|
52 return util_password_input(prompt); |
|
53 } |
|
54 |
|
55 void pwdstore_set_pwinput_func(pwdstore_pwinput_func func, void *userdata) { |
|
56 pw_input = func; |
|
57 pw_input_data = userdata; |
|
58 } |
|
59 |
|
60 PwdStore* pwdstore_open(const char *file) { |
|
61 FILE *in = fopen(file, "r"); |
|
62 if(!in) { |
|
63 return NULL; |
|
64 } |
|
65 |
|
66 CxBuffer *buf = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
|
67 cx_stream_copy(in, buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite); |
|
68 fclose(in); |
|
69 |
|
70 if(buf->size < PWDS_HEADER_SIZE || buf->space[0] != PWDS_MAGIC_CHAR) { |
|
71 cxBufferFree(buf); |
|
72 return NULL; |
|
73 } |
|
74 |
|
75 PwdStore *p = malloc(sizeof(PwdStore)); |
|
76 p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); |
|
77 p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
78 p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
79 p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); |
|
80 p->content = buf; |
|
81 p->key = NULL; |
|
82 p->unlock_cmd = NULL; |
|
83 p->lock_cmd = NULL; |
|
84 p->encoffset = PWDS_HEADER_SIZE; |
|
85 p->isdecrypted = 0; |
|
86 |
|
87 if(pwdstore_getindex(p)) { |
|
88 pwdstore_free(p); |
|
89 return NULL; |
|
90 } |
|
91 |
|
92 return p; |
|
93 } |
|
94 |
|
95 PwdStore* pwdstore_new(void) { |
|
96 PwdStore *p = calloc(1, sizeof(PwdStore)); |
|
97 p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); |
|
98 p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
99 p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
100 p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); |
|
101 p->content = cxBufferCreate(NULL, PWDS_HEADER_SIZE, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
|
102 PWDS_MAGIC(p) = PWDS_MAGIC_CHAR; |
|
103 PWDS_VERSION(p) = 1; |
|
104 PWDS_ENC(p) = DAV_KEY_AES256; |
|
105 PWDS_PWFUNC(p) = DAV_PWFUNC_PBKDF2_SHA256; |
|
106 dav_rand_bytes((unsigned char*)p->content->space+4, 16); |
|
107 p->isdecrypted = 1; |
|
108 p->encoffset = PWDS_HEADER_SIZE; |
|
109 return p; |
|
110 } |
|
111 |
|
112 static int readval(CxBuffer *in, char **val, int allowzero) { |
|
113 // value = length string |
|
114 // length = uint32 |
|
115 // string = bytes |
|
116 |
|
117 *val = NULL; |
|
118 |
|
119 // get length |
|
120 uint32_t length = 0; |
|
121 if(cxBufferRead(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) { |
|
122 return 0; |
|
123 } |
|
124 length = ntohl(length); // convert from BE to host byte order |
|
125 if(length == 0) { |
|
126 if(allowzero) { |
|
127 return 1; |
|
128 } else { |
|
129 return 0; |
|
130 } |
|
131 } |
|
132 if(length > PWDSTORE_MAX_LEN) { |
|
133 return 0; |
|
134 } |
|
135 |
|
136 // get value |
|
137 char *value = malloc(length + 1); |
|
138 value[length] = 0; |
|
139 if(cxBufferRead(value, 1, length, in) != length) { |
|
140 free(value); |
|
141 return 0; |
|
142 } |
|
143 |
|
144 *val = value; |
|
145 return 1; |
|
146 } |
|
147 |
|
148 static int read_indexentry(PwdStore *p, CxBuffer *in) { |
|
149 // read type of index element |
|
150 int type = cxBufferGet(in); |
|
151 if(type == EOF || type != 0) { |
|
152 // only type 0 supported yet |
|
153 return 0; |
|
154 } |
|
155 |
|
156 char *id = NULL; |
|
157 CxList *locations = cxLinkedListCreateSimple(CX_STORE_POINTERS); |
|
158 cxDefineDestructor(locations, free); |
|
159 |
|
160 // get id (required) |
|
161 int ret = 0; |
|
162 if(readval(in, &id, FALSE)) { |
|
163 // get locations |
|
164 char *location = NULL; |
|
165 while((ret = readval(in, &location, TRUE)) == 1) { |
|
166 if(!location) { |
|
167 break; |
|
168 } |
|
169 cxListAdd(locations, location); |
|
170 } |
|
171 } |
|
172 |
|
173 if(ret) { |
|
174 pwdstore_put_index(p, id, locations); |
|
175 } else { |
|
176 if(id) free(id); |
|
177 cxListDestroy(locations); |
|
178 } |
|
179 |
|
180 return ret; |
|
181 } |
|
182 |
|
183 static int read_pwdentry(PwdStore *p, CxBuffer *in) { |
|
184 int type = cxBufferGet(in); |
|
185 if(type == EOF || type != 0) { |
|
186 // only type 0 supported yet |
|
187 return 0; |
|
188 } |
|
189 |
|
190 char *id = NULL; |
|
191 char *user = NULL; |
|
192 char *password = NULL; |
|
193 |
|
194 int ret = 0; |
|
195 if(readval(in, &id, FALSE)) { |
|
196 if(readval(in, &user, FALSE)) { |
|
197 if(readval(in, &password, FALSE)) { |
|
198 pwdstore_put(p, id, user, password); |
|
199 ret = 1; |
|
200 } |
|
201 } |
|
202 } |
|
203 |
|
204 if(id) free(id); |
|
205 if(user) free(user); |
|
206 if(password) free(password); |
|
207 |
|
208 return ret; |
|
209 } |
|
210 |
|
211 static void remove_list_entries(PwdStore *s, const char *id) { |
|
212 CxIterator i = cxListMutIterator(s->locations); |
|
213 cx_foreach(PwdIndexEntry*, ie, i) { |
|
214 if(!strcmp(ie->id, id)) { |
|
215 cxIteratorFlagRemoval(i); |
|
216 cxIteratorNext(i); |
|
217 break; |
|
218 } |
|
219 } |
|
220 i = cxListMutIterator(s->noloc); |
|
221 cx_foreach(PwdIndexEntry*, ie, i) { |
|
222 if(!strcmp(ie->id, id)) { |
|
223 cxIteratorFlagRemoval(i); |
|
224 cxIteratorNext(i); |
|
225 break; |
|
226 } |
|
227 } |
|
228 } |
|
229 |
|
230 void pwdstore_remove_entry(PwdStore *s, const char *id) { |
|
231 remove_list_entries(s, id); |
|
232 |
|
233 CxHashKey key = cx_hash_key_str(id); |
|
234 PwdIndexEntry *i = cxMapRemoveAndGet(s->index, key); |
|
235 PwdEntry *e = cxMapRemoveAndGet(s->ids, key); |
|
236 |
|
237 if(i) { |
|
238 cxListDestroy(i->locations); |
|
239 free(i->id); |
|
240 free(i); |
|
241 } |
|
242 if(e) { |
|
243 free(e->id); |
|
244 free(e->user); |
|
245 free(e->password); |
|
246 free(e); |
|
247 } |
|
248 } |
|
249 |
|
250 int pwdstore_getindex(PwdStore *s) { |
|
251 uint32_t netindexlen; |
|
252 |
|
253 // set the position to the last 4 bytes of the header |
|
254 // for reading index length |
|
255 s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t); |
|
256 |
|
257 // read indexlen and convert to host byte order |
|
258 if(cxBufferRead(&netindexlen, 1, sizeof(uint32_t), s->content) != sizeof(uint32_t)) { |
|
259 return 1; |
|
260 } |
|
261 uint32_t indexlen = ntohl(netindexlen); |
|
262 |
|
263 // integer overflow check |
|
264 if(UINT32_MAX - PWDS_HEADER_SIZE < indexlen) { |
|
265 return 1; |
|
266 } |
|
267 if(s->content->size < PWDS_HEADER_SIZE + indexlen) { |
|
268 return 1; |
|
269 } |
|
270 // encrypted content starts after the index content |
|
271 s->encoffset = PWDS_HEADER_SIZE + indexlen; |
|
272 |
|
273 // the index starts after the header |
|
274 CxBuffer *index = cxBufferCreate(s->content->space+PWDS_HEADER_SIZE, indexlen, cxDefaultAllocator, 0); |
|
275 index->size = indexlen; |
|
276 |
|
277 // read index |
|
278 while(read_indexentry(s, index)) {} |
|
279 |
|
280 // free index buffer structure (not the content) |
|
281 cxBufferFree(index); |
|
282 |
|
283 return 0; |
|
284 } |
|
285 |
|
286 int pwdstore_decrypt(PwdStore *p) { |
|
287 if(!p->key) { |
|
288 return 1; |
|
289 } |
|
290 if(p->isdecrypted) { |
|
291 return 0; |
|
292 } |
|
293 |
|
294 // decrypt contet |
|
295 size_t encsz = p->content->size - p->encoffset; |
|
296 CxBuffer *enc = cxBufferCreate(p->content->space + p->encoffset, encsz, cxDefaultAllocator, 0); |
|
297 enc->size = encsz; |
|
298 enc->size = p->content->size - p->encoffset; |
|
299 CxBuffer *content = aes_decrypt_buffer(enc, p->key); |
|
300 cxBufferFree(enc); |
|
301 if(!content) { |
|
302 return 1; |
|
303 } |
|
304 |
|
305 while(read_pwdentry(p, content)) {} |
|
306 |
|
307 cxBufferFree(content); |
|
308 |
|
309 return 0; |
|
310 } |
|
311 |
|
312 int pwdstore_setpassword(PwdStore *p, const char *password) { |
|
313 DavKey *key = dav_pw2key( |
|
314 password, |
|
315 (unsigned char*)(p->content->space + 4), |
|
316 16, |
|
317 PWDS_PWFUNC(p), |
|
318 PWDS_ENC(p)); |
|
319 if(!key) { |
|
320 return 1; |
|
321 } |
|
322 |
|
323 p->key = key; |
|
324 return 0; |
|
325 } |
|
326 |
|
327 void pwdstore_encsettings(PwdStore *p, uint8_t enc, uint8_t pwfunc) { |
|
328 PWDS_ENC(p) = enc; |
|
329 PWDS_PWFUNC(p) = pwfunc; |
|
330 } |
|
331 |
|
332 void pwdstore_free_entry(PwdEntry *e) { |
|
333 if(e->id) free(e->id); |
|
334 if(e->user) free(e->user); |
|
335 if(e->password) free(e->password); |
|
336 free(e); |
|
337 } |
|
338 |
|
339 void pwdstore_free(PwdStore* p) { |
|
340 cxDefineDestructor(p->ids, pwdstore_free_entry); |
|
341 cxMapDestroy(p->ids); |
|
342 |
|
343 cxListDestroy(p->locations); |
|
344 |
|
345 if(p->content) { |
|
346 cxBufferFree(p->content); |
|
347 } |
|
348 |
|
349 free(p); |
|
350 } |
|
351 |
|
352 int pwdstore_has_id(PwdStore *s, const char *id) { |
|
353 return cxMapGet(s->index, cx_hash_key_str(id)) ? 1 : 0; |
|
354 } |
|
355 |
|
356 PwdEntry* pwdstore_get(PwdStore *p, const char *id) { |
|
357 PwdEntry *e = cxMapGet(p->ids, cx_hash_key_str(id)); |
|
358 if(e && e->user && e->password) { |
|
359 return e; |
|
360 } else { |
|
361 return NULL; |
|
362 } |
|
363 } |
|
364 |
|
365 void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) { |
|
366 PwdEntry *entry = malloc(sizeof(PwdEntry)); |
|
367 entry->id = strdup(id); |
|
368 entry->user = strdup(username); |
|
369 entry->password = strdup(password); |
|
370 cxMapPut(p->ids, cx_hash_key_str(id), entry); |
|
371 } |
|
372 |
|
373 void pwdstore_put_index(PwdStore *p, char *id, CxList *locations) { |
|
374 PwdIndexEntry *e = cxMapGet(p->index, cx_hash_key_str(id)); |
|
375 if(e) { |
|
376 return; |
|
377 } |
|
378 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); |
|
379 newentry->id = id; |
|
380 if(locations) { |
|
381 newentry->locations = locations; |
|
382 cxListAdd(p->locations, newentry); |
|
383 } else { |
|
384 newentry->locations = NULL; |
|
385 cxListAdd(p->noloc, newentry); |
|
386 } |
|
387 cxMapPut(p->index, cx_hash_key_str(id), newentry); |
|
388 } |
|
389 |
|
390 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) { |
|
391 uint32_t idlen = strlen(e->id); |
|
392 uint32_t netidlen = htonl(idlen); |
|
393 |
|
394 cxBufferPut(out, 0); // type |
|
395 |
|
396 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out); |
|
397 cxBufferWrite(e->id, 1, idlen, out); |
|
398 |
|
399 CxIterator i = cxListIterator(e->locations); |
|
400 cx_foreach(char *, location, i) { |
|
401 uint32_t locationlen = strlen(location); |
|
402 uint32_t netlocationlen = htonl(locationlen); |
|
403 |
|
404 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); |
|
405 cxBufferWrite(location, 1, locationlen, out); |
|
406 } |
|
407 |
|
408 uint32_t terminate = 0; |
|
409 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out); |
|
410 } |
|
411 |
|
412 int pwdstore_store(PwdStore *p, const char *file) { |
|
413 if(!p->key) { |
|
414 return 1; |
|
415 } |
|
416 |
|
417 CxBuffer *index = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
|
418 CxBuffer *content = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
|
419 |
|
420 // create index |
|
421 CxIterator i = cxListIterator(p->noloc); |
|
422 cx_foreach(PwdIndexEntry*, e, i) { |
|
423 write_index_entry(index, e); |
|
424 } |
|
425 i = cxListIterator(p->locations); |
|
426 cx_foreach(PwdIndexEntry*, e, i) { |
|
427 write_index_entry(index, e); |
|
428 } |
|
429 |
|
430 i = cxMapIteratorValues(p->ids); |
|
431 cx_foreach(PwdEntry*, value, i) { |
|
432 if(!value->id || !value->user || !value->password) { |
|
433 continue; |
|
434 } |
|
435 |
|
436 uint32_t idlen = strlen(value->id); |
|
437 uint32_t ulen = strlen(value->user); |
|
438 uint32_t plen = strlen(value->password); |
|
439 uint32_t netidlen = htonl(idlen); |
|
440 uint32_t netulen = htonl(ulen); |
|
441 uint32_t netplen = htonl(plen); |
|
442 |
|
443 // content buffer |
|
444 cxBufferPut(content, 0); // type |
|
445 |
|
446 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), content); |
|
447 cxBufferWrite(value->id, 1, idlen, content); |
|
448 cxBufferWrite(&netulen, 1, sizeof(uint32_t), content); |
|
449 cxBufferWrite(value->user, 1, ulen, content); |
|
450 cxBufferWrite(&netplen, 1, sizeof(uint32_t), content); |
|
451 cxBufferWrite(value->password, 1, plen, content); |
|
452 } |
|
453 |
|
454 content->pos = 0; |
|
455 CxBuffer *enc = aes_encrypt_buffer(content, p->key); |
|
456 |
|
457 p->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t); |
|
458 p->content->size = PWDS_HEADER_SIZE; |
|
459 |
|
460 // add index after header |
|
461 uint32_t netindexlen = htonl((uint32_t)index->size); |
|
462 cxBufferWrite(&netindexlen, 1, sizeof(uint32_t), p->content); |
|
463 cxBufferWrite(index->space, 1, index->size, p->content); |
|
464 |
|
465 // add encrypted buffer |
|
466 cxBufferWrite(enc->space, 1, enc->size, p->content); |
|
467 |
|
468 cxBufferFree(enc); |
|
469 |
|
470 FILE *out = fopen(file, "w"); |
|
471 if(!out) { |
|
472 return 1; |
|
473 } |
|
474 fwrite(p->content->space, 1, p->content->size, out); |
|
475 fclose(out); |
|
476 |
|
477 return 0; |
|
478 } |
|
479 |
|
480 int pwdstore_decrypt_secrets(PwdStore *secrets) { |
|
481 if(!pw_input) { |
|
482 return 1; |
|
483 } |
|
484 |
|
485 char *ps_password = NULL; |
|
486 if(secrets->unlock_cmd && strlen(secrets->unlock_cmd) > 0) { |
|
487 CxBuffer *cmd_out = cxBufferCreate(NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND); |
|
488 if(!util_exec_command(secrets->unlock_cmd, cmd_out)) { |
|
489 // command successful, get first line from output without newline |
|
490 // and use that as password for the secretstore |
|
491 size_t len = 0; |
|
492 for(size_t i=0;i<=cmd_out->size;i++) { |
|
493 if(i == cmd_out->size || cmd_out->space[i] == '\n') { |
|
494 len = i; |
|
495 break; |
|
496 } |
|
497 } |
|
498 if(len > 0) { |
|
499 ps_password = malloc(len + 1); |
|
500 memcpy(ps_password, cmd_out->space, len); |
|
501 ps_password[len] = 0; |
|
502 } |
|
503 } |
|
504 cxBufferFree(cmd_out); |
|
505 } |
|
506 |
|
507 if(!ps_password) { |
|
508 ps_password = pw_input(pw_input_data); |
|
509 if(!ps_password) { |
|
510 return 1; |
|
511 } |
|
512 } |
|
513 |
|
514 int err = pwdstore_setpassword(secrets, ps_password); |
|
515 free(ps_password); |
|
516 if(err) { |
|
517 fprintf(stderr, "Error: cannot create key from password\n"); |
|
518 return 1; |
|
519 } |
|
520 if(pwdstore_decrypt(secrets)) { |
|
521 fprintf(stderr, "Error: cannot decrypt secrets store\n"); |
|
522 return 1; |
|
523 } |
|
524 return 0; |
|
525 } |