blob: 005f9c7ab6282bdc40d0d5689e401113974bd377 [file] [log] [blame]
Damien Miller7ea845e2010-02-12 09:21:02 +11001/* $OpenBSD: ssh-keygen.c,v 1.177 2010/02/08 10:50:20 markus 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 Miller874d77b2000-10-14 16:23:11 +110051
Damien Miller7ea845e2010-02-12 09:21:02 +110052#ifdef ENABLE_PKCS11
53#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000054#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000055
Damien Miller3f54a9f2005-11-05 14:52:18 +110056/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
57#define DEFAULT_BITS 2048
58#define DEFAULT_BITS_DSA 1024
59u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
Damien Miller5428f641999-11-25 11:54:57 +110061/*
62 * Flag indicating that we just want to change the passphrase. This can be
63 * set on the command line.
64 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065int change_passphrase = 0;
66
Damien Miller5428f641999-11-25 11:54:57 +110067/*
68 * Flag indicating that we just want to change the comment. This can be set
69 * on the command line.
70 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071int change_comment = 0;
72
73int quiet = 0;
74
Darren Tucker35c45532008-06-13 04:43:15 +100075int log_level = SYSLOG_LEVEL_INFO;
76
Damien Miller4b42d7f2005-03-01 21:48:35 +110077/* Flag indicating that we want to hash a known_hosts file */
78int hash_hosts = 0;
79/* Flag indicating that we want lookup a host in known_hosts file */
80int find_host = 0;
81/* Flag indicating that we want to delete a host from a known_hosts file */
82int delete_host = 0;
83
Damien Miller10f6f6b1999-11-17 17:29:08 +110084/* Flag indicating that we just want to see the key fingerprint */
85int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000086int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110087
Damien Miller431f66b1999-11-21 18:31:57 +110088/* The identity file name, given on the command line or entered by the user. */
89char identity_file[1024];
90int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
92/* This is set to the passphrase if given on the command line. */
93char *identity_passphrase = NULL;
94
95/* This is set to the new passphrase if given on the command line. */
96char *identity_new_passphrase = NULL;
97
98/* This is set to the new comment if given on the command line. */
99char *identity_comment = NULL;
100
Damien Millereba71ba2000-04-29 23:57:08 +1000101/* Dump public key file in format used by real and the original SSH 2 */
102int convert_to_ssh2 = 0;
103int convert_from_ssh2 = 0;
104int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000105int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100106
Damien Millera41c8b12002-01-22 23:05:08 +1100107char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000108
Damien Miller431f66b1999-11-21 18:31:57 +1100109/* argv0 */
110extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000111
Damien Millereba71ba2000-04-29 23:57:08 +1000112char hostname[MAXHOSTNAMELEN];
113
Darren Tucker770fc012004-05-13 16:24:32 +1000114/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000115int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000116int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
117
Ben Lindstrombba81212001-06-25 05:01:22 +0000118static void
Damien Miller431f66b1999-11-21 18:31:57 +1100119ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120{
Damien Miller95def091999-11-25 00:26:21 +1100121 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100122 char *name = NULL;
123
Damien Miller993dd552002-02-19 15:22:47 +1100124 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000125 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100126 else {
Damien Miller993dd552002-02-19 15:22:47 +1100127 switch (key_type_from_name(key_type_name)) {
128 case KEY_RSA1:
129 name = _PATH_SSH_CLIENT_IDENTITY;
130 break;
131 case KEY_DSA:
132 name = _PATH_SSH_CLIENT_ID_DSA;
133 break;
134 case KEY_RSA:
135 name = _PATH_SSH_CLIENT_ID_RSA;
136 break;
137 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100138 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100139 exit(1);
140 break;
141 }
Damien Miller90967402006-03-26 14:07:26 +1100142 }
Damien Millere39cacc2000-11-29 12:18:44 +1100143 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000144 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100145 if (fgets(buf, sizeof(buf), stdin) == NULL)
146 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000147 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100148 if (strcmp(buf, "") != 0)
149 strlcpy(identity_file, buf, sizeof(identity_file));
150 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100151}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Ben Lindstrombba81212001-06-25 05:01:22 +0000153static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000154load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000155{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000156 char *pass;
157 Key *prv;
158
Ben Lindstroma3700052001-04-05 23:26:32 +0000159 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000160 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000161 if (identity_passphrase)
162 pass = xstrdup(identity_passphrase);
163 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000164 pass = read_passphrase("Enter passphrase: ",
165 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000166 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000167 memset(pass, 0, strlen(pass));
168 xfree(pass);
169 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000170 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000171}
172
Damien Miller874d77b2000-10-14 16:23:11 +1100173#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000174#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100175#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000176#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000177
Ben Lindstrombba81212001-06-25 05:01:22 +0000178static void
Damien Millereba71ba2000-04-29 23:57:08 +1000179do_convert_to_ssh2(struct passwd *pw)
180{
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000181 Key *k;
Ben Lindstromc58ab022002-02-26 18:15:09 +0000182 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000183 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100184 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000185 struct stat st;
186
187 if (!have_identity)
188 ask_filename(pw, "Enter file in which the key is");
189 if (stat(identity_file, &st) < 0) {
190 perror(identity_file);
191 exit(1);
192 }
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000193 if ((k = key_load_public(identity_file, NULL)) == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000194 if ((k = load_identity(identity_file)) == NULL) {
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000195 fprintf(stderr, "load failed\n");
196 exit(1);
197 }
Damien Millereba71ba2000-04-29 23:57:08 +1000198 }
Damien Millerdb274722003-05-14 13:45:22 +1000199 if (k->type == KEY_RSA1) {
200 fprintf(stderr, "version 1 keys are not supported\n");
201 exit(1);
202 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000203 if (key_to_blob(k, &blob, &len) <= 0) {
204 fprintf(stderr, "key_to_blob failed\n");
205 exit(1);
206 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100207 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
208 snprintf(comment, sizeof(comment),
209 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000210 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000211 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100212
213 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
214 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000215 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100216 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000217 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000218 xfree(blob);
219 exit(0);
220}
221
Ben Lindstrombba81212001-06-25 05:01:22 +0000222static void
Damien Miller874d77b2000-10-14 16:23:11 +1100223buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
224{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000225 u_int bignum_bits = buffer_get_int(b);
226 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000227
Damien Miller874d77b2000-10-14 16:23:11 +1100228 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000229 fatal("buffer_get_bignum_bits: input buffer too small: "
230 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100231 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
232 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100233 buffer_consume(b, bytes);
234}
235
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000237do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100238{
239 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100240 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100241 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000242 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000243 int magic, rlen, ktype, i1, i2, i3, i4;
244 u_int slen;
245 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100246
247 buffer_init(&b);
248 buffer_append(&b, blob, blen);
249
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100250 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100251 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
252 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
253 buffer_free(&b);
254 return NULL;
255 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000256 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100257 type = buffer_get_string(&b, NULL);
258 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000259 i2 = buffer_get_int(&b);
260 i3 = buffer_get_int(&b);
261 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100262 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100263 if (strcmp(cipher, "none") != 0) {
264 error("unsupported cipher %s", cipher);
265 xfree(cipher);
266 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000267 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100268 return NULL;
269 }
270 xfree(cipher);
271
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000272 if (strstr(type, "dsa")) {
273 ktype = KEY_DSA;
274 } else if (strstr(type, "rsa")) {
275 ktype = KEY_RSA;
276 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100277 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000278 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100279 return NULL;
280 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000281 key = key_new_private(ktype);
282 xfree(type);
283
284 switch (key->type) {
285 case KEY_DSA:
286 buffer_get_bignum_bits(&b, key->dsa->p);
287 buffer_get_bignum_bits(&b, key->dsa->g);
288 buffer_get_bignum_bits(&b, key->dsa->q);
289 buffer_get_bignum_bits(&b, key->dsa->pub_key);
290 buffer_get_bignum_bits(&b, key->dsa->priv_key);
291 break;
292 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100293 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000294 debug("e %lx", e);
295 if (e < 30) {
296 e <<= 8;
297 e += buffer_get_char(&b);
298 debug("e %lx", e);
299 e <<= 8;
300 e += buffer_get_char(&b);
301 debug("e %lx", e);
302 }
303 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000304 buffer_free(&b);
305 key_free(key);
306 return NULL;
307 }
308 buffer_get_bignum_bits(&b, key->rsa->d);
309 buffer_get_bignum_bits(&b, key->rsa->n);
310 buffer_get_bignum_bits(&b, key->rsa->iqmp);
311 buffer_get_bignum_bits(&b, key->rsa->q);
312 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000313 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000314 break;
315 }
Damien Miller874d77b2000-10-14 16:23:11 +1100316 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000317 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000318 error("do_convert_private_ssh2_from_blob: "
319 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100320 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000321
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000322 /* try the key */
323 key_sign(key, &sig, &slen, data, sizeof(data));
324 key_verify(key, sig, slen, data, sizeof(data));
325 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100326 return key;
327}
328
Damien Miller8056a9d2006-03-15 12:05:40 +1100329static int
330get_line(FILE *fp, char *line, size_t len)
331{
332 int c;
333 size_t pos = 0;
334
335 line[0] = '\0';
336 while ((c = fgetc(fp)) != EOF) {
337 if (pos >= len - 1) {
338 fprintf(stderr, "input line too long.\n");
339 exit(1);
340 }
Damien Miller90967402006-03-26 14:07:26 +1100341 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100342 case '\r':
343 c = fgetc(fp);
344 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
345 fprintf(stderr, "unget: %s\n", strerror(errno));
346 exit(1);
347 }
348 return pos;
349 case '\n':
350 return pos;
351 }
352 line[pos++] = c;
353 line[pos] = '\0';
354 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100355 /* We reached EOF */
356 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100357}
358
Ben Lindstrombba81212001-06-25 05:01:22 +0000359static void
Damien Millereba71ba2000-04-29 23:57:08 +1000360do_convert_from_ssh2(struct passwd *pw)
361{
362 Key *k;
363 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000364 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100365 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000366 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000367 char encoded[8096];
368 struct stat st;
Damien Miller874d77b2000-10-14 16:23:11 +1100369 int escaped = 0, private = 0, ok;
Damien Millereba71ba2000-04-29 23:57:08 +1000370 FILE *fp;
371
372 if (!have_identity)
373 ask_filename(pw, "Enter file in which the key is");
374 if (stat(identity_file, &st) < 0) {
375 perror(identity_file);
376 exit(1);
377 }
378 fp = fopen(identity_file, "r");
379 if (fp == NULL) {
380 perror(identity_file);
381 exit(1);
382 }
383 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100384 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
385 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000386 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000387 if (strncmp(line, "----", 4) == 0 ||
388 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100389 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
390 private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000391 if (strstr(line, " END ") != NULL) {
392 break;
393 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000394 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000395 continue;
396 }
Damien Miller30c3d422000-05-09 11:02:59 +1000397 if (escaped) {
398 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000399 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000400 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000401 }
Damien Millereba71ba2000-04-29 23:57:08 +1000402 strlcat(encoded, line, sizeof(encoded));
403 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000404 len = strlen(encoded);
405 if (((len % 4) == 3) &&
406 (encoded[len-1] == '=') &&
407 (encoded[len-2] == '=') &&
408 (encoded[len-3] == '='))
409 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100410 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000411 if (blen < 0) {
412 fprintf(stderr, "uudecode failed.\n");
413 exit(1);
414 }
Damien Miller874d77b2000-10-14 16:23:11 +1100415 k = private ?
416 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100417 key_from_blob(blob, blen);
Damien Miller874d77b2000-10-14 16:23:11 +1100418 if (k == NULL) {
419 fprintf(stderr, "decode blob failed.\n");
420 exit(1);
421 }
422 ok = private ?
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000423 (k->type == KEY_DSA ?
424 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
425 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
Damien Miller874d77b2000-10-14 16:23:11 +1100426 key_write(k, stdout);
427 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100428 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100429 exit(1);
430 }
Damien Millereba71ba2000-04-29 23:57:08 +1000431 key_free(k);
Damien Millera1db12b2002-01-22 23:20:15 +1100432 if (!private)
433 fprintf(stdout, "\n");
Damien Millereba71ba2000-04-29 23:57:08 +1000434 fclose(fp);
435 exit(0);
436}
437
Ben Lindstrombba81212001-06-25 05:01:22 +0000438static void
Damien Millereba71ba2000-04-29 23:57:08 +1000439do_print_public(struct passwd *pw)
440{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000441 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000442 struct stat st;
443
444 if (!have_identity)
445 ask_filename(pw, "Enter file in which the key is");
446 if (stat(identity_file, &st) < 0) {
447 perror(identity_file);
448 exit(1);
449 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000450 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000451 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000452 fprintf(stderr, "load failed\n");
453 exit(1);
454 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000455 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000456 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000457 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000458 fprintf(stdout, "\n");
459 exit(0);
460}
461
Ben Lindstromcd392282001-07-04 03:44:03 +0000462static void
Damien Miller7ea845e2010-02-12 09:21:02 +1100463do_download(struct passwd *pw, const char *pkcs11provider)
Ben Lindstromcd392282001-07-04 03:44:03 +0000464{
Damien Miller7ea845e2010-02-12 09:21:02 +1100465#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000466 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100467 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000468
Damien Miller7ea845e2010-02-12 09:21:02 +1100469 pkcs11_init(0);
470 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
471 if (nkeys <= 0)
472 fatal("cannot read public key from pkcs11");
473 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000474 key_write(keys[i], stdout);
475 key_free(keys[i]);
476 fprintf(stdout, "\n");
477 }
478 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100479 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000480 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100481#else
482 fatal("no pkcs11 support");
483#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000484}
Ben Lindstromcd392282001-07-04 03:44:03 +0000485
Ben Lindstrombba81212001-06-25 05:01:22 +0000486static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100487do_fingerprint(struct passwd *pw)
488{
Damien Miller98c7ad62000-03-09 21:27:49 +1100489 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000490 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000491 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100492 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000493 enum fp_rep rep;
494 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100495 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100496
Ben Lindstromd0fca422001-03-26 13:44:06 +0000497 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
498 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000499
Damien Miller95def091999-11-25 00:26:21 +1100500 if (!have_identity)
501 ask_filename(pw, "Enter file in which the key is");
502 if (stat(identity_file, &st) < 0) {
503 perror(identity_file);
504 exit(1);
505 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000506 public = key_load_public(identity_file, &comment);
507 if (public != NULL) {
508 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100509 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000510 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
511 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000512 if (log_level >= SYSLOG_LEVEL_VERBOSE)
513 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100514 key_free(public);
515 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000516 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000517 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100518 exit(0);
519 }
Damien Miller40b59852006-06-13 13:00:25 +1000520 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000521 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000522 comment = NULL;
523 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100524
525 f = fopen(identity_file, "r");
526 if (f != NULL) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100527 while (fgets(line, sizeof(line), f)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000528 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100529 error("line %d too long: %.40s...",
530 num + 1, line);
Damien Miller98c7ad62000-03-09 21:27:49 +1100531 skip = 1;
532 continue;
Damien Miller95def091999-11-25 00:26:21 +1100533 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100534 num++;
535 if (skip) {
536 skip = 0;
537 continue;
538 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000539 *cp = '\0';
Damien Miller98c7ad62000-03-09 21:27:49 +1100540
541 /* Skip leading whitespace, empty and comment lines. */
542 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
543 ;
544 if (!*cp || *cp == '\n' || *cp == '#')
Damien Miller80163902007-01-05 16:30:16 +1100545 continue;
Damien Miller98c7ad62000-03-09 21:27:49 +1100546 i = strtol(cp, &ep, 10);
547 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
548 int quoted = 0;
549 comment = cp;
Ben Lindstrom58d3b722002-06-23 21:28:13 +0000550 for (; *cp && (quoted || (*cp != ' ' &&
551 *cp != '\t')); cp++) {
Damien Miller98c7ad62000-03-09 21:27:49 +1100552 if (*cp == '\\' && cp[1] == '"')
553 cp++; /* Skip both */
554 else if (*cp == '"')
555 quoted = !quoted;
556 }
557 if (!*cp)
558 continue;
559 *cp++ = '\0';
560 }
561 ep = cp;
Ben Lindstrom2941f112000-12-29 16:50:13 +0000562 public = key_new(KEY_RSA1);
563 if (key_read(public, &cp) != 1) {
564 cp = ep;
565 key_free(public);
566 public = key_new(KEY_UNSPEC);
567 if (key_read(public, &cp) != 1) {
568 key_free(public);
569 continue;
570 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100571 }
Ben Lindstrom2941f112000-12-29 16:50:13 +0000572 comment = *cp ? cp : comment;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000573 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100574 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000575 printf("%u %s %s (%s)\n", key_size(public), fp,
576 comment ? comment : "no comment", key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000577 if (log_level >= SYSLOG_LEVEL_VERBOSE)
578 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000579 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000580 xfree(fp);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000581 key_free(public);
Ben Lindstrom2941f112000-12-29 16:50:13 +0000582 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100583 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100584 fclose(f);
Damien Miller95def091999-11-25 00:26:21 +1100585 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100586 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100587 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100588 exit(1);
589 }
Damien Miller95def091999-11-25 00:26:21 +1100590 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100591}
592
Damien Miller4b42d7f2005-03-01 21:48:35 +1100593static void
Damien Millera8796f32008-02-10 22:24:30 +1100594print_host(FILE *f, const char *name, Key *public, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100595{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000596 if (print_fingerprint) {
597 enum fp_rep rep;
598 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000599 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000600
601 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
602 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
603 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100604 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000605 printf("%u %s %s (%s)\n", key_size(public), fp, name,
606 key_type(public));
607 if (log_level >= SYSLOG_LEVEL_VERBOSE)
608 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000609 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000610 xfree(fp);
611 } else {
612 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
613 fatal("hash_host failed");
614 fprintf(f, "%s ", name);
615 if (!key_write(public, f))
616 fatal("key_write failed");
617 fprintf(f, "\n");
618 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100619}
620
621static void
622do_known_hosts(struct passwd *pw, const char *name)
623{
624 FILE *in, *out = stdout;
625 Key *public;
626 char *cp, *cp2, *kp, *kp2;
627 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100628 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100629
630 if (!have_identity) {
631 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
632 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
633 sizeof(identity_file))
634 fatal("Specified known hosts path too long");
635 xfree(cp);
636 have_identity = 1;
637 }
638 if ((in = fopen(identity_file, "r")) == NULL)
639 fatal("fopen: %s", strerror(errno));
640
641 /*
642 * Find hosts goes to stdout, hash and deletions happen in-place
643 * A corner case is ssh-keygen -HF foo, which should go to stdout
644 */
645 if (!find_host && (hash_hosts || delete_host)) {
646 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
647 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
648 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
649 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
650 fatal("known_hosts path too long");
651 umask(077);
652 if ((c = mkstemp(tmp)) == -1)
653 fatal("mkstemp: %s", strerror(errno));
654 if ((out = fdopen(c, "w")) == NULL) {
655 c = errno;
656 unlink(tmp);
657 fatal("fdopen: %s", strerror(c));
658 }
659 inplace = 1;
660 }
661
662 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000663 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100664 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100665 skip = 1;
666 invalid = 1;
667 continue;
668 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000669 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100670 if (skip) {
671 skip = 0;
672 continue;
673 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000674 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100675
676 /* Skip leading whitespace, empty and comment lines. */
677 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
678 ;
679 if (!*cp || *cp == '\n' || *cp == '#') {
680 if (inplace)
681 fprintf(out, "%s\n", cp);
682 continue;
683 }
684 /* Find the end of the host name portion. */
685 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
686 ;
687 if (*kp == '\0' || *(kp + 1) == '\0') {
688 error("line %d missing key: %.40s...",
689 num, line);
690 invalid = 1;
691 continue;
692 }
693 *kp++ = '\0';
694 kp2 = kp;
695
696 public = key_new(KEY_RSA1);
697 if (key_read(public, &kp) != 1) {
698 kp = kp2;
699 key_free(public);
700 public = key_new(KEY_UNSPEC);
701 if (key_read(public, &kp) != 1) {
702 error("line %d invalid key: %.40s...",
703 num, line);
704 key_free(public);
705 invalid = 1;
706 continue;
707 }
708 }
709
710 if (*cp == HASH_DELIM) {
711 if (find_host || delete_host) {
712 cp2 = host_hash(name, cp, strlen(cp));
713 if (cp2 == NULL) {
714 error("line %d: invalid hashed "
715 "name: %.64s...", num, line);
716 invalid = 1;
717 continue;
718 }
719 c = (strcmp(cp2, cp) == 0);
720 if (find_host && c) {
721 printf("# Host %s found: "
722 "line %d type %s\n", name,
723 num, key_type(public));
724 print_host(out, cp, public, 0);
725 }
726 if (delete_host && !c)
727 print_host(out, cp, public, 0);
728 } else if (hash_hosts)
729 print_host(out, cp, public, 0);
730 } else {
731 if (find_host || delete_host) {
732 c = (match_hostname(name, cp,
733 strlen(cp)) == 1);
734 if (find_host && c) {
735 printf("# Host %s found: "
736 "line %d type %s\n", name,
737 num, key_type(public));
Damien Millera8796f32008-02-10 22:24:30 +1100738 print_host(out, name, public,
739 hash_hosts);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100740 }
741 if (delete_host && !c)
742 print_host(out, cp, public, 0);
743 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100744 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100745 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100746 cp2 = strsep(&cp, ",")) {
747 if (strcspn(cp2, "*?!") != strlen(cp2))
748 fprintf(stderr, "Warning: "
749 "ignoring host name with "
750 "metacharacters: %.64s\n",
751 cp2);
752 else
753 print_host(out, cp2, public, 1);
754 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100755 has_unhashed = 1;
756 }
757 }
758 key_free(public);
759 }
760 fclose(in);
761
762 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100763 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +1100764 identity_file);
765 if (inplace) {
766 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +1100767 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100768 fclose(out);
769 unlink(tmp);
770 }
771 exit(1);
772 }
773
774 if (inplace) {
775 fclose(out);
776
777 /* Backup existing file */
778 if (unlink(old) == -1 && errno != ENOENT)
779 fatal("unlink %.100s: %s", old, strerror(errno));
780 if (link(identity_file, old) == -1)
781 fatal("link %.100s to %.100s: %s", identity_file, old,
782 strerror(errno));
783 /* Move new one into place */
784 if (rename(tmp, identity_file) == -1) {
785 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
786 strerror(errno));
787 unlink(tmp);
788 unlink(old);
789 exit(1);
790 }
791
792 fprintf(stderr, "%s updated.\n", identity_file);
793 fprintf(stderr, "Original contents retained as %s\n", old);
794 if (has_unhashed) {
795 fprintf(stderr, "WARNING: %s contains unhashed "
796 "entries\n", old);
797 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +1000798 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100799 }
800 }
801
802 exit(0);
803}
804
Damien Miller95def091999-11-25 00:26:21 +1100805/*
806 * Perform changing a passphrase. The argument is the passwd structure
807 * for the current user.
808 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000809static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100810do_change_passphrase(struct passwd *pw)
811{
Damien Miller95def091999-11-25 00:26:21 +1100812 char *comment;
813 char *old_passphrase, *passphrase1, *passphrase2;
814 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +1000815 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100816
Damien Miller95def091999-11-25 00:26:21 +1100817 if (!have_identity)
818 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100819 if (stat(identity_file, &st) < 0) {
820 perror(identity_file);
821 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000822 }
Damien Miller95def091999-11-25 00:26:21 +1100823 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000824 private = key_load_private(identity_file, "", &comment);
825 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100826 if (identity_passphrase)
827 old_passphrase = xstrdup(identity_passphrase);
828 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000829 old_passphrase =
830 read_passphrase("Enter old passphrase: ",
831 RP_ALLOW_STDIN);
832 private = key_load_private(identity_file, old_passphrase,
833 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000834 memset(old_passphrase, 0, strlen(old_passphrase));
835 xfree(old_passphrase);
836 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100837 printf("Bad passphrase.\n");
838 exit(1);
839 }
Damien Miller95def091999-11-25 00:26:21 +1100840 }
841 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000842
Damien Miller95def091999-11-25 00:26:21 +1100843 /* Ask the new passphrase (twice). */
844 if (identity_new_passphrase) {
845 passphrase1 = xstrdup(identity_new_passphrase);
846 passphrase2 = NULL;
847 } else {
848 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +0000849 read_passphrase("Enter new passphrase (empty for no "
850 "passphrase): ", RP_ALLOW_STDIN);
851 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +1100852 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100853
854 /* Verify that they are the same. */
855 if (strcmp(passphrase1, passphrase2) != 0) {
856 memset(passphrase1, 0, strlen(passphrase1));
857 memset(passphrase2, 0, strlen(passphrase2));
858 xfree(passphrase1);
859 xfree(passphrase2);
860 printf("Pass phrases do not match. Try again.\n");
861 exit(1);
862 }
863 /* Destroy the other copy. */
864 memset(passphrase2, 0, strlen(passphrase2));
865 xfree(passphrase2);
866 }
867
868 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000869 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000870 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100871 memset(passphrase1, 0, strlen(passphrase1));
872 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000873 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100874 xfree(comment);
875 exit(1);
876 }
877 /* Destroy the passphrase and the copy of the key in memory. */
878 memset(passphrase1, 0, strlen(passphrase1));
879 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +1000880 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +1100881 xfree(comment);
882
883 printf("Your identification has been saved with the new passphrase.\n");
884 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000885}
886
Damien Miller37876e92003-05-15 10:19:46 +1000887/*
888 * Print the SSHFP RR.
889 */
Damien Millercb314822006-03-26 13:48:01 +1100890static int
891do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +1000892{
893 Key *public;
894 char *comment = NULL;
895 struct stat st;
896
Damien Millercb314822006-03-26 13:48:01 +1100897 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +1000898 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +1100899 if (stat(fname, &st) < 0) {
900 if (errno == ENOENT)
901 return 0;
902 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +1000903 exit(1);
904 }
Damien Millercb314822006-03-26 13:48:01 +1100905 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +1000906 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000907 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +1000908 key_free(public);
909 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +1100910 return 1;
Damien Miller37876e92003-05-15 10:19:46 +1000911 }
912 if (comment)
913 xfree(comment);
914
Damien Millercb314822006-03-26 13:48:01 +1100915 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +1000916 exit(1);
917}
Damien Miller37876e92003-05-15 10:19:46 +1000918
Damien Miller95def091999-11-25 00:26:21 +1100919/*
920 * Change the comment of a private key file.
921 */
Ben Lindstrombba81212001-06-25 05:01:22 +0000922static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000923do_change_comment(struct passwd *pw)
924{
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000925 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +0000926 Key *private;
927 Key *public;
Damien Miller95def091999-11-25 00:26:21 +1100928 struct stat st;
929 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000930 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000931
Damien Miller95def091999-11-25 00:26:21 +1100932 if (!have_identity)
933 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +1100934 if (stat(identity_file, &st) < 0) {
935 perror(identity_file);
936 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000937 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000938 private = key_load_private(identity_file, "", &comment);
939 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100940 if (identity_passphrase)
941 passphrase = xstrdup(identity_passphrase);
942 else if (identity_new_passphrase)
943 passphrase = xstrdup(identity_new_passphrase);
944 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000945 passphrase = read_passphrase("Enter passphrase: ",
946 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +1100947 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000948 private = key_load_private(identity_file, passphrase, &comment);
949 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +1100950 memset(passphrase, 0, strlen(passphrase));
951 xfree(passphrase);
952 printf("Bad passphrase.\n");
953 exit(1);
954 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000955 } else {
956 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +1100957 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000958 if (private->type != KEY_RSA1) {
959 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
960 key_free(private);
961 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +1100962 }
Damien Miller95def091999-11-25 00:26:21 +1100963 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000964
Damien Miller95def091999-11-25 00:26:21 +1100965 if (identity_comment) {
966 strlcpy(new_comment, identity_comment, sizeof(new_comment));
967 } else {
968 printf("Enter new comment: ");
969 fflush(stdout);
970 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
971 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +1000972 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100973 exit(1);
974 }
Damien Miller14b017d2007-09-17 16:09:15 +1000975 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100976 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000977
Damien Miller95def091999-11-25 00:26:21 +1100978 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +0000979 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +0000980 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100981 memset(passphrase, 0, strlen(passphrase));
982 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +1000983 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +1100984 xfree(comment);
985 exit(1);
986 }
Damien Miller95def091999-11-25 00:26:21 +1100987 memset(passphrase, 0, strlen(passphrase));
988 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000989 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +1000990 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000991
Damien Miller95def091999-11-25 00:26:21 +1100992 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000993 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
994 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +1100995 printf("Could not save your public key in %s\n", identity_file);
996 exit(1);
997 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +0000998 f = fdopen(fd, "w");
999 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001000 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001001 exit(1);
1002 }
Damien Millereba71ba2000-04-29 23:57:08 +10001003 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001004 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001005 key_free(public);
1006 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001007 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001008
Damien Miller95def091999-11-25 00:26:21 +11001009 xfree(comment);
1010
1011 printf("The comment in your key file has been changed.\n");
1012 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001013}
1014
Ben Lindstrombba81212001-06-25 05:01:22 +00001015static void
Damien Miller431f66b1999-11-21 18:31:57 +11001016usage(void)
1017{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001018 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001019 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001020 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001021 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001022 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001023 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001024 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001025#ifdef ENABLE_PKCS11
1026 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1027#endif
Darren Tucker26dc3e62007-02-19 22:09:06 +11001028 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001029 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1030 fprintf(stderr, " -f filename Filename of the key file.\n");
1031 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1032 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1033 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Darren Tucker26dc3e62007-02-19 22:09:06 +11001034 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001035 fprintf(stderr, " -l Show fingerprint of key file.\n");
1036 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1037 fprintf(stderr, " -N phrase Provide new passphrase.\n");
1038 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1039 fprintf(stderr, " -p Change passphrase of private key file.\n");
1040 fprintf(stderr, " -q Quiet.\n");
1041 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1042 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1043 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1044 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1045 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001046 fprintf(stderr, " -v Verbose.\n");
1047 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1048 fprintf(stderr, " -y Read private key file and print public key.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001049
Damien Miller95def091999-11-25 00:26:21 +11001050 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001051}
1052
Damien Miller95def091999-11-25 00:26:21 +11001053/*
1054 * Main program for key management.
1055 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001056int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001057main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001058{
Damien Millera41c8b12002-01-22 23:05:08 +11001059 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller7ea845e2010-02-12 09:21:02 +11001060 char out_file[MAXPATHLEN], *pkcs11provider = NULL;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001061 char *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001062 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001063 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001064 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001065 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001066 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001067 int do_gen_candidates = 0, do_screen_candidates = 0;
1068 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001069 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001070 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001071
Damien Miller95def091999-11-25 00:26:21 +11001072 extern int optind;
1073 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001074
Darren Tuckerce321d82005-10-03 18:11:24 +10001075 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1076 sanitise_stdfd();
1077
Darren Tucker9ac56e92007-01-14 10:19:59 +11001078 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001079
Damien Millerd3a18572000-06-07 19:55:44 +10001080 SSLeay_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001081 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001082
Kevin Steves3a881912002-07-20 19:05:40 +00001083 init_rng();
1084 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001085
Damien Miller5428f641999-11-25 11:54:57 +11001086 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001087 pw = getpwuid(getuid());
1088 if (!pw) {
1089 printf("You don't exist, go away!\n");
1090 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001091 }
Damien Millereba71ba2000-04-29 23:57:08 +10001092 if (gethostname(hostname, sizeof(hostname)) < 0) {
1093 perror("gethostname");
1094 exit(1);
1095 }
Damien Miller5428f641999-11-25 11:54:57 +11001096
Damien Millerdf8b7db2007-01-05 16:22:57 +11001097 while ((opt = getopt(argc, argv,
Damien Miller7ea845e2010-02-12 09:21:02 +11001098 "degiqpclBHvxXyF:b:f:t:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001099 switch (opt) {
1100 case 'b':
Damien Miller5f340062006-03-26 14:27:57 +11001101 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001102 if (errstr)
1103 fatal("Bits has bad value %s (%s)",
1104 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001105 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001106 case 'F':
1107 find_host = 1;
1108 rr_hostname = optarg;
1109 break;
1110 case 'H':
1111 hash_hosts = 1;
1112 break;
1113 case 'R':
1114 delete_host = 1;
1115 rr_hostname = optarg;
1116 break;
Damien Miller95def091999-11-25 00:26:21 +11001117 case 'l':
1118 print_fingerprint = 1;
1119 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001120 case 'B':
1121 print_bubblebabble = 1;
1122 break;
Damien Miller95def091999-11-25 00:26:21 +11001123 case 'p':
1124 change_passphrase = 1;
1125 break;
Damien Miller95def091999-11-25 00:26:21 +11001126 case 'c':
1127 change_comment = 1;
1128 break;
Damien Miller95def091999-11-25 00:26:21 +11001129 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001130 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1131 sizeof(identity_file))
1132 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001133 have_identity = 1;
1134 break;
Damien Miller37876e92003-05-15 10:19:46 +10001135 case 'g':
1136 print_generic = 1;
1137 break;
Damien Miller95def091999-11-25 00:26:21 +11001138 case 'P':
1139 identity_passphrase = optarg;
1140 break;
Damien Miller95def091999-11-25 00:26:21 +11001141 case 'N':
1142 identity_new_passphrase = optarg;
1143 break;
Damien Miller95def091999-11-25 00:26:21 +11001144 case 'C':
1145 identity_comment = optarg;
1146 break;
Damien Miller95def091999-11-25 00:26:21 +11001147 case 'q':
1148 quiet = 1;
1149 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001150 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001151 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001152 /* export key */
Damien Millereba71ba2000-04-29 23:57:08 +10001153 convert_to_ssh2 = 1;
1154 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001155 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001156 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001157 /* import key */
Damien Millereba71ba2000-04-29 23:57:08 +10001158 convert_from_ssh2 = 1;
1159 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001160 case 'y':
1161 print_public = 1;
1162 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001163 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001164 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001165 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001166 case 't':
1167 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001168 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001169 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001170 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001171 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001172 case 'v':
1173 if (log_level == SYSLOG_LEVEL_INFO)
1174 log_level = SYSLOG_LEVEL_DEBUG1;
1175 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001176 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001177 log_level < SYSLOG_LEVEL_DEBUG3)
1178 log_level++;
1179 }
1180 break;
Damien Miller37876e92003-05-15 10:19:46 +10001181 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001182 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001183 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001184 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001185 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1186 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001187 if (errstr)
1188 fatal("Desired generator has bad value: %s (%s)",
1189 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001190 break;
1191 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001192 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001193 if (errstr)
1194 fatal("Invalid number of trials: %s (%s)",
1195 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001196 break;
1197 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001198 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001199 if (errstr) {
1200 fatal("Memory limit is %s: %s", errstr, optarg);
1201 }
Darren Tucker019cefe2003-08-02 22:40:07 +10001202 break;
1203 case 'G':
1204 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001205 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1206 sizeof(out_file))
1207 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001208 break;
1209 case 'T':
1210 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001211 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1212 sizeof(out_file))
1213 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001214 break;
1215 case 'S':
1216 /* XXX - also compare length against bits */
1217 if (BN_hex2bn(&start, optarg) == 0)
1218 fatal("Invalid start point.");
1219 break;
Damien Miller95def091999-11-25 00:26:21 +11001220 case '?':
1221 default:
1222 usage();
1223 }
1224 }
Darren Tucker06930c72003-12-31 11:34:51 +11001225
1226 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11001227 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11001228
Damien Millerdf8b7db2007-01-05 16:22:57 +11001229 if (optind < argc) {
Damien Miller95def091999-11-25 00:26:21 +11001230 printf("Too many arguments.\n");
1231 usage();
1232 }
1233 if (change_passphrase && change_comment) {
1234 printf("Can only have one of -p and -c.\n");
1235 usage();
1236 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10001237 if (print_fingerprint && (delete_host || hash_hosts)) {
1238 printf("Cannot use -l with -D or -R.\n");
1239 usage();
1240 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001241 if (delete_host || hash_hosts || find_host)
1242 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001243 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11001244 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11001245 if (change_passphrase)
1246 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11001247 if (change_comment)
1248 do_change_comment(pw);
Kevin Steves3a881912002-07-20 19:05:40 +00001249 if (convert_to_ssh2)
1250 do_convert_to_ssh2(pw);
1251 if (convert_from_ssh2)
1252 do_convert_from_ssh2(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10001253 if (print_public)
1254 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001255 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11001256 unsigned int n = 0;
1257
1258 if (have_identity) {
1259 n = do_print_resource_record(pw,
1260 identity_file, rr_hostname);
1261 if (n == 0) {
1262 perror(identity_file);
1263 exit(1);
1264 }
1265 exit(0);
1266 } else {
1267
1268 n += do_print_resource_record(pw,
1269 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1270 n += do_print_resource_record(pw,
1271 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1272
1273 if (n == 0)
1274 fatal("no keys found.");
1275 exit(0);
1276 }
Damien Miller37876e92003-05-15 10:19:46 +10001277 }
Damien Miller7ea845e2010-02-12 09:21:02 +11001278 if (pkcs11provider != NULL)
1279 do_download(pw, pkcs11provider);
Damien Miller95def091999-11-25 00:26:21 +11001280
Darren Tucker019cefe2003-08-02 22:40:07 +10001281 if (do_gen_candidates) {
1282 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11001283
Darren Tucker019cefe2003-08-02 22:40:07 +10001284 if (out == NULL) {
1285 error("Couldn't open modulus candidate file \"%s\": %s",
1286 out_file, strerror(errno));
1287 return (1);
1288 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001289 if (bits == 0)
1290 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10001291 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001292 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10001293
1294 return (0);
1295 }
1296
1297 if (do_screen_candidates) {
1298 FILE *in;
1299 FILE *out = fopen(out_file, "w");
1300
1301 if (have_identity && strcmp(identity_file, "-") != 0) {
1302 if ((in = fopen(identity_file, "r")) == NULL) {
1303 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11001304 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10001305 strerror(errno));
1306 }
1307 } else
1308 in = stdin;
1309
1310 if (out == NULL) {
1311 fatal("Couldn't open moduli file \"%s\": %s",
1312 out_file, strerror(errno));
1313 }
1314 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11001315 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10001316 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10001317 }
1318
Damien Miller95def091999-11-25 00:26:21 +11001319 arc4random_stir();
1320
Damien Millerf14be5c2005-11-05 15:15:49 +11001321 if (key_type_name == NULL)
1322 key_type_name = "rsa";
1323
Damien Millere39cacc2000-11-29 12:18:44 +11001324 type = key_type_from_name(key_type_name);
1325 if (type == KEY_UNSPEC) {
1326 fprintf(stderr, "unknown key type %s\n", key_type_name);
1327 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10001328 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11001329 if (bits == 0)
1330 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
Tim Rice660c3402005-11-28 17:45:32 -08001331 if (type == KEY_DSA && bits != 1024)
1332 fatal("DSA keys must be 1024 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11001333 if (!quiet)
1334 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11001335 private = key_generate(type, bits);
1336 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001337 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11001338 exit(1);
1339 }
1340 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11001341
1342 if (!have_identity)
1343 ask_filename(pw, "Enter file in which to save the key");
1344
Damien Miller788f2122005-11-05 15:14:59 +11001345 /* Create ~/.ssh directory if it doesn't already exist. */
Ben Lindstrom226cfa02001-01-22 05:34:40 +00001346 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
Damien Miller95def091999-11-25 00:26:21 +11001347 if (strstr(identity_file, dotsshdir) != NULL &&
1348 stat(dotsshdir, &st) < 0) {
Damien Millerbe484b52000-07-15 14:14:16 +10001349 if (mkdir(dotsshdir, 0700) < 0)
Damien Miller95def091999-11-25 00:26:21 +11001350 error("Could not create directory '%s'.", dotsshdir);
1351 else if (!quiet)
1352 printf("Created directory '%s'.\n", dotsshdir);
1353 }
1354 /* If the file already exists, ask the user to confirm. */
1355 if (stat(identity_file, &st) >= 0) {
1356 char yesno[3];
1357 printf("%s already exists.\n", identity_file);
1358 printf("Overwrite (y/n)? ");
1359 fflush(stdout);
1360 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1361 exit(1);
1362 if (yesno[0] != 'y' && yesno[0] != 'Y')
1363 exit(1);
1364 }
1365 /* Ask for a passphrase (twice). */
1366 if (identity_passphrase)
1367 passphrase1 = xstrdup(identity_passphrase);
1368 else if (identity_new_passphrase)
1369 passphrase1 = xstrdup(identity_new_passphrase);
1370 else {
1371passphrase_again:
1372 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001373 read_passphrase("Enter passphrase (empty for no "
1374 "passphrase): ", RP_ALLOW_STDIN);
1375 passphrase2 = read_passphrase("Enter same passphrase again: ",
1376 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001377 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00001378 /*
1379 * The passphrases do not match. Clear them and
1380 * retry.
1381 */
Damien Miller95def091999-11-25 00:26:21 +11001382 memset(passphrase1, 0, strlen(passphrase1));
1383 memset(passphrase2, 0, strlen(passphrase2));
1384 xfree(passphrase1);
1385 xfree(passphrase2);
1386 printf("Passphrases do not match. Try again.\n");
1387 goto passphrase_again;
1388 }
1389 /* Clear the other copy of the passphrase. */
1390 memset(passphrase2, 0, strlen(passphrase2));
1391 xfree(passphrase2);
1392 }
1393
Damien Miller95def091999-11-25 00:26:21 +11001394 if (identity_comment) {
1395 strlcpy(comment, identity_comment, sizeof(comment));
1396 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11001397 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11001398 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1399 }
1400
1401 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001402 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001403 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001404 memset(passphrase1, 0, strlen(passphrase1));
1405 xfree(passphrase1);
1406 exit(1);
1407 }
1408 /* Clear the passphrase. */
1409 memset(passphrase1, 0, strlen(passphrase1));
1410 xfree(passphrase1);
1411
1412 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11001413 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001414 arc4random_stir();
1415
1416 if (!quiet)
1417 printf("Your identification has been saved in %s.\n", identity_file);
1418
Damien Miller95def091999-11-25 00:26:21 +11001419 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001420 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1421 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001422 printf("Could not save your public key in %s\n", identity_file);
1423 exit(1);
1424 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001425 f = fdopen(fd, "w");
1426 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001427 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001428 exit(1);
1429 }
Damien Millereba71ba2000-04-29 23:57:08 +10001430 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001431 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001432 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11001433 fclose(f);
1434
1435 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00001436 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001437 char *ra = key_fingerprint(public, SSH_FP_MD5,
1438 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10001439 printf("Your public key has been saved in %s.\n",
1440 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001441 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00001442 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10001443 printf("The key's randomart image is:\n");
1444 printf("%s\n", ra);
1445 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00001446 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11001447 }
Damien Millereba71ba2000-04-29 23:57:08 +10001448
1449 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11001450 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001451}