]> andersk Git - openssh.git/blobdiff - dh.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / dh.c
diff --git a/dh.c b/dh.c
index ff31ca9918cafb17ec3174e189b15dd29d7f8e0c..b9029d867e18e42cd390314597b35fdf1179eb81 100644 (file)
--- a/dh.c
+++ b/dh.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: dh.c,v 1.37 2006/07/18 22:27:55 stevesk Exp $ */
+/* $OpenBSD: dh.c,v 1.48 2009/10/01 11:37:33 grunk Exp $ */
 /*
  * Copyright (c) 2000 Niels Provos.  All rights reserved.
  *
 
 #include "includes.h"
 
+#include <sys/param.h>
+
 #include <openssl/bn.h>
 #include <openssl/dh.h>
 
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
 #include "dh.h"
 #include "pathnames.h"
 #include "log.h"
@@ -39,6 +46,7 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg)
        char *cp, *arg;
        char *strsize, *gen, *prime;
        const char *errstr = NULL;
+       long long n;
 
        cp = line;
        if ((arg = strdelim(&cp)) == NULL)
@@ -55,15 +63,27 @@ parse_prime(int linenum, char *line, struct dhgroup *dhg)
        arg = strsep(&cp, " "); /* type */
        if (cp == NULL || *arg == '\0')
                goto fail;
+       /* Ensure this is a safe prime */
+       n = strtonum(arg, 0, 5, &errstr);
+       if (errstr != NULL || n != MODULI_TYPE_SAFE)
+               goto fail;
        arg = strsep(&cp, " "); /* tests */
        if (cp == NULL || *arg == '\0')
                goto fail;
+       /* Ensure prime has been tested and is not composite */
+       n = strtonum(arg, 0, 0x1f, &errstr);
+       if (errstr != NULL ||
+           (n & MODULI_TESTS_COMPOSITE) || !(n & ~MODULI_TESTS_COMPOSITE))
+               goto fail;
        arg = strsep(&cp, " "); /* tries */
        if (cp == NULL || *arg == '\0')
                goto fail;
+       n = strtonum(arg, 0, 1<<30, &errstr);
+       if (errstr != NULL || n == 0)
+               goto fail;
        strsize = strsep(&cp, " "); /* size */
        if (cp == NULL || *strsize == '\0' ||
-           (dhg->size = (u_int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 ||
+           (dhg->size = (int)strtonum(strsize, 0, 64*1024, &errstr)) == 0 ||
            errstr)
                goto fail;
        /* The whole group is one bit larger */
@@ -146,7 +166,7 @@ choose_dh(int min, int wantbits, int max)
        }
 
        linenum = 0;
-       which = arc4random() % bestcount;
+       which = arc4random_uniform(bestcount);
        while (fgets(line, sizeof(line), f)) {
                if (!parse_prime(linenum, line, &dhg))
                        continue;
@@ -178,7 +198,7 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
        BIGNUM *tmp;
 
        if (dh_pub->neg) {
-               logit("invalid public DH value: negativ");
+               logit("invalid public DH value: negative");
                return 0;
        }
        if (BN_cmp(dh_pub, BN_value_one()) != 1) {      /* pub_exp <= 1 */
@@ -186,8 +206,10 @@ dh_pub_is_valid(DH *dh, BIGNUM *dh_pub)
                return 0;
        }
 
-       if ((tmp = BN_new()) == NULL)
-               return (-1);
+       if ((tmp = BN_new()) == NULL) {
+               error("%s: BN_new failed", __func__);
+               return 0;
+       }
        if (!BN_sub(tmp, dh->p, BN_value_one()) ||
            BN_cmp(dh_pub, tmp) != -1) {                /* pub_exp > p-2 */
                BN_clear_free(tmp);
This page took 1.426066 seconds and 4 git commands to generate.