3 * Main header file for TrIPE
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 /*----- Header files ------------------------------------------------------*/
50 #include <sys/types.h>
57 #include <sys/socket.h>
59 #include <netinet/in.h>
60 #include <arpa/inet.h>
66 #include <mLib/alloc.h>
67 #include <mLib/arena.h>
68 #include <mLib/base64.h>
69 #include <mLib/bres.h>
70 #include <mLib/daemonize.h>
71 #include <mLib/dstr.h>
73 #include <mLib/fdflags.h>
74 #include <mLib/fdpass.h>
75 #include <mLib/fwatch.h>
76 #include <mLib/hash.h>
77 #include <mLib/macros.h>
78 #include <mLib/mdup.h>
79 #include <mLib/mdwopt.h>
80 #include <mLib/quis.h>
81 #include <mLib/report.h>
83 #include <mLib/selbuf.h>
87 #include <mLib/trace.h>
89 #include <mLib/versioncmp.h>
91 #include <catacomb/buf.h>
92 #include <catacomb/ct.h>
94 #include <catacomb/gcipher.h>
95 #include <catacomb/gmac.h>
96 #include <catacomb/grand.h>
97 #include <catacomb/key.h>
98 #include <catacomb/paranoia.h>
100 #include <catacomb/noise.h>
101 #include <catacomb/rand.h>
103 #include <catacomb/mp.h>
104 #include <catacomb/mprand.h>
105 #include <catacomb/dh.h>
106 #include <catacomb/ec.h>
107 #include <catacomb/ec-keys.h>
108 #include <catacomb/group.h>
111 #include "protocol.h"
117 /*----- Magic numbers -----------------------------------------------------*/
119 /* --- Trace flags --- */
127 #define T_KEYEXCH 64u
128 #define T_KEYMGMT 128u
130 /* T_PRIVSEP in priv.h */
136 #define SEC(n) (n##u)
137 #define MIN(n) (n##u * 60u)
138 #define F_2P32 (65536.0*65536.0)
139 #define MEG(n) (n##ul * 1024ul * 1024ul)
141 /* --- Timing parameters --- */
143 #define T_EXP MIN(60) /* Expiry time for a key */
144 #define T_REGEN MIN(40) /* Regeneration time for a key */
146 #define T_VALID SEC(20) /* Challenge validity period */
147 #define T_RETRYMIN SEC(2) /* Minimum retry interval */
148 #define T_RETRYMAX MIN(5) /* Maximum retry interval */
149 #define T_RETRYGROW (5.0/4.0) /* Retry interval growth factor */
151 #define T_WOBBLE (1.0/3.0) /* Relative timer randomness */
153 /* --- Other things --- */
155 #define PKBUFSZ 65536
157 /*----- Cipher selections -------------------------------------------------*/
159 typedef struct keyset keyset;
160 typedef struct algswitch algswitch;
162 typedef struct bulkcrypto {
165 int (*check)(const algswitch */*a*/, dstr */*e*/);
166 size_t (*overhead)(const algswitch */*a*/);
167 int (*encrypt)(keyset */*ks*/, unsigned /*ty*/, buf */*b*/, buf */*bb*/);
168 int (*decrypt)(keyset */*ks*/, unsigned /*ty*/,
169 buf */*b*/, buf */*bb*/, uint32 */*seq*/);
176 const gchash *h; /* Hash function */
177 const gccipher *mgf; /* Mask-generation function */
178 const struct bulkcrypto *bulk; /* Bulk crypto transformation */
179 const gccipher *c; /* Symmetric encryption scheme */
180 const gcmac *m; /* Message authentication code */
181 size_t hashsz; /* Hash output size */
182 size_t tagsz; /* Length to truncate MAC tags */
183 size_t expsz; /* Size of data to process */
184 size_t cksz, mksz; /* Key lengths for @c@ and @m@ */
187 typedef struct kdata {
188 unsigned ref; /* Reference counter */
189 struct knode *kn; /* Pointer to cache entry */
190 char *tag; /* Full tag name of the key */
191 group *g; /* The group we work in */
192 size_t indexsz; /* Size of exponent for the group */
193 mp *kpriv; /* The private key (or null) */
194 ge *kpub; /* The public key */
195 time_t t_exp; /* Expiry time of the key */
196 algswitch algs; /* Collection of algorithms */
199 typedef struct knode {
200 sym_base _b; /* Symbol table intrusion */
201 unsigned f; /* Various flags */
202 #define KNF_BROKEN 1u /* Don't use this key any more */
203 struct keyhalf *kh; /* Pointer to the home keyhalf */
204 kdata *kd; /* Pointer to the key data */
207 #define MAXHASHSZ 64 /* Largest possible hash size */
209 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
211 extern const struct bulkcrypto bulktab[];
213 /*----- Data structures ---------------------------------------------------*/
215 /* --- Socket addresses --- *
217 * A magic union of supported socket addresses.
222 struct sockaddr_in sin;
225 /* --- Mapping keyed on addresses --- */
227 typedef struct addrmap {
232 typedef struct addrmap_base {
237 /* --- Sequence number checking --- */
239 typedef struct seqwin {
240 uint32 seq; /* First acceptable input sequence */
241 uint32 win; /* Window of acceptable numbers */
244 #define SEQ_WINSZ 32 /* Bits in sequence number window */
246 /* --- A symmetric keyset --- *
248 * A keyset contains a set of symmetric keys for encrypting and decrypting
249 * packets. Keysets are stored in a list, sorted in reverse order of
250 * creation, so that the most recent keyset (the one most likely to be used)
253 * Each keyset has a time limit and a data limit. The keyset is destroyed
254 * when either it has existed for too long, or it has been used to encrypt
255 * too much data. New key exchanges are triggered when keys are close to
260 struct keyset *next; /* Next active keyset in the list */
261 unsigned ref; /* Reference count for keyset */
262 struct peer *p; /* Pointer to peer structure */
263 time_t t_exp; /* Expiry time for this keyset */
264 unsigned long sz_exp, sz_regen; /* Data limits for the keyset */
265 T( unsigned seq; ) /* Sequence number for tracing */
266 unsigned f; /* Various useful flags */
267 const bulkcrypto *bulk; /* Bulk crypto transform */
268 size_t tagsz; /* Length to truncate MAC tags */
270 gcipher *c; /* Keyset cipher for encryption */
271 gmac *m; /* Keyset MAC for integrity */
273 uint32 oseq; /* Outbound sequence number */
274 seqwin iseq; /* Inbound sequence number */
277 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
278 #define KSF_LINK 2u /* Key is in a linked list */
280 #define KSERR_REGEN -1 /* Regenerate keys */
281 #define KSERR_NOKEYS -2 /* No keys left */
282 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
283 #define KSERR_SEQ -4 /* Incorrect sequence number */
284 #define KSERR_MALFORMED -5 /* Input ciphertext is broken */
286 /* --- Key exchange --- *
288 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
289 * Protocol has a number of desirable features (e.g., perfect forward
290 * secrecy, and zero-knowledge authentication) which make it attractive for
291 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
295 typedef struct retry {
296 double t; /* Current retry time */
301 typedef struct kxchal {
302 struct keyexch *kx; /* Pointer back to key exchange */
303 ge *c; /* Responder's challenge */
304 ge *r; /* My reply to the challenge */
305 keyset *ks; /* Pointer to temporary keyset */
306 unsigned f; /* Various useful flags */
307 sel_timer t; /* Response timer for challenge */
308 retry rs; /* Retry state */
309 octet hc[MAXHASHSZ]; /* Hash of his challenge */
310 octet ck[MAXHASHSZ]; /* His magical check value */
311 octet hswrq_in[MAXHASHSZ]; /* Inbound switch request message */
312 octet hswok_in[MAXHASHSZ]; /* Inbound switch confirmation */
313 octet hswrq_out[MAXHASHSZ]; /* Outbound switch request message */
314 octet hswok_out[MAXHASHSZ]; /* Outbound switch confirmation */
317 typedef struct keyexch {
318 struct peer *p; /* Pointer back to the peer */
319 kdata *kpriv; /* Private key and related info */
320 kdata *kpub; /* Peer's public key */
321 keyset **ks; /* Peer's list of keysets */
322 unsigned f; /* Various useful flags */
323 unsigned s; /* Current state in exchange */
324 sel_timer t; /* Timer for next exchange */
325 retry rs; /* Retry state */
326 mp *alpha; /* My temporary secret */
327 ge *c; /* My challenge */
328 ge *rx; /* The expected response */
329 unsigned nr; /* Number of extant responses */
330 time_t t_valid; /* When this exchange goes bad */
331 octet hc[MAXHASHSZ]; /* Hash of my challenge */
332 kxchal *r[KX_NCHAL]; /* Array of challenges */
335 #define KXF_TIMER 1u /* Waiting for a timer to go off */
336 #define KXF_DEAD 2u /* The key-exchanger isn't up */
337 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
338 #define KXF_CORK 8u /* Don't send anything yet */
341 KXS_DEAD, /* Uninitialized state (magical) */
342 KXS_CHAL, /* Main answer-challenges state */
343 KXS_COMMIT, /* Committed: send switch request */
344 KXS_SWITCH /* Switched: send confirmation */
347 /* --- Tunnel structure --- *
349 * Used to maintain system-specific information about the tunnel interface.
352 typedef struct tunnel tunnel;
355 typedef struct tunnel_ops {
356 const char *name; /* Name of this tunnel driver */
357 unsigned flags; /* Various interesting flags */
358 #define TUNF_PRIVOPEN 1u /* Need helper to open file */
359 void (*init)(void); /* Initializes the system */
360 tunnel *(*create)(struct peer */*p*/, int /*fd*/, char **/*ifn*/);
361 /* Initializes a new tunnel */
362 void (*setifname)(tunnel */*t*/, const char */*ifn*/);
363 /* Notifies ifname change */
364 void (*inject)(tunnel */*t*/, buf */*b*/); /* Sends packet through if */
365 void (*destroy)(tunnel */*t*/); /* Destroys a tunnel */
368 #ifndef TUN_INTERNALS
369 struct tunnel { const tunnel_ops *ops; };
372 /* --- Peer statistics --- *
374 * Contains various interesting and not-so-interesting statistics about a
375 * peer. This is updated by various parts of the code. The format of the
376 * structure isn't considered private, and @p_stats@ returns a pointer to the
377 * statistics block for a given peer.
380 typedef struct stats {
381 unsigned long sz_in, sz_out; /* Size of all data in and out */
382 unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */
383 unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */
384 time_t t_start, t_last, t_kx; /* Time peer created, last pk, kx */
385 unsigned long n_reject; /* Number of rejected packets */
386 unsigned long n_in, n_out; /* Number of packets in and out */
387 unsigned long n_kxin, n_kxout; /* Number of key exchange packets */
388 unsigned long n_ipin, n_ipout; /* Number of encrypted packets */
391 /* --- Peer structure --- *
393 * The main structure which glues everything else together.
396 typedef struct peerspec {
397 char *name; /* Peer's name */
398 char *privtag; /* Private key tag */
399 char *tag; /* Public key tag */
400 const tunnel_ops *tops; /* Tunnel operations */
401 unsigned long t_ka; /* Keep alive interval */
402 addr sa; /* Socket address to speak to */
403 size_t sasz; /* Socket address size */
404 unsigned f; /* Flags for the peer */
405 #define PSF_KXMASK 255u /* Key-exchange flags to set */
406 #define PSF_MOBILE 256u /* Address may change rapidly */
409 typedef struct peer_byname {
414 typedef struct peer_byaddr {
419 typedef struct peer {
420 peer_byname *byname; /* Lookup-by-name block */
421 peer_byaddr *byaddr; /* Lookup-by-address block */
422 struct ping *pings; /* Pings we're waiting for */
423 peerspec spec; /* Specifications for this peer */
424 tunnel *t; /* Tunnel for local packets */
425 char *ifname; /* Interface name for tunnel */
426 keyset *ks; /* List head for keysets */
427 buf b; /* Buffer for sending packets */
428 stats st; /* Statistics */
429 keyexch kx; /* Key exchange protocol block */
430 sel_timer tka; /* Timer for keepalives */
433 typedef struct peer_iter { sym_iter i; } peer_iter;
435 typedef struct ping {
436 struct ping *next, *prev; /* Links to next and previous */
437 peer *p; /* Peer so we can free it */
438 unsigned msg; /* Kind of response expected */
439 uint32 id; /* Id so we can recognize response */
440 octet magic[32]; /* Some random data */
441 sel_timer t; /* Timeout for ping */
442 void (*func)(int /*rc*/, void */*arg*/); /* Function to call when done */
443 void *arg; /* Argument for callback */
454 /* --- Admin structure --- */
456 #define OBUFSZ 16384u
458 typedef struct obuf {
459 struct obuf *next; /* Next buffer in list */
460 char *p_in, *p_out; /* Pointers into the buffer */
461 char buf[OBUFSZ]; /* The actual buffer */
464 typedef struct oqueue {
465 obuf *hd, *tl; /* Head and tail pointers */
470 typedef struct admin_bgop {
471 struct admin_bgop *next, *prev; /* Links to next and previous */
472 struct admin *a; /* Owner job */
473 char *tag; /* Tag string for messages */
474 void (*cancel)(struct admin_bgop *); /* Destructor function */
477 typedef struct admin_resop {
478 admin_bgop bg; /* Background operation header */
479 char *addr; /* Hostname to be resolved */
480 bres_client r; /* Background resolver task */
481 sel_timer t; /* Timer for resolver */
482 addr sa; /* Socket address */
483 size_t sasz; /* Socket address size */
484 void (*func)(struct admin_resop *, int); /* Handler */
487 enum { ARES_OK, ARES_FAIL };
489 typedef struct admin_addop {
490 admin_resop r; /* Name resolution header */
491 peerspec peer; /* Peer pending creation */
494 typedef struct admin_pingop {
495 admin_bgop bg; /* Background operation header */
496 ping ping; /* Ping pending response */
497 struct timeval pingtime; /* Time last ping was sent */
500 typedef struct admin_service {
501 sym_base _b; /* Hash table base structure */
502 char *version; /* The provided version */
503 struct admin *prov; /* Which client provides me */
504 struct admin_service *next, *prev; /* Client's list of services */
507 typedef struct admin_svcop {
508 admin_bgop bg; /* Background operation header */
509 struct admin *prov; /* Client servicing this job */
510 unsigned index; /* This job's index */
511 struct admin_svcop *next, *prev; /* Links for provider's jobs */
514 typedef struct admin_jobentry {
515 unsigned short seq; /* Zero if unused */
517 admin_svcop *op; /* Operation, if slot in use, ... */
518 uint32 next; /* ... or index of next free slot */
522 typedef struct admin_jobtable {
523 uint32 n, sz; /* Used slots and table size */
524 admin_svcop *active; /* List of active jobs */
525 uint32 free; /* Index of first free slot */
526 admin_jobentry *v; /* And the big array of entries */
529 typedef struct admin {
530 struct admin *next, *prev; /* Links to next and previous */
531 unsigned f; /* Various useful flags */
532 unsigned ref; /* Reference counter */
534 unsigned seq; /* Sequence number for tracing */
536 oqueue out; /* Output buffer list */
537 oqueue delay; /* Delayed output buffer list */
538 admin_bgop *bg; /* Backgrounded operations */
539 admin_service *svcs; /* Which services I provide */
540 admin_jobtable j; /* Table of outstanding jobs */
541 selbuf b; /* Line buffer for commands */
542 sel_file w; /* Selector for write buffering */
545 #define AF_DEAD 1u /* Destroy this admin block */
546 #define AF_CLOSE 2u /* Client closed connection */
547 #define AF_NOTE 4u /* Catch notifications */
548 #define AF_WARN 8u /* Catch warning messages */
550 #define AF_TRACE 16u /* Catch tracing */
552 #define AF_FOREGROUND 32u /* Quit server when client closes */
555 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
557 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
560 /*----- Global variables --------------------------------------------------*/
562 extern sel_state sel; /* Global I/O event state */
563 extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ];
564 extern const tunnel_ops *tunnels[]; /* Table of tunnels (0-term) */
565 extern const tunnel_ops *tun_default; /* Default tunnel to use */
566 extern kdata *master; /* Default private key */
567 extern const char *tag_priv; /* Default private key tag */
570 extern const trace_opt tr_opts[]; /* Trace options array */
571 extern unsigned tr_flags; /* Trace options flags */
574 /*----- Other macros ------------------------------------------------------*/
576 #define TIMER noise_timer(RAND_GLOBAL)
578 /*----- Key management ----------------------------------------------------*/
580 /* --- @km_init@ --- *
582 * Arguments: @const char *privkr@ = private keyring file
583 * @const char *pubkr@ = public keyring file
584 * @const char *ptag@ = default private-key tag
588 * Use: Initializes the key-management machinery, loading the
589 * keyrings and so on.
592 extern void km_init(const char */*privkr*/, const char */*pubkr*/,
593 const char */*ptag*/);
595 /* --- @km_reload@ --- *
599 * Returns: Zero if OK, nonzero to force reloading of keys.
601 * Use: Checks the keyrings to see if they need reloading.
604 extern int km_reload(void);
606 /* --- @km_findpub@, @km_findpriv@ --- *
608 * Arguments: @const char *tag@ = key tag to load
610 * Returns: Pointer to the kdata object if successful, or null on error.
612 * Use: Fetches a public or private key from the keyring.
615 extern kdata *km_findpub(const char */*tag*/);
616 extern kdata *km_findpriv(const char */*tag*/);
618 /* --- @km_samealgsp@ --- *
620 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
622 * Returns: Nonzero if their two algorithm selections are the same.
624 * Use: Checks sameness of algorithm selections: used to ensure that
625 * peers are using sensible algorithms.
628 extern int km_samealgsp(const kdata */*kdx*/, const kdata */*kdy*/);
630 /* --- @km_ref@ --- *
632 * Arguments: @kdata *kd@ = pointer to the kdata object
636 * Use: Claim a new reference to a kdata object.
639 extern void km_ref(kdata */*kd*/);
641 /* --- @km_unref@ --- *
643 * Arguments: @kdata *kd@ = pointer to the kdata object
647 * Use: Releases a reference to a kdata object.
650 extern void km_unref(kdata */*kd*/);
652 /* --- @km_tag@ --- *
654 * Arguments: @kdata *kd@ - pointer to the kdata object
656 * Returns: A pointer to the short tag by which the kdata was loaded.
659 extern const char *km_tag(kdata */*kd*/);
661 /*----- Key exchange ------------------------------------------------------*/
663 /* --- @kx_start@ --- *
665 * Arguments: @keyexch *kx@ = pointer to key exchange context
666 * @int forcep@ = nonzero to ignore the quiet timer
670 * Use: Stimulates a key exchange. If a key exchage is in progress,
671 * a new challenge is sent (unless the quiet timer forbids
672 * this); if no exchange is in progress, one is commenced.
675 extern void kx_start(keyexch */*kx*/, int /*forcep*/);
677 /* --- @kx_message@ --- *
679 * Arguments: @keyexch *kx@ = pointer to key exchange context
680 * @unsigned msg@ = the message code
681 * @buf *b@ = pointer to buffer containing the packet
685 * Use: Reads a packet containing key exchange messages and handles
689 extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/);
691 /* --- @kx_free@ --- *
693 * Arguments: @keyexch *kx@ = pointer to key exchange context
697 * Use: Frees everything in a key exchange context.
700 extern void kx_free(keyexch */*kx*/);
702 /* --- @kx_newkeys@ --- *
704 * Arguments: @keyexch *kx@ = pointer to key exchange context
708 * Use: Informs the key exchange module that its keys may have
709 * changed. If fetching the new keys fails, the peer will be
710 * destroyed, we log messages and struggle along with the old
714 extern void kx_newkeys(keyexch */*kx*/);
716 /* --- @kx_init@ --- *
718 * Arguments: @keyexch *kx@ = pointer to key exchange context
719 * @peer *p@ = pointer to peer context
720 * @keyset **ks@ = pointer to keyset list
721 * @unsigned f@ = various useful flags
723 * Returns: Zero if OK, nonzero if it failed.
725 * Use: Initializes a key exchange module. The module currently
726 * contains no keys, and will attempt to initiate a key
730 extern int kx_init(keyexch */*kx*/, peer */*p*/,
731 keyset **/*ks*/, unsigned /*f*/);
733 /*----- Keysets and symmetric cryptography --------------------------------*/
735 /* --- @ks_drop@ --- *
737 * Arguments: @keyset *ks@ = pointer to a keyset
741 * Use: Decrements a keyset's reference counter. If the counter hits
742 * zero, the keyset is freed.
745 extern void ks_drop(keyset */*ks*/);
747 /* --- @ks_gen@ --- *
749 * Arguments: @const void *k@ = pointer to key material
750 * @size_t x, y, z@ = offsets into key material (see below)
751 * @peer *p@ = pointer to peer information
753 * Returns: A pointer to the new keyset.
755 * Use: Derives a new keyset from the given key material. The
756 * offsets @x@, @y@ and @z@ separate the key material into three
757 * parts. Between the @k@ and @k + x@ is `my' contribution to
758 * the key material; between @k + x@ and @k + y@ is `your'
759 * contribution; and between @k + y@ and @k + z@ is a shared
760 * value we made together. These are used to construct two
761 * pairs of symmetric keys. Each pair consists of an encryption
762 * key and a message authentication key. One pair is used for
763 * outgoing messages, the other for incoming messages.
765 * The new key is marked so that it won't be selected for output
766 * by @ksl_encrypt@. You can still encrypt data with it by
767 * calling @ks_encrypt@ directly.
770 extern keyset *ks_gen(const void */*k*/,
771 size_t /*x*/, size_t /*y*/, size_t /*z*/,
774 /* --- @ks_activate@ --- *
776 * Arguments: @keyset *ks@ = pointer to a keyset
780 * Use: Activates a keyset, so that it can be used for encrypting
784 extern void ks_activate(keyset */*ks*/);
786 /* --- @ks_encrypt@ --- *
788 * Arguments: @keyset *ks@ = pointer to a keyset
789 * @unsigned ty@ = message type
790 * @buf *b@ = pointer to input buffer
791 * @buf *bb@ = pointer to output buffer
793 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
794 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
795 * returns zero if there was insufficient buffer (but the output
796 * buffer is broken in this case).
798 * Use: Encrypts a block of data using the key. Note that the `key
799 * ought to be replaced' notification is only ever given once
800 * for each key. Also note that this call forces a keyset to be
801 * used even if it's marked as not for data output.
803 * The encryption transform is permitted to corrupt @buf_u@ for
804 * its own purposes. Neither the source nor destination should
805 * be within @buf_u@; and callers mustn't expect anything stored
806 * in @buf_u@ to still
809 extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/,
810 buf */*b*/, buf */*bb*/);
812 /* --- @ks_decrypt@ --- *
814 * Arguments: @keyset *ks@ = pointer to a keyset
815 * @unsigned ty@ = expected type code
816 * @buf *b@ = pointer to an input buffer
817 * @buf *bb@ = pointer to an output buffer
819 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
820 * zero if there was insufficient buffer (but the output buffer
821 * is broken in this case).
823 * Use: Attempts to decrypt a message using a given key. Note that
824 * requesting decryption with a key directly won't clear a
825 * marking that it's not for encryption.
827 * The decryption transform is permitted to corrupt @buf_u@ for
828 * its own purposes. Neither the source nor destination should
829 * be within @buf_u@; and callers mustn't expect anything stored
830 * in @buf_u@ to still
833 extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/,
834 buf */*b*/, buf */*bb*/);
836 /* --- @ksl_free@ --- *
838 * Arguments: @keyset **ksroot@ = pointer to keyset list head
842 * Use: Frees (releases references to) all of the keys in a keyset.
845 extern void ksl_free(keyset **/*ksroot*/);
847 /* --- @ksl_link@ --- *
849 * Arguments: @keyset **ksroot@ = pointer to keyset list head
850 * @keyset *ks@ = pointer to a keyset
854 * Use: Links a keyset into a list. A keyset can only be on one list
855 * at a time. Bad things happen otherwise.
858 extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/);
860 /* --- @ksl_prune@ --- *
862 * Arguments: @keyset **ksroot@ = pointer to keyset list head
866 * Use: Prunes the keyset list by removing keys which mustn't be used
870 extern void ksl_prune(keyset **/*ksroot*/);
872 /* --- @ksl_encrypt@ --- *
874 * Arguments: @keyset **ksroot@ = pointer to keyset list head
875 * @unsigned ty@ = message type
876 * @buf *b@ = pointer to input buffer
877 * @buf *bb@ = pointer to output buffer
879 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
880 * new key; @KSERR_NOKEYS@ if there are no suitable keys
881 * available. Also returns zero if there was insufficient
882 * buffer space (but the output buffer is broken in this case).
884 * Use: Encrypts a packet.
887 extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/,
888 buf */*b*/, buf */*bb*/);
890 /* --- @ksl_decrypt@ --- *
892 * Arguments: @keyset **ksroot@ = pointer to keyset list head
893 * @unsigned ty@ = expected type code
894 * @buf *b@ = pointer to input buffer
895 * @buf *bb@ = pointer to output buffer
897 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
898 * zero if there was insufficient buffer (but the output buffer
899 * is broken in this case).
901 * Use: Decrypts a packet.
904 extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/,
905 buf */*b*/, buf */*bb*/);
907 /*----- Challenges --------------------------------------------------------*/
911 * Arguments: @buf *b@ = where to put the challenge
913 * Returns: Zero if OK, nonzero on error.
915 * Use: Issues a new challenge.
918 extern int c_new(buf */*b*/);
920 /* --- @c_check@ --- *
922 * Arguments: @buf *b@ = where to find the challenge
924 * Returns: Zero if OK, nonzero if it didn't work.
926 * Use: Checks a challenge. On failure, the buffer is broken.
929 extern int c_check(buf */*b*/);
931 /*----- Administration interface ------------------------------------------*/
933 #define A_END ((char *)0)
935 /* --- @a_vformat@ --- *
937 * Arguments: @dstr *d@ = where to leave the formatted message
938 * @const char *fmt@ = pointer to format string
939 * @va_list ap@ = arguments in list
943 * Use: Main message token formatting driver. The arguments are
944 * interleaved formatting tokens and their parameters, finally
945 * terminated by an entry @A_END@.
949 * * "*..." ... -- pretokenized @dstr_putf@-like string
951 * * "?ADDR" SOCKADDR -- a socket address, to be converted
953 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
955 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
957 * * "?PEER" PEER -- peer's name
959 * * "?ERRNO" ERRNO -- system error code
961 * * "[!]..." ... -- @dstr_putf@-like string as single token
964 extern void a_vformat(dstr */*d*/, const char */*fmt*/, va_list /*ap*/);
966 /* --- @a_format@ --- *
968 * Arguments: @dstr *d@ = where to leave the formatted message
969 * @const char *fmt@ = pointer to format string
973 * Use: Writes a tokenized message into a string, for later
977 extern void EXECL_LIKE(0) a_format(dstr */*d*/, const char */*fmt*/, ...);
979 /* --- @a_warn@ --- *
981 * Arguments: @const char *fmt@ = pointer to format string
982 * @...@ = other arguments
986 * Use: Informs all admin connections of a warning.
989 extern void EXECL_LIKE(0) a_warn(const char */*fmt*/, ...);
991 /* --- @a_notify@ --- *
993 * Arguments: @const char *fmt@ = pointer to format string
994 * @...@ = other arguments
998 * Use: Sends a notification to interested admin connections.
1001 extern void EXECL_LIKE(0) a_notify(const char */*fmt*/, ...);
1003 /* --- @a_create@ --- *
1005 * Arguments: @int fd_in, fd_out@ = file descriptors to use
1006 * @unsigned f@ = initial flags to set
1010 * Use: Creates a new admin connection.
1013 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
1015 /* --- @a_quit@ --- *
1021 * Use: Shuts things down nicely.
1024 extern void a_quit(void);
1026 /* --- @a_preselect@ --- *
1032 * Use: Informs the admin module that we're about to select again,
1033 * and that it should do cleanup things it has delayed until a
1037 extern void a_preselect(void);
1039 /* --- @a_daemon@ --- *
1045 * Use: Informs the admin module that it's a daemon.
1048 extern void a_daemon(void);
1050 /* --- @a_init@ --- *
1052 * Arguments: @const char *sock@ = socket name to create
1053 * @uid_t u@ = user to own the socket
1054 * @gid_t g@ = group to own the socket
1055 * @mode_t m@ = permissions to set on the socket
1059 * Use: Creates the admin listening socket.
1062 extern void a_init(const char */*sock*/,
1063 uid_t /*u*/, gid_t /*g*/, mode_t /*m*/);
1065 /*----- Mapping with addresses as keys ------------------------------------*/
1067 /* --- @am_create@ --- *
1069 * Arguments: @addrmap *m@ = pointer to map
1073 * Use: Create an address map, properly set up.
1076 extern void am_create(addrmap */*m*/);
1078 /* --- @am_destroy@ --- *
1080 * Arguments: @addrmap *m@ = pointer to map
1084 * Use: Destroy an address map, throwing away all the entries.
1087 extern void am_destroy(addrmap */*m*/);
1089 /* --- @am_find@ --- *
1091 * Arguments: @addrmap *m@ = pointer to map
1092 * @const addr *a@ = address to look up
1093 * @size_t sz@ = size of block to allocate
1094 * @unsigned *f@ = where to store flags
1096 * Returns: Pointer to found item, or null.
1098 * Use: Finds a record with the given IP address, set @*f@ nonzero
1099 * and returns it. If @sz@ is zero, and no match was found,
1100 * return null; otherwise allocate a new block of @sz@ bytes,
1101 * clear @*f@ to zero and return the block pointer.
1104 extern void *am_find(addrmap */*m*/, const addr */*a*/,
1105 size_t /*sz*/, unsigned */*f*/);
1107 /* --- @am_remove@ --- *
1109 * Arguments: @addrmap *m@ = pointer to map
1110 * @void *i@ = pointer to the item
1114 * Use: Removes an item from the map.
1117 extern void am_remove(addrmap */*m*/, void */*i*/);
1119 /*----- Privilege separation ----------------------------------------------*/
1121 /* --- @ps_trace@ --- *
1123 * Arguments: @unsigned mask@ = trace mask to check
1124 * @const char *fmt@ = message format
1125 * @...@ = values for placeholders
1129 * Use: Writes a trace message.
1132 T( extern void PRINTF_LIKE(2, 3)
1133 ps_trace(unsigned /*mask*/, const char */*fmt*/, ...); )
1135 /* --- @ps_warn@ --- *
1137 * Arguments: @const char *fmt@ = message format
1138 * @...@ = values for placeholders
1142 * Use: Writes a warning message.
1145 extern void PRINTF_LIKE(1, 2) ps_warn(const char */*fmt*/, ...);
1147 /* --- @ps_tunfd@ --- *
1149 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1150 * @char **ifn@ = where to put the interface name
1152 * Returns: The file descriptor, or @-1@ on error.
1154 * Use: Fetches a file descriptor for a tunnel driver.
1157 extern int ps_tunfd(const tunnel_ops */*tops*/, char **/*ifn*/);
1159 /* --- @ps_split@ --- *
1161 * Arguments: @int detachp@ = whether to detach the child from its terminal
1165 * Use: Separates off the privileged tunnel-opening service from the
1166 * rest of the server.
1169 extern void ps_split(int /*detachp*/);
1171 /* --- @ps_quit@ --- *
1177 * Use: Detaches from the helper process.
1180 extern void ps_quit(void);
1182 /*----- Peer management ---------------------------------------------------*/
1184 /* --- @p_txstart@ --- *
1186 * Arguments: @peer *p@ = pointer to peer block
1187 * @unsigned msg@ = message type code
1189 * Returns: A pointer to a buffer to write to.
1191 * Use: Starts sending to a peer. Only one send can happen at a
1195 extern buf *p_txstart(peer */*p*/, unsigned /*msg*/);
1197 /* --- @p_txend@ --- *
1199 * Arguments: @peer *p@ = pointer to peer block
1203 * Use: Sends a packet to the peer.
1206 extern void p_txend(peer */*p*/);
1208 /* --- @p_pingsend@ --- *
1210 * Arguments: @peer *p@ = destination peer
1211 * @ping *pg@ = structure to fill in
1212 * @unsigned type@ = message type
1213 * @unsigned long timeout@ = how long to wait before giving up
1214 * @void (*func)(int, void *)@ = callback function
1215 * @void *arg@ = argument for callback
1217 * Returns: Zero if successful, nonzero if it failed.
1219 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1220 * if we get an answer within the timeout, or zero if no answer.
1223 extern int p_pingsend(peer */*p*/, ping */*pg*/, unsigned /*type*/,
1224 unsigned long /*timeout*/,
1225 void (*/*func*/)(int, void *), void */*arg*/);
1227 /* --- @p_pingdone@ --- *
1229 * Arguments: @ping *p@ = ping structure
1230 * @int rc@ = return code to pass on
1234 * Use: Disposes of a ping structure, maybe sending a notification.
1237 extern void p_pingdone(ping */*p*/, int /*rc*/);
1239 /* --- @p_greet@ --- *
1241 * Arguments: @peer *p@ = peer to send to
1242 * @const void *c@ = pointer to challenge
1243 * @size_t sz@ = size of challenge
1247 * Use: Sends a greeting packet.
1250 extern void p_greet(peer */*p*/, const void */*c*/, size_t /*sz*/);
1252 /* --- @p_tun@ --- *
1254 * Arguments: @peer *p@ = pointer to peer block
1255 * @buf *b@ = buffer containing incoming packet
1259 * Use: Handles a packet which needs to be sent to a peer.
1262 extern void p_tun(peer */*p*/, buf */*b*/);
1264 /* --- @p_keyreload@ --- *
1270 * Use: Forces a check of the daemon's keyring files.
1273 extern void p_keyreload(void);
1275 /* --- @p_interval@ --- *
1281 * Use: Called periodically to do tidying.
1284 extern void p_interval(void);
1286 /* --- @p_stats@ --- *
1288 * Arguments: @peer *p@ = pointer to a peer block
1290 * Returns: A pointer to the peer's statistics.
1293 extern stats *p_stats(peer */*p*/);
1295 /* --- @p_ifname@ --- *
1297 * Arguments: @peer *p@ = pointer to a peer block
1299 * Returns: A pointer to the peer's interface name.
1302 extern const char *p_ifname(peer */*p*/);
1304 /* --- @p_setifname@ --- *
1306 * Arguments: @peer *p@ = pointer to a peer block
1307 * @const char *name@ = pointer to the new name
1311 * Use: Changes the name held for a peer's interface.
1314 extern void p_setifname(peer */*p*/, const char */*name*/);
1316 /* --- @p_addr@ --- *
1318 * Arguments: @peer *p@ = pointer to a peer block
1320 * Returns: A pointer to the peer's address.
1323 extern const addr *p_addr(peer */*p*/);
1325 /* --- @p_init@ --- *
1327 * Arguments: @struct in_addr addr@ = address to bind to
1328 * @unsigned port@ = port number to listen to
1332 * Use: Initializes the peer system; creates the socket.
1335 extern void p_init(struct in_addr /*addr*/, unsigned /*port*/);
1337 /* --- @p_port@ --- *
1341 * Returns: Port number used for socket.
1344 unsigned p_port(void);
1346 /* --- @p_create@ --- *
1348 * Arguments: @peerspec *spec@ = information about this peer
1350 * Returns: Pointer to the peer block, or null if it failed.
1352 * Use: Creates a new named peer block. No peer is actually attached
1356 extern peer *p_create(peerspec */*spec*/);
1358 /* --- @p_name@ --- *
1360 * Arguments: @peer *p@ = pointer to a peer block
1362 * Returns: A pointer to the peer's name.
1364 * Use: Equivalent to @p_spec(p)->name@.
1367 extern const char *p_name(peer */*p*/);
1369 /* --- @p_tag@ --- *
1371 * Arguments: @peer *p@ = pointer to a peer block
1373 * Returns: A pointer to the peer's public key tag.
1376 extern const char *p_tag(peer */*p*/);
1378 /* --- @p_privtag@ --- *
1380 * Arguments: @peer *p@ = pointer to a peer block
1382 * Returns: A pointer to the peer's private key tag.
1385 extern const char *p_privtag(peer */*p*/);
1387 /* --- @p_spec@ --- *
1389 * Arguments: @peer *p@ = pointer to a peer block
1391 * Returns: Pointer to the peer's specification
1394 extern const peerspec *p_spec(peer */*p*/);
1396 /* --- @p_findbyaddr@ --- *
1398 * Arguments: @const addr *a@ = address to look up
1400 * Returns: Pointer to the peer block, or null if not found.
1402 * Use: Finds a peer by address.
1405 extern peer *p_findbyaddr(const addr */*a*/);
1407 /* --- @p_find@ --- *
1409 * Arguments: @const char *name@ = name to look up
1411 * Returns: Pointer to the peer block, or null if not found.
1413 * Use: Finds a peer by name.
1416 extern peer *p_find(const char */*name*/);
1418 /* --- @p_destroy@ --- *
1420 * Arguments: @peer *p@ = pointer to a peer
1424 * Use: Destroys a peer.
1427 extern void p_destroy(peer */*p*/);
1429 /* --- @FOREACH_PEER@ --- *
1431 * Arguments: @p@ = name to bind to each peer
1432 * @stuff@ = thing to do for each item
1434 * Use: Does something for each current peer.
1437 #define FOREACH_PEER(p, stuff) do { \
1440 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
1443 /* --- @p_mkiter@ --- *
1445 * Arguments: @peer_iter *i@ = pointer to an iterator
1449 * Use: Initializes the iterator.
1452 extern void p_mkiter(peer_iter */*i*/);
1454 /* --- @p_next@ --- *
1456 * Arguments: @peer_iter *i@ = pointer to an iterator
1458 * Returns: Next peer, or null if at the end.
1460 * Use: Returns the next peer.
1463 extern peer *p_next(peer_iter */*i*/);
1465 /*----- Tunnel drivers ----------------------------------------------------*/
1468 extern const tunnel_ops tun_linux;
1472 extern const tunnel_ops tun_unet;
1476 extern const tunnel_ops tun_bsd;
1479 extern const tunnel_ops tun_slip;
1481 /*----- Other handy utilities ---------------------------------------------*/
1483 /* --- @mpstr@ --- *
1485 * Arguments: @mp *m@ = a multiprecision integer
1487 * Returns: A pointer to the integer's textual representation.
1489 * Use: Converts a multiprecision integer to a string. Corrupts
1493 extern const char *mpstr(mp */*m*/);
1495 /* --- @gestr@ --- *
1497 * Arguments: @group *g@ = a group
1498 * @ge *x@ = a group element
1500 * Returns: A pointer to the element's textual representation.
1502 * Use: Converts a group element to a string. Corrupts
1506 extern const char *gestr(group */*g*/, ge */*x*/);
1508 /* --- @timestr@ --- *
1510 * Arguments: @time_t t@ = a time to convert
1512 * Returns: A pointer to a textual representation of the time.
1514 * Use: Converts a time to a textual representation. Corrupts
1518 extern const char *timestr(time_t /*t*/);
1520 /* --- @mystrieq@ --- *
1522 * Arguments: @const char *x, *y@ = two strings
1524 * Returns: True if @x@ and @y are equal, up to case.
1527 extern int mystrieq(const char */*x*/, const char */*y*/);
1529 /* --- @seq_reset@ --- *
1531 * Arguments: @seqwin *s@ = sequence-checking window
1535 * Use: Resets a sequence number window.
1538 extern void seq_reset(seqwin */*s*/);
1540 /* --- @seq_check@ --- *
1542 * Arguments: @seqwin *s@ = sequence-checking window
1543 * @uint32 q@ = sequence number to check
1544 * @const char *service@ = service to report message from
1546 * Returns: A @SEQ_@ code.
1548 * Use: Checks a sequence number against the window, updating things
1552 extern int seq_check(seqwin */*s*/, uint32 /*q*/, const char */*service*/);
1554 /*----- That's all, folks -------------------------------------------------*/