Commit | Line | Data |
---|---|---|
410c8acf | 1 | /* -*-c-*- |
410c8acf | 2 | * |
3 | * Main header file for TrIPE | |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
e04c2d50 | 8 | /*----- Licensing notice --------------------------------------------------* |
410c8acf | 9 | * |
10 | * This file is part of Trivial IP Encryption (TrIPE). | |
11 | * | |
11ad66c2 MW |
12 | * TrIPE is free software: you can redistribute it and/or modify it under |
13 | * the terms of the GNU General Public License as published by the Free | |
14 | * Software Foundation; either version 3 of the License, or (at your | |
15 | * option) any later version. | |
e04c2d50 | 16 | * |
11ad66c2 MW |
17 | * TrIPE is distributed in the hope that it will be useful, but WITHOUT |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
e04c2d50 | 21 | * |
410c8acf | 22 | * You should have received a copy of the GNU General Public License |
11ad66c2 | 23 | * along with TrIPE. If not, see <https://www.gnu.org/licenses/>. |
410c8acf | 24 | */ |
25 | ||
410c8acf | 26 | #ifndef TRIPE_H |
27 | #define TRIPE_H | |
28 | ||
29 | #ifdef __cplusplus | |
30 | extern "C" { | |
31 | #endif | |
32 | ||
33 | /*----- Header files ------------------------------------------------------*/ | |
34 | ||
73189848 | 35 | #include "config.h" |
36 | ||
410c8acf | 37 | #include <assert.h> |
38 | #include <ctype.h> | |
39 | #include <errno.h> | |
b9066fbb | 40 | #include <limits.h> |
410c8acf | 41 | #include <signal.h> |
42 | #include <stdarg.h> | |
43 | #include <stddef.h> | |
44 | #include <stdio.h> | |
45 | #include <stdlib.h> | |
46 | #include <string.h> | |
47 | #include <time.h> | |
48 | ||
49 | #include <sys/types.h> | |
50 | #include <sys/time.h> | |
51 | #include <unistd.h> | |
52 | #include <fcntl.h> | |
53 | #include <sys/stat.h> | |
388e0319 | 54 | #include <sys/wait.h> |
410c8acf | 55 | |
56 | #include <sys/socket.h> | |
57 | #include <sys/un.h> | |
58 | #include <netinet/in.h> | |
59 | #include <arpa/inet.h> | |
60 | #include <netdb.h> | |
61 | ||
62 | #include <pwd.h> | |
63 | #include <grp.h> | |
64 | ||
65 | #include <mLib/alloc.h> | |
66 | #include <mLib/arena.h> | |
37941236 | 67 | #include <mLib/base64.h> |
410c8acf | 68 | #include <mLib/bres.h> |
37d4c59e | 69 | #include <mLib/codec.h> |
19dd2531 | 70 | #include <mLib/daemonize.h> |
410c8acf | 71 | #include <mLib/dstr.h> |
72 | #include <mLib/env.h> | |
73 | #include <mLib/fdflags.h> | |
388e0319 | 74 | #include <mLib/fdpass.h> |
410c8acf | 75 | #include <mLib/fwatch.h> |
c8e02c8a | 76 | #include <mLib/hash.h> |
165efde7 | 77 | #include <mLib/macros.h> |
b9537f3b | 78 | #include <mLib/mdup.h> |
410c8acf | 79 | #include <mLib/mdwopt.h> |
80 | #include <mLib/quis.h> | |
81 | #include <mLib/report.h> | |
82 | #include <mLib/sel.h> | |
83 | #include <mLib/selbuf.h> | |
84 | #include <mLib/sig.h> | |
85 | #include <mLib/str.h> | |
86 | #include <mLib/sub.h> | |
87 | #include <mLib/trace.h> | |
0ba8de86 | 88 | #include <mLib/tv.h> |
19dd2531 | 89 | #include <mLib/versioncmp.h> |
410c8acf | 90 | |
165db1a8 | 91 | #include <catacomb/buf.h> |
04ed79b8 | 92 | #include <catacomb/ct.h> |
165db1a8 | 93 | |
de8edc7f | 94 | #include <catacomb/chacha.h> |
410c8acf | 95 | #include <catacomb/gcipher.h> |
96 | #include <catacomb/gmac.h> | |
97 | #include <catacomb/grand.h> | |
98 | #include <catacomb/key.h> | |
99 | #include <catacomb/paranoia.h> | |
de8edc7f MW |
100 | #include <catacomb/poly1305.h> |
101 | #include <catacomb/salsa20.h> | |
410c8acf | 102 | |
410c8acf | 103 | #include <catacomb/noise.h> |
104 | #include <catacomb/rand.h> | |
410c8acf | 105 | |
106 | #include <catacomb/mp.h> | |
5b9f3d37 | 107 | #include <catacomb/mpmont.h> |
410c8acf | 108 | #include <catacomb/mprand.h> |
109 | #include <catacomb/dh.h> | |
52c03a2a | 110 | #include <catacomb/ec.h> |
5b9f3d37 | 111 | #include <catacomb/ec-raw.h> |
52c03a2a | 112 | #include <catacomb/ec-keys.h> |
26936c83 MW |
113 | #include <catacomb/x25519.h> |
114 | #include <catacomb/x448.h> | |
410c8acf | 115 | |
388e0319 | 116 | #include "priv.h" |
78698994 | 117 | #include "protocol.h" |
10f681b1 | 118 | #include "slip.h" |
410c8acf | 119 | #include "util.h" |
120 | ||
121 | #undef sun | |
122 | ||
123 | /*----- Magic numbers -----------------------------------------------------*/ | |
124 | ||
410c8acf | 125 | /* --- Trace flags --- */ |
126 | ||
127 | #define T_TUNNEL 1u | |
128 | #define T_PEER 2u | |
129 | #define T_PACKET 4u | |
130 | #define T_ADMIN 8u | |
131 | #define T_CRYPTO 16u | |
132 | #define T_KEYSET 32u | |
133 | #define T_KEYEXCH 64u | |
134 | #define T_KEYMGMT 128u | |
37941236 | 135 | #define T_CHAL 256u |
388e0319 | 136 | /* T_PRIVSEP in priv.h */ |
410c8acf | 137 | |
388e0319 | 138 | #define T_ALL 1023u |
410c8acf | 139 | |
140 | /* --- Units --- */ | |
141 | ||
142 | #define SEC(n) (n##u) | |
143 | #define MIN(n) (n##u * 60u) | |
a06d57a3 | 144 | #define F_2P32 (65536.0*65536.0) |
410c8acf | 145 | #define MEG(n) (n##ul * 1024ul * 1024ul) |
146 | ||
e9fac70c MW |
147 | /* --- Timing parameters --- */ |
148 | ||
149 | #define T_EXP MIN(60) /* Expiry time for a key */ | |
150 | #define T_REGEN MIN(40) /* Regeneration time for a key */ | |
151 | ||
152 | #define T_VALID SEC(20) /* Challenge validity period */ | |
a06d57a3 MW |
153 | #define T_RETRYMIN SEC(2) /* Minimum retry interval */ |
154 | #define T_RETRYMAX MIN(5) /* Maximum retry interval */ | |
155 | #define T_RETRYGROW (5.0/4.0) /* Retry interval growth factor */ | |
156 | ||
157 | #define T_WOBBLE (1.0/3.0) /* Relative timer randomness */ | |
e9fac70c | 158 | |
410c8acf | 159 | /* --- Other things --- */ |
160 | ||
161 | #define PKBUFSZ 65536 | |
162 | ||
832a2ab6 | 163 | /*----- Cipher selections -------------------------------------------------*/ |
164 | ||
a93aacce MW |
165 | typedef struct keyset keyset; |
166 | typedef struct algswitch algswitch; | |
5b9f3d37 | 167 | typedef struct kdata kdata; |
c70a7c5c MW |
168 | typedef struct admin admin; |
169 | ||
5b9f3d37 MW |
170 | typedef struct dhgrp { |
171 | const struct dhops *ops; | |
172 | size_t scsz; | |
173 | } dhgrp; | |
174 | ||
175 | typedef struct dhsc dhsc; | |
176 | typedef struct dhge dhge; | |
177 | ||
178 | enum { | |
179 | DHFMT_STD, /* Fixed-width format, suitable for encryption */ | |
180 | DHFMT_HASH, /* Deterministic format, suitable for hashing */ | |
181 | DHFMT_VAR /* Variable-width-format, mostly a bad idea */ | |
182 | }; | |
183 | ||
ef09dae1 MW |
184 | typedef struct deriveargs { |
185 | const char *what; /* Operation name (hashed) */ | |
186 | unsigned f; /* Flags */ | |
187 | #define DF_IN 1u /* Make incoming key */ | |
188 | #define DF_OUT 2u /* Make outgoing key */ | |
189 | const gchash *hc; /* Hash class */ | |
190 | const octet *k; /* Pointer to contributions */ | |
191 | size_t x, y, z; /* Markers in contributions */ | |
192 | } deriveargs; | |
193 | ||
c70a7c5c MW |
194 | typedef struct bulkalgs { |
195 | const struct bulkops *ops; | |
196 | } bulkalgs; | |
197 | ||
198 | typedef struct bulkctx { | |
199 | const struct bulkops *ops; | |
200 | } bulkctx; | |
201 | ||
202 | typedef struct bulkchal { | |
203 | const struct bulkops *ops; | |
204 | size_t tagsz; | |
205 | } bulkchal; | |
206 | ||
5b9f3d37 MW |
207 | typedef struct dhops { |
208 | const char *name; | |
209 | ||
210 | int (*ldpriv)(key_file */*kf*/, key */*k*/, key_data */*d*/, | |
211 | kdata */*kd*/, dstr */*t*/, dstr */*e*/); | |
212 | /* Load a private key from @d@, storing the data in @kd@. The key's | |
f1165777 | 213 | * file and key object are in @kf@ and @k@, mostly in case its |
5b9f3d37 MW |
214 | * attributes are interesting; the key tag is in @t@; errors are |
215 | * reported by writing tokens to @e@ and returning nonzero. | |
216 | */ | |
217 | ||
218 | int (*ldpub)(key_file */*kf*/, key */*k*/, key_data */*d*/, | |
219 | kdata */*kd*/, dstr */*t*/, dstr */*e*/); | |
220 | /* Load a public key from @d@, storing the data in @kd@. The key's | |
f1165777 | 221 | * file and key object are in @kf@ and @k@, mostly in case its |
5b9f3d37 MW |
222 | * attributes are interesting; the key tag is in @t@; errors are |
223 | * reported by writing tokens to @e@ and returning nonzero. | |
224 | */ | |
225 | ||
226 | const char *(*checkgrp)(const dhgrp */*g*/); | |
227 | /* Check that the group is valid; return null on success, or an error | |
228 | * string. | |
229 | */ | |
230 | ||
231 | void (*grpinfo)(const dhgrp */*g*/, admin */*a*/); | |
232 | /* Report on the group to an admin client. */ | |
233 | ||
234 | T( void (*tracegrp)(const dhgrp */*g*/); ) | |
235 | /* Trace a description of the group. */ | |
236 | ||
237 | int (*samegrpp)(const dhgrp */*g*/, const dhgrp */*gg*/); | |
238 | /* Return nonzero if the two group objects represent the same | |
239 | * group. | |
240 | */ | |
241 | ||
242 | void (*freegrp)(dhgrp */*g*/); | |
243 | /* Free a group and the resources it holds. */ | |
244 | ||
245 | dhsc *(*ldsc)(const dhgrp */*g*/, const void */*p*/, size_t /*sz*/); | |
246 | /* Load a scalar from @p@, @sz@ and return it. Return null on | |
247 | * error. | |
248 | */ | |
249 | ||
250 | int (*stsc)(const dhgrp */*g*/, | |
251 | void */*p*/, size_t /*sz*/, const dhsc */*x*/); | |
252 | /* Store a scalar at @p@, @sz@. Return nonzero on error. */ | |
253 | ||
254 | dhsc *(*randsc)(const dhgrp */*g*/); | |
255 | /* Return a random scalar. */ | |
256 | ||
257 | T( const char *(*scstr)(const dhgrp */*g*/, const dhsc */*x*/); ) | |
258 | /* Return a human-readable representation of @x@; @buf_t@ may be used | |
259 | * to hold it. | |
260 | */ | |
261 | ||
262 | void (*freesc)(const dhgrp */*g*/, dhsc */*x*/); | |
263 | /* Free a scalar and the resources it holds. */ | |
264 | ||
265 | dhge *(*ldge)(const dhgrp */*g*/, buf */*b*/, int /*fmt*/); | |
266 | /* Load a group element from @b@, encoded using format @fmt@. Return | |
267 | * null on error. | |
268 | */ | |
269 | ||
270 | int (*stge)(const dhgrp */*g*/, buf */*b*/, | |
271 | const dhge */*Y*/, int /*fmt*/); | |
272 | /* Store a group element in @b@, encoded using format @fmt@. Return | |
273 | * nonzero on error. | |
274 | */ | |
275 | ||
276 | int (*checkge)(const dhgrp */*h*/, const dhge */*Y*/); | |
277 | /* Check a group element for validity. Return zero if everything | |
278 | * checks out; nonzero on failure. | |
279 | */ | |
280 | ||
281 | int (*eq)(const dhgrp */*g*/, const dhge */*Y*/, const dhge */*Z*/); | |
282 | /* Return nonzero if @Y@ and @Z@ are equal. */ | |
283 | ||
284 | dhge *(*mul)(const dhgrp */*g*/, const dhsc */*x*/, const dhge */*Y*/); | |
285 | /* Multiply a group element by a scalar, resulting in a shared-secret | |
286 | * group element. If @y@ is null, then multiply the well-known | |
287 | * generator. | |
288 | */ | |
289 | ||
290 | T( const char *(*gestr)(const dhgrp */*g*/, const dhge */*Y*/); ) | |
291 | /* Return a human-readable representation of @Y@; @buf_t@ may be used | |
292 | * to hold it. | |
293 | */ | |
294 | ||
295 | void (*freege)(const dhgrp */*g*/, dhge */*Y*/); | |
296 | /* Free a group element and the resources it holds. */ | |
297 | ||
298 | } dhops; | |
299 | ||
fddd7fb7 | 300 | typedef struct bulkops { |
a93aacce | 301 | const char *name; |
c70a7c5c MW |
302 | |
303 | bulkalgs *(*getalgs)(const algswitch */*asw*/, dstr */*e*/, | |
304 | key_file */*kf*/, key */*k*/); | |
305 | /* Determine algorithms to use and return a @bulkalgs@ object | |
306 | * representing the decision. On error, write tokens to @e@ and | |
307 | * return null. | |
308 | */ | |
309 | ||
310 | T( void (*tracealgs)(const bulkalgs */*a*/); ) | |
311 | /* Write trace information about the algorithm selection. */ | |
312 | ||
313 | int (*checkalgs)(bulkalgs */*a*/, const algswitch */*asw*/, dstr */*e*/); | |
314 | /* Check that the algorithms in @a@ and @asw@ are acceptable. On | |
315 | * error, write tokens to @e@ and return @-1@; otherwise return zero. | |
316 | */ | |
317 | ||
318 | int (*samealgsp)(const bulkalgs */*a*/, const bulkalgs */*aa*/); | |
319 | /* If @a@ and @aa@ represent the same algorithm selection, return | |
320 | * nonzero; if not, return zero. | |
321 | */ | |
322 | ||
323 | void (*alginfo)(const bulkalgs */*a*/, admin */*adm*/); | |
324 | /* Report on the algorithm selection to an admin client: call | |
325 | * @a_info@ with appropriate key-value pairs. | |
326 | */ | |
327 | ||
328 | size_t (*overhead)(const bulkalgs */*a*/); | |
329 | /* Return the per-packet overhead of the bulk transform, in bytes. */ | |
330 | ||
331 | size_t (*expsz)(const bulkalgs */*a*/); | |
332 | /* Return the total size limit for the bulk transform, in bytes, | |
333 | * after which the keys must no longer be used. | |
334 | */ | |
335 | ||
ef09dae1 | 336 | bulkctx *(*genkeys)(const bulkalgs */*a*/, const deriveargs */*a*/); |
c70a7c5c | 337 | /* Generate session keys and construct and return an appropriate |
ef09dae1 MW |
338 | * context for using them. The offsets @a->x@, @a->y@ and @a->z@ |
339 | * separate the key material into three parts. Between @a->k@ and | |
340 | * @a->k + a->x@ is `my' contribution to the key material; between | |
341 | * @a->k + a->x@ and @a->k + a->y@ is `your' contribution; and | |
342 | * between @a->k + a->y@ and @a->k + a->z@ is a shared value we made | |
343 | * together. These are used to construct (up to) two collections of | |
344 | * symmetric keys: one for outgoing messages, the other for incoming | |
345 | * messages. If @a->x == 0@ (or @a->y == a->x@) then my (or your) | |
346 | * contribution is omitted. | |
c70a7c5c MW |
347 | */ |
348 | ||
349 | bulkchal *(*genchal)(const bulkalgs */*a*/); | |
350 | /* Construct and return a challenge issuing and verification | |
351 | * context with a fresh random key. | |
352 | */ | |
353 | ||
354 | void (*freealgs)(bulkalgs */*a*/); | |
355 | /* Release an algorithm selection object. (Associated bulk | |
356 | * encryption contexts and challenge contexts may still exist and | |
357 | * must remain valid.) | |
358 | */ | |
359 | ||
360 | int (*encrypt)(bulkctx */*bc*/, unsigned /*ty*/, | |
361 | buf */*b*/, buf */*bb*/, uint32 /*seq*/); | |
362 | /* Encrypt the packet in @b@, with type @ty@ (which doesn't need | |
363 | * encoding separately) and sequence number @seq@ (which must be | |
364 | * recoverable by @decrypt@), and write the result to @bb@. On | |
365 | * error, return a @KSERR_...@ code and/or break the output buffer. | |
366 | */ | |
367 | ||
368 | int (*decrypt)(bulkctx */*bc*/, unsigned /*ty*/, | |
a93aacce | 369 | buf */*b*/, buf */*bb*/, uint32 */*seq*/); |
c70a7c5c MW |
370 | /* Decrypt the packet in @b@, with type @ty@, writing the result to |
371 | * @bb@ and storing the incoming (claimed) sequence number in @seq@. | |
372 | * On error, return a @KSERR_...@ code. | |
373 | */ | |
374 | ||
375 | void (*freectx)(bulkctx */*a*/); | |
376 | /* Release a bulk encryption context and the resources it holds. */ | |
377 | ||
378 | int (*chaltag)(bulkchal */*bc*/, const void */*m*/, size_t /*msz*/, | |
3deadf73 MW |
379 | uint32 /*seq*/, void */*t*/); |
380 | /* Calculate a tag for the challenge in @m@, @msz@, with the sequence | |
381 | * number @seq@, and write it to @t@. Return @-1@ on error, zero on | |
382 | * success. | |
c70a7c5c MW |
383 | */ |
384 | ||
385 | int (*chalvrf)(bulkchal */*bc*/, const void */*m*/, size_t /*msz*/, | |
3deadf73 MW |
386 | uint32 /*seq*/, const void */*t*/); |
387 | /* Check the tag @t@ on @m@, @msz@ and @seq@: return zero if the tag | |
388 | * is OK, nonzero if it's bad. | |
c70a7c5c MW |
389 | */ |
390 | ||
391 | void (*freechal)(bulkchal */*bc*/); | |
392 | /* Release a challenge context and the resources it holds. */ | |
a93aacce | 393 | |
c70a7c5c | 394 | } bulkops; |
a93aacce MW |
395 | |
396 | struct algswitch { | |
c70a7c5c | 397 | const gchash *h; size_t hashsz; /* Hash function */ |
a93aacce | 398 | const gccipher *mgf; /* Mask-generation function */ |
c70a7c5c | 399 | bulkalgs *bulk; /* Bulk crypto algorithms */ |
a93aacce | 400 | }; |
832a2ab6 | 401 | |
5b9f3d37 | 402 | struct kdata { |
799e58b9 MW |
403 | unsigned ref; /* Reference counter */ |
404 | struct knode *kn; /* Pointer to cache entry */ | |
fb6a9f13 | 405 | uint32 id; /* The underlying key's id */ |
799e58b9 | 406 | char *tag; /* Full tag name of the key */ |
5b9f3d37 MW |
407 | dhgrp *grp; /* The group we work in */ |
408 | dhsc *k; /* The private key (or null) */ | |
409 | dhge *K; /* The public key */ | |
799e58b9 MW |
410 | time_t t_exp; /* Expiry time of the key */ |
411 | algswitch algs; /* Collection of algorithms */ | |
5b9f3d37 | 412 | }; |
799e58b9 MW |
413 | |
414 | typedef struct knode { | |
415 | sym_base _b; /* Symbol table intrusion */ | |
416 | unsigned f; /* Various flags */ | |
417 | #define KNF_BROKEN 1u /* Don't use this key any more */ | |
418 | struct keyhalf *kh; /* Pointer to the home keyhalf */ | |
419 | kdata *kd; /* Pointer to the key data */ | |
420 | } knode; | |
832a2ab6 | 421 | |
b5c45da1 | 422 | #define MAXHASHSZ 64 /* Largest possible hash size */ |
832a2ab6 | 423 | |
b5c45da1 | 424 | #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s)) |
410c8acf | 425 | |
5b9f3d37 | 426 | extern const dhops dhtab[]; |
fddd7fb7 | 427 | extern const bulkops bulktab[]; |
a93aacce | 428 | |
aeeb5611 | 429 | /*----- Data structures ---------------------------------------------------*/ |
410c8acf | 430 | |
431 | /* --- Socket addresses --- * | |
432 | * | |
433 | * A magic union of supported socket addresses. | |
434 | */ | |
435 | ||
436 | typedef union addr { | |
437 | struct sockaddr sa; | |
438 | struct sockaddr_in sin; | |
439 | } addr; | |
440 | ||
c8e02c8a MW |
441 | /* --- Mapping keyed on addresses --- */ |
442 | ||
443 | typedef struct addrmap { | |
444 | hash_table t; | |
445 | size_t load; | |
446 | } addrmap; | |
447 | ||
448 | typedef struct addrmap_base { | |
449 | hash_base b; | |
450 | addr a; | |
451 | } addrmap_base; | |
452 | ||
37941236 | 453 | /* --- Sequence number checking --- */ |
454 | ||
455 | typedef struct seqwin { | |
456 | uint32 seq; /* First acceptable input sequence */ | |
457 | uint32 win; /* Window of acceptable numbers */ | |
458 | } seqwin; | |
459 | ||
460 | #define SEQ_WINSZ 32 /* Bits in sequence number window */ | |
461 | ||
410c8acf | 462 | /* --- A symmetric keyset --- * |
463 | * | |
464 | * A keyset contains a set of symmetric keys for encrypting and decrypting | |
465 | * packets. Keysets are stored in a list, sorted in reverse order of | |
466 | * creation, so that the most recent keyset (the one most likely to be used) | |
467 | * is first. | |
468 | * | |
469 | * Each keyset has a time limit and a data limit. The keyset is destroyed | |
470 | * when either it has existed for too long, or it has been used to encrypt | |
471 | * too much data. New key exchanges are triggered when keys are close to | |
472 | * expiry. | |
473 | */ | |
474 | ||
c70a7c5c MW |
475 | enum { DIR_IN, DIR_OUT, NDIR }; |
476 | ||
a93aacce | 477 | struct keyset { |
410c8acf | 478 | struct keyset *next; /* Next active keyset in the list */ |
832a2ab6 | 479 | unsigned ref; /* Reference count for keyset */ |
9466fafa | 480 | struct peer *p; /* Pointer to peer structure */ |
410c8acf | 481 | time_t t_exp; /* Expiry time for this keyset */ |
383a9d71 | 482 | unsigned long sz_exp, sz_regen; /* Data limits for the keyset */ |
832a2ab6 | 483 | T( unsigned seq; ) /* Sequence number for tracing */ |
484 | unsigned f; /* Various useful flags */ | |
c70a7c5c | 485 | bulkctx *bulk; /* Bulk crypto transform */ |
1484d822 | 486 | uint32 oseq; /* Outbound sequence number */ |
37941236 | 487 | seqwin iseq; /* Inbound sequence number */ |
a93aacce | 488 | }; |
410c8acf | 489 | |
832a2ab6 | 490 | #define KSF_LISTEN 1u /* Don't encrypt packets yet */ |
491 | #define KSF_LINK 2u /* Key is in a linked list */ | |
492 | ||
a50f9a0e MW |
493 | #define KSERR_REGEN -1 /* Regenerate keys */ |
494 | #define KSERR_NOKEYS -2 /* No keys left */ | |
495 | #define KSERR_DECRYPT -3 /* Unable to decrypt message */ | |
12a26b8b MW |
496 | #define KSERR_SEQ -4 /* Incorrect sequence number */ |
497 | #define KSERR_MALFORMED -5 /* Input ciphertext is broken */ | |
a50f9a0e | 498 | |
410c8acf | 499 | /* --- Key exchange --- * |
500 | * | |
501 | * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers | |
502 | * Protocol has a number of desirable features (e.g., perfect forward | |
503 | * secrecy, and zero-knowledge authentication) which make it attractive for | |
504 | * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and | |
505 | * Clive Jones. | |
506 | */ | |
507 | ||
a06d57a3 MW |
508 | typedef struct retry { |
509 | double t; /* Current retry time */ | |
510 | } retry; | |
511 | ||
832a2ab6 | 512 | #define KX_NCHAL 16u |
832a2ab6 | 513 | |
514 | typedef struct kxchal { | |
515 | struct keyexch *kx; /* Pointer back to key exchange */ | |
5b9f3d37 MW |
516 | dhge *C; /* Responder's challenge */ |
517 | dhge *R; /* My reply to the challenge */ | |
832a2ab6 | 518 | keyset *ks; /* Pointer to temporary keyset */ |
519 | unsigned f; /* Various useful flags */ | |
520 | sel_timer t; /* Response timer for challenge */ | |
a06d57a3 | 521 | retry rs; /* Retry state */ |
b5c45da1 | 522 | octet hc[MAXHASHSZ]; /* Hash of his challenge */ |
de7bd20b | 523 | octet ck[MAXHASHSZ]; /* His magical check value */ |
b5c45da1 | 524 | octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */ |
525 | octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */ | |
526 | octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */ | |
527 | octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */ | |
832a2ab6 | 528 | } kxchal; |
529 | ||
410c8acf | 530 | typedef struct keyexch { |
410c8acf | 531 | struct peer *p; /* Pointer back to the peer */ |
35c8b547 MW |
532 | kdata *kpriv; /* Private key and related info */ |
533 | kdata *kpub; /* Peer's public key */ | |
832a2ab6 | 534 | keyset **ks; /* Peer's list of keysets */ |
410c8acf | 535 | unsigned f; /* Various useful flags */ |
832a2ab6 | 536 | unsigned s; /* Current state in exchange */ |
410c8acf | 537 | sel_timer t; /* Timer for next exchange */ |
a06d57a3 | 538 | retry rs; /* Retry state */ |
5b9f3d37 MW |
539 | dhsc *a; /* My temporary secret */ |
540 | dhge *C; /* My challenge */ | |
541 | dhge *RX; /* The expected response */ | |
832a2ab6 | 542 | unsigned nr; /* Number of extant responses */ |
410c8acf | 543 | time_t t_valid; /* When this exchange goes bad */ |
b5c45da1 | 544 | octet hc[MAXHASHSZ]; /* Hash of my challenge */ |
832a2ab6 | 545 | kxchal *r[KX_NCHAL]; /* Array of challenges */ |
410c8acf | 546 | } keyexch; |
547 | ||
548 | #define KXF_TIMER 1u /* Waiting for a timer to go off */ | |
00e64b67 | 549 | #define KXF_DEAD 2u /* The key-exchanger isn't up */ |
550 | #define KXF_PUBKEY 4u /* Key exchanger has a public key */ | |
010e6f63 | 551 | #define KXF_CORK 8u /* Don't send anything yet */ |
832a2ab6 | 552 | |
553 | enum { | |
554 | KXS_DEAD, /* Uninitialized state (magical) */ | |
555 | KXS_CHAL, /* Main answer-challenges state */ | |
556 | KXS_COMMIT, /* Committed: send switch request */ | |
557 | KXS_SWITCH /* Switched: send confirmation */ | |
558 | }; | |
410c8acf | 559 | |
560 | /* --- Tunnel structure --- * | |
561 | * | |
562 | * Used to maintain system-specific information about the tunnel interface. | |
563 | */ | |
564 | ||
42da2a58 | 565 | typedef struct tunnel tunnel; |
566 | struct peer; | |
110d564e | 567 | |
42da2a58 | 568 | typedef struct tunnel_ops { |
569 | const char *name; /* Name of this tunnel driver */ | |
388e0319 MW |
570 | unsigned flags; /* Various interesting flags */ |
571 | #define TUNF_PRIVOPEN 1u /* Need helper to open file */ | |
42da2a58 | 572 | void (*init)(void); /* Initializes the system */ |
eb5f3fea | 573 | tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/); |
72917fe7 MW |
574 | /* Initializes a new tunnel */ |
575 | void (*setifname)(tunnel */*t*/, const char */*ifn*/); | |
576 | /* Notifies ifname change */ | |
42da2a58 | 577 | void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */ |
578 | void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */ | |
579 | } tunnel_ops; | |
b9066fbb | 580 | |
42da2a58 | 581 | #ifndef TUN_INTERNALS |
582 | struct tunnel { const tunnel_ops *ops; }; | |
410c8acf | 583 | #endif |
410c8acf | 584 | |
832a2ab6 | 585 | /* --- Peer statistics --- * |
586 | * | |
587 | * Contains various interesting and not-so-interesting statistics about a | |
588 | * peer. This is updated by various parts of the code. The format of the | |
589 | * structure isn't considered private, and @p_stats@ returns a pointer to the | |
590 | * statistics block for a given peer. | |
591 | */ | |
592 | ||
593 | typedef struct stats { | |
594 | unsigned long sz_in, sz_out; /* Size of all data in and out */ | |
595 | unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */ | |
596 | unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */ | |
3cdc3f3a | 597 | time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */ |
832a2ab6 | 598 | unsigned long n_reject; /* Number of rejected packets */ |
599 | unsigned long n_in, n_out; /* Number of packets in and out */ | |
600 | unsigned long n_kxin, n_kxout; /* Number of key exchange packets */ | |
601 | unsigned long n_ipin, n_ipout; /* Number of encrypted packets */ | |
602 | } stats; | |
603 | ||
410c8acf | 604 | /* --- Peer structure --- * |
605 | * | |
606 | * The main structure which glues everything else together. | |
607 | */ | |
608 | ||
0ba8de86 | 609 | typedef struct peerspec { |
610 | char *name; /* Peer's name */ | |
fe2a5dcf | 611 | char *privtag; /* Private key tag */ |
48b84569 | 612 | char *tag; /* Public key tag */ |
8362ac1c | 613 | char *knock; /* Knock string, or null */ |
0ba8de86 | 614 | const tunnel_ops *tops; /* Tunnel operations */ |
615 | unsigned long t_ka; /* Keep alive interval */ | |
616 | addr sa; /* Socket address to speak to */ | |
8743c776 | 617 | unsigned f; /* Flags for the peer */ |
6411163d MW |
618 | #define PSF_KXMASK 255u /* Key-exchange flags to set */ |
619 | #define PSF_MOBILE 256u /* Address may change rapidly */ | |
067aa5f0 | 620 | #define PSF_EPHEM 512u /* Association is ephemeral */ |
0ba8de86 | 621 | } peerspec; |
622 | ||
c8e02c8a MW |
623 | typedef struct peer_byname { |
624 | sym_base _b; | |
625 | struct peer *p; | |
626 | } peer_byname; | |
627 | ||
628 | typedef struct peer_byaddr { | |
629 | addrmap_base _b; | |
630 | struct peer *p; | |
631 | } peer_byaddr; | |
632 | ||
410c8acf | 633 | typedef struct peer { |
c8e02c8a MW |
634 | peer_byname *byname; /* Lookup-by-name block */ |
635 | peer_byaddr *byaddr; /* Lookup-by-address block */ | |
0ba8de86 | 636 | struct ping *pings; /* Pings we're waiting for */ |
637 | peerspec spec; /* Specifications for this peer */ | |
42da2a58 | 638 | tunnel *t; /* Tunnel for local packets */ |
64cf2223 | 639 | char *ifname; /* Interface name for tunnel */ |
410c8acf | 640 | keyset *ks; /* List head for keysets */ |
410c8acf | 641 | buf b; /* Buffer for sending packets */ |
832a2ab6 | 642 | stats st; /* Statistics */ |
643 | keyexch kx; /* Key exchange protocol block */ | |
0ba8de86 | 644 | sel_timer tka; /* Timer for keepalives */ |
410c8acf | 645 | } peer; |
646 | ||
c8e02c8a MW |
647 | typedef struct peer_iter { sym_iter i; } peer_iter; |
648 | ||
0ba8de86 | 649 | typedef struct ping { |
650 | struct ping *next, *prev; /* Links to next and previous */ | |
651 | peer *p; /* Peer so we can free it */ | |
652 | unsigned msg; /* Kind of response expected */ | |
653 | uint32 id; /* Id so we can recognize response */ | |
654 | octet magic[32]; /* Some random data */ | |
655 | sel_timer t; /* Timeout for ping */ | |
656 | void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */ | |
657 | void *arg; /* Argument for callback */ | |
658 | } ping; | |
659 | ||
660 | enum { | |
661 | PING_NONOTIFY = -1, | |
662 | PING_OK = 0, | |
663 | PING_TIMEOUT, | |
664 | PING_PEERDIED, | |
665 | PING_MAX | |
666 | }; | |
667 | ||
410c8acf | 668 | /* --- Admin structure --- */ |
669 | ||
fd3cf232 | 670 | #define OBUFSZ 16384u |
671 | ||
672 | typedef struct obuf { | |
673 | struct obuf *next; /* Next buffer in list */ | |
674 | char *p_in, *p_out; /* Pointers into the buffer */ | |
675 | char buf[OBUFSZ]; /* The actual buffer */ | |
676 | } obuf; | |
677 | ||
de014da6 | 678 | typedef struct oqueue { |
679 | obuf *hd, *tl; /* Head and tail pointers */ | |
680 | } oqueue; | |
681 | ||
682 | struct admin; | |
683 | ||
684 | typedef struct admin_bgop { | |
685 | struct admin_bgop *next, *prev; /* Links to next and previous */ | |
686 | struct admin *a; /* Owner job */ | |
687 | char *tag; /* Tag string for messages */ | |
688 | void (*cancel)(struct admin_bgop *); /* Destructor function */ | |
689 | } admin_bgop; | |
690 | ||
37941236 | 691 | typedef struct admin_resop { |
de014da6 | 692 | admin_bgop bg; /* Background operation header */ |
37941236 | 693 | char *addr; /* Hostname to be resolved */ |
de014da6 | 694 | bres_client r; /* Background resolver task */ |
695 | sel_timer t; /* Timer for resolver */ | |
37941236 | 696 | addr sa; /* Socket address */ |
697 | size_t sasz; /* Socket address size */ | |
698 | void (*func)(struct admin_resop *, int); /* Handler */ | |
699 | } admin_resop; | |
700 | ||
701 | enum { ARES_OK, ARES_FAIL }; | |
702 | ||
703 | typedef struct admin_addop { | |
704 | admin_resop r; /* Name resolution header */ | |
705 | peerspec peer; /* Peer pending creation */ | |
de014da6 | 706 | } admin_addop; |
707 | ||
708 | typedef struct admin_pingop { | |
709 | admin_bgop bg; /* Background operation header */ | |
710 | ping ping; /* Ping pending response */ | |
711 | struct timeval pingtime; /* Time last ping was sent */ | |
be6a1b7a MW |
712 | } admin_pingop; |
713 | ||
714 | typedef struct admin_service { | |
715 | sym_base _b; /* Hash table base structure */ | |
716 | char *version; /* The provided version */ | |
717 | struct admin *prov; /* Which client provides me */ | |
718 | struct admin_service *next, *prev; /* Client's list of services */ | |
719 | } admin_service; | |
de014da6 | 720 | |
5d46c0f8 MW |
721 | typedef struct admin_svcop { |
722 | admin_bgop bg; /* Background operation header */ | |
723 | struct admin *prov; /* Client servicing this job */ | |
cc921fba | 724 | unsigned index; /* This job's index */ |
5d46c0f8 MW |
725 | struct admin_svcop *next, *prev; /* Links for provider's jobs */ |
726 | } admin_svcop; | |
727 | ||
728 | typedef struct admin_jobentry { | |
729 | unsigned short seq; /* Zero if unused */ | |
730 | union { | |
731 | admin_svcop *op; /* Operation, if slot in use, ... */ | |
732 | uint32 next; /* ... or index of next free slot */ | |
733 | } u; | |
734 | } admin_jobentry; | |
735 | ||
736 | typedef struct admin_jobtable { | |
737 | uint32 n, sz; /* Used slots and table size */ | |
738 | admin_svcop *active; /* List of active jobs */ | |
739 | uint32 free; /* Index of first free slot */ | |
740 | admin_jobentry *v; /* And the big array of entries */ | |
741 | } admin_jobtable; | |
742 | ||
c70a7c5c | 743 | struct admin { |
410c8acf | 744 | struct admin *next, *prev; /* Links to next and previous */ |
fd3cf232 | 745 | unsigned f; /* Various useful flags */ |
060ca767 | 746 | unsigned ref; /* Reference counter */ |
410c8acf | 747 | #ifndef NTRACE |
748 | unsigned seq; /* Sequence number for tracing */ | |
749 | #endif | |
de014da6 | 750 | oqueue out; /* Output buffer list */ |
751 | oqueue delay; /* Delayed output buffer list */ | |
752 | admin_bgop *bg; /* Backgrounded operations */ | |
be6a1b7a | 753 | admin_service *svcs; /* Which services I provide */ |
5d46c0f8 | 754 | admin_jobtable j; /* Table of outstanding jobs */ |
fd3cf232 | 755 | selbuf b; /* Line buffer for commands */ |
756 | sel_file w; /* Selector for write buffering */ | |
c70a7c5c | 757 | }; |
410c8acf | 758 | |
fd3cf232 | 759 | #define AF_DEAD 1u /* Destroy this admin block */ |
060ca767 | 760 | #define AF_CLOSE 2u /* Client closed connection */ |
3cdc3f3a | 761 | #define AF_NOTE 4u /* Catch notifications */ |
de014da6 | 762 | #define AF_WARN 8u /* Catch warning messages */ |
3cdc3f3a | 763 | #ifndef NTRACE |
220ba5e4 | 764 | # define AF_TRACE 16u /* Catch tracing */ |
3cdc3f3a | 765 | #endif |
46dde080 | 766 | #define AF_FOREGROUND 32u /* Quit server when client closes */ |
3cdc3f3a | 767 | |
768 | #ifndef NTRACE | |
769 | # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN) | |
770 | #else | |
771 | # define AF_ALLMSGS (AF_NOTE | AF_WARN) | |
772 | #endif | |
fd3cf232 | 773 | |
410c8acf | 774 | /*----- Global variables --------------------------------------------------*/ |
775 | ||
776 | extern sel_state sel; /* Global I/O event state */ | |
a4b808b0 | 777 | extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ]; |
42da2a58 | 778 | extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */ |
779 | extern const tunnel_ops *tun_default; /* Default tunnel to use */ | |
799e58b9 MW |
780 | extern kdata *master; /* Default private key */ |
781 | extern const char *tag_priv; /* Default private key tag */ | |
410c8acf | 782 | |
783 | #ifndef NTRACE | |
784 | extern const trace_opt tr_opts[]; /* Trace options array */ | |
785 | extern unsigned tr_flags; /* Trace options flags */ | |
786 | #endif | |
787 | ||
8d0c7a83 | 788 | /*----- Other macros ------------------------------------------------------*/ |
789 | ||
36b9f99a MW |
790 | #define QUICKRAND \ |
791 | do { rand_quick(RAND_GLOBAL); noise_timer(RAND_GLOBAL); } while (0) | |
8d0c7a83 | 792 | |
410c8acf | 793 | /*----- Key management ----------------------------------------------------*/ |
794 | ||
799e58b9 MW |
795 | /* --- @km_init@ --- * |
796 | * | |
797 | * Arguments: @const char *privkr@ = private keyring file | |
798 | * @const char *pubkr@ = public keyring file | |
799 | * @const char *ptag@ = default private-key tag | |
800 | * | |
801 | * Returns: --- | |
802 | * | |
803 | * Use: Initializes the key-management machinery, loading the | |
804 | * keyrings and so on. | |
805 | */ | |
806 | ||
807 | extern void km_init(const char */*privkr*/, const char */*pubkr*/, | |
808 | const char */*ptag*/); | |
809 | ||
de014da6 | 810 | /* --- @km_reload@ --- * |
410c8acf | 811 | * |
812 | * Arguments: --- | |
813 | * | |
814 | * Returns: Zero if OK, nonzero to force reloading of keys. | |
815 | * | |
de014da6 | 816 | * Use: Checks the keyrings to see if they need reloading. |
410c8acf | 817 | */ |
818 | ||
de014da6 | 819 | extern int km_reload(void); |
410c8acf | 820 | |
799e58b9 MW |
821 | /* --- @km_findpub@, @km_findpriv@ --- * |
822 | * | |
823 | * Arguments: @const char *tag@ = key tag to load | |
824 | * | |
825 | * Returns: Pointer to the kdata object if successful, or null on error. | |
826 | * | |
827 | * Use: Fetches a public or private key from the keyring. | |
828 | */ | |
829 | ||
830 | extern kdata *km_findpub(const char */*tag*/); | |
831 | extern kdata *km_findpriv(const char */*tag*/); | |
832 | ||
fb6a9f13 MW |
833 | /* --- @km_findpubbyid@, @km_findprivbyid@ --- * |
834 | * | |
835 | * Arguments: @uint32 id@ = key id to load | |
836 | * | |
837 | * Returns: Pointer to the kdata object if successful, or null on error. | |
838 | * | |
839 | * Use: Fetches a public or private key from the keyring given its | |
840 | * numeric id. | |
841 | */ | |
842 | ||
843 | extern kdata *km_findpubbyid(uint32 /*id*/); | |
844 | extern kdata *km_findprivbyid(uint32 /*id*/); | |
845 | ||
799e58b9 MW |
846 | /* --- @km_samealgsp@ --- * |
847 | * | |
848 | * Arguments: @const kdata *kdx, *kdy@ = two key data objects | |
410c8acf | 849 | * |
799e58b9 MW |
850 | * Returns: Nonzero if their two algorithm selections are the same. |
851 | * | |
852 | * Use: Checks sameness of algorithm selections: used to ensure that | |
853 | * peers are using sensible algorithms. | |
854 | */ | |
855 | ||
856 | extern int km_samealgsp(const kdata */*kdx*/, const kdata */*kdy*/); | |
857 | ||
858 | /* --- @km_ref@ --- * | |
410c8acf | 859 | * |
799e58b9 | 860 | * Arguments: @kdata *kd@ = pointer to the kdata object |
410c8acf | 861 | * |
862 | * Returns: --- | |
863 | * | |
799e58b9 | 864 | * Use: Claim a new reference to a kdata object. |
410c8acf | 865 | */ |
866 | ||
799e58b9 | 867 | extern void km_ref(kdata */*kd*/); |
410c8acf | 868 | |
799e58b9 | 869 | /* --- @km_unref@ --- * |
410c8acf | 870 | * |
799e58b9 | 871 | * Arguments: @kdata *kd@ = pointer to the kdata object |
410c8acf | 872 | * |
799e58b9 | 873 | * Returns: --- |
410c8acf | 874 | * |
799e58b9 | 875 | * Use: Releases a reference to a kdata object. |
410c8acf | 876 | */ |
877 | ||
799e58b9 MW |
878 | extern void km_unref(kdata */*kd*/); |
879 | ||
880 | /* --- @km_tag@ --- * | |
881 | * | |
882 | * Arguments: @kdata *kd@ - pointer to the kdata object | |
883 | * | |
884 | * Returns: A pointer to the short tag by which the kdata was loaded. | |
410c8acf | 885 | */ |
886 | ||
799e58b9 | 887 | extern const char *km_tag(kdata */*kd*/); |
410c8acf | 888 | |
889 | /*----- Key exchange ------------------------------------------------------*/ | |
890 | ||
891 | /* --- @kx_start@ --- * | |
892 | * | |
893 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
de014da6 | 894 | * @int forcep@ = nonzero to ignore the quiet timer |
410c8acf | 895 | * |
896 | * Returns: --- | |
897 | * | |
898 | * Use: Stimulates a key exchange. If a key exchage is in progress, | |
899 | * a new challenge is sent (unless the quiet timer forbids | |
900 | * this); if no exchange is in progress, one is commenced. | |
901 | */ | |
902 | ||
de014da6 | 903 | extern void kx_start(keyexch */*kx*/, int /*forcep*/); |
410c8acf | 904 | |
832a2ab6 | 905 | /* --- @kx_message@ --- * |
410c8acf | 906 | * |
907 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
f6994bd0 | 908 | * @const addr *a@ = sender's IP address and port |
832a2ab6 | 909 | * @unsigned msg@ = the message code |
910 | * @buf *b@ = pointer to buffer containing the packet | |
410c8acf | 911 | * |
f6994bd0 | 912 | * Returns: Nonzero if the sender's address was unknown. |
410c8acf | 913 | * |
832a2ab6 | 914 | * Use: Reads a packet containing key exchange messages and handles |
915 | * it. | |
410c8acf | 916 | */ |
917 | ||
f6994bd0 MW |
918 | extern int kx_message(keyexch */*kx*/, const addr */*a*/, |
919 | unsigned /*msg*/, buf */*b*/); | |
410c8acf | 920 | |
921 | /* --- @kx_free@ --- * | |
922 | * | |
923 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
924 | * | |
925 | * Returns: --- | |
926 | * | |
927 | * Use: Frees everything in a key exchange context. | |
928 | */ | |
929 | ||
930 | extern void kx_free(keyexch */*kx*/); | |
931 | ||
932 | /* --- @kx_newkeys@ --- * | |
933 | * | |
934 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
935 | * | |
936 | * Returns: --- | |
937 | * | |
938 | * Use: Informs the key exchange module that its keys may have | |
939 | * changed. If fetching the new keys fails, the peer will be | |
940 | * destroyed, we log messages and struggle along with the old | |
941 | * keys. | |
942 | */ | |
943 | ||
944 | extern void kx_newkeys(keyexch */*kx*/); | |
945 | ||
0510f262 | 946 | /* --- @kx_setup@ --- * |
410c8acf | 947 | * |
948 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
949 | * @peer *p@ = pointer to peer context | |
950 | * @keyset **ks@ = pointer to keyset list | |
010e6f63 | 951 | * @unsigned f@ = various useful flags |
410c8acf | 952 | * |
953 | * Returns: Zero if OK, nonzero if it failed. | |
954 | * | |
955 | * Use: Initializes a key exchange module. The module currently | |
956 | * contains no keys, and will attempt to initiate a key | |
957 | * exchange. | |
958 | */ | |
959 | ||
0510f262 MW |
960 | extern int kx_setup(keyexch */*kx*/, peer */*p*/, |
961 | keyset **/*ks*/, unsigned /*f*/); | |
410c8acf | 962 | |
8362ac1c MW |
963 | /* --- @kx_init@ --- * |
964 | * | |
965 | * Arguments: --- | |
966 | * | |
967 | * Returns: --- | |
968 | * | |
969 | * Use: Initializes the key-exchange logic. | |
970 | */ | |
971 | ||
972 | extern void kx_init(void); | |
973 | ||
410c8acf | 974 | /*----- Keysets and symmetric cryptography --------------------------------*/ |
975 | ||
832a2ab6 | 976 | /* --- @ks_drop@ --- * |
977 | * | |
978 | * Arguments: @keyset *ks@ = pointer to a keyset | |
979 | * | |
980 | * Returns: --- | |
981 | * | |
982 | * Use: Decrements a keyset's reference counter. If the counter hits | |
983 | * zero, the keyset is freed. | |
984 | */ | |
985 | ||
986 | extern void ks_drop(keyset */*ks*/); | |
987 | ||
988 | /* --- @ks_gen@ --- * | |
989 | * | |
ef09dae1 | 990 | * Arguments: @deriveargs *a@ = key derivation parameters (modified) |
9466fafa | 991 | * @peer *p@ = pointer to peer information |
832a2ab6 | 992 | * |
993 | * Returns: A pointer to the new keyset. | |
994 | * | |
ef09dae1 MW |
995 | * Use: Derives a new keyset from the given key material. This will |
996 | * set the @what@, @f@, and @hc@ members in @*a@; other members | |
997 | * must be filled in by the caller. | |
832a2ab6 | 998 | * |
999 | * The new key is marked so that it won't be selected for output | |
1000 | * by @ksl_encrypt@. You can still encrypt data with it by | |
1001 | * calling @ks_encrypt@ directly. | |
1002 | */ | |
1003 | ||
ef09dae1 | 1004 | extern keyset *ks_gen(deriveargs */*a*/, peer */*p*/); |
832a2ab6 | 1005 | |
832a2ab6 | 1006 | /* --- @ks_activate@ --- * |
1007 | * | |
1008 | * Arguments: @keyset *ks@ = pointer to a keyset | |
1009 | * | |
1010 | * Returns: --- | |
1011 | * | |
1012 | * Use: Activates a keyset, so that it can be used for encrypting | |
1013 | * outgoing messages. | |
1014 | */ | |
1015 | ||
1016 | extern void ks_activate(keyset */*ks*/); | |
1017 | ||
1018 | /* --- @ks_encrypt@ --- * | |
1019 | * | |
1020 | * Arguments: @keyset *ks@ = pointer to a keyset | |
7ed14135 | 1021 | * @unsigned ty@ = message type |
832a2ab6 | 1022 | * @buf *b@ = pointer to input buffer |
1023 | * @buf *bb@ = pointer to output buffer | |
1024 | * | |
a50f9a0e MW |
1025 | * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a |
1026 | * new key; @KSERR_NOKEYS@ if the key is not usable. Also | |
1027 | * returns zero if there was insufficient buffer (but the output | |
1028 | * buffer is broken in this case). | |
832a2ab6 | 1029 | * |
1030 | * Use: Encrypts a block of data using the key. Note that the `key | |
1031 | * ought to be replaced' notification is only ever given once | |
1032 | * for each key. Also note that this call forces a keyset to be | |
1033 | * used even if it's marked as not for data output. | |
a93aacce MW |
1034 | * |
1035 | * The encryption transform is permitted to corrupt @buf_u@ for | |
1036 | * its own purposes. Neither the source nor destination should | |
1037 | * be within @buf_u@; and callers mustn't expect anything stored | |
1038 | * in @buf_u@ to still | |
832a2ab6 | 1039 | */ |
1040 | ||
7ed14135 | 1041 | extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/, |
1042 | buf */*b*/, buf */*bb*/); | |
832a2ab6 | 1043 | |
1044 | /* --- @ks_decrypt@ --- * | |
1045 | * | |
1046 | * Arguments: @keyset *ks@ = pointer to a keyset | |
7ed14135 | 1047 | * @unsigned ty@ = expected type code |
832a2ab6 | 1048 | * @buf *b@ = pointer to an input buffer |
1049 | * @buf *bb@ = pointer to an output buffer | |
1050 | * | |
a50f9a0e MW |
1051 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns |
1052 | * zero if there was insufficient buffer (but the output buffer | |
1053 | * is broken in this case). | |
832a2ab6 | 1054 | * |
1055 | * Use: Attempts to decrypt a message using a given key. Note that | |
1056 | * requesting decryption with a key directly won't clear a | |
1057 | * marking that it's not for encryption. | |
a93aacce MW |
1058 | * |
1059 | * The decryption transform is permitted to corrupt @buf_u@ for | |
1060 | * its own purposes. Neither the source nor destination should | |
1061 | * be within @buf_u@; and callers mustn't expect anything stored | |
1062 | * in @buf_u@ to still | |
832a2ab6 | 1063 | */ |
1064 | ||
7ed14135 | 1065 | extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/, |
1066 | buf */*b*/, buf */*bb*/); | |
832a2ab6 | 1067 | |
1068 | /* --- @ksl_free@ --- * | |
410c8acf | 1069 | * |
1070 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
1071 | * | |
1072 | * Returns: --- | |
1073 | * | |
832a2ab6 | 1074 | * Use: Frees (releases references to) all of the keys in a keyset. |
410c8acf | 1075 | */ |
1076 | ||
832a2ab6 | 1077 | extern void ksl_free(keyset **/*ksroot*/); |
410c8acf | 1078 | |
832a2ab6 | 1079 | /* --- @ksl_link@ --- * |
410c8acf | 1080 | * |
1081 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
832a2ab6 | 1082 | * @keyset *ks@ = pointer to a keyset |
410c8acf | 1083 | * |
1084 | * Returns: --- | |
1085 | * | |
832a2ab6 | 1086 | * Use: Links a keyset into a list. A keyset can only be on one list |
1087 | * at a time. Bad things happen otherwise. | |
410c8acf | 1088 | */ |
1089 | ||
832a2ab6 | 1090 | extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/); |
410c8acf | 1091 | |
832a2ab6 | 1092 | /* --- @ksl_prune@ --- * |
410c8acf | 1093 | * |
1094 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
410c8acf | 1095 | * |
832a2ab6 | 1096 | * Returns: --- |
410c8acf | 1097 | * |
832a2ab6 | 1098 | * Use: Prunes the keyset list by removing keys which mustn't be used |
1099 | * any more. | |
410c8acf | 1100 | */ |
1101 | ||
832a2ab6 | 1102 | extern void ksl_prune(keyset **/*ksroot*/); |
410c8acf | 1103 | |
832a2ab6 | 1104 | /* --- @ksl_encrypt@ --- * |
410c8acf | 1105 | * |
1106 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
7ed14135 | 1107 | * @unsigned ty@ = message type |
410c8acf | 1108 | * @buf *b@ = pointer to input buffer |
1109 | * @buf *bb@ = pointer to output buffer | |
1110 | * | |
a50f9a0e MW |
1111 | * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a |
1112 | * new key; @KSERR_NOKEYS@ if there are no suitable keys | |
1113 | * available. Also returns zero if there was insufficient | |
1114 | * buffer space (but the output buffer is broken in this case). | |
410c8acf | 1115 | * |
1116 | * Use: Encrypts a packet. | |
1117 | */ | |
1118 | ||
7ed14135 | 1119 | extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/, |
1120 | buf */*b*/, buf */*bb*/); | |
410c8acf | 1121 | |
832a2ab6 | 1122 | /* --- @ksl_decrypt@ --- * |
410c8acf | 1123 | * |
1124 | * Arguments: @keyset **ksroot@ = pointer to keyset list head | |
7ed14135 | 1125 | * @unsigned ty@ = expected type code |
410c8acf | 1126 | * @buf *b@ = pointer to input buffer |
1127 | * @buf *bb@ = pointer to output buffer | |
1128 | * | |
a50f9a0e MW |
1129 | * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns |
1130 | * zero if there was insufficient buffer (but the output buffer | |
1131 | * is broken in this case). | |
410c8acf | 1132 | * |
1133 | * Use: Decrypts a packet. | |
1134 | */ | |
1135 | ||
7ed14135 | 1136 | extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/, |
1137 | buf */*b*/, buf */*bb*/); | |
410c8acf | 1138 | |
37941236 | 1139 | /*----- Challenges --------------------------------------------------------*/ |
1140 | ||
1141 | /* --- @c_new@ --- * | |
1142 | * | |
3deadf73 MW |
1143 | * Arguments: @const void *m@ = pointer to associated message, or null |
1144 | * @size_t msz@ = length of associated message | |
1145 | * @buf *b@ = where to put the challenge | |
37941236 | 1146 | * |
1147 | * Returns: Zero if OK, nonzero on error. | |
1148 | * | |
1149 | * Use: Issues a new challenge. | |
1150 | */ | |
1151 | ||
3deadf73 | 1152 | extern int c_new(const void */*m*/, size_t /*msz*/, buf */*b*/); |
37941236 | 1153 | |
1154 | /* --- @c_check@ --- * | |
1155 | * | |
3deadf73 MW |
1156 | * Arguments: @const void *m@ = pointer to associated message, or null |
1157 | * @size_t msz@ = length of associated message | |
1158 | * @buf *b@ = where to find the challenge | |
37941236 | 1159 | * |
1160 | * Returns: Zero if OK, nonzero if it didn't work. | |
1161 | * | |
1162 | * Use: Checks a challenge. On failure, the buffer is broken. | |
1163 | */ | |
1164 | ||
3deadf73 | 1165 | extern int c_check(const void */*m*/, size_t /*msz*/, buf */*b*/); |
37941236 | 1166 | |
410c8acf | 1167 | /*----- Administration interface ------------------------------------------*/ |
1168 | ||
f43df819 MW |
1169 | #define A_END ((char *)0) |
1170 | ||
7f73baaf MW |
1171 | /* --- @a_vformat@ --- * |
1172 | * | |
1173 | * Arguments: @dstr *d@ = where to leave the formatted message | |
1174 | * @const char *fmt@ = pointer to format string | |
b730d38c | 1175 | * @va_list *ap@ = arguments in list |
7f73baaf MW |
1176 | * |
1177 | * Returns: --- | |
1178 | * | |
1179 | * Use: Main message token formatting driver. The arguments are | |
1180 | * interleaved formatting tokens and their parameters, finally | |
1181 | * terminated by an entry @A_END@. | |
1182 | * | |
1183 | * Tokens recognized: | |
1184 | * | |
1185 | * * "*..." ... -- pretokenized @dstr_putf@-like string | |
1186 | * | |
1187 | * * "?ADDR" SOCKADDR -- a socket address, to be converted | |
1188 | * | |
1189 | * * "?B64" BUFFER SIZE -- binary data to be base64-encoded | |
1190 | * | |
1191 | * * "?TOKENS" VECTOR -- null-terminated vector of tokens | |
1192 | * | |
1193 | * * "?PEER" PEER -- peer's name | |
1194 | * | |
1195 | * * "?ERRNO" ERRNO -- system error code | |
1196 | * | |
1197 | * * "[!]..." ... -- @dstr_putf@-like string as single token | |
1198 | */ | |
1199 | ||
b730d38c | 1200 | extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list */*ap*/); |
7f73baaf | 1201 | |
f241e36c MW |
1202 | /* --- @a_format@ --- * |
1203 | * | |
1204 | * Arguments: @dstr *d@ = where to leave the formatted message | |
1205 | * @const char *fmt@ = pointer to format string | |
1206 | * | |
1207 | * Returns: --- | |
1208 | * | |
1209 | * Use: Writes a tokenized message into a string, for later | |
1210 | * presentation. | |
1211 | */ | |
1212 | ||
ddb384f1 | 1213 | extern void EXECL_LIKE(0) a_format(dstr */*d*/, const char */*fmt*/, ...); |
f241e36c | 1214 | |
fe182f61 MW |
1215 | /* --- @a_info@ --- * |
1216 | * | |
1217 | * Arguments: @admin *a@ = connection | |
1218 | * @const char *fmt@ = format string | |
1219 | * @...@ = other arguments | |
1220 | * | |
1221 | * Returns: --- | |
1222 | * | |
1223 | * Use: Report information to an admin client. | |
1224 | */ | |
1225 | ||
1226 | extern void EXECL_LIKE(0) a_info(admin */*a*/, const char */*fmt*/, ...); | |
1227 | ||
410c8acf | 1228 | /* --- @a_warn@ --- * |
1229 | * | |
1230 | * Arguments: @const char *fmt@ = pointer to format string | |
1231 | * @...@ = other arguments | |
1232 | * | |
1233 | * Returns: --- | |
1234 | * | |
1235 | * Use: Informs all admin connections of a warning. | |
1236 | */ | |
1237 | ||
ddb384f1 | 1238 | extern void EXECL_LIKE(0) a_warn(const char */*fmt*/, ...); |
410c8acf | 1239 | |
3cdc3f3a | 1240 | /* --- @a_notify@ --- * |
1241 | * | |
1242 | * Arguments: @const char *fmt@ = pointer to format string | |
1243 | * @...@ = other arguments | |
1244 | * | |
1245 | * Returns: --- | |
1246 | * | |
1247 | * Use: Sends a notification to interested admin connections. | |
1248 | */ | |
1249 | ||
ddb384f1 | 1250 | extern void EXECL_LIKE(0) a_notify(const char */*fmt*/, ...); |
3cdc3f3a | 1251 | |
410c8acf | 1252 | /* --- @a_create@ --- * |
1253 | * | |
1254 | * Arguments: @int fd_in, fd_out@ = file descriptors to use | |
3cdc3f3a | 1255 | * @unsigned f@ = initial flags to set |
410c8acf | 1256 | * |
1257 | * Returns: --- | |
1258 | * | |
1259 | * Use: Creates a new admin connection. | |
1260 | */ | |
1261 | ||
3cdc3f3a | 1262 | extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/); |
410c8acf | 1263 | |
1264 | /* --- @a_quit@ --- * | |
1265 | * | |
1266 | * Arguments: --- | |
1267 | * | |
1268 | * Returns: --- | |
1269 | * | |
1270 | * Use: Shuts things down nicely. | |
1271 | */ | |
1272 | ||
1273 | extern void a_quit(void); | |
1274 | ||
c511e1f9 MW |
1275 | /* --- @a_preselect@ --- * |
1276 | * | |
1277 | * Arguments: --- | |
1278 | * | |
1279 | * Returns: --- | |
1280 | * | |
1281 | * Use: Informs the admin module that we're about to select again, | |
1282 | * and that it should do cleanup things it has delayed until a | |
1283 | * `safe' time. | |
1284 | */ | |
1285 | ||
1286 | extern void a_preselect(void); | |
1287 | ||
410c8acf | 1288 | /* --- @a_daemon@ --- * |
1289 | * | |
1290 | * Arguments: --- | |
1291 | * | |
1292 | * Returns: --- | |
1293 | * | |
1294 | * Use: Informs the admin module that it's a daemon. | |
1295 | */ | |
1296 | ||
1297 | extern void a_daemon(void); | |
1298 | ||
1299 | /* --- @a_init@ --- * | |
1300 | * | |
1301 | * Arguments: @const char *sock@ = socket name to create | |
aa2405e8 MW |
1302 | * @uid_t u@ = user to own the socket |
1303 | * @gid_t g@ = group to own the socket | |
a9279e37 | 1304 | * @mode_t m@ = permissions to set on the socket |
410c8acf | 1305 | * |
1306 | * Returns: --- | |
1307 | * | |
1308 | * Use: Creates the admin listening socket. | |
1309 | */ | |
1310 | ||
a9279e37 MW |
1311 | extern void a_init(const char */*sock*/, |
1312 | uid_t /*u*/, gid_t /*g*/, mode_t /*m*/); | |
410c8acf | 1313 | |
c8e02c8a MW |
1314 | /*----- Mapping with addresses as keys ------------------------------------*/ |
1315 | ||
1316 | /* --- @am_create@ --- * | |
1317 | * | |
1318 | * Arguments: @addrmap *m@ = pointer to map | |
1319 | * | |
1320 | * Returns: --- | |
1321 | * | |
1322 | * Use: Create an address map, properly set up. | |
1323 | */ | |
1324 | ||
1325 | extern void am_create(addrmap */*m*/); | |
1326 | ||
1327 | /* --- @am_destroy@ --- * | |
1328 | * | |
1329 | * Arguments: @addrmap *m@ = pointer to map | |
1330 | * | |
1331 | * Returns: --- | |
1332 | * | |
1333 | * Use: Destroy an address map, throwing away all the entries. | |
1334 | */ | |
1335 | ||
1336 | extern void am_destroy(addrmap */*m*/); | |
1337 | ||
1338 | /* --- @am_find@ --- * | |
1339 | * | |
1340 | * Arguments: @addrmap *m@ = pointer to map | |
1341 | * @const addr *a@ = address to look up | |
1342 | * @size_t sz@ = size of block to allocate | |
1343 | * @unsigned *f@ = where to store flags | |
1344 | * | |
1345 | * Returns: Pointer to found item, or null. | |
1346 | * | |
1347 | * Use: Finds a record with the given IP address, set @*f@ nonzero | |
1348 | * and returns it. If @sz@ is zero, and no match was found, | |
1349 | * return null; otherwise allocate a new block of @sz@ bytes, | |
1350 | * clear @*f@ to zero and return the block pointer. | |
1351 | */ | |
1352 | ||
1353 | extern void *am_find(addrmap */*m*/, const addr */*a*/, | |
1354 | size_t /*sz*/, unsigned */*f*/); | |
1355 | ||
1356 | /* --- @am_remove@ --- * | |
1357 | * | |
1358 | * Arguments: @addrmap *m@ = pointer to map | |
1359 | * @void *i@ = pointer to the item | |
1360 | * | |
1361 | * Returns: --- | |
1362 | * | |
1363 | * Use: Removes an item from the map. | |
1364 | */ | |
1365 | ||
1366 | extern void am_remove(addrmap */*m*/, void */*i*/); | |
1367 | ||
388e0319 MW |
1368 | /*----- Privilege separation ----------------------------------------------*/ |
1369 | ||
1370 | /* --- @ps_trace@ --- * | |
1371 | * | |
1372 | * Arguments: @unsigned mask@ = trace mask to check | |
1373 | * @const char *fmt@ = message format | |
1374 | * @...@ = values for placeholders | |
1375 | * | |
1376 | * Returns: --- | |
1377 | * | |
1378 | * Use: Writes a trace message. | |
1379 | */ | |
1380 | ||
ddb384f1 MW |
1381 | T( extern void PRINTF_LIKE(2, 3) |
1382 | ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); ) | |
388e0319 MW |
1383 | |
1384 | /* --- @ps_warn@ --- * | |
1385 | * | |
1386 | * Arguments: @const char *fmt@ = message format | |
1387 | * @...@ = values for placeholders | |
1388 | * | |
1389 | * Returns: --- | |
1390 | * | |
1391 | * Use: Writes a warning message. | |
1392 | */ | |
1393 | ||
ddb384f1 | 1394 | extern void PRINTF_LIKE(1, 2) ps_warn(const char */*fmt*/, ...); |
388e0319 MW |
1395 | |
1396 | /* --- @ps_tunfd@ --- * | |
1397 | * | |
1398 | * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations | |
1399 | * @char **ifn@ = where to put the interface name | |
1400 | * | |
1401 | * Returns: The file descriptor, or @-1@ on error. | |
1402 | * | |
1403 | * Use: Fetches a file descriptor for a tunnel driver. | |
1404 | */ | |
1405 | ||
1406 | extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/); | |
1407 | ||
1408 | /* --- @ps_split@ --- * | |
1409 | * | |
1410 | * Arguments: @int detachp@ = whether to detach the child from its terminal | |
1411 | * | |
1412 | * Returns: --- | |
1413 | * | |
1414 | * Use: Separates off the privileged tunnel-opening service from the | |
1415 | * rest of the server. | |
1416 | */ | |
1417 | ||
1418 | extern void ps_split(int /*detachp*/); | |
1419 | ||
1420 | /* --- @ps_quit@ --- * | |
1421 | * | |
1422 | * Arguments: --- | |
1423 | * | |
1424 | * Returns: --- | |
1425 | * | |
1426 | * Use: Detaches from the helper process. | |
1427 | */ | |
1428 | ||
1429 | extern void ps_quit(void); | |
1430 | ||
410c8acf | 1431 | /*----- Peer management ---------------------------------------------------*/ |
1432 | ||
23bbe601 MW |
1433 | /* --- @p_updateaddr@ --- * |
1434 | * | |
1435 | * Arguments: @peer *p@ = pointer to peer block | |
1436 | * @const addr *a@ = address to associate with this peer | |
1437 | * | |
1438 | * Returns: Zero if the address was changed; @+1@ if it was already | |
1439 | * right. | |
1440 | * | |
1441 | * Use: Updates our idea of @p@'s address. | |
1442 | */ | |
1443 | ||
1444 | extern int p_updateaddr(peer */*p*/, const addr */*a*/); | |
1445 | ||
410c8acf | 1446 | /* --- @p_txstart@ --- * |
1447 | * | |
1448 | * Arguments: @peer *p@ = pointer to peer block | |
1449 | * @unsigned msg@ = message type code | |
1450 | * | |
1451 | * Returns: A pointer to a buffer to write to. | |
1452 | * | |
1453 | * Use: Starts sending to a peer. Only one send can happen at a | |
1454 | * time. | |
1455 | */ | |
1456 | ||
1457 | extern buf *p_txstart(peer */*p*/, unsigned /*msg*/); | |
1458 | ||
8362ac1c MW |
1459 | /* --- @p_txaddr@ --- * |
1460 | * | |
1461 | * Arguments: @const addr *a@ = recipient address | |
1462 | * @const void *p@ = pointer to packet to send | |
1463 | * @size_t sz@ = length of packet | |
1464 | * | |
1465 | * Returns: Zero if successful, nonzero on error. | |
1466 | * | |
1467 | * Use: Sends a packet to an address which (possibly) isn't a current | |
1468 | * peer. | |
1469 | */ | |
1470 | ||
1471 | extern int p_txaddr(const addr */*a*/, const void */*p*/, size_t /*sz*/); | |
1472 | ||
410c8acf | 1473 | /* --- @p_txend@ --- * |
1474 | * | |
1475 | * Arguments: @peer *p@ = pointer to peer block | |
1476 | * | |
1477 | * Returns: --- | |
1478 | * | |
1479 | * Use: Sends a packet to the peer. | |
1480 | */ | |
1481 | ||
1482 | extern void p_txend(peer */*p*/); | |
1483 | ||
0ba8de86 | 1484 | /* --- @p_pingsend@ --- * |
1485 | * | |
1486 | * Arguments: @peer *p@ = destination peer | |
1487 | * @ping *pg@ = structure to fill in | |
1488 | * @unsigned type@ = message type | |
1489 | * @unsigned long timeout@ = how long to wait before giving up | |
1490 | * @void (*func)(int, void *)@ = callback function | |
1491 | * @void *arg@ = argument for callback | |
1492 | * | |
1493 | * Returns: Zero if successful, nonzero if it failed. | |
1494 | * | |
1495 | * Use: Sends a ping to a peer. Call @func@ with a nonzero argument | |
1496 | * if we get an answer within the timeout, or zero if no answer. | |
1497 | */ | |
1498 | ||
1499 | extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/, | |
1500 | unsigned long /*timeout*/, | |
1501 | void (*/*func*/)(int, void *), void */*arg*/); | |
1502 | ||
1503 | /* --- @p_pingdone@ --- * | |
1504 | * | |
1505 | * Arguments: @ping *p@ = ping structure | |
1506 | * @int rc@ = return code to pass on | |
1507 | * | |
1508 | * Returns: --- | |
1509 | * | |
1510 | * Use: Disposes of a ping structure, maybe sending a notification. | |
1511 | */ | |
1512 | ||
1513 | extern void p_pingdone(ping */*p*/, int /*rc*/); | |
1514 | ||
37941236 | 1515 | /* --- @p_greet@ --- * |
1516 | * | |
1517 | * Arguments: @peer *p@ = peer to send to | |
1518 | * @const void *c@ = pointer to challenge | |
1519 | * @size_t sz@ = size of challenge | |
1520 | * | |
1521 | * Returns: --- | |
1522 | * | |
1523 | * Use: Sends a greeting packet. | |
1524 | */ | |
1525 | ||
1526 | extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/); | |
1527 | ||
410c8acf | 1528 | /* --- @p_tun@ --- * |
1529 | * | |
1530 | * Arguments: @peer *p@ = pointer to peer block | |
1531 | * @buf *b@ = buffer containing incoming packet | |
1532 | * | |
1533 | * Returns: --- | |
1534 | * | |
1535 | * Use: Handles a packet which needs to be sent to a peer. | |
1536 | */ | |
1537 | ||
1538 | extern void p_tun(peer */*p*/, buf */*b*/); | |
1539 | ||
de014da6 | 1540 | /* --- @p_keyreload@ --- * |
1541 | * | |
1542 | * Arguments: --- | |
1543 | * | |
1544 | * Returns: --- | |
1545 | * | |
1546 | * Use: Forces a check of the daemon's keyring files. | |
1547 | */ | |
1548 | ||
1549 | extern void p_keyreload(void); | |
1550 | ||
410c8acf | 1551 | /* --- @p_interval@ --- * |
1552 | * | |
1553 | * Arguments: --- | |
1554 | * | |
1555 | * Returns: --- | |
1556 | * | |
1557 | * Use: Called periodically to do tidying. | |
1558 | */ | |
1559 | ||
1560 | extern void p_interval(void); | |
1561 | ||
832a2ab6 | 1562 | /* --- @p_stats@ --- * |
1563 | * | |
1564 | * Arguments: @peer *p@ = pointer to a peer block | |
1565 | * | |
1566 | * Returns: A pointer to the peer's statistics. | |
1567 | */ | |
1568 | ||
1569 | extern stats *p_stats(peer */*p*/); | |
1570 | ||
410c8acf | 1571 | /* --- @p_ifname@ --- * |
1572 | * | |
1573 | * Arguments: @peer *p@ = pointer to a peer block | |
1574 | * | |
1575 | * Returns: A pointer to the peer's interface name. | |
1576 | */ | |
1577 | ||
1578 | extern const char *p_ifname(peer */*p*/); | |
1579 | ||
64cf2223 MW |
1580 | /* --- @p_setifname@ --- * |
1581 | * | |
1582 | * Arguments: @peer *p@ = pointer to a peer block | |
1583 | * @const char *name@ = pointer to the new name | |
1584 | * | |
1585 | * Returns: --- | |
1586 | * | |
1587 | * Use: Changes the name held for a peer's interface. | |
1588 | */ | |
1589 | ||
1590 | extern void p_setifname(peer */*p*/, const char */*name*/); | |
1591 | ||
410c8acf | 1592 | /* --- @p_addr@ --- * |
1593 | * | |
1594 | * Arguments: @peer *p@ = pointer to a peer block | |
1595 | * | |
1596 | * Returns: A pointer to the peer's address. | |
1597 | */ | |
1598 | ||
1599 | extern const addr *p_addr(peer */*p*/); | |
1600 | ||
1601 | /* --- @p_init@ --- * | |
1602 | * | |
767b36e2 | 1603 | * Arguments: @struct in_addr addr@ = address to bind to |
1604 | * @unsigned port@ = port number to listen to | |
410c8acf | 1605 | * |
1606 | * Returns: --- | |
1607 | * | |
1608 | * Use: Initializes the peer system; creates the socket. | |
1609 | */ | |
1610 | ||
767b36e2 | 1611 | extern void p_init(struct in_addr /*addr*/, unsigned /*port*/); |
410c8acf | 1612 | |
1613 | /* --- @p_port@ --- * | |
1614 | * | |
1615 | * Arguments: --- | |
1616 | * | |
1617 | * Returns: Port number used for socket. | |
1618 | */ | |
1619 | ||
1620 | unsigned p_port(void); | |
1621 | ||
1622 | /* --- @p_create@ --- * | |
1623 | * | |
0ba8de86 | 1624 | * Arguments: @peerspec *spec@ = information about this peer |
410c8acf | 1625 | * |
1626 | * Returns: Pointer to the peer block, or null if it failed. | |
1627 | * | |
1628 | * Use: Creates a new named peer block. No peer is actually attached | |
1629 | * by this point. | |
1630 | */ | |
1631 | ||
0ba8de86 | 1632 | extern peer *p_create(peerspec */*spec*/); |
410c8acf | 1633 | |
1634 | /* --- @p_name@ --- * | |
1635 | * | |
1636 | * Arguments: @peer *p@ = pointer to a peer block | |
1637 | * | |
1638 | * Returns: A pointer to the peer's name. | |
060ca767 | 1639 | * |
1640 | * Use: Equivalent to @p_spec(p)->name@. | |
410c8acf | 1641 | */ |
1642 | ||
1643 | extern const char *p_name(peer */*p*/); | |
1644 | ||
48b84569 MW |
1645 | /* --- @p_tag@ --- * |
1646 | * | |
1647 | * Arguments: @peer *p@ = pointer to a peer block | |
1648 | * | |
1649 | * Returns: A pointer to the peer's public key tag. | |
1650 | */ | |
1651 | ||
1652 | extern const char *p_tag(peer */*p*/); | |
1653 | ||
fe2a5dcf MW |
1654 | /* --- @p_privtag@ --- * |
1655 | * | |
1656 | * Arguments: @peer *p@ = pointer to a peer block | |
1657 | * | |
1658 | * Returns: A pointer to the peer's private key tag. | |
1659 | */ | |
1660 | ||
1661 | extern const char *p_privtag(peer */*p*/); | |
1662 | ||
060ca767 | 1663 | /* --- @p_spec@ --- * |
1664 | * | |
1665 | * Arguments: @peer *p@ = pointer to a peer block | |
1666 | * | |
1667 | * Returns: Pointer to the peer's specification | |
1668 | */ | |
1669 | ||
1670 | extern const peerspec *p_spec(peer */*p*/); | |
1671 | ||
c8e02c8a MW |
1672 | /* --- @p_findbyaddr@ --- * |
1673 | * | |
1674 | * Arguments: @const addr *a@ = address to look up | |
1675 | * | |
1676 | * Returns: Pointer to the peer block, or null if not found. | |
1677 | * | |
1678 | * Use: Finds a peer by address. | |
1679 | */ | |
1680 | ||
1681 | extern peer *p_findbyaddr(const addr */*a*/); | |
1682 | ||
410c8acf | 1683 | /* --- @p_find@ --- * |
1684 | * | |
1685 | * Arguments: @const char *name@ = name to look up | |
1686 | * | |
1687 | * Returns: Pointer to the peer block, or null if not found. | |
1688 | * | |
1689 | * Use: Finds a peer by name. | |
1690 | */ | |
1691 | ||
1692 | extern peer *p_find(const char */*name*/); | |
1693 | ||
1694 | /* --- @p_destroy@ --- * | |
1695 | * | |
1696 | * Arguments: @peer *p@ = pointer to a peer | |
067aa5f0 | 1697 | * @int bye@ = say goodbye to the peer? |
410c8acf | 1698 | * |
1699 | * Returns: --- | |
1700 | * | |
1701 | * Use: Destroys a peer. | |
1702 | */ | |
1703 | ||
067aa5f0 | 1704 | extern void p_destroy(peer */*p*/, int /*bye*/); |
410c8acf | 1705 | |
c8e02c8a MW |
1706 | /* --- @FOREACH_PEER@ --- * |
1707 | * | |
1708 | * Arguments: @p@ = name to bind to each peer | |
1709 | * @stuff@ = thing to do for each item | |
1710 | * | |
1711 | * Use: Does something for each current peer. | |
1712 | */ | |
1713 | ||
1714 | #define FOREACH_PEER(p, stuff) do { \ | |
1715 | peer_iter i_; \ | |
1716 | peer *p; \ | |
46401b81 | 1717 | for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \ |
c8e02c8a MW |
1718 | } while (0) |
1719 | ||
1720 | /* --- @p_mkiter@ --- * | |
1721 | * | |
1722 | * Arguments: @peer_iter *i@ = pointer to an iterator | |
1723 | * | |
1724 | * Returns: --- | |
1725 | * | |
1726 | * Use: Initializes the iterator. | |
1727 | */ | |
1728 | ||
1729 | extern void p_mkiter(peer_iter */*i*/); | |
1730 | ||
1731 | /* --- @p_next@ --- * | |
1732 | * | |
1733 | * Arguments: @peer_iter *i@ = pointer to an iterator | |
410c8acf | 1734 | * |
c8e02c8a | 1735 | * Returns: Next peer, or null if at the end. |
410c8acf | 1736 | * |
c8e02c8a | 1737 | * Use: Returns the next peer. |
410c8acf | 1738 | */ |
1739 | ||
c8e02c8a | 1740 | extern peer *p_next(peer_iter */*i*/); |
410c8acf | 1741 | |
42da2a58 | 1742 | /*----- Tunnel drivers ----------------------------------------------------*/ |
410c8acf | 1743 | |
42da2a58 | 1744 | #ifdef TUN_LINUX |
1745 | extern const tunnel_ops tun_linux; | |
1746 | #endif | |
410c8acf | 1747 | |
42da2a58 | 1748 | #ifdef TUN_UNET |
1749 | extern const tunnel_ops tun_unet; | |
1750 | #endif | |
410c8acf | 1751 | |
42da2a58 | 1752 | #ifdef TUN_BSD |
1753 | extern const tunnel_ops tun_bsd; | |
1754 | #endif | |
410c8acf | 1755 | |
42da2a58 | 1756 | extern const tunnel_ops tun_slip; |
410c8acf | 1757 | |
410c8acf | 1758 | /*----- Other handy utilities ---------------------------------------------*/ |
1759 | ||
832a2ab6 | 1760 | /* --- @timestr@ --- * |
1761 | * | |
1762 | * Arguments: @time_t t@ = a time to convert | |
1763 | * | |
1764 | * Returns: A pointer to a textual representation of the time. | |
1765 | * | |
1766 | * Use: Converts a time to a textual representation. Corrupts | |
a4b808b0 | 1767 | * @buf_u@. |
832a2ab6 | 1768 | */ |
1769 | ||
1770 | extern const char *timestr(time_t /*t*/); | |
1771 | ||
42da2a58 | 1772 | /* --- @mystrieq@ --- * |
1773 | * | |
1774 | * Arguments: @const char *x, *y@ = two strings | |
1775 | * | |
1776 | * Returns: True if @x@ and @y are equal, up to case. | |
1777 | */ | |
1778 | ||
1779 | extern int mystrieq(const char */*x*/, const char */*y*/); | |
1780 | ||
cb2c2bfc MW |
1781 | /* --- @addrsz@ --- * |
1782 | * | |
1783 | * Arguments: @const addr *a@ = a network address | |
1784 | * | |
1785 | * Returns: The size of the address, for passing into the sockets API. | |
1786 | */ | |
1787 | ||
1788 | extern socklen_t addrsz(const addr */*a*/); | |
1789 | ||
37941236 | 1790 | /* --- @seq_reset@ --- * |
1791 | * | |
1792 | * Arguments: @seqwin *s@ = sequence-checking window | |
1793 | * | |
1794 | * Returns: --- | |
1795 | * | |
1796 | * Use: Resets a sequence number window. | |
1797 | */ | |
1798 | ||
1799 | extern void seq_reset(seqwin */*s*/); | |
1800 | ||
1801 | /* --- @seq_check@ --- * | |
1802 | * | |
1803 | * Arguments: @seqwin *s@ = sequence-checking window | |
1804 | * @uint32 q@ = sequence number to check | |
f43df819 | 1805 | * @const char *service@ = service to report message from |
37941236 | 1806 | * |
1807 | * Returns: A @SEQ_@ code. | |
1808 | * | |
1809 | * Use: Checks a sequence number against the window, updating things | |
1810 | * as necessary. | |
1811 | */ | |
1812 | ||
f43df819 | 1813 | extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/); |
37941236 | 1814 | |
e9fcf28e MW |
1815 | typedef struct ratelim { |
1816 | unsigned n, max, persec; | |
1817 | struct timeval when; | |
1818 | } ratelim; | |
1819 | ||
1820 | /* --- @ratelim_init@ --- * | |
1821 | * | |
1822 | * Arguments: @ratelim *r@ = rate-limiting state to fill in | |
1823 | * @unsigned persec@ = credit to accumulate per second | |
1824 | * @unsigned max@ = maximum credit to retain | |
1825 | * | |
1826 | * Returns: --- | |
1827 | * | |
1828 | * Use: Initialize a rate-limiting state. | |
1829 | */ | |
1830 | ||
1831 | extern void ratelim_init(ratelim */*r*/, | |
1832 | unsigned /*persec*/, unsigned /*max*/); | |
1833 | ||
1834 | /* --- @ratelim_withdraw@ --- * | |
1835 | * | |
1836 | * Arguments: @ratelim *r@ = rate-limiting state | |
1837 | * @unsigned n@ = credit to withdraw | |
1838 | * | |
1839 | * Returns: Zero if successful; @-1@ if there is unsufficient credit | |
1840 | * | |
1841 | * Use: Updates the state with any accumulated credit. Then, if | |
1842 | * there there are more than @n@ credits available, withdraw @n@ | |
1843 | * and return successfully; otherwise, report failure. | |
1844 | */ | |
1845 | ||
1846 | extern int ratelim_withdraw(ratelim */*r*/, unsigned /*n*/); | |
1847 | ||
0c1cede3 MW |
1848 | /* --- @ies_encrypt@ --- * |
1849 | * | |
1850 | * Arguments: @kdata *kpub@ = recipient's public key | |
1851 | * @unsigned ty@ = message type octet | |
1852 | * @buf *b@ = input message buffer | |
1853 | * @buf *bb@ = output buffer for the ciphertext | |
1854 | * | |
1855 | * Returns: On error, returns a @KSERR_...@ code or breaks the buffer; | |
1856 | * on success, returns zero and the buffer is good. | |
1857 | * | |
1858 | * Use: Encrypts a message for a recipient, given their public key. | |
1859 | * This does not (by itself) provide forward secrecy or sender | |
1860 | * authenticity. The ciphertext is self-delimiting (unlike | |
1861 | * @ks_encrypt@). | |
1862 | */ | |
1863 | ||
1864 | extern int ies_encrypt(kdata */*kpub*/, unsigned /*ty*/, | |
1865 | buf */*b*/, buf */*bb*/); | |
1866 | ||
1867 | /* --- @ies_decrypt@ --- * | |
1868 | * | |
1869 | * Arguments: @kdata *kpub@ = private key key | |
1870 | * @unsigned ty@ = message type octet | |
1871 | * @buf *b@ = input ciphertext buffer | |
1872 | * @buf *bb@ = output buffer for the message | |
1873 | * | |
1874 | * Returns: On error, returns a @KSERR_...@ code; on success, returns | |
1875 | * zero and the buffer is good. | |
1876 | * | |
1877 | * Use: Decrypts a message encrypted using @ies_encrypt@, given our | |
1878 | * private key. | |
1879 | */ | |
1880 | ||
1881 | extern int ies_decrypt(kdata */*kpriv*/, unsigned /*ty*/, | |
1882 | buf */*b*/, buf */*bb*/); | |
1883 | ||
410c8acf | 1884 | /*----- That's all, folks -------------------------------------------------*/ |
1885 | ||
1886 | #ifdef __cplusplus | |
1887 | } | |
1888 | #endif | |
1889 | ||
1890 | #endif |