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