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