3 * $Id: key-binary.c,v 1.3 2001/02/03 11:57:00 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.3 2001/02/03 11:57:00 mdw
34 * Track mLib change: symbols no longer need to include a terminating
37 * Revision 1.2 2000/06/17 11:25:20 mdw
38 * Use secure memory interface from MP library.
40 * Revision 1.1 2000/02/12 18:21:02 mdw
41 * Overhaul of key management (again).
45 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/bits.h>
51 #include <mLib/dstr.h>
59 /*----- Main code ---------------------------------------------------------*/
61 /* --- @key_decode@ --- *
63 * Arguments: @const void *p@ = pointer to buffer to read
64 * @size_t sz@ = size of the buffer
65 * @key_data *k@ = pointer to key data block to write to
67 * Returns: Zero if everything worked, nonzero otherwise.
69 * Use: Decodes a binary representation of a key.
72 int key_decode(const void *p
, size_t sz
, key_data
*k
)
78 /* --- Parse the header information --- *
80 * Make sure the size matches external reality. Security holes have been
81 * known to creep in without this sort of check. (No, this isn't an after-
91 /* --- Now decide what to do --- */
93 switch (e
& KF_ENCMASK
) {
95 /* --- Plain binary data --- */
99 k
->u
.k
.k
= sub_alloc(psz
);
100 memcpy(k
->u
.k
.k
, q
+ 4, psz
);
104 /* --- Multiprecision integer data --- */
107 k
->u
.m
= mp_loadb(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, q
+ 4, psz
);
110 /* --- Structured key data --- */
117 if ((k
->e
& ~KF_ENCMASK
) || (psz
& 3))
124 /* --- Read the tag string --- */
130 DPUTM(&d
, q
+ 1, sz
);
135 /* --- Read the encoding and size --- */
138 sz
= (LOAD16(q
+ 2) + 7) & ~3;
142 /* --- Create a table node and fill it in --- */
144 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
147 if (key_decode(q
, sz
, &ks
->k
)) {
148 sym_remove(&k
->u
.s
, ks
);
157 /* --- Tidy up after a failure --- */
165 /* --- Everything else --- */
171 /* --- OK, that was good --- */
176 /* --- @key_encode@ --- *
178 * Arguments: @key_data *k@ = pointer to key data block
179 * @dstr *d@ = pointer to destination string
180 * @const key_filter *kf@ = pointer to key selection block
182 * Returns: Nonzero if an item was actually written.
184 * Use: Encodes a key block as binary data.
187 int key_encode(key_data
*k
, dstr
*d
, const key_filter
*kf
)
190 if (!KEY_MATCH(k
, kf
))
192 switch (k
->e
& KF_ENCMASK
) {
197 DENSURE(d
, (k
->u
.k
.sz
+ 7) & ~3);
200 STORE16(p
+ 2, k
->u
.k
.sz
);
202 DPUTM(d
, k
->u
.k
.k
, k
->u
.k
.sz
);
208 size_t sz
= mp_octets(k
->u
.m
);
210 DENSURE(d
, (sz
+ 7) & ~3);
214 mp_storeb(k
->u
.m
, p
+ 4, sz
);
228 STORE16(p
, k
->e
& KF_ENCMASK
);
230 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
233 *(octet
*)(d
->buf
+ d
->len
++) = strlen(SYM_NAME(ks
));
234 DPUTS(d
, SYM_NAME(ks
));
237 if (key_encode(&ks
->k
, d
, kf
))
256 /*----- That's all, folks -------------------------------------------------*/