Commit | Line | Data |
---|---|---|
410c8acf | 1 | /* -*-c-*- |
410c8acf | 2 | * |
3 | * Key exchange protocol | |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
e04c2d50 | 8 | /*----- Licensing notice --------------------------------------------------* |
410c8acf | 9 | * |
10 | * This file is part of Trivial IP Encryption (TrIPE). | |
11 | * | |
11ad66c2 MW |
12 | * TrIPE is free software: you can redistribute it and/or modify it under |
13 | * the terms of the GNU General Public License as published by the Free | |
14 | * Software Foundation; either version 3 of the License, or (at your | |
15 | * option) any later version. | |
e04c2d50 | 16 | * |
11ad66c2 MW |
17 | * TrIPE is distributed in the hope that it will be useful, but WITHOUT |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
e04c2d50 | 21 | * |
410c8acf | 22 | * You should have received a copy of the GNU General Public License |
11ad66c2 | 23 | * along with TrIPE. If not, see <https://www.gnu.org/licenses/>. |
410c8acf | 24 | */ |
25 | ||
410c8acf | 26 | /*----- Header files ------------------------------------------------------*/ |
27 | ||
28 | #include "tripe.h" | |
29 | ||
737cc271 | 30 | /*----- Brief protocol overview -------------------------------------------* |
31 | * | |
32 | * Let %$G$% be a cyclic group; let %$g$% be a generator of %$G$%, and let | |
33 | * %$q$% be the order of %$G$%; for a key %$K$%, let %$E_K(\cdot)$% denote | |
34 | * application of the symmetric packet protocol to a message; let | |
35 | * %$H(\cdot)$% be the random oracle. Let $\alpha \inr \{0,\ldots,q - 1\}$% | |
36 | * be Alice's private key; let %$a = g^\alpha$% be her public key; let %$b$% | |
37 | * be Bob's public key. | |
38 | * | |
39 | * At the beginning of the session, Alice chooses | |
40 | * | |
41 | * %$\rho_A \inr \{0, \ldots q - 1\}$% | |
42 | * | |
43 | * We also have: | |
44 | * | |
45 | * %$r_A = g^{\rho_A}$% Alice's challenge | |
46 | * %$c_A = H(\cookie{cookie}, r_A)$% Alice's cookie | |
9317aa92 | 47 | * %$v_A = \rho_A \xor H(\cookie{expected-reply}, a, r_A, r_B, b^{\rho_A})$% |
e04c2d50 | 48 | * Alice's challenge check value |
737cc271 | 49 | * %$r_B^\alpha = a^{\rho_B}$% Alice's reply |
50 | * %$K = r_B^{\rho_A} = r_B^{\rho_A} = g^{\rho_A\rho_B}$% | |
e04c2d50 | 51 | * Alice and Bob's shared secret key |
737cc271 | 52 | * %$w_A = H(\cookie{switch-request}, c_A, c_B)$% |
e04c2d50 | 53 | * Alice's switch request value |
737cc271 | 54 | * %$u_A = H(\cookie{switch-confirm}, c_A, c_B)$% |
e04c2d50 | 55 | * Alice's switch confirm value |
737cc271 | 56 | * |
57 | * The messages are then: | |
58 | * | |
59 | * %$\cookie{kx-pre-challenge}, r_A$% | |
60 | * Initial greeting. In state @KXS_CHAL@. | |
61 | * | |
737cc271 | 62 | * %$\cookie{kx-challenge}, r_A, c_B, v_A$% |
63 | * Here's a full challenge for you to answer. | |
64 | * | |
28461f0e | 65 | * %$\cookie{kx-reply}, r_A, c_B, v_A, E_K(r_B^\alpha))$% |
737cc271 | 66 | * Challenge accpeted: here's the answer. Commit to my challenge. Move |
67 | * to @KXS_COMMIT@. | |
68 | * | |
3cdc3f3a | 69 | * %$\cookie{kx-switch-rq}, c_A, c_B, E_K(r_B^\alpha, w_A))$% |
737cc271 | 70 | * Reply received: here's my reply. Committed; send data; move to |
71 | * @KXS_SWITCH@. | |
72 | * | |
73 | * %$\cookie{kx-switch-ok}, E_K(u_A))$% | |
74 | * Switch received. Committed; send data; move to @KXS_SWITCH@. | |
e04c2d50 | 75 | */ |
737cc271 | 76 | |
3cdc3f3a | 77 | /*----- Static tables -----------------------------------------------------*/ |
78 | ||
79 | static const char *const pkname[] = { | |
c3c51798 | 80 | "pre-challenge", "challenge", "reply", "switch-rq", "switch-ok" |
3cdc3f3a | 81 | }; |
0617b6e7 | 82 | |
83 | /*----- Various utilities -------------------------------------------------*/ | |
410c8acf | 84 | |
e9fac70c MW |
85 | /* --- @VALIDP@ --- * |
86 | * | |
87 | * Arguments: @const keyexch *kx@ = key exchange state | |
88 | * @time_t now@ = current time in seconds | |
89 | * | |
90 | * Returns: Whether the challenge in the key-exchange state is still | |
91 | * valid or should be regenerated. | |
92 | */ | |
93 | ||
94 | #define VALIDP(kx, now) ((now) < (kx)->t_valid) | |
95 | ||
52c03a2a | 96 | /* --- @hashge@ --- * |
410c8acf | 97 | * |
b5c45da1 | 98 | * Arguments: @ghash *h@ = pointer to hash context |
5b9f3d37 MW |
99 | * @const dhgrp *g@ = pointer to group |
100 | * @const dhge *Y@ = pointer to group element | |
410c8acf | 101 | * |
102 | * Returns: --- | |
103 | * | |
52c03a2a | 104 | * Use: Adds the hash of a group element to the context. Corrupts |
105 | * @buf_t@. | |
410c8acf | 106 | */ |
107 | ||
5b9f3d37 | 108 | static void hashge(ghash *h, const dhgrp *g, const dhge *Y) |
410c8acf | 109 | { |
110 | buf b; | |
35c8b547 | 111 | |
0617b6e7 | 112 | buf_init(&b, buf_t, sizeof(buf_t)); |
5b9f3d37 | 113 | g->ops->stge(g, &b, Y, DHFMT_HASH); |
410c8acf | 114 | assert(BOK(&b)); |
b5c45da1 | 115 | GH_HASH(h, BBASE(&b), BLEN(&b)); |
410c8acf | 116 | } |
117 | ||
de7bd20b | 118 | /* --- @mpmask@ --- * |
5d418e24 | 119 | * |
de7bd20b | 120 | * Arguments: @buf *b@ = output buffer |
5b9f3d37 MW |
121 | * @const dhgrp *g@ = the group |
122 | * @const dhsc *x@ = the plaintext scalar | |
de7bd20b | 123 | * @size_t n@ = the expected size of the plaintext |
35c8b547 | 124 | * @gcipher *mgfc@ = mask-generating function to use |
5d418e24 | 125 | * @const octet *k@ = pointer to key material |
de7bd20b | 126 | * @size_t ksz@ = size of the key |
5d418e24 | 127 | * |
c13541b1 | 128 | * Returns: --- |
5d418e24 | 129 | * |
5b9f3d37 MW |
130 | * Use: Masks a scalar: returns %$x \xor H(k)$%, so it's a random |
131 | * oracle thing rather than an encryption thing. Breaks the | |
132 | * output buffer on error. | |
5d418e24 | 133 | */ |
134 | ||
5b9f3d37 | 135 | static void mpmask(buf *b, const dhgrp *g, const dhsc *x, size_t n, |
c13541b1 | 136 | const gccipher *mgfc, const octet *k, size_t ksz) |
5d418e24 | 137 | { |
b5c45da1 | 138 | gcipher *mgf; |
de7bd20b | 139 | octet *p; |
5d418e24 | 140 | |
c13541b1 | 141 | if ((p = buf_get(b, n)) == 0) return; |
35c8b547 | 142 | mgf = GC_INIT(mgfc, k, ksz); |
de7bd20b | 143 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
5b9f3d37 | 144 | trace(T_CRYPTO, "crypto: masking scalar = %s", g->ops->scstr(g, x)); |
61682d34 | 145 | trace_block(T_CRYPTO, "crypto: masking key", k, ksz); |
de7bd20b | 146 | })) |
5b9f3d37 | 147 | if (g->ops->stsc(g, buf_t, n, x)) { buf_break(b); return; } |
de7bd20b MW |
148 | GC_ENCRYPT(mgf, buf_t, p, n); |
149 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { | |
5b9f3d37 | 150 | trace_block(T_CRYPTO, "crypto: scalar plaintext", buf_t, n); |
61682d34 | 151 | trace_block(T_CRYPTO, "crypto: masked ciphertext", p, n); |
de7bd20b | 152 | })) |
b5c45da1 | 153 | GC_DESTROY(mgf); |
b5c45da1 | 154 | } |
155 | ||
de7bd20b MW |
156 | /* --- @mpunmask@ --- * |
157 | * | |
5b9f3d37 | 158 | * Arguments: @const dhgrp *g@ = the group |
de7bd20b MW |
159 | * @const octet *p@ = pointer to the ciphertext |
160 | * @size_t n@ = the size of the ciphertext | |
35c8b547 | 161 | * @gcipher *mgfc@ = mask-generating function to use |
de7bd20b MW |
162 | * @const octet *k@ = pointer to key material |
163 | * @size_t ksz@ = size of the key | |
164 | * | |
5b9f3d37 | 165 | * Returns: The decrypted scalar, or null. |
de7bd20b | 166 | * |
5b9f3d37 | 167 | * Use: Unmasks a scalar. |
de7bd20b MW |
168 | */ |
169 | ||
5b9f3d37 | 170 | static dhsc *mpunmask(const dhgrp *g, const octet *p, size_t n, |
76e91db9 | 171 | const gccipher *mgfc, const octet *k, size_t ksz) |
b5c45da1 | 172 | { |
173 | gcipher *mgf; | |
5b9f3d37 | 174 | dhsc *x; |
b5c45da1 | 175 | |
35c8b547 | 176 | mgf = GC_INIT(mgfc, k, ksz); |
de7bd20b | 177 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
61682d34 MW |
178 | trace_block(T_CRYPTO, "crypto: unmasking key", k, ksz); |
179 | trace_block(T_CRYPTO, "crypto: masked ciphertext", p, n); | |
de7bd20b MW |
180 | })) |
181 | GC_DECRYPT(mgf, p, buf_t, n); | |
5b9f3d37 | 182 | x = g->ops->ldsc(g, buf_t, n); |
de7bd20b | 183 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
5b9f3d37 MW |
184 | trace_block(T_CRYPTO, "crypto: scalar plaintext", buf_t, n); |
185 | trace(T_CRYPTO, "crypto: unmasked scalar = %s", | |
186 | x ? g->ops->scstr(g, x) : "<failed>"); | |
de7bd20b | 187 | })) |
b5c45da1 | 188 | GC_DESTROY(mgf); |
5b9f3d37 | 189 | return (x); |
de7bd20b MW |
190 | } |
191 | ||
192 | /* --- @hashcheck@ --- * | |
193 | * | |
35c8b547 | 194 | * Arguments: @keyexch *kx@ = pointer to key-exchange block |
5b9f3d37 MW |
195 | * @const dhge *K@ = sender's public key |
196 | * @const dhge *CC@ = receiver's challenge | |
197 | * @const dhge *C@ = sender's challenge | |
198 | * @const dhge *Y@ = reply to sender's challenge | |
de7bd20b MW |
199 | * |
200 | * Returns: Pointer to the hash value (in @buf_t@) | |
201 | * | |
202 | * Use: Computes the check-value hash, used to mask or unmask | |
203 | * indices to prove the validity of challenges. This computes | |
204 | * the masking key used in challenge check values. This is | |
205 | * really the heart of the whole thing, since it ensures that | |
5b9f3d37 | 206 | * the scalar can be recovered from the history of hashing |
de7bd20b MW |
207 | * queries, which gives us (a) a proof that the authentication |
208 | * process is zero-knowledge, and (b) a proof that the whole | |
209 | * key-exchange is deniable. | |
210 | */ | |
211 | ||
5b9f3d37 MW |
212 | static const octet *hashcheck(keyexch *kx, const dhge *K, |
213 | const dhge *CC, const dhge *C, const dhge *Y) | |
de7bd20b | 214 | { |
35c8b547 | 215 | ghash *h = GH_INIT(kx->kpriv->algs.h); |
5b9f3d37 | 216 | const dhgrp *g = kx->kpriv->grp; |
de7bd20b MW |
217 | |
218 | HASH_STRING(h, "tripe-expected-reply"); | |
5b9f3d37 MW |
219 | hashge(h, g, K); |
220 | hashge(h, g, CC); | |
221 | hashge(h, g, C); | |
222 | hashge(h, g, Y); | |
de7bd20b MW |
223 | GH_DONE(h, buf_t); |
224 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { | |
61682d34 | 225 | trace(T_CRYPTO, "crypto: computing challenge check hash"); |
5b9f3d37 MW |
226 | trace(T_CRYPTO, "crypto: public key = %s", g->ops->gestr(g, K)); |
227 | trace(T_CRYPTO, "crypto: receiver challenge = %s", g->ops->gestr(g, CC)); | |
228 | trace(T_CRYPTO, "crypto: sender challenge = %s", g->ops->gestr(g, C)); | |
229 | trace(T_CRYPTO, "crypto: sender reply = %s", g->ops->gestr(g, Y)); | |
35c8b547 | 230 | trace_block(T_CRYPTO, "crypto: hash output", buf_t, kx->kpriv->algs.hashsz); |
de7bd20b MW |
231 | })) |
232 | GH_DESTROY(h); | |
233 | return (buf_t); | |
234 | } | |
235 | ||
236 | /* --- @sendchallenge@ --- * | |
237 | * | |
238 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
239 | * @buf *b@ = output buffer for challenge | |
5b9f3d37 | 240 | * @const dhge *C@ = peer's actual challenge |
de7bd20b MW |
241 | * @const octet *hc@ = peer's challenge cookie |
242 | * | |
243 | * Returns: --- | |
244 | * | |
245 | * Use: Writes a full challenge to the message buffer. | |
246 | */ | |
247 | ||
5b9f3d37 MW |
248 | static void sendchallenge(keyexch *kx, buf *b, |
249 | const dhge *C, const octet *hc) | |
de7bd20b | 250 | { |
5b9f3d37 MW |
251 | const dhgrp *g = kx->kpriv->grp; |
252 | g->ops->stge(g, b, kx->C, DHFMT_VAR); | |
35c8b547 | 253 | buf_put(b, hc, kx->kpriv->algs.hashsz); |
5b9f3d37 MW |
254 | mpmask(b, g, kx->a, g->scsz, kx->kpriv->algs.mgf, |
255 | hashcheck(kx, kx->kpriv->K, C, kx->C, kx->RX), | |
35c8b547 | 256 | kx->kpriv->algs.hashsz); |
5d418e24 | 257 | } |
258 | ||
410c8acf | 259 | /* --- @timer@ --- * |
260 | * | |
261 | * Arguments: @struct timeval *tv@ = the current time | |
262 | * @void *v@ = pointer to key exchange context | |
263 | * | |
264 | * Returns: --- | |
265 | * | |
266 | * Use: Acts when the key exchange timer goes off. | |
267 | */ | |
268 | ||
269 | static void timer(struct timeval *tv, void *v) | |
270 | { | |
271 | keyexch *kx = v; | |
272 | kx->f &= ~KXF_TIMER; | |
273 | T( trace(T_KEYEXCH, "keyexch: timer has popped"); ) | |
de014da6 | 274 | kx_start(kx, 0); |
410c8acf | 275 | } |
276 | ||
277 | /* --- @settimer@ --- * | |
278 | * | |
279 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
ff143952 | 280 | * @struct timeval *tv@ = when to set the timer for |
410c8acf | 281 | * |
282 | * Returns: --- | |
283 | * | |
284 | * Use: Sets the timer for the next key exchange attempt. | |
285 | */ | |
286 | ||
ff143952 | 287 | static void settimer(keyexch *kx, struct timeval *tv) |
410c8acf | 288 | { |
ff143952 MW |
289 | if (kx->f & KXF_TIMER) sel_rmtimer(&kx->t); |
290 | sel_addtimer(&sel, &kx->t, tv, timer, kx); | |
410c8acf | 291 | kx->f |= KXF_TIMER; |
292 | } | |
293 | ||
a06d57a3 MW |
294 | /* --- @f2tv@ --- * |
295 | * | |
296 | * Arguments: @struct timeval *tv@ = where to write the timeval | |
297 | * @double t@ = a time as a floating point number | |
298 | * | |
299 | * Returns: --- | |
300 | * | |
301 | * Use: Converts a floating-point time into a timeval. | |
302 | */ | |
303 | ||
304 | static void f2tv(struct timeval *tv, double t) | |
305 | { | |
306 | tv->tv_sec = t; | |
307 | tv->tv_usec = (t - tv->tv_sec)*MILLION; | |
308 | } | |
309 | ||
310 | /* --- @wobble@ --- * | |
311 | * | |
312 | * Arguments: @double t@ = a time interval | |
313 | * | |
314 | * Returns: The same time interval, with a random error applied. | |
315 | */ | |
316 | ||
317 | static double wobble(double t) | |
318 | { | |
319 | uint32 r = rand_global.ops->word(&rand_global); | |
320 | double w = (r/F_2P32) - 0.5; | |
321 | return (t + t*w*T_WOBBLE); | |
322 | } | |
323 | ||
324 | /* --- @rs_time@ --- * | |
325 | * | |
326 | * Arguments: @retry *rs@ = current retry state | |
327 | * @struct timeval *tv@ = where to write the result | |
328 | * @const struct timeval *now@ = current time, or null | |
329 | * | |
330 | * Returns: --- | |
331 | * | |
332 | * Use: Computes a time at which to retry sending a key-exchange | |
333 | * packet. This algorithm is subject to change, but it's | |
334 | * currently a capped exponential backoff, slightly randomized | |
335 | * to try to keep clients from hammering a server that's only | |
336 | * just woken up. | |
337 | * | |
338 | * If @now@ is null then the function works out the time for | |
339 | * itself. | |
340 | */ | |
341 | ||
342 | static void rs_time(retry *rs, struct timeval *tv, const struct timeval *now) | |
343 | { | |
344 | double t; | |
345 | struct timeval rtv; | |
346 | ||
347 | if (!rs->t) | |
348 | t = SEC(2); | |
349 | else { | |
350 | t = (rs->t * 5)/4; | |
351 | if (t > MIN(5)) t = MIN(5); | |
352 | } | |
353 | rs->t = t; | |
354 | ||
355 | if (!now) { | |
356 | now = tv; | |
357 | gettimeofday(tv, 0); | |
358 | } | |
359 | f2tv(&rtv, wobble(t)); | |
360 | TV_ADD(tv, now, &rtv); | |
361 | } | |
362 | ||
363 | /* --- @retry_reset@ --- * | |
364 | * | |
365 | * Arguments: @retry *rs@ = retry state | |
366 | * | |
367 | * Returns: -- | |
368 | * | |
369 | * Use: Resets a retry state to indicate that progress has been | |
370 | * made. Also useful for initializing the state in the first | |
371 | * place. | |
372 | */ | |
373 | ||
374 | static void rs_reset(retry *rs) { rs->t = 0; } | |
375 | ||
664084ee MW |
376 | /* --- @notice_message@ --- * |
377 | * | |
378 | * Arguments: @keyexch *kx@ = pointer to key-exchange block | |
379 | * | |
380 | * Returns: Zero if OK; @-1@ if the public key is in a bad state. | |
381 | * | |
382 | * Use: Updates the key-exchange state following a received message. | |
383 | * Specifically, if there's no currently active key-exchange in | |
384 | * progress, and we're not in the cooling-off period, then | |
385 | * commence a new one; reset the retry timers; and if we're | |
386 | * corked then pop the cork so that we can reply. | |
387 | */ | |
388 | ||
389 | static int checkpub(keyexch *kx); | |
390 | static void stop(keyexch *kx); | |
391 | static void start(keyexch *kx, time_t now); | |
392 | ||
393 | static int notice_message(keyexch *kx) | |
394 | { | |
395 | struct timeval now, tv; | |
396 | ||
397 | gettimeofday(&now, 0); | |
398 | rs_reset(&kx->rs); | |
399 | if (kx->f & KXF_CORK) { | |
400 | start(kx, now.tv_sec); | |
401 | rs_time(&kx->rs, &tv, &now); | |
402 | settimer(kx, &tv); | |
403 | a_notify("KXSTART", "?PEER", kx->p, A_END); | |
404 | } | |
405 | if (checkpub(kx)) return (-1); | |
406 | if (!VALIDP(kx, now.tv_sec)) { | |
407 | stop(kx); | |
408 | start(kx, now.tv_sec); | |
409 | } | |
410 | return (0); | |
411 | } | |
412 | ||
413 | /* --- @update_stats_tx@, @update_stats_rx@ --- * | |
414 | * | |
415 | * Arguments: @keyexch *kx@ = pointer to key-exchange block | |
416 | * @int ok@ = nonzero if the message was valid (for @rx@) | |
417 | * @size_t sz@ = size of sent message | |
418 | * | |
419 | * Returns: --- | |
420 | * | |
421 | * Use: Records that a key-exchange message was sent to, or received | |
422 | * from, the peer. | |
423 | */ | |
424 | ||
425 | static void update_stats_tx(keyexch *kx, size_t sz) | |
426 | { stats *st = p_stats(kx->p); st->n_kxout++; st->sz_kxout += sz; } | |
427 | ||
428 | static void update_stats_rx(keyexch *kx, int ok, size_t sz) | |
429 | { | |
430 | stats *st = p_stats(kx->p); | |
431 | ||
432 | if (!ok) st->n_reject++; | |
433 | else { st->n_kxin++; st->sz_kxin += sz; } | |
434 | } | |
435 | ||
0617b6e7 | 436 | /*----- Challenge management ----------------------------------------------*/ |
437 | ||
438 | /* --- Notes on challenge management --- * | |
410c8acf | 439 | * |
0617b6e7 | 440 | * We may get multiple different replies to our key exchange; some will be |
441 | * correct, some inserted by attackers. Up until @KX_THRESH@, all challenges | |
442 | * received will be added to the table and given a full response. After | |
443 | * @KX_THRESH@ distinct challenges are received, we return only a `cookie': | |
444 | * our existing challenge, followed by a hash of the sender's challenge. We | |
445 | * do %%\emph{not}%% give a bare challenge a reply slot at this stage. All | |
446 | * properly-formed cookies are assigned a table slot: if none is spare, a | |
447 | * used slot is randomly selected and destroyed. A cookie always receives a | |
448 | * full reply. | |
449 | */ | |
450 | ||
451 | /* --- @kxc_destroy@ --- * | |
452 | * | |
453 | * Arguments: @kxchal *kxc@ = pointer to the challenge block | |
410c8acf | 454 | * |
455 | * Returns: --- | |
456 | * | |
0617b6e7 | 457 | * Use: Disposes of a challenge block. |
410c8acf | 458 | */ |
459 | ||
0617b6e7 | 460 | static void kxc_destroy(kxchal *kxc) |
410c8acf | 461 | { |
5b9f3d37 | 462 | const dhgrp *g = kxc->kx->kpriv->grp; |
0617b6e7 | 463 | if (kxc->f & KXF_TIMER) |
464 | sel_rmtimer(&kxc->t); | |
5b9f3d37 MW |
465 | g->ops->freege(g, kxc->C); |
466 | g->ops->freege(g, kxc->R); | |
0617b6e7 | 467 | ks_drop(kxc->ks); |
468 | DESTROY(kxc); | |
469 | } | |
410c8acf | 470 | |
0617b6e7 | 471 | /* --- @kxc_stoptimer@ --- * |
472 | * | |
473 | * Arguments: @kxchal *kxc@ = pointer to the challenge block | |
474 | * | |
475 | * Returns: --- | |
476 | * | |
477 | * Use: Stops the challenge's retry timer from sending messages. | |
478 | * Useful when the state machine is in the endgame of the | |
479 | * exchange. | |
480 | */ | |
410c8acf | 481 | |
0617b6e7 | 482 | static void kxc_stoptimer(kxchal *kxc) |
483 | { | |
484 | if (kxc->f & KXF_TIMER) | |
485 | sel_rmtimer(&kxc->t); | |
2de0ad0f | 486 | kxc->f &= ~KXF_TIMER; |
0617b6e7 | 487 | } |
410c8acf | 488 | |
0617b6e7 | 489 | /* --- @kxc_new@ --- * |
490 | * | |
491 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
0617b6e7 | 492 | * |
493 | * Returns: A pointer to the challenge block. | |
494 | * | |
495 | * Use: Returns a pointer to a new challenge block to fill in. | |
705ecf30 MW |
496 | * In particular, the @c@ and @r@ members are left |
497 | * uninitialized. | |
0617b6e7 | 498 | */ |
410c8acf | 499 | |
0617b6e7 | 500 | static kxchal *kxc_new(keyexch *kx) |
501 | { | |
502 | kxchal *kxc; | |
503 | unsigned i; | |
504 | ||
505 | /* --- If we're over reply threshold, discard one at random --- */ | |
506 | ||
507 | if (kx->nr < KX_NCHAL) | |
508 | i = kx->nr++; | |
509 | else { | |
510 | i = rand_global.ops->range(&rand_global, KX_NCHAL); | |
511 | kxc_destroy(kx->r[i]); | |
410c8acf | 512 | } |
513 | ||
0617b6e7 | 514 | /* --- Fill in the new structure --- */ |
410c8acf | 515 | |
0617b6e7 | 516 | kxc = CREATE(kxchal); |
0617b6e7 | 517 | kxc->ks = 0; |
518 | kxc->kx = kx; | |
519 | kxc->f = 0; | |
520 | kx->r[i] = kxc; | |
a06d57a3 | 521 | rs_reset(&kxc->rs); |
0617b6e7 | 522 | return (kxc); |
523 | } | |
410c8acf | 524 | |
0617b6e7 | 525 | /* --- @kxc_bychal@ --- * |
526 | * | |
527 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
5b9f3d37 | 528 | * @const dhge *C@ = challenge from remote host |
0617b6e7 | 529 | * |
530 | * Returns: Pointer to the challenge block, or null. | |
531 | * | |
532 | * Use: Finds a challenge block, given its challenge. | |
533 | */ | |
534 | ||
5b9f3d37 | 535 | static kxchal *kxc_bychal(keyexch *kx, const dhge *C) |
0617b6e7 | 536 | { |
5b9f3d37 | 537 | const dhgrp *g = kx->kpriv->grp; |
0617b6e7 | 538 | unsigned i; |
539 | ||
540 | for (i = 0; i < kx->nr; i++) { | |
5b9f3d37 | 541 | if (g->ops->eq(g, C, kx->r[i]->C)) |
0617b6e7 | 542 | return (kx->r[i]); |
543 | } | |
544 | return (0); | |
545 | } | |
546 | ||
547 | /* --- @kxc_byhc@ --- * | |
548 | * | |
549 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
550 | * @const octet *hc@ = challenge hash from remote host | |
551 | * | |
552 | * Returns: Pointer to the challenge block, or null. | |
553 | * | |
554 | * Use: Finds a challenge block, given a hash of its challenge. | |
555 | */ | |
410c8acf | 556 | |
0617b6e7 | 557 | static kxchal *kxc_byhc(keyexch *kx, const octet *hc) |
558 | { | |
559 | unsigned i; | |
560 | ||
561 | for (i = 0; i < kx->nr; i++) { | |
35c8b547 | 562 | if (memcmp(hc, kx->r[i]->hc, kx->kpriv->algs.hashsz) == 0) |
0617b6e7 | 563 | return (kx->r[i]); |
410c8acf | 564 | } |
0617b6e7 | 565 | return (0); |
566 | } | |
567 | ||
568 | /* --- @kxc_answer@ --- * | |
569 | * | |
570 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
571 | * @kxchal *kxc@ = pointer to challenge block | |
572 | * | |
573 | * Returns: --- | |
574 | * | |
575 | * Use: Sends a reply to the remote host, according to the data in | |
576 | * this challenge block. | |
577 | */ | |
578 | ||
579 | static void kxc_answer(keyexch *kx, kxchal *kxc); | |
580 | ||
581 | static void kxc_timer(struct timeval *tv, void *v) | |
582 | { | |
583 | kxchal *kxc = v; | |
584 | kxc->f &= ~KXF_TIMER; | |
585 | kxc_answer(kxc->kx, kxc); | |
586 | } | |
587 | ||
588 | static void kxc_answer(keyexch *kx, kxchal *kxc) | |
589 | { | |
de7bd20b | 590 | buf *b = p_txstart(kx->p, MSG_KEYEXCH | KX_REPLY); |
5b9f3d37 | 591 | const dhgrp *g = kx->kpriv->grp; |
0617b6e7 | 592 | struct timeval tv; |
593 | buf bb; | |
594 | ||
595 | /* --- Build the reply packet --- */ | |
596 | ||
de7bd20b | 597 | T( trace(T_KEYEXCH, "keyexch: sending reply to `%s'", p_name(kx->p)); ) |
5b9f3d37 | 598 | sendchallenge(kx, b, kxc->C, kxc->hc); |
de7bd20b | 599 | buf_init(&bb, buf_i, sizeof(buf_i)); |
5b9f3d37 | 600 | g->ops->stge(g, &bb, kxc->R, DHFMT_STD); |
de7bd20b MW |
601 | buf_flip(&bb); |
602 | ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_REPLY, &bb, b); | |
0617b6e7 | 603 | |
604 | /* --- Update the statistics --- */ | |
605 | ||
606 | if (BOK(b)) { | |
664084ee | 607 | update_stats_tx(kx, BLEN(b)); |
0617b6e7 | 608 | p_txend(kx->p); |
609 | } | |
610 | ||
611 | /* --- Schedule another resend --- */ | |
612 | ||
613 | if (kxc->f & KXF_TIMER) | |
614 | sel_rmtimer(&kxc->t); | |
615 | gettimeofday(&tv, 0); | |
a06d57a3 | 616 | rs_time(&kxc->rs, &tv, &tv); |
0617b6e7 | 617 | sel_addtimer(&sel, &kxc->t, &tv, kxc_timer, kxc); |
618 | kxc->f |= KXF_TIMER; | |
619 | } | |
620 | ||
621 | /*----- Individual message handlers ---------------------------------------*/ | |
622 | ||
de7bd20b | 623 | /* --- @doprechallenge@ --- * |
0617b6e7 | 624 | * |
de7bd20b MW |
625 | * Arguments: @keyexch *kx@ = pointer to key exchange block |
626 | * @buf *b@ = buffer containing the packet | |
0617b6e7 | 627 | * |
de7bd20b | 628 | * Returns: Zero if OK, nonzero of the packet was rejected. |
0617b6e7 | 629 | * |
de7bd20b | 630 | * Use: Processes a pre-challenge message. |
0617b6e7 | 631 | */ |
632 | ||
de7bd20b | 633 | static int doprechallenge(keyexch *kx, buf *b) |
0617b6e7 | 634 | { |
5b9f3d37 MW |
635 | const dhgrp *g = kx->kpriv->grp; |
636 | dhge *C = 0; | |
b5c45da1 | 637 | ghash *h; |
0617b6e7 | 638 | |
de7bd20b MW |
639 | /* --- Ensure that we're in a sensible state --- */ |
640 | ||
641 | if (kx->s != KXS_CHAL) { | |
642 | a_warn("KX", "?PEER", kx->p, "unexpected", "pre-challenge", A_END); | |
643 | goto bad; | |
644 | } | |
645 | ||
646 | /* --- Unpack the packet --- */ | |
647 | ||
5b9f3d37 | 648 | if ((C = g->ops->ldge(g, b, DHFMT_VAR)) == 0 || BLEFT(b)) |
de7bd20b | 649 | goto bad; |
b5c45da1 | 650 | |
0617b6e7 | 651 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
5b9f3d37 | 652 | trace(T_CRYPTO, "crypto: challenge = %s", g->ops->gestr(g, C)); |
0617b6e7 | 653 | })) |
de7bd20b MW |
654 | |
655 | /* --- Send out a full challenge by return --- */ | |
656 | ||
657 | b = p_txstart(kx->p, MSG_KEYEXCH | KX_CHAL); | |
35c8b547 | 658 | h = GH_INIT(kx->kpriv->algs.h); |
de7bd20b | 659 | HASH_STRING(h, "tripe-cookie"); |
5b9f3d37 MW |
660 | hashge(h, g, C); |
661 | sendchallenge(kx, b, C, GH_DONE(h, 0)); | |
b5c45da1 | 662 | GH_DESTROY(h); |
664084ee | 663 | update_stats_tx(kx, BLEN(b)); |
de7bd20b MW |
664 | p_txend(kx->p); |
665 | ||
666 | /* --- Done --- */ | |
667 | ||
5b9f3d37 | 668 | g->ops->freege(g, C); |
de7bd20b MW |
669 | return (0); |
670 | ||
671 | bad: | |
5b9f3d37 | 672 | if (C) g->ops->freege(g, C); |
de7bd20b | 673 | return (-1); |
0617b6e7 | 674 | } |
675 | ||
de7bd20b | 676 | /* --- @respond@ --- * |
0617b6e7 | 677 | * |
678 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
de7bd20b | 679 | * @unsigned msg@ = message code for this packet |
0617b6e7 | 680 | * @buf *b@ = buffer containing the packet |
681 | * | |
de7bd20b | 682 | * Returns: Key-exchange challenge block, or null. |
0617b6e7 | 683 | * |
de7bd20b MW |
684 | * Use: Computes a response for the given challenge, entering it into |
685 | * a challenge block and so on. | |
0617b6e7 | 686 | */ |
687 | ||
de7bd20b | 688 | static kxchal *respond(keyexch *kx, unsigned msg, buf *b) |
0617b6e7 | 689 | { |
5b9f3d37 | 690 | const dhgrp *g = kx->kpriv->grp; |
35c8b547 | 691 | const algswitch *algs = &kx->kpriv->algs; |
5b9f3d37 MW |
692 | size_t ixsz = g->scsz; |
693 | dhge *C = 0; | |
694 | dhge *R = 0; | |
695 | dhge *CC = 0; | |
ef09dae1 | 696 | deriveargs a; |
de7bd20b | 697 | const octet *hc, *ck; |
5b9f3d37 | 698 | dhsc *c = 0; |
0617b6e7 | 699 | kxchal *kxc; |
de7bd20b MW |
700 | ghash *h = 0; |
701 | buf bb; | |
702 | int ok; | |
0617b6e7 | 703 | |
704 | /* --- Unpack the packet --- */ | |
705 | ||
5b9f3d37 | 706 | if ((C = g->ops->ldge(g, b, DHFMT_VAR)) == 0 || |
35c8b547 MW |
707 | (hc = buf_get(b, algs->hashsz)) == 0 || |
708 | (ck = buf_get(b, ixsz)) == 0) { | |
f43df819 | 709 | a_warn("KX", "?PEER", kx->p, "invalid", "%s", pkname[msg], A_END); |
0617b6e7 | 710 | goto bad; |
711 | } | |
0617b6e7 | 712 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
5b9f3d37 | 713 | trace(T_CRYPTO, "crypto: challenge = %s", g->ops->gestr(g, C)); |
35c8b547 MW |
714 | trace_block(T_CRYPTO, "crypto: cookie", hc, algs->hashsz); |
715 | trace_block(T_CRYPTO, "crypto: check-value", ck, ixsz); | |
0617b6e7 | 716 | })) |
717 | ||
0617b6e7 | 718 | /* --- Discard a packet with an invalid cookie --- */ |
719 | ||
35c8b547 | 720 | if (hc && memcmp(hc, kx->hc, algs->hashsz) != 0) { |
5ac9463b | 721 | a_warn("KX", "?PEER", kx->p, "incorrect", "cookie", A_END); |
0617b6e7 | 722 | goto bad; |
723 | } | |
724 | ||
de7bd20b MW |
725 | /* --- Recover the check value and verify it --- * |
726 | * | |
727 | * To avoid recomputation on replays, we store a hash of the `right' | |
728 | * value. The `correct' value is unique, so this is right. | |
410c8acf | 729 | * |
de7bd20b | 730 | * This will also find a challenge block and, if necessary, populate it. |
410c8acf | 731 | */ |
732 | ||
5b9f3d37 | 733 | if ((kxc = kxc_bychal(kx, C)) != 0) { |
35c8b547 | 734 | h = GH_INIT(algs->h); |
de7bd20b | 735 | HASH_STRING(h, "tripe-check-hash"); |
35c8b547 MW |
736 | GH_HASH(h, ck, ixsz); |
737 | ok = !memcmp(kxc->ck, GH_DONE(h, 0), algs->hashsz); | |
de7bd20b MW |
738 | GH_DESTROY(h); |
739 | if (!ok) goto badcheck; | |
740 | } else { | |
741 | ||
742 | /* --- Compute the reply, and check the magic --- */ | |
743 | ||
5b9f3d37 MW |
744 | R = g->ops->mul(g, kx->kpriv->k, C); |
745 | if ((c = mpunmask(g, ck, ixsz, algs->mgf, | |
746 | hashcheck(kx, kx->kpub->K, kx->C, C, R), | |
747 | algs->hashsz)) == 0) | |
c13541b1 | 748 | goto badcheck; |
de7bd20b | 749 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
5b9f3d37 MW |
750 | trace(T_CRYPTO, "crypto: computed reply = %s", g->ops->gestr(g, R)); |
751 | trace(T_CRYPTO, "crypto: recovered log = %s", g->ops->scstr(g, c)); | |
de7bd20b | 752 | })) |
5b9f3d37 MW |
753 | CC = g->ops->mul(g, c, 0); |
754 | if (!g->ops->eq(g, CC, C)) goto badcheck; | |
de7bd20b MW |
755 | |
756 | /* --- Fill in a new challenge block --- */ | |
e04c2d50 | 757 | |
de7bd20b | 758 | kxc = kxc_new(kx); |
5b9f3d37 MW |
759 | kxc->C = C; C = 0; |
760 | kxc->R = R; R = 0; | |
0617b6e7 | 761 | |
35c8b547 MW |
762 | h = GH_INIT(algs->h); HASH_STRING(h, "tripe-check-hash"); |
763 | GH_HASH(h, ck, ixsz); | |
764 | GH_DONE(h, kxc->ck); GH_DESTROY(h); | |
0617b6e7 | 765 | |
35c8b547 | 766 | h = GH_INIT(algs->h); HASH_STRING(h, "tripe-cookie"); |
5b9f3d37 | 767 | hashge(h, g, kxc->C); |
35c8b547 | 768 | GH_DONE(h, kxc->hc); GH_DESTROY(h); |
b5c45da1 | 769 | |
770 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { | |
35c8b547 MW |
771 | trace_block(T_CRYPTO, "crypto: computed cookie", |
772 | kxc->hc, algs->hashsz); | |
b5c45da1 | 773 | })) |
0617b6e7 | 774 | |
0617b6e7 | 775 | /* --- Work out the shared key --- */ |
776 | ||
5b9f3d37 | 777 | R = g->ops->mul(g, kx->a, kxc->C); |
b5c45da1 | 778 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
5b9f3d37 | 779 | trace(T_CRYPTO, "crypto: shared secret = %s", g->ops->gestr(g, R)); |
b5c45da1 | 780 | })) |
0617b6e7 | 781 | |
782 | /* --- Compute the switch messages --- */ | |
783 | ||
35c8b547 | 784 | h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-request"); |
5b9f3d37 | 785 | hashge(h, g, kx->C); hashge(h, g, kxc->C); |
b5c45da1 | 786 | GH_DONE(h, kxc->hswrq_out); GH_DESTROY(h); |
35c8b547 | 787 | h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-confirm"); |
5b9f3d37 | 788 | hashge(h, g, kx->C); hashge(h, g, kxc->C); |
b5c45da1 | 789 | GH_DONE(h, kxc->hswok_out); GH_DESTROY(h); |
0617b6e7 | 790 | |
35c8b547 | 791 | h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-request"); |
5b9f3d37 | 792 | hashge(h, g, kxc->C); hashge(h, g, kx->C); |
b5c45da1 | 793 | GH_DONE(h, kxc->hswrq_in); GH_DESTROY(h); |
35c8b547 | 794 | h = GH_INIT(algs->h); HASH_STRING(h, "tripe-switch-confirm"); |
5b9f3d37 | 795 | hashge(h, g, kxc->C); hashge(h, g, kx->C); |
b5c45da1 | 796 | GH_DONE(h, kxc->hswok_in); GH_DESTROY(h); |
0617b6e7 | 797 | |
798 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { | |
0617b6e7 | 799 | trace_block(T_CRYPTO, "crypto: outbound switch request", |
35c8b547 | 800 | kxc->hswrq_out, algs->hashsz); |
0617b6e7 | 801 | trace_block(T_CRYPTO, "crypto: outbound switch confirm", |
35c8b547 | 802 | kxc->hswok_out, algs->hashsz); |
0617b6e7 | 803 | trace_block(T_CRYPTO, "crypto: inbound switch request", |
35c8b547 | 804 | kxc->hswrq_in, algs->hashsz); |
0617b6e7 | 805 | trace_block(T_CRYPTO, "crypto: inbound switch confirm", |
35c8b547 | 806 | kxc->hswok_in, algs->hashsz); |
0617b6e7 | 807 | })) |
808 | ||
809 | /* --- Create a new symmetric keyset --- */ | |
810 | ||
ef09dae1 MW |
811 | buf_init(&bb, buf_o, sizeof(buf_o)); a.k = BBASE(&bb); |
812 | g->ops->stge(g, &bb, kx->C, DHFMT_HASH); a.x = BLEN(&bb); | |
813 | g->ops->stge(g, &bb, kxc->C, DHFMT_HASH); a.y = BLEN(&bb); | |
814 | g->ops->stge(g, &bb, R, DHFMT_HASH); a.z = BLEN(&bb); | |
de7bd20b | 815 | assert(BOK(&bb)); |
0617b6e7 | 816 | |
ef09dae1 | 817 | kxc->ks = ks_gen(&a, kx->p); |
410c8acf | 818 | } |
819 | ||
5b9f3d37 MW |
820 | if (C) g->ops->freege(g, C); |
821 | if (CC) g->ops->freege(g, CC); | |
822 | if (R) g->ops->freege(g, R); | |
823 | if (c) g->ops->freesc(g, c); | |
de7bd20b | 824 | return (kxc); |
410c8acf | 825 | |
de7bd20b MW |
826 | badcheck: |
827 | a_warn("KX", "?PEER", kx->p, "bad-expected-reply-log", A_END); | |
828 | goto bad; | |
829 | bad: | |
5b9f3d37 MW |
830 | if (C) g->ops->freege(g, C); |
831 | if (CC) g->ops->freege(g, CC); | |
832 | if (R) g->ops->freege(g, R); | |
833 | if (c) g->ops->freesc(g, c); | |
e04c2d50 | 834 | return (0); |
de7bd20b | 835 | } |
0617b6e7 | 836 | |
de7bd20b MW |
837 | /* --- @dochallenge@ --- * |
838 | * | |
839 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
840 | * @unsigned msg@ = message code for the packet | |
841 | * @buf *b@ = buffer containing the packet | |
842 | * | |
843 | * Returns: Zero if OK, nonzero if the packet was rejected. | |
844 | * | |
845 | * Use: Processes a packet containing a challenge. | |
846 | */ | |
0617b6e7 | 847 | |
de7bd20b MW |
848 | static int dochallenge(keyexch *kx, buf *b) |
849 | { | |
850 | kxchal *kxc; | |
0617b6e7 | 851 | |
de7bd20b MW |
852 | if (kx->s != KXS_CHAL) { |
853 | a_warn("KX", "?PEER", kx->p, "unexpected", "challenge", A_END); | |
854 | goto bad; | |
855 | } | |
856 | if ((kxc = respond(kx, KX_CHAL, b)) == 0) | |
857 | goto bad; | |
858 | if (BLEFT(b)) { | |
859 | a_warn("KX", "?PEER", kx->p, "invalid", "challenge", A_END); | |
860 | goto bad; | |
861 | } | |
862 | kxc_answer(kx, kxc); | |
0617b6e7 | 863 | return (0); |
864 | ||
865 | bad: | |
0617b6e7 | 866 | return (-1); |
410c8acf | 867 | } |
868 | ||
0617b6e7 | 869 | /* --- @resend@ --- * |
410c8acf | 870 | * |
871 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
410c8acf | 872 | * |
873 | * Returns: --- | |
874 | * | |
0617b6e7 | 875 | * Use: Sends the next message for a key exchange. |
410c8acf | 876 | */ |
877 | ||
0617b6e7 | 878 | static void resend(keyexch *kx) |
410c8acf | 879 | { |
0617b6e7 | 880 | kxchal *kxc; |
881 | buf bb; | |
ff143952 | 882 | struct timeval tv; |
5b9f3d37 | 883 | const dhgrp *g = kx->kpriv->grp; |
410c8acf | 884 | buf *b; |
885 | ||
0617b6e7 | 886 | switch (kx->s) { |
887 | case KXS_CHAL: | |
00e64b67 | 888 | T( trace(T_KEYEXCH, "keyexch: sending prechallenge to `%s'", |
889 | p_name(kx->p)); ) | |
0617b6e7 | 890 | b = p_txstart(kx->p, MSG_KEYEXCH | KX_PRECHAL); |
5b9f3d37 | 891 | g->ops->stge(g, b, kx->C, DHFMT_VAR); |
0617b6e7 | 892 | break; |
893 | case KXS_COMMIT: | |
00e64b67 | 894 | T( trace(T_KEYEXCH, "keyexch: sending switch request to `%s'", |
895 | p_name(kx->p)); ) | |
0617b6e7 | 896 | kxc = kx->r[0]; |
897 | b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCH); | |
35c8b547 MW |
898 | buf_put(b, kx->hc, kx->kpriv->algs.hashsz); |
899 | buf_put(b, kxc->hc, kx->kpriv->algs.hashsz); | |
0617b6e7 | 900 | buf_init(&bb, buf_i, sizeof(buf_i)); |
5b9f3d37 | 901 | g->ops->stge(g, &bb, kxc->R, DHFMT_STD); |
35c8b547 | 902 | buf_put(&bb, kxc->hswrq_out, kx->kpriv->algs.hashsz); |
0617b6e7 | 903 | buf_flip(&bb); |
7ed14135 | 904 | ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCH, &bb, b); |
0617b6e7 | 905 | break; |
906 | case KXS_SWITCH: | |
00e64b67 | 907 | T( trace(T_KEYEXCH, "keyexch: sending switch confirmation to `%s'", |
0617b6e7 | 908 | p_name(kx->p)); ) |
909 | kxc = kx->r[0]; | |
910 | b = p_txstart(kx->p, MSG_KEYEXCH | KX_SWITCHOK); | |
911 | buf_init(&bb, buf_i, sizeof(buf_i)); | |
35c8b547 | 912 | buf_put(&bb, kxc->hswok_out, kx->kpriv->algs.hashsz); |
0617b6e7 | 913 | buf_flip(&bb); |
7ed14135 | 914 | ks_encrypt(kxc->ks, MSG_KEYEXCH | KX_SWITCHOK, &bb, b); |
0617b6e7 | 915 | break; |
916 | default: | |
917 | abort(); | |
410c8acf | 918 | } |
0617b6e7 | 919 | |
920 | if (BOK(b)) { | |
664084ee | 921 | update_stats_tx(kx, BLEN(b)); |
0617b6e7 | 922 | p_txend(kx->p); |
923 | } | |
924 | ||
ff143952 | 925 | if (kx->s < KXS_SWITCH) { |
a06d57a3 | 926 | rs_time(&kx->rs, &tv, 0); |
ff143952 MW |
927 | settimer(kx, &tv); |
928 | } | |
410c8acf | 929 | } |
930 | ||
de7bd20b | 931 | /* --- @decryptrest@ --- * |
0617b6e7 | 932 | * |
933 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
de7bd20b MW |
934 | * @kxchal *kxc@ = pointer to challenge block |
935 | * @unsigned msg@ = type of incoming message | |
0617b6e7 | 936 | * @buf *b@ = encrypted remainder of the packet |
937 | * | |
de7bd20b | 938 | * Returns: Zero if OK, nonzero on some kind of error. |
0617b6e7 | 939 | * |
de7bd20b MW |
940 | * Use: Decrypts the remainder of the packet, and points @b@ at the |
941 | * recovered plaintext. | |
0617b6e7 | 942 | */ |
943 | ||
de7bd20b | 944 | static int decryptrest(keyexch *kx, kxchal *kxc, unsigned msg, buf *b) |
410c8acf | 945 | { |
0617b6e7 | 946 | buf bb; |
0617b6e7 | 947 | |
de7bd20b MW |
948 | buf_init(&bb, buf_o, sizeof(buf_o)); |
949 | if (ks_decrypt(kxc->ks, MSG_KEYEXCH | msg, b, &bb)) { | |
950 | a_warn("KX", "?PEER", kx->p, "decrypt-failed", "%s", pkname[msg], A_END); | |
951 | return (-1); | |
0617b6e7 | 952 | } |
12a26b8b | 953 | if (!BOK(&bb)) return (-1); |
de7bd20b MW |
954 | buf_init(b, BBASE(&bb), BLEN(&bb)); |
955 | return (0); | |
956 | } | |
410c8acf | 957 | |
de7bd20b MW |
958 | /* --- @checkresponse@ --- * |
959 | * | |
960 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
961 | * @unsigned msg@ = type of incoming message | |
962 | * @buf *b@ = decrypted remainder of the packet | |
963 | * | |
964 | * Returns: Zero if OK, nonzero on some kind of error. | |
965 | * | |
966 | * Use: Checks a reply or switch packet, ensuring that its response | |
967 | * is correct. | |
968 | */ | |
0617b6e7 | 969 | |
de7bd20b MW |
970 | static int checkresponse(keyexch *kx, unsigned msg, buf *b) |
971 | { | |
5b9f3d37 MW |
972 | const dhgrp *g = kx->kpriv->grp; |
973 | dhge *R; | |
0617b6e7 | 974 | |
5b9f3d37 | 975 | if ((R = g->ops->ldge(g, b, DHFMT_STD)) == 0) { |
de7bd20b | 976 | a_warn("KX", "?PEER", kx->p, "invalid", "%s", pkname[msg], A_END); |
0617b6e7 | 977 | goto bad; |
978 | } | |
979 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { | |
5b9f3d37 | 980 | trace(T_CRYPTO, "crypto: reply = %s", g->ops->gestr(g, R)); |
0617b6e7 | 981 | })) |
5b9f3d37 | 982 | if (!g->ops->eq(g, R, kx->RX)) { |
de7bd20b | 983 | a_warn("KX", "?PEER", kx->p, "incorrect", "response", A_END); |
0617b6e7 | 984 | goto bad; |
985 | } | |
986 | ||
5b9f3d37 | 987 | g->ops->freege(g, R); |
de7bd20b | 988 | return (0); |
0617b6e7 | 989 | |
990 | bad: | |
5b9f3d37 | 991 | if (R) g->ops->freege(g, R); |
de7bd20b | 992 | return (-1); |
410c8acf | 993 | } |
994 | ||
0617b6e7 | 995 | /* --- @commit@ --- * |
410c8acf | 996 | * |
997 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
0617b6e7 | 998 | * @kxchal *kxc@ = pointer to challenge to commit to |
410c8acf | 999 | * |
1000 | * Returns: --- | |
1001 | * | |
0617b6e7 | 1002 | * Use: Commits to a particular challenge as being the `right' one, |
1003 | * since a reply has arrived for it. | |
410c8acf | 1004 | */ |
1005 | ||
0617b6e7 | 1006 | static void commit(keyexch *kx, kxchal *kxc) |
410c8acf | 1007 | { |
0617b6e7 | 1008 | unsigned i; |
410c8acf | 1009 | |
0617b6e7 | 1010 | for (i = 0; i < kx->nr; i++) { |
1011 | if (kx->r[i] != kxc) | |
1012 | kxc_destroy(kx->r[i]); | |
1013 | } | |
1014 | kx->r[0] = kxc; | |
1015 | kx->nr = 1; | |
1016 | kxc_stoptimer(kxc); | |
e04c2d50 | 1017 | ksl_link(kx->ks, kxc->ks); |
410c8acf | 1018 | } |
1019 | ||
0617b6e7 | 1020 | /* --- @doreply@ --- * |
410c8acf | 1021 | * |
1022 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
0617b6e7 | 1023 | * @buf *b@ = buffer containing packet |
410c8acf | 1024 | * |
0617b6e7 | 1025 | * Returns: Zero if OK, nonzero if the packet was rejected. |
410c8acf | 1026 | * |
0617b6e7 | 1027 | * Use: Handles a reply packet. This doesn't handle the various |
1028 | * switch packets: they're rather too different. | |
410c8acf | 1029 | */ |
1030 | ||
0617b6e7 | 1031 | static int doreply(keyexch *kx, buf *b) |
410c8acf | 1032 | { |
0617b6e7 | 1033 | kxchal *kxc; |
1034 | ||
1035 | if (kx->s != KXS_CHAL && kx->s != KXS_COMMIT) { | |
f43df819 | 1036 | a_warn("KX", "?PEER", kx->p, "unexpected", "reply", A_END); |
0617b6e7 | 1037 | goto bad; |
1038 | } | |
de7bd20b MW |
1039 | if ((kxc = respond(kx, KX_REPLY, b)) == 0 || |
1040 | decryptrest(kx, kxc, KX_REPLY, b) || | |
1041 | checkresponse(kx, KX_REPLY, b)) | |
0617b6e7 | 1042 | goto bad; |
1043 | if (BLEFT(b)) { | |
f43df819 | 1044 | a_warn("KX", "?PEER", kx->p, "invalid", "reply", A_END); |
0617b6e7 | 1045 | goto bad; |
e04c2d50 | 1046 | } |
0617b6e7 | 1047 | if (kx->s == KXS_CHAL) { |
1048 | commit(kx, kxc); | |
1049 | kx->s = KXS_COMMIT; | |
1050 | } | |
1051 | resend(kx); | |
1052 | return (0); | |
1053 | ||
1054 | bad: | |
1055 | return (-1); | |
410c8acf | 1056 | } |
1057 | ||
3cdc3f3a | 1058 | /* --- @kxfinish@ --- * |
1059 | * | |
1060 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
1061 | * | |
1062 | * Returns: --- | |
1063 | * | |
1064 | * Use: Sets everything up following a successful key exchange. | |
1065 | */ | |
1066 | ||
1067 | static void kxfinish(keyexch *kx) | |
1068 | { | |
1069 | kxchal *kxc = kx->r[0]; | |
a06d57a3 | 1070 | struct timeval now, tv; |
ff143952 | 1071 | |
3cdc3f3a | 1072 | ks_activate(kxc->ks); |
a06d57a3 MW |
1073 | gettimeofday(&now, 0); |
1074 | f2tv(&tv, wobble(T_REGEN)); | |
1075 | TV_ADD(&tv, &now, &tv); | |
ff143952 | 1076 | settimer(kx, &tv); |
3cdc3f3a | 1077 | kx->s = KXS_SWITCH; |
f43df819 | 1078 | a_notify("KXDONE", "?PEER", kx->p, A_END); |
3cdc3f3a | 1079 | p_stats(kx->p)->t_kx = time(0); |
1080 | } | |
1081 | ||
0617b6e7 | 1082 | /* --- @doswitch@ --- * |
410c8acf | 1083 | * |
0617b6e7 | 1084 | * Arguments: @keyexch *kx@ = pointer to key exchange block |
1085 | * @buf *b@ = pointer to buffer containing packet | |
410c8acf | 1086 | * |
0617b6e7 | 1087 | * Returns: Zero if OK, nonzero if the packet was rejected. |
410c8acf | 1088 | * |
0617b6e7 | 1089 | * Use: Handles a reply with a switch request bolted onto it. |
410c8acf | 1090 | */ |
1091 | ||
0617b6e7 | 1092 | static int doswitch(keyexch *kx, buf *b) |
410c8acf | 1093 | { |
35c8b547 | 1094 | size_t hsz = kx->kpriv->algs.hashsz; |
0617b6e7 | 1095 | const octet *hc_in, *hc_out, *hswrq; |
1096 | kxchal *kxc; | |
410c8acf | 1097 | |
35c8b547 MW |
1098 | if ((hc_in = buf_get(b, hsz)) == 0 || |
1099 | (hc_out = buf_get(b, hsz)) == 0) { | |
f43df819 | 1100 | a_warn("KX", "?PEER", kx->p, "invalid", "switch-rq", A_END); |
0617b6e7 | 1101 | goto bad; |
410c8acf | 1102 | } |
de7bd20b | 1103 | IF_TRACING(T_KEYEXCH, IF_TRACING(T_CRYPTO, { |
35c8b547 MW |
1104 | trace_block(T_CRYPTO, "crypto: challenge", hc_in, hsz); |
1105 | trace_block(T_CRYPTO, "crypto: cookie", hc_out, hsz); | |
de7bd20b MW |
1106 | })) |
1107 | if ((kxc = kxc_byhc(kx, hc_in)) == 0 || | |
35c8b547 | 1108 | memcmp(hc_out, kx->hc, hsz) != 0) { |
de7bd20b MW |
1109 | a_warn("KX", "?PEER", kx->p, "incorrect", "switch-rq", A_END); |
1110 | goto bad; | |
1111 | } | |
1112 | if (decryptrest(kx, kxc, KX_SWITCH, b) || | |
1113 | checkresponse(kx, KX_SWITCH, b)) | |
0617b6e7 | 1114 | goto bad; |
35c8b547 | 1115 | if ((hswrq = buf_get(b, hsz)) == 0 || BLEFT(b)) { |
5ac9463b | 1116 | a_warn("KX", "?PEER", kx->p, "invalid", "switch-rq", A_END); |
0617b6e7 | 1117 | goto bad; |
1118 | } | |
1119 | IF_TRACING(T_KEYEXCH, { | |
35c8b547 | 1120 | trace_block(T_CRYPTO, "crypto: switch request hash", hswrq, hsz); |
0617b6e7 | 1121 | }) |
35c8b547 | 1122 | if (memcmp(hswrq, kxc->hswrq_in, hsz) != 0) { |
f43df819 | 1123 | a_warn("KX", "?PEER", kx->p, "incorrect", "switch-rq", A_END); |
0617b6e7 | 1124 | goto bad; |
1125 | } | |
de7bd20b MW |
1126 | if (kx->s == KXS_CHAL) |
1127 | commit(kx, kxc); | |
1128 | if (kx->s < KXS_SWITCH) | |
1129 | kxfinish(kx); | |
0617b6e7 | 1130 | resend(kx); |
1131 | return (0); | |
1132 | ||
1133 | bad: | |
1134 | return (-1); | |
410c8acf | 1135 | } |
1136 | ||
0617b6e7 | 1137 | /* --- @doswitchok@ --- * |
1138 | * | |
1139 | * Arguments: @keyexch *kx@ = pointer to key exchange block | |
1140 | * @buf *b@ = pointer to buffer containing packet | |
1141 | * | |
1142 | * Returns: Zero if OK, nonzero if the packet was rejected. | |
1143 | * | |
1144 | * Use: Handles a reply with a switch request bolted onto it. | |
1145 | */ | |
1146 | ||
1147 | static int doswitchok(keyexch *kx, buf *b) | |
410c8acf | 1148 | { |
35c8b547 | 1149 | size_t hsz = kx->kpriv->algs.hashsz; |
0617b6e7 | 1150 | const octet *hswok; |
1151 | kxchal *kxc; | |
1152 | buf bb; | |
410c8acf | 1153 | |
0617b6e7 | 1154 | if (kx->s < KXS_COMMIT) { |
f43df819 | 1155 | a_warn("KX", "?PEER", kx->p, "unexpected", "switch-ok", A_END); |
0617b6e7 | 1156 | goto bad; |
410c8acf | 1157 | } |
0617b6e7 | 1158 | kxc = kx->r[0]; |
1159 | buf_init(&bb, buf_o, sizeof(buf_o)); | |
de7bd20b | 1160 | if (decryptrest(kx, kxc, KX_SWITCHOK, b)) |
0617b6e7 | 1161 | goto bad; |
35c8b547 | 1162 | if ((hswok = buf_get(b, hsz)) == 0 || BLEFT(b)) { |
5ac9463b | 1163 | a_warn("KX", "?PEER", kx->p, "invalid", "switch-ok", A_END); |
0617b6e7 | 1164 | goto bad; |
1165 | } | |
1166 | IF_TRACING(T_KEYEXCH, { | |
b5c45da1 | 1167 | trace_block(T_CRYPTO, "crypto: switch confirmation hash", |
35c8b547 | 1168 | hswok, hsz); |
0617b6e7 | 1169 | }) |
35c8b547 | 1170 | if (memcmp(hswok, kxc->hswok_in, hsz) != 0) { |
f43df819 | 1171 | a_warn("KX", "?PEER", kx->p, "incorrect", "switch-ok", A_END); |
0617b6e7 | 1172 | goto bad; |
1173 | } | |
3cdc3f3a | 1174 | if (kx->s < KXS_SWITCH) |
1175 | kxfinish(kx); | |
0617b6e7 | 1176 | return (0); |
1177 | ||
1178 | bad: | |
e04c2d50 | 1179 | return (-1); |
0617b6e7 | 1180 | } |
1181 | ||
1182 | /*----- Main code ---------------------------------------------------------*/ | |
1183 | ||
1184 | /* --- @stop@ --- * | |
1185 | * | |
1186 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
1187 | * | |
1188 | * Returns: --- | |
1189 | * | |
1190 | * Use: Stops a key exchange dead in its tracks. Throws away all of | |
1191 | * the context information. The context is left in an | |
1192 | * inconsistent state. The only functions which understand this | |
1193 | * state are @kx_free@ and @kx_init@ (which cause it internally | |
1194 | * it), and @start@ (which expects it to be the prevailing | |
1195 | * state). | |
1196 | */ | |
1197 | ||
1198 | static void stop(keyexch *kx) | |
1199 | { | |
5b9f3d37 | 1200 | const dhgrp *g = kx->kpriv->grp; |
0617b6e7 | 1201 | unsigned i; |
1202 | ||
00e64b67 | 1203 | if (kx->f & KXF_DEAD) |
1204 | return; | |
1205 | ||
0617b6e7 | 1206 | if (kx->f & KXF_TIMER) |
1207 | sel_rmtimer(&kx->t); | |
1208 | for (i = 0; i < kx->nr; i++) | |
1209 | kxc_destroy(kx->r[i]); | |
5b9f3d37 MW |
1210 | g->ops->freesc(g, kx->a); |
1211 | g->ops->freege(g, kx->C); | |
1212 | g->ops->freege(g, kx->RX); | |
00e64b67 | 1213 | kx->t_valid = 0; |
1214 | kx->f |= KXF_DEAD; | |
1215 | kx->f &= ~KXF_TIMER; | |
0617b6e7 | 1216 | } |
1217 | ||
1218 | /* --- @start@ --- * | |
1219 | * | |
1220 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
1221 | * @time_t now@ = the current time | |
1222 | * | |
1223 | * Returns: --- | |
1224 | * | |
1225 | * Use: Starts a new key exchange with the peer. The context must be | |
1226 | * in the bizarre state left by @stop@ or @kx_init@. | |
1227 | */ | |
1228 | ||
1229 | static void start(keyexch *kx, time_t now) | |
1230 | { | |
35c8b547 | 1231 | algswitch *algs = &kx->kpriv->algs; |
5b9f3d37 | 1232 | const dhgrp *g = kx->kpriv->grp; |
b5c45da1 | 1233 | ghash *h; |
0617b6e7 | 1234 | |
00e64b67 | 1235 | assert(kx->f & KXF_DEAD); |
1236 | ||
010e6f63 | 1237 | kx->f &= ~(KXF_DEAD | KXF_CORK); |
0617b6e7 | 1238 | kx->nr = 0; |
5b9f3d37 MW |
1239 | kx->a = g->ops->randsc(g); |
1240 | kx->C = g->ops->mul(g, kx->a, 0); | |
1241 | kx->RX = g->ops->mul(g, kx->a, kx->kpub->K); | |
0617b6e7 | 1242 | kx->s = KXS_CHAL; |
1243 | kx->t_valid = now + T_VALID; | |
1244 | ||
35c8b547 | 1245 | h = GH_INIT(algs->h); |
b5c45da1 | 1246 | HASH_STRING(h, "tripe-cookie"); |
5b9f3d37 | 1247 | hashge(h, g, kx->C); |
b5c45da1 | 1248 | GH_DONE(h, kx->hc); |
1249 | GH_DESTROY(h); | |
0617b6e7 | 1250 | |
1251 | IF_TRACING(T_KEYEXCH, { | |
1252 | trace(T_KEYEXCH, "keyexch: creating new challenge"); | |
1253 | IF_TRACING(T_CRYPTO, { | |
5b9f3d37 MW |
1254 | trace(T_CRYPTO, "crypto: secret = %s", g->ops->scstr(g, kx->a)); |
1255 | trace(T_CRYPTO, "crypto: challenge = %s", g->ops->gestr(g, kx->C)); | |
1256 | trace(T_CRYPTO, "crypto: expected response = %s", | |
1257 | g->ops->gestr(g, kx->RX)); | |
35c8b547 MW |
1258 | trace_block(T_CRYPTO, "crypto: challenge cookie", |
1259 | kx->hc, algs->hashsz); | |
0617b6e7 | 1260 | }) |
1261 | }) | |
410c8acf | 1262 | } |
1263 | ||
00e64b67 | 1264 | /* --- @checkpub@ --- * |
1265 | * | |
1266 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
1267 | * | |
1268 | * Returns: Zero if OK, nonzero if the peer's public key has expired. | |
1269 | * | |
1270 | * Use: Deactivates the key-exchange until the peer acquires a new | |
1271 | * public key. | |
1272 | */ | |
1273 | ||
1274 | static int checkpub(keyexch *kx) | |
1275 | { | |
1276 | time_t now; | |
35c8b547 MW |
1277 | unsigned f = 0; |
1278 | ||
00e64b67 | 1279 | if (kx->f & KXF_DEAD) |
1280 | return (-1); | |
1281 | now = time(0); | |
35c8b547 MW |
1282 | if (KEY_EXPIRED(now, kx->kpriv->t_exp)) f |= 1; |
1283 | if (KEY_EXPIRED(now, kx->kpub->t_exp)) f |= 2; | |
1284 | if (f) { | |
00e64b67 | 1285 | stop(kx); |
35c8b547 MW |
1286 | if (f & 1) a_warn("KX", "?PEER", kx->p, "private-key-expired", A_END); |
1287 | if (f & 2) a_warn("KX", "?PEER", kx->p, "public-key-expired", A_END); | |
00e64b67 | 1288 | kx->f &= ~KXF_PUBKEY; |
1289 | return (-1); | |
1290 | } | |
1291 | return (0); | |
1292 | } | |
1293 | ||
0617b6e7 | 1294 | /* --- @kx_start@ --- * |
410c8acf | 1295 | * |
1296 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
de014da6 | 1297 | * @int forcep@ = nonzero to ignore the quiet timer |
410c8acf | 1298 | * |
1299 | * Returns: --- | |
1300 | * | |
0617b6e7 | 1301 | * Use: Stimulates a key exchange. If a key exchage is in progress, |
1302 | * a new challenge is sent (unless the quiet timer forbids | |
1303 | * this); if no exchange is in progress, one is commenced. | |
410c8acf | 1304 | */ |
1305 | ||
de014da6 | 1306 | void kx_start(keyexch *kx, int forcep) |
410c8acf | 1307 | { |
1308 | time_t now = time(0); | |
410c8acf | 1309 | |
00e64b67 | 1310 | if (checkpub(kx)) |
1311 | return; | |
de014da6 | 1312 | if (forcep || !VALIDP(kx, now)) { |
0617b6e7 | 1313 | stop(kx); |
1314 | start(kx, now); | |
f43df819 | 1315 | a_notify("KXSTART", "?PEER", kx->p, A_END); |
410c8acf | 1316 | } |
0617b6e7 | 1317 | resend(kx); |
1318 | } | |
1319 | ||
1320 | /* --- @kx_message@ --- * | |
1321 | * | |
1322 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
1323 | * @unsigned msg@ = the message code | |
1324 | * @buf *b@ = pointer to buffer containing the packet | |
1325 | * | |
1326 | * Returns: --- | |
1327 | * | |
1328 | * Use: Reads a packet containing key exchange messages and handles | |
1329 | * it. | |
1330 | */ | |
1331 | ||
1332 | void kx_message(keyexch *kx, unsigned msg, buf *b) | |
1333 | { | |
0617b6e7 | 1334 | size_t sz = BSZ(b); |
1335 | int rc; | |
1336 | ||
664084ee | 1337 | if (notice_message(kx)) return; |
00e64b67 | 1338 | |
0617b6e7 | 1339 | T( trace(T_KEYEXCH, "keyexch: processing %s packet from `%s'", |
1340 | msg < KX_NMSG ? pkname[msg] : "unknown", p_name(kx->p)); ) | |
1341 | ||
1342 | switch (msg) { | |
1343 | case KX_PRECHAL: | |
de7bd20b MW |
1344 | rc = doprechallenge(kx, b); |
1345 | break; | |
0617b6e7 | 1346 | case KX_CHAL: |
de7bd20b | 1347 | rc = dochallenge(kx, b); |
0617b6e7 | 1348 | break; |
1349 | case KX_REPLY: | |
1350 | rc = doreply(kx, b); | |
1351 | break; | |
1352 | case KX_SWITCH: | |
1353 | rc = doswitch(kx, b); | |
1354 | break; | |
1355 | case KX_SWITCHOK: | |
1356 | rc = doswitchok(kx, b); | |
1357 | break; | |
1358 | default: | |
f43df819 | 1359 | a_warn("KX", "?PEER", kx->p, "unknown-message", "0x%02x", msg, A_END); |
0617b6e7 | 1360 | rc = -1; |
1361 | break; | |
410c8acf | 1362 | } |
410c8acf | 1363 | |
664084ee | 1364 | update_stats_rx(kx, !rc, sz); |
410c8acf | 1365 | } |
1366 | ||
1367 | /* --- @kx_free@ --- * | |
1368 | * | |
1369 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
1370 | * | |
1371 | * Returns: --- | |
1372 | * | |
1373 | * Use: Frees everything in a key exchange context. | |
1374 | */ | |
1375 | ||
1376 | void kx_free(keyexch *kx) | |
1377 | { | |
0617b6e7 | 1378 | stop(kx); |
35c8b547 MW |
1379 | km_unref(kx->kpub); |
1380 | km_unref(kx->kpriv); | |
410c8acf | 1381 | } |
1382 | ||
1383 | /* --- @kx_newkeys@ --- * | |
1384 | * | |
1385 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
1386 | * | |
1387 | * Returns: --- | |
1388 | * | |
1389 | * Use: Informs the key exchange module that its keys may have | |
1390 | * changed. If fetching the new keys fails, the peer will be | |
1391 | * destroyed, we log messages and struggle along with the old | |
1392 | * keys. | |
1393 | */ | |
1394 | ||
1395 | void kx_newkeys(keyexch *kx) | |
1396 | { | |
35c8b547 MW |
1397 | kdata *kpriv, *kpub; |
1398 | unsigned i; | |
1399 | int switchp; | |
1400 | time_t now = time(0); | |
1401 | ||
1402 | T( trace(T_KEYEXCH, "keyexch: checking new keys for `%s'", | |
1403 | p_name(kx->p)); ) | |
1404 | ||
1405 | /* --- Find out whether we can use new keys --- * | |
1406 | * | |
1407 | * Try each available combination of new and old, public and private, | |
1408 | * except both old (which is status quo anyway). The selection is encoded | |
1409 | * in @i@, with bit 0 for the private key and bit 1 for public key; a set | |
1410 | * bit means to use the old value, and a clear bit means to use the new | |
1411 | * one. | |
1412 | * | |
1413 | * This means that we currently prefer `old private and new public' over | |
1414 | * `new private and old public'. I'm not sure which way round this should | |
1415 | * actually be. | |
1416 | */ | |
1417 | ||
1418 | for (i = 0; i < 3; i++) { | |
1419 | ||
1420 | /* --- Select the keys we're going to examine --- * | |
1421 | * | |
1422 | * If we're meant to have a new key and don't, then skip this | |
1423 | * combination. | |
1424 | */ | |
1425 | ||
1426 | T( trace(T_KEYEXCH, "keyexch: checking %s private, %s public", | |
1427 | i & 1 ? "old" : "new", i & 2 ? "old" : "new"); ) | |
1428 | ||
1429 | if (i & 1) kpriv = kx->kpriv; | |
1430 | else if (kx->kpriv->kn->kd != kx->kpriv) kpriv = kx->kpriv->kn->kd; | |
1431 | else { | |
1432 | T( trace(T_KEYEXCH, "keyexch: private key unchanged, skipping"); ) | |
1433 | continue; | |
1434 | } | |
1435 | ||
1436 | if (i & 2) kpub = kx->kpub; | |
1437 | else if (kx->kpub->kn->kd != kx->kpub) kpub = kx->kpub->kn->kd; | |
1438 | else { | |
1439 | T( trace(T_KEYEXCH, "keyexch: public key unchanged, skipping"); ) | |
1440 | continue; | |
1441 | } | |
1442 | ||
1443 | /* --- Skip if either key is expired --- * | |
1444 | * | |
1445 | * We're not going to get far with expired keys, and this simplifies the | |
1446 | * logic below. | |
1447 | */ | |
1448 | ||
1449 | if (KEY_EXPIRED(now, kx->kpriv->t_exp) || | |
1450 | KEY_EXPIRED(now, kx->kpub->t_exp)) { | |
1451 | T( trace(T_KEYEXCH, "keyexch: %s expired, skipping", | |
1452 | !KEY_EXPIRED(now, kx->kpriv->t_exp) ? "public key" : | |
1453 | !KEY_EXPIRED(now, kx->kpub->t_exp) ? "private key" : | |
1454 | "both keys"); ) | |
1455 | continue; | |
1456 | } | |
1457 | ||
1458 | /* --- If the groups don't match then we can't use this pair --- */ | |
1459 | ||
1460 | if (!km_samealgsp(kpriv, kpub)) { | |
1461 | T( trace(T_KEYEXCH, "keyexch: peer `%s' group mismatch; " | |
1462 | "%s priv `%s' and %s pub `%s'", p_name(kx->p), | |
1463 | i & 1 ? "old" : "new", km_tag(kx->kpriv), | |
1464 | i & 2 ? "old" : "new", km_tag(kx->kpub)); ) | |
1465 | continue; | |
1466 | } | |
1467 | goto newkeys; | |
1468 | } | |
1469 | T( trace(T_KEYEXCH, "keyexch: peer `%s' continuing with old keys", | |
1470 | p_name(kx->p)); ) | |
1471 | return; | |
1472 | ||
1473 | /* --- We've chosen new keys --- * | |
1474 | * | |
1475 | * Switch the new ones into place. Neither of the keys we're switching to | |
1476 | * is expired (we checked that above), so we should just crank everything | |
1477 | * up. | |
1478 | * | |
1479 | * A complication arises: we don't really want to force a new key exchange | |
1480 | * unless we have to. If the group is unchanged, and we're currently | |
1481 | * running OK, then we should just let things lie. | |
1482 | */ | |
1483 | ||
1484 | newkeys: | |
1485 | switchp = ((kx->f & KXF_DEAD) || | |
1486 | kx->s != KXS_SWITCH || | |
5b9f3d37 MW |
1487 | kpriv->grp->ops != kx->kpriv->grp->ops || |
1488 | !kpriv->grp->ops->samegrpp(kpriv->grp, kx->kpriv->grp)); | |
35c8b547 MW |
1489 | |
1490 | T( trace(T_KEYEXCH, "keyexch: peer `%s' adopting " | |
1491 | "%s priv `%s' and %s pub `%s'; %sforcing exchange", p_name(kx->p), | |
1492 | i & 1 ? "old" : "new", km_tag(kx->kpriv), | |
1493 | i & 2 ? "old" : "new", km_tag(kx->kpub), | |
1494 | switchp ? "" : "not "); ) | |
1495 | ||
1496 | if (switchp) stop(kx); | |
1497 | km_ref(kpriv); km_unref(kx->kpriv); kx->kpriv = kpriv; | |
1498 | km_ref(kpub); km_unref(kx->kpub); kx->kpub = kpub; | |
00e64b67 | 1499 | kx->f |= KXF_PUBKEY; |
35c8b547 | 1500 | if (switchp) { |
410c8acf | 1501 | T( trace(T_KEYEXCH, "keyexch: restarting key negotiation with `%s'", |
1502 | p_name(kx->p)); ) | |
00e64b67 | 1503 | start(kx, time(0)); |
1504 | resend(kx); | |
410c8acf | 1505 | } |
1506 | } | |
1507 | ||
0510f262 | 1508 | /* --- @kx_setup@ --- * |
410c8acf | 1509 | * |
1510 | * Arguments: @keyexch *kx@ = pointer to key exchange context | |
1511 | * @peer *p@ = pointer to peer context | |
1512 | * @keyset **ks@ = pointer to keyset list | |
010e6f63 | 1513 | * @unsigned f@ = various useful flags |
410c8acf | 1514 | * |
1515 | * Returns: Zero if OK, nonzero if it failed. | |
1516 | * | |
1517 | * Use: Initializes a key exchange module. The module currently | |
1518 | * contains no keys, and will attempt to initiate a key | |
1519 | * exchange. | |
1520 | */ | |
1521 | ||
0510f262 | 1522 | int kx_setup(keyexch *kx, peer *p, keyset **ks, unsigned f) |
410c8acf | 1523 | { |
fe2a5dcf | 1524 | if ((kx->kpriv = km_findpriv(p_privtag(p))) == 0) goto fail_0; |
35c8b547 | 1525 | if ((kx->kpub = km_findpub(p_tag(p))) == 0) goto fail_1; |
0d9974ba | 1526 | if (!km_samealgsp(kx->kpriv, kx->kpub)) { |
cc3e30a4 | 1527 | a_warn("KX", "?PEER", p, "group-mismatch", |
fe2a5dcf | 1528 | "local-private-key", "%s", p_privtag(p), |
35c8b547 MW |
1529 | "peer-public-key", "%s", p_tag(p), |
1530 | A_END); | |
1531 | goto fail_2; | |
1532 | } | |
1533 | ||
410c8acf | 1534 | kx->ks = ks; |
1535 | kx->p = p; | |
010e6f63 | 1536 | kx->f = KXF_DEAD | KXF_PUBKEY | f; |
a06d57a3 | 1537 | rs_reset(&kx->rs); |
010e6f63 MW |
1538 | if (!(kx->f & KXF_CORK)) { |
1539 | start(kx, time(0)); | |
1540 | resend(kx); | |
1541 | /* Don't notify here: the ADD message hasn't gone out yet. */ | |
1542 | } | |
410c8acf | 1543 | return (0); |
35c8b547 MW |
1544 | |
1545 | fail_2: | |
1546 | km_unref(kx->kpub); | |
1547 | fail_1: | |
1548 | km_unref(kx->kpriv); | |
1549 | fail_0: | |
1550 | return (-1); | |
410c8acf | 1551 | } |
1552 | ||
1553 | /*----- That's all, folks -------------------------------------------------*/ |