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