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