libidav/pwdstore.c

changeset 832
dff5f4c23aa7
child 833
8aa2dc02d9b7
equal deleted inserted replaced
831:f2ee3a5c976e 832:dff5f4c23aa7
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 PwdStore* pwdstore_open(const char *file) {
56 FILE *in = fopen(file, "r");
57 if(!in) {
58 return NULL;
59 }
60
61 CxBuffer *buf = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
62 cx_stream_copy(in, buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite);
63 fclose(in);
64
65 if(buf->size < PWDS_HEADER_SIZE || buf->space[0] != PWDS_MAGIC_CHAR) {
66 cxBufferFree(buf);
67 return NULL;
68 }
69
70 PwdStore *p = malloc(sizeof(PwdStore));
71 p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
72 p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
73 p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
74 p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
75 p->content = buf;
76 p->key = NULL;
77 p->unlock_cmd = NULL;
78 p->lock_cmd = NULL;
79 p->encoffset = PWDS_HEADER_SIZE;
80 p->isdecrypted = 0;
81
82 if(pwdstore_getindex(p)) {
83 pwdstore_free(p);
84 return NULL;
85 }
86
87 return p;
88 }
89
90 PwdStore* pwdstore_new(void) {
91 PwdStore *p = calloc(1, sizeof(PwdStore));
92 p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
93 p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
94 p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
95 p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
96 p->content = cxBufferCreate(NULL, PWDS_HEADER_SIZE, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
97 PWDS_MAGIC(p) = PWDS_MAGIC_CHAR;
98 PWDS_VERSION(p) = 1;
99 PWDS_ENC(p) = DAV_KEY_AES256;
100 PWDS_PWFUNC(p) = DAV_PWFUNC_PBKDF2_SHA256;
101 dav_rand_bytes((unsigned char*)p->content->space+4, 16);
102 p->isdecrypted = 1;
103 p->encoffset = PWDS_HEADER_SIZE;
104 return p;
105 }
106
107 static int readval(CxBuffer *in, char **val, int allowzero) {
108 // value = length string
109 // length = uint32
110 // string = bytes
111
112 *val = NULL;
113
114 // get length
115 uint32_t length = 0;
116 if(cxBufferRead(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) {
117 return 0;
118 }
119 length = ntohl(length); // convert from BE to host byte order
120 if(length == 0) {
121 if(allowzero) {
122 return 1;
123 } else {
124 return 0;
125 }
126 }
127 if(length > PWDSTORE_MAX_LEN) {
128 return 0;
129 }
130
131 // get value
132 char *value = malloc(length + 1);
133 value[length] = 0;
134 if(cxBufferRead(value, 1, length, in) != length) {
135 free(value);
136 return 0;
137 }
138
139 *val = value;
140 return 1;
141 }
142
143 static int read_indexentry(PwdStore *p, CxBuffer *in) {
144 // read type of index element
145 int type = cxBufferGet(in);
146 if(type == EOF || type != 0) {
147 // only type 0 supported yet
148 return 0;
149 }
150
151 char *id = NULL;
152 CxList *locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
153 cxDefineDestructor(locations, free);
154
155 // get id (required)
156 int ret = 0;
157 if(readval(in, &id, FALSE)) {
158 // get locations
159 char *location = NULL;
160 while((ret = readval(in, &location, TRUE)) == 1) {
161 if(!location) {
162 break;
163 }
164 cxListAdd(locations, location);
165 }
166 }
167
168 if(ret) {
169 pwdstore_put_index(p, id, locations);
170 } else {
171 if(id) free(id);
172 cxListDestroy(locations);
173 }
174
175 return ret;
176 }
177
178 static int read_pwdentry(PwdStore *p, CxBuffer *in) {
179 int type = cxBufferGet(in);
180 if(type == EOF || type != 0) {
181 // only type 0 supported yet
182 return 0;
183 }
184
185 char *id = NULL;
186 char *user = NULL;
187 char *password = NULL;
188
189 int ret = 0;
190 if(readval(in, &id, FALSE)) {
191 if(readval(in, &user, FALSE)) {
192 if(readval(in, &password, FALSE)) {
193 pwdstore_put(p, id, user, password);
194 ret = 1;
195 }
196 }
197 }
198
199 if(id) free(id);
200 if(user) free(user);
201 if(password) free(password);
202
203 return ret;
204 }
205
206 static void remove_list_entries(PwdStore *s, const char *id) {
207 CxIterator i = cxListMutIterator(s->locations);
208 cx_foreach(PwdIndexEntry*, ie, i) {
209 if(!strcmp(ie->id, id)) {
210 cxIteratorFlagRemoval(i);
211 cxIteratorNext(i);
212 break;
213 }
214 }
215 i = cxListMutIterator(s->noloc);
216 cx_foreach(PwdIndexEntry*, ie, i) {
217 if(!strcmp(ie->id, id)) {
218 cxIteratorFlagRemoval(i);
219 cxIteratorNext(i);
220 break;
221 }
222 }
223 }
224
225 void pwdstore_remove_entry(PwdStore *s, const char *id) {
226 remove_list_entries(s, id);
227
228 CxHashKey key = cx_hash_key_str(id);
229 PwdIndexEntry *i = cxMapRemoveAndGet(s->index, key);
230 PwdEntry *e = cxMapRemoveAndGet(s->ids, key);
231
232 if(i) {
233 cxListDestroy(i->locations);
234 free(i->id);
235 free(i);
236 }
237 if(e) {
238 free(e->id);
239 free(e->user);
240 free(e->password);
241 free(e);
242 }
243 }
244
245 int pwdstore_getindex(PwdStore *s) {
246 uint32_t netindexlen;
247
248 // set the position to the last 4 bytes of the header
249 // for reading index length
250 s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
251
252 // read indexlen and convert to host byte order
253 if(cxBufferRead(&netindexlen, 1, sizeof(uint32_t), s->content) != sizeof(uint32_t)) {
254 return 1;
255 }
256 uint32_t indexlen = ntohl(netindexlen);
257
258 // integer overflow check
259 if(UINT32_MAX - PWDS_HEADER_SIZE < indexlen) {
260 return 1;
261 }
262 if(s->content->size < PWDS_HEADER_SIZE + indexlen) {
263 return 1;
264 }
265 // encrypted content starts after the index content
266 s->encoffset = PWDS_HEADER_SIZE + indexlen;
267
268 // the index starts after the header
269 CxBuffer *index = cxBufferCreate(s->content->space+PWDS_HEADER_SIZE, indexlen, cxDefaultAllocator, 0);
270 index->size = indexlen;
271
272 // read index
273 while(read_indexentry(s, index)) {}
274
275 // free index buffer structure (not the content)
276 cxBufferFree(index);
277
278 return 0;
279 }
280
281 int pwdstore_decrypt(PwdStore *p) {
282 if(!p->key) {
283 return 1;
284 }
285 if(p->isdecrypted) {
286 return 0;
287 }
288
289 // decrypt contet
290 size_t encsz = p->content->size - p->encoffset;
291 CxBuffer *enc = cxBufferCreate(p->content->space + p->encoffset, encsz, cxDefaultAllocator, 0);
292 enc->size = encsz;
293 enc->size = p->content->size - p->encoffset;
294 CxBuffer *content = aes_decrypt_buffer(enc, p->key);
295 cxBufferFree(enc);
296 if(!content) {
297 return 1;
298 }
299
300 while(read_pwdentry(p, content)) {}
301
302 cxBufferFree(content);
303
304 return 0;
305 }
306
307 int pwdstore_setpassword(PwdStore *p, const char *password) {
308 DavKey *key = dav_pw2key(
309 password,
310 (unsigned char*)(p->content->space + 4),
311 16,
312 PWDS_PWFUNC(p),
313 PWDS_ENC(p));
314 if(!key) {
315 return 1;
316 }
317
318 p->key = key;
319 return 0;
320 }
321
322 void pwdstore_encsettings(PwdStore *p, uint8_t enc, uint8_t pwfunc) {
323 PWDS_ENC(p) = enc;
324 PWDS_PWFUNC(p) = pwfunc;
325 }
326
327 void pwdstore_free_entry(PwdEntry *e) {
328 if(e->id) free(e->id);
329 if(e->user) free(e->user);
330 if(e->password) free(e->password);
331 free(e);
332 }
333
334 void pwdstore_free(PwdStore* p) {
335 cxDefineDestructor(p->ids, pwdstore_free_entry);
336 cxMapDestroy(p->ids);
337
338 cxListDestroy(p->locations);
339
340 if(p->content) {
341 cxBufferFree(p->content);
342 }
343
344 free(p);
345 }
346
347 int pwdstore_has_id(PwdStore *s, const char *id) {
348 return cxMapGet(s->index, cx_hash_key_str(id)) ? 1 : 0;
349 }
350
351 PwdEntry* pwdstore_get(PwdStore *p, const char *id) {
352 PwdEntry *e = cxMapGet(p->ids, cx_hash_key_str(id));
353 if(e && e->user && e->password) {
354 return e;
355 } else {
356 return NULL;
357 }
358 }
359
360 void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) {
361 PwdEntry *entry = malloc(sizeof(PwdEntry));
362 entry->id = strdup(id);
363 entry->user = strdup(username);
364 entry->password = strdup(password);
365 cxMapPut(p->ids, cx_hash_key_str(id), entry);
366 }
367
368 void pwdstore_put_index(PwdStore *p, char *id, CxList *locations) {
369 PwdIndexEntry *e = cxMapGet(p->index, cx_hash_key_str(id));
370 if(e) {
371 return;
372 }
373 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
374 newentry->id = id;
375 if(locations) {
376 newentry->locations = locations;
377 cxListAdd(p->locations, newentry);
378 } else {
379 newentry->locations = NULL;
380 cxListAdd(p->noloc, newentry);
381 }
382 cxMapPut(p->index, cx_hash_key_str(id), newentry);
383 }
384
385 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) {
386 uint32_t idlen = strlen(e->id);
387 uint32_t netidlen = htonl(idlen);
388
389 cxBufferPut(out, 0); // type
390
391 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out);
392 cxBufferWrite(e->id, 1, idlen, out);
393
394 CxIterator i = cxListIterator(e->locations);
395 cx_foreach(char *, location, i) {
396 uint32_t locationlen = strlen(location);
397 uint32_t netlocationlen = htonl(locationlen);
398
399 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out);
400 cxBufferWrite(location, 1, locationlen, out);
401 }
402
403 uint32_t terminate = 0;
404 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out);
405 }
406
407 int pwdstore_store(PwdStore *p, const char *file) {
408 if(!p->key) {
409 return 1;
410 }
411
412 CxBuffer *index = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
413 CxBuffer *content = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
414
415 // create index
416 CxIterator i = cxListIterator(p->noloc);
417 cx_foreach(PwdIndexEntry*, e, i) {
418 write_index_entry(index, e);
419 }
420 i = cxListIterator(p->locations);
421 cx_foreach(PwdIndexEntry*, e, i) {
422 write_index_entry(index, e);
423 }
424
425 i = cxMapIteratorValues(p->ids);
426 cx_foreach(PwdEntry*, value, i) {
427 if(!value->id || !value->user || !value->password) {
428 continue;
429 }
430
431 uint32_t idlen = strlen(value->id);
432 uint32_t ulen = strlen(value->user);
433 uint32_t plen = strlen(value->password);
434 uint32_t netidlen = htonl(idlen);
435 uint32_t netulen = htonl(ulen);
436 uint32_t netplen = htonl(plen);
437
438 // content buffer
439 cxBufferPut(content, 0); // type
440
441 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), content);
442 cxBufferWrite(value->id, 1, idlen, content);
443 cxBufferWrite(&netulen, 1, sizeof(uint32_t), content);
444 cxBufferWrite(value->user, 1, ulen, content);
445 cxBufferWrite(&netplen, 1, sizeof(uint32_t), content);
446 cxBufferWrite(value->password, 1, plen, content);
447 }
448
449 content->pos = 0;
450 CxBuffer *enc = aes_encrypt_buffer(content, p->key);
451
452 p->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
453 p->content->size = PWDS_HEADER_SIZE;
454
455 // add index after header
456 uint32_t netindexlen = htonl((uint32_t)index->size);
457 cxBufferWrite(&netindexlen, 1, sizeof(uint32_t), p->content);
458 cxBufferWrite(index->space, 1, index->size, p->content);
459
460 // add encrypted buffer
461 cxBufferWrite(enc->space, 1, enc->size, p->content);
462
463 cxBufferFree(enc);
464
465 FILE *out = fopen(file, "w");
466 if(!out) {
467 return 1;
468 }
469 fwrite(p->content->space, 1, p->content->size, out);
470 fclose(out);
471
472 return 0;
473 }
474
475 int pwdstore_decrypt_secrets(PwdStore *secrets) {
476 if(!pw_input) {
477 return 1;
478 }
479
480 char *ps_password = NULL;
481 if(secrets->unlock_cmd && strlen(secrets->unlock_cmd) > 0) {
482 CxBuffer *cmd_out = cxBufferCreate(NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
483 if(!util_exec_command(secrets->unlock_cmd, cmd_out)) {
484 // command successful, get first line from output without newline
485 // and use that as password for the secretstore
486 size_t len = 0;
487 for(size_t i=0;i<=cmd_out->size;i++) {
488 if(i == cmd_out->size || cmd_out->space[i] == '\n') {
489 len = i;
490 break;
491 }
492 }
493 if(len > 0) {
494 ps_password = malloc(len + 1);
495 memcpy(ps_password, cmd_out->space, len);
496 ps_password[len] = 0;
497 }
498 }
499 cxBufferFree(cmd_out);
500 }
501
502 if(!ps_password) {
503 ps_password = pw_input(pw_input_data);
504 if(!ps_password) {
505 return 1;
506 }
507 }
508
509 int err = pwdstore_setpassword(secrets, ps_password);
510 free(ps_password);
511 if(err) {
512 fprintf(stderr, "Error: cannot create key from password\n");
513 return 1;
514 }
515 if(pwdstore_decrypt(secrets)) {
516 fprintf(stderr, "Error: cannot decrypt secrets store\n");
517 return 1;
518 }
519 return 0;
520 }

mercurial