3 * $Id: keyexch.c,v 1.7 2003/05/17 11:01:28 mdw Exp $
5 * Key exchange protocol
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
32 * Revision 1.7 2003/05/17 11:01:28 mdw
33 * Handle flags on challenge timers correctly to prevent confusing the event
36 * Revision 1.6 2003/04/06 10:26:35 mdw
37 * Report peer name on decrypt errors.
39 * Revision 1.5 2002/01/13 14:54:40 mdw
40 * Patch up zero-knowledge property by passing an encrypted log with a
41 * challenge, so that the prover can verify that the challenge is good.
43 * Revision 1.4 2001/06/22 19:40:36 mdw
44 * Support expiry of other peers' public keys.
46 * Revision 1.3 2001/06/19 22:07:09 mdw
49 * Revision 1.2 2001/02/16 21:24:27 mdw
50 * Rewrite for new key exchange protocol.
52 * Revision 1.1 2001/02/03 20:26:37 mdw
57 /*----- Header files ------------------------------------------------------*/
61 /*----- Tunable parameters ------------------------------------------------*/
63 #define T_VALID MIN(2) /* Challenge validity period */
64 #define T_RETRY SEC(10) /* Challenge retransmit interval */
66 #define ISVALID(kx, now) ((now) < (kx)->t_valid)
68 /*----- Various utilities -------------------------------------------------*/
72 * Arguments: @HASH_CTX *r@ = pointer to hash context
73 * @mp *m@ = pointer to multiprecision integer
77 * Use: Adds the hash of a multiprecision integer to the context.
81 static void hashmp(HASH_CTX *r, mp *m)
84 buf_init(&b, buf_t, sizeof(buf_t));
87 HASH(r, BBASE(&b), BLEN(&b));
90 /* --- @mpcrypt@ --- *
92 * Arguments: @mp *d@ = the destination integer
93 * @mp *x@ = the plaintext/ciphertext integer
94 * @size_t sz@ = the expected size of the plaintext
95 * @const octet *k@ = pointer to key material
96 * @size_t ksz@ = size of the key
98 * Returns: The encrypted/decrypted integer.
100 * Use: Encrypts (or decrypts) a multiprecision integer using another
101 * multiprecision integer as the key. This is a slightly grotty
102 * way to do this, but it's easier than the alternatives.
105 static mp *mpcrypt(mp *d, mp *x, size_t sz, const octet *k, size_t ksz)
109 MGF_INIT(&m, k, ksz, 0);
110 mp_storeb(x, buf_t, sz);
111 MGF_CRYPT(&m, buf_t, buf_t, sz);
112 return (mp_loadb(d, buf_t, sz));
117 * Arguments: @struct timeval *tv@ = the current time
118 * @void *v@ = pointer to key exchange context
122 * Use: Acts when the key exchange timer goes off.
125 static void timer(struct timeval *tv, void *v)
129 T( trace(T_KEYEXCH, "keyexch: timer has popped"); )
133 /* --- @settimer@ --- *
135 * Arguments: @keyexch *kx@ = pointer to key exchange context
136 * @time_t t@ = when to set the timer for
140 * Use: Sets the timer for the next key exchange attempt.
143 static void settimer(keyexch *kx, time_t t)
146 if (kx->f & KXF_TIMER)
150 sel_addtimer(&sel, &kx->t, &tv, timer, kx);
154 /*----- Challenge management ----------------------------------------------*/
156 /* --- Notes on challenge management --- *
158 * We may get multiple different replies to our key exchange; some will be
159 * correct, some inserted by attackers. Up until @KX_THRESH@, all challenges
160 * received will be added to the table and given a full response. After
161 * @KX_THRESH@ distinct challenges are received, we return only a `cookie':
162 * our existing challenge, followed by a hash of the sender's challenge. We
163 * do %%\emph{not}%% give a bare challenge a reply slot at this stage. All
164 * properly-formed cookies are assigned a table slot: if none is spare, a
165 * used slot is randomly selected and destroyed. A cookie always receives a
169 /* --- @kxc_destroy@ --- *
171 * Arguments: @kxchal *kxc@ = pointer to the challenge block
175 * Use: Disposes of a challenge block.
178 static void kxc_destroy(kxchal *kxc)
180 if (kxc->f & KXF_TIMER)
181 sel_rmtimer(&kxc->t);
189 /* --- @kxc_stoptimer@ --- *
191 * Arguments: @kxchal *kxc@ = pointer to the challenge block
195 * Use: Stops the challenge's retry timer from sending messages.
196 * Useful when the state machine is in the endgame of the
200 static void kxc_stoptimer(kxchal *kxc)
202 if (kxc->f & KXF_TIMER)
203 sel_rmtimer(&kxc->t);
204 kxc->f &= ~KXF_TIMER;
207 /* --- @kxc_new@ --- *
209 * Arguments: @keyexch *kx@ = pointer to key exchange block
211 * Returns: A pointer to the challenge block.
213 * Use: Returns a pointer to a new challenge block to fill in.
216 static kxchal *kxc_new(keyexch *kx)
221 /* --- If we're over reply threshold, discard one at random --- */
223 if (kx->nr < KX_NCHAL)
226 i = rand_global.ops->range(&rand_global, KX_NCHAL);
227 kxc_destroy(kx->r[i]);
230 /* --- Fill in the new structure --- */
232 kxc = CREATE(kxchal);
243 /* --- @kxc_bychal@ --- *
245 * Arguments: @keyexch *kx@ = pointer to key exchange block
246 * @mp *c@ = challenge from remote host
248 * Returns: Pointer to the challenge block, or null.
250 * Use: Finds a challenge block, given its challenge.
253 static kxchal *kxc_bychal(keyexch *kx, mp *c)
257 for (i = 0; i < kx->nr; i++) {
258 if (MP_EQ(c, kx->r[i]->c))
264 /* --- @kxc_byhc@ --- *
266 * Arguments: @keyexch *kx@ = pointer to key exchange block
267 * @const octet *hc@ = challenge hash from remote host
269 * Returns: Pointer to the challenge block, or null.
271 * Use: Finds a challenge block, given a hash of its challenge.
274 static kxchal *kxc_byhc(keyexch *kx, const octet *hc)
278 for (i = 0; i < kx->nr; i++) {
279 if (memcmp(hc, kx->r[i]->hc, HASHSZ) == 0)
285 /* --- @kxc_answer@ --- *
287 * Arguments: @keyexch *kx@ = pointer to key exchange block
288 * @kxchal *kxc@ = pointer to challenge block
292 * Use: Sends a reply to the remote host, according to the data in
293 * this challenge block.
296 static void kxc_answer(keyexch *kx, kxchal *kxc);
298 static void kxc_timer(struct timeval *tv, void *v)
301 kxc->f &= ~KXF_TIMER;
302 kxc_answer(kxc->kx, kxc);
305 static void kxc_answer(keyexch *kx, kxchal *kxc)
307 stats *st = p_stats(kx->p);
308 buf *b = p_txstart(kx->p, MSG_KEYEXCH | (kxc->r ? KX_REPLY : KX_CHAL));
312 /* --- Build the reply packet --- */
317 buf_put(b, kx->hc, HASHSZ);
318 buf_put(b, kxc->hc, HASHSZ);
319 buf_putmp(b, kxc->ck);
321 /* --- Maybe send an actual reply, if we have one --- */
324 T( trace(T_KEYEXCH, "keyexch: resending challenge to `%s'",
327 T( trace(T_KEYEXCH, "keyexch: sending reply to `%s'", p_name(kx->p)); )
328 buf_init(&bb, buf_i, sizeof(buf_i));
329 buf_putmp(&bb, kxc->r);
331 ks_encrypt(kxc->ks, &bb, b);
334 /* --- Update the statistics --- */
338 st->sz_kxout += BLEN(b);
342 /* --- Schedule another resend --- */
344 if (kxc->f & KXF_TIMER)
345 sel_rmtimer(&kxc->t);
346 gettimeofday(&tv, 0);
347 tv.tv_sec += T_RETRY;
348 sel_addtimer(&sel, &kxc->t, &tv, kxc_timer, kxc);
352 /*----- Individual message handlers ---------------------------------------*/
354 /* --- @getreply@ --- *
356 * Arguments: @keyexch *kx@ = pointer to key exchange context
357 * @mp *c@ = a challenge
358 * @mp *ck@ = the supplied expected-reply check value
360 * Returns: A pointer to the reply, or null if the reply-hash was wrong.
362 * Use: Computes replies to challenges.
365 static mp *getreply(keyexch *kx, mp *c, mp *ck)
367 mp *r = mpmont_exp(&mg, MP_NEW, c, kpriv.x);
374 HASH_STRING(&h, "tripe-expected-reply");
380 a = mpcrypt(MP_NEW, ck, mp_octets(kpriv.dp.q), buf, sizeof(buf));
381 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
382 trace(T_CRYPTO, "crypto: computed reply = %s", mpstr(r));
383 trace_block(T_CRYPTO, "crypto: computed reply hash", buf, HASHSZ);
384 trace(T_CRYPTO, "crypto: recovered log = %s", mpstr(a));
386 a = mpmont_exp(&mg, a, kpriv.dp.g, a);
389 a_warn("invalid expected-reply check from `%s'", p_name(kx->p));
390 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
391 trace(T_CRYPTO, "crypto: computed challenge = %s", mpstr(a));
399 /* --- @dochallenge@ --- *
401 * Arguments: @keyexch *kx@ = pointer to key exchange block
402 * @unsigned msg@ = message code for the packet
403 * @buf *b@ = buffer containing the packet
405 * Returns: Zero if OK, nonzero if the packet was rejected.
407 * Use: Processes a packet containing a challenge.
410 static int dochallenge(keyexch *kx, unsigned msg, buf *b)
418 /* --- Ensure that we're in a sensible state --- */
420 if (kx->s != KXS_CHAL) {
421 a_warn("unexpected challenge from `%s'", p_name(kx->p));
425 /* --- Unpack the packet --- */
427 if ((c = buf_getmp(b)) == 0 ||
428 (msg >= KX_COOKIE && (hc = buf_get(b, HASHSZ)) == 0) ||
429 (msg >= KX_CHAL && (ck = buf_getmp(b)) == 0) ||
431 a_warn("malformed packet from `%s'", p_name(kx->p));
435 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
436 trace(T_CRYPTO, "crypto: challenge = %s", mpstr(c));
437 if (hc) trace_block(T_CRYPTO, "crypto: cookie", hc, HASHSZ);
438 if (ck) trace(T_CRYPTO, "crypto: check value = %s", mpstr(ck));
441 /* --- First, handle a bare challenge --- *
443 * If the table is heavily loaded, just emit a cookie and return.
446 if (!hc && kx->nr >= KX_THRESH) {
447 T( trace(T_KEYEXCH, "keyexch: too many challenges -- sending cookie"); )
448 b = p_txstart(kx->p, MSG_KEYEXCH | KX_COOKIE);
451 HASH_STRING(&h, "tripe-cookie");
453 HASH_DONE(&h, buf_get(b, HASHSZ));
458 /* --- Discard a packet with an invalid cookie --- */
460 if (hc && memcmp(hc, kx->hc, HASHSZ) != 0) {
461 a_warn("incorrect cookie from `%s'", p_name(kx->p));
465 /* --- Find a challenge block for this packet --- *
467 * If there isn't one already, create a new one.
470 if ((kxc = kxc_bychal(kx, c)) == 0) {
474 /* --- Be careful here --- *
476 * If this is a full challenge, and it's the first time I've seen it, I
477 * want to be able to throw it away before committing a table entry to
484 if ((r = getreply(kx, c, ck)) == 0)
491 /* --- Work out the cookie for this challenge --- */
494 HASH_STRING(&h, "tripe-cookie");
496 HASH_DONE(&h, kxc->hc);
498 /* --- Compute the expected-reply hash --- */
501 HASH_STRING(&h, "tripe-expected-reply");
506 kxc->ck = mpcrypt(MP_NEW, kx->alpha, mp_octets(kpriv.dp.q),
509 /* --- Work out the shared key --- */
511 trace(T_CRYPTO, "debug: c = %s", mpstr(c));
512 trace(T_CRYPTO, "debug: alpha = %s", mpstr(kx->alpha));
513 r = mpmont_exp(&mg, MP_NEW, c, kx->alpha);
514 trace(T_CRYPTO, "debug: r = %s", mpstr(r));
516 /* --- Compute the switch messages --- */
518 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-request");
519 hashmp(&h, kx->c); hashmp(&h, kxc->c);
520 HASH_DONE(&h, kxc->hswrq_out);
521 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-confirm");
522 hashmp(&h, kx->c); hashmp(&h, kxc->c);
523 HASH_DONE(&h, kxc->hswok_out);
525 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-request");
526 hashmp(&h, kxc->c); hashmp(&h, kx->c);
527 HASH_DONE(&h, kxc->hswrq_in);
528 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-confirm");
529 hashmp(&h, kxc->c); hashmp(&h, kx->c);
530 HASH_DONE(&h, kxc->hswok_in);
532 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
533 trace_block(T_CRYPTO, "crypto: computed cookie", kxc->hc, HASHSZ);
534 trace_block(T_CRYPTO, "crypto: expected-reply hash",
536 trace(T_CRYPTO, "crypto: my reply check = %s", mpstr(kxc->ck));
537 trace(T_CRYPTO, "crypto: shared secret = %s", mpstr(r));
538 trace_block(T_CRYPTO, "crypto: outbound switch request",
539 kxc->hswrq_out, HASHSZ);
540 trace_block(T_CRYPTO, "crypto: outbound switch confirm",
541 kxc->hswok_out, HASHSZ);
542 trace_block(T_CRYPTO, "crypto: inbound switch request",
543 kxc->hswrq_in, HASHSZ);
544 trace_block(T_CRYPTO, "crypto: inbound switch confirm",
545 kxc->hswok_in, HASHSZ);
548 /* --- Create a new symmetric keyset --- */
550 buf_init(b, buf_o, sizeof(buf_o));
551 buf_putmp(b, kx->c); x = BLEN(b);
552 buf_putmp(b, kxc->c); y = BLEN(b);
553 buf_putmp(b, r); z = BLEN(b);
556 kxc->ks = ks_gen(BBASE(b), x, y, z, kx->p);
560 /* --- Answer the challenge if we need to --- */
564 if ((r = getreply(kx, c, ck)) == 0)
571 /* --- Tidy up and go home --- */
584 /* --- @resend@ --- *
586 * Arguments: @keyexch *kx@ = pointer to key exchange context
590 * Use: Sends the next message for a key exchange.
593 static void resend(keyexch *kx)
597 stats *st = p_stats(kx->p);
602 T( trace(T_KEYEXCH, "keyexch: sending prechallenge to `%s'",
604 b = p_txstart(kx->p, MSG_KEYEXCH | KX_PRECHAL);
608 T( trace(T_KEYEXCH, "keyexch: sending switch request to `%s'",
611 b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCH);
612 buf_put(b, kx->hc, HASHSZ);
613 buf_put(b, kxc->hc, HASHSZ);
614 buf_init(&bb, buf_i, sizeof(buf_i));
615 buf_putmp(&bb, kxc->r);
616 buf_put(&bb, kxc->hswrq_out, HASHSZ);
618 ks_encrypt(kxc->ks, &bb, b);
621 T( trace(T_KEYEXCH, "keyexch: sending switch confirmation to `%s'",
624 b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCHOK);
625 buf_init(&bb, buf_i, sizeof(buf_i));
626 buf_put(&bb, kxc->hswok_out, HASHSZ);
628 ks_encrypt(kxc->ks, &bb, b);
636 st->sz_kxout += BLEN(b);
640 if (kx->s < KXS_SWITCH)
641 settimer(kx, time(0) + T_RETRY);
644 /* --- @matchreply@ --- *
646 * Arguments: @keyexch *kx@ = pointer to key exchange context
647 * @const octet *hc_in@ = a hash of his challenge
648 * @const octet *hc_out@ = a hash of my challenge (cookie)
649 * @mp *ck@ = his expected-reply hash (optional)
650 * @buf *b@ = encrypted remainder of the packet
652 * Returns: A pointer to the challenge block if OK, or null on failure.
654 * Use: Checks a reply or switch packet, ensuring that its contents
655 * are sensible and correct. If they are, @*b@ is set to point
656 * to the remainder of the encrypted data, and the correct
657 * challenge is returned.
660 static kxchal *matchreply(keyexch *kx, const octet *hc_in,
661 const octet *hc_out, mp *ck, buf *b)
667 /* --- Check the plaintext portions of the data --- */
669 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
670 trace_block(T_CRYPTO, "crypto: challenge", hc_in, HASHSZ);
671 trace_block(T_CRYPTO, "crypto: cookie", hc_out, HASHSZ);
672 if (ck) trace(T_CRYPTO, "crypto: check value = %s", mpstr(ck));
674 if (memcmp(hc_out, kx->hc, HASHSZ) != 0) {
675 a_warn("incorrect cookie from `%s'", p_name(kx->p));
678 if ((kxc = kxc_byhc(kx, hc_in)) == 0) {
679 a_warn("received reply for unknown challenge from `%s'", p_name(kx->p));
683 /* --- Maybe compute a reply for the challenge --- */
687 a_warn("unexpected switch request from `%s'", p_name(kx->p));
690 if ((r = getreply(kx, kxc->c, ck)) == 0)
696 /* --- Decrypt the rest of the packet --- */
698 buf_init(&bb, buf_o, sizeof(buf_o));
699 if (ks_decrypt(kxc->ks, b, &bb)) {
700 a_warn("failed to decrypt reply from `%s'", p_name(kx->p));
703 buf_init(b, BBASE(&bb), BLEN(&bb));
704 if ((r = buf_getmp(b)) == 0) {
705 a_warn("invalid reply packet from `%s'", p_name(kx->p));
708 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
709 trace(T_CRYPTO, "crypto: reply = %s", mpstr(r));
711 if (!mp_eq(r, kx->rx)) {
712 a_warn("incorrect reply from `%s'", p_name(kx->p));
726 /* --- @commit@ --- *
728 * Arguments: @keyexch *kx@ = pointer to key exchange context
729 * @kxchal *kxc@ = pointer to challenge to commit to
733 * Use: Commits to a particular challenge as being the `right' one,
734 * since a reply has arrived for it.
737 static void commit(keyexch *kx, kxchal *kxc)
741 for (i = 0; i < kx->nr; i++) {
743 kxc_destroy(kx->r[i]);
748 ksl_link(kx->ks, kxc->ks);
751 /* --- @doreply@ --- *
753 * Arguments: @keyexch *kx@ = pointer to key exchange context
754 * @buf *b@ = buffer containing packet
756 * Returns: Zero if OK, nonzero if the packet was rejected.
758 * Use: Handles a reply packet. This doesn't handle the various
759 * switch packets: they're rather too different.
762 static int doreply(keyexch *kx, buf *b)
764 const octet *hc_in, *hc_out;
768 if (kx->s != KXS_CHAL && kx->s != KXS_COMMIT) {
769 a_warn("unexpected reply from `%s'", p_name(kx->p));
772 if ((hc_in = buf_get(b, HASHSZ)) == 0 ||
773 (hc_out = buf_get(b, HASHSZ)) == 0 ||
774 (ck = buf_getmp(b)) == 0) {
775 a_warn("invalid reply packet from `%s'", p_name(kx->p));
778 if ((kxc = matchreply(kx, hc_in, hc_out, ck, b)) == 0)
781 a_warn("invalid reply packet from `%s'", p_name(kx->p));
784 if (kx->s == KXS_CHAL) {
796 /* --- @doswitch@ --- *
798 * Arguments: @keyexch *kx@ = pointer to key exchange block
799 * @buf *b@ = pointer to buffer containing packet
801 * Returns: Zero if OK, nonzero if the packet was rejected.
803 * Use: Handles a reply with a switch request bolted onto it.
806 static int doswitch(keyexch *kx, buf *b)
808 const octet *hc_in, *hc_out, *hswrq;
811 if ((hc_in = buf_get(b, HASHSZ)) == 0 ||
812 (hc_out = buf_get(b, HASHSZ)) == 0) {
813 a_warn("invalid switch request from `%s'", p_name(kx->p));
816 if ((kxc = matchreply(kx, hc_in, hc_out, 0, b)) == 0)
818 if ((hswrq = buf_get(b, HASHSZ)) == 0 || BLEFT(b)) {
819 a_warn("invalid switch request from `%s'", p_name(kx->p));
822 IF_TRACING(T_KEYEXCH, {
823 trace_block(T_CRYPTO, "crypto: switch request hash", hswrq, HASHSZ);
825 if (memcmp(hswrq, kxc->hswrq_in, HASHSZ) != 0) {
826 a_warn("incorrect switch request hash from `%s'", p_name(kx->p));
833 ks_activate(kxc->ks);
834 settimer(kx, ks_tregen(kxc->ks));
845 /* --- @doswitchok@ --- *
847 * Arguments: @keyexch *kx@ = pointer to key exchange block
848 * @buf *b@ = pointer to buffer containing packet
850 * Returns: Zero if OK, nonzero if the packet was rejected.
852 * Use: Handles a reply with a switch request bolted onto it.
855 static int doswitchok(keyexch *kx, buf *b)
861 if (kx->s < KXS_COMMIT) {
862 a_warn("unexpected switch confirmation from `%s'", p_name(kx->p));
866 buf_init(&bb, buf_o, sizeof(buf_o));
867 if (ks_decrypt(kxc->ks, b, &bb)) {
868 a_warn("failed to decrypt switch confirmation from `%s'", p_name(kx->p));
871 buf_init(b, BBASE(&bb), BLEN(&bb));
872 if ((hswok = buf_get(b, HASHSZ)) == 0 || BLEFT(b)) {
873 a_warn("invalid switch confirmation from `%s'", p_name(kx->p));
876 IF_TRACING(T_KEYEXCH, {
877 trace_block(T_CRYPTO, "crypto: switch confirmation hash", hswok, HASHSZ);
879 if (memcmp(hswok, kxc->hswok_in, HASHSZ) != 0) {
880 a_warn("incorrect switch confirmation hash from `%s'", p_name(kx->p));
883 if (kx->s < KXS_SWITCH) {
884 ks_activate(kxc->ks);
885 settimer(kx, ks_tregen(kxc->ks));
894 /*----- Main code ---------------------------------------------------------*/
898 * Arguments: @keyexch *kx@ = pointer to key exchange context
902 * Use: Stops a key exchange dead in its tracks. Throws away all of
903 * the context information. The context is left in an
904 * inconsistent state. The only functions which understand this
905 * state are @kx_free@ and @kx_init@ (which cause it internally
906 * it), and @start@ (which expects it to be the prevailing
910 static void stop(keyexch *kx)
914 if (kx->f & KXF_DEAD)
917 if (kx->f & KXF_TIMER)
919 for (i = 0; i < kx->nr; i++)
920 kxc_destroy(kx->r[i]);
931 * Arguments: @keyexch *kx@ = pointer to key exchange context
932 * @time_t now@ = the current time
936 * Use: Starts a new key exchange with the peer. The context must be
937 * in the bizarre state left by @stop@ or @kx_init@.
940 static void start(keyexch *kx, time_t now)
944 assert(kx->f & KXF_DEAD);
948 kx->alpha = mprand_range(MP_NEW, kpriv.dp.q, &rand_global, 0);
949 kx->c = mpmont_exp(&mg, MP_NEW, kpriv.dp.g, kx->alpha);
950 kx->rx = mpmont_exp(&mg, MP_NEW, kx->kpub.y, kx->alpha);
952 kx->t_valid = now + T_VALID;
955 HASH_STRING(&h, "tripe-cookie");
957 HASH_DONE(&h, kx->hc);
959 IF_TRACING(T_KEYEXCH, {
960 trace(T_KEYEXCH, "keyexch: creating new challenge");
961 IF_TRACING(T_CRYPTO, {
962 trace(T_CRYPTO, "crypto: secret = %s", mpstr(kx->alpha));
963 trace(T_CRYPTO, "crypto: challenge = %s", mpstr(kx->c));
964 trace(T_CRYPTO, "crypto: expected response = %s", mpstr(kx->rx));
965 trace_block(T_CRYPTO, "crypto: challenge cookie", kx->hc, HASHSZ);
970 /* --- @checkpub@ --- *
972 * Arguments: @keyexch *kx@ = pointer to key exchange context
974 * Returns: Zero if OK, nonzero if the peer's public key has expired.
976 * Use: Deactivates the key-exchange until the peer acquires a new
980 static int checkpub(keyexch *kx)
983 if (kx->f & KXF_DEAD)
986 if (KEY_EXPIRED(now, kx->texp_kpub)) {
988 a_warn("public key for `%s' has expired", p_name(kx->p));
989 dh_pubfree(&kx->kpub);
990 kx->f &= ~KXF_PUBKEY;
996 /* --- @kx_start@ --- *
998 * Arguments: @keyexch *kx@ = pointer to key exchange context
1002 * Use: Stimulates a key exchange. If a key exchage is in progress,
1003 * a new challenge is sent (unless the quiet timer forbids
1004 * this); if no exchange is in progress, one is commenced.
1007 void kx_start(keyexch *kx)
1009 time_t now = time(0);
1013 if (!ISVALID(kx, now)) {
1020 /* --- @kx_message@ --- *
1022 * Arguments: @keyexch *kx@ = pointer to key exchange context
1023 * @unsigned msg@ = the message code
1024 * @buf *b@ = pointer to buffer containing the packet
1028 * Use: Reads a packet containing key exchange messages and handles
1032 void kx_message(keyexch *kx, unsigned msg, buf *b)
1034 time_t now = time(0);
1035 stats *st = p_stats(kx->p);
1040 static const char *const pkname[] = {
1041 "prechallenge", "cookie", "challenge",
1042 "reply", "switch request", "switch confirmation"
1049 if (!ISVALID(kx, now)) {
1054 T( trace(T_KEYEXCH, "keyexch: processing %s packet from `%s'",
1055 msg < KX_NMSG ? pkname[msg] : "unknown", p_name(kx->p)); )
1061 rc = dochallenge(kx, msg, b);
1064 rc = doreply(kx, b);
1067 rc = doswitch(kx, b);
1070 rc = doswitchok(kx, b);
1073 a_warn("unexpected key exchange message type %u from `%p'",
1087 /* --- @kx_free@ --- *
1089 * Arguments: @keyexch *kx@ = pointer to key exchange context
1093 * Use: Frees everything in a key exchange context.
1096 void kx_free(keyexch *kx)
1099 if (kx->f & KXF_PUBKEY)
1100 dh_pubfree(&kx->kpub);
1103 /* --- @kx_newkeys@ --- *
1105 * Arguments: @keyexch *kx@ = pointer to key exchange context
1109 * Use: Informs the key exchange module that its keys may have
1110 * changed. If fetching the new keys fails, the peer will be
1111 * destroyed, we log messages and struggle along with the old
1115 void kx_newkeys(keyexch *kx)
1119 if (km_getpubkey(p_name(kx->p), &dp, &kx->texp_kpub))
1121 if (kx->f & KXF_PUBKEY)
1122 dh_pubfree(&kx->kpub);
1124 kx->f |= KXF_PUBKEY;
1125 if ((kx->f & KXF_DEAD) || kx->s != KXS_SWITCH) {
1126 T( trace(T_KEYEXCH, "keyexch: restarting key negotiation with `%s'",
1134 /* --- @kx_init@ --- *
1136 * Arguments: @keyexch *kx@ = pointer to key exchange context
1137 * @peer *p@ = pointer to peer context
1138 * @keyset **ks@ = pointer to keyset list
1140 * Returns: Zero if OK, nonzero if it failed.
1142 * Use: Initializes a key exchange module. The module currently
1143 * contains no keys, and will attempt to initiate a key
1147 int kx_init(keyexch *kx, peer *p, keyset **ks)
1151 if (km_getpubkey(p_name(p), &kx->kpub, &kx->texp_kpub))
1153 kx->f = KXF_DEAD | KXF_PUBKEY;
1159 /*----- That's all, folks -------------------------------------------------*/