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