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