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