| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: tripe.h,v 1.18 2004/04/03 12:35:13 mdw Exp $ |
| 4 | * |
| 5 | * Main header file for TrIPE |
| 6 | * |
| 7 | * (c) 2001 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Trivial IP Encryption (TrIPE). |
| 13 | * |
| 14 | * TrIPE is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * TrIPE is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with TrIPE; if not, write to the Free Software Foundation, |
| 26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 27 | */ |
| 28 | |
| 29 | /*----- Revision history --------------------------------------------------* |
| 30 | * |
| 31 | * $Log: tripe.h,v $ |
| 32 | * Revision 1.18 2004/04/03 12:35:13 mdw |
| 33 | * Support elliptic curve key exchange. |
| 34 | * |
| 35 | * Revision 1.17 2003/10/15 09:30:18 mdw |
| 36 | * Add support for Ethereal protocol analysis. |
| 37 | * |
| 38 | * Revision 1.16 2003/07/13 11:19:49 mdw |
| 39 | * Incompatible protocol fix! Include message type code under MAC tag to |
| 40 | * prevent cut-and-paste from key-exchange messages to general packet |
| 41 | * transport. |
| 42 | * |
| 43 | * Revision 1.15 2003/05/16 12:09:03 mdw |
| 44 | * Allow binding to a chosen address. |
| 45 | * |
| 46 | * Revision 1.14 2003/04/06 10:36:33 mdw |
| 47 | * Rearrange so as not to include Linux headers unless we need to. |
| 48 | * |
| 49 | * Revision 1.13 2003/04/06 10:26:35 mdw |
| 50 | * Report peer name on decrypt errors. |
| 51 | * |
| 52 | * Revision 1.12 2003/04/06 10:25:17 mdw |
| 53 | * Support Linux TUN/TAP device. Fix some bugs. |
| 54 | * |
| 55 | * Revision 1.11 2002/01/13 14:57:42 mdw |
| 56 | * Fix crap typo. |
| 57 | * |
| 58 | * Revision 1.10 2002/01/13 14:54:58 mdw |
| 59 | * Provide MGF macros. |
| 60 | * |
| 61 | * Revision 1.9 2001/06/22 19:40:36 mdw |
| 62 | * Support expiry of other peers' public keys. |
| 63 | * |
| 64 | * Revision 1.8 2001/06/19 22:10:57 mdw |
| 65 | * Some more constants for the algorithms. Document the packet format |
| 66 | * change for non-malleability. Moved @buf@ definitions to separate header |
| 67 | * file. |
| 68 | * |
| 69 | * Revision 1.7 2001/03/03 12:07:08 mdw |
| 70 | * Rename word get and put functions now that there's 16-bit support. |
| 71 | * |
| 72 | * Revision 1.6 2001/02/19 19:11:09 mdw |
| 73 | * Output buffering on admin connections. |
| 74 | * |
| 75 | * Revision 1.5 2001/02/16 21:41:43 mdw |
| 76 | * Major changes. See source files for details. |
| 77 | * |
| 78 | * Revision 1.4 2001/02/05 19:56:37 mdw |
| 79 | * Sequence number protection, and BSD tunnels. |
| 80 | * |
| 81 | * Revision 1.3 2001/02/04 01:17:55 mdw |
| 82 | * Create a configuration header file to tidy up command lines. |
| 83 | * |
| 84 | * Revision 1.2 2001/02/03 22:40:29 mdw |
| 85 | * Put timer information into the entropy pool when packets are received |
| 86 | * and on similar events. Reseed the generator on the interval timer. |
| 87 | * |
| 88 | * Revision 1.1 2001/02/03 20:26:37 mdw |
| 89 | * Initial checkin. |
| 90 | * |
| 91 | */ |
| 92 | |
| 93 | #ifndef TRIPE_H |
| 94 | #define TRIPE_H |
| 95 | |
| 96 | #ifdef __cplusplus |
| 97 | extern "C" { |
| 98 | #endif |
| 99 | |
| 100 | /*----- Header files ------------------------------------------------------*/ |
| 101 | |
| 102 | #include "config.h" |
| 103 | |
| 104 | #include <assert.h> |
| 105 | #include <ctype.h> |
| 106 | #include <errno.h> |
| 107 | #include <signal.h> |
| 108 | #include <stdarg.h> |
| 109 | #include <stddef.h> |
| 110 | #include <stdio.h> |
| 111 | #include <stdlib.h> |
| 112 | #include <string.h> |
| 113 | #include <time.h> |
| 114 | |
| 115 | #include <sys/types.h> |
| 116 | #include <sys/time.h> |
| 117 | #include <unistd.h> |
| 118 | #include <fcntl.h> |
| 119 | #include <sys/stat.h> |
| 120 | |
| 121 | #include <sys/socket.h> |
| 122 | #include <sys/un.h> |
| 123 | #include <netinet/in.h> |
| 124 | #include <arpa/inet.h> |
| 125 | #include <netdb.h> |
| 126 | |
| 127 | #include <pwd.h> |
| 128 | #include <grp.h> |
| 129 | |
| 130 | #include <mLib/alloc.h> |
| 131 | #include <mLib/arena.h> |
| 132 | #include <mLib/bres.h> |
| 133 | #include <mLib/dstr.h> |
| 134 | #include <mLib/env.h> |
| 135 | #include <mLib/fdflags.h> |
| 136 | #include <mLib/fwatch.h> |
| 137 | #include <mLib/mdwopt.h> |
| 138 | #include <mLib/quis.h> |
| 139 | #include <mLib/report.h> |
| 140 | #include <mLib/sel.h> |
| 141 | #include <mLib/selbuf.h> |
| 142 | #include <mLib/sig.h> |
| 143 | #include <mLib/str.h> |
| 144 | #include <mLib/sub.h> |
| 145 | #include <mLib/trace.h> |
| 146 | |
| 147 | #include <catacomb/buf.h> |
| 148 | |
| 149 | #include <catacomb/gcipher.h> |
| 150 | #include <catacomb/gmac.h> |
| 151 | #include <catacomb/grand.h> |
| 152 | #include <catacomb/key.h> |
| 153 | #include <catacomb/paranoia.h> |
| 154 | |
| 155 | #include <catacomb/noise.h> |
| 156 | #include <catacomb/rand.h> |
| 157 | |
| 158 | #include <catacomb/mp.h> |
| 159 | #include <catacomb/mprand.h> |
| 160 | #include <catacomb/dh.h> |
| 161 | #include <catacomb/ec.h> |
| 162 | #include <catacomb/ec-keys.h> |
| 163 | #include <catacomb/group.h> |
| 164 | |
| 165 | #include "tripe-protocol.h" |
| 166 | #include "util.h" |
| 167 | |
| 168 | #undef sun |
| 169 | |
| 170 | /*----- Magic numbers -----------------------------------------------------*/ |
| 171 | |
| 172 | /* --- Tunnel types --- */ |
| 173 | |
| 174 | #define TUN_NOTDEF 0 |
| 175 | #define TUN_UNET 1 |
| 176 | #define TUN_BSD 2 |
| 177 | #define TUN_LINUX 3 |
| 178 | |
| 179 | /* --- Trace flags --- */ |
| 180 | |
| 181 | #define T_TUNNEL 1u |
| 182 | #define T_PEER 2u |
| 183 | #define T_PACKET 4u |
| 184 | #define T_ADMIN 8u |
| 185 | #define T_CRYPTO 16u |
| 186 | #define T_KEYSET 32u |
| 187 | #define T_KEYEXCH 64u |
| 188 | #define T_KEYMGMT 128u |
| 189 | |
| 190 | #define T_ALL 255u |
| 191 | |
| 192 | /* --- Units --- */ |
| 193 | |
| 194 | #define SEC(n) (n##u) |
| 195 | #define MIN(n) (n##u * 60u) |
| 196 | #define MEG(n) (n##ul * 1024ul * 1024ul) |
| 197 | |
| 198 | /* --- Other things --- */ |
| 199 | |
| 200 | #define PKBUFSZ 65536 |
| 201 | |
| 202 | /*----- Cipher selections -------------------------------------------------*/ |
| 203 | |
| 204 | #include <catacomb/blowfish.h> |
| 205 | #include <catacomb/blowfish-cbc.h> |
| 206 | #include <catacomb/blowfish-counter.h> |
| 207 | #include <catacomb/rmd160.h> |
| 208 | #include <catacomb/rmd160-hmac.h> |
| 209 | |
| 210 | #define CIPHER (&blowfish_cbc) |
| 211 | #define MAC (&rmd160_hmac) |
| 212 | |
| 213 | #define HASH_CTX rmd160_ctx |
| 214 | #define HASH_INIT rmd160_init |
| 215 | #define HASH rmd160_hash |
| 216 | #define HASH_STRING(c, s) HASH((c), s, sizeof(s)) |
| 217 | #define HASH_DONE rmd160_done |
| 218 | #define HASHSZ RMD160_HASHSZ |
| 219 | |
| 220 | #define MGF_CTX blowfish_counterctx |
| 221 | #define MGF_INIT blowfish_counterinit |
| 222 | #define MGF_CRYPT blowfish_counterencrypt |
| 223 | |
| 224 | #define SEQSZ 4 |
| 225 | #define IVSZ BLOWFISH_BLKSZ |
| 226 | #define MACSZ 10 |
| 227 | |
| 228 | /*----- Data structures ---------------------------------------------------*/ |
| 229 | |
| 230 | /* --- Socket addresses --- * |
| 231 | * |
| 232 | * A magic union of supported socket addresses. |
| 233 | */ |
| 234 | |
| 235 | typedef union addr { |
| 236 | struct sockaddr sa; |
| 237 | struct sockaddr_in sin; |
| 238 | } addr; |
| 239 | |
| 240 | /* --- A symmetric keyset --- * |
| 241 | * |
| 242 | * A keyset contains a set of symmetric keys for encrypting and decrypting |
| 243 | * packets. Keysets are stored in a list, sorted in reverse order of |
| 244 | * creation, so that the most recent keyset (the one most likely to be used) |
| 245 | * is first. |
| 246 | * |
| 247 | * Each keyset has a time limit and a data limit. The keyset is destroyed |
| 248 | * when either it has existed for too long, or it has been used to encrypt |
| 249 | * too much data. New key exchanges are triggered when keys are close to |
| 250 | * expiry. |
| 251 | */ |
| 252 | |
| 253 | typedef struct keyset { |
| 254 | struct keyset *next; /* Next active keyset in the list */ |
| 255 | unsigned ref; /* Reference count for keyset */ |
| 256 | struct peer *p; /* Pointer to peer structure */ |
| 257 | time_t t_exp; /* Expiry time for this keyset */ |
| 258 | unsigned long sz_exp; /* Data limit for the keyset */ |
| 259 | T( unsigned seq; ) /* Sequence number for tracing */ |
| 260 | unsigned f; /* Various useful flags */ |
| 261 | gcipher *cin, *cout; /* Keyset ciphers for encryption */ |
| 262 | gmac *min, *mout; /* Keyset MACs for integrity */ |
| 263 | uint32 oseq; /* Outbound sequence number */ |
| 264 | uint32 iseq, iwin; /* Inbound sequence number */ |
| 265 | } keyset; |
| 266 | |
| 267 | #define KS_SEQWINSZ 32 /* Bits in sequence number window */ |
| 268 | |
| 269 | #define KSF_LISTEN 1u /* Don't encrypt packets yet */ |
| 270 | #define KSF_LINK 2u /* Key is in a linked list */ |
| 271 | |
| 272 | /* --- Key exchange --- * |
| 273 | * |
| 274 | * TrIPE uses the Wrestlers Protocol for its key exchange. The Wrestlers |
| 275 | * Protocol has a number of desirable features (e.g., perfect forward |
| 276 | * secrecy, and zero-knowledge authentication) which make it attractive for |
| 277 | * use in TrIPE. The Wrestlers Protocol was designed by Mark Wooding and |
| 278 | * Clive Jones. |
| 279 | */ |
| 280 | |
| 281 | #define KX_NCHAL 16u |
| 282 | #define KX_THRESH 4u |
| 283 | |
| 284 | typedef struct kxchal { |
| 285 | struct keyexch *kx; /* Pointer back to key exchange */ |
| 286 | ge *c; /* Responder's challenge */ |
| 287 | ge *r; /* My reply to the challenge */ |
| 288 | keyset *ks; /* Pointer to temporary keyset */ |
| 289 | unsigned f; /* Various useful flags */ |
| 290 | sel_timer t; /* Response timer for challenge */ |
| 291 | octet hc[HASHSZ]; /* Hash of his challenge */ |
| 292 | mp *ck; /* The check value */ |
| 293 | octet hswrq_in[HASHSZ]; /* Inbound switch request message */ |
| 294 | octet hswok_in[HASHSZ]; /* Inbound switch confirmation */ |
| 295 | octet hswrq_out[HASHSZ]; /* Outbound switch request message */ |
| 296 | octet hswok_out[HASHSZ]; /* Outbound switch confirmation */ |
| 297 | } kxchal; |
| 298 | |
| 299 | typedef struct keyexch { |
| 300 | struct peer *p; /* Pointer back to the peer */ |
| 301 | keyset **ks; /* Peer's list of keysets */ |
| 302 | unsigned f; /* Various useful flags */ |
| 303 | unsigned s; /* Current state in exchange */ |
| 304 | sel_timer t; /* Timer for next exchange */ |
| 305 | ge *kpub; /* Peer's public key */ |
| 306 | time_t texp_kpub; /* Expiry time for public key */ |
| 307 | mp *alpha; /* My temporary secret */ |
| 308 | ge *c; /* My challenge */ |
| 309 | ge *rx; /* The expected response */ |
| 310 | unsigned nr; /* Number of extant responses */ |
| 311 | time_t t_valid; /* When this exchange goes bad */ |
| 312 | octet hc[HASHSZ]; /* Hash of my challenge */ |
| 313 | kxchal *r[KX_NCHAL]; /* Array of challenges */ |
| 314 | } keyexch; |
| 315 | |
| 316 | #define KXF_TIMER 1u /* Waiting for a timer to go off */ |
| 317 | #define KXF_DEAD 2u /* The key-exchanger isn't up */ |
| 318 | #define KXF_PUBKEY 4u /* Key exchanger has a public key */ |
| 319 | |
| 320 | enum { |
| 321 | KXS_DEAD, /* Uninitialized state (magical) */ |
| 322 | KXS_CHAL, /* Main answer-challenges state */ |
| 323 | KXS_COMMIT, /* Committed: send switch request */ |
| 324 | KXS_SWITCH /* Switched: send confirmation */ |
| 325 | }; |
| 326 | |
| 327 | /* --- Tunnel structure --- * |
| 328 | * |
| 329 | * Used to maintain system-specific information about the tunnel interface. |
| 330 | */ |
| 331 | |
| 332 | #if TUN_TYPE == TUN_LINUX |
| 333 | # include <linux/if.h> |
| 334 | # include <linux/if_tun.h> |
| 335 | #endif |
| 336 | |
| 337 | typedef struct tunnel { |
| 338 | #if TUN_TYPE == TUN_UNET |
| 339 | sel_file f; /* Selector for Usernet device */ |
| 340 | struct peer *p; /* Pointer to my peer */ |
| 341 | #elif TUN_TYPE == TUN_LINUX |
| 342 | sel_file f; /* Selector for TUN/TAP device */ |
| 343 | struct peer *p; /* Pointer to my peer */ |
| 344 | char ifn[IFNAMSIZ]; /* Interface name buffer */ |
| 345 | #elif TUN_TYPE == TUN_BSD |
| 346 | sel_file f; /* Selector for tunnel device */ |
| 347 | struct peer *p; /* Pointer to my peer */ |
| 348 | unsigned n; /* Number of my tunnel device */ |
| 349 | #else |
| 350 | # error "No support for this tunnel type" |
| 351 | #endif |
| 352 | } tunnel; |
| 353 | |
| 354 | /* --- Peer statistics --- * |
| 355 | * |
| 356 | * Contains various interesting and not-so-interesting statistics about a |
| 357 | * peer. This is updated by various parts of the code. The format of the |
| 358 | * structure isn't considered private, and @p_stats@ returns a pointer to the |
| 359 | * statistics block for a given peer. |
| 360 | */ |
| 361 | |
| 362 | typedef struct stats { |
| 363 | unsigned long sz_in, sz_out; /* Size of all data in and out */ |
| 364 | unsigned long sz_kxin, sz_kxout; /* Size of key exchange messages */ |
| 365 | unsigned long sz_ipin, sz_ipout; /* Size of encapsulated IP packets */ |
| 366 | time_t t_start, t_last; /* Time peer created, last recv */ |
| 367 | unsigned long n_reject; /* Number of rejected packets */ |
| 368 | unsigned long n_in, n_out; /* Number of packets in and out */ |
| 369 | unsigned long n_kxin, n_kxout; /* Number of key exchange packets */ |
| 370 | unsigned long n_ipin, n_ipout; /* Number of encrypted packets */ |
| 371 | } stats; |
| 372 | |
| 373 | /* --- Peer structure --- * |
| 374 | * |
| 375 | * The main structure which glues everything else together. |
| 376 | */ |
| 377 | |
| 378 | typedef struct peer { |
| 379 | struct peer *next, *prev; /* Links to next and previous */ |
| 380 | char *name; /* Name of this peer */ |
| 381 | tunnel t; /* Tunnel for local packets */ |
| 382 | keyset *ks; /* List head for keysets */ |
| 383 | buf b; /* Buffer for sending packets */ |
| 384 | addr peer; /* Peer socket address */ |
| 385 | size_t sasz; /* Socket address size */ |
| 386 | stats st; /* Statistics */ |
| 387 | keyexch kx; /* Key exchange protocol block */ |
| 388 | } peer; |
| 389 | |
| 390 | /* --- Admin structure --- */ |
| 391 | |
| 392 | #define OBUFSZ 16384u |
| 393 | |
| 394 | typedef struct obuf { |
| 395 | struct obuf *next; /* Next buffer in list */ |
| 396 | char *p_in, *p_out; /* Pointers into the buffer */ |
| 397 | char buf[OBUFSZ]; /* The actual buffer */ |
| 398 | } obuf; |
| 399 | |
| 400 | typedef struct admin { |
| 401 | struct admin *next, *prev; /* Links to next and previous */ |
| 402 | unsigned f; /* Various useful flags */ |
| 403 | #ifndef NTRACE |
| 404 | unsigned seq; /* Sequence number for tracing */ |
| 405 | #endif |
| 406 | char *pname; /* Peer name to create */ |
| 407 | char *paddr; /* Address string to resolve */ |
| 408 | obuf *o_head, *o_tail; /* Output buffer list */ |
| 409 | selbuf b; /* Line buffer for commands */ |
| 410 | sel_file w; /* Selector for write buffering */ |
| 411 | bres_client r; /* Background resolver task */ |
| 412 | sel_timer t; /* Timer for resolver */ |
| 413 | addr peer; /* Address to set */ |
| 414 | size_t sasz; /* Size of the address */ |
| 415 | } admin; |
| 416 | |
| 417 | #define AF_DEAD 1u /* Destroy this admin block */ |
| 418 | #define AF_LOCK 2u /* Don't destroy it yet */ |
| 419 | |
| 420 | /*----- Global variables --------------------------------------------------*/ |
| 421 | |
| 422 | extern sel_state sel; /* Global I/O event state */ |
| 423 | extern group *gg; /* The group we work in */ |
| 424 | extern mp *kpriv; /* Our private key */ |
| 425 | extern octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ]; |
| 426 | |
| 427 | #ifndef NTRACE |
| 428 | extern const trace_opt tr_opts[]; /* Trace options array */ |
| 429 | extern unsigned tr_flags; /* Trace options flags */ |
| 430 | #endif |
| 431 | |
| 432 | /*----- Other macros ------------------------------------------------------*/ |
| 433 | |
| 434 | #define TIMER noise_timer(RAND_GLOBAL) |
| 435 | |
| 436 | /*----- Key management ----------------------------------------------------*/ |
| 437 | |
| 438 | /* --- @km_interval@ --- * |
| 439 | * |
| 440 | * Arguments: --- |
| 441 | * |
| 442 | * Returns: Zero if OK, nonzero to force reloading of keys. |
| 443 | * |
| 444 | * Use: Called on the interval timer to perform various useful jobs. |
| 445 | */ |
| 446 | |
| 447 | extern int km_interval(void); |
| 448 | |
| 449 | /* --- @km_init@ --- * |
| 450 | * |
| 451 | * Arguments: @const char *kr_priv@ = private keyring file |
| 452 | * @const char *kr_pub@ = public keyring file |
| 453 | * @const char *tag@ = tag to load |
| 454 | * |
| 455 | * Returns: --- |
| 456 | * |
| 457 | * Use: Initializes, and loads the private key. |
| 458 | */ |
| 459 | |
| 460 | extern void km_init(const char */*kr_priv*/, const char */*kr_pub*/, |
| 461 | const char */*tag*/); |
| 462 | |
| 463 | /* --- @km_getpubkey@ --- * |
| 464 | * |
| 465 | * Arguments: @const char *tag@ = public key tag to load |
| 466 | * @ge *kpub@ = where to put the public key |
| 467 | * @time_t *t_exp@ = where to put the expiry time |
| 468 | * |
| 469 | * Returns: Zero if OK, nonzero if it failed. |
| 470 | * |
| 471 | * Use: Fetches a public key from the keyring. |
| 472 | */ |
| 473 | |
| 474 | extern int km_getpubkey(const char */*tag*/, ge */*kpub*/, |
| 475 | time_t */*t_exp*/); |
| 476 | |
| 477 | /*----- Key exchange ------------------------------------------------------*/ |
| 478 | |
| 479 | /* --- @kx_start@ --- * |
| 480 | * |
| 481 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 482 | * |
| 483 | * Returns: --- |
| 484 | * |
| 485 | * Use: Stimulates a key exchange. If a key exchage is in progress, |
| 486 | * a new challenge is sent (unless the quiet timer forbids |
| 487 | * this); if no exchange is in progress, one is commenced. |
| 488 | */ |
| 489 | |
| 490 | extern void kx_start(keyexch */*kx*/); |
| 491 | |
| 492 | /* --- @kx_message@ --- * |
| 493 | * |
| 494 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 495 | * @unsigned msg@ = the message code |
| 496 | * @buf *b@ = pointer to buffer containing the packet |
| 497 | * |
| 498 | * Returns: --- |
| 499 | * |
| 500 | * Use: Reads a packet containing key exchange messages and handles |
| 501 | * it. |
| 502 | */ |
| 503 | |
| 504 | extern void kx_message(keyexch */*kx*/, unsigned /*msg*/, buf */*b*/); |
| 505 | |
| 506 | /* --- @kx_free@ --- * |
| 507 | * |
| 508 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 509 | * |
| 510 | * Returns: --- |
| 511 | * |
| 512 | * Use: Frees everything in a key exchange context. |
| 513 | */ |
| 514 | |
| 515 | extern void kx_free(keyexch */*kx*/); |
| 516 | |
| 517 | /* --- @kx_newkeys@ --- * |
| 518 | * |
| 519 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 520 | * |
| 521 | * Returns: --- |
| 522 | * |
| 523 | * Use: Informs the key exchange module that its keys may have |
| 524 | * changed. If fetching the new keys fails, the peer will be |
| 525 | * destroyed, we log messages and struggle along with the old |
| 526 | * keys. |
| 527 | */ |
| 528 | |
| 529 | extern void kx_newkeys(keyexch */*kx*/); |
| 530 | |
| 531 | /* --- @kx_init@ --- * |
| 532 | * |
| 533 | * Arguments: @keyexch *kx@ = pointer to key exchange context |
| 534 | * @peer *p@ = pointer to peer context |
| 535 | * @keyset **ks@ = pointer to keyset list |
| 536 | * |
| 537 | * Returns: Zero if OK, nonzero if it failed. |
| 538 | * |
| 539 | * Use: Initializes a key exchange module. The module currently |
| 540 | * contains no keys, and will attempt to initiate a key |
| 541 | * exchange. |
| 542 | */ |
| 543 | |
| 544 | extern int kx_init(keyexch */*kx*/, peer */*p*/, keyset **/*ks*/); |
| 545 | |
| 546 | /*----- Keysets and symmetric cryptography --------------------------------*/ |
| 547 | |
| 548 | /* --- @ks_drop@ --- * |
| 549 | * |
| 550 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 551 | * |
| 552 | * Returns: --- |
| 553 | * |
| 554 | * Use: Decrements a keyset's reference counter. If the counter hits |
| 555 | * zero, the keyset is freed. |
| 556 | */ |
| 557 | |
| 558 | extern void ks_drop(keyset */*ks*/); |
| 559 | |
| 560 | /* --- @ks_gen@ --- * |
| 561 | * |
| 562 | * Arguments: @const void *k@ = pointer to key material |
| 563 | * @size_t x, y, z@ = offsets into key material (see below) |
| 564 | * @peer *p@ = pointer to peer information |
| 565 | * |
| 566 | * Returns: A pointer to the new keyset. |
| 567 | * |
| 568 | * Use: Derives a new keyset from the given key material. The |
| 569 | * offsets @x@, @y@ and @z@ separate the key material into three |
| 570 | * parts. Between the @k@ and @k + x@ is `my' contribution to |
| 571 | * the key material; between @k + x@ and @k + y@ is `your' |
| 572 | * contribution; and between @k + y@ and @k + z@ is a shared |
| 573 | * value we made together. These are used to construct two |
| 574 | * pairs of symmetric keys. Each pair consists of an encryption |
| 575 | * key and a message authentication key. One pair is used for |
| 576 | * outgoing messages, the other for incoming messages. |
| 577 | * |
| 578 | * The new key is marked so that it won't be selected for output |
| 579 | * by @ksl_encrypt@. You can still encrypt data with it by |
| 580 | * calling @ks_encrypt@ directly. |
| 581 | */ |
| 582 | |
| 583 | extern keyset *ks_gen(const void */*k*/, |
| 584 | size_t /*x*/, size_t /*y*/, size_t /*z*/, |
| 585 | peer */*p*/); |
| 586 | |
| 587 | /* --- @ks_tregen@ --- * |
| 588 | * |
| 589 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 590 | * |
| 591 | * Returns: The time at which moves ought to be made to replace this key. |
| 592 | */ |
| 593 | |
| 594 | extern time_t ks_tregen(keyset */*ks*/); |
| 595 | |
| 596 | /* --- @ks_activate@ --- * |
| 597 | * |
| 598 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 599 | * |
| 600 | * Returns: --- |
| 601 | * |
| 602 | * Use: Activates a keyset, so that it can be used for encrypting |
| 603 | * outgoing messages. |
| 604 | */ |
| 605 | |
| 606 | extern void ks_activate(keyset */*ks*/); |
| 607 | |
| 608 | /* --- @ks_encrypt@ --- * |
| 609 | * |
| 610 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 611 | * @unsigned ty@ = message type |
| 612 | * @buf *b@ = pointer to input buffer |
| 613 | * @buf *bb@ = pointer to output buffer |
| 614 | * |
| 615 | * Returns: Zero if OK, nonzero if the key needs replacing. If the |
| 616 | * encryption failed, the output buffer is broken and zero is |
| 617 | * returned. |
| 618 | * |
| 619 | * Use: Encrypts a block of data using the key. Note that the `key |
| 620 | * ought to be replaced' notification is only ever given once |
| 621 | * for each key. Also note that this call forces a keyset to be |
| 622 | * used even if it's marked as not for data output. |
| 623 | */ |
| 624 | |
| 625 | extern int ks_encrypt(keyset */*ks*/, unsigned /*ty*/, |
| 626 | buf */*b*/, buf */*bb*/); |
| 627 | |
| 628 | /* --- @ks_decrypt@ --- * |
| 629 | * |
| 630 | * Arguments: @keyset *ks@ = pointer to a keyset |
| 631 | * @unsigned ty@ = expected type code |
| 632 | * @buf *b@ = pointer to an input buffer |
| 633 | * @buf *bb@ = pointer to an output buffer |
| 634 | * |
| 635 | * Returns: Zero on success, or nonzero if there was some problem. |
| 636 | * |
| 637 | * Use: Attempts to decrypt a message using a given key. Note that |
| 638 | * requesting decryption with a key directly won't clear a |
| 639 | * marking that it's not for encryption. |
| 640 | */ |
| 641 | |
| 642 | extern int ks_decrypt(keyset */*ks*/, unsigned /*ty*/, |
| 643 | buf */*b*/, buf */*bb*/); |
| 644 | |
| 645 | /* --- @ksl_free@ --- * |
| 646 | * |
| 647 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 648 | * |
| 649 | * Returns: --- |
| 650 | * |
| 651 | * Use: Frees (releases references to) all of the keys in a keyset. |
| 652 | */ |
| 653 | |
| 654 | extern void ksl_free(keyset **/*ksroot*/); |
| 655 | |
| 656 | /* --- @ksl_link@ --- * |
| 657 | * |
| 658 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 659 | * @keyset *ks@ = pointer to a keyset |
| 660 | * |
| 661 | * Returns: --- |
| 662 | * |
| 663 | * Use: Links a keyset into a list. A keyset can only be on one list |
| 664 | * at a time. Bad things happen otherwise. |
| 665 | */ |
| 666 | |
| 667 | extern void ksl_link(keyset **/*ksroot*/, keyset */*ks*/); |
| 668 | |
| 669 | /* --- @ksl_prune@ --- * |
| 670 | * |
| 671 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 672 | * |
| 673 | * Returns: --- |
| 674 | * |
| 675 | * Use: Prunes the keyset list by removing keys which mustn't be used |
| 676 | * any more. |
| 677 | */ |
| 678 | |
| 679 | extern void ksl_prune(keyset **/*ksroot*/); |
| 680 | |
| 681 | /* --- @ksl_encrypt@ --- * |
| 682 | * |
| 683 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 684 | * @unsigned ty@ = message type |
| 685 | * @buf *b@ = pointer to input buffer |
| 686 | * @buf *bb@ = pointer to output buffer |
| 687 | * |
| 688 | * Returns: Nonzero if a new key is needed. |
| 689 | * |
| 690 | * Use: Encrypts a packet. |
| 691 | */ |
| 692 | |
| 693 | extern int ksl_encrypt(keyset **/*ksroot*/, unsigned /*ty*/, |
| 694 | buf */*b*/, buf */*bb*/); |
| 695 | |
| 696 | /* --- @ksl_decrypt@ --- * |
| 697 | * |
| 698 | * Arguments: @keyset **ksroot@ = pointer to keyset list head |
| 699 | * @unsigned ty@ = expected type code |
| 700 | * @buf *b@ = pointer to input buffer |
| 701 | * @buf *bb@ = pointer to output buffer |
| 702 | * |
| 703 | * Returns: Nonzero if the packet couldn't be decrypted. |
| 704 | * |
| 705 | * Use: Decrypts a packet. |
| 706 | */ |
| 707 | |
| 708 | extern int ksl_decrypt(keyset **/*ksroot*/, unsigned /*ty*/, |
| 709 | buf */*b*/, buf */*bb*/); |
| 710 | |
| 711 | /*----- Administration interface ------------------------------------------*/ |
| 712 | |
| 713 | /* --- @a_warn@ --- * |
| 714 | * |
| 715 | * Arguments: @const char *fmt@ = pointer to format string |
| 716 | * @...@ = other arguments |
| 717 | * |
| 718 | * Returns: --- |
| 719 | * |
| 720 | * Use: Informs all admin connections of a warning. |
| 721 | */ |
| 722 | |
| 723 | extern void a_warn(const char */*fmt*/, ...); |
| 724 | |
| 725 | /* --- @a_create@ --- * |
| 726 | * |
| 727 | * Arguments: @int fd_in, fd_out@ = file descriptors to use |
| 728 | * |
| 729 | * Returns: --- |
| 730 | * |
| 731 | * Use: Creates a new admin connection. |
| 732 | */ |
| 733 | |
| 734 | extern void a_create(int /*fd_in*/, int /*fd_out*/); |
| 735 | |
| 736 | /* --- @a_quit@ --- * |
| 737 | * |
| 738 | * Arguments: --- |
| 739 | * |
| 740 | * Returns: --- |
| 741 | * |
| 742 | * Use: Shuts things down nicely. |
| 743 | */ |
| 744 | |
| 745 | extern void a_quit(void); |
| 746 | |
| 747 | /* --- @a_daemon@ --- * |
| 748 | * |
| 749 | * Arguments: --- |
| 750 | * |
| 751 | * Returns: --- |
| 752 | * |
| 753 | * Use: Informs the admin module that it's a daemon. |
| 754 | */ |
| 755 | |
| 756 | extern void a_daemon(void); |
| 757 | |
| 758 | /* --- @a_init@ --- * |
| 759 | * |
| 760 | * Arguments: @const char *sock@ = socket name to create |
| 761 | * |
| 762 | * Returns: --- |
| 763 | * |
| 764 | * Use: Creates the admin listening socket. |
| 765 | */ |
| 766 | |
| 767 | extern void a_init(const char */*sock*/); |
| 768 | |
| 769 | /*----- Peer management ---------------------------------------------------*/ |
| 770 | |
| 771 | /* --- @p_txstart@ --- * |
| 772 | * |
| 773 | * Arguments: @peer *p@ = pointer to peer block |
| 774 | * @unsigned msg@ = message type code |
| 775 | * |
| 776 | * Returns: A pointer to a buffer to write to. |
| 777 | * |
| 778 | * Use: Starts sending to a peer. Only one send can happen at a |
| 779 | * time. |
| 780 | */ |
| 781 | |
| 782 | extern buf *p_txstart(peer */*p*/, unsigned /*msg*/); |
| 783 | |
| 784 | /* --- @p_txend@ --- * |
| 785 | * |
| 786 | * Arguments: @peer *p@ = pointer to peer block |
| 787 | * |
| 788 | * Returns: --- |
| 789 | * |
| 790 | * Use: Sends a packet to the peer. |
| 791 | */ |
| 792 | |
| 793 | extern void p_txend(peer */*p*/); |
| 794 | |
| 795 | /* --- @p_tun@ --- * |
| 796 | * |
| 797 | * Arguments: @peer *p@ = pointer to peer block |
| 798 | * @buf *b@ = buffer containing incoming packet |
| 799 | * |
| 800 | * Returns: --- |
| 801 | * |
| 802 | * Use: Handles a packet which needs to be sent to a peer. |
| 803 | */ |
| 804 | |
| 805 | extern void p_tun(peer */*p*/, buf */*b*/); |
| 806 | |
| 807 | /* --- @p_interval@ --- * |
| 808 | * |
| 809 | * Arguments: --- |
| 810 | * |
| 811 | * Returns: --- |
| 812 | * |
| 813 | * Use: Called periodically to do tidying. |
| 814 | */ |
| 815 | |
| 816 | extern void p_interval(void); |
| 817 | |
| 818 | /* --- @p_stats@ --- * |
| 819 | * |
| 820 | * Arguments: @peer *p@ = pointer to a peer block |
| 821 | * |
| 822 | * Returns: A pointer to the peer's statistics. |
| 823 | */ |
| 824 | |
| 825 | extern stats *p_stats(peer */*p*/); |
| 826 | |
| 827 | /* --- @p_ifname@ --- * |
| 828 | * |
| 829 | * Arguments: @peer *p@ = pointer to a peer block |
| 830 | * |
| 831 | * Returns: A pointer to the peer's interface name. |
| 832 | */ |
| 833 | |
| 834 | extern const char *p_ifname(peer */*p*/); |
| 835 | |
| 836 | /* --- @p_addr@ --- * |
| 837 | * |
| 838 | * Arguments: @peer *p@ = pointer to a peer block |
| 839 | * |
| 840 | * Returns: A pointer to the peer's address. |
| 841 | */ |
| 842 | |
| 843 | extern const addr *p_addr(peer */*p*/); |
| 844 | |
| 845 | /* --- @p_init@ --- * |
| 846 | * |
| 847 | * Arguments: @struct in_addr addr@ = address to bind to |
| 848 | * @unsigned port@ = port number to listen to |
| 849 | * |
| 850 | * Returns: --- |
| 851 | * |
| 852 | * Use: Initializes the peer system; creates the socket. |
| 853 | */ |
| 854 | |
| 855 | extern void p_init(struct in_addr /*addr*/, unsigned /*port*/); |
| 856 | |
| 857 | /* --- @p_port@ --- * |
| 858 | * |
| 859 | * Arguments: --- |
| 860 | * |
| 861 | * Returns: Port number used for socket. |
| 862 | */ |
| 863 | |
| 864 | unsigned p_port(void); |
| 865 | |
| 866 | /* --- @p_create@ --- * |
| 867 | * |
| 868 | * Arguments: @const char *name@ = name for this peer |
| 869 | * @struct sockaddr *sa@ = socket address of peer |
| 870 | * @size_t sz@ = size of socket address |
| 871 | * |
| 872 | * Returns: Pointer to the peer block, or null if it failed. |
| 873 | * |
| 874 | * Use: Creates a new named peer block. No peer is actually attached |
| 875 | * by this point. |
| 876 | */ |
| 877 | |
| 878 | extern peer *p_create(const char */*name*/, |
| 879 | struct sockaddr */*sa*/, size_t /*sz*/); |
| 880 | |
| 881 | /* --- @p_name@ --- * |
| 882 | * |
| 883 | * Arguments: @peer *p@ = pointer to a peer block |
| 884 | * |
| 885 | * Returns: A pointer to the peer's name. |
| 886 | */ |
| 887 | |
| 888 | extern const char *p_name(peer */*p*/); |
| 889 | |
| 890 | /* --- @p_find@ --- * |
| 891 | * |
| 892 | * Arguments: @const char *name@ = name to look up |
| 893 | * |
| 894 | * Returns: Pointer to the peer block, or null if not found. |
| 895 | * |
| 896 | * Use: Finds a peer by name. |
| 897 | */ |
| 898 | |
| 899 | extern peer *p_find(const char */*name*/); |
| 900 | |
| 901 | /* --- @p_destroy@ --- * |
| 902 | * |
| 903 | * Arguments: @peer *p@ = pointer to a peer |
| 904 | * |
| 905 | * Returns: --- |
| 906 | * |
| 907 | * Use: Destroys a peer. |
| 908 | */ |
| 909 | |
| 910 | extern void p_destroy(peer */*p*/); |
| 911 | |
| 912 | /* --- @p_first@, @p_next@ --- * |
| 913 | * |
| 914 | * Arguments: @peer *p@ = a peer block |
| 915 | * |
| 916 | * Returns: @peer_first@ returns the first peer in some ordering; |
| 917 | * @peer_next@ returns the peer following a given one in the |
| 918 | * same ordering. Null is returned for the end of the list. |
| 919 | */ |
| 920 | |
| 921 | extern peer *p_first(void); |
| 922 | extern peer *p_next(peer */*p*/); |
| 923 | |
| 924 | /*----- Tunnel interface --------------------------------------------------*/ |
| 925 | |
| 926 | /* --- @tun_init@ --- * |
| 927 | * |
| 928 | * Arguments: --- |
| 929 | * |
| 930 | * Returns: --- |
| 931 | * |
| 932 | * Use: Initializes the tunneling system. Maybe this will require |
| 933 | * opening file descriptors or something. |
| 934 | */ |
| 935 | |
| 936 | extern void tun_init(void); |
| 937 | |
| 938 | /* --- @tun_create@ --- * |
| 939 | * |
| 940 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 941 | * @peer *p@ = pointer to peer block |
| 942 | * |
| 943 | * Returns: Zero if it worked, nonzero on failure. |
| 944 | * |
| 945 | * Use: Initializes a new tunnel. |
| 946 | */ |
| 947 | |
| 948 | extern int tun_create(tunnel */*t*/, peer */*p*/); |
| 949 | |
| 950 | /* --- @tun_ifname@ --- * |
| 951 | * |
| 952 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 953 | * |
| 954 | * Returns: A pointer to the tunnel's interface name. |
| 955 | */ |
| 956 | |
| 957 | extern const char *tun_ifname(tunnel */*t*/); |
| 958 | |
| 959 | /* --- @tun_inject@ --- * |
| 960 | * |
| 961 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 962 | * @buf *b@ = buffer to send |
| 963 | * |
| 964 | * Returns: --- |
| 965 | * |
| 966 | * Use: Injects a packet into the local network stack. |
| 967 | */ |
| 968 | |
| 969 | extern void tun_inject(tunnel */*t*/, buf */*b*/); |
| 970 | |
| 971 | /* --- @tun_destroy@ --- * |
| 972 | * |
| 973 | * Arguments: @tunnel *t@ = pointer to tunnel block |
| 974 | * |
| 975 | * Returns: --- |
| 976 | * |
| 977 | * Use: Destroys a tunnel. |
| 978 | */ |
| 979 | |
| 980 | extern void tun_destroy(tunnel */*t*/); |
| 981 | |
| 982 | /*----- Other handy utilities ---------------------------------------------*/ |
| 983 | |
| 984 | /* --- @mpstr@ --- * |
| 985 | * |
| 986 | * Arguments: @mp *m@ = a multiprecision integer |
| 987 | * |
| 988 | * Returns: A pointer to the integer's textual representation. |
| 989 | * |
| 990 | * Use: Converts a multiprecision integer to a string. Corrupts |
| 991 | * @buf_t@. |
| 992 | */ |
| 993 | |
| 994 | extern const char *mpstr(mp */*m*/); |
| 995 | |
| 996 | /* --- @gestr@ --- * |
| 997 | * |
| 998 | * Arguments: @group *g@ = a group |
| 999 | * @ge *x@ = a group element |
| 1000 | * |
| 1001 | * Returns: A pointer to the element's textual representation. |
| 1002 | * |
| 1003 | * Use: Converts a group element to a string. Corrupts |
| 1004 | * @buf_t@. |
| 1005 | */ |
| 1006 | |
| 1007 | extern const char *gestr(group */*g*/, ge */*x*/); |
| 1008 | |
| 1009 | /* --- @timestr@ --- * |
| 1010 | * |
| 1011 | * Arguments: @time_t t@ = a time to convert |
| 1012 | * |
| 1013 | * Returns: A pointer to a textual representation of the time. |
| 1014 | * |
| 1015 | * Use: Converts a time to a textual representation. Corrupts |
| 1016 | * @buf_t@. |
| 1017 | */ |
| 1018 | |
| 1019 | extern const char *timestr(time_t /*t*/); |
| 1020 | |
| 1021 | /*----- That's all, folks -------------------------------------------------*/ |
| 1022 | |
| 1023 | #ifdef __cplusplus |
| 1024 | } |
| 1025 | #endif |
| 1026 | |
| 1027 | #endif |