3 * $Id: key-binary.c,v 1.1 2000/02/12 18:21:02 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.1 2000/02/12 18:21:02 mdw
34 * Overhaul of key management (again).
38 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/bits.h>
44 #include <mLib/dstr.h>
52 /*----- Main code ---------------------------------------------------------*/
54 /* --- @key_decode@ --- *
56 * Arguments: @const void *p@ = pointer to buffer to read
57 * @size_t sz@ = size of the buffer
58 * @key_data *k@ = pointer to key data block to write to
60 * Returns: Zero if everything worked, nonzero otherwise.
62 * Use: Decodes a binary representation of a key.
65 int key_decode(const void *p
, size_t sz
, key_data
*k
)
71 /* --- Parse the header information --- *
73 * Make sure the size matches external reality. Security holes have been
74 * known to creep in without this sort of check. (No, this isn't an after-
84 /* --- Now decide what to do --- */
86 switch (e
& KF_ENCMASK
) {
88 /* --- Plain binary data --- */
92 k
->u
.k
.k
= sub_alloc(psz
);
93 memcpy(k
->u
.k
.k
, q
+ 4, psz
);
97 /* --- Multiprecision integer data --- */
100 k
->u
.m
= mp_loadb(MP_NEW
, q
+ 4, psz
);
105 /* --- Structured key data --- */
112 if ((k
->e
& ~KF_ENCMASK
) || (psz
& 3))
119 /* --- Read the tag string --- */
125 DPUTM(&d
, q
+ 1, sz
);
130 /* --- Read the encoding and size --- */
133 sz
= (LOAD16(q
+ 2) + 7) & ~3;
137 /* --- Create a table node and fill it in --- */
139 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
+ 1, sizeof(*ks
), &f
);
142 if (key_decode(q
, sz
, &ks
->k
)) {
143 sym_remove(&k
->u
.s
, ks
);
152 /* --- Tidy up after a failure --- */
160 /* --- Everything else --- */
166 /* --- OK, that was good --- */
171 /* --- @key_encode@ --- *
173 * Arguments: @key_data *k@ = pointer to key data block
174 * @dstr *d@ = pointer to destination string
175 * @const key_filter *kf@ = pointer to key selection block
177 * Returns: Nonzero if an item was actually written.
179 * Use: Encodes a key block as binary data.
182 int key_encode(key_data
*k
, dstr
*d
, const key_filter
*kf
)
185 if (!KEY_MATCH(k
, kf
))
187 switch (k
->e
& KF_ENCMASK
) {
192 DENSURE(d
, (k
->u
.k
.sz
+ 7) & ~3);
195 STORE16(p
+ 2, k
->u
.k
.sz
);
197 DPUTM(d
, k
->u
.k
.k
, k
->u
.k
.sz
);
203 size_t sz
= mp_octets(k
->u
.m
);
205 DENSURE(d
, (sz
+ 7) & ~3);
209 mp_storeb(k
->u
.m
, p
+ 4, sz
);
223 STORE16(p
, k
->e
& KF_ENCMASK
);
225 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
228 *(octet
*)(d
->buf
+ d
->len
++) = strlen(SYM_NAME(ks
));
229 DPUTS(d
, SYM_NAME(ks
));
232 if (key_encode(&ks
->k
, d
, kf
))
251 /*----- That's all, folks -------------------------------------------------*/