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