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

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