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);
158 /*----- Setting new values ------------------------------------------------*/
160 /* --- @key_newraw@ --- *
162 * Arguments: @unsigned e@ = encoding type to set
164 * Returns: New key block, not filled in.
167 key_data *key_newraw(unsigned e)
169 key_data *k = CREATE(key_data);
175 /* --- @key_newbinary@ --- *
177 * Arguments: @unsigned e@ = other encoding flags
178 * @const void *p@ = pointer to key data
179 * @size_t sz@ = size of the key data
181 * Returns: New key data object.
184 key_data *key_newbinary(unsigned e, const void *p, size_t sz)
186 key_data *k = key_newraw(KENC_BINARY | e);
187 k->u.k.k = sub_alloc(sz);
188 memcpy(k->u.k.k, p, sz);
193 /* --- @key_newencrypted@ --- *
195 * Arguments: @unsigned e@ = other encoding flags
196 * @const void *p@ = pointer to key data
197 * @size_t sz@ = size of the key data
199 * Returns: New key data object.
202 key_data *key_newencrypted(unsigned e, const void *p, size_t sz)
204 key_data *k = key_newraw(KENC_ENCRYPT | e);
205 k->u.k.k = sub_alloc(sz);
206 memcpy(k->u.k.k, p, sz);
211 /* --- @key_newmp@ --- *
213 * Arguments: @unsigned e@ = other encoding flags
214 * @mp *m@ = pointer to the value to set
216 * Returns: New key data object.
219 key_data *key_newmp(unsigned e, mp *m)
221 key_data *k = key_newraw(KENC_MP | e);
226 /* --- @key_newstring@ --- *
228 * Arguments: @unsigned e@ = other encoding flags
229 * @const char *p@ = pointer to the value to set
231 * Returns: New key data object.
234 key_data *key_newstring(unsigned e, const char *p)
236 key_data *k = key_newraw(KENC_STRING | e);
241 /* --- @key_newec@ --- *
243 * Arguments: @unsigned e@ = other encoding flags
244 * @const ec *pt@ = pointer to the value to set
246 * Returns: New key data object.
249 key_data *key_newec(unsigned e, const ec *pt)
251 key_data *k = key_newraw(KENC_EC | e);
253 EC_COPY(&k->u.e, pt);
257 /* --- @key_newstruct@ --- *
261 * Returns: New key data object.
264 key_data *key_newstruct(void)
266 key_data *k = key_newraw(KENC_STRUCT);
271 /* --- @key_structfind@ --- *
273 * Arguments: @key_data *k@ = pointer to key data block
274 * @const char *tag@ = pointer to tag string
276 * Returns: Pointer to key data block, or null.
278 * Use: Looks up the tag in a structured key.
281 key_data *key_structfind(key_data *k, const char *tag)
284 assert(((void)"Key is not structured",
285 (k->e & KF_ENCMASK) == KENC_STRUCT));
286 ks = sym_find(&k->u.s, tag, -1, 0, 0);
292 /* --- @key_mksubkeyiter@ --- *
294 * Arguments: @key_subkeyiter *i@ = pointer to iterator block
295 * @key_data *k@ = pointer to key data block
299 * Use: Initializes a subkey iterator.
302 void key_mksubkeyiter(key_subkeyiter *i, key_data *k)
304 assert(((void)"Key is not structured",
305 (k->e & KF_ENCMASK) == KENC_STRUCT));
306 sym_mkiter(&i->i, &k->u.s);
309 /* --- @key_nextsubkey@ --- *
311 * Arguments: @key_structiter *i@ = pointer to iterator block
312 * @const char **tag@ = where to put the tag pointer, or null
313 * @key_data **kd@ = where to put the key data pointer, or null
315 * Returns: Nonzero if there was another item, zero if we hit the
318 * Use: Collects the next subkey of a structured key.
321 int key_nextsubkey(key_subkeyiter *i, const char **tag, key_data **kd)
325 if ((ks = sym_next(&i->i)) == 0)
327 if (tag) *tag = SYM_NAME(ks);
332 /* --- @key_structset@, @key_structsteal@ --- *
334 * Arguments: @key_data *k@ = pointer to key data block
335 * @const char *tag@ = pointer to tag string
336 * @key_data *kd@ = new key data to store
340 * Use: Creates a new subkey. Stealing doesn't affect @kd@'s
341 * refcount. If @kd@ is null, the subkey is deleted.
344 static void structset(key_data *k, int stealp,
345 const char *tag, key_data *kd)
350 assert(((void)"Key is not structured", k->e == KENC_STRUCT));
351 assert(((void)"Key has multiple references", k->ref == 1));
353 ks = sym_find(&k->u.s, tag, -1, 0, 0);
354 if (ks) sym_remove(&k->u.s, ks);
356 ks = sym_find(&k->u.s, tag, -1, sizeof(*ks), &f);
359 if (!stealp) KEY_INCREF(kd);
364 void key_structset(key_data *k, const char *tag, key_data *kd)
365 { structset(k, 0, tag, kd); }
366 void key_structsteal(key_data *k, const char *tag, key_data *kd)
367 { structset(k, 1, tag, kd); }
369 /*----- Miscellaneous operations ------------------------------------------*/
371 /* --- @key_do@ --- *
373 * Arguments: @key_data *k@ = pointer to key data block
374 * @const key_filter *kf@ = pointer to filter block
375 * @dstr *d@ = pointer to base string
376 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
377 * @void *p@ = argument to function
379 * Returns: Nonzero return code from function, or zero.
381 * Use: Runs a function over all the leaves of a key.
384 int key_do(key_data *k, const key_filter *kf, dstr *d,
385 int (*func)(key_data */*kd*/, dstr */*d*/, void */*p*/),
388 if (!KEY_MATCH(k, kf))
390 if ((k->e & KF_ENCMASK) != KENC_STRUCT)
391 return (func(k, d, p));
400 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) {
403 dstr_putf(d, ".%s", tag);
405 if ((rc = key_do(k, kf, d, func, p)) != 0)
412 /* --- @key_copydata@ --- *
414 * Arguments: @key_data *k@ = key data to copy
415 * @const key_filter *kf@ = pointer to filter block
417 * Returns: Pointer to a copy of the data, or null if the root subkey
418 * didn't match the filter.
420 * Use: Copies a chunk of key data. Subkeys, whether they're
421 * structured or leaves, which don't match the filter aren't
422 * copied. The copy may or may not have structure in common
426 static int structmatchp(key_data *k, const key_filter *kf)
430 if (!KEY_MATCH(k, kf)) return (0);
431 else if ((k->e & KF_ENCMASK) == KENC_STRUCT) return (1);
433 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, 0, &k); )
434 if (!structmatchp(k, kf)) return (0);
439 key_data *key_copydata(key_data *k, const key_filter *kf)
445 /* --- Trivial cases --- */
447 if (!KEY_MATCH(k, kf))
449 else if (structmatchp(k, kf)) {
454 /* --- Copy a structured key recursively --- */
456 kkd = key_newstruct();
457 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &kd); ) {
458 if ((kd = key_copydata(kd, kf)) != 0)
459 key_structsteal(kkd, tag, kd);
467 /*----- That's all, folks -------------------------------------------------*/