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