chiark / gitweb /
rand/rand-x86ish.S: Hoist argument register allocation outside.
[catacomb] / key / key-data.c
1 /* -*-c-*-
2  *
3  * Encoding and decoding of key data
4  *
5  * (c) 1999 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <mLib/base64.h>
35 #include <mLib/bits.h>
36 #include <mLib/dstr.h>
37 #include <mLib/sub.h>
38 #include <mLib/sym.h>
39
40 #include "key-data.h"
41 #include "mp.h"
42 #include "mptext.h"
43
44 /*----- Reference counting stuff ------------------------------------------*/
45
46 /* --- @key_incref@ --- *
47  *
48  * Arguments:   @key_data *k@ = pointer to key data
49  *
50  * Returns:     ---
51  *
52  * Use:         Increments the refcount on a key data block.
53  */
54
55 void key_incref(key_data *k) { KEY_INCREF(k); }
56
57 /* --- @key_destroy@ --- *
58  *
59  * Arguments:   @key_data *k@ = pointer to key data to destroy
60  *
61  * Returns:     ---
62  *
63  * Use:         Destroys a block of key data, regardless of reference count.
64  *              Don't use this unless you know what you're doing.
65  */
66
67 void key_destroy(key_data *k)
68 {
69   switch (k->e & KF_ENCMASK) {
70     case KENC_BINARY:
71     case KENC_ENCRYPT:
72       if (k->u.k.k) {
73         if (k->e & KF_BURN)
74           memset(k->u.k.k, 0, k->u.k.sz);
75         sub_free(k->u.k.k, k->u.k.sz);
76       }
77       break;
78     case KENC_MP:
79       mp_drop(k->u.m);
80       break;
81     case KENC_STRING:
82       xfree(k->u.p);
83       break;
84     case KENC_EC:
85       EC_DESTROY(&k->u.e);
86       break;
87     case KENC_STRUCT: {
88       key_data *kd;
89       key_subkeyiter i;
90
91       for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, 0, &kd); )
92         KEY_DROP(kd);
93       sym_destroy(&k->u.s);
94     } break;
95     default:
96       abort();
97   }
98   DESTROY(k);
99 }
100
101 /* --- @key_drop@ --- *
102  *
103  * Arguments:   @key_data *k@ = pointer to key data to destroy
104  *
105  * Returns:     ---
106  *
107  * Use:         Drops a reference to key data, destroying it if necessary.
108  */
109
110 void key_drop(key_data *k) { KEY_DROP(k); }
111
112 /* --- @key_split@ --- *
113  *
114  * Arguments:   @key_data **kk@ = address of pointer to key data block
115  *
116  * Returns:     ---
117  *
118  * Use:         Replaces @*kk@ with a pointer to the same key data, but with
119  *              just one reference.
120  */
121
122 void key_split(key_data **kk)
123 {
124   key_data *k = *kk;
125
126   if (k->ref == 1)
127     return;
128   switch (k->e & KF_ENCMASK) {
129     case KENC_BINARY:
130       *kk = key_newbinary(k->e, k->u.k.k, k->u.k.sz);
131       break;
132     case KENC_ENCRYPT:
133       *kk = key_newencrypted(k->e, k->u.k.k, k->u.k.sz);
134       break;
135     case KENC_MP:
136       *kk = key_newmp(k->e, k->u.m);
137       break;
138     case KENC_STRING:
139       *kk = key_newstring(k->e, k->u.p);
140       break;
141     case KENC_EC:
142       *kk = key_newec(k->e, &k->u.e);
143       break;
144     case KENC_STRUCT: {
145       key_subkeyiter i;
146       const char *tag;
147       key_data *kd;
148
149       *kk = key_newstruct();
150       for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &kd); )
151         key_structset(*kk, tag, kd);
152     } break;
153     default:
154       abort();
155   }
156   key_drop(k);
157 }
158
159 /*----- Setting new values ------------------------------------------------*/
160
161 /* --- @key_newraw@ --- *
162  *
163  * Arguments:   @unsigned e@ = encoding type to set
164  *
165  * Returns:     New key block, not filled in.
166  */
167
168 key_data *key_newraw(unsigned e)
169 {
170   key_data *k = CREATE(key_data);
171   k->e = e;
172   k->ref = 1;
173   return (k);
174 }
175
176 /* --- @key_newbinary@ --- *
177  *
178  * Arguments:   @unsigned e@ = other encoding flags
179  *              @const void *p@ = pointer to key data
180  *              @size_t sz@ = size of the key data
181  *
182  * Returns:     New key data object.
183  */
184
185 key_data *key_newbinary(unsigned e, const void *p, size_t sz)
186 {
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);
190   k->u.k.sz = sz;
191   return (k);
192 }
193
194 /* --- @key_newencrypted@ --- *
195  *
196  * Arguments:   @unsigned e@ = other encoding flags
197  *              @const void *p@ = pointer to key data
198  *              @size_t sz@ = size of the key data
199  *
200  * Returns:     New key data object.
201  */
202
203 key_data *key_newencrypted(unsigned e, const void *p, size_t sz)
204 {
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);
208   k->u.k.sz = sz;
209   return (k);
210 }
211
212 /* --- @key_newmp@ --- *
213  *
214  * Arguments:   @unsigned e@ = other encoding flags
215  *              @mp *m@ = pointer to the value to set
216  *
217  * Returns:     New key data object.
218  */
219
220 key_data *key_newmp(unsigned e, mp *m)
221 {
222   key_data *k = key_newraw(KENC_MP | e);
223   k->u.m = MP_COPY(m);
224   return (k);
225 }
226
227 /* --- @key_newstring@ --- *
228  *
229  * Arguments:   @unsigned e@ = other encoding flags
230  *              @const char *p@ = pointer to the value to set
231  *
232  * Returns:     New key data object.
233  */
234
235 key_data *key_newstring(unsigned e, const char *p)
236 {
237   key_data *k = key_newraw(KENC_STRING | e);
238   k->u.p = xstrdup(p);
239   return (k);
240 }
241
242 /* --- @key_newec@ --- *
243  *
244  * Arguments:   @unsigned e@ = other encoding flags
245  *              @const ec *pt@ = pointer to the value to set
246  *
247  * Returns:     New key data object.
248  */
249
250 key_data *key_newec(unsigned e, const ec *pt)
251 {
252   key_data *k = key_newraw(KENC_EC | e);
253   EC_CREATE(&k->u.e);
254   EC_COPY(&k->u.e, pt);
255   return (k);
256 }
257
258 /* --- @key_newstruct@ --- *
259  *
260  * Arguments:   ---
261  *
262  * Returns:     New key data object.
263  */
264
265 key_data *key_newstruct(void)
266 {
267   key_data *k = key_newraw(KENC_STRUCT);
268   sym_create(&k->u.s);
269   return (k);
270 }
271
272 /* --- @key_structfind@ --- *
273  *
274  * Arguments:   @key_data *k@ = pointer to key data block
275  *              @const char *tag@ = pointer to tag string
276  *
277  * Returns:     Pointer to key data block, or null.
278  *
279  * Use:         Looks up the tag in a structured key.
280  */
281
282 key_data *key_structfind(key_data *k, const char *tag)
283 {
284   key_struct *ks;
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);
288   if (!ks)
289     return (0);
290   return (ks->k);
291 }
292
293 /* --- @key_mksubkeyiter@ --- *
294  *
295  * Arguments:   @key_subkeyiter *i@ = pointer to iterator block
296  *              @key_data *k@ = pointer to key data block
297  *
298  * Returns:     ---
299  *
300  * Use:         Initializes a subkey iterator.
301  */
302
303 void key_mksubkeyiter(key_subkeyiter *i, key_data *k)
304 {
305   assert(((void)"Key is not structured",
306           (k->e & KF_ENCMASK) == KENC_STRUCT));
307   sym_mkiter(&i->i, &k->u.s);
308 }
309
310 /* --- @key_nextsubkey@ --- *
311  *
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
315  *
316  * Returns:     Nonzero if there was another item, zero if we hit the
317  *              end-stop.
318  *
319  * Use:         Collects the next subkey of a structured key.
320  */
321
322 int key_nextsubkey(key_subkeyiter *i, const char **tag, key_data **kd)
323 {
324   key_struct *ks;
325
326   if ((ks = sym_next(&i->i)) == 0)
327     return (0);
328   if (tag) *tag = SYM_NAME(ks);
329   if (kd) *kd = ks->k;
330   return (1);
331 }
332
333 /* --- @key_structset@, @key_structsteal@ --- *
334  *
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
338  *
339  * Returns:     ---
340  *
341  * Use:         Creates a new subkey.  Stealing doesn't affect @kd@'s
342  *              refcount.  If @kd@ is null, the subkey is deleted.
343  */
344
345 static void structset(key_data *k, int stealp,
346                       const char *tag, key_data *kd)
347 {
348   key_struct *ks;
349   unsigned f;
350
351   assert(((void)"Key is not structured", k->e == KENC_STRUCT));
352   assert(((void)"Key has multiple references", k->ref == 1));
353   if (!kd) {
354     ks = sym_find(&k->u.s, tag, -1, 0, 0);
355     if (ks) sym_remove(&k->u.s, ks);
356   } else {
357     ks = sym_find(&k->u.s, tag, -1, sizeof(*ks), &f);
358     if (f)
359       key_drop(ks->k);
360     if (!stealp) KEY_INCREF(kd);
361     ks->k = kd;
362   }
363 }
364
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); }
369
370 /*----- Miscellaneous operations ------------------------------------------*/
371
372 /* --- @key_do@ --- *
373  *
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
379  *
380  * Returns:     Nonzero return code from function, or zero.
381  *
382  * Use:         Runs a function over all the leaves of a key.
383  */
384
385 int key_do(key_data *k, const key_filter *kf, dstr *d,
386            int (*func)(key_data */*kd*/, dstr */*d*/, void */*p*/),
387            void *p)
388 {
389   if (!KEY_MATCH(k, kf))
390     return (0);
391   if ((k->e & KF_ENCMASK) != KENC_STRUCT)
392     return (func(k, d, p));
393   else {
394     key_subkeyiter i;
395     const char *tag;
396     size_t n = 0;
397     int rc;
398
399     if (d)
400       n = d->len;
401     for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) {
402       if (d) {
403         d->len = n;
404         dstr_putf(d, ".%s", tag);
405       }
406       if ((rc = key_do(k, kf, d, func, p)) != 0)
407         return (rc);
408     }
409     return (0);
410   }
411 }
412
413 /* --- @key_copydata@ --- *
414  *
415  * Arguments:   @key_data *k@ = key data to copy
416  *              @const key_filter *kf@ = pointer to filter block
417  *
418  * Returns:     Pointer to a copy of the data, or null if the root subkey
419  *              didn't match the filter.
420  *
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
424  *              with the original.
425  */
426
427 static int structmatchp(key_data *k, const key_filter *kf)
428 {
429   key_subkeyiter i;
430
431   if ((k->e & KF_ENCMASK) != KENC_STRUCT)
432     return (KEY_MATCH(k, kf));
433   else {
434     for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, 0, &k); )
435       if (!structmatchp(k, kf)) return (0);
436     return (1);
437   }
438 }
439
440 key_data *key_copydata(key_data *k, const key_filter *kf)
441 {
442   key_subkeyiter i;
443   const char *tag;
444   key_data *kd, *kkd;
445
446   /* --- Trivial cases --- */
447
448   if (!KEY_MATCH(k, kf))
449     return (0);
450   else if (structmatchp(k, kf)) {
451     key_incref(k);
452     return (k);
453   }
454
455   /* --- Copy a structured key recursively --- */
456
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);
461   }
462
463   /* --- Done --- */
464
465   return (kkd);
466 }
467
468 /*----- That's all, folks -------------------------------------------------*/