052b36d0 |
1 | /* -*-c-*- |
2 | * |
3 | * $Id: key-text.c,v 1.1 2000/02/12 18:21:02 mdw Exp $ |
4 | * |
5 | * Key textual 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-text.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/base64.h> |
44 | #include <mLib/bits.h> |
45 | #include <mLib/dstr.h> |
46 | #include <mLib/sub.h> |
47 | #include <mLib/sym.h> |
48 | |
49 | #include "key-data.h" |
50 | #include "mp.h" |
51 | #include "mptext.h" |
52 | |
53 | /*----- Main code ---------------------------------------------------------*/ |
54 | |
55 | /* --- @key_read@ --- * |
56 | * |
57 | * Arguments: @const char *p@ = pointer to textual key representation |
58 | * @key_data *k@ = pointer to output block for key data |
59 | * @char **pp@ = where to store the end pointer |
60 | * |
61 | * Returns: Zero if all went well, nonzero if there was a problem. |
62 | * |
63 | * Use: Parses a textual key description. |
64 | */ |
65 | |
66 | int key_read(const char *p, key_data *k, char **pp) |
67 | { |
68 | unsigned e; |
69 | |
70 | /* --- Read the encoding type --- * |
71 | * |
72 | * The key format is `[FLAGS:]DATA'. If there is no encoding type |
73 | * named, assume that it's `binary' for backwards compatibility. |
74 | */ |
75 | |
76 | if (strchr(p, ':') == 0) |
77 | e = 0; |
78 | else { |
79 | char *q; |
80 | if (key_readflags(p, &q, &e, 0)) |
81 | return (-1); |
82 | p = q + 1; |
83 | } |
84 | |
85 | /* --- Now scan the data based on the encoding type --- */ |
86 | |
87 | k->e = e; |
88 | switch (e & KF_ENCMASK) { |
89 | |
90 | /* --- Binary encoding --- * |
91 | * |
92 | * Simply read out the Base64-encoded data. Since `,' and `]' are our |
93 | * delimeter characters, and they can't appear in Base64-encoded data, I |
94 | * can just do a simple search to find the end of the encoded data. |
95 | */ |
96 | |
97 | case KENC_BINARY: |
98 | case KENC_ENCRYPT: { |
99 | dstr d = DSTR_INIT; |
100 | base64_ctx b; |
101 | size_t sz = strcspn(p, ",]"); |
102 | |
103 | base64_init(&b); |
104 | base64_decode(&b, p, sz, &d); |
105 | base64_decode(&b, 0, 0, &d); |
106 | k->u.k.k = sub_alloc(d.len); |
107 | k->u.k.sz = d.len; |
108 | memcpy(k->u.k.k, d.buf, d.len); |
109 | dstr_destroy(&d); |
110 | p += sz; |
111 | } break; |
112 | |
113 | /* --- Multiprecision integer encoding --- * |
114 | * |
115 | * Multiprecision integers have a convenient reading function. |
116 | */ |
117 | |
118 | case KENC_MP: { |
119 | char *q; |
120 | mp *m = mp_readstring(MP_NEW, p, &q, 0); |
121 | if (!m) |
122 | return (-1); |
123 | if (k->e & KF_BURN) |
124 | mp_burn(m); |
125 | k->u.m = m; |
126 | p = q; |
127 | } break; |
128 | |
129 | /* --- Structured information encoding --- * |
130 | * |
131 | * The format for structured key data is `[NAME=KEY,...]', where the |
132 | * brackets are part of the syntax. Structured keys have no flags apart |
133 | * from the encoding. |
134 | * |
135 | * The binary encoding only allows names up to 255 bytes long. Check for |
136 | * this here. |
137 | */ |
138 | |
139 | case KENC_STRUCT: { |
140 | dstr d = DSTR_INIT; |
141 | char *q; |
142 | |
143 | /* --- Read the opening bracket --- */ |
144 | |
145 | k->e &= KF_ENCMASK; |
146 | if (*p != '[') |
147 | return (-1); |
148 | p++; |
149 | sym_create(&k->u.s); |
150 | |
151 | /* --- Read named key subparts --- */ |
152 | |
153 | for (;;) { |
154 | size_t sz; |
155 | key_struct *ks; |
156 | |
157 | /* --- Stop if there's a close-bracket --- * |
158 | * |
159 | * This allows `[]' to be an empty structured key, which is good. It |
160 | * also makes `[foo=enc:bar,]' legal, and that's less good but I can |
161 | * live with it. |
162 | */ |
163 | |
164 | if (*p == ']') |
165 | break; |
166 | |
167 | /* --- Read the name out and check the length --- */ |
168 | |
169 | if ((q = strchr(p, '=')) == 0) |
170 | goto fail; |
171 | sz = q - p; |
172 | if (sz >= 256) |
173 | goto fail; |
174 | DRESET(&d); |
175 | DPUTM(&d, p, sz); |
176 | DPUTZ(&d); |
177 | |
178 | /* --- Add an appropriate block to the key table --- * |
179 | * |
180 | * Simply destroy old data if there's already a match. |
181 | */ |
182 | |
183 | { |
184 | unsigned f; |
185 | ks = sym_find(&k->u.s, d.buf, d.len + 1, sizeof(*ks), &f); |
186 | if (f) |
187 | key_destroy(&ks->k); |
188 | } |
189 | |
190 | /* --- Read the key data for the subkey --- */ |
191 | |
192 | if (key_read(q + 1, &ks->k, &q)) { |
193 | sym_remove(&k->u.s, ks); |
194 | goto fail; |
195 | } |
196 | p = q; |
197 | |
198 | /* --- Read the comma or close-bracket --- */ |
199 | |
200 | if (*p == ']') |
201 | break; |
202 | else if (*p == ',') |
203 | p++; |
204 | else |
205 | goto fail; |
206 | } |
207 | |
208 | /* --- Step past the close bracket --- */ |
209 | |
210 | p++; |
211 | dstr_destroy(&d); |
212 | break; |
213 | |
214 | /* --- Tidy up after a failure --- */ |
215 | |
216 | fail: |
217 | dstr_destroy(&d); |
218 | key_destroy(k); |
219 | return (-1); |
220 | } break; |
221 | |
222 | /* --- Anything else is unknown --- */ |
223 | |
224 | default: |
225 | return (-1); |
226 | } |
227 | |
228 | /* --- Return the end pointer --- */ |
229 | |
230 | if (pp) |
231 | *pp = (char *)p; |
232 | return (0); |
233 | } |
234 | |
235 | /* --- @key_write@ --- * |
236 | * |
237 | * Arguments: @key_data *k@ = pointer to key data |
238 | * @dstr *d@ = destination string to write on |
239 | * @const key_filter *kf@ = pointer to key selection block |
240 | * |
241 | * Returns: Nonzero if an item was actually written. |
242 | * |
243 | * Use: Writes a key in a textual encoding. |
244 | */ |
245 | |
246 | int key_write(key_data *k, dstr *d, const key_filter *kf) |
247 | { |
248 | int rc = 0; |
249 | if (!KEY_MATCH(k, kf)) |
250 | return (0); |
251 | switch (k->e & KF_ENCMASK) { |
252 | case KENC_BINARY: |
253 | case KENC_ENCRYPT: { |
254 | base64_ctx b; |
255 | |
256 | if ((k->e & KF_ENCMASK) == KENC_BINARY) |
257 | key_writeflags(k->e, d); |
258 | else |
259 | DPUTS(d, "encrypt,secret"); |
260 | DPUTC(d, ':'); |
261 | base64_init(&b); |
262 | b.indent = ""; |
263 | b.maxline = 0; |
264 | base64_encode(&b, k->u.k.k, k->u.k.sz, d); |
265 | base64_encode(&b, 0, 0, d); |
266 | rc = 1; |
267 | } break; |
268 | case KENC_MP: |
269 | key_writeflags(k->e, d); |
270 | DPUTC(d, ':'); |
271 | mp_writedstr(k->u.m, d, 10); |
272 | rc = 1; |
273 | break; |
274 | case KENC_STRUCT: { |
275 | sym_iter i; |
276 | key_struct *ks; |
277 | char del = 0; |
278 | size_t n = d->len; |
279 | |
280 | DPUTS(d, "struct:["); |
281 | for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) { |
282 | size_t o = d->len; |
283 | if (del) |
284 | DPUTC(d, del); |
285 | DPUTS(d, SYM_NAME(ks)); |
286 | DPUTC(d, '='); |
287 | if (!key_write(&ks->k, d, kf)) |
288 | d->len = o; |
289 | else { |
290 | del = ','; |
291 | rc = 1; |
292 | } |
293 | } |
294 | if (!rc) |
295 | d->len = n; |
296 | else |
297 | DPUTC(d, ']'); |
298 | } break; |
299 | } |
300 | DPUTZ(d); |
301 | |
302 | return (rc); |
303 | } |
304 | |
305 | /*----- That's all, folks -------------------------------------------------*/ |