3 * Simple key management
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 #ifndef CATACOMB_KEY_H
29 #define CATACOMB_KEY_H
35 /*----- Header files ------------------------------------------------------*/
40 #include <mLib/bits.h>
41 #include <mLib/dstr.h>
42 #include <mLib/hash.h>
45 #ifndef CATACOMB_KEY_ERROR_H
46 # include "key-error.h"
49 #ifndef CATACOMB_KEY_DATA_H
50 # include "key-data.h"
53 #ifndef CATACOMB_GHASH_H
61 /*----- Data structures ---------------------------------------------------*/
63 /* --- Key attributes --- *
65 * Each attribute is stored as a symbol in a symbol table. The value is
66 * the plain (not url-encoded) text to be written to the the file. If the
67 * value is binary data, then by this point it's base-64 encoded.
70 typedef struct key_attr
{
71 sym_base _b
; /* Symbol table data */
72 char *p
; /* Pointer to attribute value */
75 /* --- Main key structure --- *
77 * Each key is stored in two symbol tables, one indexed by keyid, and the
78 * other indexed by type. Because many keys can have the same type, the type
79 * table contains a list of keys, sorted in descending order of expiry.
84 /* --- Hashtable management --- */
86 hash_base _b
; /* Symbol table data */
87 struct key
*next
; /* Next key of the same type */
89 /* --- Basic key attributes --- */
91 uint32 id
; /* Key id used to name it */
92 char *tag
; /* Textual tag name */
93 char *type
; /* Textual key type */
94 time_t exp
, del
; /* Expiry times for keys */
96 /* --- The key data itself --- */
98 key_data
*k
; /* The actual key data */
100 /* --- Other attributes and commentary --- */
102 sym_table a
; /* Hashtable of key attributes */
103 char *c
; /* Any additional comments */
106 /* --- The keys-by-type entries --- */
108 typedef struct key_ref
{
109 sym_base _b
; /* Symbol table data */
110 key
*k
; /* Pointer to first key in list */
113 /* --- A key file --- */
115 typedef struct key_file
{
116 FILE *fp
; /* File pointer open on file */
117 char *name
; /* Filename used to create it */
118 unsigned f
; /* Various useful flags */
119 hash_table byid
; /* Table of keys by keyid */
120 sym_table bytype
; /* Table of keys by type */
121 sym_table bytag
; /* Table of keys by tag */
122 size_t idload
; /* Loading on id table */
125 /* --- Key file flags --- */
127 #define KF_WRITE 1u /* File opened for writing */
128 #define KF_MODIFIED 2u /* File has been modified */
130 /* --- Iterating over keys --- *
132 * Both of these are simple symbol table iterators, but they're made distinct
133 * types for the dubious benefits that type safety brings.
136 typedef struct { hash_iter i
; time_t t
; } key_iter
;
137 typedef struct { sym_iter i
; } key_attriter
;
139 /* --- Key fetching --- */
141 typedef struct key_fetchdef
{
142 char *name
; /* Name of item */
143 size_t off
; /* Offset into target structure */
144 unsigned e
; /* Flags for the item */
145 const struct key_fetchdef
*kf
; /* Substructure pointer */
148 /* --- File opening options --- */
150 #define KOPEN_READ 0u
151 #define KOPEN_WRITE 1u
152 #define KOPEN_MASK 0xff
153 #define KOPEN_NOFILE 0x100
155 /* --- Various other magic numbers --- */
157 #define KEXP_FOREVER ((time_t)-1) /* Never expire this key */
158 #define KEXP_EXPIRE ((time_t)-2) /* Expire this key when unused */
160 /* --- Write error codes --- */
163 KWRITE_OK
, /* Everything went fine */
164 KWRITE_FAIL
= -1, /* Close attempt failed */
165 KWRITE_BROKEN
= -2 /* Key ring needs manual fixing */
168 /* --- Error reporting functions for @key_merge@ and @key_open@ --- */
170 typedef void key_reporter(const char */
*file*/
, int /*line*/,
171 const char */
*err*/
, void */
*p*/
);
173 /* --- Macros for testing expiry --- */
175 #define KEY_EXPIRED(now, exp) \
176 ((exp) == KEXP_EXPIRE || ((exp) != KEXP_FOREVER && (exp) < (now)))
178 /*----- Reading and writing keys and files --------------------------------*/
180 /* --- @key_merge@ --- *
182 * Arguments: @key_file *f@ = pointer to file structure
183 * @const char *file@ = name of file (for error messages)
184 * @FILE *fp@ = file handle to read from
185 * @key_reporter *rep@ = error reporting function
186 * @void *arg@ = argument for function
188 * Returns: Error code (one of the @KERR@ constants).
190 * Use: Reads keys from a file, and inserts them into the file.
193 extern int key_merge(key_file */
*f*/
, const char */
*file*/
, FILE */
*fp*/
,
194 key_reporter */
*rep*/
, void */
*arg*/
);
196 /* --- @key_extract@ --- *
198 * Arguments: @key_file *f@ = pointer to file structure
199 * @key *k@ = key to extract
200 * @FILE *fp@ = file to write on
201 * @const key_filter *kf@ = pointer to key selection block
203 * Returns: Zero if OK, EOF on error.
205 * Use: Extracts a key to an ouptut file.
208 extern int key_extract(key_file */
*f*/
, key */
*k*/
, FILE */
*fp*/
,
209 const key_filter */
*kf*/
);
211 /* --- @key_open@ --- *
213 * Arguments: @key_file *f@ = pointer to file structure to initialize
214 * @const char *file@ = pointer to the file name
215 * @unsigned how@ = opening options (@KOPEN_*@).
216 * @key_reporter *rep@ = error reporting function
217 * @void *arg@ = argument for function
219 * Returns: Zero if it worked, nonzero otherwise.
221 * Use: Opens a key file, reads its contents, and stores them in a
222 * structure. The file is locked appropriately until closed
223 * using @key_close@. On an error, everything is cleared away
224 * tidily. If the file is opened with @KOPEN_WRITE@, it's
225 * created if necessary, with read and write permissions for its
229 extern int key_open(key_file */
*f*/
, const char */
*file*/
, unsigned /*how*/,
230 key_reporter */
*rep*/
, void */
*arg*/
);
232 /* --- @key_discard@ --- *
234 * Arguments: @key_file *f@ = pointer to key file block
238 * Use: Frees all the key data, without writing changes.
241 extern void key_discard(key_file */
*f*/
);
243 /* --- @key_close@ --- *
245 * Arguments: @key_file *f@ = pointer to key file block
247 * Returns: A @KWRITE_@ code indicating how it went.
249 * Use: Frees all the key data, writes any changes. Make sure that
250 * all hell breaks loose if this returns @KWRITE_BROKEN@.
253 extern int key_close(key_file */
*f*/
);
255 /* --- @key_save@ --- *
257 * Arguments: @key_file *f@ = pointer to key file block
259 * Returns: A @KWRITE_@ code indicating how well it worked.
261 * Use: Writes a key file's data back to the actual file. This code
262 * is extremely careful about error handling. It should usually
263 * be able to back out somewhere sensible, but it can tell when
264 * it's got itself into a real pickle and starts leaving well
267 * Callers, please make sure that you ring alarm bells when this
268 * function returns @KWRITE_BROKEN@.
271 extern int key_save(key_file */
*f*/
);
273 /* --- @key_lockfile@ --- *
275 * Arguments: @key_file *f@ = pointer to file structure to initialize
276 * @const char *file@ = pointer to the file name
277 * @unsigned how@ = opening options (@KOPEN_*@).
279 * Returns: Zero if it worked, nonzero otherwise.
281 * Use: Opens a keyfile and stores the information needed for
282 * continued access in the structure.
284 * If the file is opened with @KOPEN_WRITE@, it's created if
285 * necessary with read and write permissions for owner only, and
286 * locked for update while it's open.
288 * This is a system-dependent routine, and only really intended
289 * for the private use of @key_open@.
292 extern int key_lockfile(key_file */
*f*/
, const char */
*file*/
,
295 /*----- Creating and manipulating keys ------------------------------------*/
299 * Arguments: @key_file *f@ = pointer to key file
300 * @uint32 id@ = keyid to set
301 * @const char *type@ = the type of this key
302 * @time_t exp@ = when the key expires
303 * @key *kk@ = where to put the key pointer
305 * Returns: Error code (one of the @KERR@ constants).
307 * Use: Attaches a new key to a key file. You must have a writable
308 * key file for this to work.
310 * The type is a key type string. This interface doesn't care
311 * about how type strings are formatted: it just treats them as
312 * opaque gobs of text. Clients are advised to choose some
313 * standard for representing key types, though.
315 * The expiry time should either be a time in the future, or the
316 * magic value @KEXP_FOREVER@ which means `never expire this
317 * key'. Be careful with `forever' keys. If I were you, I'd
318 * use a more sophisticated key management system than this for
321 * You have to set the actual key yourself.
324 extern int key_new(key_file */
*f*/
, uint32
/*id*/, const char */
*type*/
,
325 time_t /*exp*/, key
**/
*kk*/
);
327 /* --- @key_delete@ --- *
329 * Arguments: @key_file *f@ = pointer to file block
330 * @key *k@ = key to delete
332 * Returns: Error code (one of the @KERR@ constants).
334 * Use: Removes the given key from the list. The key file must be
335 * writable. (Due to the horridness of the data structures,
336 * deleted keys aren't actually removed, just marked so that
337 * they can't be looked up or iterated over. One upshot of
338 * this is that they don't get written back to the file when
342 extern int key_delete(key_file */
*f*/
, key */
*k*/
);
344 /* --- @key_expired@ --- *
346 * Arguments: @key *k@ = pointer to key block
348 * Returns: Zero if the key is OK, nonzero if it's expired.
351 int key_expired(key */
*k*/
);
353 /* --- @key_expire@ --- *
355 * Arguments: @key_file *f@ = pointer to file block
356 * @key *k@ = pointer to key block
358 * Returns: Error code (one of the @KERR@ constants).
360 * Use: Immediately marks the key as expired. It may be removed
361 * immediately, if it is no longer required, and will be removed
362 * by a tidy operation when it is no longer required. The key
363 * file must be writable.
366 extern int key_expire(key_file */
*f*/
, key */
*k*/
);
368 /* --- @key_used@ --- *
370 * Arguments: @key_file *f@ = pointer to key file
371 * @key *k@ = pointer to key block
372 * @time_t t@ = when key can be removed
374 * Returns: Zero if OK, nonzero on failure.
376 * Use: Marks a key as being required until a given time. Even
377 * though the key may expire before then (and won't be returned
378 * by type after that time), it will still be available when
379 * requested explicitly by id. The key file must be writable.
381 * The only (current) reason for failure is attempting to use
382 * a key which can expire for something which can't.
385 extern int key_used(key_file */
*f*/
, key */
*k*/
, time_t /*t*/);
387 /* --- @key_fingerprint@ --- *
389 * Arguments: @key *k@ = the key to fingerprint
390 * @ghash *h@ = the hash to use
391 * @const key_filter *kf@ = filter to apply
393 * Returns: Nonzero if the key slightly matched the filter.
395 * Use: Updates the hash context with the key contents.
398 extern int key_fingerprint(key */
*k*/
, ghash */
*h*/
,
399 const key_filter */
*kf*/
);
401 /*----- Setting and reading attributes ------------------------------------*/
403 /* --- @key_chkident@ --- *
405 * Arguments: @const char *p@ = pointer to a type string
407 * Returns: Zero if OK, -1 on error.
409 * Use: Checks whether an identification component string is OK.
412 extern int key_chkident(const char */
*p*/
);
414 /* --- @key_chkcomment@ --- *
416 * Arguments: @const char *p@ = pointer to a comment string
418 * Returns: Zero if OK, -1 on error.
420 * Use: Checks whether a comment string is OK.
423 extern int key_chkcomment(const char */
*p*/
);
425 /* --- @key_setcomment@ --- *
427 * Arguments: @key_file *f@ = pointer to key file block
428 * @key *k@ = pointer to key block
429 * @const char *c@ = pointer to comment to set, or zero
431 * Returns: Error code (one of the @KERR@ constants).
433 * Use: Replaces the key's current comment with a new one.
436 extern int key_setcomment(key_file */
*f*/
, key */
*k*/
, const char */
*c*/
);
438 /* --- @key_settag@ --- *
440 * Arguments: @key_file *f@ = pointer to key file block
441 * @key *k@ = pointer to key block
442 * @const char *tag@ = pointer to comment to set, or zero
444 * Returns: Error code (one of the @KERR@ constants).
446 * Use: Replaces the key's current tag with a new one.
449 extern int key_settag(key_file */
*f*/
, key */
*k*/
, const char */
*tag*/
);
451 /* --- @key_setkeydata@ --- *
453 * Arguments: @key_file *kf@ = pointer to key file
454 * @key *k@ = pointer to key
455 * @key_data *kd@ = new key data
457 * Returns: Zero on success, or a @KERR_@ error code on failure.
459 * Use: Sets the key data for a key.
462 extern int key_setkeydata(key_file */
*kf*/
, key */
*k*/
, key_data */
*kd*/
);
464 /* --- @key_fulltag@ --- *
466 * Arguments: @key *k@ = pointer to key
467 * @dstr *d@ = pointer to destination string
471 * Use: Emits the key's full tag, which has the form
472 * `ID:TYPE[:TAG]'. This is used in the textual file format,
473 * and to identify passphrases for locked keys.
476 extern void key_fulltag(key */
*k*/
, dstr */
*d*/
);
478 /* --- @key_qtag@ --- *
480 * Arguments: @key_file *f@ = key file to find a key from
481 * @const char *tag@ = pointer to tag string
482 * @dstr *d@ = pointer to string for full tag name
483 * @key **k@ = where to store the key pointer
484 * @key_data ***kd@ = where to store the key data pointer
486 * Returns: Zero if OK, nonzero if it failed.
488 * Use: Performs a full lookup on a qualified tag name. The tag is
489 * qualified by the names of subkeys, separated by dots. Hence,
490 * a qualified tag is ID|TAG[.TAG...]. The various result
491 * pointers can be null to indicate that the result isn't
495 extern int key_qtag(key_file */
*f*/
, const char */
*tag*/
,
496 dstr */
*d*/
, key
**/
*k*/
, key_data
***/
*kd*/
);
498 /* --- @key_getattr@ --- *
500 * Arguments: @key_file *f@ = pointer to file
501 * @key *k@ = pointer to key
502 * @const char *n@ = pointer to attribute name
504 * Returns: Pointer to attribute value, or null if not found.
506 * Use: Returns the value of a key attribute.
509 extern const char *key_getattr(key_file */
*f*/
, key */
*k*/
,
512 /* --- @key_putattr@ --- *
514 * Arguments: @key_file *f@ = pointer to file
515 * @key *k@ = pointer to key
516 * @const char *n@ = pointer to attribute name
517 * @const char *v@ = pointer to attribute value or null
519 * Returns: Error code (one of the @KERR@ constants).
521 * Use: Inserts an attribute on a key. If an attribute with the same
522 * name already exists, it is deleted. Setting a null value
523 * removes the attribute.
526 extern int key_putattr(key_file */
*f*/
, key */
*k*/
,
527 const char */
*n*/
, const char */
*v*/
);
529 /* --- @key_mkattriter@ --- *
531 * Arguments: @key_attriter *i@ = pointer to attribute iterator
532 * @key *k@ = pointer to key
536 * Use: Initializes an attribute iterator. The attributes are
537 * returned by @key_nextattr@.
540 extern void key_mkattriter(key_attriter */
*i*/
, key */
*k*/
);
542 /* --- @key_nextattr@ --- *
544 * Arguments: @key_attriter *i@ = pointer to attribute iterator
545 * @const char **n, **v@ = pointers to name and value
547 * Returns: Zero if no attribute available, or nonzero if returned OK.
549 * Use: Returns the next attribute.
552 extern int key_nextattr(key_attriter */
*i*/
,
553 const char **/
*n*/
, const char **/
*v*/
);
555 /*----- Searching and iterating -------------------------------------------*/
557 /* --- @key_bytype@ --- *
559 * Arguments: @key_file *f@ = key file we want a key from
560 * @const char *type@ = type string for desired key
562 * Returns: Pointer to the best key to use, or null.
564 * Use: Looks up a key by its type. Returns the key with the latest
565 * expiry time. This function will not return an expired key.
568 extern key
*key_bytype(key_file */
*f*/
, const char */
*type*/
);
570 /* --- @key_byid@ --- *
572 * Arguments: @key_file *f@ = key file to find a key from
573 * @uint32 id@ = id to look for
575 * Returns: Key with matching id.
577 * Use: Returns a key given its id. This function will return an
578 * expired key, but not a deleted one.
581 extern key
*key_byid(key_file */
*f*/
, uint32
/*id*/);
583 /* --- @key_bytag@ --- *
585 * Arguments: @key_file *f@ = key file to find a key from
586 * @const char *tag@ = pointer to tag string
588 * Returns: Key with matching id or tag.
590 * Use: Returns a key given its tag or id. This function will return
591 * an expired key, but not a deleted one.
594 extern key
*key_bytag(key_file */
*f*/
, const char */
*tag*/
);
596 /* --- @key_mkiter@ --- *
598 * Arguments: @key_iter *i@ = pointer to iterator object
599 * @key_file *f@ = pointer to file structure
603 * Use: Initializes a key iterator. The keys are returned by
607 extern void key_mkiter(key_iter */
*i*/
, key_file */
*f*/
);
609 /* --- @key_next@ --- *
611 * Arguments: @key_iter *i@ = pointer to iterator object
613 * Returns: Pointer to next key, or null.
615 * Use: Returns the next key in some arbitrary sequence.
618 extern key
*key_next(key_iter */
*i*/
);
620 /*----- Fetching key data conveniently ------------------------------------*/
622 /* --- @key_fetchinit@ --- *
624 * Arguments: @const key_fetchdef *kf@ = pointer to base definition
625 * @key_packstruct *kps@ = pointer to destination packing def
626 * @void *p@ = pointer to destination block
628 * Returns: Pointer to packing definition.
630 * Use: Initializes a packing definition (@key_packdef@ structure).
631 * If @kps@ is null on entry, an appropriately sized block is
632 * allocated automatically. Otherwise it must be large enough.
635 extern key_packdef
*key_fetchinit(const key_fetchdef */
*kf*/
,
636 key_packstruct */
*kp*/
, void */
*p*/
);
638 /* --- @key_fetch@ --- *
640 * Arguments: @key_packdef *kp@ = pointer to packing structure
641 * @key *k@ = key file containing desired key
643 * Returns: Error code, or zero.
645 * Use: Fetches an unpacked key from a packed one.
648 extern int key_fetch(key_packdef */
*kp*/
, key */
*k*/
);
650 /* --- @key_fetchbyname@ --- *
652 * Arguments: @key_packdef *kp@ = pointer to packing structure
653 * @key_file *kf@ = key file containing desired key
654 * @const char *tag@ = user's tag describing the key
656 * Returns: Error code, or zero.
658 * Use: Fetches a named key from a key file and unpacks it
662 extern int key_fetchbyname(key_packdef */
*kp*/
,
663 key_file */
*kf*/
, const char */
*tag*/
);
665 /* --- @key_fetchdone@ --- *
667 * Arguments: @key_packdef *kp@ = pointer to packing structure
671 * Use: Frees a packing structure. If the structure was allocated by
672 * @key_fetchinit@ then it is freed.
675 extern void key_fetchdone(key_packdef */
*kp*/
);
677 /*----- Other functions ---------------------------------------------------*/
679 /* --- @key_moan@ --- *
681 * Arguments: @const char *file@ = name of the file
682 * @int line@ = line number in file
683 * @const char *msg@ = error message
684 * @void *p@ = argument pointer
688 * Use: Reports an error message about loading a key file.
691 extern void key_moan(const char */
*file*/
, int /*line*/,
692 const char */
*msg*/
, void */
*p*/
);
694 /*----- That's all, folks -------------------------------------------------*/