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