progs/perftest.c: Use from Glibc syscall numbers.
[catacomb] / key / key-text.c
CommitLineData
052b36d0 1/* -*-c-*-
2 *
052b36d0 3 * Key textual 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
1589affa 30#include <ctype.h>
052b36d0 31#include <stdlib.h>
32#include <string.h>
33
34#include <mLib/base64.h>
35#include <mLib/bits.h>
36#include <mLib/dstr.h>
141c1284 37#include <mLib/macros.h>
052b36d0 38#include <mLib/sub.h>
39#include <mLib/sym.h>
1ba83484 40#include <mLib/url.h>
052b36d0 41
42#include "key-data.h"
43#include "mp.h"
44#include "mptext.h"
45
46/*----- Main code ---------------------------------------------------------*/
47
48/* --- @key_read@ --- *
49 *
50 * Arguments: @const char *p@ = pointer to textual key representation
052b36d0 51 * @char **pp@ = where to store the end pointer
52 *
ef13e9a4 53 * Returns: The newly-read key data, or null if it failed.
052b36d0 54 *
55 * Use: Parses a textual key description.
56 */
57
ef13e9a4 58key_data *key_read(const char *p, char **pp)
052b36d0 59{
60 unsigned e;
ef13e9a4 61 key_data *kd;
052b36d0 62
63 /* --- Read the encoding type --- *
64 *
65 * The key format is `[FLAGS:]DATA'. If there is no encoding type
66 * named, assume that it's `binary' for backwards compatibility.
67 */
68
69 if (strchr(p, ':') == 0)
70 e = 0;
71 else {
72 char *q;
73 if (key_readflags(p, &q, &e, 0))
ef13e9a4 74 return (0);
052b36d0 75 p = q + 1;
76 }
77
78 /* --- Now scan the data based on the encoding type --- */
79
052b36d0 80 switch (e & KF_ENCMASK) {
81
82 /* --- Binary encoding --- *
83 *
84 * Simply read out the Base64-encoded data. Since `,' and `]' are our
85 * delimeter characters, and they can't appear in Base64-encoded data, I
86 * can just do a simple search to find the end of the encoded data.
87 */
88
89 case KENC_BINARY:
90 case KENC_ENCRYPT: {
91 dstr d = DSTR_INIT;
92 base64_ctx b;
93 size_t sz = strcspn(p, ",]");
94
95 base64_init(&b);
96 base64_decode(&b, p, sz, &d);
97 base64_decode(&b, 0, 0, &d);
ef13e9a4 98 kd = key_newbinary(e, d.buf, d.len);
052b36d0 99 dstr_destroy(&d);
100 p += sz;
101 } break;
102
103 /* --- Multiprecision integer encoding --- *
104 *
105 * Multiprecision integers have a convenient reading function.
106 */
107
108 case KENC_MP: {
109 char *q;
ef13e9a4 110 mp *m = mp_readstring(e & KF_BURN ? MP_NEWSEC : MP_NEW, p, &q, 0);
052b36d0 111 if (!m)
ef13e9a4 112 return (0);
113 kd = key_newmp(e, m);
114 MP_DROP(m);
052b36d0 115 p = q;
116 } break;
117
1ba83484 118 /* --- String encoding --- *
119 *
120 * We use form-urlencoding to ensure that evil characters don't get out.
121 */
122
123 case KENC_STRING: {
124 dstr d = DSTR_INIT;
125 size_t sz = strcspn(p, ",]");
126 const char *l = p + sz;
127 unsigned int ch;
128 int x, n;
129
130 while (p < l) {
131 switch (*p) {
132 case '+':
133 DPUTC(&d, ' '); break;
134 case '%':
135 x = sscanf(p + 1, "%2x%n", &ch, &n);
136 if (x == 1) { DPUTC(&d, ch); p += n; break; }
137 default:
138 DPUTC(&d, *p); break;
139 }
140 p++;
141 }
142 DPUTZ(&d);
ef13e9a4 143 kd = key_newstring(e, d.buf);
1ba83484 144 dstr_destroy(&d);
145 } break;
146
147 /* --- Elliptic curve encoding --- *
148 *
149 * Again, we have a convenient function. Assume for now that points
150 * aren't secret. (Reasonably safe.)
151 */
152
153 case KENC_EC: {
ef13e9a4 154 ec pt = EC_INIT;
1ba83484 155 qd_parse qd;
156 qd.p = p;
157 qd.e = 0;
ef13e9a4 158 if (!ec_ptparse(&qd, &pt))
159 return (0);
160 kd = key_newec(e, &pt);
161 EC_DESTROY(&pt);
1ba83484 162 p = qd.p;
163 } break;
164
052b36d0 165 /* --- Structured information encoding --- *
166 *
167 * The format for structured key data is `[NAME=KEY,...]', where the
168 * brackets are part of the syntax. Structured keys have no flags apart
169 * from the encoding.
170 *
171 * The binary encoding only allows names up to 255 bytes long. Check for
172 * this here.
173 */
174
175 case KENC_STRUCT: {
176 dstr d = DSTR_INIT;
ef13e9a4 177 key_data *nkd;
052b36d0 178 char *q;
179
180 /* --- Read the opening bracket --- */
181
ef13e9a4 182 kd = key_newstruct();
052b36d0 183 if (*p != '[')
ef13e9a4 184 return (0);
052b36d0 185 p++;
052b36d0 186
187 /* --- Read named key subparts --- */
188
189 for (;;) {
190 size_t sz;
052b36d0 191
192 /* --- Stop if there's a close-bracket --- *
193 *
194 * This allows `[]' to be an empty structured key, which is good. It
195 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
196 * live with it.
197 */
198
199 if (*p == ']')
200 break;
201
202 /* --- Read the name out and check the length --- */
203
204 if ((q = strchr(p, '=')) == 0)
205 goto fail;
206 sz = q - p;
207 if (sz >= 256)
208 goto fail;
209 DRESET(&d);
210 DPUTM(&d, p, sz);
211 DPUTZ(&d);
212
052b36d0 213 /* --- Read the key data for the subkey --- */
214
ef13e9a4 215 if ((nkd = key_read(q + 1, &q)) == 0)
052b36d0 216 goto fail;
ef13e9a4 217 key_structsteal(kd, d.buf, nkd);
052b36d0 218 p = q;
219
220 /* --- Read the comma or close-bracket --- */
221
222 if (*p == ']')
223 break;
224 else if (*p == ',')
225 p++;
226 else
227 goto fail;
228 }
229
230 /* --- Step past the close bracket --- */
231
232 p++;
233 dstr_destroy(&d);
234 break;
235
236 /* --- Tidy up after a failure --- */
237
238 fail:
239 dstr_destroy(&d);
ef13e9a4 240 return (0);
052b36d0 241 } break;
242
243 /* --- Anything else is unknown --- */
244
245 default:
ef13e9a4 246 return (0);
052b36d0 247 }
248
249 /* --- Return the end pointer --- */
250
ef13e9a4 251 kd->e = e;
052b36d0 252 if (pp)
253 *pp = (char *)p;
ef13e9a4 254 return (kd);
052b36d0 255}
256
257/* --- @key_write@ --- *
258 *
259 * Arguments: @key_data *k@ = pointer to key data
260 * @dstr *d@ = destination string to write on
261 * @const key_filter *kf@ = pointer to key selection block
262 *
263 * Returns: Nonzero if an item was actually written.
264 *
265 * Use: Writes a key in a textual encoding.
266 */
45c0fd36 267
052b36d0 268int key_write(key_data *k, dstr *d, const key_filter *kf)
269{
270 int rc = 0;
271 if (!KEY_MATCH(k, kf))
272 return (0);
273 switch (k->e & KF_ENCMASK) {
274 case KENC_BINARY:
275 case KENC_ENCRYPT: {
276 base64_ctx b;
277
278 if ((k->e & KF_ENCMASK) == KENC_BINARY)
279 key_writeflags(k->e, d);
280 else
281 DPUTS(d, "encrypt,secret");
282 DPUTC(d, ':');
283 base64_init(&b);
284 b.indent = "";
285 b.maxline = 0;
286 base64_encode(&b, k->u.k.k, k->u.k.sz, d);
287 base64_encode(&b, 0, 0, d);
288 rc = 1;
289 } break;
290 case KENC_MP:
291 key_writeflags(k->e, d);
292 DPUTC(d, ':');
293 mp_writedstr(k->u.m, d, 10);
294 rc = 1;
295 break;
1ba83484 296 case KENC_STRING: {
297 const char *p = k->u.p;
298 key_writeflags(k->e, d);
299 DPUTC(d, ':');
300 while (*p) {
301 if (*p == ' ') DPUTC(d, '+');
141c1284 302 else if (!ISALNUM(*p)) dstr_putf(d, "%%%02x", *p);
1ba83484 303 else DPUTC(d, *p);
304 p++;
305 }
306 rc = 1;
307 } break;
308 case KENC_EC:
309 key_writeflags(k->e, d);
310 DPUTS(d, ":0x"); mp_writedstr(k->u.e.x, d, 16);
311 DPUTS(d, ",0x"); mp_writedstr(k->u.e.y, d, 16);
312 rc = 1;
313 break;
052b36d0 314 case KENC_STRUCT: {
ef13e9a4 315 key_subkeyiter i;
316 const char *tag;
052b36d0 317 char del = 0;
318 size_t n = d->len;
319
320 DPUTS(d, "struct:[");
ef13e9a4 321 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) {
052b36d0 322 size_t o = d->len;
323 if (del)
324 DPUTC(d, del);
ef13e9a4 325 DPUTS(d, tag);
052b36d0 326 DPUTC(d, '=');
ef13e9a4 327 if (!key_write(k, d, kf))
052b36d0 328 d->len = o;
329 else {
330 del = ',';
331 rc = 1;
332 }
333 }
334 if (!rc)
335 d->len = n;
336 else
337 DPUTC(d, ']');
338 } break;
339 }
340 DPUTZ(d);
341
342 return (rc);
343}
344
345/*----- That's all, folks -------------------------------------------------*/