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