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