Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1 | /* $OpenBSD: ssh-keygen.c,v 1.179 2010/02/26 20:29:54 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; |
| 1246 | |
| 1247 | if (strlen(s) != 8 && strlen(s) != 14) |
| 1248 | fatal("Invalid certificate time format %s", s); |
| 1249 | |
| 1250 | bzero(&tm, sizeof(tm)); |
| 1251 | if (strptime(s, |
| 1252 | strlen(s) == 8 ? "%Y%m%d" : "%Y%m%d%H%M%S", &tm) == NULL) |
| 1253 | fatal("Invalid certificate time %s", s); |
| 1254 | if ((tt = mktime(&tm)) < 0) |
| 1255 | fatal("Certificate time %s cannot be represented", s); |
| 1256 | return (u_int64_t)tt; |
| 1257 | } |
| 1258 | |
| 1259 | static void |
| 1260 | parse_cert_times(char *timespec) |
| 1261 | { |
| 1262 | char *from, *to; |
| 1263 | time_t now = time(NULL); |
| 1264 | int64_t secs; |
| 1265 | |
| 1266 | /* +timespec relative to now */ |
| 1267 | if (*timespec == '+' && strchr(timespec, ':') == NULL) { |
| 1268 | if ((secs = convtime(timespec + 1)) == -1) |
| 1269 | fatal("Invalid relative certificate life %s", timespec); |
| 1270 | cert_valid_to = now + secs; |
| 1271 | /* |
| 1272 | * Backdate certificate one minute to avoid problems on hosts |
| 1273 | * with poorly-synchronised clocks. |
| 1274 | */ |
| 1275 | cert_valid_from = ((now - 59)/ 60) * 60; |
| 1276 | return; |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * from:to, where |
| 1281 | * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS |
| 1282 | * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS |
| 1283 | */ |
| 1284 | from = xstrdup(timespec); |
| 1285 | to = strchr(from, ':'); |
| 1286 | if (to == NULL || from == to || *(to + 1) == '\0') |
| 1287 | fatal("Invalid certificate life specification %s", optarg); |
| 1288 | *to++ = '\0'; |
| 1289 | |
| 1290 | if (*from == '-' || *from == '+') |
| 1291 | cert_valid_from = parse_relative_time(from, now); |
| 1292 | else |
| 1293 | cert_valid_from = parse_absolute_time(from); |
| 1294 | |
| 1295 | if (*to == '-' || *to == '+') |
| 1296 | cert_valid_to = parse_relative_time(to, cert_valid_from); |
| 1297 | else |
| 1298 | cert_valid_to = parse_absolute_time(to); |
| 1299 | |
| 1300 | if (cert_valid_to <= cert_valid_from) |
| 1301 | fatal("Empty certificate validity interval"); |
| 1302 | xfree(from); |
| 1303 | } |
| 1304 | |
| 1305 | static void |
| 1306 | add_cert_constraint(char *opt) |
| 1307 | { |
| 1308 | char *val; |
| 1309 | |
| 1310 | if (strcmp(opt, "clear") == 0) |
| 1311 | constraint_flags = 0; |
| 1312 | else if (strcasecmp(opt, "no-x11-forwarding") == 0) |
| 1313 | constraint_flags &= ~CONSTRAINT_X_FWD; |
| 1314 | else if (strcasecmp(opt, "permit-x11-forwarding") == 0) |
| 1315 | constraint_flags |= CONSTRAINT_X_FWD; |
| 1316 | else if (strcasecmp(opt, "no-agent-forwarding") == 0) |
| 1317 | constraint_flags &= ~CONSTRAINT_AGENT_FWD; |
| 1318 | else if (strcasecmp(opt, "permit-agent-forwarding") == 0) |
| 1319 | constraint_flags |= CONSTRAINT_AGENT_FWD; |
| 1320 | else if (strcasecmp(opt, "no-port-forwarding") == 0) |
| 1321 | constraint_flags &= ~CONSTRAINT_PORT_FWD; |
| 1322 | else if (strcasecmp(opt, "permit-port-forwarding") == 0) |
| 1323 | constraint_flags |= CONSTRAINT_PORT_FWD; |
| 1324 | else if (strcasecmp(opt, "no-pty") == 0) |
| 1325 | constraint_flags &= ~CONSTRAINT_PTY; |
| 1326 | else if (strcasecmp(opt, "permit-pty") == 0) |
| 1327 | constraint_flags |= CONSTRAINT_PTY; |
| 1328 | else if (strcasecmp(opt, "no-user-rc") == 0) |
| 1329 | constraint_flags &= ~CONSTRAINT_USER_RC; |
| 1330 | else if (strcasecmp(opt, "permit-user-rc") == 0) |
| 1331 | constraint_flags |= CONSTRAINT_USER_RC; |
| 1332 | else if (strncasecmp(opt, "force-command=", 14) == 0) { |
| 1333 | val = opt + 14; |
| 1334 | if (*val == '\0') |
| 1335 | fatal("Empty force-command constraint"); |
| 1336 | if (constraint_command != NULL) |
| 1337 | fatal("force-command already specified"); |
| 1338 | constraint_command = xstrdup(val); |
| 1339 | } else if (strncasecmp(opt, "source-address=", 15) == 0) { |
| 1340 | val = opt + 15; |
| 1341 | if (*val == '\0') |
| 1342 | fatal("Empty source-address constraint"); |
| 1343 | if (constraint_src_addr != NULL) |
| 1344 | fatal("source-address already specified"); |
| 1345 | if (addr_match_cidr_list(NULL, val) != 0) |
| 1346 | fatal("Invalid source-address list"); |
| 1347 | constraint_src_addr = xstrdup(val); |
| 1348 | } else |
| 1349 | fatal("Unsupported certificate constraint \"%s\"", opt); |
| 1350 | } |
| 1351 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 1352 | static void |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 1353 | usage(void) |
| 1354 | { |
Damien Miller | 5cbe7ca | 2007-09-17 16:05:50 +1000 | [diff] [blame] | 1355 | fprintf(stderr, "usage: %s [options]\n", __progname); |
Ben Lindstrom | 97be31e | 2001-08-06 21:49:06 +0000 | [diff] [blame] | 1356 | fprintf(stderr, "Options:\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1357 | 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] | 1358 | fprintf(stderr, " -B Show bubblebabble digest of key file.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1359 | 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] | 1360 | fprintf(stderr, " -C comment Provide new comment.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1361 | fprintf(stderr, " -c Change comment in private and public key files.\n"); |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1362 | #ifdef ENABLE_PKCS11 |
| 1363 | fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n"); |
| 1364 | #endif |
Darren Tucker | 26dc3e6 | 2007-02-19 22:09:06 +1100 | [diff] [blame] | 1365 | fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1366 | fprintf(stderr, " -F hostname Find hostname in known hosts file.\n"); |
| 1367 | fprintf(stderr, " -f filename Filename of the key file.\n"); |
| 1368 | fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n"); |
| 1369 | fprintf(stderr, " -g Use generic DNS resource record format.\n"); |
| 1370 | fprintf(stderr, " -H Hash names in known_hosts file.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1371 | fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n"); |
| 1372 | fprintf(stderr, " -I key_id Key identifier to include in certificate.\n"); |
Darren Tucker | 26dc3e6 | 2007-02-19 22:09:06 +1100 | [diff] [blame] | 1373 | fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1374 | fprintf(stderr, " -l Show fingerprint of key file.\n"); |
| 1375 | 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] | 1376 | 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] | 1377 | fprintf(stderr, " -N phrase Provide new passphrase.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1378 | fprintf(stderr, " -O cnstr Specify a certificate constraint.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1379 | fprintf(stderr, " -P phrase Provide old passphrase.\n"); |
| 1380 | fprintf(stderr, " -p Change passphrase of private key file.\n"); |
| 1381 | fprintf(stderr, " -q Quiet.\n"); |
| 1382 | fprintf(stderr, " -R hostname Remove host from known_hosts file.\n"); |
| 1383 | fprintf(stderr, " -r hostname Print DNS resource record.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1384 | fprintf(stderr, " -s ca_key Certify keys with CA key.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1385 | fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n"); |
| 1386 | fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n"); |
| 1387 | fprintf(stderr, " -t type Specify type of key to create.\n"); |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1388 | fprintf(stderr, " -V from:to Specify certificate validity interval.\n"); |
Damien Miller | 9278ffa | 2005-05-26 11:59:06 +1000 | [diff] [blame] | 1389 | fprintf(stderr, " -v Verbose.\n"); |
| 1390 | fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n"); |
| 1391 | fprintf(stderr, " -y Read private key file and print public key.\n"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1392 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1393 | exit(1); |
Damien Miller | 431f66b | 1999-11-21 18:31:57 +1100 | [diff] [blame] | 1394 | } |
| 1395 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1396 | /* |
| 1397 | * Main program for key management. |
| 1398 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1399 | int |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 1400 | main(int argc, char **argv) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1401 | { |
Damien Miller | a41c8b1 | 2002-01-22 23:05:08 +1100 | [diff] [blame] | 1402 | char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1403 | char out_file[MAXPATHLEN], *pkcs11provider = NULL; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1404 | char *rr_hostname = NULL; |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1405 | Key *private, *public; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1406 | struct passwd *pw; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1407 | struct stat st; |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1408 | int opt, type, fd; |
Darren Tucker | 2db8ae6 | 2005-06-01 23:02:25 +1000 | [diff] [blame] | 1409 | u_int32_t memory = 0, generator_wanted = 0, trials = 100; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1410 | int do_gen_candidates = 0, do_screen_candidates = 0; |
| 1411 | BIGNUM *start = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1412 | FILE *f; |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 1413 | const char *errstr; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1414 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1415 | extern int optind; |
| 1416 | extern char *optarg; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1417 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 1418 | /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */ |
| 1419 | sanitise_stdfd(); |
| 1420 | |
Darren Tucker | 9ac56e9 | 2007-01-14 10:19:59 +1100 | [diff] [blame] | 1421 | __progname = ssh_get_progname(argv[0]); |
Damien Miller | f9b625c | 2000-07-09 22:42:32 +1000 | [diff] [blame] | 1422 | |
Damien Miller | d3a1857 | 2000-06-07 19:55:44 +1000 | [diff] [blame] | 1423 | SSLeay_add_all_algorithms(); |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 1424 | log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1425 | |
Kevin Steves | 3a88191 | 2002-07-20 19:05:40 +0000 | [diff] [blame] | 1426 | init_rng(); |
| 1427 | seed_rng(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1428 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1429 | /* we need this for the home * directory. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1430 | pw = getpwuid(getuid()); |
| 1431 | if (!pw) { |
| 1432 | printf("You don't exist, go away!\n"); |
| 1433 | exit(1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1434 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1435 | if (gethostname(hostname, sizeof(hostname)) < 0) { |
| 1436 | perror("gethostname"); |
| 1437 | exit(1); |
| 1438 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 1439 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1440 | while ((opt = getopt(argc, argv, "degiqpclBHhvxXyF:b:f:t:D:I:P:N:n:" |
| 1441 | "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] | 1442 | switch (opt) { |
| 1443 | case 'b': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1444 | bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr); |
Damien Miller | 02e754f | 2005-05-26 12:19:39 +1000 | [diff] [blame] | 1445 | if (errstr) |
| 1446 | fatal("Bits has bad value %s (%s)", |
| 1447 | optarg, errstr); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1448 | break; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1449 | case 'F': |
| 1450 | find_host = 1; |
| 1451 | rr_hostname = optarg; |
| 1452 | break; |
| 1453 | case 'H': |
| 1454 | hash_hosts = 1; |
| 1455 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1456 | case 'I': |
| 1457 | cert_key_id = optarg; |
| 1458 | break; |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1459 | case 'R': |
| 1460 | delete_host = 1; |
| 1461 | rr_hostname = optarg; |
| 1462 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1463 | case 'l': |
| 1464 | print_fingerprint = 1; |
| 1465 | break; |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 1466 | case 'B': |
| 1467 | print_bubblebabble = 1; |
| 1468 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1469 | case 'n': |
| 1470 | cert_principals = optarg; |
| 1471 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1472 | case 'p': |
| 1473 | change_passphrase = 1; |
| 1474 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1475 | case 'c': |
| 1476 | change_comment = 1; |
| 1477 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1478 | case 'f': |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1479 | if (strlcpy(identity_file, optarg, sizeof(identity_file)) >= |
| 1480 | sizeof(identity_file)) |
| 1481 | fatal("Identity filename too long"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1482 | have_identity = 1; |
| 1483 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1484 | case 'g': |
| 1485 | print_generic = 1; |
| 1486 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1487 | case 'P': |
| 1488 | identity_passphrase = optarg; |
| 1489 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1490 | case 'N': |
| 1491 | identity_new_passphrase = optarg; |
| 1492 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1493 | case 'O': |
| 1494 | add_cert_constraint(optarg); |
| 1495 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1496 | case 'C': |
| 1497 | identity_comment = optarg; |
| 1498 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1499 | case 'q': |
| 1500 | quiet = 1; |
| 1501 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1502 | case 'e': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1503 | case 'x': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1504 | /* export key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1505 | convert_to_ssh2 = 1; |
| 1506 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1507 | case 'h': |
| 1508 | cert_key_type = SSH2_CERT_TYPE_HOST; |
| 1509 | constraint_flags = 0; |
| 1510 | break; |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1511 | case 'i': |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1512 | case 'X': |
Ben Lindstrom | 5a70782 | 2001-04-22 17:15:46 +0000 | [diff] [blame] | 1513 | /* import key */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1514 | convert_from_ssh2 = 1; |
| 1515 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1516 | case 'y': |
| 1517 | print_public = 1; |
| 1518 | break; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1519 | case 'd': |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1520 | key_type_name = "dsa"; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1521 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1522 | case 's': |
| 1523 | ca_key_path = optarg; |
| 1524 | break; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1525 | case 't': |
| 1526 | key_type_name = optarg; |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1527 | break; |
Ben Lindstrom | 8282d6a | 2001-08-06 21:44:05 +0000 | [diff] [blame] | 1528 | case 'D': |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1529 | pkcs11provider = optarg; |
Ben Lindstrom | cd39228 | 2001-07-04 03:44:03 +0000 | [diff] [blame] | 1530 | break; |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1531 | case 'v': |
| 1532 | if (log_level == SYSLOG_LEVEL_INFO) |
| 1533 | log_level = SYSLOG_LEVEL_DEBUG1; |
| 1534 | else { |
Darren Tucker | fc95970 | 2004-07-17 16:12:08 +1000 | [diff] [blame] | 1535 | if (log_level >= SYSLOG_LEVEL_DEBUG1 && |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1536 | log_level < SYSLOG_LEVEL_DEBUG3) |
| 1537 | log_level++; |
| 1538 | } |
| 1539 | break; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1540 | case 'r': |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1541 | rr_hostname = optarg; |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1542 | break; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1543 | case 'W': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1544 | generator_wanted = (u_int32_t)strtonum(optarg, 1, |
| 1545 | UINT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1546 | if (errstr) |
| 1547 | fatal("Desired generator has bad value: %s (%s)", |
| 1548 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1549 | break; |
| 1550 | case 'a': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1551 | trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1552 | if (errstr) |
| 1553 | fatal("Invalid number of trials: %s (%s)", |
| 1554 | optarg, errstr); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1555 | break; |
| 1556 | case 'M': |
Damien Miller | 5f34006 | 2006-03-26 14:27:57 +1100 | [diff] [blame] | 1557 | memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr); |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1558 | if (errstr) { |
| 1559 | fatal("Memory limit is %s: %s", errstr, optarg); |
| 1560 | } |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1561 | break; |
| 1562 | case 'G': |
| 1563 | do_gen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1564 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 1565 | sizeof(out_file)) |
| 1566 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1567 | break; |
| 1568 | case 'T': |
| 1569 | do_screen_candidates = 1; |
Damien Miller | b089fb5 | 2005-05-26 12:16:18 +1000 | [diff] [blame] | 1570 | if (strlcpy(out_file, optarg, sizeof(out_file)) >= |
| 1571 | sizeof(out_file)) |
| 1572 | fatal("Output filename too long"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1573 | break; |
| 1574 | case 'S': |
| 1575 | /* XXX - also compare length against bits */ |
| 1576 | if (BN_hex2bn(&start, optarg) == 0) |
| 1577 | fatal("Invalid start point."); |
| 1578 | break; |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1579 | case 'V': |
| 1580 | parse_cert_times(optarg); |
| 1581 | break; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1582 | case '?': |
| 1583 | default: |
| 1584 | usage(); |
| 1585 | } |
| 1586 | } |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1587 | |
| 1588 | /* reinit */ |
Damien Miller | df8b7db | 2007-01-05 16:22:57 +1100 | [diff] [blame] | 1589 | log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1); |
Darren Tucker | 06930c7 | 2003-12-31 11:34:51 +1100 | [diff] [blame] | 1590 | |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1591 | argv += optind; |
| 1592 | argc -= optind; |
| 1593 | |
| 1594 | if (ca_key_path != NULL) { |
| 1595 | if (argc < 1) { |
| 1596 | printf("Too few arguments.\n"); |
| 1597 | usage(); |
| 1598 | } |
| 1599 | } else if (argc > 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1600 | printf("Too many arguments.\n"); |
| 1601 | usage(); |
| 1602 | } |
| 1603 | if (change_passphrase && change_comment) { |
| 1604 | printf("Can only have one of -p and -c.\n"); |
| 1605 | usage(); |
| 1606 | } |
Darren Tucker | 0f7e910 | 2008-06-08 12:54:29 +1000 | [diff] [blame] | 1607 | if (print_fingerprint && (delete_host || hash_hosts)) { |
| 1608 | printf("Cannot use -l with -D or -R.\n"); |
| 1609 | usage(); |
| 1610 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 1611 | if (ca_key_path != NULL) { |
| 1612 | if (cert_key_id == NULL) |
| 1613 | fatal("Must specify key id (-I) when certifying"); |
| 1614 | do_ca_sign(pw, argc, argv); |
| 1615 | } |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1616 | if (delete_host || hash_hosts || find_host) |
| 1617 | do_known_hosts(pw, rr_hostname); |
Ben Lindstrom | 8fd372b | 2001-03-12 03:02:17 +0000 | [diff] [blame] | 1618 | if (print_fingerprint || print_bubblebabble) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1619 | do_fingerprint(pw); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1620 | if (change_passphrase) |
| 1621 | do_change_passphrase(pw); |
Damien Miller | 4a10d2e | 2002-03-11 22:53:29 +1100 | [diff] [blame] | 1622 | if (change_comment) |
| 1623 | do_change_comment(pw); |
Kevin Steves | 3a88191 | 2002-07-20 19:05:40 +0000 | [diff] [blame] | 1624 | if (convert_to_ssh2) |
| 1625 | do_convert_to_ssh2(pw); |
| 1626 | if (convert_from_ssh2) |
| 1627 | do_convert_from_ssh2(pw); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1628 | if (print_public) |
| 1629 | do_print_public(pw); |
Damien Miller | 4b42d7f | 2005-03-01 21:48:35 +1100 | [diff] [blame] | 1630 | if (rr_hostname != NULL) { |
Damien Miller | cb31482 | 2006-03-26 13:48:01 +1100 | [diff] [blame] | 1631 | unsigned int n = 0; |
| 1632 | |
| 1633 | if (have_identity) { |
| 1634 | n = do_print_resource_record(pw, |
| 1635 | identity_file, rr_hostname); |
| 1636 | if (n == 0) { |
| 1637 | perror(identity_file); |
| 1638 | exit(1); |
| 1639 | } |
| 1640 | exit(0); |
| 1641 | } else { |
| 1642 | |
| 1643 | n += do_print_resource_record(pw, |
| 1644 | _PATH_HOST_RSA_KEY_FILE, rr_hostname); |
| 1645 | n += do_print_resource_record(pw, |
| 1646 | _PATH_HOST_DSA_KEY_FILE, rr_hostname); |
| 1647 | |
| 1648 | if (n == 0) |
| 1649 | fatal("no keys found."); |
| 1650 | exit(0); |
| 1651 | } |
Damien Miller | 37876e9 | 2003-05-15 10:19:46 +1000 | [diff] [blame] | 1652 | } |
Damien Miller | 7ea845e | 2010-02-12 09:21:02 +1100 | [diff] [blame] | 1653 | if (pkcs11provider != NULL) |
| 1654 | do_download(pw, pkcs11provider); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1655 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1656 | if (do_gen_candidates) { |
| 1657 | FILE *out = fopen(out_file, "w"); |
Damien Miller | 787b2ec | 2003-11-21 23:56:47 +1100 | [diff] [blame] | 1658 | |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1659 | if (out == NULL) { |
| 1660 | error("Couldn't open modulus candidate file \"%s\": %s", |
| 1661 | out_file, strerror(errno)); |
| 1662 | return (1); |
| 1663 | } |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 1664 | if (bits == 0) |
| 1665 | bits = DEFAULT_BITS; |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1666 | if (gen_candidates(out, memory, bits, start) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1667 | fatal("modulus candidate generation failed"); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1668 | |
| 1669 | return (0); |
| 1670 | } |
| 1671 | |
| 1672 | if (do_screen_candidates) { |
| 1673 | FILE *in; |
| 1674 | FILE *out = fopen(out_file, "w"); |
| 1675 | |
| 1676 | if (have_identity && strcmp(identity_file, "-") != 0) { |
| 1677 | if ((in = fopen(identity_file, "r")) == NULL) { |
| 1678 | fatal("Couldn't open modulus candidate " |
Damien Miller | a8e06ce | 2003-11-21 23:48:55 +1100 | [diff] [blame] | 1679 | "file \"%s\": %s", identity_file, |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1680 | strerror(errno)); |
| 1681 | } |
| 1682 | } else |
| 1683 | in = stdin; |
| 1684 | |
| 1685 | if (out == NULL) { |
| 1686 | fatal("Couldn't open moduli file \"%s\": %s", |
| 1687 | out_file, strerror(errno)); |
| 1688 | } |
| 1689 | if (prime_test(in, out, trials, generator_wanted) != 0) |
Damien Miller | 15d72a0 | 2005-11-05 15:07:33 +1100 | [diff] [blame] | 1690 | fatal("modulus screening failed"); |
Darren Tucker | f4220e6 | 2003-08-21 16:44:07 +1000 | [diff] [blame] | 1691 | return (0); |
Darren Tucker | 019cefe | 2003-08-02 22:40:07 +1000 | [diff] [blame] | 1692 | } |
| 1693 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1694 | arc4random_stir(); |
| 1695 | |
Damien Miller | f14be5c | 2005-11-05 15:15:49 +1100 | [diff] [blame] | 1696 | if (key_type_name == NULL) |
| 1697 | key_type_name = "rsa"; |
| 1698 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 1699 | type = key_type_from_name(key_type_name); |
| 1700 | if (type == KEY_UNSPEC) { |
| 1701 | fprintf(stderr, "unknown key type %s\n", key_type_name); |
| 1702 | exit(1); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1703 | } |
Damien Miller | 3f54a9f | 2005-11-05 14:52:18 +1100 | [diff] [blame] | 1704 | if (bits == 0) |
| 1705 | bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS; |
Tim Rice | 660c340 | 2005-11-28 17:45:32 -0800 | [diff] [blame] | 1706 | if (type == KEY_DSA && bits != 1024) |
| 1707 | fatal("DSA keys must be 1024 bits"); |
Darren Tucker | 3af2ac5 | 2005-11-29 13:10:24 +1100 | [diff] [blame] | 1708 | if (!quiet) |
| 1709 | printf("Generating public/private %s key pair.\n", key_type_name); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1710 | private = key_generate(type, bits); |
| 1711 | if (private == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1712 | fprintf(stderr, "key_generate failed\n"); |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1713 | exit(1); |
| 1714 | } |
| 1715 | public = key_from_private(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1716 | |
| 1717 | if (!have_identity) |
| 1718 | ask_filename(pw, "Enter file in which to save the key"); |
| 1719 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 1720 | /* Create ~/.ssh directory if it doesn't already exist. */ |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 1721 | 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] | 1722 | if (strstr(identity_file, dotsshdir) != NULL && |
| 1723 | stat(dotsshdir, &st) < 0) { |
Damien Miller | be484b5 | 2000-07-15 14:14:16 +1000 | [diff] [blame] | 1724 | if (mkdir(dotsshdir, 0700) < 0) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1725 | error("Could not create directory '%s'.", dotsshdir); |
| 1726 | else if (!quiet) |
| 1727 | printf("Created directory '%s'.\n", dotsshdir); |
| 1728 | } |
| 1729 | /* If the file already exists, ask the user to confirm. */ |
| 1730 | if (stat(identity_file, &st) >= 0) { |
| 1731 | char yesno[3]; |
| 1732 | printf("%s already exists.\n", identity_file); |
| 1733 | printf("Overwrite (y/n)? "); |
| 1734 | fflush(stdout); |
| 1735 | if (fgets(yesno, sizeof(yesno), stdin) == NULL) |
| 1736 | exit(1); |
| 1737 | if (yesno[0] != 'y' && yesno[0] != 'Y') |
| 1738 | exit(1); |
| 1739 | } |
| 1740 | /* Ask for a passphrase (twice). */ |
| 1741 | if (identity_passphrase) |
| 1742 | passphrase1 = xstrdup(identity_passphrase); |
| 1743 | else if (identity_new_passphrase) |
| 1744 | passphrase1 = xstrdup(identity_new_passphrase); |
| 1745 | else { |
| 1746 | passphrase_again: |
| 1747 | passphrase1 = |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1748 | read_passphrase("Enter passphrase (empty for no " |
| 1749 | "passphrase): ", RP_ALLOW_STDIN); |
| 1750 | passphrase2 = read_passphrase("Enter same passphrase again: ", |
| 1751 | RP_ALLOW_STDIN); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1752 | if (strcmp(passphrase1, passphrase2) != 0) { |
Ben Lindstrom | 949974b | 2001-06-25 05:20:31 +0000 | [diff] [blame] | 1753 | /* |
| 1754 | * The passphrases do not match. Clear them and |
| 1755 | * retry. |
| 1756 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1757 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1758 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1759 | xfree(passphrase1); |
| 1760 | xfree(passphrase2); |
| 1761 | printf("Passphrases do not match. Try again.\n"); |
| 1762 | goto passphrase_again; |
| 1763 | } |
| 1764 | /* Clear the other copy of the passphrase. */ |
| 1765 | memset(passphrase2, 0, strlen(passphrase2)); |
| 1766 | xfree(passphrase2); |
| 1767 | } |
| 1768 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1769 | if (identity_comment) { |
| 1770 | strlcpy(comment, identity_comment, sizeof(comment)); |
| 1771 | } else { |
Darren Tucker | e15fb09 | 2008-11-11 16:31:43 +1100 | [diff] [blame] | 1772 | /* Create default comment field for the passphrase. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1773 | snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname); |
| 1774 | } |
| 1775 | |
| 1776 | /* Save the key with the given passphrase and comment. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 1777 | if (!key_save_private(private, identity_file, passphrase1, comment)) { |
Ben Lindstrom | 15f3386 | 2001-04-16 02:00:02 +0000 | [diff] [blame] | 1778 | printf("Saving the key failed: %s.\n", identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1779 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1780 | xfree(passphrase1); |
| 1781 | exit(1); |
| 1782 | } |
| 1783 | /* Clear the passphrase. */ |
| 1784 | memset(passphrase1, 0, strlen(passphrase1)); |
| 1785 | xfree(passphrase1); |
| 1786 | |
| 1787 | /* Clear the private key and the random number generator. */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 1788 | key_free(private); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1789 | arc4random_stir(); |
| 1790 | |
| 1791 | if (!quiet) |
| 1792 | printf("Your identification has been saved in %s.\n", identity_file); |
| 1793 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1794 | strlcat(identity_file, ".pub", sizeof(identity_file)); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1795 | fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); |
| 1796 | if (fd == -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1797 | printf("Could not save your public key in %s\n", identity_file); |
| 1798 | exit(1); |
| 1799 | } |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1800 | f = fdopen(fd, "w"); |
| 1801 | if (f == NULL) { |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1802 | printf("fdopen %s failed\n", identity_file); |
Ben Lindstrom | 5fc6270 | 2001-03-09 18:19:24 +0000 | [diff] [blame] | 1803 | exit(1); |
| 1804 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1805 | if (!key_write(public, f)) |
Damien Miller | 9eab956 | 2009-02-22 08:47:02 +1100 | [diff] [blame] | 1806 | fprintf(stderr, "write key failed\n"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1807 | fprintf(f, " %s\n", comment); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1808 | fclose(f); |
| 1809 | |
| 1810 | if (!quiet) { |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1811 | char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 1812 | char *ra = key_fingerprint(public, SSH_FP_MD5, |
| 1813 | SSH_FP_RANDOMART); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1814 | printf("Your public key has been saved in %s.\n", |
| 1815 | identity_file); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1816 | printf("The key fingerprint is:\n"); |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1817 | printf("%s %s\n", fp, comment); |
Darren Tucker | 9c16ac9 | 2008-06-13 04:40:35 +1000 | [diff] [blame] | 1818 | printf("The key's randomart image is:\n"); |
| 1819 | printf("%s\n", ra); |
| 1820 | xfree(ra); |
Ben Lindstrom | cfccef9 | 2001-03-13 04:57:58 +0000 | [diff] [blame] | 1821 | xfree(fp); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1822 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 1823 | |
| 1824 | key_free(public); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 1825 | exit(0); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1826 | } |