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