djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: ssh-keygen.c,v 1.251 2014/12/21 22:27:56 djm 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 | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 51 | #include "ssh.h" |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 52 | #include "ssh2.h" |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 53 | #include "ssh-pkcs11.h" |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 54 | #include "atomicio.h" |
| 55 | #include "krl.h" |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 56 | #include "digest.h" |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 57 | |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 58 | /* Number of bits in the RSA/DSA key. This value can be set on the command line. */ |
| 59 | #define DEFAULT_BITS 2048 |
| 60 | #define DEFAULT_BITS_DSA 1024 |
Damien Miller | 6e9f680 | 2010-09-10 11:17:38 +1000 | [diff] [blame] | 61 | #define DEFAULT_BITS_ECDSA 256 |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 62 | u_int32_t bits = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 64 | /* |
| 65 | * Flag indicating that we just want to change the passphrase. This can be |
| 66 | * set on the command line. |
| 67 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 68 | int change_passphrase = 0; |
| 69 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 70 | /* |
| 71 | * Flag indicating that we just want to change the comment. This can be set |
| 72 | * on the command line. |
| 73 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 74 | int change_comment = 0; |
| 75 | |
| 76 | int quiet = 0; |
| 77 | |
Darren Tucker | 35c4553 | 2008-06-13 04:43:15 +1000 | [diff] [blame] | 78 | int log_level = SYSLOG_LEVEL_INFO; |
| 79 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 80 | /* Flag indicating that we want to hash a known_hosts file */ |
| 81 | int hash_hosts = 0; |
| 82 | /* Flag indicating that we want lookup a host in known_hosts file */ |
| 83 | int find_host = 0; |
| 84 | /* Flag indicating that we want to delete a host from a known_hosts file */ |
| 85 | int delete_host = 0; |
| 86 | |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 87 | /* Flag indicating that we want to show the contents of a certificate */ |
| 88 | int show_cert = 0; |
| 89 | |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 90 | /* Flag indicating that we just want to see the key fingerprint */ |
| 91 | int print_fingerprint = 0; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 92 | int print_bubblebabble = 0; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 93 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 94 | /* Hash algorithm to use for fingerprints. */ |
| 95 | int fingerprint_hash = SSH_FP_HASH_DEFAULT; |
| 96 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 97 | /* The identity file name, given on the command line or entered by the user. */ |
| 98 | char identity_file[1024]; |
| 99 | int have_identity = 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 100 | |
| 101 | /* This is set to the passphrase if given on the command line. */ |
| 102 | char *identity_passphrase = NULL; |
| 103 | |
| 104 | /* This is set to the new passphrase if given on the command line. */ |
| 105 | char *identity_new_passphrase = NULL; |
| 106 | |
| 107 | /* This is set to the new comment if given on the command line. */ |
| 108 | char *identity_comment = NULL; |
| 109 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 110 | /* Path to CA key when certifying keys. */ |
| 111 | char *ca_key_path = NULL; |
| 112 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 113 | /* Certificate serial number */ |
Damien Miller | 55aca02 | 2012-12-03 11:25:30 +1100 | [diff] [blame] | 114 | unsigned long long cert_serial = 0; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 115 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 116 | /* Key type when certifying */ |
| 117 | u_int cert_key_type = SSH2_CERT_TYPE_USER; |
| 118 | |
| 119 | /* "key ID" of signed key */ |
| 120 | char *cert_key_id = NULL; |
| 121 | |
| 122 | /* Comma-separated list of principal names for certifying keys */ |
| 123 | char *cert_principals = NULL; |
| 124 | |
| 125 | /* Validity period for certificates */ |
| 126 | u_int64_t cert_valid_from = 0; |
| 127 | u_int64_t cert_valid_to = ~0ULL; |
| 128 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 129 | /* Certificate options */ |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 130 | #define CERTOPT_X_FWD (1) |
| 131 | #define CERTOPT_AGENT_FWD (1<<1) |
| 132 | #define CERTOPT_PORT_FWD (1<<2) |
| 133 | #define CERTOPT_PTY (1<<3) |
| 134 | #define CERTOPT_USER_RC (1<<4) |
| 135 | #define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \ |
| 136 | CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC) |
| 137 | u_int32_t certflags_flags = CERTOPT_DEFAULT; |
| 138 | char *certflags_command = NULL; |
| 139 | char *certflags_src_addr = NULL; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 140 | |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 141 | /* Conversion to/from various formats */ |
| 142 | int convert_to = 0; |
| 143 | int convert_from = 0; |
| 144 | enum { |
| 145 | FMT_RFC4716, |
| 146 | FMT_PKCS8, |
| 147 | FMT_PEM |
| 148 | } convert_format = FMT_RFC4716; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 149 | int print_public = 0; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 150 | int print_generic = 0; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 151 | |
Damien Miller | a41c8b1 | 2002-01-22 23:05:08 +1100 | [diff] [blame] | 152 | char *key_type_name = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 153 | |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 154 | /* Load key from this PKCS#11 provider */ |
| 155 | char *pkcs11provider = NULL; |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 156 | |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 157 | /* Use new OpenSSH private key format when writing SSH2 keys instead of PEM */ |
| 158 | int use_new_format = 0; |
| 159 | |
| 160 | /* Cipher for new-format private keys */ |
| 161 | char *new_format_cipher = NULL; |
| 162 | |
| 163 | /* |
| 164 | * Number of KDF rounds to derive new format keys / |
| 165 | * number of primality trials when screening moduli. |
| 166 | */ |
| 167 | int rounds = 0; |
| 168 | |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 169 | /* argv0 */ |
| 170 | extern char *__progname; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | |
Damien Miller | e5c0d52 | 2014-07-03 21:24:19 +1000 | [diff] [blame] | 172 | char hostname[NI_MAXHOST]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 173 | |
Darren Tucker | 770fc01 | 2004-05-13 16:24:32 +1000 | [diff] [blame] | 174 | /* moduli.c */ |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 175 | int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *); |
Damien Miller | dfceafe | 2012-07-06 13:44:19 +1000 | [diff] [blame] | 176 | int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long, |
| 177 | unsigned long); |
Darren Tucker | 770fc01 | 2004-05-13 16:24:32 +1000 | [diff] [blame] | 178 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 179 | static void |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 180 | type_bits_valid(int type, u_int32_t *bitsp) |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 181 | { |
| 182 | u_int maxbits; |
| 183 | |
| 184 | if (type == KEY_UNSPEC) { |
| 185 | fprintf(stderr, "unknown key type %s\n", key_type_name); |
| 186 | exit(1); |
| 187 | } |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 188 | if (*bitsp == 0) { |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 189 | if (type == KEY_DSA) |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 190 | *bitsp = DEFAULT_BITS_DSA; |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 191 | else if (type == KEY_ECDSA) |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 192 | *bitsp = DEFAULT_BITS_ECDSA; |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 193 | else |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 194 | *bitsp = DEFAULT_BITS; |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 195 | } |
| 196 | maxbits = (type == KEY_DSA) ? |
| 197 | OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS; |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 198 | if (*bitsp > maxbits) { |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 199 | fprintf(stderr, "key bits exceeds maximum %d\n", maxbits); |
| 200 | exit(1); |
| 201 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 202 | #ifdef WITH_OPENSSL |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 203 | if (type == KEY_DSA && *bitsp != 1024) |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 204 | fatal("DSA keys must be 1024 bits"); |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 205 | else if (type != KEY_ECDSA && type != KEY_ED25519 && *bitsp < 768) |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 206 | fatal("Key must at least be 768 bits"); |
Damien Miller | 884b63a | 2011-05-05 14:14:52 +1000 | [diff] [blame] | 207 | else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(*bitsp) == -1) |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 208 | fatal("Invalid ECDSA key length - valid lengths are " |
| 209 | "256, 384 or 521 bits"); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 210 | #endif |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 214 | ask_filename(struct passwd *pw, const char *prompt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 215 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | char buf[1024]; |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 217 | char *name = NULL; |
| 218 | |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 219 | if (key_type_name == NULL) |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 220 | name = _PATH_SSH_CLIENT_ID_RSA; |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 221 | else { |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 222 | switch (key_type_from_name(key_type_name)) { |
| 223 | case KEY_RSA1: |
| 224 | name = _PATH_SSH_CLIENT_IDENTITY; |
| 225 | break; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 226 | case KEY_DSA_CERT: |
| 227 | case KEY_DSA_CERT_V00: |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 228 | case KEY_DSA: |
| 229 | name = _PATH_SSH_CLIENT_ID_DSA; |
| 230 | break; |
Damien Miller | dd190dd | 2010-11-11 14:17:02 +1100 | [diff] [blame] | 231 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 232 | case KEY_ECDSA_CERT: |
| 233 | case KEY_ECDSA: |
| 234 | name = _PATH_SSH_CLIENT_ID_ECDSA; |
| 235 | break; |
Damien Miller | dd190dd | 2010-11-11 14:17:02 +1100 | [diff] [blame] | 236 | #endif |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 237 | case KEY_RSA_CERT: |
| 238 | case KEY_RSA_CERT_V00: |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 239 | case KEY_RSA: |
| 240 | name = _PATH_SSH_CLIENT_ID_RSA; |
| 241 | break; |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 242 | case KEY_ED25519: |
| 243 | case KEY_ED25519_CERT: |
| 244 | name = _PATH_SSH_CLIENT_ID_ED25519; |
| 245 | break; |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 246 | default: |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 247 | fprintf(stderr, "bad key type\n"); |
Damien Miller | 993dd55 | 2002-02-19 15:22:47 +1100 | [diff] [blame] | 248 | exit(1); |
| 249 | break; |
| 250 | } |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 251 | } |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 252 | snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name); |
Ben Lindstrom | 3deda8b | 2000-12-22 20:27:43 +0000 | [diff] [blame] | 253 | fprintf(stderr, "%s (%s): ", prompt, identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 254 | if (fgets(buf, sizeof(buf), stdin) == NULL) |
| 255 | exit(1); |
Damien Miller | 14b017d | 2007-09-17 16:09:15 +1000 | [diff] [blame] | 256 | buf[strcspn(buf, "\n")] = '\0'; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 257 | if (strcmp(buf, "") != 0) |
| 258 | strlcpy(identity_file, buf, sizeof(identity_file)); |
| 259 | have_identity = 1; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 260 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 261 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 262 | static Key * |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 263 | load_identity(char *filename) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 264 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 265 | char *pass; |
| 266 | Key *prv; |
| 267 | |
Ben Lindstrom | a370005 | 2001-04-05 23:26:32 +0000 | [diff] [blame] | 268 | prv = key_load_private(filename, "", NULL); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 269 | if (prv == NULL) { |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 270 | if (identity_passphrase) |
| 271 | pass = xstrdup(identity_passphrase); |
| 272 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 273 | pass = read_passphrase("Enter passphrase: ", |
| 274 | RP_ALLOW_STDIN); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 275 | prv = key_load_private(filename, pass, NULL); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 276 | explicit_bzero(pass, strlen(pass)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 277 | free(pass); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 278 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 279 | return prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 280 | } |
| 281 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 282 | #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----" |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 283 | #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 284 | #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----" |
Kevin Steves | ef4eea9 | 2001-02-05 12:42:17 +0000 | [diff] [blame] | 285 | #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 286 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 287 | #ifdef WITH_OPENSSL |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 288 | static void |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 289 | do_convert_to_ssh2(struct passwd *pw, Key *k) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 290 | { |
Ben Lindstrom | c58ab02 | 2002-02-26 18:15:09 +0000 | [diff] [blame] | 291 | u_int len; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 292 | u_char *blob; |
Darren Tucker | d04758d | 2010-01-12 19:41:57 +1100 | [diff] [blame] | 293 | char comment[61]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 294 | |
Damien Miller | a563cce | 2012-04-22 11:07:28 +1000 | [diff] [blame] | 295 | if (k->type == KEY_RSA1) { |
| 296 | fprintf(stderr, "version 1 keys are not supported\n"); |
| 297 | exit(1); |
| 298 | } |
Ben Lindstrom | 99a30f1 | 2001-09-18 05:49:14 +0000 | [diff] [blame] | 299 | if (key_to_blob(k, &blob, &len) <= 0) { |
| 300 | fprintf(stderr, "key_to_blob failed\n"); |
| 301 | exit(1); |
| 302 | } |
Darren Tucker | d04758d | 2010-01-12 19:41:57 +1100 | [diff] [blame] | 303 | /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */ |
| 304 | snprintf(comment, sizeof(comment), |
| 305 | "%u-bit %s, converted by %s@%s from OpenSSH", |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 306 | key_size(k), key_type(k), |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 307 | pw->pw_name, hostname); |
Darren Tucker | d04758d | 2010-01-12 19:41:57 +1100 | [diff] [blame] | 308 | |
| 309 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN); |
| 310 | fprintf(stdout, "Comment: \"%s\"\n", comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 311 | dump_base64(stdout, blob, len); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 312 | fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END); |
Ben Lindstrom | 46c264f | 2001-04-24 16:56:58 +0000 | [diff] [blame] | 313 | key_free(k); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 314 | free(blob); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 315 | exit(0); |
| 316 | } |
| 317 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 318 | static void |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 319 | do_convert_to_pkcs8(Key *k) |
| 320 | { |
| 321 | switch (key_type_plain(k->type)) { |
Damien Miller | a563cce | 2012-04-22 11:07:28 +1000 | [diff] [blame] | 322 | case KEY_RSA1: |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 323 | case KEY_RSA: |
| 324 | if (!PEM_write_RSA_PUBKEY(stdout, k->rsa)) |
| 325 | fatal("PEM_write_RSA_PUBKEY failed"); |
| 326 | break; |
| 327 | case KEY_DSA: |
| 328 | if (!PEM_write_DSA_PUBKEY(stdout, k->dsa)) |
| 329 | fatal("PEM_write_DSA_PUBKEY failed"); |
| 330 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 331 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 332 | case KEY_ECDSA: |
| 333 | if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa)) |
| 334 | fatal("PEM_write_EC_PUBKEY failed"); |
| 335 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 336 | #endif |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 337 | default: |
| 338 | fatal("%s: unsupported key type %s", __func__, key_type(k)); |
| 339 | } |
| 340 | exit(0); |
| 341 | } |
| 342 | |
| 343 | static void |
| 344 | do_convert_to_pem(Key *k) |
| 345 | { |
| 346 | switch (key_type_plain(k->type)) { |
Damien Miller | a563cce | 2012-04-22 11:07:28 +1000 | [diff] [blame] | 347 | case KEY_RSA1: |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 348 | case KEY_RSA: |
| 349 | if (!PEM_write_RSAPublicKey(stdout, k->rsa)) |
| 350 | fatal("PEM_write_RSAPublicKey failed"); |
| 351 | break; |
| 352 | #if notyet /* OpenSSH 0.9.8 lacks this function */ |
| 353 | case KEY_DSA: |
| 354 | if (!PEM_write_DSAPublicKey(stdout, k->dsa)) |
| 355 | fatal("PEM_write_DSAPublicKey failed"); |
| 356 | break; |
| 357 | #endif |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 358 | /* XXX ECDSA? */ |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 359 | default: |
| 360 | fatal("%s: unsupported key type %s", __func__, key_type(k)); |
| 361 | } |
| 362 | exit(0); |
| 363 | } |
| 364 | |
| 365 | static void |
| 366 | do_convert_to(struct passwd *pw) |
| 367 | { |
| 368 | Key *k; |
| 369 | struct stat st; |
| 370 | |
| 371 | if (!have_identity) |
| 372 | ask_filename(pw, "Enter file in which the key is"); |
| 373 | if (stat(identity_file, &st) < 0) |
| 374 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
| 375 | if ((k = key_load_public(identity_file, NULL)) == NULL) { |
| 376 | if ((k = load_identity(identity_file)) == NULL) { |
| 377 | fprintf(stderr, "load failed\n"); |
| 378 | exit(1); |
| 379 | } |
| 380 | } |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 381 | |
| 382 | switch (convert_format) { |
| 383 | case FMT_RFC4716: |
| 384 | do_convert_to_ssh2(pw, k); |
| 385 | break; |
| 386 | case FMT_PKCS8: |
| 387 | do_convert_to_pkcs8(k); |
| 388 | break; |
| 389 | case FMT_PEM: |
| 390 | do_convert_to_pem(k); |
| 391 | break; |
| 392 | default: |
| 393 | fatal("%s: unknown key format %d", __func__, convert_format); |
| 394 | } |
| 395 | exit(0); |
| 396 | } |
| 397 | |
| 398 | static void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 399 | buffer_get_bignum_bits(Buffer *b, BIGNUM *value) |
| 400 | { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 401 | u_int bignum_bits = buffer_get_int(b); |
| 402 | u_int bytes = (bignum_bits + 7) / 8; |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 403 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 404 | if (buffer_len(b) < bytes) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 405 | fatal("buffer_get_bignum_bits: input buffer too small: " |
| 406 | "need %d have %d", bytes, buffer_len(b)); |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 407 | if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL) |
| 408 | fatal("buffer_get_bignum_bits: BN_bin2bn failed"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 409 | buffer_consume(b, bytes); |
| 410 | } |
| 411 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 412 | static Key * |
Ben Lindstrom | 90fd814 | 2002-02-26 18:09:42 +0000 | [diff] [blame] | 413 | do_convert_private_ssh2_from_blob(u_char *blob, u_int blen) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 414 | { |
| 415 | Buffer b; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 416 | Key *key = NULL; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 417 | char *type, *cipher; |
Damien Miller | 8f9cd70 | 2014-04-20 13:00:11 +1000 | [diff] [blame] | 418 | u_char *sig = NULL, data[] = "abcde12345"; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 419 | int magic, rlen, ktype, i1, i2, i3, i4; |
| 420 | u_int slen; |
| 421 | u_long e; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 422 | |
| 423 | buffer_init(&b); |
| 424 | buffer_append(&b, blob, blen); |
| 425 | |
Darren Tucker | 82a3d2b | 2007-02-19 22:10:25 +1100 | [diff] [blame] | 426 | magic = buffer_get_int(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 427 | if (magic != SSH_COM_PRIVATE_KEY_MAGIC) { |
| 428 | error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC); |
| 429 | buffer_free(&b); |
| 430 | return NULL; |
| 431 | } |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 432 | i1 = buffer_get_int(&b); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 433 | type = buffer_get_string(&b, NULL); |
| 434 | cipher = buffer_get_string(&b, NULL); |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 435 | i2 = buffer_get_int(&b); |
| 436 | i3 = buffer_get_int(&b); |
| 437 | i4 = buffer_get_int(&b); |
Damien Miller | 8016390 | 2007-01-05 16:30:16 +1100 | [diff] [blame] | 438 | debug("ignore (%d %d %d %d)", i1, i2, i3, i4); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 439 | if (strcmp(cipher, "none") != 0) { |
| 440 | error("unsupported cipher %s", cipher); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 441 | free(cipher); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 442 | buffer_free(&b); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 443 | free(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 444 | return NULL; |
| 445 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 446 | free(cipher); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 447 | |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 448 | if (strstr(type, "dsa")) { |
| 449 | ktype = KEY_DSA; |
| 450 | } else if (strstr(type, "rsa")) { |
| 451 | ktype = KEY_RSA; |
| 452 | } else { |
Darren Tucker | 7cfeecf | 2005-01-20 10:56:31 +1100 | [diff] [blame] | 453 | buffer_free(&b); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 454 | free(type); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 455 | return NULL; |
| 456 | } |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 457 | key = key_new_private(ktype); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 458 | free(type); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 459 | |
| 460 | switch (key->type) { |
| 461 | case KEY_DSA: |
| 462 | buffer_get_bignum_bits(&b, key->dsa->p); |
| 463 | buffer_get_bignum_bits(&b, key->dsa->g); |
| 464 | buffer_get_bignum_bits(&b, key->dsa->q); |
| 465 | buffer_get_bignum_bits(&b, key->dsa->pub_key); |
| 466 | buffer_get_bignum_bits(&b, key->dsa->priv_key); |
| 467 | break; |
| 468 | case KEY_RSA: |
Darren Tucker | 82a3d2b | 2007-02-19 22:10:25 +1100 | [diff] [blame] | 469 | e = buffer_get_char(&b); |
Ben Lindstrom | 34f9188 | 2001-06-25 04:47:54 +0000 | [diff] [blame] | 470 | debug("e %lx", e); |
| 471 | if (e < 30) { |
| 472 | e <<= 8; |
| 473 | e += buffer_get_char(&b); |
| 474 | debug("e %lx", e); |
| 475 | e <<= 8; |
| 476 | e += buffer_get_char(&b); |
| 477 | debug("e %lx", e); |
| 478 | } |
| 479 | if (!BN_set_word(key->rsa->e, e)) { |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 480 | buffer_free(&b); |
| 481 | key_free(key); |
| 482 | return NULL; |
| 483 | } |
| 484 | buffer_get_bignum_bits(&b, key->rsa->d); |
| 485 | buffer_get_bignum_bits(&b, key->rsa->n); |
| 486 | buffer_get_bignum_bits(&b, key->rsa->iqmp); |
| 487 | buffer_get_bignum_bits(&b, key->rsa->q); |
| 488 | buffer_get_bignum_bits(&b, key->rsa->p); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 489 | if (rsa_generate_additional_parameters(key->rsa) != 0) |
| 490 | fatal("%s: rsa_generate_additional_parameters " |
| 491 | "error", __func__); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 492 | break; |
| 493 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 494 | rlen = buffer_len(&b); |
Ben Lindstrom | 1c37c6a | 2001-12-06 18:00:18 +0000 | [diff] [blame] | 495 | if (rlen != 0) |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 496 | error("do_convert_private_ssh2_from_blob: " |
| 497 | "remaining bytes in key blob %d", rlen); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 498 | buffer_free(&b); |
Ben Lindstrom | d09fcf5 | 2001-03-29 00:29:54 +0000 | [diff] [blame] | 499 | |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 500 | /* try the key */ |
| 501 | key_sign(key, &sig, &slen, data, sizeof(data)); |
| 502 | key_verify(key, sig, slen, data, sizeof(data)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 503 | free(sig); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 504 | return key; |
| 505 | } |
| 506 | |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 507 | static int |
| 508 | get_line(FILE *fp, char *line, size_t len) |
| 509 | { |
| 510 | int c; |
| 511 | size_t pos = 0; |
| 512 | |
| 513 | line[0] = '\0'; |
| 514 | while ((c = fgetc(fp)) != EOF) { |
| 515 | if (pos >= len - 1) { |
| 516 | fprintf(stderr, "input line too long.\n"); |
| 517 | exit(1); |
| 518 | } |
Damien Miller | 9096740 | 2006-03-26 14:07:26 +1100 | [diff] [blame] | 519 | switch (c) { |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 520 | case '\r': |
| 521 | c = fgetc(fp); |
| 522 | if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) { |
| 523 | fprintf(stderr, "unget: %s\n", strerror(errno)); |
| 524 | exit(1); |
| 525 | } |
| 526 | return pos; |
| 527 | case '\n': |
| 528 | return pos; |
| 529 | } |
| 530 | line[pos++] = c; |
| 531 | line[pos] = '\0'; |
| 532 | } |
Damien Miller | 6c7439f | 2007-01-05 16:29:55 +1100 | [diff] [blame] | 533 | /* We reached EOF */ |
| 534 | return -1; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 535 | } |
| 536 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 537 | static void |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 538 | do_convert_from_ssh2(struct passwd *pw, Key **k, int *private) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 539 | { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 540 | int blen; |
Ben Lindstrom | 155b981 | 2002-04-02 20:26:26 +0000 | [diff] [blame] | 541 | u_int len; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 542 | char line[1024]; |
Ben Lindstrom | 9e0ddd4 | 2001-09-18 05:41:19 +0000 | [diff] [blame] | 543 | u_char blob[8096]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 544 | char encoded[8096]; |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 545 | int escaped = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 546 | FILE *fp; |
| 547 | |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 548 | if ((fp = fopen(identity_file, "r")) == NULL) |
| 549 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 550 | encoded[0] = '\0'; |
Damien Miller | 8056a9d | 2006-03-15 12:05:40 +1100 | [diff] [blame] | 551 | while ((blen = get_line(fp, line, sizeof(line))) != -1) { |
Damien Miller | 746d1a6 | 2013-07-18 16:13:02 +1000 | [diff] [blame] | 552 | if (blen > 0 && line[blen - 1] == '\\') |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 553 | escaped++; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 554 | if (strncmp(line, "----", 4) == 0 || |
| 555 | strstr(line, ": ") != NULL) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 556 | if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL) |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 557 | *private = 1; |
Ben Lindstrom | e586c4c | 2001-06-25 05:04:58 +0000 | [diff] [blame] | 558 | if (strstr(line, " END ") != NULL) { |
| 559 | break; |
| 560 | } |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 561 | /* fprintf(stderr, "ignore: %s", line); */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 562 | continue; |
| 563 | } |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 564 | if (escaped) { |
| 565 | escaped--; |
Ben Lindstrom | 3035860 | 2001-04-24 16:59:28 +0000 | [diff] [blame] | 566 | /* fprintf(stderr, "escaped: %s", line); */ |
Damien Miller | 30c3d42 | 2000-05-09 11:02:59 +1000 | [diff] [blame] | 567 | continue; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 568 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 569 | strlcat(encoded, line, sizeof(encoded)); |
| 570 | } |
Ben Lindstrom | 155b981 | 2002-04-02 20:26:26 +0000 | [diff] [blame] | 571 | len = strlen(encoded); |
| 572 | if (((len % 4) == 3) && |
| 573 | (encoded[len-1] == '=') && |
| 574 | (encoded[len-2] == '=') && |
| 575 | (encoded[len-3] == '=')) |
| 576 | encoded[len-3] = '\0'; |
Damien Miller | 4a8ed54 | 2002-01-22 23:33:31 +1100 | [diff] [blame] | 577 | blen = uudecode(encoded, blob, sizeof(blob)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 578 | if (blen < 0) { |
| 579 | fprintf(stderr, "uudecode failed.\n"); |
| 580 | exit(1); |
| 581 | } |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 582 | *k = *private ? |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 583 | do_convert_private_ssh2_from_blob(blob, blen) : |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 584 | key_from_blob(blob, blen); |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 585 | if (*k == NULL) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 586 | fprintf(stderr, "decode blob failed.\n"); |
| 587 | exit(1); |
| 588 | } |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 589 | fclose(fp); |
| 590 | } |
| 591 | |
| 592 | static void |
| 593 | do_convert_from_pkcs8(Key **k, int *private) |
| 594 | { |
| 595 | EVP_PKEY *pubkey; |
| 596 | FILE *fp; |
| 597 | |
| 598 | if ((fp = fopen(identity_file, "r")) == NULL) |
| 599 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
| 600 | if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) { |
| 601 | fatal("%s: %s is not a recognised public key format", __func__, |
| 602 | identity_file); |
| 603 | } |
| 604 | fclose(fp); |
| 605 | switch (EVP_PKEY_type(pubkey->type)) { |
| 606 | case EVP_PKEY_RSA: |
| 607 | *k = key_new(KEY_UNSPEC); |
| 608 | (*k)->type = KEY_RSA; |
| 609 | (*k)->rsa = EVP_PKEY_get1_RSA(pubkey); |
| 610 | break; |
| 611 | case EVP_PKEY_DSA: |
| 612 | *k = key_new(KEY_UNSPEC); |
| 613 | (*k)->type = KEY_DSA; |
| 614 | (*k)->dsa = EVP_PKEY_get1_DSA(pubkey); |
| 615 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 616 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 617 | case EVP_PKEY_EC: |
| 618 | *k = key_new(KEY_UNSPEC); |
| 619 | (*k)->type = KEY_ECDSA; |
| 620 | (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey); |
Damien Miller | b472a90 | 2010-11-05 10:19:49 +1100 | [diff] [blame] | 621 | (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 622 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 623 | #endif |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 624 | default: |
| 625 | fatal("%s: unsupported pubkey type %d", __func__, |
| 626 | EVP_PKEY_type(pubkey->type)); |
| 627 | } |
| 628 | EVP_PKEY_free(pubkey); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | static void |
| 633 | do_convert_from_pem(Key **k, int *private) |
| 634 | { |
| 635 | FILE *fp; |
| 636 | RSA *rsa; |
| 637 | #ifdef notyet |
| 638 | DSA *dsa; |
| 639 | #endif |
| 640 | |
| 641 | if ((fp = fopen(identity_file, "r")) == NULL) |
| 642 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
| 643 | if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) { |
| 644 | *k = key_new(KEY_UNSPEC); |
| 645 | (*k)->type = KEY_RSA; |
| 646 | (*k)->rsa = rsa; |
| 647 | fclose(fp); |
| 648 | return; |
| 649 | } |
| 650 | #if notyet /* OpenSSH 0.9.8 lacks this function */ |
| 651 | rewind(fp); |
| 652 | if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) { |
| 653 | *k = key_new(KEY_UNSPEC); |
| 654 | (*k)->type = KEY_DSA; |
| 655 | (*k)->dsa = dsa; |
| 656 | fclose(fp); |
| 657 | return; |
| 658 | } |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 659 | /* XXX ECDSA */ |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 660 | #endif |
| 661 | fatal("%s: unrecognised raw private key format", __func__); |
| 662 | } |
| 663 | |
| 664 | static void |
| 665 | do_convert_from(struct passwd *pw) |
| 666 | { |
| 667 | Key *k = NULL; |
Damien Miller | 844cccf | 2010-08-03 16:03:29 +1000 | [diff] [blame] | 668 | int private = 0, ok = 0; |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 669 | struct stat st; |
| 670 | |
| 671 | if (!have_identity) |
| 672 | ask_filename(pw, "Enter file in which the key is"); |
| 673 | if (stat(identity_file, &st) < 0) |
| 674 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
| 675 | |
| 676 | switch (convert_format) { |
| 677 | case FMT_RFC4716: |
| 678 | do_convert_from_ssh2(pw, &k, &private); |
| 679 | break; |
| 680 | case FMT_PKCS8: |
| 681 | do_convert_from_pkcs8(&k, &private); |
| 682 | break; |
| 683 | case FMT_PEM: |
| 684 | do_convert_from_pem(&k, &private); |
| 685 | break; |
| 686 | default: |
| 687 | fatal("%s: unknown key format %d", __func__, convert_format); |
| 688 | } |
| 689 | |
| 690 | if (!private) |
| 691 | ok = key_write(k, stdout); |
| 692 | if (ok) |
| 693 | fprintf(stdout, "\n"); |
| 694 | else { |
| 695 | switch (k->type) { |
| 696 | case KEY_DSA: |
| 697 | ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, |
| 698 | NULL, 0, NULL, NULL); |
| 699 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 700 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 701 | case KEY_ECDSA: |
| 702 | ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL, |
| 703 | NULL, 0, NULL, NULL); |
| 704 | break; |
Damien Miller | 6af914a | 2010-09-10 11:39:26 +1000 | [diff] [blame] | 705 | #endif |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 706 | case KEY_RSA: |
| 707 | ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, |
| 708 | NULL, 0, NULL, NULL); |
| 709 | break; |
| 710 | default: |
| 711 | fatal("%s: unsupported key type %s", __func__, |
| 712 | key_type(k)); |
| 713 | } |
| 714 | } |
| 715 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 716 | if (!ok) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 717 | fprintf(stderr, "key write failed\n"); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 718 | exit(1); |
| 719 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 720 | key_free(k); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 721 | exit(0); |
| 722 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 723 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 724 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 725 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 726 | do_print_public(struct passwd *pw) |
| 727 | { |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 728 | Key *prv; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 729 | struct stat st; |
| 730 | |
| 731 | if (!have_identity) |
| 732 | ask_filename(pw, "Enter file in which the key is"); |
| 733 | if (stat(identity_file, &st) < 0) { |
| 734 | perror(identity_file); |
| 735 | exit(1); |
| 736 | } |
Ben Lindstrom | d78ae76 | 2001-06-05 20:35:09 +0000 | [diff] [blame] | 737 | prv = load_identity(identity_file); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 738 | if (prv == NULL) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 739 | fprintf(stderr, "load failed\n"); |
| 740 | exit(1); |
| 741 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 742 | if (!key_write(prv, stdout)) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 743 | fprintf(stderr, "key_write failed"); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 744 | key_free(prv); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 745 | fprintf(stdout, "\n"); |
| 746 | exit(0); |
| 747 | } |
| 748 | |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 749 | static void |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 750 | do_download(struct passwd *pw) |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 751 | { |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 752 | #ifdef ENABLE_PKCS11 |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 753 | Key **keys = NULL; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 754 | int i, nkeys; |
Damien Miller | ec77c95 | 2013-01-09 15:58:00 +1100 | [diff] [blame] | 755 | enum fp_rep rep; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 756 | int fptype; |
Damien Miller | ec77c95 | 2013-01-09 15:58:00 +1100 | [diff] [blame] | 757 | char *fp, *ra; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 758 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 759 | fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash; |
| 760 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT; |
Damien Miller | 1422c08 | 2013-01-09 16:44:54 +1100 | [diff] [blame] | 761 | |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 762 | pkcs11_init(0); |
| 763 | nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys); |
| 764 | if (nkeys <= 0) |
| 765 | fatal("cannot read public key from pkcs11"); |
| 766 | for (i = 0; i < nkeys; i++) { |
Damien Miller | ec77c95 | 2013-01-09 15:58:00 +1100 | [diff] [blame] | 767 | if (print_fingerprint) { |
| 768 | fp = key_fingerprint(keys[i], fptype, rep); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 769 | ra = key_fingerprint(keys[i], fingerprint_hash, |
Damien Miller | ec77c95 | 2013-01-09 15:58:00 +1100 | [diff] [blame] | 770 | SSH_FP_RANDOMART); |
| 771 | printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]), |
| 772 | fp, key_type(keys[i])); |
| 773 | if (log_level >= SYSLOG_LEVEL_VERBOSE) |
| 774 | printf("%s\n", ra); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 775 | free(ra); |
| 776 | free(fp); |
Damien Miller | ec77c95 | 2013-01-09 15:58:00 +1100 | [diff] [blame] | 777 | } else { |
| 778 | key_write(keys[i], stdout); |
| 779 | fprintf(stdout, "\n"); |
| 780 | } |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 781 | key_free(keys[i]); |
Ben Lindstrom | 0936a5b | 2002-03-26 03:17:42 +0000 | [diff] [blame] | 782 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 783 | free(keys); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 784 | pkcs11_terminate(); |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 785 | exit(0); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 786 | #else |
| 787 | fatal("no pkcs11 support"); |
| 788 | #endif /* ENABLE_PKCS11 */ |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 789 | } |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 790 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 791 | static void |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 792 | do_fingerprint(struct passwd *pw) |
| 793 | { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 794 | FILE *f; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 795 | Key *public; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 796 | char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra; |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 797 | int i, skip = 0, num = 0, invalid = 1; |
Ben Lindstrom | 65366a8 | 2001-12-06 16:32:47 +0000 | [diff] [blame] | 798 | enum fp_rep rep; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 799 | int fptype; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 800 | struct stat st; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 801 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 802 | fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash; |
| 803 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 804 | if (!have_identity) |
| 805 | ask_filename(pw, "Enter file in which the key is"); |
| 806 | if (stat(identity_file, &st) < 0) { |
| 807 | perror(identity_file); |
| 808 | exit(1); |
| 809 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 810 | public = key_load_public(identity_file, &comment); |
| 811 | if (public != NULL) { |
| 812 | fp = key_fingerprint(public, fptype, rep); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 813 | ra = key_fingerprint(public, fingerprint_hash, |
| 814 | SSH_FP_RANDOMART); |
Darren Tucker | b68fb4a | 2008-06-13 08:57:27 +1000 | [diff] [blame] | 815 | printf("%u %s %s (%s)\n", key_size(public), fp, comment, |
| 816 | key_type(public)); |
Darren Tucker | 35c4553 | 2008-06-13 04:43:15 +1000 | [diff] [blame] | 817 | if (log_level >= SYSLOG_LEVEL_VERBOSE) |
| 818 | printf("%s\n", ra); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 819 | key_free(public); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 820 | free(comment); |
| 821 | free(ra); |
| 822 | free(fp); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 823 | exit(0); |
| 824 | } |
Damien Miller | 40b5985 | 2006-06-13 13:00:25 +1000 | [diff] [blame] | 825 | if (comment) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 826 | free(comment); |
Damien Miller | 40b5985 | 2006-06-13 13:00:25 +1000 | [diff] [blame] | 827 | comment = NULL; |
| 828 | } |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 829 | |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 830 | if ((f = fopen(identity_file, "r")) == NULL) |
| 831 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 832 | |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 833 | while (fgets(line, sizeof(line), f)) { |
| 834 | if ((cp = strchr(line, '\n')) == NULL) { |
| 835 | error("line %d too long: %.40s...", |
| 836 | num + 1, line); |
| 837 | skip = 1; |
| 838 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 839 | } |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 840 | num++; |
| 841 | if (skip) { |
| 842 | skip = 0; |
| 843 | continue; |
| 844 | } |
| 845 | *cp = '\0'; |
| 846 | |
| 847 | /* Skip leading whitespace, empty and comment lines. */ |
| 848 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 849 | ; |
| 850 | if (!*cp || *cp == '\n' || *cp == '#') |
| 851 | continue; |
| 852 | i = strtol(cp, &ep, 10); |
| 853 | if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) { |
| 854 | int quoted = 0; |
| 855 | comment = cp; |
| 856 | for (; *cp && (quoted || (*cp != ' ' && |
| 857 | *cp != '\t')); cp++) { |
| 858 | if (*cp == '\\' && cp[1] == '"') |
| 859 | cp++; /* Skip both */ |
| 860 | else if (*cp == '"') |
| 861 | quoted = !quoted; |
| 862 | } |
| 863 | if (!*cp) |
| 864 | continue; |
| 865 | *cp++ = '\0'; |
| 866 | } |
| 867 | ep = cp; |
| 868 | public = key_new(KEY_RSA1); |
| 869 | if (key_read(public, &cp) != 1) { |
| 870 | cp = ep; |
| 871 | key_free(public); |
| 872 | public = key_new(KEY_UNSPEC); |
| 873 | if (key_read(public, &cp) != 1) { |
| 874 | key_free(public); |
| 875 | continue; |
| 876 | } |
| 877 | } |
| 878 | comment = *cp ? cp : comment; |
| 879 | fp = key_fingerprint(public, fptype, rep); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 880 | ra = key_fingerprint(public, fingerprint_hash, |
| 881 | SSH_FP_RANDOMART); |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 882 | printf("%u %s %s (%s)\n", key_size(public), fp, |
| 883 | comment ? comment : "no comment", key_type(public)); |
| 884 | if (log_level >= SYSLOG_LEVEL_VERBOSE) |
| 885 | printf("%s\n", ra); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 886 | free(ra); |
| 887 | free(fp); |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 888 | key_free(public); |
| 889 | invalid = 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 890 | } |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 891 | fclose(f); |
| 892 | |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 893 | if (invalid) { |
Damien Miller | eb5fec6 | 2001-11-12 10:52:44 +1100 | [diff] [blame] | 894 | printf("%s is not a public key file.\n", identity_file); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 895 | exit(1); |
| 896 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 897 | exit(0); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 898 | } |
| 899 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 900 | static void |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 901 | do_gen_all_hostkeys(struct passwd *pw) |
| 902 | { |
| 903 | struct { |
| 904 | char *key_type; |
| 905 | char *key_type_display; |
| 906 | char *path; |
| 907 | } key_types[] = { |
| 908 | { "rsa1", "RSA1", _PATH_HOST_KEY_FILE }, |
| 909 | { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE }, |
| 910 | { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE }, |
Damien Miller | b56e493 | 2012-02-06 07:41:27 +1100 | [diff] [blame] | 911 | #ifdef OPENSSL_HAS_ECC |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 912 | { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE }, |
Damien Miller | b56e493 | 2012-02-06 07:41:27 +1100 | [diff] [blame] | 913 | #endif |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 914 | { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE }, |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 915 | { NULL, NULL, NULL } |
| 916 | }; |
| 917 | |
| 918 | int first = 0; |
| 919 | struct stat st; |
| 920 | Key *private, *public; |
| 921 | char comment[1024]; |
| 922 | int i, type, fd; |
| 923 | FILE *f; |
| 924 | |
| 925 | for (i = 0; key_types[i].key_type; i++) { |
| 926 | if (stat(key_types[i].path, &st) == 0) |
| 927 | continue; |
| 928 | if (errno != ENOENT) { |
| 929 | printf("Could not stat %s: %s", key_types[i].path, |
| 930 | strerror(errno)); |
| 931 | first = 0; |
| 932 | continue; |
| 933 | } |
| 934 | |
| 935 | if (first == 0) { |
| 936 | first = 1; |
| 937 | printf("%s: generating new host keys: ", __progname); |
| 938 | } |
| 939 | printf("%s ", key_types[i].key_type_display); |
| 940 | fflush(stdout); |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 941 | type = key_type_from_name(key_types[i].key_type); |
| 942 | strlcpy(identity_file, key_types[i].path, sizeof(identity_file)); |
| 943 | bits = 0; |
| 944 | type_bits_valid(type, &bits); |
| 945 | private = key_generate(type, bits); |
| 946 | if (private == NULL) { |
| 947 | fprintf(stderr, "key_generate failed\n"); |
| 948 | first = 0; |
| 949 | continue; |
| 950 | } |
| 951 | public = key_from_private(private); |
| 952 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, |
| 953 | hostname); |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 954 | if (!key_save_private(private, identity_file, "", comment, |
| 955 | use_new_format, new_format_cipher, rounds)) { |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 956 | printf("Saving the key failed: %s.\n", identity_file); |
| 957 | key_free(private); |
| 958 | key_free(public); |
| 959 | first = 0; |
| 960 | continue; |
| 961 | } |
| 962 | key_free(private); |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 963 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
| 964 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 965 | if (fd == -1) { |
| 966 | printf("Could not save your public key in %s\n", |
| 967 | identity_file); |
| 968 | key_free(public); |
| 969 | first = 0; |
| 970 | continue; |
| 971 | } |
| 972 | f = fdopen(fd, "w"); |
| 973 | if (f == NULL) { |
| 974 | printf("fdopen %s failed\n", identity_file); |
doug@openbsd.org | 7df8818 | 2014-08-21 01:08:52 +0000 | [diff] [blame] | 975 | close(fd); |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 976 | key_free(public); |
| 977 | first = 0; |
| 978 | continue; |
| 979 | } |
| 980 | if (!key_write(public, f)) { |
| 981 | fprintf(stderr, "write key failed\n"); |
doug@openbsd.org | 7df8818 | 2014-08-21 01:08:52 +0000 | [diff] [blame] | 982 | fclose(f); |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 983 | key_free(public); |
| 984 | first = 0; |
| 985 | continue; |
| 986 | } |
| 987 | fprintf(f, " %s\n", comment); |
| 988 | fclose(f); |
| 989 | key_free(public); |
| 990 | |
| 991 | } |
| 992 | if (first != 0) |
| 993 | printf("\n"); |
| 994 | } |
| 995 | |
| 996 | static void |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 997 | printhost(FILE *f, const char *name, Key *public, int ca, int revoked, int hash) |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 998 | { |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 999 | if (print_fingerprint) { |
| 1000 | enum fp_rep rep; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 1001 | int fptype; |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 1002 | char *fp, *ra; |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 1003 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 1004 | fptype = print_bubblebabble ? |
| 1005 | SSH_DIGEST_SHA1 : fingerprint_hash; |
| 1006 | rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT; |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 1007 | fp = key_fingerprint(public, fptype, rep); |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 1008 | ra = key_fingerprint(public, fingerprint_hash, |
| 1009 | SSH_FP_RANDOMART); |
Damien Miller | 81dec05 | 2008-07-14 11:28:29 +1000 | [diff] [blame] | 1010 | printf("%u %s %s (%s)\n", key_size(public), fp, name, |
| 1011 | key_type(public)); |
| 1012 | if (log_level >= SYSLOG_LEVEL_VERBOSE) |
| 1013 | printf("%s\n", ra); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1014 | free(ra); |
| 1015 | free(fp); |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 1016 | } else { |
| 1017 | if (hash && (name = host_hash(name, NULL, 0)) == NULL) |
| 1018 | fatal("hash_host failed"); |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1019 | fprintf(f, "%s%s%s ", ca ? CA_MARKER " " : "", |
| 1020 | revoked ? REVOKE_MARKER " " : "" , name); |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 1021 | if (!key_write(public, f)) |
| 1022 | fatal("key_write failed"); |
| 1023 | fprintf(f, "\n"); |
| 1024 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1025 | } |
| 1026 | |
| 1027 | static void |
| 1028 | do_known_hosts(struct passwd *pw, const char *name) |
| 1029 | { |
| 1030 | FILE *in, *out = stdout; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1031 | Key *pub; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1032 | char *cp, *cp2, *kp, *kp2; |
| 1033 | char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN]; |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 1034 | int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0; |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1035 | int ca, revoked; |
Damien Miller | 6608548 | 2013-09-14 09:45:03 +1000 | [diff] [blame] | 1036 | int found_key = 0; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1037 | |
| 1038 | if (!have_identity) { |
| 1039 | cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid); |
| 1040 | if (strlcpy(identity_file, cp, sizeof(identity_file)) >= |
| 1041 | sizeof(identity_file)) |
| 1042 | fatal("Specified known hosts path too long"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1043 | free(cp); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1044 | have_identity = 1; |
| 1045 | } |
| 1046 | if ((in = fopen(identity_file, "r")) == NULL) |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 1047 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1048 | |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1049 | /* XXX this code is a mess; refactor -djm */ |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1050 | /* |
| 1051 | * Find hosts goes to stdout, hash and deletions happen in-place |
| 1052 | * A corner case is ssh-keygen -HF foo, which should go to stdout |
| 1053 | */ |
| 1054 | if (!find_host && (hash_hosts || delete_host)) { |
| 1055 | if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) || |
| 1056 | strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) || |
| 1057 | strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) || |
| 1058 | strlcat(old, ".old", sizeof(old)) >= sizeof(old)) |
| 1059 | fatal("known_hosts path too long"); |
| 1060 | umask(077); |
| 1061 | if ((c = mkstemp(tmp)) == -1) |
| 1062 | fatal("mkstemp: %s", strerror(errno)); |
| 1063 | if ((out = fdopen(c, "w")) == NULL) { |
| 1064 | c = errno; |
| 1065 | unlink(tmp); |
| 1066 | fatal("fdopen: %s", strerror(c)); |
| 1067 | } |
| 1068 | inplace = 1; |
| 1069 | } |
| 1070 | |
| 1071 | while (fgets(line, sizeof(line), in)) { |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 1072 | if ((cp = strchr(line, '\n')) == NULL) { |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 1073 | error("line %d too long: %.40s...", num + 1, line); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1074 | skip = 1; |
| 1075 | invalid = 1; |
| 1076 | continue; |
| 1077 | } |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 1078 | num++; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1079 | if (skip) { |
| 1080 | skip = 0; |
| 1081 | continue; |
| 1082 | } |
Damien Miller | 0f4ed69 | 2007-10-26 14:26:32 +1000 | [diff] [blame] | 1083 | *cp = '\0'; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1084 | |
| 1085 | /* Skip leading whitespace, empty and comment lines. */ |
| 1086 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 1087 | ; |
| 1088 | if (!*cp || *cp == '\n' || *cp == '#') { |
| 1089 | if (inplace) |
| 1090 | fprintf(out, "%s\n", cp); |
| 1091 | continue; |
| 1092 | } |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1093 | /* Check whether this is a CA key or revocation marker */ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1094 | if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 && |
| 1095 | (cp[sizeof(CA_MARKER) - 1] == ' ' || |
| 1096 | cp[sizeof(CA_MARKER) - 1] == '\t')) { |
| 1097 | ca = 1; |
| 1098 | cp += sizeof(CA_MARKER); |
| 1099 | } else |
| 1100 | ca = 0; |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1101 | if (strncasecmp(cp, REVOKE_MARKER, |
| 1102 | sizeof(REVOKE_MARKER) - 1) == 0 && |
| 1103 | (cp[sizeof(REVOKE_MARKER) - 1] == ' ' || |
| 1104 | cp[sizeof(REVOKE_MARKER) - 1] == '\t')) { |
| 1105 | revoked = 1; |
| 1106 | cp += sizeof(REVOKE_MARKER); |
| 1107 | } else |
| 1108 | revoked = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1109 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1110 | /* Find the end of the host name portion. */ |
| 1111 | for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++) |
| 1112 | ; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1113 | |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1114 | if (*kp == '\0' || *(kp + 1) == '\0') { |
| 1115 | error("line %d missing key: %.40s...", |
| 1116 | num, line); |
| 1117 | invalid = 1; |
| 1118 | continue; |
| 1119 | } |
| 1120 | *kp++ = '\0'; |
| 1121 | kp2 = kp; |
| 1122 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1123 | pub = key_new(KEY_RSA1); |
| 1124 | if (key_read(pub, &kp) != 1) { |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1125 | kp = kp2; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1126 | key_free(pub); |
| 1127 | pub = key_new(KEY_UNSPEC); |
| 1128 | if (key_read(pub, &kp) != 1) { |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1129 | error("line %d invalid key: %.40s...", |
| 1130 | num, line); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1131 | key_free(pub); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1132 | invalid = 1; |
| 1133 | continue; |
| 1134 | } |
| 1135 | } |
| 1136 | |
| 1137 | if (*cp == HASH_DELIM) { |
| 1138 | if (find_host || delete_host) { |
| 1139 | cp2 = host_hash(name, cp, strlen(cp)); |
| 1140 | if (cp2 == NULL) { |
| 1141 | error("line %d: invalid hashed " |
| 1142 | "name: %.64s...", num, line); |
| 1143 | invalid = 1; |
| 1144 | continue; |
| 1145 | } |
| 1146 | c = (strcmp(cp2, cp) == 0); |
| 1147 | if (find_host && c) { |
Damien Miller | 6608548 | 2013-09-14 09:45:03 +1000 | [diff] [blame] | 1148 | if (!quiet) |
| 1149 | printf("# Host %s found: " |
| 1150 | "line %d type %s%s\n", name, |
| 1151 | num, key_type(pub), |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1152 | ca ? " (CA key)" : |
| 1153 | revoked? " (revoked)" : ""); |
| 1154 | printhost(out, cp, pub, ca, revoked, 0); |
Damien Miller | 6608548 | 2013-09-14 09:45:03 +1000 | [diff] [blame] | 1155 | found_key = 1; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1156 | } |
Darren Tucker | f09a8a6 | 2012-09-06 21:20:39 +1000 | [diff] [blame] | 1157 | if (delete_host) { |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1158 | if (!c || ca || revoked) { |
| 1159 | printhost(out, cp, pub, |
| 1160 | ca, revoked, 0); |
| 1161 | } else { |
Darren Tucker | f09a8a6 | 2012-09-06 21:20:39 +1000 | [diff] [blame] | 1162 | printf("# Host %s found: " |
| 1163 | "line %d type %s\n", name, |
| 1164 | num, key_type(pub)); |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1165 | } |
Darren Tucker | f09a8a6 | 2012-09-06 21:20:39 +1000 | [diff] [blame] | 1166 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1167 | } else if (hash_hosts) |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1168 | printhost(out, cp, pub, ca, revoked, 0); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1169 | } else { |
| 1170 | if (find_host || delete_host) { |
| 1171 | c = (match_hostname(name, cp, |
| 1172 | strlen(cp)) == 1); |
| 1173 | if (find_host && c) { |
Damien Miller | 6608548 | 2013-09-14 09:45:03 +1000 | [diff] [blame] | 1174 | if (!quiet) |
| 1175 | printf("# Host %s found: " |
| 1176 | "line %d type %s%s\n", name, |
| 1177 | num, key_type(pub), |
| 1178 | ca ? " (CA key)" : ""); |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1179 | printhost(out, name, pub, ca, revoked, |
| 1180 | hash_hosts && !(ca || revoked)); |
Damien Miller | 6608548 | 2013-09-14 09:45:03 +1000 | [diff] [blame] | 1181 | found_key = 1; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1182 | } |
Darren Tucker | f09a8a6 | 2012-09-06 21:20:39 +1000 | [diff] [blame] | 1183 | if (delete_host) { |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1184 | if (!c || ca || revoked) { |
| 1185 | printhost(out, cp, pub, |
| 1186 | ca, revoked, 0); |
| 1187 | } else { |
Darren Tucker | f09a8a6 | 2012-09-06 21:20:39 +1000 | [diff] [blame] | 1188 | printf("# Host %s found: " |
| 1189 | "line %d type %s\n", name, |
| 1190 | num, key_type(pub)); |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1191 | } |
Darren Tucker | f09a8a6 | 2012-09-06 21:20:39 +1000 | [diff] [blame] | 1192 | } |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1193 | } else if (hash_hosts && (ca || revoked)) { |
| 1194 | /* Don't hash CA and revoked keys' hostnames */ |
| 1195 | printhost(out, cp, pub, ca, revoked, 0); |
| 1196 | has_unhashed = 1; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1197 | } else if (hash_hosts) { |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1198 | /* Hash each hostname separately */ |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 1199 | for (cp2 = strsep(&cp, ","); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1200 | cp2 != NULL && *cp2 != '\0'; |
Damien Miller | 89eac80 | 2005-03-02 12:33:04 +1100 | [diff] [blame] | 1201 | cp2 = strsep(&cp, ",")) { |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1202 | if (strcspn(cp2, "*?!") != |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1203 | strlen(cp2)) { |
Damien Miller | 89eac80 | 2005-03-02 12:33:04 +1100 | [diff] [blame] | 1204 | fprintf(stderr, "Warning: " |
| 1205 | "ignoring host name with " |
| 1206 | "metacharacters: %.64s\n", |
| 1207 | cp2); |
Damien Miller | 4a1d3d5 | 2014-07-03 21:24:40 +1000 | [diff] [blame] | 1208 | printhost(out, cp2, pub, ca, |
| 1209 | revoked, 0); |
| 1210 | has_unhashed = 1; |
| 1211 | } else { |
| 1212 | printhost(out, cp2, pub, ca, |
| 1213 | revoked, 1); |
| 1214 | } |
Damien Miller | 89eac80 | 2005-03-02 12:33:04 +1100 | [diff] [blame] | 1215 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1216 | } |
| 1217 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1218 | key_free(pub); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1219 | } |
| 1220 | fclose(in); |
| 1221 | |
| 1222 | if (invalid) { |
Damien Miller | cb2fbb2 | 2008-02-10 22:24:55 +1100 | [diff] [blame] | 1223 | fprintf(stderr, "%s is not a valid known_hosts file.\n", |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1224 | identity_file); |
| 1225 | if (inplace) { |
| 1226 | fprintf(stderr, "Not replacing existing known_hosts " |
Darren Tucker | 9f438a9 | 2005-03-14 23:09:18 +1100 | [diff] [blame] | 1227 | "file because of errors\n"); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1228 | fclose(out); |
| 1229 | unlink(tmp); |
| 1230 | } |
| 1231 | exit(1); |
| 1232 | } |
| 1233 | |
| 1234 | if (inplace) { |
| 1235 | fclose(out); |
| 1236 | |
| 1237 | /* Backup existing file */ |
| 1238 | if (unlink(old) == -1 && errno != ENOENT) |
| 1239 | fatal("unlink %.100s: %s", old, strerror(errno)); |
| 1240 | if (link(identity_file, old) == -1) |
| 1241 | fatal("link %.100s to %.100s: %s", identity_file, old, |
| 1242 | strerror(errno)); |
| 1243 | /* Move new one into place */ |
| 1244 | if (rename(tmp, identity_file) == -1) { |
| 1245 | error("rename\"%s\" to \"%s\": %s", tmp, identity_file, |
| 1246 | strerror(errno)); |
| 1247 | unlink(tmp); |
| 1248 | unlink(old); |
| 1249 | exit(1); |
| 1250 | } |
| 1251 | |
| 1252 | fprintf(stderr, "%s updated.\n", identity_file); |
| 1253 | fprintf(stderr, "Original contents retained as %s\n", old); |
| 1254 | if (has_unhashed) { |
| 1255 | fprintf(stderr, "WARNING: %s contains unhashed " |
| 1256 | "entries\n", old); |
| 1257 | fprintf(stderr, "Delete this file to ensure privacy " |
Damien Miller | 0dc1bef | 2005-07-17 17:22:45 +1000 | [diff] [blame] | 1258 | "of hostnames\n"); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Damien Miller | 6608548 | 2013-09-14 09:45:03 +1000 | [diff] [blame] | 1262 | exit (find_host && !found_key); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1263 | } |
| 1264 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1265 | /* |
| 1266 | * Perform changing a passphrase. The argument is the passwd structure |
| 1267 | * for the current user. |
| 1268 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1269 | static void |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 1270 | do_change_passphrase(struct passwd *pw) |
| 1271 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1272 | char *comment; |
| 1273 | char *old_passphrase, *passphrase1, *passphrase2; |
| 1274 | struct stat st; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1275 | Key *private; |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 1276 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1277 | if (!have_identity) |
| 1278 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1279 | if (stat(identity_file, &st) < 0) { |
| 1280 | perror(identity_file); |
| 1281 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1282 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1283 | /* Try to load the file with empty passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1284 | private = key_load_private(identity_file, "", &comment); |
| 1285 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1286 | if (identity_passphrase) |
| 1287 | old_passphrase = xstrdup(identity_passphrase); |
| 1288 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1289 | old_passphrase = |
| 1290 | read_passphrase("Enter old passphrase: ", |
| 1291 | RP_ALLOW_STDIN); |
| 1292 | private = key_load_private(identity_file, old_passphrase, |
| 1293 | &comment); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1294 | explicit_bzero(old_passphrase, strlen(old_passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1295 | free(old_passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1296 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1297 | printf("Bad passphrase.\n"); |
| 1298 | exit(1); |
| 1299 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1300 | } |
| 1301 | printf("Key has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1302 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1303 | /* Ask the new passphrase (twice). */ |
| 1304 | if (identity_new_passphrase) { |
| 1305 | passphrase1 = xstrdup(identity_new_passphrase); |
| 1306 | passphrase2 = NULL; |
| 1307 | } else { |
| 1308 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1309 | read_passphrase("Enter new passphrase (empty for no " |
| 1310 | "passphrase): ", RP_ALLOW_STDIN); |
| 1311 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1312 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1313 | |
| 1314 | /* Verify that they are the same. */ |
| 1315 | if (strcmp(passphrase1, passphrase2) != 0) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1316 | explicit_bzero(passphrase1, strlen(passphrase1)); |
| 1317 | explicit_bzero(passphrase2, strlen(passphrase2)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1318 | free(passphrase1); |
| 1319 | free(passphrase2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1320 | printf("Pass phrases do not match. Try again.\n"); |
| 1321 | exit(1); |
| 1322 | } |
| 1323 | /* Destroy the other copy. */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1324 | explicit_bzero(passphrase2, strlen(passphrase2)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1325 | free(passphrase2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1326 | } |
| 1327 | |
| 1328 | /* Save the file using the new passphrase. */ |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 1329 | if (!key_save_private(private, identity_file, passphrase1, comment, |
| 1330 | use_new_format, new_format_cipher, rounds)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 1331 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1332 | explicit_bzero(passphrase1, strlen(passphrase1)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1333 | free(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1334 | key_free(private); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1335 | free(comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1336 | exit(1); |
| 1337 | } |
| 1338 | /* Destroy the passphrase and the copy of the key in memory. */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1339 | explicit_bzero(passphrase1, strlen(passphrase1)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1340 | free(passphrase1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1341 | key_free(private); /* Destroys contents */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1342 | free(comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1343 | |
| 1344 | printf("Your identification has been saved with the new passphrase.\n"); |
| 1345 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1346 | } |
| 1347 | |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1348 | /* |
| 1349 | * Print the SSHFP RR. |
| 1350 | */ |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1351 | static int |
| 1352 | do_print_resource_record(struct passwd *pw, char *fname, char *hname) |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1353 | { |
| 1354 | Key *public; |
| 1355 | char *comment = NULL; |
| 1356 | struct stat st; |
| 1357 | |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1358 | if (fname == NULL) |
Damien Miller | 5bb8833 | 2013-07-18 16:13:37 +1000 | [diff] [blame] | 1359 | fatal("%s: no filename", __func__); |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1360 | if (stat(fname, &st) < 0) { |
| 1361 | if (errno == ENOENT) |
| 1362 | return 0; |
| 1363 | perror(fname); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1364 | exit(1); |
| 1365 | } |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1366 | public = key_load_public(fname, &comment); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1367 | if (public != NULL) { |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 1368 | export_dns_rr(hname, public, stdout, print_generic); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1369 | key_free(public); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1370 | free(comment); |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1371 | return 1; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1372 | } |
| 1373 | if (comment) |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1374 | free(comment); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1375 | |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1376 | printf("failed to read v2 public key from %s.\n", fname); |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1377 | exit(1); |
| 1378 | } |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1379 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1380 | /* |
| 1381 | * Change the comment of a private key file. |
| 1382 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1383 | static void |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1384 | do_change_comment(struct passwd *pw) |
| 1385 | { |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1386 | char new_comment[1024], *comment, *passphrase; |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1387 | Key *private; |
| 1388 | Key *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1389 | struct stat st; |
| 1390 | FILE *f; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1391 | int fd; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1392 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1393 | if (!have_identity) |
| 1394 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1395 | if (stat(identity_file, &st) < 0) { |
| 1396 | perror(identity_file); |
| 1397 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1398 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1399 | private = key_load_private(identity_file, "", &comment); |
| 1400 | if (private == NULL) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1401 | if (identity_passphrase) |
| 1402 | passphrase = xstrdup(identity_passphrase); |
| 1403 | else if (identity_new_passphrase) |
| 1404 | passphrase = xstrdup(identity_new_passphrase); |
| 1405 | else |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1406 | passphrase = read_passphrase("Enter passphrase: ", |
| 1407 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1408 | /* Try to load using the passphrase. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1409 | private = key_load_private(identity_file, passphrase, &comment); |
| 1410 | if (private == NULL) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1411 | explicit_bzero(passphrase, strlen(passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1412 | free(passphrase); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1413 | printf("Bad passphrase.\n"); |
| 1414 | exit(1); |
| 1415 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1416 | } else { |
| 1417 | passphrase = xstrdup(""); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1418 | } |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1419 | if (private->type != KEY_RSA1) { |
| 1420 | fprintf(stderr, "Comments are only supported for RSA1 keys.\n"); |
| 1421 | key_free(private); |
| 1422 | exit(1); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 1423 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1424 | printf("Key now has comment '%s'\n", comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1425 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1426 | if (identity_comment) { |
| 1427 | strlcpy(new_comment, identity_comment, sizeof(new_comment)); |
| 1428 | } else { |
| 1429 | printf("Enter new comment: "); |
| 1430 | fflush(stdout); |
| 1431 | if (!fgets(new_comment, sizeof(new_comment), stdin)) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1432 | explicit_bzero(passphrase, strlen(passphrase)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1433 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1434 | exit(1); |
| 1435 | } |
Damien Miller | 14b017d | 2007-09-17 16:09:15 +1000 | [diff] [blame] | 1436 | new_comment[strcspn(new_comment, "\n")] = '\0'; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1437 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1438 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1439 | /* Save the file using the new passphrase. */ |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 1440 | if (!key_save_private(private, identity_file, passphrase, new_comment, |
| 1441 | use_new_format, new_format_cipher, rounds)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 1442 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1443 | explicit_bzero(passphrase, strlen(passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1444 | free(passphrase); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1445 | key_free(private); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1446 | free(comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1447 | exit(1); |
| 1448 | } |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 1449 | explicit_bzero(passphrase, strlen(passphrase)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1450 | free(passphrase); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1451 | public = key_from_private(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1452 | key_free(private); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1453 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1454 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1455 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1456 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1457 | printf("Could not save your public key in %s\n", identity_file); |
| 1458 | exit(1); |
| 1459 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1460 | f = fdopen(fd, "w"); |
| 1461 | if (f == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1462 | printf("fdopen %s failed\n", identity_file); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1463 | exit(1); |
| 1464 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1465 | if (!key_write(public, f)) |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1466 | fprintf(stderr, "write key failed\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1467 | key_free(public); |
| 1468 | fprintf(f, " %s\n", new_comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1469 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1470 | |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1471 | free(comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1472 | |
| 1473 | printf("The comment in your key file has been changed.\n"); |
| 1474 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1475 | } |
| 1476 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1477 | static const char * |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1478 | fmt_validity(u_int64_t valid_from, u_int64_t valid_to) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1479 | { |
| 1480 | char from[32], to[32]; |
| 1481 | static char ret[64]; |
| 1482 | time_t tt; |
| 1483 | struct tm *tm; |
| 1484 | |
| 1485 | *from = *to = '\0'; |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1486 | if (valid_from == 0 && valid_to == 0xffffffffffffffffULL) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1487 | return "forever"; |
| 1488 | |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1489 | if (valid_from != 0) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1490 | /* XXX revisit INT_MAX in 2038 :) */ |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1491 | tt = valid_from > INT_MAX ? INT_MAX : valid_from; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1492 | tm = localtime(&tt); |
| 1493 | strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm); |
| 1494 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1495 | if (valid_to != 0xffffffffffffffffULL) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1496 | /* XXX revisit INT_MAX in 2038 :) */ |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1497 | tt = valid_to > INT_MAX ? INT_MAX : valid_to; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1498 | tm = localtime(&tt); |
| 1499 | strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm); |
| 1500 | } |
| 1501 | |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1502 | if (valid_from == 0) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1503 | snprintf(ret, sizeof(ret), "before %s", to); |
| 1504 | return ret; |
| 1505 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1506 | if (valid_to == 0xffffffffffffffffULL) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1507 | snprintf(ret, sizeof(ret), "after %s", from); |
| 1508 | return ret; |
| 1509 | } |
| 1510 | |
| 1511 | snprintf(ret, sizeof(ret), "from %s to %s", from, to); |
| 1512 | return ret; |
| 1513 | } |
| 1514 | |
| 1515 | static void |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1516 | add_flag_option(Buffer *c, const char *name) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1517 | { |
| 1518 | debug3("%s: %s", __func__, name); |
| 1519 | buffer_put_cstring(c, name); |
| 1520 | buffer_put_string(c, NULL, 0); |
| 1521 | } |
| 1522 | |
| 1523 | static void |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1524 | add_string_option(Buffer *c, const char *name, const char *value) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1525 | { |
| 1526 | Buffer b; |
| 1527 | |
| 1528 | debug3("%s: %s=%s", __func__, name, value); |
| 1529 | buffer_init(&b); |
| 1530 | buffer_put_cstring(&b, value); |
| 1531 | |
| 1532 | buffer_put_cstring(c, name); |
| 1533 | buffer_put_string(c, buffer_ptr(&b), buffer_len(&b)); |
| 1534 | |
| 1535 | buffer_free(&b); |
| 1536 | } |
| 1537 | |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1538 | #define OPTIONS_CRITICAL 1 |
| 1539 | #define OPTIONS_EXTENSIONS 2 |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1540 | static void |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1541 | prepare_options_buf(Buffer *c, int which) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1542 | { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1543 | buffer_clear(c); |
Damien Miller | 1da6388 | 2010-08-05 13:03:51 +1000 | [diff] [blame] | 1544 | if ((which & OPTIONS_CRITICAL) != 0 && |
| 1545 | certflags_command != NULL) |
| 1546 | add_string_option(c, "force-command", certflags_command); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1547 | if ((which & OPTIONS_EXTENSIONS) != 0 && |
Damien Miller | 2ce12ef | 2011-05-05 14:17:18 +1000 | [diff] [blame] | 1548 | (certflags_flags & CERTOPT_X_FWD) != 0) |
| 1549 | add_flag_option(c, "permit-X11-forwarding"); |
| 1550 | if ((which & OPTIONS_EXTENSIONS) != 0 && |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1551 | (certflags_flags & CERTOPT_AGENT_FWD) != 0) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1552 | add_flag_option(c, "permit-agent-forwarding"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1553 | if ((which & OPTIONS_EXTENSIONS) != 0 && |
| 1554 | (certflags_flags & CERTOPT_PORT_FWD) != 0) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1555 | add_flag_option(c, "permit-port-forwarding"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1556 | if ((which & OPTIONS_EXTENSIONS) != 0 && |
| 1557 | (certflags_flags & CERTOPT_PTY) != 0) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1558 | add_flag_option(c, "permit-pty"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1559 | if ((which & OPTIONS_EXTENSIONS) != 0 && |
| 1560 | (certflags_flags & CERTOPT_USER_RC) != 0) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1561 | add_flag_option(c, "permit-user-rc"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1562 | if ((which & OPTIONS_CRITICAL) != 0 && |
| 1563 | certflags_src_addr != NULL) |
| 1564 | add_string_option(c, "source-address", certflags_src_addr); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1565 | } |
| 1566 | |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 1567 | static Key * |
| 1568 | load_pkcs11_key(char *path) |
| 1569 | { |
| 1570 | #ifdef ENABLE_PKCS11 |
| 1571 | Key **keys = NULL, *public, *private = NULL; |
| 1572 | int i, nkeys; |
| 1573 | |
| 1574 | if ((public = key_load_public(path, NULL)) == NULL) |
| 1575 | fatal("Couldn't load CA public key \"%s\"", path); |
| 1576 | |
| 1577 | nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys); |
| 1578 | debug3("%s: %d keys", __func__, nkeys); |
| 1579 | if (nkeys <= 0) |
| 1580 | fatal("cannot read public key from pkcs11"); |
| 1581 | for (i = 0; i < nkeys; i++) { |
| 1582 | if (key_equal_public(public, keys[i])) { |
| 1583 | private = keys[i]; |
| 1584 | continue; |
| 1585 | } |
| 1586 | key_free(keys[i]); |
| 1587 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1588 | free(keys); |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 1589 | key_free(public); |
| 1590 | return private; |
| 1591 | #else |
| 1592 | fatal("no pkcs11 support"); |
| 1593 | #endif /* ENABLE_PKCS11 */ |
| 1594 | } |
| 1595 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1596 | static void |
| 1597 | do_ca_sign(struct passwd *pw, int argc, char **argv) |
| 1598 | { |
| 1599 | int i, fd; |
| 1600 | u_int n; |
| 1601 | Key *ca, *public; |
| 1602 | char *otmp, *tmp, *cp, *out, *comment, **plist = NULL; |
| 1603 | FILE *f; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1604 | int v00 = 0; /* legacy keys */ |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1605 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1606 | if (key_type_name != NULL) { |
| 1607 | switch (key_type_from_name(key_type_name)) { |
| 1608 | case KEY_RSA_CERT_V00: |
| 1609 | case KEY_DSA_CERT_V00: |
| 1610 | v00 = 1; |
| 1611 | break; |
| 1612 | case KEY_UNSPEC: |
| 1613 | if (strcasecmp(key_type_name, "v00") == 0) { |
| 1614 | v00 = 1; |
| 1615 | break; |
| 1616 | } else if (strcasecmp(key_type_name, "v01") == 0) |
| 1617 | break; |
| 1618 | /* FALLTHROUGH */ |
| 1619 | default: |
| 1620 | fprintf(stderr, "unknown key type %s\n", key_type_name); |
| 1621 | exit(1); |
| 1622 | } |
| 1623 | } |
| 1624 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1625 | #ifdef ENABLE_PKCS11 |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 1626 | pkcs11_init(1); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1627 | #endif |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 1628 | tmp = tilde_expand_filename(ca_key_path, pw->pw_uid); |
| 1629 | if (pkcs11provider != NULL) { |
| 1630 | if ((ca = load_pkcs11_key(tmp)) == NULL) |
| 1631 | fatal("No PKCS#11 key matching %s found", ca_key_path); |
| 1632 | } else if ((ca = load_identity(tmp)) == NULL) |
| 1633 | fatal("Couldn't load CA key \"%s\"", tmp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1634 | free(tmp); |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 1635 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1636 | for (i = 0; i < argc; i++) { |
| 1637 | /* Split list of principals */ |
| 1638 | n = 0; |
| 1639 | if (cert_principals != NULL) { |
| 1640 | otmp = tmp = xstrdup(cert_principals); |
| 1641 | plist = NULL; |
| 1642 | for (; (cp = strsep(&tmp, ",")) != NULL; n++) { |
| 1643 | plist = xrealloc(plist, n + 1, sizeof(*plist)); |
| 1644 | if (*(plist[n] = xstrdup(cp)) == '\0') |
| 1645 | fatal("Empty principal name"); |
| 1646 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1647 | free(otmp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1648 | } |
| 1649 | |
| 1650 | tmp = tilde_expand_filename(argv[i], pw->pw_uid); |
| 1651 | if ((public = key_load_public(tmp, &comment)) == NULL) |
| 1652 | fatal("%s: unable to open \"%s\"", __func__, tmp); |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 1653 | if (public->type != KEY_RSA && public->type != KEY_DSA && |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 1654 | public->type != KEY_ECDSA && public->type != KEY_ED25519) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1655 | fatal("%s: key \"%s\" type %s cannot be certified", |
| 1656 | __func__, tmp, key_type(public)); |
| 1657 | |
| 1658 | /* Prepare certificate to sign */ |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1659 | if (key_to_certified(public, v00) != 0) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1660 | fatal("Could not upgrade key %s to certificate", tmp); |
| 1661 | public->cert->type = cert_key_type; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1662 | public->cert->serial = (u_int64_t)cert_serial; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1663 | public->cert->key_id = xstrdup(cert_key_id); |
| 1664 | public->cert->nprincipals = n; |
| 1665 | public->cert->principals = plist; |
| 1666 | public->cert->valid_after = cert_valid_from; |
| 1667 | public->cert->valid_before = cert_valid_to; |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1668 | if (v00) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1669 | prepare_options_buf(public->cert->critical, |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1670 | OPTIONS_CRITICAL|OPTIONS_EXTENSIONS); |
| 1671 | } else { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1672 | prepare_options_buf(public->cert->critical, |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1673 | OPTIONS_CRITICAL); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1674 | prepare_options_buf(public->cert->extensions, |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1675 | OPTIONS_EXTENSIONS); |
| 1676 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1677 | public->cert->signature_key = key_from_private(ca); |
| 1678 | |
| 1679 | if (key_certify(public, ca) != 0) |
| 1680 | fatal("Couldn't not certify key %s", tmp); |
| 1681 | |
| 1682 | if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0) |
| 1683 | *cp = '\0'; |
| 1684 | xasprintf(&out, "%s-cert.pub", tmp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1685 | free(tmp); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1686 | |
| 1687 | if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) |
| 1688 | fatal("Could not open \"%s\" for writing: %s", out, |
| 1689 | strerror(errno)); |
| 1690 | if ((f = fdopen(fd, "w")) == NULL) |
| 1691 | fatal("%s: fdopen: %s", __func__, strerror(errno)); |
| 1692 | if (!key_write(public, f)) |
| 1693 | fatal("Could not write certified key to %s", out); |
| 1694 | fprintf(f, " %s\n", comment); |
| 1695 | fclose(f); |
| 1696 | |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1697 | if (!quiet) { |
| 1698 | logit("Signed %s key %s: id \"%s\" serial %llu%s%s " |
| 1699 | "valid %s", key_cert_type(public), |
Damien Miller | 821de0a | 2011-01-11 17:20:29 +1100 | [diff] [blame] | 1700 | out, public->cert->key_id, |
| 1701 | (unsigned long long)public->cert->serial, |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1702 | cert_principals != NULL ? " for " : "", |
| 1703 | cert_principals != NULL ? cert_principals : "", |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1704 | fmt_validity(cert_valid_from, cert_valid_to)); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1705 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1706 | |
| 1707 | key_free(public); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1708 | free(out); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1709 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1710 | #ifdef ENABLE_PKCS11 |
Damien Miller | 757f34e | 2010-08-05 13:05:31 +1000 | [diff] [blame] | 1711 | pkcs11_terminate(); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1712 | #endif |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1713 | exit(0); |
| 1714 | } |
| 1715 | |
| 1716 | static u_int64_t |
| 1717 | parse_relative_time(const char *s, time_t now) |
| 1718 | { |
| 1719 | int64_t mul, secs; |
| 1720 | |
| 1721 | mul = *s == '-' ? -1 : 1; |
| 1722 | |
| 1723 | if ((secs = convtime(s + 1)) == -1) |
| 1724 | fatal("Invalid relative certificate time %s", s); |
| 1725 | if (mul == -1 && secs > now) |
| 1726 | fatal("Certificate time %s cannot be represented", s); |
| 1727 | return now + (u_int64_t)(secs * mul); |
| 1728 | } |
| 1729 | |
| 1730 | static u_int64_t |
| 1731 | parse_absolute_time(const char *s) |
| 1732 | { |
| 1733 | struct tm tm; |
| 1734 | time_t tt; |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1735 | char buf[32], *fmt; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1736 | |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1737 | /* |
| 1738 | * POSIX strptime says "The application shall ensure that there |
| 1739 | * is white-space or other non-alphanumeric characters between |
| 1740 | * any two conversion specifications" so arrange things this way. |
| 1741 | */ |
| 1742 | switch (strlen(s)) { |
| 1743 | case 8: |
Damien Miller | 3e1ee49 | 2010-03-08 09:24:11 +1100 | [diff] [blame] | 1744 | fmt = "%Y-%m-%d"; |
| 1745 | snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6); |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1746 | break; |
| 1747 | case 14: |
Damien Miller | 3e1ee49 | 2010-03-08 09:24:11 +1100 | [diff] [blame] | 1748 | fmt = "%Y-%m-%dT%H:%M:%S"; |
| 1749 | snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s", |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1750 | s, s + 4, s + 6, s + 8, s + 10, s + 12); |
| 1751 | break; |
| 1752 | default: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1753 | fatal("Invalid certificate time format %s", s); |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1754 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1755 | |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 1756 | memset(&tm, 0, sizeof(tm)); |
Damien Miller | 2ca342b | 2010-03-03 12:14:15 +1100 | [diff] [blame] | 1757 | if (strptime(buf, fmt, &tm) == NULL) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1758 | fatal("Invalid certificate time %s", s); |
| 1759 | if ((tt = mktime(&tm)) < 0) |
| 1760 | fatal("Certificate time %s cannot be represented", s); |
| 1761 | return (u_int64_t)tt; |
| 1762 | } |
| 1763 | |
| 1764 | static void |
| 1765 | parse_cert_times(char *timespec) |
| 1766 | { |
| 1767 | char *from, *to; |
| 1768 | time_t now = time(NULL); |
| 1769 | int64_t secs; |
| 1770 | |
| 1771 | /* +timespec relative to now */ |
| 1772 | if (*timespec == '+' && strchr(timespec, ':') == NULL) { |
| 1773 | if ((secs = convtime(timespec + 1)) == -1) |
| 1774 | fatal("Invalid relative certificate life %s", timespec); |
| 1775 | cert_valid_to = now + secs; |
| 1776 | /* |
| 1777 | * Backdate certificate one minute to avoid problems on hosts |
| 1778 | * with poorly-synchronised clocks. |
| 1779 | */ |
| 1780 | cert_valid_from = ((now - 59)/ 60) * 60; |
| 1781 | return; |
| 1782 | } |
| 1783 | |
| 1784 | /* |
| 1785 | * from:to, where |
| 1786 | * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS |
| 1787 | * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS |
| 1788 | */ |
| 1789 | from = xstrdup(timespec); |
| 1790 | to = strchr(from, ':'); |
| 1791 | if (to == NULL || from == to || *(to + 1) == '\0') |
Damien Miller | 910f209 | 2010-03-04 14:17:22 +1100 | [diff] [blame] | 1792 | fatal("Invalid certificate life specification %s", timespec); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1793 | *to++ = '\0'; |
| 1794 | |
| 1795 | if (*from == '-' || *from == '+') |
| 1796 | cert_valid_from = parse_relative_time(from, now); |
| 1797 | else |
| 1798 | cert_valid_from = parse_absolute_time(from); |
| 1799 | |
| 1800 | if (*to == '-' || *to == '+') |
Damien Miller | 5b01b0d | 2013-10-23 16:31:31 +1100 | [diff] [blame] | 1801 | cert_valid_to = parse_relative_time(to, now); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1802 | else |
| 1803 | cert_valid_to = parse_absolute_time(to); |
| 1804 | |
| 1805 | if (cert_valid_to <= cert_valid_from) |
| 1806 | fatal("Empty certificate validity interval"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1807 | free(from); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | static void |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1811 | add_cert_option(char *opt) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1812 | { |
| 1813 | char *val; |
| 1814 | |
Damien Miller | 044f4a6 | 2011-05-05 14:14:08 +1000 | [diff] [blame] | 1815 | if (strcasecmp(opt, "clear") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1816 | certflags_flags = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1817 | else if (strcasecmp(opt, "no-x11-forwarding") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1818 | certflags_flags &= ~CERTOPT_X_FWD; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1819 | else if (strcasecmp(opt, "permit-x11-forwarding") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1820 | certflags_flags |= CERTOPT_X_FWD; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1821 | else if (strcasecmp(opt, "no-agent-forwarding") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1822 | certflags_flags &= ~CERTOPT_AGENT_FWD; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1823 | else if (strcasecmp(opt, "permit-agent-forwarding") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1824 | certflags_flags |= CERTOPT_AGENT_FWD; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1825 | else if (strcasecmp(opt, "no-port-forwarding") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1826 | certflags_flags &= ~CERTOPT_PORT_FWD; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1827 | else if (strcasecmp(opt, "permit-port-forwarding") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1828 | certflags_flags |= CERTOPT_PORT_FWD; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1829 | else if (strcasecmp(opt, "no-pty") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1830 | certflags_flags &= ~CERTOPT_PTY; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1831 | else if (strcasecmp(opt, "permit-pty") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1832 | certflags_flags |= CERTOPT_PTY; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1833 | else if (strcasecmp(opt, "no-user-rc") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1834 | certflags_flags &= ~CERTOPT_USER_RC; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1835 | else if (strcasecmp(opt, "permit-user-rc") == 0) |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1836 | certflags_flags |= CERTOPT_USER_RC; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1837 | else if (strncasecmp(opt, "force-command=", 14) == 0) { |
| 1838 | val = opt + 14; |
| 1839 | if (*val == '\0') |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1840 | fatal("Empty force-command option"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1841 | if (certflags_command != NULL) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1842 | fatal("force-command already specified"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1843 | certflags_command = xstrdup(val); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1844 | } else if (strncasecmp(opt, "source-address=", 15) == 0) { |
| 1845 | val = opt + 15; |
| 1846 | if (*val == '\0') |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1847 | fatal("Empty source-address option"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1848 | if (certflags_src_addr != NULL) |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1849 | fatal("source-address already specified"); |
| 1850 | if (addr_match_cidr_list(NULL, val) != 0) |
| 1851 | fatal("Invalid source-address list"); |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 1852 | certflags_src_addr = xstrdup(val); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1853 | } else |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1854 | fatal("Unsupported certificate option \"%s\"", opt); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1855 | } |
| 1856 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1857 | static void |
Damien Miller | d834d35 | 2010-06-26 09:48:02 +1000 | [diff] [blame] | 1858 | show_options(const Buffer *optbuf, int v00, int in_critical) |
| 1859 | { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 1860 | char *name, *arg; |
| 1861 | const u_char *data; |
Damien Miller | d834d35 | 2010-06-26 09:48:02 +1000 | [diff] [blame] | 1862 | u_int dlen; |
| 1863 | Buffer options, option; |
| 1864 | |
| 1865 | buffer_init(&options); |
| 1866 | buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf)); |
| 1867 | |
| 1868 | buffer_init(&option); |
| 1869 | while (buffer_len(&options) != 0) { |
| 1870 | name = buffer_get_string(&options, NULL); |
| 1871 | data = buffer_get_string_ptr(&options, &dlen); |
| 1872 | buffer_append(&option, data, dlen); |
| 1873 | printf(" %s", name); |
| 1874 | if ((v00 || !in_critical) && |
| 1875 | (strcmp(name, "permit-X11-forwarding") == 0 || |
| 1876 | strcmp(name, "permit-agent-forwarding") == 0 || |
| 1877 | strcmp(name, "permit-port-forwarding") == 0 || |
| 1878 | strcmp(name, "permit-pty") == 0 || |
| 1879 | strcmp(name, "permit-user-rc") == 0)) |
| 1880 | printf("\n"); |
| 1881 | else if ((v00 || in_critical) && |
| 1882 | (strcmp(name, "force-command") == 0 || |
| 1883 | strcmp(name, "source-address") == 0)) { |
Damien Miller | 633de33 | 2014-05-15 13:48:26 +1000 | [diff] [blame] | 1884 | arg = buffer_get_cstring(&option, NULL); |
| 1885 | printf(" %s\n", arg); |
| 1886 | free(arg); |
Damien Miller | d834d35 | 2010-06-26 09:48:02 +1000 | [diff] [blame] | 1887 | } else { |
| 1888 | printf(" UNKNOWN OPTION (len %u)\n", |
| 1889 | buffer_len(&option)); |
| 1890 | buffer_clear(&option); |
| 1891 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 1892 | free(name); |
Damien Miller | d834d35 | 2010-06-26 09:48:02 +1000 | [diff] [blame] | 1893 | if (buffer_len(&option) != 0) |
| 1894 | fatal("Option corrupt: extra data at end"); |
| 1895 | } |
| 1896 | buffer_free(&option); |
| 1897 | buffer_free(&options); |
| 1898 | } |
| 1899 | |
| 1900 | static void |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1901 | do_show_cert(struct passwd *pw) |
| 1902 | { |
| 1903 | Key *key; |
| 1904 | struct stat st; |
| 1905 | char *key_fp, *ca_fp; |
Damien Miller | d834d35 | 2010-06-26 09:48:02 +1000 | [diff] [blame] | 1906 | u_int i, v00; |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1907 | |
| 1908 | if (!have_identity) |
| 1909 | ask_filename(pw, "Enter file in which the key is"); |
Damien Miller | ba3420a | 2010-06-26 09:39:07 +1000 | [diff] [blame] | 1910 | if (stat(identity_file, &st) < 0) |
| 1911 | fatal("%s: %s: %s", __progname, identity_file, strerror(errno)); |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1912 | if ((key = key_load_public(identity_file, NULL)) == NULL) |
| 1913 | fatal("%s is not a public key", identity_file); |
| 1914 | if (!key_is_cert(key)) |
| 1915 | fatal("%s is not a certificate", identity_file); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1916 | v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00; |
| 1917 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 1918 | key_fp = key_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1919 | ca_fp = key_fingerprint(key->cert->signature_key, |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 1920 | fingerprint_hash, SSH_FP_DEFAULT); |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1921 | |
| 1922 | printf("%s:\n", identity_file); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1923 | printf(" Type: %s %s certificate\n", key_ssh_name(key), |
| 1924 | key_cert_type(key)); |
| 1925 | printf(" Public key: %s %s\n", key_type(key), key_fp); |
| 1926 | printf(" Signing CA: %s %s\n", |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1927 | key_type(key->cert->signature_key), ca_fp); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1928 | printf(" Key ID: \"%s\"\n", key->cert->key_id); |
Damien Miller | 821de0a | 2011-01-11 17:20:29 +1100 | [diff] [blame] | 1929 | if (!v00) { |
| 1930 | printf(" Serial: %llu\n", |
| 1931 | (unsigned long long)key->cert->serial); |
| 1932 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1933 | printf(" Valid: %s\n", |
| 1934 | fmt_validity(key->cert->valid_after, key->cert->valid_before)); |
| 1935 | printf(" Principals: "); |
| 1936 | if (key->cert->nprincipals == 0) |
| 1937 | printf("(none)\n"); |
| 1938 | else { |
| 1939 | for (i = 0; i < key->cert->nprincipals; i++) |
| 1940 | printf("\n %s", |
| 1941 | key->cert->principals[i]); |
| 1942 | printf("\n"); |
| 1943 | } |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1944 | printf(" Critical Options: "); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1945 | if (buffer_len(key->cert->critical) == 0) |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1946 | printf("(none)\n"); |
| 1947 | else { |
| 1948 | printf("\n"); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1949 | show_options(key->cert->critical, v00, 1); |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1950 | } |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1951 | if (!v00) { |
| 1952 | printf(" Extensions: "); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1953 | if (buffer_len(key->cert->extensions) == 0) |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1954 | printf("(none)\n"); |
| 1955 | else { |
| 1956 | printf("\n"); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1957 | show_options(key->cert->extensions, v00, 0); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 1958 | } |
| 1959 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1960 | exit(0); |
| 1961 | } |
| 1962 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 1963 | #ifdef WITH_OPENSSL |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 1964 | static void |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 1965 | load_krl(const char *path, struct ssh_krl **krlp) |
| 1966 | { |
| 1967 | Buffer krlbuf; |
| 1968 | int fd; |
| 1969 | |
| 1970 | buffer_init(&krlbuf); |
| 1971 | if ((fd = open(path, O_RDONLY)) == -1) |
| 1972 | fatal("open %s: %s", path, strerror(errno)); |
| 1973 | if (!key_load_file(fd, path, &krlbuf)) |
| 1974 | fatal("Unable to load KRL"); |
| 1975 | close(fd); |
| 1976 | /* XXX check sigs */ |
| 1977 | if (ssh_krl_from_blob(&krlbuf, krlp, NULL, 0) != 0 || |
| 1978 | *krlp == NULL) |
| 1979 | fatal("Invalid KRL file"); |
| 1980 | buffer_free(&krlbuf); |
| 1981 | } |
| 1982 | |
| 1983 | static void |
| 1984 | update_krl_from_file(struct passwd *pw, const char *file, const Key *ca, |
| 1985 | struct ssh_krl *krl) |
| 1986 | { |
| 1987 | Key *key = NULL; |
| 1988 | u_long lnum = 0; |
| 1989 | char *path, *cp, *ep, line[SSH_MAX_PUBKEY_BYTES]; |
| 1990 | unsigned long long serial, serial2; |
| 1991 | int i, was_explicit_key, was_sha1, r; |
| 1992 | FILE *krl_spec; |
| 1993 | |
| 1994 | path = tilde_expand_filename(file, pw->pw_uid); |
| 1995 | if (strcmp(path, "-") == 0) { |
| 1996 | krl_spec = stdin; |
| 1997 | free(path); |
| 1998 | path = xstrdup("(standard input)"); |
| 1999 | } else if ((krl_spec = fopen(path, "r")) == NULL) |
| 2000 | fatal("fopen %s: %s", path, strerror(errno)); |
| 2001 | |
| 2002 | if (!quiet) |
| 2003 | printf("Revoking from %s\n", path); |
| 2004 | while (read_keyfile_line(krl_spec, path, line, sizeof(line), |
| 2005 | &lnum) == 0) { |
| 2006 | was_explicit_key = was_sha1 = 0; |
| 2007 | cp = line + strspn(line, " \t"); |
| 2008 | /* Trim trailing space, comments and strip \n */ |
| 2009 | for (i = 0, r = -1; cp[i] != '\0'; i++) { |
| 2010 | if (cp[i] == '#' || cp[i] == '\n') { |
| 2011 | cp[i] = '\0'; |
| 2012 | break; |
| 2013 | } |
| 2014 | if (cp[i] == ' ' || cp[i] == '\t') { |
| 2015 | /* Remember the start of a span of whitespace */ |
| 2016 | if (r == -1) |
| 2017 | r = i; |
| 2018 | } else |
| 2019 | r = -1; |
| 2020 | } |
| 2021 | if (r != -1) |
| 2022 | cp[r] = '\0'; |
| 2023 | if (*cp == '\0') |
| 2024 | continue; |
| 2025 | if (strncasecmp(cp, "serial:", 7) == 0) { |
| 2026 | if (ca == NULL) { |
Damien Miller | d234afb | 2013-08-21 02:42:58 +1000 | [diff] [blame] | 2027 | fatal("revoking certificates by serial number " |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2028 | "requires specification of a CA key"); |
| 2029 | } |
| 2030 | cp += 7; |
| 2031 | cp = cp + strspn(cp, " \t"); |
| 2032 | errno = 0; |
| 2033 | serial = strtoull(cp, &ep, 0); |
| 2034 | if (*cp == '\0' || (*ep != '\0' && *ep != '-')) |
| 2035 | fatal("%s:%lu: invalid serial \"%s\"", |
| 2036 | path, lnum, cp); |
| 2037 | if (errno == ERANGE && serial == ULLONG_MAX) |
| 2038 | fatal("%s:%lu: serial out of range", |
| 2039 | path, lnum); |
| 2040 | serial2 = serial; |
| 2041 | if (*ep == '-') { |
| 2042 | cp = ep + 1; |
| 2043 | errno = 0; |
| 2044 | serial2 = strtoull(cp, &ep, 0); |
| 2045 | if (*cp == '\0' || *ep != '\0') |
| 2046 | fatal("%s:%lu: invalid serial \"%s\"", |
| 2047 | path, lnum, cp); |
| 2048 | if (errno == ERANGE && serial2 == ULLONG_MAX) |
| 2049 | fatal("%s:%lu: serial out of range", |
| 2050 | path, lnum); |
| 2051 | if (serial2 <= serial) |
| 2052 | fatal("%s:%lu: invalid serial range " |
| 2053 | "%llu:%llu", path, lnum, |
| 2054 | (unsigned long long)serial, |
| 2055 | (unsigned long long)serial2); |
| 2056 | } |
| 2057 | if (ssh_krl_revoke_cert_by_serial_range(krl, |
| 2058 | ca, serial, serial2) != 0) { |
| 2059 | fatal("%s: revoke serial failed", |
| 2060 | __func__); |
| 2061 | } |
| 2062 | } else if (strncasecmp(cp, "id:", 3) == 0) { |
| 2063 | if (ca == NULL) { |
Damien Miller | d5d9d7b | 2013-08-21 02:43:27 +1000 | [diff] [blame] | 2064 | fatal("revoking certificates by key ID " |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2065 | "requires specification of a CA key"); |
| 2066 | } |
| 2067 | cp += 3; |
| 2068 | cp = cp + strspn(cp, " \t"); |
| 2069 | if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0) |
| 2070 | fatal("%s: revoke key ID failed", __func__); |
| 2071 | } else { |
| 2072 | if (strncasecmp(cp, "key:", 4) == 0) { |
| 2073 | cp += 4; |
| 2074 | cp = cp + strspn(cp, " \t"); |
| 2075 | was_explicit_key = 1; |
| 2076 | } else if (strncasecmp(cp, "sha1:", 5) == 0) { |
| 2077 | cp += 5; |
| 2078 | cp = cp + strspn(cp, " \t"); |
| 2079 | was_sha1 = 1; |
| 2080 | } else { |
| 2081 | /* |
| 2082 | * Just try to process the line as a key. |
| 2083 | * Parsing will fail if it isn't. |
| 2084 | */ |
| 2085 | } |
| 2086 | if ((key = key_new(KEY_UNSPEC)) == NULL) |
| 2087 | fatal("key_new"); |
| 2088 | if (key_read(key, &cp) != 1) |
| 2089 | fatal("%s:%lu: invalid key", path, lnum); |
| 2090 | if (was_explicit_key) |
| 2091 | r = ssh_krl_revoke_key_explicit(krl, key); |
| 2092 | else if (was_sha1) |
| 2093 | r = ssh_krl_revoke_key_sha1(krl, key); |
| 2094 | else |
| 2095 | r = ssh_krl_revoke_key(krl, key); |
| 2096 | if (r != 0) |
| 2097 | fatal("%s: revoke key failed", __func__); |
| 2098 | key_free(key); |
| 2099 | } |
| 2100 | } |
| 2101 | if (strcmp(path, "-") != 0) |
| 2102 | fclose(krl_spec); |
Damien Miller | 0d6771b | 2013-04-23 15:23:24 +1000 | [diff] [blame] | 2103 | free(path); |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | static void |
| 2107 | do_gen_krl(struct passwd *pw, int updating, int argc, char **argv) |
| 2108 | { |
| 2109 | struct ssh_krl *krl; |
| 2110 | struct stat sb; |
| 2111 | Key *ca = NULL; |
| 2112 | int fd, i; |
| 2113 | char *tmp; |
| 2114 | Buffer kbuf; |
| 2115 | |
| 2116 | if (*identity_file == '\0') |
| 2117 | fatal("KRL generation requires an output file"); |
| 2118 | if (stat(identity_file, &sb) == -1) { |
| 2119 | if (errno != ENOENT) |
| 2120 | fatal("Cannot access KRL \"%s\": %s", |
| 2121 | identity_file, strerror(errno)); |
| 2122 | if (updating) |
| 2123 | fatal("KRL \"%s\" does not exist", identity_file); |
| 2124 | } |
| 2125 | if (ca_key_path != NULL) { |
| 2126 | tmp = tilde_expand_filename(ca_key_path, pw->pw_uid); |
| 2127 | if ((ca = key_load_public(tmp, NULL)) == NULL) |
| 2128 | fatal("Cannot load CA public key %s", tmp); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2129 | free(tmp); |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2130 | } |
| 2131 | |
| 2132 | if (updating) |
| 2133 | load_krl(identity_file, &krl); |
| 2134 | else if ((krl = ssh_krl_init()) == NULL) |
| 2135 | fatal("couldn't create KRL"); |
| 2136 | |
| 2137 | if (cert_serial != 0) |
| 2138 | ssh_krl_set_version(krl, cert_serial); |
| 2139 | if (identity_comment != NULL) |
| 2140 | ssh_krl_set_comment(krl, identity_comment); |
| 2141 | |
| 2142 | for (i = 0; i < argc; i++) |
| 2143 | update_krl_from_file(pw, argv[i], ca, krl); |
| 2144 | |
| 2145 | buffer_init(&kbuf); |
| 2146 | if (ssh_krl_to_blob(krl, &kbuf, NULL, 0) != 0) |
| 2147 | fatal("Couldn't generate KRL"); |
| 2148 | if ((fd = open(identity_file, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1) |
| 2149 | fatal("open %s: %s", identity_file, strerror(errno)); |
| 2150 | if (atomicio(vwrite, fd, buffer_ptr(&kbuf), buffer_len(&kbuf)) != |
| 2151 | buffer_len(&kbuf)) |
| 2152 | fatal("write %s: %s", identity_file, strerror(errno)); |
| 2153 | close(fd); |
| 2154 | buffer_free(&kbuf); |
| 2155 | ssh_krl_free(krl); |
Damien Miller | 0d6771b | 2013-04-23 15:23:24 +1000 | [diff] [blame] | 2156 | if (ca != NULL) |
| 2157 | key_free(ca); |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | static void |
| 2161 | do_check_krl(struct passwd *pw, int argc, char **argv) |
| 2162 | { |
| 2163 | int i, r, ret = 0; |
| 2164 | char *comment; |
| 2165 | struct ssh_krl *krl; |
| 2166 | Key *k; |
| 2167 | |
| 2168 | if (*identity_file == '\0') |
| 2169 | fatal("KRL checking requires an input file"); |
| 2170 | load_krl(identity_file, &krl); |
| 2171 | for (i = 0; i < argc; i++) { |
| 2172 | if ((k = key_load_public(argv[i], &comment)) == NULL) |
| 2173 | fatal("Cannot load public key %s", argv[i]); |
| 2174 | r = ssh_krl_check_key(krl, k); |
| 2175 | printf("%s%s%s%s: %s\n", argv[i], |
| 2176 | *comment ? " (" : "", comment, *comment ? ")" : "", |
| 2177 | r == 0 ? "ok" : "REVOKED"); |
| 2178 | if (r != 0) |
| 2179 | ret = 1; |
| 2180 | key_free(k); |
| 2181 | free(comment); |
| 2182 | } |
| 2183 | ssh_krl_free(krl); |
| 2184 | exit(ret); |
| 2185 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 2186 | #endif |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2187 | |
| 2188 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 2189 | usage(void) |
| 2190 | { |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 2191 | fprintf(stderr, |
| 2192 | "usage: ssh-keygen [-q] [-b bits] [-t dsa | ecdsa | ed25519 | rsa | rsa1]\n" |
| 2193 | " [-N new_passphrase] [-C comment] [-f output_keyfile]\n" |
| 2194 | " ssh-keygen -p [-P old_passphrase] [-N new_passphrase] [-f keyfile]\n" |
| 2195 | " ssh-keygen -i [-m key_format] [-f input_keyfile]\n" |
| 2196 | " ssh-keygen -e [-m key_format] [-f input_keyfile]\n" |
| 2197 | " ssh-keygen -y [-f input_keyfile]\n" |
| 2198 | " ssh-keygen -c [-P passphrase] [-C comment] [-f keyfile]\n" |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 2199 | " ssh-keygen -l [-E fingerprint_hash] [-f input_keyfile]\n" |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 2200 | " ssh-keygen -B [-f input_keyfile]\n"); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 2201 | #ifdef ENABLE_PKCS11 |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 2202 | fprintf(stderr, |
| 2203 | " ssh-keygen -D pkcs11\n"); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 2204 | #endif |
Damien Miller | f0858de | 2014-04-20 13:01:30 +1000 | [diff] [blame] | 2205 | fprintf(stderr, |
| 2206 | " ssh-keygen -F hostname [-f known_hosts_file] [-l]\n" |
| 2207 | " ssh-keygen -H [-f known_hosts_file]\n" |
| 2208 | " ssh-keygen -R hostname [-f known_hosts_file]\n" |
| 2209 | " ssh-keygen -r hostname [-f input_keyfile] [-g]\n" |
| 2210 | " ssh-keygen -G output_file [-v] [-b bits] [-M memory] [-S start_point]\n" |
| 2211 | " ssh-keygen -T output_file -f input_file [-v] [-a rounds] [-J num_lines]\n" |
| 2212 | " [-j start_line] [-K checkpt] [-W generator]\n" |
| 2213 | " ssh-keygen -s ca_key -I certificate_identity [-h] [-n principals]\n" |
| 2214 | " [-O option] [-V validity_interval] [-z serial_number] file ...\n" |
| 2215 | " ssh-keygen -L [-f input_keyfile]\n" |
| 2216 | " ssh-keygen -A\n" |
| 2217 | " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n" |
| 2218 | " file ...\n" |
| 2219 | " ssh-keygen -Q -f krl_file file ...\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2220 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 2221 | } |
| 2222 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2223 | /* |
| 2224 | * Main program for key management. |
| 2225 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2226 | int |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 2227 | main(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2228 | { |
Damien Miller | a41c8b1 | 2002-01-22 23:05:08 +1100 | [diff] [blame] | 2229 | char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2; |
Damien Miller | 390d056 | 2011-10-18 16:05:19 +1100 | [diff] [blame] | 2230 | char *checkpoint = NULL; |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2231 | char out_file[MAXPATHLEN], *ep, *rr_hostname = NULL; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 2232 | Key *private, *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2233 | struct passwd *pw; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2234 | struct stat st; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 2235 | int opt, type, fd; |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2236 | u_int32_t memory = 0, generator_wanted = 0; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2237 | int do_gen_candidates = 0, do_screen_candidates = 0; |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2238 | int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0; |
Damien Miller | dfceafe | 2012-07-06 13:44:19 +1000 | [diff] [blame] | 2239 | unsigned long start_lineno = 0, lines_to_process = 0; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2240 | BIGNUM *start = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2241 | FILE *f; |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 2242 | const char *errstr; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2243 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2244 | extern int optind; |
| 2245 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2246 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 2247 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 2248 | sanitise_stdfd(); |
| 2249 | |
Darren Tucker | 9ac56e9 | 2007-01-14 10:19:59 +1100 | [diff] [blame] | 2250 | __progname = ssh_get_progname(argv[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 2251 | |
Damien Miller | 4314c2b | 2010-09-10 11:12:09 +1000 | [diff] [blame] | 2252 | OpenSSL_add_all_algorithms(); |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 2253 | log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2254 | |
Kevin Steves | 3a88191 | 2002-07-20 19:05:40 +0000 | [diff] [blame] | 2255 | seed_rng(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2256 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2257 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2258 | pw = getpwuid(getuid()); |
| 2259 | if (!pw) { |
Damien Miller | 3009d3c | 2013-07-20 13:22:31 +1000 | [diff] [blame] | 2260 | printf("No user exists for uid %lu\n", (u_long)getuid()); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2261 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2262 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2263 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 2264 | perror("gethostname"); |
| 2265 | exit(1); |
| 2266 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 2267 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 2268 | /* Remaining characters: UYdw */ |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2269 | while ((opt = getopt(argc, argv, "ABHLQXceghiklopquvxy" |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 2270 | "C:D:E:F:G:I:J:K:M:N:O:P:R:S:T:V:W:Z:" |
| 2271 | "a:b:f:g:j:m:n:r:s:t:z:")) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2272 | switch (opt) { |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 2273 | case 'A': |
| 2274 | gen_all_hostkeys = 1; |
| 2275 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2276 | case 'b': |
Damien Miller | 5773794 | 2010-09-10 11:16:37 +1000 | [diff] [blame] | 2277 | bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr); |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 2278 | if (errstr) |
| 2279 | fatal("Bits has bad value %s (%s)", |
| 2280 | optarg, errstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2281 | break; |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 2282 | case 'E': |
| 2283 | fingerprint_hash = ssh_digest_alg_by_name(optarg); |
| 2284 | if (fingerprint_hash == -1) |
| 2285 | fatal("Invalid hash algorithm \"%s\"", optarg); |
| 2286 | break; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 2287 | case 'F': |
| 2288 | find_host = 1; |
| 2289 | rr_hostname = optarg; |
| 2290 | break; |
| 2291 | case 'H': |
| 2292 | hash_hosts = 1; |
| 2293 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2294 | case 'I': |
| 2295 | cert_key_id = optarg; |
| 2296 | break; |
Damien Miller | dfceafe | 2012-07-06 13:44:19 +1000 | [diff] [blame] | 2297 | case 'J': |
| 2298 | lines_to_process = strtoul(optarg, NULL, 10); |
| 2299 | break; |
| 2300 | case 'j': |
| 2301 | start_lineno = strtoul(optarg, NULL, 10); |
| 2302 | break; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 2303 | case 'R': |
| 2304 | delete_host = 1; |
| 2305 | rr_hostname = optarg; |
| 2306 | break; |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 2307 | case 'L': |
| 2308 | show_cert = 1; |
| 2309 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2310 | case 'l': |
| 2311 | print_fingerprint = 1; |
| 2312 | break; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 2313 | case 'B': |
| 2314 | print_bubblebabble = 1; |
| 2315 | break; |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 2316 | case 'm': |
| 2317 | if (strcasecmp(optarg, "RFC4716") == 0 || |
| 2318 | strcasecmp(optarg, "ssh2") == 0) { |
| 2319 | convert_format = FMT_RFC4716; |
| 2320 | break; |
| 2321 | } |
| 2322 | if (strcasecmp(optarg, "PKCS8") == 0) { |
| 2323 | convert_format = FMT_PKCS8; |
| 2324 | break; |
| 2325 | } |
| 2326 | if (strcasecmp(optarg, "PEM") == 0) { |
| 2327 | convert_format = FMT_PEM; |
| 2328 | break; |
| 2329 | } |
| 2330 | fatal("Unsupported conversion format \"%s\"", optarg); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2331 | case 'n': |
| 2332 | cert_principals = optarg; |
| 2333 | break; |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2334 | case 'o': |
| 2335 | use_new_format = 1; |
| 2336 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2337 | case 'p': |
| 2338 | change_passphrase = 1; |
| 2339 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2340 | case 'c': |
| 2341 | change_comment = 1; |
| 2342 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2343 | case 'f': |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 2344 | if (strlcpy(identity_file, optarg, sizeof(identity_file)) >= |
| 2345 | sizeof(identity_file)) |
| 2346 | fatal("Identity filename too long"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2347 | have_identity = 1; |
| 2348 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 2349 | case 'g': |
| 2350 | print_generic = 1; |
| 2351 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2352 | case 'P': |
| 2353 | identity_passphrase = optarg; |
| 2354 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2355 | case 'N': |
| 2356 | identity_new_passphrase = optarg; |
| 2357 | break; |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2358 | case 'Q': |
| 2359 | check_krl = 1; |
| 2360 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2361 | case 'O': |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 2362 | add_cert_option(optarg); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2363 | break; |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2364 | case 'Z': |
| 2365 | new_format_cipher = optarg; |
| 2366 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2367 | case 'C': |
| 2368 | identity_comment = optarg; |
| 2369 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2370 | case 'q': |
| 2371 | quiet = 1; |
| 2372 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 2373 | case 'e': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2374 | case 'x': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 2375 | /* export key */ |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 2376 | convert_to = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2377 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2378 | case 'h': |
| 2379 | cert_key_type = SSH2_CERT_TYPE_HOST; |
Damien Miller | d0e4a8e | 2010-05-21 14:58:32 +1000 | [diff] [blame] | 2380 | certflags_flags = 0; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2381 | break; |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2382 | case 'k': |
| 2383 | gen_krl = 1; |
| 2384 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 2385 | case 'i': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2386 | case 'X': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 2387 | /* import key */ |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 2388 | convert_from = 1; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2389 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2390 | case 'y': |
| 2391 | print_public = 1; |
| 2392 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2393 | case 's': |
| 2394 | ca_key_path = optarg; |
| 2395 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2396 | case 't': |
| 2397 | key_type_name = optarg; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2398 | break; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 2399 | case 'D': |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 2400 | pkcs11provider = optarg; |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 2401 | break; |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2402 | case 'u': |
| 2403 | update_krl = 1; |
| 2404 | break; |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 2405 | case 'v': |
| 2406 | if (log_level == SYSLOG_LEVEL_INFO) |
| 2407 | log_level = SYSLOG_LEVEL_DEBUG1; |
| 2408 | else { |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 2409 | if (log_level >= SYSLOG_LEVEL_DEBUG1 && |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 2410 | log_level < SYSLOG_LEVEL_DEBUG3) |
| 2411 | log_level++; |
| 2412 | } |
| 2413 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 2414 | case 'r': |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 2415 | rr_hostname = optarg; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 2416 | break; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2417 | case 'W': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 2418 | generator_wanted = (u_int32_t)strtonum(optarg, 1, |
| 2419 | UINT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 2420 | if (errstr) |
| 2421 | fatal("Desired generator has bad value: %s (%s)", |
| 2422 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2423 | break; |
| 2424 | case 'a': |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2425 | rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 2426 | if (errstr) |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2427 | fatal("Invalid number: %s (%s)", |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 2428 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2429 | break; |
| 2430 | case 'M': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 2431 | memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 2432 | if (errstr) |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 2433 | fatal("Memory limit is %s: %s", errstr, optarg); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2434 | break; |
| 2435 | case 'G': |
| 2436 | do_gen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 2437 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 2438 | sizeof(out_file)) |
| 2439 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2440 | break; |
| 2441 | case 'T': |
| 2442 | do_screen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 2443 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 2444 | sizeof(out_file)) |
| 2445 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2446 | break; |
Damien Miller | 390d056 | 2011-10-18 16:05:19 +1100 | [diff] [blame] | 2447 | case 'K': |
| 2448 | if (strlen(optarg) >= MAXPATHLEN) |
| 2449 | fatal("Checkpoint filename too long"); |
| 2450 | checkpoint = xstrdup(optarg); |
| 2451 | break; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2452 | case 'S': |
| 2453 | /* XXX - also compare length against bits */ |
| 2454 | if (BN_hex2bn(&start, optarg) == 0) |
| 2455 | fatal("Invalid start point."); |
| 2456 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2457 | case 'V': |
| 2458 | parse_cert_times(optarg); |
| 2459 | break; |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 2460 | case 'z': |
Damien Miller | 6f3b362 | 2012-11-14 19:04:33 +1100 | [diff] [blame] | 2461 | errno = 0; |
| 2462 | cert_serial = strtoull(optarg, &ep, 10); |
| 2463 | if (*optarg < '0' || *optarg > '9' || *ep != '\0' || |
| 2464 | (errno == ERANGE && cert_serial == ULLONG_MAX)) |
| 2465 | fatal("Invalid serial number \"%s\"", optarg); |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 2466 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2467 | case '?': |
| 2468 | default: |
| 2469 | usage(); |
| 2470 | } |
| 2471 | } |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 2472 | |
| 2473 | /* reinit */ |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 2474 | log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1); |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 2475 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2476 | argv += optind; |
| 2477 | argc -= optind; |
| 2478 | |
| 2479 | if (ca_key_path != NULL) { |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2480 | if (argc < 1 && !gen_krl) { |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2481 | printf("Too few arguments.\n"); |
| 2482 | usage(); |
| 2483 | } |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2484 | } else if (argc > 0 && !gen_krl && !check_krl) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2485 | printf("Too many arguments.\n"); |
| 2486 | usage(); |
| 2487 | } |
| 2488 | if (change_passphrase && change_comment) { |
| 2489 | printf("Can only have one of -p and -c.\n"); |
| 2490 | usage(); |
| 2491 | } |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 2492 | if (print_fingerprint && (delete_host || hash_hosts)) { |
Damien Miller | ec77c95 | 2013-01-09 15:58:00 +1100 | [diff] [blame] | 2493 | printf("Cannot use -l with -H or -R.\n"); |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 2494 | usage(); |
| 2495 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 2496 | #ifdef WITH_OPENSSL |
Damien Miller | f3747bf | 2013-01-18 11:44:04 +1100 | [diff] [blame] | 2497 | if (gen_krl) { |
| 2498 | do_gen_krl(pw, update_krl, argc, argv); |
| 2499 | return (0); |
| 2500 | } |
| 2501 | if (check_krl) { |
| 2502 | do_check_krl(pw, argc, argv); |
| 2503 | return (0); |
| 2504 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 2505 | #endif |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 2506 | if (ca_key_path != NULL) { |
| 2507 | if (cert_key_id == NULL) |
| 2508 | fatal("Must specify key id (-I) when certifying"); |
| 2509 | do_ca_sign(pw, argc, argv); |
| 2510 | } |
Damien Miller | f2b70ca | 2010-03-05 07:39:35 +1100 | [diff] [blame] | 2511 | if (show_cert) |
| 2512 | do_show_cert(pw); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 2513 | if (delete_host || hash_hosts || find_host) |
| 2514 | do_known_hosts(pw, rr_hostname); |
Damien Miller | ec77c95 | 2013-01-09 15:58:00 +1100 | [diff] [blame] | 2515 | if (pkcs11provider != NULL) |
| 2516 | do_download(pw); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 2517 | if (print_fingerprint || print_bubblebabble) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2518 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2519 | if (change_passphrase) |
| 2520 | do_change_passphrase(pw); |
Damien Miller | 4a10d2e | 2002-03-11 22:53:29 +1100 | [diff] [blame] | 2521 | if (change_comment) |
| 2522 | do_change_comment(pw); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 2523 | #ifdef WITH_OPENSSL |
Damien Miller | 44b2504 | 2010-07-02 13:35:01 +1000 | [diff] [blame] | 2524 | if (convert_to) |
| 2525 | do_convert_to(pw); |
| 2526 | if (convert_from) |
| 2527 | do_convert_from(pw); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 2528 | #endif |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2529 | if (print_public) |
| 2530 | do_print_public(pw); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 2531 | if (rr_hostname != NULL) { |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 2532 | unsigned int n = 0; |
| 2533 | |
| 2534 | if (have_identity) { |
| 2535 | n = do_print_resource_record(pw, |
| 2536 | identity_file, rr_hostname); |
| 2537 | if (n == 0) { |
| 2538 | perror(identity_file); |
| 2539 | exit(1); |
| 2540 | } |
| 2541 | exit(0); |
| 2542 | } else { |
| 2543 | |
| 2544 | n += do_print_resource_record(pw, |
| 2545 | _PATH_HOST_RSA_KEY_FILE, rr_hostname); |
| 2546 | n += do_print_resource_record(pw, |
| 2547 | _PATH_HOST_DSA_KEY_FILE, rr_hostname); |
Damien Miller | 3bde12a | 2012-06-20 21:51:11 +1000 | [diff] [blame] | 2548 | n += do_print_resource_record(pw, |
| 2549 | _PATH_HOST_ECDSA_KEY_FILE, rr_hostname); |
Damien Miller | 16cd392 | 2014-05-15 13:45:58 +1000 | [diff] [blame] | 2550 | n += do_print_resource_record(pw, |
| 2551 | _PATH_HOST_ED25519_KEY_FILE, rr_hostname); |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 2552 | if (n == 0) |
| 2553 | fatal("no keys found."); |
| 2554 | exit(0); |
| 2555 | } |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 2556 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2557 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2558 | if (do_gen_candidates) { |
| 2559 | FILE *out = fopen(out_file, "w"); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 2560 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2561 | if (out == NULL) { |
| 2562 | error("Couldn't open modulus candidate file \"%s\": %s", |
| 2563 | out_file, strerror(errno)); |
| 2564 | return (1); |
| 2565 | } |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 2566 | if (bits == 0) |
| 2567 | bits = DEFAULT_BITS; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2568 | if (gen_candidates(out, memory, bits, start) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 2569 | fatal("modulus candidate generation failed"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2570 | |
| 2571 | return (0); |
| 2572 | } |
| 2573 | |
| 2574 | if (do_screen_candidates) { |
| 2575 | FILE *in; |
Damien Miller | 78d2271 | 2013-02-12 11:03:36 +1100 | [diff] [blame] | 2576 | FILE *out = fopen(out_file, "a"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2577 | |
| 2578 | if (have_identity && strcmp(identity_file, "-") != 0) { |
| 2579 | if ((in = fopen(identity_file, "r")) == NULL) { |
| 2580 | fatal("Couldn't open modulus candidate " |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 2581 | "file \"%s\": %s", identity_file, |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2582 | strerror(errno)); |
| 2583 | } |
| 2584 | } else |
| 2585 | in = stdin; |
| 2586 | |
| 2587 | if (out == NULL) { |
| 2588 | fatal("Couldn't open moduli file \"%s\": %s", |
| 2589 | out_file, strerror(errno)); |
| 2590 | } |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2591 | if (prime_test(in, out, rounds == 0 ? 100 : rounds, |
| 2592 | generator_wanted, checkpoint, |
Damien Miller | dfceafe | 2012-07-06 13:44:19 +1000 | [diff] [blame] | 2593 | start_lineno, lines_to_process) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 2594 | fatal("modulus screening failed"); |
Darren Tucker | f4220e6 | 2003-08-21 16:44:07 +1000 | [diff] [blame] | 2595 | return (0); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 2596 | } |
| 2597 | |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 2598 | if (gen_all_hostkeys) { |
| 2599 | do_gen_all_hostkeys(pw); |
| 2600 | return (0); |
| 2601 | } |
| 2602 | |
Damien Miller | f14be5c | 2005-11-05 15:15:49 +1100 | [diff] [blame] | 2603 | if (key_type_name == NULL) |
| 2604 | key_type_name = "rsa"; |
| 2605 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 2606 | type = key_type_from_name(key_type_name); |
Damien Miller | 58f1baf | 2011-05-05 14:06:15 +1000 | [diff] [blame] | 2607 | type_bits_valid(type, &bits); |
| 2608 | |
Darren Tucker | 3af2ac5 | 2005-11-29 13:10:24 +1100 | [diff] [blame] | 2609 | if (!quiet) |
| 2610 | printf("Generating public/private %s key pair.\n", key_type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2611 | private = key_generate(type, bits); |
| 2612 | if (private == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 2613 | fprintf(stderr, "key_generate failed\n"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2614 | exit(1); |
| 2615 | } |
| 2616 | public = key_from_private(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2617 | |
| 2618 | if (!have_identity) |
| 2619 | ask_filename(pw, "Enter file in which to save the key"); |
| 2620 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 2621 | /* Create ~/.ssh directory if it doesn't already exist. */ |
Damien Miller | 50af79b | 2010-05-10 11:52:00 +1000 | [diff] [blame] | 2622 | snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", |
| 2623 | pw->pw_dir, _PATH_SSH_USER_DIR); |
| 2624 | if (strstr(identity_file, dotsshdir) != NULL) { |
| 2625 | if (stat(dotsshdir, &st) < 0) { |
| 2626 | if (errno != ENOENT) { |
| 2627 | error("Could not stat %s: %s", dotsshdir, |
| 2628 | strerror(errno)); |
| 2629 | } else if (mkdir(dotsshdir, 0700) < 0) { |
| 2630 | error("Could not create directory '%s': %s", |
| 2631 | dotsshdir, strerror(errno)); |
| 2632 | } else if (!quiet) |
| 2633 | printf("Created directory '%s'.\n", dotsshdir); |
| 2634 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2635 | } |
| 2636 | /* If the file already exists, ask the user to confirm. */ |
| 2637 | if (stat(identity_file, &st) >= 0) { |
| 2638 | char yesno[3]; |
| 2639 | printf("%s already exists.\n", identity_file); |
| 2640 | printf("Overwrite (y/n)? "); |
| 2641 | fflush(stdout); |
| 2642 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 2643 | exit(1); |
| 2644 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 2645 | exit(1); |
| 2646 | } |
| 2647 | /* Ask for a passphrase (twice). */ |
| 2648 | if (identity_passphrase) |
| 2649 | passphrase1 = xstrdup(identity_passphrase); |
| 2650 | else if (identity_new_passphrase) |
| 2651 | passphrase1 = xstrdup(identity_new_passphrase); |
| 2652 | else { |
| 2653 | passphrase_again: |
| 2654 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 2655 | read_passphrase("Enter passphrase (empty for no " |
| 2656 | "passphrase): ", RP_ALLOW_STDIN); |
| 2657 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
| 2658 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2659 | if (strcmp(passphrase1, passphrase2) != 0) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 2660 | /* |
| 2661 | * The passphrases do not match. Clear them and |
| 2662 | * retry. |
| 2663 | */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 2664 | explicit_bzero(passphrase1, strlen(passphrase1)); |
| 2665 | explicit_bzero(passphrase2, strlen(passphrase2)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2666 | free(passphrase1); |
| 2667 | free(passphrase2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2668 | printf("Passphrases do not match. Try again.\n"); |
| 2669 | goto passphrase_again; |
| 2670 | } |
| 2671 | /* Clear the other copy of the passphrase. */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 2672 | explicit_bzero(passphrase2, strlen(passphrase2)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2673 | free(passphrase2); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2674 | } |
| 2675 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2676 | if (identity_comment) { |
| 2677 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 2678 | } else { |
Darren Tucker | e15fb09 | 2008-11-11 16:31:43 +1100 | [diff] [blame] | 2679 | /* Create default comment field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2680 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 2681 | } |
| 2682 | |
| 2683 | /* Save the key with the given passphrase and comment. */ |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 2684 | if (!key_save_private(private, identity_file, passphrase1, comment, |
| 2685 | use_new_format, new_format_cipher, rounds)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 2686 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 2687 | explicit_bzero(passphrase1, strlen(passphrase1)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2688 | free(passphrase1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2689 | exit(1); |
| 2690 | } |
| 2691 | /* Clear the passphrase. */ |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 2692 | explicit_bzero(passphrase1, strlen(passphrase1)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2693 | free(passphrase1); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2694 | |
| 2695 | /* Clear the private key and the random number generator. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 2696 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2697 | |
| 2698 | if (!quiet) |
| 2699 | printf("Your identification has been saved in %s.\n", identity_file); |
| 2700 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2701 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 2702 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 2703 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2704 | printf("Could not save your public key in %s\n", identity_file); |
| 2705 | exit(1); |
| 2706 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 2707 | f = fdopen(fd, "w"); |
| 2708 | if (f == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 2709 | printf("fdopen %s failed\n", identity_file); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 2710 | exit(1); |
| 2711 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2712 | if (!key_write(public, f)) |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 2713 | fprintf(stderr, "write key failed\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2714 | fprintf(f, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2715 | fclose(f); |
| 2716 | |
| 2717 | if (!quiet) { |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame^] | 2718 | char *fp = key_fingerprint(public, fingerprint_hash, |
| 2719 | SSH_FP_DEFAULT); |
| 2720 | char *ra = key_fingerprint(public, fingerprint_hash, |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 2721 | SSH_FP_RANDOMART); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2722 | printf("Your public key has been saved in %s.\n", |
| 2723 | identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2724 | printf("The key fingerprint is:\n"); |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 2725 | printf("%s %s\n", fp, comment); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 2726 | printf("The key's randomart image is:\n"); |
| 2727 | printf("%s\n", ra); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 2728 | free(ra); |
| 2729 | free(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2730 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2731 | |
| 2732 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2733 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2734 | } |