3 * $Id: key-text.c,v 1.3 2001/02/03 11:57:00 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.3 2001/02/03 11:57:00 mdw
34 * Track mLib change: symbols no longer need to include a terminating
37 * Revision 1.2 2000/06/17 11:27:20 mdw
38 * Use secure memory interface from MP library.
40 * Revision 1.1 2000/02/12 18:21:02 mdw
41 * Overhaul of key management (again).
45 /*----- Header files ------------------------------------------------------*/
50 #include <mLib/base64.h>
51 #include <mLib/bits.h>
52 #include <mLib/dstr.h>
60 /*----- Main code ---------------------------------------------------------*/
62 /* --- @key_read@ --- *
64 * Arguments: @const char *p@ = pointer to textual key representation
65 * @key_data *k@ = pointer to output block for key data
66 * @char **pp@ = where to store the end pointer
68 * Returns: Zero if all went well, nonzero if there was a problem.
70 * Use: Parses a textual key description.
73 int key_read(const char *p
, key_data
*k
, char **pp
)
77 /* --- Read the encoding type --- *
79 * The key format is `[FLAGS:]DATA'. If there is no encoding type
80 * named, assume that it's `binary' for backwards compatibility.
83 if (strchr(p
, ':') == 0)
87 if (key_readflags(p
, &q
, &e
, 0))
92 /* --- Now scan the data based on the encoding type --- */
95 switch (e
& KF_ENCMASK
) {
97 /* --- Binary encoding --- *
99 * Simply read out the Base64-encoded data. Since `,' and `]' are our
100 * delimeter characters, and they can't appear in Base64-encoded data, I
101 * can just do a simple search to find the end of the encoded data.
108 size_t sz
= strcspn(p
, ",]");
111 base64_decode(&b
, p
, sz
, &d
);
112 base64_decode(&b
, 0, 0, &d
);
113 k
->u
.k
.k
= sub_alloc(d
.len
);
115 memcpy(k
->u
.k
.k
, d
.buf
, d
.len
);
120 /* --- Multiprecision integer encoding --- *
122 * Multiprecision integers have a convenient reading function.
127 mp
*m
= mp_readstring(k
->e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, p
, &q
, 0);
134 /* --- Structured information encoding --- *
136 * The format for structured key data is `[NAME=KEY,...]', where the
137 * brackets are part of the syntax. Structured keys have no flags apart
140 * The binary encoding only allows names up to 255 bytes long. Check for
148 /* --- Read the opening bracket --- */
156 /* --- Read named key subparts --- */
162 /* --- Stop if there's a close-bracket --- *
164 * This allows `[]' to be an empty structured key, which is good. It
165 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
172 /* --- Read the name out and check the length --- */
174 if ((q
= strchr(p
, '=')) == 0)
183 /* --- Add an appropriate block to the key table --- *
185 * Simply destroy old data if there's already a match.
190 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
, sizeof(*ks
), &f
);
195 /* --- Read the key data for the subkey --- */
197 if (key_read(q
+ 1, &ks
->k
, &q
)) {
198 sym_remove(&k
->u
.s
, ks
);
203 /* --- Read the comma or close-bracket --- */
213 /* --- Step past the close bracket --- */
219 /* --- Tidy up after a failure --- */
227 /* --- Anything else is unknown --- */
233 /* --- Return the end pointer --- */
240 /* --- @key_write@ --- *
242 * Arguments: @key_data *k@ = pointer to key data
243 * @dstr *d@ = destination string to write on
244 * @const key_filter *kf@ = pointer to key selection block
246 * Returns: Nonzero if an item was actually written.
248 * Use: Writes a key in a textual encoding.
251 int key_write(key_data
*k
, dstr
*d
, const key_filter
*kf
)
254 if (!KEY_MATCH(k
, kf
))
256 switch (k
->e
& KF_ENCMASK
) {
261 if ((k
->e
& KF_ENCMASK
) == KENC_BINARY
)
262 key_writeflags(k
->e
, d
);
264 DPUTS(d
, "encrypt,secret");
269 base64_encode(&b
, k
->u
.k
.k
, k
->u
.k
.sz
, d
);
270 base64_encode(&b
, 0, 0, d
);
274 key_writeflags(k
->e
, d
);
276 mp_writedstr(k
->u
.m
, d
, 10);
285 DPUTS(d
, "struct:[");
286 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
290 DPUTS(d
, SYM_NAME(ks
));
292 if (!key_write(&ks
->k
, d
, kf
))
310 /*----- That's all, folks -------------------------------------------------*/