Update manuals.
[u/mdw/catacomb] / key.h
1 /* -*-c-*-
2 *
3 * $Id: key.h,v 1.9 2001/06/22 19:37:59 mdw Exp $
4 *
5 * Simple key management
6 *
7 * (c) 1999 Straylight/Edgeware
8 */
9
10 /*----- Licensing notice --------------------------------------------------*
11 *
12 * This file is part of Catacomb.
13 *
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.
18 *
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.
23 *
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,
27 * MA 02111-1307, USA.
28 */
29
30 /*----- Revision history --------------------------------------------------*
31 *
32 * $Log: key.h,v $
33 * Revision 1.9 2001/06/22 19:37:59 mdw
34 * New interface to enquire whether a key has expired.
35 *
36 * Revision 1.8 2001/02/03 11:57:38 mdw
37 * Allow creating keyfiles with no file attached.
38 *
39 * Revision 1.7 2000/12/06 20:33:27 mdw
40 * Make flags be macros rather than enumerations, to ensure that they're
41 * unsigned.
42 *
43 * Revision 1.6 2000/06/17 11:27:43 mdw
44 * Add key fetching interface.
45 *
46 * Revision 1.5 2000/02/12 18:55:40 mdw
47 * Make it all compile properly.
48 *
49 * Revision 1.4 2000/02/12 18:21:02 mdw
50 * Overhaul of key management (again).
51 *
52 * Revision 1.3 1999/12/22 15:47:48 mdw
53 * Major key-management revision.
54 *
55 * Revision 1.2 1999/12/10 23:29:48 mdw
56 * Change header file guard names.
57 *
58 * Revision 1.1 1999/09/03 08:41:12 mdw
59 * Initial import.
60 *
61 */
62
63 #ifndef CATACOMB_KEY_H
64 #define CATACOMB_KEY_H
65
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69
70 /*----- Header files ------------------------------------------------------*/
71
72 #include <stdio.h>
73 #include <time.h>
74
75 #include <mLib/bits.h>
76 #include <mLib/dstr.h>
77 #include <mLib/hash.h>
78 #include <mLib/sym.h>
79
80 #ifndef CATACOMB_KEY_DATA_H
81 # include "key-data.h"
82 #endif
83
84 #ifndef CATACOMB_MP_H
85 # include "mp.h"
86 #endif
87
88 /*----- Data structures ---------------------------------------------------*/
89
90 /* --- Key attributes --- *
91 *
92 * Each attribute is stored as a symbol in a symbol table. The value is
93 * the plain (not url-encoded) text to be written to the the file. If the
94 * value is binary data, then by this point it's base-64 encoded.
95 */
96
97 typedef struct key_attr {
98 sym_base _b; /* Symbol table data */
99 char *p; /* Pointer to attribute value */
100 } key_attr;
101
102 /* --- Main key structure --- *
103 *
104 * Each key is stored in two symbol tables, one indexed by keyid, and the
105 * other indexed by type. Because many keys can have the same type, the type
106 * table contains a list of keys, sorted in descending order of expiry.
107 */
108
109 typedef struct key {
110
111 /* --- Hashtable management --- */
112
113 hash_base _b; /* Symbol table data */
114 struct key *next; /* Next key of the same type */
115
116 /* --- Basic key attributes --- */
117
118 uint32 id; /* Key id used to name it */
119 char *tag; /* Textual tag name */
120 char *type; /* Textual key type */
121 time_t exp, del; /* Expiry times for keys */
122
123 /* --- The key data itself --- */
124
125 key_data k; /* The actual key data */
126
127 /* --- Other attributes and commentary --- */
128
129 sym_table a; /* Hashtable of key attributes */
130 char *c; /* Any additional comments */
131 } key;
132
133 /* --- The keys-by-type entries --- */
134
135 typedef struct key_ref {
136 sym_base _b; /* Symbol table data */
137 key *k; /* Pointer to first key in list */
138 } key_ref;
139
140 /* --- A key file --- */
141
142 typedef struct key_file {
143 FILE *fp; /* File pointer open on file */
144 char *name; /* Filename used to create it */
145 unsigned f; /* Various useful flags */
146 hash_table byid; /* Table of keys by keyid */
147 sym_table bytype; /* Table of keys by type */
148 sym_table bytag; /* Table of keys by tag */
149 size_t idload; /* Loading on id table */
150 } key_file;
151
152 /* --- Key file flags --- */
153
154 #define KF_WRITE 1u /* File opened for writing */
155 #define KF_MODIFIED 2u /* File has been modified */
156
157 /* --- Iterating over keys --- *
158 *
159 * Both of these are simple symbol table iterators, but they're made distinct
160 * types for the dubious benefits that type safety brings.
161 */
162
163 typedef struct { hash_iter i; time_t t; } key_iter;
164 typedef struct { sym_iter i; } key_attriter;
165
166 /* --- Key fetching --- */
167
168 typedef struct key_fetchdef {
169 char *name; /* Name of item */
170 size_t off; /* Offset into target structure */
171 unsigned e; /* Flags for the item */
172 const struct key_fetchdef *kf; /* Substructure pointer */
173 } key_fetchdef;
174
175 /* --- File opening options --- */
176
177 #define KOPEN_READ 0u
178 #define KOPEN_WRITE 1u
179 #define KOPEN_MASK 0xff
180 #define KOPEN_NOFILE 0x100
181
182 /* --- Various other magic numbers --- */
183
184 #define KEXP_FOREVER ((time_t)-1) /* Never expire this key */
185 #define KEXP_EXPIRE ((time_t)-2) /* Expire this key when unused */
186
187 /* --- Key error codes --- */
188
189 enum {
190 KERR_OK = 0, /* No error */
191 KERR_BADTAG = -1, /* Malformed tag string */
192 KERR_BADTYPE = -2, /* Malformed type string */
193 KERR_BADCOMMENT = -3, /* Malformed comment string */
194 KERR_DUPID = -4, /* Duplicate keyid */
195 KERR_DUPTAG = -5, /* Duplicate key tag string */
196 KERR_READONLY = -6, /* Key file is read-only */
197 KERR_WILLEXPIRE = -7, /* Key will eventually expire */
198 KERR_EXPIRED = -8, /* Key has already expired */
199 KERR_BADFLAGS = -9, /* Error in flags string */
200 KERR_BADPASS = -10, /* Error decrypting locked key */
201 KERR_WRONGTYPE = -11, /* Key has incorrect type */
202 KERR_NOTFOUND = -12, /* Key couldn't be found */
203 KERR_MAX /* Largest possible error */
204 };
205
206 /* --- Write error codes --- */
207
208 enum {
209 KWRITE_OK, /* Everything went fine */
210 KWRITE_FAIL = -1, /* Close attempt failed */
211 KWRITE_BROKEN = -2 /* Key ring needs manual fixing */
212 };
213
214 /* --- Error reporting functions for @key_merge@ and @key_open@ --- */
215
216 typedef void key_reporter(const char */*file*/, int /*line*/,
217 const char */*err*/, void */*p*/);
218
219 /* --- Macros for testing expiry --- */
220
221 #define KEY_EXPIRED(now, exp) \
222 ((exp) == KEXP_EXPIRE || ((exp) != KEXP_FOREVER && (exp) < (now)))
223
224 /*----- Reading and writing keys and files --------------------------------*/
225
226 /* --- @key_merge@ --- *
227 *
228 * Arguments: @key_file *f@ = pointer to file structure
229 * @const char *file@ = name of file (for error messages)
230 * @FILE *fp@ = file handle to read from
231 * @key_reporter *rep@ = error reporting function
232 * @void *arg@ = argument for function
233 *
234 * Returns: Error code (one of the @KERR@ constants).
235 *
236 * Use: Reads keys from a file, and inserts them into the file.
237 */
238
239 extern int key_merge(key_file */*f*/, const char */*file*/, FILE */*fp*/,
240 key_reporter */*rep*/, void */*arg*/);
241
242 /* --- @key_extract@ --- *
243 *
244 * Arguments: @key_file *f@ = pointer to file structure
245 * @key *k@ = key to extract
246 * @FILE *fp@ = file to write on
247 * @const key_filter *kf@ = pointer to key selection block
248 *
249 * Returns: Zero if OK, EOF on error.
250 *
251 * Use: Extracts a key to an ouptut file.
252 */
253
254 extern int key_extract(key_file */*f*/, key */*k*/, FILE */*fp*/,
255 const key_filter */*kf*/);
256
257 /* --- @key_open@ --- *
258 *
259 * Arguments: @key_file *f@ = pointer to file structure to initialize
260 * @const char *file@ = pointer to the file name
261 * @unsigned how@ = opening options (@KOPEN_*@).
262 * @key_reporter *rep@ = error reporting function
263 * @void *arg@ = argument for function
264 *
265 * Returns: Zero if it worked, nonzero otherwise.
266 *
267 * Use: Opens a key file, reads its contents, and stores them in a
268 * structure. The file is locked appropriately until closed
269 * using @key_close@. On an error, everything is cleared away
270 * tidily. If the file is opened with @KOPEN_WRITE@, it's
271 * created if necessary, with read and write permissions for its
272 * owner only.
273 */
274
275 extern int key_open(key_file */*f*/, const char */*file*/, unsigned /*how*/,
276 key_reporter */*rep*/, void */*arg*/);
277
278 /* --- @key_close@ --- *
279 *
280 * Arguments: @key_file *f@ = pointer to key file block
281 *
282 * Returns: A @KWRITE_@ code indicating how it went.
283 *
284 * Use: Frees all the key data, writes any changes. Make sure that
285 * all hell breaks loose if this returns @KWRITE_BROKEN@.
286 */
287
288 extern int key_close(key_file */*f*/);
289
290 /* --- @key_save@ --- *
291 *
292 * Arguments: @key_file *f@ = pointer to key file block
293 *
294 * Returns: A @KWRITE_@ code indicating how well it worked.
295 *
296 * Use: Writes a key file's data back to the actual file. This code
297 * is extremely careful about error handling. It should usually
298 * be able to back out somewhere sensible, but it can tell when
299 * it's got itself into a real pickle and starts leaving well
300 * alone.
301 *
302 * Callers, please make sure that you ring alarm bells when this
303 * function returns @KWRITE_BROKEN@.
304 */
305
306 extern int key_save(key_file */*f*/);
307
308 /* --- @key_lockfile@ --- *
309 *
310 * Arguments: @key_file *f@ = pointer to file structure to initialize
311 * @const char *file@ = pointer to the file name
312 * @unsigned how@ = opening options (@KOPEN_*@).
313 *
314 * Returns: Zero if it worked, nonzero otherwise.
315 *
316 * Use: Opens a keyfile and stores the information needed for
317 * continued access in the structure.
318 *
319 * If the file is opened with @KOPEN_WRITE@, it's created if
320 * necessary with read and write permissions for owner only, and
321 * locked for update while it's open.
322 *
323 * This is a system-dependent routine, and only really intended
324 * for the private use of @key_open@.
325 */
326
327 extern int key_lockfile(key_file */*f*/, const char */*file*/,
328 unsigned /*how*/);
329
330 /*----- Creating and manipulating keys ------------------------------------*/
331
332 /* --- @key_new@ ---
333 *
334 * Arguments: @key_file *f@ = pointer to key file
335 * @uint32 id@ = keyid to set
336 * @const char *type@ = the type of this key
337 * @time_t exp@ = when the key expires
338 * @int *err@ = where to store the error condition
339 *
340 * Returns: Key block containing new data, or null if it couldn't be
341 * done.
342 *
343 * Use: Attaches a new key to a key file. You must have a writable
344 * key file for this to work.
345 *
346 * The type is a key type string. This interface doesn't care
347 * about how type strings are formatted: it just treats them as
348 * opaque gobs of text. Clients are advised to choose some
349 * standard for representing key types, though.
350 *
351 * The expiry time should either be a time in the future, or the
352 * magic value @KEXP_FOREVER@ which means `never expire this
353 * key'. Be careful with `forever' keys. If I were you, I'd
354 * use a more sophisticated key management system than this for
355 * them.
356 *
357 * You have to set the actual key yourself.
358 */
359
360 extern key *key_new(key_file */*f*/, uint32 /*id*/, const char */*type*/,
361 time_t /*exp*/, int */*err*/);
362
363 /* --- @key_delete@ --- *
364 *
365 * Arguments: @key_file *f@ = pointer to file block
366 * @key *k@ = key to delete
367 *
368 * Returns: Error code (one of the @KERR@ constants).
369 *
370 * Use: Removes the given key from the list. The key file must be
371 * writable. (Due to the horridness of the data structures,
372 * deleted keys aren't actually removed, just marked so that
373 * they can't be looked up or iterated over. One upshot of
374 * this is that they don't get written back to the file when
375 * it's closed.)
376 */
377
378 extern int key_delete(key_file */*f*/, key */*k*/);
379
380 /* --- @key_expired@ --- *
381 *
382 * Arguments: @key *k@ = pointer to key block
383 *
384 * Returns: Zero if the key is OK, nonzero if it's expired.
385 */
386
387 int key_expired(key */*k*/);
388
389 /* --- @key_expire@ --- *
390 *
391 * Arguments: @key_file *f@ = pointer to file block
392 * @key *k@ = pointer to key block
393 *
394 * Returns: Error code (one of the @KERR@ constants).
395 *
396 * Use: Immediately marks the key as expired. It may be removed
397 * immediately, if it is no longer required, and will be removed
398 * by a tidy operation when it is no longer required. The key
399 * file must be writable.
400 */
401
402 extern int key_expire(key_file */*f*/, key */*k*/);
403
404 /* --- @key_used@ --- *
405 *
406 * Arguments: @key_file *f@ = pointer to key file
407 * @key *k@ = pointer to key block
408 * @time_t t@ = when key can be removed
409 *
410 * Returns: Zero if OK, nonzero on failure.
411 *
412 * Use: Marks a key as being required until a given time. Even
413 * though the key may expire before then (and won't be returned
414 * by type after that time), it will still be available when
415 * requested explicitly by id. The key file must be writable.
416 *
417 * The only (current) reason for failure is attempting to use
418 * a key which can expire for something which can't.
419 */
420
421 extern int key_used(key_file */*f*/, key */*k*/, time_t /*t*/);
422
423 /*----- Setting and reading attributes ------------------------------------*/
424
425 /* --- @key_chkident@ --- *
426 *
427 * Arguments: @const char *p@ = pointer to a type string
428 *
429 * Returns: Zero if OK, -1 on error.
430 *
431 * Use: Checks whether an identification component string is OK.
432 */
433
434 extern int key_chkident(const char */*p*/);
435
436 /* --- @key_chkcomment@ --- *
437 *
438 * Arguments: @const char *p@ = pointer to a comment string
439 *
440 * Returns: Zero if OK, -1 on error.
441 *
442 * Use: Checks whether a comment string is OK.
443 */
444
445 extern int key_chkcomment(const char */*p*/);
446
447 /* --- @key_setcomment@ --- *
448 *
449 * Arguments: @key_file *f@ = pointer to key file block
450 * @key *k@ = pointer to key block
451 * @const char *c@ = pointer to comment to set, or zero
452 *
453 * Returns: Error code (one of the @KERR@ constants).
454 *
455 * Use: Replaces the key's current comment with a new one.
456 */
457
458 extern int key_setcomment(key_file */*f*/, key */*k*/, const char */*c*/);
459
460 /* --- @key_settag@ --- *
461 *
462 * Arguments: @key_file *f@ = pointer to key file block
463 * @key *k@ = pointer to key block
464 * @const char *tag@ = pointer to comment to set, or zero
465 *
466 * Returns: Error code (one of the @KERR@ constants).
467 *
468 * Use: Replaces the key's current tag with a new one.
469 */
470
471 extern int key_settag(key_file */*f*/, key */*k*/, const char */*tag*/);
472
473 /* --- @key_fulltag@ --- *
474 *
475 * Arguments: @key *k@ = pointer to key
476 * @dstr *d@ = pointer to destination string
477 *
478 * Returns: ---
479 *
480 * Use: Emits the key's full tag, which has the form
481 * `ID:TYPE[:TAG]'. This is used in the textual file format,
482 * and to identify passphrases for locked keys.
483 */
484
485 extern void key_fulltag(key */*k*/, dstr */*d*/);
486
487 /* --- @key_qtag@ --- *
488 *
489 * Arguments: @key_file *f@ = key file to find a key from
490 * @const char *tag@ = pointer to tag string
491 * @dstr *d@ = pointer to string for full tag name
492 * @key **k@ = where to store the key pointer
493 * @key_data **kd@ = where to store the key data pointer
494 *
495 * Returns: Zero if OK, nonzero if it failed.
496 *
497 * Use: Performs a full lookup on a qualified tag name. The tag is
498 * qualified by the names of subkeys, separated by dots. Hence,
499 * a qualified tag is ID|TAG[.TAG...]. The various result
500 * pointers can be null to indicate that the result isn't
501 * interesting.
502 */
503
504 extern int key_qtag(key_file */*f*/, const char */*tag*/,
505 dstr */*d*/, key **/*k*/, key_data **/*kd*/);
506
507 /* --- @key_getattr@ --- *
508 *
509 * Arguments: @key_file *f@ = pointer to file
510 * @key *k@ = pointer to key
511 * @const char *n@ = pointer to attribute name
512 *
513 * Returns: Pointer to attribute value, or null if not found.
514 *
515 * Use: Returns the value of a key attribute.
516 */
517
518 extern const char *key_getattr(key_file */*f*/, key */*k*/,
519 const char */*n*/);
520
521 /* --- @key_putattr@ --- *
522 *
523 * Arguments: @key_file *f@ = pointer to file
524 * @key *k@ = pointer to key
525 * @const char *n@ = pointer to attribute name
526 * @const char *v@ = pointer to attribute value or null
527 *
528 * Returns: Error code (one of the @KERR@ constants).
529 *
530 * Use: Inserts an attribute on a key. If an attribute with the same
531 * name already exists, it is deleted. Setting a null value
532 * removes the attribute.
533 */
534
535 extern int key_putattr(key_file */*f*/, key */*k*/,
536 const char */*n*/, const char */*v*/);
537
538 /* --- @key_mkattriter@ --- *
539 *
540 * Arguments: @key_attriter *i@ = pointer to attribute iterator
541 * @key *k@ = pointer to key
542 *
543 * Returns: ---
544 *
545 * Use: Initializes an attribute iterator. The attributes are
546 * returned by @key_nextattr@.
547 */
548
549 extern void key_mkattriter(key_attriter */*i*/, key */*k*/);
550
551 /* --- @key_nextattr@ --- *
552 *
553 * Arguments: @key_attriter *i@ = pointer to attribute iterator
554 * @const char **n, **v@ = pointers to name and value
555 *
556 * Returns: Zero if no attribute available, or nonzero if returned OK.
557 *
558 * Use: Returns the next attribute.
559 */
560
561 extern int key_nextattr(key_attriter */*i*/,
562 const char **/*n*/, const char **/*v*/);
563
564 /*----- Searching and iterating -------------------------------------------*/
565
566 /* --- @key_bytype@ --- *
567 *
568 * Arguments: @key_file *f@ = key file we want a key from
569 * @const char *type@ = type string for desired key
570 *
571 * Returns: Pointer to the best key to use, or null.
572 *
573 * Use: Looks up a key by its type. Returns the key with the latest
574 * expiry time. This function will not return an expired key.
575 */
576
577 extern key *key_bytype(key_file */*f*/, const char */*type*/);
578
579 /* --- @key_byid@ --- *
580 *
581 * Arguments: @key_file *f@ = key file to find a key from
582 * @uint32 id@ = id to look for
583 *
584 * Returns: Key with matching id.
585 *
586 * Use: Returns a key given its id. This function will return an
587 * expired key, but not a deleted one.
588 */
589
590 extern key *key_byid(key_file */*f*/, uint32 /*id*/);
591
592 /* --- @key_bytag@ --- *
593 *
594 * Arguments: @key_file *f@ = key file to find a key from
595 * @const char *tag@ = pointer to tag string
596 *
597 * Returns: Key with matching id or tag.
598 *
599 * Use: Returns a key given its tag or id. This function will return
600 * an expired key, but not a deleted one.
601 */
602
603 extern key *key_bytag(key_file */*f*/, const char */*tag*/);
604
605 /* --- @key_mkiter@ --- *
606 *
607 * Arguments: @key_iter *i@ = pointer to iterator object
608 * @key_file *f@ = pointer to file structure
609 *
610 * Returns: ---
611 *
612 * Use: Initializes a key iterator. The keys are returned by
613 * @key_next@.
614 */
615
616 extern void key_mkiter(key_iter */*i*/, key_file */*f*/);
617
618 /* --- @key_next@ --- *
619 *
620 * Arguments: @key_iter *i@ = pointer to iterator object
621 *
622 * Returns: Pointer to next key, or null.
623 *
624 * Use: Returns the next key in some arbitrary sequence.
625 */
626
627 extern key *key_next(key_iter */*i*/);
628
629 /*----- Fetching key data conveniently ------------------------------------*/
630
631 /* --- @key_fetchinit@ --- *
632 *
633 * Arguments: @const key_fetchdef *kf@ = pointer to base definition
634 * @key_packstruct *kps@ = pointer to destination packing def
635 * @void *p@ = pointer to destination block
636 *
637 * Returns: Pointer to packing definition.
638 *
639 * Use: Initializes a packing definition (@key_packdef@ structure).
640 * If @kps@ is null on entry, an appropriately sized block is
641 * allocated automatically. Otherwise it must be large enough.
642 */
643
644 extern key_packdef *key_fetchinit(const key_fetchdef */*kf*/,
645 key_packstruct */*kp*/, void */*p*/);
646
647 /* --- @key_fetch@ --- *
648 *
649 * Arguments: @key_packdef *kp@ = pointer to packing structure
650 * @key *k@ = key file containing desired key
651 *
652 * Returns: Error code, or zero.
653 *
654 * Use: Fetches an unpacked key from a packed one.
655 */
656
657 extern int key_fetch(key_packdef */*kp*/, key */*k*/);
658
659 /* --- @key_fetchbyname@ --- *
660 *
661 * Arguments: @key_packdef *kp@ = pointer to packing structure
662 * @key_file *kf@ = key file containing desired key
663 * @const char *tag@ = user's tag describing the key
664 *
665 * Returns: Error code, or zero.
666 *
667 * Use: Fetches a named key from a key file and unpacks it
668 * conveniently.
669 */
670
671 extern int key_fetchbyname(key_packdef */*kp*/,
672 key_file */*kf*/, const char */*tag*/);
673
674 /* --- @key_fetchdone@ --- *
675 *
676 * Arguments: @key_packdef *kp@ = pointer to packing structure
677 *
678 * Returns: ---
679 *
680 * Use: Frees a packing structure. If the structure was allocated by
681 * @key_fetchinit@ then it is freed.
682 */
683
684 extern void key_fetchdone(key_packdef */*kp*/);
685
686 /*----- Other functions ---------------------------------------------------*/
687
688 /* --- @key_moan@ --- *
689 *
690 * Arguments: @const char *file@ = name of the file
691 * @int line@ = line number in file
692 * @const char *msg@ = error message
693 * @void *p@ = argument pointer
694 *
695 * Returns: ---
696 *
697 * Use: Reports an error message about loading a key file.
698 */
699
700 extern void key_moan(const char */*file*/, int /*line*/,
701 const char */*msg*/, void */*p*/);
702
703 /* --- @key_strerror@ --- *
704 *
705 * Arguments: @int err@ = error code from @key_new@
706 *
707 * Returns: Pointer to error string.
708 *
709 * Use: Translates a @KERR@ error code into a human-readable string.
710 */
711
712 extern const char *key_strerror(int /*err*/);
713
714 /*----- That's all, folks -------------------------------------------------*/
715
716 #ifdef __cplusplus
717 }
718 #endif
719
720 #endif