3 * $Id: keyexch.c,v 1.2 2001/02/16 21:24:27 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.2 2001/02/16 21:24:27 mdw
33 * Rewrite for new key exchange protocol.
35 * Revision 1.1 2001/02/03 20:26:37 mdw
40 /*----- Header files ------------------------------------------------------*/
44 /*----- Tunable parameters ------------------------------------------------*/
46 #define T_VALID MIN(2)
47 #define T_RETRY SEC(10)
49 #define ISVALID(kx, now) ((now) < (kx)->t_valid)
51 /*----- Various utilities -------------------------------------------------*/
55 * Arguments: @HASH_CTX *r@ = pointer to hash context
56 * @mp *m@ = pointer to multiprecision integer
60 * Use: Adds the hash of a multiprecision integer to the context.
64 static void hashmp(HASH_CTX
*r
, mp
*m
)
67 buf_init(&b
, buf_t
, sizeof(buf_t
));
70 HASH(r
, BBASE(&b
), BLEN(&b
));
75 * Arguments: @struct timeval *tv@ = the current time
76 * @void *v@ = pointer to key exchange context
80 * Use: Acts when the key exchange timer goes off.
83 static void timer(struct timeval
*tv
, void *v
)
87 T( trace(T_KEYEXCH
, "keyexch: timer has popped"); )
91 /* --- @settimer@ --- *
93 * Arguments: @keyexch *kx@ = pointer to key exchange context
94 * @time_t t@ = when to set the timer for
98 * Use: Sets the timer for the next key exchange attempt.
101 static void settimer(keyexch
*kx
, time_t t
)
104 if (kx
->f
& KXF_TIMER
)
108 sel_addtimer(&sel
, &kx
->t
, &tv
, timer
, kx
);
112 /*----- Challenge management ----------------------------------------------*/
114 /* --- Notes on challenge management --- *
116 * We may get multiple different replies to our key exchange; some will be
117 * correct, some inserted by attackers. Up until @KX_THRESH@, all challenges
118 * received will be added to the table and given a full response. After
119 * @KX_THRESH@ distinct challenges are received, we return only a `cookie':
120 * our existing challenge, followed by a hash of the sender's challenge. We
121 * do %%\emph{not}%% give a bare challenge a reply slot at this stage. All
122 * properly-formed cookies are assigned a table slot: if none is spare, a
123 * used slot is randomly selected and destroyed. A cookie always receives a
127 /* --- @kxc_destroy@ --- *
129 * Arguments: @kxchal *kxc@ = pointer to the challenge block
133 * Use: Disposes of a challenge block.
136 static void kxc_destroy(kxchal
*kxc
)
138 if (kxc
->f
& KXF_TIMER
)
139 sel_rmtimer(&kxc
->t
);
146 /* --- @kxc_stoptimer@ --- *
148 * Arguments: @kxchal *kxc@ = pointer to the challenge block
152 * Use: Stops the challenge's retry timer from sending messages.
153 * Useful when the state machine is in the endgame of the
157 static void kxc_stoptimer(kxchal
*kxc
)
159 if (kxc
->f
& KXF_TIMER
)
160 sel_rmtimer(&kxc
->t
);
163 /* --- @kxc_new@ --- *
165 * Arguments: @keyexch *kx@ = pointer to key exchange block
168 * Returns: A pointer to the challenge block.
170 * Use: Returns a pointer to a new challenge block to fill in.
173 static kxchal
*kxc_new(keyexch
*kx
)
178 /* --- If we're over reply threshold, discard one at random --- */
180 if (kx
->nr
< KX_NCHAL
)
183 i
= rand_global
.ops
->range(&rand_global
, KX_NCHAL
);
184 kxc_destroy(kx
->r
[i
]);
187 /* --- Fill in the new structure --- */
189 kxc
= CREATE(kxchal
);
199 /* --- @kxc_bychal@ --- *
201 * Arguments: @keyexch *kx@ = pointer to key exchange block
202 * @mp *c@ = challenge from remote host
204 * Returns: Pointer to the challenge block, or null.
206 * Use: Finds a challenge block, given its challenge.
209 static kxchal
*kxc_bychal(keyexch
*kx
, mp
*c
)
213 for (i
= 0; i
< kx
->nr
; i
++) {
214 if (MP_EQ(c
, kx
->r
[i
]->c
))
220 /* --- @kxc_byhc@ --- *
222 * Arguments: @keyexch *kx@ = pointer to key exchange block
223 * @const octet *hc@ = challenge hash from remote host
225 * Returns: Pointer to the challenge block, or null.
227 * Use: Finds a challenge block, given a hash of its challenge.
230 static kxchal
*kxc_byhc(keyexch
*kx
, const octet
*hc
)
234 for (i
= 0; i
< kx
->nr
; i
++) {
235 if (memcmp(hc
, kx
->r
[i
]->hc
, HASHSZ
) == 0)
241 /* --- @kxc_answer@ --- *
243 * Arguments: @keyexch *kx@ = pointer to key exchange block
244 * @kxchal *kxc@ = pointer to challenge block
248 * Use: Sends a reply to the remote host, according to the data in
249 * this challenge block.
252 static void kxc_answer(keyexch
*kx
, kxchal
*kxc
);
254 static void kxc_timer(struct timeval
*tv
, void *v
)
257 kxc
->f
&= ~KXF_TIMER
;
258 kxc_answer(kxc
->kx
, kxc
);
261 static void kxc_answer(keyexch
*kx
, kxchal
*kxc
)
263 stats
*st
= p_stats(kx
->p
);
264 buf
*b
= p_txstart(kx
->p
, MSG_KEYEXCH
| (kxc
->r ? KX_REPLY
: KX_CHAL
));
268 /* --- Build the reply packet --- */
273 buf_put(b
, kx
->hc
, HASHSZ
);
274 buf_put(b
, kxc
->hc
, HASHSZ
);
275 buf_put(b
, kxc
->hrx
, HASHSZ
);
277 /* --- Maybe send an actual reply, if we have one --- */
280 T( trace(T_KEYEXCH
, "keyexch: resending challenge to `%s'",
283 T( trace(T_KEYEXCH
, "keyexch: sending reply to `%s'", p_name(kx
->p
)); )
284 buf_init(&bb
, buf_i
, sizeof(buf_i
));
285 buf_putmp(&bb
, kxc
->r
);
287 ks_encrypt(kxc
->ks
, &bb
, b
);
290 /* --- Update the statistics --- */
294 st
->sz_kxout
+= BLEN(b
);
298 /* --- Schedule another resend --- */
300 if (kxc
->f
& KXF_TIMER
)
301 sel_rmtimer(&kxc
->t
);
302 gettimeofday(&tv
, 0);
303 tv
.tv_sec
+= T_RETRY
;
304 sel_addtimer(&sel
, &kxc
->t
, &tv
, kxc_timer
, kxc
);
308 /*----- Individual message handlers ---------------------------------------*/
310 /* --- @getreply@ --- *
312 * Arguments: @keyexch *kx@ = pointer to key exchange context
313 * @mp *c@ = a challenge
314 * @const octet *hrx@ = the supplied expected-reply hash
316 * Returns: A pointer to the reply, or null if the reply-hash was wrong.
318 * Use: Computes replies to challenges.
321 static mp
*getreply(keyexch
*kx
, mp
*c
, const octet
*hrx
)
323 mp
*r
= mpmont_exp(&mg
, MP_NEW
, c
, kpriv
.x
);
328 HASH_STRING(&h
, "tripe-expected-reply");
333 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
334 trace(T_CRYPTO
, "crypto: computed reply = %s", mpstr(r
));
335 trace_block(T_CRYPTO
, "crypto: computed reply hash", buf
, HASHSZ
);
337 if (memcmp(buf
, hrx
, HASHSZ
) != 0) {
338 a_warn("invalid expected-reply hash from `%s'", p_name(kx
->p
));
345 /* --- @dochallenge@ --- *
347 * Arguments: @keyexch *kx@ = pointer to key exchange block
348 * @unsigned msg@ = message code for the packet
349 * @buf *b@ = buffer containing the packet
351 * Returns: Zero if OK, nonzero if the packet was rejected.
353 * Use: Processes a packet containing a challenge.
356 static int dochallenge(keyexch
*kx
, unsigned msg
, buf
*b
)
359 const octet
*hc
= 0, *hrx
= 0;
363 /* --- Ensure that we're in a sensible state --- */
365 if (kx
->s
!= KXS_CHAL
) {
366 a_warn("unexpected challenge from `%s'", p_name(kx
->p
));
370 /* --- Unpack the packet --- */
372 if ((c
= buf_getmp(b
)) == 0 ||
373 (msg
>= KX_COOKIE
&& (hc
= buf_get(b
, HASHSZ
)) == 0) ||
374 (msg
>= KX_CHAL
&& (hrx
= buf_get(b
, HASHSZ
)) == 0) ||
376 a_warn("malformed packet from `%s'", p_name(kx
->p
));
380 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
381 trace(T_CRYPTO
, "crypto: challenge = %s", mpstr(c
));
382 if (hc
) trace_block(T_CRYPTO
, "crypto: cookie", hc
, HASHSZ
);
383 if (hrx
) trace_block(T_CRYPTO
, "crypto: response hash", hrx
, HASHSZ
);
386 /* --- First, handle a bare challenge --- *
388 * If the table is heavily loaded, just emit a cookie and return.
391 if (!hc
&& kx
->nr
>= KX_THRESH
) {
392 T( trace(T_KEYEXCH
, "keyexch: too many challenges -- sending cookie"); )
393 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_COOKIE
);
396 HASH_STRING(&h
, "tripe-cookie");
398 HASH_DONE(&h
, buf_get(b
, HASHSZ
));
403 /* --- Discard a packet with an invalid cookie --- */
405 if (hc
&& memcmp(hc
, kx
->hc
, HASHSZ
) != 0) {
406 a_warn("incorrect cookie from `%s'", p_name(kx
->p
));
410 /* --- Find a challenge block for this packet --- *
412 * If there isn't one already, create a new one.
415 if ((kxc
= kxc_bychal(kx
, c
)) == 0) {
419 /* --- Be careful here --- *
421 * If this is a full challenge, and it's the first time I've seen it, I
422 * want to be able to throw it away before committing a table entry to
429 if ((r
= getreply(kx
, c
, hrx
)) == 0)
436 /* --- Work out the cookie for this challenge --- */
439 HASH_STRING(&h
, "tripe-cookie");
441 HASH_DONE(&h
, kxc
->hc
);
443 /* --- Compute the expected-reply hash --- */
446 HASH_STRING(&h
, "tripe-expected-reply");
450 HASH_DONE(&h
, kxc
->hrx
);
452 /* --- Work out the shared key --- */
454 r
= mpmont_exp(&mg
, MP_NEW
, c
, kx
->alpha
);
456 /* --- Compute the switch messages --- */
458 HASH_INIT(&h
); HASH_STRING(&h
, "tripe-switch-request");
459 hashmp(&h
, kx
->c
); hashmp(&h
, kxc
->c
);
460 HASH_DONE(&h
, kxc
->hswrq_out
);
461 HASH_INIT(&h
); HASH_STRING(&h
, "tripe-switch-confirm");
462 hashmp(&h
, kx
->c
); hashmp(&h
, kxc
->c
);
463 HASH_DONE(&h
, kxc
->hswok_out
);
465 HASH_INIT(&h
); HASH_STRING(&h
, "tripe-switch-request");
466 hashmp(&h
, kxc
->c
); hashmp(&h
, kx
->c
);
467 HASH_DONE(&h
, kxc
->hswrq_in
);
468 HASH_INIT(&h
); HASH_STRING(&h
, "tripe-switch-confirm");
469 hashmp(&h
, kxc
->c
); hashmp(&h
, kx
->c
);
470 HASH_DONE(&h
, kxc
->hswok_in
);
472 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
473 trace_block(T_CRYPTO
, "crypto: computed cookie", kxc
->hc
, HASHSZ
);
474 trace_block(T_CRYPTO
, "crypto: my reply hash", kxc
->hrx
, HASHSZ
);
475 trace(T_CRYPTO
, "crypto: shared secret = %s", mpstr(r
));
476 trace_block(T_CRYPTO
, "crypto: outbound switch request",
477 kxc
->hswrq_out
, HASHSZ
);
478 trace_block(T_CRYPTO
, "crypto: outbound switch confirm",
479 kxc
->hswok_out
, HASHSZ
);
480 trace_block(T_CRYPTO
, "crypto: inbound switch request",
481 kxc
->hswrq_in
, HASHSZ
);
482 trace_block(T_CRYPTO
, "crypto: inbound switch confirm",
483 kxc
->hswok_in
, HASHSZ
);
486 /* --- Create a new symmetric keyset --- */
488 buf_init(b
, buf_o
, sizeof(buf_o
));
489 buf_putmp(b
, kx
->c
); x
= BLEN(b
);
490 buf_putmp(b
, kxc
->c
); y
= BLEN(b
);
491 buf_putmp(b
, r
); z
= BLEN(b
);
494 kxc
->ks
= ks_gen(BBASE(b
), x
, y
, z
);
498 /* --- Answer the challenge if we need to --- */
500 if (hrx
&& !kxc
->r
) {
502 if ((r
= getreply(kx
, c
, hrx
)) == 0)
509 /* --- Tidy up and go home --- */
520 /* --- @resend@ --- *
522 * Arguments: @keyexch *kx@ = pointer to key exchange context
526 * Use: Sends the next message for a key exchange.
529 static void resend(keyexch
*kx
)
533 stats
*st
= p_stats(kx
->p
);
538 T( trace(T_KEYEXCH
, "sending prechallenge to `%s'", p_name(kx
->p
)); )
539 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_PRECHAL
);
543 T( trace(T_KEYEXCH
, "sending switch request to `%s'", p_name(kx
->p
)); )
545 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_SWITCH
);
546 buf_put(b
, kx
->hc
, HASHSZ
);
547 buf_put(b
, kxc
->hc
, HASHSZ
);
548 buf_init(&bb
, buf_i
, sizeof(buf_i
));
549 buf_putmp(&bb
, kxc
->r
);
550 buf_put(&bb
, kxc
->hswrq_out
, HASHSZ
);
552 ks_encrypt(kxc
->ks
, &bb
, b
);
555 T( trace(T_KEYEXCH
, "sending switch confirmation to `%s'",
558 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_SWITCHOK
);
559 buf_init(&bb
, buf_i
, sizeof(buf_i
));
560 buf_put(&bb
, kxc
->hswok_out
, HASHSZ
);
562 ks_encrypt(kxc
->ks
, &bb
, b
);
570 st
->sz_kxout
+= BLEN(b
);
574 if (kx
->s
< KXS_SWITCH
)
575 settimer(kx
, time(0) + T_RETRY
);
578 /* --- @matchreply@ --- *
580 * Arguments: @keyexch *kx@ = pointer to key exchange context
581 * @const octet *hc_in@ = a hash of his challenge
582 * @const octet *hc_out@ = a hash of my challenge (cookie)
583 * @const octet *krx@ = his expected-reply hash (optional)
584 * @buf *b@ = encrypted remainder of the packet
586 * Returns: A pointer to the challenge block if OK, or null on failure.
588 * Use: Checks a reply or switch packet, ensuring that its contents
589 * are sensible and correct. If they are, @*b@ is set to point
590 * to the remainder of the encrypted data, and the correct
591 * challenge is returned.
594 static kxchal
*matchreply(keyexch
*kx
, const octet
*hc_in
,
595 const octet
*hc_out
, const octet
*hrx
, buf
*b
)
601 /* --- Check the plaintext portions of the data --- */
603 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
604 trace_block(T_CRYPTO
, "crypto: challenge", hc_in
, HASHSZ
);
605 trace_block(T_CRYPTO
, "crypto: cookie", hc_out
, HASHSZ
);
606 if (hrx
) trace_block(T_CRYPTO
, "crypto: response hash", hrx
, HASHSZ
);
608 if (memcmp(hc_out
, kx
->hc
, HASHSZ
) != 0) {
609 a_warn("incorrect cookie from `%s'", p_name(kx
->p
));
612 if ((kxc
= kxc_byhc(kx
, hc_in
)) == 0) {
613 a_warn("received reply for unknown challenge from `%s'", p_name(kx
->p
));
617 /* --- Maybe compute a reply for the challenge --- */
621 a_warn("unexpected switch request from `%s'", p_name(kx
->p
));
624 if ((r
= getreply(kx
, kxc
->c
, hrx
)) == 0)
630 /* --- Decrypt the rest of the packet --- */
632 buf_init(&bb
, buf_o
, sizeof(buf_o
));
633 if (ks_decrypt(kxc
->ks
, b
, &bb
)) {
634 a_warn("failed to decrypt reply from `%s'", p_name(kx
->p
));
637 buf_init(b
, BBASE(&bb
), BLEN(&bb
));
638 if ((r
= buf_getmp(b
)) == 0) {
639 a_warn("invalid reply packet from `%s'", p_name(kx
->p
));
642 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
643 trace(T_CRYPTO
, "crypto: reply = %s", mpstr(r
));
645 if (!mp_eq(r
, kx
->rx
)) {
646 a_warn("incorrect reply from `%s'", p_name(kx
->p
));
660 /* --- @commit@ --- *
662 * Arguments: @keyexch *kx@ = pointer to key exchange context
663 * @kxchal *kxc@ = pointer to challenge to commit to
667 * Use: Commits to a particular challenge as being the `right' one,
668 * since a reply has arrived for it.
671 static void commit(keyexch
*kx
, kxchal
*kxc
)
675 for (i
= 0; i
< kx
->nr
; i
++) {
677 kxc_destroy(kx
->r
[i
]);
682 ksl_link(kx
->ks
, kxc
->ks
);
685 /* --- @doreply@ --- *
687 * Arguments: @keyexch *kx@ = pointer to key exchange context
688 * @buf *b@ = buffer containing packet
690 * Returns: Zero if OK, nonzero if the packet was rejected.
692 * Use: Handles a reply packet. This doesn't handle the various
693 * switch packets: they're rather too different.
696 static int doreply(keyexch
*kx
, buf
*b
)
698 const octet
*hc_in
, *hc_out
, *hrx
;
701 if (kx
->s
!= KXS_CHAL
&& kx
->s
!= KXS_COMMIT
) {
702 a_warn("unexpected reply from `%s'", p_name(kx
->p
));
705 if ((hc_in
= buf_get(b
, HASHSZ
)) == 0 ||
706 (hc_out
= buf_get(b
, HASHSZ
)) == 0 ||
707 (hrx
= buf_get(b
, HASHSZ
)) == 0) {
708 a_warn("invalid reply packet from `%s'", p_name(kx
->p
));
711 if ((kxc
= matchreply(kx
, hc_in
, hc_out
, hrx
, b
)) == 0)
714 a_warn("invalid reply packet from `%s'", p_name(kx
->p
));
717 if (kx
->s
== KXS_CHAL
) {
728 /* --- @doswitch@ --- *
730 * Arguments: @keyexch *kx@ = pointer to key exchange block
731 * @buf *b@ = pointer to buffer containing packet
733 * Returns: Zero if OK, nonzero if the packet was rejected.
735 * Use: Handles a reply with a switch request bolted onto it.
738 static int doswitch(keyexch
*kx
, buf
*b
)
740 const octet
*hc_in
, *hc_out
, *hswrq
;
743 if ((hc_in
= buf_get(b
, HASHSZ
)) == 0 ||
744 (hc_out
= buf_get(b
, HASHSZ
)) == 0) {
745 a_warn("invalid switch request from `%s'", p_name(kx
->p
));
748 if ((kxc
= matchreply(kx
, hc_in
, hc_out
, 0, b
)) == 0)
750 if ((hswrq
= buf_get(b
, HASHSZ
)) == 0 || BLEFT(b
)) {
751 a_warn("invalid switch request from `%s'", p_name(kx
->p
));
754 IF_TRACING(T_KEYEXCH
, {
755 trace_block(T_CRYPTO
, "crypto: switch request hash", hswrq
, HASHSZ
);
757 if (memcmp(hswrq
, kxc
->hswrq_in
, HASHSZ
) != 0) {
758 a_warn("incorrect switch request hash from `%s'", p_name(kx
->p
));
765 ks_activate(kxc
->ks
);
766 settimer(kx
, ks_tregen(kxc
->ks
));
777 /* --- @doswitchok@ --- *
779 * Arguments: @keyexch *kx@ = pointer to key exchange block
780 * @buf *b@ = pointer to buffer containing packet
782 * Returns: Zero if OK, nonzero if the packet was rejected.
784 * Use: Handles a reply with a switch request bolted onto it.
787 static int doswitchok(keyexch
*kx
, buf
*b
)
793 if (kx
->s
< KXS_COMMIT
) {
794 a_warn("unexpected switch confirmation from `%s'", p_name(kx
->p
));
798 buf_init(&bb
, buf_o
, sizeof(buf_o
));
799 if (ks_decrypt(kxc
->ks
, b
, &bb
)) {
800 a_warn("failed to decrypt switch confirmation from `%s'", p_name(kx
->p
));
803 buf_init(b
, BBASE(&bb
), BLEN(&bb
));
804 if ((hswok
= buf_get(b
, HASHSZ
)) == 0 || BLEFT(b
)) {
805 a_warn("invalid switch confirmation from `%s'", p_name(kx
->p
));
808 IF_TRACING(T_KEYEXCH
, {
809 trace_block(T_CRYPTO
, "crypto: switch confirmation hash", hswok
, HASHSZ
);
811 if (memcmp(hswok
, kxc
->hswok_in
, HASHSZ
) != 0) {
812 a_warn("incorrect switch confirmation hash from `%s'", p_name(kx
->p
));
815 if (kx
->s
< KXS_SWITCH
) {
816 ks_activate(kxc
->ks
);
817 settimer(kx
, ks_tregen(kxc
->ks
));
826 /*----- Main code ---------------------------------------------------------*/
830 * Arguments: @keyexch *kx@ = pointer to key exchange context
834 * Use: Stops a key exchange dead in its tracks. Throws away all of
835 * the context information. The context is left in an
836 * inconsistent state. The only functions which understand this
837 * state are @kx_free@ and @kx_init@ (which cause it internally
838 * it), and @start@ (which expects it to be the prevailing
842 static void stop(keyexch
*kx
)
846 if (kx
->f
& KXF_TIMER
)
848 for (i
= 0; i
< kx
->nr
; i
++)
849 kxc_destroy(kx
->r
[i
]);
857 * Arguments: @keyexch *kx@ = pointer to key exchange context
858 * @time_t now@ = the current time
862 * Use: Starts a new key exchange with the peer. The context must be
863 * in the bizarre state left by @stop@ or @kx_init@.
866 static void start(keyexch
*kx
, time_t now
)
872 kx
->alpha
= mprand_range(MP_NEW
, kpriv
.dp
.q
, &rand_global
, 0);
873 kx
->c
= mpmont_exp(&mg
, MP_NEW
, kpriv
.dp
.g
, kx
->alpha
);
874 kx
->rx
= mpmont_exp(&mg
, MP_NEW
, kx
->kpub
.y
, kx
->alpha
);
876 kx
->t_valid
= now
+ T_VALID
;
879 HASH_STRING(&h
, "tripe-cookie");
881 HASH_DONE(&h
, kx
->hc
);
883 IF_TRACING(T_KEYEXCH
, {
884 trace(T_KEYEXCH
, "keyexch: creating new challenge");
885 IF_TRACING(T_CRYPTO
, {
886 trace(T_CRYPTO
, "crypto: secret = %s", mpstr(kx
->alpha
));
887 trace(T_CRYPTO
, "crypto: challenge = %s", mpstr(kx
->c
));
888 trace(T_CRYPTO
, "crypto: expected response = %s", mpstr(kx
->rx
));
889 trace_block(T_CRYPTO
, "crypto: challenge cookie", kx
->hc
, HASHSZ
);
894 /* --- @kx_start@ --- *
896 * Arguments: @keyexch *kx@ = pointer to key exchange context
900 * Use: Stimulates a key exchange. If a key exchage is in progress,
901 * a new challenge is sent (unless the quiet timer forbids
902 * this); if no exchange is in progress, one is commenced.
905 void kx_start(keyexch
*kx
)
907 time_t now
= time(0);
909 if (!ISVALID(kx
, now
)) {
916 /* --- @kx_message@ --- *
918 * Arguments: @keyexch *kx@ = pointer to key exchange context
919 * @unsigned msg@ = the message code
920 * @buf *b@ = pointer to buffer containing the packet
924 * Use: Reads a packet containing key exchange messages and handles
928 void kx_message(keyexch
*kx
, unsigned msg
, buf
*b
)
930 time_t now
= time(0);
931 stats
*st
= p_stats(kx
->p
);
936 static const char *const pkname
[] = {
937 "prechallenge", "cookie", "challenge",
938 "reply", "switch request", "switch confirmation"
942 if (!ISVALID(kx
, now
)) {
947 T( trace(T_KEYEXCH
, "keyexch: processing %s packet from `%s'",
948 msg
< KX_NMSG ? pkname
[msg
] : "unknown", p_name(kx
->p
)); )
954 rc
= dochallenge(kx
, msg
, b
);
960 rc
= doswitch(kx
, b
);
963 rc
= doswitchok(kx
, b
);
966 a_warn("unexpected key exchange message type %u from `%p'",
980 /* --- @kx_free@ --- *
982 * Arguments: @keyexch *kx@ = pointer to key exchange context
986 * Use: Frees everything in a key exchange context.
989 void kx_free(keyexch
*kx
)
992 dh_pubfree(&kx
->kpub
);
995 /* --- @kx_newkeys@ --- *
997 * Arguments: @keyexch *kx@ = pointer to key exchange context
1001 * Use: Informs the key exchange module that its keys may have
1002 * changed. If fetching the new keys fails, the peer will be
1003 * destroyed, we log messages and struggle along with the old
1007 void kx_newkeys(keyexch
*kx
)
1011 if (km_getpubkey(p_name(kx
->p
), &dp
))
1013 dh_pubfree(&kx
->kpub
);
1015 if (kx
->s
!= KXS_SWITCH
) {
1016 T( trace(T_KEYEXCH
, "keyexch: restarting key negotiation with `%s'",
1023 /* --- @kx_init@ --- *
1025 * Arguments: @keyexch *kx@ = pointer to key exchange context
1026 * @peer *p@ = pointer to peer context
1027 * @keyset **ks@ = pointer to keyset list
1029 * Returns: Zero if OK, nonzero if it failed.
1031 * Use: Initializes a key exchange module. The module currently
1032 * contains no keys, and will attempt to initiate a key
1036 int kx_init(keyexch
*kx
, peer
*p
, keyset
**ks
)
1040 if (km_getpubkey(p_name(p
), &kx
->kpub
))
1047 /*----- That's all, folks -------------------------------------------------*/