blob: 8938dc051bae0c1381bbb713c684646bea69af21 [file] [log] [blame]
Damien Miller4e270b02010-04-16 15:56:21 +10001/* $OpenBSD: ssh-keygen.c,v 1.186 2010/04/16 01:47:26 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110051#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110052
Damien Miller7ea845e2010-02-12 09:21:02 +110053#ifdef ENABLE_PKCS11
54#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000055#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000056
Damien Miller3f54a9f2005-11-05 14:52:18 +110057/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
58#define DEFAULT_BITS 2048
59#define DEFAULT_BITS_DSA 1024
60u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Flag indicating that we just want to change the passphrase. This can be
64 * set on the command line.
65 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100066int change_passphrase = 0;
67
Damien Miller5428f641999-11-25 11:54:57 +110068/*
69 * Flag indicating that we just want to change the comment. This can be set
70 * on the command line.
71 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072int change_comment = 0;
73
74int quiet = 0;
75
Darren Tucker35c45532008-06-13 04:43:15 +100076int log_level = SYSLOG_LEVEL_INFO;
77
Damien Miller4b42d7f2005-03-01 21:48:35 +110078/* Flag indicating that we want to hash a known_hosts file */
79int hash_hosts = 0;
80/* Flag indicating that we want lookup a host in known_hosts file */
81int find_host = 0;
82/* Flag indicating that we want to delete a host from a known_hosts file */
83int delete_host = 0;
84
Damien Millerf2b70ca2010-03-05 07:39:35 +110085/* Flag indicating that we want to show the contents of a certificate */
86int show_cert = 0;
87
Damien Miller10f6f6b1999-11-17 17:29:08 +110088/* Flag indicating that we just want to see the key fingerprint */
89int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000090int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110091
Damien Miller431f66b1999-11-21 18:31:57 +110092/* The identity file name, given on the command line or entered by the user. */
93char identity_file[1024];
94int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100095
96/* This is set to the passphrase if given on the command line. */
97char *identity_passphrase = NULL;
98
99/* This is set to the new passphrase if given on the command line. */
100char *identity_new_passphrase = NULL;
101
102/* This is set to the new comment if given on the command line. */
103char *identity_comment = NULL;
104
Damien Miller0a80ca12010-02-27 07:55:05 +1100105/* Path to CA key when certifying keys. */
106char *ca_key_path = NULL;
107
Damien Miller4e270b02010-04-16 15:56:21 +1000108/* Certificate serial number */
109long long cert_serial = 0;
110
Damien Miller0a80ca12010-02-27 07:55:05 +1100111/* Key type when certifying */
112u_int cert_key_type = SSH2_CERT_TYPE_USER;
113
114/* "key ID" of signed key */
115char *cert_key_id = NULL;
116
117/* Comma-separated list of principal names for certifying keys */
118char *cert_principals = NULL;
119
120/* Validity period for certificates */
121u_int64_t cert_valid_from = 0;
122u_int64_t cert_valid_to = ~0ULL;
123
Damien Miller4e270b02010-04-16 15:56:21 +1000124/* Certificate options */
125#define CRITOPT_X_FWD (1)
126#define CRITOPT_AGENT_FWD (1<<1)
127#define CRITOPT_PORT_FWD (1<<2)
128#define CRITOPT_PTY (1<<3)
129#define CRITOPT_USER_RC (1<<4)
130#define CRITOPT_DEFAULT (CRITOPT_X_FWD|CRITOPT_AGENT_FWD| \
131 CRITOPT_PORT_FWD|CRITOPT_PTY| \
132 CRITOPT_USER_RC)
133u_int32_t critical_flags = CRITOPT_DEFAULT;
134char *critical_command = NULL;
135char *critical_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100136
Damien Millereba71ba2000-04-29 23:57:08 +1000137/* Dump public key file in format used by real and the original SSH 2 */
138int convert_to_ssh2 = 0;
139int convert_from_ssh2 = 0;
140int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000141int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100142
Damien Millera41c8b12002-01-22 23:05:08 +1100143char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000144
Damien Miller431f66b1999-11-21 18:31:57 +1100145/* argv0 */
146extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000147
Damien Millereba71ba2000-04-29 23:57:08 +1000148char hostname[MAXHOSTNAMELEN];
149
Darren Tucker770fc012004-05-13 16:24:32 +1000150/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000151int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000152int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
153
Ben Lindstrombba81212001-06-25 05:01:22 +0000154static void
Damien Miller431f66b1999-11-21 18:31:57 +1100155ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156{
Damien Miller95def091999-11-25 00:26:21 +1100157 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100158 char *name = NULL;
159
Damien Miller993dd552002-02-19 15:22:47 +1100160 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000161 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100162 else {
Damien Miller993dd552002-02-19 15:22:47 +1100163 switch (key_type_from_name(key_type_name)) {
164 case KEY_RSA1:
165 name = _PATH_SSH_CLIENT_IDENTITY;
166 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000167 case KEY_DSA_CERT:
168 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100169 case KEY_DSA:
170 name = _PATH_SSH_CLIENT_ID_DSA;
171 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000172 case KEY_RSA_CERT:
173 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100174 case KEY_RSA:
175 name = _PATH_SSH_CLIENT_ID_RSA;
176 break;
177 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100178 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100179 exit(1);
180 break;
181 }
Damien Miller90967402006-03-26 14:07:26 +1100182 }
Damien Millere39cacc2000-11-29 12:18:44 +1100183 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000184 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100185 if (fgets(buf, sizeof(buf), stdin) == NULL)
186 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000187 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100188 if (strcmp(buf, "") != 0)
189 strlcpy(identity_file, buf, sizeof(identity_file));
190 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100191}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192
Ben Lindstrombba81212001-06-25 05:01:22 +0000193static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000194load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000195{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000196 char *pass;
197 Key *prv;
198
Ben Lindstroma3700052001-04-05 23:26:32 +0000199 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000200 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000201 if (identity_passphrase)
202 pass = xstrdup(identity_passphrase);
203 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000204 pass = read_passphrase("Enter passphrase: ",
205 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000206 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000207 memset(pass, 0, strlen(pass));
208 xfree(pass);
209 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000210 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000211}
212
Damien Miller874d77b2000-10-14 16:23:11 +1100213#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000214#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100215#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000216#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000217
Ben Lindstrombba81212001-06-25 05:01:22 +0000218static void
Damien Millereba71ba2000-04-29 23:57:08 +1000219do_convert_to_ssh2(struct passwd *pw)
220{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000221 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000222 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000223 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100224 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000225 struct stat st;
226
227 if (!have_identity)
228 ask_filename(pw, "Enter file in which the key is");
229 if (stat(identity_file, &st) < 0) {
230 perror(identity_file);
231 exit(1);
232 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000233 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000234 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000235 fprintf(stderr, "load failed\n");
236 exit(1);
237 }
Damien Millereba71ba2000-04-29 23:57:08 +1000238 }
Damien Millerdb274722003-05-14 13:45:22 +1000239 if (k->type == KEY_RSA1) {
240 fprintf(stderr, "version 1 keys are not supported\n");
241 exit(1);
242 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000243 if (key_to_blob(k, &blob, &len) <= 0) {
244 fprintf(stderr, "key_to_blob failed\n");
245 exit(1);
246 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100247 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
248 snprintf(comment, sizeof(comment),
249 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000250 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000251 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100252
253 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
254 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000255 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100256 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000257 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000258 xfree(blob);
259 exit(0);
260}
261
Ben Lindstrombba81212001-06-25 05:01:22 +0000262static void
Damien Miller874d77b2000-10-14 16:23:11 +1100263buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
264{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000265 u_int bignum_bits = buffer_get_int(b);
266 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000267
Damien Miller874d77b2000-10-14 16:23:11 +1100268 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000269 fatal("buffer_get_bignum_bits: input buffer too small: "
270 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100271 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
272 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100273 buffer_consume(b, bytes);
274}
275
Ben Lindstrombba81212001-06-25 05:01:22 +0000276static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000277do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100278{
279 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100280 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100281 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000282 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000283 int magic, rlen, ktype, i1, i2, i3, i4;
284 u_int slen;
285 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100286
287 buffer_init(&b);
288 buffer_append(&b, blob, blen);
289
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100290 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100291 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
292 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
293 buffer_free(&b);
294 return NULL;
295 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000296 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100297 type = buffer_get_string(&b, NULL);
298 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000299 i2 = buffer_get_int(&b);
300 i3 = buffer_get_int(&b);
301 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100302 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100303 if (strcmp(cipher, "none") != 0) {
304 error("unsupported cipher %s", cipher);
305 xfree(cipher);
306 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000307 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100308 return NULL;
309 }
310 xfree(cipher);
311
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000312 if (strstr(type, "dsa")) {
313 ktype = KEY_DSA;
314 } else if (strstr(type, "rsa")) {
315 ktype = KEY_RSA;
316 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100317 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000318 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100319 return NULL;
320 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000321 key = key_new_private(ktype);
322 xfree(type);
323
324 switch (key->type) {
325 case KEY_DSA:
326 buffer_get_bignum_bits(&b, key->dsa->p);
327 buffer_get_bignum_bits(&b, key->dsa->g);
328 buffer_get_bignum_bits(&b, key->dsa->q);
329 buffer_get_bignum_bits(&b, key->dsa->pub_key);
330 buffer_get_bignum_bits(&b, key->dsa->priv_key);
331 break;
332 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100333 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000334 debug("e %lx", e);
335 if (e < 30) {
336 e <<= 8;
337 e += buffer_get_char(&b);
338 debug("e %lx", e);
339 e <<= 8;
340 e += buffer_get_char(&b);
341 debug("e %lx", e);
342 }
343 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000344 buffer_free(&b);
345 key_free(key);
346 return NULL;
347 }
348 buffer_get_bignum_bits(&b, key->rsa->d);
349 buffer_get_bignum_bits(&b, key->rsa->n);
350 buffer_get_bignum_bits(&b, key->rsa->iqmp);
351 buffer_get_bignum_bits(&b, key->rsa->q);
352 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000353 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000354 break;
355 }
Damien Miller874d77b2000-10-14 16:23:11 +1100356 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000357 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000358 error("do_convert_private_ssh2_from_blob: "
359 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100360 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000361
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000362 /* try the key */
363 key_sign(key, &sig, &slen, data, sizeof(data));
364 key_verify(key, sig, slen, data, sizeof(data));
365 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100366 return key;
367}
368
Damien Miller8056a9d2006-03-15 12:05:40 +1100369static int
370get_line(FILE *fp, char *line, size_t len)
371{
372 int c;
373 size_t pos = 0;
374
375 line[0] = '\0';
376 while ((c = fgetc(fp)) != EOF) {
377 if (pos >= len - 1) {
378 fprintf(stderr, "input line too long.\n");
379 exit(1);
380 }
Damien Miller90967402006-03-26 14:07:26 +1100381 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100382 case '\r':
383 c = fgetc(fp);
384 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
385 fprintf(stderr, "unget: %s\n", strerror(errno));
386 exit(1);
387 }
388 return pos;
389 case '\n':
390 return pos;
391 }
392 line[pos++] = c;
393 line[pos] = '\0';
394 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100395 /* We reached EOF */
396 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100397}
398
Ben Lindstrombba81212001-06-25 05:01:22 +0000399static void
Damien Millereba71ba2000-04-29 23:57:08 +1000400do_convert_from_ssh2(struct passwd *pw)
401{
402 Key *k;
403 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000404 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100405 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000406 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000407 char encoded[8096];
408 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100409 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000410 FILE *fp;
411
412 if (!have_identity)
413 ask_filename(pw, "Enter file in which the key is");
414 if (stat(identity_file, &st) < 0) {
415 perror(identity_file);
416 exit(1);
417 }
418 fp = fopen(identity_file, "r");
419 if (fp == NULL) {
420 perror(identity_file);
421 exit(1);
422 }
423 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100424 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
425 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000426 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000427 if (strncmp(line, "----", 4) == 0 ||
428 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100429 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
430 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000431 if (strstr(line, " END ") != NULL) {
432 break;
433 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000434 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000435 continue;
436 }
Damien Miller30c3d422000-05-09 11:02:59 +1000437 if (escaped) {
438 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000439 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000440 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000441 }
Damien Millereba71ba2000-04-29 23:57:08 +1000442 strlcat(encoded, line, sizeof(encoded));
443 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000444 len = strlen(encoded);
445 if (((len % 4) == 3) &&
446 (encoded[len-1] == '=') &&
447 (encoded[len-2] == '=') &&
448 (encoded[len-3] == '='))
449 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100450 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000451 if (blen < 0) {
452 fprintf(stderr, "uudecode failed.\n");
453 exit(1);
454 }
Damien Miller874d77b2000-10-14 16:23:11 +1100455 k = private ?
456 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100457 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100458 if (k == NULL) {
459 fprintf(stderr, "decode blob failed.\n");
460 exit(1);
461 }
462 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000463 (k->type == KEY_DSA ?
464 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
465 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100466 key_write(k, stdout);
467 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100468 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100469 exit(1);
470 }
Damien Millereba71ba2000-04-29 23:57:08 +1000471 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100472 if (!private)
473 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000474 fclose(fp);
475 exit(0);
476}
477
Ben Lindstrombba81212001-06-25 05:01:22 +0000478static void
Damien Millereba71ba2000-04-29 23:57:08 +1000479do_print_public(struct passwd *pw)
480{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000481 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000482 struct stat st;
483
484 if (!have_identity)
485 ask_filename(pw, "Enter file in which the key is");
486 if (stat(identity_file, &st) < 0) {
487 perror(identity_file);
488 exit(1);
489 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000490 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000491 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000492 fprintf(stderr, "load failed\n");
493 exit(1);
494 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000495 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000496 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000497 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000498 fprintf(stdout, "\n");
499 exit(0);
500}
501
Ben Lindstromcd392282001-07-04 03:44:03 +0000502static void
Damien Miller86cbb442010-02-12 09:22:57 +1100503do_download(struct passwd *pw, char *pkcs11provider)
Ben Lindstromcd392282001-07-04 03:44:03 +0000504{
Damien Miller7ea845e2010-02-12 09:21:02 +1100505#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000506 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100507 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000508
Damien Miller7ea845e2010-02-12 09:21:02 +1100509 pkcs11_init(0);
510 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
511 if (nkeys <= 0)
512 fatal("cannot read public key from pkcs11");
513 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000514 key_write(keys[i], stdout);
515 key_free(keys[i]);
516 fprintf(stdout, "\n");
517 }
518 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100519 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000520 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100521#else
522 fatal("no pkcs11 support");
523#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000524}
Ben Lindstromcd392282001-07-04 03:44:03 +0000525
Ben Lindstrombba81212001-06-25 05:01:22 +0000526static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100527do_fingerprint(struct passwd *pw)
528{
Damien Miller98c7ad62000-03-09 21:27:49 +1100529 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000530 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000531 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100532 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000533 enum fp_rep rep;
534 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100535 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100536
Ben Lindstromd0fca422001-03-26 13:44:06 +0000537 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
538 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000539
Damien Miller95def091999-11-25 00:26:21 +1100540 if (!have_identity)
541 ask_filename(pw, "Enter file in which the key is");
542 if (stat(identity_file, &st) < 0) {
543 perror(identity_file);
544 exit(1);
545 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000546 public = key_load_public(identity_file, &comment);
547 if (public != NULL) {
548 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100549 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000550 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
551 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000552 if (log_level >= SYSLOG_LEVEL_VERBOSE)
553 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100554 key_free(public);
555 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000556 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000557 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100558 exit(0);
559 }
Damien Miller40b59852006-06-13 13:00:25 +1000560 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000561 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000562 comment = NULL;
563 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100564
565 f = fopen(identity_file, "r");
566 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100567 while (fgets(line, sizeof(line), f)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000568 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100569 error("line %d too long: %.40s...",
570 num + 1, line);
Damien Miller98c7ad62000-03-09 21:27:49 +1100571 skip = 1;
572 continue;
Damien Miller95def091999-11-25 00:26:21 +1100573 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100574 num++;
575 if (skip) {
576 skip = 0;
577 continue;
578 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000579 *cp = '\0';
Damien Miller98c7ad62000-03-09 21:27:49 +1100580
581 /* Skip leading whitespace, empty and comment lines. */
582 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
583 ;
584 if (!*cp || *cp == '\n' || *cp == '#')
Damien Miller80163902007-01-05 16:30:16 +1100585 continue;
Damien Miller98c7ad62000-03-09 21:27:49 +1100586 i = strtol(cp, &ep, 10);
587 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
588 int quoted = 0;
589 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000590 for (; *cp && (quoted || (*cp != ' ' &&
591 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100592 if (*cp == '\\' && cp[1] == '"')
593 cp++; /* Skip both */
594 else if (*cp == '"')
595 quoted = !quoted;
596 }
597 if (!*cp)
598 continue;
599 *cp++ = '\0';
600 }
601 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000602 public = key_new(KEY_RSA1);
603 if (key_read(public, &cp) != 1) {
604 cp = ep;
605 key_free(public);
606 public = key_new(KEY_UNSPEC);
607 if (key_read(public, &cp) != 1) {
608 key_free(public);
609 continue;
610 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100611 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000612 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000613 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100614 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000615 printf("%u %s %s (%s)\n", key_size(public), fp,
616 comment ? comment : "no comment", key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000617 if (log_level >= SYSLOG_LEVEL_VERBOSE)
618 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000619 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000620 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000621 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000622 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100623 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100624 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100625 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100626 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100627 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100628 exit(1);
629 }
Damien Miller95def091999-11-25 00:26:21 +1100630 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100631}
632
Damien Miller4b42d7f2005-03-01 21:48:35 +1100633static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100634printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100635{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000636 if (print_fingerprint) {
637 enum fp_rep rep;
638 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000639 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000640
641 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
642 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
643 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100644 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000645 printf("%u %s %s (%s)\n", key_size(public), fp, name,
646 key_type(public));
647 if (log_level >= SYSLOG_LEVEL_VERBOSE)
648 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000649 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000650 xfree(fp);
651 } else {
652 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
653 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100654 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000655 if (!key_write(public, f))
656 fatal("key_write failed");
657 fprintf(f, "\n");
658 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100659}
660
661static void
662do_known_hosts(struct passwd *pw, const char *name)
663{
664 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100665 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100666 char *cp, *cp2, *kp, *kp2;
667 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100668 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100669 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100670
671 if (!have_identity) {
672 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
673 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
674 sizeof(identity_file))
675 fatal("Specified known hosts path too long");
676 xfree(cp);
677 have_identity = 1;
678 }
679 if ((in = fopen(identity_file, "r")) == NULL)
680 fatal("fopen: %s", strerror(errno));
681
682 /*
683 * Find hosts goes to stdout, hash and deletions happen in-place
684 * A corner case is ssh-keygen -HF foo, which should go to stdout
685 */
686 if (!find_host && (hash_hosts || delete_host)) {
687 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
688 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
689 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
690 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
691 fatal("known_hosts path too long");
692 umask(077);
693 if ((c = mkstemp(tmp)) == -1)
694 fatal("mkstemp: %s", strerror(errno));
695 if ((out = fdopen(c, "w")) == NULL) {
696 c = errno;
697 unlink(tmp);
698 fatal("fdopen: %s", strerror(c));
699 }
700 inplace = 1;
701 }
702
703 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000704 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100705 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100706 skip = 1;
707 invalid = 1;
708 continue;
709 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000710 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100711 if (skip) {
712 skip = 0;
713 continue;
714 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000715 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100716
717 /* Skip leading whitespace, empty and comment lines. */
718 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
719 ;
720 if (!*cp || *cp == '\n' || *cp == '#') {
721 if (inplace)
722 fprintf(out, "%s\n", cp);
723 continue;
724 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100725 /* Check whether this is a CA key */
726 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
727 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
728 cp[sizeof(CA_MARKER) - 1] == '\t')) {
729 ca = 1;
730 cp += sizeof(CA_MARKER);
731 } else
732 ca = 0;
733
Damien Miller4b42d7f2005-03-01 21:48:35 +1100734 /* Find the end of the host name portion. */
735 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
736 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100737
Damien Miller4b42d7f2005-03-01 21:48:35 +1100738 if (*kp == '\0' || *(kp + 1) == '\0') {
739 error("line %d missing key: %.40s...",
740 num, line);
741 invalid = 1;
742 continue;
743 }
744 *kp++ = '\0';
745 kp2 = kp;
746
Damien Miller0a80ca12010-02-27 07:55:05 +1100747 pub = key_new(KEY_RSA1);
748 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100749 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100750 key_free(pub);
751 pub = key_new(KEY_UNSPEC);
752 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100753 error("line %d invalid key: %.40s...",
754 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100755 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100756 invalid = 1;
757 continue;
758 }
759 }
760
761 if (*cp == HASH_DELIM) {
762 if (find_host || delete_host) {
763 cp2 = host_hash(name, cp, strlen(cp));
764 if (cp2 == NULL) {
765 error("line %d: invalid hashed "
766 "name: %.64s...", num, line);
767 invalid = 1;
768 continue;
769 }
770 c = (strcmp(cp2, cp) == 0);
771 if (find_host && c) {
772 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100773 "line %d type %s%s\n", name,
774 num, key_type(pub),
775 ca ? " (CA key)" : "");
776 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100777 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100778 if (delete_host && !c && !ca)
779 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100780 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100781 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100782 } else {
783 if (find_host || delete_host) {
784 c = (match_hostname(name, cp,
785 strlen(cp)) == 1);
786 if (find_host && c) {
787 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100788 "line %d type %s%s\n", name,
789 num, key_type(pub),
790 ca ? " (CA key)" : "");
791 printhost(out, name, pub,
792 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100793 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100794 if (delete_host && !c && !ca)
795 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100796 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100797 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100798 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100799 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100800 if (ca) {
801 fprintf(stderr, "Warning: "
802 "ignoring CA key for host: "
803 "%.64s\n", cp2);
804 printhost(out, cp2, pub, ca, 0);
805 } else if (strcspn(cp2, "*?!") !=
806 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100807 fprintf(stderr, "Warning: "
808 "ignoring host name with "
809 "metacharacters: %.64s\n",
810 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100811 printhost(out, cp2, pub, ca, 0);
812 } else
813 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +1100814 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100815 has_unhashed = 1;
816 }
817 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100818 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100819 }
820 fclose(in);
821
822 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100823 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100824 identity_file);
825 if (inplace) {
826 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100827 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100828 fclose(out);
829 unlink(tmp);
830 }
831 exit(1);
832 }
833
834 if (inplace) {
835 fclose(out);
836
837 /* Backup existing file */
838 if (unlink(old) == -1 && errno != ENOENT)
839 fatal("unlink %.100s: %s", old, strerror(errno));
840 if (link(identity_file, old) == -1)
841 fatal("link %.100s to %.100s: %s", identity_file, old,
842 strerror(errno));
843 /* Move new one into place */
844 if (rename(tmp, identity_file) == -1) {
845 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
846 strerror(errno));
847 unlink(tmp);
848 unlink(old);
849 exit(1);
850 }
851
852 fprintf(stderr, "%s updated.\n", identity_file);
853 fprintf(stderr, "Original contents retained as %s\n", old);
854 if (has_unhashed) {
855 fprintf(stderr, "WARNING: %s contains unhashed "
856 "entries\n", old);
857 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000858 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100859 }
860 }
861
862 exit(0);
863}
864
Damien Miller95def091999-11-25 00:26:21 +1100865/*
866 * Perform changing a passphrase. The argument is the passwd structure
867 * for the current user.
868 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000869static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100870do_change_passphrase(struct passwd *pw)
871{
Damien Miller95def091999-11-25 00:26:21 +1100872 char *comment;
873 char *old_passphrase, *passphrase1, *passphrase2;
874 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000875 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100876
Damien Miller95def091999-11-25 00:26:21 +1100877 if (!have_identity)
878 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100879 if (stat(identity_file, &st) < 0) {
880 perror(identity_file);
881 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000882 }
Damien Miller95def091999-11-25 00:26:21 +1100883 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000884 private = key_load_private(identity_file, "", &comment);
885 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100886 if (identity_passphrase)
887 old_passphrase = xstrdup(identity_passphrase);
888 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000889 old_passphrase =
890 read_passphrase("Enter old passphrase: ",
891 RP_ALLOW_STDIN);
892 private = key_load_private(identity_file, old_passphrase,
893 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000894 memset(old_passphrase, 0, strlen(old_passphrase));
895 xfree(old_passphrase);
896 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100897 printf("Bad passphrase.\n");
898 exit(1);
899 }
Damien Miller95def091999-11-25 00:26:21 +1100900 }
901 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000902
Damien Miller95def091999-11-25 00:26:21 +1100903 /* Ask the new passphrase (twice). */
904 if (identity_new_passphrase) {
905 passphrase1 = xstrdup(identity_new_passphrase);
906 passphrase2 = NULL;
907 } else {
908 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000909 read_passphrase("Enter new passphrase (empty for no "
910 "passphrase): ", RP_ALLOW_STDIN);
911 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100912 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100913
914 /* Verify that they are the same. */
915 if (strcmp(passphrase1, passphrase2) != 0) {
916 memset(passphrase1, 0, strlen(passphrase1));
917 memset(passphrase2, 0, strlen(passphrase2));
918 xfree(passphrase1);
919 xfree(passphrase2);
920 printf("Pass phrases do not match. Try again.\n");
921 exit(1);
922 }
923 /* Destroy the other copy. */
924 memset(passphrase2, 0, strlen(passphrase2));
925 xfree(passphrase2);
926 }
927
928 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000929 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000930 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100931 memset(passphrase1, 0, strlen(passphrase1));
932 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000933 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100934 xfree(comment);
935 exit(1);
936 }
937 /* Destroy the passphrase and the copy of the key in memory. */
938 memset(passphrase1, 0, strlen(passphrase1));
939 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000940 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100941 xfree(comment);
942
943 printf("Your identification has been saved with the new passphrase.\n");
944 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000945}
946
Damien Miller37876e92003-05-15 10:19:46 +1000947/*
948 * Print the SSHFP RR.
949 */
Damien Millercb314822006-03-26 13:48:01 +1100950static int
951do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000952{
953 Key *public;
954 char *comment = NULL;
955 struct stat st;
956
Damien Millercb314822006-03-26 13:48:01 +1100957 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000958 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100959 if (stat(fname, &st) < 0) {
960 if (errno == ENOENT)
961 return 0;
962 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000963 exit(1);
964 }
Damien Millercb314822006-03-26 13:48:01 +1100965 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000966 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000967 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000968 key_free(public);
969 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100970 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000971 }
972 if (comment)
973 xfree(comment);
974
Damien Millercb314822006-03-26 13:48:01 +1100975 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000976 exit(1);
977}
Damien Miller37876e92003-05-15 10:19:46 +1000978
Damien Miller95def091999-11-25 00:26:21 +1100979/*
980 * Change the comment of a private key file.
981 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000982static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000983do_change_comment(struct passwd *pw)
984{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000985 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000986 Key *private;
987 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100988 struct stat st;
989 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000990 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller95def091999-11-25 00:26:21 +1100992 if (!have_identity)
993 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100994 if (stat(identity_file, &st) < 0) {
995 perror(identity_file);
996 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000997 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000998 private = key_load_private(identity_file, "", &comment);
999 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001000 if (identity_passphrase)
1001 passphrase = xstrdup(identity_passphrase);
1002 else if (identity_new_passphrase)
1003 passphrase = xstrdup(identity_new_passphrase);
1004 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001005 passphrase = read_passphrase("Enter passphrase: ",
1006 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001007 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001008 private = key_load_private(identity_file, passphrase, &comment);
1009 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001010 memset(passphrase, 0, strlen(passphrase));
1011 xfree(passphrase);
1012 printf("Bad passphrase.\n");
1013 exit(1);
1014 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001015 } else {
1016 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001017 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001018 if (private->type != KEY_RSA1) {
1019 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1020 key_free(private);
1021 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001022 }
Damien Miller95def091999-11-25 00:26:21 +11001023 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001024
Damien Miller95def091999-11-25 00:26:21 +11001025 if (identity_comment) {
1026 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1027 } else {
1028 printf("Enter new comment: ");
1029 fflush(stdout);
1030 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1031 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001032 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001033 exit(1);
1034 }
Damien Miller14b017d2007-09-17 16:09:15 +10001035 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001036 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001037
Damien Miller95def091999-11-25 00:26:21 +11001038 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001039 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001040 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001041 memset(passphrase, 0, strlen(passphrase));
1042 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001043 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001044 xfree(comment);
1045 exit(1);
1046 }
Damien Miller95def091999-11-25 00:26:21 +11001047 memset(passphrase, 0, strlen(passphrase));
1048 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001049 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001050 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001051
Damien Miller95def091999-11-25 00:26:21 +11001052 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001053 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1054 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001055 printf("Could not save your public key in %s\n", identity_file);
1056 exit(1);
1057 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001058 f = fdopen(fd, "w");
1059 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001060 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001061 exit(1);
1062 }
Damien Millereba71ba2000-04-29 23:57:08 +10001063 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001064 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001065 key_free(public);
1066 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001067 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068
Damien Miller95def091999-11-25 00:26:21 +11001069 xfree(comment);
1070
1071 printf("The comment in your key file has been changed.\n");
1072 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001073}
1074
Damien Miller0a80ca12010-02-27 07:55:05 +11001075static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001076fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001077{
1078 char from[32], to[32];
1079 static char ret[64];
1080 time_t tt;
1081 struct tm *tm;
1082
1083 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001084 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001085 return "forever";
1086
Damien Millerf2b70ca2010-03-05 07:39:35 +11001087 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001088 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001089 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001090 tm = localtime(&tt);
1091 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1092 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001093 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001094 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001095 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001096 tm = localtime(&tt);
1097 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1098 }
1099
Damien Millerf2b70ca2010-03-05 07:39:35 +11001100 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001101 snprintf(ret, sizeof(ret), "before %s", to);
1102 return ret;
1103 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001104 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001105 snprintf(ret, sizeof(ret), "after %s", from);
1106 return ret;
1107 }
1108
1109 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1110 return ret;
1111}
1112
1113static void
Damien Miller4e270b02010-04-16 15:56:21 +10001114add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001115{
1116 debug3("%s: %s", __func__, name);
1117 buffer_put_cstring(c, name);
1118 buffer_put_string(c, NULL, 0);
1119}
1120
1121static void
Damien Miller4e270b02010-04-16 15:56:21 +10001122add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001123{
1124 Buffer b;
1125
1126 debug3("%s: %s=%s", __func__, name, value);
1127 buffer_init(&b);
1128 buffer_put_cstring(&b, value);
1129
1130 buffer_put_cstring(c, name);
1131 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1132
1133 buffer_free(&b);
1134}
1135
1136static void
Damien Miller4e270b02010-04-16 15:56:21 +10001137prepare_options_buf(Buffer *c)
Damien Miller0a80ca12010-02-27 07:55:05 +11001138{
Damien Miller0a80ca12010-02-27 07:55:05 +11001139 buffer_clear(c);
Damien Miller4e270b02010-04-16 15:56:21 +10001140 if ((critical_flags & CRITOPT_X_FWD) != 0)
1141 add_flag_option(c, "permit-X11-forwarding");
1142 if ((critical_flags & CRITOPT_AGENT_FWD) != 0)
1143 add_flag_option(c, "permit-agent-forwarding");
1144 if ((critical_flags & CRITOPT_PORT_FWD) != 0)
1145 add_flag_option(c, "permit-port-forwarding");
1146 if ((critical_flags & CRITOPT_PTY) != 0)
1147 add_flag_option(c, "permit-pty");
1148 if ((critical_flags & CRITOPT_USER_RC) != 0)
1149 add_flag_option(c, "permit-user-rc");
1150 if (critical_command != NULL)
1151 add_string_option(c, "force-command", critical_command);
1152 if (critical_src_addr != NULL)
1153 add_string_option(c, "source-address", critical_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001154}
1155
1156static void
1157do_ca_sign(struct passwd *pw, int argc, char **argv)
1158{
1159 int i, fd;
1160 u_int n;
1161 Key *ca, *public;
1162 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1163 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001164 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001165
1166 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1167 if ((ca = load_identity(tmp)) == NULL)
1168 fatal("Couldn't load CA key \"%s\"", tmp);
1169 xfree(tmp);
1170
Damien Miller4e270b02010-04-16 15:56:21 +10001171 if (key_type_name != NULL) {
1172 switch (key_type_from_name(key_type_name)) {
1173 case KEY_RSA_CERT_V00:
1174 case KEY_DSA_CERT_V00:
1175 v00 = 1;
1176 break;
1177 case KEY_UNSPEC:
1178 if (strcasecmp(key_type_name, "v00") == 0) {
1179 v00 = 1;
1180 break;
1181 } else if (strcasecmp(key_type_name, "v01") == 0)
1182 break;
1183 /* FALLTHROUGH */
1184 default:
1185 fprintf(stderr, "unknown key type %s\n", key_type_name);
1186 exit(1);
1187 }
1188 }
1189
Damien Miller0a80ca12010-02-27 07:55:05 +11001190 for (i = 0; i < argc; i++) {
1191 /* Split list of principals */
1192 n = 0;
1193 if (cert_principals != NULL) {
1194 otmp = tmp = xstrdup(cert_principals);
1195 plist = NULL;
1196 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1197 plist = xrealloc(plist, n + 1, sizeof(*plist));
1198 if (*(plist[n] = xstrdup(cp)) == '\0')
1199 fatal("Empty principal name");
1200 }
1201 xfree(otmp);
1202 }
1203
1204 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1205 if ((public = key_load_public(tmp, &comment)) == NULL)
1206 fatal("%s: unable to open \"%s\"", __func__, tmp);
1207 if (public->type != KEY_RSA && public->type != KEY_DSA)
1208 fatal("%s: key \"%s\" type %s cannot be certified",
1209 __func__, tmp, key_type(public));
1210
1211 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001212 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001213 fatal("Could not upgrade key %s to certificate", tmp);
1214 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001215 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001216 public->cert->key_id = xstrdup(cert_key_id);
1217 public->cert->nprincipals = n;
1218 public->cert->principals = plist;
1219 public->cert->valid_after = cert_valid_from;
1220 public->cert->valid_before = cert_valid_to;
Damien Miller4e270b02010-04-16 15:56:21 +10001221 prepare_options_buf(&public->cert->critical);
Damien Miller0a80ca12010-02-27 07:55:05 +11001222 public->cert->signature_key = key_from_private(ca);
1223
1224 if (key_certify(public, ca) != 0)
1225 fatal("Couldn't not certify key %s", tmp);
1226
1227 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1228 *cp = '\0';
1229 xasprintf(&out, "%s-cert.pub", tmp);
1230 xfree(tmp);
1231
1232 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1233 fatal("Could not open \"%s\" for writing: %s", out,
1234 strerror(errno));
1235 if ((f = fdopen(fd, "w")) == NULL)
1236 fatal("%s: fdopen: %s", __func__, strerror(errno));
1237 if (!key_write(public, f))
1238 fatal("Could not write certified key to %s", out);
1239 fprintf(f, " %s\n", comment);
1240 fclose(f);
1241
Damien Miller4e270b02010-04-16 15:56:21 +10001242 if (!quiet) {
1243 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1244 "valid %s", key_cert_type(public),
1245 out, public->cert->key_id, public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001246 cert_principals != NULL ? " for " : "",
1247 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001248 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001249 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001250
1251 key_free(public);
1252 xfree(out);
1253 }
1254 exit(0);
1255}
1256
1257static u_int64_t
1258parse_relative_time(const char *s, time_t now)
1259{
1260 int64_t mul, secs;
1261
1262 mul = *s == '-' ? -1 : 1;
1263
1264 if ((secs = convtime(s + 1)) == -1)
1265 fatal("Invalid relative certificate time %s", s);
1266 if (mul == -1 && secs > now)
1267 fatal("Certificate time %s cannot be represented", s);
1268 return now + (u_int64_t)(secs * mul);
1269}
1270
1271static u_int64_t
1272parse_absolute_time(const char *s)
1273{
1274 struct tm tm;
1275 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001276 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001277
Damien Miller2ca342b2010-03-03 12:14:15 +11001278 /*
1279 * POSIX strptime says "The application shall ensure that there
1280 * is white-space or other non-alphanumeric characters between
1281 * any two conversion specifications" so arrange things this way.
1282 */
1283 switch (strlen(s)) {
1284 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001285 fmt = "%Y-%m-%d";
1286 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001287 break;
1288 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001289 fmt = "%Y-%m-%dT%H:%M:%S";
1290 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001291 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1292 break;
1293 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001294 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001295 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001296
1297 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001298 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001299 fatal("Invalid certificate time %s", s);
1300 if ((tt = mktime(&tm)) < 0)
1301 fatal("Certificate time %s cannot be represented", s);
1302 return (u_int64_t)tt;
1303}
1304
1305static void
1306parse_cert_times(char *timespec)
1307{
1308 char *from, *to;
1309 time_t now = time(NULL);
1310 int64_t secs;
1311
1312 /* +timespec relative to now */
1313 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1314 if ((secs = convtime(timespec + 1)) == -1)
1315 fatal("Invalid relative certificate life %s", timespec);
1316 cert_valid_to = now + secs;
1317 /*
1318 * Backdate certificate one minute to avoid problems on hosts
1319 * with poorly-synchronised clocks.
1320 */
1321 cert_valid_from = ((now - 59)/ 60) * 60;
1322 return;
1323 }
1324
1325 /*
1326 * from:to, where
1327 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1328 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1329 */
1330 from = xstrdup(timespec);
1331 to = strchr(from, ':');
1332 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001333 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001334 *to++ = '\0';
1335
1336 if (*from == '-' || *from == '+')
1337 cert_valid_from = parse_relative_time(from, now);
1338 else
1339 cert_valid_from = parse_absolute_time(from);
1340
1341 if (*to == '-' || *to == '+')
1342 cert_valid_to = parse_relative_time(to, cert_valid_from);
1343 else
1344 cert_valid_to = parse_absolute_time(to);
1345
1346 if (cert_valid_to <= cert_valid_from)
1347 fatal("Empty certificate validity interval");
1348 xfree(from);
1349}
1350
1351static void
Damien Miller4e270b02010-04-16 15:56:21 +10001352add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001353{
1354 char *val;
1355
1356 if (strcmp(opt, "clear") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001357 critical_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001358 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001359 critical_flags &= ~CRITOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001360 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001361 critical_flags |= CRITOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001362 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001363 critical_flags &= ~CRITOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001364 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001365 critical_flags |= CRITOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001366 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001367 critical_flags &= ~CRITOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001368 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001369 critical_flags |= CRITOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001370 else if (strcasecmp(opt, "no-pty") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001371 critical_flags &= ~CRITOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001372 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001373 critical_flags |= CRITOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001374 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001375 critical_flags &= ~CRITOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001376 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001377 critical_flags |= CRITOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001378 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1379 val = opt + 14;
1380 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001381 fatal("Empty force-command option");
1382 if (critical_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001383 fatal("force-command already specified");
Damien Miller4e270b02010-04-16 15:56:21 +10001384 critical_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001385 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1386 val = opt + 15;
1387 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001388 fatal("Empty source-address option");
1389 if (critical_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001390 fatal("source-address already specified");
1391 if (addr_match_cidr_list(NULL, val) != 0)
1392 fatal("Invalid source-address list");
Damien Miller4e270b02010-04-16 15:56:21 +10001393 critical_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001394 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001395 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001396}
1397
Ben Lindstrombba81212001-06-25 05:01:22 +00001398static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001399do_show_cert(struct passwd *pw)
1400{
1401 Key *key;
1402 struct stat st;
1403 char *key_fp, *ca_fp;
Damien Miller4e270b02010-04-16 15:56:21 +10001404 Buffer options, option;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001405 u_char *name, *data;
Damien Miller4e270b02010-04-16 15:56:21 +10001406 u_int i, dlen, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001407
1408 if (!have_identity)
1409 ask_filename(pw, "Enter file in which the key is");
1410 if (stat(identity_file, &st) < 0) {
1411 perror(identity_file);
1412 exit(1);
1413 }
1414 if ((key = key_load_public(identity_file, NULL)) == NULL)
1415 fatal("%s is not a public key", identity_file);
1416 if (!key_is_cert(key))
1417 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001418 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1419
Damien Millerf2b70ca2010-03-05 07:39:35 +11001420 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1421 ca_fp = key_fingerprint(key->cert->signature_key,
1422 SSH_FP_MD5, SSH_FP_HEX);
1423
1424 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001425 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1426 key_cert_type(key));
1427 printf(" Public key: %s %s\n", key_type(key), key_fp);
1428 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001429 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001430 printf(" Key ID: \"%s\"\n", key->cert->key_id);
1431 if (!v00)
1432 printf(" Serial: %llu\n", key->cert->serial);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001433 printf(" Valid: %s\n",
1434 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1435 printf(" Principals: ");
1436 if (key->cert->nprincipals == 0)
1437 printf("(none)\n");
1438 else {
1439 for (i = 0; i < key->cert->nprincipals; i++)
1440 printf("\n %s",
1441 key->cert->principals[i]);
1442 printf("\n");
1443 }
Damien Miller4e270b02010-04-16 15:56:21 +10001444 printf(" Critical Options: ");
1445 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001446 printf("(none)\n");
1447 else {
1448 printf("\n");
Damien Miller4e270b02010-04-16 15:56:21 +10001449 buffer_init(&options);
1450 buffer_append(&options,
1451 buffer_ptr(&key->cert->critical),
1452 buffer_len(&key->cert->critical));
1453 buffer_init(&option);
1454 while (buffer_len(&options) != 0) {
1455 name = buffer_get_string(&options, NULL);
1456 data = buffer_get_string_ptr(&options, &dlen);
1457 buffer_append(&option, data, dlen);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001458 printf(" %s", name);
1459 if (strcmp(name, "permit-X11-forwarding") == 0 ||
1460 strcmp(name, "permit-agent-forwarding") == 0 ||
1461 strcmp(name, "permit-port-forwarding") == 0 ||
1462 strcmp(name, "permit-pty") == 0 ||
1463 strcmp(name, "permit-user-rc") == 0)
1464 printf("\n");
1465 else if (strcmp(name, "force-command") == 0 ||
1466 strcmp(name, "source-address") == 0) {
Damien Miller4e270b02010-04-16 15:56:21 +10001467 data = buffer_get_string(&option, NULL);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001468 printf(" %s\n", data);
1469 xfree(data);
1470 } else {
Damien Miller4e270b02010-04-16 15:56:21 +10001471 printf(" UNKNOWN OPTION (len %u)\n",
1472 buffer_len(&option));
1473 buffer_clear(&option);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001474 }
1475 xfree(name);
Damien Miller4e270b02010-04-16 15:56:21 +10001476 if (buffer_len(&option) != 0)
1477 fatal("Option corrupt: extra data at end");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001478 }
Damien Miller4e270b02010-04-16 15:56:21 +10001479 buffer_free(&option);
1480 buffer_free(&options);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001481 }
Damien Miller4e270b02010-04-16 15:56:21 +10001482 if (!v00) {
1483 printf(" Extensions: ");
1484 if (buffer_len(&key->cert->extensions) == 0)
1485 printf("(none)\n");
1486 else {
1487 printf("\n");
1488 buffer_init(&options);
1489 buffer_append(&options,
1490 buffer_ptr(&key->cert->extensions),
1491 buffer_len(&key->cert->extensions));
1492 buffer_init(&option);
1493 while (buffer_len(&options) != 0) {
1494 name = buffer_get_string(&options, NULL);
1495 (void)buffer_get_string_ptr(&options, &dlen);
1496 printf(" %s UNKNOWN OPTION "
1497 "(len %u)\n", name, dlen);
1498 xfree(name);
1499 }
1500 buffer_free(&option);
1501 buffer_free(&options);
1502 }
1503 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001504 exit(0);
1505}
1506
1507static void
Damien Miller431f66b1999-11-21 18:31:57 +11001508usage(void)
1509{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001510 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001511 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001512 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001513 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001514 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001515 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001516 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001517#ifdef ENABLE_PKCS11
1518 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1519#endif
Darren Tucker26dc3e62007-02-19 22:09:06 +11001520 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001521 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1522 fprintf(stderr, " -f filename Filename of the key file.\n");
1523 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1524 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1525 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001526 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1527 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Darren Tucker26dc3e62007-02-19 22:09:06 +11001528 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001529 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001530 fprintf(stderr, " -l Show fingerprint of key file.\n");
1531 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001532 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001533 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller4e270b02010-04-16 15:56:21 +10001534 fprintf(stderr, " -O cnstr Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001535 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1536 fprintf(stderr, " -p Change passphrase of private key file.\n");
1537 fprintf(stderr, " -q Quiet.\n");
1538 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1539 fprintf(stderr, " -r hostname Print DNS resource record.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001540 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001541 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1542 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1543 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001544 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001545 fprintf(stderr, " -v Verbose.\n");
1546 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1547 fprintf(stderr, " -y Read private key file and print public key.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001548
Damien Miller95def091999-11-25 00:26:21 +11001549 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001550}
1551
Damien Miller95def091999-11-25 00:26:21 +11001552/*
1553 * Main program for key management.
1554 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001555int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001556main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001557{
Damien Millera41c8b12002-01-22 23:05:08 +11001558 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller7ea845e2010-02-12 09:21:02 +11001559 char out_file[MAXPATHLEN], *pkcs11provider = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001560 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001561 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001562 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001563 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001564 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001565 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001566 int do_gen_candidates = 0, do_screen_candidates = 0;
1567 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001568 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001569 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001570
Damien Miller95def091999-11-25 00:26:21 +11001571 extern int optind;
1572 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001573
Darren Tuckerce321d82005-10-03 18:11:24 +10001574 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1575 sanitise_stdfd();
1576
Darren Tucker9ac56e92007-01-14 10:19:59 +11001577 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001578
Damien Millerd3a18572000-06-07 19:55:44 +10001579 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001580 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001581
Kevin Steves3a881912002-07-20 19:05:40 +00001582 init_rng();
1583 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001584
Damien Miller5428f641999-11-25 11:54:57 +11001585 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001586 pw = getpwuid(getuid());
1587 if (!pw) {
1588 printf("You don't exist, go away!\n");
1589 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001590 }
Damien Millereba71ba2000-04-29 23:57:08 +10001591 if (gethostname(hostname, sizeof(hostname)) < 0) {
1592 perror("gethostname");
1593 exit(1);
1594 }
Damien Miller5428f641999-11-25 11:54:57 +11001595
Damien Millerf2b70ca2010-03-05 07:39:35 +11001596 while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001597 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001598 switch (opt) {
1599 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001600 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001601 if (errstr)
1602 fatal("Bits has bad value %s (%s)",
1603 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001604 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001605 case 'F':
1606 find_host = 1;
1607 rr_hostname = optarg;
1608 break;
1609 case 'H':
1610 hash_hosts = 1;
1611 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001612 case 'I':
1613 cert_key_id = optarg;
1614 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001615 case 'R':
1616 delete_host = 1;
1617 rr_hostname = optarg;
1618 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001619 case 'L':
1620 show_cert = 1;
1621 break;
Damien Miller95def091999-11-25 00:26:21 +11001622 case 'l':
1623 print_fingerprint = 1;
1624 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001625 case 'B':
1626 print_bubblebabble = 1;
1627 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001628 case 'n':
1629 cert_principals = optarg;
1630 break;
Damien Miller95def091999-11-25 00:26:21 +11001631 case 'p':
1632 change_passphrase = 1;
1633 break;
Damien Miller95def091999-11-25 00:26:21 +11001634 case 'c':
1635 change_comment = 1;
1636 break;
Damien Miller95def091999-11-25 00:26:21 +11001637 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001638 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1639 sizeof(identity_file))
1640 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001641 have_identity = 1;
1642 break;
Damien Miller37876e92003-05-15 10:19:46 +10001643 case 'g':
1644 print_generic = 1;
1645 break;
Damien Miller95def091999-11-25 00:26:21 +11001646 case 'P':
1647 identity_passphrase = optarg;
1648 break;
Damien Miller95def091999-11-25 00:26:21 +11001649 case 'N':
1650 identity_new_passphrase = optarg;
1651 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001652 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10001653 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001654 break;
Damien Miller95def091999-11-25 00:26:21 +11001655 case 'C':
1656 identity_comment = optarg;
1657 break;
Damien Miller95def091999-11-25 00:26:21 +11001658 case 'q':
1659 quiet = 1;
1660 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001661 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001662 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001663 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001664 convert_to_ssh2 = 1;
1665 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001666 case 'h':
1667 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Miller4e270b02010-04-16 15:56:21 +10001668 critical_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001669 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001670 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001671 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001672 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001673 convert_from_ssh2 = 1;
1674 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001675 case 'y':
1676 print_public = 1;
1677 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001678 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001679 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001680 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001681 case 's':
1682 ca_key_path = optarg;
1683 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001684 case 't':
1685 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001686 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001687 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001688 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001689 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001690 case 'v':
1691 if (log_level == SYSLOG_LEVEL_INFO)
1692 log_level = SYSLOG_LEVEL_DEBUG1;
1693 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001694 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001695 log_level < SYSLOG_LEVEL_DEBUG3)
1696 log_level++;
1697 }
1698 break;
Damien Miller37876e92003-05-15 10:19:46 +10001699 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001700 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001701 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001702 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001703 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1704 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001705 if (errstr)
1706 fatal("Desired generator has bad value: %s (%s)",
1707 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001708 break;
1709 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001710 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001711 if (errstr)
1712 fatal("Invalid number of trials: %s (%s)",
1713 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001714 break;
1715 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001716 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10001717 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10001718 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10001719 break;
1720 case 'G':
1721 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001722 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1723 sizeof(out_file))
1724 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001725 break;
1726 case 'T':
1727 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001728 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1729 sizeof(out_file))
1730 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001731 break;
1732 case 'S':
1733 /* XXX - also compare length against bits */
1734 if (BN_hex2bn(&start, optarg) == 0)
1735 fatal("Invalid start point.");
1736 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001737 case 'V':
1738 parse_cert_times(optarg);
1739 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001740 case 'z':
1741 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
1742 if (errstr)
1743 fatal("Invalid serial number: %s", errstr);
1744 break;
Damien Miller95def091999-11-25 00:26:21 +11001745 case '?':
1746 default:
1747 usage();
1748 }
1749 }
Darren Tucker06930c72003-12-31 11:34:51 +11001750
1751 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001752 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001753
Damien Miller0a80ca12010-02-27 07:55:05 +11001754 argv += optind;
1755 argc -= optind;
1756
1757 if (ca_key_path != NULL) {
1758 if (argc < 1) {
1759 printf("Too few arguments.\n");
1760 usage();
1761 }
1762 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11001763 printf("Too many arguments.\n");
1764 usage();
1765 }
1766 if (change_passphrase && change_comment) {
1767 printf("Can only have one of -p and -c.\n");
1768 usage();
1769 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001770 if (print_fingerprint && (delete_host || hash_hosts)) {
1771 printf("Cannot use -l with -D or -R.\n");
1772 usage();
1773 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001774 if (ca_key_path != NULL) {
1775 if (cert_key_id == NULL)
1776 fatal("Must specify key id (-I) when certifying");
1777 do_ca_sign(pw, argc, argv);
1778 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001779 if (show_cert)
1780 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001781 if (delete_host || hash_hosts || find_host)
1782 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001783 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001784 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001785 if (change_passphrase)
1786 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001787 if (change_comment)
1788 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001789 if (convert_to_ssh2)
1790 do_convert_to_ssh2(pw);
1791 if (convert_from_ssh2)
1792 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001793 if (print_public)
1794 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001795 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001796 unsigned int n = 0;
1797
1798 if (have_identity) {
1799 n = do_print_resource_record(pw,
1800 identity_file, rr_hostname);
1801 if (n == 0) {
1802 perror(identity_file);
1803 exit(1);
1804 }
1805 exit(0);
1806 } else {
1807
1808 n += do_print_resource_record(pw,
1809 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1810 n += do_print_resource_record(pw,
1811 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1812
1813 if (n == 0)
1814 fatal("no keys found.");
1815 exit(0);
1816 }
Damien Miller37876e92003-05-15 10:19:46 +10001817 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001818 if (pkcs11provider != NULL)
1819 do_download(pw, pkcs11provider);
Damien Miller95def091999-11-25 00:26:21 +11001820
Darren Tucker019cefe2003-08-02 22:40:07 +10001821 if (do_gen_candidates) {
1822 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001823
Darren Tucker019cefe2003-08-02 22:40:07 +10001824 if (out == NULL) {
1825 error("Couldn't open modulus candidate file \"%s\": %s",
1826 out_file, strerror(errno));
1827 return (1);
1828 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001829 if (bits == 0)
1830 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001831 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001832 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001833
1834 return (0);
1835 }
1836
1837 if (do_screen_candidates) {
1838 FILE *in;
1839 FILE *out = fopen(out_file, "w");
1840
1841 if (have_identity && strcmp(identity_file, "-") != 0) {
1842 if ((in = fopen(identity_file, "r")) == NULL) {
1843 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001844 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001845 strerror(errno));
1846 }
1847 } else
1848 in = stdin;
1849
1850 if (out == NULL) {
1851 fatal("Couldn't open moduli file \"%s\": %s",
1852 out_file, strerror(errno));
1853 }
1854 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001855 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001856 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001857 }
1858
Damien Miller95def091999-11-25 00:26:21 +11001859 arc4random_stir();
1860
Damien Millerf14be5c2005-11-05 15:15:49 +11001861 if (key_type_name == NULL)
1862 key_type_name = "rsa";
1863
Damien Millere39cacc2000-11-29 12:18:44 +11001864 type = key_type_from_name(key_type_name);
1865 if (type == KEY_UNSPEC) {
1866 fprintf(stderr, "unknown key type %s\n", key_type_name);
1867 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001868 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001869 if (bits == 0)
1870 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Tim Rice660c3402005-11-28 17:45:32 -08001871 if (type == KEY_DSA && bits != 1024)
1872 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001873 if (!quiet)
1874 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001875 private = key_generate(type, bits);
1876 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001877 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001878 exit(1);
1879 }
1880 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001881
1882 if (!have_identity)
1883 ask_filename(pw, "Enter file in which to save the key");
1884
Damien Miller788f2122005-11-05 15:14:59 +11001885 /* Create ~/.ssh directory if it doesn't already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001886 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001887 if (strstr(identity_file, dotsshdir) != NULL &&
1888 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001889 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001890 error("Could not create directory '%s'.", dotsshdir);
1891 else if (!quiet)
1892 printf("Created directory '%s'.\n", dotsshdir);
1893 }
1894 /* If the file already exists, ask the user to confirm. */
1895 if (stat(identity_file, &st) >= 0) {
1896 char yesno[3];
1897 printf("%s already exists.\n", identity_file);
1898 printf("Overwrite (y/n)? ");
1899 fflush(stdout);
1900 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1901 exit(1);
1902 if (yesno[0] != 'y' && yesno[0] != 'Y')
1903 exit(1);
1904 }
1905 /* Ask for a passphrase (twice). */
1906 if (identity_passphrase)
1907 passphrase1 = xstrdup(identity_passphrase);
1908 else if (identity_new_passphrase)
1909 passphrase1 = xstrdup(identity_new_passphrase);
1910 else {
1911passphrase_again:
1912 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001913 read_passphrase("Enter passphrase (empty for no "
1914 "passphrase): ", RP_ALLOW_STDIN);
1915 passphrase2 = read_passphrase("Enter same passphrase again: ",
1916 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001917 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001918 /*
1919 * The passphrases do not match. Clear them and
1920 * retry.
1921 */
Damien Miller95def091999-11-25 00:26:21 +11001922 memset(passphrase1, 0, strlen(passphrase1));
1923 memset(passphrase2, 0, strlen(passphrase2));
1924 xfree(passphrase1);
1925 xfree(passphrase2);
1926 printf("Passphrases do not match. Try again.\n");
1927 goto passphrase_again;
1928 }
1929 /* Clear the other copy of the passphrase. */
1930 memset(passphrase2, 0, strlen(passphrase2));
1931 xfree(passphrase2);
1932 }
1933
Damien Miller95def091999-11-25 00:26:21 +11001934 if (identity_comment) {
1935 strlcpy(comment, identity_comment, sizeof(comment));
1936 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11001937 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001938 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1939 }
1940
1941 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001942 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001943 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001944 memset(passphrase1, 0, strlen(passphrase1));
1945 xfree(passphrase1);
1946 exit(1);
1947 }
1948 /* Clear the passphrase. */
1949 memset(passphrase1, 0, strlen(passphrase1));
1950 xfree(passphrase1);
1951
1952 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001953 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001954 arc4random_stir();
1955
1956 if (!quiet)
1957 printf("Your identification has been saved in %s.\n", identity_file);
1958
Damien Miller95def091999-11-25 00:26:21 +11001959 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001960 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1961 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001962 printf("Could not save your public key in %s\n", identity_file);
1963 exit(1);
1964 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001965 f = fdopen(fd, "w");
1966 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001967 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001968 exit(1);
1969 }
Damien Millereba71ba2000-04-29 23:57:08 +10001970 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001971 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001972 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001973 fclose(f);
1974
1975 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001976 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001977 char *ra = key_fingerprint(public, SSH_FP_MD5,
1978 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10001979 printf("Your public key has been saved in %s.\n",
1980 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001981 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001982 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001983 printf("The key's randomart image is:\n");
1984 printf("%s\n", ra);
1985 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00001986 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001987 }
Damien Millereba71ba2000-04-29 23:57:08 +10001988
1989 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001990 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001991}