5 * Encoding and decoding of key data
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
36 #include <mLib/base64.h>
37 #include <mLib/bits.h>
38 #include <mLib/dstr.h>
46 /*----- Reference counting stuff ------------------------------------------*/
48 /* --- @key_incref@ --- *
50 * Arguments: @key_data *k@ = pointer to key data
54 * Use: Increments the refcount on a key data block.
57 void key_incref(key_data
*k
) { KEY_INCREF(k
); }
59 /* --- @key_destroy@ --- *
61 * Arguments: @key_data *k@ = pointer to key data to destroy
65 * Use: Destroys a block of key data, regardless of reference count.
66 * Don't use this unless you know what you're doing.
69 void key_destroy(key_data
*k
)
71 switch (k
->e
& KF_ENCMASK
) {
76 memset(k
->u
.k
.k
, 0, k
->u
.k
.sz
);
77 sub_free(k
->u
.k
.k
, k
->u
.k
.sz
);
93 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, 0, &kd
); )
103 /* --- @key_drop@ --- *
105 * Arguments: @key_data *k@ = pointer to key data to destroy
109 * Use: Drops a reference to key data, destroying it if necessary.
112 void key_drop(key_data
*k
) { KEY_DROP(k
); }
114 /* --- @key_split@ --- *
116 * Arguments: @key_data **kk@ = address of pointer to key data block
120 * Use: Replaces @*kk@ with a pointer to the same key data, but with
121 * just one reference.
124 void key_split(key_data
**kk
)
130 switch (k
->e
& KF_ENCMASK
) {
132 *kk
= key_newbinary(k
->e
, k
->u
.k
.k
, k
->u
.k
.sz
);
135 *kk
= key_newencrypted(k
->e
, k
->u
.k
.k
, k
->u
.k
.sz
);
138 *kk
= key_newmp(k
->e
, k
->u
.m
);
141 *kk
= key_newstring(k
->e
, k
->u
.p
);
144 *kk
= key_newec(k
->e
, &k
->u
.e
);
151 *kk
= key_newstruct();
152 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &kd
); )
153 key_structset(*kk
, tag
, kd
);
160 /*----- Setting new values ------------------------------------------------*/
162 /* --- @key_newraw@ --- *
164 * Arguments: @unsigned e@ = encoding type to set
166 * Returns: New key block, not filled in.
169 key_data
*key_newraw(unsigned e
)
171 key_data
*k
= CREATE(key_data
);
177 /* --- @key_newbinary@ --- *
179 * Arguments: @unsigned e@ = other encoding flags
180 * @const void *p@ = pointer to key data
181 * @size_t sz@ = size of the key data
183 * Returns: New key data object.
186 key_data
*key_newbinary(unsigned e
, const void *p
, size_t sz
)
188 key_data
*k
= key_newraw(KENC_BINARY
| e
);
189 k
->u
.k
.k
= sub_alloc(sz
);
190 memcpy(k
->u
.k
.k
, p
, sz
);
195 /* --- @key_newencrypted@ --- *
197 * Arguments: @unsigned e@ = other encoding flags
198 * @const void *p@ = pointer to key data
199 * @size_t sz@ = size of the key data
201 * Returns: New key data object.
204 key_data
*key_newencrypted(unsigned e
, const void *p
, size_t sz
)
206 key_data
*k
= key_newraw(KENC_ENCRYPT
| e
);
207 k
->u
.k
.k
= sub_alloc(sz
);
208 memcpy(k
->u
.k
.k
, p
, sz
);
213 /* --- @key_mewmp@ --- *
215 * Arguments: @unsigned e@ = other encoding flags
216 * @mp *m@ = pointer to the value to set
218 * Returns: New key data object.
221 key_data
*key_newmp(unsigned e
, mp
*m
)
223 key_data
*k
= key_newraw(KENC_MP
| e
);
228 /* --- @key_newstring@ --- *
230 * Arguments: @unsigned e@ = other encoding flags
231 * @const char *p@ = pointer to the value to set
233 * Returns: New key data object.
236 key_data
*key_newstring(unsigned e
, const char *p
)
238 key_data
*k
= key_newraw(KENC_STRING
| e
);
243 /* --- @key_newec@ --- *
245 * Arguments: @unsigned e@ = other encoding flags
246 * @const ec *pt@ = pointer to the value to set
248 * Returns: New key data object.
251 key_data
*key_newec(unsigned e
, const ec
*pt
)
253 key_data
*k
= key_newraw(KENC_EC
| e
);
255 EC_COPY(&k
->u
.e
, pt
);
259 /* --- @key_newstruct@ --- *
263 * Returns: New key data object.
266 key_data
*key_newstruct(void)
268 key_data
*k
= key_newraw(KENC_STRUCT
);
273 /* --- @key_structfind@ --- *
275 * Arguments: @key_data *k@ = pointer to key data block
276 * @const char *tag@ = pointer to tag string
278 * Returns: Pointer to key data block, or null.
280 * Use: Looks up the tag in a structured key.
283 key_data
*key_structfind(key_data
*k
, const char *tag
)
286 assert(((void)"Key is not structured",
287 (k
->e
& KF_ENCMASK
) == KENC_STRUCT
));
288 ks
= sym_find(&k
->u
.s
, tag
, -1, 0, 0);
294 /* --- @key_mksubkeyiter@ --- *
296 * Arguments: @key_subkeyiter *i@ = pointer to iterator block
297 * @key_data *k@ = pointer to key data block
301 * Use: Initializes a subkey iterator.
304 void key_mksubkeyiter(key_subkeyiter
*i
, key_data
*k
)
306 assert(((void)"Key is not structured",
307 (k
->e
& KF_ENCMASK
) == KENC_STRUCT
));
308 sym_mkiter(&i
->i
, &k
->u
.s
);
311 /* --- @key_nextsubkey@ --- *
313 * Arguments: @key_structiter *i@ = pointer to iterator block
314 * @const char **tag@ = where to put the tag pointer, or null
315 * @key_data **kd@ = where to put the key data pointer, or null
317 * Returns: Nonzero if there was another item, zero if we hit the
320 * Use: Collects the next subkey of a structured key.
323 int key_nextsubkey(key_subkeyiter
*i
, const char **tag
, key_data
**kd
)
327 if ((ks
= sym_next(&i
->i
)) == 0)
329 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
));
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 /*----- That's all, folks -------------------------------------------------*/