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

Annotation of src/usr.bin/ssh/key.c, Revision 1.75

1.75    ! grunk       1: /* $OpenBSD: key.c,v 1.74 2008/06/12 05:42:46 grunk Exp $ */
1.1       markus      2: /*
1.11      deraadt     3:  * read_bignum():
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *
                      6:  * As far as I am concerned, the code I have written for this software
                      7:  * can be used freely for any purpose.  Any derived versions of this
                      8:  * software must be clearly marked as such, and if the derived work is
                      9:  * incompatible with the protocol description in the RFC file, it must be
                     10:  * called by a name other than "ssh" or "Secure Shell".
                     11:  *
                     12:  *
1.28      markus     13:  * Copyright (c) 2000, 2001 Markus Friedl.  All rights reserved.
1.1       markus     14:  *
                     15:  * Redistribution and use in source and binary forms, with or without
                     16:  * modification, are permitted provided that the following conditions
                     17:  * are met:
                     18:  * 1. Redistributions of source code must retain the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer.
                     20:  * 2. Redistributions in binary form must reproduce the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer in the
                     22:  *    documentation and/or other materials provided with the distribution.
                     23:  *
                     24:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     25:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     26:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     27:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     28:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     29:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     30:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     31:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     32:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     33:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     34:  */
1.67      deraadt    35:
1.70      grunk      36: #include <sys/param.h>
1.67      deraadt    37: #include <sys/types.h>
1.1       markus     38:
1.2       markus     39: #include <openssl/evp.h>
1.65      stevesk    40:
1.66      stevesk    41: #include <stdio.h>
1.65      stevesk    42: #include <string.h>
1.15      markus     43:
1.1       markus     44: #include "xmalloc.h"
                     45: #include "key.h"
1.12      markus     46: #include "rsa.h"
1.3       markus     47: #include "uuencode.h"
1.12      markus     48: #include "buffer.h"
1.15      markus     49: #include "log.h"
1.1       markus     50:
                     51: Key *
                     52: key_new(int type)
                     53: {
                     54:        Key *k;
                     55:        RSA *rsa;
                     56:        DSA *dsa;
1.63      djm        57:        k = xcalloc(1, sizeof(*k));
1.1       markus     58:        k->type = type;
1.3       markus     59:        k->dsa = NULL;
                     60:        k->rsa = NULL;
1.1       markus     61:        switch (k->type) {
1.12      markus     62:        case KEY_RSA1:
1.1       markus     63:        case KEY_RSA:
1.38      markus     64:                if ((rsa = RSA_new()) == NULL)
                     65:                        fatal("key_new: RSA_new failed");
                     66:                if ((rsa->n = BN_new()) == NULL)
                     67:                        fatal("key_new: BN_new failed");
                     68:                if ((rsa->e = BN_new()) == NULL)
                     69:                        fatal("key_new: BN_new failed");
1.1       markus     70:                k->rsa = rsa;
                     71:                break;
                     72:        case KEY_DSA:
1.38      markus     73:                if ((dsa = DSA_new()) == NULL)
                     74:                        fatal("key_new: DSA_new failed");
                     75:                if ((dsa->p = BN_new()) == NULL)
                     76:                        fatal("key_new: BN_new failed");
                     77:                if ((dsa->q = BN_new()) == NULL)
                     78:                        fatal("key_new: BN_new failed");
                     79:                if ((dsa->g = BN_new()) == NULL)
                     80:                        fatal("key_new: BN_new failed");
                     81:                if ((dsa->pub_key = BN_new()) == NULL)
                     82:                        fatal("key_new: BN_new failed");
1.1       markus     83:                k->dsa = dsa;
                     84:                break;
1.12      markus     85:        case KEY_UNSPEC:
1.1       markus     86:                break;
                     87:        default:
                     88:                fatal("key_new: bad key type %d", k->type);
                     89:                break;
                     90:        }
                     91:        return k;
                     92: }
1.45      deraadt    93:
1.12      markus     94: Key *
                     95: key_new_private(int type)
                     96: {
                     97:        Key *k = key_new(type);
                     98:        switch (k->type) {
                     99:        case KEY_RSA1:
                    100:        case KEY_RSA:
1.38      markus    101:                if ((k->rsa->d = BN_new()) == NULL)
                    102:                        fatal("key_new_private: BN_new failed");
                    103:                if ((k->rsa->iqmp = BN_new()) == NULL)
                    104:                        fatal("key_new_private: BN_new failed");
                    105:                if ((k->rsa->q = BN_new()) == NULL)
                    106:                        fatal("key_new_private: BN_new failed");
                    107:                if ((k->rsa->p = BN_new()) == NULL)
                    108:                        fatal("key_new_private: BN_new failed");
                    109:                if ((k->rsa->dmq1 = BN_new()) == NULL)
                    110:                        fatal("key_new_private: BN_new failed");
                    111:                if ((k->rsa->dmp1 = BN_new()) == NULL)
                    112:                        fatal("key_new_private: BN_new failed");
1.12      markus    113:                break;
                    114:        case KEY_DSA:
1.38      markus    115:                if ((k->dsa->priv_key = BN_new()) == NULL)
                    116:                        fatal("key_new_private: BN_new failed");
1.12      markus    117:                break;
                    118:        case KEY_UNSPEC:
                    119:                break;
                    120:        default:
                    121:                break;
                    122:        }
                    123:        return k;
                    124: }
1.45      deraadt   125:
1.1       markus    126: void
                    127: key_free(Key *k)
                    128: {
1.60      djm       129:        if (k == NULL)
1.62      deraadt   130:                fatal("key_free: key is NULL");
1.1       markus    131:        switch (k->type) {
1.12      markus    132:        case KEY_RSA1:
1.1       markus    133:        case KEY_RSA:
                    134:                if (k->rsa != NULL)
                    135:                        RSA_free(k->rsa);
                    136:                k->rsa = NULL;
                    137:                break;
                    138:        case KEY_DSA:
                    139:                if (k->dsa != NULL)
                    140:                        DSA_free(k->dsa);
                    141:                k->dsa = NULL;
                    142:                break;
1.12      markus    143:        case KEY_UNSPEC:
                    144:                break;
1.1       markus    145:        default:
                    146:                fatal("key_free: bad key type %d", k->type);
                    147:                break;
                    148:        }
                    149:        xfree(k);
                    150: }
1.55      jakob     151:
1.1       markus    152: int
1.55      jakob     153: key_equal(const Key *a, const Key *b)
1.1       markus    154: {
                    155:        if (a == NULL || b == NULL || a->type != b->type)
                    156:                return 0;
                    157:        switch (a->type) {
1.12      markus    158:        case KEY_RSA1:
1.1       markus    159:        case KEY_RSA:
                    160:                return a->rsa != NULL && b->rsa != NULL &&
                    161:                    BN_cmp(a->rsa->e, b->rsa->e) == 0 &&
                    162:                    BN_cmp(a->rsa->n, b->rsa->n) == 0;
                    163:        case KEY_DSA:
                    164:                return a->dsa != NULL && b->dsa != NULL &&
                    165:                    BN_cmp(a->dsa->p, b->dsa->p) == 0 &&
                    166:                    BN_cmp(a->dsa->q, b->dsa->q) == 0 &&
                    167:                    BN_cmp(a->dsa->g, b->dsa->g) == 0 &&
                    168:                    BN_cmp(a->dsa->pub_key, b->dsa->pub_key) == 0;
                    169:        default:
1.3       markus    170:                fatal("key_equal: bad key type %d", a->type);
1.1       markus    171:        }
                    172: }
                    173:
1.52      jakob     174: u_char*
1.55      jakob     175: key_fingerprint_raw(const Key *k, enum fp_type dgst_type,
                    176:     u_int *dgst_raw_length)
1.1       markus    177: {
1.41      markus    178:        const EVP_MD *md = NULL;
1.21      markus    179:        EVP_MD_CTX ctx;
1.13      markus    180:        u_char *blob = NULL;
1.19      jakob     181:        u_char *retval = NULL;
1.40      markus    182:        u_int len = 0;
1.3       markus    183:        int nlen, elen;
1.1       markus    184:
1.19      jakob     185:        *dgst_raw_length = 0;
                    186:
1.21      markus    187:        switch (dgst_type) {
                    188:        case SSH_FP_MD5:
                    189:                md = EVP_md5();
                    190:                break;
                    191:        case SSH_FP_SHA1:
                    192:                md = EVP_sha1();
                    193:                break;
                    194:        default:
                    195:                fatal("key_fingerprint_raw: bad digest type %d",
                    196:                    dgst_type);
                    197:        }
1.1       markus    198:        switch (k->type) {
1.12      markus    199:        case KEY_RSA1:
1.1       markus    200:                nlen = BN_num_bytes(k->rsa->n);
                    201:                elen = BN_num_bytes(k->rsa->e);
                    202:                len = nlen + elen;
1.3       markus    203:                blob = xmalloc(len);
                    204:                BN_bn2bin(k->rsa->n, blob);
                    205:                BN_bn2bin(k->rsa->e, blob + nlen);
1.1       markus    206:                break;
                    207:        case KEY_DSA:
1.12      markus    208:        case KEY_RSA:
                    209:                key_to_blob(k, &blob, &len);
                    210:                break;
                    211:        case KEY_UNSPEC:
                    212:                return retval;
1.1       markus    213:        default:
1.19      jakob     214:                fatal("key_fingerprint_raw: bad key type %d", k->type);
1.1       markus    215:                break;
                    216:        }
1.3       markus    217:        if (blob != NULL) {
1.19      jakob     218:                retval = xmalloc(EVP_MAX_MD_SIZE);
1.8       markus    219:                EVP_DigestInit(&ctx, md);
                    220:                EVP_DigestUpdate(&ctx, blob, len);
1.39      markus    221:                EVP_DigestFinal(&ctx, retval, dgst_raw_length);
1.3       markus    222:                memset(blob, 0, len);
                    223:                xfree(blob);
1.19      jakob     224:        } else {
                    225:                fatal("key_fingerprint_raw: blob is null");
1.1       markus    226:        }
1.19      jakob     227:        return retval;
                    228: }
                    229:
1.46      deraadt   230: static char *
                    231: key_fingerprint_hex(u_char *dgst_raw, u_int dgst_raw_len)
1.19      jakob     232: {
                    233:        char *retval;
1.58      djm       234:        u_int i;
1.19      jakob     235:
1.63      djm       236:        retval = xcalloc(1, dgst_raw_len * 3 + 1);
1.36      deraadt   237:        for (i = 0; i < dgst_raw_len; i++) {
1.19      jakob     238:                char hex[4];
                    239:                snprintf(hex, sizeof(hex), "%02x:", dgst_raw[i]);
1.54      avsm      240:                strlcat(retval, hex, dgst_raw_len * 3 + 1);
1.19      jakob     241:        }
1.54      avsm      242:
                    243:        /* Remove the trailing ':' character */
1.19      jakob     244:        retval[(dgst_raw_len * 3) - 1] = '\0';
                    245:        return retval;
                    246: }
                    247:
1.46      deraadt   248: static char *
                    249: key_fingerprint_bubblebabble(u_char *dgst_raw, u_int dgst_raw_len)
1.19      jakob     250: {
                    251:        char vowels[] = { 'a', 'e', 'i', 'o', 'u', 'y' };
                    252:        char consonants[] = { 'b', 'c', 'd', 'f', 'g', 'h', 'k', 'l', 'm',
                    253:            'n', 'p', 'r', 's', 't', 'v', 'z', 'x' };
1.20      jakob     254:        u_int i, j = 0, rounds, seed = 1;
1.19      jakob     255:        char *retval;
                    256:
                    257:        rounds = (dgst_raw_len / 2) + 1;
1.63      djm       258:        retval = xcalloc((rounds * 6), sizeof(char));
1.20      jakob     259:        retval[j++] = 'x';
                    260:        for (i = 0; i < rounds; i++) {
1.19      jakob     261:                u_int idx0, idx1, idx2, idx3, idx4;
1.20      jakob     262:                if ((i + 1 < rounds) || (dgst_raw_len % 2 != 0)) {
                    263:                        idx0 = (((((u_int)(dgst_raw[2 * i])) >> 6) & 3) +
1.19      jakob     264:                            seed) % 6;
1.20      jakob     265:                        idx1 = (((u_int)(dgst_raw[2 * i])) >> 2) & 15;
                    266:                        idx2 = ((((u_int)(dgst_raw[2 * i])) & 3) +
1.19      jakob     267:                            (seed / 6)) % 6;
1.20      jakob     268:                        retval[j++] = vowels[idx0];
                    269:                        retval[j++] = consonants[idx1];
                    270:                        retval[j++] = vowels[idx2];
                    271:                        if ((i + 1) < rounds) {
                    272:                                idx3 = (((u_int)(dgst_raw[(2 * i) + 1])) >> 4) & 15;
                    273:                                idx4 = (((u_int)(dgst_raw[(2 * i) + 1]))) & 15;
                    274:                                retval[j++] = consonants[idx3];
                    275:                                retval[j++] = '-';
                    276:                                retval[j++] = consonants[idx4];
1.19      jakob     277:                                seed = ((seed * 5) +
1.20      jakob     278:                                    ((((u_int)(dgst_raw[2 * i])) * 7) +
                    279:                                    ((u_int)(dgst_raw[(2 * i) + 1])))) % 36;
1.19      jakob     280:                        }
                    281:                } else {
                    282:                        idx0 = seed % 6;
                    283:                        idx1 = 16;
                    284:                        idx2 = seed / 6;
1.20      jakob     285:                        retval[j++] = vowels[idx0];
                    286:                        retval[j++] = consonants[idx1];
                    287:                        retval[j++] = vowels[idx2];
1.19      jakob     288:                }
                    289:        }
1.20      jakob     290:        retval[j++] = 'x';
                    291:        retval[j++] = '\0';
1.19      jakob     292:        return retval;
                    293: }
                    294:
1.70      grunk     295: /*
                    296:  * Draw an ASCII-Art representing the fingerprint so human brain can
                    297:  * profit from its built-in pattern recognition ability.
                    298:  * This technique is called "random art" and can be found in some
                    299:  * scientific publications like this original paper:
                    300:  *
                    301:  * "Hash Visualization: a New Technique to improve Real-World Security",
                    302:  * Perrig A. and Song D., 1999, International Workshop on Cryptographic
                    303:  * Techniques and E-Commerce (CrypTEC '99)
                    304:  * sparrow.ece.cmu.edu/~adrian/projects/validation/validation.pdf
                    305:  *
                    306:  * The subject came up in a talk by Dan Kaminsky, too.
                    307:  *
                    308:  * If you see the picture is different, the key is different.
                    309:  * If the picture looks the same, you still know nothing.
                    310:  *
                    311:  * The algorithm used here is a worm crawling over a discrete plane,
                    312:  * leaving a trace (augmenting the field) everywhere it goes.
                    313:  * Movement is taken from dgst_raw 2bit-wise.  Bumping into walls
                    314:  * makes the respective movement vector be ignored for this turn.
                    315:  * Graphs are not unambiguous, because circles in graphs can be
                    316:  * walked in either direction.
                    317:  */
1.74      grunk     318:
                    319: /*
                    320:  * Field sizes for the random art.  Have to be odd, so the starting point
                    321:  * can be in the exact middle of the picture, and FLDBASE should be >=8 .
                    322:  * Else pictures would be too dense, and drawing the frame would
                    323:  * fail, too, because the key type would not fit in anymore.
                    324:  */
                    325: #define        FLDBASE         8
                    326: #define        FLDSIZE_Y       (FLDBASE + 1)
                    327: #define        FLDSIZE_X       (FLDBASE * 2 + 1)
1.70      grunk     328: static char *
1.74      grunk     329: key_fingerprint_randomart(u_char *dgst_raw, u_int dgst_raw_len, const Key *k)
1.70      grunk     330: {
                    331:        /*
                    332:         * Chars to be used after each other every time the worm
                    333:         * intersects with itself.  Matter of taste.
                    334:         */
1.75    ! grunk     335:        char    *augmentation_string = " .o+=*BOX@%&#/^SE";
1.70      grunk     336:        char    *retval, *p;
1.71      otto      337:        u_char   field[FLDSIZE_X][FLDSIZE_Y];
1.70      grunk     338:        u_int    i, b;
                    339:        int      x, y;
1.72      grunk     340:        size_t   len = strlen(augmentation_string) - 1;
1.70      grunk     341:
                    342:        retval = xcalloc(1, (FLDSIZE_X + 3) * (FLDSIZE_Y + 2));
                    343:
                    344:        /* initialize field */
1.71      otto      345:        memset(field, 0, FLDSIZE_X * FLDSIZE_Y * sizeof(char));
1.70      grunk     346:        x = FLDSIZE_X / 2;
                    347:        y = FLDSIZE_Y / 2;
                    348:
                    349:        /* process raw key */
                    350:        for (i = 0; i < dgst_raw_len; i++) {
                    351:                int input;
                    352:                /* each byte conveys four 2-bit move commands */
                    353:                input = dgst_raw[i];
                    354:                for (b = 0; b < 4; b++) {
                    355:                        /* evaluate 2 bit, rest is shifted later */
                    356:                        x += (input & 0x1) ? 1 : -1;
                    357:                        y += (input & 0x2) ? 1 : -1;
                    358:
                    359:                        /* assure we are still in bounds */
                    360:                        x = MAX(x, 0);
                    361:                        y = MAX(y, 0);
                    362:                        x = MIN(x, FLDSIZE_X - 1);
                    363:                        y = MIN(y, FLDSIZE_Y - 1);
                    364:
                    365:                        /* augment the field */
1.71      otto      366:                        field[x][y]++;
1.70      grunk     367:                        input = input >> 2;
                    368:                }
                    369:        }
1.75    ! grunk     370:
        !           371:        /* mark starting point and end point*/
        !           372:        field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
        !           373:        field[x][y] = len;
1.70      grunk     374:
                    375:        /* fill in retval */
1.74      grunk     376:        snprintf(retval, 10, "+--[%4s]", key_type(k));
                    377:        p = strchr(retval, '\0');
1.70      grunk     378:
                    379:        /* output upper border */
1.74      grunk     380:        for (i = 0; i < FLDSIZE_X - 8; i++)
1.70      grunk     381:                *p++ = '-';
                    382:        *p++ = '+';
                    383:        *p++ = '\n';
                    384:
                    385:        /* output content */
                    386:        for (y = 0; y < FLDSIZE_Y; y++) {
                    387:                *p++ = '|';
                    388:                for (x = 0; x < FLDSIZE_X; x++)
1.72      grunk     389:                        *p++ = augmentation_string[MIN(field[x][y], len)];
1.70      grunk     390:                *p++ = '|';
                    391:                *p++ = '\n';
                    392:        }
                    393:
                    394:        /* output lower border */
                    395:        *p++ = '+';
                    396:        for (i = 0; i < FLDSIZE_X; i++)
                    397:                *p++ = '-';
                    398:        *p++ = '+';
                    399:
                    400:        return retval;
                    401: }
                    402:
1.46      deraadt   403: char *
1.55      jakob     404: key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
1.19      jakob     405: {
1.23      markus    406:        char *retval = NULL;
1.19      jakob     407:        u_char *dgst_raw;
1.39      markus    408:        u_int dgst_raw_len;
1.36      deraadt   409:
1.19      jakob     410:        dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
                    411:        if (!dgst_raw)
1.22      markus    412:                fatal("key_fingerprint: null from key_fingerprint_raw()");
1.35      deraadt   413:        switch (dgst_rep) {
1.19      jakob     414:        case SSH_FP_HEX:
                    415:                retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
                    416:                break;
                    417:        case SSH_FP_BUBBLEBABBLE:
                    418:                retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1.70      grunk     419:                break;
                    420:        case SSH_FP_RANDOMART:
1.74      grunk     421:                retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k);
1.19      jakob     422:                break;
                    423:        default:
                    424:                fatal("key_fingerprint_ex: bad digest representation %d",
                    425:                    dgst_rep);
                    426:                break;
                    427:        }
                    428:        memset(dgst_raw, 0, dgst_raw_len);
                    429:        xfree(dgst_raw);
1.1       markus    430:        return retval;
                    431: }
                    432:
                    433: /*
                    434:  * Reads a multiple-precision integer in decimal from the buffer, and advances
                    435:  * the pointer.  The integer must already be initialized.  This function is
                    436:  * permitted to modify the buffer.  This leaves *cpp to point just beyond the
                    437:  * last processed (and maybe modified) character.  Note that this may modify
                    438:  * the buffer containing the number.
                    439:  */
1.27      itojun    440: static int
1.1       markus    441: read_bignum(char **cpp, BIGNUM * value)
                    442: {
                    443:        char *cp = *cpp;
                    444:        int old;
                    445:
                    446:        /* Skip any leading whitespace. */
                    447:        for (; *cp == ' ' || *cp == '\t'; cp++)
                    448:                ;
                    449:
                    450:        /* Check that it begins with a decimal digit. */
                    451:        if (*cp < '0' || *cp > '9')
                    452:                return 0;
                    453:
                    454:        /* Save starting position. */
                    455:        *cpp = cp;
                    456:
                    457:        /* Move forward until all decimal digits skipped. */
                    458:        for (; *cp >= '0' && *cp <= '9'; cp++)
                    459:                ;
                    460:
                    461:        /* Save the old terminating character, and replace it by \0. */
                    462:        old = *cp;
                    463:        *cp = 0;
                    464:
                    465:        /* Parse the number. */
                    466:        if (BN_dec2bn(&value, *cpp) == 0)
                    467:                return 0;
                    468:
                    469:        /* Restore old terminating character. */
                    470:        *cp = old;
                    471:
                    472:        /* Move beyond the number and return success. */
                    473:        *cpp = cp;
                    474:        return 1;
                    475: }
1.45      deraadt   476:
1.27      itojun    477: static int
1.1       markus    478: write_bignum(FILE *f, BIGNUM *num)
                    479: {
                    480:        char *buf = BN_bn2dec(num);
                    481:        if (buf == NULL) {
                    482:                error("write_bignum: BN_bn2dec() failed");
                    483:                return 0;
                    484:        }
                    485:        fprintf(f, " %s", buf);
1.33      markus    486:        OPENSSL_free(buf);
1.1       markus    487:        return 1;
                    488: }
1.12      markus    489:
1.32      markus    490: /* returns 1 ok, -1 error */
1.12      markus    491: int
1.3       markus    492: key_read(Key *ret, char **cpp)
1.1       markus    493: {
1.3       markus    494:        Key *k;
1.12      markus    495:        int success = -1;
                    496:        char *cp, *space;
                    497:        int len, n, type;
                    498:        u_int bits;
1.13      markus    499:        u_char *blob;
1.3       markus    500:
                    501:        cp = *cpp;
                    502:
1.35      deraadt   503:        switch (ret->type) {
1.12      markus    504:        case KEY_RSA1:
1.3       markus    505:                /* Get number of bits. */
                    506:                if (*cp < '0' || *cp > '9')
1.12      markus    507:                        return -1;      /* Bad bit count... */
1.3       markus    508:                for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
                    509:                        bits = 10 * bits + *cp - '0';
1.1       markus    510:                if (bits == 0)
1.12      markus    511:                        return -1;
1.3       markus    512:                *cpp = cp;
1.1       markus    513:                /* Get public exponent, public modulus. */
                    514:                if (!read_bignum(cpp, ret->rsa->e))
1.12      markus    515:                        return -1;
1.1       markus    516:                if (!read_bignum(cpp, ret->rsa->n))
1.12      markus    517:                        return -1;
                    518:                success = 1;
1.1       markus    519:                break;
1.12      markus    520:        case KEY_UNSPEC:
                    521:        case KEY_RSA:
1.1       markus    522:        case KEY_DSA:
1.12      markus    523:                space = strchr(cp, ' ');
                    524:                if (space == NULL) {
1.50      markus    525:                        debug3("key_read: missing whitespace");
1.12      markus    526:                        return -1;
                    527:                }
                    528:                *space = '\0';
                    529:                type = key_type_from_name(cp);
                    530:                *space = ' ';
                    531:                if (type == KEY_UNSPEC) {
1.50      markus    532:                        debug3("key_read: missing keytype");
1.12      markus    533:                        return -1;
                    534:                }
                    535:                cp = space+1;
                    536:                if (*cp == '\0') {
                    537:                        debug3("key_read: short string");
                    538:                        return -1;
                    539:                }
                    540:                if (ret->type == KEY_UNSPEC) {
                    541:                        ret->type = type;
                    542:                } else if (ret->type != type) {
                    543:                        /* is a key, but different type */
                    544:                        debug3("key_read: type mismatch");
1.32      markus    545:                        return -1;
1.12      markus    546:                }
1.3       markus    547:                len = 2*strlen(cp);
                    548:                blob = xmalloc(len);
                    549:                n = uudecode(cp, blob, len);
1.6       markus    550:                if (n < 0) {
1.7       markus    551:                        error("key_read: uudecode %s failed", cp);
1.34      markus    552:                        xfree(blob);
1.12      markus    553:                        return -1;
1.6       markus    554:                }
1.53      markus    555:                k = key_from_blob(blob, (u_int)n);
1.34      markus    556:                xfree(blob);
1.7       markus    557:                if (k == NULL) {
1.12      markus    558:                        error("key_read: key_from_blob %s failed", cp);
                    559:                        return -1;
1.7       markus    560:                }
1.12      markus    561:                if (k->type != type) {
                    562:                        error("key_read: type mismatch: encoding error");
                    563:                        key_free(k);
                    564:                        return -1;
                    565:                }
                    566: /*XXXX*/
                    567:                if (ret->type == KEY_RSA) {
                    568:                        if (ret->rsa != NULL)
                    569:                                RSA_free(ret->rsa);
                    570:                        ret->rsa = k->rsa;
                    571:                        k->rsa = NULL;
                    572:                        success = 1;
                    573: #ifdef DEBUG_PK
                    574:                        RSA_print_fp(stderr, ret->rsa, 8);
                    575: #endif
                    576:                } else {
                    577:                        if (ret->dsa != NULL)
                    578:                                DSA_free(ret->dsa);
                    579:                        ret->dsa = k->dsa;
                    580:                        k->dsa = NULL;
                    581:                        success = 1;
                    582: #ifdef DEBUG_PK
                    583:                        DSA_print_fp(stderr, ret->dsa, 8);
                    584: #endif
                    585:                }
                    586: /*XXXX*/
1.34      markus    587:                key_free(k);
1.12      markus    588:                if (success != 1)
                    589:                        break;
1.7       markus    590:                /* advance cp: skip whitespace and data */
                    591:                while (*cp == ' ' || *cp == '\t')
                    592:                        cp++;
                    593:                while (*cp != '\0' && *cp != ' ' && *cp != '\t')
                    594:                        cp++;
                    595:                *cpp = cp;
1.1       markus    596:                break;
                    597:        default:
1.3       markus    598:                fatal("key_read: bad key type: %d", ret->type);
1.1       markus    599:                break;
                    600:        }
1.12      markus    601:        return success;
1.1       markus    602: }
1.45      deraadt   603:
1.1       markus    604: int
1.55      jakob     605: key_write(const Key *key, FILE *f)
1.1       markus    606: {
1.40      markus    607:        int n, success = 0;
                    608:        u_int len, bits = 0;
1.49      markus    609:        u_char *blob;
                    610:        char *uu;
1.1       markus    611:
1.12      markus    612:        if (key->type == KEY_RSA1 && key->rsa != NULL) {
1.1       markus    613:                /* size of modulus 'n' */
                    614:                bits = BN_num_bits(key->rsa->n);
                    615:                fprintf(f, "%u", bits);
                    616:                if (write_bignum(f, key->rsa->e) &&
                    617:                    write_bignum(f, key->rsa->n)) {
                    618:                        success = 1;
                    619:                } else {
                    620:                        error("key_write: failed for RSA key");
                    621:                }
1.12      markus    622:        } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
                    623:            (key->type == KEY_RSA && key->rsa != NULL)) {
                    624:                key_to_blob(key, &blob, &len);
1.3       markus    625:                uu = xmalloc(2*len);
1.5       markus    626:                n = uuencode(blob, len, uu, 2*len);
                    627:                if (n > 0) {
1.12      markus    628:                        fprintf(f, "%s %s", key_ssh_name(key), uu);
1.5       markus    629:                        success = 1;
                    630:                }
1.3       markus    631:                xfree(blob);
                    632:                xfree(uu);
1.1       markus    633:        }
                    634:        return success;
                    635: }
1.45      deraadt   636:
1.55      jakob     637: const char *
                    638: key_type(const Key *k)
1.4       markus    639: {
                    640:        switch (k->type) {
1.12      markus    641:        case KEY_RSA1:
                    642:                return "RSA1";
1.4       markus    643:        case KEY_RSA:
                    644:                return "RSA";
                    645:        case KEY_DSA:
                    646:                return "DSA";
                    647:        }
                    648:        return "unknown";
1.10      markus    649: }
1.45      deraadt   650:
1.55      jakob     651: const char *
                    652: key_ssh_name(const Key *k)
1.12      markus    653: {
                    654:        switch (k->type) {
                    655:        case KEY_RSA:
                    656:                return "ssh-rsa";
                    657:        case KEY_DSA:
                    658:                return "ssh-dss";
                    659:        }
                    660:        return "ssh-unknown";
                    661: }
1.45      deraadt   662:
1.12      markus    663: u_int
1.55      jakob     664: key_size(const Key *k)
1.35      deraadt   665: {
1.10      markus    666:        switch (k->type) {
1.12      markus    667:        case KEY_RSA1:
1.10      markus    668:        case KEY_RSA:
                    669:                return BN_num_bits(k->rsa->n);
                    670:        case KEY_DSA:
                    671:                return BN_num_bits(k->dsa->p);
                    672:        }
                    673:        return 0;
1.12      markus    674: }
                    675:
1.27      itojun    676: static RSA *
1.13      markus    677: rsa_generate_private_key(u_int bits)
1.12      markus    678: {
1.17      stevesk   679:        RSA *private;
1.61      deraadt   680:
1.17      stevesk   681:        private = RSA_generate_key(bits, 35, NULL, NULL);
                    682:        if (private == NULL)
                    683:                fatal("rsa_generate_private_key: key generation failed.");
                    684:        return private;
1.12      markus    685: }
                    686:
1.27      itojun    687: static DSA*
1.13      markus    688: dsa_generate_private_key(u_int bits)
1.12      markus    689: {
                    690:        DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
1.61      deraadt   691:
1.12      markus    692:        if (private == NULL)
                    693:                fatal("dsa_generate_private_key: DSA_generate_parameters failed");
                    694:        if (!DSA_generate_key(private))
1.17      stevesk   695:                fatal("dsa_generate_private_key: DSA_generate_key failed.");
                    696:        if (private == NULL)
                    697:                fatal("dsa_generate_private_key: NULL.");
1.12      markus    698:        return private;
                    699: }
                    700:
                    701: Key *
1.13      markus    702: key_generate(int type, u_int bits)
1.12      markus    703: {
                    704:        Key *k = key_new(KEY_UNSPEC);
                    705:        switch (type) {
1.17      stevesk   706:        case KEY_DSA:
1.12      markus    707:                k->dsa = dsa_generate_private_key(bits);
                    708:                break;
                    709:        case KEY_RSA:
                    710:        case KEY_RSA1:
                    711:                k->rsa = rsa_generate_private_key(bits);
                    712:                break;
                    713:        default:
1.17      stevesk   714:                fatal("key_generate: unknown type %d", type);
1.12      markus    715:        }
1.17      stevesk   716:        k->type = type;
1.12      markus    717:        return k;
                    718: }
                    719:
                    720: Key *
1.55      jakob     721: key_from_private(const Key *k)
1.12      markus    722: {
                    723:        Key *n = NULL;
                    724:        switch (k->type) {
1.17      stevesk   725:        case KEY_DSA:
1.12      markus    726:                n = key_new(k->type);
1.68      markus    727:                if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
                    728:                    (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
                    729:                    (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
                    730:                    (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
                    731:                        fatal("key_from_private: BN_copy failed");
1.12      markus    732:                break;
                    733:        case KEY_RSA:
                    734:        case KEY_RSA1:
                    735:                n = key_new(k->type);
1.68      markus    736:                if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
                    737:                    (BN_copy(n->rsa->e, k->rsa->e) == NULL))
                    738:                        fatal("key_from_private: BN_copy failed");
1.12      markus    739:                break;
                    740:        default:
1.17      stevesk   741:                fatal("key_from_private: unknown type %d", k->type);
1.12      markus    742:                break;
                    743:        }
                    744:        return n;
                    745: }
                    746:
                    747: int
                    748: key_type_from_name(char *name)
                    749: {
1.35      deraadt   750:        if (strcmp(name, "rsa1") == 0) {
1.12      markus    751:                return KEY_RSA1;
1.35      deraadt   752:        } else if (strcmp(name, "rsa") == 0) {
1.12      markus    753:                return KEY_RSA;
1.35      deraadt   754:        } else if (strcmp(name, "dsa") == 0) {
1.12      markus    755:                return KEY_DSA;
1.35      deraadt   756:        } else if (strcmp(name, "ssh-rsa") == 0) {
1.12      markus    757:                return KEY_RSA;
1.35      deraadt   758:        } else if (strcmp(name, "ssh-dss") == 0) {
1.12      markus    759:                return KEY_DSA;
                    760:        }
1.18      markus    761:        debug2("key_type_from_name: unknown key type '%s'", name);
1.12      markus    762:        return KEY_UNSPEC;
1.25      markus    763: }
                    764:
                    765: int
                    766: key_names_valid2(const char *names)
                    767: {
                    768:        char *s, *cp, *p;
                    769:
                    770:        if (names == NULL || strcmp(names, "") == 0)
                    771:                return 0;
                    772:        s = cp = xstrdup(names);
                    773:        for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36      deraadt   774:            (p = strsep(&cp, ","))) {
1.25      markus    775:                switch (key_type_from_name(p)) {
                    776:                case KEY_RSA1:
                    777:                case KEY_UNSPEC:
                    778:                        xfree(s);
                    779:                        return 0;
                    780:                }
                    781:        }
                    782:        debug3("key names ok: [%s]", names);
                    783:        xfree(s);
                    784:        return 1;
1.12      markus    785: }
                    786:
                    787: Key *
1.55      jakob     788: key_from_blob(const u_char *blob, u_int blen)
1.12      markus    789: {
                    790:        Buffer b;
                    791:        int rlen, type;
1.57      djm       792:        char *ktype = NULL;
1.12      markus    793:        Key *key = NULL;
                    794:
                    795: #ifdef DEBUG_PK
                    796:        dump_base64(stderr, blob, blen);
                    797: #endif
                    798:        buffer_init(&b);
                    799:        buffer_append(&b, blob, blen);
1.57      djm       800:        if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
                    801:                error("key_from_blob: can't read key type");
                    802:                goto out;
                    803:        }
                    804:
1.12      markus    805:        type = key_type_from_name(ktype);
                    806:
1.35      deraadt   807:        switch (type) {
1.12      markus    808:        case KEY_RSA:
                    809:                key = key_new(type);
1.57      djm       810:                if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
                    811:                    buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
                    812:                        error("key_from_blob: can't read rsa key");
                    813:                        key_free(key);
                    814:                        key = NULL;
                    815:                        goto out;
                    816:                }
1.12      markus    817: #ifdef DEBUG_PK
                    818:                RSA_print_fp(stderr, key->rsa, 8);
                    819: #endif
                    820:                break;
                    821:        case KEY_DSA:
                    822:                key = key_new(type);
1.57      djm       823:                if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
                    824:                    buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
                    825:                    buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
                    826:                    buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
                    827:                        error("key_from_blob: can't read dsa key");
                    828:                        key_free(key);
                    829:                        key = NULL;
                    830:                        goto out;
                    831:                }
1.12      markus    832: #ifdef DEBUG_PK
                    833:                DSA_print_fp(stderr, key->dsa, 8);
                    834: #endif
                    835:                break;
                    836:        case KEY_UNSPEC:
                    837:                key = key_new(type);
                    838:                break;
                    839:        default:
                    840:                error("key_from_blob: cannot handle type %s", ktype);
1.57      djm       841:                goto out;
1.12      markus    842:        }
                    843:        rlen = buffer_len(&b);
                    844:        if (key != NULL && rlen != 0)
                    845:                error("key_from_blob: remaining bytes in key blob %d", rlen);
1.57      djm       846:  out:
                    847:        if (ktype != NULL)
                    848:                xfree(ktype);
1.12      markus    849:        buffer_free(&b);
                    850:        return key;
                    851: }
                    852:
                    853: int
1.55      jakob     854: key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1.12      markus    855: {
                    856:        Buffer b;
                    857:        int len;
                    858:
                    859:        if (key == NULL) {
                    860:                error("key_to_blob: key == NULL");
                    861:                return 0;
                    862:        }
                    863:        buffer_init(&b);
1.35      deraadt   864:        switch (key->type) {
1.12      markus    865:        case KEY_DSA:
                    866:                buffer_put_cstring(&b, key_ssh_name(key));
                    867:                buffer_put_bignum2(&b, key->dsa->p);
                    868:                buffer_put_bignum2(&b, key->dsa->q);
                    869:                buffer_put_bignum2(&b, key->dsa->g);
                    870:                buffer_put_bignum2(&b, key->dsa->pub_key);
                    871:                break;
                    872:        case KEY_RSA:
                    873:                buffer_put_cstring(&b, key_ssh_name(key));
1.14      markus    874:                buffer_put_bignum2(&b, key->rsa->e);
1.12      markus    875:                buffer_put_bignum2(&b, key->rsa->n);
                    876:                break;
                    877:        default:
1.31      markus    878:                error("key_to_blob: unsupported key type %d", key->type);
                    879:                buffer_free(&b);
                    880:                return 0;
1.12      markus    881:        }
                    882:        len = buffer_len(&b);
1.48      markus    883:        if (lenp != NULL)
                    884:                *lenp = len;
                    885:        if (blobp != NULL) {
                    886:                *blobp = xmalloc(len);
                    887:                memcpy(*blobp, buffer_ptr(&b), len);
                    888:        }
1.12      markus    889:        memset(buffer_ptr(&b), 0, len);
                    890:        buffer_free(&b);
                    891:        return len;
                    892: }
                    893:
                    894: int
                    895: key_sign(
1.55      jakob     896:     const Key *key,
1.40      markus    897:     u_char **sigp, u_int *lenp,
1.55      jakob     898:     const u_char *data, u_int datalen)
1.12      markus    899: {
1.35      deraadt   900:        switch (key->type) {
1.12      markus    901:        case KEY_DSA:
                    902:                return ssh_dss_sign(key, sigp, lenp, data, datalen);
                    903:        case KEY_RSA:
                    904:                return ssh_rsa_sign(key, sigp, lenp, data, datalen);
                    905:        default:
1.56      markus    906:                error("key_sign: invalid key type %d", key->type);
1.12      markus    907:                return -1;
                    908:        }
                    909: }
                    910:
1.44      markus    911: /*
                    912:  * key_verify returns 1 for a correct signature, 0 for an incorrect signature
                    913:  * and -1 on error.
                    914:  */
1.12      markus    915: int
                    916: key_verify(
1.55      jakob     917:     const Key *key,
                    918:     const u_char *signature, u_int signaturelen,
                    919:     const u_char *data, u_int datalen)
1.12      markus    920: {
1.26      markus    921:        if (signaturelen == 0)
                    922:                return -1;
                    923:
1.35      deraadt   924:        switch (key->type) {
1.12      markus    925:        case KEY_DSA:
                    926:                return ssh_dss_verify(key, signature, signaturelen, data, datalen);
                    927:        case KEY_RSA:
                    928:                return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
                    929:        default:
1.56      markus    930:                error("key_verify: invalid key type %d", key->type);
1.12      markus    931:                return -1;
                    932:        }
1.42      markus    933: }
                    934:
                    935: /* Converts a private to a public key */
                    936: Key *
1.55      jakob     937: key_demote(const Key *k)
1.42      markus    938: {
                    939:        Key *pk;
1.43      markus    940:
1.63      djm       941:        pk = xcalloc(1, sizeof(*pk));
1.42      markus    942:        pk->type = k->type;
                    943:        pk->flags = k->flags;
                    944:        pk->dsa = NULL;
                    945:        pk->rsa = NULL;
                    946:
                    947:        switch (k->type) {
                    948:        case KEY_RSA1:
                    949:        case KEY_RSA:
                    950:                if ((pk->rsa = RSA_new()) == NULL)
                    951:                        fatal("key_demote: RSA_new failed");
                    952:                if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
                    953:                        fatal("key_demote: BN_dup failed");
                    954:                if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
                    955:                        fatal("key_demote: BN_dup failed");
                    956:                break;
                    957:        case KEY_DSA:
                    958:                if ((pk->dsa = DSA_new()) == NULL)
                    959:                        fatal("key_demote: DSA_new failed");
                    960:                if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
                    961:                        fatal("key_demote: BN_dup failed");
                    962:                if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
                    963:                        fatal("key_demote: BN_dup failed");
                    964:                if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
                    965:                        fatal("key_demote: BN_dup failed");
                    966:                if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
                    967:                        fatal("key_demote: BN_dup failed");
                    968:                break;
                    969:        default:
                    970:                fatal("key_free: bad key type %d", k->type);
                    971:                break;
                    972:        }
                    973:
                    974:        return (pk);
1.4       markus    975: }