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