3 * $Id: key-text.c,v 1.5 2004/04/01 13:42:48 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.5 2004/04/01 13:42:48 mdw
34 * Missed off <ctype.h>\!
36 * Revision 1.4 2004/03/28 01:58:47 mdw
37 * Generate, store and retreive elliptic curve keys.
39 * Revision 1.3 2001/02/03 11:57:00 mdw
40 * Track mLib change: symbols no longer need to include a terminating
43 * Revision 1.2 2000/06/17 11:27:20 mdw
44 * Use secure memory interface from MP library.
46 * Revision 1.1 2000/02/12 18:21:02 mdw
47 * Overhaul of key management (again).
51 /*----- Header files ------------------------------------------------------*/
57 #include <mLib/base64.h>
58 #include <mLib/bits.h>
59 #include <mLib/dstr.h>
68 /*----- Main code ---------------------------------------------------------*/
70 /* --- @key_read@ --- *
72 * Arguments: @const char *p@ = pointer to textual key representation
73 * @key_data *k@ = pointer to output block for key data
74 * @char **pp@ = where to store the end pointer
76 * Returns: Zero if all went well, nonzero if there was a problem.
78 * Use: Parses a textual key description.
81 int key_read(const char *p
, key_data
*k
, char **pp
)
85 /* --- Read the encoding type --- *
87 * The key format is `[FLAGS:]DATA'. If there is no encoding type
88 * named, assume that it's `binary' for backwards compatibility.
91 if (strchr(p
, ':') == 0)
95 if (key_readflags(p
, &q
, &e
, 0))
100 /* --- Now scan the data based on the encoding type --- */
103 switch (e
& KF_ENCMASK
) {
105 /* --- Binary encoding --- *
107 * Simply read out the Base64-encoded data. Since `,' and `]' are our
108 * delimeter characters, and they can't appear in Base64-encoded data, I
109 * can just do a simple search to find the end of the encoded data.
116 size_t sz
= strcspn(p
, ",]");
119 base64_decode(&b
, p
, sz
, &d
);
120 base64_decode(&b
, 0, 0, &d
);
121 k
->u
.k
.k
= sub_alloc(d
.len
);
123 memcpy(k
->u
.k
.k
, d
.buf
, d
.len
);
128 /* --- Multiprecision integer encoding --- *
130 * Multiprecision integers have a convenient reading function.
135 mp
*m
= mp_readstring(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, p
, &q
, 0);
142 /* --- String encoding --- *
144 * We use form-urlencoding to ensure that evil characters don't get out.
149 size_t sz
= strcspn(p
, ",]");
150 const char *l
= p
+ sz
;
157 DPUTC(&d
, ' '); break;
159 x
= sscanf(p
+ 1, "%2x%n", &ch
, &n
);
160 if (x
== 1) { DPUTC(&d
, ch
); p
+= n
; break; }
162 DPUTC(&d
, *p
); break;
167 k
->u
.p
= xstrdup(d
.buf
);
171 /* --- Elliptic curve encoding --- *
173 * Again, we have a convenient function. Assume for now that points
174 * aren't secret. (Reasonably safe.)
182 if (!ec_ptparse(&qd
, &k
->u
.e
))
187 /* --- Structured information encoding --- *
189 * The format for structured key data is `[NAME=KEY,...]', where the
190 * brackets are part of the syntax. Structured keys have no flags apart
193 * The binary encoding only allows names up to 255 bytes long. Check for
201 /* --- Read the opening bracket --- */
209 /* --- Read named key subparts --- */
215 /* --- Stop if there's a close-bracket --- *
217 * This allows `[]' to be an empty structured key, which is good. It
218 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
225 /* --- Read the name out and check the length --- */
227 if ((q
= strchr(p
, '=')) == 0)
236 /* --- Add an appropriate block to the key table --- *
238 * Simply destroy old data if there's already a match.
243 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
248 /* --- Read the key data for the subkey --- */
250 if (key_read(q
+ 1, &ks
->k
, &q
)) {
251 sym_remove(&k
->u
.s
, ks
);
256 /* --- Read the comma or close-bracket --- */
266 /* --- Step past the close bracket --- */
272 /* --- Tidy up after a failure --- */
280 /* --- Anything else is unknown --- */
286 /* --- Return the end pointer --- */
293 /* --- @key_write@ --- *
295 * Arguments: @key_data *k@ = pointer to key data
296 * @dstr *d@ = destination string to write on
297 * @const key_filter *kf@ = pointer to key selection block
299 * Returns: Nonzero if an item was actually written.
301 * Use: Writes a key in a textual encoding.
304 int key_write(key_data
*k
, dstr
*d
, const key_filter
*kf
)
307 if (!KEY_MATCH(k
, kf
))
309 switch (k
->e
& KF_ENCMASK
) {
314 if ((k
->e
& KF_ENCMASK
) == KENC_BINARY
)
315 key_writeflags(k
->e
, d
);
317 DPUTS(d
, "encrypt,secret");
322 base64_encode(&b
, k
->u
.k
.k
, k
->u
.k
.sz
, d
);
323 base64_encode(&b
, 0, 0, d
);
327 key_writeflags(k
->e
, d
);
329 mp_writedstr(k
->u
.m
, d
, 10);
333 const char *p
= k
->u
.p
;
334 key_writeflags(k
->e
, d
);
337 if (*p
== ' ') DPUTC(d
, '+');
338 else if (!isalnum((unsigned char)*p
)) dstr_putf(d
, "%%%02x", *p
);
345 key_writeflags(k
->e
, d
);
346 DPUTS(d
, ":0x"); mp_writedstr(k
->u
.e
.x
, d
, 16);
347 DPUTS(d
, ",0x"); mp_writedstr(k
->u
.e
.y
, d
, 16);
356 DPUTS(d
, "struct:[");
357 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
361 DPUTS(d
, SYM_NAME(ks
));
363 if (!key_write(&ks
->k
, d
, kf
))
381 /*----- That's all, folks -------------------------------------------------*/