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