3 * Encoding and decoding of key data
5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/base64.h>
35 #include <mLib/bits.h>
36 #include <mLib/dstr.h>
44 /*----- Reference counting stuff ------------------------------------------*/
46 /* --- @key_incref@ --- *
48 * Arguments: @key_data *k@ = pointer to key data
52 * Use: Increments the refcount on a key data block.
55 void key_incref(key_data
*k
) { KEY_INCREF(k
); }
57 /* --- @key_destroy@ --- *
59 * Arguments: @key_data *k@ = pointer to key data to destroy
63 * Use: Destroys a block of key data, regardless of reference count.
64 * Don't use this unless you know what you're doing.
67 void key_destroy(key_data
*k
)
69 switch (k
->e
& KF_ENCMASK
) {
74 memset(k
->u
.k
.k
, 0, k
->u
.k
.sz
);
75 sub_free(k
->u
.k
.k
, k
->u
.k
.sz
);
91 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, 0, &kd
); )
101 /* --- @key_drop@ --- *
103 * Arguments: @key_data *k@ = pointer to key data to destroy
107 * Use: Drops a reference to key data, destroying it if necessary.
110 void key_drop(key_data
*k
) { KEY_DROP(k
); }
112 /* --- @key_split@ --- *
114 * Arguments: @key_data **kk@ = address of pointer to key data block
118 * Use: Replaces @*kk@ with a pointer to the same key data, but with
119 * just one reference.
122 void key_split(key_data
**kk
)
128 switch (k
->e
& KF_ENCMASK
) {
130 *kk
= key_newbinary(k
->e
, k
->u
.k
.k
, k
->u
.k
.sz
);
133 *kk
= key_newencrypted(k
->e
, k
->u
.k
.k
, k
->u
.k
.sz
);
136 *kk
= key_newmp(k
->e
, k
->u
.m
);
139 *kk
= key_newstring(k
->e
, k
->u
.p
);
142 *kk
= key_newec(k
->e
, &k
->u
.e
);
149 *kk
= key_newstruct();
150 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &kd
); )
151 key_structset(*kk
, tag
, kd
);
159 /*----- Setting new values ------------------------------------------------*/
161 /* --- @key_newraw@ --- *
163 * Arguments: @unsigned e@ = encoding type to set
165 * Returns: New key block, not filled in.
168 key_data
*key_newraw(unsigned e
)
170 key_data
*k
= CREATE(key_data
);
176 /* --- @key_newbinary@ --- *
178 * Arguments: @unsigned e@ = other encoding flags
179 * @const void *p@ = pointer to key data
180 * @size_t sz@ = size of the key data
182 * Returns: New key data object.
185 key_data
*key_newbinary(unsigned e
, const void *p
, size_t sz
)
187 key_data
*k
= key_newraw(KENC_BINARY
| e
);
188 k
->u
.k
.k
= sub_alloc(sz
);
189 memcpy(k
->u
.k
.k
, p
, sz
);
194 /* --- @key_newencrypted@ --- *
196 * Arguments: @unsigned e@ = other encoding flags
197 * @const void *p@ = pointer to key data
198 * @size_t sz@ = size of the key data
200 * Returns: New key data object.
203 key_data
*key_newencrypted(unsigned e
, const void *p
, size_t sz
)
205 key_data
*k
= key_newraw(KENC_ENCRYPT
| e
);
206 k
->u
.k
.k
= sub_alloc(sz
);
207 memcpy(k
->u
.k
.k
, p
, sz
);
212 /* --- @key_newmp@ --- *
214 * Arguments: @unsigned e@ = other encoding flags
215 * @mp *m@ = pointer to the value to set
217 * Returns: New key data object.
220 key_data
*key_newmp(unsigned e
, mp
*m
)
222 key_data
*k
= key_newraw(KENC_MP
| e
);
227 /* --- @key_newstring@ --- *
229 * Arguments: @unsigned e@ = other encoding flags
230 * @const char *p@ = pointer to the value to set
232 * Returns: New key data object.
235 key_data
*key_newstring(unsigned e
, const char *p
)
237 key_data
*k
= key_newraw(KENC_STRING
| e
);
242 /* --- @key_newec@ --- *
244 * Arguments: @unsigned e@ = other encoding flags
245 * @const ec *pt@ = pointer to the value to set
247 * Returns: New key data object.
250 key_data
*key_newec(unsigned e
, const ec
*pt
)
252 key_data
*k
= key_newraw(KENC_EC
| e
);
254 EC_COPY(&k
->u
.e
, pt
);
258 /* --- @key_newstruct@ --- *
262 * Returns: New key data object.
265 key_data
*key_newstruct(void)
267 key_data
*k
= key_newraw(KENC_STRUCT
);
272 /* --- @key_structfind@ --- *
274 * Arguments: @key_data *k@ = pointer to key data block
275 * @const char *tag@ = pointer to tag string
277 * Returns: Pointer to key data block, or null.
279 * Use: Looks up the tag in a structured key.
282 key_data
*key_structfind(key_data
*k
, const char *tag
)
285 assert(((void)"Key is not structured",
286 (k
->e
& KF_ENCMASK
) == KENC_STRUCT
));
287 ks
= sym_find(&k
->u
.s
, tag
, -1, 0, 0);
293 /* --- @key_mksubkeyiter@ --- *
295 * Arguments: @key_subkeyiter *i@ = pointer to iterator block
296 * @key_data *k@ = pointer to key data block
300 * Use: Initializes a subkey iterator.
303 void key_mksubkeyiter(key_subkeyiter
*i
, key_data
*k
)
305 assert(((void)"Key is not structured",
306 (k
->e
& KF_ENCMASK
) == KENC_STRUCT
));
307 sym_mkiter(&i
->i
, &k
->u
.s
);
310 /* --- @key_nextsubkey@ --- *
312 * Arguments: @key_structiter *i@ = pointer to iterator block
313 * @const char **tag@ = where to put the tag pointer, or null
314 * @key_data **kd@ = where to put the key data pointer, or null
316 * Returns: Nonzero if there was another item, zero if we hit the
319 * Use: Collects the next subkey of a structured key.
322 int key_nextsubkey(key_subkeyiter
*i
, const char **tag
, key_data
**kd
)
326 if ((ks
= sym_next(&i
->i
)) == 0)
328 if (tag
) *tag
= SYM_NAME(ks
);
333 /* --- @key_structset@, @key_structsteal@ --- *
335 * Arguments: @key_data *k@ = pointer to key data block
336 * @const char *tag@ = pointer to tag string
337 * @key_data *kd@ = new key data to store
341 * Use: Creates a new subkey. Stealing doesn't affect @kd@'s
342 * refcount. If @kd@ is null, the subkey is deleted.
345 static void structset(key_data
*k
, int stealp
,
346 const char *tag
, key_data
*kd
)
351 assert(((void)"Key is not structured", k
->e
== KENC_STRUCT
));
352 assert(((void)"Key has multiple references", k
->ref
== 1));
354 ks
= sym_find(&k
->u
.s
, tag
, -1, 0, 0);
355 if (ks
) sym_remove(&k
->u
.s
, ks
);
357 ks
= sym_find(&k
->u
.s
, tag
, -1, sizeof(*ks
), &f
);
360 if (!stealp
) KEY_INCREF(kd
);
365 void key_structset(key_data
*k
, const char *tag
, key_data
*kd
)
366 { structset(k
, 0, tag
, kd
); }
367 void key_structsteal(key_data
*k
, const char *tag
, key_data
*kd
)
368 { structset(k
, 1, tag
, kd
); }
370 /*----- Miscellaneous operations ------------------------------------------*/
372 /* --- @key_do@ --- *
374 * Arguments: @key_data *k@ = pointer to key data block
375 * @const key_filter *kf@ = pointer to filter block
376 * @dstr *d@ = pointer to base string
377 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
378 * @void *p@ = argument to function
380 * Returns: Nonzero return code from function, or zero.
382 * Use: Runs a function over all the leaves of a key.
385 int key_do(key_data
*k
, const key_filter
*kf
, dstr
*d
,
386 int (*func
)(key_data */
*kd*/
, dstr */
*d*/
, void */
*p*/
),
389 if (!KEY_MATCH(k
, kf
))
391 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
392 return (func(k
, d
, p
));
401 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
404 dstr_putf(d
, ".%s", tag
);
406 if ((rc
= key_do(k
, kf
, d
, func
, p
)) != 0)
413 /* --- @key_copydata@ --- *
415 * Arguments: @key_data *k@ = key data to copy
416 * @const key_filter *kf@ = pointer to filter block
418 * Returns: Pointer to a copy of the data, or null if the root subkey
419 * didn't match the filter.
421 * Use: Copies a chunk of key data. Subkeys, whether they're
422 * structured or leaves, which don't match the filter aren't
423 * copied. The copy may or may not have structure in common
427 static int structmatchp(key_data
*k
, const key_filter
*kf
)
431 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
432 return (KEY_MATCH(k
, kf
));
434 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, 0, &k
); )
435 if (!structmatchp(k
, kf
)) return (0);
440 key_data
*key_copydata(key_data
*k
, const key_filter
*kf
)
446 /* --- Trivial cases --- */
448 if (!KEY_MATCH(k
, kf
))
450 else if (structmatchp(k
, kf
)) {
455 /* --- Copy a structured key recursively --- */
457 kkd
= key_newstruct();
458 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &kd
); ) {
459 if ((kd
= key_copydata(kd
, kf
)) != 0)
460 key_structsteal(kkd
, tag
, kd
);
468 /*----- That's all, folks -------------------------------------------------*/