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