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