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@, @key_mergeline@ --- *
182 * Arguments: @key_file *f@ = pointer to file structure
183 * @const char *file@ = name of file (for error messages)
184 * @int lno@ = line number (for error messages, @key_mergeline@)
185 * @FILE *fp@ = file handle to read from (@key_merge@)
186 * @const char *line@ = line from the input (@key_mergeline@)
187 * @key_reporter *rep@ = error reporting function
188 * @void *arg@ = argument for function
190 * Returns: Error code (one of the @KERR@ constants).
192 * Use: The @key_merge@ function reads keys from a file, and inserts
193 * them into the keyring.
195 * The @key_mergeline@ function reads a key from a single input
196 * line (which may, but need not, have a final newline), and
197 * adds it to the keyring.
199 * The @key_mergeline@ function is intended to help with
200 * interfacing Catacomb to runtimes which don't use C's @stdio@
201 * streams, rather than as a general-purpose service, though if
202 * it turns out to be useful in other ways then so much the
206 extern int key_merge(key_file */
*f*/
, const char */
*file*/
, FILE */
*fp*/
,
207 key_reporter */
*rep*/
, void */
*arg*/
);
208 extern int key_mergeline(key_file */
*f*/
, const char */
*file*/
, int /*lno*/,
209 const char */
*line*/
,
210 key_reporter */
*rep*/
, void */
*arg*/
);
212 /* --- @key_extract@, @key_extractline@ --- *
214 * Arguments: @key_file *f@ = pointer to file structure
215 * @key *k@ = key to extract
216 * @FILE *fp@ = file to write on (@key_extract@)
217 * @dstr *d@ = string to write on (@key_extractline@)
218 * @const key_filter *kf@ = pointer to key selection block
220 * Returns: @key_extract@ returns zero if OK, EOF on error.
221 * @key_extractline@ does not return a value.
223 * Use: Extracts a key to an ouptut file or buffer.
225 * The @key_extractline@ includes a final newline in its output.
227 * The @key_extractline@ function is intended to help with
228 * interfacing Catacomb to runtimes which don't use C's @stdio@
229 * streams, rather than as a general-purpose service, though if
230 * it turns out to be useful in other ways then so much the
234 extern int key_extract(key_file */
*f*/
, key */
*k*/
, FILE */
*fp*/
,
235 const key_filter */
*kf*/
);
236 extern void key_extractline(key_file */
*f*/
, key */
*k*/
,
237 dstr */
*d*/
, const key_filter */
*kf*/
);
239 /* --- @key_open@ --- *
241 * Arguments: @key_file *f@ = pointer to file structure to initialize
242 * @const char *file@ = pointer to the file name
243 * @unsigned how@ = opening options (@KOPEN_*@).
244 * @key_reporter *rep@ = error reporting function
245 * @void *arg@ = argument for function
247 * Returns: Zero if it worked, nonzero otherwise.
249 * Use: Opens a key file, reads its contents, and stores them in a
250 * structure. The file is locked appropriately until closed
251 * using @key_close@. On an error, everything is cleared away
252 * tidily. If the file is opened with @KOPEN_WRITE@, it's
253 * created if necessary, with read and write permissions for its
257 extern int key_open(key_file */
*f*/
, const char */
*file*/
, unsigned /*how*/,
258 key_reporter */
*rep*/
, void */
*arg*/
);
260 /* --- @key_discard@ --- *
262 * Arguments: @key_file *f@ = pointer to key file block
266 * Use: Frees all the key data, without writing changes.
269 extern void key_discard(key_file */
*f*/
);
271 /* --- @key_close@ --- *
273 * Arguments: @key_file *f@ = pointer to key file block
275 * Returns: A @KWRITE_@ code indicating how it went.
277 * Use: Frees all the key data, writes any changes. Make sure that
278 * all hell breaks loose if this returns @KWRITE_BROKEN@.
281 extern int key_close(key_file */
*f*/
);
283 /* --- @key_save@ --- *
285 * Arguments: @key_file *f@ = pointer to key file block
287 * Returns: A @KWRITE_@ code indicating how well it worked.
289 * Use: Writes a key file's data back to the actual file. This code
290 * is extremely careful about error handling. It should usually
291 * be able to back out somewhere sensible, but it can tell when
292 * it's got itself into a real pickle and starts leaving well
295 * Callers, please make sure that you ring alarm bells when this
296 * function returns @KWRITE_BROKEN@.
299 extern int key_save(key_file */
*f*/
);
301 /* --- @key_lockfile@ --- *
303 * Arguments: @key_file *f@ = pointer to file structure to initialize
304 * @const char *file@ = pointer to the file name
305 * @unsigned how@ = opening options (@KOPEN_*@).
307 * Returns: Zero if it worked, nonzero otherwise.
309 * Use: Opens a keyfile and stores the information needed for
310 * continued access in the structure.
312 * If the file is opened with @KOPEN_WRITE@, it's created if
313 * necessary with read and write permissions for owner only, and
314 * locked for update while it's open.
316 * This is a system-dependent routine, and only really intended
317 * for the private use of @key_open@.
320 extern int key_lockfile(key_file */
*f*/
, const char */
*file*/
,
323 /*----- Creating and manipulating keys ------------------------------------*/
327 * Arguments: @key_file *f@ = pointer to key file
328 * @uint32 id@ = keyid to set
329 * @const char *type@ = the type of this key
330 * @time_t exp@ = when the key expires
331 * @key *kk@ = where to put the key pointer
333 * Returns: Error code (one of the @KERR@ constants).
335 * Use: Attaches a new key to a key file. You must have a writable
336 * key file for this to work.
338 * The type is a key type string. This interface doesn't care
339 * about how type strings are formatted: it just treats them as
340 * opaque gobs of text. Clients are advised to choose some
341 * standard for representing key types, though.
343 * The expiry time should either be a time in the future, or the
344 * magic value @KEXP_FOREVER@ which means `never expire this
345 * key'. Be careful with `forever' keys. If I were you, I'd
346 * use a more sophisticated key management system than this for
349 * You have to set the actual key yourself.
352 extern int key_new(key_file */
*f*/
, uint32
/*id*/, const char */
*type*/
,
353 time_t /*exp*/, key
**/
*kk*/
);
355 /* --- @key_delete@ --- *
357 * Arguments: @key_file *f@ = pointer to file block
358 * @key *k@ = key to delete
360 * Returns: Error code (one of the @KERR@ constants).
362 * Use: Removes the given key from the list. The key file must be
363 * writable. (Due to the horridness of the data structures,
364 * deleted keys aren't actually removed, just marked so that
365 * they can't be looked up or iterated over. One upshot of
366 * this is that they don't get written back to the file when
370 extern int key_delete(key_file */
*f*/
, key */
*k*/
);
372 /* --- @key_expired@ --- *
374 * Arguments: @key *k@ = pointer to key block
376 * Returns: Zero if the key is OK, nonzero if it's expired.
379 int key_expired(key */
*k*/
);
381 /* --- @key_expire@ --- *
383 * Arguments: @key_file *f@ = pointer to file block
384 * @key *k@ = pointer to key block
386 * Returns: Error code (one of the @KERR@ constants).
388 * Use: Immediately marks the key as expired. It may be removed
389 * immediately, if it is no longer required, and will be removed
390 * by a tidy operation when it is no longer required. The key
391 * file must be writable.
394 extern int key_expire(key_file */
*f*/
, key */
*k*/
);
396 /* --- @key_used@ --- *
398 * Arguments: @key_file *f@ = pointer to key file
399 * @key *k@ = pointer to key block
400 * @time_t t@ = when key can be removed
402 * Returns: Zero if OK, nonzero on failure.
404 * Use: Marks a key as being required until a given time. Even
405 * though the key may expire before then (and won't be returned
406 * by type after that time), it will still be available when
407 * requested explicitly by id. The key file must be writable.
409 * The only (current) reason for failure is attempting to use
410 * a key which can expire for something which can't.
413 extern int key_used(key_file */
*f*/
, key */
*k*/
, time_t /*t*/);
415 /* --- @key_fingerprint@ --- *
417 * Arguments: @key *k@ = the key to fingerprint
418 * @ghash *h@ = the hash to use
419 * @const key_filter *kf@ = filter to apply
421 * Returns: Nonzero if the key slightly matched the filter.
423 * Use: Updates the hash context with the key contents.
426 extern int key_fingerprint(key */
*k*/
, ghash */
*h*/
,
427 const key_filter */
*kf*/
);
429 /*----- Setting and reading attributes ------------------------------------*/
431 /* --- @key_chkident@ --- *
433 * Arguments: @const char *p@ = pointer to a type string
435 * Returns: Zero if OK, -1 on error.
437 * Use: Checks whether an identification component string is OK.
440 extern int key_chkident(const char */
*p*/
);
442 /* --- @key_chkcomment@ --- *
444 * Arguments: @const char *p@ = pointer to a comment string
446 * Returns: Zero if OK, -1 on error.
448 * Use: Checks whether a comment string is OK.
451 extern int key_chkcomment(const char */
*p*/
);
453 /* --- @key_setcomment@ --- *
455 * Arguments: @key_file *f@ = pointer to key file block
456 * @key *k@ = pointer to key block
457 * @const char *c@ = pointer to comment to set, or zero
459 * Returns: Error code (one of the @KERR@ constants).
461 * Use: Replaces the key's current comment with a new one.
464 extern int key_setcomment(key_file */
*f*/
, key */
*k*/
, const char */
*c*/
);
466 /* --- @key_settag@ --- *
468 * Arguments: @key_file *f@ = pointer to key file block
469 * @key *k@ = pointer to key block
470 * @const char *tag@ = pointer to comment to set, or zero
472 * Returns: Error code (one of the @KERR@ constants).
474 * Use: Replaces the key's current tag with a new one.
477 extern int key_settag(key_file */
*f*/
, key */
*k*/
, const char */
*tag*/
);
479 /* --- @key_setkeydata@ --- *
481 * Arguments: @key_file *kf@ = pointer to key file
482 * @key *k@ = pointer to key
483 * @key_data *kd@ = new key data
485 * Returns: Zero on success, or a @KERR_@ error code on failure.
487 * Use: Sets the key data for a key.
490 extern int key_setkeydata(key_file */
*kf*/
, key */
*k*/
, key_data */
*kd*/
);
492 /* --- @key_fulltag@ --- *
494 * Arguments: @key *k@ = pointer to key
495 * @dstr *d@ = pointer to destination string
499 * Use: Emits the key's full tag, which has the form
500 * `ID:TYPE[:TAG]'. This is used in the textual file format,
501 * and to identify passphrases for locked keys.
504 extern void key_fulltag(key */
*k*/
, dstr */
*d*/
);
506 /* --- @key_qtag@ --- *
508 * Arguments: @key_file *f@ = key file to find a key from
509 * @const char *tag@ = pointer to tag string
510 * @dstr *d@ = pointer to string for full tag name
511 * @key **k@ = where to store the key pointer
512 * @key_data ***kd@ = where to store the key data pointer
514 * Returns: Zero if OK, nonzero if it failed.
516 * Use: Performs a full lookup on a qualified tag name. The tag is
517 * qualified by the names of subkeys, separated by dots. Hence,
518 * a qualified tag is ID|TAG[.TAG...]. The various result
519 * pointers can be null to indicate that the result isn't
523 extern int key_qtag(key_file */
*f*/
, const char */
*tag*/
,
524 dstr */
*d*/
, key
**/
*k*/
, key_data
***/
*kd*/
);
526 /* --- @key_getattr@ --- *
528 * Arguments: @key_file *f@ = pointer to file
529 * @key *k@ = pointer to key
530 * @const char *n@ = pointer to attribute name
532 * Returns: Pointer to attribute value, or null if not found.
534 * Use: Returns the value of a key attribute.
537 extern const char *key_getattr(key_file */
*f*/
, key */
*k*/
,
540 /* --- @key_putattr@ --- *
542 * Arguments: @key_file *f@ = pointer to file
543 * @key *k@ = pointer to key
544 * @const char *n@ = pointer to attribute name
545 * @const char *v@ = pointer to attribute value or null
547 * Returns: Error code (one of the @KERR@ constants).
549 * Use: Inserts an attribute on a key. If an attribute with the same
550 * name already exists, it is deleted. Setting a null value
551 * removes the attribute.
554 extern int key_putattr(key_file */
*f*/
, key */
*k*/
,
555 const char */
*n*/
, const char */
*v*/
);
557 /* --- @key_mkattriter@ --- *
559 * Arguments: @key_attriter *i@ = pointer to attribute iterator
560 * @key *k@ = pointer to key
564 * Use: Initializes an attribute iterator. The attributes are
565 * returned by @key_nextattr@.
568 extern void key_mkattriter(key_attriter */
*i*/
, key */
*k*/
);
570 /* --- @key_nextattr@ --- *
572 * Arguments: @key_attriter *i@ = pointer to attribute iterator
573 * @const char **n, **v@ = pointers to name and value
575 * Returns: Zero if no attribute available, or nonzero if returned OK.
577 * Use: Returns the next attribute.
580 extern int key_nextattr(key_attriter */
*i*/
,
581 const char **/
*n*/
, const char **/
*v*/
);
583 /*----- Searching and iterating -------------------------------------------*/
585 /* --- @key_bytype@ --- *
587 * Arguments: @key_file *f@ = key file we want a key from
588 * @const char *type@ = type string for desired key
590 * Returns: Pointer to the best key to use, or null.
592 * Use: Looks up a key by its type. Returns the key with the latest
593 * expiry time. This function will not return an expired key.
596 extern key
*key_bytype(key_file */
*f*/
, const char */
*type*/
);
598 /* --- @key_byid@ --- *
600 * Arguments: @key_file *f@ = key file to find a key from
601 * @uint32 id@ = id to look for
603 * Returns: Key with matching id.
605 * Use: Returns a key given its id. This function will return an
606 * expired key, but not a deleted one.
609 extern key
*key_byid(key_file */
*f*/
, uint32
/*id*/);
611 /* --- @key_bytag@ --- *
613 * Arguments: @key_file *f@ = key file to find a key from
614 * @const char *tag@ = pointer to tag string
616 * Returns: Key with matching id or tag.
618 * Use: Returns a key given its tag or id. This function will return
619 * an expired key, but not a deleted one.
622 extern key
*key_bytag(key_file */
*f*/
, const char */
*tag*/
);
624 /* --- @key_mkiter@ --- *
626 * Arguments: @key_iter *i@ = pointer to iterator object
627 * @key_file *f@ = pointer to file structure
631 * Use: Initializes a key iterator. The keys are returned by
635 extern void key_mkiter(key_iter */
*i*/
, key_file */
*f*/
);
637 /* --- @key_next@ --- *
639 * Arguments: @key_iter *i@ = pointer to iterator object
641 * Returns: Pointer to next key, or null.
643 * Use: Returns the next key in some arbitrary sequence.
646 extern key
*key_next(key_iter */
*i*/
);
648 /*----- Fetching key data conveniently ------------------------------------*/
650 /* --- @key_fetchinit@ --- *
652 * Arguments: @const key_fetchdef *kf@ = pointer to base definition
653 * @key_packstruct *kps@ = pointer to destination packing def
654 * @void *p@ = pointer to destination block
656 * Returns: Pointer to packing definition.
658 * Use: Initializes a packing definition (@key_packdef@ structure).
659 * If @kps@ is null on entry, an appropriately sized block is
660 * allocated automatically. Otherwise it must be large enough.
663 extern key_packdef
*key_fetchinit(const key_fetchdef */
*kf*/
,
664 key_packstruct */
*kp*/
, void */
*p*/
);
666 /* --- @key_fetch@ --- *
668 * Arguments: @key_packdef *kp@ = pointer to packing structure
669 * @key *k@ = key file containing desired key
671 * Returns: Error code, or zero.
673 * Use: Fetches an unpacked key from a packed one.
676 extern int key_fetch(key_packdef */
*kp*/
, key */
*k*/
);
678 /* --- @key_fetchbyname@ --- *
680 * Arguments: @key_packdef *kp@ = pointer to packing structure
681 * @key_file *kf@ = key file containing desired key
682 * @const char *tag@ = user's tag describing the key
684 * Returns: Error code, or zero.
686 * Use: Fetches a named key from a key file and unpacks it
690 extern int key_fetchbyname(key_packdef */
*kp*/
,
691 key_file */
*kf*/
, const char */
*tag*/
);
693 /* --- @key_fetchdone@ --- *
695 * Arguments: @key_packdef *kp@ = pointer to packing structure
699 * Use: Frees a packing structure. If the structure was allocated by
700 * @key_fetchinit@ then it is freed.
703 extern void key_fetchdone(key_packdef */
*kp*/
);
705 /*----- Other functions ---------------------------------------------------*/
707 /* --- @key_moan@ --- *
709 * Arguments: @const char *file@ = name of the file
710 * @int line@ = line number in file
711 * @const char *msg@ = error message
712 * @void *p@ = argument pointer
716 * Use: Reports an error message about loading a key file.
719 extern void key_moan(const char */
*file*/
, int /*line*/,
720 const char */
*msg*/
, void */
*p*/
);
722 /*----- That's all, folks -------------------------------------------------*/