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