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