blob: 492c301d34988931702e25677745326ed0853070 [file] [log] [blame]
Damien Millerf2b70ca2010-03-05 07:39:35 +11001/* $OpenBSD: ssh-keygen.c,v 1.182 2010/03/04 20:35:08 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
108/* Key type when certifying */
109u_int cert_key_type = SSH2_CERT_TYPE_USER;
110
111/* "key ID" of signed key */
112char *cert_key_id = NULL;
113
114/* Comma-separated list of principal names for certifying keys */
115char *cert_principals = NULL;
116
117/* Validity period for certificates */
118u_int64_t cert_valid_from = 0;
119u_int64_t cert_valid_to = ~0ULL;
120
121/* Certificate constraints */
122#define CONSTRAINT_X_FWD (1)
123#define CONSTRAINT_AGENT_FWD (1<<1)
124#define CONSTRAINT_PORT_FWD (1<<2)
125#define CONSTRAINT_PTY (1<<3)
126#define CONSTRAINT_USER_RC (1<<4)
127#define CONSTRAINT_DEFAULT (CONSTRAINT_X_FWD|CONSTRAINT_AGENT_FWD| \
128 CONSTRAINT_PORT_FWD|CONSTRAINT_PTY| \
129 CONSTRAINT_USER_RC)
130u_int32_t constraint_flags = CONSTRAINT_DEFAULT;
131char *constraint_command = NULL;
132char *constraint_src_addr = NULL;
133
Damien Millereba71ba2000-04-29 23:57:08 +1000134/* Dump public key file in format used by real and the original SSH 2 */
135int convert_to_ssh2 = 0;
136int convert_from_ssh2 = 0;
137int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000138int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100139
Damien Millera41c8b12002-01-22 23:05:08 +1100140char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000141
Damien Miller431f66b1999-11-21 18:31:57 +1100142/* argv0 */
143extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Millereba71ba2000-04-29 23:57:08 +1000145char hostname[MAXHOSTNAMELEN];
146
Darren Tucker770fc012004-05-13 16:24:32 +1000147/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000148int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000149int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
150
Ben Lindstrombba81212001-06-25 05:01:22 +0000151static void
Damien Miller431f66b1999-11-21 18:31:57 +1100152ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100155 char *name = NULL;
156
Damien Miller993dd552002-02-19 15:22:47 +1100157 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000158 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100159 else {
Damien Miller993dd552002-02-19 15:22:47 +1100160 switch (key_type_from_name(key_type_name)) {
161 case KEY_RSA1:
162 name = _PATH_SSH_CLIENT_IDENTITY;
163 break;
164 case KEY_DSA:
165 name = _PATH_SSH_CLIENT_ID_DSA;
166 break;
167 case KEY_RSA:
168 name = _PATH_SSH_CLIENT_ID_RSA;
169 break;
170 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100171 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100172 exit(1);
173 break;
174 }
Damien Miller90967402006-03-26 14:07:26 +1100175 }
Damien Millere39cacc2000-11-29 12:18:44 +1100176 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000177 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100178 if (fgets(buf, sizeof(buf), stdin) == NULL)
179 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000180 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100181 if (strcmp(buf, "") != 0)
182 strlcpy(identity_file, buf, sizeof(identity_file));
183 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100184}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185
Ben Lindstrombba81212001-06-25 05:01:22 +0000186static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000187load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000188{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000189 char *pass;
190 Key *prv;
191
Ben Lindstroma3700052001-04-05 23:26:32 +0000192 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000193 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000194 if (identity_passphrase)
195 pass = xstrdup(identity_passphrase);
196 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000197 pass = read_passphrase("Enter passphrase: ",
198 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000199 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000200 memset(pass, 0, strlen(pass));
201 xfree(pass);
202 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000203 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000204}
205
Damien Miller874d77b2000-10-14 16:23:11 +1100206#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000207#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100208#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000209#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000210
Ben Lindstrombba81212001-06-25 05:01:22 +0000211static void
Damien Millereba71ba2000-04-29 23:57:08 +1000212do_convert_to_ssh2(struct passwd *pw)
213{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000214 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000215 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000216 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100217 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000218 struct stat st;
219
220 if (!have_identity)
221 ask_filename(pw, "Enter file in which the key is");
222 if (stat(identity_file, &st) < 0) {
223 perror(identity_file);
224 exit(1);
225 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000226 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000227 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000228 fprintf(stderr, "load failed\n");
229 exit(1);
230 }
Damien Millereba71ba2000-04-29 23:57:08 +1000231 }
Damien Millerdb274722003-05-14 13:45:22 +1000232 if (k->type == KEY_RSA1) {
233 fprintf(stderr, "version 1 keys are not supported\n");
234 exit(1);
235 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000236 if (key_to_blob(k, &blob, &len) <= 0) {
237 fprintf(stderr, "key_to_blob failed\n");
238 exit(1);
239 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100240 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
241 snprintf(comment, sizeof(comment),
242 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000243 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000244 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100245
246 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
247 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000248 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100249 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000250 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000251 xfree(blob);
252 exit(0);
253}
254
Ben Lindstrombba81212001-06-25 05:01:22 +0000255static void
Damien Miller874d77b2000-10-14 16:23:11 +1100256buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
257{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000258 u_int bignum_bits = buffer_get_int(b);
259 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000260
Damien Miller874d77b2000-10-14 16:23:11 +1100261 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000262 fatal("buffer_get_bignum_bits: input buffer too small: "
263 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100264 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
265 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100266 buffer_consume(b, bytes);
267}
268
Ben Lindstrombba81212001-06-25 05:01:22 +0000269static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000270do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100271{
272 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100273 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100274 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000275 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000276 int magic, rlen, ktype, i1, i2, i3, i4;
277 u_int slen;
278 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100279
280 buffer_init(&b);
281 buffer_append(&b, blob, blen);
282
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100283 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100284 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
285 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
286 buffer_free(&b);
287 return NULL;
288 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000289 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100290 type = buffer_get_string(&b, NULL);
291 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000292 i2 = buffer_get_int(&b);
293 i3 = buffer_get_int(&b);
294 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100295 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100296 if (strcmp(cipher, "none") != 0) {
297 error("unsupported cipher %s", cipher);
298 xfree(cipher);
299 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000300 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100301 return NULL;
302 }
303 xfree(cipher);
304
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000305 if (strstr(type, "dsa")) {
306 ktype = KEY_DSA;
307 } else if (strstr(type, "rsa")) {
308 ktype = KEY_RSA;
309 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100310 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000311 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100312 return NULL;
313 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000314 key = key_new_private(ktype);
315 xfree(type);
316
317 switch (key->type) {
318 case KEY_DSA:
319 buffer_get_bignum_bits(&b, key->dsa->p);
320 buffer_get_bignum_bits(&b, key->dsa->g);
321 buffer_get_bignum_bits(&b, key->dsa->q);
322 buffer_get_bignum_bits(&b, key->dsa->pub_key);
323 buffer_get_bignum_bits(&b, key->dsa->priv_key);
324 break;
325 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100326 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000327 debug("e %lx", e);
328 if (e < 30) {
329 e <<= 8;
330 e += buffer_get_char(&b);
331 debug("e %lx", e);
332 e <<= 8;
333 e += buffer_get_char(&b);
334 debug("e %lx", e);
335 }
336 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000337 buffer_free(&b);
338 key_free(key);
339 return NULL;
340 }
341 buffer_get_bignum_bits(&b, key->rsa->d);
342 buffer_get_bignum_bits(&b, key->rsa->n);
343 buffer_get_bignum_bits(&b, key->rsa->iqmp);
344 buffer_get_bignum_bits(&b, key->rsa->q);
345 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000346 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000347 break;
348 }
Damien Miller874d77b2000-10-14 16:23:11 +1100349 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000350 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000351 error("do_convert_private_ssh2_from_blob: "
352 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100353 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000354
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000355 /* try the key */
356 key_sign(key, &sig, &slen, data, sizeof(data));
357 key_verify(key, sig, slen, data, sizeof(data));
358 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100359 return key;
360}
361
Damien Miller8056a9d2006-03-15 12:05:40 +1100362static int
363get_line(FILE *fp, char *line, size_t len)
364{
365 int c;
366 size_t pos = 0;
367
368 line[0] = '\0';
369 while ((c = fgetc(fp)) != EOF) {
370 if (pos >= len - 1) {
371 fprintf(stderr, "input line too long.\n");
372 exit(1);
373 }
Damien Miller90967402006-03-26 14:07:26 +1100374 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100375 case '\r':
376 c = fgetc(fp);
377 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
378 fprintf(stderr, "unget: %s\n", strerror(errno));
379 exit(1);
380 }
381 return pos;
382 case '\n':
383 return pos;
384 }
385 line[pos++] = c;
386 line[pos] = '\0';
387 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100388 /* We reached EOF */
389 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100390}
391
Ben Lindstrombba81212001-06-25 05:01:22 +0000392static void
Damien Millereba71ba2000-04-29 23:57:08 +1000393do_convert_from_ssh2(struct passwd *pw)
394{
395 Key *k;
396 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000397 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100398 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000399 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000400 char encoded[8096];
401 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100402 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000403 FILE *fp;
404
405 if (!have_identity)
406 ask_filename(pw, "Enter file in which the key is");
407 if (stat(identity_file, &st) < 0) {
408 perror(identity_file);
409 exit(1);
410 }
411 fp = fopen(identity_file, "r");
412 if (fp == NULL) {
413 perror(identity_file);
414 exit(1);
415 }
416 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100417 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
418 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000419 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000420 if (strncmp(line, "----", 4) == 0 ||
421 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100422 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
423 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000424 if (strstr(line, " END ") != NULL) {
425 break;
426 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000427 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000428 continue;
429 }
Damien Miller30c3d422000-05-09 11:02:59 +1000430 if (escaped) {
431 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000432 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000433 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000434 }
Damien Millereba71ba2000-04-29 23:57:08 +1000435 strlcat(encoded, line, sizeof(encoded));
436 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000437 len = strlen(encoded);
438 if (((len % 4) == 3) &&
439 (encoded[len-1] == '=') &&
440 (encoded[len-2] == '=') &&
441 (encoded[len-3] == '='))
442 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100443 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000444 if (blen < 0) {
445 fprintf(stderr, "uudecode failed.\n");
446 exit(1);
447 }
Damien Miller874d77b2000-10-14 16:23:11 +1100448 k = private ?
449 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100450 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100451 if (k == NULL) {
452 fprintf(stderr, "decode blob failed.\n");
453 exit(1);
454 }
455 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000456 (k->type == KEY_DSA ?
457 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
458 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100459 key_write(k, stdout);
460 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100461 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100462 exit(1);
463 }
Damien Millereba71ba2000-04-29 23:57:08 +1000464 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100465 if (!private)
466 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000467 fclose(fp);
468 exit(0);
469}
470
Ben Lindstrombba81212001-06-25 05:01:22 +0000471static void
Damien Millereba71ba2000-04-29 23:57:08 +1000472do_print_public(struct passwd *pw)
473{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000474 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000475 struct stat st;
476
477 if (!have_identity)
478 ask_filename(pw, "Enter file in which the key is");
479 if (stat(identity_file, &st) < 0) {
480 perror(identity_file);
481 exit(1);
482 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000483 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000484 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000485 fprintf(stderr, "load failed\n");
486 exit(1);
487 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000488 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000489 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000490 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000491 fprintf(stdout, "\n");
492 exit(0);
493}
494
Ben Lindstromcd392282001-07-04 03:44:03 +0000495static void
Damien Miller86cbb442010-02-12 09:22:57 +1100496do_download(struct passwd *pw, char *pkcs11provider)
Ben Lindstromcd392282001-07-04 03:44:03 +0000497{
Damien Miller7ea845e2010-02-12 09:21:02 +1100498#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000499 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100500 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000501
Damien Miller7ea845e2010-02-12 09:21:02 +1100502 pkcs11_init(0);
503 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
504 if (nkeys <= 0)
505 fatal("cannot read public key from pkcs11");
506 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000507 key_write(keys[i], stdout);
508 key_free(keys[i]);
509 fprintf(stdout, "\n");
510 }
511 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100512 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000513 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100514#else
515 fatal("no pkcs11 support");
516#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000517}
Ben Lindstromcd392282001-07-04 03:44:03 +0000518
Ben Lindstrombba81212001-06-25 05:01:22 +0000519static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100520do_fingerprint(struct passwd *pw)
521{
Damien Miller98c7ad62000-03-09 21:27:49 +1100522 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000523 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000524 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100525 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000526 enum fp_rep rep;
527 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100528 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100529
Ben Lindstromd0fca422001-03-26 13:44:06 +0000530 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
531 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000532
Damien Miller95def091999-11-25 00:26:21 +1100533 if (!have_identity)
534 ask_filename(pw, "Enter file in which the key is");
535 if (stat(identity_file, &st) < 0) {
536 perror(identity_file);
537 exit(1);
538 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000539 public = key_load_public(identity_file, &comment);
540 if (public != NULL) {
541 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100542 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000543 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
544 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000545 if (log_level >= SYSLOG_LEVEL_VERBOSE)
546 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100547 key_free(public);
548 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000549 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000550 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100551 exit(0);
552 }
Damien Miller40b59852006-06-13 13:00:25 +1000553 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000554 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000555 comment = NULL;
556 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100557
558 f = fopen(identity_file, "r");
559 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100560 while (fgets(line, sizeof(line), f)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000561 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100562 error("line %d too long: %.40s...",
563 num + 1, line);
Damien Miller98c7ad62000-03-09 21:27:49 +1100564 skip = 1;
565 continue;
Damien Miller95def091999-11-25 00:26:21 +1100566 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100567 num++;
568 if (skip) {
569 skip = 0;
570 continue;
571 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000572 *cp = '\0';
Damien Miller98c7ad62000-03-09 21:27:49 +1100573
574 /* Skip leading whitespace, empty and comment lines. */
575 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
576 ;
577 if (!*cp || *cp == '\n' || *cp == '#')
Damien Miller80163902007-01-05 16:30:16 +1100578 continue;
Damien Miller98c7ad62000-03-09 21:27:49 +1100579 i = strtol(cp, &ep, 10);
580 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
581 int quoted = 0;
582 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000583 for (; *cp && (quoted || (*cp != ' ' &&
584 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100585 if (*cp == '\\' && cp[1] == '"')
586 cp++; /* Skip both */
587 else if (*cp == '"')
588 quoted = !quoted;
589 }
590 if (!*cp)
591 continue;
592 *cp++ = '\0';
593 }
594 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000595 public = key_new(KEY_RSA1);
596 if (key_read(public, &cp) != 1) {
597 cp = ep;
598 key_free(public);
599 public = key_new(KEY_UNSPEC);
600 if (key_read(public, &cp) != 1) {
601 key_free(public);
602 continue;
603 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100604 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000605 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000606 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100607 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000608 printf("%u %s %s (%s)\n", key_size(public), fp,
609 comment ? comment : "no comment", key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000610 if (log_level >= SYSLOG_LEVEL_VERBOSE)
611 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000612 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000613 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000614 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000615 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100616 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100617 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100618 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100619 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100620 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100621 exit(1);
622 }
Damien Miller95def091999-11-25 00:26:21 +1100623 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100624}
625
Damien Miller4b42d7f2005-03-01 21:48:35 +1100626static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100627printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100628{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000629 if (print_fingerprint) {
630 enum fp_rep rep;
631 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000632 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000633
634 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
635 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
636 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100637 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000638 printf("%u %s %s (%s)\n", key_size(public), fp, name,
639 key_type(public));
640 if (log_level >= SYSLOG_LEVEL_VERBOSE)
641 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000642 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000643 xfree(fp);
644 } else {
645 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
646 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100647 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000648 if (!key_write(public, f))
649 fatal("key_write failed");
650 fprintf(f, "\n");
651 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100652}
653
654static void
655do_known_hosts(struct passwd *pw, const char *name)
656{
657 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100658 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100659 char *cp, *cp2, *kp, *kp2;
660 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100661 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100662 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100663
664 if (!have_identity) {
665 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
666 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
667 sizeof(identity_file))
668 fatal("Specified known hosts path too long");
669 xfree(cp);
670 have_identity = 1;
671 }
672 if ((in = fopen(identity_file, "r")) == NULL)
673 fatal("fopen: %s", strerror(errno));
674
675 /*
676 * Find hosts goes to stdout, hash and deletions happen in-place
677 * A corner case is ssh-keygen -HF foo, which should go to stdout
678 */
679 if (!find_host && (hash_hosts || delete_host)) {
680 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
681 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
682 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
683 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
684 fatal("known_hosts path too long");
685 umask(077);
686 if ((c = mkstemp(tmp)) == -1)
687 fatal("mkstemp: %s", strerror(errno));
688 if ((out = fdopen(c, "w")) == NULL) {
689 c = errno;
690 unlink(tmp);
691 fatal("fdopen: %s", strerror(c));
692 }
693 inplace = 1;
694 }
695
696 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000697 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100698 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100699 skip = 1;
700 invalid = 1;
701 continue;
702 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000703 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100704 if (skip) {
705 skip = 0;
706 continue;
707 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000708 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100709
710 /* Skip leading whitespace, empty and comment lines. */
711 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
712 ;
713 if (!*cp || *cp == '\n' || *cp == '#') {
714 if (inplace)
715 fprintf(out, "%s\n", cp);
716 continue;
717 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100718 /* Check whether this is a CA key */
719 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
720 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
721 cp[sizeof(CA_MARKER) - 1] == '\t')) {
722 ca = 1;
723 cp += sizeof(CA_MARKER);
724 } else
725 ca = 0;
726
Damien Miller4b42d7f2005-03-01 21:48:35 +1100727 /* Find the end of the host name portion. */
728 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
729 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100730
Damien Miller4b42d7f2005-03-01 21:48:35 +1100731 if (*kp == '\0' || *(kp + 1) == '\0') {
732 error("line %d missing key: %.40s...",
733 num, line);
734 invalid = 1;
735 continue;
736 }
737 *kp++ = '\0';
738 kp2 = kp;
739
Damien Miller0a80ca12010-02-27 07:55:05 +1100740 pub = key_new(KEY_RSA1);
741 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100742 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100743 key_free(pub);
744 pub = key_new(KEY_UNSPEC);
745 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100746 error("line %d invalid key: %.40s...",
747 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100748 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100749 invalid = 1;
750 continue;
751 }
752 }
753
754 if (*cp == HASH_DELIM) {
755 if (find_host || delete_host) {
756 cp2 = host_hash(name, cp, strlen(cp));
757 if (cp2 == NULL) {
758 error("line %d: invalid hashed "
759 "name: %.64s...", num, line);
760 invalid = 1;
761 continue;
762 }
763 c = (strcmp(cp2, cp) == 0);
764 if (find_host && c) {
765 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100766 "line %d type %s%s\n", name,
767 num, key_type(pub),
768 ca ? " (CA key)" : "");
769 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100770 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100771 if (delete_host && !c && !ca)
772 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100773 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100774 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100775 } else {
776 if (find_host || delete_host) {
777 c = (match_hostname(name, cp,
778 strlen(cp)) == 1);
779 if (find_host && c) {
780 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100781 "line %d type %s%s\n", name,
782 num, key_type(pub),
783 ca ? " (CA key)" : "");
784 printhost(out, name, pub,
785 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100786 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100787 if (delete_host && !c && !ca)
788 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100789 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100790 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100791 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100792 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100793 if (ca) {
794 fprintf(stderr, "Warning: "
795 "ignoring CA key for host: "
796 "%.64s\n", cp2);
797 printhost(out, cp2, pub, ca, 0);
798 } else if (strcspn(cp2, "*?!") !=
799 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100800 fprintf(stderr, "Warning: "
801 "ignoring host name with "
802 "metacharacters: %.64s\n",
803 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100804 printhost(out, cp2, pub, ca, 0);
805 } else
806 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +1100807 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100808 has_unhashed = 1;
809 }
810 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100811 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100812 }
813 fclose(in);
814
815 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100816 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100817 identity_file);
818 if (inplace) {
819 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100820 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100821 fclose(out);
822 unlink(tmp);
823 }
824 exit(1);
825 }
826
827 if (inplace) {
828 fclose(out);
829
830 /* Backup existing file */
831 if (unlink(old) == -1 && errno != ENOENT)
832 fatal("unlink %.100s: %s", old, strerror(errno));
833 if (link(identity_file, old) == -1)
834 fatal("link %.100s to %.100s: %s", identity_file, old,
835 strerror(errno));
836 /* Move new one into place */
837 if (rename(tmp, identity_file) == -1) {
838 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
839 strerror(errno));
840 unlink(tmp);
841 unlink(old);
842 exit(1);
843 }
844
845 fprintf(stderr, "%s updated.\n", identity_file);
846 fprintf(stderr, "Original contents retained as %s\n", old);
847 if (has_unhashed) {
848 fprintf(stderr, "WARNING: %s contains unhashed "
849 "entries\n", old);
850 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000851 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100852 }
853 }
854
855 exit(0);
856}
857
Damien Miller95def091999-11-25 00:26:21 +1100858/*
859 * Perform changing a passphrase. The argument is the passwd structure
860 * for the current user.
861 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000862static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100863do_change_passphrase(struct passwd *pw)
864{
Damien Miller95def091999-11-25 00:26:21 +1100865 char *comment;
866 char *old_passphrase, *passphrase1, *passphrase2;
867 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000868 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100869
Damien Miller95def091999-11-25 00:26:21 +1100870 if (!have_identity)
871 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100872 if (stat(identity_file, &st) < 0) {
873 perror(identity_file);
874 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000875 }
Damien Miller95def091999-11-25 00:26:21 +1100876 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000877 private = key_load_private(identity_file, "", &comment);
878 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100879 if (identity_passphrase)
880 old_passphrase = xstrdup(identity_passphrase);
881 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000882 old_passphrase =
883 read_passphrase("Enter old passphrase: ",
884 RP_ALLOW_STDIN);
885 private = key_load_private(identity_file, old_passphrase,
886 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000887 memset(old_passphrase, 0, strlen(old_passphrase));
888 xfree(old_passphrase);
889 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100890 printf("Bad passphrase.\n");
891 exit(1);
892 }
Damien Miller95def091999-11-25 00:26:21 +1100893 }
894 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000895
Damien Miller95def091999-11-25 00:26:21 +1100896 /* Ask the new passphrase (twice). */
897 if (identity_new_passphrase) {
898 passphrase1 = xstrdup(identity_new_passphrase);
899 passphrase2 = NULL;
900 } else {
901 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000902 read_passphrase("Enter new passphrase (empty for no "
903 "passphrase): ", RP_ALLOW_STDIN);
904 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100905 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100906
907 /* Verify that they are the same. */
908 if (strcmp(passphrase1, passphrase2) != 0) {
909 memset(passphrase1, 0, strlen(passphrase1));
910 memset(passphrase2, 0, strlen(passphrase2));
911 xfree(passphrase1);
912 xfree(passphrase2);
913 printf("Pass phrases do not match. Try again.\n");
914 exit(1);
915 }
916 /* Destroy the other copy. */
917 memset(passphrase2, 0, strlen(passphrase2));
918 xfree(passphrase2);
919 }
920
921 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000922 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000923 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100924 memset(passphrase1, 0, strlen(passphrase1));
925 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000926 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100927 xfree(comment);
928 exit(1);
929 }
930 /* Destroy the passphrase and the copy of the key in memory. */
931 memset(passphrase1, 0, strlen(passphrase1));
932 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000933 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100934 xfree(comment);
935
936 printf("Your identification has been saved with the new passphrase.\n");
937 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000938}
939
Damien Miller37876e92003-05-15 10:19:46 +1000940/*
941 * Print the SSHFP RR.
942 */
Damien Millercb314822006-03-26 13:48:01 +1100943static int
944do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000945{
946 Key *public;
947 char *comment = NULL;
948 struct stat st;
949
Damien Millercb314822006-03-26 13:48:01 +1100950 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000951 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100952 if (stat(fname, &st) < 0) {
953 if (errno == ENOENT)
954 return 0;
955 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000956 exit(1);
957 }
Damien Millercb314822006-03-26 13:48:01 +1100958 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000959 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000960 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000961 key_free(public);
962 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100963 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000964 }
965 if (comment)
966 xfree(comment);
967
Damien Millercb314822006-03-26 13:48:01 +1100968 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000969 exit(1);
970}
Damien Miller37876e92003-05-15 10:19:46 +1000971
Damien Miller95def091999-11-25 00:26:21 +1100972/*
973 * Change the comment of a private key file.
974 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000975static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000976do_change_comment(struct passwd *pw)
977{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000978 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000979 Key *private;
980 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100981 struct stat st;
982 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000983 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000984
Damien Miller95def091999-11-25 00:26:21 +1100985 if (!have_identity)
986 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100987 if (stat(identity_file, &st) < 0) {
988 perror(identity_file);
989 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000990 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000991 private = key_load_private(identity_file, "", &comment);
992 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100993 if (identity_passphrase)
994 passphrase = xstrdup(identity_passphrase);
995 else if (identity_new_passphrase)
996 passphrase = xstrdup(identity_new_passphrase);
997 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000998 passphrase = read_passphrase("Enter passphrase: ",
999 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001000 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001001 private = key_load_private(identity_file, passphrase, &comment);
1002 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001003 memset(passphrase, 0, strlen(passphrase));
1004 xfree(passphrase);
1005 printf("Bad passphrase.\n");
1006 exit(1);
1007 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001008 } else {
1009 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001010 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001011 if (private->type != KEY_RSA1) {
1012 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1013 key_free(private);
1014 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001015 }
Damien Miller95def091999-11-25 00:26:21 +11001016 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001017
Damien Miller95def091999-11-25 00:26:21 +11001018 if (identity_comment) {
1019 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1020 } else {
1021 printf("Enter new comment: ");
1022 fflush(stdout);
1023 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1024 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001025 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001026 exit(1);
1027 }
Damien Miller14b017d2007-09-17 16:09:15 +10001028 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001029 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001030
Damien Miller95def091999-11-25 00:26:21 +11001031 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001032 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001033 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001034 memset(passphrase, 0, strlen(passphrase));
1035 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001036 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001037 xfree(comment);
1038 exit(1);
1039 }
Damien Miller95def091999-11-25 00:26:21 +11001040 memset(passphrase, 0, strlen(passphrase));
1041 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001042 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001043 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001044
Damien Miller95def091999-11-25 00:26:21 +11001045 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001046 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1047 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001048 printf("Could not save your public key in %s\n", identity_file);
1049 exit(1);
1050 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001051 f = fdopen(fd, "w");
1052 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001053 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001054 exit(1);
1055 }
Damien Millereba71ba2000-04-29 23:57:08 +10001056 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001057 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001058 key_free(public);
1059 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001060 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001061
Damien Miller95def091999-11-25 00:26:21 +11001062 xfree(comment);
1063
1064 printf("The comment in your key file has been changed.\n");
1065 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001066}
1067
Damien Miller0a80ca12010-02-27 07:55:05 +11001068static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001069fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001070{
1071 char from[32], to[32];
1072 static char ret[64];
1073 time_t tt;
1074 struct tm *tm;
1075
1076 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001077 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001078 return "forever";
1079
Damien Millerf2b70ca2010-03-05 07:39:35 +11001080 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001081 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001082 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001083 tm = localtime(&tt);
1084 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1085 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001086 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001087 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001088 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001089 tm = localtime(&tt);
1090 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1091 }
1092
Damien Millerf2b70ca2010-03-05 07:39:35 +11001093 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001094 snprintf(ret, sizeof(ret), "before %s", to);
1095 return ret;
1096 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001097 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001098 snprintf(ret, sizeof(ret), "after %s", from);
1099 return ret;
1100 }
1101
1102 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1103 return ret;
1104}
1105
1106static void
1107add_flag_constraint(Buffer *c, const char *name)
1108{
1109 debug3("%s: %s", __func__, name);
1110 buffer_put_cstring(c, name);
1111 buffer_put_string(c, NULL, 0);
1112}
1113
1114static void
1115add_string_constraint(Buffer *c, const char *name, const char *value)
1116{
1117 Buffer b;
1118
1119 debug3("%s: %s=%s", __func__, name, value);
1120 buffer_init(&b);
1121 buffer_put_cstring(&b, value);
1122
1123 buffer_put_cstring(c, name);
1124 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1125
1126 buffer_free(&b);
1127}
1128
1129static void
1130prepare_constraint_buf(Buffer *c)
1131{
1132
1133 buffer_clear(c);
1134 if ((constraint_flags & CONSTRAINT_X_FWD) != 0)
1135 add_flag_constraint(c, "permit-X11-forwarding");
1136 if ((constraint_flags & CONSTRAINT_AGENT_FWD) != 0)
1137 add_flag_constraint(c, "permit-agent-forwarding");
1138 if ((constraint_flags & CONSTRAINT_PORT_FWD) != 0)
1139 add_flag_constraint(c, "permit-port-forwarding");
1140 if ((constraint_flags & CONSTRAINT_PTY) != 0)
1141 add_flag_constraint(c, "permit-pty");
1142 if ((constraint_flags & CONSTRAINT_USER_RC) != 0)
1143 add_flag_constraint(c, "permit-user-rc");
1144 if (constraint_command != NULL)
1145 add_string_constraint(c, "forced-command", constraint_command);
1146 if (constraint_src_addr != NULL)
1147 add_string_constraint(c, "source-address", constraint_src_addr);
1148}
1149
1150static void
1151do_ca_sign(struct passwd *pw, int argc, char **argv)
1152{
1153 int i, fd;
1154 u_int n;
1155 Key *ca, *public;
1156 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1157 FILE *f;
1158
1159 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1160 if ((ca = load_identity(tmp)) == NULL)
1161 fatal("Couldn't load CA key \"%s\"", tmp);
1162 xfree(tmp);
1163
1164 for (i = 0; i < argc; i++) {
1165 /* Split list of principals */
1166 n = 0;
1167 if (cert_principals != NULL) {
1168 otmp = tmp = xstrdup(cert_principals);
1169 plist = NULL;
1170 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1171 plist = xrealloc(plist, n + 1, sizeof(*plist));
1172 if (*(plist[n] = xstrdup(cp)) == '\0')
1173 fatal("Empty principal name");
1174 }
1175 xfree(otmp);
1176 }
1177
1178 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1179 if ((public = key_load_public(tmp, &comment)) == NULL)
1180 fatal("%s: unable to open \"%s\"", __func__, tmp);
1181 if (public->type != KEY_RSA && public->type != KEY_DSA)
1182 fatal("%s: key \"%s\" type %s cannot be certified",
1183 __func__, tmp, key_type(public));
1184
1185 /* Prepare certificate to sign */
1186 if (key_to_certified(public) != 0)
1187 fatal("Could not upgrade key %s to certificate", tmp);
1188 public->cert->type = cert_key_type;
1189 public->cert->key_id = xstrdup(cert_key_id);
1190 public->cert->nprincipals = n;
1191 public->cert->principals = plist;
1192 public->cert->valid_after = cert_valid_from;
1193 public->cert->valid_before = cert_valid_to;
1194 prepare_constraint_buf(&public->cert->constraints);
1195 public->cert->signature_key = key_from_private(ca);
1196
1197 if (key_certify(public, ca) != 0)
1198 fatal("Couldn't not certify key %s", tmp);
1199
1200 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1201 *cp = '\0';
1202 xasprintf(&out, "%s-cert.pub", tmp);
1203 xfree(tmp);
1204
1205 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1206 fatal("Could not open \"%s\" for writing: %s", out,
1207 strerror(errno));
1208 if ((f = fdopen(fd, "w")) == NULL)
1209 fatal("%s: fdopen: %s", __func__, strerror(errno));
1210 if (!key_write(public, f))
1211 fatal("Could not write certified key to %s", out);
1212 fprintf(f, " %s\n", comment);
1213 fclose(f);
1214
1215 if (!quiet)
1216 logit("Signed %s key %s: id \"%s\"%s%s valid %s",
1217 cert_key_type == SSH2_CERT_TYPE_USER?"user":"host",
1218 out, cert_key_id,
1219 cert_principals != NULL ? " for " : "",
1220 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001221 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller0a80ca12010-02-27 07:55:05 +11001222
1223 key_free(public);
1224 xfree(out);
1225 }
1226 exit(0);
1227}
1228
1229static u_int64_t
1230parse_relative_time(const char *s, time_t now)
1231{
1232 int64_t mul, secs;
1233
1234 mul = *s == '-' ? -1 : 1;
1235
1236 if ((secs = convtime(s + 1)) == -1)
1237 fatal("Invalid relative certificate time %s", s);
1238 if (mul == -1 && secs > now)
1239 fatal("Certificate time %s cannot be represented", s);
1240 return now + (u_int64_t)(secs * mul);
1241}
1242
1243static u_int64_t
1244parse_absolute_time(const char *s)
1245{
1246 struct tm tm;
1247 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001248 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001249
Damien Miller2ca342b2010-03-03 12:14:15 +11001250 /*
1251 * POSIX strptime says "The application shall ensure that there
1252 * is white-space or other non-alphanumeric characters between
1253 * any two conversion specifications" so arrange things this way.
1254 */
1255 switch (strlen(s)) {
1256 case 8:
1257 fmt = "%Y/%m/%d";
1258 snprintf(buf, sizeof(buf), "%.4s/%.2s/%.2s", s, s + 4, s + 6);
1259 break;
1260 case 14:
1261 fmt = "%Y/%m/%d %H:%M:%S";
1262 snprintf(buf, sizeof(buf), "%.4s/%.2s/%.2s %.2s:%.2s:%.2s",
1263 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1264 break;
1265 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001266 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001267 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001268
1269 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001270 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001271 fatal("Invalid certificate time %s", s);
1272 if ((tt = mktime(&tm)) < 0)
1273 fatal("Certificate time %s cannot be represented", s);
1274 return (u_int64_t)tt;
1275}
1276
1277static void
1278parse_cert_times(char *timespec)
1279{
1280 char *from, *to;
1281 time_t now = time(NULL);
1282 int64_t secs;
1283
1284 /* +timespec relative to now */
1285 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1286 if ((secs = convtime(timespec + 1)) == -1)
1287 fatal("Invalid relative certificate life %s", timespec);
1288 cert_valid_to = now + secs;
1289 /*
1290 * Backdate certificate one minute to avoid problems on hosts
1291 * with poorly-synchronised clocks.
1292 */
1293 cert_valid_from = ((now - 59)/ 60) * 60;
1294 return;
1295 }
1296
1297 /*
1298 * from:to, where
1299 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1300 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1301 */
1302 from = xstrdup(timespec);
1303 to = strchr(from, ':');
1304 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001305 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001306 *to++ = '\0';
1307
1308 if (*from == '-' || *from == '+')
1309 cert_valid_from = parse_relative_time(from, now);
1310 else
1311 cert_valid_from = parse_absolute_time(from);
1312
1313 if (*to == '-' || *to == '+')
1314 cert_valid_to = parse_relative_time(to, cert_valid_from);
1315 else
1316 cert_valid_to = parse_absolute_time(to);
1317
1318 if (cert_valid_to <= cert_valid_from)
1319 fatal("Empty certificate validity interval");
1320 xfree(from);
1321}
1322
1323static void
1324add_cert_constraint(char *opt)
1325{
1326 char *val;
1327
1328 if (strcmp(opt, "clear") == 0)
1329 constraint_flags = 0;
1330 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
1331 constraint_flags &= ~CONSTRAINT_X_FWD;
1332 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
1333 constraint_flags |= CONSTRAINT_X_FWD;
1334 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
1335 constraint_flags &= ~CONSTRAINT_AGENT_FWD;
1336 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
1337 constraint_flags |= CONSTRAINT_AGENT_FWD;
1338 else if (strcasecmp(opt, "no-port-forwarding") == 0)
1339 constraint_flags &= ~CONSTRAINT_PORT_FWD;
1340 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
1341 constraint_flags |= CONSTRAINT_PORT_FWD;
1342 else if (strcasecmp(opt, "no-pty") == 0)
1343 constraint_flags &= ~CONSTRAINT_PTY;
1344 else if (strcasecmp(opt, "permit-pty") == 0)
1345 constraint_flags |= CONSTRAINT_PTY;
1346 else if (strcasecmp(opt, "no-user-rc") == 0)
1347 constraint_flags &= ~CONSTRAINT_USER_RC;
1348 else if (strcasecmp(opt, "permit-user-rc") == 0)
1349 constraint_flags |= CONSTRAINT_USER_RC;
1350 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1351 val = opt + 14;
1352 if (*val == '\0')
1353 fatal("Empty force-command constraint");
1354 if (constraint_command != NULL)
1355 fatal("force-command already specified");
1356 constraint_command = xstrdup(val);
1357 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1358 val = opt + 15;
1359 if (*val == '\0')
1360 fatal("Empty source-address constraint");
1361 if (constraint_src_addr != NULL)
1362 fatal("source-address already specified");
1363 if (addr_match_cidr_list(NULL, val) != 0)
1364 fatal("Invalid source-address list");
1365 constraint_src_addr = xstrdup(val);
1366 } else
1367 fatal("Unsupported certificate constraint \"%s\"", opt);
1368}
1369
Ben Lindstrombba81212001-06-25 05:01:22 +00001370static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001371do_show_cert(struct passwd *pw)
1372{
1373 Key *key;
1374 struct stat st;
1375 char *key_fp, *ca_fp;
1376 Buffer constraints, constraint;
1377 u_char *name, *data;
1378 u_int i, dlen;
1379
1380 if (!have_identity)
1381 ask_filename(pw, "Enter file in which the key is");
1382 if (stat(identity_file, &st) < 0) {
1383 perror(identity_file);
1384 exit(1);
1385 }
1386 if ((key = key_load_public(identity_file, NULL)) == NULL)
1387 fatal("%s is not a public key", identity_file);
1388 if (!key_is_cert(key))
1389 fatal("%s is not a certificate", identity_file);
1390
1391 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1392 ca_fp = key_fingerprint(key->cert->signature_key,
1393 SSH_FP_MD5, SSH_FP_HEX);
1394
1395 printf("%s:\n", identity_file);
1396 printf(" %s certificate %s\n", key_type(key), key_fp);
1397 printf(" Signed by %s CA %s\n",
1398 key_type(key->cert->signature_key), ca_fp);
1399 printf(" Key ID \"%s\"\n", key->cert->key_id);
1400 printf(" Valid: %s\n",
1401 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1402 printf(" Principals: ");
1403 if (key->cert->nprincipals == 0)
1404 printf("(none)\n");
1405 else {
1406 for (i = 0; i < key->cert->nprincipals; i++)
1407 printf("\n %s",
1408 key->cert->principals[i]);
1409 printf("\n");
1410 }
1411 printf(" Constraints: ");
1412 if (buffer_len(&key->cert->constraints) == 0)
1413 printf("(none)\n");
1414 else {
1415 printf("\n");
1416 buffer_init(&constraints);
1417 buffer_append(&constraints,
1418 buffer_ptr(&key->cert->constraints),
1419 buffer_len(&key->cert->constraints));
1420 buffer_init(&constraint);
1421 while (buffer_len(&constraints) != 0) {
1422 name = buffer_get_string(&constraints, NULL);
1423 data = buffer_get_string_ptr(&constraints, &dlen);
1424 buffer_append(&constraint, data, dlen);
1425 printf(" %s", name);
1426 if (strcmp(name, "permit-X11-forwarding") == 0 ||
1427 strcmp(name, "permit-agent-forwarding") == 0 ||
1428 strcmp(name, "permit-port-forwarding") == 0 ||
1429 strcmp(name, "permit-pty") == 0 ||
1430 strcmp(name, "permit-user-rc") == 0)
1431 printf("\n");
1432 else if (strcmp(name, "force-command") == 0 ||
1433 strcmp(name, "source-address") == 0) {
1434 data = buffer_get_string(&constraint, NULL);
1435 printf(" %s\n", data);
1436 xfree(data);
1437 } else {
1438 printf(" UNKNOWN CONSTRAINT (len %u)\n",
1439 buffer_len(&constraint));
1440 buffer_clear(&constraint);
1441 }
1442 xfree(name);
1443 if (buffer_len(&constraint) != 0)
1444 fatal("Constraint corrupt: extra data at end");
1445 }
1446 buffer_free(&constraint);
1447 buffer_free(&constraints);
1448 }
1449
1450 exit(0);
1451}
1452
1453static void
Damien Miller431f66b1999-11-21 18:31:57 +11001454usage(void)
1455{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001456 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001457 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001458 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001459 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001460 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001461 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001462 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001463#ifdef ENABLE_PKCS11
1464 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1465#endif
Darren Tucker26dc3e62007-02-19 22:09:06 +11001466 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001467 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1468 fprintf(stderr, " -f filename Filename of the key file.\n");
1469 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1470 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1471 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001472 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1473 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Darren Tucker26dc3e62007-02-19 22:09:06 +11001474 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001475 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001476 fprintf(stderr, " -l Show fingerprint of key file.\n");
1477 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001478 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001479 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001480 fprintf(stderr, " -O cnstr Specify a certificate constraint.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001481 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1482 fprintf(stderr, " -p Change passphrase of private key file.\n");
1483 fprintf(stderr, " -q Quiet.\n");
1484 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1485 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001486 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001487 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1488 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1489 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001490 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001491 fprintf(stderr, " -v Verbose.\n");
1492 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1493 fprintf(stderr, " -y Read private key file and print public key.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001494
Damien Miller95def091999-11-25 00:26:21 +11001495 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001496}
1497
Damien Miller95def091999-11-25 00:26:21 +11001498/*
1499 * Main program for key management.
1500 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001501int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001502main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001503{
Damien Millera41c8b12002-01-22 23:05:08 +11001504 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller7ea845e2010-02-12 09:21:02 +11001505 char out_file[MAXPATHLEN], *pkcs11provider = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001506 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001507 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001508 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001509 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001510 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001511 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001512 int do_gen_candidates = 0, do_screen_candidates = 0;
1513 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001514 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001515 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001516
Damien Miller95def091999-11-25 00:26:21 +11001517 extern int optind;
1518 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001519
Darren Tuckerce321d82005-10-03 18:11:24 +10001520 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1521 sanitise_stdfd();
1522
Darren Tucker9ac56e92007-01-14 10:19:59 +11001523 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001524
Damien Millerd3a18572000-06-07 19:55:44 +10001525 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001526 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001527
Kevin Steves3a881912002-07-20 19:05:40 +00001528 init_rng();
1529 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001530
Damien Miller5428f641999-11-25 11:54:57 +11001531 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001532 pw = getpwuid(getuid());
1533 if (!pw) {
1534 printf("You don't exist, go away!\n");
1535 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001536 }
Damien Millereba71ba2000-04-29 23:57:08 +10001537 if (gethostname(hostname, sizeof(hostname)) < 0) {
1538 perror("gethostname");
1539 exit(1);
1540 }
Damien Miller5428f641999-11-25 11:54:57 +11001541
Damien Millerf2b70ca2010-03-05 07:39:35 +11001542 while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:N:n:"
Damien Miller0a80ca12010-02-27 07:55:05 +11001543 "O:C:r:g:R:T:G:M:S:s:a:V:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001544 switch (opt) {
1545 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001546 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001547 if (errstr)
1548 fatal("Bits has bad value %s (%s)",
1549 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001550 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001551 case 'F':
1552 find_host = 1;
1553 rr_hostname = optarg;
1554 break;
1555 case 'H':
1556 hash_hosts = 1;
1557 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001558 case 'I':
1559 cert_key_id = optarg;
1560 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001561 case 'R':
1562 delete_host = 1;
1563 rr_hostname = optarg;
1564 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001565 case 'L':
1566 show_cert = 1;
1567 break;
Damien Miller95def091999-11-25 00:26:21 +11001568 case 'l':
1569 print_fingerprint = 1;
1570 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001571 case 'B':
1572 print_bubblebabble = 1;
1573 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001574 case 'n':
1575 cert_principals = optarg;
1576 break;
Damien Miller95def091999-11-25 00:26:21 +11001577 case 'p':
1578 change_passphrase = 1;
1579 break;
Damien Miller95def091999-11-25 00:26:21 +11001580 case 'c':
1581 change_comment = 1;
1582 break;
Damien Miller95def091999-11-25 00:26:21 +11001583 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001584 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1585 sizeof(identity_file))
1586 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001587 have_identity = 1;
1588 break;
Damien Miller37876e92003-05-15 10:19:46 +10001589 case 'g':
1590 print_generic = 1;
1591 break;
Damien Miller95def091999-11-25 00:26:21 +11001592 case 'P':
1593 identity_passphrase = optarg;
1594 break;
Damien Miller95def091999-11-25 00:26:21 +11001595 case 'N':
1596 identity_new_passphrase = optarg;
1597 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001598 case 'O':
1599 add_cert_constraint(optarg);
1600 break;
Damien Miller95def091999-11-25 00:26:21 +11001601 case 'C':
1602 identity_comment = optarg;
1603 break;
Damien Miller95def091999-11-25 00:26:21 +11001604 case 'q':
1605 quiet = 1;
1606 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001607 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001608 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001609 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001610 convert_to_ssh2 = 1;
1611 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001612 case 'h':
1613 cert_key_type = SSH2_CERT_TYPE_HOST;
1614 constraint_flags = 0;
1615 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001616 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001617 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001618 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001619 convert_from_ssh2 = 1;
1620 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001621 case 'y':
1622 print_public = 1;
1623 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001624 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001625 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001626 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001627 case 's':
1628 ca_key_path = optarg;
1629 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001630 case 't':
1631 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001632 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001633 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001634 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001635 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001636 case 'v':
1637 if (log_level == SYSLOG_LEVEL_INFO)
1638 log_level = SYSLOG_LEVEL_DEBUG1;
1639 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001640 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001641 log_level < SYSLOG_LEVEL_DEBUG3)
1642 log_level++;
1643 }
1644 break;
Damien Miller37876e92003-05-15 10:19:46 +10001645 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001646 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001647 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001648 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001649 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1650 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001651 if (errstr)
1652 fatal("Desired generator has bad value: %s (%s)",
1653 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001654 break;
1655 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001656 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001657 if (errstr)
1658 fatal("Invalid number of trials: %s (%s)",
1659 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001660 break;
1661 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001662 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001663 if (errstr) {
1664 fatal("Memory limit is %s: %s", errstr, optarg);
1665 }
Darren Tucker019cefe2003-08-02 22:40:07 +10001666 break;
1667 case 'G':
1668 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001669 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1670 sizeof(out_file))
1671 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001672 break;
1673 case 'T':
1674 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001675 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1676 sizeof(out_file))
1677 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001678 break;
1679 case 'S':
1680 /* XXX - also compare length against bits */
1681 if (BN_hex2bn(&start, optarg) == 0)
1682 fatal("Invalid start point.");
1683 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001684 case 'V':
1685 parse_cert_times(optarg);
1686 break;
Damien Miller95def091999-11-25 00:26:21 +11001687 case '?':
1688 default:
1689 usage();
1690 }
1691 }
Darren Tucker06930c72003-12-31 11:34:51 +11001692
1693 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001694 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001695
Damien Miller0a80ca12010-02-27 07:55:05 +11001696 argv += optind;
1697 argc -= optind;
1698
1699 if (ca_key_path != NULL) {
1700 if (argc < 1) {
1701 printf("Too few arguments.\n");
1702 usage();
1703 }
1704 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11001705 printf("Too many arguments.\n");
1706 usage();
1707 }
1708 if (change_passphrase && change_comment) {
1709 printf("Can only have one of -p and -c.\n");
1710 usage();
1711 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001712 if (print_fingerprint && (delete_host || hash_hosts)) {
1713 printf("Cannot use -l with -D or -R.\n");
1714 usage();
1715 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001716 if (ca_key_path != NULL) {
1717 if (cert_key_id == NULL)
1718 fatal("Must specify key id (-I) when certifying");
1719 do_ca_sign(pw, argc, argv);
1720 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001721 if (show_cert)
1722 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001723 if (delete_host || hash_hosts || find_host)
1724 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001725 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001726 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001727 if (change_passphrase)
1728 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001729 if (change_comment)
1730 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001731 if (convert_to_ssh2)
1732 do_convert_to_ssh2(pw);
1733 if (convert_from_ssh2)
1734 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001735 if (print_public)
1736 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001737 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001738 unsigned int n = 0;
1739
1740 if (have_identity) {
1741 n = do_print_resource_record(pw,
1742 identity_file, rr_hostname);
1743 if (n == 0) {
1744 perror(identity_file);
1745 exit(1);
1746 }
1747 exit(0);
1748 } else {
1749
1750 n += do_print_resource_record(pw,
1751 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1752 n += do_print_resource_record(pw,
1753 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1754
1755 if (n == 0)
1756 fatal("no keys found.");
1757 exit(0);
1758 }
Damien Miller37876e92003-05-15 10:19:46 +10001759 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001760 if (pkcs11provider != NULL)
1761 do_download(pw, pkcs11provider);
Damien Miller95def091999-11-25 00:26:21 +11001762
Darren Tucker019cefe2003-08-02 22:40:07 +10001763 if (do_gen_candidates) {
1764 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001765
Darren Tucker019cefe2003-08-02 22:40:07 +10001766 if (out == NULL) {
1767 error("Couldn't open modulus candidate file \"%s\": %s",
1768 out_file, strerror(errno));
1769 return (1);
1770 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001771 if (bits == 0)
1772 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001773 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001774 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001775
1776 return (0);
1777 }
1778
1779 if (do_screen_candidates) {
1780 FILE *in;
1781 FILE *out = fopen(out_file, "w");
1782
1783 if (have_identity && strcmp(identity_file, "-") != 0) {
1784 if ((in = fopen(identity_file, "r")) == NULL) {
1785 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001786 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001787 strerror(errno));
1788 }
1789 } else
1790 in = stdin;
1791
1792 if (out == NULL) {
1793 fatal("Couldn't open moduli file \"%s\": %s",
1794 out_file, strerror(errno));
1795 }
1796 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001797 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001798 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001799 }
1800
Damien Miller95def091999-11-25 00:26:21 +11001801 arc4random_stir();
1802
Damien Millerf14be5c2005-11-05 15:15:49 +11001803 if (key_type_name == NULL)
1804 key_type_name = "rsa";
1805
Damien Millere39cacc2000-11-29 12:18:44 +11001806 type = key_type_from_name(key_type_name);
1807 if (type == KEY_UNSPEC) {
1808 fprintf(stderr, "unknown key type %s\n", key_type_name);
1809 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001810 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001811 if (bits == 0)
1812 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Tim Rice660c3402005-11-28 17:45:32 -08001813 if (type == KEY_DSA && bits != 1024)
1814 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001815 if (!quiet)
1816 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001817 private = key_generate(type, bits);
1818 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001819 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001820 exit(1);
1821 }
1822 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001823
1824 if (!have_identity)
1825 ask_filename(pw, "Enter file in which to save the key");
1826
Damien Miller788f2122005-11-05 15:14:59 +11001827 /* Create ~/.ssh directory if it doesn't already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001828 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001829 if (strstr(identity_file, dotsshdir) != NULL &&
1830 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001831 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001832 error("Could not create directory '%s'.", dotsshdir);
1833 else if (!quiet)
1834 printf("Created directory '%s'.\n", dotsshdir);
1835 }
1836 /* If the file already exists, ask the user to confirm. */
1837 if (stat(identity_file, &st) >= 0) {
1838 char yesno[3];
1839 printf("%s already exists.\n", identity_file);
1840 printf("Overwrite (y/n)? ");
1841 fflush(stdout);
1842 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1843 exit(1);
1844 if (yesno[0] != 'y' && yesno[0] != 'Y')
1845 exit(1);
1846 }
1847 /* Ask for a passphrase (twice). */
1848 if (identity_passphrase)
1849 passphrase1 = xstrdup(identity_passphrase);
1850 else if (identity_new_passphrase)
1851 passphrase1 = xstrdup(identity_new_passphrase);
1852 else {
1853passphrase_again:
1854 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001855 read_passphrase("Enter passphrase (empty for no "
1856 "passphrase): ", RP_ALLOW_STDIN);
1857 passphrase2 = read_passphrase("Enter same passphrase again: ",
1858 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001859 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001860 /*
1861 * The passphrases do not match. Clear them and
1862 * retry.
1863 */
Damien Miller95def091999-11-25 00:26:21 +11001864 memset(passphrase1, 0, strlen(passphrase1));
1865 memset(passphrase2, 0, strlen(passphrase2));
1866 xfree(passphrase1);
1867 xfree(passphrase2);
1868 printf("Passphrases do not match. Try again.\n");
1869 goto passphrase_again;
1870 }
1871 /* Clear the other copy of the passphrase. */
1872 memset(passphrase2, 0, strlen(passphrase2));
1873 xfree(passphrase2);
1874 }
1875
Damien Miller95def091999-11-25 00:26:21 +11001876 if (identity_comment) {
1877 strlcpy(comment, identity_comment, sizeof(comment));
1878 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11001879 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001880 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1881 }
1882
1883 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001884 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001885 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001886 memset(passphrase1, 0, strlen(passphrase1));
1887 xfree(passphrase1);
1888 exit(1);
1889 }
1890 /* Clear the passphrase. */
1891 memset(passphrase1, 0, strlen(passphrase1));
1892 xfree(passphrase1);
1893
1894 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001895 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001896 arc4random_stir();
1897
1898 if (!quiet)
1899 printf("Your identification has been saved in %s.\n", identity_file);
1900
Damien Miller95def091999-11-25 00:26:21 +11001901 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001902 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1903 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001904 printf("Could not save your public key in %s\n", identity_file);
1905 exit(1);
1906 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001907 f = fdopen(fd, "w");
1908 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001909 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001910 exit(1);
1911 }
Damien Millereba71ba2000-04-29 23:57:08 +10001912 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001913 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001914 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001915 fclose(f);
1916
1917 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001918 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001919 char *ra = key_fingerprint(public, SSH_FP_MD5,
1920 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10001921 printf("Your public key has been saved in %s.\n",
1922 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001923 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001924 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001925 printf("The key's randomart image is:\n");
1926 printf("%s\n", ra);
1927 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00001928 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001929 }
Damien Millereba71ba2000-04-29 23:57:08 +10001930
1931 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001932 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001933}