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