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