3 * $Id: key-text.c,v 1.2 2000/06/17 11:27:20 mdw Exp $
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: key-text.c,v $
33 * Revision 1.2 2000/06/17 11:27:20 mdw
34 * Use secure memory interface from MP library.
36 * Revision 1.1 2000/02/12 18:21:02 mdw
37 * Overhaul of key management (again).
41 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/base64.h>
47 #include <mLib/bits.h>
48 #include <mLib/dstr.h>
56 /*----- Main code ---------------------------------------------------------*/
58 /* --- @key_read@ --- *
60 * Arguments: @const char *p@ = pointer to textual key representation
61 * @key_data *k@ = pointer to output block for key data
62 * @char **pp@ = where to store the end pointer
64 * Returns: Zero if all went well, nonzero if there was a problem.
66 * Use: Parses a textual key description.
69 int key_read(const char *p, key_data *k, char **pp)
73 /* --- Read the encoding type --- *
75 * The key format is `[FLAGS:]DATA'. If there is no encoding type
76 * named, assume that it's `binary' for backwards compatibility.
79 if (strchr(p, ':') == 0)
83 if (key_readflags(p, &q, &e, 0))
88 /* --- Now scan the data based on the encoding type --- */
91 switch (e & KF_ENCMASK) {
93 /* --- Binary encoding --- *
95 * Simply read out the Base64-encoded data. Since `,' and `]' are our
96 * delimeter characters, and they can't appear in Base64-encoded data, I
97 * can just do a simple search to find the end of the encoded data.
104 size_t sz = strcspn(p, ",]");
107 base64_decode(&b, p, sz, &d);
108 base64_decode(&b, 0, 0, &d);
109 k->u.k.k = sub_alloc(d.len);
111 memcpy(k->u.k.k, d.buf, d.len);
116 /* --- Multiprecision integer encoding --- *
118 * Multiprecision integers have a convenient reading function.
123 mp *m = mp_readstring(k->e & KF_BURN ? MP_NEWSEC : MP_NEW, p, &q, 0);
130 /* --- Structured information encoding --- *
132 * The format for structured key data is `[NAME=KEY,...]', where the
133 * brackets are part of the syntax. Structured keys have no flags apart
136 * The binary encoding only allows names up to 255 bytes long. Check for
144 /* --- Read the opening bracket --- */
152 /* --- Read named key subparts --- */
158 /* --- Stop if there's a close-bracket --- *
160 * This allows `[]' to be an empty structured key, which is good. It
161 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
168 /* --- Read the name out and check the length --- */
170 if ((q = strchr(p, '=')) == 0)
179 /* --- Add an appropriate block to the key table --- *
181 * Simply destroy old data if there's already a match.
186 ks = sym_find(&k->u.s, d.buf, d.len + 1, sizeof(*ks), &f);
191 /* --- Read the key data for the subkey --- */
193 if (key_read(q + 1, &ks->k, &q)) {
194 sym_remove(&k->u.s, ks);
199 /* --- Read the comma or close-bracket --- */
209 /* --- Step past the close bracket --- */
215 /* --- Tidy up after a failure --- */
223 /* --- Anything else is unknown --- */
229 /* --- Return the end pointer --- */
236 /* --- @key_write@ --- *
238 * Arguments: @key_data *k@ = pointer to key data
239 * @dstr *d@ = destination string to write on
240 * @const key_filter *kf@ = pointer to key selection block
242 * Returns: Nonzero if an item was actually written.
244 * Use: Writes a key in a textual encoding.
247 int key_write(key_data *k, dstr *d, const key_filter *kf)
250 if (!KEY_MATCH(k, kf))
252 switch (k->e & KF_ENCMASK) {
257 if ((k->e & KF_ENCMASK) == KENC_BINARY)
258 key_writeflags(k->e, d);
260 DPUTS(d, "encrypt,secret");
265 base64_encode(&b, k->u.k.k, k->u.k.sz, d);
266 base64_encode(&b, 0, 0, d);
270 key_writeflags(k->e, d);
272 mp_writedstr(k->u.m, d, 10);
281 DPUTS(d, "struct:[");
282 for (sym_mkiter(&i, &k->u.s); (ks = sym_next(&i)) != 0; ) {
286 DPUTS(d, SYM_NAME(ks));
288 if (!key_write(&ks->k, d, kf))
306 /*----- That's all, folks -------------------------------------------------*/