application/pwd.c

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

mercurial