5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
33 #include <mLib/bits.h>
34 #include <mLib/dstr.h>
35 #include <mLib/macros.h>
43 /*----- Main code ---------------------------------------------------------*/
45 /* --- @key_decode@ --- *
47 * Arguments: @const void *p@ = pointer to buffer to read
48 * @size_t sz@ = size of the buffer
50 * Returns: The newly-read key data, or null if it failed.
52 * Use: Decodes a binary representation of a key.
55 key_data
*key_decode(const void *p
, size_t sz
)
62 /* --- Parse the header information --- *
64 * Make sure the size matches external reality. Security holes have been
65 * known to creep in without this sort of check. (No, this isn't an after-
74 /* --- Now decide what to do --- */
76 switch (e
& KF_ENCMASK
) {
78 /* --- Plain binary data --- */
82 kd
= key_newbinary(e
, q
+ 4, psz
);
85 /* --- Multiprecision integer data --- */
88 kd
= key_newmp(e
, mp_loadb(e
& KF_BURN ? MP_NEWSEC
: MP_NEW
,
92 /* --- String data --- */
96 kd
->u
.p
= xmalloc(sz
+ 1);
97 memcpy(kd
->u
.p
, q
+ 4, sz
);
101 /* --- Elliptic curve point data --- */
108 if (sz
< 2) return (0);
110 if (sz
< xsz
+ 4) return (0);
111 ysz
= LOAD16(q
+ 6 + xsz
);
112 if (sz
< xsz
+ ysz
+ 4) return (0);
113 kd
->u
.e
.x
= mp_loadb(MP_NEW
, q
+ 6, xsz
);
114 kd
->u
.e
.y
= mp_loadb(MP_NEW
, q
+ 8 + xsz
, ysz
);
117 /* --- Structured key data --- */
123 if ((e
& ~KF_ENCMASK
) || (psz
& 3))
126 kd
= key_newstruct();
130 /* --- Read the tag string --- */
136 DPUTM(&d
, q
+ 1, sz
);
141 /* --- Read the encoding and size --- */
143 sz
= (LOAD16(q
+ 2) + 7) & ~3;
147 /* --- Create a table node and fill it in --- */
149 if ((nkd
= key_decode(q
, sz
)) == 0)
151 key_structsteal(kd
, d
.buf
, nkd
);
158 /* --- Tidy up after a failure --- */
166 /* --- Everything else --- */
172 /* --- OK, that was good --- */
178 /* --- @key_encode@ --- *
180 * Arguments: @key_data *k@ = pointer to key data block
181 * @dstr *d@ = pointer to destination string
182 * @const key_filter *kf@ = pointer to key selection block
184 * Returns: Nonzero if an item was actually written.
186 * Use: Encodes a key block as binary data.
189 static int ksbyname(const void *a
, const void *b
) {
190 key_struct
*const *x
= a
, *const *y
= b
;
191 return (strcmp(SYM_NAME(*x
), SYM_NAME(*y
)));
194 int key_encode(key_data
*k
, dstr
*d
, const key_filter
*kf
)
197 if (!KEY_MATCH(k
, kf
))
199 switch (k
->e
& KF_ENCMASK
) {
204 DENSURE(d
, (k
->u
.k
.sz
+ 7) & ~3);
207 STORE16(p
+ 2, k
->u
.k
.sz
);
209 DPUTM(d
, k
->u
.k
.k
, k
->u
.k
.sz
);
215 size_t sz
= mp_octets(k
->u
.m
);
217 DENSURE(d
, (sz
+ 7) & ~3);
221 mp_storeb(k
->u
.m
, p
+ 4, sz
);
228 size_t sz
= strlen(k
->u
.p
);
230 DENSURE(d
, (sz
+ 7) & ~3);
234 memcpy(p
+ 4, k
->u
.p
, sz
);
241 size_t xsz
= 0, ysz
= 0;
244 if (EC_ATINF(&k
->u
.e
))
247 xsz
= mp_octets(k
->u
.e
.x
);
248 ysz
= mp_octets(k
->u
.e
.y
);
251 DENSURE(d
, (sz
+ 7) & ~3);
255 if (!EC_ATINF(&k
->u
.e
)) {
257 mp_storeb(k
->u
.e
.x
, p
+ 6, xsz
);
258 STORE16(p
+ 6 + xsz
, ysz
);
259 mp_storeb(k
->u
.e
.y
, p
+ 8 + xsz
, ysz
);
268 key_struct
*ks
, **ksv
;
275 STORE16(p
, k
->e
& KF_ENCMASK
);
278 for (nks
= 0, sym_mkiter(&i
, &k
->u
.s
);
279 (ks
= sym_next(&i
)) != 0;
282 ksv
= xmalloc(nks
* sizeof(*ksv
));
283 for (j
= 0, sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; j
++)
285 qsort(ksv
, nks
, sizeof(*ksv
), ksbyname
);
286 for (j
= 0; j
< nks
; j
++) {
290 *(octet
*)(d
->buf
+ d
->len
++) = strlen(SYM_NAME(ks
));
291 DPUTS(d
, SYM_NAME(ks
));
294 if (key_encode(ks
->k
, d
, kf
))
315 /*----- That's all, folks -------------------------------------------------*/