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