dav/pwd.c

Fri, 04 Jun 2021 18:24:55 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 04 Jun 2021 18:24:55 +0200
changeset 728
35a421f441d5
parent 654
8f2b8f2a5cde
child 731
e0358fa1a3b1
permissions
-rw-r--r--

add stream API

470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include "pwd.h"
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <ucx/buffer.h>
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <ucx/utils.h>
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
608
3e4c0285a868 fix build on windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 515
diff changeset
37 #ifdef _WIN32
3e4c0285a868 fix build on windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 515
diff changeset
38 #include <winsock.h>
3e4c0285a868 fix build on windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 515
diff changeset
39 #else
3e4c0285a868 fix build on windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 515
diff changeset
40 #include <netinet/in.h>
3e4c0285a868 fix build on windows
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 515
diff changeset
41 #endif
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 PwdStore* pwdstore_open(const char *file) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 FILE *in = fopen(file, "r");
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 if(!in) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 return NULL;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 UcxBuffer *buf = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 ucx_stream_copy(in, buf, (read_func)fread, (write_func)ucx_buffer_write);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 fclose(in);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 if(buf->size < PWDS_HEADER_SIZE || buf->space[0] != PWDS_MAGIC_CHAR) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 ucx_buffer_free(buf);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 return NULL;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 PwdStore *p = malloc(sizeof(PwdStore));
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
59 p->ids = ucx_map_new(16);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
60 p->locations = NULL;
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
61 p->noloc = NULL;
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
62 p->index = ucx_map_new(16);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 p->content = buf;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 p->key = NULL;
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
65 p->encoffset = PWDS_HEADER_SIZE;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 p->isdecrypted = 0;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
68 if(pwdstore_getindex(p)) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
69 pwdstore_free(p);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
70 return NULL;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
71 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
72
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 return p;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 PwdStore* pwdstore_new(void) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 PwdStore *p = calloc(1, sizeof(PwdStore));
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
78 p->ids = ucx_map_new(16);
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
79 p->locations = NULL;
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
80 p->noloc = NULL;
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
81 p->index = ucx_map_new(16);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 p->content = ucx_buffer_new(NULL, PWDS_HEADER_SIZE, UCX_BUFFER_AUTOEXTEND);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 PWDS_MAGIC(p) = PWDS_MAGIC_CHAR;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 PWDS_VERSION(p) = 1;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 PWDS_ENC(p) = DAV_KEY_AES256;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 PWDS_PWFUNC(p) = DAV_PWFUNC_PBKDF2_SHA256;
488
29b979ca8750 adds dav_pw2key commoncrypto implementation
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 474
diff changeset
87 dav_rand_bytes((unsigned char*)p->content->space+4, 16);
473
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
88 p->isdecrypted = 1;
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
89 p->encoffset = PWDS_HEADER_SIZE;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 return p;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
93 static int readval(UcxBuffer *in, char **val, int allowzero) {
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
94 // value = length string
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
95 // length = uint32
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
96 // string = bytes
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
97
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
98 *val = NULL;
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
99
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
100 // get length
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
101 uint32_t length = 0;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
102 if(ucx_buffer_read(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
103 return 0;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
104 }
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
105 length = ntohl(length); // convert from BE to host byte order
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
106 if(length == 0) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
107 if(allowzero) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
108 return 1;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
109 } else {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
110 return 0;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
111 }
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
112 }
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
113 if(length > PWDSTORE_MAX_LEN) {
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
114 return 0;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
115 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
116
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
117 // get value
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
118 char *value = malloc(length + 1);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
119 value[length] = 0;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
120 if(ucx_buffer_read(value, 1, length, in) != length) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
121 free(value);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
122 return 0;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
123 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
124
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
125 *val = value;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
126 return 1;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
127 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
128
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
129 static int read_indexentry(PwdStore *p, UcxBuffer *in) {
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
130 // read type of index element
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
131 int type = ucx_buffer_getc(in);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
132 if(type == EOF || type != 0) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
133 // only type 0 supported yet
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
134 return 0;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
135 }
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
136
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
137 char *id = NULL;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
138 UcxList *locations = NULL;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
139
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
140 // get id (required)
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
141 int ret = 0;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
142 if(readval(in, &id, FALSE)) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
143 ret = 1;
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
144 // get locations
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
145 char *location = NULL;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
146 while((ret = readval(in, &location, TRUE)) == 1) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
147 if(!location) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
148 break;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
149 }
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
150 locations = ucx_list_append(locations, location);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
151 }
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
152 }
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
153
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
154 if(ret) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
155 pwdstore_put_index(p, id, locations);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
156 } else {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
157 if(id) free(id);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
158 ucx_list_free_content(locations, free);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
159 }
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
160
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
161 return ret;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
162 }
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
163
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
164 static int read_pwdentry(PwdStore *p, UcxBuffer *in) {
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 int type = ucx_buffer_getc(in);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 if(type == EOF || type != 0) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 // only type 0 supported yet
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 return 0;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
171 char *id = NULL;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
172 char *location = NULL;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
173 char *user = NULL;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
174 char *password = NULL;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
176 int ret = 0;
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
177 if(readval(in, &id, FALSE)) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
178 if(readval(in, &user, FALSE)) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
179 if(readval(in, &password, FALSE)) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
180 pwdstore_put(p, id, user, password);
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
181 ret = 1;
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
182 }
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
183 }
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
186 if(id) free(id);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
187 if(location) free(location);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
188 if(user) free(user);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
189 if(password) free(password);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
190
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
191 return ret;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
192 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
193
654
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
194 static int remove_list_entries(PwdStore *s, const char *id) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
195 int ret = 0;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
196
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
197 UcxList *loc_entry = NULL;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
198 UcxList *noloc_entry = NULL;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
199 UCX_FOREACH(elm, s->locations) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
200 PwdIndexEntry *ie = elm->data;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
201 if(!strcmp(ie->id, id)) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
202 loc_entry = elm;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
203 ret = 1;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
204 break;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
205 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
206 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
207 UCX_FOREACH(elm, s->noloc) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
208 PwdIndexEntry *ie = elm->data;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
209 if(!strcmp(ie->id, id)) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
210 noloc_entry = elm;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
211 ret = 1;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
212 break;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
213 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
214 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
215
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
216 if(loc_entry) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
217 s->locations = ucx_list_remove(s->locations, loc_entry);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
218 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
219 if(noloc_entry) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
220 s->noloc = ucx_list_remove(s->noloc, noloc_entry);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
221 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
222
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
223 return ret;
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
224 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
225
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
226 void pwdstore_remove_entry(PwdStore *s, const char *id) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
227 while(remove_list_entries(s, id)) {}
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
228
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
229 PwdIndexEntry *i = ucx_map_cstr_remove(s->index, id);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
230 PwdEntry *e = ucx_map_cstr_remove(s->ids, id);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
231
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
232 if(i) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
233 ucx_list_free_content(i->locations, free);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
234 ucx_list_free(i->locations);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
235 free(i->id);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
236 free(i);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
237 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
238 if(e) {
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
239 free(e->id);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
240 free(e->user);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
241 free(e->password);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
242 free(e);
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
243 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
244 }
8f2b8f2a5cde add remove-user and list-users commands
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 653
diff changeset
245
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
246 int pwdstore_getindex(PwdStore *s) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
247 uint32_t netindexlen;
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
248
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
249 // set the position to the last 4 bytes of the header
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
250 // for reading index length
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
251 s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
252
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
253 // read indexlen and convert to host byte order
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
254 if(ucx_buffer_read(&netindexlen, 1, sizeof(uint32_t), s->content) != sizeof(uint32_t)) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
255 return 1;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
257 uint32_t indexlen = ntohl(netindexlen);
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
258
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
259 // integer overflow check
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
260 if(UINT32_MAX - PWDS_HEADER_SIZE < indexlen) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
261 return 1;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
262 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
263 if(s->content->size < PWDS_HEADER_SIZE + indexlen) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
264 return 1;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 }
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
266 // encrypted content starts after the index content
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
267 s->encoffset = PWDS_HEADER_SIZE + indexlen;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
269 // the index starts after the header
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
270 UcxBuffer *index = ucx_buffer_new(s->content->space+PWDS_HEADER_SIZE, indexlen, 0);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
271 index->size = indexlen;
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
272
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
273 // read index
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
274 while(read_indexentry(s, index)) {}
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275
653
f503c272bd70 update secret store format description
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 608
diff changeset
276 // free index buffer structure (not the content)
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
277 ucx_buffer_free(index);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
278
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
279 return 0;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 int pwdstore_decrypt(PwdStore *p) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 if(!p->key) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 return 1;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 }
473
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
286 if(p->isdecrypted) {
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
287 return 0;
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
288 }
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 // decrypt contet
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
291 size_t encsz = p->content->size - p->encoffset;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
292 UcxBuffer *enc = ucx_buffer_new(p->content->space + p->encoffset, encsz, 0);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 enc->size = encsz;
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
294 enc->size = p->content->size - p->encoffset;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 UcxBuffer *content = aes_decrypt_buffer(enc, p->key);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 ucx_buffer_free(enc);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 if(!content) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 return 1;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
301 while(read_pwdentry(p, content)) {}
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 ucx_buffer_free(content);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 return 0;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 int pwdstore_setpassword(PwdStore *p, const char *password) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 DavKey *key = dav_pw2key(
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 password,
515
2465dd550bb5 fixes signedness of salt in dav_pw2key()
Mike Becker <universe@uap-core.de>
parents: 489
diff changeset
311 (unsigned char*)(p->content->space + 4),
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 16,
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 PWDS_PWFUNC(p),
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 PWDS_ENC(p));
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 if(!key) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 return 1;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 p->key = key;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 return 0;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 void pwdstore_encsettings(PwdStore *p, uint8_t enc, uint8_t pwfunc) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 PWDS_ENC(p) = enc;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 PWDS_PWFUNC(p) = pwfunc;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327
473
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
328 void pwdstore_free_entry(PwdEntry *e) {
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
329 if(e->id) free(e->id);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
330 if(e->user) free(e->user);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
331 if(e->password) free(e->password);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 free(e);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 void pwdstore_free(PwdStore* p) {
473
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
336 ucx_map_free_content(p->ids, (ucx_destructor)pwdstore_free_entry);
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
337 ucx_map_free(p->ids);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
338
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
339 ucx_list_free(p->locations);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 if(p->content) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 ucx_buffer_free(p->content);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 free(p);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
348 int pwdstore_has_id(PwdStore *s, const char *id) {
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
349 return ucx_map_cstr_get(s->index, id) ? 1 : 0;
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
352 PwdEntry* pwdstore_get(PwdStore *p, const char *id) {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
353 PwdEntry *e = ucx_map_cstr_get(p->ids, id);
473
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
354 if(e && e->user && e->password) {
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
355 return e;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
356 } else {
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
357 return NULL;
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
358 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
359 }
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
360
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
361 void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) {
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 PwdEntry *entry = malloc(sizeof(PwdEntry));
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
363 entry->id = strdup(id);
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
364 entry->user = strdup(username);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
365 entry->password = strdup(password);
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
366 ucx_map_cstr_put(p->ids, id, entry);
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
367 }
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
368
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
369 void pwdstore_put_index(PwdStore *p, char *id, UcxList *locations) {
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
370 PwdIndexEntry *e = ucx_map_cstr_get(p->index, id);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
371 if(e) {
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
372 return;
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
373 }
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
374 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
375 newentry->id = id;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
376 if(locations) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
377 newentry->locations = locations;
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
378 p->locations = ucx_list_append(p->locations, newentry);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
379 } else {
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
380 newentry->locations = NULL;
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
381 p->noloc = ucx_list_append(p->noloc, newentry);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
382 }
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
383 ucx_map_cstr_put(p->index, id, newentry);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
384 }
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
385
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
386 void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) {
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
387 uint32_t idlen = strlen(e->id);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
388 uint32_t netidlen = htonl(idlen);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
389
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
390 ucx_buffer_putc(out, 0); // type
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
391
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
392 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
393 ucx_buffer_write(e->id, 1, idlen, out);
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
394
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
395 UCX_FOREACH(elm, e->locations) {
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
396 char *location = elm->data;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
397 uint32_t locationlen = strlen(location);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
398 uint32_t netlocationlen = htonl(locationlen);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
399
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
400 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out);
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
401 ucx_buffer_write(location, 1, locationlen, out);
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
402 }
489
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
403
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
404 uint32_t terminate = 0;
fb69eae42ef0 credentials can have multiple locations now
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 488
diff changeset
405 ucx_buffer_write(&terminate, 1, sizeof(uint32_t), out);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 int pwdstore_store(PwdStore *p, const char *file) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409 if(!p->key) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 return 1;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
413 UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415
474
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
416 // create index
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
417 UCX_FOREACH(elm, p->noloc) {
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
418 PwdIndexEntry *e = elm->data;
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
419 write_index_entry(index, e);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
420 }
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
421 UCX_FOREACH(elm, p->locations) {
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
422 PwdIndexEntry *e = elm->data;
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
423 write_index_entry(index, e);
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
424 }
017a4f09e6fa improves secret store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 473
diff changeset
425
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
426 UcxMapIterator i = ucx_map_iterator(p->ids);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 PwdEntry *value;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 UCX_MAP_FOREACH(key, value, i) {
473
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
429 if(!value->id || !value->user || !value->password) {
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
430 continue;
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
431 }
6740adb5fccd adds support for location credentials
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 472
diff changeset
432
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
433 uint32_t idlen = strlen(value->id);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 uint32_t ulen = strlen(value->user);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 uint32_t plen = strlen(value->password);
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
436 uint32_t netidlen = htonl(idlen);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 uint32_t netulen = htonl(ulen);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 uint32_t netplen = htonl(plen);
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
439
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
440 // content buffer
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
441 ucx_buffer_putc(content, 0); // type
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
442
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
443 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
444 ucx_buffer_write(value->id, 1, idlen, content);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 ucx_buffer_write(value->user, 1, ulen, content);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 ucx_buffer_write(value->password, 1, plen, content);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 }
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
450
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 content->pos = 0;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 UcxBuffer *enc = aes_encrypt_buffer(content, p->key);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
454 p->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 p->content->size = PWDS_HEADER_SIZE;
472
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
456
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
457 // add index after header
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
458 uint32_t netindexlen = htonl((uint32_t)index->size);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
459 ucx_buffer_write(&netindexlen, 1, sizeof(uint32_t), p->content);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
460 ucx_buffer_write(index->space, 1, index->size, p->content);
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
461
08d2d1263429 new secrets store file format
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 470
diff changeset
462 // add encrypted buffer
470
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 ucx_buffer_write(enc->space, 1, enc->size, p->content);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465 ucx_buffer_free(enc);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467 FILE *out = fopen(file, "w");
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 if(!out) {
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 return 1;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 }
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 fwrite(p->content->space, 1, p->content->size, out);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 fclose(out);
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 return 0;
6bf798ad3aec adds encrypted password store
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 }

mercurial