3 * $Id: key-text.c,v 1.4 2004/03/28 01:58:47 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.4 2004/03/28 01:58:47 mdw
34 * Generate, store and retreive elliptic curve keys.
36 * Revision 1.3 2001/02/03 11:57:00 mdw
37 * Track mLib change: symbols no longer need to include a terminating
40 * Revision 1.2 2000/06/17 11:27:20 mdw
41 * Use secure memory interface from MP library.
43 * Revision 1.1 2000/02/12 18:21:02 mdw
44 * Overhaul of key management (again).
48 /*----- Header files ------------------------------------------------------*/
53 #include <mLib/base64.h>
54 #include <mLib/bits.h>
55 #include <mLib/dstr.h>
64 /*----- Main code ---------------------------------------------------------*/
66 /* --- @key_read@ --- *
68 * Arguments: @const char *p@ = pointer to textual key representation
69 * @key_data *k@ = pointer to output block for key data
70 * @char **pp@ = where to store the end pointer
72 * Returns: Zero if all went well, nonzero if there was a problem.
74 * Use: Parses a textual key description.
77 int key_read(const char *p
, key_data
*k
, char **pp
)
81 /* --- Read the encoding type --- *
83 * The key format is `[FLAGS:]DATA'. If there is no encoding type
84 * named, assume that it's `binary' for backwards compatibility.
87 if (strchr(p
, ':') == 0)
91 if (key_readflags(p
, &q
, &e
, 0))
96 /* --- Now scan the data based on the encoding type --- */
99 switch (e
& KF_ENCMASK
) {
101 /* --- Binary encoding --- *
103 * Simply read out the Base64-encoded data. Since `,' and `]' are our
104 * delimeter characters, and they can't appear in Base64-encoded data, I
105 * can just do a simple search to find the end of the encoded data.
112 size_t sz
= strcspn(p
, ",]");
115 base64_decode(&b
, p
, sz
, &d
);
116 base64_decode(&b
, 0, 0, &d
);
117 k
->u
.k
.k
= sub_alloc(d
.len
);
119 memcpy(k
->u
.k
.k
, d
.buf
, d
.len
);
124 /* --- Multiprecision integer encoding --- *
126 * Multiprecision integers have a convenient reading function.
131 mp
*m
= mp_readstring(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, p
, &q
, 0);
138 /* --- String encoding --- *
140 * We use form-urlencoding to ensure that evil characters don't get out.
145 size_t sz
= strcspn(p
, ",]");
146 const char *l
= p
+ sz
;
153 DPUTC(&d
, ' '); break;
155 x
= sscanf(p
+ 1, "%2x%n", &ch
, &n
);
156 if (x
== 1) { DPUTC(&d
, ch
); p
+= n
; break; }
158 DPUTC(&d
, *p
); break;
163 k
->u
.p
= xstrdup(d
.buf
);
167 /* --- Elliptic curve encoding --- *
169 * Again, we have a convenient function. Assume for now that points
170 * aren't secret. (Reasonably safe.)
178 if (!ec_ptparse(&qd
, &k
->u
.e
))
183 /* --- Structured information encoding --- *
185 * The format for structured key data is `[NAME=KEY,...]', where the
186 * brackets are part of the syntax. Structured keys have no flags apart
189 * The binary encoding only allows names up to 255 bytes long. Check for
197 /* --- Read the opening bracket --- */
205 /* --- Read named key subparts --- */
211 /* --- Stop if there's a close-bracket --- *
213 * This allows `[]' to be an empty structured key, which is good. It
214 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
221 /* --- Read the name out and check the length --- */
223 if ((q
= strchr(p
, '=')) == 0)
232 /* --- Add an appropriate block to the key table --- *
234 * Simply destroy old data if there's already a match.
239 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
244 /* --- Read the key data for the subkey --- */
246 if (key_read(q
+ 1, &ks
->k
, &q
)) {
247 sym_remove(&k
->u
.s
, ks
);
252 /* --- Read the comma or close-bracket --- */
262 /* --- Step past the close bracket --- */
268 /* --- Tidy up after a failure --- */
276 /* --- Anything else is unknown --- */
282 /* --- Return the end pointer --- */
289 /* --- @key_write@ --- *
291 * Arguments: @key_data *k@ = pointer to key data
292 * @dstr *d@ = destination string to write on
293 * @const key_filter *kf@ = pointer to key selection block
295 * Returns: Nonzero if an item was actually written.
297 * Use: Writes a key in a textual encoding.
300 int key_write(key_data
*k
, dstr
*d
, const key_filter
*kf
)
303 if (!KEY_MATCH(k
, kf
))
305 switch (k
->e
& KF_ENCMASK
) {
310 if ((k
->e
& KF_ENCMASK
) == KENC_BINARY
)
311 key_writeflags(k
->e
, d
);
313 DPUTS(d
, "encrypt,secret");
318 base64_encode(&b
, k
->u
.k
.k
, k
->u
.k
.sz
, d
);
319 base64_encode(&b
, 0, 0, d
);
323 key_writeflags(k
->e
, d
);
325 mp_writedstr(k
->u
.m
, d
, 10);
329 const char *p
= k
->u
.p
;
330 key_writeflags(k
->e
, d
);
333 if (*p
== ' ') DPUTC(d
, '+');
334 else if (!isalnum((unsigned char)*p
)) dstr_putf(d
, "%%%02x", *p
);
341 key_writeflags(k
->e
, d
);
342 DPUTS(d
, ":0x"); mp_writedstr(k
->u
.e
.x
, d
, 16);
343 DPUTS(d
, ",0x"); mp_writedstr(k
->u
.e
.y
, d
, 16);
352 DPUTS(d
, "struct:[");
353 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
357 DPUTS(d
, SYM_NAME(ks
));
359 if (!key_write(&ks
->k
, d
, kf
))
377 /*----- That's all, folks -------------------------------------------------*/