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