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