5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/base64.h>
35 #include <mLib/bits.h>
36 #include <mLib/dstr.h>
45 /*----- Main code ---------------------------------------------------------*/
47 /* --- @key_read@ --- *
49 * Arguments: @const char *p@ = pointer to textual key representation
50 * @char **pp@ = where to store the end pointer
52 * Returns: The newly-read key data, or null if it failed.
54 * Use: Parses a textual key description.
57 key_data *key_read(const char *p, char **pp)
62 /* --- Read the encoding type --- *
64 * The key format is `[FLAGS:]DATA'. If there is no encoding type
65 * named, assume that it's `binary' for backwards compatibility.
68 if (strchr(p, ':') == 0)
72 if (key_readflags(p, &q, &e, 0))
77 /* --- Now scan the data based on the encoding type --- */
79 switch (e & KF_ENCMASK) {
81 /* --- Binary encoding --- *
83 * Simply read out the Base64-encoded data. Since `,' and `]' are our
84 * delimeter characters, and they can't appear in Base64-encoded data, I
85 * can just do a simple search to find the end of the encoded data.
92 size_t sz = strcspn(p, ",]");
95 base64_decode(&b, p, sz, &d);
96 base64_decode(&b, 0, 0, &d);
97 kd = key_newbinary(e, d.buf, d.len);
102 /* --- Multiprecision integer encoding --- *
104 * Multiprecision integers have a convenient reading function.
109 mp *m = mp_readstring(e & KF_BURN ? MP_NEWSEC : MP_NEW, p, &q, 0);
112 kd = key_newmp(e, m);
117 /* --- String encoding --- *
119 * We use form-urlencoding to ensure that evil characters don't get out.
124 size_t sz = strcspn(p, ",]");
125 const char *l = p + sz;
132 DPUTC(&d, ' '); break;
134 x = sscanf(p + 1, "%2x%n", &ch, &n);
135 if (x == 1) { DPUTC(&d, ch); p += n; break; }
137 DPUTC(&d, *p); break;
142 kd = key_newstring(e, d.buf);
146 /* --- Elliptic curve encoding --- *
148 * Again, we have a convenient function. Assume for now that points
149 * aren't secret. (Reasonably safe.)
157 if (!ec_ptparse(&qd, &pt))
159 kd = key_newec(e, &pt);
164 /* --- Structured information encoding --- *
166 * The format for structured key data is `[NAME=KEY,...]', where the
167 * brackets are part of the syntax. Structured keys have no flags apart
170 * The binary encoding only allows names up to 255 bytes long. Check for
179 /* --- Read the opening bracket --- */
181 kd = key_newstruct();
186 /* --- Read named key subparts --- */
191 /* --- Stop if there's a close-bracket --- *
193 * This allows `[]' to be an empty structured key, which is good. It
194 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
201 /* --- Read the name out and check the length --- */
203 if ((q = strchr(p, '=')) == 0)
212 /* --- Read the key data for the subkey --- */
214 if ((nkd = key_read(q + 1, &q)) == 0)
216 key_structsteal(kd, d.buf, nkd);
219 /* --- Read the comma or close-bracket --- */
229 /* --- Step past the close bracket --- */
235 /* --- Tidy up after a failure --- */
242 /* --- Anything else is unknown --- */
248 /* --- Return the end pointer --- */
256 /* --- @key_write@ --- *
258 * Arguments: @key_data *k@ = pointer to key data
259 * @dstr *d@ = destination string to write on
260 * @const key_filter *kf@ = pointer to key selection block
262 * Returns: Nonzero if an item was actually written.
264 * Use: Writes a key in a textual encoding.
267 int key_write(key_data *k, dstr *d, const key_filter *kf)
270 if (!KEY_MATCH(k, kf))
272 switch (k->e & KF_ENCMASK) {
277 if ((k->e & KF_ENCMASK) == KENC_BINARY)
278 key_writeflags(k->e, d);
280 DPUTS(d, "encrypt,secret");
285 base64_encode(&b, k->u.k.k, k->u.k.sz, d);
286 base64_encode(&b, 0, 0, d);
290 key_writeflags(k->e, d);
292 mp_writedstr(k->u.m, d, 10);
296 const char *p = k->u.p;
297 key_writeflags(k->e, d);
300 if (*p == ' ') DPUTC(d, '+');
301 else if (!isalnum((unsigned char)*p)) dstr_putf(d, "%%%02x", *p);
308 key_writeflags(k->e, d);
309 DPUTS(d, ":0x"); mp_writedstr(k->u.e.x, d, 16);
310 DPUTS(d, ",0x"); mp_writedstr(k->u.e.y, d, 16);
319 DPUTS(d, "struct:[");
320 for (key_mksubkeyiter(&i, k); key_nextsubkey(&i, &tag, &k); ) {
326 if (!key_write(k, d, kf))
344 /*----- That's all, folks -------------------------------------------------*/