3 * $Id: key-data.c,v 1.2 2000/02/12 18:21:02 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 /*----- Revision history --------------------------------------------------*
32 * $Log: key-data.c,v $
33 * Revision 1.2 2000/02/12 18:21:02 mdw
34 * Overhaul of key management (again).
36 * Revision 1.1 1999/12/22 15:47:48 mdw
37 * Major key-management revision.
41 /*----- Header files ------------------------------------------------------*/
47 #include <mLib/base64.h>
48 #include <mLib/bits.h>
49 #include <mLib/dstr.h>
57 /*----- Setting new values ------------------------------------------------*/
59 /* --- @key_binary@ --- *
61 * Arguments: @key_data *k@ = pointer to key data block
62 * @const void *p@ = pointer to key data
63 * @size_t sz@ = size of the key data
67 * Use: Sets a binary key in a key data block.
70 void key_binary(key_data
*k
, const void *p
, size_t sz
)
72 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_BINARY
;
73 k
->u
.k
.k
= sub_alloc(sz
);
74 memcpy(k
->u
.k
.k
, p
, sz
);
78 /* --- @key_encrypted@ --- *
80 * Arguments: @key_data *k@ = pointer to key data block
81 * @const void *p@ = pointer to key data
82 * @size_t sz@ = size of the key data
86 * Use: Sets an encrypted key in a key data block.
89 void key_encrypted(key_data
*k
, const void *p
, size_t sz
)
91 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_ENCRYPT
;
92 k
->u
.k
.k
= sub_alloc(sz
);
93 memcpy(k
->u
.k
.k
, p
, sz
);
99 * Arguments: @key_data *k@ = pointer to key data block
100 * @mp *m@ = pointer to the value to set
104 * Use: Sets a multiprecision integer key in a key block.
107 void key_mp(key_data
*k
, mp
*m
)
109 k
->e
= (k
->e
& ~KF_ENCMASK
) | KENC_MP
;
113 /* --- @key_structure@ --- *
115 * Arguments: @key_data *k@ = pointer to key data block
119 * Use: Initializes a structured key type.
122 void key_structure(key_data
*k
)
128 /* --- @key_structfind@ --- *
130 * Arguments: @key_data *k@ = pointer to key data block
131 * @const char *tag@ = pointer to tag string
133 * Returns: Pointer to key data block, or null.
135 * Use: Looks up the tag in a structured key.
138 key_data
*key_structfind(key_data
*k
, const char *tag
)
141 assert(((void)"Key is not structured", k
->e
== KENC_STRUCT
));
142 ks
= sym_find(&k
->u
.s
, tag
, -1, 0, 0);
146 /* --- @key_structcreate@ --- *
148 * Arguments: @key_data *k@ = pointer to key data block
149 * @const char *tag@ = pointer to tag string
151 * Returns: Pointer to newly created key data.
153 * Use: Creates a new uninitialized subkey.
156 key_data
*key_structcreate(key_data
*k
, const char *tag
)
161 assert(((void)"Key is not structured", k
->e
== KENC_STRUCT
));
162 ks
= sym_find(&k
->u
.s
, tag
, -1, sizeof(*ks
), &f
);
169 /*----- Miscellaneous operations ------------------------------------------*/
171 /* --- @key_destroy@ --- *
173 * Arguments: @key_data *k@ = pointer to key data to destroy
177 * Use: Destroys a lump of key data.
180 void key_destroy(key_data
*k
)
182 switch (k
->e
& KF_ENCMASK
) {
186 memset(k
->u
.k
.k
, 0, k
->u
.k
.sz
);
187 sub_free(k
->u
.k
.k
, k
->u
.k
.sz
);
196 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
197 if (!(ks
->k
.e
& KF_TEMP
))
200 sym_destroy(&k
->u
.s
);
205 /* --- @key_do@ --- *
207 * Arguments: @key_data *k@ = pointer to key data block
208 * @const key_filter *kf@ = pointer to filter block
209 * @dstr *d@ = pointer to base string
210 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
211 * @void *p@ = argument to function
213 * Returns: Nonzero return code from function, or zero.
215 * Use: Runs a function over all the leaves of a key.
218 int key_do(key_data
*k
, const key_filter
*kf
, dstr
*d
,
219 int (*func
)(key_data */
*kd*/
, dstr */
*d*/
, void */
*p*/
),
222 if (!KEY_MATCH(k
, kf
))
224 if ((k
->e
& KF_ENCMASK
) != KENC_STRUCT
)
225 return (func(k
, d
, p
));
234 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
237 dstr_putf(d
, ".%s", SYM_NAME(ks
));
239 if ((rc
= key_do(&ks
->k
, kf
, d
, func
, p
)) != 0)
246 /* --- @key_copy@ --- *
248 * Arguments: @key_data *kd@ = pointer to destination data block
249 * @key_data *k@ = pointer to source data block
250 * @const key_filter *kf@ = pointer to filter block
252 * Returns: Nonzero if an item was actually copied.
254 * Use: Copies a chunk of key data from one place to another.
257 int key_copy(key_data
*kd
, key_data
*k
, const key_filter
*kf
)
261 if (!KEY_MATCH(kd
, kf
))
263 switch (k
->e
& KF_ENCMASK
) {
265 /* --- Plain binary data --- */
269 kd
->u
.k
.k
= sub_alloc(k
->u
.k
.sz
);
270 memcpy(kd
->u
.k
.k
, k
->u
.k
.k
, k
->u
.k
.sz
);
271 kd
->u
.k
.sz
= k
->u
.k
.sz
;
274 /* --- Multiprecision integers --- */
277 kd
->u
.m
= MP_COPY(k
->u
.m
);
280 /* --- Structured key data --- */
287 sym_create(&kd
->u
.s
);
288 for (sym_mkiter(&i
, &k
->u
.s
); (ks
= sym_next(&i
)) != 0; ) {
290 key_struct
*kks
= sym_find(&kd
->u
.s
, SYM_NAME(ks
), -1,
292 assert(((void)"Duplicate subkey tags", !f
));
293 if (key_copy(&kks
->k
, &ks
->k
, kf
))
296 sym_remove(&kd
->u
.s
, kks
);
299 sym_destroy(&kd
->u
.s
);
307 /*----- That's all, folks -------------------------------------------------*/