5 * Encoding and decoding of key data
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>
46 /*----- Setting new values ------------------------------------------------*/
48 /* --- @key_binary@ --- *
50 * Arguments: @key_data *k@ = pointer to key data block
51 * @const void *p@ = pointer to key data
52 * @size_t sz@ = size of the key data
56 * Use: Sets a binary key in a key data block.
59 void key_binary(key_data
*k
, const void *p
, size_t sz
)
63 k
->u
.k
.k
= sub_alloc(sz
);
64 memcpy(k
->u
.k
.k
, p
, sz
);
68 /* --- @key_encrypted@ --- *
70 * Arguments: @key_data *k@ = pointer to key data block
71 * @const void *p@ = pointer to key data
72 * @size_t sz@ = size of the key data
76 * Use: Sets an encrypted key in a key data block.
79 void key_encrypted(key_data
*k
, const void *p
, size_t sz
)
83 k
->u
.k
.k
= sub_alloc(sz
);
84 memcpy(k
->u
.k
.k
, p
, sz
);
90 * Arguments: @key_data *k@ = pointer to key data block
91 * @mp *m@ = pointer to the value to set
95 * Use: Sets a multiprecision integer key in a key block.
98 void key_mp(key_data
*k
, mp
*m
)
105 /* --- @key_string@ --- *
107 * Arguments: @key_data *k@ = pointer to key data block
108 * @const char *p@ = pointer to the value to set
112 * Use: Sets a plain string in a key block.
115 void key_string(key_data
*k
, const char *p
)
122 /* --- @key_ec@ --- *
124 * Arguments: @key_data *k@ = pointer to key data block
125 * @const ec *e@ = pointer to the value to set
129 * Use: Sets an elliptic curve point in a key block.
132 void key_ec(key_data
*k
, const ec
*e
)
140 /* --- @key_structure@ --- *
142 * Arguments: @key_data *k@ = pointer to key data block
146 * Use: Initializes a structured key type.
149 void key_structure(key_data
*k
)
156 /* --- @key_structfind@ --- *
158 * Arguments: @key_data *k@ = pointer to key data block
159 * @const char *tag@ = pointer to tag string
161 * Returns: Pointer to key data block, or null.
163 * Use: Looks up the tag in a structured key.
166 key_data
*key_structfind(key_data
*k
, const char *tag
)
169 assert(((void)"Key is not structured",
170 (k
->e
& KF_ENCMASK
) == KENC_STRUCT
));
171 ks
= sym_find(&k
->u
.s
, tag
, -1, 0, 0);
177 /* --- @key_structcreate@ --- *
179 * Arguments: @key_data *k@ = pointer to key data block
180 * @const char *tag@ = pointer to tag string
182 * Returns: Pointer to newly created key data.
184 * Use: Creates a new uninitialized subkey.
187 key_data
*key_structcreate(key_data
*k
, const char *tag
)
192 assert(((void)"Key is not structured", k
->e
== KENC_STRUCT
));
193 ks
= sym_find(&k
->u
.s
, tag
, -1, sizeof(*ks
), &f
);
202 /*----- Miscellaneous operations ------------------------------------------*/
204 /* --- @key_destroy@ --- *
206 * Arguments: @key_data *k@ = pointer to key data to destroy
210 * Use: Destroys a lump of key data.
213 void key_destroy(key_data
*k
)
215 switch (k
->e
& KF_ENCMASK
) {
220 memset(k
->u
.k
.k
, 0, k
->u
.k
.sz
);
221 sub_free(k
->u
.k
.k
, k
->u
.k
.sz
);
237 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; )
239 sym_destroy(&k
->u
.s
);
242 k
->e
= (k
->e
& ~KF_ENCMASK
);
247 /* --- @key_do@ --- *
249 * Arguments: @key_data *k@ = pointer to key data block
250 * @const key_filter *kf@ = pointer to filter block
251 * @dstr *d@ = pointer to base string
252 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
253 * @void *p@ = argument to function
255 * Returns: Nonzero return code from function, or zero.
257 * Use: Runs a function over all the leaves of a key.
260 int key_do(key_data
*k
, const key_filter
*kf
, dstr
*d
,
261 int (*func
)(key_data */
*kd*/
, dstr */
*d*/
, void */
*p*/
),
264 if (!KEY_MATCH(k
, kf
))
266 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
267 return (func(k
, d
, p
));
276 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
279 dstr_putf(d
, ".%s", SYM_NAME(ks
));
281 if ((rc
= key_do(&ks
->k
, kf
, d
, func
, p
)) != 0)
288 /* --- @key_copy@ --- *
290 * Arguments: @key_data *kd@ = pointer to destination data block
291 * @key_data *k@ = pointer to source data block
292 * @const key_filter *kf@ = pointer to filter block
294 * Returns: Nonzero if an item was actually copied.
296 * Use: Copies a chunk of key data from one place to another.
299 int key_copy(key_data
*kd
, key_data
*k
, const key_filter
*kf
)
303 if (!KEY_MATCH(kd
, kf
))
305 switch (k
->e
& KF_ENCMASK
) {
307 /* --- Plain binary data --- */
314 kd
->u
.k
.k
= sub_alloc(k
->u
.k
.sz
);
315 memcpy(kd
->u
.k
.k
, k
->u
.k
.k
, k
->u
.k
.sz
);
317 kd
->u
.k
.sz
= k
->u
.k
.sz
;
320 /* --- Multiprecision integers --- */
323 kd
->u
.m
= MP_COPY(k
->u
.m
);
326 /* --- Strings --- */
329 kd
->u
.p
= xstrdup(k
->u
.p
);
332 /* --- Elliptic curve points --- */
336 EC_COPY(&kd
->u
.e
, &k
->u
.e
);
339 /* --- Structured key data --- */
346 sym_create(&kd
->u
.s
);
347 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
349 key_struct
*kks
= sym_find(&kd
->u
.s
, SYM_NAME(ks
), -1,
351 assert(((void)"Duplicate subkey tags", !f
));
352 if (key_copy(&kks
->k
, &ks
->k
, kf
))
355 sym_remove(&kd
->u
.s
, kks
);
358 sym_destroy(&kd
->u
.s
);
366 /*----- That's all, folks -------------------------------------------------*/