diff options
Diffstat (limited to 'dh.c')
-rw-r--r-- | dh.c | 39 |
1 files changed, 30 insertions, 9 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* $OpenBSD: dh.c,v 1.44 2006/11/07 13:02:07 markus Exp $ */ | ||
1 | /* | 2 | /* |
2 | * Copyright (c) 2000 Niels Provos. All rights reserved. | 3 | * Copyright (c) 2000 Niels Provos. All rights reserved. |
3 | * | 4 | * |
@@ -23,17 +24,17 @@ | |||
23 | */ | 24 | */ |
24 | 25 | ||
25 | #include "includes.h" | 26 | #include "includes.h" |
26 | RCSID("$OpenBSD: dh.c,v 1.31 2004/08/04 10:37:52 djm Exp $"); | ||
27 | 27 | ||
28 | #include "xmalloc.h" | 28 | #include <sys/param.h> |
29 | 29 | ||
30 | #include <openssl/bn.h> | 30 | #include <openssl/bn.h> |
31 | #include <openssl/dh.h> | 31 | #include <openssl/dh.h> |
32 | #include <openssl/evp.h> | ||
33 | 32 | ||
34 | #include "buffer.h" | 33 | #include <stdarg.h> |
35 | #include "cipher.h" | 34 | #include <stdio.h> |
36 | #include "kex.h" | 35 | #include <stdlib.h> |
36 | #include <string.h> | ||
37 | |||
37 | #include "dh.h" | 38 | #include "dh.h" |
38 | #include "pathnames.h" | 39 | #include "pathnames.h" |
39 | #include "log.h" | 40 | #include "log.h" |
@@ -44,9 +45,11 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg) | |||
44 | { | 45 | { |
45 | char *cp, *arg; | 46 | char *cp, *arg; |
46 | char *strsize, *gen, *prime; | 47 | char *strsize, *gen, *prime; |
48 | const char *errstr = NULL; | ||
47 | 49 | ||
48 | cp = line; | 50 | cp = line; |
49 | arg = strdelim(&cp); | 51 | if ((arg = strdelim(&cp)) == NULL) |
52 | return 0; | ||
50 | /* Ignore leading whitespace */ | 53 | /* Ignore leading whitespace */ |
51 | if (*arg == '\0') | 54 | if (*arg == '\0') |
52 | arg = strdelim(&cp); | 55 | arg = strdelim(&cp); |
@@ -67,7 +70,8 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg) | |||
67 | goto fail; | 70 | goto fail; |
68 | strsize = strsep(&cp, " "); /* size */ | 71 | strsize = strsep(&cp, " "); /* size */ |
69 | if (cp == NULL || *strsize == '\0' || | 72 | if (cp == NULL || *strsize == '\0' || |
70 | (dhg->size = atoi(strsize)) == 0) | 73 | (dhg->size = (u_int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 || |
74 | errstr) | ||
71 | goto fail; | 75 | goto fail; |
72 | /* The whole group is one bit larger */ | 76 | /* The whole group is one bit larger */ |
73 | dhg->size++; | 77 | dhg->size++; |
@@ -178,19 +182,36 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub) | |||
178 | int i; | 182 | int i; |
179 | int n = BN_num_bits(dh_pub); | 183 | int n = BN_num_bits(dh_pub); |
180 | int bits_set = 0; | 184 | int bits_set = 0; |
185 | BIGNUM *tmp; | ||
181 | 186 | ||
182 | if (dh_pub->neg) { | 187 | if (dh_pub->neg) { |
183 | logit("invalid public DH value: negativ"); | 188 | logit("invalid public DH value: negativ"); |
184 | return 0; | 189 | return 0; |
185 | } | 190 | } |
191 | if (BN_cmp(dh_pub, BN_value_one()) != 1) { /* pub_exp <= 1 */ | ||
192 | logit("invalid public DH value: <= 1"); | ||
193 | return 0; | ||
194 | } | ||
195 | |||
196 | if ((tmp = BN_new()) == NULL) | ||
197 | return (-1); | ||
198 | if (!BN_sub(tmp, dh->p, BN_value_one()) || | ||
199 | BN_cmp(dh_pub, tmp) != -1) { /* pub_exp > p-2 */ | ||
200 | BN_clear_free(tmp); | ||
201 | logit("invalid public DH value: >= p-1"); | ||
202 | return 0; | ||
203 | } | ||
204 | BN_clear_free(tmp); | ||
205 | |||
186 | for (i = 0; i <= n; i++) | 206 | for (i = 0; i <= n; i++) |
187 | if (BN_is_bit_set(dh_pub, i)) | 207 | if (BN_is_bit_set(dh_pub, i)) |
188 | bits_set++; | 208 | bits_set++; |
189 | debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p)); | 209 | debug2("bits set: %d/%d", bits_set, BN_num_bits(dh->p)); |
190 | 210 | ||
191 | /* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial */ | 211 | /* if g==2 and bits_set==1 then computing log_g(dh_pub) is trivial */ |
192 | if (bits_set > 1 && (BN_cmp(dh_pub, dh->p) == -1)) | 212 | if (bits_set > 1) |
193 | return 1; | 213 | return 1; |
214 | |||
194 | logit("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p)); | 215 | logit("invalid public DH value (%d/%d)", bits_set, BN_num_bits(dh->p)); |
195 | return 0; | 216 | return 0; |
196 | } | 217 | } |