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