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