3 * $Id: keyexch.c,v 1.12 2004/04/08 01:36:17 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 /*----- Header files ------------------------------------------------------*/
33 /*----- Brief protocol overview -------------------------------------------*
35 * Let %$G$% be a cyclic group; let %$g$% be a generator of %$G$%, and let
36 * %$q$% be the order of %$G$%; for a key %$K$%, let %$E_K(\cdot)$% denote
37 * application of the symmetric packet protocol to a message; let
38 * %$H(\cdot)$% be the random oracle. Let $\alpha \inr \{0,\ldots,q - 1\}$%
39 * be Alice's private key; let %$a = g^\alpha$% be her public key; let %$b$%
40 * be Bob's public key.
42 * At the beginning of the session, Alice chooses
44 * %$\rho_A \inr \{0, \ldots q - 1\}$%
48 * %$r_A = g^{\rho_A}$% Alice's challenge
49 * %$c_A = H(\cookie{cookie}, r_A)$% Alice's cookie
50 * %$v_A = \rho_A \xor H(\cookie{expected-reply}, r_A, r_B, b^{\rho_A})$%
51 * Alice's challenge check value
52 * %$r_B^\alpha = a^{\rho_B}$% Alice's reply
53 * %$K = r_B^{\rho_A} = r_B^{\rho_A} = g^{\rho_A\rho_B}$%
54 * Alice and Bob's shared secret key
55 * %$w_A = H(\cookie{switch-request}, c_A, c_B)$%
56 * Alice's switch request value
57 * %$u_A = H(\cookie{switch-confirm}, c_A, c_B)$%
58 * Alice's switch confirm value
60 * The messages are then:
62 * %$\cookie{kx-pre-challenge}, r_A$%
63 * Initial greeting. In state @KXS_CHAL@.
65 * %$\cookie{kx-cookie}, r_A, c_B$%
66 * My table is full but I got your message.
68 * %$\cookie{kx-challenge}, r_A, c_B, v_A$%
69 * Here's a full challenge for you to answer.
71 * %$\cookie{kx-reply}, c_A, c_B, v_A, E_K(r_B^\alpha))$%
72 * Challenge accpeted: here's the answer. Commit to my challenge. Move
75 * %$\cookie{kx-switch}, c_A, c_B, E_K(r_B^\alpha, w_A))$%
76 * Reply received: here's my reply. Committed; send data; move to
79 * %$\cookie{kx-switch-ok}, E_K(u_A))$%
80 * Switch received. Committed; send data; move to @KXS_SWITCH@.
83 /*----- Tunable parameters ------------------------------------------------*/
85 #define T_VALID MIN(2) /* Challenge validity period */
86 #define T_RETRY SEC(10) /* Challenge retransmit interval */
88 #define ISVALID(kx, now) ((now) < (kx)->t_valid)
90 /*----- Various utilities -------------------------------------------------*/
94 * Arguments: @HASH_CTX *r@ = pointer to hash context
95 * @ge *x@ = pointer to group element
99 * Use: Adds the hash of a group element to the context. Corrupts
103 static void hashge(HASH_CTX *r, ge *x)
106 buf_init(&b, buf_t, sizeof(buf_t));
109 HASH(r, BBASE(&b), BLEN(&b));
112 /* --- @mpcrypt@ --- *
114 * Arguments: @mp *d@ = the destination integer
115 * @mp *x@ = the plaintext/ciphertext integer
116 * @size_t sz@ = the expected size of the plaintext
117 * @const octet *k@ = pointer to key material
118 * @size_t ksz@ = size of the key
120 * Returns: The encrypted/decrypted integer.
122 * Use: Encrypts (or decrypts) a multiprecision integer. In fact,
123 * the title is a bit of a misnomer: we actually compute
124 * %$x \xor H(k)$%, so it's a random oracle thing rather than an
128 static mp *mpcrypt(mp *d, mp *x, size_t sz, const octet *k, size_t ksz)
132 MGF_INIT(&m, k, ksz, 0);
133 mp_storeb(x, buf_t, sz);
134 MGF_CRYPT(&m, buf_t, buf_t, sz);
135 return (mp_loadb(d, buf_t, sz));
140 * Arguments: @struct timeval *tv@ = the current time
141 * @void *v@ = pointer to key exchange context
145 * Use: Acts when the key exchange timer goes off.
148 static void timer(struct timeval *tv, void *v)
152 T( trace(T_KEYEXCH, "keyexch: timer has popped"); )
156 /* --- @settimer@ --- *
158 * Arguments: @keyexch *kx@ = pointer to key exchange context
159 * @time_t t@ = when to set the timer for
163 * Use: Sets the timer for the next key exchange attempt.
166 static void settimer(keyexch *kx, time_t t)
169 if (kx->f & KXF_TIMER)
173 sel_addtimer(&sel, &kx->t, &tv, timer, kx);
177 /*----- Challenge management ----------------------------------------------*/
179 /* --- Notes on challenge management --- *
181 * We may get multiple different replies to our key exchange; some will be
182 * correct, some inserted by attackers. Up until @KX_THRESH@, all challenges
183 * received will be added to the table and given a full response. After
184 * @KX_THRESH@ distinct challenges are received, we return only a `cookie':
185 * our existing challenge, followed by a hash of the sender's challenge. We
186 * do %%\emph{not}%% give a bare challenge a reply slot at this stage. All
187 * properly-formed cookies are assigned a table slot: if none is spare, a
188 * used slot is randomly selected and destroyed. A cookie always receives a
192 /* --- @kxc_destroy@ --- *
194 * Arguments: @kxchal *kxc@ = pointer to the challenge block
198 * Use: Disposes of a challenge block.
201 static void kxc_destroy(kxchal *kxc)
203 if (kxc->f & KXF_TIMER)
204 sel_rmtimer(&kxc->t);
205 G_DESTROY(gg, kxc->c);
206 if (kxc->r) G_DESTROY(gg, kxc->r);
212 /* --- @kxc_stoptimer@ --- *
214 * Arguments: @kxchal *kxc@ = pointer to the challenge block
218 * Use: Stops the challenge's retry timer from sending messages.
219 * Useful when the state machine is in the endgame of the
223 static void kxc_stoptimer(kxchal *kxc)
225 if (kxc->f & KXF_TIMER)
226 sel_rmtimer(&kxc->t);
227 kxc->f &= ~KXF_TIMER;
230 /* --- @kxc_new@ --- *
232 * Arguments: @keyexch *kx@ = pointer to key exchange block
234 * Returns: A pointer to the challenge block.
236 * Use: Returns a pointer to a new challenge block to fill in.
239 static kxchal *kxc_new(keyexch *kx)
244 /* --- If we're over reply threshold, discard one at random --- */
246 if (kx->nr < KX_NCHAL)
249 i = rand_global.ops->range(&rand_global, KX_NCHAL);
250 kxc_destroy(kx->r[i]);
253 /* --- Fill in the new structure --- */
255 kxc = CREATE(kxchal);
256 kxc->c = G_CREATE(gg);
266 /* --- @kxc_bychal@ --- *
268 * Arguments: @keyexch *kx@ = pointer to key exchange block
269 * @ge *c@ = challenge from remote host
271 * Returns: Pointer to the challenge block, or null.
273 * Use: Finds a challenge block, given its challenge.
276 static kxchal *kxc_bychal(keyexch *kx, ge *c)
280 for (i = 0; i < kx->nr; i++) {
281 if (G_EQ(gg, c, kx->r[i]->c))
287 /* --- @kxc_byhc@ --- *
289 * Arguments: @keyexch *kx@ = pointer to key exchange block
290 * @const octet *hc@ = challenge hash from remote host
292 * Returns: Pointer to the challenge block, or null.
294 * Use: Finds a challenge block, given a hash of its challenge.
297 static kxchal *kxc_byhc(keyexch *kx, const octet *hc)
301 for (i = 0; i < kx->nr; i++) {
302 if (memcmp(hc, kx->r[i]->hc, HASHSZ) == 0)
308 /* --- @kxc_answer@ --- *
310 * Arguments: @keyexch *kx@ = pointer to key exchange block
311 * @kxchal *kxc@ = pointer to challenge block
315 * Use: Sends a reply to the remote host, according to the data in
316 * this challenge block.
319 static void kxc_answer(keyexch *kx, kxchal *kxc);
321 static void kxc_timer(struct timeval *tv, void *v)
324 kxc->f &= ~KXF_TIMER;
325 kxc_answer(kxc->kx, kxc);
328 static void kxc_answer(keyexch *kx, kxchal *kxc)
330 stats *st = p_stats(kx->p);
331 buf *b = p_txstart(kx->p, MSG_KEYEXCH | (kxc->r ? KX_REPLY : KX_CHAL));
335 /* --- Build the reply packet --- */
338 G_TOBUF(gg, b, kx->c);
340 buf_put(b, kx->hc, HASHSZ);
341 buf_put(b, kxc->hc, HASHSZ);
342 buf_putmp(b, kxc->ck);
344 /* --- Maybe send an actual reply, if we have one --- */
347 T( trace(T_KEYEXCH, "keyexch: resending challenge to `%s'",
350 T( trace(T_KEYEXCH, "keyexch: sending reply to `%s'", p_name(kx->p)); )
351 buf_init(&bb, buf_i, sizeof(buf_i));
352 G_TOBUF(gg, &bb, kxc->r);
354 ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_REPLY, &bb, b);
357 /* --- Update the statistics --- */
361 st->sz_kxout += BLEN(b);
365 /* --- Schedule another resend --- */
367 if (kxc->f & KXF_TIMER)
368 sel_rmtimer(&kxc->t);
369 gettimeofday(&tv, 0);
370 tv.tv_sec += T_RETRY;
371 sel_addtimer(&sel, &kxc->t, &tv, kxc_timer, kxc);
375 /*----- Individual message handlers ---------------------------------------*/
377 /* --- @getreply@ --- *
379 * Arguments: @keyexch *kx@ = pointer to key exchange context
380 * @ge *c@ = a challenge
381 * @mp *ck@ = the supplied expected-reply check value
383 * Returns: A pointer to the reply, or null if the reply-hash was wrong.
385 * Use: Computes replies to challenges.
388 static ge *getreply(keyexch *kx, ge *c, mp *ck)
390 ge *r = G_CREATE(gg);
391 ge *y = G_CREATE(gg);
397 G_EXP(gg, r, c, kpriv);
399 HASH_STRING(&h, "tripe-expected-reply");
405 a = mpcrypt(MP_NEW, ck, mp_octets(gg->r), buf, sizeof(buf));
406 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
407 trace(T_CRYPTO, "crypto: computed reply = %s", gestr(gg, r));
408 trace_block(T_CRYPTO, "crypto: computed reply hash", buf, HASHSZ);
409 trace(T_CRYPTO, "crypto: recovered log = %s", mpstr(a));
411 G_EXP(gg, y, gg->g, a);
414 a_warn("invalid expected-reply check from `%s'", p_name(kx->p));
415 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
416 trace(T_CRYPTO, "crypto: computed challenge = %s", gestr(gg, y));
426 /* --- @dochallenge@ --- *
428 * Arguments: @keyexch *kx@ = pointer to key exchange block
429 * @unsigned msg@ = message code for the packet
430 * @buf *b@ = buffer containing the packet
432 * Returns: Zero if OK, nonzero if the packet was rejected.
434 * Use: Processes a packet containing a challenge.
437 static int dochallenge(keyexch *kx, unsigned msg, buf *b)
439 ge *c = G_CREATE(gg);
446 /* --- Ensure that we're in a sensible state --- */
448 if (kx->s != KXS_CHAL) {
449 a_warn("unexpected challenge from `%s'", p_name(kx->p));
453 /* --- Unpack the packet --- */
455 if (G_FROMBUF(gg, b, c) ||
456 (msg >= KX_COOKIE && (hc = buf_get(b, HASHSZ)) == 0) ||
457 (msg >= KX_CHAL && (ck = buf_getmp(b)) == 0) ||
459 a_warn("malformed packet from `%s'", p_name(kx->p));
463 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
464 trace(T_CRYPTO, "crypto: challenge = %s", gestr(gg, c));
465 if (hc) trace_block(T_CRYPTO, "crypto: cookie", hc, HASHSZ);
466 if (ck) trace(T_CRYPTO, "crypto: check value = %s", mpstr(ck));
469 /* --- First, handle a bare challenge --- *
471 * If the table is heavily loaded, just emit a cookie and return.
474 if (!hc && kx->nr >= KX_THRESH) {
475 T( trace(T_KEYEXCH, "keyexch: too many challenges -- sending cookie"); )
476 b = p_txstart(kx->p, MSG_KEYEXCH | KX_COOKIE);
477 G_TOBUF(gg, b, kx->c);
479 HASH_STRING(&h, "tripe-cookie");
481 HASH_DONE(&h, buf_get(b, HASHSZ));
486 /* --- Discard a packet with an invalid cookie --- */
488 if (hc && memcmp(hc, kx->hc, HASHSZ) != 0) {
489 a_warn("incorrect cookie from `%s'", p_name(kx->p));
493 /* --- Find a challenge block for this packet --- *
495 * If there isn't one already, create a new one.
498 if ((kxc = kxc_bychal(kx, c)) == 0) {
502 /* --- Be careful here --- *
504 * If this is a full challenge, and it's the first time I've seen it, I
505 * want to be able to throw it away before committing a table entry to
512 if ((r = getreply(kx, c, ck)) == 0)
517 kxc->c = G_CREATE(gg);
518 G_COPY(gg, kxc->c, c);
520 /* --- Work out the cookie for this challenge --- */
523 HASH_STRING(&h, "tripe-cookie");
525 HASH_DONE(&h, kxc->hc);
527 /* --- Compute the expected-reply hash --- */
530 HASH_STRING(&h, "tripe-expected-reply");
535 kxc->ck = mpcrypt(MP_NEW, kx->alpha, mp_octets(gg->r),
538 /* --- Work out the shared key --- */
541 G_EXP(gg, r, c, kx->alpha);
543 /* --- Compute the switch messages --- */
545 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-request");
546 hashge(&h, kx->c); hashge(&h, kxc->c);
547 HASH_DONE(&h, kxc->hswrq_out);
548 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-confirm");
549 hashge(&h, kx->c); hashge(&h, kxc->c);
550 HASH_DONE(&h, kxc->hswok_out);
552 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-request");
553 hashge(&h, kxc->c); hashge(&h, kx->c);
554 HASH_DONE(&h, kxc->hswrq_in);
555 HASH_INIT(&h); HASH_STRING(&h, "tripe-switch-confirm");
556 hashge(&h, kxc->c); hashge(&h, kx->c);
557 HASH_DONE(&h, kxc->hswok_in);
559 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
560 trace_block(T_CRYPTO, "crypto: computed cookie", kxc->hc, HASHSZ);
561 trace_block(T_CRYPTO, "crypto: expected-reply hash",
563 trace(T_CRYPTO, "crypto: my reply check = %s", mpstr(kxc->ck));
564 trace(T_CRYPTO, "crypto: shared secret = %s", gestr(gg, r));
565 trace_block(T_CRYPTO, "crypto: outbound switch request",
566 kxc->hswrq_out, HASHSZ);
567 trace_block(T_CRYPTO, "crypto: outbound switch confirm",
568 kxc->hswok_out, HASHSZ);
569 trace_block(T_CRYPTO, "crypto: inbound switch request",
570 kxc->hswrq_in, HASHSZ);
571 trace_block(T_CRYPTO, "crypto: inbound switch confirm",
572 kxc->hswok_in, HASHSZ);
575 /* --- Create a new symmetric keyset --- */
577 buf_init(b, buf_o, sizeof(buf_o));
578 G_TOBUF(gg, b, kx->c); x = BLEN(b);
579 G_TOBUF(gg, b, kxc->c); y = BLEN(b);
580 G_TOBUF(gg, b, r); z = BLEN(b);
583 kxc->ks = ks_gen(BBASE(b), x, y, z, kx->p);
587 /* --- Answer the challenge if we need to --- */
591 if ((r = getreply(kx, c, ck)) == 0)
598 /* --- Tidy up and go home --- */
611 /* --- @resend@ --- *
613 * Arguments: @keyexch *kx@ = pointer to key exchange context
617 * Use: Sends the next message for a key exchange.
620 static void resend(keyexch *kx)
624 stats *st = p_stats(kx->p);
629 T( trace(T_KEYEXCH, "keyexch: sending prechallenge to `%s'",
631 b = p_txstart(kx->p, MSG_KEYEXCH | KX_PRECHAL);
632 G_TOBUF(gg, b, kx->c);
635 T( trace(T_KEYEXCH, "keyexch: sending switch request to `%s'",
638 b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCH);
639 buf_put(b, kx->hc, HASHSZ);
640 buf_put(b, kxc->hc, HASHSZ);
641 buf_init(&bb, buf_i, sizeof(buf_i));
642 G_TOBUF(gg, &bb, kxc->r);
643 buf_put(&bb, kxc->hswrq_out, HASHSZ);
645 ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCH, &bb, b);
648 T( trace(T_KEYEXCH, "keyexch: sending switch confirmation to `%s'",
651 b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCHOK);
652 buf_init(&bb, buf_i, sizeof(buf_i));
653 buf_put(&bb, kxc->hswok_out, HASHSZ);
655 ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, &bb, b);
663 st->sz_kxout += BLEN(b);
667 if (kx->s < KXS_SWITCH)
668 settimer(kx, time(0) + T_RETRY);
671 /* --- @matchreply@ --- *
673 * Arguments: @keyexch *kx@ = pointer to key exchange context
674 * @unsigned ty@ = type of incoming message
675 * @const octet *hc_in@ = a hash of his challenge
676 * @const octet *hc_out@ = a hash of my challenge (cookie)
677 * @mp *ck@ = his expected-reply hash (optional)
678 * @buf *b@ = encrypted remainder of the packet
680 * Returns: A pointer to the challenge block if OK, or null on failure.
682 * Use: Checks a reply or switch packet, ensuring that its contents
683 * are sensible and correct. If they are, @*b@ is set to point
684 * to the remainder of the encrypted data, and the correct
685 * challenge is returned.
688 static kxchal *matchreply(keyexch *kx, unsigned ty, const octet *hc_in,
689 const octet *hc_out, mp *ck, buf *b)
695 /* --- Check the plaintext portions of the data --- */
697 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
698 trace_block(T_CRYPTO, "crypto: challenge", hc_in, HASHSZ);
699 trace_block(T_CRYPTO, "crypto: cookie", hc_out, HASHSZ);
700 if (ck) trace(T_CRYPTO, "crypto: check value = %s", mpstr(ck));
702 if (memcmp(hc_out, kx->hc, HASHSZ) != 0) {
703 a_warn("incorrect cookie from `%s'", p_name(kx->p));
706 if ((kxc = kxc_byhc(kx, hc_in)) == 0) {
707 a_warn("received reply for unknown challenge from `%s'", p_name(kx->p));
711 /* --- Maybe compute a reply for the challenge --- */
715 a_warn("unexpected switch request from `%s'", p_name(kx->p));
718 if ((r = getreply(kx, kxc->c, ck)) == 0)
724 /* --- Decrypt the rest of the packet --- */
726 buf_init(&bb, buf_o, sizeof(buf_o));
727 if (ks_decrypt(kxc->ks, ty, b, &bb)) {
728 a_warn("failed to decrypt reply from `%s'", p_name(kx->p));
731 buf_init(b, BBASE(&bb), BLEN(&bb));
733 if (G_FROMBUF(gg, b, r)) {
734 a_warn("invalid reply packet from `%s'", p_name(kx->p));
737 IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, {
738 trace(T_CRYPTO, "crypto: reply = %s", gestr(gg, r));
740 if (!G_EQ(gg, r, kx->rx)) {
741 a_warn("incorrect reply from `%s'", p_name(kx->p));
751 if (r) G_DESTROY(gg, r);
755 /* --- @commit@ --- *
757 * Arguments: @keyexch *kx@ = pointer to key exchange context
758 * @kxchal *kxc@ = pointer to challenge to commit to
762 * Use: Commits to a particular challenge as being the `right' one,
763 * since a reply has arrived for it.
766 static void commit(keyexch *kx, kxchal *kxc)
770 for (i = 0; i < kx->nr; i++) {
772 kxc_destroy(kx->r[i]);
777 ksl_link(kx->ks, kxc->ks);
780 /* --- @doreply@ --- *
782 * Arguments: @keyexch *kx@ = pointer to key exchange context
783 * @buf *b@ = buffer containing packet
785 * Returns: Zero if OK, nonzero if the packet was rejected.
787 * Use: Handles a reply packet. This doesn't handle the various
788 * switch packets: they're rather too different.
791 static int doreply(keyexch *kx, buf *b)
793 const octet *hc_in, *hc_out;
797 if (kx->s != KXS_CHAL && kx->s != KXS_COMMIT) {
798 a_warn("unexpected reply from `%s'", p_name(kx->p));
801 if ((hc_in = buf_get(b, HASHSZ)) == 0 ||
802 (hc_out = buf_get(b, HASHSZ)) == 0 ||
803 (ck = buf_getmp(b)) == 0) {
804 a_warn("invalid reply packet from `%s'", p_name(kx->p));
807 if ((kxc = matchreply(kx, MSG_KEYEXCH | KX_REPLY,
808 hc_in, hc_out, ck, b)) == 0)
811 a_warn("invalid reply packet from `%s'", p_name(kx->p));
814 if (kx->s == KXS_CHAL) {
826 /* --- @doswitch@ --- *
828 * Arguments: @keyexch *kx@ = pointer to key exchange block
829 * @buf *b@ = pointer to buffer containing packet
831 * Returns: Zero if OK, nonzero if the packet was rejected.
833 * Use: Handles a reply with a switch request bolted onto it.
836 static int doswitch(keyexch *kx, buf *b)
838 const octet *hc_in, *hc_out, *hswrq;
841 if ((hc_in = buf_get(b, HASHSZ)) == 0 ||
842 (hc_out = buf_get(b, HASHSZ)) == 0) {
843 a_warn("invalid switch request from `%s'", p_name(kx->p));
846 if ((kxc = matchreply(kx, MSG_KEYEXCH | KX_SWITCH,
847 hc_in, hc_out, 0, b)) == 0)
849 if ((hswrq = buf_get(b, HASHSZ)) == 0 || BLEFT(b)) {
850 a_warn("invalid switch request from `%s'", p_name(kx->p));
853 IF_TRACING(T_KEYEXCH, {
854 trace_block(T_CRYPTO, "crypto: switch request hash", hswrq, HASHSZ);
856 if (memcmp(hswrq, kxc->hswrq_in, HASHSZ) != 0) {
857 a_warn("incorrect switch request hash from `%s'", p_name(kx->p));
864 ks_activate(kxc->ks);
865 settimer(kx, ks_tregen(kxc->ks));
876 /* --- @doswitchok@ --- *
878 * Arguments: @keyexch *kx@ = pointer to key exchange block
879 * @buf *b@ = pointer to buffer containing packet
881 * Returns: Zero if OK, nonzero if the packet was rejected.
883 * Use: Handles a reply with a switch request bolted onto it.
886 static int doswitchok(keyexch *kx, buf *b)
892 if (kx->s < KXS_COMMIT) {
893 a_warn("unexpected switch confirmation from `%s'", p_name(kx->p));
897 buf_init(&bb, buf_o, sizeof(buf_o));
898 if (ks_decrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, b, &bb)) {
899 a_warn("failed to decrypt switch confirmation from `%s'", p_name(kx->p));
902 buf_init(b, BBASE(&bb), BLEN(&bb));
903 if ((hswok = buf_get(b, HASHSZ)) == 0 || BLEFT(b)) {
904 a_warn("invalid switch confirmation from `%s'", p_name(kx->p));
907 IF_TRACING(T_KEYEXCH, {
908 trace_block(T_CRYPTO, "crypto: switch confirmation hash", hswok, HASHSZ);
910 if (memcmp(hswok, kxc->hswok_in, HASHSZ) != 0) {
911 a_warn("incorrect switch confirmation hash from `%s'", p_name(kx->p));
914 if (kx->s < KXS_SWITCH) {
915 ks_activate(kxc->ks);
916 settimer(kx, ks_tregen(kxc->ks));
925 /*----- Main code ---------------------------------------------------------*/
929 * Arguments: @keyexch *kx@ = pointer to key exchange context
933 * Use: Stops a key exchange dead in its tracks. Throws away all of
934 * the context information. The context is left in an
935 * inconsistent state. The only functions which understand this
936 * state are @kx_free@ and @kx_init@ (which cause it internally
937 * it), and @start@ (which expects it to be the prevailing
941 static void stop(keyexch *kx)
945 if (kx->f & KXF_DEAD)
948 if (kx->f & KXF_TIMER)
950 for (i = 0; i < kx->nr; i++)
951 kxc_destroy(kx->r[i]);
953 G_DESTROY(gg, kx->c);
954 G_DESTROY(gg, kx->rx);
962 * Arguments: @keyexch *kx@ = pointer to key exchange context
963 * @time_t now@ = the current time
967 * Use: Starts a new key exchange with the peer. The context must be
968 * in the bizarre state left by @stop@ or @kx_init@.
971 static void start(keyexch *kx, time_t now)
975 assert(kx->f & KXF_DEAD);
979 kx->alpha = mprand_range(MP_NEW, gg->r, &rand_global, 0);
980 kx->c = G_CREATE(gg); G_EXP(gg, kx->c, gg->g, kx->alpha);
981 kx->rx = G_CREATE(gg); G_EXP(gg, kx->rx, kx->kpub, kx->alpha);
983 kx->t_valid = now + T_VALID;
986 HASH_STRING(&h, "tripe-cookie");
988 HASH_DONE(&h, kx->hc);
990 IF_TRACING(T_KEYEXCH, {
991 trace(T_KEYEXCH, "keyexch: creating new challenge");
992 IF_TRACING(T_CRYPTO, {
993 trace(T_CRYPTO, "crypto: secret = %s", mpstr(kx->alpha));
994 trace(T_CRYPTO, "crypto: challenge = %s", gestr(gg, kx->c));
995 trace(T_CRYPTO, "crypto: expected response = %s", gestr(gg, kx->rx));
996 trace_block(T_CRYPTO, "crypto: challenge cookie", kx->hc, HASHSZ);
1001 /* --- @checkpub@ --- *
1003 * Arguments: @keyexch *kx@ = pointer to key exchange context
1005 * Returns: Zero if OK, nonzero if the peer's public key has expired.
1007 * Use: Deactivates the key-exchange until the peer acquires a new
1011 static int checkpub(keyexch *kx)
1014 if (kx->f & KXF_DEAD)
1017 if (KEY_EXPIRED(now, kx->texp_kpub)) {
1019 a_warn("public key for `%s' has expired", p_name(kx->p));
1020 G_COPY(gg, kx->kpub, gg->i);
1021 kx->f &= ~KXF_PUBKEY;
1027 /* --- @kx_start@ --- *
1029 * Arguments: @keyexch *kx@ = pointer to key exchange context
1033 * Use: Stimulates a key exchange. If a key exchage is in progress,
1034 * a new challenge is sent (unless the quiet timer forbids
1035 * this); if no exchange is in progress, one is commenced.
1038 void kx_start(keyexch *kx)
1040 time_t now = time(0);
1044 if (!ISVALID(kx, now)) {
1051 /* --- @kx_message@ --- *
1053 * Arguments: @keyexch *kx@ = pointer to key exchange context
1054 * @unsigned msg@ = the message code
1055 * @buf *b@ = pointer to buffer containing the packet
1059 * Use: Reads a packet containing key exchange messages and handles
1063 void kx_message(keyexch *kx, unsigned msg, buf *b)
1065 time_t now = time(0);
1066 stats *st = p_stats(kx->p);
1071 static const char *const pkname[] = {
1072 "prechallenge", "cookie", "challenge",
1073 "reply", "switch request", "switch confirmation"
1080 if (!ISVALID(kx, now)) {
1085 T( trace(T_KEYEXCH, "keyexch: processing %s packet from `%s'",
1086 msg < KX_NMSG ? pkname[msg] : "unknown", p_name(kx->p)); )
1092 rc = dochallenge(kx, msg, b);
1095 rc = doreply(kx, b);
1098 rc = doswitch(kx, b);
1101 rc = doswitchok(kx, b);
1104 a_warn("unexpected key exchange message type %u from `%p'",
1118 /* --- @kx_free@ --- *
1120 * Arguments: @keyexch *kx@ = pointer to key exchange context
1124 * Use: Frees everything in a key exchange context.
1127 void kx_free(keyexch *kx)
1130 G_DESTROY(gg, kx->kpub);
1133 /* --- @kx_newkeys@ --- *
1135 * Arguments: @keyexch *kx@ = pointer to key exchange context
1139 * Use: Informs the key exchange module that its keys may have
1140 * changed. If fetching the new keys fails, the peer will be
1141 * destroyed, we log messages and struggle along with the old
1145 void kx_newkeys(keyexch *kx)
1147 if (km_getpubkey(p_name(kx->p), kx->kpub, &kx->texp_kpub))
1149 kx->f |= KXF_PUBKEY;
1150 if ((kx->f & KXF_DEAD) || kx->s != KXS_SWITCH) {
1151 T( trace(T_KEYEXCH, "keyexch: restarting key negotiation with `%s'",
1159 /* --- @kx_init@ --- *
1161 * Arguments: @keyexch *kx@ = pointer to key exchange context
1162 * @peer *p@ = pointer to peer context
1163 * @keyset **ks@ = pointer to keyset list
1165 * Returns: Zero if OK, nonzero if it failed.
1167 * Use: Initializes a key exchange module. The module currently
1168 * contains no keys, and will attempt to initiate a key
1172 int kx_init(keyexch *kx, peer *p, keyset **ks)
1176 kx->kpub = G_CREATE(gg);
1177 if (km_getpubkey(p_name(p), kx->kpub, &kx->texp_kpub)) {
1178 G_DESTROY(gg, kx->kpub);
1181 kx->f = KXF_DEAD | KXF_PUBKEY;
1187 /*----- That's all, folks -------------------------------------------------*/