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