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>
42 /*----- Main code ---------------------------------------------------------*/
44 /* --- @key_decode@ --- *
46 * Arguments: @const void *p@ = pointer to buffer to read
47 * @size_t sz@ = size of the buffer
49 * Returns: The newly-read key data, or null if it failed.
51 * Use: Decodes a binary representation of a key.
54 key_data
*key_decode(const void *p
, size_t sz
)
61 /* --- Parse the header information --- *
63 * Make sure the size matches external reality. Security holes have been
64 * known to creep in without this sort of check. (No, this isn't an after-
73 /* --- Now decide what to do --- */
75 switch (e
& KF_ENCMASK
) {
77 /* --- Plain binary data --- */
81 kd
= key_newbinary(e
, q
+ 4, psz
);
84 /* --- Multiprecision integer data --- */
87 kd
= key_newmp(e
, mp_loadb(e
& KF_BURN ? MP_NEWSEC
: MP_NEW
,
91 /* --- String data --- */
95 kd
->u
.p
= xmalloc(sz
+ 1);
96 memcpy(kd
->u
.p
, q
+ 4, sz
);
100 /* --- Elliptic curve point data --- */
107 if (sz
< 2) return (0);
109 if (sz
< xsz
+ 4) return (0);
110 ysz
= LOAD16(q
+ 6 + xsz
);
111 if (sz
< xsz
+ ysz
+ 4) return (0);
112 kd
->u
.e
.x
= mp_loadb(MP_NEW
, q
+ 6, xsz
);
113 kd
->u
.e
.y
= mp_loadb(MP_NEW
, q
+ 8 + xsz
, ysz
);
116 /* --- Structured key data --- */
122 if ((e
& ~KF_ENCMASK
) || (psz
& 3))
125 kd
= key_newstruct();
129 /* --- Read the tag string --- */
135 DPUTM(&d
, q
+ 1, sz
);
140 /* --- Read the encoding and size --- */
142 sz
= (LOAD16(q
+ 2) + 7) & ~3;
146 /* --- Create a table node and fill it in --- */
148 if ((nkd
= key_decode(q
, sz
)) == 0)
150 key_structsteal(kd
, d
.buf
, nkd
);
157 /* --- Tidy up after a failure --- */
165 /* --- Everything else --- */
171 /* --- OK, that was good --- */
177 /* --- @key_encode@ --- *
179 * Arguments: @key_data *k@ = pointer to key data block
180 * @dstr *d@ = pointer to destination string
181 * @const key_filter *kf@ = pointer to key selection block
183 * Returns: Nonzero if an item was actually written.
185 * Use: Encodes a key block as binary data.
188 static int ksbyname(const void *a
, const void *b
) {
189 key_struct
*const *x
= a
, *const *y
= b
;
190 return (strcmp(SYM_NAME(*x
), SYM_NAME(*y
)));
193 int key_encode(key_data
*k
, dstr
*d
, const key_filter
*kf
)
196 if (!KEY_MATCH(k
, kf
))
198 switch (k
->e
& KF_ENCMASK
) {
203 DENSURE(d
, (k
->u
.k
.sz
+ 7) & ~3);
206 STORE16(p
+ 2, k
->u
.k
.sz
);
208 DPUTM(d
, k
->u
.k
.k
, k
->u
.k
.sz
);
214 size_t sz
= mp_octets(k
->u
.m
);
216 DENSURE(d
, (sz
+ 7) & ~3);
220 mp_storeb(k
->u
.m
, p
+ 4, sz
);
227 size_t sz
= strlen(k
->u
.p
);
229 DENSURE(d
, (sz
+ 7) & ~3);
233 memcpy(p
+ 4, k
->u
.p
, sz
);
240 size_t xsz
= 0, ysz
= 0;
243 if (EC_ATINF(&k
->u
.e
))
246 xsz
= mp_octets(k
->u
.e
.x
);
247 ysz
= mp_octets(k
->u
.e
.y
);
250 DENSURE(d
, (sz
+ 7) & ~3);
254 if (!EC_ATINF(&k
->u
.e
)) {
256 mp_storeb(k
->u
.e
.x
, p
+ 6, xsz
);
257 STORE16(p
+ 6 + xsz
, ysz
);
258 mp_storeb(k
->u
.e
.y
, p
+ 8 + xsz
, ysz
);
267 key_struct
*ks
, **ksv
;
274 STORE16(p
, k
->e
& KF_ENCMASK
);
277 for (nks
= 0, sym_mkiter(&i
, &k
->u
.s
);
278 (ks
= sym_next(&i
)) != 0;
281 ksv
= xmalloc(nks
* sizeof(*ksv
));
282 for (j
= 0, sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; j
++)
284 qsort(ksv
, nks
, sizeof(*ksv
), ksbyname
);
285 for (j
= 0; j
< nks
; j
++) {
289 *(octet
*)(d
->buf
+ d
->len
++) = strlen(SYM_NAME(ks
));
290 DPUTS(d
, SYM_NAME(ks
));
293 if (key_encode(ks
->k
, d
, kf
))
314 /*----- That's all, folks -------------------------------------------------*/