]> andersk Git - openssh.git/blobdiff - ssh-keygen.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / ssh-keygen.c
index e06ae1a5249ef830954603e1784a8afcc557faa8..7f5185f8e4b70a04bf37f2168ea0ec630f832765 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: ssh-keygen.c,v 1.144 2006/05/17 12:43:34 markus Exp $ */
+/* $OpenBSD: ssh-keygen.c,v 1.176 2010/01/11 10:51:07 djm Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
 #include "includes.h"
 
 #include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/stat.h>
+#include <sys/param.h>
 
 #include <openssl/evp.h>
 #include <openssl/pem.h>
+#include "openbsd-compat/openssl-compat.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <netdb.h>
+#ifdef HAVE_PATHS_H
+# include <paths.h>
+#endif
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
 
 #include "xmalloc.h"
 #include "key.h"
 #include "authfile.h"
 #include "uuencode.h"
 #include "buffer.h"
-#include "bufaux.h"
 #include "pathnames.h"
 #include "log.h"
 #include "misc.h"
 #include "match.h"
 #include "hostfile.h"
+#include "dns.h"
 
 #ifdef SMARTCARD
 #include "scard.h"
 #endif
-#include "dns.h"
 
 /* Number of bits in the RSA/DSA key.  This value can be set on the command line. */
 #define DEFAULT_BITS           2048
@@ -57,6 +72,8 @@ int change_comment = 0;
 
 int quiet = 0;
 
+int log_level = SYSLOG_LEVEL_INFO;
+
 /* Flag indicating that we want to hash a known_hosts file */
 int hash_hosts = 0;
 /* Flag indicating that we want lookup a host in known_hosts file */
@@ -118,7 +135,7 @@ ask_filename(struct passwd *pw, const char *prompt)
                        name = _PATH_SSH_CLIENT_ID_RSA;
                        break;
                default:
-                       fprintf(stderr, "bad key type");
+                       fprintf(stderr, "bad key type\n");
                        exit(1);
                        break;
                }
@@ -127,8 +144,7 @@ ask_filename(struct passwd *pw, const char *prompt)
        fprintf(stderr, "%s (%s): ", prompt, identity_file);
        if (fgets(buf, sizeof(buf), stdin) == NULL)
                exit(1);
-       if (strchr(buf, '\n'))
-               *strchr(buf, '\n') = 0;
+       buf[strcspn(buf, "\n")] = '\0';
        if (strcmp(buf, "") != 0)
                strlcpy(identity_file, buf, sizeof(identity_file));
        have_identity = 1;
@@ -165,6 +181,7 @@ do_convert_to_ssh2(struct passwd *pw)
        Key *k;
        u_int len;
        u_char *blob;
+       char comment[61];
        struct stat st;
 
        if (!have_identity)
@@ -187,11 +204,14 @@ do_convert_to_ssh2(struct passwd *pw)
                fprintf(stderr, "key_to_blob failed\n");
                exit(1);
        }
-       fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
-       fprintf(stdout,
-           "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n",
+       /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
+       snprintf(comment, sizeof(comment),
+           "%u-bit %s, converted by %s@%s from OpenSSH",
            key_size(k), key_type(k),
            pw->pw_name, hostname);
+
+       fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
+       fprintf(stdout, "Comment: \"%s\"\n", comment);
        dump_base64(stdout, blob, len);
        fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
        key_free(k);
@@ -208,7 +228,8 @@ buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
        if (buffer_len(b) < bytes)
                fatal("buffer_get_bignum_bits: input buffer too small: "
                    "need %d have %d", bytes, buffer_len(b));
-       BN_bin2bn(buffer_ptr(b), bytes, value);
+       if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
+               fatal("buffer_get_bignum_bits: BN_bin2bn failed");
        buffer_consume(b, bytes);
 }
 
@@ -226,7 +247,7 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
        buffer_init(&b);
        buffer_append(&b, blob, blen);
 
-       magic  = buffer_get_int(&b);
+       magic = buffer_get_int(&b);
        if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
                error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
                buffer_free(&b);
@@ -238,7 +259,7 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
        i2 = buffer_get_int(&b);
        i3 = buffer_get_int(&b);
        i4 = buffer_get_int(&b);
-       debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
+       debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
        if (strcmp(cipher, "none") != 0) {
                error("unsupported cipher %s", cipher);
                xfree(cipher);
@@ -269,7 +290,7 @@ do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
                buffer_get_bignum_bits(&b, key->dsa->priv_key);
                break;
        case KEY_RSA:
-               e  = buffer_get_char(&b);
+               e = buffer_get_char(&b);
                debug("e %lx", e);
                if (e < 30) {
                        e <<= 8;
@@ -331,9 +352,8 @@ get_line(FILE *fp, char *line, size_t len)
                line[pos++] = c;
                line[pos] = '\0';
        }
-       if (c == EOF)
-               return -1;
-       return pos;
+       /* We reached EOF */
+       return -1;
 }
 
 static void
@@ -405,7 +425,7 @@ do_convert_from_ssh2(struct passwd *pw)
                 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
            key_write(k, stdout);
        if (!ok) {
-               fprintf(stderr, "key write failed");
+               fprintf(stderr, "key write failed\n");
                exit(1);
        }
        key_free(k);
@@ -490,8 +510,8 @@ do_fingerprint(struct passwd *pw)
 {
        FILE *f;
        Key *public;
-       char *comment = NULL, *cp, *ep, line[16*1024], *fp;
-       int i, skip = 0, num = 1, invalid = 1;
+       char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
+       int i, skip = 0, num = 0, invalid = 1;
        enum fp_rep rep;
        enum fp_type fptype;
        struct stat st;
@@ -508,9 +528,14 @@ do_fingerprint(struct passwd *pw)
        public = key_load_public(identity_file, &comment);
        if (public != NULL) {
                fp = key_fingerprint(public, fptype, rep);
-               printf("%u %s %s\n", key_size(public), fp, comment);
+               ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
+               printf("%u %s %s (%s)\n", key_size(public), fp, comment,
+                   key_type(public));
+               if (log_level >= SYSLOG_LEVEL_VERBOSE)
+                       printf("%s\n", ra);
                key_free(public);
                xfree(comment);
+               xfree(ra);
                xfree(fp);
                exit(0);
        }
@@ -522,9 +547,9 @@ do_fingerprint(struct passwd *pw)
        f = fopen(identity_file, "r");
        if (f != NULL) {
                while (fgets(line, sizeof(line), f)) {
-                       i = strlen(line) - 1;
-                       if (line[i] != '\n') {
-                               error("line %d too long: %.40s...", num, line);
+                       if ((cp = strchr(line, '\n')) == NULL) {
+                               error("line %d too long: %.40s...",
+                                   num + 1, line);
                                skip = 1;
                                continue;
                        }
@@ -533,13 +558,13 @@ do_fingerprint(struct passwd *pw)
                                skip = 0;
                                continue;
                        }
-                       line[i] = '\0';
+                       *cp = '\0';
 
                        /* Skip leading whitespace, empty and comment lines. */
                        for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                                ;
                        if (!*cp || *cp == '\n' || *cp == '#')
-                               continue ;
+                               continue;
                        i = strtol(cp, &ep, 10);
                        if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
                                int quoted = 0;
@@ -568,8 +593,12 @@ do_fingerprint(struct passwd *pw)
                        }
                        comment = *cp ? cp : comment;
                        fp = key_fingerprint(public, fptype, rep);
-                       printf("%u %s %s\n", key_size(public), fp,
-                           comment ? comment : "no comment");
+                       ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
+                       printf("%u %s %s (%s)\n", key_size(public), fp,
+                           comment ? comment : "no comment", key_type(public));
+                       if (log_level >= SYSLOG_LEVEL_VERBOSE)
+                               printf("%s\n", ra);
+                       xfree(ra);
                        xfree(fp);
                        key_free(public);
                        invalid = 0;
@@ -584,14 +613,31 @@ do_fingerprint(struct passwd *pw)
 }
 
 static void
-print_host(FILE *f, char *name, Key *public, int hash)
+print_host(FILE *f, const char *name, Key *public, int hash)
 {
-       if (hash && (name = host_hash(name, NULL, 0)) == NULL)
-               fatal("hash_host failed");
-       fprintf(f, "%s ", name);
-       if (!key_write(public, f))
-               fatal("key_write failed");
-       fprintf(f, "\n");
+       if (print_fingerprint) {
+               enum fp_rep rep;
+               enum fp_type fptype;
+               char *fp, *ra;
+
+               fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
+               rep =    print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
+               fp = key_fingerprint(public, fptype, rep);
+               ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
+               printf("%u %s %s (%s)\n", key_size(public), fp, name,
+                   key_type(public));
+               if (log_level >= SYSLOG_LEVEL_VERBOSE)
+                       printf("%s\n", ra);
+               xfree(ra);
+               xfree(fp);
+       } else {
+               if (hash && (name = host_hash(name, NULL, 0)) == NULL)
+                       fatal("hash_host failed");
+               fprintf(f, "%s ", name);
+               if (!key_write(public, f))
+                       fatal("key_write failed");
+               fprintf(f, "\n");
+       }
 }
 
 static void
@@ -601,7 +647,7 @@ do_known_hosts(struct passwd *pw, const char *name)
        Key *public;
        char *cp, *cp2, *kp, *kp2;
        char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
-       int c, i, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
+       int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
 
        if (!have_identity) {
                cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
@@ -636,19 +682,18 @@ do_known_hosts(struct passwd *pw, const char *name)
        }
 
        while (fgets(line, sizeof(line), in)) {
-               num++;
-               i = strlen(line) - 1;
-               if (line[i] != '\n') {
-                       error("line %d too long: %.40s...", num, line);
+               if ((cp = strchr(line, '\n')) == NULL) {
+                       error("line %d too long: %.40s...", num + 1, line);
                        skip = 1;
                        invalid = 1;
                        continue;
                }
+               num++;
                if (skip) {
                        skip = 0;
                        continue;
                }
-               line[i] = '\0';
+               *cp = '\0';
 
                /* Skip leading whitespace, empty and comment lines. */
                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
@@ -712,7 +757,8 @@ do_known_hosts(struct passwd *pw, const char *name)
                                        printf("# Host %s found: "
                                            "line %d type %s\n", name,
                                            num, key_type(public));
-                                       print_host(out, cp, public, hash_hosts);
+                                       print_host(out, name, public,
+                                           hash_hosts);
                                }
                                if (delete_host && !c)
                                        print_host(out, cp, public, 0);
@@ -736,7 +782,7 @@ do_known_hosts(struct passwd *pw, const char *name)
        fclose(in);
 
        if (invalid) {
-               fprintf(stderr, "%s is not a valid known_host file.\n",
+               fprintf(stderr, "%s is not a valid known_hosts file.\n",
                    identity_file);
                if (inplace) {
                        fprintf(stderr, "Not replacing existing known_hosts "
@@ -948,8 +994,7 @@ do_change_comment(struct passwd *pw)
                        key_free(private);
                        exit(1);
                }
-               if (strchr(new_comment, '\n'))
-                       *strchr(new_comment, '\n') = 0;
+               new_comment[strcspn(new_comment, "\n")] = '\0';
        }
 
        /* Save the file using the new passphrase. */
@@ -974,11 +1019,11 @@ do_change_comment(struct passwd *pw)
        }
        f = fdopen(fd, "w");
        if (f == NULL) {
-               printf("fdopen %s failed", identity_file);
+               printf("fdopen %s failed\n", identity_file);
                exit(1);
        }
        if (!key_write(public, f))
-               fprintf(stderr, "write key failed");
+               fprintf(stderr, "write key failed\n");
        key_free(public);
        fprintf(f, " %s\n", new_comment);
        fclose(f);
@@ -992,7 +1037,7 @@ do_change_comment(struct passwd *pw)
 static void
 usage(void)
 {
-       fprintf(stderr, "Usage: %s [options]\n", __progname);
+       fprintf(stderr, "usage: %s [options]\n", __progname);
        fprintf(stderr, "Options:\n");
        fprintf(stderr, "  -a trials   Number of trials for screening DH-GEX moduli.\n");
        fprintf(stderr, "  -B          Show bubblebabble digest of key file.\n");
@@ -1002,13 +1047,13 @@ usage(void)
 #ifdef SMARTCARD
        fprintf(stderr, "  -D reader   Download public key from smartcard.\n");
 #endif /* SMARTCARD */
-       fprintf(stderr, "  -e          Convert OpenSSH to IETF SECSH key file.\n");
+       fprintf(stderr, "  -e          Convert OpenSSH to RFC 4716 key file.\n");
        fprintf(stderr, "  -F hostname Find hostname in known hosts file.\n");
        fprintf(stderr, "  -f filename Filename of the key file.\n");
        fprintf(stderr, "  -G file     Generate candidates for DH-GEX moduli.\n");
        fprintf(stderr, "  -g          Use generic DNS resource record format.\n");
        fprintf(stderr, "  -H          Hash names in known_hosts file.\n");
-       fprintf(stderr, "  -i          Convert IETF SECSH to OpenSSH key file.\n");
+       fprintf(stderr, "  -i          Convert RFC 4716 to OpenSSH key file.\n");
        fprintf(stderr, "  -l          Show fingerprint of key file.\n");
        fprintf(stderr, "  -M memory   Amount of memory (MB) to use for generating DH-GEX moduli.\n");
        fprintf(stderr, "  -N phrase   Provide new passphrase.\n");
@@ -1034,7 +1079,7 @@ usage(void)
  * Main program for key management.
  */
 int
-main(int ac, char **av)
+main(int argc, char **argv)
 {
        char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
        char out_file[MAXPATHLEN], *reader_id = NULL;
@@ -1045,7 +1090,6 @@ main(int ac, char **av)
        int opt, type, fd, download = 0;
        u_int32_t memory = 0, generator_wanted = 0, trials = 100;
        int do_gen_candidates = 0, do_screen_candidates = 0;
-       int log_level = SYSLOG_LEVEL_INFO;
        BIGNUM *start = NULL;
        FILE *f;
        const char *errstr;
@@ -1056,10 +1100,10 @@ main(int ac, char **av)
        /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
        sanitise_stdfd();
 
-       __progname = ssh_get_progname(av[0]);
+       __progname = ssh_get_progname(argv[0]);
 
        SSLeay_add_all_algorithms();
-       log_init(av[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
+       log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
 
        init_rng();
        seed_rng();
@@ -1075,7 +1119,7 @@ main(int ac, char **av)
                exit(1);
        }
 
-       while ((opt = getopt(ac, av,
+       while ((opt = getopt(argc, argv,
            "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
                switch (opt) {
                case 'b':
@@ -1208,9 +1252,9 @@ main(int ac, char **av)
        }
 
        /* reinit */
-       log_init(av[0], log_level, SYSLOG_FACILITY_USER, 1);
+       log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
 
-       if (optind < ac) {
+       if (optind < argc) {
                printf("Too many arguments.\n");
                usage();
        }
@@ -1218,6 +1262,10 @@ main(int ac, char **av)
                printf("Can only have one of -p and -c.\n");
                usage();
        }
+       if (print_fingerprint && (delete_host || hash_hosts)) {
+               printf("Cannot use -l with -D or -R.\n");
+               usage();
+       }
        if (delete_host || hash_hosts || find_host)
                do_known_hosts(pw, rr_hostname);
        if (print_fingerprint || print_bubblebabble)
@@ -1322,7 +1370,7 @@ main(int ac, char **av)
                printf("Generating public/private %s key pair.\n", key_type_name);
        private = key_generate(type, bits);
        if (private == NULL) {
-               fprintf(stderr, "key_generate failed");
+               fprintf(stderr, "key_generate failed\n");
                exit(1);
        }
        public  = key_from_private(private);
@@ -1382,7 +1430,7 @@ passphrase_again:
        if (identity_comment) {
                strlcpy(comment, identity_comment, sizeof(comment));
        } else {
-               /* Create default commend field for the passphrase. */
+               /* Create default comment field for the passphrase. */
                snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
        }
 
@@ -1412,20 +1460,25 @@ passphrase_again:
        }
        f = fdopen(fd, "w");
        if (f == NULL) {
-               printf("fdopen %s failed", identity_file);
+               printf("fdopen %s failed\n", identity_file);
                exit(1);
        }
        if (!key_write(public, f))
-               fprintf(stderr, "write key failed");
+               fprintf(stderr, "write key failed\n");
        fprintf(f, " %s\n", comment);
        fclose(f);
 
        if (!quiet) {
                char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
+               char *ra = key_fingerprint(public, SSH_FP_MD5,
+                   SSH_FP_RANDOMART);
                printf("Your public key has been saved in %s.\n",
                    identity_file);
                printf("The key fingerprint is:\n");
                printf("%s %s\n", fp, comment);
+               printf("The key's randomart image is:\n");
+               printf("%s\n", ra);
+               xfree(ra);
                xfree(fp);
        }
 
This page took 0.332209 seconds and 4 git commands to generate.