Annotation of src/usr.bin/ssh/dh.c, Revision 1.54
1.54 ! markus 1: /* $OpenBSD: dh.c,v 1.53 2013/11/21 00:45:44 djm Exp $ */
1.1 provos 2: /*
3: * Copyright (c) 2000 Niels Provos. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: *
14: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24: */
1.39 stevesk 25:
26: #include <sys/param.h>
1.1 provos 27:
28: #include <openssl/bn.h>
29: #include <openssl/dh.h>
1.38 stevesk 30:
1.41 stevesk 31: #include <stdio.h>
1.40 stevesk 32: #include <stdlib.h>
1.38 stevesk 33: #include <string.h>
1.1 provos 34:
35: #include "dh.h"
1.5 markus 36: #include "pathnames.h"
1.6 markus 37: #include "log.h"
38: #include "misc.h"
1.54 ! markus 39: #include "ssherr.h"
1.1 provos 40:
1.17 itojun 41: static int
1.1 provos 42: parse_prime(int linenum, char *line, struct dhgroup *dhg)
43: {
44: char *cp, *arg;
45: char *strsize, *gen, *prime;
1.35 deraadt 46: const char *errstr = NULL;
1.47 djm 47: long long n;
1.1 provos 48:
1.51 markus 49: dhg->p = dhg->g = NULL;
1.1 provos 50: cp = line;
1.32 djm 51: if ((arg = strdelim(&cp)) == NULL)
52: return 0;
1.1 provos 53: /* Ignore leading whitespace */
54: if (*arg == '\0')
55: arg = strdelim(&cp);
1.22 markus 56: if (!arg || !*arg || *arg == '#')
1.1 provos 57: return 0;
58:
59: /* time */
60: if (cp == NULL || *arg == '\0')
1.50 djm 61: goto truncated;
1.1 provos 62: arg = strsep(&cp, " "); /* type */
63: if (cp == NULL || *arg == '\0')
1.50 djm 64: goto truncated;
1.47 djm 65: /* Ensure this is a safe prime */
66: n = strtonum(arg, 0, 5, &errstr);
1.50 djm 67: if (errstr != NULL || n != MODULI_TYPE_SAFE) {
68: error("moduli:%d: type is not %d", linenum, MODULI_TYPE_SAFE);
1.47 djm 69: goto fail;
1.50 djm 70: }
1.1 provos 71: arg = strsep(&cp, " "); /* tests */
72: if (cp == NULL || *arg == '\0')
1.50 djm 73: goto truncated;
1.47 djm 74: /* Ensure prime has been tested and is not composite */
75: n = strtonum(arg, 0, 0x1f, &errstr);
76: if (errstr != NULL ||
1.50 djm 77: (n & MODULI_TESTS_COMPOSITE) || !(n & ~MODULI_TESTS_COMPOSITE)) {
78: error("moduli:%d: invalid moduli tests flag", linenum);
1.47 djm 79: goto fail;
1.50 djm 80: }
1.1 provos 81: arg = strsep(&cp, " "); /* tries */
82: if (cp == NULL || *arg == '\0')
1.50 djm 83: goto truncated;
1.47 djm 84: n = strtonum(arg, 0, 1<<30, &errstr);
1.50 djm 85: if (errstr != NULL || n == 0) {
86: error("moduli:%d: invalid primality trial count", linenum);
1.1 provos 87: goto fail;
1.50 djm 88: }
1.1 provos 89: strsize = strsep(&cp, " "); /* size */
90: if (cp == NULL || *strsize == '\0' ||
1.48 grunk 91: (dhg->size = (int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 ||
1.50 djm 92: errstr) {
93: error("moduli:%d: invalid prime length", linenum);
1.1 provos 94: goto fail;
1.50 djm 95: }
1.9 provos 96: /* The whole group is one bit larger */
97: dhg->size++;
1.1 provos 98: gen = strsep(&cp, " "); /* gen */
99: if (cp == NULL || *gen == '\0')
1.50 djm 100: goto truncated;
1.1 provos 101: prime = strsep(&cp, " "); /* prime */
1.50 djm 102: if (cp != NULL || *prime == '\0') {
103: truncated:
104: error("moduli:%d: truncated", linenum);
1.1 provos 105: goto fail;
1.50 djm 106: }
1.1 provos 107:
1.54 ! markus 108: if ((dhg->g = BN_new()) == NULL ||
! 109: (dhg->p = BN_new()) == NULL) {
! 110: error("parse_prime: BN_new failed");
! 111: goto fail;
! 112: }
1.50 djm 113: if (BN_hex2bn(&dhg->g, gen) == 0) {
114: error("moduli:%d: could not parse generator value", linenum);
115: goto fail;
116: }
117: if (BN_hex2bn(&dhg->p, prime) == 0) {
118: error("moduli:%d: could not parse prime value", linenum);
119: goto fail;
120: }
121: if (BN_num_bits(dhg->p) != dhg->size) {
122: error("moduli:%d: prime has wrong size: actual %d listed %d",
123: linenum, BN_num_bits(dhg->p), dhg->size - 1);
124: goto fail;
125: }
126: if (BN_cmp(dhg->g, BN_value_one()) <= 0) {
127: error("moduli:%d: generator is invalid", linenum);
128: goto fail;
129: }
130: return 1;
1.10 provos 131:
1.1 provos 132: fail:
1.50 djm 133: if (dhg->g != NULL)
134: BN_clear_free(dhg->g);
135: if (dhg->p != NULL)
136: BN_clear_free(dhg->p);
137: dhg->g = dhg->p = NULL;
138: return 0;
1.1 provos 139: }
140:
141: DH *
1.9 provos 142: choose_dh(int min, int wantbits, int max)
1.1 provos 143: {
144: FILE *f;
1.28 dtucker 145: char line[4096];
1.1 provos 146: int best, bestcount, which;
147: int linenum;
148: struct dhgroup dhg;
149:
1.15 provos 150: if ((f = fopen(_PATH_DH_MODULI, "r")) == NULL &&
151: (f = fopen(_PATH_DH_PRIMES, "r")) == NULL) {
1.30 djm 152: logit("WARNING: %s does not exist, using fixed modulus",
153: _PATH_DH_MODULI);
154: return (dh_new_group14());
1.1 provos 155: }
156:
157: linenum = 0;
158: best = bestcount = 0;
159: while (fgets(line, sizeof(line), f)) {
160: linenum++;
161: if (!parse_prime(linenum, line, &dhg))
162: continue;
1.19 markus 163: BN_clear_free(dhg.g);
164: BN_clear_free(dhg.p);
1.1 provos 165:
1.9 provos 166: if (dhg.size > max || dhg.size < min)
167: continue;
168:
169: if ((dhg.size > wantbits && dhg.size < best) ||
170: (dhg.size > best && best < wantbits)) {
1.1 provos 171: best = dhg.size;
172: bestcount = 0;
173: }
174: if (dhg.size == best)
175: bestcount++;
176: }
1.16 provos 177: rewind(f);
1.1 provos 178:
179: if (bestcount == 0) {
1.16 provos 180: fclose(f);
1.24 itojun 181: logit("WARNING: no suitable primes in %s", _PATH_DH_PRIMES);
1.31 djm 182: return (dh_new_group14());
1.1 provos 183: }
184:
185: linenum = 0;
1.46 djm 186: which = arc4random_uniform(bestcount);
1.1 provos 187: while (fgets(line, sizeof(line), f)) {
188: if (!parse_prime(linenum, line, &dhg))
1.9 provos 189: continue;
1.13 markus 190: if ((dhg.size > max || dhg.size < min) ||
191: dhg.size != best ||
192: linenum++ != which) {
1.19 markus 193: BN_clear_free(dhg.g);
194: BN_clear_free(dhg.p);
1.1 provos 195: continue;
196: }
197: break;
198: }
199: fclose(f);
1.54 ! markus 200: if (linenum != which+1) {
! 201: logit("WARNING: line %d disappeared in %s, giving up",
1.13 markus 202: which, _PATH_DH_PRIMES);
1.54 ! markus 203: return (dh_new_group14());
! 204: }
1.1 provos 205:
206: return (dh_new_group(dhg.g, dhg.p));
1.11 markus 207: }
208:
1.30 djm 209: /* diffie-hellman-groupN-sha1 */
1.11 markus 210:
211: int
212: dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
213: {
214: int i;
215: int n = BN_num_bits(dh_pub);
216: int bits_set = 0;
1.36 djm 217: BIGNUM *tmp;
1.11 markus 218:
219: if (dh_pub->neg) {
1.45 ray 220: logit("invalid public DH value: negative");
1.11 markus 221: return 0;
222: }
1.36 djm 223: if (BN_cmp(dh_pub, BN_value_one()) != 1) { /* pub_exp <= 1 */
224: logit("invalid public DH value: <= 1");
225: return 0;
226: }
227:
1.45 ray 228: if ((tmp = BN_new()) == NULL) {
229: error("%s: BN_new failed", __func__);
230: return 0;
231: }
1.36 djm 232: if (!BN_sub(tmp, dh->p, BN_value_one()) ||
233: BN_cmp(dh_pub, tmp) != -1) { /* pub_exp > p-2 */
234: BN_clear_free(tmp);
235: logit("invalid public DH value: >= p-1");
236: return 0;
237: }
238: BN_clear_free(tmp);
239:
1.11 markus 240: for (i = 0; i <= n; i++)
241: if (BN_is_bit_set(dh_pub, i))
242: bits_set++;
1.23 markus 243: debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p));
1.11 markus 244:
245: /* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial */
1.36 djm 246: if (bits_set > 1)
1.11 markus 247: return 1;
1.36 djm 248:
1.24 itojun 249: logit("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p));
1.11 markus 250: return 0;
251: }
252:
1.54 ! markus 253: int
1.11 markus 254: dh_gen_key(DH *dh, int need)
255: {
1.53 djm 256: int pbits;
1.11 markus 257:
1.54 ! markus 258: if (need < 0 || dh->p == NULL ||
! 259: (pbits = BN_num_bits(dh->p)) <= 0 ||
! 260: need > INT_MAX / 2 || 2 * need >= pbits)
! 261: return SSH_ERR_INVALID_ARGUMENT;
1.53 djm 262: dh->length = MIN(need * 2, pbits - 1);
1.54 ! markus 263: if (DH_generate_key(dh) == 0 ||
! 264: !dh_pub_is_valid(dh, dh->pub_key)) {
! 265: BN_clear_free(dh->priv_key);
! 266: return SSH_ERR_LIBCRYPTO_ERROR;
! 267: }
! 268: return 0;
1.11 markus 269: }
270:
271: DH *
272: dh_new_group_asc(const char *gen, const char *modulus)
273: {
274: DH *dh;
275:
1.18 markus 276: if ((dh = DH_new()) == NULL)
1.54 ! markus 277: return NULL;
! 278: if (BN_hex2bn(&dh->p, modulus) == 0 ||
! 279: BN_hex2bn(&dh->g, gen) == 0) {
! 280: DH_free(dh);
! 281: return NULL;
! 282: }
1.11 markus 283: return (dh);
284: }
285:
286: /*
287: * This just returns the group, we still need to generate the exchange
288: * value.
289: */
290:
291: DH *
292: dh_new_group(BIGNUM *gen, BIGNUM *modulus)
293: {
294: DH *dh;
295:
1.18 markus 296: if ((dh = DH_new()) == NULL)
1.54 ! markus 297: return NULL;
1.11 markus 298: dh->p = modulus;
299: dh->g = gen;
300:
301: return (dh);
302: }
303:
304: DH *
305: dh_new_group1(void)
306: {
307: static char *gen = "2", *group1 =
308: "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
309: "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
310: "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
311: "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
312: "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE65381"
313: "FFFFFFFF" "FFFFFFFF";
314:
315: return (dh_new_group_asc(gen, group1));
1.30 djm 316: }
317:
318: DH *
319: dh_new_group14(void)
320: {
321: static char *gen = "2", *group14 =
322: "FFFFFFFF" "FFFFFFFF" "C90FDAA2" "2168C234" "C4C6628B" "80DC1CD1"
323: "29024E08" "8A67CC74" "020BBEA6" "3B139B22" "514A0879" "8E3404DD"
324: "EF9519B3" "CD3A431B" "302B0A6D" "F25F1437" "4FE1356D" "6D51C245"
325: "E485B576" "625E7EC6" "F44C42E9" "A637ED6B" "0BFF5CB6" "F406B7ED"
326: "EE386BFB" "5A899FA5" "AE9F2411" "7C4B1FE6" "49286651" "ECE45B3D"
327: "C2007CB8" "A163BF05" "98DA4836" "1C55D39A" "69163FA8" "FD24CF5F"
328: "83655D23" "DCA3AD96" "1C62F356" "208552BB" "9ED52907" "7096966D"
329: "670C354E" "4ABC9804" "F1746C08" "CA18217C" "32905E46" "2E36CE3B"
330: "E39E772C" "180E8603" "9B2783A2" "EC07A28F" "B5C55DF0" "6F4C52C9"
331: "DE2BCBF6" "95581718" "3995497C" "EA956AE5" "15D22618" "98FA0510"
332: "15728E5A" "8AACAA68" "FFFFFFFF" "FFFFFFFF";
333:
334: return (dh_new_group_asc(gen, group14));
1.12 markus 335: }
336:
337: /*
338: * Estimates the group order for a Diffie-Hellman group that has an
1.52 dtucker 339: * attack complexity approximately the same as O(2**bits).
340: * Values from NIST Special Publication 800-57: Recommendation for Key
341: * Management Part 1 (rev 3) limited by the recommended maximum value
342: * from RFC4419 section 3.
1.12 markus 343: */
344:
1.54 ! markus 345: u_int
1.12 markus 346: dh_estimate(int bits)
347: {
1.52 dtucker 348: if (bits <= 112)
349: return 2048;
1.26 markus 350: if (bits <= 128)
1.52 dtucker 351: return 3072;
1.26 markus 352: if (bits <= 192)
1.52 dtucker 353: return 7680;
354: return 8192;
1.1 provos 355: }