Damien Miller | 1cfbfaf | 2010-03-22 05:58:24 +1100 | [diff] [blame] | 1 | /* $OpenBSD: ssh-keygen.c,v 1.185 2010/03/15 19:40:02 stevesk Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Identity and host key generation and maintenance. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 7 | * |
| 8 | * As far as I am concerned, the code I have written for this software |
| 9 | * can be used freely for any purpose. Any derived versions of this |
| 10 | * software must be clearly marked as such, and if the derived work is |
| 11 | * incompatible with the protocol description in the RFC file, it must be |
| 12 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 13 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 14 | |
| 15 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 16 | |
| 17 | #include <sys/types.h> |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 18 | #include <sys/socket.h> |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 19 | #include <sys/stat.h> |
Damien Miller | 8dbffe7 | 2006-08-05 11:02:17 +1000 | [diff] [blame] | 20 | #include <sys/param.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 21 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 22 | #include <openssl/evp.h> |
| 23 | #include <openssl/pem.h> |
Darren Tucker | bfaaf96 | 2008-02-28 19:13:52 +1100 | [diff] [blame] | 24 | #include "openbsd-compat/openssl-compat.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 25 | |
Darren Tucker | 3997249 | 2006-07-12 22:22:46 +1000 | [diff] [blame] | 26 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 27 | #include <fcntl.h> |
Damien Miller | b8fe89c | 2006-07-24 14:51:00 +1000 | [diff] [blame] | 28 | #include <netdb.h> |
Darren Tucker | 2ee50c5 | 2006-07-11 18:55:05 +1000 | [diff] [blame] | 29 | #ifdef HAVE_PATHS_H |
| 30 | # include <paths.h> |
| 31 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 32 | #include <pwd.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 33 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 34 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 35 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 36 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 37 | #include <unistd.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 38 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | #include "xmalloc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 40 | #include "key.h" |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 41 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | #include "authfile.h" |
| 43 | #include "uuencode.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 44 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 45 | #include "pathnames.h" |
| 46 | #include "log.h" |
Darren Tucker | e608ca2 | 2004-05-13 16:15:47 +1000 | [diff] [blame] | 47 | #include "misc.h" |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 48 | #include "match.h" |
| 49 | #include "hostfile.h" |
Damien Miller | 6999610 | 2006-07-10 20:53:31 +1000 | [diff] [blame] | 50 | #include "dns.h" |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 51 | #include "ssh2.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 52 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 53 | #ifdef ENABLE_PKCS11 |
| 54 | #include "ssh-pkcs11.h" |
Ben Lindstrom | bcc1808 | 2001-08-06 21:59:25 +0000 | [diff] [blame] | 55 | #endif |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 56 | |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 57 | /* Number of bits in the RSA/DSA key. This value can be set on the command line. */ |
| 58 | #define DEFAULT_BITS 2048 |
| 59 | #define DEFAULT_BITS_DSA 1024 |
| 60 | u_int32_t bits = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 61 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 62 | /* |
| 63 | * Flag indicating that we just want to change the passphrase. This can be |
| 64 | * set on the command line. |
| 65 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 66 | int change_passphrase = 0; |
| 67 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 68 | /* |
| 69 | * Flag indicating that we just want to change the comment. This can be set |
| 70 | * on the command line. |
| 71 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | int change_comment = 0; |
| 73 | |
| 74 | int quiet = 0; |
| 75 | |
Darren Tucker | 35c4553 | 2008-06-13 04:43:15 +1000 | [diff] [blame] | 76 | int log_level = SYSLOG_LEVEL_INFO; |
| 77 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 78 | /* Flag indicating that we want to hash a known_hosts file */ |
| 79 | int hash_hosts = 0; |
| 80 | /* Flag indicating that we want lookup a host in known_hosts file */ |
| 81 | int find_host = 0; |
| 82 | /* Flag indicating that we want to delete a host from a known_hosts file */ |
| 83 | int delete_host = 0; |
| 84 | |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 85 | /* Flag indicating that we want to show the contents of a certificate */ |
| 86 | int show_cert = 0; |
| 87 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 88 | /* Flag indicating that we just want to see the key fingerprint */ |
| 89 | int print_fingerprint = 0; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 90 | int print_bubblebabble = 0; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 91 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 92 | /* The identity file name, given on the command line or entered by the user. */ |
| 93 | char identity_file[1024]; |
| 94 | int have_identity = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 95 | |
| 96 | /* This is set to the passphrase if given on the command line. */ |
| 97 | char *identity_passphrase = NULL; |
| 98 | |
| 99 | /* This is set to the new passphrase if given on the command line. */ |
| 100 | char *identity_new_passphrase = NULL; |
| 101 | |
| 102 | /* This is set to the new comment if given on the command line. */ |
| 103 | char *identity_comment = NULL; |
| 104 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 105 | /* Path to CA key when certifying keys. */ |
| 106 | char *ca_key_path = NULL; |
| 107 | |
| 108 | /* Key type when certifying */ |
| 109 | u_int cert_key_type = SSH2_CERT_TYPE_USER; |
| 110 | |
| 111 | /* "key ID" of signed key */ |
| 112 | char *cert_key_id = NULL; |
| 113 | |
| 114 | /* Comma-separated list of principal names for certifying keys */ |
| 115 | char *cert_principals = NULL; |
| 116 | |
| 117 | /* Validity period for certificates */ |
| 118 | u_int64_t cert_valid_from = 0; |
| 119 | u_int64_t cert_valid_to = ~0ULL; |
| 120 | |
| 121 | /* Certificate constraints */ |
| 122 | #define CONSTRAINT_X_FWD (1) |
| 123 | #define CONSTRAINT_AGENT_FWD (1<<1) |
| 124 | #define CONSTRAINT_PORT_FWD (1<<2) |
| 125 | #define CONSTRAINT_PTY (1<<3) |
| 126 | #define CONSTRAINT_USER_RC (1<<4) |
| 127 | #define CONSTRAINT_DEFAULT (CONSTRAINT_X_FWD|CONSTRAINT_AGENT_FWD| \ |
| 128 | CONSTRAINT_PORT_FWD|CONSTRAINT_PTY| \ |
| 129 | CONSTRAINT_USER_RC) |
| 130 | u_int32_t constraint_flags = CONSTRAINT_DEFAULT; |
| 131 | char *constraint_command = NULL; |
| 132 | char *constraint_src_addr = NULL; |
| 133 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 134 | /* Dump public key file in format used by real and the original SSH 2 */ |
| 135 | int convert_to_ssh2 = 0; |
| 136 | int convert_from_ssh2 = 0; |
| 137 | int print_public = 0; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 138 | int print_generic = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 139 | |
Damien Miller | a41c8b1 | 2002-01-22 23:05:08 +1100 | [diff] [blame] | 140 | char *key_type_name = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 141 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 142 | /* argv0 */ |
| 143 | extern char *__progname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 144 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 145 | char hostname[MAXHOSTNAMELEN]; |
| 146 | |
Darren Tucker | 770fc01 | 2004-05-13 16:24:32 +1000 | [diff] [blame] | 147 | /* moduli.c */ |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 148 | int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *); |
Darren Tucker | 770fc01 | 2004-05-13 16:24:32 +1000 | [diff] [blame] | 149 | int prime_test(FILE *, FILE *, u_int32_t, u_int32_t); |
| 150 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 151 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 152 | ask_filename(struct passwd *pw, const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | char buf[1024]; |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 155 | char *name = NULL; |
| 156 | |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 157 | if (key_type_name == NULL) |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 158 | name = _PATH_SSH_CLIENT_ID_RSA; |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 159 | else { |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 160 | switch (key_type_from_name(key_type_name)) { |
| 161 | case KEY_RSA1: |
| 162 | name = _PATH_SSH_CLIENT_IDENTITY; |
| 163 | break; |
| 164 | case KEY_DSA: |
| 165 | name = _PATH_SSH_CLIENT_ID_DSA; |
| 166 | break; |
| 167 | case KEY_RSA: |
| 168 | name = _PATH_SSH_CLIENT_ID_RSA; |
| 169 | break; |
| 170 | default: |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 171 | fprintf(stderr, "bad key type\n"); |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 172 | exit(1); |
| 173 | break; |
| 174 | } |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 175 | } |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 176 | snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); |
Ben Lindstrom | 3deda8b | 2000-12-22 20:27:43 +0000 | [diff] [blame] | 177 | fprintf(stderr, "%s (%s): ", prompt, identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 178 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 179 | exit(1); |
Damien Miller | 14b017d | 2007-09-17 16:09:15 +1000 | [diff] [blame] | 180 | buf[strcspn(buf, "\n")] = '\0'; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | if (strcmp(buf, "") != 0) |
| 182 | strlcpy(identity_file, buf, sizeof(identity_file)); |
| 183 | have_identity = 1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 184 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 185 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 186 | static Key * |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 187 | load_identity(char *filename) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 188 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 189 | char *pass; |
| 190 | Key *prv; |
| 191 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 192 | prv = key_load_private(filename, "", NULL); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 193 | if (prv == NULL) { |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 194 | if (identity_passphrase) |
| 195 | pass = xstrdup(identity_passphrase); |
| 196 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 197 | pass = read_passphrase("Enter passphrase: ", |
| 198 | RP_ALLOW_STDIN); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 199 | prv = key_load_private(filename, pass, NULL); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 200 | memset(pass, 0, strlen(pass)); |
| 201 | xfree(pass); |
| 202 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 203 | return prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 204 | } |
| 205 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 206 | #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 207 | #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 208 | #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----" |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 209 | #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 210 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 211 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 212 | do_convert_to_ssh2(struct passwd *pw) |
| 213 | { |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 214 | Key *k; |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 215 | u_int len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 216 | u_char *blob; |
Darren Tucker | d04758d | 2010-01-12 19:41:57 +1100 | [diff] [blame] | 217 | char comment[61]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 218 | struct stat st; |
| 219 | |
| 220 | if (!have_identity) |
| 221 | ask_filename(pw, "Enter file in which the key is"); |
| 222 | if (stat(identity_file, &st) < 0) { |
| 223 | perror(identity_file); |
| 224 | exit(1); |
| 225 | } |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 226 | if ((k = key_load_public(identity_file, NULL)) == NULL) { |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 227 | if ((k = load_identity(identity_file)) == NULL) { |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 228 | fprintf(stderr, "load failed\n"); |
| 229 | exit(1); |
| 230 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 231 | } |
Damien Miller | db27472 | 2003-05-14 13:45:22 +1000 | [diff] [blame] | 232 | if (k->type == KEY_RSA1) { |
| 233 | fprintf(stderr, "version 1 keys are not supported\n"); |
| 234 | exit(1); |
| 235 | } |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 236 | if (key_to_blob(k, &blob, &len) <= 0) { |
| 237 | fprintf(stderr, "key_to_blob failed\n"); |
| 238 | exit(1); |
| 239 | } |
Darren Tucker | d04758d | 2010-01-12 19:41:57 +1100 | [diff] [blame] | 240 | /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */ |
| 241 | snprintf(comment, sizeof(comment), |
| 242 | "%u-bit %s, converted by %s@%s from OpenSSH", |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 243 | key_size(k), key_type(k), |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 244 | pw->pw_name, hostname); |
Darren Tucker | d04758d | 2010-01-12 19:41:57 +1100 | [diff] [blame] | 245 | |
| 246 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); |
| 247 | fprintf(stdout, "Comment: \"%s\"\n", comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 248 | dump_base64(stdout, blob, len); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 249 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 250 | key_free(k); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 251 | xfree(blob); |
| 252 | exit(0); |
| 253 | } |
| 254 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 255 | static void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 256 | buffer_get_bignum_bits(Buffer *b, BIGNUM *value) |
| 257 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 258 | u_int bignum_bits = buffer_get_int(b); |
| 259 | u_int bytes = (bignum_bits + 7) / 8; |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 260 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 261 | if (buffer_len(b) < bytes) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 262 | fatal("buffer_get_bignum_bits: input buffer too small: " |
| 263 | "need %d have %d", bytes, buffer_len(b)); |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 264 | if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL) |
| 265 | fatal("buffer_get_bignum_bits: BN_bin2bn failed"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 266 | buffer_consume(b, bytes); |
| 267 | } |
| 268 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 269 | static Key * |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 270 | do_convert_private_ssh2_from_blob(u_char *blob, u_int blen) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 271 | { |
| 272 | Buffer b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 273 | Key *key = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 274 | char *type, *cipher; |
Ben Lindstrom | 511d69e | 2001-07-04 05:05:27 +0000 | [diff] [blame] | 275 | u_char *sig, data[] = "abcde12345"; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 276 | int magic, rlen, ktype, i1, i2, i3, i4; |
| 277 | u_int slen; |
| 278 | u_long e; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 279 | |
| 280 | buffer_init(&b); |
| 281 | buffer_append(&b, blob, blen); |
| 282 | |
Darren Tucker | 82a3d2b | 2007-02-19 22:10:25 +1100 | [diff] [blame] | 283 | magic = buffer_get_int(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 284 | if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { |
| 285 | error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC); |
| 286 | buffer_free(&b); |
| 287 | return NULL; |
| 288 | } |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 289 | i1 = buffer_get_int(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 290 | type = buffer_get_string(&b, NULL); |
| 291 | cipher = buffer_get_string(&b, NULL); |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 292 | i2 = buffer_get_int(&b); |
| 293 | i3 = buffer_get_int(&b); |
| 294 | i4 = buffer_get_int(&b); |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 295 | debug("ignore (%d %d %d %d)", i1, i2, i3, i4); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 296 | if (strcmp(cipher, "none") != 0) { |
| 297 | error("unsupported cipher %s", cipher); |
| 298 | xfree(cipher); |
| 299 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 300 | xfree(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 301 | return NULL; |
| 302 | } |
| 303 | xfree(cipher); |
| 304 | |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 305 | if (strstr(type, "dsa")) { |
| 306 | ktype = KEY_DSA; |
| 307 | } else if (strstr(type, "rsa")) { |
| 308 | ktype = KEY_RSA; |
| 309 | } else { |
Darren Tucker | 7cfeecf | 2005-01-20 10:56:31 +1100 | [diff] [blame] | 310 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 311 | xfree(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 312 | return NULL; |
| 313 | } |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 314 | key = key_new_private(ktype); |
| 315 | xfree(type); |
| 316 | |
| 317 | switch (key->type) { |
| 318 | case KEY_DSA: |
| 319 | buffer_get_bignum_bits(&b, key->dsa->p); |
| 320 | buffer_get_bignum_bits(&b, key->dsa->g); |
| 321 | buffer_get_bignum_bits(&b, key->dsa->q); |
| 322 | buffer_get_bignum_bits(&b, key->dsa->pub_key); |
| 323 | buffer_get_bignum_bits(&b, key->dsa->priv_key); |
| 324 | break; |
| 325 | case KEY_RSA: |
Darren Tucker | 82a3d2b | 2007-02-19 22:10:25 +1100 | [diff] [blame] | 326 | e = buffer_get_char(&b); |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 327 | debug("e %lx", e); |
| 328 | if (e < 30) { |
| 329 | e <<= 8; |
| 330 | e += buffer_get_char(&b); |
| 331 | debug("e %lx", e); |
| 332 | e <<= 8; |
| 333 | e += buffer_get_char(&b); |
| 334 | debug("e %lx", e); |
| 335 | } |
| 336 | if (!BN_set_word(key->rsa->e, e)) { |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 337 | buffer_free(&b); |
| 338 | key_free(key); |
| 339 | return NULL; |
| 340 | } |
| 341 | buffer_get_bignum_bits(&b, key->rsa->d); |
| 342 | buffer_get_bignum_bits(&b, key->rsa->n); |
| 343 | buffer_get_bignum_bits(&b, key->rsa->iqmp); |
| 344 | buffer_get_bignum_bits(&b, key->rsa->q); |
| 345 | buffer_get_bignum_bits(&b, key->rsa->p); |
Ben Lindstrom | f7297dd | 2001-07-04 05:02:23 +0000 | [diff] [blame] | 346 | rsa_generate_additional_parameters(key->rsa); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 347 | break; |
| 348 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 349 | rlen = buffer_len(&b); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 350 | if (rlen != 0) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 351 | error("do_convert_private_ssh2_from_blob: " |
| 352 | "remaining bytes in key blob %d", rlen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 353 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 354 | |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 355 | /* try the key */ |
| 356 | key_sign(key, &sig, &slen, data, sizeof(data)); |
| 357 | key_verify(key, sig, slen, data, sizeof(data)); |
| 358 | xfree(sig); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 359 | return key; |
| 360 | } |
| 361 | |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 362 | static int |
| 363 | get_line(FILE *fp, char *line, size_t len) |
| 364 | { |
| 365 | int c; |
| 366 | size_t pos = 0; |
| 367 | |
| 368 | line[0] = '\0'; |
| 369 | while ((c = fgetc(fp)) != EOF) { |
| 370 | if (pos >= len - 1) { |
| 371 | fprintf(stderr, "input line too long.\n"); |
| 372 | exit(1); |
| 373 | } |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 374 | switch (c) { |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 375 | case '\r': |
| 376 | c = fgetc(fp); |
| 377 | if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) { |
| 378 | fprintf(stderr, "unget: %s\n", strerror(errno)); |
| 379 | exit(1); |
| 380 | } |
| 381 | return pos; |
| 382 | case '\n': |
| 383 | return pos; |
| 384 | } |
| 385 | line[pos++] = c; |
| 386 | line[pos] = '\0'; |
| 387 | } |
Damien Miller | 6c7439f | 2007-01-05 16:29:55 +1100 | [diff] [blame] | 388 | /* We reached EOF */ |
| 389 | return -1; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 390 | } |
| 391 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 392 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 393 | do_convert_from_ssh2(struct passwd *pw) |
| 394 | { |
| 395 | Key *k; |
| 396 | int blen; |
Ben Lindstrom | 155b981 | 2002-04-02 20:26:26 +0000 | [diff] [blame] | 397 | u_int len; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 398 | char line[1024]; |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 399 | u_char blob[8096]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 400 | char encoded[8096]; |
| 401 | struct stat st; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 402 | int escaped = 0, private = 0, ok; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 403 | FILE *fp; |
| 404 | |
| 405 | if (!have_identity) |
| 406 | ask_filename(pw, "Enter file in which the key is"); |
| 407 | if (stat(identity_file, &st) < 0) { |
| 408 | perror(identity_file); |
| 409 | exit(1); |
| 410 | } |
| 411 | fp = fopen(identity_file, "r"); |
| 412 | if (fp == NULL) { |
| 413 | perror(identity_file); |
| 414 | exit(1); |
| 415 | } |
| 416 | encoded[0] = '\0'; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 417 | while ((blen = get_line(fp, line, sizeof(line))) != -1) { |
| 418 | if (line[blen - 1] == '\\') |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 419 | escaped++; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 420 | if (strncmp(line, "----", 4) == 0 || |
| 421 | strstr(line, ": ") != NULL) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 422 | if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL) |
| 423 | private = 1; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 424 | if (strstr(line, " END ") != NULL) { |
| 425 | break; |
| 426 | } |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 427 | /* fprintf(stderr, "ignore: %s", line); */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 428 | continue; |
| 429 | } |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 430 | if (escaped) { |
| 431 | escaped--; |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 432 | /* fprintf(stderr, "escaped: %s", line); */ |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 433 | continue; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 434 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 435 | strlcat(encoded, line, sizeof(encoded)); |
| 436 | } |
Ben Lindstrom | 155b981 | 2002-04-02 20:26:26 +0000 | [diff] [blame] | 437 | len = strlen(encoded); |
| 438 | if (((len % 4) == 3) && |
| 439 | (encoded[len-1] == '=') && |
| 440 | (encoded[len-2] == '=') && |
| 441 | (encoded[len-3] == '=')) |
| 442 | encoded[len-3] = '\0'; |
Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 443 | blen = uudecode(encoded, blob, sizeof(blob)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 444 | if (blen < 0) { |
| 445 | fprintf(stderr, "uudecode failed.\n"); |
| 446 | exit(1); |
| 447 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 448 | k = private ? |
| 449 | do_convert_private_ssh2_from_blob(blob, blen) : |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 450 | key_from_blob(blob, blen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 451 | if (k == NULL) { |
| 452 | fprintf(stderr, "decode blob failed.\n"); |
| 453 | exit(1); |
| 454 | } |
| 455 | ok = private ? |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 456 | (k->type == KEY_DSA ? |
| 457 | PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) : |
| 458 | PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) : |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 459 | key_write(k, stdout); |
| 460 | if (!ok) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 461 | fprintf(stderr, "key write failed\n"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 462 | exit(1); |
| 463 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 464 | key_free(k); |
Damien Miller | a1db12b | 2002-01-22 23:20:15 +1100 | [diff] [blame] | 465 | if (!private) |
| 466 | fprintf(stdout, "\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 467 | fclose(fp); |
| 468 | exit(0); |
| 469 | } |
| 470 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 471 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 472 | do_print_public(struct passwd *pw) |
| 473 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 474 | Key *prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 475 | struct stat st; |
| 476 | |
| 477 | if (!have_identity) |
| 478 | ask_filename(pw, "Enter file in which the key is"); |
| 479 | if (stat(identity_file, &st) < 0) { |
| 480 | perror(identity_file); |
| 481 | exit(1); |
| 482 | } |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 483 | prv = load_identity(identity_file); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 484 | if (prv == NULL) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 485 | fprintf(stderr, "load failed\n"); |
| 486 | exit(1); |
| 487 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 488 | if (!key_write(prv, stdout)) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 489 | fprintf(stderr, "key_write failed"); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 490 | key_free(prv); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 491 | fprintf(stdout, "\n"); |
| 492 | exit(0); |
| 493 | } |
| 494 | |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 495 | static void |
Damien Miller | 86cbb44 | 2010-02-12 09:22:57 +1100 | [diff] [blame] | 496 | do_download(struct passwd *pw, char *pkcs11provider) |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 497 | { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 498 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 499 | Key **keys = NULL; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 500 | int i, nkeys; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 501 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 502 | pkcs11_init(0); |
| 503 | nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys); |
| 504 | if (nkeys <= 0) |
| 505 | fatal("cannot read public key from pkcs11"); |
| 506 | for (i = 0; i < nkeys; i++) { |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 507 | key_write(keys[i], stdout); |
| 508 | key_free(keys[i]); |
| 509 | fprintf(stdout, "\n"); |
| 510 | } |
| 511 | xfree(keys); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 512 | pkcs11_terminate(); |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 513 | exit(0); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 514 | #else |
| 515 | fatal("no pkcs11 support"); |
| 516 | #endif /* ENABLE_PKCS11 */ |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 517 | } |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 518 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 519 | static void |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 520 | do_fingerprint(struct passwd *pw) |
| 521 | { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 522 | FILE *f; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 523 | Key *public; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 524 | char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra; |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 525 | int i, skip = 0, num = 0, invalid = 1; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 526 | enum fp_rep rep; |
| 527 | enum fp_type fptype; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 528 | struct stat st; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 529 | |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 530 | fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5; |
| 531 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 532 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 533 | if (!have_identity) |
| 534 | ask_filename(pw, "Enter file in which the key is"); |
| 535 | if (stat(identity_file, &st) < 0) { |
| 536 | perror(identity_file); |
| 537 | exit(1); |
| 538 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 539 | public = key_load_public(identity_file, &comment); |
| 540 | if (public != NULL) { |
| 541 | fp = key_fingerprint(public, fptype, rep); |
Darren Tucker | 9bcd25b | 2009-10-07 08:45:48 +1100 | [diff] [blame] | 542 | ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART); |
Darren Tucker | b68fb4a | 2008-06-13 08:57:27 +1000 | [diff] [blame] | 543 | printf("%u %s %s (%s)\n", key_size(public), fp, comment, |
| 544 | key_type(public)); |
Darren Tucker | 35c4553 | 2008-06-13 04:43:15 +1000 | [diff] [blame] | 545 | if (log_level >= SYSLOG_LEVEL_VERBOSE) |
| 546 | printf("%s\n", ra); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 547 | key_free(public); |
| 548 | xfree(comment); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 549 | xfree(ra); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 550 | xfree(fp); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 551 | exit(0); |
| 552 | } |
Damien Miller | 40b5985 | 2006-06-13 13:00:25 +1000 | [diff] [blame] | 553 | if (comment) { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 554 | xfree(comment); |
Damien Miller | 40b5985 | 2006-06-13 13:00:25 +1000 | [diff] [blame] | 555 | comment = NULL; |
| 556 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 557 | |
| 558 | f = fopen(identity_file, "r"); |
| 559 | if (f != NULL) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 560 | while (fgets(line, sizeof(line), f)) { |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 561 | if ((cp = strchr(line, '\n')) == NULL) { |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 562 | error("line %d too long: %.40s...", |
| 563 | num + 1, line); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 564 | skip = 1; |
| 565 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 566 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 567 | num++; |
| 568 | if (skip) { |
| 569 | skip = 0; |
| 570 | continue; |
| 571 | } |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 572 | *cp = '\0'; |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 573 | |
| 574 | /* Skip leading whitespace, empty and comment lines. */ |
| 575 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 576 | ; |
| 577 | if (!*cp || *cp == '\n' || *cp == '#') |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 578 | continue; |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 579 | i = strtol(cp, &ep, 10); |
| 580 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { |
| 581 | int quoted = 0; |
| 582 | comment = cp; |
Ben Lindstrom | 58d3b72 | 2002-06-23 21:28:13 +0000 | [diff] [blame] | 583 | for (; *cp && (quoted || (*cp != ' ' && |
| 584 | *cp != '\t')); cp++) { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 585 | if (*cp == '\\' && cp[1] == '"') |
| 586 | cp++; /* Skip both */ |
| 587 | else if (*cp == '"') |
| 588 | quoted = !quoted; |
| 589 | } |
| 590 | if (!*cp) |
| 591 | continue; |
| 592 | *cp++ = '\0'; |
| 593 | } |
| 594 | ep = cp; |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 595 | public = key_new(KEY_RSA1); |
| 596 | if (key_read(public, &cp) != 1) { |
| 597 | cp = ep; |
| 598 | key_free(public); |
| 599 | public = key_new(KEY_UNSPEC); |
| 600 | if (key_read(public, &cp) != 1) { |
| 601 | key_free(public); |
| 602 | continue; |
| 603 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 604 | } |
Ben Lindstrom | 2941f11 | 2000-12-29 16:50:13 +0000 | [diff] [blame] | 605 | comment = *cp ? cp : comment; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 606 | fp = key_fingerprint(public, fptype, rep); |
Darren Tucker | 9bcd25b | 2009-10-07 08:45:48 +1100 | [diff] [blame] | 607 | ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART); |
Darren Tucker | b68fb4a | 2008-06-13 08:57:27 +1000 | [diff] [blame] | 608 | printf("%u %s %s (%s)\n", key_size(public), fp, |
| 609 | comment ? comment : "no comment", key_type(public)); |
Darren Tucker | 35c4553 | 2008-06-13 04:43:15 +1000 | [diff] [blame] | 610 | if (log_level >= SYSLOG_LEVEL_VERBOSE) |
| 611 | printf("%s\n", ra); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 612 | xfree(ra); |
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 | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 626 | static void |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 627 | printhost(FILE *f, const char *name, Key *public, int ca, int hash) |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 628 | { |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 629 | if (print_fingerprint) { |
| 630 | enum fp_rep rep; |
| 631 | enum fp_type fptype; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 632 | char *fp, *ra; |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 633 | |
| 634 | fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5; |
| 635 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX; |
| 636 | fp = key_fingerprint(public, fptype, rep); |
Darren Tucker | 9bcd25b | 2009-10-07 08:45:48 +1100 | [diff] [blame] | 637 | ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART); |
Damien Miller | 81dec05 | 2008-07-14 11:28:29 +1000 | [diff] [blame] | 638 | printf("%u %s %s (%s)\n", key_size(public), fp, name, |
| 639 | key_type(public)); |
| 640 | if (log_level >= SYSLOG_LEVEL_VERBOSE) |
| 641 | printf("%s\n", ra); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 642 | xfree(ra); |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 643 | xfree(fp); |
| 644 | } else { |
| 645 | if (hash && (name = host_hash(name, NULL, 0)) == NULL) |
| 646 | fatal("hash_host failed"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 647 | fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name); |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 648 | if (!key_write(public, f)) |
| 649 | fatal("key_write failed"); |
| 650 | fprintf(f, "\n"); |
| 651 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | static void |
| 655 | do_known_hosts(struct passwd *pw, const char *name) |
| 656 | { |
| 657 | FILE *in, *out = stdout; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 658 | Key *pub; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 659 | char *cp, *cp2, *kp, *kp2; |
| 660 | char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN]; |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 661 | int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 662 | int ca; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 663 | |
| 664 | if (!have_identity) { |
| 665 | cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid); |
| 666 | if (strlcpy(identity_file, cp, sizeof(identity_file)) >= |
| 667 | sizeof(identity_file)) |
| 668 | fatal("Specified known hosts path too long"); |
| 669 | xfree(cp); |
| 670 | have_identity = 1; |
| 671 | } |
| 672 | if ((in = fopen(identity_file, "r")) == NULL) |
| 673 | fatal("fopen: %s", strerror(errno)); |
| 674 | |
| 675 | /* |
| 676 | * Find hosts goes to stdout, hash and deletions happen in-place |
| 677 | * A corner case is ssh-keygen -HF foo, which should go to stdout |
| 678 | */ |
| 679 | if (!find_host && (hash_hosts || delete_host)) { |
| 680 | if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) || |
| 681 | strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) || |
| 682 | strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) || |
| 683 | strlcat(old, ".old", sizeof(old)) >= sizeof(old)) |
| 684 | fatal("known_hosts path too long"); |
| 685 | umask(077); |
| 686 | if ((c = mkstemp(tmp)) == -1) |
| 687 | fatal("mkstemp: %s", strerror(errno)); |
| 688 | if ((out = fdopen(c, "w")) == NULL) { |
| 689 | c = errno; |
| 690 | unlink(tmp); |
| 691 | fatal("fdopen: %s", strerror(c)); |
| 692 | } |
| 693 | inplace = 1; |
| 694 | } |
| 695 | |
| 696 | while (fgets(line, sizeof(line), in)) { |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 697 | if ((cp = strchr(line, '\n')) == NULL) { |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 698 | error("line %d too long: %.40s...", num + 1, line); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 699 | skip = 1; |
| 700 | invalid = 1; |
| 701 | continue; |
| 702 | } |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 703 | num++; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 704 | if (skip) { |
| 705 | skip = 0; |
| 706 | continue; |
| 707 | } |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 708 | *cp = '\0'; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 709 | |
| 710 | /* Skip leading whitespace, empty and comment lines. */ |
| 711 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 712 | ; |
| 713 | if (!*cp || *cp == '\n' || *cp == '#') { |
| 714 | if (inplace) |
| 715 | fprintf(out, "%s\n", cp); |
| 716 | continue; |
| 717 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 718 | /* Check whether this is a CA key */ |
| 719 | if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 && |
| 720 | (cp[sizeof(CA_MARKER) - 1] == ' ' || |
| 721 | cp[sizeof(CA_MARKER) - 1] == '\t')) { |
| 722 | ca = 1; |
| 723 | cp += sizeof(CA_MARKER); |
| 724 | } else |
| 725 | ca = 0; |
| 726 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 727 | /* Find the end of the host name portion. */ |
| 728 | for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++) |
| 729 | ; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 730 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 731 | if (*kp == '\0' || *(kp + 1) == '\0') { |
| 732 | error("line %d missing key: %.40s...", |
| 733 | num, line); |
| 734 | invalid = 1; |
| 735 | continue; |
| 736 | } |
| 737 | *kp++ = '\0'; |
| 738 | kp2 = kp; |
| 739 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 740 | pub = key_new(KEY_RSA1); |
| 741 | if (key_read(pub, &kp) != 1) { |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 742 | kp = kp2; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 743 | key_free(pub); |
| 744 | pub = key_new(KEY_UNSPEC); |
| 745 | if (key_read(pub, &kp) != 1) { |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 746 | error("line %d invalid key: %.40s...", |
| 747 | num, line); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 748 | key_free(pub); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 749 | invalid = 1; |
| 750 | continue; |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | if (*cp == HASH_DELIM) { |
| 755 | if (find_host || delete_host) { |
| 756 | cp2 = host_hash(name, cp, strlen(cp)); |
| 757 | if (cp2 == NULL) { |
| 758 | error("line %d: invalid hashed " |
| 759 | "name: %.64s...", num, line); |
| 760 | invalid = 1; |
| 761 | continue; |
| 762 | } |
| 763 | c = (strcmp(cp2, cp) == 0); |
| 764 | if (find_host && c) { |
| 765 | printf("# Host %s found: " |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 766 | "line %d type %s%s\n", name, |
| 767 | num, key_type(pub), |
| 768 | ca ? " (CA key)" : ""); |
| 769 | printhost(out, cp, pub, ca, 0); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 770 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 771 | if (delete_host && !c && !ca) |
| 772 | printhost(out, cp, pub, ca, 0); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 773 | } else if (hash_hosts) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 774 | printhost(out, cp, pub, ca, 0); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 775 | } else { |
| 776 | if (find_host || delete_host) { |
| 777 | c = (match_hostname(name, cp, |
| 778 | strlen(cp)) == 1); |
| 779 | if (find_host && c) { |
| 780 | printf("# Host %s found: " |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 781 | "line %d type %s%s\n", name, |
| 782 | num, key_type(pub), |
| 783 | ca ? " (CA key)" : ""); |
| 784 | printhost(out, name, pub, |
| 785 | ca, hash_hosts && !ca); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 786 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 787 | if (delete_host && !c && !ca) |
| 788 | printhost(out, cp, pub, ca, 0); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 789 | } else if (hash_hosts) { |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 790 | for (cp2 = strsep(&cp, ","); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 791 | cp2 != NULL && *cp2 != '\0'; |
Damien Miller | 89eac80 | 2005-03-02 12:33:04 +1100 | [diff] [blame] | 792 | cp2 = strsep(&cp, ",")) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 793 | if (ca) { |
| 794 | fprintf(stderr, "Warning: " |
| 795 | "ignoring CA key for host: " |
| 796 | "%.64s\n", cp2); |
| 797 | printhost(out, cp2, pub, ca, 0); |
| 798 | } else if (strcspn(cp2, "*?!") != |
| 799 | strlen(cp2)) { |
Damien Miller | 89eac80 | 2005-03-02 12:33:04 +1100 | [diff] [blame] | 800 | fprintf(stderr, "Warning: " |
| 801 | "ignoring host name with " |
| 802 | "metacharacters: %.64s\n", |
| 803 | cp2); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 804 | printhost(out, cp2, pub, ca, 0); |
| 805 | } else |
| 806 | printhost(out, cp2, pub, ca, 1); |
Damien Miller | 89eac80 | 2005-03-02 12:33:04 +1100 | [diff] [blame] | 807 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 808 | has_unhashed = 1; |
| 809 | } |
| 810 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 811 | key_free(pub); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 812 | } |
| 813 | fclose(in); |
| 814 | |
| 815 | if (invalid) { |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 816 | fprintf(stderr, "%s is not a valid known_hosts file.\n", |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 817 | identity_file); |
| 818 | if (inplace) { |
| 819 | fprintf(stderr, "Not replacing existing known_hosts " |
Darren Tucker | 9f438a9 | 2005-03-14 23:09:18 +1100 | [diff] [blame] | 820 | "file because of errors\n"); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 821 | fclose(out); |
| 822 | unlink(tmp); |
| 823 | } |
| 824 | exit(1); |
| 825 | } |
| 826 | |
| 827 | if (inplace) { |
| 828 | fclose(out); |
| 829 | |
| 830 | /* Backup existing file */ |
| 831 | if (unlink(old) == -1 && errno != ENOENT) |
| 832 | fatal("unlink %.100s: %s", old, strerror(errno)); |
| 833 | if (link(identity_file, old) == -1) |
| 834 | fatal("link %.100s to %.100s: %s", identity_file, old, |
| 835 | strerror(errno)); |
| 836 | /* Move new one into place */ |
| 837 | if (rename(tmp, identity_file) == -1) { |
| 838 | error("rename\"%s\" to \"%s\": %s", tmp, identity_file, |
| 839 | strerror(errno)); |
| 840 | unlink(tmp); |
| 841 | unlink(old); |
| 842 | exit(1); |
| 843 | } |
| 844 | |
| 845 | fprintf(stderr, "%s updated.\n", identity_file); |
| 846 | fprintf(stderr, "Original contents retained as %s\n", old); |
| 847 | if (has_unhashed) { |
| 848 | fprintf(stderr, "WARNING: %s contains unhashed " |
| 849 | "entries\n", old); |
| 850 | fprintf(stderr, "Delete this file to ensure privacy " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 851 | "of hostnames\n"); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 852 | } |
| 853 | } |
| 854 | |
| 855 | exit(0); |
| 856 | } |
| 857 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 858 | /* |
| 859 | * Perform changing a passphrase. The argument is the passwd structure |
| 860 | * for the current user. |
| 861 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 862 | static void |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 863 | do_change_passphrase(struct passwd *pw) |
| 864 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 865 | char *comment; |
| 866 | char *old_passphrase, *passphrase1, *passphrase2; |
| 867 | struct stat st; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 868 | Key *private; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 869 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 870 | if (!have_identity) |
| 871 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 872 | if (stat(identity_file, &st) < 0) { |
| 873 | perror(identity_file); |
| 874 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 875 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 876 | /* Try to load the file with empty passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 877 | private = key_load_private(identity_file, "", &comment); |
| 878 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 879 | if (identity_passphrase) |
| 880 | old_passphrase = xstrdup(identity_passphrase); |
| 881 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 882 | old_passphrase = |
| 883 | read_passphrase("Enter old passphrase: ", |
| 884 | RP_ALLOW_STDIN); |
| 885 | private = key_load_private(identity_file, old_passphrase, |
| 886 | &comment); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 887 | memset(old_passphrase, 0, strlen(old_passphrase)); |
| 888 | xfree(old_passphrase); |
| 889 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 890 | printf("Bad passphrase.\n"); |
| 891 | exit(1); |
| 892 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 893 | } |
| 894 | printf("Key has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 895 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 896 | /* Ask the new passphrase (twice). */ |
| 897 | if (identity_new_passphrase) { |
| 898 | passphrase1 = xstrdup(identity_new_passphrase); |
| 899 | passphrase2 = NULL; |
| 900 | } else { |
| 901 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 902 | read_passphrase("Enter new passphrase (empty for no " |
| 903 | "passphrase): ", RP_ALLOW_STDIN); |
| 904 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 905 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 906 | |
| 907 | /* Verify that they are the same. */ |
| 908 | if (strcmp(passphrase1, passphrase2) != 0) { |
| 909 | memset(passphrase1, 0, strlen(passphrase1)); |
| 910 | memset(passphrase2, 0, strlen(passphrase2)); |
| 911 | xfree(passphrase1); |
| 912 | xfree(passphrase2); |
| 913 | printf("Pass phrases do not match. Try again.\n"); |
| 914 | exit(1); |
| 915 | } |
| 916 | /* Destroy the other copy. */ |
| 917 | memset(passphrase2, 0, strlen(passphrase2)); |
| 918 | xfree(passphrase2); |
| 919 | } |
| 920 | |
| 921 | /* Save the file using the new passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 922 | if (!key_save_private(private, identity_file, passphrase1, comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 923 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 924 | memset(passphrase1, 0, strlen(passphrase1)); |
| 925 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 926 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 927 | xfree(comment); |
| 928 | exit(1); |
| 929 | } |
| 930 | /* Destroy the passphrase and the copy of the key in memory. */ |
| 931 | memset(passphrase1, 0, strlen(passphrase1)); |
| 932 | xfree(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 933 | key_free(private); /* Destroys contents */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 934 | xfree(comment); |
| 935 | |
| 936 | printf("Your identification has been saved with the new passphrase.\n"); |
| 937 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 938 | } |
| 939 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 940 | /* |
| 941 | * Print the SSHFP RR. |
| 942 | */ |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 943 | static int |
| 944 | do_print_resource_record(struct passwd *pw, char *fname, char *hname) |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 945 | { |
| 946 | Key *public; |
| 947 | char *comment = NULL; |
| 948 | struct stat st; |
| 949 | |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 950 | if (fname == NULL) |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 951 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 952 | if (stat(fname, &st) < 0) { |
| 953 | if (errno == ENOENT) |
| 954 | return 0; |
| 955 | perror(fname); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 956 | exit(1); |
| 957 | } |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 958 | public = key_load_public(fname, &comment); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 959 | if (public != NULL) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 960 | export_dns_rr(hname, public, stdout, print_generic); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 961 | key_free(public); |
| 962 | xfree(comment); |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 963 | return 1; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 964 | } |
| 965 | if (comment) |
| 966 | xfree(comment); |
| 967 | |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 968 | printf("failed to read v2 public key from %s.\n", fname); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 969 | exit(1); |
| 970 | } |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 971 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 972 | /* |
| 973 | * Change the comment of a private key file. |
| 974 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 975 | static void |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 976 | do_change_comment(struct passwd *pw) |
| 977 | { |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 978 | char new_comment[1024], *comment, *passphrase; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 979 | Key *private; |
| 980 | Key *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 981 | struct stat st; |
| 982 | FILE *f; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 983 | int fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 984 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 985 | if (!have_identity) |
| 986 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 987 | if (stat(identity_file, &st) < 0) { |
| 988 | perror(identity_file); |
| 989 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 990 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 991 | private = key_load_private(identity_file, "", &comment); |
| 992 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 993 | if (identity_passphrase) |
| 994 | passphrase = xstrdup(identity_passphrase); |
| 995 | else if (identity_new_passphrase) |
| 996 | passphrase = xstrdup(identity_new_passphrase); |
| 997 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 998 | passphrase = read_passphrase("Enter passphrase: ", |
| 999 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1000 | /* Try to load using the passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1001 | private = key_load_private(identity_file, passphrase, &comment); |
| 1002 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1003 | memset(passphrase, 0, strlen(passphrase)); |
| 1004 | xfree(passphrase); |
| 1005 | printf("Bad passphrase.\n"); |
| 1006 | exit(1); |
| 1007 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1008 | } else { |
| 1009 | passphrase = xstrdup(""); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1010 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1011 | if (private->type != KEY_RSA1) { |
| 1012 | fprintf(stderr, "Comments are only supported for RSA1 keys.\n"); |
| 1013 | key_free(private); |
| 1014 | exit(1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1015 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1016 | printf("Key now has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1017 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1018 | if (identity_comment) { |
| 1019 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); |
| 1020 | } else { |
| 1021 | printf("Enter new comment: "); |
| 1022 | fflush(stdout); |
| 1023 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { |
| 1024 | memset(passphrase, 0, strlen(passphrase)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1025 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1026 | exit(1); |
| 1027 | } |
Damien Miller | 14b017d | 2007-09-17 16:09:15 +1000 | [diff] [blame] | 1028 | new_comment[strcspn(new_comment, "\n")] = '\0'; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1029 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1030 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1031 | /* Save the file using the new passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1032 | if (!key_save_private(private, identity_file, passphrase, new_comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 1033 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1034 | memset(passphrase, 0, strlen(passphrase)); |
| 1035 | xfree(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1036 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1037 | xfree(comment); |
| 1038 | exit(1); |
| 1039 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1040 | memset(passphrase, 0, strlen(passphrase)); |
| 1041 | xfree(passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1042 | public = key_from_private(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1043 | key_free(private); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1044 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1045 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1046 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1047 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1048 | printf("Could not save your public key in %s\n", identity_file); |
| 1049 | exit(1); |
| 1050 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1051 | f = fdopen(fd, "w"); |
| 1052 | if (f == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1053 | printf("fdopen %s failed\n", identity_file); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1054 | exit(1); |
| 1055 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1056 | if (!key_write(public, f)) |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1057 | fprintf(stderr, "write key failed\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1058 | key_free(public); |
| 1059 | fprintf(f, " %s\n", new_comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1060 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1061 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1062 | xfree(comment); |
| 1063 | |
| 1064 | printf("The comment in your key file has been changed.\n"); |
| 1065 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1066 | } |
| 1067 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1068 | static const char * |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1069 | fmt_validity(u_int64_t valid_from, u_int64_t valid_to) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1070 | { |
| 1071 | char from[32], to[32]; |
| 1072 | static char ret[64]; |
| 1073 | time_t tt; |
| 1074 | struct tm *tm; |
| 1075 | |
| 1076 | *from = *to = '\0'; |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1077 | if (valid_from == 0 && valid_to == 0xffffffffffffffffULL) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1078 | return "forever"; |
| 1079 | |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1080 | if (valid_from != 0) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1081 | /* XXX revisit INT_MAX in 2038 :) */ |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1082 | tt = valid_from > INT_MAX ? INT_MAX : valid_from; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1083 | tm = localtime(&tt); |
| 1084 | strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm); |
| 1085 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1086 | if (valid_to != 0xffffffffffffffffULL) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1087 | /* XXX revisit INT_MAX in 2038 :) */ |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1088 | tt = valid_to > INT_MAX ? INT_MAX : valid_to; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1089 | tm = localtime(&tt); |
| 1090 | strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm); |
| 1091 | } |
| 1092 | |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1093 | if (valid_from == 0) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1094 | snprintf(ret, sizeof(ret), "before %s", to); |
| 1095 | return ret; |
| 1096 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1097 | if (valid_to == 0xffffffffffffffffULL) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1098 | snprintf(ret, sizeof(ret), "after %s", from); |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
| 1102 | snprintf(ret, sizeof(ret), "from %s to %s", from, to); |
| 1103 | return ret; |
| 1104 | } |
| 1105 | |
| 1106 | static void |
| 1107 | add_flag_constraint(Buffer *c, const char *name) |
| 1108 | { |
| 1109 | debug3("%s: %s", __func__, name); |
| 1110 | buffer_put_cstring(c, name); |
| 1111 | buffer_put_string(c, NULL, 0); |
| 1112 | } |
| 1113 | |
| 1114 | static void |
| 1115 | add_string_constraint(Buffer *c, const char *name, const char *value) |
| 1116 | { |
| 1117 | Buffer b; |
| 1118 | |
| 1119 | debug3("%s: %s=%s", __func__, name, value); |
| 1120 | buffer_init(&b); |
| 1121 | buffer_put_cstring(&b, value); |
| 1122 | |
| 1123 | buffer_put_cstring(c, name); |
| 1124 | buffer_put_string(c, buffer_ptr(&b), buffer_len(&b)); |
| 1125 | |
| 1126 | buffer_free(&b); |
| 1127 | } |
| 1128 | |
| 1129 | static void |
| 1130 | prepare_constraint_buf(Buffer *c) |
| 1131 | { |
| 1132 | |
| 1133 | buffer_clear(c); |
| 1134 | if ((constraint_flags & CONSTRAINT_X_FWD) != 0) |
| 1135 | add_flag_constraint(c, "permit-X11-forwarding"); |
| 1136 | if ((constraint_flags & CONSTRAINT_AGENT_FWD) != 0) |
| 1137 | add_flag_constraint(c, "permit-agent-forwarding"); |
| 1138 | if ((constraint_flags & CONSTRAINT_PORT_FWD) != 0) |
| 1139 | add_flag_constraint(c, "permit-port-forwarding"); |
| 1140 | if ((constraint_flags & CONSTRAINT_PTY) != 0) |
| 1141 | add_flag_constraint(c, "permit-pty"); |
| 1142 | if ((constraint_flags & CONSTRAINT_USER_RC) != 0) |
| 1143 | add_flag_constraint(c, "permit-user-rc"); |
| 1144 | if (constraint_command != NULL) |
Damien Miller | 689b872 | 2010-03-05 10:42:24 +1100 | [diff] [blame] | 1145 | add_string_constraint(c, "force-command", constraint_command); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1146 | if (constraint_src_addr != NULL) |
| 1147 | add_string_constraint(c, "source-address", constraint_src_addr); |
| 1148 | } |
| 1149 | |
| 1150 | static void |
| 1151 | do_ca_sign(struct passwd *pw, int argc, char **argv) |
| 1152 | { |
| 1153 | int i, fd; |
| 1154 | u_int n; |
| 1155 | Key *ca, *public; |
| 1156 | char *otmp, *tmp, *cp, *out, *comment, **plist = NULL; |
| 1157 | FILE *f; |
| 1158 | |
| 1159 | tmp = tilde_expand_filename(ca_key_path, pw->pw_uid); |
| 1160 | if ((ca = load_identity(tmp)) == NULL) |
| 1161 | fatal("Couldn't load CA key \"%s\"", tmp); |
| 1162 | xfree(tmp); |
| 1163 | |
| 1164 | for (i = 0; i < argc; i++) { |
| 1165 | /* Split list of principals */ |
| 1166 | n = 0; |
| 1167 | if (cert_principals != NULL) { |
| 1168 | otmp = tmp = xstrdup(cert_principals); |
| 1169 | plist = NULL; |
| 1170 | for (; (cp = strsep(&tmp, ",")) != NULL; n++) { |
| 1171 | plist = xrealloc(plist, n + 1, sizeof(*plist)); |
| 1172 | if (*(plist[n] = xstrdup(cp)) == '\0') |
| 1173 | fatal("Empty principal name"); |
| 1174 | } |
| 1175 | xfree(otmp); |
| 1176 | } |
| 1177 | |
| 1178 | tmp = tilde_expand_filename(argv[i], pw->pw_uid); |
| 1179 | if ((public = key_load_public(tmp, &comment)) == NULL) |
| 1180 | fatal("%s: unable to open \"%s\"", __func__, tmp); |
| 1181 | if (public->type != KEY_RSA && public->type != KEY_DSA) |
| 1182 | fatal("%s: key \"%s\" type %s cannot be certified", |
| 1183 | __func__, tmp, key_type(public)); |
| 1184 | |
| 1185 | /* Prepare certificate to sign */ |
| 1186 | if (key_to_certified(public) != 0) |
| 1187 | fatal("Could not upgrade key %s to certificate", tmp); |
| 1188 | public->cert->type = cert_key_type; |
| 1189 | public->cert->key_id = xstrdup(cert_key_id); |
| 1190 | public->cert->nprincipals = n; |
| 1191 | public->cert->principals = plist; |
| 1192 | public->cert->valid_after = cert_valid_from; |
| 1193 | public->cert->valid_before = cert_valid_to; |
| 1194 | prepare_constraint_buf(&public->cert->constraints); |
| 1195 | public->cert->signature_key = key_from_private(ca); |
| 1196 | |
| 1197 | if (key_certify(public, ca) != 0) |
| 1198 | fatal("Couldn't not certify key %s", tmp); |
| 1199 | |
| 1200 | if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0) |
| 1201 | *cp = '\0'; |
| 1202 | xasprintf(&out, "%s-cert.pub", tmp); |
| 1203 | xfree(tmp); |
| 1204 | |
| 1205 | if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) |
| 1206 | fatal("Could not open \"%s\" for writing: %s", out, |
| 1207 | strerror(errno)); |
| 1208 | if ((f = fdopen(fd, "w")) == NULL) |
| 1209 | fatal("%s: fdopen: %s", __func__, strerror(errno)); |
| 1210 | if (!key_write(public, f)) |
| 1211 | fatal("Could not write certified key to %s", out); |
| 1212 | fprintf(f, " %s\n", comment); |
| 1213 | fclose(f); |
| 1214 | |
| 1215 | if (!quiet) |
| 1216 | logit("Signed %s key %s: id \"%s\"%s%s valid %s", |
| 1217 | cert_key_type == SSH2_CERT_TYPE_USER?"user":"host", |
| 1218 | out, cert_key_id, |
| 1219 | cert_principals != NULL ? " for " : "", |
| 1220 | cert_principals != NULL ? cert_principals : "", |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1221 | fmt_validity(cert_valid_from, cert_valid_to)); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1222 | |
| 1223 | key_free(public); |
| 1224 | xfree(out); |
| 1225 | } |
| 1226 | exit(0); |
| 1227 | } |
| 1228 | |
| 1229 | static u_int64_t |
| 1230 | parse_relative_time(const char *s, time_t now) |
| 1231 | { |
| 1232 | int64_t mul, secs; |
| 1233 | |
| 1234 | mul = *s == '-' ? -1 : 1; |
| 1235 | |
| 1236 | if ((secs = convtime(s + 1)) == -1) |
| 1237 | fatal("Invalid relative certificate time %s", s); |
| 1238 | if (mul == -1 && secs > now) |
| 1239 | fatal("Certificate time %s cannot be represented", s); |
| 1240 | return now + (u_int64_t)(secs * mul); |
| 1241 | } |
| 1242 | |
| 1243 | static u_int64_t |
| 1244 | parse_absolute_time(const char *s) |
| 1245 | { |
| 1246 | struct tm tm; |
| 1247 | time_t tt; |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1248 | char buf[32], *fmt; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1249 | |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1250 | /* |
| 1251 | * POSIX strptime says "The application shall ensure that there |
| 1252 | * is white-space or other non-alphanumeric characters between |
| 1253 | * any two conversion specifications" so arrange things this way. |
| 1254 | */ |
| 1255 | switch (strlen(s)) { |
| 1256 | case 8: |
Damien Miller | 3e1ee49 | 2010-03-08 09:24:11 +1100 | [diff] [blame] | 1257 | fmt = "%Y-%m-%d"; |
| 1258 | snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6); |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1259 | break; |
| 1260 | case 14: |
Damien Miller | 3e1ee49 | 2010-03-08 09:24:11 +1100 | [diff] [blame] | 1261 | fmt = "%Y-%m-%dT%H:%M:%S"; |
| 1262 | snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s", |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1263 | s, s + 4, s + 6, s + 8, s + 10, s + 12); |
| 1264 | break; |
| 1265 | default: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1266 | fatal("Invalid certificate time format %s", s); |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1267 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1268 | |
| 1269 | bzero(&tm, sizeof(tm)); |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1270 | if (strptime(buf, fmt, &tm) == NULL) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1271 | fatal("Invalid certificate time %s", s); |
| 1272 | if ((tt = mktime(&tm)) < 0) |
| 1273 | fatal("Certificate time %s cannot be represented", s); |
| 1274 | return (u_int64_t)tt; |
| 1275 | } |
| 1276 | |
| 1277 | static void |
| 1278 | parse_cert_times(char *timespec) |
| 1279 | { |
| 1280 | char *from, *to; |
| 1281 | time_t now = time(NULL); |
| 1282 | int64_t secs; |
| 1283 | |
| 1284 | /* +timespec relative to now */ |
| 1285 | if (*timespec == '+' && strchr(timespec, ':') == NULL) { |
| 1286 | if ((secs = convtime(timespec + 1)) == -1) |
| 1287 | fatal("Invalid relative certificate life %s", timespec); |
| 1288 | cert_valid_to = now + secs; |
| 1289 | /* |
| 1290 | * Backdate certificate one minute to avoid problems on hosts |
| 1291 | * with poorly-synchronised clocks. |
| 1292 | */ |
| 1293 | cert_valid_from = ((now - 59)/ 60) * 60; |
| 1294 | return; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * from:to, where |
| 1299 | * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS |
| 1300 | * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS |
| 1301 | */ |
| 1302 | from = xstrdup(timespec); |
| 1303 | to = strchr(from, ':'); |
| 1304 | if (to == NULL || from == to || *(to + 1) == '\0') |
Damien Miller | 910f209 | 2010-03-04 14:17:22 +1100 | [diff] [blame] | 1305 | fatal("Invalid certificate life specification %s", timespec); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1306 | *to++ = '\0'; |
| 1307 | |
| 1308 | if (*from == '-' || *from == '+') |
| 1309 | cert_valid_from = parse_relative_time(from, now); |
| 1310 | else |
| 1311 | cert_valid_from = parse_absolute_time(from); |
| 1312 | |
| 1313 | if (*to == '-' || *to == '+') |
| 1314 | cert_valid_to = parse_relative_time(to, cert_valid_from); |
| 1315 | else |
| 1316 | cert_valid_to = parse_absolute_time(to); |
| 1317 | |
| 1318 | if (cert_valid_to <= cert_valid_from) |
| 1319 | fatal("Empty certificate validity interval"); |
| 1320 | xfree(from); |
| 1321 | } |
| 1322 | |
| 1323 | static void |
| 1324 | add_cert_constraint(char *opt) |
| 1325 | { |
| 1326 | char *val; |
| 1327 | |
| 1328 | if (strcmp(opt, "clear") == 0) |
| 1329 | constraint_flags = 0; |
| 1330 | else if (strcasecmp(opt, "no-x11-forwarding") == 0) |
| 1331 | constraint_flags &= ~CONSTRAINT_X_FWD; |
| 1332 | else if (strcasecmp(opt, "permit-x11-forwarding") == 0) |
| 1333 | constraint_flags |= CONSTRAINT_X_FWD; |
| 1334 | else if (strcasecmp(opt, "no-agent-forwarding") == 0) |
| 1335 | constraint_flags &= ~CONSTRAINT_AGENT_FWD; |
| 1336 | else if (strcasecmp(opt, "permit-agent-forwarding") == 0) |
| 1337 | constraint_flags |= CONSTRAINT_AGENT_FWD; |
| 1338 | else if (strcasecmp(opt, "no-port-forwarding") == 0) |
| 1339 | constraint_flags &= ~CONSTRAINT_PORT_FWD; |
| 1340 | else if (strcasecmp(opt, "permit-port-forwarding") == 0) |
| 1341 | constraint_flags |= CONSTRAINT_PORT_FWD; |
| 1342 | else if (strcasecmp(opt, "no-pty") == 0) |
| 1343 | constraint_flags &= ~CONSTRAINT_PTY; |
| 1344 | else if (strcasecmp(opt, "permit-pty") == 0) |
| 1345 | constraint_flags |= CONSTRAINT_PTY; |
| 1346 | else if (strcasecmp(opt, "no-user-rc") == 0) |
| 1347 | constraint_flags &= ~CONSTRAINT_USER_RC; |
| 1348 | else if (strcasecmp(opt, "permit-user-rc") == 0) |
| 1349 | constraint_flags |= CONSTRAINT_USER_RC; |
| 1350 | else if (strncasecmp(opt, "force-command=", 14) == 0) { |
| 1351 | val = opt + 14; |
| 1352 | if (*val == '\0') |
| 1353 | fatal("Empty force-command constraint"); |
| 1354 | if (constraint_command != NULL) |
| 1355 | fatal("force-command already specified"); |
| 1356 | constraint_command = xstrdup(val); |
| 1357 | } else if (strncasecmp(opt, "source-address=", 15) == 0) { |
| 1358 | val = opt + 15; |
| 1359 | if (*val == '\0') |
| 1360 | fatal("Empty source-address constraint"); |
| 1361 | if (constraint_src_addr != NULL) |
| 1362 | fatal("source-address already specified"); |
| 1363 | if (addr_match_cidr_list(NULL, val) != 0) |
| 1364 | fatal("Invalid source-address list"); |
| 1365 | constraint_src_addr = xstrdup(val); |
| 1366 | } else |
| 1367 | fatal("Unsupported certificate constraint \"%s\"", opt); |
| 1368 | } |
| 1369 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1370 | static void |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1371 | do_show_cert(struct passwd *pw) |
| 1372 | { |
| 1373 | Key *key; |
| 1374 | struct stat st; |
| 1375 | char *key_fp, *ca_fp; |
| 1376 | Buffer constraints, constraint; |
| 1377 | u_char *name, *data; |
| 1378 | u_int i, dlen; |
| 1379 | |
| 1380 | if (!have_identity) |
| 1381 | ask_filename(pw, "Enter file in which the key is"); |
| 1382 | if (stat(identity_file, &st) < 0) { |
| 1383 | perror(identity_file); |
| 1384 | exit(1); |
| 1385 | } |
| 1386 | if ((key = key_load_public(identity_file, NULL)) == NULL) |
| 1387 | fatal("%s is not a public key", identity_file); |
| 1388 | if (!key_is_cert(key)) |
| 1389 | fatal("%s is not a certificate", identity_file); |
| 1390 | |
| 1391 | key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); |
| 1392 | ca_fp = key_fingerprint(key->cert->signature_key, |
| 1393 | SSH_FP_MD5, SSH_FP_HEX); |
| 1394 | |
| 1395 | printf("%s:\n", identity_file); |
Damien Miller | 1cfbfaf | 2010-03-22 05:58:24 +1100 | [diff] [blame] | 1396 | printf(" %s %s certificate %s\n", key_type(key), |
| 1397 | key_cert_type(key), key_fp); |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1398 | printf(" Signed by %s CA %s\n", |
| 1399 | key_type(key->cert->signature_key), ca_fp); |
| 1400 | printf(" Key ID \"%s\"\n", key->cert->key_id); |
| 1401 | printf(" Valid: %s\n", |
| 1402 | fmt_validity(key->cert->valid_after, key->cert->valid_before)); |
| 1403 | printf(" Principals: "); |
| 1404 | if (key->cert->nprincipals == 0) |
| 1405 | printf("(none)\n"); |
| 1406 | else { |
| 1407 | for (i = 0; i < key->cert->nprincipals; i++) |
| 1408 | printf("\n %s", |
| 1409 | key->cert->principals[i]); |
| 1410 | printf("\n"); |
| 1411 | } |
| 1412 | printf(" Constraints: "); |
| 1413 | if (buffer_len(&key->cert->constraints) == 0) |
| 1414 | printf("(none)\n"); |
| 1415 | else { |
| 1416 | printf("\n"); |
| 1417 | buffer_init(&constraints); |
| 1418 | buffer_append(&constraints, |
| 1419 | buffer_ptr(&key->cert->constraints), |
| 1420 | buffer_len(&key->cert->constraints)); |
| 1421 | buffer_init(&constraint); |
| 1422 | while (buffer_len(&constraints) != 0) { |
| 1423 | name = buffer_get_string(&constraints, NULL); |
| 1424 | data = buffer_get_string_ptr(&constraints, &dlen); |
| 1425 | buffer_append(&constraint, data, dlen); |
| 1426 | printf(" %s", name); |
| 1427 | if (strcmp(name, "permit-X11-forwarding") == 0 || |
| 1428 | strcmp(name, "permit-agent-forwarding") == 0 || |
| 1429 | strcmp(name, "permit-port-forwarding") == 0 || |
| 1430 | strcmp(name, "permit-pty") == 0 || |
| 1431 | strcmp(name, "permit-user-rc") == 0) |
| 1432 | printf("\n"); |
| 1433 | else if (strcmp(name, "force-command") == 0 || |
| 1434 | strcmp(name, "source-address") == 0) { |
| 1435 | data = buffer_get_string(&constraint, NULL); |
| 1436 | printf(" %s\n", data); |
| 1437 | xfree(data); |
| 1438 | } else { |
| 1439 | printf(" UNKNOWN CONSTRAINT (len %u)\n", |
| 1440 | buffer_len(&constraint)); |
| 1441 | buffer_clear(&constraint); |
| 1442 | } |
| 1443 | xfree(name); |
| 1444 | if (buffer_len(&constraint) != 0) |
| 1445 | fatal("Constraint corrupt: extra data at end"); |
| 1446 | } |
| 1447 | buffer_free(&constraint); |
| 1448 | buffer_free(&constraints); |
| 1449 | } |
| 1450 | |
| 1451 | exit(0); |
| 1452 | } |
| 1453 | |
| 1454 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 1455 | usage(void) |
| 1456 | { |
Damien Miller | 5cbe7ca | 2007-09-17 16:05:50 +1000 | [diff] [blame] | 1457 | fprintf(stderr, "usage: %s [options]\n", __progname); |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 1458 | fprintf(stderr, "Options:\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1459 | fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n"); |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 1460 | fprintf(stderr, " -B Show bubblebabble digest of key file.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1461 | fprintf(stderr, " -b bits Number of bits in the key to create.\n"); |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 1462 | fprintf(stderr, " -C comment Provide new comment.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1463 | fprintf(stderr, " -c Change comment in private and public key files.\n"); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1464 | #ifdef ENABLE_PKCS11 |
| 1465 | fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n"); |
| 1466 | #endif |
Darren Tucker | 26dc3e6 | 2007-02-19 22:09:06 +1100 | [diff] [blame] | 1467 | fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1468 | fprintf(stderr, " -F hostname Find hostname in known hosts file.\n"); |
| 1469 | fprintf(stderr, " -f filename Filename of the key file.\n"); |
| 1470 | fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n"); |
| 1471 | fprintf(stderr, " -g Use generic DNS resource record format.\n"); |
| 1472 | fprintf(stderr, " -H Hash names in known_hosts file.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1473 | fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n"); |
| 1474 | fprintf(stderr, " -I key_id Key identifier to include in certificate.\n"); |
Darren Tucker | 26dc3e6 | 2007-02-19 22:09:06 +1100 | [diff] [blame] | 1475 | fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n"); |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1476 | fprintf(stderr, " -L Print the contents of a certificate.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1477 | fprintf(stderr, " -l Show fingerprint of key file.\n"); |
| 1478 | fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1479 | fprintf(stderr, " -n name,... User/host principal names to include in certificate\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1480 | fprintf(stderr, " -N phrase Provide new passphrase.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1481 | fprintf(stderr, " -O cnstr Specify a certificate constraint.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1482 | fprintf(stderr, " -P phrase Provide old passphrase.\n"); |
| 1483 | fprintf(stderr, " -p Change passphrase of private key file.\n"); |
| 1484 | fprintf(stderr, " -q Quiet.\n"); |
| 1485 | fprintf(stderr, " -R hostname Remove host from known_hosts file.\n"); |
| 1486 | fprintf(stderr, " -r hostname Print DNS resource record.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1487 | fprintf(stderr, " -s ca_key Certify keys with CA key.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1488 | fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n"); |
| 1489 | fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n"); |
| 1490 | fprintf(stderr, " -t type Specify type of key to create.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1491 | fprintf(stderr, " -V from:to Specify certificate validity interval.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1492 | fprintf(stderr, " -v Verbose.\n"); |
| 1493 | fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n"); |
| 1494 | fprintf(stderr, " -y Read private key file and print public key.\n"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1495 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1496 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 1497 | } |
| 1498 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1499 | /* |
| 1500 | * Main program for key management. |
| 1501 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1502 | int |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 1503 | main(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1504 | { |
Damien Miller | a41c8b1 | 2002-01-22 23:05:08 +1100 | [diff] [blame] | 1505 | char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1506 | char out_file[MAXPATHLEN], *pkcs11provider = NULL; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1507 | char *rr_hostname = NULL; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1508 | Key *private, *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1509 | struct passwd *pw; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1510 | struct stat st; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1511 | int opt, type, fd; |
Darren Tucker | 2db8ae6 | 2005-06-01 23:02:25 +1000 | [diff] [blame] | 1512 | u_int32_t memory = 0, generator_wanted = 0, trials = 100; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1513 | int do_gen_candidates = 0, do_screen_candidates = 0; |
| 1514 | BIGNUM *start = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1515 | FILE *f; |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 1516 | const char *errstr; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1517 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1518 | extern int optind; |
| 1519 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1520 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1521 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1522 | sanitise_stdfd(); |
| 1523 | |
Darren Tucker | 9ac56e9 | 2007-01-14 10:19:59 +1100 | [diff] [blame] | 1524 | __progname = ssh_get_progname(argv[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 1525 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1526 | SSLeay_add_all_algorithms(); |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 1527 | log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1528 | |
Kevin Steves | 3a88191 | 2002-07-20 19:05:40 +0000 | [diff] [blame] | 1529 | init_rng(); |
| 1530 | seed_rng(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1531 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1532 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1533 | pw = getpwuid(getuid()); |
| 1534 | if (!pw) { |
| 1535 | printf("You don't exist, go away!\n"); |
| 1536 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1537 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1538 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 1539 | perror("gethostname"); |
| 1540 | exit(1); |
| 1541 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1542 | |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1543 | while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:N:n:" |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1544 | "O:C:r:g:R:T:G:M:S:s:a:V:W:")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1545 | switch (opt) { |
| 1546 | case 'b': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1547 | bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr); |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 1548 | if (errstr) |
| 1549 | fatal("Bits has bad value %s (%s)", |
| 1550 | optarg, errstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1551 | break; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1552 | case 'F': |
| 1553 | find_host = 1; |
| 1554 | rr_hostname = optarg; |
| 1555 | break; |
| 1556 | case 'H': |
| 1557 | hash_hosts = 1; |
| 1558 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1559 | case 'I': |
| 1560 | cert_key_id = optarg; |
| 1561 | break; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1562 | case 'R': |
| 1563 | delete_host = 1; |
| 1564 | rr_hostname = optarg; |
| 1565 | break; |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1566 | case 'L': |
| 1567 | show_cert = 1; |
| 1568 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1569 | case 'l': |
| 1570 | print_fingerprint = 1; |
| 1571 | break; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 1572 | case 'B': |
| 1573 | print_bubblebabble = 1; |
| 1574 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1575 | case 'n': |
| 1576 | cert_principals = optarg; |
| 1577 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1578 | case 'p': |
| 1579 | change_passphrase = 1; |
| 1580 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1581 | case 'c': |
| 1582 | change_comment = 1; |
| 1583 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1584 | case 'f': |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1585 | if (strlcpy(identity_file, optarg, sizeof(identity_file)) >= |
| 1586 | sizeof(identity_file)) |
| 1587 | fatal("Identity filename too long"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1588 | have_identity = 1; |
| 1589 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1590 | case 'g': |
| 1591 | print_generic = 1; |
| 1592 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1593 | case 'P': |
| 1594 | identity_passphrase = optarg; |
| 1595 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1596 | case 'N': |
| 1597 | identity_new_passphrase = optarg; |
| 1598 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1599 | case 'O': |
| 1600 | add_cert_constraint(optarg); |
| 1601 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1602 | case 'C': |
| 1603 | identity_comment = optarg; |
| 1604 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1605 | case 'q': |
| 1606 | quiet = 1; |
| 1607 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1608 | case 'e': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1609 | case 'x': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1610 | /* export key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1611 | convert_to_ssh2 = 1; |
| 1612 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1613 | case 'h': |
| 1614 | cert_key_type = SSH2_CERT_TYPE_HOST; |
| 1615 | constraint_flags = 0; |
| 1616 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1617 | case 'i': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1618 | case 'X': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1619 | /* import key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1620 | convert_from_ssh2 = 1; |
| 1621 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1622 | case 'y': |
| 1623 | print_public = 1; |
| 1624 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1625 | case 'd': |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1626 | key_type_name = "dsa"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1627 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1628 | case 's': |
| 1629 | ca_key_path = optarg; |
| 1630 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1631 | case 't': |
| 1632 | key_type_name = optarg; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1633 | break; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 1634 | case 'D': |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1635 | pkcs11provider = optarg; |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 1636 | break; |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1637 | case 'v': |
| 1638 | if (log_level == SYSLOG_LEVEL_INFO) |
| 1639 | log_level = SYSLOG_LEVEL_DEBUG1; |
| 1640 | else { |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1641 | if (log_level >= SYSLOG_LEVEL_DEBUG1 && |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1642 | log_level < SYSLOG_LEVEL_DEBUG3) |
| 1643 | log_level++; |
| 1644 | } |
| 1645 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1646 | case 'r': |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1647 | rr_hostname = optarg; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1648 | break; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1649 | case 'W': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1650 | generator_wanted = (u_int32_t)strtonum(optarg, 1, |
| 1651 | UINT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1652 | if (errstr) |
| 1653 | fatal("Desired generator has bad value: %s (%s)", |
| 1654 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1655 | break; |
| 1656 | case 'a': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1657 | trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1658 | if (errstr) |
| 1659 | fatal("Invalid number of trials: %s (%s)", |
| 1660 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1661 | break; |
| 1662 | case 'M': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1663 | memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1664 | if (errstr) { |
| 1665 | fatal("Memory limit is %s: %s", errstr, optarg); |
| 1666 | } |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1667 | break; |
| 1668 | case 'G': |
| 1669 | do_gen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1670 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 1671 | sizeof(out_file)) |
| 1672 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1673 | break; |
| 1674 | case 'T': |
| 1675 | do_screen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1676 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 1677 | sizeof(out_file)) |
| 1678 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1679 | break; |
| 1680 | case 'S': |
| 1681 | /* XXX - also compare length against bits */ |
| 1682 | if (BN_hex2bn(&start, optarg) == 0) |
| 1683 | fatal("Invalid start point."); |
| 1684 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1685 | case 'V': |
| 1686 | parse_cert_times(optarg); |
| 1687 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1688 | case '?': |
| 1689 | default: |
| 1690 | usage(); |
| 1691 | } |
| 1692 | } |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1693 | |
| 1694 | /* reinit */ |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 1695 | log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1); |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1696 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1697 | argv += optind; |
| 1698 | argc -= optind; |
| 1699 | |
| 1700 | if (ca_key_path != NULL) { |
| 1701 | if (argc < 1) { |
| 1702 | printf("Too few arguments.\n"); |
| 1703 | usage(); |
| 1704 | } |
| 1705 | } else if (argc > 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1706 | printf("Too many arguments.\n"); |
| 1707 | usage(); |
| 1708 | } |
| 1709 | if (change_passphrase && change_comment) { |
| 1710 | printf("Can only have one of -p and -c.\n"); |
| 1711 | usage(); |
| 1712 | } |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 1713 | if (print_fingerprint && (delete_host || hash_hosts)) { |
| 1714 | printf("Cannot use -l with -D or -R.\n"); |
| 1715 | usage(); |
| 1716 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1717 | if (ca_key_path != NULL) { |
| 1718 | if (cert_key_id == NULL) |
| 1719 | fatal("Must specify key id (-I) when certifying"); |
| 1720 | do_ca_sign(pw, argc, argv); |
| 1721 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1722 | if (show_cert) |
| 1723 | do_show_cert(pw); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1724 | if (delete_host || hash_hosts || find_host) |
| 1725 | do_known_hosts(pw, rr_hostname); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 1726 | if (print_fingerprint || print_bubblebabble) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1727 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1728 | if (change_passphrase) |
| 1729 | do_change_passphrase(pw); |
Damien Miller | 4a10d2e | 2002-03-11 22:53:29 +1100 | [diff] [blame] | 1730 | if (change_comment) |
| 1731 | do_change_comment(pw); |
Kevin Steves | 3a88191 | 2002-07-20 19:05:40 +0000 | [diff] [blame] | 1732 | if (convert_to_ssh2) |
| 1733 | do_convert_to_ssh2(pw); |
| 1734 | if (convert_from_ssh2) |
| 1735 | do_convert_from_ssh2(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1736 | if (print_public) |
| 1737 | do_print_public(pw); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1738 | if (rr_hostname != NULL) { |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1739 | unsigned int n = 0; |
| 1740 | |
| 1741 | if (have_identity) { |
| 1742 | n = do_print_resource_record(pw, |
| 1743 | identity_file, rr_hostname); |
| 1744 | if (n == 0) { |
| 1745 | perror(identity_file); |
| 1746 | exit(1); |
| 1747 | } |
| 1748 | exit(0); |
| 1749 | } else { |
| 1750 | |
| 1751 | n += do_print_resource_record(pw, |
| 1752 | _PATH_HOST_RSA_KEY_FILE, rr_hostname); |
| 1753 | n += do_print_resource_record(pw, |
| 1754 | _PATH_HOST_DSA_KEY_FILE, rr_hostname); |
| 1755 | |
| 1756 | if (n == 0) |
| 1757 | fatal("no keys found."); |
| 1758 | exit(0); |
| 1759 | } |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1760 | } |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1761 | if (pkcs11provider != NULL) |
| 1762 | do_download(pw, pkcs11provider); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1763 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1764 | if (do_gen_candidates) { |
| 1765 | FILE *out = fopen(out_file, "w"); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1766 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1767 | if (out == NULL) { |
| 1768 | error("Couldn't open modulus candidate file \"%s\": %s", |
| 1769 | out_file, strerror(errno)); |
| 1770 | return (1); |
| 1771 | } |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 1772 | if (bits == 0) |
| 1773 | bits = DEFAULT_BITS; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1774 | if (gen_candidates(out, memory, bits, start) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1775 | fatal("modulus candidate generation failed"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1776 | |
| 1777 | return (0); |
| 1778 | } |
| 1779 | |
| 1780 | if (do_screen_candidates) { |
| 1781 | FILE *in; |
| 1782 | FILE *out = fopen(out_file, "w"); |
| 1783 | |
| 1784 | if (have_identity && strcmp(identity_file, "-") != 0) { |
| 1785 | if ((in = fopen(identity_file, "r")) == NULL) { |
| 1786 | fatal("Couldn't open modulus candidate " |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1787 | "file \"%s\": %s", identity_file, |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1788 | strerror(errno)); |
| 1789 | } |
| 1790 | } else |
| 1791 | in = stdin; |
| 1792 | |
| 1793 | if (out == NULL) { |
| 1794 | fatal("Couldn't open moduli file \"%s\": %s", |
| 1795 | out_file, strerror(errno)); |
| 1796 | } |
| 1797 | if (prime_test(in, out, trials, generator_wanted) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1798 | fatal("modulus screening failed"); |
Darren Tucker | f4220e6 | 2003-08-21 16:44:07 +1000 | [diff] [blame] | 1799 | return (0); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1800 | } |
| 1801 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1802 | arc4random_stir(); |
| 1803 | |
Damien Miller | f14be5c | 2005-11-05 15:15:49 +1100 | [diff] [blame] | 1804 | if (key_type_name == NULL) |
| 1805 | key_type_name = "rsa"; |
| 1806 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 1807 | type = key_type_from_name(key_type_name); |
| 1808 | if (type == KEY_UNSPEC) { |
| 1809 | fprintf(stderr, "unknown key type %s\n", key_type_name); |
| 1810 | exit(1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1811 | } |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 1812 | if (bits == 0) |
| 1813 | bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS; |
Tim Rice | 660c340 | 2005-11-28 17:45:32 -0800 | [diff] [blame] | 1814 | if (type == KEY_DSA && bits != 1024) |
| 1815 | fatal("DSA keys must be 1024 bits"); |
Darren Tucker | 3af2ac5 | 2005-11-29 13:10:24 +1100 | [diff] [blame] | 1816 | if (!quiet) |
| 1817 | printf("Generating public/private %s key pair.\n", key_type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1818 | private = key_generate(type, bits); |
| 1819 | if (private == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1820 | fprintf(stderr, "key_generate failed\n"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1821 | exit(1); |
| 1822 | } |
| 1823 | public = key_from_private(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1824 | |
| 1825 | if (!have_identity) |
| 1826 | ask_filename(pw, "Enter file in which to save the key"); |
| 1827 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 1828 | /* Create ~/.ssh directory if it doesn't already exist. */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1829 | 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] | 1830 | if (strstr(identity_file, dotsshdir) != NULL && |
| 1831 | stat(dotsshdir, &st) < 0) { |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1832 | if (mkdir(dotsshdir, 0700) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1833 | error("Could not create directory '%s'.", dotsshdir); |
| 1834 | else if (!quiet) |
| 1835 | printf("Created directory '%s'.\n", dotsshdir); |
| 1836 | } |
| 1837 | /* If the file already exists, ask the user to confirm. */ |
| 1838 | if (stat(identity_file, &st) >= 0) { |
| 1839 | char yesno[3]; |
| 1840 | printf("%s already exists.\n", identity_file); |
| 1841 | printf("Overwrite (y/n)? "); |
| 1842 | fflush(stdout); |
| 1843 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 1844 | exit(1); |
| 1845 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 1846 | exit(1); |
| 1847 | } |
| 1848 | /* Ask for a passphrase (twice). */ |
| 1849 | if (identity_passphrase) |
| 1850 | passphrase1 = xstrdup(identity_passphrase); |
| 1851 | else if (identity_new_passphrase) |
| 1852 | passphrase1 = xstrdup(identity_new_passphrase); |
| 1853 | else { |
| 1854 | passphrase_again: |
| 1855 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1856 | read_passphrase("Enter passphrase (empty for no " |
| 1857 | "passphrase): ", RP_ALLOW_STDIN); |
| 1858 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
| 1859 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1860 | if (strcmp(passphrase1, passphrase2) != 0) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1861 | /* |
| 1862 | * The passphrases do not match. Clear them and |
| 1863 | * retry. |
| 1864 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1865 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1866 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1867 | xfree(passphrase1); |
| 1868 | xfree(passphrase2); |
| 1869 | printf("Passphrases do not match. Try again.\n"); |
| 1870 | goto passphrase_again; |
| 1871 | } |
| 1872 | /* Clear the other copy of the passphrase. */ |
| 1873 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1874 | xfree(passphrase2); |
| 1875 | } |
| 1876 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1877 | if (identity_comment) { |
| 1878 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 1879 | } else { |
Darren Tucker | e15fb09 | 2008-11-11 16:31:43 +1100 | [diff] [blame] | 1880 | /* Create default comment field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1881 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 1882 | } |
| 1883 | |
| 1884 | /* Save the key with the given passphrase and comment. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1885 | if (!key_save_private(private, identity_file, passphrase1, comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 1886 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1887 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1888 | xfree(passphrase1); |
| 1889 | exit(1); |
| 1890 | } |
| 1891 | /* Clear the passphrase. */ |
| 1892 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1893 | xfree(passphrase1); |
| 1894 | |
| 1895 | /* Clear the private key and the random number generator. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1896 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1897 | arc4random_stir(); |
| 1898 | |
| 1899 | if (!quiet) |
| 1900 | printf("Your identification has been saved in %s.\n", identity_file); |
| 1901 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1902 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1903 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1904 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1905 | printf("Could not save your public key in %s\n", identity_file); |
| 1906 | exit(1); |
| 1907 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1908 | f = fdopen(fd, "w"); |
| 1909 | if (f == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1910 | printf("fdopen %s failed\n", identity_file); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1911 | exit(1); |
| 1912 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1913 | if (!key_write(public, f)) |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1914 | fprintf(stderr, "write key failed\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1915 | fprintf(f, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1916 | fclose(f); |
| 1917 | |
| 1918 | if (!quiet) { |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1919 | char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 1920 | char *ra = key_fingerprint(public, SSH_FP_MD5, |
| 1921 | SSH_FP_RANDOMART); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1922 | printf("Your public key has been saved in %s.\n", |
| 1923 | identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1924 | printf("The key fingerprint is:\n"); |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1925 | printf("%s %s\n", fp, comment); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 1926 | printf("The key's randomart image is:\n"); |
| 1927 | printf("%s\n", ra); |
| 1928 | xfree(ra); |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1929 | xfree(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1930 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1931 | |
| 1932 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1933 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1934 | } |