chiark / gitweb /
2e1c630aa49f3a23742e6b1d3096402ff44ded7d
[tripe] / tripe.h
1 /* -*-c-*-
2  *
3  * $Id: tripe.h,v 1.10 2002/01/13 14:54:58 mdw Exp $
4  *
5  * Main header file for TrIPE
6  *
7  * (c) 2001 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of Trivial IP Encryption (TrIPE).
13  *
14  * TrIPE is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * TrIPE 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 General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with TrIPE; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Revision history --------------------------------------------------* 
30  *
31  * $Log: tripe.h,v $
32  * Revision 1.10  2002/01/13 14:54:58  mdw
33  * Provide MGF macros.
34  *
35  * Revision 1.9  2001/06/22 19:40:36  mdw
36  * Support expiry of other peers' public keys.
37  *
38  * Revision 1.8  2001/06/19 22:10:57  mdw
39  * Some more constants for the algorithms.  Document the packet format
40  * change for non-malleability.  Moved @buf@ definitions to separate header
41  * file.
42  *
43  * Revision 1.7  2001/03/03 12:07:08  mdw
44  * Rename word get and put functions now that there's 16-bit support.
45  *
46  * Revision 1.6  2001/02/19 19:11:09  mdw
47  * Output buffering on admin connections.
48  *
49  * Revision 1.5  2001/02/16 21:41:43  mdw
50  * Major changes.  See source files for details.
51  *
52  * Revision 1.4  2001/02/05 19:56:37  mdw
53  * Sequence number protection, and BSD tunnels.
54  *
55  * Revision 1.3  2001/02/04 01:17:55  mdw
56  * Create a configuration header file to tidy up command lines.
57  *
58  * Revision 1.2  2001/02/03 22:40:29  mdw
59  * Put timer information into the entropy pool when packets are received
60  * and on similar events.  Reseed the generator on the interval timer.
61  *
62  * Revision 1.1  2001/02/03 20:26:37  mdw
63  * Initial checkin.
64  *
65  */
66
67 #ifndef TRIPE_H
68 #define TRIPE_H
69
70 #ifdef __cplusplus
71   extern "C" {
72 #endif
73
74 /*----- Header files ------------------------------------------------------*/
75
76 #include "config.h"
77
78 #include <assert.h>
79 #include <ctype.h>
80 #include <errno.h>
81 #include <signal.h>
82 #include <stdarg.h>
83 #include <stddef.h>
84 #include <stdio.h>
85 #include <stdlib.h>
86 #include <string.h>
87 #include <time.h>
88
89 #include <sys/types.h>
90 #include <sys/time.h>
91 #include <unistd.h>
92 #include <fcntl.h>
93 #include <sys/stat.h>
94
95 #include <sys/socket.h>
96 #include <sys/un.h>
97 #include <netinet/in.h>
98 #include <arpa/inet.h>
99 #include <netdb.h>
100
101 #include <pwd.h>
102 #include <grp.h>
103
104 #include <mLib/alloc.h>
105 #include <mLib/arena.h>
106 #include <mLib/bres.h>
107 #include <mLib/dstr.h>
108 #include <mLib/env.h>
109 #include <mLib/fdflags.h>
110 #include <mLib/fwatch.h>
111 #include <mLib/mdwopt.h>
112 #include <mLib/quis.h>
113 #include <mLib/report.h>
114 #include <mLib/sel.h>
115 #include <mLib/selbuf.h>
116 #include <mLib/sig.h>
117 #include <mLib/str.h>
118 #include <mLib/sub.h>
119 #include <mLib/trace.h>
120
121 #include <catacomb/gcipher.h>
122 #include <catacomb/gmac.h>
123 #include <catacomb/grand.h>
124 #include <catacomb/key.h>
125 #include <catacomb/paranoia.h>
126
127 #include <catacomb/noise.h>
128 #include <catacomb/rand.h>
129
130 #include <catacomb/mp.h>
131 #include <catacomb/mpmont.h>
132 #include <catacomb/mprand.h>
133 #include <catacomb/dh.h>
134
135 #include "buf.h"
136 #include "util.h"
137
138 #undef sun
139
140 /*----- Magic numbers -----------------------------------------------------*/
141
142 /* --- Tunnel types --- */
143
144 #define TUN_NOTDEF 0
145 #define TUN_UNET 1
146 #define TUN_BSD 2
147
148 /* --- Trace flags --- */
149
150 #define T_TUNNEL 1u
151 #define T_PEER 2u
152 #define T_PACKET 4u
153 #define T_ADMIN 8u
154 #define T_CRYPTO 16u
155 #define T_KEYSET 32u
156 #define T_KEYEXCH 64u
157 #define T_KEYMGMT 128u
158
159 #define T_ALL 255u
160
161 /* --- Units --- */
162
163 #define SEC(n) (n##u)
164 #define MIN(n) (n##u * 60u)
165 #define MEG(n) (n##ul * 1024ul * 1024ul)
166
167 /* --- Other things --- */
168
169 #define PKBUFSZ 65536
170
171 /*----- TrIPE protocol ----------------------------------------------------*/
172
173 /* --- TrIPE message format --- *
174  *
175  * A packet begins with a single-byte message type.  The top four bits are a
176  * category code used to send the message to the right general place in the
177  * code; the bottom bits identify the actual message type.
178  */
179
180 #define MSG_CATMASK 0xf0
181 #define MSG_TYPEMASK 0x0f
182
183 /* --- Encrypted message packets --- *
184  *
185  * Messages of category @MSG_PACKET@ contain encrypted network packets.  The
186  * message content is a symmetric-encrypted block (see below).  Reception of
187  * a packet encrypted under a new key implicitly permits that key to be used
188  * to send further packets.
189  *
190  * The only packet type accepted is zero.
191  *
192  * Packets may be encrypted under any live keyset, but should use the most
193  * recent one.
194  */
195
196 #define MSG_PACKET 0x00
197
198 /* --- Key exchange packets --- */
199
200 #define MSG_KEYEXCH 0x10
201
202 #define KX_PRECHAL 0u
203 #define KX_COOKIE 1u
204 #define KX_CHAL 2u
205 #define KX_REPLY 3u
206 #define KX_SWITCH 4u
207 #define KX_SWITCHOK 5u
208 #define KX_NMSG 6u
209
210 /* --- Symmetric encryption and keysets --- *
211  *
212  * Packets consist of an 80-bit MAC, a 32-bit sequence number, and the
213  * encrypted payload.
214  *
215  * The plaintext is encrypted using Blowfish in CBC mode with ciphertext
216  * stealing (as described in [Schneier].  The initialization vector is
217  * selected randomly, and prepended to the actual ciphertext.
218  *
219  * The MAC is computed using the HMAC construction with RIPEMD160 over the
220  * sequence number and the ciphertext (with iV); the first 80 bits of the
221  * output are used.  (This is the minimum allowed by the draft FIPS for HMAC,
222  * and the recommended truncation.)
223  *
224  * A keyset consists of
225  *
226  *   * an integrity (MAC) key;
227  *   * a confidentiality (encryption) key; and
228  *   * a sequence numbering space
229  *
230  * in each direction.  The packets sent by a host encrypted under a
231  * particular keyset are assigned consecutive sequence numbers starting from
232  * zero.  The receiving host must ensure that it only accepts each packet at
233  * most once.  It should maintain a window of sequence numbers: packets with
234  * numbers beyond the end of the window are accepted and cause the window to
235  * be advanced; packets with numbers before the start of the window are
236  * rejected; packets with numbers which appear within the window are accepted
237  * only if the number has not been seen before.
238  *
239  * When a host sends a @KX_SWITCH@ or @KX_SWITCHOK@ message, it installs the
240  * newly-negotiated keyset in a `listen-only' state: it may not send a packet
241  * encrypted under the keyset until either it has received a @KX_SWITCH@ or
242  * @KX_SWITCHOK@ message, or a @MSG_PACKET@ encrypted under the keyset, from
243  * its peer.
244  */
245
246 /*----- Cipher selections -------------------------------------------------*/
247
248 #include <catacomb/blowfish.h>
249 #include <catacomb/blowfish-cbc.h>
250 #include <catacomb/blowfish-counter.h>
251 #include <catacomb/rmd160.h>
252 #include <catacomb/rmd160-hmac.h>
253
254 #define CIPHER (&blowfish_cbc)
255 #define MAC (&rmd160_hmac)
256
257 #define HASH_CTX rmd160_ctx
258 #define HASH_INIT rmd160_init
259 #define HASH rmd160_hash
260 #define HASH_STRING(c, s) HASH((c), s, sizeof(s))
261 #define HASH_DONE rmd160_done
262 #define HASHSZ RMD160_HASHSZ
263
264 #define MGF_CTX blowfish_counterctx
265 #define MGF_INIT blowfish_counterinit
266 #define MGF_CRYPT blowfish_counterencrypt
267
268 #define SEQSZ 4
269 #define IVSZ BLOWFISH_BLKSZ
270 #define MACSZ 10
271
272 /*----- Data structures ---------------------------------------------------*/
273
274 /* --- Socket addresses --- *
275  *
276  * A magic union of supported socket addresses.
277  */
278
279 typedef union addr {
280   struct sockaddr sa;
281   struct sockaddr_in sin;
282 } addr;
283
284 /* --- A symmetric keyset --- *
285  *
286  * A keyset contains a set of symmetric keys for encrypting and decrypting
287  * packets.  Keysets are stored in a list, sorted in reverse order of
288  * creation, so that the most recent keyset (the one most likely to be used)
289  * is first.
290  *
291  * Each keyset has a time limit and a data limit.  The keyset is destroyed
292  * when either it has existed for too long, or it has been used to encrypt
293  * too much data.  New key exchanges are triggered when keys are close to
294  * expiry.
295  */
296
297 typedef struct keyset {
298   struct keyset *next;                  /* Next active keyset in the list */
299   unsigned ref;                         /* Reference count for keyset */
300   time_t t_exp;                         /* Expiry time for this keyset */
301   unsigned long sz_exp;                 /* Data limit for the keyset */
302   T( unsigned seq; )                    /* Sequence number for tracing */
303   unsigned f;                           /* Various useful flags */
304   gcipher *cin, *cout;                  /* Keyset ciphers for encryption */
305   gmac *min, *mout;                     /* Keyset MACs for integrity */
306   uint32 oseq;                          /* Outbound sequence number */
307   uint32 iseq, iwin;                    /* Inbound sequence number */
308 } keyset;
309
310 #define KS_SEQWINSZ 32                  /* Bits in sequence number window */
311
312 #define KSF_LISTEN 1u                   /* Don't encrypt packets yet */
313 #define KSF_LINK 2u                     /* Key is in a linked list */
314
315 /* --- Key exchange --- *
316  *
317  * TrIPE uses the Wrestlers Protocol for its key exchange.  The Wrestlers
318  * Protocol has a number of desirable features (e.g., perfect forward
319  * secrecy, and zero-knowledge authentication) which make it attractive for
320  * use in TrIPE.  The Wrestlers Protocol was designed by Mark Wooding and
321  * Clive Jones.
322  */
323
324 #define KX_NCHAL 16u
325 #define KX_THRESH 4u
326
327 typedef struct kxchal {
328   struct keyexch *kx;                   /* Pointer back to key exchange */
329   mp *c;                                /* Responder's challenge */
330   mp *r;                                /* My reply to the challenge */
331   keyset *ks;                           /* Pointer to temporary keyset */
332   unsigned f;                           /* Various useful flags */
333   sel_timer t;                          /* Response timer for challenge */
334   octet hc[HASHSZ];                     /* Hash of his challenge */
335   mp *ck;                               /* The check value */
336   octet hswrq_in[HASHSZ];               /* Inbound switch request message */
337   octet hswok_in[HASHSZ];               /* Inbound switch confirmation */
338   octet hswrq_out[HASHSZ];              /* Outbound switch request message */
339   octet hswok_out[HASHSZ];              /* Outbound switch confirmation */
340 } kxchal;
341
342 typedef struct keyexch {
343   struct peer *p;                       /* Pointer back to the peer */
344   keyset **ks;                          /* Peer's list of keysets */
345   unsigned f;                           /* Various useful flags */
346   unsigned s;                           /* Current state in exchange */
347   sel_timer t;                          /* Timer for next exchange */
348   dh_pub kpub;                          /* Peer's public key */
349   time_t texp_kpub;                     /* Expiry time for public key */
350   mp *alpha;                            /* My temporary secret */
351   mp *c;                                /* My challenge */
352   mp *rx;                               /* The expected response */
353   unsigned nr;                          /* Number of extant responses */
354   time_t t_valid;                       /* When this exchange goes bad */
355   octet hc[HASHSZ];                     /* Hash of my challenge */
356   kxchal *r[KX_NCHAL];                  /* Array of challenges */
357 } keyexch;
358
359 #define KXF_TIMER 1u                    /* Waiting for a timer to go off */
360 #define KXF_DEAD 2u                     /* The key-exchanger isn't up */
361 #define KXF_PUBKEY 4u                   /* Key exchanger has a public key */
362
363 enum {
364   KXS_DEAD,                             /* Uninitialized state (magical) */
365   KXS_CHAL,                             /* Main answer-challenges state */
366   KXS_COMMIT,                           /* Committed: send switch request */
367   KXS_SWITCH                            /* Switched: send confirmation */
368 };
369
370 /* --- Tunnel structure --- *
371  *
372  * Used to maintain system-specific information about the tunnel interface.
373  */
374
375 typedef struct tunnel {
376 #if TUN_TYPE == TUN_UNET
377   sel_file f;                           /* Selector for Usernet device */
378   struct peer *p;                       /* Pointer to my peer */
379 #elif TUN_TYPE == TUN_BSD
380   sel_file f;                           /* Selector for tunnel device */
381   struct peer *p;                       /* Pointer to my peer */
382   unsigned n;                           /* Number of my tunnel device */
383 #else
384 #  error "No support for this tunnel type"
385 #endif
386 } tunnel;
387
388 /* --- Peer statistics --- *
389  *
390  * Contains various interesting and not-so-interesting statistics about a
391  * peer.  This is updated by various parts of the code.  The format of the
392  * structure isn't considered private, and @p_stats@ returns a pointer to the
393  * statistics block for a given peer.
394  */
395
396 typedef struct stats {
397   unsigned long sz_in, sz_out;          /* Size of all data in and out */
398   unsigned long sz_kxin, sz_kxout;      /* Size of key exchange messages */
399   unsigned long sz_ipin, sz_ipout;      /* Size of encapsulated IP packets */
400   time_t t_start, t_last;               /* Time peer created, last recv */
401   unsigned long n_reject;               /* Number of rejected packets */
402   unsigned long n_in, n_out;            /* Number of packets in and out */
403   unsigned long n_kxin, n_kxout;        /* Number of key exchange packets */
404   unsigned long n_ipin, n_ipout;        /* Number of encrypted packets */
405 } stats;
406
407 /* --- Peer structure --- *
408  *
409  * The main structure which glues everything else together.
410  */
411
412 typedef struct peer {
413   struct peer *next, *prev;             /* Links to next and previous */
414   char *name;                           /* Name of this peer */
415   tunnel t;                             /* Tunnel for local packets */
416   keyset *ks;                           /* List head for keysets */
417   buf b;                                /* Buffer for sending packets */
418   addr peer;                            /* Peer socket address */
419   size_t sasz;                          /* Socket address size */
420   stats st;                             /* Statistics */
421   keyexch kx;                           /* Key exchange protocol block */
422 } peer;
423
424 /* --- Admin structure --- */
425
426 #define OBUFSZ 16384u
427
428 typedef struct obuf {
429   struct obuf *next;                    /* Next buffer in list */
430   char *p_in, *p_out;                   /* Pointers into the buffer */
431   char buf[OBUFSZ];                     /* The actual buffer */
432 } obuf;
433
434 typedef struct admin {
435   struct admin *next, *prev;            /* Links to next and previous */
436   unsigned f;                           /* Various useful flags */
437 #ifndef NTRACE
438   unsigned seq;                         /* Sequence number for tracing */
439 #endif
440   char *pname;                          /* Peer name to create */
441   char *paddr;                          /* Address string to resolve */
442   obuf *o_head, *o_tail;                /* Output buffer list */
443   selbuf b;                             /* Line buffer for commands */
444   sel_file w;                           /* Selector for write buffering */
445   bres_client r;                        /* Background resolver task */
446   sel_timer t;                          /* Timer for resolver */
447   addr peer;                            /* Address to set */
448   size_t sasz;                          /* Size of the address */
449 } admin;
450
451 #define AF_DEAD 1u                      /* Destroy this admin block */
452 #define AF_LOCK 2u                      /* Don't destroy it yet */
453
454 /*----- Global variables --------------------------------------------------*/
455
456 extern sel_state sel;                   /* Global I/O event state */
457 extern dh_priv kpriv;                   /* Our private key */
458 extern mpmont mg;                       /* Montgomery context for DH group */
459 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ];
460
461 #ifndef NTRACE
462 extern const trace_opt tr_opts[];       /* Trace options array */
463 extern unsigned tr_flags;               /* Trace options flags */
464 #endif
465
466 /*----- Other macros ------------------------------------------------------*/
467
468 #define TIMER noise_timer(RAND_GLOBAL)
469
470 /*----- Key management ----------------------------------------------------*/
471
472 /* --- @km_interval@ --- *
473  *
474  * Arguments:   ---
475  *
476  * Returns:     Zero if OK, nonzero to force reloading of keys.
477  *
478  * Use:         Called on the interval timer to perform various useful jobs.
479  */
480
481 extern int km_interval(void);
482
483 /* --- @km_init@ --- *
484  *
485  * Arguments:   @const char *kr_priv@ = private keyring file
486  *              @const char *kr_pub@ = public keyring file
487  *              @const char *tag@ = tag to load
488  *
489  * Returns:     ---
490  *
491  * Use:         Initializes, and loads the private key.
492  */
493
494 extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
495                     const char */*tag*/);
496
497 /* --- @km_getpubkey@ --- *
498  *
499  * Arguments:   @const char *tag@ = public key tag to load
500  *              @dh_pub *kpub@ = where to put the public key
501  *              @time_t *t_exp@ = where to put the expiry time
502  *
503  * Returns:     Zero if OK, nonzero if it failed.
504  *
505  * Use:         Fetches a public key from the keyring.
506  */
507
508 extern int km_getpubkey(const char */*tag*/, dh_pub */*kpub*/,
509                         time_t */*t_exp*/);
510
511 /*----- Key exchange ------------------------------------------------------*/
512
513 /* --- @kx_start@ --- *
514  *
515  * Arguments:   @keyexch *kx@ = pointer to key exchange context
516  *
517  * Returns:     ---
518  *
519  * Use:         Stimulates a key exchange.  If a key exchage is in progress,
520  *              a new challenge is sent (unless the quiet timer forbids
521  *              this); if no exchange is in progress, one is commenced.
522  */
523
524 extern void kx_start(keyexch */*kx*/);
525
526 /* --- @kx_message@ --- *
527  *
528  * Arguments:   @keyexch *kx@ = pointer to key exchange context
529  *              @unsigned msg@ = the message code
530  *              @buf *b@ = pointer to buffer containing the packet
531  *
532  * Returns:     ---
533  *
534  * Use:         Reads a packet containing key exchange messages and handles
535  *              it.
536  */
537
538 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
539
540 /* --- @kx_free@ --- *
541  *
542  * Arguments:   @keyexch *kx@ = pointer to key exchange context
543  *
544  * Returns:     ---
545  *
546  * Use:         Frees everything in a key exchange context.
547  */
548
549 extern void kx_free(keyexch */*kx*/);
550
551 /* --- @kx_newkeys@ --- *
552  *
553  * Arguments:   @keyexch *kx@ = pointer to key exchange context
554  *
555  * Returns:     ---
556  *
557  * Use:         Informs the key exchange module that its keys may have
558  *              changed.  If fetching the new keys fails, the peer will be
559  *              destroyed, we log messages and struggle along with the old
560  *              keys.
561  */
562
563 extern void kx_newkeys(keyexch */*kx*/);
564
565 /* --- @kx_init@ --- *
566  *
567  * Arguments:   @keyexch *kx@ = pointer to key exchange context
568  *              @peer *p@ = pointer to peer context
569  *              @keyset **ks@ = pointer to keyset list
570  *
571  * Returns:     Zero if OK, nonzero if it failed.
572  *
573  * Use:         Initializes a key exchange module.  The module currently
574  *              contains no keys, and will attempt to initiate a key
575  *              exchange.
576  */
577
578 extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/);
579
580 /*----- Keysets and symmetric cryptography --------------------------------*/
581
582 /* --- @ks_drop@ --- *
583  *
584  * Arguments:   @keyset *ks@ = pointer to a keyset
585  *
586  * Returns:     ---
587  *
588  * Use:         Decrements a keyset's reference counter.  If the counter hits
589  *              zero, the keyset is freed.
590  */
591
592 extern void ks_drop(keyset */*ks*/);
593
594 /* --- @ks_gen@ --- *
595  *
596  * Arguments:   @const void *k@ = pointer to key material
597  *              @size_t x, y, z@ = offsets into key material (see below)
598  *
599  * Returns:     A pointer to the new keyset.
600  *
601  * Use:         Derives a new keyset from the given key material.  The
602  *              offsets @x@, @y@ and @z@ separate the key material into three
603  *              parts.  Between the @k@ and @k + x@ is `my' contribution to
604  *              the key material; between @k + x@ and @k + y@ is `your'
605  *              contribution; and between @k + y@ and @k + z@ is a shared
606  *              value we made together.  These are used to construct two
607  *              pairs of symmetric keys.  Each pair consists of an encryption
608  *              key and a message authentication key.  One pair is used for
609  *              outgoing messages, the other for incoming messages.
610  *
611  *              The new key is marked so that it won't be selected for output
612  *              by @ksl_encrypt@.  You can still encrypt data with it by
613  *              calling @ks_encrypt@ directly.
614  */
615
616 extern keyset *ks_gen(const void */*k*/,
617                       size_t /*x*/, size_t /*y*/, size_t /*z*/);
618
619 /* --- @ks_tregen@ --- *
620  *
621  * Arguments:   @keyset *ks@ = pointer to a keyset
622  *
623  * Returns:     The time at which moves ought to be made to replace this key.
624  */
625
626 extern time_t ks_tregen(keyset */*ks*/);
627
628 /* --- @ks_activate@ --- *
629  *
630  * Arguments:   @keyset *ks@ = pointer to a keyset
631  *
632  * Returns:     ---
633  *
634  * Use:         Activates a keyset, so that it can be used for encrypting
635  *              outgoing messages.
636  */
637
638 extern void ks_activate(keyset */*ks*/);
639
640 /* --- @ks_encrypt@ --- *
641  *
642  * Arguments:   @keyset *ks@ = pointer to a keyset
643  *              @buf *b@ = pointer to input buffer
644  *              @buf *bb@ = pointer to output buffer
645  *
646  * Returns:     Zero if OK, nonzero if the key needs replacing.  If the
647  *              encryption failed, the output buffer is broken and zero is
648  *              returned.
649  *
650  * Use:         Encrypts a block of data using the key.  Note that the `key
651  *              ought to be replaced' notification is only ever given once
652  *              for each key.  Also note that this call forces a keyset to be
653  *              used even if it's marked as not for data output.
654  */
655
656 extern int ks_encrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
657
658 /* --- @ks_decrypt@ --- *
659  *
660  * Arguments:   @keyset *ks@ = pointer to a keyset
661  *              @buf *b@ = pointer to an input buffer
662  *              @buf *bb@ = pointer to an output buffer
663  *
664  * Returns:     Zero on success, or nonzero if there was some problem.
665  *
666  * Use:         Attempts to decrypt a message using a given key.  Note that
667  *              requesting decryption with a key directly won't clear a
668  *              marking that it's not for encryption.
669  */
670
671 extern int ks_decrypt(keyset */*ks*/, buf */*b*/, buf */*bb*/);
672
673 /* --- @ksl_free@ --- *
674  *
675  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
676  *
677  * Returns:     ---
678  *
679  * Use:         Frees (releases references to) all of the keys in a keyset.
680  */
681
682 extern void ksl_free(keyset **/*ksroot*/);
683
684 /* --- @ksl_link@ --- *
685  *
686  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
687  *              @keyset *ks@ = pointer to a keyset
688  *
689  * Returns:     ---
690  *
691  * Use:         Links a keyset into a list.  A keyset can only be on one list
692  *              at a time.  Bad things happen otherwise.
693  */
694
695 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
696
697 /* --- @ksl_prune@ --- *
698  *
699  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
700  *
701  * Returns:     ---
702  *
703  * Use:         Prunes the keyset list by removing keys which mustn't be used
704  *              any more.
705  */
706
707 extern void ksl_prune(keyset **/*ksroot*/);
708
709 /* --- @ksl_encrypt@ --- *
710  *
711  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
712  *              @buf *b@ = pointer to input buffer
713  *              @buf *bb@ = pointer to output buffer
714  *
715  * Returns:     Nonzero if a new key is needed.
716  *
717  * Use:         Encrypts a packet.
718  */
719
720 extern int ksl_encrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
721
722 /* --- @ksl_decrypt@ --- *
723  *
724  * Arguments:   @keyset **ksroot@ = pointer to keyset list head
725  *              @buf *b@ = pointer to input buffer
726  *              @buf *bb@ = pointer to output buffer
727  *
728  * Returns:     Nonzero if the packet couldn't be decrypted.
729  *
730  * Use:         Decrypts a packet.
731  */
732
733 extern int ksl_decrypt(keyset **/*ksroot*/, buf */*b*/, buf */*bb*/);
734
735 /*----- Administration interface ------------------------------------------*/
736
737 /* --- @a_warn@ --- *
738  *
739  * Arguments:   @const char *fmt@ = pointer to format string
740  *              @...@ = other arguments
741  *
742  * Returns:     ---
743  *
744  * Use:         Informs all admin connections of a warning.
745  */
746
747 extern void a_warn(const char */*fmt*/, ...);
748
749 /* --- @a_create@ --- *
750  *
751  * Arguments:   @int fd_in, fd_out@ = file descriptors to use
752  *
753  * Returns:     ---
754  *
755  * Use:         Creates a new admin connection.
756  */
757
758 extern void a_create(int /*fd_in*/, int /*fd_out*/);
759
760 /* --- @a_quit@ --- *
761  *
762  * Arguments:   ---
763  *
764  * Returns:     ---
765  *
766  * Use:         Shuts things down nicely.
767  */
768
769 extern void a_quit(void);
770
771 /* --- @a_daemon@ --- *
772  *
773  * Arguments:   ---
774  *
775  * Returns:     ---
776  *
777  * Use:         Informs the admin module that it's a daemon.
778  */
779
780 extern void a_daemon(void);
781
782 /* --- @a_init@ --- *
783  *
784  * Arguments:   @const char *sock@ = socket name to create
785  *
786  * Returns:     ---
787  *
788  * Use:         Creates the admin listening socket.
789  */
790
791 extern void a_init(const char */*sock*/);
792
793 /*----- Peer management ---------------------------------------------------*/
794
795 /* --- @p_txstart@ --- *
796  *
797  * Arguments:   @peer *p@ = pointer to peer block
798  *              @unsigned msg@ = message type code
799  *
800  * Returns:     A pointer to a buffer to write to.
801  *
802  * Use:         Starts sending to a peer.  Only one send can happen at a
803  *              time.
804  */
805
806 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
807
808 /* --- @p_txend@ --- *
809  *
810  * Arguments:   @peer *p@ = pointer to peer block
811  *
812  * Returns:     ---
813  *
814  * Use:         Sends a packet to the peer.
815  */
816
817 extern void p_txend(peer */*p*/);
818
819 /* --- @p_tun@ --- *
820  *
821  * Arguments:   @peer *p@ = pointer to peer block
822  *              @buf *b@ = buffer containing incoming packet
823  *
824  * Returns:     ---
825  *
826  * Use:         Handles a packet which needs to be sent to a peer.
827  */
828
829 extern void p_tun(peer */*p*/, buf */*b*/);
830
831 /* --- @p_interval@ --- *
832  *
833  * Arguments:   ---
834  *
835  * Returns:     ---
836  *
837  * Use:         Called periodically to do tidying.
838  */
839
840 extern void p_interval(void);
841
842 /* --- @p_stats@ --- *
843  *
844  * Arguments:   @peer *p@ = pointer to a peer block
845  *
846  * Returns:     A pointer to the peer's statistics.
847  */
848
849 extern stats *p_stats(peer */*p*/);
850
851 /* --- @p_ifname@ --- *
852  *
853  * Arguments:   @peer *p@ = pointer to a peer block
854  *
855  * Returns:     A pointer to the peer's interface name.
856  */
857
858 extern const char *p_ifname(peer */*p*/);
859
860 /* --- @p_addr@ --- *
861  *
862  * Arguments:   @peer *p@ = pointer to a peer block
863  *
864  * Returns:     A pointer to the peer's address.
865  */
866
867 extern const addr *p_addr(peer */*p*/);
868
869 /* --- @p_init@ --- *
870  *
871  * Arguments:   @unsigned port@ = port number to listen to
872  *
873  * Returns:     ---
874  *
875  * Use:         Initializes the peer system; creates the socket.
876  */
877
878 extern void p_init(unsigned /*port*/);
879
880 /* --- @p_port@ --- *
881  *
882  * Arguments:   ---
883  *
884  * Returns:     Port number used for socket.
885  */
886
887 unsigned p_port(void);
888
889 /* --- @p_create@ --- *
890  *
891  * Arguments:   @const char *name@ = name for this peer
892  *              @struct sockaddr *sa@ = socket address of peer
893  *              @size_t sz@ = size of socket address
894  *
895  * Returns:     Pointer to the peer block, or null if it failed.
896  *
897  * Use:         Creates a new named peer block.  No peer is actually attached
898  *              by this point.
899  */
900
901 extern peer *p_create(const char */*name*/,
902                       struct sockaddr */*sa*/, size_t /*sz*/);
903
904 /* --- @p_name@ --- *
905  *
906  * Arguments:   @peer *p@ = pointer to a peer block
907  *
908  * Returns:     A pointer to the peer's name.
909  */
910
911 extern const char *p_name(peer */*p*/);
912
913 /* --- @p_find@ --- *
914  *
915  * Arguments:   @const char *name@ = name to look up
916  *
917  * Returns:     Pointer to the peer block, or null if not found.
918  *
919  * Use:         Finds a peer by name.
920  */
921
922 extern peer *p_find(const char */*name*/);
923
924 /* --- @p_destroy@ --- *
925  *
926  * Arguments:   @peer *p@ = pointer to a peer
927  *
928  * Returns:     ---
929  *
930  * Use:         Destroys a peer.
931  */
932
933 extern void p_destroy(peer */*p*/);
934
935 /* --- @p_first@, @p_next@ --- *
936  *
937  * Arguments:   @peer *p@ = a peer block
938  *
939  * Returns:     @peer_first@ returns the first peer in some ordering;
940  *              @peer_next@ returns the peer following a given one in the
941  *              same ordering.  Null is returned for the end of the list.
942  */
943
944 extern peer *p_first(void);
945 extern peer *p_next(peer */*p*/);
946
947 /*----- Tunnel interface --------------------------------------------------*/
948
949 /* --- @tun_init@ --- *
950  *
951  * Arguments:   ---
952  *
953  * Returns:     ---
954  *
955  * Use:         Initializes the tunneling system.  Maybe this will require
956  *              opening file descriptors or something.
957  */
958
959 extern void tun_init(void);
960
961 /* --- @tun_create@ --- *
962  *
963  * Arguments:   @tunnel *t@ = pointer to tunnel block
964  *              @peer *p@ = pointer to peer block
965  *
966  * Returns:     Zero if it worked, nonzero on failure.
967  *
968  * Use:         Initializes a new tunnel.
969  */
970
971 extern int tun_create(tunnel */*t*/, peer */*p*/);
972
973 /* --- @tun_ifname@ --- *
974  *
975  * Arguments:   @tunnel *t@ = pointer to tunnel block
976  *
977  * Returns:     A pointer to the tunnel's interface name.
978  */
979
980 extern const char *tun_ifname(tunnel */*t*/);
981
982 /* --- @tun_inject@ --- *
983  *
984  * Arguments:   @tunnel *t@ = pointer to tunnel block
985  *              @buf *b@ = buffer to send
986  *
987  * Returns:     ---
988  *
989  * Use:         Injects a packet into the local network stack.
990  */
991
992 extern void tun_inject(tunnel */*t*/, buf */*b*/);
993
994 /* --- @tun_destroy@ --- *
995  *
996  * Arguments:   @tunnel *t@ = pointer to tunnel block
997  *
998  * Returns:     ---
999  *
1000  * Use:         Destroys a tunnel.
1001  */
1002
1003 extern void tun_destroy(tunnel */*t*/);
1004
1005 /*----- Other handy utilities ---------------------------------------------*/
1006
1007 /* --- @mpstr@ --- *
1008  *
1009  * Arguments:   @mp *m@ = a multiprecision integer
1010  *
1011  * Returns:     A pointer to the integer's textual representation.
1012  *
1013  * Use:         Converts a multiprecision integer to a string.  Corrupts
1014  *              @buf_t@.
1015  */
1016
1017 extern const char *mpstr(mp */*m*/);
1018
1019 /* --- @timestr@ --- *
1020  *
1021  * Arguments:   @time_t t@ = a time to convert
1022  *
1023  * Returns:     A pointer to a textual representation of the time.
1024  *
1025  * Use:         Converts a time to a textual representation.  Corrupts
1026  *              @buf_t@.
1027  */
1028
1029 extern const char *timestr(time_t /*t*/);
1030
1031 /*----- That's all, folks -------------------------------------------------*/
1032
1033 #ifdef __cplusplus
1034   }
1035 #endif
1036
1037 #endif