blob: 14eee6f87269e32a33f4dbe8da2674afe743ba62 [file] [log] [blame]
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001/* $OpenBSD: ssh-keygen.c,v 1.190 2010/05/20 23:46:02 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
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 Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110051#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110052
Damien Miller7ea845e2010-02-12 09:21:02 +110053#ifdef ENABLE_PKCS11
54#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000055#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000056
Damien Miller3f54a9f2005-11-05 14:52:18 +110057/* 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
60u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Flag indicating that we just want to change the passphrase. This can be
64 * set on the command line.
65 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066int change_passphrase = 0;
67
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Flag indicating that we just want to change the comment. This can be set
70 * on the command line.
71 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072int change_comment = 0;
73
74int quiet = 0;
75
Darren Tucker35c45532008-06-13 04:43:15 +100076int log_level = SYSLOG_LEVEL_INFO;
77
Damien Miller4b42d7f2005-03-01 21:48:35 +110078/* Flag indicating that we want to hash a known_hosts file */
79int hash_hosts = 0;
80/* Flag indicating that we want lookup a host in known_hosts file */
81int find_host = 0;
82/* Flag indicating that we want to delete a host from a known_hosts file */
83int delete_host = 0;
84
Damien Millerf2b70ca2010-03-05 07:39:35 +110085/* Flag indicating that we want to show the contents of a certificate */
86int show_cert = 0;
87
Damien Miller10f6f6b1999-11-17 17:29:08 +110088/* Flag indicating that we just want to see the key fingerprint */
89int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000090int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110091
Damien Miller431f66b1999-11-21 18:31:57 +110092/* The identity file name, given on the command line or entered by the user. */
93char identity_file[1024];
94int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96/* This is set to the passphrase if given on the command line. */
97char *identity_passphrase = NULL;
98
99/* This is set to the new passphrase if given on the command line. */
100char *identity_new_passphrase = NULL;
101
102/* This is set to the new comment if given on the command line. */
103char *identity_comment = NULL;
104
Damien Miller0a80ca12010-02-27 07:55:05 +1100105/* Path to CA key when certifying keys. */
106char *ca_key_path = NULL;
107
Damien Miller4e270b02010-04-16 15:56:21 +1000108/* Certificate serial number */
109long long cert_serial = 0;
110
Damien Miller0a80ca12010-02-27 07:55:05 +1100111/* Key type when certifying */
112u_int cert_key_type = SSH2_CERT_TYPE_USER;
113
114/* "key ID" of signed key */
115char *cert_key_id = NULL;
116
117/* Comma-separated list of principal names for certifying keys */
118char *cert_principals = NULL;
119
120/* Validity period for certificates */
121u_int64_t cert_valid_from = 0;
122u_int64_t cert_valid_to = ~0ULL;
123
Damien Miller4e270b02010-04-16 15:56:21 +1000124/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000125#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)
132u_int32_t certflags_flags = CERTOPT_DEFAULT;
133char *certflags_command = NULL;
134char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100135
Damien Millereba71ba2000-04-29 23:57:08 +1000136/* Dump public key file in format used by real and the original SSH 2 */
137int convert_to_ssh2 = 0;
138int convert_from_ssh2 = 0;
139int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000140int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100141
Damien Millera41c8b12002-01-22 23:05:08 +1100142char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000143
Damien Miller431f66b1999-11-21 18:31:57 +1100144/* argv0 */
145extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146
Damien Millereba71ba2000-04-29 23:57:08 +1000147char hostname[MAXHOSTNAMELEN];
148
Darren Tucker770fc012004-05-13 16:24:32 +1000149/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000150int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000151int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
152
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static void
Damien Miller431f66b1999-11-21 18:31:57 +1100154ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155{
Damien Miller95def091999-11-25 00:26:21 +1100156 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100157 char *name = NULL;
158
Damien Miller993dd552002-02-19 15:22:47 +1100159 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000160 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100161 else {
Damien Miller993dd552002-02-19 15:22:47 +1100162 switch (key_type_from_name(key_type_name)) {
163 case KEY_RSA1:
164 name = _PATH_SSH_CLIENT_IDENTITY;
165 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000166 case KEY_DSA_CERT:
167 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100168 case KEY_DSA:
169 name = _PATH_SSH_CLIENT_ID_DSA;
170 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000171 case KEY_RSA_CERT:
172 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100173 case KEY_RSA:
174 name = _PATH_SSH_CLIENT_ID_RSA;
175 break;
176 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100177 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100178 exit(1);
179 break;
180 }
Damien Miller90967402006-03-26 14:07:26 +1100181 }
Damien Millere39cacc2000-11-29 12:18:44 +1100182 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000183 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100184 if (fgets(buf, sizeof(buf), stdin) == NULL)
185 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000186 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100187 if (strcmp(buf, "") != 0)
188 strlcpy(identity_file, buf, sizeof(identity_file));
189 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100190}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000191
Ben Lindstrombba81212001-06-25 05:01:22 +0000192static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000193load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000194{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000195 char *pass;
196 Key *prv;
197
Ben Lindstroma3700052001-04-05 23:26:32 +0000198 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000199 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000200 if (identity_passphrase)
201 pass = xstrdup(identity_passphrase);
202 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000203 pass = read_passphrase("Enter passphrase: ",
204 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000205 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000206 memset(pass, 0, strlen(pass));
207 xfree(pass);
208 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000209 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000210}
211
Damien Miller874d77b2000-10-14 16:23:11 +1100212#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000213#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100214#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000215#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000216
Ben Lindstrombba81212001-06-25 05:01:22 +0000217static void
Damien Millereba71ba2000-04-29 23:57:08 +1000218do_convert_to_ssh2(struct passwd *pw)
219{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000220 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000221 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000222 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100223 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000224 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 Lindstrom46c264f2001-04-24 16:56:58 +0000232 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000233 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000234 fprintf(stderr, "load failed\n");
235 exit(1);
236 }
Damien Millereba71ba2000-04-29 23:57:08 +1000237 }
Damien Millerdb274722003-05-14 13:45:22 +1000238 if (k->type == KEY_RSA1) {
239 fprintf(stderr, "version 1 keys are not supported\n");
240 exit(1);
241 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000242 if (key_to_blob(k, &blob, &len) <= 0) {
243 fprintf(stderr, "key_to_blob failed\n");
244 exit(1);
245 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100246 /* 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 Lindstrom46c264f2001-04-24 16:56:58 +0000249 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000250 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100251
252 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
253 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000254 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100255 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000256 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000257 xfree(blob);
258 exit(0);
259}
260
Ben Lindstrombba81212001-06-25 05:01:22 +0000261static void
Damien Miller874d77b2000-10-14 16:23:11 +1100262buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
263{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000264 u_int bignum_bits = buffer_get_int(b);
265 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000266
Damien Miller874d77b2000-10-14 16:23:11 +1100267 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000268 fatal("buffer_get_bignum_bits: input buffer too small: "
269 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100270 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
271 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100272 buffer_consume(b, bytes);
273}
274
Ben Lindstrombba81212001-06-25 05:01:22 +0000275static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000276do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100277{
278 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100279 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100280 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000281 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000282 int magic, rlen, ktype, i1, i2, i3, i4;
283 u_int slen;
284 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100285
286 buffer_init(&b);
287 buffer_append(&b, blob, blen);
288
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100289 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100290 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 Lindstrom34f91882001-06-25 04:47:54 +0000295 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100296 type = buffer_get_string(&b, NULL);
297 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000298 i2 = buffer_get_int(&b);
299 i3 = buffer_get_int(&b);
300 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100301 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100302 if (strcmp(cipher, "none") != 0) {
303 error("unsupported cipher %s", cipher);
304 xfree(cipher);
305 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000306 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100307 return NULL;
308 }
309 xfree(cipher);
310
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000311 if (strstr(type, "dsa")) {
312 ktype = KEY_DSA;
313 } else if (strstr(type, "rsa")) {
314 ktype = KEY_RSA;
315 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100316 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000317 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100318 return NULL;
319 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000320 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 Tucker82a3d2b2007-02-19 22:10:25 +1100332 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000333 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 Lindstromd09fcf52001-03-29 00:29:54 +0000343 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 Lindstromf7297dd2001-07-04 05:02:23 +0000352 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000353 break;
354 }
Damien Miller874d77b2000-10-14 16:23:11 +1100355 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000356 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000357 error("do_convert_private_ssh2_from_blob: "
358 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100359 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000360
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000361 /* 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 Miller874d77b2000-10-14 16:23:11 +1100365 return key;
366}
367
Damien Miller8056a9d2006-03-15 12:05:40 +1100368static int
369get_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 Miller90967402006-03-26 14:07:26 +1100380 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100381 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 Miller6c7439f2007-01-05 16:29:55 +1100394 /* We reached EOF */
395 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100396}
397
Ben Lindstrombba81212001-06-25 05:01:22 +0000398static void
Damien Millereba71ba2000-04-29 23:57:08 +1000399do_convert_from_ssh2(struct passwd *pw)
400{
401 Key *k;
402 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000403 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100404 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000405 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000406 char encoded[8096];
407 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100408 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000409 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 }
417 fp = fopen(identity_file, "r");
418 if (fp == NULL) {
419 perror(identity_file);
420 exit(1);
421 }
422 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100423 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
424 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000425 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000426 if (strncmp(line, "----", 4) == 0 ||
427 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100428 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
429 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000430 if (strstr(line, " END ") != NULL) {
431 break;
432 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000433 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000434 continue;
435 }
Damien Miller30c3d422000-05-09 11:02:59 +1000436 if (escaped) {
437 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000438 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000439 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000440 }
Damien Millereba71ba2000-04-29 23:57:08 +1000441 strlcat(encoded, line, sizeof(encoded));
442 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000443 len = strlen(encoded);
444 if (((len % 4) == 3) &&
445 (encoded[len-1] == '=') &&
446 (encoded[len-2] == '=') &&
447 (encoded[len-3] == '='))
448 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100449 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000450 if (blen < 0) {
451 fprintf(stderr, "uudecode failed.\n");
452 exit(1);
453 }
Damien Miller874d77b2000-10-14 16:23:11 +1100454 k = private ?
455 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100456 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100457 if (k == NULL) {
458 fprintf(stderr, "decode blob failed.\n");
459 exit(1);
460 }
461 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000462 (k->type == KEY_DSA ?
463 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
464 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100465 key_write(k, stdout);
466 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100467 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100468 exit(1);
469 }
Damien Millereba71ba2000-04-29 23:57:08 +1000470 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100471 if (!private)
472 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000473 fclose(fp);
474 exit(0);
475}
476
Ben Lindstrombba81212001-06-25 05:01:22 +0000477static void
Damien Millereba71ba2000-04-29 23:57:08 +1000478do_print_public(struct passwd *pw)
479{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000480 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000481 struct stat st;
482
483 if (!have_identity)
484 ask_filename(pw, "Enter file in which the key is");
485 if (stat(identity_file, &st) < 0) {
486 perror(identity_file);
487 exit(1);
488 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000489 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000490 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000491 fprintf(stderr, "load failed\n");
492 exit(1);
493 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000494 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000495 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000496 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000497 fprintf(stdout, "\n");
498 exit(0);
499}
500
Ben Lindstromcd392282001-07-04 03:44:03 +0000501static void
Damien Miller86cbb442010-02-12 09:22:57 +1100502do_download(struct passwd *pw, char *pkcs11provider)
Ben Lindstromcd392282001-07-04 03:44:03 +0000503{
Damien Miller7ea845e2010-02-12 09:21:02 +1100504#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000505 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100506 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000507
Damien Miller7ea845e2010-02-12 09:21:02 +1100508 pkcs11_init(0);
509 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
510 if (nkeys <= 0)
511 fatal("cannot read public key from pkcs11");
512 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000513 key_write(keys[i], stdout);
514 key_free(keys[i]);
515 fprintf(stdout, "\n");
516 }
517 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100518 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000519 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100520#else
521 fatal("no pkcs11 support");
522#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000523}
Ben Lindstromcd392282001-07-04 03:44:03 +0000524
Ben Lindstrombba81212001-06-25 05:01:22 +0000525static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100526do_fingerprint(struct passwd *pw)
527{
Damien Miller98c7ad62000-03-09 21:27:49 +1100528 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000529 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000530 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100531 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000532 enum fp_rep rep;
533 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100534 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100535
Ben Lindstromd0fca422001-03-26 13:44:06 +0000536 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
537 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000538
Damien Miller95def091999-11-25 00:26:21 +1100539 if (!have_identity)
540 ask_filename(pw, "Enter file in which the key is");
541 if (stat(identity_file, &st) < 0) {
542 perror(identity_file);
543 exit(1);
544 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000545 public = key_load_public(identity_file, &comment);
546 if (public != NULL) {
547 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100548 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000549 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
550 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000551 if (log_level >= SYSLOG_LEVEL_VERBOSE)
552 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100553 key_free(public);
554 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000555 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000556 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100557 exit(0);
558 }
Damien Miller40b59852006-06-13 13:00:25 +1000559 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000560 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000561 comment = NULL;
562 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100563
564 f = fopen(identity_file, "r");
565 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100566 while (fgets(line, sizeof(line), f)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000567 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100568 error("line %d too long: %.40s...",
569 num + 1, line);
Damien Miller98c7ad62000-03-09 21:27:49 +1100570 skip = 1;
571 continue;
Damien Miller95def091999-11-25 00:26:21 +1100572 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100573 num++;
574 if (skip) {
575 skip = 0;
576 continue;
577 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000578 *cp = '\0';
Damien Miller98c7ad62000-03-09 21:27:49 +1100579
580 /* Skip leading whitespace, empty and comment lines. */
581 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
582 ;
583 if (!*cp || *cp == '\n' || *cp == '#')
Damien Miller80163902007-01-05 16:30:16 +1100584 continue;
Damien Miller98c7ad62000-03-09 21:27:49 +1100585 i = strtol(cp, &ep, 10);
586 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
587 int quoted = 0;
588 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000589 for (; *cp && (quoted || (*cp != ' ' &&
590 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100591 if (*cp == '\\' && cp[1] == '"')
592 cp++; /* Skip both */
593 else if (*cp == '"')
594 quoted = !quoted;
595 }
596 if (!*cp)
597 continue;
598 *cp++ = '\0';
599 }
600 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000601 public = key_new(KEY_RSA1);
602 if (key_read(public, &cp) != 1) {
603 cp = ep;
604 key_free(public);
605 public = key_new(KEY_UNSPEC);
606 if (key_read(public, &cp) != 1) {
607 key_free(public);
608 continue;
609 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100610 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000611 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000612 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100613 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000614 printf("%u %s %s (%s)\n", key_size(public), fp,
615 comment ? comment : "no comment", key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000616 if (log_level >= SYSLOG_LEVEL_VERBOSE)
617 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000618 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000619 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000620 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000621 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100622 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100623 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100624 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100625 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100626 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100627 exit(1);
628 }
Damien Miller95def091999-11-25 00:26:21 +1100629 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100630}
631
Damien Miller4b42d7f2005-03-01 21:48:35 +1100632static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100633printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100634{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000635 if (print_fingerprint) {
636 enum fp_rep rep;
637 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000638 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000639
640 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
641 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
642 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100643 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000644 printf("%u %s %s (%s)\n", key_size(public), fp, name,
645 key_type(public));
646 if (log_level >= SYSLOG_LEVEL_VERBOSE)
647 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000648 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000649 xfree(fp);
650 } else {
651 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
652 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100653 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000654 if (!key_write(public, f))
655 fatal("key_write failed");
656 fprintf(f, "\n");
657 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100658}
659
660static void
661do_known_hosts(struct passwd *pw, const char *name)
662{
663 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100664 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100665 char *cp, *cp2, *kp, *kp2;
666 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100667 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100668 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100669
670 if (!have_identity) {
671 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
672 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
673 sizeof(identity_file))
674 fatal("Specified known hosts path too long");
675 xfree(cp);
676 have_identity = 1;
677 }
678 if ((in = fopen(identity_file, "r")) == NULL)
679 fatal("fopen: %s", strerror(errno));
680
681 /*
682 * Find hosts goes to stdout, hash and deletions happen in-place
683 * A corner case is ssh-keygen -HF foo, which should go to stdout
684 */
685 if (!find_host && (hash_hosts || delete_host)) {
686 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
687 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
688 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
689 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
690 fatal("known_hosts path too long");
691 umask(077);
692 if ((c = mkstemp(tmp)) == -1)
693 fatal("mkstemp: %s", strerror(errno));
694 if ((out = fdopen(c, "w")) == NULL) {
695 c = errno;
696 unlink(tmp);
697 fatal("fdopen: %s", strerror(c));
698 }
699 inplace = 1;
700 }
701
702 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000703 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100704 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100705 skip = 1;
706 invalid = 1;
707 continue;
708 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000709 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100710 if (skip) {
711 skip = 0;
712 continue;
713 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000714 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100715
716 /* Skip leading whitespace, empty and comment lines. */
717 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
718 ;
719 if (!*cp || *cp == '\n' || *cp == '#') {
720 if (inplace)
721 fprintf(out, "%s\n", cp);
722 continue;
723 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100724 /* Check whether this is a CA key */
725 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
726 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
727 cp[sizeof(CA_MARKER) - 1] == '\t')) {
728 ca = 1;
729 cp += sizeof(CA_MARKER);
730 } else
731 ca = 0;
732
Damien Miller4b42d7f2005-03-01 21:48:35 +1100733 /* Find the end of the host name portion. */
734 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
735 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100736
Damien Miller4b42d7f2005-03-01 21:48:35 +1100737 if (*kp == '\0' || *(kp + 1) == '\0') {
738 error("line %d missing key: %.40s...",
739 num, line);
740 invalid = 1;
741 continue;
742 }
743 *kp++ = '\0';
744 kp2 = kp;
745
Damien Miller0a80ca12010-02-27 07:55:05 +1100746 pub = key_new(KEY_RSA1);
747 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100748 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100749 key_free(pub);
750 pub = key_new(KEY_UNSPEC);
751 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100752 error("line %d invalid key: %.40s...",
753 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100754 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100755 invalid = 1;
756 continue;
757 }
758 }
759
760 if (*cp == HASH_DELIM) {
761 if (find_host || delete_host) {
762 cp2 = host_hash(name, cp, strlen(cp));
763 if (cp2 == NULL) {
764 error("line %d: invalid hashed "
765 "name: %.64s...", num, line);
766 invalid = 1;
767 continue;
768 }
769 c = (strcmp(cp2, cp) == 0);
770 if (find_host && c) {
771 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100772 "line %d type %s%s\n", name,
773 num, key_type(pub),
774 ca ? " (CA key)" : "");
775 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100776 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100777 if (delete_host && !c && !ca)
778 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100779 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100780 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100781 } else {
782 if (find_host || delete_host) {
783 c = (match_hostname(name, cp,
784 strlen(cp)) == 1);
785 if (find_host && c) {
786 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100787 "line %d type %s%s\n", name,
788 num, key_type(pub),
789 ca ? " (CA key)" : "");
790 printhost(out, name, pub,
791 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100792 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100793 if (delete_host && !c && !ca)
794 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100795 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100796 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100797 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100798 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100799 if (ca) {
800 fprintf(stderr, "Warning: "
801 "ignoring CA key for host: "
802 "%.64s\n", cp2);
803 printhost(out, cp2, pub, ca, 0);
804 } else if (strcspn(cp2, "*?!") !=
805 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100806 fprintf(stderr, "Warning: "
807 "ignoring host name with "
808 "metacharacters: %.64s\n",
809 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100810 printhost(out, cp2, pub, ca, 0);
811 } else
812 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +1100813 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100814 has_unhashed = 1;
815 }
816 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100817 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100818 }
819 fclose(in);
820
821 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100822 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100823 identity_file);
824 if (inplace) {
825 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100826 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100827 fclose(out);
828 unlink(tmp);
829 }
830 exit(1);
831 }
832
833 if (inplace) {
834 fclose(out);
835
836 /* Backup existing file */
837 if (unlink(old) == -1 && errno != ENOENT)
838 fatal("unlink %.100s: %s", old, strerror(errno));
839 if (link(identity_file, old) == -1)
840 fatal("link %.100s to %.100s: %s", identity_file, old,
841 strerror(errno));
842 /* Move new one into place */
843 if (rename(tmp, identity_file) == -1) {
844 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
845 strerror(errno));
846 unlink(tmp);
847 unlink(old);
848 exit(1);
849 }
850
851 fprintf(stderr, "%s updated.\n", identity_file);
852 fprintf(stderr, "Original contents retained as %s\n", old);
853 if (has_unhashed) {
854 fprintf(stderr, "WARNING: %s contains unhashed "
855 "entries\n", old);
856 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000857 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100858 }
859 }
860
861 exit(0);
862}
863
Damien Miller95def091999-11-25 00:26:21 +1100864/*
865 * Perform changing a passphrase. The argument is the passwd structure
866 * for the current user.
867 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000868static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100869do_change_passphrase(struct passwd *pw)
870{
Damien Miller95def091999-11-25 00:26:21 +1100871 char *comment;
872 char *old_passphrase, *passphrase1, *passphrase2;
873 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000874 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100875
Damien Miller95def091999-11-25 00:26:21 +1100876 if (!have_identity)
877 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100878 if (stat(identity_file, &st) < 0) {
879 perror(identity_file);
880 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000881 }
Damien Miller95def091999-11-25 00:26:21 +1100882 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000883 private = key_load_private(identity_file, "", &comment);
884 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100885 if (identity_passphrase)
886 old_passphrase = xstrdup(identity_passphrase);
887 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000888 old_passphrase =
889 read_passphrase("Enter old passphrase: ",
890 RP_ALLOW_STDIN);
891 private = key_load_private(identity_file, old_passphrase,
892 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000893 memset(old_passphrase, 0, strlen(old_passphrase));
894 xfree(old_passphrase);
895 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100896 printf("Bad passphrase.\n");
897 exit(1);
898 }
Damien Miller95def091999-11-25 00:26:21 +1100899 }
900 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000901
Damien Miller95def091999-11-25 00:26:21 +1100902 /* Ask the new passphrase (twice). */
903 if (identity_new_passphrase) {
904 passphrase1 = xstrdup(identity_new_passphrase);
905 passphrase2 = NULL;
906 } else {
907 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000908 read_passphrase("Enter new passphrase (empty for no "
909 "passphrase): ", RP_ALLOW_STDIN);
910 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100911 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100912
913 /* Verify that they are the same. */
914 if (strcmp(passphrase1, passphrase2) != 0) {
915 memset(passphrase1, 0, strlen(passphrase1));
916 memset(passphrase2, 0, strlen(passphrase2));
917 xfree(passphrase1);
918 xfree(passphrase2);
919 printf("Pass phrases do not match. Try again.\n");
920 exit(1);
921 }
922 /* Destroy the other copy. */
923 memset(passphrase2, 0, strlen(passphrase2));
924 xfree(passphrase2);
925 }
926
927 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000928 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000929 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100930 memset(passphrase1, 0, strlen(passphrase1));
931 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000932 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100933 xfree(comment);
934 exit(1);
935 }
936 /* Destroy the passphrase and the copy of the key in memory. */
937 memset(passphrase1, 0, strlen(passphrase1));
938 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000939 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100940 xfree(comment);
941
942 printf("Your identification has been saved with the new passphrase.\n");
943 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000944}
945
Damien Miller37876e92003-05-15 10:19:46 +1000946/*
947 * Print the SSHFP RR.
948 */
Damien Millercb314822006-03-26 13:48:01 +1100949static int
950do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000951{
952 Key *public;
953 char *comment = NULL;
954 struct stat st;
955
Damien Millercb314822006-03-26 13:48:01 +1100956 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000957 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100958 if (stat(fname, &st) < 0) {
959 if (errno == ENOENT)
960 return 0;
961 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000962 exit(1);
963 }
Damien Millercb314822006-03-26 13:48:01 +1100964 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000965 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000966 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000967 key_free(public);
968 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100969 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000970 }
971 if (comment)
972 xfree(comment);
973
Damien Millercb314822006-03-26 13:48:01 +1100974 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000975 exit(1);
976}
Damien Miller37876e92003-05-15 10:19:46 +1000977
Damien Miller95def091999-11-25 00:26:21 +1100978/*
979 * Change the comment of a private key file.
980 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000981static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000982do_change_comment(struct passwd *pw)
983{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000984 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000985 Key *private;
986 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100987 struct stat st;
988 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000989 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990
Damien Miller95def091999-11-25 00:26:21 +1100991 if (!have_identity)
992 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100993 if (stat(identity_file, &st) < 0) {
994 perror(identity_file);
995 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000996 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000997 private = key_load_private(identity_file, "", &comment);
998 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100999 if (identity_passphrase)
1000 passphrase = xstrdup(identity_passphrase);
1001 else if (identity_new_passphrase)
1002 passphrase = xstrdup(identity_new_passphrase);
1003 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001004 passphrase = read_passphrase("Enter passphrase: ",
1005 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001006 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001007 private = key_load_private(identity_file, passphrase, &comment);
1008 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001009 memset(passphrase, 0, strlen(passphrase));
1010 xfree(passphrase);
1011 printf("Bad passphrase.\n");
1012 exit(1);
1013 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001014 } else {
1015 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001016 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001017 if (private->type != KEY_RSA1) {
1018 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1019 key_free(private);
1020 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001021 }
Damien Miller95def091999-11-25 00:26:21 +11001022 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001023
Damien Miller95def091999-11-25 00:26:21 +11001024 if (identity_comment) {
1025 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1026 } else {
1027 printf("Enter new comment: ");
1028 fflush(stdout);
1029 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1030 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001031 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001032 exit(1);
1033 }
Damien Miller14b017d2007-09-17 16:09:15 +10001034 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001035 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001036
Damien Miller95def091999-11-25 00:26:21 +11001037 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001038 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001039 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001040 memset(passphrase, 0, strlen(passphrase));
1041 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001042 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001043 xfree(comment);
1044 exit(1);
1045 }
Damien Miller95def091999-11-25 00:26:21 +11001046 memset(passphrase, 0, strlen(passphrase));
1047 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001048 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001049 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001050
Damien Miller95def091999-11-25 00:26:21 +11001051 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001052 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1053 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001054 printf("Could not save your public key in %s\n", identity_file);
1055 exit(1);
1056 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001057 f = fdopen(fd, "w");
1058 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001059 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001060 exit(1);
1061 }
Damien Millereba71ba2000-04-29 23:57:08 +10001062 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001063 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001064 key_free(public);
1065 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001066 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001067
Damien Miller95def091999-11-25 00:26:21 +11001068 xfree(comment);
1069
1070 printf("The comment in your key file has been changed.\n");
1071 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001072}
1073
Damien Miller0a80ca12010-02-27 07:55:05 +11001074static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001075fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001076{
1077 char from[32], to[32];
1078 static char ret[64];
1079 time_t tt;
1080 struct tm *tm;
1081
1082 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001083 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001084 return "forever";
1085
Damien Millerf2b70ca2010-03-05 07:39:35 +11001086 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001087 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001088 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001089 tm = localtime(&tt);
1090 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1091 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001092 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001093 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001094 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001095 tm = localtime(&tt);
1096 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1097 }
1098
Damien Millerf2b70ca2010-03-05 07:39:35 +11001099 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001100 snprintf(ret, sizeof(ret), "before %s", to);
1101 return ret;
1102 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001103 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001104 snprintf(ret, sizeof(ret), "after %s", from);
1105 return ret;
1106 }
1107
1108 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1109 return ret;
1110}
1111
1112static void
Damien Miller4e270b02010-04-16 15:56:21 +10001113add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001114{
1115 debug3("%s: %s", __func__, name);
1116 buffer_put_cstring(c, name);
1117 buffer_put_string(c, NULL, 0);
1118}
1119
1120static void
Damien Miller4e270b02010-04-16 15:56:21 +10001121add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001122{
1123 Buffer b;
1124
1125 debug3("%s: %s=%s", __func__, name, value);
1126 buffer_init(&b);
1127 buffer_put_cstring(&b, value);
1128
1129 buffer_put_cstring(c, name);
1130 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1131
1132 buffer_free(&b);
1133}
1134
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001135#define OPTIONS_CRITICAL 1
1136#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001137static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001138prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001139{
Damien Miller0a80ca12010-02-27 07:55:05 +11001140 buffer_clear(c);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001141 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1142 (certflags_flags & CERTOPT_X_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001143 add_flag_option(c, "permit-X11-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001144 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1145 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001146 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001147 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1148 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001149 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001150 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1151 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001152 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001153 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1154 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001155 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001156 if ((which & OPTIONS_CRITICAL) != 0 &&
1157 certflags_command != NULL)
1158 add_string_option(c, "force-command", certflags_command);
1159 if ((which & OPTIONS_CRITICAL) != 0 &&
1160 certflags_src_addr != NULL)
1161 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001162}
1163
1164static void
1165do_ca_sign(struct passwd *pw, int argc, char **argv)
1166{
1167 int i, fd;
1168 u_int n;
1169 Key *ca, *public;
1170 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1171 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001172 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001173
1174 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1175 if ((ca = load_identity(tmp)) == NULL)
1176 fatal("Couldn't load CA key \"%s\"", tmp);
1177 xfree(tmp);
1178
Damien Miller4e270b02010-04-16 15:56:21 +10001179 if (key_type_name != NULL) {
1180 switch (key_type_from_name(key_type_name)) {
1181 case KEY_RSA_CERT_V00:
1182 case KEY_DSA_CERT_V00:
1183 v00 = 1;
1184 break;
1185 case KEY_UNSPEC:
1186 if (strcasecmp(key_type_name, "v00") == 0) {
1187 v00 = 1;
1188 break;
1189 } else if (strcasecmp(key_type_name, "v01") == 0)
1190 break;
1191 /* FALLTHROUGH */
1192 default:
1193 fprintf(stderr, "unknown key type %s\n", key_type_name);
1194 exit(1);
1195 }
1196 }
1197
Damien Miller0a80ca12010-02-27 07:55:05 +11001198 for (i = 0; i < argc; i++) {
1199 /* Split list of principals */
1200 n = 0;
1201 if (cert_principals != NULL) {
1202 otmp = tmp = xstrdup(cert_principals);
1203 plist = NULL;
1204 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1205 plist = xrealloc(plist, n + 1, sizeof(*plist));
1206 if (*(plist[n] = xstrdup(cp)) == '\0')
1207 fatal("Empty principal name");
1208 }
1209 xfree(otmp);
1210 }
1211
1212 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1213 if ((public = key_load_public(tmp, &comment)) == NULL)
1214 fatal("%s: unable to open \"%s\"", __func__, tmp);
1215 if (public->type != KEY_RSA && public->type != KEY_DSA)
1216 fatal("%s: key \"%s\" type %s cannot be certified",
1217 __func__, tmp, key_type(public));
1218
1219 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001220 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001221 fatal("Could not upgrade key %s to certificate", tmp);
1222 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001223 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001224 public->cert->key_id = xstrdup(cert_key_id);
1225 public->cert->nprincipals = n;
1226 public->cert->principals = plist;
1227 public->cert->valid_after = cert_valid_from;
1228 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001229 if (v00) {
1230 prepare_options_buf(&public->cert->critical,
1231 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1232 } else {
1233 prepare_options_buf(&public->cert->critical,
1234 OPTIONS_CRITICAL);
1235 prepare_options_buf(&public->cert->extensions,
1236 OPTIONS_EXTENSIONS);
1237 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001238 public->cert->signature_key = key_from_private(ca);
1239
1240 if (key_certify(public, ca) != 0)
1241 fatal("Couldn't not certify key %s", tmp);
1242
1243 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1244 *cp = '\0';
1245 xasprintf(&out, "%s-cert.pub", tmp);
1246 xfree(tmp);
1247
1248 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1249 fatal("Could not open \"%s\" for writing: %s", out,
1250 strerror(errno));
1251 if ((f = fdopen(fd, "w")) == NULL)
1252 fatal("%s: fdopen: %s", __func__, strerror(errno));
1253 if (!key_write(public, f))
1254 fatal("Could not write certified key to %s", out);
1255 fprintf(f, " %s\n", comment);
1256 fclose(f);
1257
Damien Miller4e270b02010-04-16 15:56:21 +10001258 if (!quiet) {
1259 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1260 "valid %s", key_cert_type(public),
1261 out, public->cert->key_id, public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001262 cert_principals != NULL ? " for " : "",
1263 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001264 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001265 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001266
1267 key_free(public);
1268 xfree(out);
1269 }
1270 exit(0);
1271}
1272
1273static u_int64_t
1274parse_relative_time(const char *s, time_t now)
1275{
1276 int64_t mul, secs;
1277
1278 mul = *s == '-' ? -1 : 1;
1279
1280 if ((secs = convtime(s + 1)) == -1)
1281 fatal("Invalid relative certificate time %s", s);
1282 if (mul == -1 && secs > now)
1283 fatal("Certificate time %s cannot be represented", s);
1284 return now + (u_int64_t)(secs * mul);
1285}
1286
1287static u_int64_t
1288parse_absolute_time(const char *s)
1289{
1290 struct tm tm;
1291 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001292 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001293
Damien Miller2ca342b2010-03-03 12:14:15 +11001294 /*
1295 * POSIX strptime says "The application shall ensure that there
1296 * is white-space or other non-alphanumeric characters between
1297 * any two conversion specifications" so arrange things this way.
1298 */
1299 switch (strlen(s)) {
1300 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001301 fmt = "%Y-%m-%d";
1302 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001303 break;
1304 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001305 fmt = "%Y-%m-%dT%H:%M:%S";
1306 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001307 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1308 break;
1309 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001310 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001311 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001312
1313 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001314 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001315 fatal("Invalid certificate time %s", s);
1316 if ((tt = mktime(&tm)) < 0)
1317 fatal("Certificate time %s cannot be represented", s);
1318 return (u_int64_t)tt;
1319}
1320
1321static void
1322parse_cert_times(char *timespec)
1323{
1324 char *from, *to;
1325 time_t now = time(NULL);
1326 int64_t secs;
1327
1328 /* +timespec relative to now */
1329 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1330 if ((secs = convtime(timespec + 1)) == -1)
1331 fatal("Invalid relative certificate life %s", timespec);
1332 cert_valid_to = now + secs;
1333 /*
1334 * Backdate certificate one minute to avoid problems on hosts
1335 * with poorly-synchronised clocks.
1336 */
1337 cert_valid_from = ((now - 59)/ 60) * 60;
1338 return;
1339 }
1340
1341 /*
1342 * from:to, where
1343 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1344 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1345 */
1346 from = xstrdup(timespec);
1347 to = strchr(from, ':');
1348 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001349 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001350 *to++ = '\0';
1351
1352 if (*from == '-' || *from == '+')
1353 cert_valid_from = parse_relative_time(from, now);
1354 else
1355 cert_valid_from = parse_absolute_time(from);
1356
1357 if (*to == '-' || *to == '+')
1358 cert_valid_to = parse_relative_time(to, cert_valid_from);
1359 else
1360 cert_valid_to = parse_absolute_time(to);
1361
1362 if (cert_valid_to <= cert_valid_from)
1363 fatal("Empty certificate validity interval");
1364 xfree(from);
1365}
1366
1367static void
Damien Miller4e270b02010-04-16 15:56:21 +10001368add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001369{
1370 char *val;
1371
1372 if (strcmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001373 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001374 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001375 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001376 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001377 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001378 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001379 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001380 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001381 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001382 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001383 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001384 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001385 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001386 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001387 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001388 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001389 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001390 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001391 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001392 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001393 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001394 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1395 val = opt + 14;
1396 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001397 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001398 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001399 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001400 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001401 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1402 val = opt + 15;
1403 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001404 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001405 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001406 fatal("source-address already specified");
1407 if (addr_match_cidr_list(NULL, val) != 0)
1408 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001409 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001410 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001411 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001412}
1413
Ben Lindstrombba81212001-06-25 05:01:22 +00001414static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001415do_show_cert(struct passwd *pw)
1416{
1417 Key *key;
1418 struct stat st;
1419 char *key_fp, *ca_fp;
Damien Miller4e270b02010-04-16 15:56:21 +10001420 Buffer options, option;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001421 u_char *name, *data;
Damien Miller4e270b02010-04-16 15:56:21 +10001422 u_int i, dlen, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001423
1424 if (!have_identity)
1425 ask_filename(pw, "Enter file in which the key is");
1426 if (stat(identity_file, &st) < 0) {
1427 perror(identity_file);
1428 exit(1);
1429 }
1430 if ((key = key_load_public(identity_file, NULL)) == NULL)
1431 fatal("%s is not a public key", identity_file);
1432 if (!key_is_cert(key))
1433 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001434 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1435
Damien Millerf2b70ca2010-03-05 07:39:35 +11001436 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1437 ca_fp = key_fingerprint(key->cert->signature_key,
1438 SSH_FP_MD5, SSH_FP_HEX);
1439
1440 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001441 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1442 key_cert_type(key));
1443 printf(" Public key: %s %s\n", key_type(key), key_fp);
1444 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001445 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001446 printf(" Key ID: \"%s\"\n", key->cert->key_id);
1447 if (!v00)
1448 printf(" Serial: %llu\n", key->cert->serial);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001449 printf(" Valid: %s\n",
1450 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1451 printf(" Principals: ");
1452 if (key->cert->nprincipals == 0)
1453 printf("(none)\n");
1454 else {
1455 for (i = 0; i < key->cert->nprincipals; i++)
1456 printf("\n %s",
1457 key->cert->principals[i]);
1458 printf("\n");
1459 }
Damien Miller4e270b02010-04-16 15:56:21 +10001460 printf(" Critical Options: ");
1461 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001462 printf("(none)\n");
1463 else {
1464 printf("\n");
Damien Miller4e270b02010-04-16 15:56:21 +10001465 buffer_init(&options);
1466 buffer_append(&options,
1467 buffer_ptr(&key->cert->critical),
1468 buffer_len(&key->cert->critical));
1469 buffer_init(&option);
1470 while (buffer_len(&options) != 0) {
1471 name = buffer_get_string(&options, NULL);
1472 data = buffer_get_string_ptr(&options, &dlen);
1473 buffer_append(&option, data, dlen);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001474 printf(" %s", name);
1475 if (strcmp(name, "permit-X11-forwarding") == 0 ||
1476 strcmp(name, "permit-agent-forwarding") == 0 ||
1477 strcmp(name, "permit-port-forwarding") == 0 ||
1478 strcmp(name, "permit-pty") == 0 ||
1479 strcmp(name, "permit-user-rc") == 0)
1480 printf("\n");
1481 else if (strcmp(name, "force-command") == 0 ||
1482 strcmp(name, "source-address") == 0) {
Damien Miller4e270b02010-04-16 15:56:21 +10001483 data = buffer_get_string(&option, NULL);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001484 printf(" %s\n", data);
1485 xfree(data);
1486 } else {
Damien Miller4e270b02010-04-16 15:56:21 +10001487 printf(" UNKNOWN OPTION (len %u)\n",
1488 buffer_len(&option));
1489 buffer_clear(&option);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001490 }
1491 xfree(name);
Damien Miller4e270b02010-04-16 15:56:21 +10001492 if (buffer_len(&option) != 0)
1493 fatal("Option corrupt: extra data at end");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001494 }
Damien Miller4e270b02010-04-16 15:56:21 +10001495 buffer_free(&option);
1496 buffer_free(&options);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001497 }
Damien Miller4e270b02010-04-16 15:56:21 +10001498 if (!v00) {
1499 printf(" Extensions: ");
1500 if (buffer_len(&key->cert->extensions) == 0)
1501 printf("(none)\n");
1502 else {
1503 printf("\n");
1504 buffer_init(&options);
1505 buffer_append(&options,
1506 buffer_ptr(&key->cert->extensions),
1507 buffer_len(&key->cert->extensions));
1508 buffer_init(&option);
1509 while (buffer_len(&options) != 0) {
1510 name = buffer_get_string(&options, NULL);
1511 (void)buffer_get_string_ptr(&options, &dlen);
1512 printf(" %s UNKNOWN OPTION "
1513 "(len %u)\n", name, dlen);
1514 xfree(name);
1515 }
1516 buffer_free(&option);
1517 buffer_free(&options);
1518 }
1519 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001520 exit(0);
1521}
1522
1523static void
Damien Miller431f66b1999-11-21 18:31:57 +11001524usage(void)
1525{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001526 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001527 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001528 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001529 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001530 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001531 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001532 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001533#ifdef ENABLE_PKCS11
1534 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1535#endif
Darren Tucker26dc3e62007-02-19 22:09:06 +11001536 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001537 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1538 fprintf(stderr, " -f filename Filename of the key file.\n");
1539 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1540 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1541 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001542 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1543 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Darren Tucker26dc3e62007-02-19 22:09:06 +11001544 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001545 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001546 fprintf(stderr, " -l Show fingerprint of key file.\n");
1547 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001548 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001549 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001550 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001551 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1552 fprintf(stderr, " -p Change passphrase of private key file.\n");
1553 fprintf(stderr, " -q Quiet.\n");
1554 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1555 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001556 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001557 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1558 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1559 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001560 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001561 fprintf(stderr, " -v Verbose.\n");
1562 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1563 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001564 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001565
Damien Miller95def091999-11-25 00:26:21 +11001566 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001567}
1568
Damien Miller95def091999-11-25 00:26:21 +11001569/*
1570 * Main program for key management.
1571 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001572int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001573main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001574{
Damien Millera41c8b12002-01-22 23:05:08 +11001575 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller7ea845e2010-02-12 09:21:02 +11001576 char out_file[MAXPATHLEN], *pkcs11provider = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001577 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001578 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001579 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001580 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001581 int opt, type, fd;
Damien Millerbebbb7e2010-05-10 11:54:38 +10001582 u_int maxbits;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001583 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001584 int do_gen_candidates = 0, do_screen_candidates = 0;
1585 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001586 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001587 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001588
Damien Miller95def091999-11-25 00:26:21 +11001589 extern int optind;
1590 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001591
Darren Tuckerce321d82005-10-03 18:11:24 +10001592 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1593 sanitise_stdfd();
1594
Darren Tucker9ac56e92007-01-14 10:19:59 +11001595 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001596
Damien Millerd3a18572000-06-07 19:55:44 +10001597 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001598 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001599
Kevin Steves3a881912002-07-20 19:05:40 +00001600 init_rng();
1601 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001602
Damien Miller5428f641999-11-25 11:54:57 +11001603 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001604 pw = getpwuid(getuid());
1605 if (!pw) {
1606 printf("You don't exist, go away!\n");
1607 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001608 }
Damien Millereba71ba2000-04-29 23:57:08 +10001609 if (gethostname(hostname, sizeof(hostname)) < 0) {
1610 perror("gethostname");
1611 exit(1);
1612 }
Damien Miller5428f641999-11-25 11:54:57 +11001613
Damien Millerf2b70ca2010-03-05 07:39:35 +11001614 while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001615 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001616 switch (opt) {
1617 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001618 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001619 if (errstr)
1620 fatal("Bits has bad value %s (%s)",
1621 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001622 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001623 case 'F':
1624 find_host = 1;
1625 rr_hostname = optarg;
1626 break;
1627 case 'H':
1628 hash_hosts = 1;
1629 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001630 case 'I':
1631 cert_key_id = optarg;
1632 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001633 case 'R':
1634 delete_host = 1;
1635 rr_hostname = optarg;
1636 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001637 case 'L':
1638 show_cert = 1;
1639 break;
Damien Miller95def091999-11-25 00:26:21 +11001640 case 'l':
1641 print_fingerprint = 1;
1642 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001643 case 'B':
1644 print_bubblebabble = 1;
1645 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001646 case 'n':
1647 cert_principals = optarg;
1648 break;
Damien Miller95def091999-11-25 00:26:21 +11001649 case 'p':
1650 change_passphrase = 1;
1651 break;
Damien Miller95def091999-11-25 00:26:21 +11001652 case 'c':
1653 change_comment = 1;
1654 break;
Damien Miller95def091999-11-25 00:26:21 +11001655 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001656 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1657 sizeof(identity_file))
1658 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001659 have_identity = 1;
1660 break;
Damien Miller37876e92003-05-15 10:19:46 +10001661 case 'g':
1662 print_generic = 1;
1663 break;
Damien Miller95def091999-11-25 00:26:21 +11001664 case 'P':
1665 identity_passphrase = optarg;
1666 break;
Damien Miller95def091999-11-25 00:26:21 +11001667 case 'N':
1668 identity_new_passphrase = optarg;
1669 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001670 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10001671 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001672 break;
Damien Miller95def091999-11-25 00:26:21 +11001673 case 'C':
1674 identity_comment = optarg;
1675 break;
Damien Miller95def091999-11-25 00:26:21 +11001676 case 'q':
1677 quiet = 1;
1678 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001679 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001680 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001681 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001682 convert_to_ssh2 = 1;
1683 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001684 case 'h':
1685 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001686 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001687 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001688 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001689 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001690 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001691 convert_from_ssh2 = 1;
1692 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001693 case 'y':
1694 print_public = 1;
1695 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001696 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001697 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001698 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001699 case 's':
1700 ca_key_path = optarg;
1701 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001702 case 't':
1703 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001704 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001705 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001706 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001707 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001708 case 'v':
1709 if (log_level == SYSLOG_LEVEL_INFO)
1710 log_level = SYSLOG_LEVEL_DEBUG1;
1711 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001712 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001713 log_level < SYSLOG_LEVEL_DEBUG3)
1714 log_level++;
1715 }
1716 break;
Damien Miller37876e92003-05-15 10:19:46 +10001717 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001718 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001719 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001720 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001721 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1722 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001723 if (errstr)
1724 fatal("Desired generator has bad value: %s (%s)",
1725 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001726 break;
1727 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001728 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001729 if (errstr)
1730 fatal("Invalid number of trials: %s (%s)",
1731 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001732 break;
1733 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001734 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10001735 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10001736 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10001737 break;
1738 case 'G':
1739 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001740 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1741 sizeof(out_file))
1742 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001743 break;
1744 case 'T':
1745 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001746 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1747 sizeof(out_file))
1748 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001749 break;
1750 case 'S':
1751 /* XXX - also compare length against bits */
1752 if (BN_hex2bn(&start, optarg) == 0)
1753 fatal("Invalid start point.");
1754 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001755 case 'V':
1756 parse_cert_times(optarg);
1757 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001758 case 'z':
1759 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
1760 if (errstr)
1761 fatal("Invalid serial number: %s", errstr);
1762 break;
Damien Miller95def091999-11-25 00:26:21 +11001763 case '?':
1764 default:
1765 usage();
1766 }
1767 }
Darren Tucker06930c72003-12-31 11:34:51 +11001768
1769 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001770 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001771
Damien Miller0a80ca12010-02-27 07:55:05 +11001772 argv += optind;
1773 argc -= optind;
1774
1775 if (ca_key_path != NULL) {
1776 if (argc < 1) {
1777 printf("Too few arguments.\n");
1778 usage();
1779 }
1780 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11001781 printf("Too many arguments.\n");
1782 usage();
1783 }
1784 if (change_passphrase && change_comment) {
1785 printf("Can only have one of -p and -c.\n");
1786 usage();
1787 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001788 if (print_fingerprint && (delete_host || hash_hosts)) {
1789 printf("Cannot use -l with -D or -R.\n");
1790 usage();
1791 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001792 if (ca_key_path != NULL) {
1793 if (cert_key_id == NULL)
1794 fatal("Must specify key id (-I) when certifying");
1795 do_ca_sign(pw, argc, argv);
1796 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001797 if (show_cert)
1798 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001799 if (delete_host || hash_hosts || find_host)
1800 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001801 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001802 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001803 if (change_passphrase)
1804 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001805 if (change_comment)
1806 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001807 if (convert_to_ssh2)
1808 do_convert_to_ssh2(pw);
1809 if (convert_from_ssh2)
1810 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001811 if (print_public)
1812 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001813 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001814 unsigned int n = 0;
1815
1816 if (have_identity) {
1817 n = do_print_resource_record(pw,
1818 identity_file, rr_hostname);
1819 if (n == 0) {
1820 perror(identity_file);
1821 exit(1);
1822 }
1823 exit(0);
1824 } else {
1825
1826 n += do_print_resource_record(pw,
1827 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1828 n += do_print_resource_record(pw,
1829 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1830
1831 if (n == 0)
1832 fatal("no keys found.");
1833 exit(0);
1834 }
Damien Miller37876e92003-05-15 10:19:46 +10001835 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001836 if (pkcs11provider != NULL)
1837 do_download(pw, pkcs11provider);
Damien Miller95def091999-11-25 00:26:21 +11001838
Darren Tucker019cefe2003-08-02 22:40:07 +10001839 if (do_gen_candidates) {
1840 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001841
Darren Tucker019cefe2003-08-02 22:40:07 +10001842 if (out == NULL) {
1843 error("Couldn't open modulus candidate file \"%s\": %s",
1844 out_file, strerror(errno));
1845 return (1);
1846 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001847 if (bits == 0)
1848 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001849 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001850 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001851
1852 return (0);
1853 }
1854
1855 if (do_screen_candidates) {
1856 FILE *in;
1857 FILE *out = fopen(out_file, "w");
1858
1859 if (have_identity && strcmp(identity_file, "-") != 0) {
1860 if ((in = fopen(identity_file, "r")) == NULL) {
1861 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001862 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001863 strerror(errno));
1864 }
1865 } else
1866 in = stdin;
1867
1868 if (out == NULL) {
1869 fatal("Couldn't open moduli file \"%s\": %s",
1870 out_file, strerror(errno));
1871 }
1872 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001873 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001874 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001875 }
1876
Damien Miller95def091999-11-25 00:26:21 +11001877 arc4random_stir();
1878
Damien Millerf14be5c2005-11-05 15:15:49 +11001879 if (key_type_name == NULL)
1880 key_type_name = "rsa";
1881
Damien Millere39cacc2000-11-29 12:18:44 +11001882 type = key_type_from_name(key_type_name);
1883 if (type == KEY_UNSPEC) {
1884 fprintf(stderr, "unknown key type %s\n", key_type_name);
1885 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001886 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001887 if (bits == 0)
1888 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Damien Millerbebbb7e2010-05-10 11:54:38 +10001889 maxbits = (type == KEY_DSA) ?
1890 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
1891 if (bits > maxbits) {
1892 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
1893 exit(1);
1894 }
Tim Rice660c3402005-11-28 17:45:32 -08001895 if (type == KEY_DSA && bits != 1024)
1896 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001897 if (!quiet)
1898 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001899 private = key_generate(type, bits);
1900 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001901 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001902 exit(1);
1903 }
1904 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001905
1906 if (!have_identity)
1907 ask_filename(pw, "Enter file in which to save the key");
1908
Damien Miller788f2122005-11-05 15:14:59 +11001909 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10001910 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
1911 pw->pw_dir, _PATH_SSH_USER_DIR);
1912 if (strstr(identity_file, dotsshdir) != NULL) {
1913 if (stat(dotsshdir, &st) < 0) {
1914 if (errno != ENOENT) {
1915 error("Could not stat %s: %s", dotsshdir,
1916 strerror(errno));
1917 } else if (mkdir(dotsshdir, 0700) < 0) {
1918 error("Could not create directory '%s': %s",
1919 dotsshdir, strerror(errno));
1920 } else if (!quiet)
1921 printf("Created directory '%s'.\n", dotsshdir);
1922 }
Damien Miller95def091999-11-25 00:26:21 +11001923 }
1924 /* If the file already exists, ask the user to confirm. */
1925 if (stat(identity_file, &st) >= 0) {
1926 char yesno[3];
1927 printf("%s already exists.\n", identity_file);
1928 printf("Overwrite (y/n)? ");
1929 fflush(stdout);
1930 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1931 exit(1);
1932 if (yesno[0] != 'y' && yesno[0] != 'Y')
1933 exit(1);
1934 }
1935 /* Ask for a passphrase (twice). */
1936 if (identity_passphrase)
1937 passphrase1 = xstrdup(identity_passphrase);
1938 else if (identity_new_passphrase)
1939 passphrase1 = xstrdup(identity_new_passphrase);
1940 else {
1941passphrase_again:
1942 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001943 read_passphrase("Enter passphrase (empty for no "
1944 "passphrase): ", RP_ALLOW_STDIN);
1945 passphrase2 = read_passphrase("Enter same passphrase again: ",
1946 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001947 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001948 /*
1949 * The passphrases do not match. Clear them and
1950 * retry.
1951 */
Damien Miller95def091999-11-25 00:26:21 +11001952 memset(passphrase1, 0, strlen(passphrase1));
1953 memset(passphrase2, 0, strlen(passphrase2));
1954 xfree(passphrase1);
1955 xfree(passphrase2);
1956 printf("Passphrases do not match. Try again.\n");
1957 goto passphrase_again;
1958 }
1959 /* Clear the other copy of the passphrase. */
1960 memset(passphrase2, 0, strlen(passphrase2));
1961 xfree(passphrase2);
1962 }
1963
Damien Miller95def091999-11-25 00:26:21 +11001964 if (identity_comment) {
1965 strlcpy(comment, identity_comment, sizeof(comment));
1966 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11001967 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001968 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1969 }
1970
1971 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001972 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001973 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001974 memset(passphrase1, 0, strlen(passphrase1));
1975 xfree(passphrase1);
1976 exit(1);
1977 }
1978 /* Clear the passphrase. */
1979 memset(passphrase1, 0, strlen(passphrase1));
1980 xfree(passphrase1);
1981
1982 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001983 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001984 arc4random_stir();
1985
1986 if (!quiet)
1987 printf("Your identification has been saved in %s.\n", identity_file);
1988
Damien Miller95def091999-11-25 00:26:21 +11001989 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001990 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1991 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001992 printf("Could not save your public key in %s\n", identity_file);
1993 exit(1);
1994 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001995 f = fdopen(fd, "w");
1996 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001997 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001998 exit(1);
1999 }
Damien Millereba71ba2000-04-29 23:57:08 +10002000 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002001 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002002 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002003 fclose(f);
2004
2005 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002006 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002007 char *ra = key_fingerprint(public, SSH_FP_MD5,
2008 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002009 printf("Your public key has been saved in %s.\n",
2010 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002011 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002012 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002013 printf("The key's randomart image is:\n");
2014 printf("%s\n", ra);
2015 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002016 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002017 }
Damien Millereba71ba2000-04-29 23:57:08 +10002018
2019 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002020 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002021}