5 * (c) 1999 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
34 #include <mLib/base64.h>
35 #include <mLib/bits.h>
36 #include <mLib/dstr.h>
37 #include <mLib/macros.h>
46 /*----- Main code ---------------------------------------------------------*/
48 /* --- @key_read@ --- *
50 * Arguments: @const char *p@ = pointer to textual key representation
51 * @char **pp@ = where to store the end pointer
53 * Returns: The newly-read key data, or null if it failed.
55 * Use: Parses a textual key description.
58 key_data
*key_read(const char *p
, char **pp
)
63 /* --- Read the encoding type --- *
65 * The key format is `[FLAGS:]DATA'. If there is no encoding type
66 * named, assume that it's `binary' for backwards compatibility.
69 if (strchr(p
, ':') == 0)
73 if (key_readflags(p
, &q
, &e
, 0))
78 /* --- Now scan the data based on the encoding type --- */
80 switch (e
& KF_ENCMASK
) {
82 /* --- Binary encoding --- *
84 * Simply read out the Base64-encoded data. Since `,' and `]' are our
85 * delimeter characters, and they can't appear in Base64-encoded data, I
86 * can just do a simple search to find the end of the encoded data.
93 size_t sz
= strcspn(p
, ",]");
96 base64_decode(&b
, p
, sz
, &d
);
97 base64_decode(&b
, 0, 0, &d
);
98 kd
= key_newbinary(e
, d
.buf
, d
.len
);
103 /* --- Multiprecision integer encoding --- *
105 * Multiprecision integers have a convenient reading function.
110 mp
*m
= mp_readstring(e
& KF_BURN ? MP_NEWSEC
: MP_NEW
, p
, &q
, 0);
113 kd
= key_newmp(e
, m
);
118 /* --- String encoding --- *
120 * We use form-urlencoding to ensure that evil characters don't get out.
125 size_t sz
= strcspn(p
, ",]");
126 const char *l
= p
+ sz
;
133 DPUTC(&d
, ' '); break;
135 x
= sscanf(p
+ 1, "%2x%n", &ch
, &n
);
136 if (x
== 1) { DPUTC(&d
, ch
); p
+= n
; break; }
138 DPUTC(&d
, *p
); break;
143 kd
= key_newstring(e
, d
.buf
);
147 /* --- Elliptic curve encoding --- *
149 * Again, we have a convenient function. Assume for now that points
150 * aren't secret. (Reasonably safe.)
158 if (!ec_ptparse(&qd
, &pt
))
160 kd
= key_newec(e
, &pt
);
165 /* --- Structured information encoding --- *
167 * The format for structured key data is `[NAME=KEY,...]', where the
168 * brackets are part of the syntax. Structured keys have no flags apart
171 * The binary encoding only allows names up to 255 bytes long. Check for
180 /* --- Read the opening bracket --- */
182 kd
= key_newstruct();
187 /* --- Read named key subparts --- */
192 /* --- Stop if there's a close-bracket --- *
194 * This allows `[]' to be an empty structured key, which is good. It
195 * also makes `[foo=enc:bar,]' legal, and that's less good but I can
202 /* --- Read the name out and check the length --- */
204 if ((q
= strchr(p
, '=')) == 0)
213 /* --- Read the key data for the subkey --- */
215 if ((nkd
= key_read(q
+ 1, &q
)) == 0)
217 key_structsteal(kd
, d
.buf
, nkd
);
220 /* --- Read the comma or close-bracket --- */
230 /* --- Step past the close bracket --- */
236 /* --- Tidy up after a failure --- */
243 /* --- Anything else is unknown --- */
249 /* --- Return the end pointer --- */
257 /* --- @key_write@ --- *
259 * Arguments: @key_data *k@ = pointer to key data
260 * @dstr *d@ = destination string to write on
261 * @const key_filter *kf@ = pointer to key selection block
263 * Returns: Nonzero if an item was actually written.
265 * Use: Writes a key in a textual encoding.
268 int key_write(key_data
*k
, dstr
*d
, const key_filter
*kf
)
271 if (!KEY_MATCH(k
, kf
))
273 switch (k
->e
& KF_ENCMASK
) {
278 if ((k
->e
& KF_ENCMASK
) == KENC_BINARY
)
279 key_writeflags(k
->e
, d
);
281 DPUTS(d
, "encrypt,secret");
286 base64_encode(&b
, k
->u
.k
.k
, k
->u
.k
.sz
, d
);
287 base64_encode(&b
, 0, 0, d
);
291 key_writeflags(k
->e
, d
);
293 mp_writedstr(k
->u
.m
, d
, 10);
297 const char *p
= k
->u
.p
;
298 key_writeflags(k
->e
, d
);
301 if (*p
== ' ') DPUTC(d
, '+');
302 else if (!ISALNUM(*p
)) dstr_putf(d
, "%%%02x", *p
);
309 key_writeflags(k
->e
, d
);
310 DPUTS(d
, ":0x"); mp_writedstr(k
->u
.e
.x
, d
, 16);
311 DPUTS(d
, ",0x"); mp_writedstr(k
->u
.e
.y
, d
, 16);
320 DPUTS(d
, "struct:[");
321 for (key_mksubkeyiter(&i
, k
); key_nextsubkey(&i
, &tag
, &k
); ) {
327 if (!key_write(k
, d
, kf
))
345 /*----- That's all, folks -------------------------------------------------*/