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 * @char **pp@ = where to store the end pointer
54 * Returns: The newly-read key data, or null if it failed.
56 * Use: Parses a textual key description.
59 key_data
*key_read(const char *p
, 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 --- */
81 switch (e
& KF_ENCMASK
) {
83 /* --- Binary encoding --- *
85 * Simply read out the Base64-encoded data. Since `,' and `]' are our
86 * delimeter characters, and they can't appear in Base64-encoded data, I
87 * can just do a simple search to find the end of the encoded data.
94 size_t sz
= strcspn(p
, ",]");
97 base64_decode(&b
, p
, sz
, &d
);
98 base64_decode(&b
, 0, 0, &d
);
99 kd
= key_newbinary(e
, d
.buf
, d
.len
);
104 /* --- Multiprecision integer encoding --- *
106 * Multiprecision integers have a convenient reading function.
111 mp
*m
= mp_readstring(e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, p
, &q
, 0);
114 kd
= key_newmp(e
, m
);
119 /* --- String encoding --- *
121 * We use form-urlencoding to ensure that evil characters don't get out.
126 size_t sz
= strcspn(p
, ",]");
127 const char *l
= p
+ sz
;
134 DPUTC(&d
, ' '); break;
136 x
= sscanf(p
+ 1, "%2x%n", &ch
, &n
);
137 if (x
== 1) { DPUTC(&d
, ch
); p
+= n
; break; }
139 DPUTC(&d
, *p
); break;
144 kd
= key_newstring(e
, d
.buf
);
148 /* --- Elliptic curve encoding --- *
150 * Again, we have a convenient function. Assume for now that points
151 * aren't secret. (Reasonably safe.)
159 if (!ec_ptparse(&qd
, &pt
))
161 kd
= key_newec(e
, &pt
);
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
181 /* --- Read the opening bracket --- */
183 kd
= key_newstruct();
188 /* --- Read named key subparts --- */
193 /* --- Stop if there's a close-bracket --- *
195 * This allows `[]' to be an empty structured key, which is good. It
196 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
203 /* --- Read the name out and check the length --- */
205 if ((q
= strchr(p
, '=')) == 0)
214 /* --- Read the key data for the subkey --- */
216 if ((nkd
= key_read(q
+ 1, &q
)) == 0)
218 key_structsteal(kd
, d
.buf
, nkd
);
221 /* --- Read the comma or close-bracket --- */
231 /* --- Step past the close bracket --- */
237 /* --- Tidy up after a failure --- */
244 /* --- Anything else is unknown --- */
250 /* --- Return the end pointer --- */
258 /* --- @key_write@ --- *
260 * Arguments: @key_data *k@ = pointer to key data
261 * @dstr *d@ = destination string to write on
262 * @const key_filter *kf@ = pointer to key selection block
264 * Returns: Nonzero if an item was actually written.
266 * Use: Writes a key in a textual encoding.
269 int key_write(key_data
*k
, dstr
*d
, const key_filter
*kf
)
272 if (!KEY_MATCH(k
, kf
))
274 switch (k
->e
& KF_ENCMASK
) {
279 if ((k
->e
& KF_ENCMASK
) == KENC_BINARY
)
280 key_writeflags(k
->e
, d
);
282 DPUTS(d
, "encrypt,secret");
287 base64_encode(&b
, k
->u
.k
.k
, k
->u
.k
.sz
, d
);
288 base64_encode(&b
, 0, 0, d
);
292 key_writeflags(k
->e
, d
);
294 mp_writedstr(k
->u
.m
, d
, 10);
298 const char *p
= k
->u
.p
;
299 key_writeflags(k
->e
, d
);
302 if (*p
== ' ') DPUTC(d
, '+');
303 else if (!isalnum((unsigned char)*p
)) dstr_putf(d
, "%%%02x", *p
);
310 key_writeflags(k
->e
, d
);
311 DPUTS(d
, ":0x"); mp_writedstr(k
->u
.e
.x
, d
, 16);
312 DPUTS(d
, ",0x"); mp_writedstr(k
->u
.e
.y
, d
, 16);
321 DPUTS(d
, "struct:[");
322 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
328 if (!key_write(k
, d
, kf
))
346 /*----- That's all, folks -------------------------------------------------*/