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