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

1.79    ! grunk       1: /* $OpenBSD: key.c,v 1.78 2008/07/07 23:32:51 stevesk 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.79    ! grunk     368:                        if (field[x][y] < len - 2)
        !           369:                                field[x][y]++;
1.70      grunk     370:                        input = input >> 2;
                    371:                }
                    372:        }
1.75      grunk     373:
                    374:        /* mark starting point and end point*/
                    375:        field[FLDSIZE_X / 2][FLDSIZE_Y / 2] = len - 1;
                    376:        field[x][y] = len;
1.70      grunk     377:
                    378:        /* fill in retval */
1.77      otto      379:        snprintf(retval, FLDSIZE_X, "+--[%4s %4u]", key_type(k), key_size(k));
1.74      grunk     380:        p = strchr(retval, '\0');
1.70      grunk     381:
                    382:        /* output upper border */
1.77      otto      383:        for (i = p - retval - 1; i < FLDSIZE_X; i++)
1.70      grunk     384:                *p++ = '-';
                    385:        *p++ = '+';
                    386:        *p++ = '\n';
                    387:
                    388:        /* output content */
                    389:        for (y = 0; y < FLDSIZE_Y; y++) {
                    390:                *p++ = '|';
                    391:                for (x = 0; x < FLDSIZE_X; x++)
1.72      grunk     392:                        *p++ = augmentation_string[MIN(field[x][y], len)];
1.70      grunk     393:                *p++ = '|';
                    394:                *p++ = '\n';
                    395:        }
                    396:
                    397:        /* output lower border */
                    398:        *p++ = '+';
                    399:        for (i = 0; i < FLDSIZE_X; i++)
                    400:                *p++ = '-';
                    401:        *p++ = '+';
                    402:
                    403:        return retval;
                    404: }
                    405:
1.46      deraadt   406: char *
1.55      jakob     407: key_fingerprint(const Key *k, enum fp_type dgst_type, enum fp_rep dgst_rep)
1.19      jakob     408: {
1.23      markus    409:        char *retval = NULL;
1.19      jakob     410:        u_char *dgst_raw;
1.39      markus    411:        u_int dgst_raw_len;
1.36      deraadt   412:
1.19      jakob     413:        dgst_raw = key_fingerprint_raw(k, dgst_type, &dgst_raw_len);
                    414:        if (!dgst_raw)
1.22      markus    415:                fatal("key_fingerprint: null from key_fingerprint_raw()");
1.35      deraadt   416:        switch (dgst_rep) {
1.19      jakob     417:        case SSH_FP_HEX:
                    418:                retval = key_fingerprint_hex(dgst_raw, dgst_raw_len);
                    419:                break;
                    420:        case SSH_FP_BUBBLEBABBLE:
                    421:                retval = key_fingerprint_bubblebabble(dgst_raw, dgst_raw_len);
1.70      grunk     422:                break;
                    423:        case SSH_FP_RANDOMART:
1.74      grunk     424:                retval = key_fingerprint_randomart(dgst_raw, dgst_raw_len, k);
1.19      jakob     425:                break;
                    426:        default:
                    427:                fatal("key_fingerprint_ex: bad digest representation %d",
                    428:                    dgst_rep);
                    429:                break;
                    430:        }
                    431:        memset(dgst_raw, 0, dgst_raw_len);
                    432:        xfree(dgst_raw);
1.1       markus    433:        return retval;
                    434: }
                    435:
                    436: /*
                    437:  * Reads a multiple-precision integer in decimal from the buffer, and advances
                    438:  * the pointer.  The integer must already be initialized.  This function is
                    439:  * permitted to modify the buffer.  This leaves *cpp to point just beyond the
                    440:  * last processed (and maybe modified) character.  Note that this may modify
                    441:  * the buffer containing the number.
                    442:  */
1.27      itojun    443: static int
1.1       markus    444: read_bignum(char **cpp, BIGNUM * value)
                    445: {
                    446:        char *cp = *cpp;
                    447:        int old;
                    448:
                    449:        /* Skip any leading whitespace. */
                    450:        for (; *cp == ' ' || *cp == '\t'; cp++)
                    451:                ;
                    452:
                    453:        /* Check that it begins with a decimal digit. */
                    454:        if (*cp < '0' || *cp > '9')
                    455:                return 0;
                    456:
                    457:        /* Save starting position. */
                    458:        *cpp = cp;
                    459:
                    460:        /* Move forward until all decimal digits skipped. */
                    461:        for (; *cp >= '0' && *cp <= '9'; cp++)
                    462:                ;
                    463:
                    464:        /* Save the old terminating character, and replace it by \0. */
                    465:        old = *cp;
                    466:        *cp = 0;
                    467:
                    468:        /* Parse the number. */
                    469:        if (BN_dec2bn(&value, *cpp) == 0)
                    470:                return 0;
                    471:
                    472:        /* Restore old terminating character. */
                    473:        *cp = old;
                    474:
                    475:        /* Move beyond the number and return success. */
                    476:        *cpp = cp;
                    477:        return 1;
                    478: }
1.45      deraadt   479:
1.27      itojun    480: static int
1.1       markus    481: write_bignum(FILE *f, BIGNUM *num)
                    482: {
                    483:        char *buf = BN_bn2dec(num);
                    484:        if (buf == NULL) {
                    485:                error("write_bignum: BN_bn2dec() failed");
                    486:                return 0;
                    487:        }
                    488:        fprintf(f, " %s", buf);
1.33      markus    489:        OPENSSL_free(buf);
1.1       markus    490:        return 1;
                    491: }
1.12      markus    492:
1.32      markus    493: /* returns 1 ok, -1 error */
1.12      markus    494: int
1.3       markus    495: key_read(Key *ret, char **cpp)
1.1       markus    496: {
1.3       markus    497:        Key *k;
1.12      markus    498:        int success = -1;
                    499:        char *cp, *space;
                    500:        int len, n, type;
                    501:        u_int bits;
1.13      markus    502:        u_char *blob;
1.3       markus    503:
                    504:        cp = *cpp;
                    505:
1.35      deraadt   506:        switch (ret->type) {
1.12      markus    507:        case KEY_RSA1:
1.3       markus    508:                /* Get number of bits. */
                    509:                if (*cp < '0' || *cp > '9')
1.12      markus    510:                        return -1;      /* Bad bit count... */
1.3       markus    511:                for (bits = 0; *cp >= '0' && *cp <= '9'; cp++)
                    512:                        bits = 10 * bits + *cp - '0';
1.1       markus    513:                if (bits == 0)
1.12      markus    514:                        return -1;
1.3       markus    515:                *cpp = cp;
1.1       markus    516:                /* Get public exponent, public modulus. */
                    517:                if (!read_bignum(cpp, ret->rsa->e))
1.12      markus    518:                        return -1;
1.1       markus    519:                if (!read_bignum(cpp, ret->rsa->n))
1.12      markus    520:                        return -1;
                    521:                success = 1;
1.1       markus    522:                break;
1.12      markus    523:        case KEY_UNSPEC:
                    524:        case KEY_RSA:
1.1       markus    525:        case KEY_DSA:
1.12      markus    526:                space = strchr(cp, ' ');
                    527:                if (space == NULL) {
1.50      markus    528:                        debug3("key_read: missing whitespace");
1.12      markus    529:                        return -1;
                    530:                }
                    531:                *space = '\0';
                    532:                type = key_type_from_name(cp);
                    533:                *space = ' ';
                    534:                if (type == KEY_UNSPEC) {
1.50      markus    535:                        debug3("key_read: missing keytype");
1.12      markus    536:                        return -1;
                    537:                }
                    538:                cp = space+1;
                    539:                if (*cp == '\0') {
                    540:                        debug3("key_read: short string");
                    541:                        return -1;
                    542:                }
                    543:                if (ret->type == KEY_UNSPEC) {
                    544:                        ret->type = type;
                    545:                } else if (ret->type != type) {
                    546:                        /* is a key, but different type */
                    547:                        debug3("key_read: type mismatch");
1.32      markus    548:                        return -1;
1.12      markus    549:                }
1.3       markus    550:                len = 2*strlen(cp);
                    551:                blob = xmalloc(len);
                    552:                n = uudecode(cp, blob, len);
1.6       markus    553:                if (n < 0) {
1.7       markus    554:                        error("key_read: uudecode %s failed", cp);
1.34      markus    555:                        xfree(blob);
1.12      markus    556:                        return -1;
1.6       markus    557:                }
1.53      markus    558:                k = key_from_blob(blob, (u_int)n);
1.34      markus    559:                xfree(blob);
1.7       markus    560:                if (k == NULL) {
1.12      markus    561:                        error("key_read: key_from_blob %s failed", cp);
                    562:                        return -1;
1.7       markus    563:                }
1.12      markus    564:                if (k->type != type) {
                    565:                        error("key_read: type mismatch: encoding error");
                    566:                        key_free(k);
                    567:                        return -1;
                    568:                }
                    569: /*XXXX*/
                    570:                if (ret->type == KEY_RSA) {
                    571:                        if (ret->rsa != NULL)
                    572:                                RSA_free(ret->rsa);
                    573:                        ret->rsa = k->rsa;
                    574:                        k->rsa = NULL;
                    575:                        success = 1;
                    576: #ifdef DEBUG_PK
                    577:                        RSA_print_fp(stderr, ret->rsa, 8);
                    578: #endif
                    579:                } else {
                    580:                        if (ret->dsa != NULL)
                    581:                                DSA_free(ret->dsa);
                    582:                        ret->dsa = k->dsa;
                    583:                        k->dsa = NULL;
                    584:                        success = 1;
                    585: #ifdef DEBUG_PK
                    586:                        DSA_print_fp(stderr, ret->dsa, 8);
                    587: #endif
                    588:                }
                    589: /*XXXX*/
1.34      markus    590:                key_free(k);
1.12      markus    591:                if (success != 1)
                    592:                        break;
1.7       markus    593:                /* advance cp: skip whitespace and data */
                    594:                while (*cp == ' ' || *cp == '\t')
                    595:                        cp++;
                    596:                while (*cp != '\0' && *cp != ' ' && *cp != '\t')
                    597:                        cp++;
                    598:                *cpp = cp;
1.1       markus    599:                break;
                    600:        default:
1.3       markus    601:                fatal("key_read: bad key type: %d", ret->type);
1.1       markus    602:                break;
                    603:        }
1.12      markus    604:        return success;
1.1       markus    605: }
1.45      deraadt   606:
1.1       markus    607: int
1.55      jakob     608: key_write(const Key *key, FILE *f)
1.1       markus    609: {
1.40      markus    610:        int n, success = 0;
                    611:        u_int len, bits = 0;
1.49      markus    612:        u_char *blob;
                    613:        char *uu;
1.1       markus    614:
1.12      markus    615:        if (key->type == KEY_RSA1 && key->rsa != NULL) {
1.1       markus    616:                /* size of modulus 'n' */
                    617:                bits = BN_num_bits(key->rsa->n);
                    618:                fprintf(f, "%u", bits);
                    619:                if (write_bignum(f, key->rsa->e) &&
                    620:                    write_bignum(f, key->rsa->n)) {
                    621:                        success = 1;
                    622:                } else {
                    623:                        error("key_write: failed for RSA key");
                    624:                }
1.12      markus    625:        } else if ((key->type == KEY_DSA && key->dsa != NULL) ||
                    626:            (key->type == KEY_RSA && key->rsa != NULL)) {
                    627:                key_to_blob(key, &blob, &len);
1.3       markus    628:                uu = xmalloc(2*len);
1.5       markus    629:                n = uuencode(blob, len, uu, 2*len);
                    630:                if (n > 0) {
1.12      markus    631:                        fprintf(f, "%s %s", key_ssh_name(key), uu);
1.5       markus    632:                        success = 1;
                    633:                }
1.3       markus    634:                xfree(blob);
                    635:                xfree(uu);
1.1       markus    636:        }
                    637:        return success;
                    638: }
1.45      deraadt   639:
1.55      jakob     640: const char *
                    641: key_type(const Key *k)
1.4       markus    642: {
                    643:        switch (k->type) {
1.12      markus    644:        case KEY_RSA1:
                    645:                return "RSA1";
1.4       markus    646:        case KEY_RSA:
                    647:                return "RSA";
                    648:        case KEY_DSA:
                    649:                return "DSA";
                    650:        }
                    651:        return "unknown";
1.10      markus    652: }
1.45      deraadt   653:
1.55      jakob     654: const char *
                    655: key_ssh_name(const Key *k)
1.12      markus    656: {
                    657:        switch (k->type) {
                    658:        case KEY_RSA:
                    659:                return "ssh-rsa";
                    660:        case KEY_DSA:
                    661:                return "ssh-dss";
                    662:        }
                    663:        return "ssh-unknown";
                    664: }
1.45      deraadt   665:
1.12      markus    666: u_int
1.55      jakob     667: key_size(const Key *k)
1.35      deraadt   668: {
1.10      markus    669:        switch (k->type) {
1.12      markus    670:        case KEY_RSA1:
1.10      markus    671:        case KEY_RSA:
                    672:                return BN_num_bits(k->rsa->n);
                    673:        case KEY_DSA:
                    674:                return BN_num_bits(k->dsa->p);
                    675:        }
                    676:        return 0;
1.12      markus    677: }
                    678:
1.27      itojun    679: static RSA *
1.13      markus    680: rsa_generate_private_key(u_int bits)
1.12      markus    681: {
1.17      stevesk   682:        RSA *private;
1.61      deraadt   683:
1.17      stevesk   684:        private = RSA_generate_key(bits, 35, NULL, NULL);
                    685:        if (private == NULL)
                    686:                fatal("rsa_generate_private_key: key generation failed.");
                    687:        return private;
1.12      markus    688: }
                    689:
1.27      itojun    690: static DSA*
1.13      markus    691: dsa_generate_private_key(u_int bits)
1.12      markus    692: {
                    693:        DSA *private = DSA_generate_parameters(bits, NULL, 0, NULL, NULL, NULL, NULL);
1.61      deraadt   694:
1.12      markus    695:        if (private == NULL)
                    696:                fatal("dsa_generate_private_key: DSA_generate_parameters failed");
                    697:        if (!DSA_generate_key(private))
1.17      stevesk   698:                fatal("dsa_generate_private_key: DSA_generate_key failed.");
                    699:        if (private == NULL)
                    700:                fatal("dsa_generate_private_key: NULL.");
1.12      markus    701:        return private;
                    702: }
                    703:
                    704: Key *
1.13      markus    705: key_generate(int type, u_int bits)
1.12      markus    706: {
                    707:        Key *k = key_new(KEY_UNSPEC);
                    708:        switch (type) {
1.17      stevesk   709:        case KEY_DSA:
1.12      markus    710:                k->dsa = dsa_generate_private_key(bits);
                    711:                break;
                    712:        case KEY_RSA:
                    713:        case KEY_RSA1:
                    714:                k->rsa = rsa_generate_private_key(bits);
                    715:                break;
                    716:        default:
1.17      stevesk   717:                fatal("key_generate: unknown type %d", type);
1.12      markus    718:        }
1.17      stevesk   719:        k->type = type;
1.12      markus    720:        return k;
                    721: }
                    722:
                    723: Key *
1.55      jakob     724: key_from_private(const Key *k)
1.12      markus    725: {
                    726:        Key *n = NULL;
                    727:        switch (k->type) {
1.17      stevesk   728:        case KEY_DSA:
1.12      markus    729:                n = key_new(k->type);
1.68      markus    730:                if ((BN_copy(n->dsa->p, k->dsa->p) == NULL) ||
                    731:                    (BN_copy(n->dsa->q, k->dsa->q) == NULL) ||
                    732:                    (BN_copy(n->dsa->g, k->dsa->g) == NULL) ||
                    733:                    (BN_copy(n->dsa->pub_key, k->dsa->pub_key) == NULL))
                    734:                        fatal("key_from_private: BN_copy failed");
1.12      markus    735:                break;
                    736:        case KEY_RSA:
                    737:        case KEY_RSA1:
                    738:                n = key_new(k->type);
1.68      markus    739:                if ((BN_copy(n->rsa->n, k->rsa->n) == NULL) ||
                    740:                    (BN_copy(n->rsa->e, k->rsa->e) == NULL))
                    741:                        fatal("key_from_private: BN_copy failed");
1.12      markus    742:                break;
                    743:        default:
1.17      stevesk   744:                fatal("key_from_private: unknown type %d", k->type);
1.12      markus    745:                break;
                    746:        }
                    747:        return n;
                    748: }
                    749:
                    750: int
                    751: key_type_from_name(char *name)
                    752: {
1.35      deraadt   753:        if (strcmp(name, "rsa1") == 0) {
1.12      markus    754:                return KEY_RSA1;
1.35      deraadt   755:        } else if (strcmp(name, "rsa") == 0) {
1.12      markus    756:                return KEY_RSA;
1.35      deraadt   757:        } else if (strcmp(name, "dsa") == 0) {
1.12      markus    758:                return KEY_DSA;
1.35      deraadt   759:        } else if (strcmp(name, "ssh-rsa") == 0) {
1.12      markus    760:                return KEY_RSA;
1.35      deraadt   761:        } else if (strcmp(name, "ssh-dss") == 0) {
1.12      markus    762:                return KEY_DSA;
                    763:        }
1.18      markus    764:        debug2("key_type_from_name: unknown key type '%s'", name);
1.12      markus    765:        return KEY_UNSPEC;
1.25      markus    766: }
                    767:
                    768: int
                    769: key_names_valid2(const char *names)
                    770: {
                    771:        char *s, *cp, *p;
                    772:
                    773:        if (names == NULL || strcmp(names, "") == 0)
                    774:                return 0;
                    775:        s = cp = xstrdup(names);
                    776:        for ((p = strsep(&cp, ",")); p && *p != '\0';
1.36      deraadt   777:            (p = strsep(&cp, ","))) {
1.25      markus    778:                switch (key_type_from_name(p)) {
                    779:                case KEY_RSA1:
                    780:                case KEY_UNSPEC:
                    781:                        xfree(s);
                    782:                        return 0;
                    783:                }
                    784:        }
                    785:        debug3("key names ok: [%s]", names);
                    786:        xfree(s);
                    787:        return 1;
1.12      markus    788: }
                    789:
                    790: Key *
1.55      jakob     791: key_from_blob(const u_char *blob, u_int blen)
1.12      markus    792: {
                    793:        Buffer b;
                    794:        int rlen, type;
1.57      djm       795:        char *ktype = NULL;
1.12      markus    796:        Key *key = NULL;
                    797:
                    798: #ifdef DEBUG_PK
                    799:        dump_base64(stderr, blob, blen);
                    800: #endif
                    801:        buffer_init(&b);
                    802:        buffer_append(&b, blob, blen);
1.57      djm       803:        if ((ktype = buffer_get_string_ret(&b, NULL)) == NULL) {
                    804:                error("key_from_blob: can't read key type");
                    805:                goto out;
                    806:        }
                    807:
1.12      markus    808:        type = key_type_from_name(ktype);
                    809:
1.35      deraadt   810:        switch (type) {
1.12      markus    811:        case KEY_RSA:
                    812:                key = key_new(type);
1.57      djm       813:                if (buffer_get_bignum2_ret(&b, key->rsa->e) == -1 ||
                    814:                    buffer_get_bignum2_ret(&b, key->rsa->n) == -1) {
                    815:                        error("key_from_blob: can't read rsa key");
                    816:                        key_free(key);
                    817:                        key = NULL;
                    818:                        goto out;
                    819:                }
1.12      markus    820: #ifdef DEBUG_PK
                    821:                RSA_print_fp(stderr, key->rsa, 8);
                    822: #endif
                    823:                break;
                    824:        case KEY_DSA:
                    825:                key = key_new(type);
1.57      djm       826:                if (buffer_get_bignum2_ret(&b, key->dsa->p) == -1 ||
                    827:                    buffer_get_bignum2_ret(&b, key->dsa->q) == -1 ||
                    828:                    buffer_get_bignum2_ret(&b, key->dsa->g) == -1 ||
                    829:                    buffer_get_bignum2_ret(&b, key->dsa->pub_key) == -1) {
                    830:                        error("key_from_blob: can't read dsa key");
                    831:                        key_free(key);
                    832:                        key = NULL;
                    833:                        goto out;
                    834:                }
1.12      markus    835: #ifdef DEBUG_PK
                    836:                DSA_print_fp(stderr, key->dsa, 8);
                    837: #endif
                    838:                break;
                    839:        case KEY_UNSPEC:
                    840:                key = key_new(type);
                    841:                break;
                    842:        default:
                    843:                error("key_from_blob: cannot handle type %s", ktype);
1.57      djm       844:                goto out;
1.12      markus    845:        }
                    846:        rlen = buffer_len(&b);
                    847:        if (key != NULL && rlen != 0)
                    848:                error("key_from_blob: remaining bytes in key blob %d", rlen);
1.57      djm       849:  out:
                    850:        if (ktype != NULL)
                    851:                xfree(ktype);
1.12      markus    852:        buffer_free(&b);
                    853:        return key;
                    854: }
                    855:
                    856: int
1.55      jakob     857: key_to_blob(const Key *key, u_char **blobp, u_int *lenp)
1.12      markus    858: {
                    859:        Buffer b;
                    860:        int len;
                    861:
                    862:        if (key == NULL) {
                    863:                error("key_to_blob: key == NULL");
                    864:                return 0;
                    865:        }
                    866:        buffer_init(&b);
1.35      deraadt   867:        switch (key->type) {
1.12      markus    868:        case KEY_DSA:
                    869:                buffer_put_cstring(&b, key_ssh_name(key));
                    870:                buffer_put_bignum2(&b, key->dsa->p);
                    871:                buffer_put_bignum2(&b, key->dsa->q);
                    872:                buffer_put_bignum2(&b, key->dsa->g);
                    873:                buffer_put_bignum2(&b, key->dsa->pub_key);
                    874:                break;
                    875:        case KEY_RSA:
                    876:                buffer_put_cstring(&b, key_ssh_name(key));
1.14      markus    877:                buffer_put_bignum2(&b, key->rsa->e);
1.12      markus    878:                buffer_put_bignum2(&b, key->rsa->n);
                    879:                break;
                    880:        default:
1.31      markus    881:                error("key_to_blob: unsupported key type %d", key->type);
                    882:                buffer_free(&b);
                    883:                return 0;
1.12      markus    884:        }
                    885:        len = buffer_len(&b);
1.48      markus    886:        if (lenp != NULL)
                    887:                *lenp = len;
                    888:        if (blobp != NULL) {
                    889:                *blobp = xmalloc(len);
                    890:                memcpy(*blobp, buffer_ptr(&b), len);
                    891:        }
1.12      markus    892:        memset(buffer_ptr(&b), 0, len);
                    893:        buffer_free(&b);
                    894:        return len;
                    895: }
                    896:
                    897: int
                    898: key_sign(
1.55      jakob     899:     const Key *key,
1.40      markus    900:     u_char **sigp, u_int *lenp,
1.55      jakob     901:     const u_char *data, u_int datalen)
1.12      markus    902: {
1.35      deraadt   903:        switch (key->type) {
1.12      markus    904:        case KEY_DSA:
                    905:                return ssh_dss_sign(key, sigp, lenp, data, datalen);
                    906:        case KEY_RSA:
                    907:                return ssh_rsa_sign(key, sigp, lenp, data, datalen);
                    908:        default:
1.56      markus    909:                error("key_sign: invalid key type %d", key->type);
1.12      markus    910:                return -1;
                    911:        }
                    912: }
                    913:
1.44      markus    914: /*
                    915:  * key_verify returns 1 for a correct signature, 0 for an incorrect signature
                    916:  * and -1 on error.
                    917:  */
1.12      markus    918: int
                    919: key_verify(
1.55      jakob     920:     const Key *key,
                    921:     const u_char *signature, u_int signaturelen,
                    922:     const u_char *data, u_int datalen)
1.12      markus    923: {
1.26      markus    924:        if (signaturelen == 0)
                    925:                return -1;
                    926:
1.35      deraadt   927:        switch (key->type) {
1.12      markus    928:        case KEY_DSA:
                    929:                return ssh_dss_verify(key, signature, signaturelen, data, datalen);
                    930:        case KEY_RSA:
                    931:                return ssh_rsa_verify(key, signature, signaturelen, data, datalen);
                    932:        default:
1.56      markus    933:                error("key_verify: invalid key type %d", key->type);
1.12      markus    934:                return -1;
                    935:        }
1.42      markus    936: }
                    937:
                    938: /* Converts a private to a public key */
                    939: Key *
1.55      jakob     940: key_demote(const Key *k)
1.42      markus    941: {
                    942:        Key *pk;
1.43      markus    943:
1.63      djm       944:        pk = xcalloc(1, sizeof(*pk));
1.42      markus    945:        pk->type = k->type;
                    946:        pk->flags = k->flags;
                    947:        pk->dsa = NULL;
                    948:        pk->rsa = NULL;
                    949:
                    950:        switch (k->type) {
                    951:        case KEY_RSA1:
                    952:        case KEY_RSA:
                    953:                if ((pk->rsa = RSA_new()) == NULL)
                    954:                        fatal("key_demote: RSA_new failed");
                    955:                if ((pk->rsa->e = BN_dup(k->rsa->e)) == NULL)
                    956:                        fatal("key_demote: BN_dup failed");
                    957:                if ((pk->rsa->n = BN_dup(k->rsa->n)) == NULL)
                    958:                        fatal("key_demote: BN_dup failed");
                    959:                break;
                    960:        case KEY_DSA:
                    961:                if ((pk->dsa = DSA_new()) == NULL)
                    962:                        fatal("key_demote: DSA_new failed");
                    963:                if ((pk->dsa->p = BN_dup(k->dsa->p)) == NULL)
                    964:                        fatal("key_demote: BN_dup failed");
                    965:                if ((pk->dsa->q = BN_dup(k->dsa->q)) == NULL)
                    966:                        fatal("key_demote: BN_dup failed");
                    967:                if ((pk->dsa->g = BN_dup(k->dsa->g)) == NULL)
                    968:                        fatal("key_demote: BN_dup failed");
                    969:                if ((pk->dsa->pub_key = BN_dup(k->dsa->pub_key)) == NULL)
                    970:                        fatal("key_demote: BN_dup failed");
                    971:                break;
                    972:        default:
                    973:                fatal("key_free: bad key type %d", k->type);
                    974:                break;
                    975:        }
                    976:
                    977:        return (pk);
1.4       markus    978: }