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 | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 15 | RCSID("$OpenBSD: ssh-keygen.c,v 1.34 2000/11/15 20:24:43 millert 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> |
| 19 | #include <openssl/rsa.h> |
| 20 | #include <openssl/dsa.h> |
| 21 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 22 | #include "ssh.h" |
| 23 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 24 | #include "key.h" |
| 25 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 26 | #include "authfile.h" |
| 27 | #include "uuencode.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 29 | #include "buffer.h" |
| 30 | #include "bufaux.h" |
| 31 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 32 | /* 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] | 33 | int bits = 1024; |
| 34 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 35 | /* |
| 36 | * Flag indicating that we just want to change the passphrase. This can be |
| 37 | * set on the command line. |
| 38 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | int change_passphrase = 0; |
| 40 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 41 | /* |
| 42 | * Flag indicating that we just want to change the comment. This can be set |
| 43 | * on the command line. |
| 44 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | int change_comment = 0; |
| 46 | |
| 47 | int quiet = 0; |
| 48 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 49 | /* Flag indicating that we just want to see the key fingerprint */ |
| 50 | int print_fingerprint = 0; |
| 51 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 52 | /* The identity file name, given on the command line or entered by the user. */ |
| 53 | char identity_file[1024]; |
| 54 | int have_identity = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 55 | |
| 56 | /* This is set to the passphrase if given on the command line. */ |
| 57 | char *identity_passphrase = NULL; |
| 58 | |
| 59 | /* This is set to the new passphrase if given on the command line. */ |
| 60 | char *identity_new_passphrase = NULL; |
| 61 | |
| 62 | /* This is set to the new comment if given on the command line. */ |
| 63 | char *identity_comment = NULL; |
| 64 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 65 | /* Dump public key file in format used by real and the original SSH 2 */ |
| 66 | int convert_to_ssh2 = 0; |
| 67 | int convert_from_ssh2 = 0; |
| 68 | int print_public = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 69 | |
| 70 | /* key type */ |
| 71 | int dsa_mode = 0; /* compat */ |
| 72 | char *key_type_name = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 73 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 74 | /* argv0 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 75 | #ifdef HAVE___PROGNAME |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 76 | extern char *__progname; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 77 | #else |
| 78 | char *__progname; |
| 79 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 80 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 81 | char hostname[MAXHOSTNAMELEN]; |
| 82 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 83 | void |
| 84 | ask_filename(struct passwd *pw, const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 86 | char buf[1024]; |
| 87 | snprintf(identity_file, sizeof(identity_file), "%s/%s", |
Damien Miller | e247cc4 | 2000-05-07 12:03:14 +1000 | [diff] [blame] | 88 | pw->pw_dir, |
| 89 | dsa_mode ? SSH_CLIENT_ID_DSA: SSH_CLIENT_IDENTITY); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 90 | printf("%s (%s): ", prompt, identity_file); |
| 91 | fflush(stdout); |
| 92 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 93 | exit(1); |
| 94 | if (strchr(buf, '\n')) |
| 95 | *strchr(buf, '\n') = 0; |
| 96 | if (strcmp(buf, "") != 0) |
| 97 | strlcpy(identity_file, buf, sizeof(identity_file)); |
| 98 | have_identity = 1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 99 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 101 | int |
| 102 | try_load_key(char *filename, Key *k) |
| 103 | { |
| 104 | int success = 1; |
| 105 | if (!load_private_key(filename, "", k, NULL)) { |
| 106 | char *pass = read_passphrase("Enter passphrase: ", 1); |
| 107 | if (!load_private_key(filename, pass, k, NULL)) { |
| 108 | success = 0; |
| 109 | } |
| 110 | memset(pass, 0, strlen(pass)); |
| 111 | xfree(pass); |
| 112 | } |
| 113 | return success; |
| 114 | } |
| 115 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 116 | #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" |
| 117 | #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----" |
| 118 | #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----" |
| 119 | #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 120 | |
| 121 | void |
| 122 | do_convert_to_ssh2(struct passwd *pw) |
| 123 | { |
| 124 | Key *k; |
| 125 | int len; |
| 126 | unsigned char *blob; |
| 127 | struct stat st; |
| 128 | |
| 129 | if (!have_identity) |
| 130 | ask_filename(pw, "Enter file in which the key is"); |
| 131 | if (stat(identity_file, &st) < 0) { |
| 132 | perror(identity_file); |
| 133 | exit(1); |
| 134 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 135 | k = key_new(KEY_UNSPEC); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 136 | if (!try_load_key(identity_file, k)) { |
| 137 | fprintf(stderr, "load failed\n"); |
| 138 | exit(1); |
| 139 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 140 | key_to_blob(k, &blob, &len); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 141 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 142 | fprintf(stdout, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 143 | "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n", |
| 144 | key_size(k), key_type(k), |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 145 | pw->pw_name, hostname); |
| 146 | dump_base64(stdout, blob, len); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 147 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 148 | key_free(k); |
| 149 | xfree(blob); |
| 150 | exit(0); |
| 151 | } |
| 152 | |
| 153 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 154 | buffer_get_bignum_bits(Buffer *b, BIGNUM *value) |
| 155 | { |
| 156 | int bits = buffer_get_int(b); |
| 157 | int bytes = (bits + 7) / 8; |
| 158 | if (buffer_len(b) < bytes) |
| 159 | fatal("buffer_get_bignum_bits: input buffer too small"); |
| 160 | BN_bin2bn((unsigned char *)buffer_ptr(b), bytes, value); |
| 161 | buffer_consume(b, bytes); |
| 162 | } |
| 163 | |
| 164 | Key * |
| 165 | do_convert_private_ssh2_from_blob(char *blob, int blen) |
| 166 | { |
| 167 | Buffer b; |
| 168 | DSA *dsa; |
| 169 | Key *key = NULL; |
| 170 | int ignore, magic, rlen; |
| 171 | char *type, *cipher; |
| 172 | |
| 173 | buffer_init(&b); |
| 174 | buffer_append(&b, blob, blen); |
| 175 | |
| 176 | magic = buffer_get_int(&b); |
| 177 | if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { |
| 178 | error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC); |
| 179 | buffer_free(&b); |
| 180 | return NULL; |
| 181 | } |
| 182 | ignore = buffer_get_int(&b); |
| 183 | type = buffer_get_string(&b, NULL); |
| 184 | cipher = buffer_get_string(&b, NULL); |
| 185 | ignore = buffer_get_int(&b); |
| 186 | ignore = buffer_get_int(&b); |
| 187 | ignore = buffer_get_int(&b); |
| 188 | xfree(type); |
| 189 | |
| 190 | if (strcmp(cipher, "none") != 0) { |
| 191 | error("unsupported cipher %s", cipher); |
| 192 | xfree(cipher); |
| 193 | buffer_free(&b); |
| 194 | return NULL; |
| 195 | } |
| 196 | xfree(cipher); |
| 197 | |
| 198 | key = key_new(KEY_DSA); |
| 199 | dsa = key->dsa; |
| 200 | dsa->priv_key = BN_new(); |
| 201 | if (dsa->priv_key == NULL) { |
| 202 | error("alloc priv_key failed"); |
| 203 | key_free(key); |
| 204 | return NULL; |
| 205 | } |
| 206 | buffer_get_bignum_bits(&b, dsa->p); |
| 207 | buffer_get_bignum_bits(&b, dsa->g); |
| 208 | buffer_get_bignum_bits(&b, dsa->q); |
| 209 | buffer_get_bignum_bits(&b, dsa->pub_key); |
| 210 | buffer_get_bignum_bits(&b, dsa->priv_key); |
| 211 | rlen = buffer_len(&b); |
| 212 | if(rlen != 0) |
| 213 | error("do_convert_private_ssh2_from_blob: remaining bytes in key blob %d", rlen); |
| 214 | buffer_free(&b); |
| 215 | return key; |
| 216 | } |
| 217 | |
| 218 | void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 219 | do_convert_from_ssh2(struct passwd *pw) |
| 220 | { |
| 221 | Key *k; |
| 222 | int blen; |
| 223 | char line[1024], *p; |
| 224 | char blob[8096]; |
| 225 | char encoded[8096]; |
| 226 | struct stat st; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 227 | int escaped = 0, private = 0, ok; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 228 | FILE *fp; |
| 229 | |
| 230 | if (!have_identity) |
| 231 | ask_filename(pw, "Enter file in which the key is"); |
| 232 | if (stat(identity_file, &st) < 0) { |
| 233 | perror(identity_file); |
| 234 | exit(1); |
| 235 | } |
| 236 | fp = fopen(identity_file, "r"); |
| 237 | if (fp == NULL) { |
| 238 | perror(identity_file); |
| 239 | exit(1); |
| 240 | } |
| 241 | encoded[0] = '\0'; |
| 242 | while (fgets(line, sizeof(line), fp)) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 243 | if (!(p = strchr(line, '\n'))) { |
| 244 | fprintf(stderr, "input line too long.\n"); |
| 245 | exit(1); |
| 246 | } |
| 247 | if (p > line && p[-1] == '\\') |
| 248 | escaped++; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 249 | if (strncmp(line, "----", 4) == 0 || |
| 250 | strstr(line, ": ") != NULL) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 251 | if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL) |
| 252 | private = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 253 | fprintf(stderr, "ignore: %s", line); |
| 254 | continue; |
| 255 | } |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 256 | if (escaped) { |
| 257 | escaped--; |
| 258 | fprintf(stderr, "escaped: %s", line); |
| 259 | continue; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 260 | } |
| 261 | *p = '\0'; |
| 262 | strlcat(encoded, line, sizeof(encoded)); |
| 263 | } |
| 264 | blen = uudecode(encoded, (unsigned char *)blob, sizeof(blob)); |
| 265 | if (blen < 0) { |
| 266 | fprintf(stderr, "uudecode failed.\n"); |
| 267 | exit(1); |
| 268 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 269 | k = private ? |
| 270 | do_convert_private_ssh2_from_blob(blob, blen) : |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 271 | key_from_blob(blob, blen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 272 | if (k == NULL) { |
| 273 | fprintf(stderr, "decode blob failed.\n"); |
| 274 | exit(1); |
| 275 | } |
| 276 | ok = private ? |
| 277 | PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) : |
| 278 | key_write(k, stdout); |
| 279 | if (!ok) { |
| 280 | fprintf(stderr, "key write failed"); |
| 281 | exit(1); |
| 282 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 283 | key_free(k); |
| 284 | fprintf(stdout, "\n"); |
| 285 | fclose(fp); |
| 286 | exit(0); |
| 287 | } |
| 288 | |
| 289 | void |
| 290 | do_print_public(struct passwd *pw) |
| 291 | { |
| 292 | Key *k; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 293 | struct stat st; |
| 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 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 301 | k = key_new(KEY_UNSPEC); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 302 | if (!try_load_key(identity_file, k)) { |
| 303 | fprintf(stderr, "load failed\n"); |
| 304 | exit(1); |
| 305 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 306 | if (!key_write(k, stdout)) |
| 307 | fprintf(stderr, "key_write failed"); |
| 308 | key_free(k); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 309 | fprintf(stdout, "\n"); |
| 310 | exit(0); |
| 311 | } |
| 312 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 313 | void |
| 314 | do_fingerprint(struct passwd *pw) |
| 315 | { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 316 | |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 317 | FILE *f; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 318 | Key *public; |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 319 | char *comment = NULL, *cp, *ep, line[16*1024]; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 320 | int i, skip = 0, num = 1, invalid = 1, success = 0; |
Damien Miller | 7684ee1 | 2000-03-17 23:40:15 +1100 | [diff] [blame] | 321 | unsigned int ignore; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 322 | struct stat st; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 323 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 324 | if (!have_identity) |
| 325 | ask_filename(pw, "Enter file in which the key is"); |
| 326 | if (stat(identity_file, &st) < 0) { |
| 327 | perror(identity_file); |
| 328 | exit(1); |
| 329 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 330 | public = key_new(KEY_RSA1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 331 | if (load_public_key(identity_file, public, &comment)) { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 332 | success = 1; |
| 333 | } else { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 334 | key_free(public); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 335 | public = key_new(KEY_UNSPEC); |
| 336 | if (try_load_public_key(identity_file, public, &comment)) |
| 337 | success = 1; |
| 338 | else |
| 339 | error("try_load_public_key KEY_UNSPEC failed"); |
| 340 | } |
| 341 | if (success) { |
| 342 | printf("%d %s %s\n", key_size(public), key_fingerprint(public), comment); |
| 343 | key_free(public); |
| 344 | xfree(comment); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 345 | exit(0); |
| 346 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 347 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 348 | /* XXX RSA1 only */ |
| 349 | |
| 350 | public = key_new(KEY_RSA1); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 351 | f = fopen(identity_file, "r"); |
| 352 | if (f != NULL) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 353 | while (fgets(line, sizeof(line), f)) { |
| 354 | i = strlen(line) - 1; |
| 355 | if (line[i] != '\n') { |
| 356 | error("line %d too long: %.40s...", num, line); |
| 357 | skip = 1; |
| 358 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 359 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 360 | num++; |
| 361 | if (skip) { |
| 362 | skip = 0; |
| 363 | continue; |
| 364 | } |
| 365 | line[i] = '\0'; |
| 366 | |
| 367 | /* Skip leading whitespace, empty and comment lines. */ |
| 368 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 369 | ; |
| 370 | if (!*cp || *cp == '\n' || *cp == '#') |
| 371 | continue ; |
| 372 | i = strtol(cp, &ep, 10); |
| 373 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { |
| 374 | int quoted = 0; |
| 375 | comment = cp; |
| 376 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 377 | if (*cp == '\\' && cp[1] == '"') |
| 378 | cp++; /* Skip both */ |
| 379 | else if (*cp == '"') |
| 380 | quoted = !quoted; |
| 381 | } |
| 382 | if (!*cp) |
| 383 | continue; |
| 384 | *cp++ = '\0'; |
| 385 | } |
| 386 | ep = cp; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 387 | if (auth_rsa_read_key(&cp, &ignore, public->rsa->e, public->rsa->n)) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 388 | invalid = 0; |
| 389 | comment = *cp ? cp : comment; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 390 | printf("%d %s %s\n", key_size(public), |
| 391 | key_fingerprint(public), |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 392 | comment ? comment : "no comment"); |
| 393 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 394 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 395 | fclose(f); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 396 | } |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 397 | key_free(public); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 398 | if (invalid) { |
| 399 | printf("%s is not a valid key file.\n", identity_file); |
| 400 | exit(1); |
| 401 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 402 | exit(0); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 403 | } |
| 404 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 405 | /* |
| 406 | * Perform changing a passphrase. The argument is the passwd structure |
| 407 | * for the current user. |
| 408 | */ |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 409 | void |
| 410 | do_change_passphrase(struct passwd *pw) |
| 411 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 412 | char *comment; |
| 413 | char *old_passphrase, *passphrase1, *passphrase2; |
| 414 | struct stat st; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 415 | Key *private; |
| 416 | Key *public; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 417 | int type = KEY_RSA1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 418 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 419 | if (!have_identity) |
| 420 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 421 | if (stat(identity_file, &st) < 0) { |
| 422 | perror(identity_file); |
| 423 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 424 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 425 | public = key_new(type); |
| 426 | if (!load_public_key(identity_file, public, NULL)) { |
| 427 | type = KEY_UNSPEC; |
| 428 | } else { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 429 | /* Clear the public key since we are just about to load the whole file. */ |
| 430 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 431 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 432 | /* Try to load the file with empty passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 433 | private = key_new(type); |
| 434 | if (!load_private_key(identity_file, "", private, &comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 435 | if (identity_passphrase) |
| 436 | old_passphrase = xstrdup(identity_passphrase); |
| 437 | else |
| 438 | old_passphrase = read_passphrase("Enter old passphrase: ", 1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 439 | if (!load_private_key(identity_file, old_passphrase, private, &comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 440 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 441 | xfree(old_passphrase); |
| 442 | printf("Bad passphrase.\n"); |
| 443 | exit(1); |
| 444 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 445 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 446 | xfree(old_passphrase); |
| 447 | } |
| 448 | printf("Key has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 449 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 450 | /* Ask the new passphrase (twice). */ |
| 451 | if (identity_new_passphrase) { |
| 452 | passphrase1 = xstrdup(identity_new_passphrase); |
| 453 | passphrase2 = NULL; |
| 454 | } else { |
| 455 | passphrase1 = |
| 456 | read_passphrase("Enter new passphrase (empty for no passphrase): ", 1); |
| 457 | passphrase2 = read_passphrase("Enter same passphrase again: ", 1); |
| 458 | |
| 459 | /* Verify that they are the same. */ |
| 460 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 461 | memset(passphrase1, 0, strlen(passphrase1)); |
| 462 | memset(passphrase2, 0, strlen(passphrase2)); |
| 463 | xfree(passphrase1); |
| 464 | xfree(passphrase2); |
| 465 | printf("Pass phrases do not match. Try again.\n"); |
| 466 | exit(1); |
| 467 | } |
| 468 | /* Destroy the other copy. */ |
| 469 | memset(passphrase2, 0, strlen(passphrase2)); |
| 470 | xfree(passphrase2); |
| 471 | } |
| 472 | |
| 473 | /* Save the file using the new passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 474 | if (!save_private_key(identity_file, passphrase1, private, comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 475 | printf("Saving the key failed: %s: %s.\n", |
| 476 | identity_file, strerror(errno)); |
| 477 | memset(passphrase1, 0, strlen(passphrase1)); |
| 478 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 479 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 480 | xfree(comment); |
| 481 | exit(1); |
| 482 | } |
| 483 | /* Destroy the passphrase and the copy of the key in memory. */ |
| 484 | memset(passphrase1, 0, strlen(passphrase1)); |
| 485 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 486 | key_free(private); /* Destroys contents */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 487 | xfree(comment); |
| 488 | |
| 489 | printf("Your identification has been saved with the new passphrase.\n"); |
| 490 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 491 | } |
| 492 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 493 | /* |
| 494 | * Change the comment of a private key file. |
| 495 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 496 | void |
| 497 | do_change_comment(struct passwd *pw) |
| 498 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 499 | char new_comment[1024], *comment; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 500 | Key *private; |
| 501 | Key *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 502 | char *passphrase; |
| 503 | struct stat st; |
| 504 | FILE *f; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 505 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 506 | if (!have_identity) |
| 507 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 508 | if (stat(identity_file, &st) < 0) { |
| 509 | perror(identity_file); |
| 510 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 511 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 512 | /* |
| 513 | * Try to load the public key from the file the verify that it is |
| 514 | * readable and of the proper format. |
| 515 | */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 516 | public = key_new(KEY_RSA1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 517 | if (!load_public_key(identity_file, public, NULL)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 518 | printf("%s is not a valid key file.\n", identity_file); |
| 519 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 520 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 521 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 522 | private = key_new(KEY_RSA1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 523 | if (load_private_key(identity_file, "", private, &comment)) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 524 | passphrase = xstrdup(""); |
| 525 | else { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 526 | if (identity_passphrase) |
| 527 | passphrase = xstrdup(identity_passphrase); |
| 528 | else if (identity_new_passphrase) |
| 529 | passphrase = xstrdup(identity_new_passphrase); |
| 530 | else |
| 531 | passphrase = read_passphrase("Enter passphrase: ", 1); |
| 532 | /* Try to load using the passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 533 | if (!load_private_key(identity_file, passphrase, private, &comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 534 | memset(passphrase, 0, strlen(passphrase)); |
| 535 | xfree(passphrase); |
| 536 | printf("Bad passphrase.\n"); |
| 537 | exit(1); |
| 538 | } |
| 539 | } |
| 540 | printf("Key now has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 541 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 542 | if (identity_comment) { |
| 543 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); |
| 544 | } else { |
| 545 | printf("Enter new comment: "); |
| 546 | fflush(stdout); |
| 547 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { |
| 548 | memset(passphrase, 0, strlen(passphrase)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 549 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 550 | exit(1); |
| 551 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 552 | if (strchr(new_comment, '\n')) |
| 553 | *strchr(new_comment, '\n') = 0; |
| 554 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 555 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 556 | /* Save the file using the new passphrase. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 557 | if (!save_private_key(identity_file, passphrase, private, new_comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 558 | printf("Saving the key failed: %s: %s.\n", |
| 559 | identity_file, strerror(errno)); |
| 560 | memset(passphrase, 0, strlen(passphrase)); |
| 561 | xfree(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 562 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 563 | xfree(comment); |
| 564 | exit(1); |
| 565 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 566 | memset(passphrase, 0, strlen(passphrase)); |
| 567 | xfree(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 568 | key_free(private); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 569 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 570 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
| 571 | f = fopen(identity_file, "w"); |
| 572 | if (!f) { |
| 573 | printf("Could not save your public key in %s\n", identity_file); |
| 574 | exit(1); |
| 575 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 576 | if (!key_write(public, f)) |
| 577 | fprintf(stderr, "write key failed"); |
| 578 | key_free(public); |
| 579 | fprintf(f, " %s\n", new_comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 580 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 581 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 582 | xfree(comment); |
| 583 | |
| 584 | printf("The comment in your key file has been changed.\n"); |
| 585 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 586 | } |
| 587 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 588 | void |
| 589 | usage(void) |
| 590 | { |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 591 | printf("Usage: %s [-lpqxXyc] [-t type] [-b bits] [-f file] [-C comment] [-N new-pass] [-P pass]\n", __progname); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 592 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 593 | } |
| 594 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 595 | /* |
| 596 | * Main program for key management. |
| 597 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 598 | int |
| 599 | main(int ac, char **av) |
| 600 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 601 | char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2; |
| 602 | struct passwd *pw; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 603 | int opt; |
| 604 | struct stat st; |
| 605 | FILE *f; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 606 | int type = KEY_RSA1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 607 | Key *private; |
| 608 | Key *public; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 609 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 610 | extern int optind; |
| 611 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 612 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 613 | __progname = get_progname(av[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 614 | init_rng(); |
| 615 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 616 | SSLeay_add_all_algorithms(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 617 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 618 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 619 | pw = getpwuid(getuid()); |
| 620 | if (!pw) { |
| 621 | printf("You don't exist, go away!\n"); |
| 622 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 623 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 624 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 625 | perror("gethostname"); |
| 626 | exit(1); |
| 627 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 628 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 629 | while ((opt = getopt(ac, av, "dqpclRxXyb:f:t:P:N:C:")) != EOF) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 630 | switch (opt) { |
| 631 | case 'b': |
| 632 | bits = atoi(optarg); |
| 633 | if (bits < 512 || bits > 32768) { |
| 634 | printf("Bits has bad value.\n"); |
| 635 | exit(1); |
| 636 | } |
| 637 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 638 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 639 | case 'l': |
| 640 | print_fingerprint = 1; |
| 641 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 642 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 643 | case 'p': |
| 644 | change_passphrase = 1; |
| 645 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 646 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 647 | case 'c': |
| 648 | change_comment = 1; |
| 649 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 650 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 651 | case 'f': |
| 652 | strlcpy(identity_file, optarg, sizeof(identity_file)); |
| 653 | have_identity = 1; |
| 654 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 655 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 656 | case 'P': |
| 657 | identity_passphrase = optarg; |
| 658 | break; |
| 659 | |
| 660 | case 'N': |
| 661 | identity_new_passphrase = optarg; |
| 662 | break; |
| 663 | |
| 664 | case 'C': |
| 665 | identity_comment = optarg; |
| 666 | break; |
| 667 | |
| 668 | case 'q': |
| 669 | quiet = 1; |
| 670 | break; |
| 671 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 672 | case 'R': |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 673 | /* unused */ |
| 674 | exit(0); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 675 | break; |
| 676 | |
| 677 | case 'x': |
| 678 | convert_to_ssh2 = 1; |
| 679 | break; |
| 680 | |
| 681 | case 'X': |
| 682 | convert_from_ssh2 = 1; |
| 683 | break; |
| 684 | |
| 685 | case 'y': |
| 686 | print_public = 1; |
| 687 | break; |
| 688 | |
| 689 | case 'd': |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 690 | key_type_name = "dsa"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 691 | dsa_mode = 1; |
| 692 | break; |
| 693 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 694 | case 't': |
| 695 | key_type_name = optarg; |
| 696 | dsa_mode = (strcmp(optarg, "dsa") == 0); |
| 697 | break; |
| 698 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 699 | case '?': |
| 700 | default: |
| 701 | usage(); |
| 702 | } |
| 703 | } |
| 704 | if (optind < ac) { |
| 705 | printf("Too many arguments.\n"); |
| 706 | usage(); |
| 707 | } |
| 708 | if (change_passphrase && change_comment) { |
| 709 | printf("Can only have one of -p and -c.\n"); |
| 710 | usage(); |
| 711 | } |
| 712 | if (print_fingerprint) |
| 713 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 714 | if (change_passphrase) |
| 715 | do_change_passphrase(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 716 | if (change_comment) |
| 717 | do_change_comment(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 718 | if (convert_to_ssh2) |
| 719 | do_convert_to_ssh2(pw); |
| 720 | if (convert_from_ssh2) |
| 721 | do_convert_from_ssh2(pw); |
| 722 | if (print_public) |
| 723 | do_print_public(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 724 | |
| 725 | arc4random_stir(); |
| 726 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 727 | if (key_type_name != NULL) { |
| 728 | type = key_type_from_name(key_type_name); |
| 729 | if (type == KEY_UNSPEC) { |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 730 | fprintf(stderr, "unknown key type %s\n", key_type_name); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 731 | exit(1); |
| 732 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 733 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 734 | if (!quiet) |
| 735 | printf("Generating public/private key pair.\n"); |
| 736 | private = key_generate(type, bits); |
| 737 | if (private == NULL) { |
| 738 | fprintf(stderr, "key_generate failed"); |
| 739 | exit(1); |
| 740 | } |
| 741 | public = key_from_private(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 742 | |
| 743 | if (!have_identity) |
| 744 | ask_filename(pw, "Enter file in which to save the key"); |
| 745 | |
| 746 | /* Create ~/.ssh directory if it doesn\'t already exist. */ |
| 747 | snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, SSH_USER_DIR); |
| 748 | if (strstr(identity_file, dotsshdir) != NULL && |
| 749 | stat(dotsshdir, &st) < 0) { |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 750 | if (mkdir(dotsshdir, 0700) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 751 | error("Could not create directory '%s'.", dotsshdir); |
| 752 | else if (!quiet) |
| 753 | printf("Created directory '%s'.\n", dotsshdir); |
| 754 | } |
| 755 | /* If the file already exists, ask the user to confirm. */ |
| 756 | if (stat(identity_file, &st) >= 0) { |
| 757 | char yesno[3]; |
| 758 | printf("%s already exists.\n", identity_file); |
| 759 | printf("Overwrite (y/n)? "); |
| 760 | fflush(stdout); |
| 761 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 762 | exit(1); |
| 763 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 764 | exit(1); |
| 765 | } |
| 766 | /* Ask for a passphrase (twice). */ |
| 767 | if (identity_passphrase) |
| 768 | passphrase1 = xstrdup(identity_passphrase); |
| 769 | else if (identity_new_passphrase) |
| 770 | passphrase1 = xstrdup(identity_new_passphrase); |
| 771 | else { |
| 772 | passphrase_again: |
| 773 | passphrase1 = |
| 774 | read_passphrase("Enter passphrase (empty for no passphrase): ", 1); |
| 775 | passphrase2 = read_passphrase("Enter same passphrase again: ", 1); |
| 776 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 777 | /* The passphrases do not match. Clear them and retry. */ |
| 778 | memset(passphrase1, 0, strlen(passphrase1)); |
| 779 | memset(passphrase2, 0, strlen(passphrase2)); |
| 780 | xfree(passphrase1); |
| 781 | xfree(passphrase2); |
| 782 | printf("Passphrases do not match. Try again.\n"); |
| 783 | goto passphrase_again; |
| 784 | } |
| 785 | /* Clear the other copy of the passphrase. */ |
| 786 | memset(passphrase2, 0, strlen(passphrase2)); |
| 787 | xfree(passphrase2); |
| 788 | } |
| 789 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 790 | if (identity_comment) { |
| 791 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 792 | } else { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 793 | /* Create default commend field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 794 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 795 | } |
| 796 | |
| 797 | /* Save the key with the given passphrase and comment. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 798 | if (!save_private_key(identity_file, passphrase1, private, comment)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 799 | printf("Saving the key failed: %s: %s.\n", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 800 | identity_file, strerror(errno)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 801 | memset(passphrase1, 0, strlen(passphrase1)); |
| 802 | xfree(passphrase1); |
| 803 | exit(1); |
| 804 | } |
| 805 | /* Clear the passphrase. */ |
| 806 | memset(passphrase1, 0, strlen(passphrase1)); |
| 807 | xfree(passphrase1); |
| 808 | |
| 809 | /* Clear the private key and the random number generator. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 810 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 811 | arc4random_stir(); |
| 812 | |
| 813 | if (!quiet) |
| 814 | printf("Your identification has been saved in %s.\n", identity_file); |
| 815 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 816 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
| 817 | f = fopen(identity_file, "w"); |
| 818 | if (!f) { |
| 819 | printf("Could not save your public key in %s\n", identity_file); |
| 820 | exit(1); |
| 821 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 822 | if (!key_write(public, f)) |
| 823 | fprintf(stderr, "write key failed"); |
| 824 | fprintf(f, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 825 | fclose(f); |
| 826 | |
| 827 | if (!quiet) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 828 | printf("Your public key has been saved in %s.\n", |
| 829 | identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 830 | printf("The key fingerprint is:\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 831 | printf("%s %s\n", key_fingerprint(public), comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 832 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 833 | |
| 834 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 835 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 836 | } |