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

Annotation of src/usr.bin/ssh/ssh-keygen.c, Revision 1.96

1.1       deraadt     1: /*
1.13      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
                      5:  * Identity and host key generation and maintenance.
1.31      deraadt     6:  *
                      7:  * As far as I am concerned, the code I have written for this software
                      8:  * can be used freely for any purpose.  Any derived versions of this
                      9:  * software must be clearly marked as such, and if the derived work is
                     10:  * incompatible with the protocol description in the RFC file, it must be
                     11:  * called by a name other than "ssh" or "Secure Shell".
1.13      deraadt    12:  */
1.1       deraadt    13:
                     14: #include "includes.h"
1.96    ! rees       15: RCSID("$OpenBSD: ssh-keygen.c,v 1.95 2002/03/21 16:54:53 markus Exp $");
1.19      markus     16:
                     17: #include <openssl/evp.h>
                     18: #include <openssl/pem.h>
1.1       deraadt    19:
                     20: #include "xmalloc.h"
1.19      markus     21: #include "key.h"
1.53      markus     22: #include "rsa.h"
1.19      markus     23: #include "authfile.h"
                     24: #include "uuencode.h"
1.32      markus     25: #include "buffer.h"
                     26: #include "bufaux.h"
1.40      markus     27: #include "pathnames.h"
1.41      markus     28: #include "log.h"
                     29: #include "readpass.h"
1.32      markus     30:
1.75      markus     31: #ifdef SMARTCARD
                     32: #include "scard.h"
1.79      jakob      33: #endif
1.66      markus     34:
1.19      markus     35: /* Number of bits in the RSA/DSA key.  This value can be changed on the command line. */
1.1       deraadt    36: int bits = 1024;
                     37:
1.14      markus     38: /*
                     39:  * Flag indicating that we just want to change the passphrase.  This can be
                     40:  * set on the command line.
                     41:  */
1.1       deraadt    42: int change_passphrase = 0;
                     43:
1.14      markus     44: /*
                     45:  * Flag indicating that we just want to change the comment.  This can be set
                     46:  * on the command line.
                     47:  */
1.1       deraadt    48: int change_comment = 0;
                     49:
1.2       provos     50: int quiet = 0;
                     51:
1.8       markus     52: /* Flag indicating that we just want to see the key fingerprint */
                     53: int print_fingerprint = 0;
1.49      markus     54: int print_bubblebabble = 0;
1.8       markus     55:
1.10      markus     56: /* The identity file name, given on the command line or entered by the user. */
                     57: char identity_file[1024];
                     58: int have_identity = 0;
1.1       deraadt    59:
                     60: /* This is set to the passphrase if given on the command line. */
                     61: char *identity_passphrase = NULL;
                     62:
                     63: /* This is set to the new passphrase if given on the command line. */
                     64: char *identity_new_passphrase = NULL;
                     65:
                     66: /* This is set to the new comment if given on the command line. */
                     67: char *identity_comment = NULL;
                     68:
1.19      markus     69: /* Dump public key file in format used by real and the original SSH 2 */
                     70: int convert_to_ssh2 = 0;
                     71: int convert_from_ssh2 = 0;
                     72: int print_public = 0;
1.33      markus     73:
1.87      djm        74: char *key_type_name = NULL;
1.19      markus     75:
1.10      markus     76: /* argv0 */
                     77: extern char *__progname;
1.1       deraadt    78:
1.19      markus     79: char hostname[MAXHOSTNAMELEN];
                     80:
1.63      itojun     81: static void
1.10      markus     82: ask_filename(struct passwd *pw, const char *prompt)
1.1       deraadt    83: {
1.12      markus     84:        char buf[1024];
1.35      markus     85:        char *name = NULL;
                     86:
1.92      stevesk    87:        if (key_type_name == NULL)
1.40      markus     88:                name = _PATH_SSH_CLIENT_ID_RSA;
1.92      stevesk    89:        else
                     90:                switch (key_type_from_name(key_type_name)) {
                     91:                case KEY_RSA1:
                     92:                        name = _PATH_SSH_CLIENT_IDENTITY;
                     93:                        break;
                     94:                case KEY_DSA:
                     95:                        name = _PATH_SSH_CLIENT_ID_DSA;
                     96:                        break;
                     97:                case KEY_RSA:
                     98:                        name = _PATH_SSH_CLIENT_ID_RSA;
                     99:                        break;
                    100:                default:
                    101:                        fprintf(stderr, "bad key type");
                    102:                        exit(1);
                    103:                        break;
                    104:                }
                    105:
1.35      markus    106:        snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
1.37      markus    107:        fprintf(stderr, "%s (%s): ", prompt, identity_file);
                    108:        fflush(stderr);
1.12      markus    109:        if (fgets(buf, sizeof(buf), stdin) == NULL)
                    110:                exit(1);
                    111:        if (strchr(buf, '\n'))
                    112:                *strchr(buf, '\n') = 0;
                    113:        if (strcmp(buf, "") != 0)
                    114:                strlcpy(identity_file, buf, sizeof(identity_file));
                    115:        have_identity = 1;
1.7       markus    116: }
                    117:
1.63      itojun    118: static Key *
1.61      markus    119: load_identity(char *filename)
1.19      markus    120: {
1.52      markus    121:        char *pass;
                    122:        Key *prv;
                    123:
1.55      markus    124:        prv = key_load_private(filename, "", NULL);
1.52      markus    125:        if (prv == NULL) {
1.61      markus    126:                if (identity_passphrase)
                    127:                        pass = xstrdup(identity_passphrase);
                    128:                else
1.65      markus    129:                        pass = read_passphrase("Enter passphrase: ",
                    130:                            RP_ALLOW_STDIN);
1.52      markus    131:                prv = key_load_private(filename, pass, NULL);
1.19      markus    132:                memset(pass, 0, strlen(pass));
                    133:                xfree(pass);
                    134:        }
1.52      markus    135:        return prv;
1.19      markus    136: }
                    137:
1.32      markus    138: #define SSH_COM_PUBLIC_BEGIN           "---- BEGIN SSH2 PUBLIC KEY ----"
                    139: #define SSH_COM_PUBLIC_END             "---- END SSH2 PUBLIC KEY ----"
                    140: #define SSH_COM_PRIVATE_BEGIN          "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
1.42      stevesk   141: #define        SSH_COM_PRIVATE_KEY_MAGIC       0x3f6ff9eb
1.19      markus    142:
1.63      itojun    143: static void
1.19      markus    144: do_convert_to_ssh2(struct passwd *pw)
                    145: {
1.59      markus    146:        Key *k;
1.94      markus    147:        u_int len;
1.36      markus    148:        u_char *blob;
1.19      markus    149:        struct stat st;
                    150:
                    151:        if (!have_identity)
                    152:                ask_filename(pw, "Enter file in which the key is");
                    153:        if (stat(identity_file, &st) < 0) {
                    154:                perror(identity_file);
                    155:                exit(1);
                    156:        }
1.59      markus    157:        if ((k = key_load_public(identity_file, NULL)) == NULL) {
1.61      markus    158:                if ((k = load_identity(identity_file)) == NULL) {
1.59      markus    159:                        fprintf(stderr, "load failed\n");
                    160:                        exit(1);
                    161:                }
1.19      markus    162:        }
1.81      markus    163:        if (key_to_blob(k, &blob, &len) <= 0) {
                    164:                fprintf(stderr, "key_to_blob failed\n");
                    165:                exit(1);
                    166:        }
1.32      markus    167:        fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
1.19      markus    168:        fprintf(stdout,
1.32      markus    169:            "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n",
1.59      markus    170:            key_size(k), key_type(k),
1.19      markus    171:            pw->pw_name, hostname);
                    172:        dump_base64(stdout, blob, len);
1.32      markus    173:        fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
1.59      markus    174:        key_free(k);
1.21      markus    175:        xfree(blob);
1.19      markus    176:        exit(0);
                    177: }
                    178:
1.63      itojun    179: static void
1.32      markus    180: buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
                    181: {
                    182:        int bits = buffer_get_int(b);
                    183:        int bytes = (bits + 7) / 8;
1.53      markus    184:
1.32      markus    185:        if (buffer_len(b) < bytes)
1.53      markus    186:                fatal("buffer_get_bignum_bits: input buffer too small: "
                    187:                    "need %d have %d", bytes, buffer_len(b));
1.89      stevesk   188:        BN_bin2bn(buffer_ptr(b), bytes, value);
1.32      markus    189:        buffer_consume(b, bytes);
                    190: }
                    191:
1.63      itojun    192: static Key *
1.93      markus    193: do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
1.32      markus    194: {
                    195:        Buffer b;
                    196:        Key *key = NULL;
1.64      markus    197:        char *type, *cipher;
1.69      stevesk   198:        u_char *sig, data[] = "abcde12345";
1.62      markus    199:        int magic, rlen, ktype, i1, i2, i3, i4;
1.64      markus    200:        u_int slen;
1.62      markus    201:        u_long e;
1.32      markus    202:
                    203:        buffer_init(&b);
                    204:        buffer_append(&b, blob, blen);
                    205:
                    206:        magic  = buffer_get_int(&b);
                    207:        if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
                    208:                error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
                    209:                buffer_free(&b);
                    210:                return NULL;
                    211:        }
1.62      markus    212:        i1 = buffer_get_int(&b);
1.32      markus    213:        type   = buffer_get_string(&b, NULL);
                    214:        cipher = buffer_get_string(&b, NULL);
1.62      markus    215:        i2 = buffer_get_int(&b);
                    216:        i3 = buffer_get_int(&b);
                    217:        i4 = buffer_get_int(&b);
                    218:        debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
1.32      markus    219:        if (strcmp(cipher, "none") != 0) {
                    220:                error("unsupported cipher %s", cipher);
                    221:                xfree(cipher);
                    222:                buffer_free(&b);
1.53      markus    223:                xfree(type);
1.32      markus    224:                return NULL;
                    225:        }
                    226:        xfree(cipher);
                    227:
1.53      markus    228:        if (strstr(type, "dsa")) {
                    229:                ktype = KEY_DSA;
                    230:        } else if (strstr(type, "rsa")) {
                    231:                ktype = KEY_RSA;
                    232:        } else {
                    233:                xfree(type);
1.32      markus    234:                return NULL;
                    235:        }
1.53      markus    236:        key = key_new_private(ktype);
                    237:        xfree(type);
                    238:
                    239:        switch (key->type) {
                    240:        case KEY_DSA:
                    241:                buffer_get_bignum_bits(&b, key->dsa->p);
                    242:                buffer_get_bignum_bits(&b, key->dsa->g);
                    243:                buffer_get_bignum_bits(&b, key->dsa->q);
                    244:                buffer_get_bignum_bits(&b, key->dsa->pub_key);
                    245:                buffer_get_bignum_bits(&b, key->dsa->priv_key);
                    246:                break;
                    247:        case KEY_RSA:
1.62      markus    248:                e  = buffer_get_char(&b);
                    249:                debug("e %lx", e);
                    250:                if (e < 30) {
                    251:                        e <<= 8;
                    252:                        e += buffer_get_char(&b);
                    253:                        debug("e %lx", e);
                    254:                        e <<= 8;
                    255:                        e += buffer_get_char(&b);
                    256:                        debug("e %lx", e);
                    257:                }
                    258:                if (!BN_set_word(key->rsa->e, e)) {
1.53      markus    259:                        buffer_free(&b);
                    260:                        key_free(key);
                    261:                        return NULL;
                    262:                }
                    263:                buffer_get_bignum_bits(&b, key->rsa->d);
                    264:                buffer_get_bignum_bits(&b, key->rsa->n);
                    265:                buffer_get_bignum_bits(&b, key->rsa->iqmp);
                    266:                buffer_get_bignum_bits(&b, key->rsa->q);
                    267:                buffer_get_bignum_bits(&b, key->rsa->p);
1.68      markus    268:                rsa_generate_additional_parameters(key->rsa);
1.53      markus    269:                break;
                    270:        }
1.32      markus    271:        rlen = buffer_len(&b);
1.85      deraadt   272:        if (rlen != 0)
1.53      markus    273:                error("do_convert_private_ssh2_from_blob: "
                    274:                    "remaining bytes in key blob %d", rlen);
1.32      markus    275:        buffer_free(&b);
1.64      markus    276:
                    277:        /* try the key */
                    278:        key_sign(key, &sig, &slen, data, sizeof(data));
                    279:        key_verify(key, sig, slen, data, sizeof(data));
                    280:        xfree(sig);
1.32      markus    281:        return key;
                    282: }
                    283:
1.63      itojun    284: static void
1.19      markus    285: do_convert_from_ssh2(struct passwd *pw)
                    286: {
                    287:        Key *k;
                    288:        int blen;
                    289:        char line[1024], *p;
1.80      stevesk   290:        u_char blob[8096];
1.19      markus    291:        char encoded[8096];
                    292:        struct stat st;
1.32      markus    293:        int escaped = 0, private = 0, ok;
1.19      markus    294:        FILE *fp;
                    295:
                    296:        if (!have_identity)
                    297:                ask_filename(pw, "Enter file in which the key is");
                    298:        if (stat(identity_file, &st) < 0) {
                    299:                perror(identity_file);
                    300:                exit(1);
                    301:        }
                    302:        fp = fopen(identity_file, "r");
                    303:        if (fp == NULL) {
                    304:                perror(identity_file);
                    305:                exit(1);
                    306:        }
                    307:        encoded[0] = '\0';
                    308:        while (fgets(line, sizeof(line), fp)) {
1.25      markus    309:                if (!(p = strchr(line, '\n'))) {
                    310:                        fprintf(stderr, "input line too long.\n");
                    311:                        exit(1);
                    312:                }
                    313:                if (p > line && p[-1] == '\\')
                    314:                        escaped++;
1.19      markus    315:                if (strncmp(line, "----", 4) == 0 ||
                    316:                    strstr(line, ": ") != NULL) {
1.32      markus    317:                        if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
                    318:                                private = 1;
1.64      markus    319:                        if (strstr(line, " END ") != NULL) {
                    320:                                break;
                    321:                        }
1.60      markus    322:                        /* fprintf(stderr, "ignore: %s", line); */
1.19      markus    323:                        continue;
                    324:                }
1.25      markus    325:                if (escaped) {
                    326:                        escaped--;
1.60      markus    327:                        /* fprintf(stderr, "escaped: %s", line); */
1.25      markus    328:                        continue;
1.19      markus    329:                }
                    330:                *p = '\0';
                    331:                strlcat(encoded, line, sizeof(encoded));
                    332:        }
1.91      stevesk   333:        blen = uudecode(encoded, blob, sizeof(blob));
1.19      markus    334:        if (blen < 0) {
                    335:                fprintf(stderr, "uudecode failed.\n");
                    336:                exit(1);
                    337:        }
1.32      markus    338:        k = private ?
                    339:            do_convert_private_ssh2_from_blob(blob, blen) :
1.33      markus    340:            key_from_blob(blob, blen);
1.32      markus    341:        if (k == NULL) {
                    342:                fprintf(stderr, "decode blob failed.\n");
                    343:                exit(1);
                    344:        }
                    345:        ok = private ?
1.53      markus    346:            (k->type == KEY_DSA ?
                    347:                 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
                    348:                 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
1.32      markus    349:            key_write(k, stdout);
                    350:        if (!ok) {
                    351:                fprintf(stderr, "key write failed");
                    352:                exit(1);
                    353:        }
1.19      markus    354:        key_free(k);
1.90      markus    355:        if (!private)
                    356:                fprintf(stdout, "\n");
1.19      markus    357:        fclose(fp);
                    358:        exit(0);
                    359: }
                    360:
1.63      itojun    361: static void
1.19      markus    362: do_print_public(struct passwd *pw)
                    363: {
1.52      markus    364:        Key *prv;
1.19      markus    365:        struct stat st;
                    366:
                    367:        if (!have_identity)
                    368:                ask_filename(pw, "Enter file in which the key is");
                    369:        if (stat(identity_file, &st) < 0) {
                    370:                perror(identity_file);
                    371:                exit(1);
                    372:        }
1.61      markus    373:        prv = load_identity(identity_file);
1.52      markus    374:        if (prv == NULL) {
1.19      markus    375:                fprintf(stderr, "load failed\n");
                    376:                exit(1);
                    377:        }
1.52      markus    378:        if (!key_write(prv, stdout))
1.19      markus    379:                fprintf(stderr, "key_write failed");
1.52      markus    380:        key_free(prv);
1.19      markus    381:        fprintf(stdout, "\n");
                    382:        exit(0);
                    383: }
                    384:
1.74      markus    385: #ifdef SMARTCARD
1.66      markus    386: static void
1.75      markus    387: do_upload(struct passwd *pw, const char *sc_reader_id)
1.66      markus    388: {
                    389:        Key *prv = NULL;
                    390:        struct stat st;
1.95      markus    391:        int ret;
1.66      markus    392:
                    393:        if (!have_identity)
                    394:                ask_filename(pw, "Enter file in which the key is");
                    395:        if (stat(identity_file, &st) < 0) {
                    396:                perror(identity_file);
1.95      markus    397:                exit(1);
1.66      markus    398:        }
                    399:        prv = load_identity(identity_file);
                    400:        if (prv == NULL) {
                    401:                error("load failed");
1.95      markus    402:                exit(1);
1.66      markus    403:        }
1.95      markus    404:        ret = sc_put_key(prv, sc_reader_id);
                    405:        key_free(prv);
                    406:        if (ret < 0)
                    407:                exit(1);
                    408:         log("loading key done");
                    409:        exit(0);
1.74      markus    410: }
1.75      markus    411:
                    412: static void
                    413: do_download(struct passwd *pw, const char *sc_reader_id)
                    414: {
                    415:        Key *pub = NULL;
                    416:
1.96    ! rees      417:        pub = sc_get_key(sc_reader_id, NULL);
1.75      markus    418:        if (pub == NULL)
                    419:                fatal("cannot read public key from smartcard");
                    420:        key_write(pub, stdout);
                    421:        key_free(pub);
                    422:        fprintf(stdout, "\n");
                    423:        exit(0);
                    424: }
1.78      jakob     425: #endif /* SMARTCARD */
1.66      markus    426:
1.63      itojun    427: static void
1.8       markus    428: do_fingerprint(struct passwd *pw)
                    429: {
1.15      markus    430:        FILE *f;
1.19      markus    431:        Key *public;
1.49      markus    432:        char *comment = NULL, *cp, *ep, line[16*1024], *fp;
1.84      stevesk   433:        int i, skip = 0, num = 1, invalid = 1;
                    434:        enum fp_rep rep;
                    435:        enum fp_type fptype;
1.12      markus    436:        struct stat st;
                    437:
1.52      markus    438:        fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
                    439:        rep =    print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
1.49      markus    440:
1.12      markus    441:        if (!have_identity)
                    442:                ask_filename(pw, "Enter file in which the key is");
                    443:        if (stat(identity_file, &st) < 0) {
                    444:                perror(identity_file);
                    445:                exit(1);
                    446:        }
1.52      markus    447:        public = key_load_public(identity_file, &comment);
                    448:        if (public != NULL) {
                    449:                fp = key_fingerprint(public, fptype, rep);
                    450:                printf("%d %s %s\n", key_size(public), fp, comment);
1.33      markus    451:                key_free(public);
                    452:                xfree(comment);
1.49      markus    453:                xfree(fp);
1.15      markus    454:                exit(0);
                    455:        }
1.52      markus    456:        if (comment)
                    457:                xfree(comment);
1.15      markus    458:
                    459:        f = fopen(identity_file, "r");
                    460:        if (f != NULL) {
                    461:                while (fgets(line, sizeof(line), f)) {
                    462:                        i = strlen(line) - 1;
                    463:                        if (line[i] != '\n') {
                    464:                                error("line %d too long: %.40s...", num, line);
                    465:                                skip = 1;
                    466:                                continue;
                    467:                        }
                    468:                        num++;
                    469:                        if (skip) {
                    470:                                skip = 0;
                    471:                                continue;
                    472:                        }
                    473:                        line[i] = '\0';
                    474:
                    475:                        /* Skip leading whitespace, empty and comment lines. */
                    476:                        for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    477:                                ;
                    478:                        if (!*cp || *cp == '\n' || *cp == '#')
                    479:                                continue ;
                    480:                        i = strtol(cp, &ep, 10);
                    481:                        if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
                    482:                                int quoted = 0;
                    483:                                comment = cp;
                    484:                                for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    485:                                        if (*cp == '\\' && cp[1] == '"')
                    486:                                                cp++;   /* Skip both */
                    487:                                        else if (*cp == '"')
                    488:                                                quoted = !quoted;
                    489:                                }
                    490:                                if (!*cp)
                    491:                                        continue;
                    492:                                *cp++ = '\0';
                    493:                        }
                    494:                        ep = cp;
1.38      markus    495:                        public = key_new(KEY_RSA1);
                    496:                        if (key_read(public, &cp) != 1) {
                    497:                                cp = ep;
                    498:                                key_free(public);
                    499:                                public = key_new(KEY_UNSPEC);
                    500:                                if (key_read(public, &cp) != 1) {
                    501:                                        key_free(public);
                    502:                                        continue;
                    503:                                }
1.12      markus    504:                        }
1.38      markus    505:                        comment = *cp ? cp : comment;
1.52      markus    506:                        fp = key_fingerprint(public, fptype, rep);
1.49      markus    507:                        printf("%d %s %s\n", key_size(public), fp,
1.38      markus    508:                            comment ? comment : "no comment");
1.49      markus    509:                        xfree(fp);
1.52      markus    510:                        key_free(public);
1.38      markus    511:                        invalid = 0;
1.12      markus    512:                }
1.15      markus    513:                fclose(f);
                    514:        }
                    515:        if (invalid) {
1.83      markus    516:                printf("%s is not a public key file.\n", identity_file);
1.15      markus    517:                exit(1);
1.12      markus    518:        }
                    519:        exit(0);
1.8       markus    520: }
                    521:
1.13      deraadt   522: /*
                    523:  * Perform changing a passphrase.  The argument is the passwd structure
                    524:  * for the current user.
                    525:  */
1.63      itojun    526: static void
1.7       markus    527: do_change_passphrase(struct passwd *pw)
                    528: {
1.12      markus    529:        char *comment;
                    530:        char *old_passphrase, *passphrase1, *passphrase2;
                    531:        struct stat st;
1.19      markus    532:        Key *private;
1.12      markus    533:
                    534:        if (!have_identity)
                    535:                ask_filename(pw, "Enter file in which the key is");
                    536:        if (stat(identity_file, &st) < 0) {
                    537:                perror(identity_file);
                    538:                exit(1);
                    539:        }
                    540:        /* Try to load the file with empty passphrase. */
1.52      markus    541:        private = key_load_private(identity_file, "", &comment);
                    542:        if (private == NULL) {
1.12      markus    543:                if (identity_passphrase)
                    544:                        old_passphrase = xstrdup(identity_passphrase);
                    545:                else
1.65      markus    546:                        old_passphrase =
                    547:                            read_passphrase("Enter old passphrase: ",
                    548:                            RP_ALLOW_STDIN);
                    549:                private = key_load_private(identity_file, old_passphrase,
                    550:                    &comment);
1.52      markus    551:                memset(old_passphrase, 0, strlen(old_passphrase));
                    552:                xfree(old_passphrase);
                    553:                if (private == NULL) {
1.12      markus    554:                        printf("Bad passphrase.\n");
                    555:                        exit(1);
                    556:                }
                    557:        }
                    558:        printf("Key has comment '%s'\n", comment);
                    559:
                    560:        /* Ask the new passphrase (twice). */
                    561:        if (identity_new_passphrase) {
                    562:                passphrase1 = xstrdup(identity_new_passphrase);
                    563:                passphrase2 = NULL;
                    564:        } else {
                    565:                passphrase1 =
1.65      markus    566:                        read_passphrase("Enter new passphrase (empty for no "
                    567:                            "passphrase): ", RP_ALLOW_STDIN);
                    568:                passphrase2 = read_passphrase("Enter same passphrase again: ",
1.86      deraadt   569:                    RP_ALLOW_STDIN);
1.12      markus    570:
                    571:                /* Verify that they are the same. */
                    572:                if (strcmp(passphrase1, passphrase2) != 0) {
                    573:                        memset(passphrase1, 0, strlen(passphrase1));
                    574:                        memset(passphrase2, 0, strlen(passphrase2));
                    575:                        xfree(passphrase1);
                    576:                        xfree(passphrase2);
                    577:                        printf("Pass phrases do not match.  Try again.\n");
                    578:                        exit(1);
                    579:                }
                    580:                /* Destroy the other copy. */
                    581:                memset(passphrase2, 0, strlen(passphrase2));
                    582:                xfree(passphrase2);
                    583:        }
                    584:
                    585:        /* Save the file using the new passphrase. */
1.52      markus    586:        if (!key_save_private(private, identity_file, passphrase1, comment)) {
1.56      markus    587:                printf("Saving the key failed: %s.\n", identity_file);
1.12      markus    588:                memset(passphrase1, 0, strlen(passphrase1));
                    589:                xfree(passphrase1);
1.19      markus    590:                key_free(private);
1.12      markus    591:                xfree(comment);
                    592:                exit(1);
                    593:        }
                    594:        /* Destroy the passphrase and the copy of the key in memory. */
                    595:        memset(passphrase1, 0, strlen(passphrase1));
                    596:        xfree(passphrase1);
1.19      markus    597:        key_free(private);               /* Destroys contents */
1.12      markus    598:        xfree(comment);
1.1       deraadt   599:
1.12      markus    600:        printf("Your identification has been saved with the new passphrase.\n");
                    601:        exit(0);
1.1       deraadt   602: }
                    603:
1.13      deraadt   604: /*
                    605:  * Change the comment of a private key file.
                    606:  */
1.63      itojun    607: static void
1.2       provos    608: do_change_comment(struct passwd *pw)
1.1       deraadt   609: {
1.46      deraadt   610:        char new_comment[1024], *comment, *passphrase;
1.52      markus    611:        Key *private;
                    612:        Key *public;
1.12      markus    613:        struct stat st;
                    614:        FILE *f;
1.46      deraadt   615:        int fd;
1.12      markus    616:
                    617:        if (!have_identity)
                    618:                ask_filename(pw, "Enter file in which the key is");
                    619:        if (stat(identity_file, &st) < 0) {
                    620:                perror(identity_file);
                    621:                exit(1);
                    622:        }
1.52      markus    623:        private = key_load_private(identity_file, "", &comment);
                    624:        if (private == NULL) {
1.12      markus    625:                if (identity_passphrase)
                    626:                        passphrase = xstrdup(identity_passphrase);
                    627:                else if (identity_new_passphrase)
                    628:                        passphrase = xstrdup(identity_new_passphrase);
                    629:                else
1.65      markus    630:                        passphrase = read_passphrase("Enter passphrase: ",
                    631:                            RP_ALLOW_STDIN);
1.12      markus    632:                /* Try to load using the passphrase. */
1.52      markus    633:                private = key_load_private(identity_file, passphrase, &comment);
                    634:                if (private == NULL) {
1.12      markus    635:                        memset(passphrase, 0, strlen(passphrase));
                    636:                        xfree(passphrase);
                    637:                        printf("Bad passphrase.\n");
                    638:                        exit(1);
                    639:                }
1.52      markus    640:        } else {
                    641:                passphrase = xstrdup("");
1.12      markus    642:        }
1.52      markus    643:        if (private->type != KEY_RSA1) {
                    644:                fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
                    645:                key_free(private);
                    646:                exit(1);
1.86      deraadt   647:        }
1.12      markus    648:        printf("Key now has comment '%s'\n", comment);
                    649:
                    650:        if (identity_comment) {
                    651:                strlcpy(new_comment, identity_comment, sizeof(new_comment));
                    652:        } else {
                    653:                printf("Enter new comment: ");
                    654:                fflush(stdout);
                    655:                if (!fgets(new_comment, sizeof(new_comment), stdin)) {
                    656:                        memset(passphrase, 0, strlen(passphrase));
1.19      markus    657:                        key_free(private);
1.12      markus    658:                        exit(1);
                    659:                }
                    660:                if (strchr(new_comment, '\n'))
                    661:                        *strchr(new_comment, '\n') = 0;
                    662:        }
                    663:
                    664:        /* Save the file using the new passphrase. */
1.52      markus    665:        if (!key_save_private(private, identity_file, passphrase, new_comment)) {
1.56      markus    666:                printf("Saving the key failed: %s.\n", identity_file);
1.12      markus    667:                memset(passphrase, 0, strlen(passphrase));
                    668:                xfree(passphrase);
1.19      markus    669:                key_free(private);
1.12      markus    670:                xfree(comment);
                    671:                exit(1);
                    672:        }
                    673:        memset(passphrase, 0, strlen(passphrase));
                    674:        xfree(passphrase);
1.52      markus    675:        public = key_from_private(private);
1.19      markus    676:        key_free(private);
1.12      markus    677:
                    678:        strlcat(identity_file, ".pub", sizeof(identity_file));
1.46      deraadt   679:        fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
                    680:        if (fd == -1) {
1.12      markus    681:                printf("Could not save your public key in %s\n", identity_file);
                    682:                exit(1);
                    683:        }
1.46      deraadt   684:        f = fdopen(fd, "w");
                    685:        if (f == NULL) {
                    686:                printf("fdopen %s failed", identity_file);
                    687:                exit(1);
                    688:        }
1.19      markus    689:        if (!key_write(public, f))
                    690:                fprintf(stderr, "write key failed");
                    691:        key_free(public);
                    692:        fprintf(f, " %s\n", new_comment);
1.12      markus    693:        fclose(f);
1.1       deraadt   694:
1.12      markus    695:        xfree(comment);
1.1       deraadt   696:
1.12      markus    697:        printf("The comment in your key file has been changed.\n");
                    698:        exit(0);
1.1       deraadt   699: }
                    700:
1.63      itojun    701: static void
1.10      markus    702: usage(void)
                    703: {
1.77      jakob     704:        fprintf(stderr, "Usage: %s [options]\n", __progname);
                    705:        fprintf(stderr, "Options:\n");
                    706:        fprintf(stderr, "  -b bits     Number of bits in the key to create.\n");
                    707:        fprintf(stderr, "  -c          Change comment in private and public key files.\n");
                    708:        fprintf(stderr, "  -e          Convert OpenSSH to IETF SECSH key file.\n");
                    709:        fprintf(stderr, "  -f filename Filename of the key file.\n");
                    710:        fprintf(stderr, "  -i          Convert IETF SECSH to OpenSSH key file.\n");
                    711:        fprintf(stderr, "  -l          Show fingerprint of key file.\n");
                    712:        fprintf(stderr, "  -p          Change passphrase of private key file.\n");
                    713:        fprintf(stderr, "  -q          Quiet.\n");
                    714:        fprintf(stderr, "  -y          Read private key file and print public key.\n");
                    715:        fprintf(stderr, "  -t type     Specify type of key to create.\n");
                    716:        fprintf(stderr, "  -B          Show bubblebabble digest of key file.\n");
                    717:        fprintf(stderr, "  -C comment  Provide new comment.\n");
                    718:        fprintf(stderr, "  -N phrase   Provide new passphrase.\n");
                    719:        fprintf(stderr, "  -P phrase   Provide old passphrase.\n");
                    720: #ifdef SMARTCARD
                    721:        fprintf(stderr, "  -D reader   Download public key from smartcard.\n");
                    722:        fprintf(stderr, "  -U reader   Upload private key to smartcard.\n");
                    723: #endif /* SMARTCARD */
                    724:
1.12      markus    725:        exit(1);
1.10      markus    726: }
                    727:
1.13      deraadt   728: /*
                    729:  * Main program for key management.
                    730:  */
1.2       provos    731: int
                    732: main(int ac, char **av)
1.1       deraadt   733: {
1.87      djm       734:        char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
1.75      markus    735:        char *reader_id = NULL;
1.46      deraadt   736:        Key *private, *public;
1.12      markus    737:        struct passwd *pw;
                    738:        struct stat st;
1.75      markus    739:        int opt, type, fd, download = 0;
1.12      markus    740:        FILE *f;
1.33      markus    741:
1.12      markus    742:        extern int optind;
                    743:        extern char *optarg;
                    744:
1.26      markus    745:        SSLeay_add_all_algorithms();
1.19      markus    746:
1.14      markus    747:        /* we need this for the home * directory.  */
1.12      markus    748:        pw = getpwuid(getuid());
                    749:        if (!pw) {
                    750:                printf("You don't exist, go away!\n");
                    751:                exit(1);
                    752:        }
1.19      markus    753:        if (gethostname(hostname, sizeof(hostname)) < 0) {
                    754:                perror("gethostname");
                    755:                exit(1);
                    756:        }
1.14      markus    757:
1.76      jakob     758:        while ((opt = getopt(ac, av, "deiqpclBRxXyb:f:t:U:D:P:N:C:")) != -1) {
1.12      markus    759:                switch (opt) {
                    760:                case 'b':
                    761:                        bits = atoi(optarg);
                    762:                        if (bits < 512 || bits > 32768) {
                    763:                                printf("Bits has bad value.\n");
                    764:                                exit(1);
                    765:                        }
                    766:                        break;
                    767:                case 'l':
                    768:                        print_fingerprint = 1;
                    769:                        break;
1.49      markus    770:                case 'B':
                    771:                        print_bubblebabble = 1;
                    772:                        break;
1.12      markus    773:                case 'p':
                    774:                        change_passphrase = 1;
                    775:                        break;
                    776:                case 'c':
                    777:                        change_comment = 1;
                    778:                        break;
                    779:                case 'f':
                    780:                        strlcpy(identity_file, optarg, sizeof(identity_file));
                    781:                        have_identity = 1;
                    782:                        break;
                    783:                case 'P':
                    784:                        identity_passphrase = optarg;
                    785:                        break;
                    786:                case 'N':
                    787:                        identity_new_passphrase = optarg;
                    788:                        break;
                    789:                case 'C':
                    790:                        identity_comment = optarg;
                    791:                        break;
                    792:                case 'q':
                    793:                        quiet = 1;
1.20      deraadt   794:                        break;
                    795:                case 'R':
1.33      markus    796:                        /* unused */
                    797:                        exit(0);
1.12      markus    798:                        break;
1.57      markus    799:                case 'e':
1.19      markus    800:                case 'x':
1.57      markus    801:                        /* export key */
1.19      markus    802:                        convert_to_ssh2 = 1;
                    803:                        break;
1.57      markus    804:                case 'i':
1.19      markus    805:                case 'X':
1.57      markus    806:                        /* import key */
1.19      markus    807:                        convert_from_ssh2 = 1;
                    808:                        break;
                    809:                case 'y':
                    810:                        print_public = 1;
1.47      jakob     811:                        break;
1.19      markus    812:                case 'd':
1.33      markus    813:                        key_type_name = "dsa";
1.19      markus    814:                        break;
1.33      markus    815:                case 't':
                    816:                        key_type_name = optarg;
                    817:                        break;
1.75      markus    818:                case 'D':
                    819:                        download = 1;
1.76      jakob     820:                case 'U':
1.75      markus    821:                        reader_id = optarg;
1.66      markus    822:                        break;
1.12      markus    823:                case '?':
                    824:                default:
                    825:                        usage();
                    826:                }
                    827:        }
                    828:        if (optind < ac) {
                    829:                printf("Too many arguments.\n");
1.87      djm       830:                usage();
                    831:        }
1.12      markus    832:        if (change_passphrase && change_comment) {
                    833:                printf("Can only have one of -p and -c.\n");
                    834:                usage();
                    835:        }
1.49      markus    836:        if (print_fingerprint || print_bubblebabble)
1.12      markus    837:                do_fingerprint(pw);
                    838:        if (change_passphrase)
                    839:                do_change_passphrase(pw);
                    840:        if (change_comment)
                    841:                do_change_comment(pw);
1.19      markus    842:        if (convert_to_ssh2)
                    843:                do_convert_to_ssh2(pw);
                    844:        if (convert_from_ssh2)
                    845:                do_convert_from_ssh2(pw);
                    846:        if (print_public)
                    847:                do_print_public(pw);
1.75      markus    848:        if (reader_id != NULL) {
1.74      markus    849: #ifdef SMARTCARD
1.75      markus    850:                if (download)
                    851:                        do_download(pw, reader_id);
                    852:                else
                    853:                        do_upload(pw, reader_id);
1.78      jakob     854: #else /* SMARTCARD */
1.74      markus    855:                fatal("no support for smartcards.");
1.78      jakob     856: #endif /* SMARTCARD */
1.75      markus    857:        }
1.12      markus    858:
                    859:        arc4random_stir();
                    860:
1.88      markus    861:        if (key_type_name == NULL) {
                    862:                printf("You must specify a key type (-t).\n");
                    863:                usage();
                    864:        }
1.35      markus    865:        type = key_type_from_name(key_type_name);
                    866:        if (type == KEY_UNSPEC) {
                    867:                fprintf(stderr, "unknown key type %s\n", key_type_name);
                    868:                exit(1);
1.19      markus    869:        }
1.33      markus    870:        if (!quiet)
1.35      markus    871:                printf("Generating public/private %s key pair.\n", key_type_name);
1.33      markus    872:        private = key_generate(type, bits);
                    873:        if (private == NULL) {
                    874:                fprintf(stderr, "key_generate failed");
                    875:                exit(1);
                    876:        }
                    877:        public  = key_from_private(private);
1.12      markus    878:
                    879:        if (!have_identity)
                    880:                ask_filename(pw, "Enter file in which to save the key");
                    881:
                    882:        /* Create ~/.ssh directory if it doesn\'t already exist. */
1.40      markus    883:        snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
1.12      markus    884:        if (strstr(identity_file, dotsshdir) != NULL &&
                    885:            stat(dotsshdir, &st) < 0) {
1.29      djm       886:                if (mkdir(dotsshdir, 0700) < 0)
1.12      markus    887:                        error("Could not create directory '%s'.", dotsshdir);
                    888:                else if (!quiet)
                    889:                        printf("Created directory '%s'.\n", dotsshdir);
                    890:        }
                    891:        /* If the file already exists, ask the user to confirm. */
                    892:        if (stat(identity_file, &st) >= 0) {
                    893:                char yesno[3];
                    894:                printf("%s already exists.\n", identity_file);
                    895:                printf("Overwrite (y/n)? ");
                    896:                fflush(stdout);
                    897:                if (fgets(yesno, sizeof(yesno), stdin) == NULL)
                    898:                        exit(1);
                    899:                if (yesno[0] != 'y' && yesno[0] != 'Y')
                    900:                        exit(1);
                    901:        }
                    902:        /* Ask for a passphrase (twice). */
                    903:        if (identity_passphrase)
                    904:                passphrase1 = xstrdup(identity_passphrase);
                    905:        else if (identity_new_passphrase)
                    906:                passphrase1 = xstrdup(identity_new_passphrase);
                    907:        else {
                    908: passphrase_again:
                    909:                passphrase1 =
1.65      markus    910:                        read_passphrase("Enter passphrase (empty for no "
                    911:                            "passphrase): ", RP_ALLOW_STDIN);
                    912:                passphrase2 = read_passphrase("Enter same passphrase again: ",
                    913:                    RP_ALLOW_STDIN);
1.12      markus    914:                if (strcmp(passphrase1, passphrase2) != 0) {
1.65      markus    915:                        /*
                    916:                         * The passphrases do not match.  Clear them and
                    917:                         * retry.
                    918:                         */
1.12      markus    919:                        memset(passphrase1, 0, strlen(passphrase1));
                    920:                        memset(passphrase2, 0, strlen(passphrase2));
                    921:                        xfree(passphrase1);
                    922:                        xfree(passphrase2);
                    923:                        printf("Passphrases do not match.  Try again.\n");
                    924:                        goto passphrase_again;
                    925:                }
                    926:                /* Clear the other copy of the passphrase. */
                    927:                memset(passphrase2, 0, strlen(passphrase2));
                    928:                xfree(passphrase2);
                    929:        }
                    930:
                    931:        if (identity_comment) {
                    932:                strlcpy(comment, identity_comment, sizeof(comment));
                    933:        } else {
1.18      markus    934:                /* Create default commend field for the passphrase. */
1.12      markus    935:                snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
                    936:        }
                    937:
                    938:        /* Save the key with the given passphrase and comment. */
1.52      markus    939:        if (!key_save_private(private, identity_file, passphrase1, comment)) {
1.56      markus    940:                printf("Saving the key failed: %s.\n", identity_file);
1.12      markus    941:                memset(passphrase1, 0, strlen(passphrase1));
                    942:                xfree(passphrase1);
                    943:                exit(1);
                    944:        }
                    945:        /* Clear the passphrase. */
                    946:        memset(passphrase1, 0, strlen(passphrase1));
                    947:        xfree(passphrase1);
                    948:
                    949:        /* Clear the private key and the random number generator. */
1.33      markus    950:        key_free(private);
1.12      markus    951:        arc4random_stir();
                    952:
                    953:        if (!quiet)
                    954:                printf("Your identification has been saved in %s.\n", identity_file);
                    955:
                    956:        strlcat(identity_file, ".pub", sizeof(identity_file));
1.46      deraadt   957:        fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
                    958:        if (fd == -1) {
1.12      markus    959:                printf("Could not save your public key in %s\n", identity_file);
1.46      deraadt   960:                exit(1);
                    961:        }
                    962:        f = fdopen(fd, "w");
                    963:        if (f == NULL) {
                    964:                printf("fdopen %s failed", identity_file);
1.12      markus    965:                exit(1);
                    966:        }
1.19      markus    967:        if (!key_write(public, f))
                    968:                fprintf(stderr, "write key failed");
                    969:        fprintf(f, " %s\n", comment);
1.12      markus    970:        fclose(f);
                    971:
                    972:        if (!quiet) {
1.50      markus    973:                char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1.19      markus    974:                printf("Your public key has been saved in %s.\n",
                    975:                    identity_file);
1.12      markus    976:                printf("The key fingerprint is:\n");
1.50      markus    977:                printf("%s %s\n", fp, comment);
                    978:                xfree(fp);
1.12      markus    979:        }
1.19      markus    980:
                    981:        key_free(public);
1.12      markus    982:        exit(0);
1.1       deraadt   983: }