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