Deploy the new <ctype.h> and `foocmp' macros from mLib.
[catacomb] / key / key-binary.c
1 /* -*-c-*-
2 *
3 * Key binary encoding
4 *
5 * (c) 1999 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <mLib/bits.h>
34 #include <mLib/dstr.h>
35 #include <mLib/macros.h>
36 #include <mLib/sub.h>
37 #include <mLib/sym.h>
38
39 #include "key-data.h"
40 #include "mp.h"
41 #include "mptext.h"
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 /* --- @key_decode@ --- *
46 *
47 * Arguments: @const void *p@ = pointer to buffer to read
48 * @size_t sz@ = size of the buffer
49 *
50 * Returns: The newly-read key data, or null if it failed.
51 *
52 * Use: Decodes a binary representation of a key.
53 */
54
55 key_data *key_decode(const void *p, size_t sz)
56 {
57 const octet *q = p;
58 size_t psz;
59 key_data *kd;
60 unsigned e;
61
62 /* --- Parse the header information --- *
63 *
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-
66 * the-fact patch-up.)
67 */
68
69 e = LOAD16(q);
70 psz = LOAD16(q + 2);
71 if (psz + 4 > sz)
72 return (0);
73
74 /* --- Now decide what to do --- */
75
76 switch (e & KF_ENCMASK) {
77
78 /* --- Plain binary data --- */
79
80 case KENC_BINARY:
81 case KENC_ENCRYPT:
82 kd = key_newbinary(e, q + 4, psz);
83 break;
84
85 /* --- Multiprecision integer data --- */
86
87 case KENC_MP:
88 kd = key_newmp(e, mp_loadb(e & KF_BURN ? MP_NEWSEC : MP_NEW,
89 q + 4, psz));
90 break;
91
92 /* --- String data --- */
93
94 case KENC_STRING:
95 kd = key_newraw(e);
96 kd->u.p = xmalloc(sz + 1);
97 memcpy(kd->u.p, q + 4, sz);
98 kd->u.p[sz] = 0;
99 break;
100
101 /* --- Elliptic curve point data --- */
102
103 case KENC_EC: {
104 size_t xsz, ysz;
105 kd = key_newraw(e);
106 EC_CREATE(&kd->u.e);
107 if (!sz) break;
108 if (sz < 2) return (0);
109 xsz = LOAD16(q + 4);
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);
115 } break;
116
117 /* --- Structured key data --- */
118
119 case KENC_STRUCT: {
120 dstr d = DSTR_INIT;
121 key_data *nkd;
122
123 if ((e & ~KF_ENCMASK) || (psz & 3))
124 return (0);
125 q += 4;
126 kd = key_newstruct();
127
128 while (psz) {
129
130 /* --- Read the tag string --- */
131
132 DRESET(&d);
133 sz = LOAD8(q);
134 if (sz >= psz)
135 goto fail;
136 DPUTM(&d, q + 1, sz);
137 DPUTZ(&d);
138 sz = (sz + 4) & ~3;
139 q += sz; psz -= sz;
140
141 /* --- Read the encoding and size --- */
142
143 sz = (LOAD16(q + 2) + 7) & ~3;
144 if (sz > psz)
145 goto fail;
146
147 /* --- Create a table node and fill it in --- */
148
149 if ((nkd = key_decode(q, sz)) == 0)
150 goto fail;
151 key_structsteal(kd, d.buf, nkd);
152 psz -= sz;
153 q += sz;
154 }
155 dstr_destroy(&d);
156 break;
157
158 /* --- Tidy up after a failure --- */
159
160 fail:
161 dstr_destroy(&d);
162 key_drop(kd);
163 return (0);
164 } break;
165
166 /* --- Everything else --- */
167
168 default:
169 return (0);
170 }
171
172 /* --- OK, that was good --- */
173
174 kd->e = e;
175 return (kd);
176 }
177
178 /* --- @key_encode@ --- *
179 *
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
183 *
184 * Returns: Nonzero if an item was actually written.
185 *
186 * Use: Encodes a key block as binary data.
187 */
188
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)));
192 }
193
194 int key_encode(key_data *k, dstr *d, const key_filter *kf)
195 {
196 int rc = 0;
197 if (!KEY_MATCH(k, kf))
198 return (0);
199 switch (k->e & KF_ENCMASK) {
200 case KENC_BINARY:
201 case KENC_ENCRYPT: {
202 char *p;
203
204 DENSURE(d, (k->u.k.sz + 7) & ~3);
205 p = d->buf + d->len;
206 STORE16(p, k->e);
207 STORE16(p + 2, k->u.k.sz);
208 d->len += 4;
209 DPUTM(d, k->u.k.k, k->u.k.sz);
210 rc = 1;
211 } break;
212
213 case KENC_MP: {
214 char *p;
215 size_t sz = mp_octets(k->u.m);
216
217 DENSURE(d, (sz + 7) & ~3);
218 p = d->buf + d->len;
219 STORE16(p, k->e);
220 STORE16(p + 2, sz);
221 mp_storeb(k->u.m, p + 4, sz);
222 d->len += sz + 4;
223 rc = 1;
224 } break;
225
226 case KENC_STRING: {
227 char *p;
228 size_t sz = strlen(k->u.p);
229
230 DENSURE(d, (sz + 7) & ~3);
231 p = d->buf + d->len;
232 STORE16(p, k->e);
233 STORE16(p + 2, sz);
234 memcpy(p + 4, k->u.p, sz);
235 d->len += sz + 4;
236 rc = 1;
237 } break;
238
239 case KENC_EC: {
240 char *p;
241 size_t xsz = 0, ysz = 0;
242 size_t sz;
243
244 if (EC_ATINF(&k->u.e))
245 sz = 0;
246 else {
247 xsz = mp_octets(k->u.e.x);
248 ysz = mp_octets(k->u.e.y);
249 sz = xsz + ysz + 4;
250 }
251 DENSURE(d, (sz + 7) & ~3);
252 p = d->buf + d->len;
253 STORE16(p, k->e);
254 STORE16(p + 2, sz);
255 if (!EC_ATINF(&k->u.e)) {
256 STORE16(p + 4, xsz);
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);
260 }
261 d->len += sz + 4;
262 rc = 1;
263 } break;
264
265 case KENC_STRUCT: {
266 size_t n;
267 char *p;
268 key_struct *ks, **ksv;
269 size_t nks, j;
270 sym_iter i;
271
272 n = d->len;
273 DENSURE(d, 4);
274 p = d->buf + n;
275 STORE16(p, k->e & KF_ENCMASK);
276 d->len += 4;
277
278 for (nks = 0, sym_mkiter(&i, &k->u.s);
279 (ks = sym_next(&i)) != 0;
280 nks++);
281 if (nks) {
282 ksv = xmalloc(nks * sizeof(*ksv));
283 for (j = 0, sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; j++)
284 ksv[j] = ks;
285 qsort(ksv, nks, sizeof(*ksv), ksbyname);
286 for (j = 0; j < nks; j++) {
287 size_t o = d->len;
288 ks = ksv[j];
289 DENSURE(d, 1);
290 *(octet *)(d->buf + d->len++) = strlen(SYM_NAME(ks));
291 DPUTS(d, SYM_NAME(ks));
292 while (d->len & 3)
293 DPUTC(d, 0);
294 if (key_encode(ks->k, d, kf))
295 rc = 1;
296 else
297 d->len = o;
298 }
299 xfree(ksv);
300 }
301 if (!rc)
302 d->len = n;
303 else {
304 p = d->buf + n + 2;
305 n = d->len - n - 4;
306 STORE16(p, n);
307 }
308 } break;
309 }
310 while (d->len & 3)
311 DPUTC(d, 0);
312 return (rc);
313 }
314
315 /*----- That's all, folks -------------------------------------------------*/