blob: de7c4409d07077fad60bea81991e2030467b9be1 [file] [log] [blame]
Damien Millerd834d352010-06-26 09:48:02 +10001/* $OpenBSD: ssh-keygen.c,v 1.192 2010/06/23 02:59: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 }
Damien Millerba3420a2010-06-26 09:39:07 +1000417 if ((fp = fopen(identity_file, "r")) == NULL)
418 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000419 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100420 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
421 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000422 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000423 if (strncmp(line, "----", 4) == 0 ||
424 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100425 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
426 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000427 if (strstr(line, " END ") != NULL) {
428 break;
429 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000430 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000431 continue;
432 }
Damien Miller30c3d422000-05-09 11:02:59 +1000433 if (escaped) {
434 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000435 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000436 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000437 }
Damien Millereba71ba2000-04-29 23:57:08 +1000438 strlcat(encoded, line, sizeof(encoded));
439 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000440 len = strlen(encoded);
441 if (((len % 4) == 3) &&
442 (encoded[len-1] == '=') &&
443 (encoded[len-2] == '=') &&
444 (encoded[len-3] == '='))
445 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100446 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000447 if (blen < 0) {
448 fprintf(stderr, "uudecode failed.\n");
449 exit(1);
450 }
Damien Miller874d77b2000-10-14 16:23:11 +1100451 k = private ?
452 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100453 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100454 if (k == NULL) {
455 fprintf(stderr, "decode blob failed.\n");
456 exit(1);
457 }
458 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000459 (k->type == KEY_DSA ?
460 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
461 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100462 key_write(k, stdout);
463 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100464 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100465 exit(1);
466 }
Damien Millereba71ba2000-04-29 23:57:08 +1000467 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100468 if (!private)
469 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000470 fclose(fp);
471 exit(0);
472}
473
Ben Lindstrombba81212001-06-25 05:01:22 +0000474static void
Damien Millereba71ba2000-04-29 23:57:08 +1000475do_print_public(struct passwd *pw)
476{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000477 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000478 struct stat st;
479
480 if (!have_identity)
481 ask_filename(pw, "Enter file in which the key is");
482 if (stat(identity_file, &st) < 0) {
483 perror(identity_file);
484 exit(1);
485 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000486 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000487 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000488 fprintf(stderr, "load failed\n");
489 exit(1);
490 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000491 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000492 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000493 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000494 fprintf(stdout, "\n");
495 exit(0);
496}
497
Ben Lindstromcd392282001-07-04 03:44:03 +0000498static void
Damien Miller86cbb442010-02-12 09:22:57 +1100499do_download(struct passwd *pw, char *pkcs11provider)
Ben Lindstromcd392282001-07-04 03:44:03 +0000500{
Damien Miller7ea845e2010-02-12 09:21:02 +1100501#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000502 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100503 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000504
Damien Miller7ea845e2010-02-12 09:21:02 +1100505 pkcs11_init(0);
506 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
507 if (nkeys <= 0)
508 fatal("cannot read public key from pkcs11");
509 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000510 key_write(keys[i], stdout);
511 key_free(keys[i]);
512 fprintf(stdout, "\n");
513 }
514 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100515 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000516 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100517#else
518 fatal("no pkcs11 support");
519#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000520}
Ben Lindstromcd392282001-07-04 03:44:03 +0000521
Ben Lindstrombba81212001-06-25 05:01:22 +0000522static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100523do_fingerprint(struct passwd *pw)
524{
Damien Miller98c7ad62000-03-09 21:27:49 +1100525 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000526 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000527 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100528 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000529 enum fp_rep rep;
530 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100531 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100532
Ben Lindstromd0fca422001-03-26 13:44:06 +0000533 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
534 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000535
Damien Miller95def091999-11-25 00:26:21 +1100536 if (!have_identity)
537 ask_filename(pw, "Enter file in which the key is");
538 if (stat(identity_file, &st) < 0) {
539 perror(identity_file);
540 exit(1);
541 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000542 public = key_load_public(identity_file, &comment);
543 if (public != NULL) {
544 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100545 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000546 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
547 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000548 if (log_level >= SYSLOG_LEVEL_VERBOSE)
549 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100550 key_free(public);
551 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000552 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000553 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100554 exit(0);
555 }
Damien Miller40b59852006-06-13 13:00:25 +1000556 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000557 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000558 comment = NULL;
559 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100560
Damien Millerba3420a2010-06-26 09:39:07 +1000561 if ((f = fopen(identity_file, "r")) == NULL)
562 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100563
Damien Millerba3420a2010-06-26 09:39:07 +1000564 while (fgets(line, sizeof(line), f)) {
565 if ((cp = strchr(line, '\n')) == NULL) {
566 error("line %d too long: %.40s...",
567 num + 1, line);
568 skip = 1;
569 continue;
Damien Miller95def091999-11-25 00:26:21 +1100570 }
Damien Millerba3420a2010-06-26 09:39:07 +1000571 num++;
572 if (skip) {
573 skip = 0;
574 continue;
575 }
576 *cp = '\0';
577
578 /* Skip leading whitespace, empty and comment lines. */
579 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
580 ;
581 if (!*cp || *cp == '\n' || *cp == '#')
582 continue;
583 i = strtol(cp, &ep, 10);
584 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
585 int quoted = 0;
586 comment = cp;
587 for (; *cp && (quoted || (*cp != ' ' &&
588 *cp != '\t')); cp++) {
589 if (*cp == '\\' && cp[1] == '"')
590 cp++; /* Skip both */
591 else if (*cp == '"')
592 quoted = !quoted;
593 }
594 if (!*cp)
595 continue;
596 *cp++ = '\0';
597 }
598 ep = cp;
599 public = key_new(KEY_RSA1);
600 if (key_read(public, &cp) != 1) {
601 cp = ep;
602 key_free(public);
603 public = key_new(KEY_UNSPEC);
604 if (key_read(public, &cp) != 1) {
605 key_free(public);
606 continue;
607 }
608 }
609 comment = *cp ? cp : comment;
610 fp = key_fingerprint(public, fptype, rep);
611 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
612 printf("%u %s %s (%s)\n", key_size(public), fp,
613 comment ? comment : "no comment", key_type(public));
614 if (log_level >= SYSLOG_LEVEL_VERBOSE)
615 printf("%s\n", ra);
616 xfree(ra);
617 xfree(fp);
618 key_free(public);
619 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100620 }
Damien Millerba3420a2010-06-26 09:39:07 +1000621 fclose(f);
622
Damien Miller98c7ad62000-03-09 21:27:49 +1100623 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100624 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100625 exit(1);
626 }
Damien Miller95def091999-11-25 00:26:21 +1100627 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100628}
629
Damien Miller4b42d7f2005-03-01 21:48:35 +1100630static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100631printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100632{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000633 if (print_fingerprint) {
634 enum fp_rep rep;
635 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000636 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000637
638 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
639 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
640 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100641 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000642 printf("%u %s %s (%s)\n", key_size(public), fp, name,
643 key_type(public));
644 if (log_level >= SYSLOG_LEVEL_VERBOSE)
645 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000646 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000647 xfree(fp);
648 } else {
649 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
650 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100651 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000652 if (!key_write(public, f))
653 fatal("key_write failed");
654 fprintf(f, "\n");
655 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100656}
657
658static void
659do_known_hosts(struct passwd *pw, const char *name)
660{
661 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100662 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100663 char *cp, *cp2, *kp, *kp2;
664 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100665 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100666 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100667
668 if (!have_identity) {
669 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
670 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
671 sizeof(identity_file))
672 fatal("Specified known hosts path too long");
673 xfree(cp);
674 have_identity = 1;
675 }
676 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +1000677 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +1100678
679 /*
680 * Find hosts goes to stdout, hash and deletions happen in-place
681 * A corner case is ssh-keygen -HF foo, which should go to stdout
682 */
683 if (!find_host && (hash_hosts || delete_host)) {
684 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
685 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
686 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
687 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
688 fatal("known_hosts path too long");
689 umask(077);
690 if ((c = mkstemp(tmp)) == -1)
691 fatal("mkstemp: %s", strerror(errno));
692 if ((out = fdopen(c, "w")) == NULL) {
693 c = errno;
694 unlink(tmp);
695 fatal("fdopen: %s", strerror(c));
696 }
697 inplace = 1;
698 }
699
700 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000701 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100702 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100703 skip = 1;
704 invalid = 1;
705 continue;
706 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000707 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100708 if (skip) {
709 skip = 0;
710 continue;
711 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000712 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100713
714 /* Skip leading whitespace, empty and comment lines. */
715 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
716 ;
717 if (!*cp || *cp == '\n' || *cp == '#') {
718 if (inplace)
719 fprintf(out, "%s\n", cp);
720 continue;
721 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100722 /* Check whether this is a CA key */
723 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
724 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
725 cp[sizeof(CA_MARKER) - 1] == '\t')) {
726 ca = 1;
727 cp += sizeof(CA_MARKER);
728 } else
729 ca = 0;
730
Damien Miller4b42d7f2005-03-01 21:48:35 +1100731 /* Find the end of the host name portion. */
732 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
733 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100734
Damien Miller4b42d7f2005-03-01 21:48:35 +1100735 if (*kp == '\0' || *(kp + 1) == '\0') {
736 error("line %d missing key: %.40s...",
737 num, line);
738 invalid = 1;
739 continue;
740 }
741 *kp++ = '\0';
742 kp2 = kp;
743
Damien Miller0a80ca12010-02-27 07:55:05 +1100744 pub = key_new(KEY_RSA1);
745 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100746 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100747 key_free(pub);
748 pub = key_new(KEY_UNSPEC);
749 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100750 error("line %d invalid key: %.40s...",
751 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100752 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100753 invalid = 1;
754 continue;
755 }
756 }
757
758 if (*cp == HASH_DELIM) {
759 if (find_host || delete_host) {
760 cp2 = host_hash(name, cp, strlen(cp));
761 if (cp2 == NULL) {
762 error("line %d: invalid hashed "
763 "name: %.64s...", num, line);
764 invalid = 1;
765 continue;
766 }
767 c = (strcmp(cp2, cp) == 0);
768 if (find_host && c) {
769 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100770 "line %d type %s%s\n", name,
771 num, key_type(pub),
772 ca ? " (CA key)" : "");
773 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100774 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100775 if (delete_host && !c && !ca)
776 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100777 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100778 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100779 } else {
780 if (find_host || delete_host) {
781 c = (match_hostname(name, cp,
782 strlen(cp)) == 1);
783 if (find_host && c) {
784 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100785 "line %d type %s%s\n", name,
786 num, key_type(pub),
787 ca ? " (CA key)" : "");
788 printhost(out, name, pub,
789 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100790 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100791 if (delete_host && !c && !ca)
792 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100793 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100794 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100795 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100796 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100797 if (ca) {
798 fprintf(stderr, "Warning: "
799 "ignoring CA key for host: "
800 "%.64s\n", cp2);
801 printhost(out, cp2, pub, ca, 0);
802 } else if (strcspn(cp2, "*?!") !=
803 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100804 fprintf(stderr, "Warning: "
805 "ignoring host name with "
806 "metacharacters: %.64s\n",
807 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100808 printhost(out, cp2, pub, ca, 0);
809 } else
810 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +1100811 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100812 has_unhashed = 1;
813 }
814 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100815 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100816 }
817 fclose(in);
818
819 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100820 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100821 identity_file);
822 if (inplace) {
823 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100824 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100825 fclose(out);
826 unlink(tmp);
827 }
828 exit(1);
829 }
830
831 if (inplace) {
832 fclose(out);
833
834 /* Backup existing file */
835 if (unlink(old) == -1 && errno != ENOENT)
836 fatal("unlink %.100s: %s", old, strerror(errno));
837 if (link(identity_file, old) == -1)
838 fatal("link %.100s to %.100s: %s", identity_file, old,
839 strerror(errno));
840 /* Move new one into place */
841 if (rename(tmp, identity_file) == -1) {
842 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
843 strerror(errno));
844 unlink(tmp);
845 unlink(old);
846 exit(1);
847 }
848
849 fprintf(stderr, "%s updated.\n", identity_file);
850 fprintf(stderr, "Original contents retained as %s\n", old);
851 if (has_unhashed) {
852 fprintf(stderr, "WARNING: %s contains unhashed "
853 "entries\n", old);
854 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000855 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100856 }
857 }
858
859 exit(0);
860}
861
Damien Miller95def091999-11-25 00:26:21 +1100862/*
863 * Perform changing a passphrase. The argument is the passwd structure
864 * for the current user.
865 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000866static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100867do_change_passphrase(struct passwd *pw)
868{
Damien Miller95def091999-11-25 00:26:21 +1100869 char *comment;
870 char *old_passphrase, *passphrase1, *passphrase2;
871 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000872 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100873
Damien Miller95def091999-11-25 00:26:21 +1100874 if (!have_identity)
875 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100876 if (stat(identity_file, &st) < 0) {
877 perror(identity_file);
878 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000879 }
Damien Miller95def091999-11-25 00:26:21 +1100880 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000881 private = key_load_private(identity_file, "", &comment);
882 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100883 if (identity_passphrase)
884 old_passphrase = xstrdup(identity_passphrase);
885 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000886 old_passphrase =
887 read_passphrase("Enter old passphrase: ",
888 RP_ALLOW_STDIN);
889 private = key_load_private(identity_file, old_passphrase,
890 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000891 memset(old_passphrase, 0, strlen(old_passphrase));
892 xfree(old_passphrase);
893 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100894 printf("Bad passphrase.\n");
895 exit(1);
896 }
Damien Miller95def091999-11-25 00:26:21 +1100897 }
898 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000899
Damien Miller95def091999-11-25 00:26:21 +1100900 /* Ask the new passphrase (twice). */
901 if (identity_new_passphrase) {
902 passphrase1 = xstrdup(identity_new_passphrase);
903 passphrase2 = NULL;
904 } else {
905 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000906 read_passphrase("Enter new passphrase (empty for no "
907 "passphrase): ", RP_ALLOW_STDIN);
908 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100909 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100910
911 /* Verify that they are the same. */
912 if (strcmp(passphrase1, passphrase2) != 0) {
913 memset(passphrase1, 0, strlen(passphrase1));
914 memset(passphrase2, 0, strlen(passphrase2));
915 xfree(passphrase1);
916 xfree(passphrase2);
917 printf("Pass phrases do not match. Try again.\n");
918 exit(1);
919 }
920 /* Destroy the other copy. */
921 memset(passphrase2, 0, strlen(passphrase2));
922 xfree(passphrase2);
923 }
924
925 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000926 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000927 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100928 memset(passphrase1, 0, strlen(passphrase1));
929 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000930 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100931 xfree(comment);
932 exit(1);
933 }
934 /* Destroy the passphrase and the copy of the key in memory. */
935 memset(passphrase1, 0, strlen(passphrase1));
936 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000937 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100938 xfree(comment);
939
940 printf("Your identification has been saved with the new passphrase.\n");
941 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000942}
943
Damien Miller37876e92003-05-15 10:19:46 +1000944/*
945 * Print the SSHFP RR.
946 */
Damien Millercb314822006-03-26 13:48:01 +1100947static int
948do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000949{
950 Key *public;
951 char *comment = NULL;
952 struct stat st;
953
Damien Millercb314822006-03-26 13:48:01 +1100954 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000955 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100956 if (stat(fname, &st) < 0) {
957 if (errno == ENOENT)
958 return 0;
959 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000960 exit(1);
961 }
Damien Millercb314822006-03-26 13:48:01 +1100962 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000963 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000964 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000965 key_free(public);
966 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100967 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000968 }
969 if (comment)
970 xfree(comment);
971
Damien Millercb314822006-03-26 13:48:01 +1100972 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000973 exit(1);
974}
Damien Miller37876e92003-05-15 10:19:46 +1000975
Damien Miller95def091999-11-25 00:26:21 +1100976/*
977 * Change the comment of a private key file.
978 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000979static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000980do_change_comment(struct passwd *pw)
981{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000982 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000983 Key *private;
984 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100985 struct stat st;
986 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000987 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000988
Damien Miller95def091999-11-25 00:26:21 +1100989 if (!have_identity)
990 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100991 if (stat(identity_file, &st) < 0) {
992 perror(identity_file);
993 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000994 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000995 private = key_load_private(identity_file, "", &comment);
996 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100997 if (identity_passphrase)
998 passphrase = xstrdup(identity_passphrase);
999 else if (identity_new_passphrase)
1000 passphrase = xstrdup(identity_new_passphrase);
1001 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001002 passphrase = read_passphrase("Enter passphrase: ",
1003 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001004 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001005 private = key_load_private(identity_file, passphrase, &comment);
1006 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001007 memset(passphrase, 0, strlen(passphrase));
1008 xfree(passphrase);
1009 printf("Bad passphrase.\n");
1010 exit(1);
1011 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001012 } else {
1013 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001014 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001015 if (private->type != KEY_RSA1) {
1016 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1017 key_free(private);
1018 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001019 }
Damien Miller95def091999-11-25 00:26:21 +11001020 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001021
Damien Miller95def091999-11-25 00:26:21 +11001022 if (identity_comment) {
1023 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1024 } else {
1025 printf("Enter new comment: ");
1026 fflush(stdout);
1027 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1028 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001029 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001030 exit(1);
1031 }
Damien Miller14b017d2007-09-17 16:09:15 +10001032 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001033 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001034
Damien Miller95def091999-11-25 00:26:21 +11001035 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001036 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001037 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001038 memset(passphrase, 0, strlen(passphrase));
1039 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001040 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001041 xfree(comment);
1042 exit(1);
1043 }
Damien Miller95def091999-11-25 00:26:21 +11001044 memset(passphrase, 0, strlen(passphrase));
1045 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001046 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001047 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001048
Damien Miller95def091999-11-25 00:26:21 +11001049 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001050 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1051 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001052 printf("Could not save your public key in %s\n", identity_file);
1053 exit(1);
1054 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001055 f = fdopen(fd, "w");
1056 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001057 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001058 exit(1);
1059 }
Damien Millereba71ba2000-04-29 23:57:08 +10001060 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001061 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001062 key_free(public);
1063 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001064 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001065
Damien Miller95def091999-11-25 00:26:21 +11001066 xfree(comment);
1067
1068 printf("The comment in your key file has been changed.\n");
1069 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001070}
1071
Damien Miller0a80ca12010-02-27 07:55:05 +11001072static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001073fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001074{
1075 char from[32], to[32];
1076 static char ret[64];
1077 time_t tt;
1078 struct tm *tm;
1079
1080 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001081 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001082 return "forever";
1083
Damien Millerf2b70ca2010-03-05 07:39:35 +11001084 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001085 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001086 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001087 tm = localtime(&tt);
1088 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1089 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001090 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001091 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001092 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001093 tm = localtime(&tt);
1094 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1095 }
1096
Damien Millerf2b70ca2010-03-05 07:39:35 +11001097 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001098 snprintf(ret, sizeof(ret), "before %s", to);
1099 return ret;
1100 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001101 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001102 snprintf(ret, sizeof(ret), "after %s", from);
1103 return ret;
1104 }
1105
1106 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1107 return ret;
1108}
1109
1110static void
Damien Miller4e270b02010-04-16 15:56:21 +10001111add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001112{
1113 debug3("%s: %s", __func__, name);
1114 buffer_put_cstring(c, name);
1115 buffer_put_string(c, NULL, 0);
1116}
1117
1118static void
Damien Miller4e270b02010-04-16 15:56:21 +10001119add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001120{
1121 Buffer b;
1122
1123 debug3("%s: %s=%s", __func__, name, value);
1124 buffer_init(&b);
1125 buffer_put_cstring(&b, value);
1126
1127 buffer_put_cstring(c, name);
1128 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1129
1130 buffer_free(&b);
1131}
1132
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001133#define OPTIONS_CRITICAL 1
1134#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001135static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001136prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001137{
Damien Miller0a80ca12010-02-27 07:55:05 +11001138 buffer_clear(c);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001139 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1140 (certflags_flags & CERTOPT_X_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001141 add_flag_option(c, "permit-X11-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001142 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1143 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001144 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001145 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1146 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001147 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001148 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1149 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001150 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001151 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1152 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001153 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001154 if ((which & OPTIONS_CRITICAL) != 0 &&
1155 certflags_command != NULL)
1156 add_string_option(c, "force-command", certflags_command);
1157 if ((which & OPTIONS_CRITICAL) != 0 &&
1158 certflags_src_addr != NULL)
1159 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001160}
1161
1162static void
1163do_ca_sign(struct passwd *pw, int argc, char **argv)
1164{
1165 int i, fd;
1166 u_int n;
1167 Key *ca, *public;
1168 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1169 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001170 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001171
1172 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1173 if ((ca = load_identity(tmp)) == NULL)
1174 fatal("Couldn't load CA key \"%s\"", tmp);
1175 xfree(tmp);
1176
Damien Miller4e270b02010-04-16 15:56:21 +10001177 if (key_type_name != NULL) {
1178 switch (key_type_from_name(key_type_name)) {
1179 case KEY_RSA_CERT_V00:
1180 case KEY_DSA_CERT_V00:
1181 v00 = 1;
1182 break;
1183 case KEY_UNSPEC:
1184 if (strcasecmp(key_type_name, "v00") == 0) {
1185 v00 = 1;
1186 break;
1187 } else if (strcasecmp(key_type_name, "v01") == 0)
1188 break;
1189 /* FALLTHROUGH */
1190 default:
1191 fprintf(stderr, "unknown key type %s\n", key_type_name);
1192 exit(1);
1193 }
1194 }
1195
Damien Miller0a80ca12010-02-27 07:55:05 +11001196 for (i = 0; i < argc; i++) {
1197 /* Split list of principals */
1198 n = 0;
1199 if (cert_principals != NULL) {
1200 otmp = tmp = xstrdup(cert_principals);
1201 plist = NULL;
1202 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1203 plist = xrealloc(plist, n + 1, sizeof(*plist));
1204 if (*(plist[n] = xstrdup(cp)) == '\0')
1205 fatal("Empty principal name");
1206 }
1207 xfree(otmp);
1208 }
1209
1210 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1211 if ((public = key_load_public(tmp, &comment)) == NULL)
1212 fatal("%s: unable to open \"%s\"", __func__, tmp);
1213 if (public->type != KEY_RSA && public->type != KEY_DSA)
1214 fatal("%s: key \"%s\" type %s cannot be certified",
1215 __func__, tmp, key_type(public));
1216
1217 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001218 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001219 fatal("Could not upgrade key %s to certificate", tmp);
1220 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001221 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001222 public->cert->key_id = xstrdup(cert_key_id);
1223 public->cert->nprincipals = n;
1224 public->cert->principals = plist;
1225 public->cert->valid_after = cert_valid_from;
1226 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001227 if (v00) {
1228 prepare_options_buf(&public->cert->critical,
1229 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1230 } else {
1231 prepare_options_buf(&public->cert->critical,
1232 OPTIONS_CRITICAL);
1233 prepare_options_buf(&public->cert->extensions,
1234 OPTIONS_EXTENSIONS);
1235 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001236 public->cert->signature_key = key_from_private(ca);
1237
1238 if (key_certify(public, ca) != 0)
1239 fatal("Couldn't not certify key %s", tmp);
1240
1241 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1242 *cp = '\0';
1243 xasprintf(&out, "%s-cert.pub", tmp);
1244 xfree(tmp);
1245
1246 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1247 fatal("Could not open \"%s\" for writing: %s", out,
1248 strerror(errno));
1249 if ((f = fdopen(fd, "w")) == NULL)
1250 fatal("%s: fdopen: %s", __func__, strerror(errno));
1251 if (!key_write(public, f))
1252 fatal("Could not write certified key to %s", out);
1253 fprintf(f, " %s\n", comment);
1254 fclose(f);
1255
Damien Miller4e270b02010-04-16 15:56:21 +10001256 if (!quiet) {
1257 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1258 "valid %s", key_cert_type(public),
1259 out, public->cert->key_id, public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001260 cert_principals != NULL ? " for " : "",
1261 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001262 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001263 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001264
1265 key_free(public);
1266 xfree(out);
1267 }
1268 exit(0);
1269}
1270
1271static u_int64_t
1272parse_relative_time(const char *s, time_t now)
1273{
1274 int64_t mul, secs;
1275
1276 mul = *s == '-' ? -1 : 1;
1277
1278 if ((secs = convtime(s + 1)) == -1)
1279 fatal("Invalid relative certificate time %s", s);
1280 if (mul == -1 && secs > now)
1281 fatal("Certificate time %s cannot be represented", s);
1282 return now + (u_int64_t)(secs * mul);
1283}
1284
1285static u_int64_t
1286parse_absolute_time(const char *s)
1287{
1288 struct tm tm;
1289 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001290 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001291
Damien Miller2ca342b2010-03-03 12:14:15 +11001292 /*
1293 * POSIX strptime says "The application shall ensure that there
1294 * is white-space or other non-alphanumeric characters between
1295 * any two conversion specifications" so arrange things this way.
1296 */
1297 switch (strlen(s)) {
1298 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001299 fmt = "%Y-%m-%d";
1300 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001301 break;
1302 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001303 fmt = "%Y-%m-%dT%H:%M:%S";
1304 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001305 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1306 break;
1307 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001308 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001309 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001310
1311 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001312 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001313 fatal("Invalid certificate time %s", s);
1314 if ((tt = mktime(&tm)) < 0)
1315 fatal("Certificate time %s cannot be represented", s);
1316 return (u_int64_t)tt;
1317}
1318
1319static void
1320parse_cert_times(char *timespec)
1321{
1322 char *from, *to;
1323 time_t now = time(NULL);
1324 int64_t secs;
1325
1326 /* +timespec relative to now */
1327 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1328 if ((secs = convtime(timespec + 1)) == -1)
1329 fatal("Invalid relative certificate life %s", timespec);
1330 cert_valid_to = now + secs;
1331 /*
1332 * Backdate certificate one minute to avoid problems on hosts
1333 * with poorly-synchronised clocks.
1334 */
1335 cert_valid_from = ((now - 59)/ 60) * 60;
1336 return;
1337 }
1338
1339 /*
1340 * from:to, where
1341 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1342 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1343 */
1344 from = xstrdup(timespec);
1345 to = strchr(from, ':');
1346 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001347 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001348 *to++ = '\0';
1349
1350 if (*from == '-' || *from == '+')
1351 cert_valid_from = parse_relative_time(from, now);
1352 else
1353 cert_valid_from = parse_absolute_time(from);
1354
1355 if (*to == '-' || *to == '+')
1356 cert_valid_to = parse_relative_time(to, cert_valid_from);
1357 else
1358 cert_valid_to = parse_absolute_time(to);
1359
1360 if (cert_valid_to <= cert_valid_from)
1361 fatal("Empty certificate validity interval");
1362 xfree(from);
1363}
1364
1365static void
Damien Miller4e270b02010-04-16 15:56:21 +10001366add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001367{
1368 char *val;
1369
1370 if (strcmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001371 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001372 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001373 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001374 else if (strcasecmp(opt, "permit-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, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001377 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001378 else if (strcasecmp(opt, "permit-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, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001381 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001382 else if (strcasecmp(opt, "permit-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, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001385 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001386 else if (strcasecmp(opt, "permit-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, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001389 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001390 else if (strcasecmp(opt, "permit-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 (strncasecmp(opt, "force-command=", 14) == 0) {
1393 val = opt + 14;
1394 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001395 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001396 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001397 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001398 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001399 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1400 val = opt + 15;
1401 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001402 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001403 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001404 fatal("source-address already specified");
1405 if (addr_match_cidr_list(NULL, val) != 0)
1406 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001407 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001408 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001409 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001410}
1411
Ben Lindstrombba81212001-06-25 05:01:22 +00001412static void
Damien Millerd834d352010-06-26 09:48:02 +10001413show_options(const Buffer *optbuf, int v00, int in_critical)
1414{
1415 u_char *name, *data;
1416 u_int dlen;
1417 Buffer options, option;
1418
1419 buffer_init(&options);
1420 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1421
1422 buffer_init(&option);
1423 while (buffer_len(&options) != 0) {
1424 name = buffer_get_string(&options, NULL);
1425 data = buffer_get_string_ptr(&options, &dlen);
1426 buffer_append(&option, data, dlen);
1427 printf(" %s", name);
1428 if ((v00 || !in_critical) &&
1429 (strcmp(name, "permit-X11-forwarding") == 0 ||
1430 strcmp(name, "permit-agent-forwarding") == 0 ||
1431 strcmp(name, "permit-port-forwarding") == 0 ||
1432 strcmp(name, "permit-pty") == 0 ||
1433 strcmp(name, "permit-user-rc") == 0))
1434 printf("\n");
1435 else if ((v00 || in_critical) &&
1436 (strcmp(name, "force-command") == 0 ||
1437 strcmp(name, "source-address") == 0)) {
1438 data = buffer_get_string(&option, NULL);
1439 printf(" %s\n", data);
1440 xfree(data);
1441 } else {
1442 printf(" UNKNOWN OPTION (len %u)\n",
1443 buffer_len(&option));
1444 buffer_clear(&option);
1445 }
1446 xfree(name);
1447 if (buffer_len(&option) != 0)
1448 fatal("Option corrupt: extra data at end");
1449 }
1450 buffer_free(&option);
1451 buffer_free(&options);
1452}
1453
1454static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001455do_show_cert(struct passwd *pw)
1456{
1457 Key *key;
1458 struct stat st;
1459 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001460 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001461
1462 if (!have_identity)
1463 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001464 if (stat(identity_file, &st) < 0)
1465 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001466 if ((key = key_load_public(identity_file, NULL)) == NULL)
1467 fatal("%s is not a public key", identity_file);
1468 if (!key_is_cert(key))
1469 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001470 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1471
Damien Millerf2b70ca2010-03-05 07:39:35 +11001472 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1473 ca_fp = key_fingerprint(key->cert->signature_key,
1474 SSH_FP_MD5, SSH_FP_HEX);
1475
1476 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001477 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1478 key_cert_type(key));
1479 printf(" Public key: %s %s\n", key_type(key), key_fp);
1480 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001481 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001482 printf(" Key ID: \"%s\"\n", key->cert->key_id);
1483 if (!v00)
1484 printf(" Serial: %llu\n", key->cert->serial);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001485 printf(" Valid: %s\n",
1486 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1487 printf(" Principals: ");
1488 if (key->cert->nprincipals == 0)
1489 printf("(none)\n");
1490 else {
1491 for (i = 0; i < key->cert->nprincipals; i++)
1492 printf("\n %s",
1493 key->cert->principals[i]);
1494 printf("\n");
1495 }
Damien Miller4e270b02010-04-16 15:56:21 +10001496 printf(" Critical Options: ");
1497 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001498 printf("(none)\n");
1499 else {
1500 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001501 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001502 }
Damien Miller4e270b02010-04-16 15:56:21 +10001503 if (!v00) {
1504 printf(" Extensions: ");
1505 if (buffer_len(&key->cert->extensions) == 0)
1506 printf("(none)\n");
1507 else {
1508 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001509 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001510 }
1511 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001512 exit(0);
1513}
1514
1515static void
Damien Miller431f66b1999-11-21 18:31:57 +11001516usage(void)
1517{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001518 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001519 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001520 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001521 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001522 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001523 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001524 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001525#ifdef ENABLE_PKCS11
1526 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1527#endif
Darren Tucker26dc3e62007-02-19 22:09:06 +11001528 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001529 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1530 fprintf(stderr, " -f filename Filename of the key file.\n");
1531 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1532 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1533 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001534 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1535 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Darren Tucker26dc3e62007-02-19 22:09:06 +11001536 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001537 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001538 fprintf(stderr, " -l Show fingerprint of key file.\n");
1539 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001540 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001541 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001542 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001543 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1544 fprintf(stderr, " -p Change passphrase of private key file.\n");
1545 fprintf(stderr, " -q Quiet.\n");
1546 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1547 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001548 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001549 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1550 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1551 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001552 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001553 fprintf(stderr, " -v Verbose.\n");
1554 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1555 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001556 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001557
Damien Miller95def091999-11-25 00:26:21 +11001558 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001559}
1560
Damien Miller95def091999-11-25 00:26:21 +11001561/*
1562 * Main program for key management.
1563 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001564int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001565main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001566{
Damien Millera41c8b12002-01-22 23:05:08 +11001567 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller7ea845e2010-02-12 09:21:02 +11001568 char out_file[MAXPATHLEN], *pkcs11provider = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001569 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001570 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001571 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001572 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001573 int opt, type, fd;
Damien Millerbebbb7e2010-05-10 11:54:38 +10001574 u_int maxbits;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001575 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001576 int do_gen_candidates = 0, do_screen_candidates = 0;
1577 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001578 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001579 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001580
Damien Miller95def091999-11-25 00:26:21 +11001581 extern int optind;
1582 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001583
Darren Tuckerce321d82005-10-03 18:11:24 +10001584 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1585 sanitise_stdfd();
1586
Darren Tucker9ac56e92007-01-14 10:19:59 +11001587 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001588
Damien Millerd3a18572000-06-07 19:55:44 +10001589 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001590 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001591
Kevin Steves3a881912002-07-20 19:05:40 +00001592 init_rng();
1593 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001594
Damien Miller5428f641999-11-25 11:54:57 +11001595 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001596 pw = getpwuid(getuid());
1597 if (!pw) {
1598 printf("You don't exist, go away!\n");
1599 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001600 }
Damien Millereba71ba2000-04-29 23:57:08 +10001601 if (gethostname(hostname, sizeof(hostname)) < 0) {
1602 perror("gethostname");
1603 exit(1);
1604 }
Damien Miller5428f641999-11-25 11:54:57 +11001605
Damien Millerf2b70ca2010-03-05 07:39:35 +11001606 while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001607 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001608 switch (opt) {
1609 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001610 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001611 if (errstr)
1612 fatal("Bits has bad value %s (%s)",
1613 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001614 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001615 case 'F':
1616 find_host = 1;
1617 rr_hostname = optarg;
1618 break;
1619 case 'H':
1620 hash_hosts = 1;
1621 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001622 case 'I':
1623 cert_key_id = optarg;
1624 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001625 case 'R':
1626 delete_host = 1;
1627 rr_hostname = optarg;
1628 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001629 case 'L':
1630 show_cert = 1;
1631 break;
Damien Miller95def091999-11-25 00:26:21 +11001632 case 'l':
1633 print_fingerprint = 1;
1634 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001635 case 'B':
1636 print_bubblebabble = 1;
1637 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001638 case 'n':
1639 cert_principals = optarg;
1640 break;
Damien Miller95def091999-11-25 00:26:21 +11001641 case 'p':
1642 change_passphrase = 1;
1643 break;
Damien Miller95def091999-11-25 00:26:21 +11001644 case 'c':
1645 change_comment = 1;
1646 break;
Damien Miller95def091999-11-25 00:26:21 +11001647 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001648 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1649 sizeof(identity_file))
1650 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001651 have_identity = 1;
1652 break;
Damien Miller37876e92003-05-15 10:19:46 +10001653 case 'g':
1654 print_generic = 1;
1655 break;
Damien Miller95def091999-11-25 00:26:21 +11001656 case 'P':
1657 identity_passphrase = optarg;
1658 break;
Damien Miller95def091999-11-25 00:26:21 +11001659 case 'N':
1660 identity_new_passphrase = optarg;
1661 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001662 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10001663 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001664 break;
Damien Miller95def091999-11-25 00:26:21 +11001665 case 'C':
1666 identity_comment = optarg;
1667 break;
Damien Miller95def091999-11-25 00:26:21 +11001668 case 'q':
1669 quiet = 1;
1670 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001671 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001672 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001673 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001674 convert_to_ssh2 = 1;
1675 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001676 case 'h':
1677 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001678 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001679 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001680 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001681 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001682 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001683 convert_from_ssh2 = 1;
1684 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001685 case 'y':
1686 print_public = 1;
1687 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001688 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001689 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001690 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001691 case 's':
1692 ca_key_path = optarg;
1693 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001694 case 't':
1695 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001696 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001697 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001698 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001699 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001700 case 'v':
1701 if (log_level == SYSLOG_LEVEL_INFO)
1702 log_level = SYSLOG_LEVEL_DEBUG1;
1703 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001704 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001705 log_level < SYSLOG_LEVEL_DEBUG3)
1706 log_level++;
1707 }
1708 break;
Damien Miller37876e92003-05-15 10:19:46 +10001709 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001710 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001711 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001712 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001713 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1714 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001715 if (errstr)
1716 fatal("Desired generator has bad value: %s (%s)",
1717 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001718 break;
1719 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001720 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001721 if (errstr)
1722 fatal("Invalid number of trials: %s (%s)",
1723 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001724 break;
1725 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001726 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10001727 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10001728 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10001729 break;
1730 case 'G':
1731 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001732 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1733 sizeof(out_file))
1734 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001735 break;
1736 case 'T':
1737 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001738 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1739 sizeof(out_file))
1740 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001741 break;
1742 case 'S':
1743 /* XXX - also compare length against bits */
1744 if (BN_hex2bn(&start, optarg) == 0)
1745 fatal("Invalid start point.");
1746 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001747 case 'V':
1748 parse_cert_times(optarg);
1749 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001750 case 'z':
1751 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
1752 if (errstr)
1753 fatal("Invalid serial number: %s", errstr);
1754 break;
Damien Miller95def091999-11-25 00:26:21 +11001755 case '?':
1756 default:
1757 usage();
1758 }
1759 }
Darren Tucker06930c72003-12-31 11:34:51 +11001760
1761 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001762 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001763
Damien Miller0a80ca12010-02-27 07:55:05 +11001764 argv += optind;
1765 argc -= optind;
1766
1767 if (ca_key_path != NULL) {
1768 if (argc < 1) {
1769 printf("Too few arguments.\n");
1770 usage();
1771 }
1772 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11001773 printf("Too many arguments.\n");
1774 usage();
1775 }
1776 if (change_passphrase && change_comment) {
1777 printf("Can only have one of -p and -c.\n");
1778 usage();
1779 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001780 if (print_fingerprint && (delete_host || hash_hosts)) {
1781 printf("Cannot use -l with -D or -R.\n");
1782 usage();
1783 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001784 if (ca_key_path != NULL) {
1785 if (cert_key_id == NULL)
1786 fatal("Must specify key id (-I) when certifying");
1787 do_ca_sign(pw, argc, argv);
1788 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001789 if (show_cert)
1790 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001791 if (delete_host || hash_hosts || find_host)
1792 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001793 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001794 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001795 if (change_passphrase)
1796 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001797 if (change_comment)
1798 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001799 if (convert_to_ssh2)
1800 do_convert_to_ssh2(pw);
1801 if (convert_from_ssh2)
1802 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001803 if (print_public)
1804 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001805 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001806 unsigned int n = 0;
1807
1808 if (have_identity) {
1809 n = do_print_resource_record(pw,
1810 identity_file, rr_hostname);
1811 if (n == 0) {
1812 perror(identity_file);
1813 exit(1);
1814 }
1815 exit(0);
1816 } else {
1817
1818 n += do_print_resource_record(pw,
1819 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1820 n += do_print_resource_record(pw,
1821 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1822
1823 if (n == 0)
1824 fatal("no keys found.");
1825 exit(0);
1826 }
Damien Miller37876e92003-05-15 10:19:46 +10001827 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001828 if (pkcs11provider != NULL)
1829 do_download(pw, pkcs11provider);
Damien Miller95def091999-11-25 00:26:21 +11001830
Darren Tucker019cefe2003-08-02 22:40:07 +10001831 if (do_gen_candidates) {
1832 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001833
Darren Tucker019cefe2003-08-02 22:40:07 +10001834 if (out == NULL) {
1835 error("Couldn't open modulus candidate file \"%s\": %s",
1836 out_file, strerror(errno));
1837 return (1);
1838 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001839 if (bits == 0)
1840 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001841 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001842 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001843
1844 return (0);
1845 }
1846
1847 if (do_screen_candidates) {
1848 FILE *in;
1849 FILE *out = fopen(out_file, "w");
1850
1851 if (have_identity && strcmp(identity_file, "-") != 0) {
1852 if ((in = fopen(identity_file, "r")) == NULL) {
1853 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001854 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001855 strerror(errno));
1856 }
1857 } else
1858 in = stdin;
1859
1860 if (out == NULL) {
1861 fatal("Couldn't open moduli file \"%s\": %s",
1862 out_file, strerror(errno));
1863 }
1864 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001865 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001866 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001867 }
1868
Damien Miller95def091999-11-25 00:26:21 +11001869 arc4random_stir();
1870
Damien Millerf14be5c2005-11-05 15:15:49 +11001871 if (key_type_name == NULL)
1872 key_type_name = "rsa";
1873
Damien Millere39cacc2000-11-29 12:18:44 +11001874 type = key_type_from_name(key_type_name);
1875 if (type == KEY_UNSPEC) {
1876 fprintf(stderr, "unknown key type %s\n", key_type_name);
1877 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001878 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001879 if (bits == 0)
1880 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Damien Millerbebbb7e2010-05-10 11:54:38 +10001881 maxbits = (type == KEY_DSA) ?
1882 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
1883 if (bits > maxbits) {
1884 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
1885 exit(1);
1886 }
Tim Rice660c3402005-11-28 17:45:32 -08001887 if (type == KEY_DSA && bits != 1024)
1888 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001889 if (!quiet)
1890 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001891 private = key_generate(type, bits);
1892 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001893 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001894 exit(1);
1895 }
1896 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001897
1898 if (!have_identity)
1899 ask_filename(pw, "Enter file in which to save the key");
1900
Damien Miller788f2122005-11-05 15:14:59 +11001901 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10001902 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
1903 pw->pw_dir, _PATH_SSH_USER_DIR);
1904 if (strstr(identity_file, dotsshdir) != NULL) {
1905 if (stat(dotsshdir, &st) < 0) {
1906 if (errno != ENOENT) {
1907 error("Could not stat %s: %s", dotsshdir,
1908 strerror(errno));
1909 } else if (mkdir(dotsshdir, 0700) < 0) {
1910 error("Could not create directory '%s': %s",
1911 dotsshdir, strerror(errno));
1912 } else if (!quiet)
1913 printf("Created directory '%s'.\n", dotsshdir);
1914 }
Damien Miller95def091999-11-25 00:26:21 +11001915 }
1916 /* If the file already exists, ask the user to confirm. */
1917 if (stat(identity_file, &st) >= 0) {
1918 char yesno[3];
1919 printf("%s already exists.\n", identity_file);
1920 printf("Overwrite (y/n)? ");
1921 fflush(stdout);
1922 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1923 exit(1);
1924 if (yesno[0] != 'y' && yesno[0] != 'Y')
1925 exit(1);
1926 }
1927 /* Ask for a passphrase (twice). */
1928 if (identity_passphrase)
1929 passphrase1 = xstrdup(identity_passphrase);
1930 else if (identity_new_passphrase)
1931 passphrase1 = xstrdup(identity_new_passphrase);
1932 else {
1933passphrase_again:
1934 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001935 read_passphrase("Enter passphrase (empty for no "
1936 "passphrase): ", RP_ALLOW_STDIN);
1937 passphrase2 = read_passphrase("Enter same passphrase again: ",
1938 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001939 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001940 /*
1941 * The passphrases do not match. Clear them and
1942 * retry.
1943 */
Damien Miller95def091999-11-25 00:26:21 +11001944 memset(passphrase1, 0, strlen(passphrase1));
1945 memset(passphrase2, 0, strlen(passphrase2));
1946 xfree(passphrase1);
1947 xfree(passphrase2);
1948 printf("Passphrases do not match. Try again.\n");
1949 goto passphrase_again;
1950 }
1951 /* Clear the other copy of the passphrase. */
1952 memset(passphrase2, 0, strlen(passphrase2));
1953 xfree(passphrase2);
1954 }
1955
Damien Miller95def091999-11-25 00:26:21 +11001956 if (identity_comment) {
1957 strlcpy(comment, identity_comment, sizeof(comment));
1958 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11001959 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001960 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1961 }
1962
1963 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001964 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001965 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001966 memset(passphrase1, 0, strlen(passphrase1));
1967 xfree(passphrase1);
1968 exit(1);
1969 }
1970 /* Clear the passphrase. */
1971 memset(passphrase1, 0, strlen(passphrase1));
1972 xfree(passphrase1);
1973
1974 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001975 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001976 arc4random_stir();
1977
1978 if (!quiet)
1979 printf("Your identification has been saved in %s.\n", identity_file);
1980
Damien Miller95def091999-11-25 00:26:21 +11001981 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001982 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1983 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001984 printf("Could not save your public key in %s\n", identity_file);
1985 exit(1);
1986 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001987 f = fdopen(fd, "w");
1988 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001989 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001990 exit(1);
1991 }
Damien Millereba71ba2000-04-29 23:57:08 +10001992 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001993 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001994 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001995 fclose(f);
1996
1997 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001998 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001999 char *ra = key_fingerprint(public, SSH_FP_MD5,
2000 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002001 printf("Your public key has been saved in %s.\n",
2002 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002003 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002004 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002005 printf("The key's randomart image is:\n");
2006 printf("%s\n", ra);
2007 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002008 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002009 }
Damien Millereba71ba2000-04-29 23:57:08 +10002010
2011 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002012 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002013}