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