410c8acf |
1 | /* -*-c-*- |
2 | * |
3cdc3f3a |
3 | * $Id$ |
410c8acf |
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 | |
410c8acf |
29 | #ifndef TRIPE_H |
30 | #define TRIPE_H |
31 | |
32 | #ifdef __cplusplus |
33 | extern "C" { |
34 | #endif |
35 | |
36 | /*----- Header files ------------------------------------------------------*/ |
37 | |
73189848 |
38 | #include "config.h" |
39 | |
410c8acf |
40 | #include <assert.h> |
41 | #include <ctype.h> |
42 | #include <errno.h> |
b9066fbb |
43 | #include <limits.h> |
410c8acf |
44 | #include <signal.h> |
45 | #include <stdarg.h> |
46 | #include <stddef.h> |
47 | #include <stdio.h> |
48 | #include <stdlib.h> |
49 | #include <string.h> |
50 | #include <time.h> |
51 | |
52 | #include <sys/types.h> |
53 | #include <sys/time.h> |
54 | #include <unistd.h> |
55 | #include <fcntl.h> |
56 | #include <sys/stat.h> |
57 | |
58 | #include <sys/socket.h> |
59 | #include <sys/un.h> |
60 | #include <netinet/in.h> |
61 | #include <arpa/inet.h> |
62 | #include <netdb.h> |
63 | |
64 | #include <pwd.h> |
65 | #include <grp.h> |
66 | |
67 | #include <mLib/alloc.h> |
68 | #include <mLib/arena.h> |
69 | #include <mLib/bres.h> |
70 | #include <mLib/dstr.h> |
71 | #include <mLib/env.h> |
72 | #include <mLib/fdflags.h> |
73 | #include <mLib/fwatch.h> |
74 | #include <mLib/mdwopt.h> |
75 | #include <mLib/quis.h> |
76 | #include <mLib/report.h> |
77 | #include <mLib/sel.h> |
78 | #include <mLib/selbuf.h> |
79 | #include <mLib/sig.h> |
80 | #include <mLib/str.h> |
81 | #include <mLib/sub.h> |
82 | #include <mLib/trace.h> |
0ba8de86 |
83 | #include <mLib/tv.h> |
410c8acf |
84 | |
165db1a8 |
85 | #include <catacomb/buf.h> |
86 | |
410c8acf |
87 | #include <catacomb/gcipher.h> |
88 | #include <catacomb/gmac.h> |
89 | #include <catacomb/grand.h> |
90 | #include <catacomb/key.h> |
91 | #include <catacomb/paranoia.h> |
92 | |
410c8acf |
93 | #include <catacomb/noise.h> |
94 | #include <catacomb/rand.h> |
410c8acf |
95 | |
96 | #include <catacomb/mp.h> |
410c8acf |
97 | #include <catacomb/mprand.h> |
98 | #include <catacomb/dh.h> |
52c03a2a |
99 | #include <catacomb/ec.h> |
100 | #include <catacomb/ec-keys.h> |
101 | #include <catacomb/group.h> |
410c8acf |
102 | |
165db1a8 |
103 | #include "tripe-protocol.h" |
410c8acf |
104 | #include "util.h" |
105 | |
106 | #undef sun |
107 | |
108 | /*----- Magic numbers -----------------------------------------------------*/ |
109 | |
410c8acf |
110 | /* --- Trace flags --- */ |
111 | |
112 | #define T_TUNNEL 1u |
113 | #define T_PEER 2u |
114 | #define T_PACKET 4u |
115 | #define T_ADMIN 8u |
116 | #define T_CRYPTO 16u |
117 | #define T_KEYSET 32u |
118 | #define T_KEYEXCH 64u |
119 | #define T_KEYMGMT 128u |
120 | |
121 | #define T_ALL 255u |
122 | |
123 | /* --- Units --- */ |
124 | |
125 | #define SEC(n) (n##u) |
126 | #define MIN(n) (n##u * 60u) |
127 | #define MEG(n) (n##ul * 1024ul * 1024ul) |
128 | |
129 | /* --- Other things --- */ |
130 | |
131 | #define PKBUFSZ 65536 |
132 | |
832a2ab6 |
133 | /*----- Cipher selections -------------------------------------------------*/ |
134 | |
b5c45da1 |
135 | typedef struct algswitch { |
136 | const gccipher *c; /* Symmetric encryption scheme */ |
137 | const gccipher *mgf; /* Mask-generation function */ |
138 | const gchash *h; /* Hash function */ |
139 | const gcmac *m; /* Message authentication code */ |
140 | size_t hashsz; /* Hash output size */ |
141 | size_t tagsz; /* Length to truncate MAC tags */ |
142 | size_t cksz, mksz; /* Key lengths for @c@ and @m@ */ |
143 | } algswitch; |
832a2ab6 |
144 | |
b5c45da1 |
145 | extern algswitch algs; |
832a2ab6 |
146 | |
b5c45da1 |
147 | #define MAXHASHSZ 64 /* Largest possible hash size */ |
832a2ab6 |
148 | |
b5c45da1 |
149 | #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s)) |
410c8acf |
150 | |
aeeb5611 |
151 | /*----- Data structures ---------------------------------------------------*/ |
410c8acf |
152 | |
153 | /* --- Socket addresses --- * |
154 | * |
155 | * A magic union of supported socket addresses. |
156 | */ |
157 | |
158 | typedef union addr { |
159 | struct sockaddr sa; |
160 | struct sockaddr_in sin; |
161 | } addr; |
162 | |
163 | /* --- A symmetric keyset --- * |
164 | * |
165 | * A keyset contains a set of symmetric keys for encrypting and decrypting |
166 | * packets. Keysets are stored in a list, sorted in reverse order of |
167 | * creation, so that the most recent keyset (the one most likely to be used) |
168 | * is first. |
169 | * |
170 | * Each keyset has a time limit and a data limit. The keyset is destroyed |
171 | * when either it has existed for too long, or it has been used to encrypt |
172 | * too much data. New key exchanges are triggered when keys are close to |
173 | * expiry. |
174 | */ |
175 | |
176 | typedef struct keyset { |
177 | struct keyset *next; /* Next active keyset in the list */ |
832a2ab6 |
178 | unsigned ref; /* Reference count for keyset */ |
9466fafa |
179 | struct peer *p; /* Pointer to peer structure */ |
410c8acf |
180 | time_t t_exp; /* Expiry time for this keyset */ |
181 | unsigned long sz_exp; /* Data limit for the keyset */ |
832a2ab6 |
182 | T( unsigned seq; ) /* Sequence number for tracing */ |
183 | unsigned f; /* Various useful flags */ |
184 | gcipher *cin, *cout; /* Keyset ciphers for encryption */ |
b5c45da1 |
185 | size_t tagsz; /* Length to truncate MAC tags */ |
832a2ab6 |
186 | gmac *min, *mout; /* Keyset MACs for integrity */ |
1484d822 |
187 | uint32 oseq; /* Outbound sequence number */ |
188 | uint32 iseq, iwin; /* Inbound sequence number */ |
410c8acf |
189 | } keyset; |
190 | |
1484d822 |
191 | #define KS_SEQWINSZ 32 /* Bits in sequence number window */ |
192 | |
832a2ab6 |
193 | #define KSF_LISTEN 1u /* Don't encrypt packets yet */ |
194 | #define KSF_LINK 2u /* Key is in a linked list */ |
195 | |
410c8acf |
196 | /* --- Key exchange --- * |
197 | * |
198 | * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers |
199 | * Protocol has a number of desirable features (e.g., perfect forward |
200 | * secrecy, and zero-knowledge authentication) which make it attractive for |
201 | * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and |
202 | * Clive Jones. |
203 | */ |
204 | |
832a2ab6 |
205 | #define KX_NCHAL 16u |
206 | #define KX_THRESH 4u |
207 | |
208 | typedef struct kxchal { |
209 | struct keyexch *kx; /* Pointer back to key exchange */ |
52c03a2a |
210 | ge *c; /* Responder's challenge */ |
211 | ge *r; /* My reply to the challenge */ |
832a2ab6 |
212 | keyset *ks; /* Pointer to temporary keyset */ |
213 | unsigned f; /* Various useful flags */ |
214 | sel_timer t; /* Response timer for challenge */ |
b5c45da1 |
215 | octet hc[MAXHASHSZ]; /* Hash of his challenge */ |
1d27afe6 |
216 | mp *ck; /* The check value */ |
b5c45da1 |
217 | octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */ |
218 | octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */ |
219 | octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */ |
220 | octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */ |
832a2ab6 |
221 | } kxchal; |
222 | |
410c8acf |
223 | typedef struct keyexch { |
410c8acf |
224 | struct peer *p; /* Pointer back to the peer */ |
832a2ab6 |
225 | keyset **ks; /* Peer's list of keysets */ |
410c8acf |
226 | unsigned f; /* Various useful flags */ |
832a2ab6 |
227 | unsigned s; /* Current state in exchange */ |
410c8acf |
228 | sel_timer t; /* Timer for next exchange */ |
52c03a2a |
229 | ge *kpub; /* Peer's public key */ |
00e64b67 |
230 | time_t texp_kpub; /* Expiry time for public key */ |
832a2ab6 |
231 | mp *alpha; /* My temporary secret */ |
52c03a2a |
232 | ge *c; /* My challenge */ |
233 | ge *rx; /* The expected response */ |
832a2ab6 |
234 | unsigned nr; /* Number of extant responses */ |
410c8acf |
235 | time_t t_valid; /* When this exchange goes bad */ |
b5c45da1 |
236 | octet hc[MAXHASHSZ]; /* Hash of my challenge */ |
832a2ab6 |
237 | kxchal *r[KX_NCHAL]; /* Array of challenges */ |
410c8acf |
238 | } keyexch; |
239 | |
240 | #define KXF_TIMER 1u /* Waiting for a timer to go off */ |
00e64b67 |
241 | #define KXF_DEAD 2u /* The key-exchanger isn't up */ |
242 | #define KXF_PUBKEY 4u /* Key exchanger has a public key */ |
832a2ab6 |
243 | |
244 | enum { |
245 | KXS_DEAD, /* Uninitialized state (magical) */ |
246 | KXS_CHAL, /* Main answer-challenges state */ |
247 | KXS_COMMIT, /* Committed: send switch request */ |
248 | KXS_SWITCH /* Switched: send confirmation */ |
249 | }; |
410c8acf |
250 | |
251 | /* --- Tunnel structure --- * |
252 | * |
253 | * Used to maintain system-specific information about the tunnel interface. |
254 | */ |
255 | |
42da2a58 |
256 | typedef struct tunnel tunnel; |
257 | struct peer; |
110d564e |
258 | |
42da2a58 |
259 | typedef struct tunnel_ops { |
260 | const char *name; /* Name of this tunnel driver */ |
261 | void (*init)(void); /* Initializes the system */ |
262 | tunnel *(*create)(struct peer */*p*/); /* Initializes a new tunnel */ |
263 | const char *(*ifname)(tunnel */*t*/); /* Returns tunnel's interface name */ |
264 | void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */ |
265 | void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */ |
266 | } tunnel_ops; |
b9066fbb |
267 | |
42da2a58 |
268 | #ifndef TUN_INTERNALS |
269 | struct tunnel { const tunnel_ops *ops; }; |
410c8acf |
270 | #endif |
410c8acf |
271 | |
832a2ab6 |
272 | /* --- Peer statistics --- * |
273 | * |
274 | * Contains various interesting and not-so-interesting statistics about a |
275 | * peer. This is updated by various parts of the code. The format of the |
276 | * structure isn't considered private, and @p_stats@ returns a pointer to the |
277 | * statistics block for a given peer. |
278 | */ |
279 | |
280 | typedef struct stats { |
281 | unsigned long sz_in, sz_out; /* Size of all data in and out */ |
282 | unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */ |
283 | unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */ |
3cdc3f3a |
284 | time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */ |
832a2ab6 |
285 | unsigned long n_reject; /* Number of rejected packets */ |
286 | unsigned long n_in, n_out; /* Number of packets in and out */ |
287 | unsigned long n_kxin, n_kxout; /* Number of key exchange packets */ |
288 | unsigned long n_ipin, n_ipout; /* Number of encrypted packets */ |
289 | } stats; |
290 | |
410c8acf |
291 | /* --- Peer structure --- * |
292 | * |
293 | * The main structure which glues everything else together. |
294 | */ |
295 | |
0ba8de86 |
296 | typedef struct peerspec { |
297 | char *name; /* Peer's name */ |
298 | const tunnel_ops *tops; /* Tunnel operations */ |
299 | unsigned long t_ka; /* Keep alive interval */ |
300 | addr sa; /* Socket address to speak to */ |
301 | size_t sasz; /* Socket address size */ |
302 | } peerspec; |
303 | |
410c8acf |
304 | typedef struct peer { |
305 | struct peer *next, *prev; /* Links to next and previous */ |
0ba8de86 |
306 | struct ping *pings; /* Pings we're waiting for */ |
307 | peerspec spec; /* Specifications for this peer */ |
42da2a58 |
308 | tunnel *t; /* Tunnel for local packets */ |
410c8acf |
309 | keyset *ks; /* List head for keysets */ |
410c8acf |
310 | buf b; /* Buffer for sending packets */ |
832a2ab6 |
311 | stats st; /* Statistics */ |
312 | keyexch kx; /* Key exchange protocol block */ |
0ba8de86 |
313 | sel_timer tka; /* Timer for keepalives */ |
410c8acf |
314 | } peer; |
315 | |
0ba8de86 |
316 | typedef struct ping { |
317 | struct ping *next, *prev; /* Links to next and previous */ |
318 | peer *p; /* Peer so we can free it */ |
319 | unsigned msg; /* Kind of response expected */ |
320 | uint32 id; /* Id so we can recognize response */ |
321 | octet magic[32]; /* Some random data */ |
322 | sel_timer t; /* Timeout for ping */ |
323 | void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */ |
324 | void *arg; /* Argument for callback */ |
325 | } ping; |
326 | |
327 | enum { |
328 | PING_NONOTIFY = -1, |
329 | PING_OK = 0, |
330 | PING_TIMEOUT, |
331 | PING_PEERDIED, |
332 | PING_MAX |
333 | }; |
334 | |
410c8acf |
335 | /* --- Admin structure --- */ |
336 | |
fd3cf232 |
337 | #define OBUFSZ 16384u |
338 | |
339 | typedef struct obuf { |
340 | struct obuf *next; /* Next buffer in list */ |
341 | char *p_in, *p_out; /* Pointers into the buffer */ |
342 | char buf[OBUFSZ]; /* The actual buffer */ |
343 | } obuf; |
344 | |
410c8acf |
345 | typedef struct admin { |
346 | struct admin *next, *prev; /* Links to next and previous */ |
fd3cf232 |
347 | unsigned f; /* Various useful flags */ |
410c8acf |
348 | #ifndef NTRACE |
349 | unsigned seq; /* Sequence number for tracing */ |
350 | #endif |
fd3cf232 |
351 | obuf *o_head, *o_tail; /* Output buffer list */ |
352 | selbuf b; /* Line buffer for commands */ |
353 | sel_file w; /* Selector for write buffering */ |
0ba8de86 |
354 | peerspec peer; /* Peer pending creation */ |
355 | char *paddr; /* Hostname to be resolved */ |
410c8acf |
356 | bres_client r; /* Background resolver task */ |
357 | sel_timer t; /* Timer for resolver */ |
0ba8de86 |
358 | ping ping; /* Ping pending response */ |
359 | struct timeval pingtime; /* Time last ping was sent */ |
410c8acf |
360 | } admin; |
361 | |
fd3cf232 |
362 | #define AF_DEAD 1u /* Destroy this admin block */ |
363 | #define AF_LOCK 2u /* Don't destroy it yet */ |
3cdc3f3a |
364 | #define AF_NOTE 4u /* Catch notifications */ |
365 | #ifndef NTRACE |
366 | #define AF_TRACE 8u /* Catch tracing */ |
367 | #endif |
368 | #define AF_WARN 16u /* Catch warning messages */ |
369 | |
370 | #ifndef NTRACE |
371 | # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN) |
372 | #else |
373 | # define AF_ALLMSGS (AF_NOTE | AF_WARN) |
374 | #endif |
fd3cf232 |
375 | |
410c8acf |
376 | /*----- Global variables --------------------------------------------------*/ |
377 | |
378 | extern sel_state sel; /* Global I/O event state */ |
52c03a2a |
379 | extern group *gg; /* The group we work in */ |
380 | extern mp *kpriv; /* Our private key */ |
832a2ab6 |
381 | extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ]; |
42da2a58 |
382 | extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */ |
383 | extern const tunnel_ops *tun_default; /* Default tunnel to use */ |
410c8acf |
384 | |
385 | #ifndef NTRACE |
386 | extern const trace_opt tr_opts[]; /* Trace options array */ |
387 | extern unsigned tr_flags; /* Trace options flags */ |
388 | #endif |
389 | |
8d0c7a83 |
390 | /*----- Other macros ------------------------------------------------------*/ |
391 | |
392 | #define TIMER noise_timer(RAND_GLOBAL) |
393 | |
410c8acf |
394 | /*----- Key management ----------------------------------------------------*/ |
395 | |
396 | /* --- @km_interval@ --- * |
397 | * |
398 | * Arguments: --- |
399 | * |
400 | * Returns: Zero if OK, nonzero to force reloading of keys. |
401 | * |
402 | * Use: Called on the interval timer to perform various useful jobs. |
403 | */ |
404 | |
405 | extern int km_interval(void); |
406 | |
407 | /* --- @km_init@ --- * |
408 | * |
409 | * Arguments: @const char *kr_priv@ = private keyring file |
410 | * @const char *kr_pub@ = public keyring file |
411 | * @const char *tag@ = tag to load |
412 | * |
413 | * Returns: --- |
414 | * |
415 | * Use: Initializes, and loads the private key. |
416 | */ |
417 | |
418 | extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/, |
419 | const char */*tag*/); |
420 | |
421 | /* --- @km_getpubkey@ --- * |
422 | * |
423 | * Arguments: @const char *tag@ = public key tag to load |
52c03a2a |
424 | * @ge *kpub@ = where to put the public key |
00e64b67 |
425 | * @time_t *t_exp@ = where to put the expiry time |
410c8acf |
426 | * |
427 | * Returns: Zero if OK, nonzero if it failed. |
428 | * |
429 | * Use: Fetches a public key from the keyring. |
430 | */ |
431 | |
52c03a2a |
432 | extern int km_getpubkey(const char */*tag*/, ge */*kpub*/, |
00e64b67 |
433 | time_t */*t_exp*/); |
410c8acf |
434 | |
435 | /*----- Key exchange ------------------------------------------------------*/ |
436 | |
437 | /* --- @kx_start@ --- * |
438 | * |
439 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
440 | * |
441 | * Returns: --- |
442 | * |
443 | * Use: Stimulates a key exchange. If a key exchage is in progress, |
444 | * a new challenge is sent (unless the quiet timer forbids |
445 | * this); if no exchange is in progress, one is commenced. |
446 | */ |
447 | |
448 | extern void kx_start(keyexch */*kx*/); |
449 | |
832a2ab6 |
450 | /* --- @kx_message@ --- * |
410c8acf |
451 | * |
452 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
832a2ab6 |
453 | * @unsigned msg@ = the message code |
454 | * @buf *b@ = pointer to buffer containing the packet |
410c8acf |
455 | * |
456 | * Returns: --- |
457 | * |
832a2ab6 |
458 | * Use: Reads a packet containing key exchange messages and handles |
459 | * it. |
410c8acf |
460 | */ |
461 | |
832a2ab6 |
462 | extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/); |
410c8acf |
463 | |
464 | /* --- @kx_free@ --- * |
465 | * |
466 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
467 | * |
468 | * Returns: --- |
469 | * |
470 | * Use: Frees everything in a key exchange context. |
471 | */ |
472 | |
473 | extern void kx_free(keyexch */*kx*/); |
474 | |
475 | /* --- @kx_newkeys@ --- * |
476 | * |
477 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
478 | * |
479 | * Returns: --- |
480 | * |
481 | * Use: Informs the key exchange module that its keys may have |
482 | * changed. If fetching the new keys fails, the peer will be |
483 | * destroyed, we log messages and struggle along with the old |
484 | * keys. |
485 | */ |
486 | |
487 | extern void kx_newkeys(keyexch */*kx*/); |
488 | |
489 | /* --- @kx_init@ --- * |
490 | * |
491 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
492 | * @peer *p@ = pointer to peer context |
493 | * @keyset **ks@ = pointer to keyset list |
494 | * |
495 | * Returns: Zero if OK, nonzero if it failed. |
496 | * |
497 | * Use: Initializes a key exchange module. The module currently |
498 | * contains no keys, and will attempt to initiate a key |
499 | * exchange. |
500 | */ |
501 | |
502 | extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/); |
503 | |
504 | /*----- Keysets and symmetric cryptography --------------------------------*/ |
505 | |
832a2ab6 |
506 | /* --- @ks_drop@ --- * |
507 | * |
508 | * Arguments: @keyset *ks@ = pointer to a keyset |
509 | * |
510 | * Returns: --- |
511 | * |
512 | * Use: Decrements a keyset's reference counter. If the counter hits |
513 | * zero, the keyset is freed. |
514 | */ |
515 | |
516 | extern void ks_drop(keyset */*ks*/); |
517 | |
518 | /* --- @ks_gen@ --- * |
519 | * |
520 | * Arguments: @const void *k@ = pointer to key material |
521 | * @size_t x, y, z@ = offsets into key material (see below) |
9466fafa |
522 | * @peer *p@ = pointer to peer information |
832a2ab6 |
523 | * |
524 | * Returns: A pointer to the new keyset. |
525 | * |
526 | * Use: Derives a new keyset from the given key material. The |
527 | * offsets @x@, @y@ and @z@ separate the key material into three |
528 | * parts. Between the @k@ and @k + x@ is `my' contribution to |
529 | * the key material; between @k + x@ and @k + y@ is `your' |
530 | * contribution; and between @k + y@ and @k + z@ is a shared |
531 | * value we made together. These are used to construct two |
532 | * pairs of symmetric keys. Each pair consists of an encryption |
533 | * key and a message authentication key. One pair is used for |
534 | * outgoing messages, the other for incoming messages. |
535 | * |
536 | * The new key is marked so that it won't be selected for output |
537 | * by @ksl_encrypt@. You can still encrypt data with it by |
538 | * calling @ks_encrypt@ directly. |
539 | */ |
540 | |
541 | extern keyset *ks_gen(const void */*k*/, |
9466fafa |
542 | size_t /*x*/, size_t /*y*/, size_t /*z*/, |
543 | peer */*p*/); |
832a2ab6 |
544 | |
545 | /* --- @ks_tregen@ --- * |
546 | * |
547 | * Arguments: @keyset *ks@ = pointer to a keyset |
548 | * |
549 | * Returns: The time at which moves ought to be made to replace this key. |
550 | */ |
551 | |
552 | extern time_t ks_tregen(keyset */*ks*/); |
553 | |
554 | /* --- @ks_activate@ --- * |
555 | * |
556 | * Arguments: @keyset *ks@ = pointer to a keyset |
557 | * |
558 | * Returns: --- |
559 | * |
560 | * Use: Activates a keyset, so that it can be used for encrypting |
561 | * outgoing messages. |
562 | */ |
563 | |
564 | extern void ks_activate(keyset */*ks*/); |
565 | |
566 | /* --- @ks_encrypt@ --- * |
567 | * |
568 | * Arguments: @keyset *ks@ = pointer to a keyset |
7ed14135 |
569 | * @unsigned ty@ = message type |
832a2ab6 |
570 | * @buf *b@ = pointer to input buffer |
571 | * @buf *bb@ = pointer to output buffer |
572 | * |
573 | * Returns: Zero if OK, nonzero if the key needs replacing. If the |
574 | * encryption failed, the output buffer is broken and zero is |
575 | * returned. |
576 | * |
577 | * Use: Encrypts a block of data using the key. Note that the `key |
578 | * ought to be replaced' notification is only ever given once |
579 | * for each key. Also note that this call forces a keyset to be |
580 | * used even if it's marked as not for data output. |
581 | */ |
582 | |
7ed14135 |
583 | extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/, |
584 | buf */*b*/, buf */*bb*/); |
832a2ab6 |
585 | |
586 | /* --- @ks_decrypt@ --- * |
587 | * |
588 | * Arguments: @keyset *ks@ = pointer to a keyset |
7ed14135 |
589 | * @unsigned ty@ = expected type code |
832a2ab6 |
590 | * @buf *b@ = pointer to an input buffer |
591 | * @buf *bb@ = pointer to an output buffer |
592 | * |
593 | * Returns: Zero on success, or nonzero if there was some problem. |
594 | * |
595 | * Use: Attempts to decrypt a message using a given key. Note that |
596 | * requesting decryption with a key directly won't clear a |
597 | * marking that it's not for encryption. |
598 | */ |
599 | |
7ed14135 |
600 | extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/, |
601 | buf */*b*/, buf */*bb*/); |
832a2ab6 |
602 | |
603 | /* --- @ksl_free@ --- * |
410c8acf |
604 | * |
605 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
606 | * |
607 | * Returns: --- |
608 | * |
832a2ab6 |
609 | * Use: Frees (releases references to) all of the keys in a keyset. |
410c8acf |
610 | */ |
611 | |
832a2ab6 |
612 | extern void ksl_free(keyset **/*ksroot*/); |
410c8acf |
613 | |
832a2ab6 |
614 | /* --- @ksl_link@ --- * |
410c8acf |
615 | * |
616 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
832a2ab6 |
617 | * @keyset *ks@ = pointer to a keyset |
410c8acf |
618 | * |
619 | * Returns: --- |
620 | * |
832a2ab6 |
621 | * Use: Links a keyset into a list. A keyset can only be on one list |
622 | * at a time. Bad things happen otherwise. |
410c8acf |
623 | */ |
624 | |
832a2ab6 |
625 | extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/); |
410c8acf |
626 | |
832a2ab6 |
627 | /* --- @ksl_prune@ --- * |
410c8acf |
628 | * |
629 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
410c8acf |
630 | * |
832a2ab6 |
631 | * Returns: --- |
410c8acf |
632 | * |
832a2ab6 |
633 | * Use: Prunes the keyset list by removing keys which mustn't be used |
634 | * any more. |
410c8acf |
635 | */ |
636 | |
832a2ab6 |
637 | extern void ksl_prune(keyset **/*ksroot*/); |
410c8acf |
638 | |
832a2ab6 |
639 | /* --- @ksl_encrypt@ --- * |
410c8acf |
640 | * |
641 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
7ed14135 |
642 | * @unsigned ty@ = message type |
410c8acf |
643 | * @buf *b@ = pointer to input buffer |
644 | * @buf *bb@ = pointer to output buffer |
645 | * |
646 | * Returns: Nonzero if a new key is needed. |
647 | * |
648 | * Use: Encrypts a packet. |
649 | */ |
650 | |
7ed14135 |
651 | extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/, |
652 | buf */*b*/, buf */*bb*/); |
410c8acf |
653 | |
832a2ab6 |
654 | /* --- @ksl_decrypt@ --- * |
410c8acf |
655 | * |
656 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
7ed14135 |
657 | * @unsigned ty@ = expected type code |
410c8acf |
658 | * @buf *b@ = pointer to input buffer |
659 | * @buf *bb@ = pointer to output buffer |
660 | * |
661 | * Returns: Nonzero if the packet couldn't be decrypted. |
662 | * |
663 | * Use: Decrypts a packet. |
664 | */ |
665 | |
7ed14135 |
666 | extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/, |
667 | buf */*b*/, buf */*bb*/); |
410c8acf |
668 | |
669 | /*----- Administration interface ------------------------------------------*/ |
670 | |
671 | /* --- @a_warn@ --- * |
672 | * |
673 | * Arguments: @const char *fmt@ = pointer to format string |
674 | * @...@ = other arguments |
675 | * |
676 | * Returns: --- |
677 | * |
678 | * Use: Informs all admin connections of a warning. |
679 | */ |
680 | |
681 | extern void a_warn(const char */*fmt*/, ...); |
682 | |
3cdc3f3a |
683 | /* --- @a_notify@ --- * |
684 | * |
685 | * Arguments: @const char *fmt@ = pointer to format string |
686 | * @...@ = other arguments |
687 | * |
688 | * Returns: --- |
689 | * |
690 | * Use: Sends a notification to interested admin connections. |
691 | */ |
692 | |
693 | extern void a_notify(const char */*fmt*/, ...); |
694 | |
410c8acf |
695 | /* --- @a_create@ --- * |
696 | * |
697 | * Arguments: @int fd_in, fd_out@ = file descriptors to use |
3cdc3f3a |
698 | * @unsigned f@ = initial flags to set |
410c8acf |
699 | * |
700 | * Returns: --- |
701 | * |
702 | * Use: Creates a new admin connection. |
703 | */ |
704 | |
3cdc3f3a |
705 | extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/); |
410c8acf |
706 | |
707 | /* --- @a_quit@ --- * |
708 | * |
709 | * Arguments: --- |
710 | * |
711 | * Returns: --- |
712 | * |
713 | * Use: Shuts things down nicely. |
714 | */ |
715 | |
716 | extern void a_quit(void); |
717 | |
718 | /* --- @a_daemon@ --- * |
719 | * |
720 | * Arguments: --- |
721 | * |
722 | * Returns: --- |
723 | * |
724 | * Use: Informs the admin module that it's a daemon. |
725 | */ |
726 | |
727 | extern void a_daemon(void); |
728 | |
729 | /* --- @a_init@ --- * |
730 | * |
731 | * Arguments: @const char *sock@ = socket name to create |
732 | * |
733 | * Returns: --- |
734 | * |
735 | * Use: Creates the admin listening socket. |
736 | */ |
737 | |
738 | extern void a_init(const char */*sock*/); |
739 | |
740 | /*----- Peer management ---------------------------------------------------*/ |
741 | |
742 | /* --- @p_txstart@ --- * |
743 | * |
744 | * Arguments: @peer *p@ = pointer to peer block |
745 | * @unsigned msg@ = message type code |
746 | * |
747 | * Returns: A pointer to a buffer to write to. |
748 | * |
749 | * Use: Starts sending to a peer. Only one send can happen at a |
750 | * time. |
751 | */ |
752 | |
753 | extern buf *p_txstart(peer */*p*/, unsigned /*msg*/); |
754 | |
755 | /* --- @p_txend@ --- * |
756 | * |
757 | * Arguments: @peer *p@ = pointer to peer block |
758 | * |
759 | * Returns: --- |
760 | * |
761 | * Use: Sends a packet to the peer. |
762 | */ |
763 | |
764 | extern void p_txend(peer */*p*/); |
765 | |
0ba8de86 |
766 | /* --- @p_pingsend@ --- * |
767 | * |
768 | * Arguments: @peer *p@ = destination peer |
769 | * @ping *pg@ = structure to fill in |
770 | * @unsigned type@ = message type |
771 | * @unsigned long timeout@ = how long to wait before giving up |
772 | * @void (*func)(int, void *)@ = callback function |
773 | * @void *arg@ = argument for callback |
774 | * |
775 | * Returns: Zero if successful, nonzero if it failed. |
776 | * |
777 | * Use: Sends a ping to a peer. Call @func@ with a nonzero argument |
778 | * if we get an answer within the timeout, or zero if no answer. |
779 | */ |
780 | |
781 | extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/, |
782 | unsigned long /*timeout*/, |
783 | void (*/*func*/)(int, void *), void */*arg*/); |
784 | |
785 | /* --- @p_pingdone@ --- * |
786 | * |
787 | * Arguments: @ping *p@ = ping structure |
788 | * @int rc@ = return code to pass on |
789 | * |
790 | * Returns: --- |
791 | * |
792 | * Use: Disposes of a ping structure, maybe sending a notification. |
793 | */ |
794 | |
795 | extern void p_pingdone(ping */*p*/, int /*rc*/); |
796 | |
410c8acf |
797 | /* --- @p_tun@ --- * |
798 | * |
799 | * Arguments: @peer *p@ = pointer to peer block |
800 | * @buf *b@ = buffer containing incoming packet |
801 | * |
802 | * Returns: --- |
803 | * |
804 | * Use: Handles a packet which needs to be sent to a peer. |
805 | */ |
806 | |
807 | extern void p_tun(peer */*p*/, buf */*b*/); |
808 | |
809 | /* --- @p_interval@ --- * |
810 | * |
811 | * Arguments: --- |
812 | * |
813 | * Returns: --- |
814 | * |
815 | * Use: Called periodically to do tidying. |
816 | */ |
817 | |
818 | extern void p_interval(void); |
819 | |
832a2ab6 |
820 | /* --- @p_stats@ --- * |
821 | * |
822 | * Arguments: @peer *p@ = pointer to a peer block |
823 | * |
824 | * Returns: A pointer to the peer's statistics. |
825 | */ |
826 | |
827 | extern stats *p_stats(peer */*p*/); |
828 | |
410c8acf |
829 | /* --- @p_ifname@ --- * |
830 | * |
831 | * Arguments: @peer *p@ = pointer to a peer block |
832 | * |
833 | * Returns: A pointer to the peer's interface name. |
834 | */ |
835 | |
836 | extern const char *p_ifname(peer */*p*/); |
837 | |
838 | /* --- @p_addr@ --- * |
839 | * |
840 | * Arguments: @peer *p@ = pointer to a peer block |
841 | * |
842 | * Returns: A pointer to the peer's address. |
843 | */ |
844 | |
845 | extern const addr *p_addr(peer */*p*/); |
846 | |
847 | /* --- @p_init@ --- * |
848 | * |
767b36e2 |
849 | * Arguments: @struct in_addr addr@ = address to bind to |
850 | * @unsigned port@ = port number to listen to |
410c8acf |
851 | * |
852 | * Returns: --- |
853 | * |
854 | * Use: Initializes the peer system; creates the socket. |
855 | */ |
856 | |
767b36e2 |
857 | extern void p_init(struct in_addr /*addr*/, unsigned /*port*/); |
410c8acf |
858 | |
859 | /* --- @p_port@ --- * |
860 | * |
861 | * Arguments: --- |
862 | * |
863 | * Returns: Port number used for socket. |
864 | */ |
865 | |
866 | unsigned p_port(void); |
867 | |
868 | /* --- @p_create@ --- * |
869 | * |
0ba8de86 |
870 | * Arguments: @peerspec *spec@ = information about this peer |
410c8acf |
871 | * |
872 | * Returns: Pointer to the peer block, or null if it failed. |
873 | * |
874 | * Use: Creates a new named peer block. No peer is actually attached |
875 | * by this point. |
876 | */ |
877 | |
0ba8de86 |
878 | extern peer *p_create(peerspec */*spec*/); |
410c8acf |
879 | |
880 | /* --- @p_name@ --- * |
881 | * |
882 | * Arguments: @peer *p@ = pointer to a peer block |
883 | * |
884 | * Returns: A pointer to the peer's name. |
885 | */ |
886 | |
887 | extern const char *p_name(peer */*p*/); |
888 | |
889 | /* --- @p_find@ --- * |
890 | * |
891 | * Arguments: @const char *name@ = name to look up |
892 | * |
893 | * Returns: Pointer to the peer block, or null if not found. |
894 | * |
895 | * Use: Finds a peer by name. |
896 | */ |
897 | |
898 | extern peer *p_find(const char */*name*/); |
899 | |
900 | /* --- @p_destroy@ --- * |
901 | * |
902 | * Arguments: @peer *p@ = pointer to a peer |
903 | * |
904 | * Returns: --- |
905 | * |
906 | * Use: Destroys a peer. |
907 | */ |
908 | |
909 | extern void p_destroy(peer */*p*/); |
910 | |
911 | /* --- @p_first@, @p_next@ --- * |
912 | * |
913 | * Arguments: @peer *p@ = a peer block |
914 | * |
915 | * Returns: @peer_first@ returns the first peer in some ordering; |
916 | * @peer_next@ returns the peer following a given one in the |
917 | * same ordering. Null is returned for the end of the list. |
918 | */ |
919 | |
920 | extern peer *p_first(void); |
921 | extern peer *p_next(peer */*p*/); |
922 | |
42da2a58 |
923 | /*----- Tunnel drivers ----------------------------------------------------*/ |
410c8acf |
924 | |
42da2a58 |
925 | #ifdef TUN_LINUX |
926 | extern const tunnel_ops tun_linux; |
927 | #endif |
410c8acf |
928 | |
42da2a58 |
929 | #ifdef TUN_UNET |
930 | extern const tunnel_ops tun_unet; |
931 | #endif |
410c8acf |
932 | |
42da2a58 |
933 | #ifdef TUN_BSD |
934 | extern const tunnel_ops tun_bsd; |
935 | #endif |
410c8acf |
936 | |
42da2a58 |
937 | extern const tunnel_ops tun_slip; |
410c8acf |
938 | |
410c8acf |
939 | /*----- Other handy utilities ---------------------------------------------*/ |
940 | |
941 | /* --- @mpstr@ --- * |
942 | * |
943 | * Arguments: @mp *m@ = a multiprecision integer |
944 | * |
945 | * Returns: A pointer to the integer's textual representation. |
946 | * |
947 | * Use: Converts a multiprecision integer to a string. Corrupts |
832a2ab6 |
948 | * @buf_t@. |
410c8acf |
949 | */ |
950 | |
951 | extern const char *mpstr(mp */*m*/); |
952 | |
52c03a2a |
953 | /* --- @gestr@ --- * |
954 | * |
955 | * Arguments: @group *g@ = a group |
956 | * @ge *x@ = a group element |
957 | * |
958 | * Returns: A pointer to the element's textual representation. |
959 | * |
960 | * Use: Converts a group element to a string. Corrupts |
961 | * @buf_t@. |
962 | */ |
963 | |
964 | extern const char *gestr(group */*g*/, ge */*x*/); |
965 | |
832a2ab6 |
966 | /* --- @timestr@ --- * |
967 | * |
968 | * Arguments: @time_t t@ = a time to convert |
969 | * |
970 | * Returns: A pointer to a textual representation of the time. |
971 | * |
972 | * Use: Converts a time to a textual representation. Corrupts |
973 | * @buf_t@. |
974 | */ |
975 | |
976 | extern const char *timestr(time_t /*t*/); |
977 | |
42da2a58 |
978 | /* --- @mystrieq@ --- * |
979 | * |
980 | * Arguments: @const char *x, *y@ = two strings |
981 | * |
982 | * Returns: True if @x@ and @y are equal, up to case. |
983 | */ |
984 | |
985 | extern int mystrieq(const char */*x*/, const char */*y*/); |
986 | |
410c8acf |
987 | /*----- That's all, folks -------------------------------------------------*/ |
988 | |
989 | #ifdef __cplusplus |
990 | } |
991 | #endif |
992 | |
993 | #endif |