blob: f910dce38c7dbc543d00607de7dfa83b864778eb [file] [log] [blame]
Damien Miller2ca342b2010-03-03 12:14:15 +11001/* $OpenBSD: ssh-keygen.c,v 1.180 2010/03/02 23:20:57 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 Miller10f6f6b1999-11-17 17:29:08 +110085/* Flag indicating that we just want to see the key fingerprint */
86int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000087int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110088
Damien Miller431f66b1999-11-21 18:31:57 +110089/* The identity file name, given on the command line or entered by the user. */
90char identity_file[1024];
91int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
93/* This is set to the passphrase if given on the command line. */
94char *identity_passphrase = NULL;
95
96/* This is set to the new passphrase if given on the command line. */
97char *identity_new_passphrase = NULL;
98
99/* This is set to the new comment if given on the command line. */
100char *identity_comment = NULL;
101
Damien Miller0a80ca12010-02-27 07:55:05 +1100102/* Path to CA key when certifying keys. */
103char *ca_key_path = NULL;
104
105/* Key type when certifying */
106u_int cert_key_type = SSH2_CERT_TYPE_USER;
107
108/* "key ID" of signed key */
109char *cert_key_id = NULL;
110
111/* Comma-separated list of principal names for certifying keys */
112char *cert_principals = NULL;
113
114/* Validity period for certificates */
115u_int64_t cert_valid_from = 0;
116u_int64_t cert_valid_to = ~0ULL;
117
118/* Certificate constraints */
119#define CONSTRAINT_X_FWD (1)
120#define CONSTRAINT_AGENT_FWD (1<<1)
121#define CONSTRAINT_PORT_FWD (1<<2)
122#define CONSTRAINT_PTY (1<<3)
123#define CONSTRAINT_USER_RC (1<<4)
124#define CONSTRAINT_DEFAULT (CONSTRAINT_X_FWD|CONSTRAINT_AGENT_FWD| \
125 CONSTRAINT_PORT_FWD|CONSTRAINT_PTY| \
126 CONSTRAINT_USER_RC)
127u_int32_t constraint_flags = CONSTRAINT_DEFAULT;
128char *constraint_command = NULL;
129char *constraint_src_addr = NULL;
130
Damien Millereba71ba2000-04-29 23:57:08 +1000131/* Dump public key file in format used by real and the original SSH 2 */
132int convert_to_ssh2 = 0;
133int convert_from_ssh2 = 0;
134int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000135int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100136
Damien Millera41c8b12002-01-22 23:05:08 +1100137char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000138
Damien Miller431f66b1999-11-21 18:31:57 +1100139/* argv0 */
140extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Millereba71ba2000-04-29 23:57:08 +1000142char hostname[MAXHOSTNAMELEN];
143
Darren Tucker770fc012004-05-13 16:24:32 +1000144/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000145int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000146int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
147
Ben Lindstrombba81212001-06-25 05:01:22 +0000148static void
Damien Miller431f66b1999-11-21 18:31:57 +1100149ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150{
Damien Miller95def091999-11-25 00:26:21 +1100151 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100152 char *name = NULL;
153
Damien Miller993dd552002-02-19 15:22:47 +1100154 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000155 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100156 else {
Damien Miller993dd552002-02-19 15:22:47 +1100157 switch (key_type_from_name(key_type_name)) {
158 case KEY_RSA1:
159 name = _PATH_SSH_CLIENT_IDENTITY;
160 break;
161 case KEY_DSA:
162 name = _PATH_SSH_CLIENT_ID_DSA;
163 break;
164 case KEY_RSA:
165 name = _PATH_SSH_CLIENT_ID_RSA;
166 break;
167 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100168 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100169 exit(1);
170 break;
171 }
Damien Miller90967402006-03-26 14:07:26 +1100172 }
Damien Millere39cacc2000-11-29 12:18:44 +1100173 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000174 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100175 if (fgets(buf, sizeof(buf), stdin) == NULL)
176 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000177 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100178 if (strcmp(buf, "") != 0)
179 strlcpy(identity_file, buf, sizeof(identity_file));
180 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100181}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000182
Ben Lindstrombba81212001-06-25 05:01:22 +0000183static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000184load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000185{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000186 char *pass;
187 Key *prv;
188
Ben Lindstroma3700052001-04-05 23:26:32 +0000189 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000190 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000191 if (identity_passphrase)
192 pass = xstrdup(identity_passphrase);
193 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000194 pass = read_passphrase("Enter passphrase: ",
195 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000196 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000197 memset(pass, 0, strlen(pass));
198 xfree(pass);
199 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000200 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000201}
202
Damien Miller874d77b2000-10-14 16:23:11 +1100203#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000204#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100205#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000206#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000207
Ben Lindstrombba81212001-06-25 05:01:22 +0000208static void
Damien Millereba71ba2000-04-29 23:57:08 +1000209do_convert_to_ssh2(struct passwd *pw)
210{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000211 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000212 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000213 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100214 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000215 struct stat st;
216
217 if (!have_identity)
218 ask_filename(pw, "Enter file in which the key is");
219 if (stat(identity_file, &st) < 0) {
220 perror(identity_file);
221 exit(1);
222 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000223 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000224 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000225 fprintf(stderr, "load failed\n");
226 exit(1);
227 }
Damien Millereba71ba2000-04-29 23:57:08 +1000228 }
Damien Millerdb274722003-05-14 13:45:22 +1000229 if (k->type == KEY_RSA1) {
230 fprintf(stderr, "version 1 keys are not supported\n");
231 exit(1);
232 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000233 if (key_to_blob(k, &blob, &len) <= 0) {
234 fprintf(stderr, "key_to_blob failed\n");
235 exit(1);
236 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100237 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
238 snprintf(comment, sizeof(comment),
239 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000240 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000241 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100242
243 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
244 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000245 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100246 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000247 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000248 xfree(blob);
249 exit(0);
250}
251
Ben Lindstrombba81212001-06-25 05:01:22 +0000252static void
Damien Miller874d77b2000-10-14 16:23:11 +1100253buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
254{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000255 u_int bignum_bits = buffer_get_int(b);
256 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000257
Damien Miller874d77b2000-10-14 16:23:11 +1100258 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000259 fatal("buffer_get_bignum_bits: input buffer too small: "
260 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100261 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
262 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100263 buffer_consume(b, bytes);
264}
265
Ben Lindstrombba81212001-06-25 05:01:22 +0000266static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000267do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100268{
269 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100270 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100271 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000272 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000273 int magic, rlen, ktype, i1, i2, i3, i4;
274 u_int slen;
275 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100276
277 buffer_init(&b);
278 buffer_append(&b, blob, blen);
279
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100280 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100281 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
282 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
283 buffer_free(&b);
284 return NULL;
285 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000286 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100287 type = buffer_get_string(&b, NULL);
288 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000289 i2 = buffer_get_int(&b);
290 i3 = buffer_get_int(&b);
291 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100292 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100293 if (strcmp(cipher, "none") != 0) {
294 error("unsupported cipher %s", cipher);
295 xfree(cipher);
296 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000297 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100298 return NULL;
299 }
300 xfree(cipher);
301
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000302 if (strstr(type, "dsa")) {
303 ktype = KEY_DSA;
304 } else if (strstr(type, "rsa")) {
305 ktype = KEY_RSA;
306 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100307 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000308 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100309 return NULL;
310 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000311 key = key_new_private(ktype);
312 xfree(type);
313
314 switch (key->type) {
315 case KEY_DSA:
316 buffer_get_bignum_bits(&b, key->dsa->p);
317 buffer_get_bignum_bits(&b, key->dsa->g);
318 buffer_get_bignum_bits(&b, key->dsa->q);
319 buffer_get_bignum_bits(&b, key->dsa->pub_key);
320 buffer_get_bignum_bits(&b, key->dsa->priv_key);
321 break;
322 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100323 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000324 debug("e %lx", e);
325 if (e < 30) {
326 e <<= 8;
327 e += buffer_get_char(&b);
328 debug("e %lx", e);
329 e <<= 8;
330 e += buffer_get_char(&b);
331 debug("e %lx", e);
332 }
333 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000334 buffer_free(&b);
335 key_free(key);
336 return NULL;
337 }
338 buffer_get_bignum_bits(&b, key->rsa->d);
339 buffer_get_bignum_bits(&b, key->rsa->n);
340 buffer_get_bignum_bits(&b, key->rsa->iqmp);
341 buffer_get_bignum_bits(&b, key->rsa->q);
342 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000343 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000344 break;
345 }
Damien Miller874d77b2000-10-14 16:23:11 +1100346 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000347 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000348 error("do_convert_private_ssh2_from_blob: "
349 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100350 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000351
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000352 /* try the key */
353 key_sign(key, &sig, &slen, data, sizeof(data));
354 key_verify(key, sig, slen, data, sizeof(data));
355 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100356 return key;
357}
358
Damien Miller8056a9d2006-03-15 12:05:40 +1100359static int
360get_line(FILE *fp, char *line, size_t len)
361{
362 int c;
363 size_t pos = 0;
364
365 line[0] = '\0';
366 while ((c = fgetc(fp)) != EOF) {
367 if (pos >= len - 1) {
368 fprintf(stderr, "input line too long.\n");
369 exit(1);
370 }
Damien Miller90967402006-03-26 14:07:26 +1100371 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100372 case '\r':
373 c = fgetc(fp);
374 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
375 fprintf(stderr, "unget: %s\n", strerror(errno));
376 exit(1);
377 }
378 return pos;
379 case '\n':
380 return pos;
381 }
382 line[pos++] = c;
383 line[pos] = '\0';
384 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100385 /* We reached EOF */
386 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100387}
388
Ben Lindstrombba81212001-06-25 05:01:22 +0000389static void
Damien Millereba71ba2000-04-29 23:57:08 +1000390do_convert_from_ssh2(struct passwd *pw)
391{
392 Key *k;
393 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000394 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100395 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000396 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000397 char encoded[8096];
398 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100399 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000400 FILE *fp;
401
402 if (!have_identity)
403 ask_filename(pw, "Enter file in which the key is");
404 if (stat(identity_file, &st) < 0) {
405 perror(identity_file);
406 exit(1);
407 }
408 fp = fopen(identity_file, "r");
409 if (fp == NULL) {
410 perror(identity_file);
411 exit(1);
412 }
413 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100414 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
415 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000416 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000417 if (strncmp(line, "----", 4) == 0 ||
418 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100419 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
420 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000421 if (strstr(line, " END ") != NULL) {
422 break;
423 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000424 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000425 continue;
426 }
Damien Miller30c3d422000-05-09 11:02:59 +1000427 if (escaped) {
428 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000429 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000430 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000431 }
Damien Millereba71ba2000-04-29 23:57:08 +1000432 strlcat(encoded, line, sizeof(encoded));
433 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000434 len = strlen(encoded);
435 if (((len % 4) == 3) &&
436 (encoded[len-1] == '=') &&
437 (encoded[len-2] == '=') &&
438 (encoded[len-3] == '='))
439 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100440 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000441 if (blen < 0) {
442 fprintf(stderr, "uudecode failed.\n");
443 exit(1);
444 }
Damien Miller874d77b2000-10-14 16:23:11 +1100445 k = private ?
446 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100447 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100448 if (k == NULL) {
449 fprintf(stderr, "decode blob failed.\n");
450 exit(1);
451 }
452 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000453 (k->type == KEY_DSA ?
454 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
455 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100456 key_write(k, stdout);
457 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100458 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100459 exit(1);
460 }
Damien Millereba71ba2000-04-29 23:57:08 +1000461 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100462 if (!private)
463 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000464 fclose(fp);
465 exit(0);
466}
467
Ben Lindstrombba81212001-06-25 05:01:22 +0000468static void
Damien Millereba71ba2000-04-29 23:57:08 +1000469do_print_public(struct passwd *pw)
470{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000471 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000472 struct stat st;
473
474 if (!have_identity)
475 ask_filename(pw, "Enter file in which the key is");
476 if (stat(identity_file, &st) < 0) {
477 perror(identity_file);
478 exit(1);
479 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000480 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000481 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000482 fprintf(stderr, "load failed\n");
483 exit(1);
484 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000485 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000486 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000487 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000488 fprintf(stdout, "\n");
489 exit(0);
490}
491
Ben Lindstromcd392282001-07-04 03:44:03 +0000492static void
Damien Miller86cbb442010-02-12 09:22:57 +1100493do_download(struct passwd *pw, char *pkcs11provider)
Ben Lindstromcd392282001-07-04 03:44:03 +0000494{
Damien Miller7ea845e2010-02-12 09:21:02 +1100495#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000496 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100497 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000498
Damien Miller7ea845e2010-02-12 09:21:02 +1100499 pkcs11_init(0);
500 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
501 if (nkeys <= 0)
502 fatal("cannot read public key from pkcs11");
503 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000504 key_write(keys[i], stdout);
505 key_free(keys[i]);
506 fprintf(stdout, "\n");
507 }
508 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100509 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000510 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100511#else
512 fatal("no pkcs11 support");
513#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000514}
Ben Lindstromcd392282001-07-04 03:44:03 +0000515
Ben Lindstrombba81212001-06-25 05:01:22 +0000516static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100517do_fingerprint(struct passwd *pw)
518{
Damien Miller98c7ad62000-03-09 21:27:49 +1100519 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000520 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000521 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100522 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000523 enum fp_rep rep;
524 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100525 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100526
Ben Lindstromd0fca422001-03-26 13:44:06 +0000527 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
528 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000529
Damien Miller95def091999-11-25 00:26:21 +1100530 if (!have_identity)
531 ask_filename(pw, "Enter file in which the key is");
532 if (stat(identity_file, &st) < 0) {
533 perror(identity_file);
534 exit(1);
535 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000536 public = key_load_public(identity_file, &comment);
537 if (public != NULL) {
538 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100539 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000540 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
541 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000542 if (log_level >= SYSLOG_LEVEL_VERBOSE)
543 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100544 key_free(public);
545 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000546 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000547 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100548 exit(0);
549 }
Damien Miller40b59852006-06-13 13:00:25 +1000550 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000551 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000552 comment = NULL;
553 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100554
555 f = fopen(identity_file, "r");
556 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100557 while (fgets(line, sizeof(line), f)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000558 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100559 error("line %d too long: %.40s...",
560 num + 1, line);
Damien Miller98c7ad62000-03-09 21:27:49 +1100561 skip = 1;
562 continue;
Damien Miller95def091999-11-25 00:26:21 +1100563 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100564 num++;
565 if (skip) {
566 skip = 0;
567 continue;
568 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000569 *cp = '\0';
Damien Miller98c7ad62000-03-09 21:27:49 +1100570
571 /* Skip leading whitespace, empty and comment lines. */
572 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
573 ;
574 if (!*cp || *cp == '\n' || *cp == '#')
Damien Miller80163902007-01-05 16:30:16 +1100575 continue;
Damien Miller98c7ad62000-03-09 21:27:49 +1100576 i = strtol(cp, &ep, 10);
577 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
578 int quoted = 0;
579 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000580 for (; *cp && (quoted || (*cp != ' ' &&
581 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100582 if (*cp == '\\' && cp[1] == '"')
583 cp++; /* Skip both */
584 else if (*cp == '"')
585 quoted = !quoted;
586 }
587 if (!*cp)
588 continue;
589 *cp++ = '\0';
590 }
591 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000592 public = key_new(KEY_RSA1);
593 if (key_read(public, &cp) != 1) {
594 cp = ep;
595 key_free(public);
596 public = key_new(KEY_UNSPEC);
597 if (key_read(public, &cp) != 1) {
598 key_free(public);
599 continue;
600 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100601 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000602 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000603 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100604 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000605 printf("%u %s %s (%s)\n", key_size(public), fp,
606 comment ? comment : "no comment", key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000607 if (log_level >= SYSLOG_LEVEL_VERBOSE)
608 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000609 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000610 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000611 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000612 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100613 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100614 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100615 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100616 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100617 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100618 exit(1);
619 }
Damien Miller95def091999-11-25 00:26:21 +1100620 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100621}
622
Damien Miller4b42d7f2005-03-01 21:48:35 +1100623static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100624printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100625{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000626 if (print_fingerprint) {
627 enum fp_rep rep;
628 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000629 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000630
631 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
632 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
633 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100634 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000635 printf("%u %s %s (%s)\n", key_size(public), fp, name,
636 key_type(public));
637 if (log_level >= SYSLOG_LEVEL_VERBOSE)
638 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000639 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000640 xfree(fp);
641 } else {
642 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
643 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100644 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000645 if (!key_write(public, f))
646 fatal("key_write failed");
647 fprintf(f, "\n");
648 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100649}
650
651static void
652do_known_hosts(struct passwd *pw, const char *name)
653{
654 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100655 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100656 char *cp, *cp2, *kp, *kp2;
657 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100658 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100659 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100660
661 if (!have_identity) {
662 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
663 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
664 sizeof(identity_file))
665 fatal("Specified known hosts path too long");
666 xfree(cp);
667 have_identity = 1;
668 }
669 if ((in = fopen(identity_file, "r")) == NULL)
670 fatal("fopen: %s", strerror(errno));
671
672 /*
673 * Find hosts goes to stdout, hash and deletions happen in-place
674 * A corner case is ssh-keygen -HF foo, which should go to stdout
675 */
676 if (!find_host && (hash_hosts || delete_host)) {
677 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
678 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
679 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
680 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
681 fatal("known_hosts path too long");
682 umask(077);
683 if ((c = mkstemp(tmp)) == -1)
684 fatal("mkstemp: %s", strerror(errno));
685 if ((out = fdopen(c, "w")) == NULL) {
686 c = errno;
687 unlink(tmp);
688 fatal("fdopen: %s", strerror(c));
689 }
690 inplace = 1;
691 }
692
693 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000694 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100695 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100696 skip = 1;
697 invalid = 1;
698 continue;
699 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000700 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100701 if (skip) {
702 skip = 0;
703 continue;
704 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000705 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100706
707 /* Skip leading whitespace, empty and comment lines. */
708 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
709 ;
710 if (!*cp || *cp == '\n' || *cp == '#') {
711 if (inplace)
712 fprintf(out, "%s\n", cp);
713 continue;
714 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100715 /* Check whether this is a CA key */
716 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
717 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
718 cp[sizeof(CA_MARKER) - 1] == '\t')) {
719 ca = 1;
720 cp += sizeof(CA_MARKER);
721 } else
722 ca = 0;
723
Damien Miller4b42d7f2005-03-01 21:48:35 +1100724 /* Find the end of the host name portion. */
725 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
726 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100727
Damien Miller4b42d7f2005-03-01 21:48:35 +1100728 if (*kp == '\0' || *(kp + 1) == '\0') {
729 error("line %d missing key: %.40s...",
730 num, line);
731 invalid = 1;
732 continue;
733 }
734 *kp++ = '\0';
735 kp2 = kp;
736
Damien Miller0a80ca12010-02-27 07:55:05 +1100737 pub = key_new(KEY_RSA1);
738 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100739 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100740 key_free(pub);
741 pub = key_new(KEY_UNSPEC);
742 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100743 error("line %d invalid key: %.40s...",
744 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100745 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100746 invalid = 1;
747 continue;
748 }
749 }
750
751 if (*cp == HASH_DELIM) {
752 if (find_host || delete_host) {
753 cp2 = host_hash(name, cp, strlen(cp));
754 if (cp2 == NULL) {
755 error("line %d: invalid hashed "
756 "name: %.64s...", num, line);
757 invalid = 1;
758 continue;
759 }
760 c = (strcmp(cp2, cp) == 0);
761 if (find_host && c) {
762 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100763 "line %d type %s%s\n", name,
764 num, key_type(pub),
765 ca ? " (CA key)" : "");
766 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100767 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100768 if (delete_host && !c && !ca)
769 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100770 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100771 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100772 } else {
773 if (find_host || delete_host) {
774 c = (match_hostname(name, cp,
775 strlen(cp)) == 1);
776 if (find_host && c) {
777 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100778 "line %d type %s%s\n", name,
779 num, key_type(pub),
780 ca ? " (CA key)" : "");
781 printhost(out, name, pub,
782 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100783 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100784 if (delete_host && !c && !ca)
785 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100786 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100787 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100788 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100789 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100790 if (ca) {
791 fprintf(stderr, "Warning: "
792 "ignoring CA key for host: "
793 "%.64s\n", cp2);
794 printhost(out, cp2, pub, ca, 0);
795 } else if (strcspn(cp2, "*?!") !=
796 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100797 fprintf(stderr, "Warning: "
798 "ignoring host name with "
799 "metacharacters: %.64s\n",
800 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100801 printhost(out, cp2, pub, ca, 0);
802 } else
803 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +1100804 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100805 has_unhashed = 1;
806 }
807 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100808 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100809 }
810 fclose(in);
811
812 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100813 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100814 identity_file);
815 if (inplace) {
816 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100817 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100818 fclose(out);
819 unlink(tmp);
820 }
821 exit(1);
822 }
823
824 if (inplace) {
825 fclose(out);
826
827 /* Backup existing file */
828 if (unlink(old) == -1 && errno != ENOENT)
829 fatal("unlink %.100s: %s", old, strerror(errno));
830 if (link(identity_file, old) == -1)
831 fatal("link %.100s to %.100s: %s", identity_file, old,
832 strerror(errno));
833 /* Move new one into place */
834 if (rename(tmp, identity_file) == -1) {
835 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
836 strerror(errno));
837 unlink(tmp);
838 unlink(old);
839 exit(1);
840 }
841
842 fprintf(stderr, "%s updated.\n", identity_file);
843 fprintf(stderr, "Original contents retained as %s\n", old);
844 if (has_unhashed) {
845 fprintf(stderr, "WARNING: %s contains unhashed "
846 "entries\n", old);
847 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000848 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100849 }
850 }
851
852 exit(0);
853}
854
Damien Miller95def091999-11-25 00:26:21 +1100855/*
856 * Perform changing a passphrase. The argument is the passwd structure
857 * for the current user.
858 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000859static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100860do_change_passphrase(struct passwd *pw)
861{
Damien Miller95def091999-11-25 00:26:21 +1100862 char *comment;
863 char *old_passphrase, *passphrase1, *passphrase2;
864 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000865 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100866
Damien Miller95def091999-11-25 00:26:21 +1100867 if (!have_identity)
868 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100869 if (stat(identity_file, &st) < 0) {
870 perror(identity_file);
871 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000872 }
Damien Miller95def091999-11-25 00:26:21 +1100873 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000874 private = key_load_private(identity_file, "", &comment);
875 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100876 if (identity_passphrase)
877 old_passphrase = xstrdup(identity_passphrase);
878 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000879 old_passphrase =
880 read_passphrase("Enter old passphrase: ",
881 RP_ALLOW_STDIN);
882 private = key_load_private(identity_file, old_passphrase,
883 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000884 memset(old_passphrase, 0, strlen(old_passphrase));
885 xfree(old_passphrase);
886 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100887 printf("Bad passphrase.\n");
888 exit(1);
889 }
Damien Miller95def091999-11-25 00:26:21 +1100890 }
891 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000892
Damien Miller95def091999-11-25 00:26:21 +1100893 /* Ask the new passphrase (twice). */
894 if (identity_new_passphrase) {
895 passphrase1 = xstrdup(identity_new_passphrase);
896 passphrase2 = NULL;
897 } else {
898 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000899 read_passphrase("Enter new passphrase (empty for no "
900 "passphrase): ", RP_ALLOW_STDIN);
901 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100902 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100903
904 /* Verify that they are the same. */
905 if (strcmp(passphrase1, passphrase2) != 0) {
906 memset(passphrase1, 0, strlen(passphrase1));
907 memset(passphrase2, 0, strlen(passphrase2));
908 xfree(passphrase1);
909 xfree(passphrase2);
910 printf("Pass phrases do not match. Try again.\n");
911 exit(1);
912 }
913 /* Destroy the other copy. */
914 memset(passphrase2, 0, strlen(passphrase2));
915 xfree(passphrase2);
916 }
917
918 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000919 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000920 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100921 memset(passphrase1, 0, strlen(passphrase1));
922 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000923 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100924 xfree(comment);
925 exit(1);
926 }
927 /* Destroy the passphrase and the copy of the key in memory. */
928 memset(passphrase1, 0, strlen(passphrase1));
929 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000930 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100931 xfree(comment);
932
933 printf("Your identification has been saved with the new passphrase.\n");
934 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000935}
936
Damien Miller37876e92003-05-15 10:19:46 +1000937/*
938 * Print the SSHFP RR.
939 */
Damien Millercb314822006-03-26 13:48:01 +1100940static int
941do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000942{
943 Key *public;
944 char *comment = NULL;
945 struct stat st;
946
Damien Millercb314822006-03-26 13:48:01 +1100947 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000948 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100949 if (stat(fname, &st) < 0) {
950 if (errno == ENOENT)
951 return 0;
952 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000953 exit(1);
954 }
Damien Millercb314822006-03-26 13:48:01 +1100955 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000956 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000957 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000958 key_free(public);
959 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100960 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000961 }
962 if (comment)
963 xfree(comment);
964
Damien Millercb314822006-03-26 13:48:01 +1100965 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000966 exit(1);
967}
Damien Miller37876e92003-05-15 10:19:46 +1000968
Damien Miller95def091999-11-25 00:26:21 +1100969/*
970 * Change the comment of a private key file.
971 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000972static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000973do_change_comment(struct passwd *pw)
974{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000975 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000976 Key *private;
977 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100978 struct stat st;
979 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000980 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000981
Damien Miller95def091999-11-25 00:26:21 +1100982 if (!have_identity)
983 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100984 if (stat(identity_file, &st) < 0) {
985 perror(identity_file);
986 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000987 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000988 private = key_load_private(identity_file, "", &comment);
989 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100990 if (identity_passphrase)
991 passphrase = xstrdup(identity_passphrase);
992 else if (identity_new_passphrase)
993 passphrase = xstrdup(identity_new_passphrase);
994 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000995 passphrase = read_passphrase("Enter passphrase: ",
996 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100997 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000998 private = key_load_private(identity_file, passphrase, &comment);
999 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001000 memset(passphrase, 0, strlen(passphrase));
1001 xfree(passphrase);
1002 printf("Bad passphrase.\n");
1003 exit(1);
1004 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001005 } else {
1006 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001007 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001008 if (private->type != KEY_RSA1) {
1009 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1010 key_free(private);
1011 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001012 }
Damien Miller95def091999-11-25 00:26:21 +11001013 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001014
Damien Miller95def091999-11-25 00:26:21 +11001015 if (identity_comment) {
1016 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1017 } else {
1018 printf("Enter new comment: ");
1019 fflush(stdout);
1020 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1021 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001022 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001023 exit(1);
1024 }
Damien Miller14b017d2007-09-17 16:09:15 +10001025 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001026 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001027
Damien Miller95def091999-11-25 00:26:21 +11001028 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001029 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001030 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001031 memset(passphrase, 0, strlen(passphrase));
1032 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001033 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001034 xfree(comment);
1035 exit(1);
1036 }
Damien Miller95def091999-11-25 00:26:21 +11001037 memset(passphrase, 0, strlen(passphrase));
1038 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001039 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001040 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001041
Damien Miller95def091999-11-25 00:26:21 +11001042 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001043 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1044 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001045 printf("Could not save your public key in %s\n", identity_file);
1046 exit(1);
1047 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001048 f = fdopen(fd, "w");
1049 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001050 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001051 exit(1);
1052 }
Damien Millereba71ba2000-04-29 23:57:08 +10001053 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001054 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001055 key_free(public);
1056 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001057 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058
Damien Miller95def091999-11-25 00:26:21 +11001059 xfree(comment);
1060
1061 printf("The comment in your key file has been changed.\n");
1062 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001063}
1064
Damien Miller0a80ca12010-02-27 07:55:05 +11001065static const char *
1066fmt_validity(void)
1067{
1068 char from[32], to[32];
1069 static char ret[64];
1070 time_t tt;
1071 struct tm *tm;
1072
1073 *from = *to = '\0';
1074 if (cert_valid_from == 0 &&
1075 cert_valid_to == 0xffffffffffffffffULL)
1076 return "forever";
1077
1078 if (cert_valid_from != 0) {
1079 /* XXX revisit INT_MAX in 2038 :) */
1080 tt = cert_valid_from > INT_MAX ? INT_MAX : cert_valid_from;
1081 tm = localtime(&tt);
1082 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1083 }
1084 if (cert_valid_to != 0xffffffffffffffffULL) {
1085 /* XXX revisit INT_MAX in 2038 :) */
1086 tt = cert_valid_to > INT_MAX ? INT_MAX : cert_valid_to;
1087 tm = localtime(&tt);
1088 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1089 }
1090
1091 if (cert_valid_from == 0) {
1092 snprintf(ret, sizeof(ret), "before %s", to);
1093 return ret;
1094 }
1095 if (cert_valid_to == 0xffffffffffffffffULL) {
1096 snprintf(ret, sizeof(ret), "after %s", from);
1097 return ret;
1098 }
1099
1100 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1101 return ret;
1102}
1103
1104static void
1105add_flag_constraint(Buffer *c, const char *name)
1106{
1107 debug3("%s: %s", __func__, name);
1108 buffer_put_cstring(c, name);
1109 buffer_put_string(c, NULL, 0);
1110}
1111
1112static void
1113add_string_constraint(Buffer *c, const char *name, const char *value)
1114{
1115 Buffer b;
1116
1117 debug3("%s: %s=%s", __func__, name, value);
1118 buffer_init(&b);
1119 buffer_put_cstring(&b, value);
1120
1121 buffer_put_cstring(c, name);
1122 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1123
1124 buffer_free(&b);
1125}
1126
1127static void
1128prepare_constraint_buf(Buffer *c)
1129{
1130
1131 buffer_clear(c);
1132 if ((constraint_flags & CONSTRAINT_X_FWD) != 0)
1133 add_flag_constraint(c, "permit-X11-forwarding");
1134 if ((constraint_flags & CONSTRAINT_AGENT_FWD) != 0)
1135 add_flag_constraint(c, "permit-agent-forwarding");
1136 if ((constraint_flags & CONSTRAINT_PORT_FWD) != 0)
1137 add_flag_constraint(c, "permit-port-forwarding");
1138 if ((constraint_flags & CONSTRAINT_PTY) != 0)
1139 add_flag_constraint(c, "permit-pty");
1140 if ((constraint_flags & CONSTRAINT_USER_RC) != 0)
1141 add_flag_constraint(c, "permit-user-rc");
1142 if (constraint_command != NULL)
1143 add_string_constraint(c, "forced-command", constraint_command);
1144 if (constraint_src_addr != NULL)
1145 add_string_constraint(c, "source-address", constraint_src_addr);
1146}
1147
1148static void
1149do_ca_sign(struct passwd *pw, int argc, char **argv)
1150{
1151 int i, fd;
1152 u_int n;
1153 Key *ca, *public;
1154 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1155 FILE *f;
1156
1157 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1158 if ((ca = load_identity(tmp)) == NULL)
1159 fatal("Couldn't load CA key \"%s\"", tmp);
1160 xfree(tmp);
1161
1162 for (i = 0; i < argc; i++) {
1163 /* Split list of principals */
1164 n = 0;
1165 if (cert_principals != NULL) {
1166 otmp = tmp = xstrdup(cert_principals);
1167 plist = NULL;
1168 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1169 plist = xrealloc(plist, n + 1, sizeof(*plist));
1170 if (*(plist[n] = xstrdup(cp)) == '\0')
1171 fatal("Empty principal name");
1172 }
1173 xfree(otmp);
1174 }
1175
1176 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1177 if ((public = key_load_public(tmp, &comment)) == NULL)
1178 fatal("%s: unable to open \"%s\"", __func__, tmp);
1179 if (public->type != KEY_RSA && public->type != KEY_DSA)
1180 fatal("%s: key \"%s\" type %s cannot be certified",
1181 __func__, tmp, key_type(public));
1182
1183 /* Prepare certificate to sign */
1184 if (key_to_certified(public) != 0)
1185 fatal("Could not upgrade key %s to certificate", tmp);
1186 public->cert->type = cert_key_type;
1187 public->cert->key_id = xstrdup(cert_key_id);
1188 public->cert->nprincipals = n;
1189 public->cert->principals = plist;
1190 public->cert->valid_after = cert_valid_from;
1191 public->cert->valid_before = cert_valid_to;
1192 prepare_constraint_buf(&public->cert->constraints);
1193 public->cert->signature_key = key_from_private(ca);
1194
1195 if (key_certify(public, ca) != 0)
1196 fatal("Couldn't not certify key %s", tmp);
1197
1198 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1199 *cp = '\0';
1200 xasprintf(&out, "%s-cert.pub", tmp);
1201 xfree(tmp);
1202
1203 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1204 fatal("Could not open \"%s\" for writing: %s", out,
1205 strerror(errno));
1206 if ((f = fdopen(fd, "w")) == NULL)
1207 fatal("%s: fdopen: %s", __func__, strerror(errno));
1208 if (!key_write(public, f))
1209 fatal("Could not write certified key to %s", out);
1210 fprintf(f, " %s\n", comment);
1211 fclose(f);
1212
1213 if (!quiet)
1214 logit("Signed %s key %s: id \"%s\"%s%s valid %s",
1215 cert_key_type == SSH2_CERT_TYPE_USER?"user":"host",
1216 out, cert_key_id,
1217 cert_principals != NULL ? " for " : "",
1218 cert_principals != NULL ? cert_principals : "",
1219 fmt_validity());
1220
1221 key_free(public);
1222 xfree(out);
1223 }
1224 exit(0);
1225}
1226
1227static u_int64_t
1228parse_relative_time(const char *s, time_t now)
1229{
1230 int64_t mul, secs;
1231
1232 mul = *s == '-' ? -1 : 1;
1233
1234 if ((secs = convtime(s + 1)) == -1)
1235 fatal("Invalid relative certificate time %s", s);
1236 if (mul == -1 && secs > now)
1237 fatal("Certificate time %s cannot be represented", s);
1238 return now + (u_int64_t)(secs * mul);
1239}
1240
1241static u_int64_t
1242parse_absolute_time(const char *s)
1243{
1244 struct tm tm;
1245 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001246 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001247
Damien Miller2ca342b2010-03-03 12:14:15 +11001248 /*
1249 * POSIX strptime says "The application shall ensure that there
1250 * is white-space or other non-alphanumeric characters between
1251 * any two conversion specifications" so arrange things this way.
1252 */
1253 switch (strlen(s)) {
1254 case 8:
1255 fmt = "%Y/%m/%d";
1256 snprintf(buf, sizeof(buf), "%.4s/%.2s/%.2s", s, s + 4, s + 6);
1257 break;
1258 case 14:
1259 fmt = "%Y/%m/%d %H:%M:%S";
1260 snprintf(buf, sizeof(buf), "%.4s/%.2s/%.2s %.2s:%.2s:%.2s",
1261 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1262 break;
1263 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001264 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001265 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001266
1267 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001268 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001269 fatal("Invalid certificate time %s", s);
1270 if ((tt = mktime(&tm)) < 0)
1271 fatal("Certificate time %s cannot be represented", s);
1272 return (u_int64_t)tt;
1273}
1274
1275static void
1276parse_cert_times(char *timespec)
1277{
1278 char *from, *to;
1279 time_t now = time(NULL);
1280 int64_t secs;
1281
1282 /* +timespec relative to now */
1283 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1284 if ((secs = convtime(timespec + 1)) == -1)
1285 fatal("Invalid relative certificate life %s", timespec);
1286 cert_valid_to = now + secs;
1287 /*
1288 * Backdate certificate one minute to avoid problems on hosts
1289 * with poorly-synchronised clocks.
1290 */
1291 cert_valid_from = ((now - 59)/ 60) * 60;
1292 return;
1293 }
1294
1295 /*
1296 * from:to, where
1297 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1298 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1299 */
1300 from = xstrdup(timespec);
1301 to = strchr(from, ':');
1302 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001303 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001304 *to++ = '\0';
1305
1306 if (*from == '-' || *from == '+')
1307 cert_valid_from = parse_relative_time(from, now);
1308 else
1309 cert_valid_from = parse_absolute_time(from);
1310
1311 if (*to == '-' || *to == '+')
1312 cert_valid_to = parse_relative_time(to, cert_valid_from);
1313 else
1314 cert_valid_to = parse_absolute_time(to);
1315
1316 if (cert_valid_to <= cert_valid_from)
1317 fatal("Empty certificate validity interval");
1318 xfree(from);
1319}
1320
1321static void
1322add_cert_constraint(char *opt)
1323{
1324 char *val;
1325
1326 if (strcmp(opt, "clear") == 0)
1327 constraint_flags = 0;
1328 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
1329 constraint_flags &= ~CONSTRAINT_X_FWD;
1330 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
1331 constraint_flags |= CONSTRAINT_X_FWD;
1332 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
1333 constraint_flags &= ~CONSTRAINT_AGENT_FWD;
1334 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
1335 constraint_flags |= CONSTRAINT_AGENT_FWD;
1336 else if (strcasecmp(opt, "no-port-forwarding") == 0)
1337 constraint_flags &= ~CONSTRAINT_PORT_FWD;
1338 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
1339 constraint_flags |= CONSTRAINT_PORT_FWD;
1340 else if (strcasecmp(opt, "no-pty") == 0)
1341 constraint_flags &= ~CONSTRAINT_PTY;
1342 else if (strcasecmp(opt, "permit-pty") == 0)
1343 constraint_flags |= CONSTRAINT_PTY;
1344 else if (strcasecmp(opt, "no-user-rc") == 0)
1345 constraint_flags &= ~CONSTRAINT_USER_RC;
1346 else if (strcasecmp(opt, "permit-user-rc") == 0)
1347 constraint_flags |= CONSTRAINT_USER_RC;
1348 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1349 val = opt + 14;
1350 if (*val == '\0')
1351 fatal("Empty force-command constraint");
1352 if (constraint_command != NULL)
1353 fatal("force-command already specified");
1354 constraint_command = xstrdup(val);
1355 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1356 val = opt + 15;
1357 if (*val == '\0')
1358 fatal("Empty source-address constraint");
1359 if (constraint_src_addr != NULL)
1360 fatal("source-address already specified");
1361 if (addr_match_cidr_list(NULL, val) != 0)
1362 fatal("Invalid source-address list");
1363 constraint_src_addr = xstrdup(val);
1364 } else
1365 fatal("Unsupported certificate constraint \"%s\"", opt);
1366}
1367
Ben Lindstrombba81212001-06-25 05:01:22 +00001368static void
Damien Miller431f66b1999-11-21 18:31:57 +11001369usage(void)
1370{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001371 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001372 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001373 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001374 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001375 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001376 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001377 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001378#ifdef ENABLE_PKCS11
1379 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1380#endif
Darren Tucker26dc3e62007-02-19 22:09:06 +11001381 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001382 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1383 fprintf(stderr, " -f filename Filename of the key file.\n");
1384 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1385 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1386 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001387 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1388 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Darren Tucker26dc3e62007-02-19 22:09:06 +11001389 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001390 fprintf(stderr, " -l Show fingerprint of key file.\n");
1391 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001392 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001393 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001394 fprintf(stderr, " -O cnstr Specify a certificate constraint.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001395 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1396 fprintf(stderr, " -p Change passphrase of private key file.\n");
1397 fprintf(stderr, " -q Quiet.\n");
1398 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1399 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001400 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001401 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1402 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1403 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001404 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001405 fprintf(stderr, " -v Verbose.\n");
1406 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1407 fprintf(stderr, " -y Read private key file and print public key.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001408
Damien Miller95def091999-11-25 00:26:21 +11001409 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001410}
1411
Damien Miller95def091999-11-25 00:26:21 +11001412/*
1413 * Main program for key management.
1414 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001415int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001416main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001417{
Damien Millera41c8b12002-01-22 23:05:08 +11001418 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller7ea845e2010-02-12 09:21:02 +11001419 char out_file[MAXPATHLEN], *pkcs11provider = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001420 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001421 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001422 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001423 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001424 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001425 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001426 int do_gen_candidates = 0, do_screen_candidates = 0;
1427 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001428 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001429 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001430
Damien Miller95def091999-11-25 00:26:21 +11001431 extern int optind;
1432 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001433
Darren Tuckerce321d82005-10-03 18:11:24 +10001434 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1435 sanitise_stdfd();
1436
Darren Tucker9ac56e92007-01-14 10:19:59 +11001437 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001438
Damien Millerd3a18572000-06-07 19:55:44 +10001439 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001440 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001441
Kevin Steves3a881912002-07-20 19:05:40 +00001442 init_rng();
1443 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001444
Damien Miller5428f641999-11-25 11:54:57 +11001445 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001446 pw = getpwuid(getuid());
1447 if (!pw) {
1448 printf("You don't exist, go away!\n");
1449 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001450 }
Damien Millereba71ba2000-04-29 23:57:08 +10001451 if (gethostname(hostname, sizeof(hostname)) < 0) {
1452 perror("gethostname");
1453 exit(1);
1454 }
Damien Miller5428f641999-11-25 11:54:57 +11001455
Damien Miller0a80ca12010-02-27 07:55:05 +11001456 while ((opt = getopt(argc, argv, "degiqpclBHhvxXyF:b:f:t:D:I:P:N:n:"
1457 "O:C:r:g:R:T:G:M:S:s:a:V:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001458 switch (opt) {
1459 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001460 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001461 if (errstr)
1462 fatal("Bits has bad value %s (%s)",
1463 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001464 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001465 case 'F':
1466 find_host = 1;
1467 rr_hostname = optarg;
1468 break;
1469 case 'H':
1470 hash_hosts = 1;
1471 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001472 case 'I':
1473 cert_key_id = optarg;
1474 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001475 case 'R':
1476 delete_host = 1;
1477 rr_hostname = optarg;
1478 break;
Damien Miller95def091999-11-25 00:26:21 +11001479 case 'l':
1480 print_fingerprint = 1;
1481 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001482 case 'B':
1483 print_bubblebabble = 1;
1484 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001485 case 'n':
1486 cert_principals = optarg;
1487 break;
Damien Miller95def091999-11-25 00:26:21 +11001488 case 'p':
1489 change_passphrase = 1;
1490 break;
Damien Miller95def091999-11-25 00:26:21 +11001491 case 'c':
1492 change_comment = 1;
1493 break;
Damien Miller95def091999-11-25 00:26:21 +11001494 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001495 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1496 sizeof(identity_file))
1497 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001498 have_identity = 1;
1499 break;
Damien Miller37876e92003-05-15 10:19:46 +10001500 case 'g':
1501 print_generic = 1;
1502 break;
Damien Miller95def091999-11-25 00:26:21 +11001503 case 'P':
1504 identity_passphrase = optarg;
1505 break;
Damien Miller95def091999-11-25 00:26:21 +11001506 case 'N':
1507 identity_new_passphrase = optarg;
1508 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001509 case 'O':
1510 add_cert_constraint(optarg);
1511 break;
Damien Miller95def091999-11-25 00:26:21 +11001512 case 'C':
1513 identity_comment = optarg;
1514 break;
Damien Miller95def091999-11-25 00:26:21 +11001515 case 'q':
1516 quiet = 1;
1517 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001518 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001519 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001520 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001521 convert_to_ssh2 = 1;
1522 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001523 case 'h':
1524 cert_key_type = SSH2_CERT_TYPE_HOST;
1525 constraint_flags = 0;
1526 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001527 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001528 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001529 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001530 convert_from_ssh2 = 1;
1531 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001532 case 'y':
1533 print_public = 1;
1534 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001535 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001536 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001537 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001538 case 's':
1539 ca_key_path = optarg;
1540 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001541 case 't':
1542 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001543 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001544 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001545 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001546 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001547 case 'v':
1548 if (log_level == SYSLOG_LEVEL_INFO)
1549 log_level = SYSLOG_LEVEL_DEBUG1;
1550 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001551 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001552 log_level < SYSLOG_LEVEL_DEBUG3)
1553 log_level++;
1554 }
1555 break;
Damien Miller37876e92003-05-15 10:19:46 +10001556 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001557 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001558 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001559 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001560 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1561 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001562 if (errstr)
1563 fatal("Desired generator has bad value: %s (%s)",
1564 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001565 break;
1566 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001567 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001568 if (errstr)
1569 fatal("Invalid number of trials: %s (%s)",
1570 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001571 break;
1572 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001573 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001574 if (errstr) {
1575 fatal("Memory limit is %s: %s", errstr, optarg);
1576 }
Darren Tucker019cefe2003-08-02 22:40:07 +10001577 break;
1578 case 'G':
1579 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001580 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1581 sizeof(out_file))
1582 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001583 break;
1584 case 'T':
1585 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001586 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1587 sizeof(out_file))
1588 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001589 break;
1590 case 'S':
1591 /* XXX - also compare length against bits */
1592 if (BN_hex2bn(&start, optarg) == 0)
1593 fatal("Invalid start point.");
1594 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001595 case 'V':
1596 parse_cert_times(optarg);
1597 break;
Damien Miller95def091999-11-25 00:26:21 +11001598 case '?':
1599 default:
1600 usage();
1601 }
1602 }
Darren Tucker06930c72003-12-31 11:34:51 +11001603
1604 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001605 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001606
Damien Miller0a80ca12010-02-27 07:55:05 +11001607 argv += optind;
1608 argc -= optind;
1609
1610 if (ca_key_path != NULL) {
1611 if (argc < 1) {
1612 printf("Too few arguments.\n");
1613 usage();
1614 }
1615 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11001616 printf("Too many arguments.\n");
1617 usage();
1618 }
1619 if (change_passphrase && change_comment) {
1620 printf("Can only have one of -p and -c.\n");
1621 usage();
1622 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001623 if (print_fingerprint && (delete_host || hash_hosts)) {
1624 printf("Cannot use -l with -D or -R.\n");
1625 usage();
1626 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001627 if (ca_key_path != NULL) {
1628 if (cert_key_id == NULL)
1629 fatal("Must specify key id (-I) when certifying");
1630 do_ca_sign(pw, argc, argv);
1631 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001632 if (delete_host || hash_hosts || find_host)
1633 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001634 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001635 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001636 if (change_passphrase)
1637 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001638 if (change_comment)
1639 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001640 if (convert_to_ssh2)
1641 do_convert_to_ssh2(pw);
1642 if (convert_from_ssh2)
1643 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001644 if (print_public)
1645 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001646 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001647 unsigned int n = 0;
1648
1649 if (have_identity) {
1650 n = do_print_resource_record(pw,
1651 identity_file, rr_hostname);
1652 if (n == 0) {
1653 perror(identity_file);
1654 exit(1);
1655 }
1656 exit(0);
1657 } else {
1658
1659 n += do_print_resource_record(pw,
1660 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1661 n += do_print_resource_record(pw,
1662 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1663
1664 if (n == 0)
1665 fatal("no keys found.");
1666 exit(0);
1667 }
Damien Miller37876e92003-05-15 10:19:46 +10001668 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001669 if (pkcs11provider != NULL)
1670 do_download(pw, pkcs11provider);
Damien Miller95def091999-11-25 00:26:21 +11001671
Darren Tucker019cefe2003-08-02 22:40:07 +10001672 if (do_gen_candidates) {
1673 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001674
Darren Tucker019cefe2003-08-02 22:40:07 +10001675 if (out == NULL) {
1676 error("Couldn't open modulus candidate file \"%s\": %s",
1677 out_file, strerror(errno));
1678 return (1);
1679 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001680 if (bits == 0)
1681 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001682 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001683 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001684
1685 return (0);
1686 }
1687
1688 if (do_screen_candidates) {
1689 FILE *in;
1690 FILE *out = fopen(out_file, "w");
1691
1692 if (have_identity && strcmp(identity_file, "-") != 0) {
1693 if ((in = fopen(identity_file, "r")) == NULL) {
1694 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001695 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001696 strerror(errno));
1697 }
1698 } else
1699 in = stdin;
1700
1701 if (out == NULL) {
1702 fatal("Couldn't open moduli file \"%s\": %s",
1703 out_file, strerror(errno));
1704 }
1705 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001706 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001707 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001708 }
1709
Damien Miller95def091999-11-25 00:26:21 +11001710 arc4random_stir();
1711
Damien Millerf14be5c2005-11-05 15:15:49 +11001712 if (key_type_name == NULL)
1713 key_type_name = "rsa";
1714
Damien Millere39cacc2000-11-29 12:18:44 +11001715 type = key_type_from_name(key_type_name);
1716 if (type == KEY_UNSPEC) {
1717 fprintf(stderr, "unknown key type %s\n", key_type_name);
1718 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001719 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001720 if (bits == 0)
1721 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Tim Rice660c3402005-11-28 17:45:32 -08001722 if (type == KEY_DSA && bits != 1024)
1723 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001724 if (!quiet)
1725 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001726 private = key_generate(type, bits);
1727 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001728 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001729 exit(1);
1730 }
1731 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001732
1733 if (!have_identity)
1734 ask_filename(pw, "Enter file in which to save the key");
1735
Damien Miller788f2122005-11-05 15:14:59 +11001736 /* Create ~/.ssh directory if it doesn't already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001737 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001738 if (strstr(identity_file, dotsshdir) != NULL &&
1739 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001740 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001741 error("Could not create directory '%s'.", dotsshdir);
1742 else if (!quiet)
1743 printf("Created directory '%s'.\n", dotsshdir);
1744 }
1745 /* If the file already exists, ask the user to confirm. */
1746 if (stat(identity_file, &st) >= 0) {
1747 char yesno[3];
1748 printf("%s already exists.\n", identity_file);
1749 printf("Overwrite (y/n)? ");
1750 fflush(stdout);
1751 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1752 exit(1);
1753 if (yesno[0] != 'y' && yesno[0] != 'Y')
1754 exit(1);
1755 }
1756 /* Ask for a passphrase (twice). */
1757 if (identity_passphrase)
1758 passphrase1 = xstrdup(identity_passphrase);
1759 else if (identity_new_passphrase)
1760 passphrase1 = xstrdup(identity_new_passphrase);
1761 else {
1762passphrase_again:
1763 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001764 read_passphrase("Enter passphrase (empty for no "
1765 "passphrase): ", RP_ALLOW_STDIN);
1766 passphrase2 = read_passphrase("Enter same passphrase again: ",
1767 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001768 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001769 /*
1770 * The passphrases do not match. Clear them and
1771 * retry.
1772 */
Damien Miller95def091999-11-25 00:26:21 +11001773 memset(passphrase1, 0, strlen(passphrase1));
1774 memset(passphrase2, 0, strlen(passphrase2));
1775 xfree(passphrase1);
1776 xfree(passphrase2);
1777 printf("Passphrases do not match. Try again.\n");
1778 goto passphrase_again;
1779 }
1780 /* Clear the other copy of the passphrase. */
1781 memset(passphrase2, 0, strlen(passphrase2));
1782 xfree(passphrase2);
1783 }
1784
Damien Miller95def091999-11-25 00:26:21 +11001785 if (identity_comment) {
1786 strlcpy(comment, identity_comment, sizeof(comment));
1787 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11001788 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001789 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1790 }
1791
1792 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001793 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001794 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001795 memset(passphrase1, 0, strlen(passphrase1));
1796 xfree(passphrase1);
1797 exit(1);
1798 }
1799 /* Clear the passphrase. */
1800 memset(passphrase1, 0, strlen(passphrase1));
1801 xfree(passphrase1);
1802
1803 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001804 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001805 arc4random_stir();
1806
1807 if (!quiet)
1808 printf("Your identification has been saved in %s.\n", identity_file);
1809
Damien Miller95def091999-11-25 00:26:21 +11001810 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001811 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1812 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001813 printf("Could not save your public key in %s\n", identity_file);
1814 exit(1);
1815 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001816 f = fdopen(fd, "w");
1817 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001818 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001819 exit(1);
1820 }
Damien Millereba71ba2000-04-29 23:57:08 +10001821 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001822 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001823 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001824 fclose(f);
1825
1826 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001827 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001828 char *ra = key_fingerprint(public, SSH_FP_MD5,
1829 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10001830 printf("Your public key has been saved in %s.\n",
1831 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001832 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001833 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001834 printf("The key's randomart image is:\n");
1835 printf("%s\n", ra);
1836 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00001837 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001838 }
Damien Millereba71ba2000-04-29 23:57:08 +10001839
1840 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001841 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001842}