Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 3 | * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Identity and host key generation and maintenance. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 6 | * |
| 7 | * As far as I am concerned, the code I have written for this software |
| 8 | * can be used freely for any purpose. Any derived versions of this |
| 9 | * software must be clearly marked as such, and if the derived work is |
| 10 | * incompatible with the protocol description in the RFC file, it must be |
| 11 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 13 | |
| 14 | #include "includes.h" |
Ben Lindstrom | 1af4d3b | 2001-10-03 17:18:37 +0000 | [diff] [blame^] | 15 | RCSID("$OpenBSD: ssh-keygen.c,v 1.82 2001/09/28 12:07:09 djm Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 17 | #include <openssl/evp.h> |
| 18 | #include <openssl/pem.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 19 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 20 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 21 | #include "key.h" |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 22 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 23 | #include "authfile.h" |
| 24 | #include "uuencode.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 25 | #include "buffer.h" |
| 26 | #include "bufaux.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 27 | #include "pathnames.h" |
| 28 | #include "log.h" |
| 29 | #include "readpass.h" |
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 | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 76 | /* default to RSA for SSH-1 */ |
| 77 | char *key_type_name = "rsa1"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 78 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 79 | /* argv0 */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 80 | #ifdef HAVE___PROGNAME |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 81 | extern char *__progname; |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 82 | #else |
| 83 | char *__progname; |
| 84 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 85 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 86 | char hostname[MAXHOSTNAMELEN]; |
| 87 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 88 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 89 | ask_filename(struct passwd *pw, const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 90 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | char buf[1024]; |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 92 | char *name = NULL; |
| 93 | |
| 94 | switch (key_type_from_name(key_type_name)) { |
| 95 | case KEY_RSA1: |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 96 | name = _PATH_SSH_CLIENT_IDENTITY; |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 97 | break; |
| 98 | case KEY_DSA: |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 99 | name = _PATH_SSH_CLIENT_ID_DSA; |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 100 | break; |
| 101 | case KEY_RSA: |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 102 | name = _PATH_SSH_CLIENT_ID_RSA; |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 103 | break; |
| 104 | default: |
| 105 | fprintf(stderr, "bad key type"); |
| 106 | exit(1); |
| 107 | break; |
| 108 | } |
| 109 | snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); |
Ben Lindstrom | 3deda8b | 2000-12-22 20:27:43 +0000 | [diff] [blame] | 110 | fprintf(stderr, "%s (%s): ", prompt, identity_file); |
| 111 | fflush(stderr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 112 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 113 | exit(1); |
| 114 | if (strchr(buf, '\n')) |
| 115 | *strchr(buf, '\n') = 0; |
| 116 | if (strcmp(buf, "") != 0) |
| 117 | strlcpy(identity_file, buf, sizeof(identity_file)); |
| 118 | have_identity = 1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 119 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 120 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 121 | static Key * |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 122 | load_identity(char *filename) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 123 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 124 | char *pass; |
| 125 | Key *prv; |
| 126 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 127 | prv = key_load_private(filename, "", NULL); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 128 | if (prv == NULL) { |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 129 | if (identity_passphrase) |
| 130 | pass = xstrdup(identity_passphrase); |
| 131 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 132 | pass = read_passphrase("Enter passphrase: ", |
| 133 | RP_ALLOW_STDIN); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 134 | prv = key_load_private(filename, pass, NULL); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 135 | memset(pass, 0, strlen(pass)); |
| 136 | xfree(pass); |
| 137 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 138 | return prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 139 | } |
| 140 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 141 | #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" |
| 142 | #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----" |
| 143 | #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----" |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 144 | #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 145 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 146 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 147 | do_convert_to_ssh2(struct passwd *pw) |
| 148 | { |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 149 | Key *k; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 150 | int len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 151 | u_char *blob; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 152 | struct stat st; |
| 153 | |
| 154 | if (!have_identity) |
| 155 | ask_filename(pw, "Enter file in which the key is"); |
| 156 | if (stat(identity_file, &st) < 0) { |
| 157 | perror(identity_file); |
| 158 | exit(1); |
| 159 | } |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 160 | if ((k = key_load_public(identity_file, NULL)) == NULL) { |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 161 | if ((k = load_identity(identity_file)) == NULL) { |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 162 | fprintf(stderr, "load failed\n"); |
| 163 | exit(1); |
| 164 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 165 | } |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 166 | if (key_to_blob(k, &blob, &len) <= 0) { |
| 167 | fprintf(stderr, "key_to_blob failed\n"); |
| 168 | exit(1); |
| 169 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 170 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 171 | fprintf(stdout, |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 172 | "Comment: \"%d-bit %s, converted from OpenSSH by %s@%s\"\n", |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 173 | key_size(k), key_type(k), |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 174 | pw->pw_name, hostname); |
| 175 | dump_base64(stdout, blob, len); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 176 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 177 | key_free(k); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 178 | xfree(blob); |
| 179 | exit(0); |
| 180 | } |
| 181 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 182 | static void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 183 | buffer_get_bignum_bits(Buffer *b, BIGNUM *value) |
| 184 | { |
| 185 | int bits = buffer_get_int(b); |
| 186 | int bytes = (bits + 7) / 8; |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 187 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 188 | if (buffer_len(b) < bytes) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 189 | fatal("buffer_get_bignum_bits: input buffer too small: " |
| 190 | "need %d have %d", bytes, buffer_len(b)); |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 191 | BN_bin2bn((u_char *)buffer_ptr(b), bytes, value); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 192 | buffer_consume(b, bytes); |
| 193 | } |
| 194 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 195 | static Key * |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 196 | do_convert_private_ssh2_from_blob(u_char *blob, int blen) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 197 | { |
| 198 | Buffer b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 199 | Key *key = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 200 | char *type, *cipher; |
Ben Lindstrom | 511d69e | 2001-07-04 05:05:27 +0000 | [diff] [blame] | 201 | u_char *sig, data[] = "abcde12345"; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 202 | int magic, rlen, ktype, i1, i2, i3, i4; |
| 203 | u_int slen; |
| 204 | u_long e; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 205 | |
| 206 | buffer_init(&b); |
| 207 | buffer_append(&b, blob, blen); |
| 208 | |
| 209 | magic = buffer_get_int(&b); |
| 210 | if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { |
| 211 | error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC); |
| 212 | buffer_free(&b); |
| 213 | return NULL; |
| 214 | } |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 215 | i1 = buffer_get_int(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 216 | type = buffer_get_string(&b, NULL); |
| 217 | cipher = buffer_get_string(&b, NULL); |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 218 | i2 = buffer_get_int(&b); |
| 219 | i3 = buffer_get_int(&b); |
| 220 | i4 = buffer_get_int(&b); |
| 221 | debug("ignore (%d %d %d %d)", i1,i2,i3,i4); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 222 | if (strcmp(cipher, "none") != 0) { |
| 223 | error("unsupported cipher %s", cipher); |
| 224 | xfree(cipher); |
| 225 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 226 | xfree(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 227 | return NULL; |
| 228 | } |
| 229 | xfree(cipher); |
| 230 | |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 231 | if (strstr(type, "dsa")) { |
| 232 | ktype = KEY_DSA; |
| 233 | } else if (strstr(type, "rsa")) { |
| 234 | ktype = KEY_RSA; |
| 235 | } else { |
| 236 | xfree(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 239 | key = key_new_private(ktype); |
| 240 | xfree(type); |
| 241 | |
| 242 | switch (key->type) { |
| 243 | case KEY_DSA: |
| 244 | buffer_get_bignum_bits(&b, key->dsa->p); |
| 245 | buffer_get_bignum_bits(&b, key->dsa->g); |
| 246 | buffer_get_bignum_bits(&b, key->dsa->q); |
| 247 | buffer_get_bignum_bits(&b, key->dsa->pub_key); |
| 248 | buffer_get_bignum_bits(&b, key->dsa->priv_key); |
| 249 | break; |
| 250 | case KEY_RSA: |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 251 | e = buffer_get_char(&b); |
| 252 | debug("e %lx", e); |
| 253 | if (e < 30) { |
| 254 | e <<= 8; |
| 255 | e += buffer_get_char(&b); |
| 256 | debug("e %lx", e); |
| 257 | e <<= 8; |
| 258 | e += buffer_get_char(&b); |
| 259 | debug("e %lx", e); |
| 260 | } |
| 261 | if (!BN_set_word(key->rsa->e, e)) { |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 262 | buffer_free(&b); |
| 263 | key_free(key); |
| 264 | return NULL; |
| 265 | } |
| 266 | buffer_get_bignum_bits(&b, key->rsa->d); |
| 267 | buffer_get_bignum_bits(&b, key->rsa->n); |
| 268 | buffer_get_bignum_bits(&b, key->rsa->iqmp); |
| 269 | buffer_get_bignum_bits(&b, key->rsa->q); |
| 270 | buffer_get_bignum_bits(&b, key->rsa->p); |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 271 | rsa_generate_additional_parameters(key->rsa); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 272 | break; |
| 273 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 274 | rlen = buffer_len(&b); |
| 275 | if(rlen != 0) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 276 | error("do_convert_private_ssh2_from_blob: " |
| 277 | "remaining bytes in key blob %d", rlen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 278 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 279 | |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 280 | /* try the key */ |
| 281 | key_sign(key, &sig, &slen, data, sizeof(data)); |
| 282 | key_verify(key, sig, slen, data, sizeof(data)); |
| 283 | xfree(sig); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 284 | return key; |
| 285 | } |
| 286 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 287 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 288 | do_convert_from_ssh2(struct passwd *pw) |
| 289 | { |
| 290 | Key *k; |
| 291 | int blen; |
| 292 | char line[1024], *p; |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 293 | u_char blob[8096]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 294 | char encoded[8096]; |
| 295 | struct stat st; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 296 | int escaped = 0, private = 0, ok; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 297 | FILE *fp; |
| 298 | |
| 299 | if (!have_identity) |
| 300 | ask_filename(pw, "Enter file in which the key is"); |
| 301 | if (stat(identity_file, &st) < 0) { |
| 302 | perror(identity_file); |
| 303 | exit(1); |
| 304 | } |
| 305 | fp = fopen(identity_file, "r"); |
| 306 | if (fp == NULL) { |
| 307 | perror(identity_file); |
| 308 | exit(1); |
| 309 | } |
| 310 | encoded[0] = '\0'; |
| 311 | while (fgets(line, sizeof(line), fp)) { |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 312 | if (!(p = strchr(line, '\n'))) { |
| 313 | fprintf(stderr, "input line too long.\n"); |
| 314 | exit(1); |
| 315 | } |
| 316 | if (p > line && p[-1] == '\\') |
| 317 | escaped++; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 318 | if (strncmp(line, "----", 4) == 0 || |
| 319 | strstr(line, ": ") != NULL) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 320 | if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL) |
| 321 | private = 1; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 322 | if (strstr(line, " END ") != NULL) { |
| 323 | break; |
| 324 | } |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 325 | /* fprintf(stderr, "ignore: %s", line); */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 326 | continue; |
| 327 | } |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 328 | if (escaped) { |
| 329 | escaped--; |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 330 | /* fprintf(stderr, "escaped: %s", line); */ |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 331 | continue; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 332 | } |
| 333 | *p = '\0'; |
| 334 | strlcat(encoded, line, sizeof(encoded)); |
| 335 | } |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 336 | blen = uudecode(encoded, (u_char *)blob, sizeof(blob)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 337 | if (blen < 0) { |
| 338 | fprintf(stderr, "uudecode failed.\n"); |
| 339 | exit(1); |
| 340 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 341 | k = private ? |
| 342 | do_convert_private_ssh2_from_blob(blob, blen) : |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 343 | key_from_blob(blob, blen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 344 | if (k == NULL) { |
| 345 | fprintf(stderr, "decode blob failed.\n"); |
| 346 | exit(1); |
| 347 | } |
| 348 | ok = private ? |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 349 | (k->type == KEY_DSA ? |
| 350 | PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) : |
| 351 | PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) : |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 352 | key_write(k, stdout); |
| 353 | if (!ok) { |
| 354 | fprintf(stderr, "key write failed"); |
| 355 | exit(1); |
| 356 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 357 | key_free(k); |
| 358 | fprintf(stdout, "\n"); |
| 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; \ |
| 396 | } while(0) |
| 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 | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 537 | int i, skip = 0, num = 1, invalid = 1, rep, fptype; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 538 | struct stat st; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 539 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 540 | fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5; |
| 541 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 542 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 543 | if (!have_identity) |
| 544 | ask_filename(pw, "Enter file in which the key is"); |
| 545 | if (stat(identity_file, &st) < 0) { |
| 546 | perror(identity_file); |
| 547 | exit(1); |
| 548 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 549 | public = key_load_public(identity_file, &comment); |
| 550 | if (public != NULL) { |
| 551 | fp = key_fingerprint(public, fptype, rep); |
| 552 | printf("%d %s %s\n", key_size(public), fp, comment); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 553 | key_free(public); |
| 554 | xfree(comment); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 555 | xfree(fp); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 556 | exit(0); |
| 557 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 558 | if (comment) |
| 559 | xfree(comment); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 560 | |
| 561 | f = fopen(identity_file, "r"); |
| 562 | if (f != NULL) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 563 | while (fgets(line, sizeof(line), f)) { |
| 564 | i = strlen(line) - 1; |
| 565 | if (line[i] != '\n') { |
| 566 | error("line %d too long: %.40s...", num, line); |
| 567 | skip = 1; |
| 568 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 569 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 570 | num++; |
| 571 | if (skip) { |
| 572 | skip = 0; |
| 573 | continue; |
| 574 | } |
| 575 | line[i] = '\0'; |
| 576 | |
| 577 | /* Skip leading whitespace, empty and comment lines. */ |
| 578 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 579 | ; |
| 580 | if (!*cp || *cp == '\n' || *cp == '#') |
| 581 | continue ; |
| 582 | i = strtol(cp, &ep, 10); |
| 583 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { |
| 584 | int quoted = 0; |
| 585 | comment = cp; |
| 586 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 587 | if (*cp == '\\' && cp[1] == '"') |
| 588 | cp++; /* Skip both */ |
| 589 | else if (*cp == '"') |
| 590 | quoted = !quoted; |
| 591 | } |
| 592 | if (!*cp) |
| 593 | continue; |
| 594 | *cp++ = '\0'; |
| 595 | } |
| 596 | ep = cp; |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 597 | public = key_new(KEY_RSA1); |
| 598 | if (key_read(public, &cp) != 1) { |
| 599 | cp = ep; |
| 600 | key_free(public); |
| 601 | public = key_new(KEY_UNSPEC); |
| 602 | if (key_read(public, &cp) != 1) { |
| 603 | key_free(public); |
| 604 | continue; |
| 605 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 606 | } |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 607 | comment = *cp ? cp : comment; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 608 | fp = key_fingerprint(public, fptype, rep); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 609 | printf("%d %s %s\n", key_size(public), fp, |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 610 | comment ? comment : "no comment"); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 611 | xfree(fp); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 612 | key_free(public); |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 613 | invalid = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 614 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 615 | fclose(f); |
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 | if (invalid) { |
| 618 | printf("%s is not a valid key file.\n", identity_file); |
| 619 | exit(1); |
| 620 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 621 | exit(0); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 622 | } |
| 623 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 624 | /* |
| 625 | * Perform changing a passphrase. The argument is the passwd structure |
| 626 | * for the current user. |
| 627 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 628 | static void |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 629 | do_change_passphrase(struct passwd *pw) |
| 630 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 631 | char *comment; |
| 632 | char *old_passphrase, *passphrase1, *passphrase2; |
| 633 | struct stat st; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 634 | Key *private; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 635 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 636 | if (!have_identity) |
| 637 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 638 | if (stat(identity_file, &st) < 0) { |
| 639 | perror(identity_file); |
| 640 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 641 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 642 | /* Try to load the file with empty passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 643 | private = key_load_private(identity_file, "", &comment); |
| 644 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 645 | if (identity_passphrase) |
| 646 | old_passphrase = xstrdup(identity_passphrase); |
| 647 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 648 | old_passphrase = |
| 649 | read_passphrase("Enter old passphrase: ", |
| 650 | RP_ALLOW_STDIN); |
| 651 | private = key_load_private(identity_file, old_passphrase, |
| 652 | &comment); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 653 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 654 | xfree(old_passphrase); |
| 655 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 656 | printf("Bad passphrase.\n"); |
| 657 | exit(1); |
| 658 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 659 | } |
| 660 | printf("Key has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 661 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 662 | /* Ask the new passphrase (twice). */ |
| 663 | if (identity_new_passphrase) { |
| 664 | passphrase1 = xstrdup(identity_new_passphrase); |
| 665 | passphrase2 = NULL; |
| 666 | } else { |
| 667 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 668 | read_passphrase("Enter new passphrase (empty for no " |
| 669 | "passphrase): ", RP_ALLOW_STDIN); |
| 670 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
| 671 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 672 | |
| 673 | /* Verify that they are the same. */ |
| 674 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 675 | memset(passphrase1, 0, strlen(passphrase1)); |
| 676 | memset(passphrase2, 0, strlen(passphrase2)); |
| 677 | xfree(passphrase1); |
| 678 | xfree(passphrase2); |
| 679 | printf("Pass phrases do not match. Try again.\n"); |
| 680 | exit(1); |
| 681 | } |
| 682 | /* Destroy the other copy. */ |
| 683 | memset(passphrase2, 0, strlen(passphrase2)); |
| 684 | xfree(passphrase2); |
| 685 | } |
| 686 | |
| 687 | /* Save the file using the new passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 688 | if (!key_save_private(private, identity_file, passphrase1, comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 689 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 690 | memset(passphrase1, 0, strlen(passphrase1)); |
| 691 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 692 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 693 | xfree(comment); |
| 694 | exit(1); |
| 695 | } |
| 696 | /* Destroy the passphrase and the copy of the key in memory. */ |
| 697 | memset(passphrase1, 0, strlen(passphrase1)); |
| 698 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 699 | key_free(private); /* Destroys contents */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 700 | xfree(comment); |
| 701 | |
| 702 | printf("Your identification has been saved with the new passphrase.\n"); |
| 703 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 704 | } |
| 705 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 706 | /* |
| 707 | * Change the comment of a private key file. |
| 708 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 709 | static void |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 710 | do_change_comment(struct passwd *pw) |
| 711 | { |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 712 | char new_comment[1024], *comment, *passphrase; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 713 | Key *private; |
| 714 | Key *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 715 | struct stat st; |
| 716 | FILE *f; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 717 | int fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 718 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 719 | if (!have_identity) |
| 720 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 721 | if (stat(identity_file, &st) < 0) { |
| 722 | perror(identity_file); |
| 723 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 724 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 725 | private = key_load_private(identity_file, "", &comment); |
| 726 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 727 | if (identity_passphrase) |
| 728 | passphrase = xstrdup(identity_passphrase); |
| 729 | else if (identity_new_passphrase) |
| 730 | passphrase = xstrdup(identity_new_passphrase); |
| 731 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 732 | passphrase = read_passphrase("Enter passphrase: ", |
| 733 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 734 | /* Try to load using the passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 735 | private = key_load_private(identity_file, passphrase, &comment); |
| 736 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 737 | memset(passphrase, 0, strlen(passphrase)); |
| 738 | xfree(passphrase); |
| 739 | printf("Bad passphrase.\n"); |
| 740 | exit(1); |
| 741 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 742 | } else { |
| 743 | passphrase = xstrdup(""); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 744 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 745 | if (private->type != KEY_RSA1) { |
| 746 | fprintf(stderr, "Comments are only supported for RSA1 keys.\n"); |
| 747 | key_free(private); |
| 748 | exit(1); |
| 749 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 750 | printf("Key now has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 751 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 752 | if (identity_comment) { |
| 753 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); |
| 754 | } else { |
| 755 | printf("Enter new comment: "); |
| 756 | fflush(stdout); |
| 757 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { |
| 758 | memset(passphrase, 0, strlen(passphrase)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 759 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 760 | exit(1); |
| 761 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 762 | if (strchr(new_comment, '\n')) |
| 763 | *strchr(new_comment, '\n') = 0; |
| 764 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 765 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 766 | /* Save the file using the new passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 767 | if (!key_save_private(private, identity_file, passphrase, new_comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 768 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 769 | memset(passphrase, 0, strlen(passphrase)); |
| 770 | xfree(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 771 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 772 | xfree(comment); |
| 773 | exit(1); |
| 774 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 775 | memset(passphrase, 0, strlen(passphrase)); |
| 776 | xfree(passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 777 | public = key_from_private(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 778 | key_free(private); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 779 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 780 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 781 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 782 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 783 | printf("Could not save your public key in %s\n", identity_file); |
| 784 | exit(1); |
| 785 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 786 | f = fdopen(fd, "w"); |
| 787 | if (f == NULL) { |
| 788 | printf("fdopen %s failed", identity_file); |
| 789 | exit(1); |
| 790 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 791 | if (!key_write(public, f)) |
| 792 | fprintf(stderr, "write key failed"); |
| 793 | key_free(public); |
| 794 | fprintf(f, " %s\n", new_comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 795 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 796 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 797 | xfree(comment); |
| 798 | |
| 799 | printf("The comment in your key file has been changed.\n"); |
| 800 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 801 | } |
| 802 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 803 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 804 | usage(void) |
| 805 | { |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 806 | fprintf(stderr, "Usage: %s [options]\n", __progname); |
| 807 | fprintf(stderr, "Options:\n"); |
| 808 | fprintf(stderr, " -b bits Number of bits in the key to create.\n"); |
| 809 | fprintf(stderr, " -c Change comment in private and public key files.\n"); |
| 810 | fprintf(stderr, " -e Convert OpenSSH to IETF SECSH key file.\n"); |
| 811 | fprintf(stderr, " -f filename Filename of the key file.\n"); |
| 812 | fprintf(stderr, " -i Convert IETF SECSH to OpenSSH key file.\n"); |
| 813 | fprintf(stderr, " -l Show fingerprint of key file.\n"); |
| 814 | fprintf(stderr, " -p Change passphrase of private key file.\n"); |
| 815 | fprintf(stderr, " -q Quiet.\n"); |
| 816 | fprintf(stderr, " -y Read private key file and print public key.\n"); |
| 817 | fprintf(stderr, " -t type Specify type of key to create.\n"); |
| 818 | fprintf(stderr, " -B Show bubblebabble digest of key file.\n"); |
| 819 | fprintf(stderr, " -C comment Provide new comment.\n"); |
| 820 | fprintf(stderr, " -N phrase Provide new passphrase.\n"); |
| 821 | fprintf(stderr, " -P phrase Provide old passphrase.\n"); |
| 822 | #ifdef SMARTCARD |
| 823 | fprintf(stderr, " -D reader Download public key from smartcard.\n"); |
| 824 | fprintf(stderr, " -U reader Upload private key to smartcard.\n"); |
| 825 | #endif /* SMARTCARD */ |
| 826 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 827 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 828 | } |
| 829 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 830 | /* |
| 831 | * Main program for key management. |
| 832 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 833 | int |
| 834 | main(int ac, char **av) |
| 835 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 836 | char dotsshdir[16 * 1024], comment[1024], *passphrase1, *passphrase2; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 837 | char *reader_id = NULL; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 838 | Key *private, *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 839 | struct passwd *pw; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 840 | struct stat st; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 841 | int opt, type, fd, download = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 842 | FILE *f; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 843 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 844 | extern int optind; |
| 845 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 846 | |
Ben Lindstrom | 49a79c0 | 2000-11-17 03:47:20 +0000 | [diff] [blame] | 847 | __progname = get_progname(av[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 848 | init_rng(); |
Damien Miller | 60bc517 | 2001-03-19 09:38:15 +1100 | [diff] [blame] | 849 | seed_rng(); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 850 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 851 | SSLeay_add_all_algorithms(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 852 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 853 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 854 | pw = getpwuid(getuid()); |
| 855 | if (!pw) { |
| 856 | printf("You don't exist, go away!\n"); |
| 857 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 858 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 859 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 860 | perror("gethostname"); |
| 861 | exit(1); |
| 862 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 863 | |
Ben Lindstrom | f19578c | 2001-08-06 21:46:54 +0000 | [diff] [blame] | 864 | 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] | 865 | switch (opt) { |
| 866 | case 'b': |
| 867 | bits = atoi(optarg); |
| 868 | if (bits < 512 || bits > 32768) { |
| 869 | printf("Bits has bad value.\n"); |
| 870 | exit(1); |
| 871 | } |
| 872 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 873 | case 'l': |
| 874 | print_fingerprint = 1; |
| 875 | break; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 876 | case 'B': |
| 877 | print_bubblebabble = 1; |
| 878 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 879 | case 'p': |
| 880 | change_passphrase = 1; |
| 881 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 882 | case 'c': |
| 883 | change_comment = 1; |
| 884 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 885 | case 'f': |
| 886 | strlcpy(identity_file, optarg, sizeof(identity_file)); |
| 887 | have_identity = 1; |
| 888 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 889 | case 'P': |
| 890 | identity_passphrase = optarg; |
| 891 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 892 | case 'N': |
| 893 | identity_new_passphrase = optarg; |
| 894 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 895 | case 'C': |
| 896 | identity_comment = optarg; |
| 897 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 898 | case 'q': |
| 899 | quiet = 1; |
| 900 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 901 | case 'R': |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 902 | /* unused */ |
| 903 | exit(0); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 904 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 905 | case 'e': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 906 | case 'x': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 907 | /* export key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 908 | convert_to_ssh2 = 1; |
| 909 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 910 | case 'i': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 911 | case 'X': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 912 | /* import key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 913 | convert_from_ssh2 = 1; |
| 914 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 915 | case 'y': |
| 916 | print_public = 1; |
| 917 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 918 | case 'd': |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 919 | key_type_name = "dsa"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 920 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 921 | case 't': |
| 922 | key_type_name = optarg; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 923 | break; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 924 | case 'D': |
| 925 | download = 1; |
Ben Lindstrom | f19578c | 2001-08-06 21:46:54 +0000 | [diff] [blame] | 926 | case 'U': |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 927 | reader_id = optarg; |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 928 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 929 | case '?': |
| 930 | default: |
| 931 | usage(); |
| 932 | } |
| 933 | } |
| 934 | if (optind < ac) { |
| 935 | printf("Too many arguments.\n"); |
| 936 | usage(); |
| 937 | } |
| 938 | if (change_passphrase && change_comment) { |
| 939 | printf("Can only have one of -p and -c.\n"); |
| 940 | usage(); |
| 941 | } |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 942 | if (print_fingerprint || print_bubblebabble) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 943 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 944 | if (change_passphrase) |
| 945 | do_change_passphrase(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 946 | if (change_comment) |
| 947 | do_change_comment(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 948 | if (convert_to_ssh2) |
| 949 | do_convert_to_ssh2(pw); |
| 950 | if (convert_from_ssh2) |
| 951 | do_convert_from_ssh2(pw); |
| 952 | if (print_public) |
| 953 | do_print_public(pw); |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 954 | if (reader_id != NULL) { |
Ben Lindstrom | 6818bfb | 2001-08-06 21:40:04 +0000 | [diff] [blame] | 955 | #ifdef SMARTCARD |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 956 | if (download) |
| 957 | do_download(pw, reader_id); |
| 958 | else |
| 959 | do_upload(pw, reader_id); |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 960 | #else /* SMARTCARD */ |
Ben Lindstrom | 6818bfb | 2001-08-06 21:40:04 +0000 | [diff] [blame] | 961 | fatal("no support for smartcards."); |
Ben Lindstrom | ffce147 | 2001-08-06 21:57:31 +0000 | [diff] [blame] | 962 | #endif /* SMARTCARD */ |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 963 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 964 | |
| 965 | arc4random_stir(); |
| 966 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 967 | type = key_type_from_name(key_type_name); |
| 968 | if (type == KEY_UNSPEC) { |
| 969 | fprintf(stderr, "unknown key type %s\n", key_type_name); |
| 970 | exit(1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 971 | } |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 972 | if (!quiet) |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 973 | printf("Generating public/private %s key pair.\n", key_type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 974 | private = key_generate(type, bits); |
| 975 | if (private == NULL) { |
| 976 | fprintf(stderr, "key_generate failed"); |
| 977 | exit(1); |
| 978 | } |
| 979 | public = key_from_private(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 980 | |
| 981 | if (!have_identity) |
| 982 | ask_filename(pw, "Enter file in which to save the key"); |
| 983 | |
| 984 | /* Create ~/.ssh directory if it doesn\'t already exist. */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 985 | 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] | 986 | if (strstr(identity_file, dotsshdir) != NULL && |
| 987 | stat(dotsshdir, &st) < 0) { |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 988 | if (mkdir(dotsshdir, 0700) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 989 | error("Could not create directory '%s'.", dotsshdir); |
| 990 | else if (!quiet) |
| 991 | printf("Created directory '%s'.\n", dotsshdir); |
| 992 | } |
| 993 | /* If the file already exists, ask the user to confirm. */ |
| 994 | if (stat(identity_file, &st) >= 0) { |
| 995 | char yesno[3]; |
| 996 | printf("%s already exists.\n", identity_file); |
| 997 | printf("Overwrite (y/n)? "); |
| 998 | fflush(stdout); |
| 999 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 1000 | exit(1); |
| 1001 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 1002 | exit(1); |
| 1003 | } |
| 1004 | /* Ask for a passphrase (twice). */ |
| 1005 | if (identity_passphrase) |
| 1006 | passphrase1 = xstrdup(identity_passphrase); |
| 1007 | else if (identity_new_passphrase) |
| 1008 | passphrase1 = xstrdup(identity_new_passphrase); |
| 1009 | else { |
| 1010 | passphrase_again: |
| 1011 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1012 | read_passphrase("Enter passphrase (empty for no " |
| 1013 | "passphrase): ", RP_ALLOW_STDIN); |
| 1014 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
| 1015 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1016 | if (strcmp(passphrase1, passphrase2) != 0) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1017 | /* |
| 1018 | * The passphrases do not match. Clear them and |
| 1019 | * retry. |
| 1020 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1021 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1022 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1023 | xfree(passphrase1); |
| 1024 | xfree(passphrase2); |
| 1025 | printf("Passphrases do not match. Try again.\n"); |
| 1026 | goto passphrase_again; |
| 1027 | } |
| 1028 | /* Clear the other copy of the passphrase. */ |
| 1029 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1030 | xfree(passphrase2); |
| 1031 | } |
| 1032 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1033 | if (identity_comment) { |
| 1034 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 1035 | } else { |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 1036 | /* Create default commend field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1037 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 1038 | } |
| 1039 | |
| 1040 | /* Save the key with the given passphrase and comment. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1041 | if (!key_save_private(private, identity_file, passphrase1, comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 1042 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1043 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1044 | xfree(passphrase1); |
| 1045 | exit(1); |
| 1046 | } |
| 1047 | /* Clear the passphrase. */ |
| 1048 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1049 | xfree(passphrase1); |
| 1050 | |
| 1051 | /* Clear the private key and the random number generator. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1052 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1053 | arc4random_stir(); |
| 1054 | |
| 1055 | if (!quiet) |
| 1056 | printf("Your identification has been saved in %s.\n", identity_file); |
| 1057 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1058 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1059 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1060 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1061 | printf("Could not save your public key in %s\n", identity_file); |
| 1062 | exit(1); |
| 1063 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1064 | f = fdopen(fd, "w"); |
| 1065 | if (f == NULL) { |
| 1066 | printf("fdopen %s failed", identity_file); |
| 1067 | exit(1); |
| 1068 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1069 | if (!key_write(public, f)) |
| 1070 | fprintf(stderr, "write key failed"); |
| 1071 | fprintf(f, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1072 | fclose(f); |
| 1073 | |
| 1074 | if (!quiet) { |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1075 | char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1076 | printf("Your public key has been saved in %s.\n", |
| 1077 | identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1078 | printf("The key fingerprint is:\n"); |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1079 | printf("%s %s\n", fp, comment); |
| 1080 | xfree(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1081 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1082 | |
| 1083 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1084 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1085 | } |