projects/libpws

view pws-field.c @ 13:2bb1bbac1d0a

Added tag version-1.0.0 for changeset 1926dfc9feb0
author Guido Berhoerster <guido+libpws@berhoerster.name>
date Sun Aug 04 21:37:56 2019 +0200 (2019-08-04)
parents 1c0e7f79e737
children
line source
1 /*
2 * Copyright (C) 2015 Guido Berhoerster <guido+libpws@berhoerster.name>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included
13 * in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
24 #include "compat.h"
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
30 #include "pws-internal.h"
32 static const enum pws_data_type header_data_types[256] = {
33 [PWS3_HEADER_FIELD_VERSION] = PWS_DATA_TYPE_UINT16,
34 [PWS3_HEADER_FIELD_UUID] = PWS_DATA_TYPE_UUID,
35 [PWS3_HEADER_FIELD_NON_DEFAULT_PREFERENCES] = PWS_DATA_TYPE_TEXT,
36 [PWS3_HEADER_FIELD_TREE_DISPLAY_STATUS] = PWS_DATA_TYPE_TEXT,
37 [PWS3_HEADER_FIELD_SAVE_TIMESTAMP] = PWS_DATA_TYPE_TIME,
38 [PWS3_HEADER_FIELD_SAVE_USER_HOST] = PWS_DATA_TYPE_TEXT,
39 [PWS3_HEADER_FIELD_SAVE_APPLICATION] = PWS_DATA_TYPE_TEXT,
40 [PWS3_HEADER_FIELD_SAVE_USER] = PWS_DATA_TYPE_TEXT,
41 [PWS3_HEADER_FIELD_SAVE_HOST] = PWS_DATA_TYPE_TEXT,
42 [PWS3_HEADER_FIELD_DATABASE_NAME] = PWS_DATA_TYPE_TEXT,
43 [PWS3_HEADER_FIELD_DATABASE_DESCRIPTION] = PWS_DATA_TYPE_TEXT,
44 [PWS3_HEADER_FIELD_DATABASE_FILTERS] = PWS_DATA_TYPE_TEXT,
45 [PWS3_HEADER_FIELD_RESERVED_1] = PWS_DATA_TYPE_BYTES,
46 [PWS3_HEADER_FIELD_RESERVED_2] = PWS_DATA_TYPE_BYTES,
47 [PWS3_HEADER_FIELD_RESERVED_3] = PWS_DATA_TYPE_BYTES,
48 [PWS3_HEADER_FIELD_RECENTLY_USED_ENTRIES] = PWS_DATA_TYPE_TEXT,
49 [PWS3_HEADER_FIELD_NAMED_PASSWORD_POLICIES] = PWS_DATA_TYPE_TEXT,
50 [PWS3_HEADER_FIELD_EMPTY_GROUPS] = PWS_DATA_TYPE_TEXT,
51 [PWS3_HEADER_FIELD_YUBICO] = PWS_DATA_TYPE_TEXT,
52 [PWS3_HEADER_FIELD_PASSWORD_TIMESTAMP] = PWS_DATA_TYPE_TIME
53 };
55 static const enum pws_data_type record_data_types[256] = {
56 [PWS3_RECORD_FIELD_UUID] = PWS_DATA_TYPE_UUID,
57 [PWS3_RECORD_FIELD_GROUP] = PWS_DATA_TYPE_TEXT,
58 [PWS3_RECORD_FIELD_TITLE] = PWS_DATA_TYPE_TEXT,
59 [PWS3_RECORD_FIELD_USERNAME] = PWS_DATA_TYPE_TEXT,
60 [PWS3_RECORD_FIELD_NOTES] = PWS_DATA_TYPE_TEXT,
61 [PWS3_RECORD_FIELD_PASSWORD] = PWS_DATA_TYPE_TEXT,
62 [PWS3_RECORD_FIELD_CREATION_TIME] = PWS_DATA_TYPE_TIME,
63 [PWS3_RECORD_FIELD_PASSWORD_MODIFICATION_TIME] = PWS_DATA_TYPE_TIME,
64 [PWS3_RECORD_FIELD_ACCESS_TIME] = PWS_DATA_TYPE_TIME,
65 [PWS3_RECORD_FIELD_PASSWORD_EXPIRY_TIME] = PWS_DATA_TYPE_TIME,
66 [PWS3_RECORD_FIELD_RESERVED_1] = PWS_DATA_TYPE_BYTES,
67 [PWS3_RECORD_FIELD_MODIFICATION_TIME] = PWS_DATA_TYPE_TIME,
68 [PWS3_RECORD_FIELD_URL] = PWS_DATA_TYPE_TEXT,
69 [PWS3_RECORD_FIELD_AUTOTYPE] = PWS_DATA_TYPE_TEXT,
70 [PWS3_RECORD_FIELD_PASSWORD_HISTORY] = PWS_DATA_TYPE_TEXT,
71 [PWS3_RECORD_FIELD_PASSWORD_POLICY] = PWS_DATA_TYPE_TEXT,
72 [PWS3_RECORD_FIELD_PASSWORD_EXPIRY_INTERVAL] = PWS_DATA_TYPE_UINT32,
73 [PWS3_RECORD_FIELD_RUN_COMMAND] = PWS_DATA_TYPE_TEXT,
74 [PWS3_RECORD_FIELD_DOUBLE_CLICK_ACTION] = PWS_DATA_TYPE_BYTES,
75 [PWS3_RECORD_FIELD_EMAIL_ADDRESS] = PWS_DATA_TYPE_TEXT,
76 [PWS3_RECORD_FIELD_PROTECTED] = PWS_DATA_TYPE_UINT8,
77 [PWS3_RECORD_FIELD_ALLOWED_PASSWORD_SYMBOLS] = PWS_DATA_TYPE_TEXT,
78 [PWS3_RECORD_FIELD_SHIFT_DOUBLE_CLICK_ACTION] = PWS_DATA_TYPE_BYTES,
79 [PWS3_RECORD_FIELD_PASSWORD_POLICY_NAME] = PWS_DATA_TYPE_TEXT,
80 [PWS3_RECORD_FIELD_KEYBOARD_SHORTCUT] = PWS_DATA_TYPE_BYTES,
81 [PWS3_RECORD_FIELD_RESERVED_2] = PWS_DATA_TYPE_UUID,
82 [PWS3_RECORD_FIELD_TWO_FACTOR_KEY] = PWS_DATA_TYPE_BYTES,
83 [PWS3_RECORD_FIELD_CREDIT_CARD_NUMBER] = PWS_DATA_TYPE_TEXT,
84 [PWS3_RECORD_FIELD_CREDIT_CARD_EXPIRATION] = PWS_DATA_TYPE_TEXT,
85 [PWS3_RECORD_FIELD_CREDIT_CARD_VERIFICATION_VALUE] = PWS_DATA_TYPE_TEXT,
86 [PWS3_RECORD_FIELD_CREDIT_CARD_PIN] = PWS_DATA_TYPE_TEXT,
87 [PWS3_RECORD_FIELD_QR_CODE] = PWS_DATA_TYPE_TEXT,
88 [PWS3_RECORD_FIELD_TESTING_1] = PWS_DATA_TYPE_BYTES
89 };
91 struct pws3_field *
92 pws3_field_create(int is_header, uint8_t field_type)
93 {
94 struct pws3_field *field;
96 field = pws_alloc(sizeof (struct pws3_field));
97 if (field == NULL) {
98 return (NULL);
99 }
101 field->is_header = is_header;
102 field->field_type = field_type;
103 field->size = 0;
104 switch (pws3_field_get_data_type(field)) {
105 case PWS_DATA_TYPE_BYTES:
106 field->value.bytes = NULL;
107 break;
108 case PWS_DATA_TYPE_UUID:
109 memset(field->value.uuid, 0, PWS3_UUID_SIZE);
110 break;
111 case PWS_DATA_TYPE_TEXT:
112 field->value.text = NULL;
113 break;
114 case PWS_DATA_TYPE_UINT8:
115 field->value.uint8 = 0;
116 break;
117 case PWS_DATA_TYPE_UINT16:
118 field->value.uint16 = 0;
119 break;
120 case PWS_DATA_TYPE_TIME: /* FALLTHROUGH */
121 case PWS_DATA_TYPE_UINT32:
122 field->value.uint32 = 0;
123 }
125 return (field);
126 }
128 void
129 pws3_field_destroy(struct pws3_field *field)
130 {
131 if (field == NULL) {
132 return;
133 }
135 switch (pws3_field_get_data_type(field)) {
136 case PWS_DATA_TYPE_BYTES:
137 pws_free(field->value.bytes, field->size);
138 break;
139 case PWS_DATA_TYPE_TEXT:
140 if (!field->is_header &&
141 (field->field_type == PWS3_RECORD_FIELD_PASSWORD)) {
142 pws_secure_free(field->value.text, field->size + 1);
143 } else {
144 pws_free(field->value.text, field->size + 1);
145 }
146 break;
147 default:
148 break;
149 }
151 pws_free(field, sizeof (struct pws3_field));
152 }
154 int
155 pws3_field_is_header(struct pws3_field *field)
156 {
157 return (field->is_header);
158 }
160 uint8_t
161 pws3_field_get_type(struct pws3_field *field)
162 {
163 return (field->field_type);
164 }
166 enum pws_data_type
167 pws3_field_get_data_type(struct pws3_field *field)
168 {
169 return (field->is_header ? header_data_types[field->field_type] :
170 record_data_types[field->field_type]);
171 }
173 int
174 pws3_field_set_uuid(struct pws3_field *field,
175 const unsigned char s[static PWS3_UUID_SIZE])
176 {
177 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UUID);
179 field->size = PWS3_UUID_SIZE;
180 memcpy(field->value.uuid, s, PWS3_UUID_SIZE);
182 return (0);
183 }
185 int
186 pws3_field_set_text(struct pws3_field *field, const char s[static 1])
187 {
188 size_t len;
189 char *t;
191 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TEXT);
193 len = strlen(s);
194 if (len > PWS3_MAX_FIELD_SIZE) {
195 return (-1);
196 }
198 if (!field->is_header &&
199 (field->field_type == PWS3_RECORD_FIELD_PASSWORD)) {
200 if (len > PWS3_MAX_PASSWORD_LEN) {
201 return (-1);
202 }
203 t = pws_secure_realloc(field->value.text, len + 1);
204 } else {
205 t = pws_realloc(field->value.text, len + 1);
206 }
207 if (t == NULL) {
208 return (-1);
209 }
210 field->value.text = t;
211 field->size = len;
212 memcpy(field->value.text, s, len + 1);
214 return (0);
215 }
217 int
218 pws3_field_set_time(struct pws3_field *field, time_t time)
219 {
220 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TIME);
222 field->size = 4;
223 field->value.uint32 = CLAMP(time, 0, UINT32_MAX);
225 return (0);
226 }
228 int
229 pws3_field_set_uint8(struct pws3_field *field, uint8_t u8)
230 {
231 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT8);
233 field->size = 1;
234 field->value.uint8 = u8;
236 return (0);
237 }
239 int
240 pws3_field_set_uint16(struct pws3_field *field, uint16_t u16)
241 {
242 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT16);
244 field->size = 2;
245 field->value.uint16 = u16;
247 return (0);
248 }
250 int
251 pws3_field_set_uint32(struct pws3_field *field, uint32_t u32)
252 {
253 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT32);
255 field->size = 4;
256 field->value.uint32 = u32;
258 return (0);
259 }
261 int
262 pws3_field_set_bytes(struct pws3_field *field, const unsigned char s[static 1],
263 size_t n)
264 {
265 unsigned char *t;
267 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_BYTES);
269 if (n > PWS3_MAX_FIELD_SIZE) {
270 return (-1);
271 }
273 t = pws_realloc(field->value.bytes, n);
274 if (t == NULL) {
275 return (-1);
276 }
277 field->size = n;
278 field->value.bytes = t;
279 memcpy(t, s, n);
281 return (0);
282 }
284 const unsigned char *
285 pws3_field_get_uuid(struct pws3_field *field)
286 {
287 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UUID);
289 return (field->value.uuid);
290 }
292 const char *
293 pws3_field_get_text(struct pws3_field *field)
294 {
295 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TEXT);
297 return (field->value.text);
298 }
300 time_t
301 pws3_field_get_time(struct pws3_field *field)
302 {
303 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_TIME);
305 /* assume time_t can hold at least a INT32_MAX */
306 return ((time_t)CLAMP(field->value.uint32, 0, INT32_MAX));
307 }
309 uint8_t
310 pws3_field_get_uint8(struct pws3_field *field)
311 {
312 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT8);
314 return (field->value.uint8);
315 }
317 uint16_t
318 pws3_field_get_uint16(struct pws3_field *field)
319 {
320 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT16);
322 return (field->value.uint16);
323 }
325 uint32_t
326 pws3_field_get_uint32(struct pws3_field *field)
327 {
328 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_UINT32);
330 return (field->value.uint32);
331 }
333 void
334 pws3_field_get_bytes(struct pws3_field *field, const unsigned char **sp,
335 size_t *np)
336 {
337 PWS_ASSERT(pws3_field_get_data_type(field) == PWS_DATA_TYPE_BYTES);
339 *sp = field->value.bytes;
340 *np = field->size;
341 }