[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.78

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