[BACK]Return to dh.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/dh.c, Revision 1.5

1.1       provos      1: /*
                      2:  * Copyright (c) 2000 Niels Provos.  All rights reserved.
                      3:  *
                      4:  * Redistribution and use in source and binary forms, with or without
                      5:  * modification, are permitted provided that the following conditions
                      6:  * are met:
                      7:  * 1. Redistributions of source code must retain the above copyright
                      8:  *    notice, this list of conditions and the following disclaimer.
                      9:  * 2. Redistributions in binary form must reproduce the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer in the
                     11:  *    documentation and/or other materials provided with the distribution.
                     12:  *
                     13:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     14:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     15:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     16:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     17:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     18:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     19:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     20:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     21:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     22:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     23:  */
                     24:
                     25: #include "includes.h"
1.5     ! markus     26: RCSID("$OpenBSD: dh.c,v 1.4 2001/01/15 21:43:51 markus Exp $");
1.1       provos     27:
                     28: #include "xmalloc.h"
                     29:
                     30: #include <openssl/bn.h>
                     31: #include <openssl/dh.h>
                     32: #include <openssl/evp.h>
                     33:
                     34: #include "ssh.h"
                     35: #include "buffer.h"
                     36: #include "kex.h"
                     37: #include "dh.h"
1.5     ! markus     38: #include "pathnames.h"
1.1       provos     39:
                     40: int
                     41: parse_prime(int linenum, char *line, struct dhgroup *dhg)
                     42: {
                     43:        char *cp, *arg;
                     44:        char *strsize, *gen, *prime;
                     45:
                     46:        cp = line;
                     47:        arg = strdelim(&cp);
                     48:        /* Ignore leading whitespace */
                     49:        if (*arg == '\0')
                     50:                arg = strdelim(&cp);
                     51:        if (!*arg || *arg == '#')
                     52:                return 0;
                     53:
                     54:        /* time */
                     55:        if (cp == NULL || *arg == '\0')
                     56:                goto fail;
                     57:        arg = strsep(&cp, " "); /* type */
                     58:        if (cp == NULL || *arg == '\0')
                     59:                goto fail;
                     60:        arg = strsep(&cp, " "); /* tests */
                     61:        if (cp == NULL || *arg == '\0')
                     62:                goto fail;
                     63:        arg = strsep(&cp, " "); /* tries */
                     64:        if (cp == NULL || *arg == '\0')
                     65:                goto fail;
                     66:        strsize = strsep(&cp, " "); /* size */
                     67:        if (cp == NULL || *strsize == '\0' ||
                     68:            (dhg->size = atoi(strsize)) == 0)
                     69:                goto fail;
                     70:        gen = strsep(&cp, " "); /* gen */
                     71:        if (cp == NULL || *gen == '\0')
                     72:                goto fail;
                     73:        prime = strsep(&cp, " "); /* prime */
                     74:        if (cp != NULL || *prime == '\0')
                     75:                goto fail;
                     76:
                     77:        dhg->g = BN_new();
                     78:        if (BN_hex2bn(&dhg->g, gen) < 0) {
                     79:                BN_free(dhg->g);
                     80:                goto fail;
                     81:        }
                     82:        dhg->p = BN_new();
                     83:        if (BN_hex2bn(&dhg->p, prime) < 0) {
                     84:                BN_free(dhg->g);
                     85:                BN_free(dhg->p);
                     86:                goto fail;
                     87:        }
                     88:
                     89:        return (1);
                     90:  fail:
1.4       markus     91:        error("Bad prime description in line %d\n", linenum);
1.1       provos     92:        return (0);
                     93: }
                     94:
                     95: DH *
                     96: choose_dh(int minbits)
                     97: {
                     98:        FILE *f;
                     99:        char line[1024];
                    100:        int best, bestcount, which;
                    101:        int linenum;
                    102:        struct dhgroup dhg;
                    103:
1.5     ! markus    104:        f = fopen(_PATH_DH_PRIMES, "r");
1.1       provos    105:        if (!f) {
1.5     ! markus    106:                log("WARNING: %s does not exist, using old prime", _PATH_DH_PRIMES);
1.1       provos    107:                return (dh_new_group1());
                    108:        }
                    109:
                    110:        linenum = 0;
                    111:        best = bestcount = 0;
                    112:        while (fgets(line, sizeof(line), f)) {
                    113:                linenum++;
                    114:                if (!parse_prime(linenum, line, &dhg))
                    115:                        continue;
                    116:                BN_free(dhg.g);
                    117:                BN_free(dhg.p);
                    118:
                    119:                if ((dhg.size > minbits && dhg.size < best) ||
                    120:                    (dhg.size > best && best < minbits)) {
                    121:                        best = dhg.size;
                    122:                        bestcount = 0;
                    123:                }
                    124:                if (dhg.size == best)
                    125:                        bestcount++;
                    126:        }
                    127:        fclose (f);
                    128:
                    129:        if (bestcount == 0) {
1.5     ! markus    130:                log("WARNING: no primes in %s, using old prime", _PATH_DH_PRIMES);
1.1       provos    131:                return (dh_new_group1());
                    132:        }
                    133:
1.5     ! markus    134:        f = fopen(_PATH_DH_PRIMES, "r");
1.1       provos    135:        if (!f) {
1.5     ! markus    136:                fatal("WARNING: %s dissappeared, giving up", _PATH_DH_PRIMES);
1.1       provos    137:        }
                    138:
                    139:        linenum = 0;
                    140:        which = arc4random() % bestcount;
                    141:        while (fgets(line, sizeof(line), f)) {
                    142:                if (!parse_prime(linenum, line, &dhg))
                    143:                        continue;
                    144:                if (dhg.size != best)
                    145:                        continue;
                    146:                if (linenum++ != which) {
                    147:                        BN_free(dhg.g);
                    148:                        BN_free(dhg.p);
                    149:                        continue;
                    150:                }
                    151:                break;
                    152:        }
                    153:        fclose(f);
                    154:
                    155:        return (dh_new_group(dhg.g, dhg.p));
                    156: }