3 * $Id: key-text.c,v 1.1 2000/02/12 18:21:02 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.1 2000/02/12 18:21:02 mdw
34 * Overhaul of key management (again).
38 /*----- Header files ------------------------------------------------------*/
43 #include <mLib/base64.h>
44 #include <mLib/bits.h>
45 #include <mLib/dstr.h>
53 /*----- Main code ---------------------------------------------------------*/
55 /* --- @key_read@ --- *
57 * Arguments: @const char *p@ = pointer to textual key representation
58 * @key_data *k@ = pointer to output block for key data
59 * @char **pp@ = where to store the end pointer
61 * Returns: Zero if all went well, nonzero if there was a problem.
63 * Use: Parses a textual key description.
66 int key_read(const char *p
, key_data
*k
, char **pp
)
70 /* --- Read the encoding type --- *
72 * The key format is `[FLAGS:]DATA'. If there is no encoding type
73 * named, assume that it's `binary' for backwards compatibility.
76 if (strchr(p
, ':') == 0)
80 if (key_readflags(p
, &q
, &e
, 0))
85 /* --- Now scan the data based on the encoding type --- */
88 switch (e
& KF_ENCMASK
) {
90 /* --- Binary encoding --- *
92 * Simply read out the Base64-encoded data. Since `,' and `]' are our
93 * delimeter characters, and they can't appear in Base64-encoded data, I
94 * can just do a simple search to find the end of the encoded data.
101 size_t sz
= strcspn(p
, ",]");
104 base64_decode(&b
, p
, sz
, &d
);
105 base64_decode(&b
, 0, 0, &d
);
106 k
->u
.k
.k
= sub_alloc(d
.len
);
108 memcpy(k
->u
.k
.k
, d
.buf
, d
.len
);
113 /* --- Multiprecision integer encoding --- *
115 * Multiprecision integers have a convenient reading function.
120 mp
*m
= mp_readstring(MP_NEW
, p
, &q
, 0);
129 /* --- Structured information encoding --- *
131 * The format for structured key data is `[NAME=KEY,...]', where the
132 * brackets are part of the syntax. Structured keys have no flags apart
135 * The binary encoding only allows names up to 255 bytes long. Check for
143 /* --- Read the opening bracket --- */
151 /* --- Read named key subparts --- */
157 /* --- Stop if there's a close-bracket --- *
159 * This allows `[]' to be an empty structured key, which is good. It
160 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
167 /* --- Read the name out and check the length --- */
169 if ((q
= strchr(p
, '=')) == 0)
178 /* --- Add an appropriate block to the key table --- *
180 * Simply destroy old data if there's already a match.
185 ks
= sym_find(&k
->u
.s
, d
.buf
, d
.len
+ 1, sizeof(*ks
), &f
);
190 /* --- Read the key data for the subkey --- */
192 if (key_read(q
+ 1, &ks
->k
, &q
)) {
193 sym_remove(&k
->u
.s
, ks
);
198 /* --- Read the comma or close-bracket --- */
208 /* --- Step past the close bracket --- */
214 /* --- Tidy up after a failure --- */
222 /* --- Anything else is unknown --- */
228 /* --- Return the end pointer --- */
235 /* --- @key_write@ --- *
237 * Arguments: @key_data *k@ = pointer to key data
238 * @dstr *d@ = destination string to write on
239 * @const key_filter *kf@ = pointer to key selection block
241 * Returns: Nonzero if an item was actually written.
243 * Use: Writes a key in a textual encoding.
246 int key_write(key_data
*k
, dstr
*d
, const key_filter
*kf
)
249 if (!KEY_MATCH(k
, kf
))
251 switch (k
->e
& KF_ENCMASK
) {
256 if ((k
->e
& KF_ENCMASK
) == KENC_BINARY
)
257 key_writeflags(k
->e
, d
);
259 DPUTS(d
, "encrypt,secret");
264 base64_encode(&b
, k
->u
.k
.k
, k
->u
.k
.sz
, d
);
265 base64_encode(&b
, 0, 0, d
);
269 key_writeflags(k
->e
, d
);
271 mp_writedstr(k
->u
.m
, d
, 10);
280 DPUTS(d
, "struct:[");
281 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
285 DPUTS(d
, SYM_NAME(ks
));
287 if (!key_write(&ks
->k
, d
, kf
))
305 /*----- That's all, folks -------------------------------------------------*/