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 ------------------------------------------------------*/
35 #include <mLib/bits.h>
36 #include <mLib/dstr.h>
44 /*----- Main code ---------------------------------------------------------*/
46 /* --- @key_decode@ --- *
48 * Arguments: @const void *p@ = pointer to buffer to read
49 * @size_t sz@ = size of the buffer
51 * Returns: The newly-read key data, or null if it failed.
53 * Use: Decodes a binary representation of a key.
56 key_data *key_decode(const void *p, size_t sz)
63 /* --- Parse the header information --- *
65 * Make sure the size matches external reality. Security holes have been
66 * known to creep in without this sort of check. (No, this isn't an after-
75 /* --- Now decide what to do --- */
77 switch (e & KF_ENCMASK) {
79 /* --- Plain binary data --- */
83 kd = key_newbinary(e, q + 4, psz);
86 /* --- Multiprecision integer data --- */
89 kd = key_newmp(e, mp_loadb(e & KF_BURN ? MP_NEWSEC : MP_NEW,
93 /* --- String data --- */
97 kd->u.p = xmalloc(sz + 1);
98 memcpy(kd->u.p, q + 4, sz);
102 /* --- Elliptic curve point data --- */
109 if (sz < 2) return (0);
111 if (sz < xsz + 4) return (0);
112 ysz = LOAD16(q + 6 + xsz);
113 if (sz < xsz + ysz + 4) return (0);
114 kd->u.e.x = mp_loadb(MP_NEW, q + 6, xsz);
115 kd->u.e.y = mp_loadb(MP_NEW, q + 8 + xsz, ysz);
118 /* --- Structured key data --- */
124 if ((e & ~KF_ENCMASK) || (psz & 3))
127 kd = key_newstruct();
131 /* --- Read the tag string --- */
137 DPUTM(&d, q + 1, sz);
142 /* --- Read the encoding and size --- */
144 sz = (LOAD16(q + 2) + 7) & ~3;
148 /* --- Create a table node and fill it in --- */
150 if ((nkd = key_decode(q, sz)) == 0)
152 key_structsteal(kd, d.buf, nkd);
159 /* --- Tidy up after a failure --- */
167 /* --- Everything else --- */
173 /* --- OK, that was good --- */
179 /* --- @key_encode@ --- *
181 * Arguments: @key_data *k@ = pointer to key data block
182 * @dstr *d@ = pointer to destination string
183 * @const key_filter *kf@ = pointer to key selection block
185 * Returns: Nonzero if an item was actually written.
187 * Use: Encodes a key block as binary data.
190 static int ksbyname(const void *a, const void *b) {
191 key_struct *const *x = a, *const *y = b;
192 return (strcmp(SYM_NAME(*x), SYM_NAME(*y)));
195 int key_encode(key_data *k, dstr *d, const key_filter *kf)
198 if (!KEY_MATCH(k, kf))
200 switch (k->e & KF_ENCMASK) {
205 DENSURE(d, (k->u.k.sz + 7) & ~3);
208 STORE16(p + 2, k->u.k.sz);
210 DPUTM(d, k->u.k.k, k->u.k.sz);
216 size_t sz = mp_octets(k->u.m);
218 DENSURE(d, (sz + 7) & ~3);
222 mp_storeb(k->u.m, p + 4, sz);
229 size_t sz = strlen(k->u.p);
231 DENSURE(d, (sz + 7) & ~3);
235 memcpy(p + 4, k->u.p, sz);
242 size_t xsz = 0, ysz = 0;
245 if (EC_ATINF(&k->u.e))
248 xsz = mp_octets(k->u.e.x);
249 ysz = mp_octets(k->u.e.y);
252 DENSURE(d, (sz + 7) & ~3);
256 if (!EC_ATINF(&k->u.e)) {
258 mp_storeb(k->u.e.x, p + 6, xsz);
259 STORE16(p + 6 + xsz, ysz);
260 mp_storeb(k->u.e.y, p + 8 + xsz, ysz);
269 key_struct *ks, **ksv;
276 STORE16(p, k->e & KF_ENCMASK);
279 for (nks = 0, sym_mkiter(&i, &k->u.s);
280 (ks = sym_next(&i)) != 0;
283 ksv = xmalloc(nks * sizeof(*ksv));
284 for (j = 0, sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; j++)
286 qsort(ksv, nks, sizeof(*ksv), ksbyname);
287 for (j = 0; j < nks; j++) {
291 *(octet *)(d->buf + d->len++) = strlen(SYM_NAME(ks));
292 DPUTS(d, SYM_NAME(ks));
295 if (key_encode(ks->k, d, kf))
316 /*----- That's all, folks -------------------------------------------------*/