chiark / gitweb /
server/keyexch.c: Lower the validity time for a challenge.
[tripe] / server / tripe.h
CommitLineData
410c8acf 1/* -*-c-*-
410c8acf 2 *
3 * Main header file for TrIPE
4 *
5 * (c) 2001 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
410c8acf 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
e04c2d50 16 *
410c8acf 17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
e04c2d50 21 *
410c8acf 22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
410c8acf 27#ifndef TRIPE_H
28#define TRIPE_H
29
30#ifdef __cplusplus
31 extern "C" {
32#endif
33
34/*----- Header files ------------------------------------------------------*/
35
73189848 36#include "config.h"
37
410c8acf 38#include <assert.h>
39#include <ctype.h>
40#include <errno.h>
b9066fbb 41#include <limits.h>
410c8acf 42#include <signal.h>
43#include <stdarg.h>
44#include <stddef.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <time.h>
49
50#include <sys/types.h>
51#include <sys/time.h>
52#include <unistd.h>
53#include <fcntl.h>
54#include <sys/stat.h>
55
56#include <sys/socket.h>
57#include <sys/un.h>
58#include <netinet/in.h>
59#include <arpa/inet.h>
60#include <netdb.h>
61
62#include <pwd.h>
63#include <grp.h>
64
65#include <mLib/alloc.h>
66#include <mLib/arena.h>
37941236 67#include <mLib/base64.h>
410c8acf 68#include <mLib/bres.h>
19dd2531 69#include <mLib/daemonize.h>
410c8acf 70#include <mLib/dstr.h>
71#include <mLib/env.h>
72#include <mLib/fdflags.h>
73#include <mLib/fwatch.h>
c8e02c8a 74#include <mLib/hash.h>
165efde7 75#include <mLib/macros.h>
410c8acf 76#include <mLib/mdwopt.h>
77#include <mLib/quis.h>
78#include <mLib/report.h>
79#include <mLib/sel.h>
80#include <mLib/selbuf.h>
81#include <mLib/sig.h>
82#include <mLib/str.h>
83#include <mLib/sub.h>
84#include <mLib/trace.h>
0ba8de86 85#include <mLib/tv.h>
19dd2531 86#include <mLib/versioncmp.h>
410c8acf 87
165db1a8 88#include <catacomb/buf.h>
89
410c8acf 90#include <catacomb/gcipher.h>
91#include <catacomb/gmac.h>
92#include <catacomb/grand.h>
93#include <catacomb/key.h>
94#include <catacomb/paranoia.h>
95
410c8acf 96#include <catacomb/noise.h>
97#include <catacomb/rand.h>
410c8acf 98
99#include <catacomb/mp.h>
410c8acf 100#include <catacomb/mprand.h>
101#include <catacomb/dh.h>
52c03a2a 102#include <catacomb/ec.h>
103#include <catacomb/ec-keys.h>
104#include <catacomb/group.h>
410c8acf 105
78698994 106#include "protocol.h"
10f681b1 107#include "slip.h"
410c8acf 108#include "util.h"
109
110#undef sun
111
112/*----- Magic numbers -----------------------------------------------------*/
113
410c8acf 114/* --- Trace flags --- */
115
116#define T_TUNNEL 1u
117#define T_PEER 2u
118#define T_PACKET 4u
119#define T_ADMIN 8u
120#define T_CRYPTO 16u
121#define T_KEYSET 32u
122#define T_KEYEXCH 64u
123#define T_KEYMGMT 128u
37941236 124#define T_CHAL 256u
410c8acf 125
37941236 126#define T_ALL 511u
410c8acf 127
128/* --- Units --- */
129
130#define SEC(n) (n##u)
131#define MIN(n) (n##u * 60u)
132#define MEG(n) (n##ul * 1024ul * 1024ul)
133
134/* --- Other things --- */
135
136#define PKBUFSZ 65536
137
832a2ab6 138/*----- Cipher selections -------------------------------------------------*/
139
b5c45da1 140typedef struct algswitch {
141 const gccipher *c; /* Symmetric encryption scheme */
142 const gccipher *mgf; /* Mask-generation function */
143 const gchash *h; /* Hash function */
144 const gcmac *m; /* Message authentication code */
145 size_t hashsz; /* Hash output size */
146 size_t tagsz; /* Length to truncate MAC tags */
147 size_t cksz, mksz; /* Key lengths for @c@ and @m@ */
148} algswitch;
832a2ab6 149
b5c45da1 150extern algswitch algs;
832a2ab6 151
b5c45da1 152#define MAXHASHSZ 64 /* Largest possible hash size */
832a2ab6 153
b5c45da1 154#define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
410c8acf 155
aeeb5611 156/*----- Data structures ---------------------------------------------------*/
410c8acf 157
158/* --- Socket addresses --- *
159 *
160 * A magic union of supported socket addresses.
161 */
162
163typedef union addr {
164 struct sockaddr sa;
165 struct sockaddr_in sin;
166} addr;
167
c8e02c8a
MW
168/* --- Mapping keyed on addresses --- */
169
170typedef struct addrmap {
171 hash_table t;
172 size_t load;
173} addrmap;
174
175typedef struct addrmap_base {
176 hash_base b;
177 addr a;
178} addrmap_base;
179
37941236 180/* --- Sequence number checking --- */
181
182typedef struct seqwin {
183 uint32 seq; /* First acceptable input sequence */
184 uint32 win; /* Window of acceptable numbers */
185} seqwin;
186
187#define SEQ_WINSZ 32 /* Bits in sequence number window */
188
410c8acf 189/* --- A symmetric keyset --- *
190 *
191 * A keyset contains a set of symmetric keys for encrypting and decrypting
192 * packets. Keysets are stored in a list, sorted in reverse order of
193 * creation, so that the most recent keyset (the one most likely to be used)
194 * is first.
195 *
196 * Each keyset has a time limit and a data limit. The keyset is destroyed
197 * when either it has existed for too long, or it has been used to encrypt
198 * too much data. New key exchanges are triggered when keys are close to
199 * expiry.
200 */
201
202typedef struct keyset {
203 struct keyset *next; /* Next active keyset in the list */
832a2ab6 204 unsigned ref; /* Reference count for keyset */
9466fafa 205 struct peer *p; /* Pointer to peer structure */
410c8acf 206 time_t t_exp; /* Expiry time for this keyset */
207 unsigned long sz_exp; /* Data limit for the keyset */
832a2ab6 208 T( unsigned seq; ) /* Sequence number for tracing */
209 unsigned f; /* Various useful flags */
e04c2d50 210 gcipher *cin, *cout; /* Keyset ciphers for encryption */
b5c45da1 211 size_t tagsz; /* Length to truncate MAC tags */
e04c2d50 212 gmac *min, *mout; /* Keyset MACs for integrity */
1484d822 213 uint32 oseq; /* Outbound sequence number */
37941236 214 seqwin iseq; /* Inbound sequence number */
410c8acf 215} keyset;
216
832a2ab6 217#define KSF_LISTEN 1u /* Don't encrypt packets yet */
218#define KSF_LINK 2u /* Key is in a linked list */
219
410c8acf 220/* --- Key exchange --- *
221 *
222 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
223 * Protocol has a number of desirable features (e.g., perfect forward
224 * secrecy, and zero-knowledge authentication) which make it attractive for
225 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
226 * Clive Jones.
227 */
228
832a2ab6 229#define KX_NCHAL 16u
832a2ab6 230
231typedef struct kxchal {
232 struct keyexch *kx; /* Pointer back to key exchange */
52c03a2a 233 ge *c; /* Responder's challenge */
234 ge *r; /* My reply to the challenge */
832a2ab6 235 keyset *ks; /* Pointer to temporary keyset */
236 unsigned f; /* Various useful flags */
237 sel_timer t; /* Response timer for challenge */
b5c45da1 238 octet hc[MAXHASHSZ]; /* Hash of his challenge */
de7bd20b 239 octet ck[MAXHASHSZ]; /* His magical check value */
b5c45da1 240 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
241 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
242 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
243 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
832a2ab6 244} kxchal;
245
410c8acf 246typedef struct keyexch {
410c8acf 247 struct peer *p; /* Pointer back to the peer */
832a2ab6 248 keyset **ks; /* Peer's list of keysets */
410c8acf 249 unsigned f; /* Various useful flags */
832a2ab6 250 unsigned s; /* Current state in exchange */
410c8acf 251 sel_timer t; /* Timer for next exchange */
52c03a2a 252 ge *kpub; /* Peer's public key */
00e64b67 253 time_t texp_kpub; /* Expiry time for public key */
832a2ab6 254 mp *alpha; /* My temporary secret */
52c03a2a 255 ge *c; /* My challenge */
256 ge *rx; /* The expected response */
832a2ab6 257 unsigned nr; /* Number of extant responses */
410c8acf 258 time_t t_valid; /* When this exchange goes bad */
b5c45da1 259 octet hc[MAXHASHSZ]; /* Hash of my challenge */
832a2ab6 260 kxchal *r[KX_NCHAL]; /* Array of challenges */
410c8acf 261} keyexch;
262
263#define KXF_TIMER 1u /* Waiting for a timer to go off */
00e64b67 264#define KXF_DEAD 2u /* The key-exchanger isn't up */
265#define KXF_PUBKEY 4u /* Key exchanger has a public key */
010e6f63 266#define KXF_CORK 8u /* Don't send anything yet */
832a2ab6 267
268enum {
269 KXS_DEAD, /* Uninitialized state (magical) */
270 KXS_CHAL, /* Main answer-challenges state */
271 KXS_COMMIT, /* Committed: send switch request */
272 KXS_SWITCH /* Switched: send confirmation */
273};
410c8acf 274
275/* --- Tunnel structure --- *
276 *
277 * Used to maintain system-specific information about the tunnel interface.
278 */
279
42da2a58 280typedef struct tunnel tunnel;
281struct peer;
110d564e 282
42da2a58 283typedef struct tunnel_ops {
284 const char *name; /* Name of this tunnel driver */
285 void (*init)(void); /* Initializes the system */
72917fe7
MW
286 tunnel *(*create)(struct peer */*p*/, char **/*ifn*/);
287 /* Initializes a new tunnel */
288 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
289 /* Notifies ifname change */
42da2a58 290 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
291 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
292} tunnel_ops;
b9066fbb 293
42da2a58 294#ifndef TUN_INTERNALS
295struct tunnel { const tunnel_ops *ops; };
410c8acf 296#endif
410c8acf 297
832a2ab6 298/* --- Peer statistics --- *
299 *
300 * Contains various interesting and not-so-interesting statistics about a
301 * peer. This is updated by various parts of the code. The format of the
302 * structure isn't considered private, and @p_stats@ returns a pointer to the
303 * statistics block for a given peer.
304 */
305
306typedef struct stats {
307 unsigned long sz_in, sz_out; /* Size of all data in and out */
308 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
309 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
3cdc3f3a 310 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
832a2ab6 311 unsigned long n_reject; /* Number of rejected packets */
312 unsigned long n_in, n_out; /* Number of packets in and out */
313 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
314 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
315} stats;
316
410c8acf 317/* --- Peer structure --- *
318 *
319 * The main structure which glues everything else together.
320 */
321
0ba8de86 322typedef struct peerspec {
323 char *name; /* Peer's name */
324 const tunnel_ops *tops; /* Tunnel operations */
325 unsigned long t_ka; /* Keep alive interval */
326 addr sa; /* Socket address to speak to */
327 size_t sasz; /* Socket address size */
010e6f63 328 unsigned kxf; /* Key exchange flags to set */
0ba8de86 329} peerspec;
330
c8e02c8a
MW
331typedef struct peer_byname {
332 sym_base _b;
333 struct peer *p;
334} peer_byname;
335
336typedef struct peer_byaddr {
337 addrmap_base _b;
338 struct peer *p;
339} peer_byaddr;
340
410c8acf 341typedef struct peer {
c8e02c8a
MW
342 peer_byname *byname; /* Lookup-by-name block */
343 peer_byaddr *byaddr; /* Lookup-by-address block */
0ba8de86 344 struct ping *pings; /* Pings we're waiting for */
345 peerspec spec; /* Specifications for this peer */
42da2a58 346 tunnel *t; /* Tunnel for local packets */
64cf2223 347 char *ifname; /* Interface name for tunnel */
410c8acf 348 keyset *ks; /* List head for keysets */
410c8acf 349 buf b; /* Buffer for sending packets */
832a2ab6 350 stats st; /* Statistics */
351 keyexch kx; /* Key exchange protocol block */
0ba8de86 352 sel_timer tka; /* Timer for keepalives */
410c8acf 353} peer;
354
c8e02c8a
MW
355typedef struct peer_iter { sym_iter i; } peer_iter;
356
0ba8de86 357typedef struct ping {
358 struct ping *next, *prev; /* Links to next and previous */
359 peer *p; /* Peer so we can free it */
360 unsigned msg; /* Kind of response expected */
361 uint32 id; /* Id so we can recognize response */
362 octet magic[32]; /* Some random data */
363 sel_timer t; /* Timeout for ping */
364 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
365 void *arg; /* Argument for callback */
366} ping;
367
368enum {
369 PING_NONOTIFY = -1,
370 PING_OK = 0,
371 PING_TIMEOUT,
372 PING_PEERDIED,
373 PING_MAX
374};
375
410c8acf 376/* --- Admin structure --- */
377
fd3cf232 378#define OBUFSZ 16384u
379
380typedef struct obuf {
381 struct obuf *next; /* Next buffer in list */
382 char *p_in, *p_out; /* Pointers into the buffer */
383 char buf[OBUFSZ]; /* The actual buffer */
384} obuf;
385
de014da6 386typedef struct oqueue {
387 obuf *hd, *tl; /* Head and tail pointers */
388} oqueue;
389
390struct admin;
391
392typedef struct admin_bgop {
393 struct admin_bgop *next, *prev; /* Links to next and previous */
394 struct admin *a; /* Owner job */
395 char *tag; /* Tag string for messages */
396 void (*cancel)(struct admin_bgop *); /* Destructor function */
397} admin_bgop;
398
37941236 399typedef struct admin_resop {
de014da6 400 admin_bgop bg; /* Background operation header */
37941236 401 char *addr; /* Hostname to be resolved */
de014da6 402 bres_client r; /* Background resolver task */
403 sel_timer t; /* Timer for resolver */
37941236 404 addr sa; /* Socket address */
405 size_t sasz; /* Socket address size */
406 void (*func)(struct admin_resop *, int); /* Handler */
407} admin_resop;
408
409enum { ARES_OK, ARES_FAIL };
410
411typedef struct admin_addop {
412 admin_resop r; /* Name resolution header */
413 peerspec peer; /* Peer pending creation */
de014da6 414} admin_addop;
415
37941236 416typedef struct admin_greetop {
417 admin_resop r; /* Name resolution header */
418 void *c; /* Challenge block */
419 size_t sz; /* Length of challenge */
420} admin_greetop;
421
de014da6 422typedef struct admin_pingop {
423 admin_bgop bg; /* Background operation header */
424 ping ping; /* Ping pending response */
425 struct timeval pingtime; /* Time last ping was sent */
be6a1b7a
MW
426} admin_pingop;
427
428typedef struct admin_service {
429 sym_base _b; /* Hash table base structure */
430 char *version; /* The provided version */
431 struct admin *prov; /* Which client provides me */
432 struct admin_service *next, *prev; /* Client's list of services */
433} admin_service;
de014da6 434
5d46c0f8
MW
435typedef struct admin_svcop {
436 admin_bgop bg; /* Background operation header */
437 struct admin *prov; /* Client servicing this job */
cc921fba 438 unsigned index; /* This job's index */
5d46c0f8
MW
439 struct admin_svcop *next, *prev; /* Links for provider's jobs */
440} admin_svcop;
441
442typedef struct admin_jobentry {
443 unsigned short seq; /* Zero if unused */
444 union {
445 admin_svcop *op; /* Operation, if slot in use, ... */
446 uint32 next; /* ... or index of next free slot */
447 } u;
448} admin_jobentry;
449
450typedef struct admin_jobtable {
451 uint32 n, sz; /* Used slots and table size */
452 admin_svcop *active; /* List of active jobs */
453 uint32 free; /* Index of first free slot */
454 admin_jobentry *v; /* And the big array of entries */
455} admin_jobtable;
456
410c8acf 457typedef struct admin {
458 struct admin *next, *prev; /* Links to next and previous */
fd3cf232 459 unsigned f; /* Various useful flags */
060ca767 460 unsigned ref; /* Reference counter */
410c8acf 461#ifndef NTRACE
462 unsigned seq; /* Sequence number for tracing */
463#endif
de014da6 464 oqueue out; /* Output buffer list */
465 oqueue delay; /* Delayed output buffer list */
466 admin_bgop *bg; /* Backgrounded operations */
be6a1b7a 467 admin_service *svcs; /* Which services I provide */
5d46c0f8 468 admin_jobtable j; /* Table of outstanding jobs */
fd3cf232 469 selbuf b; /* Line buffer for commands */
470 sel_file w; /* Selector for write buffering */
410c8acf 471} admin;
472
fd3cf232 473#define AF_DEAD 1u /* Destroy this admin block */
060ca767 474#define AF_CLOSE 2u /* Client closed connection */
3cdc3f3a 475#define AF_NOTE 4u /* Catch notifications */
de014da6 476#define AF_WARN 8u /* Catch warning messages */
3cdc3f3a 477#ifndef NTRACE
de014da6 478 #define AF_TRACE 16u /* Catch tracing */
3cdc3f3a 479#endif
46dde080 480#define AF_FOREGROUND 32u /* Quit server when client closes */
3cdc3f3a 481
482#ifndef NTRACE
483# define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
484#else
485# define AF_ALLMSGS (AF_NOTE | AF_WARN)
486#endif
fd3cf232 487
410c8acf 488/*----- Global variables --------------------------------------------------*/
489
490extern sel_state sel; /* Global I/O event state */
52c03a2a 491extern group *gg; /* The group we work in */
de7bd20b 492extern size_t indexsz; /* Size of exponent for the group */
52c03a2a 493extern mp *kpriv; /* Our private key */
9317aa92 494extern ge *kpub; /* Our public key */
a4b808b0 495extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
42da2a58 496extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
497extern const tunnel_ops *tun_default; /* Default tunnel to use */
410c8acf 498
499#ifndef NTRACE
500extern const trace_opt tr_opts[]; /* Trace options array */
501extern unsigned tr_flags; /* Trace options flags */
502#endif
503
8d0c7a83 504/*----- Other macros ------------------------------------------------------*/
505
506#define TIMER noise_timer(RAND_GLOBAL)
507
410c8acf 508/*----- Key management ----------------------------------------------------*/
509
de014da6 510/* --- @km_reload@ --- *
410c8acf 511 *
512 * Arguments: ---
513 *
514 * Returns: Zero if OK, nonzero to force reloading of keys.
515 *
de014da6 516 * Use: Checks the keyrings to see if they need reloading.
410c8acf 517 */
518
de014da6 519extern int km_reload(void);
410c8acf 520
521/* --- @km_init@ --- *
522 *
523 * Arguments: @const char *kr_priv@ = private keyring file
524 * @const char *kr_pub@ = public keyring file
525 * @const char *tag@ = tag to load
526 *
527 * Returns: ---
528 *
529 * Use: Initializes, and loads the private key.
530 */
531
532extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/,
533 const char */*tag*/);
534
535/* --- @km_getpubkey@ --- *
536 *
537 * Arguments: @const char *tag@ = public key tag to load
52c03a2a 538 * @ge *kpub@ = where to put the public key
00e64b67 539 * @time_t *t_exp@ = where to put the expiry time
410c8acf 540 *
541 * Returns: Zero if OK, nonzero if it failed.
542 *
543 * Use: Fetches a public key from the keyring.
544 */
545
52c03a2a 546extern int km_getpubkey(const char */*tag*/, ge */*kpub*/,
00e64b67 547 time_t */*t_exp*/);
410c8acf 548
549/*----- Key exchange ------------------------------------------------------*/
550
551/* --- @kx_start@ --- *
552 *
553 * Arguments: @keyexch *kx@ = pointer to key exchange context
de014da6 554 * @int forcep@ = nonzero to ignore the quiet timer
410c8acf 555 *
556 * Returns: ---
557 *
558 * Use: Stimulates a key exchange. If a key exchage is in progress,
559 * a new challenge is sent (unless the quiet timer forbids
560 * this); if no exchange is in progress, one is commenced.
561 */
562
de014da6 563extern void kx_start(keyexch */*kx*/, int /*forcep*/);
410c8acf 564
832a2ab6 565/* --- @kx_message@ --- *
410c8acf 566 *
567 * Arguments: @keyexch *kx@ = pointer to key exchange context
832a2ab6 568 * @unsigned msg@ = the message code
569 * @buf *b@ = pointer to buffer containing the packet
410c8acf 570 *
571 * Returns: ---
572 *
832a2ab6 573 * Use: Reads a packet containing key exchange messages and handles
574 * it.
410c8acf 575 */
576
832a2ab6 577extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
410c8acf 578
579/* --- @kx_free@ --- *
580 *
581 * Arguments: @keyexch *kx@ = pointer to key exchange context
582 *
583 * Returns: ---
584 *
585 * Use: Frees everything in a key exchange context.
586 */
587
588extern void kx_free(keyexch */*kx*/);
589
590/* --- @kx_newkeys@ --- *
591 *
592 * Arguments: @keyexch *kx@ = pointer to key exchange context
593 *
594 * Returns: ---
595 *
596 * Use: Informs the key exchange module that its keys may have
597 * changed. If fetching the new keys fails, the peer will be
598 * destroyed, we log messages and struggle along with the old
599 * keys.
600 */
601
602extern void kx_newkeys(keyexch */*kx*/);
603
604/* --- @kx_init@ --- *
605 *
606 * Arguments: @keyexch *kx@ = pointer to key exchange context
607 * @peer *p@ = pointer to peer context
608 * @keyset **ks@ = pointer to keyset list
010e6f63 609 * @unsigned f@ = various useful flags
410c8acf 610 *
611 * Returns: Zero if OK, nonzero if it failed.
612 *
613 * Use: Initializes a key exchange module. The module currently
614 * contains no keys, and will attempt to initiate a key
615 * exchange.
616 */
617
010e6f63
MW
618extern int kx_init(keyexch */*kx*/, peer */*p*/,
619 keyset **/*ks*/, unsigned /*f*/);
410c8acf 620
621/*----- Keysets and symmetric cryptography --------------------------------*/
622
832a2ab6 623/* --- @ks_drop@ --- *
624 *
625 * Arguments: @keyset *ks@ = pointer to a keyset
626 *
627 * Returns: ---
628 *
629 * Use: Decrements a keyset's reference counter. If the counter hits
630 * zero, the keyset is freed.
631 */
632
633extern void ks_drop(keyset */*ks*/);
634
635/* --- @ks_gen@ --- *
636 *
637 * Arguments: @const void *k@ = pointer to key material
638 * @size_t x, y, z@ = offsets into key material (see below)
9466fafa 639 * @peer *p@ = pointer to peer information
832a2ab6 640 *
641 * Returns: A pointer to the new keyset.
642 *
643 * Use: Derives a new keyset from the given key material. The
644 * offsets @x@, @y@ and @z@ separate the key material into three
645 * parts. Between the @k@ and @k + x@ is `my' contribution to
646 * the key material; between @k + x@ and @k + y@ is `your'
647 * contribution; and between @k + y@ and @k + z@ is a shared
648 * value we made together. These are used to construct two
649 * pairs of symmetric keys. Each pair consists of an encryption
650 * key and a message authentication key. One pair is used for
651 * outgoing messages, the other for incoming messages.
652 *
653 * The new key is marked so that it won't be selected for output
654 * by @ksl_encrypt@. You can still encrypt data with it by
655 * calling @ks_encrypt@ directly.
656 */
657
658extern keyset *ks_gen(const void */*k*/,
9466fafa 659 size_t /*x*/, size_t /*y*/, size_t /*z*/,
660 peer */*p*/);
832a2ab6 661
662/* --- @ks_tregen@ --- *
663 *
664 * Arguments: @keyset *ks@ = pointer to a keyset
665 *
666 * Returns: The time at which moves ought to be made to replace this key.
667 */
668
669extern time_t ks_tregen(keyset */*ks*/);
670
671/* --- @ks_activate@ --- *
672 *
673 * Arguments: @keyset *ks@ = pointer to a keyset
674 *
675 * Returns: ---
676 *
677 * Use: Activates a keyset, so that it can be used for encrypting
678 * outgoing messages.
679 */
680
681extern void ks_activate(keyset */*ks*/);
682
683/* --- @ks_encrypt@ --- *
684 *
685 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 686 * @unsigned ty@ = message type
832a2ab6 687 * @buf *b@ = pointer to input buffer
688 * @buf *bb@ = pointer to output buffer
689 *
690 * Returns: Zero if OK, nonzero if the key needs replacing. If the
691 * encryption failed, the output buffer is broken and zero is
692 * returned.
693 *
694 * Use: Encrypts a block of data using the key. Note that the `key
695 * ought to be replaced' notification is only ever given once
696 * for each key. Also note that this call forces a keyset to be
697 * used even if it's marked as not for data output.
698 */
699
7ed14135 700extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
701 buf */*b*/, buf */*bb*/);
832a2ab6 702
703/* --- @ks_decrypt@ --- *
704 *
705 * Arguments: @keyset *ks@ = pointer to a keyset
7ed14135 706 * @unsigned ty@ = expected type code
832a2ab6 707 * @buf *b@ = pointer to an input buffer
708 * @buf *bb@ = pointer to an output buffer
709 *
710 * Returns: Zero on success, or nonzero if there was some problem.
711 *
712 * Use: Attempts to decrypt a message using a given key. Note that
713 * requesting decryption with a key directly won't clear a
714 * marking that it's not for encryption.
715 */
716
7ed14135 717extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
718 buf */*b*/, buf */*bb*/);
832a2ab6 719
720/* --- @ksl_free@ --- *
410c8acf 721 *
722 * Arguments: @keyset **ksroot@ = pointer to keyset list head
723 *
724 * Returns: ---
725 *
832a2ab6 726 * Use: Frees (releases references to) all of the keys in a keyset.
410c8acf 727 */
728
832a2ab6 729extern void ksl_free(keyset **/*ksroot*/);
410c8acf 730
832a2ab6 731/* --- @ksl_link@ --- *
410c8acf 732 *
733 * Arguments: @keyset **ksroot@ = pointer to keyset list head
832a2ab6 734 * @keyset *ks@ = pointer to a keyset
410c8acf 735 *
736 * Returns: ---
737 *
832a2ab6 738 * Use: Links a keyset into a list. A keyset can only be on one list
739 * at a time. Bad things happen otherwise.
410c8acf 740 */
741
832a2ab6 742extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
410c8acf 743
832a2ab6 744/* --- @ksl_prune@ --- *
410c8acf 745 *
746 * Arguments: @keyset **ksroot@ = pointer to keyset list head
410c8acf 747 *
832a2ab6 748 * Returns: ---
410c8acf 749 *
832a2ab6 750 * Use: Prunes the keyset list by removing keys which mustn't be used
751 * any more.
410c8acf 752 */
753
832a2ab6 754extern void ksl_prune(keyset **/*ksroot*/);
410c8acf 755
832a2ab6 756/* --- @ksl_encrypt@ --- *
410c8acf 757 *
758 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 759 * @unsigned ty@ = message type
410c8acf 760 * @buf *b@ = pointer to input buffer
761 * @buf *bb@ = pointer to output buffer
762 *
763 * Returns: Nonzero if a new key is needed.
764 *
765 * Use: Encrypts a packet.
766 */
767
7ed14135 768extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
769 buf */*b*/, buf */*bb*/);
410c8acf 770
832a2ab6 771/* --- @ksl_decrypt@ --- *
410c8acf 772 *
773 * Arguments: @keyset **ksroot@ = pointer to keyset list head
7ed14135 774 * @unsigned ty@ = expected type code
410c8acf 775 * @buf *b@ = pointer to input buffer
776 * @buf *bb@ = pointer to output buffer
777 *
778 * Returns: Nonzero if the packet couldn't be decrypted.
779 *
780 * Use: Decrypts a packet.
781 */
782
7ed14135 783extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
784 buf */*b*/, buf */*bb*/);
410c8acf 785
37941236 786/*----- Challenges --------------------------------------------------------*/
787
788/* --- @c_new@ --- *
789 *
790 * Arguments: @buf *b@ = where to put the challenge
791 *
792 * Returns: Zero if OK, nonzero on error.
793 *
794 * Use: Issues a new challenge.
795 */
796
797extern int c_new(buf */*b*/);
798
799/* --- @c_check@ --- *
800 *
801 * Arguments: @buf *b@ = where to find the challenge
802 *
803 * Returns: Zero if OK, nonzero if it didn't work.
804 *
805 * Use: Checks a challenge. On failure, the buffer is broken.
806 */
807
808extern int c_check(buf */*b*/);
809
410c8acf 810/*----- Administration interface ------------------------------------------*/
811
f43df819
MW
812#define A_END ((char *)0)
813
410c8acf 814/* --- @a_warn@ --- *
815 *
816 * Arguments: @const char *fmt@ = pointer to format string
817 * @...@ = other arguments
818 *
819 * Returns: ---
820 *
821 * Use: Informs all admin connections of a warning.
822 */
823
824extern void a_warn(const char */*fmt*/, ...);
825
3cdc3f3a 826/* --- @a_notify@ --- *
827 *
828 * Arguments: @const char *fmt@ = pointer to format string
829 * @...@ = other arguments
830 *
831 * Returns: ---
832 *
833 * Use: Sends a notification to interested admin connections.
834 */
835
836extern void a_notify(const char */*fmt*/, ...);
837
410c8acf 838/* --- @a_create@ --- *
839 *
840 * Arguments: @int fd_in, fd_out@ = file descriptors to use
3cdc3f3a 841 * @unsigned f@ = initial flags to set
410c8acf 842 *
843 * Returns: ---
844 *
845 * Use: Creates a new admin connection.
846 */
847
3cdc3f3a 848extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
410c8acf 849
850/* --- @a_quit@ --- *
851 *
852 * Arguments: ---
853 *
854 * Returns: ---
855 *
856 * Use: Shuts things down nicely.
857 */
858
859extern void a_quit(void);
860
c511e1f9
MW
861/* --- @a_preselect@ --- *
862 *
863 * Arguments: ---
864 *
865 * Returns: ---
866 *
867 * Use: Informs the admin module that we're about to select again,
868 * and that it should do cleanup things it has delayed until a
869 * `safe' time.
870 */
871
872extern void a_preselect(void);
873
410c8acf 874/* --- @a_daemon@ --- *
875 *
876 * Arguments: ---
877 *
878 * Returns: ---
879 *
880 * Use: Informs the admin module that it's a daemon.
881 */
882
883extern void a_daemon(void);
884
885/* --- @a_init@ --- *
886 *
887 * Arguments: @const char *sock@ = socket name to create
888 *
889 * Returns: ---
890 *
891 * Use: Creates the admin listening socket.
892 */
893
894extern void a_init(const char */*sock*/);
895
c8e02c8a
MW
896/*----- Mapping with addresses as keys ------------------------------------*/
897
898/* --- @am_create@ --- *
899 *
900 * Arguments: @addrmap *m@ = pointer to map
901 *
902 * Returns: ---
903 *
904 * Use: Create an address map, properly set up.
905 */
906
907extern void am_create(addrmap */*m*/);
908
909/* --- @am_destroy@ --- *
910 *
911 * Arguments: @addrmap *m@ = pointer to map
912 *
913 * Returns: ---
914 *
915 * Use: Destroy an address map, throwing away all the entries.
916 */
917
918extern void am_destroy(addrmap */*m*/);
919
920/* --- @am_find@ --- *
921 *
922 * Arguments: @addrmap *m@ = pointer to map
923 * @const addr *a@ = address to look up
924 * @size_t sz@ = size of block to allocate
925 * @unsigned *f@ = where to store flags
926 *
927 * Returns: Pointer to found item, or null.
928 *
929 * Use: Finds a record with the given IP address, set @*f@ nonzero
930 * and returns it. If @sz@ is zero, and no match was found,
931 * return null; otherwise allocate a new block of @sz@ bytes,
932 * clear @*f@ to zero and return the block pointer.
933 */
934
935extern void *am_find(addrmap */*m*/, const addr */*a*/,
936 size_t /*sz*/, unsigned */*f*/);
937
938/* --- @am_remove@ --- *
939 *
940 * Arguments: @addrmap *m@ = pointer to map
941 * @void *i@ = pointer to the item
942 *
943 * Returns: ---
944 *
945 * Use: Removes an item from the map.
946 */
947
948extern void am_remove(addrmap */*m*/, void */*i*/);
949
410c8acf 950/*----- Peer management ---------------------------------------------------*/
951
952/* --- @p_txstart@ --- *
953 *
954 * Arguments: @peer *p@ = pointer to peer block
955 * @unsigned msg@ = message type code
956 *
957 * Returns: A pointer to a buffer to write to.
958 *
959 * Use: Starts sending to a peer. Only one send can happen at a
960 * time.
961 */
962
963extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
964
965/* --- @p_txend@ --- *
966 *
967 * Arguments: @peer *p@ = pointer to peer block
968 *
969 * Returns: ---
970 *
971 * Use: Sends a packet to the peer.
972 */
973
974extern void p_txend(peer */*p*/);
975
0ba8de86 976/* --- @p_pingsend@ --- *
977 *
978 * Arguments: @peer *p@ = destination peer
979 * @ping *pg@ = structure to fill in
980 * @unsigned type@ = message type
981 * @unsigned long timeout@ = how long to wait before giving up
982 * @void (*func)(int, void *)@ = callback function
983 * @void *arg@ = argument for callback
984 *
985 * Returns: Zero if successful, nonzero if it failed.
986 *
987 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
988 * if we get an answer within the timeout, or zero if no answer.
989 */
990
991extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
992 unsigned long /*timeout*/,
993 void (*/*func*/)(int, void *), void */*arg*/);
994
995/* --- @p_pingdone@ --- *
996 *
997 * Arguments: @ping *p@ = ping structure
998 * @int rc@ = return code to pass on
999 *
1000 * Returns: ---
1001 *
1002 * Use: Disposes of a ping structure, maybe sending a notification.
1003 */
1004
1005extern void p_pingdone(ping */*p*/, int /*rc*/);
1006
37941236 1007/* --- @p_greet@ --- *
1008 *
1009 * Arguments: @peer *p@ = peer to send to
1010 * @const void *c@ = pointer to challenge
1011 * @size_t sz@ = size of challenge
1012 *
1013 * Returns: ---
1014 *
1015 * Use: Sends a greeting packet.
1016 */
1017
1018extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1019
410c8acf 1020/* --- @p_tun@ --- *
1021 *
1022 * Arguments: @peer *p@ = pointer to peer block
1023 * @buf *b@ = buffer containing incoming packet
1024 *
1025 * Returns: ---
1026 *
1027 * Use: Handles a packet which needs to be sent to a peer.
1028 */
1029
1030extern void p_tun(peer */*p*/, buf */*b*/);
1031
de014da6 1032/* --- @p_keyreload@ --- *
1033 *
1034 * Arguments: ---
1035 *
1036 * Returns: ---
1037 *
1038 * Use: Forces a check of the daemon's keyring files.
1039 */
1040
1041extern void p_keyreload(void);
1042
410c8acf 1043/* --- @p_interval@ --- *
1044 *
1045 * Arguments: ---
1046 *
1047 * Returns: ---
1048 *
1049 * Use: Called periodically to do tidying.
1050 */
1051
1052extern void p_interval(void);
1053
832a2ab6 1054/* --- @p_stats@ --- *
1055 *
1056 * Arguments: @peer *p@ = pointer to a peer block
1057 *
1058 * Returns: A pointer to the peer's statistics.
1059 */
1060
1061extern stats *p_stats(peer */*p*/);
1062
410c8acf 1063/* --- @p_ifname@ --- *
1064 *
1065 * Arguments: @peer *p@ = pointer to a peer block
1066 *
1067 * Returns: A pointer to the peer's interface name.
1068 */
1069
1070extern const char *p_ifname(peer */*p*/);
1071
64cf2223
MW
1072/* --- @p_setifname@ --- *
1073 *
1074 * Arguments: @peer *p@ = pointer to a peer block
1075 * @const char *name@ = pointer to the new name
1076 *
1077 * Returns: ---
1078 *
1079 * Use: Changes the name held for a peer's interface.
1080 */
1081
1082extern void p_setifname(peer */*p*/, const char */*name*/);
1083
410c8acf 1084/* --- @p_addr@ --- *
1085 *
1086 * Arguments: @peer *p@ = pointer to a peer block
1087 *
1088 * Returns: A pointer to the peer's address.
1089 */
1090
1091extern const addr *p_addr(peer */*p*/);
1092
1093/* --- @p_init@ --- *
1094 *
767b36e2 1095 * Arguments: @struct in_addr addr@ = address to bind to
1096 * @unsigned port@ = port number to listen to
410c8acf 1097 *
1098 * Returns: ---
1099 *
1100 * Use: Initializes the peer system; creates the socket.
1101 */
1102
767b36e2 1103extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
410c8acf 1104
1105/* --- @p_port@ --- *
1106 *
1107 * Arguments: ---
1108 *
1109 * Returns: Port number used for socket.
1110 */
1111
1112unsigned p_port(void);
1113
1114/* --- @p_create@ --- *
1115 *
0ba8de86 1116 * Arguments: @peerspec *spec@ = information about this peer
410c8acf 1117 *
1118 * Returns: Pointer to the peer block, or null if it failed.
1119 *
1120 * Use: Creates a new named peer block. No peer is actually attached
1121 * by this point.
1122 */
1123
0ba8de86 1124extern peer *p_create(peerspec */*spec*/);
410c8acf 1125
1126/* --- @p_name@ --- *
1127 *
1128 * Arguments: @peer *p@ = pointer to a peer block
1129 *
1130 * Returns: A pointer to the peer's name.
060ca767 1131 *
1132 * Use: Equivalent to @p_spec(p)->name@.
410c8acf 1133 */
1134
1135extern const char *p_name(peer */*p*/);
1136
060ca767 1137/* --- @p_spec@ --- *
1138 *
1139 * Arguments: @peer *p@ = pointer to a peer block
1140 *
1141 * Returns: Pointer to the peer's specification
1142 */
1143
1144extern const peerspec *p_spec(peer */*p*/);
1145
c8e02c8a
MW
1146/* --- @p_findbyaddr@ --- *
1147 *
1148 * Arguments: @const addr *a@ = address to look up
1149 *
1150 * Returns: Pointer to the peer block, or null if not found.
1151 *
1152 * Use: Finds a peer by address.
1153 */
1154
1155extern peer *p_findbyaddr(const addr */*a*/);
1156
410c8acf 1157/* --- @p_find@ --- *
1158 *
1159 * Arguments: @const char *name@ = name to look up
1160 *
1161 * Returns: Pointer to the peer block, or null if not found.
1162 *
1163 * Use: Finds a peer by name.
1164 */
1165
1166extern peer *p_find(const char */*name*/);
1167
1168/* --- @p_destroy@ --- *
1169 *
1170 * Arguments: @peer *p@ = pointer to a peer
1171 *
1172 * Returns: ---
1173 *
1174 * Use: Destroys a peer.
1175 */
1176
1177extern void p_destroy(peer */*p*/);
1178
c8e02c8a
MW
1179/* --- @FOREACH_PEER@ --- *
1180 *
1181 * Arguments: @p@ = name to bind to each peer
1182 * @stuff@ = thing to do for each item
1183 *
1184 * Use: Does something for each current peer.
1185 */
1186
1187#define FOREACH_PEER(p, stuff) do { \
1188 peer_iter i_; \
1189 peer *p; \
1190 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) do stuff while (0); \
1191} while (0)
1192
1193/* --- @p_mkiter@ --- *
1194 *
1195 * Arguments: @peer_iter *i@ = pointer to an iterator
1196 *
1197 * Returns: ---
1198 *
1199 * Use: Initializes the iterator.
1200 */
1201
1202extern void p_mkiter(peer_iter */*i*/);
1203
1204/* --- @p_next@ --- *
1205 *
1206 * Arguments: @peer_iter *i@ = pointer to an iterator
410c8acf 1207 *
c8e02c8a 1208 * Returns: Next peer, or null if at the end.
410c8acf 1209 *
c8e02c8a 1210 * Use: Returns the next peer.
410c8acf 1211 */
1212
c8e02c8a 1213extern peer *p_next(peer_iter */*i*/);
410c8acf 1214
42da2a58 1215/*----- Tunnel drivers ----------------------------------------------------*/
410c8acf 1216
42da2a58 1217#ifdef TUN_LINUX
1218 extern const tunnel_ops tun_linux;
1219#endif
410c8acf 1220
42da2a58 1221#ifdef TUN_UNET
1222 extern const tunnel_ops tun_unet;
1223#endif
410c8acf 1224
42da2a58 1225#ifdef TUN_BSD
1226 extern const tunnel_ops tun_bsd;
1227#endif
410c8acf 1228
42da2a58 1229extern const tunnel_ops tun_slip;
410c8acf 1230
410c8acf 1231/*----- Other handy utilities ---------------------------------------------*/
1232
1233/* --- @mpstr@ --- *
1234 *
1235 * Arguments: @mp *m@ = a multiprecision integer
1236 *
1237 * Returns: A pointer to the integer's textual representation.
1238 *
1239 * Use: Converts a multiprecision integer to a string. Corrupts
a4b808b0 1240 * @buf_u@.
410c8acf 1241 */
1242
1243extern const char *mpstr(mp */*m*/);
1244
52c03a2a 1245/* --- @gestr@ --- *
1246 *
1247 * Arguments: @group *g@ = a group
1248 * @ge *x@ = a group element
1249 *
1250 * Returns: A pointer to the element's textual representation.
1251 *
1252 * Use: Converts a group element to a string. Corrupts
a4b808b0 1253 * @buf_u@.
52c03a2a 1254 */
1255
1256extern const char *gestr(group */*g*/, ge */*x*/);
1257
832a2ab6 1258/* --- @timestr@ --- *
1259 *
1260 * Arguments: @time_t t@ = a time to convert
1261 *
1262 * Returns: A pointer to a textual representation of the time.
1263 *
1264 * Use: Converts a time to a textual representation. Corrupts
a4b808b0 1265 * @buf_u@.
832a2ab6 1266 */
1267
1268extern const char *timestr(time_t /*t*/);
1269
42da2a58 1270/* --- @mystrieq@ --- *
1271 *
1272 * Arguments: @const char *x, *y@ = two strings
1273 *
1274 * Returns: True if @x@ and @y are equal, up to case.
1275 */
1276
1277extern int mystrieq(const char */*x*/, const char */*y*/);
1278
37941236 1279/* --- @seq_reset@ --- *
1280 *
1281 * Arguments: @seqwin *s@ = sequence-checking window
1282 *
1283 * Returns: ---
1284 *
1285 * Use: Resets a sequence number window.
1286 */
1287
1288extern void seq_reset(seqwin */*s*/);
1289
1290/* --- @seq_check@ --- *
1291 *
1292 * Arguments: @seqwin *s@ = sequence-checking window
1293 * @uint32 q@ = sequence number to check
f43df819 1294 * @const char *service@ = service to report message from
37941236 1295 *
1296 * Returns: A @SEQ_@ code.
1297 *
1298 * Use: Checks a sequence number against the window, updating things
1299 * as necessary.
1300 */
1301
f43df819 1302extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
37941236 1303
410c8acf 1304/*----- That's all, folks -------------------------------------------------*/
1305
1306#ifdef __cplusplus
1307 }
1308#endif
1309
1310#endif