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