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
50 * @key_data *k@ = pointer to key data block to write to
52 * Returns: Zero if everything worked, nonzero otherwise.
54 * Use: Decodes a binary representation of a key.
57 int key_decode(const void *p
, size_t sz
, key_data
*k
)
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-
76 /* --- Now decide what to do --- */
78 switch (e
& KF_ENCMASK
) {
80 /* --- Plain binary data --- */
84 k
->u
.k
.k
= sub_alloc(psz
);
85 memcpy(k
->u
.k
.k
, q
+ 4, psz
);
89 /* --- Multiprecision integer data --- */
92 k
->u
.m
= mp_loadb(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, q
+ 4, psz
);
95 /* --- String data --- */
98 k
->u
.p
= xmalloc(sz
+ 1);
99 memcpy(k
->u
.p
, q
+ 4, sz
);
103 /* --- Elliptic curve point data --- */
109 if (sz
< 2) return (-1);
111 if (sz
< xsz
+ 4) return (-1);
112 ysz
= LOAD16(q
+ 6 + xsz
);
113 if (sz
< xsz
+ ysz
+ 4) return (-1);
114 k
->u
.e
.x
= mp_loadb(MP_NEW
, q
+ 6, xsz
);
115 k
->u
.e
.y
= mp_loadb(MP_NEW
, q
+ 8 + xsz
, ysz
);
118 /* --- Structured key data --- */
125 if ((k
->e
& ~KF_ENCMASK
) || (psz
& 3))
132 /* --- Read the tag string --- */
138 DPUTM(&d
, q
+ 1, sz
);
143 /* --- Read the encoding and size --- */
146 sz
= (LOAD16(q
+ 2) + 7) & ~3;
150 /* --- Create a table node and fill it in --- */
152 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
155 if (key_decode(q
, sz
, &ks
->k
)) {
156 sym_remove(&k
->u
.s
, ks
);
165 /* --- Tidy up after a failure --- */
173 /* --- Everything else --- */
179 /* --- OK, that was good --- */
184 /* --- @key_encode@ --- *
186 * Arguments: @key_data *k@ = pointer to key data block
187 * @dstr *d@ = pointer to destination string
188 * @const key_filter *kf@ = pointer to key selection block
190 * Returns: Nonzero if an item was actually written.
192 * Use: Encodes a key block as binary data.
195 static int ksbyname(const void *a
, const void *b
) {
196 key_struct
*const *x
= a
, *const *y
= b
;
197 return (strcmp(SYM_NAME(*x
), SYM_NAME(*y
)));
200 int key_encode(key_data
*k
, dstr
*d
, const key_filter
*kf
)
203 if (!KEY_MATCH(k
, kf
))
205 switch (k
->e
& KF_ENCMASK
) {
210 DENSURE(d
, (k
->u
.k
.sz
+ 7) & ~3);
213 STORE16(p
+ 2, k
->u
.k
.sz
);
215 DPUTM(d
, k
->u
.k
.k
, k
->u
.k
.sz
);
221 size_t sz
= mp_octets(k
->u
.m
);
223 DENSURE(d
, (sz
+ 7) & ~3);
227 mp_storeb(k
->u
.m
, p
+ 4, sz
);
234 size_t sz
= strlen(k
->u
.p
);
236 DENSURE(d
, (sz
+ 7) & ~3);
240 memcpy(p
+ 4, k
->u
.p
, sz
);
247 size_t xsz
= 0, ysz
= 0;
250 if (EC_ATINF(&k
->u
.e
))
253 xsz
= mp_octets(k
->u
.e
.x
);
254 ysz
= mp_octets(k
->u
.e
.y
);
257 DENSURE(d
, (sz
+ 7) & ~3);
261 if (!EC_ATINF(&k
->u
.e
)) {
263 mp_storeb(k
->u
.e
.x
, p
+ 6, xsz
);
264 STORE16(p
+ 6 + xsz
, ysz
);
265 mp_storeb(k
->u
.e
.y
, p
+ 8 + xsz
, ysz
);
274 key_struct
*ks
, **ksv
;
281 STORE16(p
, k
->e
& KF_ENCMASK
);
284 for (nks
= 0, sym_mkiter(&i
, &k
->u
.s
);
285 (ks
= sym_next(&i
)) != 0;
288 ksv
= xmalloc(nks
* sizeof(*ksv
));
289 for (j
= 0, sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; j
++)
291 qsort(ksv
, nks
, sizeof(*ksv
), ksbyname
);
292 for (j
= 0; j
< nks
; j
++) {
296 *(octet
*)(d
->buf
+ d
->len
++) = strlen(SYM_NAME(ks
));
297 DPUTS(d
, SYM_NAME(ks
));
300 if (key_encode(&ks
->k
, d
, kf
))
321 /*----- That's all, folks -------------------------------------------------*/