Table for driving key data extraction.
[u/mdw/catacomb] / key-binary.c
CommitLineData
052b36d0 1/* -*-c-*-
2 *
3 * $Id: key-binary.c,v 1.1 2000/02/12 18:21:02 mdw Exp $
4 *
5 * Key binary encoding
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10/*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30/*----- Revision history --------------------------------------------------*
31 *
32 * $Log: key-binary.c,v $
33 * Revision 1.1 2000/02/12 18:21:02 mdw
34 * Overhaul of key management (again).
35 *
36 */
37
38/*----- Header files ------------------------------------------------------*/
39
40#include <stdlib.h>
41#include <string.h>
42
43#include <mLib/bits.h>
44#include <mLib/dstr.h>
45#include <mLib/sub.h>
46#include <mLib/sym.h>
47
48#include "key-data.h"
49#include "mp.h"
50#include "mptext.h"
51
52/*----- Main code ---------------------------------------------------------*/
53
54/* --- @key_decode@ --- *
55 *
56 * Arguments: @const void *p@ = pointer to buffer to read
57 * @size_t sz@ = size of the buffer
58 * @key_data *k@ = pointer to key data block to write to
59 *
60 * Returns: Zero if everything worked, nonzero otherwise.
61 *
62 * Use: Decodes a binary representation of a key.
63 */
64
65int key_decode(const void *p, size_t sz, key_data *k)
66{
67 const octet *q = p;
68 size_t psz;
69 unsigned e;
70
71 /* --- Parse the header information --- *
72 *
73 * Make sure the size matches external reality. Security holes have been
74 * known to creep in without this sort of check. (No, this isn't an after-
75 * the-fact patch-up.)
76 */
77
78 e = LOAD16(q);
79 psz = LOAD16(q + 2);
80 if (psz + 4 > sz)
81 return (-1);
82 k->e = e;
83
84 /* --- Now decide what to do --- */
85
86 switch (e & KF_ENCMASK) {
87
88 /* --- Plain binary data --- */
89
90 case KENC_BINARY:
91 case KENC_ENCRYPT:
92 k->u.k.k = sub_alloc(psz);
93 memcpy(k->u.k.k, q + 4, psz);
94 k->u.k.sz = psz;
95 break;
96
97 /* --- Multiprecision integer data --- */
98
99 case KENC_MP:
100 k->u.m = mp_loadb(MP_NEW, q + 4, psz);
101 if (k->e & KF_BURN)
102 mp_burn(k->u.m);
103 break;
104
105 /* --- Structured key data --- */
106
107 case KENC_STRUCT: {
108 dstr d = DSTR_INIT;
109 key_struct *ks;
110 unsigned f;
111
112 if ((k->e & ~KF_ENCMASK) || (psz & 3))
113 return (-1);
114 q += 4;
115 sym_create(&k->u.s);
116
117 while (psz) {
118
119 /* --- Read the tag string --- */
120
121 DRESET(&d);
122 sz = LOAD8(q);
123 if (sz >= psz)
124 goto fail;
125 DPUTM(&d, q + 1, sz);
126 DPUTZ(&d);
127 sz = (sz + 4) & ~3;
128 q += sz; psz -= sz;
129
130 /* --- Read the encoding and size --- */
131
132 e = LOAD16(q);
133 sz = (LOAD16(q + 2) + 7) & ~3;
134 if (sz > psz)
135 goto fail;
136
137 /* --- Create a table node and fill it in --- */
138
139 ks = sym_find(&k->u.s, d.buf, d.len + 1, sizeof(*ks), &f);
140 if (f)
141 goto fail;
142 if (key_decode(q, sz, &ks->k)) {
143 sym_remove(&k->u.s, ks);
144 goto fail;
145 }
146 psz -= sz;
147 q += sz;
148 }
149 dstr_destroy(&d);
150 break;
151
152 /* --- Tidy up after a failure --- */
153
154 fail:
155 dstr_destroy(&d);
156 key_destroy(k);
157 return (-1);
158 } break;
159
160 /* --- Everything else --- */
161
162 default:
163 return (-1);
164 }
165
166 /* --- OK, that was good --- */
167
168 return (0);
169}
170
171/* --- @key_encode@ --- *
172 *
173 * Arguments: @key_data *k@ = pointer to key data block
174 * @dstr *d@ = pointer to destination string
175 * @const key_filter *kf@ = pointer to key selection block
176 *
177 * Returns: Nonzero if an item was actually written.
178 *
179 * Use: Encodes a key block as binary data.
180 */
181
182int key_encode(key_data *k, dstr *d, const key_filter *kf)
183{
184 int rc = 0;
185 if (!KEY_MATCH(k, kf))
186 return (0);
187 switch (k->e & KF_ENCMASK) {
188 case KENC_BINARY:
189 case KENC_ENCRYPT: {
190 char *p;
191
192 DENSURE(d, (k->u.k.sz + 7) & ~3);
193 p = d->buf + d->len;
194 STORE16(p, k->e);
195 STORE16(p + 2, k->u.k.sz);
196 d->len += 4;
197 DPUTM(d, k->u.k.k, k->u.k.sz);
198 rc = 1;
199 } break;
200
201 case KENC_MP: {
202 char *p;
203 size_t sz = mp_octets(k->u.m);
204
205 DENSURE(d, (sz + 7) & ~3);
206 p = d->buf + d->len;
207 STORE16(p, k->e);
208 STORE16(p + 2, sz);
209 mp_storeb(k->u.m, p + 4, sz);
210 d->len += sz + 4;
211 rc = 1;
212 } break;
213
214 case KENC_STRUCT: {
215 size_t n;
216 char *p;
217 key_struct *ks;
218 sym_iter i;
219
220 n = d->len;
221 DENSURE(d, 4);
222 p = d->buf + n;
223 STORE16(p, k->e & KF_ENCMASK);
224 d->len += 4;
225 for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) {
226 size_t o = d->len;
227 DENSURE(d, 1);
228 *(octet *)(d->buf + d->len++) = strlen(SYM_NAME(ks));
229 DPUTS(d, SYM_NAME(ks));
230 while (d->len & 3)
231 DPUTC(d, 0);
232 if (key_encode(&ks->k, d, kf))
233 rc = 1;
234 else
235 d->len = o;
236 }
237 if (!rc)
238 d->len = n;
239 else {
240 p = d->buf + n + 2;
241 n = d->len - n - 4;
242 STORE16(p, n);
243 }
244 } break;
245 }
246 while (d->len & 3)
247 DPUTC(d, 0);
248 return (rc);
249}
250
251/*----- That's all, folks -------------------------------------------------*/