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 algswitch
{
160 const gccipher
*c
; /* Symmetric encryption scheme */
161 const gccipher
*mgf
; /* Mask-generation function */
162 const gchash
*h
; /* Hash function */
163 const gcmac
*m
; /* Message authentication code */
164 size_t hashsz
; /* Hash output size */
165 size_t tagsz
; /* Length to truncate MAC tags */
166 size_t expsz
; /* Size of data to process */
167 size_t cksz
, mksz
; /* Key lengths for @c@ and @m@ */
170 typedef struct kdata
{
171 unsigned ref
; /* Reference counter */
172 struct knode
*kn
; /* Pointer to cache entry */
173 char *tag
; /* Full tag name of the key */
174 group
*g
; /* The group we work in */
175 size_t indexsz
; /* Size of exponent for the group */
176 mp
*kpriv
; /* The private key (or null) */
177 ge
*kpub
; /* The public key */
178 time_t t_exp
; /* Expiry time of the key */
179 algswitch algs
; /* Collection of algorithms */
182 typedef struct knode
{
183 sym_base _b
; /* Symbol table intrusion */
184 unsigned f
; /* Various flags */
185 #define KNF_BROKEN 1u /* Don't use this key any more */
186 struct keyhalf
*kh
; /* Pointer to the home keyhalf */
187 kdata
*kd
; /* Pointer to the key data */
190 #define MAXHASHSZ 64 /* Largest possible hash size */
192 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
194 /*----- Data structures ---------------------------------------------------*/
196 /* --- Socket addresses --- *
198 * A magic union of supported socket addresses.
203 struct sockaddr_in sin
;
206 /* --- Mapping keyed on addresses --- */
208 typedef struct addrmap
{
213 typedef struct addrmap_base
{
218 /* --- Sequence number checking --- */
220 typedef struct seqwin
{
221 uint32 seq
; /* First acceptable input sequence */
222 uint32 win
; /* Window of acceptable numbers */
225 #define SEQ_WINSZ 32 /* Bits in sequence number window */
227 /* --- A symmetric keyset --- *
229 * A keyset contains a set of symmetric keys for encrypting and decrypting
230 * packets. Keysets are stored in a list, sorted in reverse order of
231 * creation, so that the most recent keyset (the one most likely to be used)
234 * Each keyset has a time limit and a data limit. The keyset is destroyed
235 * when either it has existed for too long, or it has been used to encrypt
236 * too much data. New key exchanges are triggered when keys are close to
240 typedef struct keyset
{
241 struct keyset
*next
; /* Next active keyset in the list */
242 unsigned ref
; /* Reference count for keyset */
243 struct peer
*p
; /* Pointer to peer structure */
244 time_t t_exp
; /* Expiry time for this keyset */
245 unsigned long sz_exp
, sz_regen
; /* Data limits for the keyset */
246 T( unsigned seq
; ) /* Sequence number for tracing */
247 unsigned f
; /* Various useful flags */
248 gcipher
*cin
, *cout
; /* Keyset ciphers for encryption */
249 size_t tagsz
; /* Length to truncate MAC tags */
250 gmac
*min
, *mout
; /* Keyset MACs for integrity */
251 uint32 oseq
; /* Outbound sequence number */
252 seqwin iseq
; /* Inbound sequence number */
255 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
256 #define KSF_LINK 2u /* Key is in a linked list */
258 #define KSERR_REGEN -1 /* Regenerate keys */
259 #define KSERR_NOKEYS -2 /* No keys left */
260 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
261 #define KSERR_SEQ -4 /* Incorrect sequence number */
262 #define KSERR_MALFORMED -5 /* Input ciphertext is broken */
264 /* --- Key exchange --- *
266 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
267 * Protocol has a number of desirable features (e.g., perfect forward
268 * secrecy, and zero-knowledge authentication) which make it attractive for
269 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
273 typedef struct retry
{
274 double t
; /* Current retry time */
279 typedef struct kxchal
{
280 struct keyexch
*kx
; /* Pointer back to key exchange */
281 ge
*c
; /* Responder's challenge */
282 ge
*r
; /* My reply to the challenge */
283 keyset
*ks
; /* Pointer to temporary keyset */
284 unsigned f
; /* Various useful flags */
285 sel_timer t
; /* Response timer for challenge */
286 retry rs
; /* Retry state */
287 octet hc
[MAXHASHSZ
]; /* Hash of his challenge */
288 octet ck
[MAXHASHSZ
]; /* His magical check value */
289 octet hswrq_in
[MAXHASHSZ
]; /* Inbound switch request message */
290 octet hswok_in
[MAXHASHSZ
]; /* Inbound switch confirmation */
291 octet hswrq_out
[MAXHASHSZ
]; /* Outbound switch request message */
292 octet hswok_out
[MAXHASHSZ
]; /* Outbound switch confirmation */
295 typedef struct keyexch
{
296 struct peer
*p
; /* Pointer back to the peer */
297 kdata
*kpriv
; /* Private key and related info */
298 kdata
*kpub
; /* Peer's public key */
299 keyset
**ks
; /* Peer's list of keysets */
300 unsigned f
; /* Various useful flags */
301 unsigned s
; /* Current state in exchange */
302 sel_timer t
; /* Timer for next exchange */
303 retry rs
; /* Retry state */
304 mp
*alpha
; /* My temporary secret */
305 ge
*c
; /* My challenge */
306 ge
*rx
; /* The expected response */
307 unsigned nr
; /* Number of extant responses */
308 time_t t_valid
; /* When this exchange goes bad */
309 octet hc
[MAXHASHSZ
]; /* Hash of my challenge */
310 kxchal
*r
[KX_NCHAL
]; /* Array of challenges */
313 #define KXF_TIMER 1u /* Waiting for a timer to go off */
314 #define KXF_DEAD 2u /* The key-exchanger isn't up */
315 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
316 #define KXF_CORK 8u /* Don't send anything yet */
319 KXS_DEAD
, /* Uninitialized state (magical) */
320 KXS_CHAL
, /* Main answer-challenges state */
321 KXS_COMMIT
, /* Committed: send switch request */
322 KXS_SWITCH
/* Switched: send confirmation */
325 /* --- Tunnel structure --- *
327 * Used to maintain system-specific information about the tunnel interface.
330 typedef struct tunnel tunnel
;
333 typedef struct tunnel_ops
{
334 const char *name
; /* Name of this tunnel driver */
335 unsigned flags
; /* Various interesting flags */
336 #define TUNF_PRIVOPEN 1u /* Need helper to open file */
337 void (*init
)(void); /* Initializes the system */
338 tunnel
*(*create
)(struct peer */
*p*/
, int /*fd*/, char **/
*ifn*/
);
339 /* Initializes a new tunnel */
340 void (*setifname
)(tunnel */
*t*/
, const char */
*ifn*/
);
341 /* Notifies ifname change */
342 void (*inject
)(tunnel */
*t*/
, buf */
*b*/
); /* Sends packet through if */
343 void (*destroy
)(tunnel */
*t*/
); /* Destroys a tunnel */
346 #ifndef TUN_INTERNALS
347 struct tunnel
{ const tunnel_ops
*ops
; };
350 /* --- Peer statistics --- *
352 * Contains various interesting and not-so-interesting statistics about a
353 * peer. This is updated by various parts of the code. The format of the
354 * structure isn't considered private, and @p_stats@ returns a pointer to the
355 * statistics block for a given peer.
358 typedef struct stats
{
359 unsigned long sz_in
, sz_out
; /* Size of all data in and out */
360 unsigned long sz_kxin
, sz_kxout
; /* Size of key exchange messages */
361 unsigned long sz_ipin
, sz_ipout
; /* Size of encapsulated IP packets */
362 time_t t_start
, t_last
, t_kx
; /* Time peer created, last pk, kx */
363 unsigned long n_reject
; /* Number of rejected packets */
364 unsigned long n_in
, n_out
; /* Number of packets in and out */
365 unsigned long n_kxin
, n_kxout
; /* Number of key exchange packets */
366 unsigned long n_ipin
, n_ipout
; /* Number of encrypted packets */
369 /* --- Peer structure --- *
371 * The main structure which glues everything else together.
374 typedef struct peerspec
{
375 char *name
; /* Peer's name */
376 char *privtag
; /* Private key tag */
377 char *tag
; /* Public key tag */
378 const tunnel_ops
*tops
; /* Tunnel operations */
379 unsigned long t_ka
; /* Keep alive interval */
380 addr sa
; /* Socket address to speak to */
381 size_t sasz
; /* Socket address size */
382 unsigned f
; /* Flags for the peer */
383 #define PSF_KXMASK 255u /* Key-exchange flags to set */
384 #define PSF_MOBILE 256u /* Address may change rapidly */
387 typedef struct peer_byname
{
392 typedef struct peer_byaddr
{
397 typedef struct peer
{
398 peer_byname
*byname
; /* Lookup-by-name block */
399 peer_byaddr
*byaddr
; /* Lookup-by-address block */
400 struct ping
*pings
; /* Pings we're waiting for */
401 peerspec spec
; /* Specifications for this peer */
402 tunnel
*t
; /* Tunnel for local packets */
403 char *ifname
; /* Interface name for tunnel */
404 keyset
*ks
; /* List head for keysets */
405 buf b
; /* Buffer for sending packets */
406 stats st
; /* Statistics */
407 keyexch kx
; /* Key exchange protocol block */
408 sel_timer tka
; /* Timer for keepalives */
411 typedef struct peer_iter
{ sym_iter i
; } peer_iter
;
413 typedef struct ping
{
414 struct ping
*next
, *prev
; /* Links to next and previous */
415 peer
*p
; /* Peer so we can free it */
416 unsigned msg
; /* Kind of response expected */
417 uint32 id
; /* Id so we can recognize response */
418 octet magic
[32]; /* Some random data */
419 sel_timer t
; /* Timeout for ping */
420 void (*func
)(int /*rc*/, void */
*arg*/
); /* Function to call when done */
421 void *arg
; /* Argument for callback */
432 /* --- Admin structure --- */
434 #define OBUFSZ 16384u
436 typedef struct obuf
{
437 struct obuf
*next
; /* Next buffer in list */
438 char *p_in
, *p_out
; /* Pointers into the buffer */
439 char buf
[OBUFSZ
]; /* The actual buffer */
442 typedef struct oqueue
{
443 obuf
*hd
, *tl
; /* Head and tail pointers */
448 typedef struct admin_bgop
{
449 struct admin_bgop
*next
, *prev
; /* Links to next and previous */
450 struct admin
*a
; /* Owner job */
451 char *tag
; /* Tag string for messages */
452 void (*cancel
)(struct admin_bgop
*); /* Destructor function */
455 typedef struct admin_resop
{
456 admin_bgop bg
; /* Background operation header */
457 char *addr
; /* Hostname to be resolved */
458 bres_client r
; /* Background resolver task */
459 sel_timer t
; /* Timer for resolver */
460 addr sa
; /* Socket address */
461 size_t sasz
; /* Socket address size */
462 void (*func
)(struct admin_resop
*, int); /* Handler */
465 enum { ARES_OK
, ARES_FAIL
};
467 typedef struct admin_addop
{
468 admin_resop r
; /* Name resolution header */
469 peerspec peer
; /* Peer pending creation */
472 typedef struct admin_pingop
{
473 admin_bgop bg
; /* Background operation header */
474 ping ping
; /* Ping pending response */
475 struct timeval pingtime
; /* Time last ping was sent */
478 typedef struct admin_service
{
479 sym_base _b
; /* Hash table base structure */
480 char *version
; /* The provided version */
481 struct admin
*prov
; /* Which client provides me */
482 struct admin_service
*next
, *prev
; /* Client's list of services */
485 typedef struct admin_svcop
{
486 admin_bgop bg
; /* Background operation header */
487 struct admin
*prov
; /* Client servicing this job */
488 unsigned index
; /* This job's index */
489 struct admin_svcop
*next
, *prev
; /* Links for provider's jobs */
492 typedef struct admin_jobentry
{
493 unsigned short seq
; /* Zero if unused */
495 admin_svcop
*op
; /* Operation, if slot in use, ... */
496 uint32 next
; /* ... or index of next free slot */
500 typedef struct admin_jobtable
{
501 uint32 n
, sz
; /* Used slots and table size */
502 admin_svcop
*active
; /* List of active jobs */
503 uint32 free
; /* Index of first free slot */
504 admin_jobentry
*v
; /* And the big array of entries */
507 typedef struct admin
{
508 struct admin
*next
, *prev
; /* Links to next and previous */
509 unsigned f
; /* Various useful flags */
510 unsigned ref
; /* Reference counter */
512 unsigned seq
; /* Sequence number for tracing */
514 oqueue out
; /* Output buffer list */
515 oqueue delay
; /* Delayed output buffer list */
516 admin_bgop
*bg
; /* Backgrounded operations */
517 admin_service
*svcs
; /* Which services I provide */
518 admin_jobtable j
; /* Table of outstanding jobs */
519 selbuf b
; /* Line buffer for commands */
520 sel_file w
; /* Selector for write buffering */
523 #define AF_DEAD 1u /* Destroy this admin block */
524 #define AF_CLOSE 2u /* Client closed connection */
525 #define AF_NOTE 4u /* Catch notifications */
526 #define AF_WARN 8u /* Catch warning messages */
528 #define AF_TRACE 16u /* Catch tracing */
530 #define AF_FOREGROUND 32u /* Quit server when client closes */
533 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
535 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
538 /*----- Global variables --------------------------------------------------*/
540 extern sel_state sel
; /* Global I/O event state */
541 extern octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
], buf_t
[PKBUFSZ
], buf_u
[PKBUFSZ
];
542 extern const tunnel_ops
*tunnels
[]; /* Table of tunnels (0-term) */
543 extern const tunnel_ops
*tun_default
; /* Default tunnel to use */
544 extern kdata
*master
; /* Default private key */
545 extern const char *tag_priv
; /* Default private key tag */
548 extern const trace_opt tr_opts
[]; /* Trace options array */
549 extern unsigned tr_flags
; /* Trace options flags */
552 /*----- Other macros ------------------------------------------------------*/
554 #define TIMER noise_timer(RAND_GLOBAL)
556 /*----- Key management ----------------------------------------------------*/
558 /* --- @km_init@ --- *
560 * Arguments: @const char *privkr@ = private keyring file
561 * @const char *pubkr@ = public keyring file
562 * @const char *ptag@ = default private-key tag
566 * Use: Initializes the key-management machinery, loading the
567 * keyrings and so on.
570 extern void km_init(const char */
*privkr*/
, const char */
*pubkr*/
,
571 const char */
*ptag*/
);
573 /* --- @km_reload@ --- *
577 * Returns: Zero if OK, nonzero to force reloading of keys.
579 * Use: Checks the keyrings to see if they need reloading.
582 extern int km_reload(void);
584 /* --- @km_findpub@, @km_findpriv@ --- *
586 * Arguments: @const char *tag@ = key tag to load
588 * Returns: Pointer to the kdata object if successful, or null on error.
590 * Use: Fetches a public or private key from the keyring.
593 extern kdata
*km_findpub(const char */
*tag*/
);
594 extern kdata
*km_findpriv(const char */
*tag*/
);
596 /* --- @km_samealgsp@ --- *
598 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
600 * Returns: Nonzero if their two algorithm selections are the same.
602 * Use: Checks sameness of algorithm selections: used to ensure that
603 * peers are using sensible algorithms.
606 extern int km_samealgsp(const kdata */
*kdx*/
, const kdata */
*kdy*/
);
608 /* --- @km_ref@ --- *
610 * Arguments: @kdata *kd@ = pointer to the kdata object
614 * Use: Claim a new reference to a kdata object.
617 extern void km_ref(kdata */
*kd*/
);
619 /* --- @km_unref@ --- *
621 * Arguments: @kdata *kd@ = pointer to the kdata object
625 * Use: Releases a reference to a kdata object.
628 extern void km_unref(kdata */
*kd*/
);
630 /* --- @km_tag@ --- *
632 * Arguments: @kdata *kd@ - pointer to the kdata object
634 * Returns: A pointer to the short tag by which the kdata was loaded.
637 extern const char *km_tag(kdata */
*kd*/
);
639 /*----- Key exchange ------------------------------------------------------*/
641 /* --- @kx_start@ --- *
643 * Arguments: @keyexch *kx@ = pointer to key exchange context
644 * @int forcep@ = nonzero to ignore the quiet timer
648 * Use: Stimulates a key exchange. If a key exchage is in progress,
649 * a new challenge is sent (unless the quiet timer forbids
650 * this); if no exchange is in progress, one is commenced.
653 extern void kx_start(keyexch */
*kx*/
, int /*forcep*/);
655 /* --- @kx_message@ --- *
657 * Arguments: @keyexch *kx@ = pointer to key exchange context
658 * @unsigned msg@ = the message code
659 * @buf *b@ = pointer to buffer containing the packet
663 * Use: Reads a packet containing key exchange messages and handles
667 extern void kx_message(keyexch */
*kx*/
, unsigned /*msg*/, buf */
*b*/
);
669 /* --- @kx_free@ --- *
671 * Arguments: @keyexch *kx@ = pointer to key exchange context
675 * Use: Frees everything in a key exchange context.
678 extern void kx_free(keyexch */
*kx*/
);
680 /* --- @kx_newkeys@ --- *
682 * Arguments: @keyexch *kx@ = pointer to key exchange context
686 * Use: Informs the key exchange module that its keys may have
687 * changed. If fetching the new keys fails, the peer will be
688 * destroyed, we log messages and struggle along with the old
692 extern void kx_newkeys(keyexch */
*kx*/
);
694 /* --- @kx_init@ --- *
696 * Arguments: @keyexch *kx@ = pointer to key exchange context
697 * @peer *p@ = pointer to peer context
698 * @keyset **ks@ = pointer to keyset list
699 * @unsigned f@ = various useful flags
701 * Returns: Zero if OK, nonzero if it failed.
703 * Use: Initializes a key exchange module. The module currently
704 * contains no keys, and will attempt to initiate a key
708 extern int kx_init(keyexch */
*kx*/
, peer */
*p*/
,
709 keyset
**/
*ks*/
, unsigned /*f*/);
711 /*----- Keysets and symmetric cryptography --------------------------------*/
713 /* --- @ks_drop@ --- *
715 * Arguments: @keyset *ks@ = pointer to a keyset
719 * Use: Decrements a keyset's reference counter. If the counter hits
720 * zero, the keyset is freed.
723 extern void ks_drop(keyset */
*ks*/
);
725 /* --- @ks_gen@ --- *
727 * Arguments: @const void *k@ = pointer to key material
728 * @size_t x, y, z@ = offsets into key material (see below)
729 * @peer *p@ = pointer to peer information
731 * Returns: A pointer to the new keyset.
733 * Use: Derives a new keyset from the given key material. The
734 * offsets @x@, @y@ and @z@ separate the key material into three
735 * parts. Between the @k@ and @k + x@ is `my' contribution to
736 * the key material; between @k + x@ and @k + y@ is `your'
737 * contribution; and between @k + y@ and @k + z@ is a shared
738 * value we made together. These are used to construct two
739 * pairs of symmetric keys. Each pair consists of an encryption
740 * key and a message authentication key. One pair is used for
741 * outgoing messages, the other for incoming messages.
743 * The new key is marked so that it won't be selected for output
744 * by @ksl_encrypt@. You can still encrypt data with it by
745 * calling @ks_encrypt@ directly.
748 extern keyset
*ks_gen(const void */
*k*/
,
749 size_t /*x*/, size_t /*y*/, size_t /*z*/,
752 /* --- @ks_activate@ --- *
754 * Arguments: @keyset *ks@ = pointer to a keyset
758 * Use: Activates a keyset, so that it can be used for encrypting
762 extern void ks_activate(keyset */
*ks*/
);
764 /* --- @ks_encrypt@ --- *
766 * Arguments: @keyset *ks@ = pointer to a keyset
767 * @unsigned ty@ = message type
768 * @buf *b@ = pointer to input buffer
769 * @buf *bb@ = pointer to output buffer
771 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
772 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
773 * returns zero if there was insufficient buffer (but the output
774 * buffer is broken in this case).
776 * Use: Encrypts a block of data using the key. Note that the `key
777 * ought to be replaced' notification is only ever given once
778 * for each key. Also note that this call forces a keyset to be
779 * used even if it's marked as not for data output.
782 extern int ks_encrypt(keyset */
*ks*/
, unsigned /*ty*/,
783 buf */
*b*/
, buf */
*bb*/
);
785 /* --- @ks_decrypt@ --- *
787 * Arguments: @keyset *ks@ = pointer to a keyset
788 * @unsigned ty@ = expected type code
789 * @buf *b@ = pointer to an input buffer
790 * @buf *bb@ = pointer to an output buffer
792 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
793 * zero if there was insufficient buffer (but the output buffer
794 * is broken in this case).
796 * Use: Attempts to decrypt a message using a given key. Note that
797 * requesting decryption with a key directly won't clear a
798 * marking that it's not for encryption.
801 extern int ks_decrypt(keyset */
*ks*/
, unsigned /*ty*/,
802 buf */
*b*/
, buf */
*bb*/
);
804 /* --- @ksl_free@ --- *
806 * Arguments: @keyset **ksroot@ = pointer to keyset list head
810 * Use: Frees (releases references to) all of the keys in a keyset.
813 extern void ksl_free(keyset
**/
*ksroot*/
);
815 /* --- @ksl_link@ --- *
817 * Arguments: @keyset **ksroot@ = pointer to keyset list head
818 * @keyset *ks@ = pointer to a keyset
822 * Use: Links a keyset into a list. A keyset can only be on one list
823 * at a time. Bad things happen otherwise.
826 extern void ksl_link(keyset
**/
*ksroot*/
, keyset */
*ks*/
);
828 /* --- @ksl_prune@ --- *
830 * Arguments: @keyset **ksroot@ = pointer to keyset list head
834 * Use: Prunes the keyset list by removing keys which mustn't be used
838 extern void ksl_prune(keyset
**/
*ksroot*/
);
840 /* --- @ksl_encrypt@ --- *
842 * Arguments: @keyset **ksroot@ = pointer to keyset list head
843 * @unsigned ty@ = message type
844 * @buf *b@ = pointer to input buffer
845 * @buf *bb@ = pointer to output buffer
847 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
848 * new key; @KSERR_NOKEYS@ if there are no suitable keys
849 * available. Also returns zero if there was insufficient
850 * buffer space (but the output buffer is broken in this case).
852 * Use: Encrypts a packet.
855 extern int ksl_encrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
856 buf */
*b*/
, buf */
*bb*/
);
858 /* --- @ksl_decrypt@ --- *
860 * Arguments: @keyset **ksroot@ = pointer to keyset list head
861 * @unsigned ty@ = expected type code
862 * @buf *b@ = pointer to input buffer
863 * @buf *bb@ = pointer to output buffer
865 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
866 * zero if there was insufficient buffer (but the output buffer
867 * is broken in this case).
869 * Use: Decrypts a packet.
872 extern int ksl_decrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
873 buf */
*b*/
, buf */
*bb*/
);
875 /*----- Challenges --------------------------------------------------------*/
879 * Arguments: @buf *b@ = where to put the challenge
881 * Returns: Zero if OK, nonzero on error.
883 * Use: Issues a new challenge.
886 extern int c_new(buf */
*b*/
);
888 /* --- @c_check@ --- *
890 * Arguments: @buf *b@ = where to find the challenge
892 * Returns: Zero if OK, nonzero if it didn't work.
894 * Use: Checks a challenge. On failure, the buffer is broken.
897 extern int c_check(buf */
*b*/
);
899 /*----- Administration interface ------------------------------------------*/
901 #define A_END ((char *)0)
903 /* --- @a_vformat@ --- *
905 * Arguments: @dstr *d@ = where to leave the formatted message
906 * @const char *fmt@ = pointer to format string
907 * @va_list ap@ = arguments in list
911 * Use: Main message token formatting driver. The arguments are
912 * interleaved formatting tokens and their parameters, finally
913 * terminated by an entry @A_END@.
917 * * "*..." ... -- pretokenized @dstr_putf@-like string
919 * * "?ADDR" SOCKADDR -- a socket address, to be converted
921 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
923 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
925 * * "?PEER" PEER -- peer's name
927 * * "?ERRNO" ERRNO -- system error code
929 * * "[!]..." ... -- @dstr_putf@-like string as single token
932 extern void a_vformat(dstr */
*d*/
, const char */
*fmt*/
, va_list /*ap*/);
934 /* --- @a_format@ --- *
936 * Arguments: @dstr *d@ = where to leave the formatted message
937 * @const char *fmt@ = pointer to format string
941 * Use: Writes a tokenized message into a string, for later
945 extern void a_format(dstr */
*d*/
, const char */
*fmt*/
, ...);
947 /* --- @a_warn@ --- *
949 * Arguments: @const char *fmt@ = pointer to format string
950 * @...@ = other arguments
954 * Use: Informs all admin connections of a warning.
957 extern void a_warn(const char */
*fmt*/
, ...);
959 /* --- @a_notify@ --- *
961 * Arguments: @const char *fmt@ = pointer to format string
962 * @...@ = other arguments
966 * Use: Sends a notification to interested admin connections.
969 extern void a_notify(const char */
*fmt*/
, ...);
971 /* --- @a_create@ --- *
973 * Arguments: @int fd_in, fd_out@ = file descriptors to use
974 * @unsigned f@ = initial flags to set
978 * Use: Creates a new admin connection.
981 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
983 /* --- @a_quit@ --- *
989 * Use: Shuts things down nicely.
992 extern void a_quit(void);
994 /* --- @a_preselect@ --- *
1000 * Use: Informs the admin module that we're about to select again,
1001 * and that it should do cleanup things it has delayed until a
1005 extern void a_preselect(void);
1007 /* --- @a_daemon@ --- *
1013 * Use: Informs the admin module that it's a daemon.
1016 extern void a_daemon(void);
1018 /* --- @a_init@ --- *
1020 * Arguments: @const char *sock@ = socket name to create
1021 * @uid_t u@ = user to own the socket
1022 * @gid_t g@ = group to own the socket
1023 * @mode_t m@ = permissions to set on the socket
1027 * Use: Creates the admin listening socket.
1030 extern void a_init(const char */
*sock*/
,
1031 uid_t
/*u*/, gid_t
/*g*/, mode_t
/*m*/);
1033 /*----- Mapping with addresses as keys ------------------------------------*/
1035 /* --- @am_create@ --- *
1037 * Arguments: @addrmap *m@ = pointer to map
1041 * Use: Create an address map, properly set up.
1044 extern void am_create(addrmap */
*m*/
);
1046 /* --- @am_destroy@ --- *
1048 * Arguments: @addrmap *m@ = pointer to map
1052 * Use: Destroy an address map, throwing away all the entries.
1055 extern void am_destroy(addrmap */
*m*/
);
1057 /* --- @am_find@ --- *
1059 * Arguments: @addrmap *m@ = pointer to map
1060 * @const addr *a@ = address to look up
1061 * @size_t sz@ = size of block to allocate
1062 * @unsigned *f@ = where to store flags
1064 * Returns: Pointer to found item, or null.
1066 * Use: Finds a record with the given IP address, set @*f@ nonzero
1067 * and returns it. If @sz@ is zero, and no match was found,
1068 * return null; otherwise allocate a new block of @sz@ bytes,
1069 * clear @*f@ to zero and return the block pointer.
1072 extern void *am_find(addrmap */
*m*/
, const addr */
*a*/
,
1073 size_t /*sz*/, unsigned */
*f*/
);
1075 /* --- @am_remove@ --- *
1077 * Arguments: @addrmap *m@ = pointer to map
1078 * @void *i@ = pointer to the item
1082 * Use: Removes an item from the map.
1085 extern void am_remove(addrmap */
*m*/
, void */
*i*/
);
1087 /*----- Privilege separation ----------------------------------------------*/
1089 /* --- @ps_trace@ --- *
1091 * Arguments: @unsigned mask@ = trace mask to check
1092 * @const char *fmt@ = message format
1093 * @...@ = values for placeholders
1097 * Use: Writes a trace message.
1100 T( extern void ps_trace(unsigned /*mask*/, const char */
*fmt*/
, ...); )
1102 /* --- @ps_warn@ --- *
1104 * Arguments: @const char *fmt@ = message format
1105 * @...@ = values for placeholders
1109 * Use: Writes a warning message.
1112 extern void ps_warn(const char */
*fmt*/
, ...);
1114 /* --- @ps_tunfd@ --- *
1116 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1117 * @char **ifn@ = where to put the interface name
1119 * Returns: The file descriptor, or @-1@ on error.
1121 * Use: Fetches a file descriptor for a tunnel driver.
1124 extern int ps_tunfd(const tunnel_ops */
*tops*/
, char **/
*ifn*/
);
1126 /* --- @ps_split@ --- *
1128 * Arguments: @int detachp@ = whether to detach the child from its terminal
1132 * Use: Separates off the privileged tunnel-opening service from the
1133 * rest of the server.
1136 extern void ps_split(int /*detachp*/);
1138 /* --- @ps_quit@ --- *
1144 * Use: Detaches from the helper process.
1147 extern void ps_quit(void);
1149 /*----- Peer management ---------------------------------------------------*/
1151 /* --- @p_txstart@ --- *
1153 * Arguments: @peer *p@ = pointer to peer block
1154 * @unsigned msg@ = message type code
1156 * Returns: A pointer to a buffer to write to.
1158 * Use: Starts sending to a peer. Only one send can happen at a
1162 extern buf
*p_txstart(peer */
*p*/
, unsigned /*msg*/);
1164 /* --- @p_txend@ --- *
1166 * Arguments: @peer *p@ = pointer to peer block
1170 * Use: Sends a packet to the peer.
1173 extern void p_txend(peer */
*p*/
);
1175 /* --- @p_pingsend@ --- *
1177 * Arguments: @peer *p@ = destination peer
1178 * @ping *pg@ = structure to fill in
1179 * @unsigned type@ = message type
1180 * @unsigned long timeout@ = how long to wait before giving up
1181 * @void (*func)(int, void *)@ = callback function
1182 * @void *arg@ = argument for callback
1184 * Returns: Zero if successful, nonzero if it failed.
1186 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1187 * if we get an answer within the timeout, or zero if no answer.
1190 extern int p_pingsend(peer */
*p*/
, ping */
*pg*/
, unsigned /*type*/,
1191 unsigned long /*timeout*/,
1192 void (*/
*func*/
)(int, void *), void */
*arg*/
);
1194 /* --- @p_pingdone@ --- *
1196 * Arguments: @ping *p@ = ping structure
1197 * @int rc@ = return code to pass on
1201 * Use: Disposes of a ping structure, maybe sending a notification.
1204 extern void p_pingdone(ping */
*p*/
, int /*rc*/);
1206 /* --- @p_greet@ --- *
1208 * Arguments: @peer *p@ = peer to send to
1209 * @const void *c@ = pointer to challenge
1210 * @size_t sz@ = size of challenge
1214 * Use: Sends a greeting packet.
1217 extern void p_greet(peer */
*p*/
, const void */
*c*/
, size_t /*sz*/);
1219 /* --- @p_tun@ --- *
1221 * Arguments: @peer *p@ = pointer to peer block
1222 * @buf *b@ = buffer containing incoming packet
1226 * Use: Handles a packet which needs to be sent to a peer.
1229 extern void p_tun(peer */
*p*/
, buf */
*b*/
);
1231 /* --- @p_keyreload@ --- *
1237 * Use: Forces a check of the daemon's keyring files.
1240 extern void p_keyreload(void);
1242 /* --- @p_interval@ --- *
1248 * Use: Called periodically to do tidying.
1251 extern void p_interval(void);
1253 /* --- @p_stats@ --- *
1255 * Arguments: @peer *p@ = pointer to a peer block
1257 * Returns: A pointer to the peer's statistics.
1260 extern stats
*p_stats(peer */
*p*/
);
1262 /* --- @p_ifname@ --- *
1264 * Arguments: @peer *p@ = pointer to a peer block
1266 * Returns: A pointer to the peer's interface name.
1269 extern const char *p_ifname(peer */
*p*/
);
1271 /* --- @p_setifname@ --- *
1273 * Arguments: @peer *p@ = pointer to a peer block
1274 * @const char *name@ = pointer to the new name
1278 * Use: Changes the name held for a peer's interface.
1281 extern void p_setifname(peer */
*p*/
, const char */
*name*/
);
1283 /* --- @p_addr@ --- *
1285 * Arguments: @peer *p@ = pointer to a peer block
1287 * Returns: A pointer to the peer's address.
1290 extern const addr
*p_addr(peer */
*p*/
);
1292 /* --- @p_init@ --- *
1294 * Arguments: @struct in_addr addr@ = address to bind to
1295 * @unsigned port@ = port number to listen to
1299 * Use: Initializes the peer system; creates the socket.
1302 extern void p_init(struct in_addr
/*addr*/, unsigned /*port*/);
1304 /* --- @p_port@ --- *
1308 * Returns: Port number used for socket.
1311 unsigned p_port(void);
1313 /* --- @p_create@ --- *
1315 * Arguments: @peerspec *spec@ = information about this peer
1317 * Returns: Pointer to the peer block, or null if it failed.
1319 * Use: Creates a new named peer block. No peer is actually attached
1323 extern peer
*p_create(peerspec */
*spec*/
);
1325 /* --- @p_name@ --- *
1327 * Arguments: @peer *p@ = pointer to a peer block
1329 * Returns: A pointer to the peer's name.
1331 * Use: Equivalent to @p_spec(p)->name@.
1334 extern const char *p_name(peer */
*p*/
);
1336 /* --- @p_tag@ --- *
1338 * Arguments: @peer *p@ = pointer to a peer block
1340 * Returns: A pointer to the peer's public key tag.
1343 extern const char *p_tag(peer */
*p*/
);
1345 /* --- @p_privtag@ --- *
1347 * Arguments: @peer *p@ = pointer to a peer block
1349 * Returns: A pointer to the peer's private key tag.
1352 extern const char *p_privtag(peer */
*p*/
);
1354 /* --- @p_spec@ --- *
1356 * Arguments: @peer *p@ = pointer to a peer block
1358 * Returns: Pointer to the peer's specification
1361 extern const peerspec
*p_spec(peer */
*p*/
);
1363 /* --- @p_findbyaddr@ --- *
1365 * Arguments: @const addr *a@ = address to look up
1367 * Returns: Pointer to the peer block, or null if not found.
1369 * Use: Finds a peer by address.
1372 extern peer
*p_findbyaddr(const addr */
*a*/
);
1374 /* --- @p_find@ --- *
1376 * Arguments: @const char *name@ = name to look up
1378 * Returns: Pointer to the peer block, or null if not found.
1380 * Use: Finds a peer by name.
1383 extern peer
*p_find(const char */
*name*/
);
1385 /* --- @p_destroy@ --- *
1387 * Arguments: @peer *p@ = pointer to a peer
1391 * Use: Destroys a peer.
1394 extern void p_destroy(peer */
*p*/
);
1396 /* --- @FOREACH_PEER@ --- *
1398 * Arguments: @p@ = name to bind to each peer
1399 * @stuff@ = thing to do for each item
1401 * Use: Does something for each current peer.
1404 #define FOREACH_PEER(p, stuff) do { \
1407 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
1410 /* --- @p_mkiter@ --- *
1412 * Arguments: @peer_iter *i@ = pointer to an iterator
1416 * Use: Initializes the iterator.
1419 extern void p_mkiter(peer_iter */
*i*/
);
1421 /* --- @p_next@ --- *
1423 * Arguments: @peer_iter *i@ = pointer to an iterator
1425 * Returns: Next peer, or null if at the end.
1427 * Use: Returns the next peer.
1430 extern peer
*p_next(peer_iter */
*i*/
);
1432 /*----- Tunnel drivers ----------------------------------------------------*/
1435 extern const tunnel_ops tun_linux
;
1439 extern const tunnel_ops tun_unet
;
1443 extern const tunnel_ops tun_bsd
;
1446 extern const tunnel_ops tun_slip
;
1448 /*----- Other handy utilities ---------------------------------------------*/
1450 /* --- @mpstr@ --- *
1452 * Arguments: @mp *m@ = a multiprecision integer
1454 * Returns: A pointer to the integer's textual representation.
1456 * Use: Converts a multiprecision integer to a string. Corrupts
1460 extern const char *mpstr(mp */
*m*/
);
1462 /* --- @gestr@ --- *
1464 * Arguments: @group *g@ = a group
1465 * @ge *x@ = a group element
1467 * Returns: A pointer to the element's textual representation.
1469 * Use: Converts a group element to a string. Corrupts
1473 extern const char *gestr(group */
*g*/
, ge */
*x*/
);
1475 /* --- @timestr@ --- *
1477 * Arguments: @time_t t@ = a time to convert
1479 * Returns: A pointer to a textual representation of the time.
1481 * Use: Converts a time to a textual representation. Corrupts
1485 extern const char *timestr(time_t /*t*/);
1487 /* --- @mystrieq@ --- *
1489 * Arguments: @const char *x, *y@ = two strings
1491 * Returns: True if @x@ and @y are equal, up to case.
1494 extern int mystrieq(const char */
*x*/
, const char */
*y*/
);
1496 /* --- @seq_reset@ --- *
1498 * Arguments: @seqwin *s@ = sequence-checking window
1502 * Use: Resets a sequence number window.
1505 extern void seq_reset(seqwin */
*s*/
);
1507 /* --- @seq_check@ --- *
1509 * Arguments: @seqwin *s@ = sequence-checking window
1510 * @uint32 q@ = sequence number to check
1511 * @const char *service@ = service to report message from
1513 * Returns: A @SEQ_@ code.
1515 * Use: Checks a sequence number against the window, updating things
1519 extern int seq_check(seqwin */
*s*/
, uint32
/*q*/, const char */
*service*/
);
1521 /*----- That's all, folks -------------------------------------------------*/