3 * $Id: key-text.c,v 1.6 2004/04/08 01:36:15 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 /*----- Header files ------------------------------------------------------*/
36 #include <mLib/base64.h>
37 #include <mLib/bits.h>
38 #include <mLib/dstr.h>
47 /*----- Main code ---------------------------------------------------------*/
49 /* --- @key_read@ --- *
51 * Arguments: @const char *p@ = pointer to textual key representation
52 * @key_data *k@ = pointer to output block for key data
53 * @char **pp@ = where to store the end pointer
55 * Returns: Zero if all went well, nonzero if there was a problem.
57 * Use: Parses a textual key description.
60 int key_read(const char *p
, key_data
*k
, char **pp
)
64 /* --- Read the encoding type --- *
66 * The key format is `[FLAGS:]DATA'. If there is no encoding type
67 * named, assume that it's `binary' for backwards compatibility.
70 if (strchr(p
, ':') == 0)
74 if (key_readflags(p
, &q
, &e
, 0))
79 /* --- Now scan the data based on the encoding type --- */
82 switch (e
& KF_ENCMASK
) {
84 /* --- Binary encoding --- *
86 * Simply read out the Base64-encoded data. Since `,' and `]' are our
87 * delimeter characters, and they can't appear in Base64-encoded data, I
88 * can just do a simple search to find the end of the encoded data.
95 size_t sz
= strcspn(p
, ",]");
98 base64_decode(&b
, p
, sz
, &d
);
99 base64_decode(&b
, 0, 0, &d
);
100 k
->u
.k
.k
= sub_alloc(d
.len
);
102 memcpy(k
->u
.k
.k
, d
.buf
, d
.len
);
107 /* --- Multiprecision integer encoding --- *
109 * Multiprecision integers have a convenient reading function.
114 mp
*m
= mp_readstring(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, p
, &q
, 0);
121 /* --- String encoding --- *
123 * We use form-urlencoding to ensure that evil characters don't get out.
128 size_t sz
= strcspn(p
, ",]");
129 const char *l
= p
+ sz
;
136 DPUTC(&d
, ' '); break;
138 x
= sscanf(p
+ 1, "%2x%n", &ch
, &n
);
139 if (x
== 1) { DPUTC(&d
, ch
); p
+= n
; break; }
141 DPUTC(&d
, *p
); break;
146 k
->u
.p
= xstrdup(d
.buf
);
150 /* --- Elliptic curve encoding --- *
152 * Again, we have a convenient function. Assume for now that points
153 * aren't secret. (Reasonably safe.)
161 if (!ec_ptparse(&qd
, &k
->u
.e
))
166 /* --- Structured information encoding --- *
168 * The format for structured key data is `[NAME=KEY,...]', where the
169 * brackets are part of the syntax. Structured keys have no flags apart
172 * The binary encoding only allows names up to 255 bytes long. Check for
180 /* --- Read the opening bracket --- */
188 /* --- Read named key subparts --- */
194 /* --- Stop if there's a close-bracket --- *
196 * This allows `[]' to be an empty structured key, which is good. It
197 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
204 /* --- Read the name out and check the length --- */
206 if ((q
= strchr(p
, '=')) == 0)
215 /* --- Add an appropriate block to the key table --- *
217 * Simply destroy old data if there's already a match.
222 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
227 /* --- Read the key data for the subkey --- */
229 if (key_read(q
+ 1, &ks
->k
, &q
)) {
230 sym_remove(&k
->u
.s
, ks
);
235 /* --- Read the comma or close-bracket --- */
245 /* --- Step past the close bracket --- */
251 /* --- Tidy up after a failure --- */
259 /* --- Anything else is unknown --- */
265 /* --- Return the end pointer --- */
272 /* --- @key_write@ --- *
274 * Arguments: @key_data *k@ = pointer to key data
275 * @dstr *d@ = destination string to write on
276 * @const key_filter *kf@ = pointer to key selection block
278 * Returns: Nonzero if an item was actually written.
280 * Use: Writes a key in a textual encoding.
283 int key_write(key_data
*k
, dstr
*d
, const key_filter
*kf
)
286 if (!KEY_MATCH(k
, kf
))
288 switch (k
->e
& KF_ENCMASK
) {
293 if ((k
->e
& KF_ENCMASK
) == KENC_BINARY
)
294 key_writeflags(k
->e
, d
);
296 DPUTS(d
, "encrypt,secret");
301 base64_encode(&b
, k
->u
.k
.k
, k
->u
.k
.sz
, d
);
302 base64_encode(&b
, 0, 0, d
);
306 key_writeflags(k
->e
, d
);
308 mp_writedstr(k
->u
.m
, d
, 10);
312 const char *p
= k
->u
.p
;
313 key_writeflags(k
->e
, d
);
316 if (*p
== ' ') DPUTC(d
, '+');
317 else if (!isalnum((unsigned char)*p
)) dstr_putf(d
, "%%%02x", *p
);
324 key_writeflags(k
->e
, d
);
325 DPUTS(d
, ":0x"); mp_writedstr(k
->u
.e
.x
, d
, 16);
326 DPUTS(d
, ",0x"); mp_writedstr(k
->u
.e
.y
, d
, 16);
335 DPUTS(d
, "struct:[");
336 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
340 DPUTS(d
, SYM_NAME(ks
));
342 if (!key_write(&ks
->k
, d
, kf
))
360 /*----- That's all, folks -------------------------------------------------*/