3 * $Id: keyexch.c,v 1.13 2004/04/18 18:08:11 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: @ghash *h@ = 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(ghash
*h
, ge
*x
)
106 buf_init(&b
, buf_t
, sizeof(buf_t
));
109 GH_HASH(h
, BBASE(&b
), BLEN(&b
));
112 /* --- @mpencrypt@, @mpdecrypt@ --- *
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
119 * Returns: The encrypted/decrypted integer.
121 * Use: Encrypts (or decrypts) a multiprecision integer. In fact,
122 * the title is a bit of a misnomer: we actually compute
123 * %$x \xor H(k)$%, so it's a random oracle thing rather than an
127 static mp
*mpencrypt(mp
*d
, mp
*x
, size_t sz
, const octet
*k
)
131 mgf
= GC_INIT(algs
.mgf
, k
, algs
.hashsz
);
132 mp_storeb(x
, buf_t
, sz
);
133 GC_ENCRYPT(mgf
, buf_t
, buf_t
, sz
);
135 return (mp_loadb(d
, buf_t
, sz
));
138 static mp
*mpdecrypt(mp
*d
, mp
*x
, size_t sz
, const octet
*k
)
142 mgf
= GC_INIT(algs
.mgf
, k
, algs
.hashsz
);
143 mp_storeb(x
, buf_t
, sz
);
144 GC_DECRYPT(mgf
, buf_t
, buf_t
, sz
);
146 return (mp_loadb(d
, buf_t
, sz
));
151 * Arguments: @struct timeval *tv@ = the current time
152 * @void *v@ = pointer to key exchange context
156 * Use: Acts when the key exchange timer goes off.
159 static void timer(struct timeval
*tv
, void *v
)
163 T( trace(T_KEYEXCH
, "keyexch: timer has popped"); )
167 /* --- @settimer@ --- *
169 * Arguments: @keyexch *kx@ = pointer to key exchange context
170 * @time_t t@ = when to set the timer for
174 * Use: Sets the timer for the next key exchange attempt.
177 static void settimer(keyexch
*kx
, time_t t
)
180 if (kx
->f
& KXF_TIMER
)
184 sel_addtimer(&sel
, &kx
->t
, &tv
, timer
, kx
);
188 /*----- Challenge management ----------------------------------------------*/
190 /* --- Notes on challenge management --- *
192 * We may get multiple different replies to our key exchange; some will be
193 * correct, some inserted by attackers. Up until @KX_THRESH@, all challenges
194 * received will be added to the table and given a full response. After
195 * @KX_THRESH@ distinct challenges are received, we return only a `cookie':
196 * our existing challenge, followed by a hash of the sender's challenge. We
197 * do %%\emph{not}%% give a bare challenge a reply slot at this stage. All
198 * properly-formed cookies are assigned a table slot: if none is spare, a
199 * used slot is randomly selected and destroyed. A cookie always receives a
203 /* --- @kxc_destroy@ --- *
205 * Arguments: @kxchal *kxc@ = pointer to the challenge block
209 * Use: Disposes of a challenge block.
212 static void kxc_destroy(kxchal
*kxc
)
214 if (kxc
->f
& KXF_TIMER
)
215 sel_rmtimer(&kxc
->t
);
216 G_DESTROY(gg
, kxc
->c
);
217 if (kxc
->r
) G_DESTROY(gg
, kxc
->r
);
223 /* --- @kxc_stoptimer@ --- *
225 * Arguments: @kxchal *kxc@ = pointer to the challenge block
229 * Use: Stops the challenge's retry timer from sending messages.
230 * Useful when the state machine is in the endgame of the
234 static void kxc_stoptimer(kxchal
*kxc
)
236 if (kxc
->f
& KXF_TIMER
)
237 sel_rmtimer(&kxc
->t
);
238 kxc
->f
&= ~KXF_TIMER
;
241 /* --- @kxc_new@ --- *
243 * Arguments: @keyexch *kx@ = pointer to key exchange block
245 * Returns: A pointer to the challenge block.
247 * Use: Returns a pointer to a new challenge block to fill in.
250 static kxchal
*kxc_new(keyexch
*kx
)
255 /* --- If we're over reply threshold, discard one at random --- */
257 if (kx
->nr
< KX_NCHAL
)
260 i
= rand_global
.ops
->range(&rand_global
, KX_NCHAL
);
261 kxc_destroy(kx
->r
[i
]);
264 /* --- Fill in the new structure --- */
266 kxc
= CREATE(kxchal
);
267 kxc
->c
= G_CREATE(gg
);
277 /* --- @kxc_bychal@ --- *
279 * Arguments: @keyexch *kx@ = pointer to key exchange block
280 * @ge *c@ = challenge from remote host
282 * Returns: Pointer to the challenge block, or null.
284 * Use: Finds a challenge block, given its challenge.
287 static kxchal
*kxc_bychal(keyexch
*kx
, ge
*c
)
291 for (i
= 0; i
< kx
->nr
; i
++) {
292 if (G_EQ(gg
, c
, kx
->r
[i
]->c
))
298 /* --- @kxc_byhc@ --- *
300 * Arguments: @keyexch *kx@ = pointer to key exchange block
301 * @const octet *hc@ = challenge hash from remote host
303 * Returns: Pointer to the challenge block, or null.
305 * Use: Finds a challenge block, given a hash of its challenge.
308 static kxchal
*kxc_byhc(keyexch
*kx
, const octet
*hc
)
312 for (i
= 0; i
< kx
->nr
; i
++) {
313 if (memcmp(hc
, kx
->r
[i
]->hc
, algs
.hashsz
) == 0)
319 /* --- @kxc_answer@ --- *
321 * Arguments: @keyexch *kx@ = pointer to key exchange block
322 * @kxchal *kxc@ = pointer to challenge block
326 * Use: Sends a reply to the remote host, according to the data in
327 * this challenge block.
330 static void kxc_answer(keyexch
*kx
, kxchal
*kxc
);
332 static void kxc_timer(struct timeval
*tv
, void *v
)
335 kxc
->f
&= ~KXF_TIMER
;
336 kxc_answer(kxc
->kx
, kxc
);
339 static void kxc_answer(keyexch
*kx
, kxchal
*kxc
)
341 stats
*st
= p_stats(kx
->p
);
342 buf
*b
= p_txstart(kx
->p
, MSG_KEYEXCH
| (kxc
->r ? KX_REPLY
: KX_CHAL
));
346 /* --- Build the reply packet --- */
349 G_TOBUF(gg
, b
, kx
->c
);
351 buf_put(b
, kx
->hc
, algs
.hashsz
);
352 buf_put(b
, kxc
->hc
, algs
.hashsz
);
353 buf_putmp(b
, kxc
->ck
);
355 /* --- Maybe send an actual reply, if we have one --- */
358 T( trace(T_KEYEXCH
, "keyexch: resending challenge to `%s'",
361 T( trace(T_KEYEXCH
, "keyexch: sending reply to `%s'", p_name(kx
->p
)); )
362 buf_init(&bb
, buf_i
, sizeof(buf_i
));
363 G_TOBUF(gg
, &bb
, kxc
->r
);
365 ks_encrypt(kxc
->ks
, MSG_KEYEXCH
| KX_REPLY
, &bb
, b
);
368 /* --- Update the statistics --- */
372 st
->sz_kxout
+= BLEN(b
);
376 /* --- Schedule another resend --- */
378 if (kxc
->f
& KXF_TIMER
)
379 sel_rmtimer(&kxc
->t
);
380 gettimeofday(&tv
, 0);
381 tv
.tv_sec
+= T_RETRY
;
382 sel_addtimer(&sel
, &kxc
->t
, &tv
, kxc_timer
, kxc
);
386 /*----- Individual message handlers ---------------------------------------*/
388 /* --- @getreply@ --- *
390 * Arguments: @keyexch *kx@ = pointer to key exchange context
391 * @ge *c@ = a challenge
392 * @mp *ck@ = the supplied expected-reply check value
394 * Returns: A pointer to the reply, or null if the reply-hash was wrong.
396 * Use: Computes replies to challenges.
399 static ge
*getreply(keyexch
*kx
, ge
*c
, mp
*ck
)
401 ge
*r
= G_CREATE(gg
);
402 ge
*y
= G_CREATE(gg
);
408 G_EXP(gg
, r
, c
, kpriv
);
410 HASH_STRING(h
, "tripe-expected-reply");
416 a
= mpdecrypt(MP_NEW
, ck
, mp_octets(gg
->r
), hh
);
417 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
418 trace(T_CRYPTO
, "crypto: computed reply = %s", gestr(gg
, r
));
419 trace_block(T_CRYPTO
, "crypto: computed reply hash", hh
, algs
.hashsz
);
420 trace(T_CRYPTO
, "crypto: recovered log = %s", mpstr(a
));
423 G_EXP(gg
, y
, gg
->g
, a
);
426 a_warn("invalid expected-reply check from `%s'", p_name(kx
->p
));
427 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
428 trace(T_CRYPTO
, "crypto: computed challenge = %s", gestr(gg
, y
));
438 /* --- @dochallenge@ --- *
440 * Arguments: @keyexch *kx@ = pointer to key exchange block
441 * @unsigned msg@ = message code for the packet
442 * @buf *b@ = buffer containing the packet
444 * Returns: Zero if OK, nonzero if the packet was rejected.
446 * Use: Processes a packet containing a challenge.
449 static int dochallenge(keyexch
*kx
, unsigned msg
, buf
*b
)
451 ge
*c
= G_CREATE(gg
);
457 /* --- Ensure that we're in a sensible state --- */
459 if (kx
->s
!= KXS_CHAL
) {
460 a_warn("unexpected challenge from `%s'", p_name(kx
->p
));
464 /* --- Unpack the packet --- */
466 if (G_FROMBUF(gg
, b
, c
) ||
467 (msg
>= KX_COOKIE
&& (hc
= buf_get(b
, algs
.hashsz
)) == 0) ||
468 (msg
>= KX_CHAL
&& (ck
= buf_getmp(b
)) == 0) ||
470 a_warn("malformed packet from `%s'", p_name(kx
->p
));
474 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
475 trace(T_CRYPTO
, "crypto: challenge = %s", gestr(gg
, c
));
476 if (hc
) trace_block(T_CRYPTO
, "crypto: cookie", hc
, algs
.hashsz
);
477 if (ck
) trace(T_CRYPTO
, "crypto: check value = %s", mpstr(ck
));
480 /* --- First, handle a bare challenge --- *
482 * If the table is heavily loaded, just emit a cookie and return.
485 if (!hc
&& kx
->nr
>= KX_THRESH
) {
486 T( trace(T_KEYEXCH
, "keyexch: too many challenges -- sending cookie"); )
487 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_COOKIE
);
488 G_TOBUF(gg
, b
, kx
->c
);
490 HASH_STRING(h
, "tripe-cookie");
492 GH_DONE(h
, buf_get(b
, algs
.hashsz
));
498 /* --- Discard a packet with an invalid cookie --- */
500 if (hc
&& memcmp(hc
, kx
->hc
, algs
.hashsz
) != 0) {
501 a_warn("incorrect cookie from `%s'", p_name(kx
->p
));
505 /* --- Find a challenge block for this packet --- *
507 * If there isn't one already, create a new one.
510 if ((kxc
= kxc_bychal(kx
, c
)) == 0) {
514 /* --- Be careful here --- *
516 * If this is a full challenge, and it's the first time I've seen it, I
517 * want to be able to throw it away before committing a table entry to
524 if ((r
= getreply(kx
, c
, ck
)) == 0)
529 kxc
->c
= G_CREATE(gg
);
530 G_COPY(gg
, kxc
->c
, c
);
532 /* --- Work out the cookie for this challenge --- */
535 HASH_STRING(h
, "tripe-cookie");
540 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
541 trace_block(T_CRYPTO
, "crypto: computed cookie", kxc
->hc
, algs
.hashsz
);
544 /* --- Compute the expected-reply hash --- */
547 HASH_STRING(h
, "tripe-expected-reply");
552 kxc
->ck
= mpencrypt(MP_NEW
, kx
->alpha
, mp_octets(gg
->r
), hc
);
553 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
554 trace_block(T_CRYPTO
, "crypto: expected-reply hash", hc
, algs
.hashsz
);
555 trace(T_CRYPTO
, "crypto: my reply check = %s", mpstr(kxc
->ck
));
559 /* --- Work out the shared key --- */
562 G_EXP(gg
, r
, c
, kx
->alpha
);
563 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
564 trace(T_CRYPTO
, "crypto: shared secret = %s", gestr(gg
, r
));
567 /* --- Compute the switch messages --- */
569 h
= GH_INIT(algs
.h
); HASH_STRING(h
, "tripe-switch-request");
570 hashge(h
, kx
->c
); hashge(h
, kxc
->c
);
571 GH_DONE(h
, kxc
->hswrq_out
); GH_DESTROY(h
);
572 h
= GH_INIT(algs
.h
); HASH_STRING(h
, "tripe-switch-confirm");
573 hashge(h
, kx
->c
); hashge(h
, kxc
->c
);
574 GH_DONE(h
, kxc
->hswok_out
); GH_DESTROY(h
);
576 h
= GH_INIT(algs
.h
); HASH_STRING(h
, "tripe-switch-request");
577 hashge(h
, kxc
->c
); hashge(h
, kx
->c
);
578 GH_DONE(h
, kxc
->hswrq_in
); GH_DESTROY(h
);
579 h
= GH_INIT(algs
.h
); HASH_STRING(h
, "tripe-switch-confirm");
580 hashge(h
, kxc
->c
); hashge(h
, kx
->c
);
581 GH_DONE(h
, kxc
->hswok_in
); GH_DESTROY(h
);
583 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
584 trace_block(T_CRYPTO
, "crypto: outbound switch request",
585 kxc
->hswrq_out
, algs
.hashsz
);
586 trace_block(T_CRYPTO
, "crypto: outbound switch confirm",
587 kxc
->hswok_out
, algs
.hashsz
);
588 trace_block(T_CRYPTO
, "crypto: inbound switch request",
589 kxc
->hswrq_in
, algs
.hashsz
);
590 trace_block(T_CRYPTO
, "crypto: inbound switch confirm",
591 kxc
->hswok_in
, algs
.hashsz
);
594 /* --- Create a new symmetric keyset --- */
596 buf_init(b
, buf_o
, sizeof(buf_o
));
597 G_TOBUF(gg
, b
, kx
->c
); x
= BLEN(b
);
598 G_TOBUF(gg
, b
, kxc
->c
); y
= BLEN(b
);
599 G_TOBUF(gg
, b
, r
); z
= BLEN(b
);
602 kxc
->ks
= ks_gen(BBASE(b
), x
, y
, z
, kx
->p
);
606 /* --- Answer the challenge if we need to --- */
610 if ((r
= getreply(kx
, c
, ck
)) == 0)
617 /* --- Tidy up and go home --- */
630 /* --- @resend@ --- *
632 * Arguments: @keyexch *kx@ = pointer to key exchange context
636 * Use: Sends the next message for a key exchange.
639 static void resend(keyexch
*kx
)
643 stats
*st
= p_stats(kx
->p
);
648 T( trace(T_KEYEXCH
, "keyexch: sending prechallenge to `%s'",
650 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_PRECHAL
);
651 G_TOBUF(gg
, b
, kx
->c
);
654 T( trace(T_KEYEXCH
, "keyexch: sending switch request to `%s'",
657 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_SWITCH
);
658 buf_put(b
, kx
->hc
, algs
.hashsz
);
659 buf_put(b
, kxc
->hc
, algs
.hashsz
);
660 buf_init(&bb
, buf_i
, sizeof(buf_i
));
661 G_TOBUF(gg
, &bb
, kxc
->r
);
662 buf_put(&bb
, kxc
->hswrq_out
, algs
.hashsz
);
664 ks_encrypt(kxc
->ks
, MSG_KEYEXCH
| KX_SWITCH
, &bb
, b
);
667 T( trace(T_KEYEXCH
, "keyexch: sending switch confirmation to `%s'",
670 b
= p_txstart(kx
->p
, MSG_KEYEXCH
| KX_SWITCHOK
);
671 buf_init(&bb
, buf_i
, sizeof(buf_i
));
672 buf_put(&bb
, kxc
->hswok_out
, algs
.hashsz
);
674 ks_encrypt(kxc
->ks
, MSG_KEYEXCH
| KX_SWITCHOK
, &bb
, b
);
682 st
->sz_kxout
+= BLEN(b
);
686 if (kx
->s
< KXS_SWITCH
)
687 settimer(kx
, time(0) + T_RETRY
);
690 /* --- @matchreply@ --- *
692 * Arguments: @keyexch *kx@ = pointer to key exchange context
693 * @unsigned ty@ = type of incoming message
694 * @const octet *hc_in@ = a hash of his challenge
695 * @const octet *hc_out@ = a hash of my challenge (cookie)
696 * @mp *ck@ = his expected-reply hash (optional)
697 * @buf *b@ = encrypted remainder of the packet
699 * Returns: A pointer to the challenge block if OK, or null on failure.
701 * Use: Checks a reply or switch packet, ensuring that its contents
702 * are sensible and correct. If they are, @*b@ is set to point
703 * to the remainder of the encrypted data, and the correct
704 * challenge is returned.
707 static kxchal
*matchreply(keyexch
*kx
, unsigned ty
, const octet
*hc_in
,
708 const octet
*hc_out
, mp
*ck
, buf
*b
)
714 /* --- Check the plaintext portions of the data --- */
716 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
717 trace_block(T_CRYPTO
, "crypto: challenge", hc_in
, algs
.hashsz
);
718 trace_block(T_CRYPTO
, "crypto: cookie", hc_out
, algs
.hashsz
);
719 if (ck
) trace(T_CRYPTO
, "crypto: check value = %s", mpstr(ck
));
721 if (memcmp(hc_out
, kx
->hc
, algs
.hashsz
) != 0) {
722 a_warn("incorrect cookie from `%s'", p_name(kx
->p
));
725 if ((kxc
= kxc_byhc(kx
, hc_in
)) == 0) {
726 a_warn("received reply for unknown challenge from `%s'", p_name(kx
->p
));
730 /* --- Maybe compute a reply for the challenge --- */
734 a_warn("unexpected switch request from `%s'", p_name(kx
->p
));
737 if ((r
= getreply(kx
, kxc
->c
, ck
)) == 0)
743 /* --- Decrypt the rest of the packet --- */
745 buf_init(&bb
, buf_o
, sizeof(buf_o
));
746 if (ks_decrypt(kxc
->ks
, ty
, b
, &bb
)) {
747 a_warn("failed to decrypt reply from `%s'", p_name(kx
->p
));
750 buf_init(b
, BBASE(&bb
), BLEN(&bb
));
752 if (G_FROMBUF(gg
, b
, r
)) {
753 a_warn("invalid reply packet from `%s'", p_name(kx
->p
));
756 IF_TRACING(T_KEYEXCH
, IF_TRACING(T_CRYPTO
, {
757 trace(T_CRYPTO
, "crypto: reply = %s", gestr(gg
, r
));
759 if (!G_EQ(gg
, r
, kx
->rx
)) {
760 a_warn("incorrect reply from `%s'", p_name(kx
->p
));
770 if (r
) G_DESTROY(gg
, r
);
774 /* --- @commit@ --- *
776 * Arguments: @keyexch *kx@ = pointer to key exchange context
777 * @kxchal *kxc@ = pointer to challenge to commit to
781 * Use: Commits to a particular challenge as being the `right' one,
782 * since a reply has arrived for it.
785 static void commit(keyexch
*kx
, kxchal
*kxc
)
789 for (i
= 0; i
< kx
->nr
; i
++) {
791 kxc_destroy(kx
->r
[i
]);
796 ksl_link(kx
->ks
, kxc
->ks
);
799 /* --- @doreply@ --- *
801 * Arguments: @keyexch *kx@ = pointer to key exchange context
802 * @buf *b@ = buffer containing packet
804 * Returns: Zero if OK, nonzero if the packet was rejected.
806 * Use: Handles a reply packet. This doesn't handle the various
807 * switch packets: they're rather too different.
810 static int doreply(keyexch
*kx
, buf
*b
)
812 const octet
*hc_in
, *hc_out
;
816 if (kx
->s
!= KXS_CHAL
&& kx
->s
!= KXS_COMMIT
) {
817 a_warn("unexpected reply from `%s'", p_name(kx
->p
));
820 if ((hc_in
= buf_get(b
, algs
.hashsz
)) == 0 ||
821 (hc_out
= buf_get(b
, algs
.hashsz
)) == 0 ||
822 (ck
= buf_getmp(b
)) == 0) {
823 a_warn("invalid reply packet from `%s'", p_name(kx
->p
));
826 if ((kxc
= matchreply(kx
, MSG_KEYEXCH
| KX_REPLY
,
827 hc_in
, hc_out
, ck
, b
)) == 0)
830 a_warn("invalid reply packet from `%s'", p_name(kx
->p
));
833 if (kx
->s
== KXS_CHAL
) {
845 /* --- @doswitch@ --- *
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 doswitch(keyexch
*kx
, buf
*b
)
857 const octet
*hc_in
, *hc_out
, *hswrq
;
860 if ((hc_in
= buf_get(b
, algs
.hashsz
)) == 0 ||
861 (hc_out
= buf_get(b
, algs
.hashsz
)) == 0) {
862 a_warn("invalid switch request from `%s'", p_name(kx
->p
));
865 if ((kxc
= matchreply(kx
, MSG_KEYEXCH
| KX_SWITCH
,
866 hc_in
, hc_out
, 0, b
)) == 0)
868 if ((hswrq
= buf_get(b
, algs
.hashsz
)) == 0 || BLEFT(b
)) {
869 a_warn("invalid switch request from `%s'", p_name(kx
->p
));
872 IF_TRACING(T_KEYEXCH
, {
873 trace_block(T_CRYPTO
, "crypto: switch request hash", hswrq
, algs
.hashsz
);
875 if (memcmp(hswrq
, kxc
->hswrq_in
, algs
.hashsz
) != 0) {
876 a_warn("incorrect switch request hash from `%s'", p_name(kx
->p
));
883 ks_activate(kxc
->ks
);
884 settimer(kx
, ks_tregen(kxc
->ks
));
895 /* --- @doswitchok@ --- *
897 * Arguments: @keyexch *kx@ = pointer to key exchange block
898 * @buf *b@ = pointer to buffer containing packet
900 * Returns: Zero if OK, nonzero if the packet was rejected.
902 * Use: Handles a reply with a switch request bolted onto it.
905 static int doswitchok(keyexch
*kx
, buf
*b
)
911 if (kx
->s
< KXS_COMMIT
) {
912 a_warn("unexpected switch confirmation from `%s'", p_name(kx
->p
));
916 buf_init(&bb
, buf_o
, sizeof(buf_o
));
917 if (ks_decrypt(kxc
->ks
, MSG_KEYEXCH
| KX_SWITCHOK
, b
, &bb
)) {
918 a_warn("failed to decrypt switch confirmation from `%s'", p_name(kx
->p
));
921 buf_init(b
, BBASE(&bb
), BLEN(&bb
));
922 if ((hswok
= buf_get(b
, algs
.hashsz
)) == 0 || BLEFT(b
)) {
923 a_warn("invalid switch confirmation from `%s'", p_name(kx
->p
));
926 IF_TRACING(T_KEYEXCH
, {
927 trace_block(T_CRYPTO
, "crypto: switch confirmation hash",
930 if (memcmp(hswok
, kxc
->hswok_in
, algs
.hashsz
) != 0) {
931 a_warn("incorrect switch confirmation hash from `%s'", p_name(kx
->p
));
934 if (kx
->s
< KXS_SWITCH
) {
935 ks_activate(kxc
->ks
);
936 settimer(kx
, ks_tregen(kxc
->ks
));
945 /*----- Main code ---------------------------------------------------------*/
949 * Arguments: @keyexch *kx@ = pointer to key exchange context
953 * Use: Stops a key exchange dead in its tracks. Throws away all of
954 * the context information. The context is left in an
955 * inconsistent state. The only functions which understand this
956 * state are @kx_free@ and @kx_init@ (which cause it internally
957 * it), and @start@ (which expects it to be the prevailing
961 static void stop(keyexch
*kx
)
965 if (kx
->f
& KXF_DEAD
)
968 if (kx
->f
& KXF_TIMER
)
970 for (i
= 0; i
< kx
->nr
; i
++)
971 kxc_destroy(kx
->r
[i
]);
973 G_DESTROY(gg
, kx
->c
);
974 G_DESTROY(gg
, kx
->rx
);
982 * Arguments: @keyexch *kx@ = pointer to key exchange context
983 * @time_t now@ = the current time
987 * Use: Starts a new key exchange with the peer. The context must be
988 * in the bizarre state left by @stop@ or @kx_init@.
991 static void start(keyexch
*kx
, time_t now
)
995 assert(kx
->f
& KXF_DEAD
);
999 kx
->alpha
= mprand_range(MP_NEW
, gg
->r
, &rand_global
, 0);
1000 kx
->c
= G_CREATE(gg
); G_EXP(gg
, kx
->c
, gg
->g
, kx
->alpha
);
1001 kx
->rx
= G_CREATE(gg
); G_EXP(gg
, kx
->rx
, kx
->kpub
, kx
->alpha
);
1003 kx
->t_valid
= now
+ T_VALID
;
1005 h
= GH_INIT(algs
.h
);
1006 HASH_STRING(h
, "tripe-cookie");
1011 IF_TRACING(T_KEYEXCH
, {
1012 trace(T_KEYEXCH
, "keyexch: creating new challenge");
1013 IF_TRACING(T_CRYPTO
, {
1014 trace(T_CRYPTO
, "crypto: secret = %s", mpstr(kx
->alpha
));
1015 trace(T_CRYPTO
, "crypto: challenge = %s", gestr(gg
, kx
->c
));
1016 trace(T_CRYPTO
, "crypto: expected response = %s", gestr(gg
, kx
->rx
));
1017 trace_block(T_CRYPTO
, "crypto: challenge cookie", kx
->hc
, algs
.hashsz
);
1022 /* --- @checkpub@ --- *
1024 * Arguments: @keyexch *kx@ = pointer to key exchange context
1026 * Returns: Zero if OK, nonzero if the peer's public key has expired.
1028 * Use: Deactivates the key-exchange until the peer acquires a new
1032 static int checkpub(keyexch
*kx
)
1035 if (kx
->f
& KXF_DEAD
)
1038 if (KEY_EXPIRED(now
, kx
->texp_kpub
)) {
1040 a_warn("public key for `%s' has expired", p_name(kx
->p
));
1041 G_COPY(gg
, kx
->kpub
, gg
->i
);
1042 kx
->f
&= ~KXF_PUBKEY
;
1048 /* --- @kx_start@ --- *
1050 * Arguments: @keyexch *kx@ = pointer to key exchange context
1054 * Use: Stimulates a key exchange. If a key exchage is in progress,
1055 * a new challenge is sent (unless the quiet timer forbids
1056 * this); if no exchange is in progress, one is commenced.
1059 void kx_start(keyexch
*kx
)
1061 time_t now
= time(0);
1065 if (!ISVALID(kx
, now
)) {
1072 /* --- @kx_message@ --- *
1074 * Arguments: @keyexch *kx@ = pointer to key exchange context
1075 * @unsigned msg@ = the message code
1076 * @buf *b@ = pointer to buffer containing the packet
1080 * Use: Reads a packet containing key exchange messages and handles
1084 void kx_message(keyexch
*kx
, unsigned msg
, buf
*b
)
1086 time_t now
= time(0);
1087 stats
*st
= p_stats(kx
->p
);
1092 static const char *const pkname
[] = {
1093 "prechallenge", "cookie", "challenge",
1094 "reply", "switch request", "switch confirmation"
1101 if (!ISVALID(kx
, now
)) {
1106 T( trace(T_KEYEXCH
, "keyexch: processing %s packet from `%s'",
1107 msg
< KX_NMSG ? pkname
[msg
] : "unknown", p_name(kx
->p
)); )
1113 rc
= dochallenge(kx
, msg
, b
);
1116 rc
= doreply(kx
, b
);
1119 rc
= doswitch(kx
, b
);
1122 rc
= doswitchok(kx
, b
);
1125 a_warn("unexpected key exchange message type %u from `%p'",
1139 /* --- @kx_free@ --- *
1141 * Arguments: @keyexch *kx@ = pointer to key exchange context
1145 * Use: Frees everything in a key exchange context.
1148 void kx_free(keyexch
*kx
)
1151 G_DESTROY(gg
, kx
->kpub
);
1154 /* --- @kx_newkeys@ --- *
1156 * Arguments: @keyexch *kx@ = pointer to key exchange context
1160 * Use: Informs the key exchange module that its keys may have
1161 * changed. If fetching the new keys fails, the peer will be
1162 * destroyed, we log messages and struggle along with the old
1166 void kx_newkeys(keyexch
*kx
)
1168 if (km_getpubkey(p_name(kx
->p
), kx
->kpub
, &kx
->texp_kpub
))
1170 kx
->f
|= KXF_PUBKEY
;
1171 if ((kx
->f
& KXF_DEAD
) || kx
->s
!= KXS_SWITCH
) {
1172 T( trace(T_KEYEXCH
, "keyexch: restarting key negotiation with `%s'",
1180 /* --- @kx_init@ --- *
1182 * Arguments: @keyexch *kx@ = pointer to key exchange context
1183 * @peer *p@ = pointer to peer context
1184 * @keyset **ks@ = pointer to keyset list
1186 * Returns: Zero if OK, nonzero if it failed.
1188 * Use: Initializes a key exchange module. The module currently
1189 * contains no keys, and will attempt to initiate a key
1193 int kx_init(keyexch
*kx
, peer
*p
, keyset
**ks
)
1197 kx
->kpub
= G_CREATE(gg
);
1198 if (km_getpubkey(p_name(p
), kx
->kpub
, &kx
->texp_kpub
)) {
1199 G_DESTROY(gg
, kx
->kpub
);
1202 kx
->f
= KXF_DEAD
| KXF_PUBKEY
;
1208 /*----- That's all, folks -------------------------------------------------*/