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>
93 #include <catacomb/gcipher.h>
94 #include <catacomb/gmac.h>
95 #include <catacomb/grand.h>
96 #include <catacomb/key.h>
97 #include <catacomb/paranoia.h>
99 #include <catacomb/noise.h>
100 #include <catacomb/rand.h>
102 #include <catacomb/mp.h>
103 #include <catacomb/mprand.h>
104 #include <catacomb/dh.h>
105 #include <catacomb/ec.h>
106 #include <catacomb/ec-keys.h>
107 #include <catacomb/group.h>
110 #include "protocol.h"
116 /*----- Magic numbers -----------------------------------------------------*/
118 /* --- Trace flags --- */
126 #define T_KEYEXCH 64u
127 #define T_KEYMGMT 128u
129 /* T_PRIVSEP in priv.h */
135 #define SEC(n) (n##u)
136 #define MIN(n) (n##u * 60u)
137 #define F_2P32 (65536.0*65536.0)
138 #define MEG(n) (n##ul * 1024ul * 1024ul)
140 /* --- Timing parameters --- */
142 #define T_EXP MIN(60) /* Expiry time for a key */
143 #define T_REGEN MIN(40) /* Regeneration time for a key */
145 #define T_VALID SEC(20) /* Challenge validity period */
146 #define T_RETRYMIN SEC(2) /* Minimum retry interval */
147 #define T_RETRYMAX MIN(5) /* Maximum retry interval */
148 #define T_RETRYGROW (5.0/4.0) /* Retry interval growth factor */
150 #define T_WOBBLE (1.0/3.0) /* Relative timer randomness */
152 /* --- Other things --- */
154 #define PKBUFSZ 65536
156 /*----- Cipher selections -------------------------------------------------*/
158 typedef struct algswitch
{
159 const gccipher
*c
; /* Symmetric encryption scheme */
160 const gccipher
*mgf
; /* Mask-generation function */
161 const gchash
*h
; /* Hash function */
162 const gcmac
*m
; /* Message authentication code */
163 size_t hashsz
; /* Hash output size */
164 size_t tagsz
; /* Length to truncate MAC tags */
165 size_t expsz
; /* Size of data to process */
166 size_t cksz
, mksz
; /* Key lengths for @c@ and @m@ */
169 typedef struct kdata
{
170 unsigned ref
; /* Reference counter */
171 struct knode
*kn
; /* Pointer to cache entry */
172 char *tag
; /* Full tag name of the key */
173 group
*g
; /* The group we work in */
174 size_t indexsz
; /* Size of exponent for the group */
175 mp
*kpriv
; /* The private key (or null) */
176 ge
*kpub
; /* The public key */
177 time_t t_exp
; /* Expiry time of the key */
178 algswitch algs
; /* Collection of algorithms */
181 typedef struct knode
{
182 sym_base _b
; /* Symbol table intrusion */
183 unsigned f
; /* Various flags */
184 #define KNF_BROKEN 1u /* Don't use this key any more */
185 struct keyhalf
*kh
; /* Pointer to the home keyhalf */
186 kdata
*kd
; /* Pointer to the key data */
189 #define MAXHASHSZ 64 /* Largest possible hash size */
191 #define HASH_STRING(h, s) GH_HASH((h), (s), sizeof(s))
193 /*----- Data structures ---------------------------------------------------*/
195 /* --- Socket addresses --- *
197 * A magic union of supported socket addresses.
202 struct sockaddr_in sin
;
205 /* --- Mapping keyed on addresses --- */
207 typedef struct addrmap
{
212 typedef struct addrmap_base
{
217 /* --- Sequence number checking --- */
219 typedef struct seqwin
{
220 uint32 seq
; /* First acceptable input sequence */
221 uint32 win
; /* Window of acceptable numbers */
224 #define SEQ_WINSZ 32 /* Bits in sequence number window */
226 /* --- A symmetric keyset --- *
228 * A keyset contains a set of symmetric keys for encrypting and decrypting
229 * packets. Keysets are stored in a list, sorted in reverse order of
230 * creation, so that the most recent keyset (the one most likely to be used)
233 * Each keyset has a time limit and a data limit. The keyset is destroyed
234 * when either it has existed for too long, or it has been used to encrypt
235 * too much data. New key exchanges are triggered when keys are close to
239 typedef struct keyset
{
240 struct keyset
*next
; /* Next active keyset in the list */
241 unsigned ref
; /* Reference count for keyset */
242 struct peer
*p
; /* Pointer to peer structure */
243 time_t t_exp
; /* Expiry time for this keyset */
244 unsigned long sz_exp
, sz_regen
; /* Data limits for the keyset */
245 T( unsigned seq
; ) /* Sequence number for tracing */
246 unsigned f
; /* Various useful flags */
247 gcipher
*cin
, *cout
; /* Keyset ciphers for encryption */
248 size_t tagsz
; /* Length to truncate MAC tags */
249 gmac
*min
, *mout
; /* Keyset MACs for integrity */
250 uint32 oseq
; /* Outbound sequence number */
251 seqwin iseq
; /* Inbound sequence number */
254 #define KSF_LISTEN 1u /* Don't encrypt packets yet */
255 #define KSF_LINK 2u /* Key is in a linked list */
257 #define KSERR_REGEN -1 /* Regenerate keys */
258 #define KSERR_NOKEYS -2 /* No keys left */
259 #define KSERR_DECRYPT -3 /* Unable to decrypt message */
260 #define KSERR_SEQ -4 /* Incorrect sequence number */
261 #define KSERR_MALFORMED -5 /* Input ciphertext is broken */
263 /* --- Key exchange --- *
265 * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers
266 * Protocol has a number of desirable features (e.g., perfect forward
267 * secrecy, and zero-knowledge authentication) which make it attractive for
268 * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and
272 typedef struct retry
{
273 double t
; /* Current retry time */
278 typedef struct kxchal
{
279 struct keyexch
*kx
; /* Pointer back to key exchange */
280 ge
*c
; /* Responder's challenge */
281 ge
*r
; /* My reply to the challenge */
282 keyset
*ks
; /* Pointer to temporary keyset */
283 unsigned f
; /* Various useful flags */
284 sel_timer t
; /* Response timer for challenge */
285 retry rs
; /* Retry state */
286 octet hc
[MAXHASHSZ
]; /* Hash of his challenge */
287 octet ck
[MAXHASHSZ
]; /* His magical check value */
288 octet hswrq_in
[MAXHASHSZ
]; /* Inbound switch request message */
289 octet hswok_in
[MAXHASHSZ
]; /* Inbound switch confirmation */
290 octet hswrq_out
[MAXHASHSZ
]; /* Outbound switch request message */
291 octet hswok_out
[MAXHASHSZ
]; /* Outbound switch confirmation */
294 typedef struct keyexch
{
295 struct peer
*p
; /* Pointer back to the peer */
296 kdata
*kpriv
; /* Private key and related info */
297 kdata
*kpub
; /* Peer's public key */
298 keyset
**ks
; /* Peer's list of keysets */
299 unsigned f
; /* Various useful flags */
300 unsigned s
; /* Current state in exchange */
301 sel_timer t
; /* Timer for next exchange */
302 retry rs
; /* Retry state */
303 mp
*alpha
; /* My temporary secret */
304 ge
*c
; /* My challenge */
305 ge
*rx
; /* The expected response */
306 unsigned nr
; /* Number of extant responses */
307 time_t t_valid
; /* When this exchange goes bad */
308 octet hc
[MAXHASHSZ
]; /* Hash of my challenge */
309 kxchal
*r
[KX_NCHAL
]; /* Array of challenges */
312 #define KXF_TIMER 1u /* Waiting for a timer to go off */
313 #define KXF_DEAD 2u /* The key-exchanger isn't up */
314 #define KXF_PUBKEY 4u /* Key exchanger has a public key */
315 #define KXF_CORK 8u /* Don't send anything yet */
318 KXS_DEAD
, /* Uninitialized state (magical) */
319 KXS_CHAL
, /* Main answer-challenges state */
320 KXS_COMMIT
, /* Committed: send switch request */
321 KXS_SWITCH
/* Switched: send confirmation */
324 /* --- Tunnel structure --- *
326 * Used to maintain system-specific information about the tunnel interface.
329 typedef struct tunnel tunnel
;
332 typedef struct tunnel_ops
{
333 const char *name
; /* Name of this tunnel driver */
334 unsigned flags
; /* Various interesting flags */
335 #define TUNF_PRIVOPEN 1u /* Need helper to open file */
336 void (*init
)(void); /* Initializes the system */
337 tunnel
*(*create
)(struct peer */
*p*/
, int /*fd*/, char **/
*ifn*/
);
338 /* Initializes a new tunnel */
339 void (*setifname
)(tunnel */
*t*/
, const char */
*ifn*/
);
340 /* Notifies ifname change */
341 void (*inject
)(tunnel */
*t*/
, buf */
*b*/
); /* Sends packet through if */
342 void (*destroy
)(tunnel */
*t*/
); /* Destroys a tunnel */
345 #ifndef TUN_INTERNALS
346 struct tunnel
{ const tunnel_ops
*ops
; };
349 /* --- Peer statistics --- *
351 * Contains various interesting and not-so-interesting statistics about a
352 * peer. This is updated by various parts of the code. The format of the
353 * structure isn't considered private, and @p_stats@ returns a pointer to the
354 * statistics block for a given peer.
357 typedef struct stats
{
358 unsigned long sz_in
, sz_out
; /* Size of all data in and out */
359 unsigned long sz_kxin
, sz_kxout
; /* Size of key exchange messages */
360 unsigned long sz_ipin
, sz_ipout
; /* Size of encapsulated IP packets */
361 time_t t_start
, t_last
, t_kx
; /* Time peer created, last pk, kx */
362 unsigned long n_reject
; /* Number of rejected packets */
363 unsigned long n_in
, n_out
; /* Number of packets in and out */
364 unsigned long n_kxin
, n_kxout
; /* Number of key exchange packets */
365 unsigned long n_ipin
, n_ipout
; /* Number of encrypted packets */
368 /* --- Peer structure --- *
370 * The main structure which glues everything else together.
373 typedef struct peerspec
{
374 char *name
; /* Peer's name */
375 char *privtag
; /* Private key tag */
376 char *tag
; /* Public key tag */
377 const tunnel_ops
*tops
; /* Tunnel operations */
378 unsigned long t_ka
; /* Keep alive interval */
379 addr sa
; /* Socket address to speak to */
380 size_t sasz
; /* Socket address size */
381 unsigned f
; /* Flags for the peer */
382 #define PSF_KXMASK 255u /* Key-exchange flags to set */
383 #define PSF_MOBILE 256u /* Address may change rapidly */
386 typedef struct peer_byname
{
391 typedef struct peer_byaddr
{
396 typedef struct peer
{
397 peer_byname
*byname
; /* Lookup-by-name block */
398 peer_byaddr
*byaddr
; /* Lookup-by-address block */
399 struct ping
*pings
; /* Pings we're waiting for */
400 peerspec spec
; /* Specifications for this peer */
401 tunnel
*t
; /* Tunnel for local packets */
402 char *ifname
; /* Interface name for tunnel */
403 keyset
*ks
; /* List head for keysets */
404 buf b
; /* Buffer for sending packets */
405 stats st
; /* Statistics */
406 keyexch kx
; /* Key exchange protocol block */
407 sel_timer tka
; /* Timer for keepalives */
410 typedef struct peer_iter
{ sym_iter i
; } peer_iter
;
412 typedef struct ping
{
413 struct ping
*next
, *prev
; /* Links to next and previous */
414 peer
*p
; /* Peer so we can free it */
415 unsigned msg
; /* Kind of response expected */
416 uint32 id
; /* Id so we can recognize response */
417 octet magic
[32]; /* Some random data */
418 sel_timer t
; /* Timeout for ping */
419 void (*func
)(int /*rc*/, void */
*arg*/
); /* Function to call when done */
420 void *arg
; /* Argument for callback */
431 /* --- Admin structure --- */
433 #define OBUFSZ 16384u
435 typedef struct obuf
{
436 struct obuf
*next
; /* Next buffer in list */
437 char *p_in
, *p_out
; /* Pointers into the buffer */
438 char buf
[OBUFSZ
]; /* The actual buffer */
441 typedef struct oqueue
{
442 obuf
*hd
, *tl
; /* Head and tail pointers */
447 typedef struct admin_bgop
{
448 struct admin_bgop
*next
, *prev
; /* Links to next and previous */
449 struct admin
*a
; /* Owner job */
450 char *tag
; /* Tag string for messages */
451 void (*cancel
)(struct admin_bgop
*); /* Destructor function */
454 typedef struct admin_resop
{
455 admin_bgop bg
; /* Background operation header */
456 char *addr
; /* Hostname to be resolved */
457 bres_client r
; /* Background resolver task */
458 sel_timer t
; /* Timer for resolver */
459 addr sa
; /* Socket address */
460 size_t sasz
; /* Socket address size */
461 void (*func
)(struct admin_resop
*, int); /* Handler */
464 enum { ARES_OK
, ARES_FAIL
};
466 typedef struct admin_addop
{
467 admin_resop r
; /* Name resolution header */
468 peerspec peer
; /* Peer pending creation */
471 typedef struct admin_pingop
{
472 admin_bgop bg
; /* Background operation header */
473 ping ping
; /* Ping pending response */
474 struct timeval pingtime
; /* Time last ping was sent */
477 typedef struct admin_service
{
478 sym_base _b
; /* Hash table base structure */
479 char *version
; /* The provided version */
480 struct admin
*prov
; /* Which client provides me */
481 struct admin_service
*next
, *prev
; /* Client's list of services */
484 typedef struct admin_svcop
{
485 admin_bgop bg
; /* Background operation header */
486 struct admin
*prov
; /* Client servicing this job */
487 unsigned index
; /* This job's index */
488 struct admin_svcop
*next
, *prev
; /* Links for provider's jobs */
491 typedef struct admin_jobentry
{
492 unsigned short seq
; /* Zero if unused */
494 admin_svcop
*op
; /* Operation, if slot in use, ... */
495 uint32 next
; /* ... or index of next free slot */
499 typedef struct admin_jobtable
{
500 uint32 n
, sz
; /* Used slots and table size */
501 admin_svcop
*active
; /* List of active jobs */
502 uint32 free
; /* Index of first free slot */
503 admin_jobentry
*v
; /* And the big array of entries */
506 typedef struct admin
{
507 struct admin
*next
, *prev
; /* Links to next and previous */
508 unsigned f
; /* Various useful flags */
509 unsigned ref
; /* Reference counter */
511 unsigned seq
; /* Sequence number for tracing */
513 oqueue out
; /* Output buffer list */
514 oqueue delay
; /* Delayed output buffer list */
515 admin_bgop
*bg
; /* Backgrounded operations */
516 admin_service
*svcs
; /* Which services I provide */
517 admin_jobtable j
; /* Table of outstanding jobs */
518 selbuf b
; /* Line buffer for commands */
519 sel_file w
; /* Selector for write buffering */
522 #define AF_DEAD 1u /* Destroy this admin block */
523 #define AF_CLOSE 2u /* Client closed connection */
524 #define AF_NOTE 4u /* Catch notifications */
525 #define AF_WARN 8u /* Catch warning messages */
527 #define AF_TRACE 16u /* Catch tracing */
529 #define AF_FOREGROUND 32u /* Quit server when client closes */
532 # define AF_ALLMSGS (AF_NOTE | AF_TRACE | AF_WARN)
534 # define AF_ALLMSGS (AF_NOTE | AF_WARN)
537 /*----- Global variables --------------------------------------------------*/
539 extern sel_state sel
; /* Global I/O event state */
540 extern octet buf_i
[PKBUFSZ
], buf_o
[PKBUFSZ
], buf_t
[PKBUFSZ
], buf_u
[PKBUFSZ
];
541 extern const tunnel_ops
*tunnels
[]; /* Table of tunnels (0-term) */
542 extern const tunnel_ops
*tun_default
; /* Default tunnel to use */
543 extern kdata
*master
; /* Default private key */
544 extern const char *tag_priv
; /* Default private key tag */
547 extern const trace_opt tr_opts
[]; /* Trace options array */
548 extern unsigned tr_flags
; /* Trace options flags */
551 /*----- Other macros ------------------------------------------------------*/
553 #define TIMER noise_timer(RAND_GLOBAL)
555 /*----- Key management ----------------------------------------------------*/
557 /* --- @km_init@ --- *
559 * Arguments: @const char *privkr@ = private keyring file
560 * @const char *pubkr@ = public keyring file
561 * @const char *ptag@ = default private-key tag
565 * Use: Initializes the key-management machinery, loading the
566 * keyrings and so on.
569 extern void km_init(const char */
*privkr*/
, const char */
*pubkr*/
,
570 const char */
*ptag*/
);
572 /* --- @km_reload@ --- *
576 * Returns: Zero if OK, nonzero to force reloading of keys.
578 * Use: Checks the keyrings to see if they need reloading.
581 extern int km_reload(void);
583 /* --- @km_findpub@, @km_findpriv@ --- *
585 * Arguments: @const char *tag@ = key tag to load
587 * Returns: Pointer to the kdata object if successful, or null on error.
589 * Use: Fetches a public or private key from the keyring.
592 extern kdata
*km_findpub(const char */
*tag*/
);
593 extern kdata
*km_findpriv(const char */
*tag*/
);
595 /* --- @km_samealgsp@ --- *
597 * Arguments: @const kdata *kdx, *kdy@ = two key data objects
599 * Returns: Nonzero if their two algorithm selections are the same.
601 * Use: Checks sameness of algorithm selections: used to ensure that
602 * peers are using sensible algorithms.
605 extern int km_samealgsp(const kdata */
*kdx*/
, const kdata */
*kdy*/
);
607 /* --- @km_ref@ --- *
609 * Arguments: @kdata *kd@ = pointer to the kdata object
613 * Use: Claim a new reference to a kdata object.
616 extern void km_ref(kdata */
*kd*/
);
618 /* --- @km_unref@ --- *
620 * Arguments: @kdata *kd@ = pointer to the kdata object
624 * Use: Releases a reference to a kdata object.
627 extern void km_unref(kdata */
*kd*/
);
629 /* --- @km_tag@ --- *
631 * Arguments: @kdata *kd@ - pointer to the kdata object
633 * Returns: A pointer to the short tag by which the kdata was loaded.
636 extern const char *km_tag(kdata */
*kd*/
);
638 /*----- Key exchange ------------------------------------------------------*/
640 /* --- @kx_start@ --- *
642 * Arguments: @keyexch *kx@ = pointer to key exchange context
643 * @int forcep@ = nonzero to ignore the quiet timer
647 * Use: Stimulates a key exchange. If a key exchage is in progress,
648 * a new challenge is sent (unless the quiet timer forbids
649 * this); if no exchange is in progress, one is commenced.
652 extern void kx_start(keyexch */
*kx*/
, int /*forcep*/);
654 /* --- @kx_message@ --- *
656 * Arguments: @keyexch *kx@ = pointer to key exchange context
657 * @unsigned msg@ = the message code
658 * @buf *b@ = pointer to buffer containing the packet
662 * Use: Reads a packet containing key exchange messages and handles
666 extern void kx_message(keyexch */
*kx*/
, unsigned /*msg*/, buf */
*b*/
);
668 /* --- @kx_free@ --- *
670 * Arguments: @keyexch *kx@ = pointer to key exchange context
674 * Use: Frees everything in a key exchange context.
677 extern void kx_free(keyexch */
*kx*/
);
679 /* --- @kx_newkeys@ --- *
681 * Arguments: @keyexch *kx@ = pointer to key exchange context
685 * Use: Informs the key exchange module that its keys may have
686 * changed. If fetching the new keys fails, the peer will be
687 * destroyed, we log messages and struggle along with the old
691 extern void kx_newkeys(keyexch */
*kx*/
);
693 /* --- @kx_init@ --- *
695 * Arguments: @keyexch *kx@ = pointer to key exchange context
696 * @peer *p@ = pointer to peer context
697 * @keyset **ks@ = pointer to keyset list
698 * @unsigned f@ = various useful flags
700 * Returns: Zero if OK, nonzero if it failed.
702 * Use: Initializes a key exchange module. The module currently
703 * contains no keys, and will attempt to initiate a key
707 extern int kx_init(keyexch */
*kx*/
, peer */
*p*/
,
708 keyset
**/
*ks*/
, unsigned /*f*/);
710 /*----- Keysets and symmetric cryptography --------------------------------*/
712 /* --- @ks_drop@ --- *
714 * Arguments: @keyset *ks@ = pointer to a keyset
718 * Use: Decrements a keyset's reference counter. If the counter hits
719 * zero, the keyset is freed.
722 extern void ks_drop(keyset */
*ks*/
);
724 /* --- @ks_gen@ --- *
726 * Arguments: @const void *k@ = pointer to key material
727 * @size_t x, y, z@ = offsets into key material (see below)
728 * @peer *p@ = pointer to peer information
730 * Returns: A pointer to the new keyset.
732 * Use: Derives a new keyset from the given key material. The
733 * offsets @x@, @y@ and @z@ separate the key material into three
734 * parts. Between the @k@ and @k + x@ is `my' contribution to
735 * the key material; between @k + x@ and @k + y@ is `your'
736 * contribution; and between @k + y@ and @k + z@ is a shared
737 * value we made together. These are used to construct two
738 * pairs of symmetric keys. Each pair consists of an encryption
739 * key and a message authentication key. One pair is used for
740 * outgoing messages, the other for incoming messages.
742 * The new key is marked so that it won't be selected for output
743 * by @ksl_encrypt@. You can still encrypt data with it by
744 * calling @ks_encrypt@ directly.
747 extern keyset
*ks_gen(const void */
*k*/
,
748 size_t /*x*/, size_t /*y*/, size_t /*z*/,
751 /* --- @ks_activate@ --- *
753 * Arguments: @keyset *ks@ = pointer to a keyset
757 * Use: Activates a keyset, so that it can be used for encrypting
761 extern void ks_activate(keyset */
*ks*/
);
763 /* --- @ks_encrypt@ --- *
765 * Arguments: @keyset *ks@ = pointer to a keyset
766 * @unsigned ty@ = message type
767 * @buf *b@ = pointer to input buffer
768 * @buf *bb@ = pointer to output buffer
770 * Returns: Zero if successful; @KSERR_REGEN@ if we should negotiate a
771 * new key; @KSERR_NOKEYS@ if the key is not usable. Also
772 * returns zero if there was insufficient buffer (but the output
773 * buffer is broken in this case).
775 * Use: Encrypts a block of data using the key. Note that the `key
776 * ought to be replaced' notification is only ever given once
777 * for each key. Also note that this call forces a keyset to be
778 * used even if it's marked as not for data output.
781 extern int ks_encrypt(keyset */
*ks*/
, unsigned /*ty*/,
782 buf */
*b*/
, buf */
*bb*/
);
784 /* --- @ks_decrypt@ --- *
786 * Arguments: @keyset *ks@ = pointer to a keyset
787 * @unsigned ty@ = expected type code
788 * @buf *b@ = pointer to an input buffer
789 * @buf *bb@ = pointer to an output buffer
791 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
792 * zero if there was insufficient buffer (but the output buffer
793 * is broken in this case).
795 * Use: Attempts to decrypt a message using a given key. Note that
796 * requesting decryption with a key directly won't clear a
797 * marking that it's not for encryption.
800 extern int ks_decrypt(keyset */
*ks*/
, unsigned /*ty*/,
801 buf */
*b*/
, buf */
*bb*/
);
803 /* --- @ksl_free@ --- *
805 * Arguments: @keyset **ksroot@ = pointer to keyset list head
809 * Use: Frees (releases references to) all of the keys in a keyset.
812 extern void ksl_free(keyset
**/
*ksroot*/
);
814 /* --- @ksl_link@ --- *
816 * Arguments: @keyset **ksroot@ = pointer to keyset list head
817 * @keyset *ks@ = pointer to a keyset
821 * Use: Links a keyset into a list. A keyset can only be on one list
822 * at a time. Bad things happen otherwise.
825 extern void ksl_link(keyset
**/
*ksroot*/
, keyset */
*ks*/
);
827 /* --- @ksl_prune@ --- *
829 * Arguments: @keyset **ksroot@ = pointer to keyset list head
833 * Use: Prunes the keyset list by removing keys which mustn't be used
837 extern void ksl_prune(keyset
**/
*ksroot*/
);
839 /* --- @ksl_encrypt@ --- *
841 * Arguments: @keyset **ksroot@ = pointer to keyset list head
842 * @unsigned ty@ = message type
843 * @buf *b@ = pointer to input buffer
844 * @buf *bb@ = pointer to output buffer
846 * Returns: Zero if successful; @KSERR_REGEN@ if it's time to negotiate a
847 * new key; @KSERR_NOKEYS@ if there are no suitable keys
848 * available. Also returns zero if there was insufficient
849 * buffer space (but the output buffer is broken in this case).
851 * Use: Encrypts a packet.
854 extern int ksl_encrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
855 buf */
*b*/
, buf */
*bb*/
);
857 /* --- @ksl_decrypt@ --- *
859 * Arguments: @keyset **ksroot@ = pointer to keyset list head
860 * @unsigned ty@ = expected type code
861 * @buf *b@ = pointer to input buffer
862 * @buf *bb@ = pointer to output buffer
864 * Returns: Zero on success; @KSERR_DECRYPT@ on failure. Also returns
865 * zero if there was insufficient buffer (but the output buffer
866 * is broken in this case).
868 * Use: Decrypts a packet.
871 extern int ksl_decrypt(keyset
**/
*ksroot*/
, unsigned /*ty*/,
872 buf */
*b*/
, buf */
*bb*/
);
874 /*----- Challenges --------------------------------------------------------*/
878 * Arguments: @buf *b@ = where to put the challenge
880 * Returns: Zero if OK, nonzero on error.
882 * Use: Issues a new challenge.
885 extern int c_new(buf */
*b*/
);
887 /* --- @c_check@ --- *
889 * Arguments: @buf *b@ = where to find the challenge
891 * Returns: Zero if OK, nonzero if it didn't work.
893 * Use: Checks a challenge. On failure, the buffer is broken.
896 extern int c_check(buf */
*b*/
);
898 /*----- Administration interface ------------------------------------------*/
900 #define A_END ((char *)0)
902 /* --- @a_vformat@ --- *
904 * Arguments: @dstr *d@ = where to leave the formatted message
905 * @const char *fmt@ = pointer to format string
906 * @va_list ap@ = arguments in list
910 * Use: Main message token formatting driver. The arguments are
911 * interleaved formatting tokens and their parameters, finally
912 * terminated by an entry @A_END@.
916 * * "*..." ... -- pretokenized @dstr_putf@-like string
918 * * "?ADDR" SOCKADDR -- a socket address, to be converted
920 * * "?B64" BUFFER SIZE -- binary data to be base64-encoded
922 * * "?TOKENS" VECTOR -- null-terminated vector of tokens
924 * * "?PEER" PEER -- peer's name
926 * * "?ERRNO" ERRNO -- system error code
928 * * "[!]..." ... -- @dstr_putf@-like string as single token
931 extern void a_vformat(dstr */
*d*/
, const char */
*fmt*/
, va_list /*ap*/);
933 /* --- @a_format@ --- *
935 * Arguments: @dstr *d@ = where to leave the formatted message
936 * @const char *fmt@ = pointer to format string
940 * Use: Writes a tokenized message into a string, for later
944 extern void a_format(dstr */
*d*/
, const char */
*fmt*/
, ...);
946 /* --- @a_warn@ --- *
948 * Arguments: @const char *fmt@ = pointer to format string
949 * @...@ = other arguments
953 * Use: Informs all admin connections of a warning.
956 extern void a_warn(const char */
*fmt*/
, ...);
958 /* --- @a_notify@ --- *
960 * Arguments: @const char *fmt@ = pointer to format string
961 * @...@ = other arguments
965 * Use: Sends a notification to interested admin connections.
968 extern void a_notify(const char */
*fmt*/
, ...);
970 /* --- @a_create@ --- *
972 * Arguments: @int fd_in, fd_out@ = file descriptors to use
973 * @unsigned f@ = initial flags to set
977 * Use: Creates a new admin connection.
980 extern void a_create(int /*fd_in*/, int /*fd_out*/, unsigned /*f*/);
982 /* --- @a_quit@ --- *
988 * Use: Shuts things down nicely.
991 extern void a_quit(void);
993 /* --- @a_preselect@ --- *
999 * Use: Informs the admin module that we're about to select again,
1000 * and that it should do cleanup things it has delayed until a
1004 extern void a_preselect(void);
1006 /* --- @a_daemon@ --- *
1012 * Use: Informs the admin module that it's a daemon.
1015 extern void a_daemon(void);
1017 /* --- @a_init@ --- *
1019 * Arguments: @const char *sock@ = socket name to create
1020 * @uid_t u@ = user to own the socket
1021 * @gid_t g@ = group to own the socket
1022 * @mode_t m@ = permissions to set on the socket
1026 * Use: Creates the admin listening socket.
1029 extern void a_init(const char */
*sock*/
,
1030 uid_t
/*u*/, gid_t
/*g*/, mode_t
/*m*/);
1032 /*----- Mapping with addresses as keys ------------------------------------*/
1034 /* --- @am_create@ --- *
1036 * Arguments: @addrmap *m@ = pointer to map
1040 * Use: Create an address map, properly set up.
1043 extern void am_create(addrmap */
*m*/
);
1045 /* --- @am_destroy@ --- *
1047 * Arguments: @addrmap *m@ = pointer to map
1051 * Use: Destroy an address map, throwing away all the entries.
1054 extern void am_destroy(addrmap */
*m*/
);
1056 /* --- @am_find@ --- *
1058 * Arguments: @addrmap *m@ = pointer to map
1059 * @const addr *a@ = address to look up
1060 * @size_t sz@ = size of block to allocate
1061 * @unsigned *f@ = where to store flags
1063 * Returns: Pointer to found item, or null.
1065 * Use: Finds a record with the given IP address, set @*f@ nonzero
1066 * and returns it. If @sz@ is zero, and no match was found,
1067 * return null; otherwise allocate a new block of @sz@ bytes,
1068 * clear @*f@ to zero and return the block pointer.
1071 extern void *am_find(addrmap */
*m*/
, const addr */
*a*/
,
1072 size_t /*sz*/, unsigned */
*f*/
);
1074 /* --- @am_remove@ --- *
1076 * Arguments: @addrmap *m@ = pointer to map
1077 * @void *i@ = pointer to the item
1081 * Use: Removes an item from the map.
1084 extern void am_remove(addrmap */
*m*/
, void */
*i*/
);
1086 /*----- Privilege separation ----------------------------------------------*/
1088 /* --- @ps_trace@ --- *
1090 * Arguments: @unsigned mask@ = trace mask to check
1091 * @const char *fmt@ = message format
1092 * @...@ = values for placeholders
1096 * Use: Writes a trace message.
1099 T( extern void ps_trace(unsigned /*mask*/, const char */
*fmt*/
, ...); )
1101 /* --- @ps_warn@ --- *
1103 * Arguments: @const char *fmt@ = message format
1104 * @...@ = values for placeholders
1108 * Use: Writes a warning message.
1111 extern void ps_warn(const char */
*fmt*/
, ...);
1113 /* --- @ps_tunfd@ --- *
1115 * Arguments: @const tunnel_ops *tops@ = pointer to tunnel operations
1116 * @char **ifn@ = where to put the interface name
1118 * Returns: The file descriptor, or @-1@ on error.
1120 * Use: Fetches a file descriptor for a tunnel driver.
1123 extern int ps_tunfd(const tunnel_ops */
*tops*/
, char **/
*ifn*/
);
1125 /* --- @ps_split@ --- *
1127 * Arguments: @int detachp@ = whether to detach the child from its terminal
1131 * Use: Separates off the privileged tunnel-opening service from the
1132 * rest of the server.
1135 extern void ps_split(int /*detachp*/);
1137 /* --- @ps_quit@ --- *
1143 * Use: Detaches from the helper process.
1146 extern void ps_quit(void);
1148 /*----- Peer management ---------------------------------------------------*/
1150 /* --- @p_txstart@ --- *
1152 * Arguments: @peer *p@ = pointer to peer block
1153 * @unsigned msg@ = message type code
1155 * Returns: A pointer to a buffer to write to.
1157 * Use: Starts sending to a peer. Only one send can happen at a
1161 extern buf
*p_txstart(peer */
*p*/
, unsigned /*msg*/);
1163 /* --- @p_txend@ --- *
1165 * Arguments: @peer *p@ = pointer to peer block
1169 * Use: Sends a packet to the peer.
1172 extern void p_txend(peer */
*p*/
);
1174 /* --- @p_pingsend@ --- *
1176 * Arguments: @peer *p@ = destination peer
1177 * @ping *pg@ = structure to fill in
1178 * @unsigned type@ = message type
1179 * @unsigned long timeout@ = how long to wait before giving up
1180 * @void (*func)(int, void *)@ = callback function
1181 * @void *arg@ = argument for callback
1183 * Returns: Zero if successful, nonzero if it failed.
1185 * Use: Sends a ping to a peer. Call @func@ with a nonzero argument
1186 * if we get an answer within the timeout, or zero if no answer.
1189 extern int p_pingsend(peer */
*p*/
, ping */
*pg*/
, unsigned /*type*/,
1190 unsigned long /*timeout*/,
1191 void (*/
*func*/
)(int, void *), void */
*arg*/
);
1193 /* --- @p_pingdone@ --- *
1195 * Arguments: @ping *p@ = ping structure
1196 * @int rc@ = return code to pass on
1200 * Use: Disposes of a ping structure, maybe sending a notification.
1203 extern void p_pingdone(ping */
*p*/
, int /*rc*/);
1205 /* --- @p_greet@ --- *
1207 * Arguments: @peer *p@ = peer to send to
1208 * @const void *c@ = pointer to challenge
1209 * @size_t sz@ = size of challenge
1213 * Use: Sends a greeting packet.
1216 extern void p_greet(peer */
*p*/
, const void */
*c*/
, size_t /*sz*/);
1218 /* --- @p_tun@ --- *
1220 * Arguments: @peer *p@ = pointer to peer block
1221 * @buf *b@ = buffer containing incoming packet
1225 * Use: Handles a packet which needs to be sent to a peer.
1228 extern void p_tun(peer */
*p*/
, buf */
*b*/
);
1230 /* --- @p_keyreload@ --- *
1236 * Use: Forces a check of the daemon's keyring files.
1239 extern void p_keyreload(void);
1241 /* --- @p_interval@ --- *
1247 * Use: Called periodically to do tidying.
1250 extern void p_interval(void);
1252 /* --- @p_stats@ --- *
1254 * Arguments: @peer *p@ = pointer to a peer block
1256 * Returns: A pointer to the peer's statistics.
1259 extern stats
*p_stats(peer */
*p*/
);
1261 /* --- @p_ifname@ --- *
1263 * Arguments: @peer *p@ = pointer to a peer block
1265 * Returns: A pointer to the peer's interface name.
1268 extern const char *p_ifname(peer */
*p*/
);
1270 /* --- @p_setifname@ --- *
1272 * Arguments: @peer *p@ = pointer to a peer block
1273 * @const char *name@ = pointer to the new name
1277 * Use: Changes the name held for a peer's interface.
1280 extern void p_setifname(peer */
*p*/
, const char */
*name*/
);
1282 /* --- @p_addr@ --- *
1284 * Arguments: @peer *p@ = pointer to a peer block
1286 * Returns: A pointer to the peer's address.
1289 extern const addr
*p_addr(peer */
*p*/
);
1291 /* --- @p_init@ --- *
1293 * Arguments: @struct in_addr addr@ = address to bind to
1294 * @unsigned port@ = port number to listen to
1298 * Use: Initializes the peer system; creates the socket.
1301 extern void p_init(struct in_addr
/*addr*/, unsigned /*port*/);
1303 /* --- @p_port@ --- *
1307 * Returns: Port number used for socket.
1310 unsigned p_port(void);
1312 /* --- @p_create@ --- *
1314 * Arguments: @peerspec *spec@ = information about this peer
1316 * Returns: Pointer to the peer block, or null if it failed.
1318 * Use: Creates a new named peer block. No peer is actually attached
1322 extern peer
*p_create(peerspec */
*spec*/
);
1324 /* --- @p_name@ --- *
1326 * Arguments: @peer *p@ = pointer to a peer block
1328 * Returns: A pointer to the peer's name.
1330 * Use: Equivalent to @p_spec(p)->name@.
1333 extern const char *p_name(peer */
*p*/
);
1335 /* --- @p_tag@ --- *
1337 * Arguments: @peer *p@ = pointer to a peer block
1339 * Returns: A pointer to the peer's public key tag.
1342 extern const char *p_tag(peer */
*p*/
);
1344 /* --- @p_privtag@ --- *
1346 * Arguments: @peer *p@ = pointer to a peer block
1348 * Returns: A pointer to the peer's private key tag.
1351 extern const char *p_privtag(peer */
*p*/
);
1353 /* --- @p_spec@ --- *
1355 * Arguments: @peer *p@ = pointer to a peer block
1357 * Returns: Pointer to the peer's specification
1360 extern const peerspec
*p_spec(peer */
*p*/
);
1362 /* --- @p_findbyaddr@ --- *
1364 * Arguments: @const addr *a@ = address to look up
1366 * Returns: Pointer to the peer block, or null if not found.
1368 * Use: Finds a peer by address.
1371 extern peer
*p_findbyaddr(const addr */
*a*/
);
1373 /* --- @p_find@ --- *
1375 * Arguments: @const char *name@ = name to look up
1377 * Returns: Pointer to the peer block, or null if not found.
1379 * Use: Finds a peer by name.
1382 extern peer
*p_find(const char */
*name*/
);
1384 /* --- @p_destroy@ --- *
1386 * Arguments: @peer *p@ = pointer to a peer
1390 * Use: Destroys a peer.
1393 extern void p_destroy(peer */
*p*/
);
1395 /* --- @FOREACH_PEER@ --- *
1397 * Arguments: @p@ = name to bind to each peer
1398 * @stuff@ = thing to do for each item
1400 * Use: Does something for each current peer.
1403 #define FOREACH_PEER(p, stuff) do { \
1406 for (p_mkiter(&i_); (p = p_next(&i_)) != 0; ) stuff \
1409 /* --- @p_mkiter@ --- *
1411 * Arguments: @peer_iter *i@ = pointer to an iterator
1415 * Use: Initializes the iterator.
1418 extern void p_mkiter(peer_iter */
*i*/
);
1420 /* --- @p_next@ --- *
1422 * Arguments: @peer_iter *i@ = pointer to an iterator
1424 * Returns: Next peer, or null if at the end.
1426 * Use: Returns the next peer.
1429 extern peer
*p_next(peer_iter */
*i*/
);
1431 /*----- Tunnel drivers ----------------------------------------------------*/
1434 extern const tunnel_ops tun_linux
;
1438 extern const tunnel_ops tun_unet
;
1442 extern const tunnel_ops tun_bsd
;
1445 extern const tunnel_ops tun_slip
;
1447 /*----- Other handy utilities ---------------------------------------------*/
1449 /* --- @mpstr@ --- *
1451 * Arguments: @mp *m@ = a multiprecision integer
1453 * Returns: A pointer to the integer's textual representation.
1455 * Use: Converts a multiprecision integer to a string. Corrupts
1459 extern const char *mpstr(mp */
*m*/
);
1461 /* --- @gestr@ --- *
1463 * Arguments: @group *g@ = a group
1464 * @ge *x@ = a group element
1466 * Returns: A pointer to the element's textual representation.
1468 * Use: Converts a group element to a string. Corrupts
1472 extern const char *gestr(group */
*g*/
, ge */
*x*/
);
1474 /* --- @timestr@ --- *
1476 * Arguments: @time_t t@ = a time to convert
1478 * Returns: A pointer to a textual representation of the time.
1480 * Use: Converts a time to a textual representation. Corrupts
1484 extern const char *timestr(time_t /*t*/);
1486 /* --- @mystrieq@ --- *
1488 * Arguments: @const char *x, *y@ = two strings
1490 * Returns: True if @x@ and @y are equal, up to case.
1493 extern int mystrieq(const char */
*x*/
, const char */
*y*/
);
1495 /* --- @seq_reset@ --- *
1497 * Arguments: @seqwin *s@ = sequence-checking window
1501 * Use: Resets a sequence number window.
1504 extern void seq_reset(seqwin */
*s*/
);
1506 /* --- @seq_check@ --- *
1508 * Arguments: @seqwin *s@ = sequence-checking window
1509 * @uint32 q@ = sequence number to check
1510 * @const char *service@ = service to report message from
1512 * Returns: A @SEQ_@ code.
1514 * Use: Checks a sequence number against the window, updating things
1518 extern int seq_check(seqwin */
*s*/
, uint32
/*q*/, const char */
*service*/
);
1520 /*----- That's all, folks -------------------------------------------------*/