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