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