3 * $Id: key-data.c,v 1.6 2004/04/08 01:36:15 mdw Exp $
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
)
61 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_BINARY
;
62 k
->u
.k
.k
= sub_alloc(sz
);
63 memcpy(k
->u
.k
.k
, p
, sz
);
67 /* --- @key_encrypted@ --- *
69 * Arguments: @key_data *k@ = pointer to key data block
70 * @const void *p@ = pointer to key data
71 * @size_t sz@ = size of the key data
75 * Use: Sets an encrypted key in a key data block.
78 void key_encrypted(key_data
*k
, const void *p
, size_t sz
)
80 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_ENCRYPT
;
81 k
->u
.k
.k
= sub_alloc(sz
);
82 memcpy(k
->u
.k
.k
, p
, sz
);
88 * Arguments: @key_data *k@ = pointer to key data block
89 * @mp *m@ = pointer to the value to set
93 * Use: Sets a multiprecision integer key in a key block.
96 void key_mp(key_data
*k
, mp
*m
)
98 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_MP
;
102 /* --- @key_string@ --- *
104 * Arguments: @key_data *k@ = pointer to key data block
105 * @const char *p@ = pointer to the value to set
109 * Use: Sets a plain string in a key block.
112 void key_string(key_data
*k
, const char *p
)
114 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_STRING
;
118 /* --- @key_ec@ --- *
120 * Arguments: @key_data *k@ = pointer to key data block
121 * @const ec *e@ = pointer to the value to set
125 * Use: Sets an elliptic curve point in a key block.
128 void key_ec(key_data
*k
, const ec
*e
)
130 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_EC
;
135 /* --- @key_structure@ --- *
137 * Arguments: @key_data *k@ = pointer to key data block
141 * Use: Initializes a structured key type.
144 void key_structure(key_data
*k
)
150 /* --- @key_structfind@ --- *
152 * Arguments: @key_data *k@ = pointer to key data block
153 * @const char *tag@ = pointer to tag string
155 * Returns: Pointer to key data block, or null.
157 * Use: Looks up the tag in a structured key.
160 key_data
*key_structfind(key_data
*k
, const char *tag
)
163 assert(((void)"Key is not structured",
164 (k
->e
& KF_ENCMASK
) == KENC_STRUCT
));
165 ks
= sym_find(&k
->u
.s
, tag
, -1, 0, 0);
171 /* --- @key_structcreate@ --- *
173 * Arguments: @key_data *k@ = pointer to key data block
174 * @const char *tag@ = pointer to tag string
176 * Returns: Pointer to newly created key data.
178 * Use: Creates a new uninitialized subkey.
181 key_data
*key_structcreate(key_data
*k
, const char *tag
)
186 assert(((void)"Key is not structured", k
->e
== KENC_STRUCT
));
187 ks
= sym_find(&k
->u
.s
, tag
, -1, sizeof(*ks
), &f
);
194 /*----- Miscellaneous operations ------------------------------------------*/
196 /* --- @key_destroy@ --- *
198 * Arguments: @key_data *k@ = pointer to key data to destroy
202 * Use: Destroys a lump of key data.
205 void key_destroy(key_data
*k
)
207 switch (k
->e
& KF_ENCMASK
) {
211 memset(k
->u
.k
.k
, 0, k
->u
.k
.sz
);
212 sub_free(k
->u
.k
.k
, k
->u
.k
.sz
);
227 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
228 if (!(ks
->k
.e
& KF_TEMP
))
231 sym_destroy(&k
->u
.s
);
236 /* --- @key_do@ --- *
238 * Arguments: @key_data *k@ = pointer to key data block
239 * @const key_filter *kf@ = pointer to filter block
240 * @dstr *d@ = pointer to base string
241 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
242 * @void *p@ = argument to function
244 * Returns: Nonzero return code from function, or zero.
246 * Use: Runs a function over all the leaves of a key.
249 int key_do(key_data
*k
, const key_filter
*kf
, dstr
*d
,
250 int (*func
)(key_data */
*kd*/
, dstr */
*d*/
, void */
*p*/
),
253 if (!KEY_MATCH(k
, kf
))
255 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
256 return (func(k
, d
, p
));
265 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
268 dstr_putf(d
, ".%s", SYM_NAME(ks
));
270 if ((rc
= key_do(&ks
->k
, kf
, d
, func
, p
)) != 0)
277 /* --- @key_copy@ --- *
279 * Arguments: @key_data *kd@ = pointer to destination data block
280 * @key_data *k@ = pointer to source data block
281 * @const key_filter *kf@ = pointer to filter block
283 * Returns: Nonzero if an item was actually copied.
285 * Use: Copies a chunk of key data from one place to another.
288 int key_copy(key_data
*kd
, key_data
*k
, const key_filter
*kf
)
292 if (!KEY_MATCH(kd
, kf
))
294 switch (k
->e
& KF_ENCMASK
) {
296 /* --- Plain binary data --- */
300 kd
->u
.k
.k
= sub_alloc(k
->u
.k
.sz
);
301 memcpy(kd
->u
.k
.k
, k
->u
.k
.k
, k
->u
.k
.sz
);
302 kd
->u
.k
.sz
= k
->u
.k
.sz
;
305 /* --- Multiprecision integers --- */
308 kd
->u
.m
= MP_COPY(k
->u
.m
);
311 /* --- Strings --- */
314 kd
->u
.p
= xstrdup(k
->u
.p
);
317 /* --- Elliptic curve points --- */
321 EC_COPY(&kd
->u
.e
, &k
->u
.e
);
324 /* --- Structured key data --- */
331 sym_create(&kd
->u
.s
);
332 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
334 key_struct
*kks
= sym_find(&kd
->u
.s
, SYM_NAME(ks
), -1,
336 assert(((void)"Duplicate subkey tags", !f
));
337 if (key_copy(&kks
->k
, &ks
->k
, kf
))
340 sym_remove(&kd
->u
.s
, kks
);
343 sym_destroy(&kd
->u
.s
);
351 /*----- That's all, folks -------------------------------------------------*/