Only produce initialization message if verbose.
[u/mdw/catacomb] / key.h
CommitLineData
d03ab969 1/* -*-c-*-
2 *
d11a0bf7 3 * $Id: key.h,v 1.3 1999/12/22 15:47:48 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 $
d11a0bf7 33 * Revision 1.3 1999/12/22 15:47:48 mdw
34 * Major key-management revision.
35 *
b3f05084 36 * Revision 1.2 1999/12/10 23:29:48 mdw
37 * Change header file guard names.
38 *
d03ab969 39 * Revision 1.1 1999/09/03 08:41:12 mdw
40 * Initial import.
41 *
42 */
43
b3f05084 44#ifndef CATACOMB_KEY_H
45#define CATACOMB_KEY_H
d03ab969 46
47#ifdef __cplusplus
48 extern "C" {
49#endif
50
51/*----- Header files ------------------------------------------------------*/
52
53#include <stdio.h>
54#include <time.h>
55
56#include <mLib/bits.h>
d11a0bf7 57#include <mLib/dstr.h>
d03ab969 58#include <mLib/hash.h>
59#include <mLib/sym.h>
60
d11a0bf7 61#ifndef CATACOMB_MP_H
62# include "mp.h"
63#endif
64
d03ab969 65/*----- Data structures ---------------------------------------------------*/
66
67/* --- Key attributes --- *
68 *
69 * Each attribute is stored as a symbol in a symbol table. The value is
70 * the plain (not url-encoded) text to be written to the the file. If the
71 * value is binary data, then by this point it's base-64 encoded.
72 */
73
74typedef struct key_attr {
75 sym_base _b; /* Symbol table data */
76 char *p; /* Pointer to attribute value */
77} key_attr;
78
d11a0bf7 79/* --- Key data structure --- */
80
81typedef struct key_data {
82 unsigned e; /* Encoding type for key data */
83 union {
84
85 /* --- Plain binary key data --- *
86 *
87 * Also used for encrypted key types.
88 */
89
90 struct {
91 octet *k; /* Actual key data */
92 size_t sz; /* Size of the key data */
93 } k; /* Plain binary key */
94
95 /* --- Multiprecision integer keys --- */
96
97 mp *m; /* Multiprecision integer */
98
99 /* --- Structured key data --- */
100
101 sym_table s; /* Structured key data */
102 } u;
103} key_data;
104
105typedef struct key_struct {
106 sym_base _b;
107 key_data k;
108} key_struct;
109
110/* --- Key binary encoding --- *
111 *
112 * The binary encoding consists of a header containing a 16-bit encoding type
113 * and a 16-bit length, followed immediately by the key data, followed by
114 * between zero and three zero bytes to make the total length a multiple of
115 * four. The format of the following data depends on the encoding type:
116 *
117 * @KENC_BINARY@ Binary data.
118 *
119 * @KENC_MP@ Octet array interpreted in big-endian byte order.
120 *
121 * @KENC_STRUCT@ An array of pairs, each containing a string (8-bit
122 * length followed by data and zero-padding to 4-byte
123 * boundary) and key binary encodings.
124 *
125 * @KENC_ENCRYPT@ Binary data, format
126 */
127
128/* --- Key encoding methods and other flags--- */
129
130enum {
131
132 /* --- Bottom two bits are the encoding type --- */
133
134 KF_ENCMASK = 0x03, /* Encoding mask */
135 KENC_BINARY = 0x00, /* Plain binary key (@k@) */
136 KENC_MP = 0x01, /* Multiprecision integer (@i@) */
137 KENC_STRUCT = 0x02, /* Structured key data (@s@) */
138 KENC_ENCRYPT = 0x03, /* Encrypted key type (@k@) */
139
140 /* --- Key category bits --- */
141
142 KF_CATMASK = 0x0c, /* Category mask */
143 KCAT_SYMM = 0x00, /* Symmetric encryption key */
144 KCAT_PRIV = 0x04, /* Private (asymmetric) key */
145 KCAT_PUB = 0x08, /* Public (asymmetric) key */
146 KCAT_SHARE = 0x0c, /* Shared (asymmetric) key */
147 KF_NONSECRET = 0x08, /* Bit flag for non-secret keys */
148
149 /* --- Other flags --- */
150
151 KF_BURN = 0x10, /* Burn key after use */
152
153 /* --- Tag end --- */
154
155 KENC_MAX /* Dummy limit constant */
156};
157
158/* --- Key flag filtering --- */
159
160typedef struct key_filter {
161 unsigned f;
162 unsigned m;
163} key_filter;
164
165/* --- Matching aginst key selection --- */
166
167#define KEY_MATCH(kd, kf) \
168 (!(kf) || \
169 ((kd)->e & KF_ENCMASK) == KENC_STRUCT || \
170 ((kd)->e & (kf)->m) == (kf)->f)
171
d03ab969 172/* --- Main key structure --- *
173 *
174 * Each key is stored in two symbol tables, one indexed by keyid, and the
175 * other indexed by type. Because many keys can have the same type, the type
176 * table contains a list of keys, sorted in descending order of expiry.
177 */
178
179typedef struct key {
d11a0bf7 180
181 /* --- Hashtable management --- */
182
d03ab969 183 hash_base _b; /* Symbol table data */
184 struct key *next; /* Next key of the same type */
d11a0bf7 185
186 /* --- Basic key attributes --- */
187
d03ab969 188 uint32 id; /* Key id used to name it */
d11a0bf7 189 char *tag; /* Textual tag name */
d03ab969 190 char *type; /* Textual key type */
d03ab969 191 time_t exp, del; /* Expiry times for keys */
d11a0bf7 192
193 /* --- The key data itself --- */
194
195 key_data k; /* The actual key data */
196
197 /* --- Other attributes and commentary --- */
198
d03ab969 199 sym_table a; /* Hashtable of key attributes */
200 char *c; /* Any additional comments */
201} key;
202
203/* --- The keys-by-type entries --- */
204
d11a0bf7 205typedef struct key_ref {
d03ab969 206 sym_base _b; /* Symbol table data */
207 key *k; /* Pointer to first key in list */
d11a0bf7 208} key_ref;
d03ab969 209
210/* --- A key file --- */
211
212typedef struct key_file {
213 FILE *fp; /* File pointer open on file */
d03ab969 214 char *name; /* Filename used to create it */
215 unsigned f; /* Various useful flags */
216 hash_table byid; /* Table of keys by keyid */
217 sym_table bytype; /* Table of keys by type */
d11a0bf7 218 sym_table bytag; /* Table of keys by tag */
d03ab969 219 size_t idload; /* Loading on id table */
220} key_file;
221
222/* --- Key file flags --- */
223
224enum {
225 KF_WRITE = 1, /* File opened for writing */
226 KF_MODIFIED = 2 /* File has been modified */
227};
228
229/* --- Iterating over keys --- *
230 *
231 * Both of these are simple symbol table iterators, but they're made distinct
232 * types for the dubious benefits that type safety brings.
233 */
234
235typedef struct { hash_iter i; time_t t; } key_iter;
236typedef struct { sym_iter i; } key_attriter;
237
238/* --- File opening options --- */
239
240enum {
241 KOPEN_READ,
242 KOPEN_WRITE
243};
244
245/* --- Various other magic numbers --- */
246
d03ab969 247#define KEXP_FOREVER ((time_t)-1) /* Never expire this key */
248#define KEXP_EXPIRE ((time_t)-2) /* Expire this key when unused */
249
d11a0bf7 250/* --- Key error codes --- */
251
252enum {
253 KERR_OK, /* No error */
254 KERR_BADTAG = -1, /* Malformed tag string */
255 KERR_BADTYPE = -2, /* Malformed type string */
256 KERR_BADCOMMENT = -3, /* Malformed comment string */
257 KERR_DUPID = -4, /* Duplicate keyid */
258 KERR_DUPTAG = -5, /* Duplicate key tag string */
259 KERR_READONLY = -6, /* Key file is read-only */
260 KERR_WILLEXPIRE = -7, /* Key will eventually expire */
261 KERR_EXPIRED = -8, /* Key has already expired */
262 KERR_BADFLAGS = -9, /* Error in flags string */
263 KERR_MAX /* Largest possible error */
264};
265
266/* --- Write error codes --- */
d03ab969 267
268enum {
269 KWRITE_OK, /* Everything went fine */
270 KWRITE_FAIL = -1, /* Close attempt failed */
271 KWRITE_BROKEN = -2 /* Key ring needs manual fixing */
272};
273
d11a0bf7 274/* --- Error reporting functions for @key_merge@ and @key_open@ --- */
275
276typedef void key_reporter(const char */*file*/, int /*line*/,
277 const char */*err*/, void */*p*/);
278
d03ab969 279/* --- Macros for testing expiry --- */
280
281#define KEY_EXPIRED(now, exp) \
282 ((exp) == KEXP_EXPIRE || ((exp) != KEXP_FOREVER && (exp) < (now)))
283
d11a0bf7 284/*----- Key data manipulation ---------------------------------------------*/
285
286/* --- @key_destroy@ --- *
287 *
288 * Arguments: @key_data *k@ = pointer to key data to destroy
289 *
290 * Returns: ---
291 *
292 * Use: Destroys a lump of key data.
293 */
d03ab969 294
d11a0bf7 295extern void key_destroy(key_data */*k*/);
d03ab969 296
d11a0bf7 297/* --- @key_readflags@ --- *
d03ab969 298 *
d11a0bf7 299 * Arguments: @const char *p@ = pointer to string to read
300 * @char **pp@ = where to store the end pointer
301 * @unsigned *ff@ = where to store the flags
302 * @unsigned *mm@ = where to store the mask
d03ab969 303 *
d11a0bf7 304 * Returns: Zero if all went well, nonzero if there was an error.
d03ab969 305 *
d11a0bf7 306 * Use: Reads a flag string.
d03ab969 307 */
308
d11a0bf7 309extern int key_readflags(const char */*p*/, char **/*pp*/,
310 unsigned */*ff*/, unsigned */*mm*/);
d03ab969 311
d11a0bf7 312/* --- @key_writeflags@ --- *
d03ab969 313 *
d11a0bf7 314 * Arguments: @unsigned f@ = flags to write
315 * @dstr *d@ = pointer to destination string
d03ab969 316 *
d11a0bf7 317 * Returns: ---
d03ab969 318 *
d11a0bf7 319 * Use: Emits a flags word as a string representation.
d03ab969 320 */
321
d11a0bf7 322extern void key_writeflags(unsigned /*f*/, dstr */*d*/);
d03ab969 323
d11a0bf7 324/* --- @key_binary@ --- *
d03ab969 325 *
d11a0bf7 326 * Arguments: @key_data *k@ = pointer to key data block
327 * @const void *p@ = pointer to key data
328 * @size_t sz@ = size of the key data
d03ab969 329 *
330 * Returns: ---
331 *
d11a0bf7 332 * Use: Sets a binary key in a key data block.
d03ab969 333 */
334
d11a0bf7 335extern void key_binary(key_data */*k*/, const void */*p*/, size_t /*sz*/);
d03ab969 336
d11a0bf7 337/* --- @key_encrypted@ --- *
d03ab969 338 *
d11a0bf7 339 * Arguments: @key_data *k@ = pointer to key data block
340 * @const void *p@ = pointer to key data
341 * @size_t sz@ = size of the key data
d03ab969 342 *
d11a0bf7 343 * Returns: ---
d03ab969 344 *
d11a0bf7 345 * Use: Sets an encrypted key in a key data block.
d03ab969 346 */
347
d11a0bf7 348extern void key_encrypted(key_data */*k*/, const void */*p*/, size_t /*sz*/);
d03ab969 349
d11a0bf7 350/* --- @key_mp@ --- *
d03ab969 351 *
d11a0bf7 352 * Arguments: @key_data *k@ = pointer to key data block
353 * @mp *m@ = pointer to the value to set
d03ab969 354 *
355 * Returns: ---
356 *
d11a0bf7 357 * Use: Sets a multiprecision integer key in a key block.
d03ab969 358 */
359
d11a0bf7 360extern void key_mp(key_data */*k*/, mp */*m*/);
d03ab969 361
d11a0bf7 362/* --- @key_structure@ --- *
d03ab969 363 *
d11a0bf7 364 * Arguments: @key_data *k@ = pointer to key data block
d03ab969 365 *
d11a0bf7 366 * Returns: ---
d03ab969 367 *
d11a0bf7 368 * Use: Initializes a structured key type.
d03ab969 369 */
370
d11a0bf7 371extern void key_structure(key_data */*k*/);
d03ab969 372
d11a0bf7 373/* --- @key_structfind@ --- *
d03ab969 374 *
d11a0bf7 375 * Arguments: @key_data *k@ = pointer to key data block
376 * @const char *tag@ = pointer to tag string
d03ab969 377 *
d11a0bf7 378 * Returns: Pointer to key data block, or null.
d03ab969 379 *
d11a0bf7 380 * Use: Looks up the tag in a structured key.
d03ab969 381 */
382
d11a0bf7 383extern key_data *key_structfind(key_data */*k*/, const char */*tag*/);
d03ab969 384
d11a0bf7 385/* --- @key_structcreate@ --- *
d03ab969 386 *
d11a0bf7 387 * Arguments: @key_data *k@ = pointer to key data block
388 * @const char *tag@ = pointer to tag string
d03ab969 389 *
d11a0bf7 390 * Returns: Pointer to newly created key data.
d03ab969 391 *
d11a0bf7 392 * Use: Creates a new uninitialized subkey.
d03ab969 393 */
394
d11a0bf7 395extern key_data *key_structcreate(key_data */*k*/, const char */*tag*/);
d03ab969 396
d11a0bf7 397/* --- @key_match@ --- *
d03ab969 398 *
d11a0bf7 399 * Arguments: @key_data *k@ = pointer to key data block
400 * @const key_filter *kf@ = pointer to filter block
d03ab969 401 *
d11a0bf7 402 * Returns: Nonzero if the key matches the filter.
d03ab969 403 *
d11a0bf7 404 * Use: Checks whether a key matches a filter.
d03ab969 405 */
406
d11a0bf7 407extern int key_match(key_data */*k*/, const key_filter */*kf*/);
d03ab969 408
d11a0bf7 409/* --- @key_do@ --- *
d03ab969 410 *
d11a0bf7 411 * Arguments: @key_data *k@ = pointer to key data block
412 * @const key_filter *kf@ = pointer to filter block
413 * @dstr *d@ = pointer to base string
414 * @int (*func)(key_data *kd, dstr *d, void *p@ = function
415 * @void *p@ = argument to function
d03ab969 416 *
d11a0bf7 417 * Returns: Nonzero return code from function, or zero.
d03ab969 418 *
d11a0bf7 419 * Use: Runs a function over all the leaves of a key.
d03ab969 420 */
421
d11a0bf7 422extern int key_do(key_data */*k*/, const key_filter */*kf*/, dstr */*d*/,
423 int (*/*func*/)(key_data */*kd*/,
424 dstr */*d*/, void */*p*/),
425 void */*p*/);
d03ab969 426
d11a0bf7 427/* --- @key_copy@ --- *
d03ab969 428 *
d11a0bf7 429 * Arguments: @key_data *kd@ = pointer to destination data block
430 * @key_data *k@ = pointer to source data block
431 * @const key_filter *kf@ = pointer to filter block
d03ab969 432 *
d11a0bf7 433 * Returns: Nonzero if an item was actually copied.
d03ab969 434 *
d11a0bf7 435 * Use: Copies a chunk of key data from one place to another.
436 */
437
438extern int key_copy(key_data */*kd*/, key_data */*k*/,
439 const key_filter */*kf*/);
440
441/* --- @key_read@ --- *
442 *
443 * Arguments: @const char *p@ = pointer to textual key representation
444 * @key_data *k@ = pointer to output block for key data
445 * @char **pp@ = where to store the end pointer
446 *
447 * Returns: Zero if all went well, nonzero if there was a problem.
448 *
449 * Use: Parses a textual key description.
450 */
451
452extern int key_read(const char */*p*/, key_data */*k*/, char **/*pp*/);
453
454/* --- @key_write@ --- *
455 *
456 * Arguments: @key_data *k@ = pointer to key data
457 * @dstr *d@ = destination string to write on
458 * @const key_filter *kf@ = pointer to key selection block
459 *
460 * Returns: Nonzero if any items were actually written.
461 *
462 * Use: Writes a key in a textual encoding.
463 */
464
465extern int key_write(key_data */*k*/, dstr */*d*/,
466 const key_filter */*kf*/);
467
468/* --- @key_decode@ --- *
469 *
470 * Arguments: @const void *p@ = pointer to buffer to read
471 * @size_t sz@ = size of the buffer
472 * @key_data *k@ = pointer to key data block to write to
473 *
474 * Returns: Zero if everything worked, nonzero otherwise.
475 *
476 * Use: Decodes a binary representation of a key.
477 */
478
479extern int key_decode(const void */*p*/, size_t /*sz*/, key_data */*k*/);
480
481/* --- @key_encode@ --- *
482 *
483 * Arguments: @key_data *k@ = pointer to key data block
484 * @dstr *d@ = pointer to destination string
485 * @const key_filter *kf@ = pointer to key selection block
486 *
487 * Returns: Nonzero if any items were actually written.
488 *
489 * Use: Encodes a key block as binary data.
490 */
491
492extern int key_encode(key_data */*k*/, dstr */*d*/,
493 const key_filter */*kf*/);
494
495/* --- @key_plock@ --- *
496 *
497 * Arguments: @const char *tag@ = tag to use for passphrase
498 * @key_data *k@ = source key data block
499 * @key_data *kt@ = target key data block
500 *
501 * Returns: Zero if successful, nonzero if there was a problem.
502 *
503 * Use: Locks a key by encrypting it with a passphrase.
504 */
505
506extern int key_plock(const char */*tag*/, key_data */*k*/, key_data */*kt*/);
507
508/* --- @key_punlock@ --- *
509 *
510 * Arguments: @const char *tag@ = tag to use for passphrase
511 * @key_data *k@ = source key data block
512 * @key_data *kt@ = target key data block
513 *
514 * Returns: Zero if it worked, nonzero if it didn't.
515 *
516 * Use: Unlocks a passphrase-locked key.
d03ab969 517 */
518
d11a0bf7 519extern int key_punlock(const char */*tag*/,
520 key_data */*k*/, key_data */*kt*/);
521
522/*----- Reading and writing keys and files --------------------------------*/
d03ab969 523
524/* --- @key_merge@ --- *
525 *
526 * Arguments: @key_file *f@ = pointer to file structure
527 * @const char *file@ = name of file (for error messages)
528 * @FILE *fp@ = file handle to read from
d11a0bf7 529 * @key_reporter *rep@ = error reporting function
530 * @void *arg@ = argument for function
d03ab969 531 *
d11a0bf7 532 * Returns: Error code (one of the @KERR@ constants).
d03ab969 533 *
534 * Use: Reads keys from a file, and inserts them into the file.
535 */
536
d11a0bf7 537extern int key_merge(key_file */*f*/, const char */*file*/, FILE */*fp*/,
538 key_reporter */*rep*/, void */*arg*/);
d03ab969 539
540/* --- @key_extract@ --- *
541 *
542 * Arguments: @key_file *f@ = pointer to file structure
543 * @key *k@ = key to extract
544 * @FILE *fp@ = file to write on
d11a0bf7 545 * @const key_filter *kf@ = pointer to key selection block
d03ab969 546 *
547 * Returns: Zero if OK, EOF on error.
548 *
549 * Use: Extracts a key to an ouptut file.
550 */
551
d11a0bf7 552extern int key_extract(key_file */*f*/, key */*k*/, FILE */*fp*/,
553 const key_filter */*kf*/);
d03ab969 554
555/* --- @key_open@ --- *
556 *
557 * Arguments: @key_file *f@ = pointer to file structure to initialize
558 * @const char *file@ = pointer to the file name
559 * @int how@ = opening options (@KOPEN_*@).
d11a0bf7 560 * @key_reporter *rep@ = error reporting function
561 * @void *arg@ = argument for function
d03ab969 562 *
563 * Returns: Zero if it worked, nonzero otherwise.
564 *
565 * Use: Opens a key file, reads its contents, and stores them in a
566 * structure. The file is locked appropriately until closed
567 * using @key_close@. On an error, everything is cleared away
568 * tidily. If the file is opened with @KOPEN_WRITE@, it's
569 * created if necessary, with read and write permissions for its
570 * owner only.
571 */
572
d11a0bf7 573extern int key_open(key_file */*f*/, const char */*file*/, int /*how*/,
574 key_reporter */*rep*/, void */*arg*/);
d03ab969 575
576/* --- @key_close@ --- *
577 *
578 * Arguments: @key_file *f@ = pointer to key file block
579 *
580 * Returns: A @KWRITE_@ code indicating how it went.
581 *
582 * Use: Frees all the key data, writes any changes. Make sure that
583 * all hell breaks loose if this returns @KWRITE_BROKEN@.
584 */
585
586extern int key_close(key_file */*f*/);
587
d11a0bf7 588/* --- @key_save@ --- *
589 *
590 * Arguments: @key_file *f@ = pointer to key file block
591 *
592 * Returns: A @KWRITE_@ code indicating how well it worked.
593 *
594 * Use: Writes a key file's data back to the actual file. This code
595 * is extremely careful about error handling. It should usually
596 * be able to back out somewhere sensible, but it can tell when
597 * it's got itself into a real pickle and starts leaving well
598 * alone.
599 *
600 * Callers, please make sure that you ring alarm bells when this
601 * function returns @KWRITE_BROKEN@.
602 */
603
604extern int key_save(key_file */*f*/);
605
606/* --- @key_lockfile@ --- *
607 *
608 * Arguments: @key_file *f@ = pointer to file structure to initialize
609 * @const char *file@ = pointer to the file name
610 * @int how@ = opening options (@KOPEN_*@).
611 *
612 * Returns: Zero if it worked, nonzero otherwise.
613 *
614 * Use: Opens a keyfile and stores the information needed for
615 * continued access in the structure.
616 *
617 * If the file is opened with @KOPEN_WRITE@, it's created if
618 * necessary with read and write permissions for owner only, and
619 * locked for update while it's open.
620 *
621 * This is a system-dependent routine, and only really intended
622 * for the private use of @key_open@.
623 */
624
625extern int key_lockfile(key_file */*f*/, const char */*file*/, int /*how*/);
626
627/*----- Creating and manipulating keys ------------------------------------*/
628
d03ab969 629/* --- @key_new@ ---
630 *
631 * Arguments: @key_file *f@ = pointer to key file
d11a0bf7 632 * @uint32 id@ = keyid to set
d03ab969 633 * @const char *type@ = the type of this key
d03ab969 634 * @time_t exp@ = when the key expires
d11a0bf7 635 * @int *err@ = where to store the error condition
d03ab969 636 *
637 * Returns: Key block containing new data, or null if it couldn't be
638 * done.
639 *
640 * Use: Attaches a new key to a key file. You must have a writable
641 * key file for this to work.
642 *
643 * The type is a key type string. This interface doesn't care
644 * about how type strings are formatted: it just treats them as
645 * opaque gobs of text. Clients are advised to choose some
646 * standard for representing key types, though.
647 *
d03ab969 648 * The expiry time should either be a time in the future, or the
649 * magic value @KEXP_FOREVER@ which means `never expire this
650 * key'. Be careful with `forever' keys. If I were you, I'd
651 * use a more sophisticated key management system than this for
652 * them.
653 *
d11a0bf7 654 * You have to set the actual key yourself.
d03ab969 655 */
656
d11a0bf7 657extern key *key_new(key_file */*f*/, uint32 /*id*/, const char */*type*/,
658 time_t /*exp*/, int */*err*/);
d03ab969 659
660/* --- @key_delete@ --- *
661 *
662 * Arguments: @key_file *f@ = pointer to file block
663 * @key *k@ = key to delete
664 *
d11a0bf7 665 * Returns: Error code (one of the @KERR@ constants).
d03ab969 666 *
667 * Use: Removes the given key from the list. The key file must be
668 * writable. (Due to the horridness of the data structures,
669 * deleted keys aren't actually removed, just marked so that
670 * they can't be looked up or iterated over. One upshot of
671 * this is that they don't get written back to the file when
672 * it's closed.)
673 */
674
d11a0bf7 675extern int key_delete(key_file */*f*/, key */*k*/);
d03ab969 676
677/* --- @key_expire@ --- *
678 *
679 * Arguments: @key_file *f@ = pointer to file block
680 * @key *k@ = pointer to key block
681 *
d11a0bf7 682 * Returns: Error code (one of the @KERR@ constants).
d03ab969 683 *
684 * Use: Immediately marks the key as expired. It may be removed
685 * immediately, if it is no longer required, and will be removed
686 * by a tidy operation when it is no longer required. The key
687 * file must be writable.
688 */
689
d11a0bf7 690extern int key_expire(key_file */*f*/, key */*k*/);
d03ab969 691
692/* --- @key_used@ --- *
693 *
694 * Arguments: @key_file *f@ = pointer to key file
695 * @key *k@ = pointer to key block
696 * @time_t t@ = when key can be removed
697 *
698 * Returns: Zero if OK, nonzero on failure.
699 *
700 * Use: Marks a key as being required until a given time. Even
701 * though the key may expire before then (and won't be returned
702 * by type after that time), it will still be available when
703 * requested explicitly by id. The key file must be writable.
704 *
705 * The only (current) reason for failure is attempting to use
706 * a key which can expire for something which can't.
707 */
708
709extern int key_used(key_file */*f*/, key */*k*/, time_t /*t*/);
710
d11a0bf7 711/*----- Setting and reading attributes ------------------------------------*/
712
713/* --- @key_chkident@ --- *
714 *
715 * Arguments: @const char *p@ = pointer to a type string
716 *
717 * Returns: Zero if OK, -1 on error.
718 *
719 * Use: Checks whether an identification component string is OK.
720 */
721
722extern int key_chkident(const char */*p*/);
723
724/* --- @key_chkcomment@ --- *
725 *
726 * Arguments: @const char *p@ = pointer to a comment string
727 *
728 * Returns: Zero if OK, -1 on error.
729 *
730 * Use: Checks whether a comment string is OK.
731 */
732
733extern int key_chkcomment(const char */*p*/);
734
735/* --- @key_setcomment@ --- *
736 *
737 * Arguments: @key_file *f@ = pointer to key file block
738 * @key *k@ = pointer to key block
739 * @const char *c@ = pointer to comment to set, or zero
740 *
741 * Returns: Error code (one of the @KERR@ constants).
742 *
743 * Use: Replaces the key's current comment with a new one.
744 */
745
746extern int key_setcomment(key_file */*f*/, key */*k*/, const char */*c*/);
747
748/* --- @key_settag@ --- *
749 *
750 * Arguments: @key_file *f@ = pointer to key file block
751 * @key *k@ = pointer to key block
752 * @const char *tag@ = pointer to comment to set, or zero
753 *
754 * Returns: Error code (one of the @KERR@ constants).
755 *
756 * Use: Replaces the key's current tag with a new one.
757 */
758
759extern int key_settag(key_file */*f*/, key */*k*/, const char */*tag*/);
760
761/* --- @key_fulltag@ --- *
762 *
763 * Arguments: @key *k@ = pointer to key
764 * @dstr *d@ = pointer to destination string
765 *
766 * Returns: ---
767 *
768 * Use: Emits the key's full tag, which has the form
769 * `ID:TYPE[:TAG]'. This is used in the textual file format,
770 * and to identify passphrases for locked keys.
771 */
772
773extern void key_fulltag(key */*k*/, dstr */*d*/);
774
775/* --- @key_qtag@ --- *
776 *
777 * Arguments: @key_file *f@ = key file to find a key from
778 * @const char *tag@ = pointer to tag string
779 * @dstr *d@ = pointer to string for full tag name
780 * @key **k@ = where to store the key pointer
781 * @key_data **kd@ = where to store the key data pointer
782 *
783 * Returns: Zero if OK, nonzero if it failed.
784 *
785 * Use: Performs a full lookup on a qualified tag name. The tag is
786 * qualified by the names of subkeys, separated by dots. Hence,
787 * a qualified tag is ID|TAG[.TAG...]. The various result
788 * pointers can be null to indicate that the result isn't
789 * interesting.
790 */
791
792extern int key_qtag(key_file */*f*/, const char */*tag*/,
793 dstr */*d*/, key **/*k*/, key_data **/*kd*/);
794
795/* --- @key_getattr@ --- *
796 *
797 * Arguments: @key_file *f@ = pointer to file
798 * @key *k@ = pointer to key
799 * @const char *n@ = pointer to attribute name
800 *
801 * Returns: Pointer to attribute value, or null if not found.
802 *
803 * Use: Returns the value of a key attribute.
804 */
805
806extern const char *key_getattr(key_file */*f*/, key */*k*/,
807 const char */*n*/);
808
809/* --- @key_putattr@ --- *
810 *
811 * Arguments: @key_file *f@ = pointer to file
812 * @key *k@ = pointer to key
813 * @const char *n@ = pointer to attribute name
814 * @const char *v@ = pointer to attribute value or null
815 *
816 * Returns: Error code (one of the @KERR@ constants).
817 *
818 * Use: Inserts an attribute on a key. If an attribute with the same
819 * name already exists, it is deleted. Setting a null value
820 * removes the attribute.
821 */
822
823extern int key_putattr(key_file */*f*/, key */*k*/,
824 const char */*n*/, const char */*v*/);
825
826/* --- @key_mkattriter@ --- *
827 *
828 * Arguments: @key_attriter *i@ = pointer to attribute iterator
829 * @key *k@ = pointer to key
830 *
831 * Returns: ---
832 *
833 * Use: Initializes an attribute iterator. The attributes are
834 * returned by @key_nextattr@.
835 */
836
837extern void key_mkattriter(key_attriter */*i*/, key */*k*/);
838
839/* --- @key_nextattr@ --- *
840 *
841 * Arguments: @key_attriter *i@ = pointer to attribute iterator
842 * @const char **n, **v@ = pointers to name and value
843 *
844 * Returns: Zero if no attribute available, or nonzero if returned OK.
845 *
846 * Use: Returns the next attribute.
847 */
848
849extern int key_nextattr(key_attriter */*i*/,
850 const char **/*n*/, const char **/*v*/);
851
852/*----- Searching and iterating -------------------------------------------*/
853
854/* --- @key_bytype@ --- *
855 *
856 * Arguments: @key_file *f@ = key file we want a key from
857 * @const char *type@ = type string for desired key
858 *
859 * Returns: Pointer to the best key to use, or null.
860 *
861 * Use: Looks up a key by its type. Returns the key with the latest
862 * expiry time. This function will not return an expired key.
863 */
864
865extern key *key_bytype(key_file */*f*/, const char */*type*/);
866
867/* --- @key_byid@ --- *
868 *
869 * Arguments: @key_file *f@ = key file to find a key from
870 * @uint32 id@ = id to look for
871 *
872 * Returns: Key with matching id.
873 *
874 * Use: Returns a key given its id. This function will return an
875 * expired key, but not a deleted one.
876 */
877
878extern key *key_byid(key_file */*f*/, uint32 /*id*/);
879
880/* --- @key_bytag@ --- *
881 *
882 * Arguments: @key_file *f@ = key file to find a key from
883 * @const char *tag@ = pointer to tag string
884 *
885 * Returns: Key with matching id or tag.
886 *
887 * Use: Returns a key given its tag or id. This function will return
888 * an expired key, but not a deleted one.
889 */
890
891extern key *key_bytag(key_file */*f*/, const char */*tag*/);
892
893/* --- @key_mkiter@ --- *
894 *
895 * Arguments: @key_iter *i@ = pointer to iterator object
896 * @key_file *f@ = pointer to file structure
897 *
898 * Returns: ---
899 *
900 * Use: Initializes a key iterator. The keys are returned by
901 * @key_next@.
902 */
903
904extern void key_mkiter(key_iter */*i*/, key_file */*f*/);
905
906/* --- @key_next@ --- *
907 *
908 * Arguments: @key_iter *i@ = pointer to iterator object
909 *
910 * Returns: Pointer to next key, or null.
911 *
912 * Use: Returns the next key in some arbitrary sequence.
913 */
914
915extern key *key_next(key_iter */*i*/);
916
917/*----- Other functions ---------------------------------------------------*/
918
919/* --- @key_moan@ --- *
920 *
921 * Arguments: @const char *file@ = name of the file
922 * @int line@ = line number in file
923 * @const char *msg@ = error message
924 * @void *p@ = argument pointer
925 *
926 * Returns: ---
927 *
928 * Use: Reports an error message about loading a key file.
929 */
930
931extern void key_moan(const char */*file*/, int /*line*/,
932 const char */*msg*/, void */*p*/);
933
934/* --- @key_strerror@ --- *
935 *
936 * Arguments: @int err@ = error code from @key_new@
937 *
938 * Returns: Pointer to error string.
939 *
940 * Use: Translates a @KERR@ error code into a human-readable string.
941 */
942
943extern const char *key_strerror(int /*err*/);
944
d03ab969 945/*----- That's all, folks -------------------------------------------------*/
946
947#ifdef __cplusplus
948 }
949#endif
950
951#endif