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