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