5 * Simple key management
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 #ifndef CATACOMB_KEY_H
31 #define CATACOMB_KEY_H
37 /*----- Header files ------------------------------------------------------*/
42 #include <mLib/bits.h>
43 #include <mLib/dstr.h>
44 #include <mLib/hash.h>
47 #ifndef CATACOMB_KEY_DATA_H
48 # include "key-data.h"
51 #ifndef CATACOMB_GHASH_H
59 /*----- Data structures ---------------------------------------------------*/
61 /* --- Key attributes --- *
63 * Each attribute is stored as a symbol in a symbol table. The value is
64 * the plain (not url-encoded) text to be written to the the file. If the
65 * value is binary data, then by this point it's base-64 encoded.
68 typedef struct key_attr
{
69 sym_base _b
; /* Symbol table data */
70 char *p
; /* Pointer to attribute value */
73 /* --- Main key structure --- *
75 * Each key is stored in two symbol tables, one indexed by keyid, and the
76 * other indexed by type. Because many keys can have the same type, the type
77 * table contains a list of keys, sorted in descending order of expiry.
82 /* --- Hashtable management --- */
84 hash_base _b
; /* Symbol table data */
85 struct key
*next
; /* Next key of the same type */
87 /* --- Basic key attributes --- */
89 uint32 id
; /* Key id used to name it */
90 char *tag
; /* Textual tag name */
91 char *type
; /* Textual key type */
92 time_t exp
, del
; /* Expiry times for keys */
94 /* --- The key data itself --- */
96 key_data k
; /* The actual key data */
98 /* --- Other attributes and commentary --- */
100 sym_table a
; /* Hashtable of key attributes */
101 char *c
; /* Any additional comments */
104 /* --- The keys-by-type entries --- */
106 typedef struct key_ref
{
107 sym_base _b
; /* Symbol table data */
108 key
*k
; /* Pointer to first key in list */
111 /* --- A key file --- */
113 typedef struct key_file
{
114 FILE *fp
; /* File pointer open on file */
115 char *name
; /* Filename used to create it */
116 unsigned f
; /* Various useful flags */
117 hash_table byid
; /* Table of keys by keyid */
118 sym_table bytype
; /* Table of keys by type */
119 sym_table bytag
; /* Table of keys by tag */
120 size_t idload
; /* Loading on id table */
123 /* --- Key file flags --- */
125 #define KF_WRITE 1u /* File opened for writing */
126 #define KF_MODIFIED 2u /* File has been modified */
128 /* --- Iterating over keys --- *
130 * Both of these are simple symbol table iterators, but they're made distinct
131 * types for the dubious benefits that type safety brings.
134 typedef struct { hash_iter i
; time_t t
; } key_iter
;
135 typedef struct { sym_iter i
; } key_attriter
;
137 /* --- Key fetching --- */
139 typedef struct key_fetchdef
{
140 char *name
; /* Name of item */
141 size_t off
; /* Offset into target structure */
142 unsigned e
; /* Flags for the item */
143 const struct key_fetchdef
*kf
; /* Substructure pointer */
146 /* --- File opening options --- */
148 #define KOPEN_READ 0u
149 #define KOPEN_WRITE 1u
150 #define KOPEN_MASK 0xff
151 #define KOPEN_NOFILE 0x100
153 /* --- Various other magic numbers --- */
155 #define KEXP_FOREVER ((time_t)-1) /* Never expire this key */
156 #define KEXP_EXPIRE ((time_t)-2) /* Expire this key when unused */
158 /* --- Key error codes --- */
161 KERR_OK
= 0, /* No error */
162 KERR_BADTAG
= -1, /* Malformed tag string */
163 KERR_BADTYPE
= -2, /* Malformed type string */
164 KERR_BADCOMMENT
= -3, /* Malformed comment string */
165 KERR_DUPID
= -4, /* Duplicate keyid */
166 KERR_DUPTAG
= -5, /* Duplicate key tag string */
167 KERR_READONLY
= -6, /* Key file is read-only */
168 KERR_WILLEXPIRE
= -7, /* Key will eventually expire */
169 KERR_EXPIRED
= -8, /* Key has already expired */
170 KERR_BADFLAGS
= -9, /* Error in flags string */
171 KERR_BADPASS
= -10, /* Error decrypting locked key */
172 KERR_WRONGTYPE
= -11, /* Key has incorrect type */
173 KERR_NOTFOUND
= -12, /* Key couldn't be found */
174 KERR_BADATTR
= -13, /* Malformed attribute name */
175 KERR_MAX
/* Largest possible error */
178 /* --- Write error codes --- */
181 KWRITE_OK
, /* Everything went fine */
182 KWRITE_FAIL
= -1, /* Close attempt failed */
183 KWRITE_BROKEN
= -2 /* Key ring needs manual fixing */
186 /* --- Error reporting functions for @key_merge@ and @key_open@ --- */
188 typedef void key_reporter(const char */
*file*/
, int /*line*/,
189 const char */
*err*/
, void */
*p*/
);
191 /* --- Macros for testing expiry --- */
193 #define KEY_EXPIRED(now, exp) \
194 ((exp) == KEXP_EXPIRE || ((exp) != KEXP_FOREVER && (exp) < (now)))
196 /*----- Reading and writing keys and files --------------------------------*/
198 /* --- @key_merge@ --- *
200 * Arguments: @key_file *f@ = pointer to file structure
201 * @const char *file@ = name of file (for error messages)
202 * @FILE *fp@ = file handle to read from
203 * @key_reporter *rep@ = error reporting function
204 * @void *arg@ = argument for function
206 * Returns: Error code (one of the @KERR@ constants).
208 * Use: Reads keys from a file, and inserts them into the file.
211 extern int key_merge(key_file */
*f*/
, const char */
*file*/
, FILE */
*fp*/
,
212 key_reporter */
*rep*/
, void */
*arg*/
);
214 /* --- @key_extract@ --- *
216 * Arguments: @key_file *f@ = pointer to file structure
217 * @key *k@ = key to extract
218 * @FILE *fp@ = file to write on
219 * @const key_filter *kf@ = pointer to key selection block
221 * Returns: Zero if OK, EOF on error.
223 * Use: Extracts a key to an ouptut file.
226 extern int key_extract(key_file */
*f*/
, key */
*k*/
, FILE */
*fp*/
,
227 const key_filter */
*kf*/
);
229 /* --- @key_open@ --- *
231 * Arguments: @key_file *f@ = pointer to file structure to initialize
232 * @const char *file@ = pointer to the file name
233 * @unsigned how@ = opening options (@KOPEN_*@).
234 * @key_reporter *rep@ = error reporting function
235 * @void *arg@ = argument for function
237 * Returns: Zero if it worked, nonzero otherwise.
239 * Use: Opens a key file, reads its contents, and stores them in a
240 * structure. The file is locked appropriately until closed
241 * using @key_close@. On an error, everything is cleared away
242 * tidily. If the file is opened with @KOPEN_WRITE@, it's
243 * created if necessary, with read and write permissions for its
247 extern int key_open(key_file */
*f*/
, const char */
*file*/
, unsigned /*how*/,
248 key_reporter */
*rep*/
, void */
*arg*/
);
250 /* --- @key_discard@ --- *
252 * Arguments: @key_file *f@ = pointer to key file block
256 * Use: Frees all the key data, without writing changes.
259 extern int key_discard(key_file */
*f*/
);
261 /* --- @key_close@ --- *
263 * Arguments: @key_file *f@ = pointer to key file block
265 * Returns: A @KWRITE_@ code indicating how it went.
267 * Use: Frees all the key data, writes any changes. Make sure that
268 * all hell breaks loose if this returns @KWRITE_BROKEN@.
271 extern int key_close(key_file */
*f*/
);
273 /* --- @key_save@ --- *
275 * Arguments: @key_file *f@ = pointer to key file block
277 * Returns: A @KWRITE_@ code indicating how well it worked.
279 * Use: Writes a key file's data back to the actual file. This code
280 * is extremely careful about error handling. It should usually
281 * be able to back out somewhere sensible, but it can tell when
282 * it's got itself into a real pickle and starts leaving well
285 * Callers, please make sure that you ring alarm bells when this
286 * function returns @KWRITE_BROKEN@.
289 extern int key_save(key_file */
*f*/
);
291 /* --- @key_lockfile@ --- *
293 * Arguments: @key_file *f@ = pointer to file structure to initialize
294 * @const char *file@ = pointer to the file name
295 * @unsigned how@ = opening options (@KOPEN_*@).
297 * Returns: Zero if it worked, nonzero otherwise.
299 * Use: Opens a keyfile and stores the information needed for
300 * continued access in the structure.
302 * If the file is opened with @KOPEN_WRITE@, it's created if
303 * necessary with read and write permissions for owner only, and
304 * locked for update while it's open.
306 * This is a system-dependent routine, and only really intended
307 * for the private use of @key_open@.
310 extern int key_lockfile(key_file */
*f*/
, const char */
*file*/
,
313 /*----- Creating and manipulating keys ------------------------------------*/
317 * Arguments: @key_file *f@ = pointer to key file
318 * @uint32 id@ = keyid to set
319 * @const char *type@ = the type of this key
320 * @time_t exp@ = when the key expires
321 * @int *err@ = where to store the error condition
323 * Returns: Key block containing new data, or null if it couldn't be
326 * Use: Attaches a new key to a key file. You must have a writable
327 * key file for this to work.
329 * The type is a key type string. This interface doesn't care
330 * about how type strings are formatted: it just treats them as
331 * opaque gobs of text. Clients are advised to choose some
332 * standard for representing key types, though.
334 * The expiry time should either be a time in the future, or the
335 * magic value @KEXP_FOREVER@ which means `never expire this
336 * key'. Be careful with `forever' keys. If I were you, I'd
337 * use a more sophisticated key management system than this for
340 * You have to set the actual key yourself.
343 extern key
*key_new(key_file */
*f*/
, uint32
/*id*/, const char */
*type*/
,
344 time_t /*exp*/, int */
*err*/
);
346 /* --- @key_delete@ --- *
348 * Arguments: @key_file *f@ = pointer to file block
349 * @key *k@ = key to delete
351 * Returns: Error code (one of the @KERR@ constants).
353 * Use: Removes the given key from the list. The key file must be
354 * writable. (Due to the horridness of the data structures,
355 * deleted keys aren't actually removed, just marked so that
356 * they can't be looked up or iterated over. One upshot of
357 * this is that they don't get written back to the file when
361 extern int key_delete(key_file */
*f*/
, key */
*k*/
);
363 /* --- @key_expired@ --- *
365 * Arguments: @key *k@ = pointer to key block
367 * Returns: Zero if the key is OK, nonzero if it's expired.
370 int key_expired(key */
*k*/
);
372 /* --- @key_expire@ --- *
374 * Arguments: @key_file *f@ = pointer to file block
375 * @key *k@ = pointer to key block
377 * Returns: Error code (one of the @KERR@ constants).
379 * Use: Immediately marks the key as expired. It may be removed
380 * immediately, if it is no longer required, and will be removed
381 * by a tidy operation when it is no longer required. The key
382 * file must be writable.
385 extern int key_expire(key_file */
*f*/
, key */
*k*/
);
387 /* --- @key_used@ --- *
389 * Arguments: @key_file *f@ = pointer to key file
390 * @key *k@ = pointer to key block
391 * @time_t t@ = when key can be removed
393 * Returns: Zero if OK, nonzero on failure.
395 * Use: Marks a key as being required until a given time. Even
396 * though the key may expire before then (and won't be returned
397 * by type after that time), it will still be available when
398 * requested explicitly by id. The key file must be writable.
400 * The only (current) reason for failure is attempting to use
401 * a key which can expire for something which can't.
404 extern int key_used(key_file */
*f*/
, key */
*k*/
, time_t /*t*/);
406 /* --- @key_fingerprint@ --- *
408 * Arguments: @key *k@ = the key to fingerprint
409 * @ghash *h@ = the hash to use
410 * @const key_filter *kf@ = filter to apply
412 * Returns: Nonzero if the key slightly matched the filter.
414 * Use: Updates the hash context with the key contents.
417 extern int key_fingerprint(key */
*k*/
, ghash */
*h*/
,
418 const key_filter */
*kf*/
);
420 /*----- Setting and reading attributes ------------------------------------*/
422 /* --- @key_chkident@ --- *
424 * Arguments: @const char *p@ = pointer to a type string
426 * Returns: Zero if OK, -1 on error.
428 * Use: Checks whether an identification component string is OK.
431 extern int key_chkident(const char */
*p*/
);
433 /* --- @key_chkcomment@ --- *
435 * Arguments: @const char *p@ = pointer to a comment string
437 * Returns: Zero if OK, -1 on error.
439 * Use: Checks whether a comment string is OK.
442 extern int key_chkcomment(const char */
*p*/
);
444 /* --- @key_setcomment@ --- *
446 * Arguments: @key_file *f@ = pointer to key file block
447 * @key *k@ = pointer to key block
448 * @const char *c@ = pointer to comment to set, or zero
450 * Returns: Error code (one of the @KERR@ constants).
452 * Use: Replaces the key's current comment with a new one.
455 extern int key_setcomment(key_file */
*f*/
, key */
*k*/
, const char */
*c*/
);
457 /* --- @key_settag@ --- *
459 * Arguments: @key_file *f@ = pointer to key file block
460 * @key *k@ = pointer to key block
461 * @const char *tag@ = pointer to comment to set, or zero
463 * Returns: Error code (one of the @KERR@ constants).
465 * Use: Replaces the key's current tag with a new one.
468 extern int key_settag(key_file */
*f*/
, key */
*k*/
, const char */
*tag*/
);
470 /* --- @key_fulltag@ --- *
472 * Arguments: @key *k@ = pointer to key
473 * @dstr *d@ = pointer to destination string
477 * Use: Emits the key's full tag, which has the form
478 * `ID:TYPE[:TAG]'. This is used in the textual file format,
479 * and to identify passphrases for locked keys.
482 extern void key_fulltag(key */
*k*/
, dstr */
*d*/
);
484 /* --- @key_qtag@ --- *
486 * Arguments: @key_file *f@ = key file to find a key from
487 * @const char *tag@ = pointer to tag string
488 * @dstr *d@ = pointer to string for full tag name
489 * @key **k@ = where to store the key pointer
490 * @key_data **kd@ = where to store the key data pointer
492 * Returns: Zero if OK, nonzero if it failed.
494 * Use: Performs a full lookup on a qualified tag name. The tag is
495 * qualified by the names of subkeys, separated by dots. Hence,
496 * a qualified tag is ID|TAG[.TAG...]. The various result
497 * pointers can be null to indicate that the result isn't
501 extern int key_qtag(key_file */
*f*/
, const char */
*tag*/
,
502 dstr */
*d*/
, key
**/
*k*/
, key_data
**/
*kd*/
);
504 /* --- @key_getattr@ --- *
506 * Arguments: @key_file *f@ = pointer to file
507 * @key *k@ = pointer to key
508 * @const char *n@ = pointer to attribute name
510 * Returns: Pointer to attribute value, or null if not found.
512 * Use: Returns the value of a key attribute.
515 extern const char *key_getattr(key_file */
*f*/
, key */
*k*/
,
518 /* --- @key_putattr@ --- *
520 * Arguments: @key_file *f@ = pointer to file
521 * @key *k@ = pointer to key
522 * @const char *n@ = pointer to attribute name
523 * @const char *v@ = pointer to attribute value or null
525 * Returns: Error code (one of the @KERR@ constants).
527 * Use: Inserts an attribute on a key. If an attribute with the same
528 * name already exists, it is deleted. Setting a null value
529 * removes the attribute.
532 extern int key_putattr(key_file */
*f*/
, key */
*k*/
,
533 const char */
*n*/
, const char */
*v*/
);
535 /* --- @key_mkattriter@ --- *
537 * Arguments: @key_attriter *i@ = pointer to attribute iterator
538 * @key *k@ = pointer to key
542 * Use: Initializes an attribute iterator. The attributes are
543 * returned by @key_nextattr@.
546 extern void key_mkattriter(key_attriter */
*i*/
, key */
*k*/
);
548 /* --- @key_nextattr@ --- *
550 * Arguments: @key_attriter *i@ = pointer to attribute iterator
551 * @const char **n, **v@ = pointers to name and value
553 * Returns: Zero if no attribute available, or nonzero if returned OK.
555 * Use: Returns the next attribute.
558 extern int key_nextattr(key_attriter */
*i*/
,
559 const char **/
*n*/
, const char **/
*v*/
);
561 /*----- Searching and iterating -------------------------------------------*/
563 /* --- @key_bytype@ --- *
565 * Arguments: @key_file *f@ = key file we want a key from
566 * @const char *type@ = type string for desired key
568 * Returns: Pointer to the best key to use, or null.
570 * Use: Looks up a key by its type. Returns the key with the latest
571 * expiry time. This function will not return an expired key.
574 extern key
*key_bytype(key_file */
*f*/
, const char */
*type*/
);
576 /* --- @key_byid@ --- *
578 * Arguments: @key_file *f@ = key file to find a key from
579 * @uint32 id@ = id to look for
581 * Returns: Key with matching id.
583 * Use: Returns a key given its id. This function will return an
584 * expired key, but not a deleted one.
587 extern key
*key_byid(key_file */
*f*/
, uint32
/*id*/);
589 /* --- @key_bytag@ --- *
591 * Arguments: @key_file *f@ = key file to find a key from
592 * @const char *tag@ = pointer to tag string
594 * Returns: Key with matching id or tag.
596 * Use: Returns a key given its tag or id. This function will return
597 * an expired key, but not a deleted one.
600 extern key
*key_bytag(key_file */
*f*/
, const char */
*tag*/
);
602 /* --- @key_mkiter@ --- *
604 * Arguments: @key_iter *i@ = pointer to iterator object
605 * @key_file *f@ = pointer to file structure
609 * Use: Initializes a key iterator. The keys are returned by
613 extern void key_mkiter(key_iter */
*i*/
, key_file */
*f*/
);
615 /* --- @key_next@ --- *
617 * Arguments: @key_iter *i@ = pointer to iterator object
619 * Returns: Pointer to next key, or null.
621 * Use: Returns the next key in some arbitrary sequence.
624 extern key
*key_next(key_iter */
*i*/
);
626 /*----- Fetching key data conveniently ------------------------------------*/
628 /* --- @key_fetchinit@ --- *
630 * Arguments: @const key_fetchdef *kf@ = pointer to base definition
631 * @key_packstruct *kps@ = pointer to destination packing def
632 * @void *p@ = pointer to destination block
634 * Returns: Pointer to packing definition.
636 * Use: Initializes a packing definition (@key_packdef@ structure).
637 * If @kps@ is null on entry, an appropriately sized block is
638 * allocated automatically. Otherwise it must be large enough.
641 extern key_packdef
*key_fetchinit(const key_fetchdef */
*kf*/
,
642 key_packstruct */
*kp*/
, void */
*p*/
);
644 /* --- @key_fetch@ --- *
646 * Arguments: @key_packdef *kp@ = pointer to packing structure
647 * @key *k@ = key file containing desired key
649 * Returns: Error code, or zero.
651 * Use: Fetches an unpacked key from a packed one.
654 extern int key_fetch(key_packdef */
*kp*/
, key */
*k*/
);
656 /* --- @key_fetchbyname@ --- *
658 * Arguments: @key_packdef *kp@ = pointer to packing structure
659 * @key_file *kf@ = key file containing desired key
660 * @const char *tag@ = user's tag describing the key
662 * Returns: Error code, or zero.
664 * Use: Fetches a named key from a key file and unpacks it
668 extern int key_fetchbyname(key_packdef */
*kp*/
,
669 key_file */
*kf*/
, const char */
*tag*/
);
671 /* --- @key_fetchdone@ --- *
673 * Arguments: @key_packdef *kp@ = pointer to packing structure
677 * Use: Frees a packing structure. If the structure was allocated by
678 * @key_fetchinit@ then it is freed.
681 extern void key_fetchdone(key_packdef */
*kp*/
);
683 /*----- Other functions ---------------------------------------------------*/
685 /* --- @key_moan@ --- *
687 * Arguments: @const char *file@ = name of the file
688 * @int line@ = line number in file
689 * @const char *msg@ = error message
690 * @void *p@ = argument pointer
694 * Use: Reports an error message about loading a key file.
697 extern void key_moan(const char */
*file*/
, int /*line*/,
698 const char */
*msg*/
, void */
*p*/
);
700 /* --- @key_strerror@ --- *
702 * Arguments: @int err@ = error code from @key_new@
704 * Returns: Pointer to error string.
706 * Use: Translates a @KERR@ error code into a human-readable string.
709 extern const char *key_strerror(int /*err*/);
711 /*----- That's all, folks -------------------------------------------------*/