3 * $Id: key-binary.c,v 1.2 2000/06/17 11:25:20 mdw Exp $
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 /*----- Revision history --------------------------------------------------*
32 * $Log: key-binary.c,v $
33 * Revision 1.2 2000/06/17 11:25:20 mdw
34 * Use secure memory interface from MP library.
36 * Revision 1.1 2000/02/12 18:21:02 mdw
37 * Overhaul of key management (again).
41 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/bits.h>
47 #include <mLib/dstr.h>
55 /*----- Main code ---------------------------------------------------------*/
57 /* --- @key_decode@ --- *
59 * Arguments: @const void *p@ = pointer to buffer to read
60 * @size_t sz@ = size of the buffer
61 * @key_data *k@ = pointer to key data block to write to
63 * Returns: Zero if everything worked, nonzero otherwise.
65 * Use: Decodes a binary representation of a key.
68 int key_decode(const void *p, size_t sz, key_data *k)
74 /* --- Parse the header information --- *
76 * Make sure the size matches external reality. Security holes have been
77 * known to creep in without this sort of check. (No, this isn't an after-
87 /* --- Now decide what to do --- */
89 switch (e & KF_ENCMASK) {
91 /* --- Plain binary data --- */
95 k->u.k.k = sub_alloc(psz);
96 memcpy(k->u.k.k, q + 4, psz);
100 /* --- Multiprecision integer data --- */
103 k->u.m = mp_loadb(k->e & KF_BURN ? MP_NEWSEC : MP_NEW, q + 4, psz);
106 /* --- Structured key data --- */
113 if ((k->e & ~KF_ENCMASK) || (psz & 3))
120 /* --- Read the tag string --- */
126 DPUTM(&d, q + 1, sz);
131 /* --- Read the encoding and size --- */
134 sz = (LOAD16(q + 2) + 7) & ~3;
138 /* --- Create a table node and fill it in --- */
140 ks = sym_find(&k->u.s, d.buf, d.len + 1, sizeof(*ks), &f);
143 if (key_decode(q, sz, &ks->k)) {
144 sym_remove(&k->u.s, ks);
153 /* --- Tidy up after a failure --- */
161 /* --- Everything else --- */
167 /* --- OK, that was good --- */
172 /* --- @key_encode@ --- *
174 * Arguments: @key_data *k@ = pointer to key data block
175 * @dstr *d@ = pointer to destination string
176 * @const key_filter *kf@ = pointer to key selection block
178 * Returns: Nonzero if an item was actually written.
180 * Use: Encodes a key block as binary data.
183 int key_encode(key_data *k, dstr *d, const key_filter *kf)
186 if (!KEY_MATCH(k, kf))
188 switch (k->e & KF_ENCMASK) {
193 DENSURE(d, (k->u.k.sz + 7) & ~3);
196 STORE16(p + 2, k->u.k.sz);
198 DPUTM(d, k->u.k.k, k->u.k.sz);
204 size_t sz = mp_octets(k->u.m);
206 DENSURE(d, (sz + 7) & ~3);
210 mp_storeb(k->u.m, p + 4, sz);
224 STORE16(p, k->e & KF_ENCMASK);
226 for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) {
229 *(octet *)(d->buf + d->len++) = strlen(SYM_NAME(ks));
230 DPUTS(d, SYM_NAME(ks));
233 if (key_encode(&ks->k, d, kf))
252 /*----- That's all, folks -------------------------------------------------*/