xref: /titanic_50/usr/src/cmd/ssh/ssh-keygen/ssh-keygen.c (revision e7b3b5cf9440e185b31e8fa6a8226e2bb510af5e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate  *                    All rights reserved
57c478bd9Sstevel@tonic-gate  * Identity and host key generation and maintenance.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
87c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
97c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
107c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
117c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
14442d23f4Sjp161948 /* $OpenBSD: ssh-keygen.c,v 1.160 2007/01/21 01:41:54 stevesk Exp $ */
157c478bd9Sstevel@tonic-gate 
16442d23f4Sjp161948 #include "includes.h"
177c478bd9Sstevel@tonic-gate #include <openssl/evp.h>
187c478bd9Sstevel@tonic-gate #include <openssl/pem.h>
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate #include "xmalloc.h"
217c478bd9Sstevel@tonic-gate #include "key.h"
227c478bd9Sstevel@tonic-gate #include "rsa.h"
237c478bd9Sstevel@tonic-gate #include "authfile.h"
247c478bd9Sstevel@tonic-gate #include "uuencode.h"
257c478bd9Sstevel@tonic-gate #include "buffer.h"
267c478bd9Sstevel@tonic-gate #include "bufaux.h"
277c478bd9Sstevel@tonic-gate #include "pathnames.h"
287c478bd9Sstevel@tonic-gate #include "log.h"
297c478bd9Sstevel@tonic-gate #include "readpass.h"
3026ba1984Sjp161948 #include "misc.h"
317c478bd9Sstevel@tonic-gate #include <langinfo.h>
32442d23f4Sjp161948 #include "match.h"
33442d23f4Sjp161948 #include "hostfile.h"
34442d23f4Sjp161948 #include "tildexpand.h"
357c478bd9Sstevel@tonic-gate 
36442d23f4Sjp161948 /* Number of bits in the RSA/DSA key.  This value can be set on the command line. */
37*e7b3b5cfSHai-May Chao #define	DEFAULT_BITS_RSA	2048
38*e7b3b5cfSHai-May Chao #define	DEFAULT_BITS_DSA	1024
39*e7b3b5cfSHai-May Chao u_int32_t bits = 0;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * Flag indicating that we just want to change the passphrase.  This can be
437c478bd9Sstevel@tonic-gate  * set on the command line.
447c478bd9Sstevel@tonic-gate  */
457c478bd9Sstevel@tonic-gate int change_passphrase = 0;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Flag indicating that we just want to change the comment.  This can be set
497c478bd9Sstevel@tonic-gate  * on the command line.
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate int change_comment = 0;
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate int quiet = 0;
547c478bd9Sstevel@tonic-gate 
55442d23f4Sjp161948 /* Flag indicating that we want to hash a known_hosts file */
56442d23f4Sjp161948 int hash_hosts = 0;
57442d23f4Sjp161948 /* Flag indicating that we want to lookup a host in known_hosts file */
58442d23f4Sjp161948 int find_host = 0;
59442d23f4Sjp161948 /* Flag indicating that we want to delete a host from a known_hosts file */
60442d23f4Sjp161948 int delete_host = 0;
61442d23f4Sjp161948 
627c478bd9Sstevel@tonic-gate /* Flag indicating that we just want to see the key fingerprint */
637c478bd9Sstevel@tonic-gate int print_fingerprint = 0;
647c478bd9Sstevel@tonic-gate int print_bubblebabble = 0;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /* The identity file name, given on the command line or entered by the user. */
677c478bd9Sstevel@tonic-gate char identity_file[1024];
687c478bd9Sstevel@tonic-gate int have_identity = 0;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate /* This is set to the passphrase if given on the command line. */
717c478bd9Sstevel@tonic-gate char *identity_passphrase = NULL;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /* This is set to the new passphrase if given on the command line. */
747c478bd9Sstevel@tonic-gate char *identity_new_passphrase = NULL;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* This is set to the new comment if given on the command line. */
777c478bd9Sstevel@tonic-gate char *identity_comment = NULL;
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate /* Dump public key file in format used by real and the original SSH 2 */
807c478bd9Sstevel@tonic-gate int convert_to_ssh2 = 0;
817c478bd9Sstevel@tonic-gate int convert_from_ssh2 = 0;
827c478bd9Sstevel@tonic-gate int print_public = 0;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate char *key_type_name = NULL;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* argv0 */
877c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
887c478bd9Sstevel@tonic-gate extern char *__progname;
897c478bd9Sstevel@tonic-gate #else
907c478bd9Sstevel@tonic-gate char *__progname;
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static void
ask_filename(struct passwd * pw,const char * prompt)967c478bd9Sstevel@tonic-gate ask_filename(struct passwd *pw, const char *prompt)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	char buf[1024];
997c478bd9Sstevel@tonic-gate 	char *name = NULL;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	if (key_type_name == NULL)
1027c478bd9Sstevel@tonic-gate 		name = _PATH_SSH_CLIENT_ID_RSA;
103442d23f4Sjp161948 	else {
1047c478bd9Sstevel@tonic-gate 		switch (key_type_from_name(key_type_name)) {
1057c478bd9Sstevel@tonic-gate 		case KEY_RSA1:
1067c478bd9Sstevel@tonic-gate 			name = _PATH_SSH_CLIENT_IDENTITY;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 		case KEY_DSA:
1097c478bd9Sstevel@tonic-gate 			name = _PATH_SSH_CLIENT_ID_DSA;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case KEY_RSA:
1127c478bd9Sstevel@tonic-gate 			name = _PATH_SSH_CLIENT_ID_RSA;
1137c478bd9Sstevel@tonic-gate 			break;
1147c478bd9Sstevel@tonic-gate 		default:
115442d23f4Sjp161948 			fprintf(stderr, gettext("bad key type"));
1167c478bd9Sstevel@tonic-gate 			exit(1);
1177c478bd9Sstevel@tonic-gate 			break;
1187c478bd9Sstevel@tonic-gate 		}
119442d23f4Sjp161948 	}
120442d23f4Sjp161948 	snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
121442d23f4Sjp161948 	fprintf(stderr, "%s (%s): ", gettext(prompt), identity_file);
1227c478bd9Sstevel@tonic-gate 	if (fgets(buf, sizeof(buf), stdin) == NULL)
1237c478bd9Sstevel@tonic-gate 		exit(1);
1247c478bd9Sstevel@tonic-gate 	if (strchr(buf, '\n'))
1257c478bd9Sstevel@tonic-gate 		*strchr(buf, '\n') = 0;
1267c478bd9Sstevel@tonic-gate 	if (strcmp(buf, "") != 0)
127442d23f4Sjp161948 		strlcpy(identity_file, buf, sizeof(identity_file));
1287c478bd9Sstevel@tonic-gate 	have_identity = 1;
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate static Key *
load_identity(char * filename)1327c478bd9Sstevel@tonic-gate load_identity(char *filename)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 	char *pass;
1357c478bd9Sstevel@tonic-gate 	Key *prv;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	prv = key_load_private(filename, "", NULL);
1387c478bd9Sstevel@tonic-gate 	if (prv == NULL) {
1397c478bd9Sstevel@tonic-gate 		if (identity_passphrase)
1407c478bd9Sstevel@tonic-gate 			pass = xstrdup(identity_passphrase);
1417c478bd9Sstevel@tonic-gate 		else
1427c478bd9Sstevel@tonic-gate 			pass = read_passphrase(gettext("Enter passphrase: "),
1437c478bd9Sstevel@tonic-gate 			    RP_ALLOW_STDIN);
1447c478bd9Sstevel@tonic-gate 		prv = key_load_private(filename, pass, NULL);
145442d23f4Sjp161948 		memset(pass, 0, strlen(pass));
1467c478bd9Sstevel@tonic-gate 		xfree(pass);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	return prv;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate #define SSH_COM_PUBLIC_BEGIN		"---- BEGIN SSH2 PUBLIC KEY ----"
1527c478bd9Sstevel@tonic-gate #define SSH_COM_PUBLIC_END		"---- END SSH2 PUBLIC KEY ----"
1537c478bd9Sstevel@tonic-gate #define SSH_COM_PRIVATE_BEGIN		"---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
1547c478bd9Sstevel@tonic-gate #define	SSH_COM_PRIVATE_KEY_MAGIC	0x3f6ff9eb
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate static void
do_convert_to_ssh2(struct passwd * pw)1577c478bd9Sstevel@tonic-gate do_convert_to_ssh2(struct passwd *pw)
1587c478bd9Sstevel@tonic-gate {
1597c478bd9Sstevel@tonic-gate 	Key *k;
1607c478bd9Sstevel@tonic-gate 	u_int len;
1617c478bd9Sstevel@tonic-gate 	u_char *blob;
1627c478bd9Sstevel@tonic-gate 	struct stat st;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (!have_identity)
1657c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
1667c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
1677c478bd9Sstevel@tonic-gate 		perror(identity_file);
1687c478bd9Sstevel@tonic-gate 		exit(1);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 	if ((k = key_load_public(identity_file, NULL)) == NULL) {
1717c478bd9Sstevel@tonic-gate 		if ((k = load_identity(identity_file)) == NULL) {
172442d23f4Sjp161948 			fprintf(stderr, gettext("load failed\n"));
1737c478bd9Sstevel@tonic-gate 			exit(1);
1747c478bd9Sstevel@tonic-gate 		}
1757c478bd9Sstevel@tonic-gate 	}
1767c478bd9Sstevel@tonic-gate 	if (key_to_blob(k, &blob, &len) <= 0) {
177442d23f4Sjp161948 		fprintf(stderr, gettext("key_to_blob failed\n"));
1787c478bd9Sstevel@tonic-gate 		exit(1);
1797c478bd9Sstevel@tonic-gate 	}
180442d23f4Sjp161948 	fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
181442d23f4Sjp161948 	fprintf(stdout, gettext(
1827c478bd9Sstevel@tonic-gate 	    "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n"),
1837c478bd9Sstevel@tonic-gate 	    key_size(k), key_type(k),
1847c478bd9Sstevel@tonic-gate 	    pw->pw_name, hostname);
1857c478bd9Sstevel@tonic-gate 	dump_base64(stdout, blob, len);
186442d23f4Sjp161948 	fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
1877c478bd9Sstevel@tonic-gate 	key_free(k);
1887c478bd9Sstevel@tonic-gate 	xfree(blob);
1897c478bd9Sstevel@tonic-gate 	exit(0);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate static void
buffer_get_bignum_bits(Buffer * b,BIGNUM * value)1937c478bd9Sstevel@tonic-gate buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
1947c478bd9Sstevel@tonic-gate {
195442d23f4Sjp161948 	u_int bignum_bits = buffer_get_int(b);
196442d23f4Sjp161948 	u_int bytes = (bignum_bits + 7) / 8;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if (buffer_len(b) < bytes)
1997c478bd9Sstevel@tonic-gate 		fatal("buffer_get_bignum_bits: input buffer too small: "
2007c478bd9Sstevel@tonic-gate 		    "need %d have %d", bytes, buffer_len(b));
201442d23f4Sjp161948 	if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
202442d23f4Sjp161948 		fatal("buffer_get_bignum_bits: BN_bin2bn failed");
2037c478bd9Sstevel@tonic-gate 	buffer_consume(b, bytes);
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate static Key *
do_convert_private_ssh2_from_blob(u_char * blob,u_int blen)2077c478bd9Sstevel@tonic-gate do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
2087c478bd9Sstevel@tonic-gate {
2097c478bd9Sstevel@tonic-gate 	Buffer b;
2107c478bd9Sstevel@tonic-gate 	Key *key = NULL;
2117c478bd9Sstevel@tonic-gate 	char *type, *cipher;
2127c478bd9Sstevel@tonic-gate 	u_char *sig, data[] = "abcde12345";
2137c478bd9Sstevel@tonic-gate 	int magic, rlen, ktype, i1, i2, i3, i4;
2147c478bd9Sstevel@tonic-gate 	u_int slen;
2157c478bd9Sstevel@tonic-gate 	u_long e;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	buffer_init(&b);
2187c478bd9Sstevel@tonic-gate 	buffer_append(&b, blob, blen);
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	magic  = buffer_get_int(&b);
2217c478bd9Sstevel@tonic-gate 	if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
2227c478bd9Sstevel@tonic-gate 		error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
2237c478bd9Sstevel@tonic-gate 		buffer_free(&b);
2247c478bd9Sstevel@tonic-gate 		return NULL;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	i1 = buffer_get_int(&b);
2277c478bd9Sstevel@tonic-gate 	type   = buffer_get_string(&b, NULL);
2287c478bd9Sstevel@tonic-gate 	cipher = buffer_get_string(&b, NULL);
2297c478bd9Sstevel@tonic-gate 	i2 = buffer_get_int(&b);
2307c478bd9Sstevel@tonic-gate 	i3 = buffer_get_int(&b);
2317c478bd9Sstevel@tonic-gate 	i4 = buffer_get_int(&b);
2327c478bd9Sstevel@tonic-gate 	debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
2337c478bd9Sstevel@tonic-gate 	if (strcmp(cipher, "none") != 0) {
2347c478bd9Sstevel@tonic-gate 		error("unsupported cipher %s", cipher);
2357c478bd9Sstevel@tonic-gate 		xfree(cipher);
2367c478bd9Sstevel@tonic-gate 		buffer_free(&b);
2377c478bd9Sstevel@tonic-gate 		xfree(type);
2387c478bd9Sstevel@tonic-gate 		return NULL;
2397c478bd9Sstevel@tonic-gate 	}
2407c478bd9Sstevel@tonic-gate 	xfree(cipher);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (strstr(type, "dsa")) {
2437c478bd9Sstevel@tonic-gate 		ktype = KEY_DSA;
2447c478bd9Sstevel@tonic-gate 	} else if (strstr(type, "rsa")) {
2457c478bd9Sstevel@tonic-gate 		ktype = KEY_RSA;
2467c478bd9Sstevel@tonic-gate 	} else {
247442d23f4Sjp161948 		buffer_free(&b);
2487c478bd9Sstevel@tonic-gate 		xfree(type);
2497c478bd9Sstevel@tonic-gate 		return NULL;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate 	key = key_new_private(ktype);
2527c478bd9Sstevel@tonic-gate 	xfree(type);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	switch (key->type) {
2557c478bd9Sstevel@tonic-gate 	case KEY_DSA:
2567c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->p);
2577c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->g);
2587c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->q);
2597c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->pub_key);
2607c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->priv_key);
2617c478bd9Sstevel@tonic-gate 		break;
2627c478bd9Sstevel@tonic-gate 	case KEY_RSA:
2637c478bd9Sstevel@tonic-gate 		e  = buffer_get_char(&b);
2647c478bd9Sstevel@tonic-gate 		debug("e %lx", e);
2657c478bd9Sstevel@tonic-gate 		if (e < 30) {
2667c478bd9Sstevel@tonic-gate 			e <<= 8;
2677c478bd9Sstevel@tonic-gate 			e += buffer_get_char(&b);
2687c478bd9Sstevel@tonic-gate 			debug("e %lx", e);
2697c478bd9Sstevel@tonic-gate 			e <<= 8;
2707c478bd9Sstevel@tonic-gate 			e += buffer_get_char(&b);
2717c478bd9Sstevel@tonic-gate 			debug("e %lx", e);
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 		if (!BN_set_word(key->rsa->e, e)) {
2747c478bd9Sstevel@tonic-gate 			buffer_free(&b);
2757c478bd9Sstevel@tonic-gate 			key_free(key);
2767c478bd9Sstevel@tonic-gate 			return NULL;
2777c478bd9Sstevel@tonic-gate 		}
2787c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->d);
2797c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->n);
2807c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->iqmp);
2817c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->q);
2827c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->p);
2837c478bd9Sstevel@tonic-gate 		rsa_generate_additional_parameters(key->rsa);
2847c478bd9Sstevel@tonic-gate 		break;
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 	rlen = buffer_len(&b);
2877c478bd9Sstevel@tonic-gate 	if (rlen != 0)
2887c478bd9Sstevel@tonic-gate 		error("do_convert_private_ssh2_from_blob: "
2897c478bd9Sstevel@tonic-gate 		    "remaining bytes in key blob %d", rlen);
2907c478bd9Sstevel@tonic-gate 	buffer_free(&b);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	/* try the key */
293442d23f4Sjp161948 	key_sign(key, &sig, &slen, data, sizeof(data));
2947c478bd9Sstevel@tonic-gate 	key_verify(key, sig, slen, data, sizeof(data));
2957c478bd9Sstevel@tonic-gate 	xfree(sig);
2967c478bd9Sstevel@tonic-gate 	return key;
2977c478bd9Sstevel@tonic-gate }
2987c478bd9Sstevel@tonic-gate 
299442d23f4Sjp161948 static int
get_line(FILE * fp,char * line,size_t len)300442d23f4Sjp161948 get_line(FILE *fp, char *line, size_t len)
301442d23f4Sjp161948 {
302442d23f4Sjp161948 	int c;
303442d23f4Sjp161948 	size_t pos = 0;
304442d23f4Sjp161948 
305442d23f4Sjp161948 	line[0] = '\0';
306442d23f4Sjp161948 	while ((c = fgetc(fp)) != EOF) {
307442d23f4Sjp161948 		if (pos >= len - 1) {
308442d23f4Sjp161948 			fprintf(stderr, "input line too long.\n");
309442d23f4Sjp161948 			exit(1);
310442d23f4Sjp161948 		}
311442d23f4Sjp161948 		switch (c) {
312442d23f4Sjp161948 		case '\r':
313442d23f4Sjp161948 			c = fgetc(fp);
314442d23f4Sjp161948 			if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
315442d23f4Sjp161948 				fprintf(stderr, "unget: %s\n", strerror(errno));
316442d23f4Sjp161948 				exit(1);
317442d23f4Sjp161948 			}
318442d23f4Sjp161948 			return pos;
319442d23f4Sjp161948 		case '\n':
320442d23f4Sjp161948 			return pos;
321442d23f4Sjp161948 		}
322442d23f4Sjp161948 		line[pos++] = c;
323442d23f4Sjp161948 		line[pos] = '\0';
324442d23f4Sjp161948 	}
325442d23f4Sjp161948 	/* We reached EOF */
326442d23f4Sjp161948 	return -1;
327442d23f4Sjp161948 }
328442d23f4Sjp161948 
3297c478bd9Sstevel@tonic-gate static void
do_convert_from_ssh2(struct passwd * pw)3307c478bd9Sstevel@tonic-gate do_convert_from_ssh2(struct passwd *pw)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	Key *k;
3337c478bd9Sstevel@tonic-gate 	int blen;
3347c478bd9Sstevel@tonic-gate 	u_int len;
335442d23f4Sjp161948 	char line[1024];
3367c478bd9Sstevel@tonic-gate 	u_char blob[8096];
3377c478bd9Sstevel@tonic-gate 	char encoded[8096];
3387c478bd9Sstevel@tonic-gate 	struct stat st;
3397c478bd9Sstevel@tonic-gate 	int escaped = 0, private = 0, ok;
3407c478bd9Sstevel@tonic-gate 	FILE *fp;
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	if (!have_identity)
3437c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
3447c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
3457c478bd9Sstevel@tonic-gate 		perror(identity_file);
3467c478bd9Sstevel@tonic-gate 		exit(1);
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate 	fp = fopen(identity_file, "r");
3497c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
3507c478bd9Sstevel@tonic-gate 		perror(identity_file);
3517c478bd9Sstevel@tonic-gate 		exit(1);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 	encoded[0] = '\0';
354442d23f4Sjp161948 	while ((blen = get_line(fp, line, sizeof(line))) != -1) {
355442d23f4Sjp161948 		if (line[blen - 1] == '\\')
3567c478bd9Sstevel@tonic-gate 			escaped++;
3577c478bd9Sstevel@tonic-gate 		if (strncmp(line, "----", 4) == 0 ||
3587c478bd9Sstevel@tonic-gate 		    strstr(line, ": ") != NULL) {
3597c478bd9Sstevel@tonic-gate 			if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
3607c478bd9Sstevel@tonic-gate 				private = 1;
3617c478bd9Sstevel@tonic-gate 			if (strstr(line, " END ") != NULL) {
3627c478bd9Sstevel@tonic-gate 				break;
3637c478bd9Sstevel@tonic-gate 			}
3647c478bd9Sstevel@tonic-gate 			/* fprintf(stderr, "ignore: %s", line); */
3657c478bd9Sstevel@tonic-gate 			continue;
3667c478bd9Sstevel@tonic-gate 		}
3677c478bd9Sstevel@tonic-gate 		if (escaped) {
3687c478bd9Sstevel@tonic-gate 			escaped--;
3697c478bd9Sstevel@tonic-gate 			/* fprintf(stderr, "escaped: %s", line); */
3707c478bd9Sstevel@tonic-gate 			continue;
3717c478bd9Sstevel@tonic-gate 		}
372442d23f4Sjp161948 		strlcat(encoded, line, sizeof(encoded));
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 	len = strlen(encoded);
3757c478bd9Sstevel@tonic-gate 	if (((len % 4) == 3) &&
3767c478bd9Sstevel@tonic-gate 	    (encoded[len-1] == '=') &&
3777c478bd9Sstevel@tonic-gate 	    (encoded[len-2] == '=') &&
3787c478bd9Sstevel@tonic-gate 	    (encoded[len-3] == '='))
3797c478bd9Sstevel@tonic-gate 		encoded[len-3] = '\0';
3807c478bd9Sstevel@tonic-gate 	blen = uudecode(encoded, blob, sizeof(blob));
3817c478bd9Sstevel@tonic-gate 	if (blen < 0) {
382442d23f4Sjp161948 		fprintf(stderr, gettext("uudecode failed.\n"));
3837c478bd9Sstevel@tonic-gate 		exit(1);
3847c478bd9Sstevel@tonic-gate 	}
3857c478bd9Sstevel@tonic-gate 	k = private ?
3867c478bd9Sstevel@tonic-gate 	    do_convert_private_ssh2_from_blob(blob, blen) :
3877c478bd9Sstevel@tonic-gate 	    key_from_blob(blob, blen);
3887c478bd9Sstevel@tonic-gate 	if (k == NULL) {
389442d23f4Sjp161948 		fprintf(stderr, gettext("decode blob failed.\n"));
3907c478bd9Sstevel@tonic-gate 		exit(1);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	ok = private ?
3937c478bd9Sstevel@tonic-gate 	    (k->type == KEY_DSA ?
3947c478bd9Sstevel@tonic-gate 		 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
3957c478bd9Sstevel@tonic-gate 		 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
3967c478bd9Sstevel@tonic-gate 	    key_write(k, stdout);
3977c478bd9Sstevel@tonic-gate 	if (!ok) {
398442d23f4Sjp161948 		fprintf(stderr, gettext("key write failed"));
3997c478bd9Sstevel@tonic-gate 		exit(1);
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate 	key_free(k);
4027c478bd9Sstevel@tonic-gate 	if (!private)
403442d23f4Sjp161948 		fprintf(stdout, "\n");
404442d23f4Sjp161948 	fclose(fp);
4057c478bd9Sstevel@tonic-gate 	exit(0);
4067c478bd9Sstevel@tonic-gate }
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate static void
do_print_public(struct passwd * pw)4097c478bd9Sstevel@tonic-gate do_print_public(struct passwd *pw)
4107c478bd9Sstevel@tonic-gate {
4117c478bd9Sstevel@tonic-gate 	Key *prv;
4127c478bd9Sstevel@tonic-gate 	struct stat st;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	if (!have_identity)
4157c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
4167c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
4177c478bd9Sstevel@tonic-gate 		perror(identity_file);
4187c478bd9Sstevel@tonic-gate 		exit(1);
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 	prv = load_identity(identity_file);
4217c478bd9Sstevel@tonic-gate 	if (prv == NULL) {
422442d23f4Sjp161948 		fprintf(stderr, gettext("load failed\n"));
4237c478bd9Sstevel@tonic-gate 		exit(1);
4247c478bd9Sstevel@tonic-gate 	}
4257c478bd9Sstevel@tonic-gate 	if (!key_write(prv, stdout))
426442d23f4Sjp161948 		fprintf(stderr, gettext("key_write failed"));
4277c478bd9Sstevel@tonic-gate 	key_free(prv);
428442d23f4Sjp161948 	fprintf(stdout, "\n");
4297c478bd9Sstevel@tonic-gate 	exit(0);
4307c478bd9Sstevel@tonic-gate }
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate static void
do_fingerprint(struct passwd * pw)4337c478bd9Sstevel@tonic-gate do_fingerprint(struct passwd *pw)
4347c478bd9Sstevel@tonic-gate {
4357c478bd9Sstevel@tonic-gate 	FILE *f;
4367c478bd9Sstevel@tonic-gate 	Key *public;
4377c478bd9Sstevel@tonic-gate 	char *comment = NULL, *cp, *ep, line[16*1024], *fp;
4387c478bd9Sstevel@tonic-gate 	int i, skip = 0, num = 1, invalid = 1;
4397c478bd9Sstevel@tonic-gate 	enum fp_rep rep;
4407c478bd9Sstevel@tonic-gate 	enum fp_type fptype;
4417c478bd9Sstevel@tonic-gate 	struct stat st;
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
4447c478bd9Sstevel@tonic-gate 	rep =    print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	if (!have_identity)
4477c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
4487c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
4497c478bd9Sstevel@tonic-gate 		perror(identity_file);
4507c478bd9Sstevel@tonic-gate 		exit(1);
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	public = key_load_public(identity_file, &comment);
4537c478bd9Sstevel@tonic-gate 	if (public != NULL) {
4547c478bd9Sstevel@tonic-gate 		fp = key_fingerprint(public, fptype, rep);
455442d23f4Sjp161948 		printf("%u %s %s\n", key_size(public), fp, comment);
4567c478bd9Sstevel@tonic-gate 		key_free(public);
4577c478bd9Sstevel@tonic-gate 		xfree(comment);
4587c478bd9Sstevel@tonic-gate 		xfree(fp);
4597c478bd9Sstevel@tonic-gate 		exit(0);
4607c478bd9Sstevel@tonic-gate 	}
461442d23f4Sjp161948 	if (comment) {
4627c478bd9Sstevel@tonic-gate 		xfree(comment);
463442d23f4Sjp161948 		comment = NULL;
464442d23f4Sjp161948 	}
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	f = fopen(identity_file, "r");
4677c478bd9Sstevel@tonic-gate 	if (f != NULL) {
4687c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof(line), f)) {
4697c478bd9Sstevel@tonic-gate 			i = strlen(line) - 1;
4707c478bd9Sstevel@tonic-gate 			if (line[i] != '\n') {
4717c478bd9Sstevel@tonic-gate 				error("line %d too long: %.40s...", num, line);
4727c478bd9Sstevel@tonic-gate 				skip = 1;
4737c478bd9Sstevel@tonic-gate 				continue;
4747c478bd9Sstevel@tonic-gate 			}
4757c478bd9Sstevel@tonic-gate 			num++;
4767c478bd9Sstevel@tonic-gate 			if (skip) {
4777c478bd9Sstevel@tonic-gate 				skip = 0;
4787c478bd9Sstevel@tonic-gate 				continue;
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 			line[i] = '\0';
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 			/* Skip leading whitespace, empty and comment lines. */
4837c478bd9Sstevel@tonic-gate 			for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
4847c478bd9Sstevel@tonic-gate 				;
4857c478bd9Sstevel@tonic-gate 			if (!*cp || *cp == '\n' || *cp == '#')
4867c478bd9Sstevel@tonic-gate 				continue;
4877c478bd9Sstevel@tonic-gate 			i = strtol(cp, &ep, 10);
4887c478bd9Sstevel@tonic-gate 			if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
4897c478bd9Sstevel@tonic-gate 				int quoted = 0;
4907c478bd9Sstevel@tonic-gate 				comment = cp;
4917c478bd9Sstevel@tonic-gate 				for (; *cp && (quoted || (*cp != ' ' &&
4927c478bd9Sstevel@tonic-gate 				    *cp != '\t')); cp++) {
4937c478bd9Sstevel@tonic-gate 					if (*cp == '\\' && cp[1] == '"')
4947c478bd9Sstevel@tonic-gate 						cp++;	/* Skip both */
4957c478bd9Sstevel@tonic-gate 					else if (*cp == '"')
4967c478bd9Sstevel@tonic-gate 						quoted = !quoted;
4977c478bd9Sstevel@tonic-gate 				}
4987c478bd9Sstevel@tonic-gate 				if (!*cp)
4997c478bd9Sstevel@tonic-gate 					continue;
5007c478bd9Sstevel@tonic-gate 				*cp++ = '\0';
5017c478bd9Sstevel@tonic-gate 			}
5027c478bd9Sstevel@tonic-gate 			ep = cp;
5037c478bd9Sstevel@tonic-gate 			public = key_new(KEY_RSA1);
5047c478bd9Sstevel@tonic-gate 			if (key_read(public, &cp) != 1) {
5057c478bd9Sstevel@tonic-gate 				cp = ep;
5067c478bd9Sstevel@tonic-gate 				key_free(public);
5077c478bd9Sstevel@tonic-gate 				public = key_new(KEY_UNSPEC);
5087c478bd9Sstevel@tonic-gate 				if (key_read(public, &cp) != 1) {
5097c478bd9Sstevel@tonic-gate 					key_free(public);
5107c478bd9Sstevel@tonic-gate 					continue;
5117c478bd9Sstevel@tonic-gate 				}
5127c478bd9Sstevel@tonic-gate 			}
5137c478bd9Sstevel@tonic-gate 			comment = *cp ? cp : comment;
5147c478bd9Sstevel@tonic-gate 			fp = key_fingerprint(public, fptype, rep);
515442d23f4Sjp161948 			printf("%u %s %s\n", key_size(public), fp,
5167c478bd9Sstevel@tonic-gate 			    comment ? comment : gettext("no comment"));
5177c478bd9Sstevel@tonic-gate 			xfree(fp);
5187c478bd9Sstevel@tonic-gate 			key_free(public);
5197c478bd9Sstevel@tonic-gate 			invalid = 0;
5207c478bd9Sstevel@tonic-gate 		}
521442d23f4Sjp161948 		fclose(f);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	if (invalid) {
524442d23f4Sjp161948 		printf(gettext("%s is not a public key file.\n"),
5257c478bd9Sstevel@tonic-gate 		       identity_file);
5267c478bd9Sstevel@tonic-gate 		exit(1);
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 	exit(0);
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate 
531442d23f4Sjp161948 static void
print_host(FILE * f,const char * name,Key * public,int hash)532442d23f4Sjp161948 print_host(FILE *f, const char *name, Key *public, int hash)
533442d23f4Sjp161948 {
534442d23f4Sjp161948 	if (hash && (name = host_hash(name, NULL, 0)) == NULL)
535442d23f4Sjp161948 		fatal("hash_host failed");
536442d23f4Sjp161948 	fprintf(f, "%s ", name);
537442d23f4Sjp161948 	if (!key_write(public, f))
538442d23f4Sjp161948 		fatal("key_write failed");
539442d23f4Sjp161948 	fprintf(f, "\n");
540442d23f4Sjp161948 }
541442d23f4Sjp161948 
542442d23f4Sjp161948 static void
do_known_hosts(struct passwd * pw,const char * name)543442d23f4Sjp161948 do_known_hosts(struct passwd *pw, const char *name)
544442d23f4Sjp161948 {
545442d23f4Sjp161948 	FILE *in, *out = stdout;
546442d23f4Sjp161948 	Key *public;
547442d23f4Sjp161948 	char *cp, *cp2, *kp, *kp2;
548442d23f4Sjp161948 	char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
549442d23f4Sjp161948 	int c, i, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
550442d23f4Sjp161948 
551442d23f4Sjp161948 	if (!have_identity) {
552442d23f4Sjp161948 		cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
553442d23f4Sjp161948 		if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
554442d23f4Sjp161948 		    sizeof(identity_file))
555442d23f4Sjp161948 			fatal("Specified known hosts path too long");
556442d23f4Sjp161948 		xfree(cp);
557442d23f4Sjp161948 		have_identity = 1;
558442d23f4Sjp161948 	}
559442d23f4Sjp161948 	if ((in = fopen(identity_file, "r")) == NULL)
560442d23f4Sjp161948 		fatal("fopen: %s", strerror(errno));
561442d23f4Sjp161948 
562442d23f4Sjp161948 	/*
563442d23f4Sjp161948 	 * Find hosts goes to stdout, hash and deletions happen in-place
564442d23f4Sjp161948 	 * A corner case is ssh-keygen -HF foo, which should go to stdout
565442d23f4Sjp161948 	 */
566442d23f4Sjp161948 	if (!find_host && (hash_hosts || delete_host)) {
567442d23f4Sjp161948 		if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
568442d23f4Sjp161948 		    strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
569442d23f4Sjp161948 		    strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
570442d23f4Sjp161948 		    strlcat(old, ".old", sizeof(old)) >= sizeof(old))
571442d23f4Sjp161948 			fatal("known_hosts path too long");
572442d23f4Sjp161948 		umask(077);
573442d23f4Sjp161948 		if ((c = mkstemp(tmp)) == -1)
574442d23f4Sjp161948 			fatal("mkstemp: %s", strerror(errno));
575442d23f4Sjp161948 		if ((out = fdopen(c, "w")) == NULL) {
576442d23f4Sjp161948 			c = errno;
577442d23f4Sjp161948 			unlink(tmp);
578442d23f4Sjp161948 			fatal("fdopen: %s", strerror(c));
579442d23f4Sjp161948 		}
580442d23f4Sjp161948 		inplace = 1;
581442d23f4Sjp161948 	}
582442d23f4Sjp161948 
583442d23f4Sjp161948 	while (fgets(line, sizeof(line), in)) {
584442d23f4Sjp161948 		num++;
585442d23f4Sjp161948 		i = strlen(line) - 1;
586442d23f4Sjp161948 		if (line[i] != '\n') {
587442d23f4Sjp161948 			error("line %d too long: %.40s...", num, line);
588442d23f4Sjp161948 			skip = 1;
589442d23f4Sjp161948 			invalid = 1;
590442d23f4Sjp161948 			continue;
591442d23f4Sjp161948 		}
592442d23f4Sjp161948 		if (skip) {
593442d23f4Sjp161948 			skip = 0;
594442d23f4Sjp161948 			continue;
595442d23f4Sjp161948 		}
596442d23f4Sjp161948 		line[i] = '\0';
597442d23f4Sjp161948 
598442d23f4Sjp161948 		/* Skip leading whitespace, empty and comment lines. */
599442d23f4Sjp161948 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
600442d23f4Sjp161948 			;
601442d23f4Sjp161948 		if (!*cp || *cp == '\n' || *cp == '#') {
602442d23f4Sjp161948 			if (inplace)
603442d23f4Sjp161948 				fprintf(out, "%s\n", cp);
604442d23f4Sjp161948 			continue;
605442d23f4Sjp161948 		}
606442d23f4Sjp161948 		/* Find the end of the host name portion. */
607442d23f4Sjp161948 		for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
608442d23f4Sjp161948 			;
609442d23f4Sjp161948 		if (*kp == '\0' || *(kp + 1) == '\0') {
610442d23f4Sjp161948 			error("line %d missing key: %.40s...",
611442d23f4Sjp161948 			    num, line);
612442d23f4Sjp161948 			invalid = 1;
613442d23f4Sjp161948 			continue;
614442d23f4Sjp161948 		}
615442d23f4Sjp161948 		*kp++ = '\0';
616442d23f4Sjp161948 		kp2 = kp;
617442d23f4Sjp161948 
618442d23f4Sjp161948 		public = key_new(KEY_RSA1);
619442d23f4Sjp161948 		if (key_read(public, &kp) != 1) {
620442d23f4Sjp161948 			kp = kp2;
621442d23f4Sjp161948 			key_free(public);
622442d23f4Sjp161948 			public = key_new(KEY_UNSPEC);
623442d23f4Sjp161948 			if (key_read(public, &kp) != 1) {
624442d23f4Sjp161948 				error("line %d invalid key: %.40s...",
625442d23f4Sjp161948 				    num, line);
626442d23f4Sjp161948 				key_free(public);
627442d23f4Sjp161948 				invalid = 1;
628442d23f4Sjp161948 				continue;
629442d23f4Sjp161948 			}
630442d23f4Sjp161948 		}
631442d23f4Sjp161948 
632442d23f4Sjp161948 		if (*cp == HASH_DELIM) {
633442d23f4Sjp161948 			if (find_host || delete_host) {
634442d23f4Sjp161948 				cp2 = host_hash(name, cp, strlen(cp));
635442d23f4Sjp161948 				if (cp2 == NULL) {
636442d23f4Sjp161948 					error("line %d: invalid hashed "
637442d23f4Sjp161948 					    "name: %.64s...", num, line);
638442d23f4Sjp161948 					invalid = 1;
639442d23f4Sjp161948 					continue;
640442d23f4Sjp161948 				}
641442d23f4Sjp161948 				c = (strcmp(cp2, cp) == 0);
642442d23f4Sjp161948 				if (find_host && c) {
643442d23f4Sjp161948 					printf(gettext("# Host %s found: "
644442d23f4Sjp161948 					    "line %d type %s\n"), name,
645442d23f4Sjp161948 					    num, key_type(public));
646442d23f4Sjp161948 					print_host(out, cp, public, 0);
647442d23f4Sjp161948 				}
648442d23f4Sjp161948 				if (delete_host && !c)
649442d23f4Sjp161948 					print_host(out, cp, public, 0);
650442d23f4Sjp161948 			} else if (hash_hosts)
651442d23f4Sjp161948 				print_host(out, cp, public, 0);
652442d23f4Sjp161948 		} else {
653442d23f4Sjp161948 			if (find_host || delete_host) {
654442d23f4Sjp161948 				c = (match_hostname(name, cp,
655442d23f4Sjp161948 				    strlen(cp)) == 1);
656442d23f4Sjp161948 				if (find_host && c) {
657442d23f4Sjp161948 					printf(gettext("# Host %s found: "
658442d23f4Sjp161948 					    "line %d type %s\n"), name,
659442d23f4Sjp161948 					    num, key_type(public));
660442d23f4Sjp161948 					print_host(out, name, public, hash_hosts);
661442d23f4Sjp161948 				}
662442d23f4Sjp161948 				if (delete_host && !c)
663442d23f4Sjp161948 					print_host(out, cp, public, 0);
664442d23f4Sjp161948 			} else if (hash_hosts) {
665442d23f4Sjp161948 				for (cp2 = strsep(&cp, ",");
666442d23f4Sjp161948 				    cp2 != NULL && *cp2 != '\0';
667442d23f4Sjp161948 				    cp2 = strsep(&cp, ",")) {
668442d23f4Sjp161948 					if (strcspn(cp2, "*?!") != strlen(cp2))
669442d23f4Sjp161948 						fprintf(stderr, gettext("Warning: "
670442d23f4Sjp161948 						   "ignoring host name with "
671442d23f4Sjp161948 						   "metacharacters: %.64s\n"),
672442d23f4Sjp161948 						    cp2);
673442d23f4Sjp161948 					else
674442d23f4Sjp161948 						print_host(out, cp2, public, 1);
675442d23f4Sjp161948 				}
676442d23f4Sjp161948 				has_unhashed = 1;
677442d23f4Sjp161948 			}
678442d23f4Sjp161948 		}
679442d23f4Sjp161948 		key_free(public);
680442d23f4Sjp161948 	}
681442d23f4Sjp161948 	fclose(in);
682442d23f4Sjp161948 
683442d23f4Sjp161948 	if (invalid) {
684442d23f4Sjp161948 		fprintf(stderr, gettext("%s is not a valid known_host file.\n"),
685442d23f4Sjp161948 		    identity_file);
686442d23f4Sjp161948 		if (inplace) {
687442d23f4Sjp161948 			fprintf(stderr, gettext("Not replacing existing known_hosts "
688442d23f4Sjp161948 			   "file because of errors\n"));
689442d23f4Sjp161948 			fclose(out);
690442d23f4Sjp161948 			unlink(tmp);
691442d23f4Sjp161948 		}
692442d23f4Sjp161948 		exit(1);
693442d23f4Sjp161948 	}
694442d23f4Sjp161948 
695442d23f4Sjp161948 	if (inplace) {
696442d23f4Sjp161948 		fclose(out);
697442d23f4Sjp161948 
698442d23f4Sjp161948 		/* Backup existing file */
699442d23f4Sjp161948 		if (unlink(old) == -1 && errno != ENOENT)
700442d23f4Sjp161948 			fatal("unlink %.100s: %s", old, strerror(errno));
701442d23f4Sjp161948 		if (link(identity_file, old) == -1)
702442d23f4Sjp161948 			fatal("link %.100s to %.100s: %s", identity_file, old,
703442d23f4Sjp161948 			    strerror(errno));
704442d23f4Sjp161948 		/* Move new one into place */
705442d23f4Sjp161948 		if (rename(tmp, identity_file) == -1) {
706442d23f4Sjp161948 			error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
707442d23f4Sjp161948 			    strerror(errno));
708442d23f4Sjp161948 			unlink(tmp);
709442d23f4Sjp161948 			unlink(old);
710442d23f4Sjp161948 			exit(1);
711442d23f4Sjp161948 		}
712442d23f4Sjp161948 
713442d23f4Sjp161948 		fprintf(stderr, gettext("%s updated.\n"), identity_file);
714442d23f4Sjp161948 		fprintf(stderr, gettext("Original contents retained as %s\n"), old);
715442d23f4Sjp161948 		if (has_unhashed) {
716442d23f4Sjp161948 			fprintf(stderr, gettext("WARNING: %s contains unhashed "
717442d23f4Sjp161948 			    "entries\n"), old);
718442d23f4Sjp161948 			fprintf(stderr, gettext("Delete this file to ensure privacy "
719442d23f4Sjp161948 			    "of hostnames\n"));
720442d23f4Sjp161948 		}
721442d23f4Sjp161948 	}
722442d23f4Sjp161948 
723442d23f4Sjp161948 	exit(0);
724442d23f4Sjp161948 }
725442d23f4Sjp161948 
7267c478bd9Sstevel@tonic-gate /*
7277c478bd9Sstevel@tonic-gate  * Perform changing a passphrase.  The argument is the passwd structure
7287c478bd9Sstevel@tonic-gate  * for the current user.
7297c478bd9Sstevel@tonic-gate  */
7307c478bd9Sstevel@tonic-gate static void
do_change_passphrase(struct passwd * pw)7317c478bd9Sstevel@tonic-gate do_change_passphrase(struct passwd *pw)
7327c478bd9Sstevel@tonic-gate {
7337c478bd9Sstevel@tonic-gate 	char *comment;
7347c478bd9Sstevel@tonic-gate 	char *old_passphrase, *passphrase1, *passphrase2;
7357c478bd9Sstevel@tonic-gate 	struct stat st;
7367c478bd9Sstevel@tonic-gate 	Key *private;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 	if (!have_identity)
7397c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
7407c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
7417c478bd9Sstevel@tonic-gate 		perror(identity_file);
7427c478bd9Sstevel@tonic-gate 		exit(1);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 	/* Try to load the file with empty passphrase. */
7457c478bd9Sstevel@tonic-gate 	private = key_load_private(identity_file, "", &comment);
7467c478bd9Sstevel@tonic-gate 	if (private == NULL) {
7477c478bd9Sstevel@tonic-gate 		if (identity_passphrase)
7487c478bd9Sstevel@tonic-gate 			old_passphrase = xstrdup(identity_passphrase);
7497c478bd9Sstevel@tonic-gate 		else
7507c478bd9Sstevel@tonic-gate 			old_passphrase =
7517c478bd9Sstevel@tonic-gate 			    read_passphrase(gettext("Enter old passphrase: "),
7527c478bd9Sstevel@tonic-gate 			    RP_ALLOW_STDIN);
7537c478bd9Sstevel@tonic-gate 		private = key_load_private(identity_file, old_passphrase,
7547c478bd9Sstevel@tonic-gate 		    &comment);
755442d23f4Sjp161948 		memset(old_passphrase, 0, strlen(old_passphrase));
7567c478bd9Sstevel@tonic-gate 		xfree(old_passphrase);
7577c478bd9Sstevel@tonic-gate 		if (private == NULL) {
758442d23f4Sjp161948 			printf(gettext("Bad passphrase.\n"));
7597c478bd9Sstevel@tonic-gate 			exit(1);
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 	}
762442d23f4Sjp161948 	printf(gettext("Key has comment '%s'\n"), comment);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/* Ask the new passphrase (twice). */
7657c478bd9Sstevel@tonic-gate 	if (identity_new_passphrase) {
7667c478bd9Sstevel@tonic-gate 		passphrase1 = xstrdup(identity_new_passphrase);
7677c478bd9Sstevel@tonic-gate 		passphrase2 = NULL;
7687c478bd9Sstevel@tonic-gate 	} else {
7697c478bd9Sstevel@tonic-gate 		passphrase1 =
7707c478bd9Sstevel@tonic-gate 			read_passphrase(gettext("Enter new passphrase (empty"
7717c478bd9Sstevel@tonic-gate 			    " for no passphrase): "), RP_ALLOW_STDIN);
7727c478bd9Sstevel@tonic-gate 		passphrase2 = read_passphrase(gettext("Enter same "
7737c478bd9Sstevel@tonic-gate 			    "passphrase again: "), RP_ALLOW_STDIN);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 		/* Verify that they are the same. */
7767c478bd9Sstevel@tonic-gate 		if (strcmp(passphrase1, passphrase2) != 0) {
777442d23f4Sjp161948 			memset(passphrase1, 0, strlen(passphrase1));
778442d23f4Sjp161948 			memset(passphrase2, 0, strlen(passphrase2));
7797c478bd9Sstevel@tonic-gate 			xfree(passphrase1);
7807c478bd9Sstevel@tonic-gate 			xfree(passphrase2);
781442d23f4Sjp161948 			printf(gettext("Pass phrases do not match.  Try "
7827c478bd9Sstevel@tonic-gate 			    "again.\n"));
7837c478bd9Sstevel@tonic-gate 			exit(1);
7847c478bd9Sstevel@tonic-gate 		}
7857c478bd9Sstevel@tonic-gate 		/* Destroy the other copy. */
786442d23f4Sjp161948 		memset(passphrase2, 0, strlen(passphrase2));
7877c478bd9Sstevel@tonic-gate 		xfree(passphrase2);
7887c478bd9Sstevel@tonic-gate 	}
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate 	/* Save the file using the new passphrase. */
7917c478bd9Sstevel@tonic-gate 	if (!key_save_private(private, identity_file, passphrase1, comment)) {
792442d23f4Sjp161948 		printf(gettext("Saving the key failed: %s.\n"), identity_file);
793442d23f4Sjp161948 		memset(passphrase1, 0, strlen(passphrase1));
7947c478bd9Sstevel@tonic-gate 		xfree(passphrase1);
7957c478bd9Sstevel@tonic-gate 		key_free(private);
7967c478bd9Sstevel@tonic-gate 		xfree(comment);
7977c478bd9Sstevel@tonic-gate 		exit(1);
7987c478bd9Sstevel@tonic-gate 	}
7997c478bd9Sstevel@tonic-gate 	/* Destroy the passphrase and the copy of the key in memory. */
800442d23f4Sjp161948 	memset(passphrase1, 0, strlen(passphrase1));
8017c478bd9Sstevel@tonic-gate 	xfree(passphrase1);
8027c478bd9Sstevel@tonic-gate 	key_free(private);		 /* Destroys contents */
8037c478bd9Sstevel@tonic-gate 	xfree(comment);
8047c478bd9Sstevel@tonic-gate 
805442d23f4Sjp161948 	printf(gettext("Your identification has been saved with the new "
8067c478bd9Sstevel@tonic-gate 	    "passphrase.\n"));
8077c478bd9Sstevel@tonic-gate 	exit(0);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * Change the comment of a private key file.
8127c478bd9Sstevel@tonic-gate  */
8137c478bd9Sstevel@tonic-gate static void
do_change_comment(struct passwd * pw)8147c478bd9Sstevel@tonic-gate do_change_comment(struct passwd *pw)
8157c478bd9Sstevel@tonic-gate {
8167c478bd9Sstevel@tonic-gate 	char new_comment[1024], *comment, *passphrase;
8177c478bd9Sstevel@tonic-gate 	Key *private;
8187c478bd9Sstevel@tonic-gate 	Key *public;
8197c478bd9Sstevel@tonic-gate 	struct stat st;
8207c478bd9Sstevel@tonic-gate 	FILE *f;
8217c478bd9Sstevel@tonic-gate 	int fd;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	if (!have_identity)
8247c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
8257c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
8267c478bd9Sstevel@tonic-gate 		perror(identity_file);
8277c478bd9Sstevel@tonic-gate 		exit(1);
8287c478bd9Sstevel@tonic-gate 	}
8297c478bd9Sstevel@tonic-gate 	private = key_load_private(identity_file, "", &comment);
8307c478bd9Sstevel@tonic-gate 	if (private == NULL) {
8317c478bd9Sstevel@tonic-gate 		if (identity_passphrase)
8327c478bd9Sstevel@tonic-gate 			passphrase = xstrdup(identity_passphrase);
8337c478bd9Sstevel@tonic-gate 		else if (identity_new_passphrase)
8347c478bd9Sstevel@tonic-gate 			passphrase = xstrdup(identity_new_passphrase);
8357c478bd9Sstevel@tonic-gate 		else
8367c478bd9Sstevel@tonic-gate 			passphrase =
8377c478bd9Sstevel@tonic-gate 			    read_passphrase(gettext("Enter passphrase: "),
8387c478bd9Sstevel@tonic-gate 			    RP_ALLOW_STDIN);
8397c478bd9Sstevel@tonic-gate 		/* Try to load using the passphrase. */
8407c478bd9Sstevel@tonic-gate 		private = key_load_private(identity_file, passphrase, &comment);
8417c478bd9Sstevel@tonic-gate 		if (private == NULL) {
842442d23f4Sjp161948 			memset(passphrase, 0, strlen(passphrase));
8437c478bd9Sstevel@tonic-gate 			xfree(passphrase);
844442d23f4Sjp161948 			printf(gettext("Bad passphrase.\n"));
8457c478bd9Sstevel@tonic-gate 			exit(1);
8467c478bd9Sstevel@tonic-gate 		}
8477c478bd9Sstevel@tonic-gate 	} else {
8487c478bd9Sstevel@tonic-gate 		passphrase = xstrdup("");
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 	if (private->type != KEY_RSA1) {
851442d23f4Sjp161948 		fprintf(stderr, gettext("Comments are only supported for "
8527c478bd9Sstevel@tonic-gate 		    "RSA1 keys.\n"));
8537c478bd9Sstevel@tonic-gate 		key_free(private);
8547c478bd9Sstevel@tonic-gate 		exit(1);
8557c478bd9Sstevel@tonic-gate 	}
856442d23f4Sjp161948 	printf(gettext("Key now has comment '%s'\n"), comment);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	if (identity_comment) {
859442d23f4Sjp161948 		strlcpy(new_comment, identity_comment, sizeof(new_comment));
8607c478bd9Sstevel@tonic-gate 	} else {
861442d23f4Sjp161948 		printf(gettext("Enter new comment: "));
862442d23f4Sjp161948 		fflush(stdout);
8637c478bd9Sstevel@tonic-gate 		if (!fgets(new_comment, sizeof(new_comment), stdin)) {
864442d23f4Sjp161948 			memset(passphrase, 0, strlen(passphrase));
8657c478bd9Sstevel@tonic-gate 			key_free(private);
8667c478bd9Sstevel@tonic-gate 			exit(1);
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 		if (strchr(new_comment, '\n'))
8697c478bd9Sstevel@tonic-gate 			*strchr(new_comment, '\n') = 0;
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate 	/* Save the file using the new passphrase. */
8737c478bd9Sstevel@tonic-gate 	if (!key_save_private(private, identity_file, passphrase, new_comment)) {
874442d23f4Sjp161948 		printf(gettext("Saving the key failed: %s.\n"), identity_file);
875442d23f4Sjp161948 		memset(passphrase, 0, strlen(passphrase));
8767c478bd9Sstevel@tonic-gate 		xfree(passphrase);
8777c478bd9Sstevel@tonic-gate 		key_free(private);
8787c478bd9Sstevel@tonic-gate 		xfree(comment);
8797c478bd9Sstevel@tonic-gate 		exit(1);
8807c478bd9Sstevel@tonic-gate 	}
881442d23f4Sjp161948 	memset(passphrase, 0, strlen(passphrase));
8827c478bd9Sstevel@tonic-gate 	xfree(passphrase);
8837c478bd9Sstevel@tonic-gate 	public = key_from_private(private);
8847c478bd9Sstevel@tonic-gate 	key_free(private);
8857c478bd9Sstevel@tonic-gate 
886442d23f4Sjp161948 	strlcat(identity_file, ".pub", sizeof(identity_file));
8877c478bd9Sstevel@tonic-gate 	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
8887c478bd9Sstevel@tonic-gate 	if (fd == -1) {
889442d23f4Sjp161948 		printf(gettext("Could not save your public key in %s\n"),
8907c478bd9Sstevel@tonic-gate 		    identity_file);
8917c478bd9Sstevel@tonic-gate 		exit(1);
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate 	f = fdopen(fd, "w");
8947c478bd9Sstevel@tonic-gate 	if (f == NULL) {
895442d23f4Sjp161948 		printf(gettext("fdopen %s failed"), identity_file);
8967c478bd9Sstevel@tonic-gate 		exit(1);
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 	if (!key_write(public, f))
899442d23f4Sjp161948 		fprintf(stderr, gettext("write key failed"));
9007c478bd9Sstevel@tonic-gate 	key_free(public);
901442d23f4Sjp161948 	fprintf(f, " %s\n", new_comment);
902442d23f4Sjp161948 	fclose(f);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	xfree(comment);
9057c478bd9Sstevel@tonic-gate 
906442d23f4Sjp161948 	printf(gettext("The comment in your key file has been changed.\n"));
9077c478bd9Sstevel@tonic-gate 	exit(0);
9087c478bd9Sstevel@tonic-gate }
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate static void
usage(void)9117c478bd9Sstevel@tonic-gate usage(void)
9127c478bd9Sstevel@tonic-gate {
913442d23f4Sjp161948 	fprintf(stderr, gettext(
9147c478bd9Sstevel@tonic-gate 	"Usage: %s [options]\n"
9157c478bd9Sstevel@tonic-gate 	"Options:\n"
9167c478bd9Sstevel@tonic-gate 	"  -b bits     Number of bits in the key to create.\n"
9177c478bd9Sstevel@tonic-gate 	"  -B          Show bubblebabble digest of key file.\n"
918442d23f4Sjp161948 	"  -c          Change comment in private and public key files.\n"
9197c478bd9Sstevel@tonic-gate 	"  -C comment  Provide new comment.\n"
920442d23f4Sjp161948 	"  -e          Convert OpenSSH to IETF SECSH key file.\n"
921442d23f4Sjp161948 	"  -f filename Filename of the key file.\n"
922442d23f4Sjp161948 	"  -F hostname Find hostname in known hosts file.\n"
923442d23f4Sjp161948 	"  -H          Hash names in known_hosts file.\n"
924442d23f4Sjp161948 	"  -i          Convert IETF SECSH to OpenSSH key file.\n"
925442d23f4Sjp161948 	"  -l          Show fingerprint of key file.\n"
926442d23f4Sjp161948 	"  -N phrase   Provide new passphrase.\n"
927442d23f4Sjp161948 	"  -p          Change passphrase of private key file.\n"
928442d23f4Sjp161948 	"  -P phrase   Provide old passphrase.\n"
929442d23f4Sjp161948 	"  -q          Quiet.\n"
930442d23f4Sjp161948 	"  -R hostname Remove host from known_hosts file.\n"
931442d23f4Sjp161948 	"  -t type     Specify type of key to create.\n"
932442d23f4Sjp161948 	"  -y          Read private key file and print public key.\n"
9337c478bd9Sstevel@tonic-gate 	), __progname);
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	exit(1);
9367c478bd9Sstevel@tonic-gate }
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate /*
9397c478bd9Sstevel@tonic-gate  * Main program for key management.
9407c478bd9Sstevel@tonic-gate  */
9417c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)942442d23f4Sjp161948 main(int argc, char **argv)
9437c478bd9Sstevel@tonic-gate {
9447c478bd9Sstevel@tonic-gate 	char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
945442d23f4Sjp161948 	char *rr_hostname = NULL;
9467c478bd9Sstevel@tonic-gate 	Key *private, *public;
9477c478bd9Sstevel@tonic-gate 	struct passwd *pw;
9487c478bd9Sstevel@tonic-gate 	struct stat st;
9497c478bd9Sstevel@tonic-gate 	int opt, type, fd;
9507c478bd9Sstevel@tonic-gate 	FILE *f;
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 	extern int optind;
9537c478bd9Sstevel@tonic-gate 	extern char *optarg;
9547c478bd9Sstevel@tonic-gate 
955442d23f4Sjp161948 	/* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
956442d23f4Sjp161948 	sanitise_stdfd();
9577c478bd9Sstevel@tonic-gate 
958442d23f4Sjp161948 	__progname = get_progname(argv[0]);
959442d23f4Sjp161948 
960442d23f4Sjp161948 	g11n_setlocale(LC_ALL, "");
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	SSLeay_add_all_algorithms();
9637c478bd9Sstevel@tonic-gate 	init_rng();
9647c478bd9Sstevel@tonic-gate 	seed_rng();
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	/* we need this for the home * directory.  */
9677c478bd9Sstevel@tonic-gate 	pw = getpwuid(getuid());
9687c478bd9Sstevel@tonic-gate 	if (!pw) {
969442d23f4Sjp161948 		printf(gettext("You don't exist, go away!\n"));
9707c478bd9Sstevel@tonic-gate 		exit(1);
9717c478bd9Sstevel@tonic-gate 	}
9727c478bd9Sstevel@tonic-gate 	if (gethostname(hostname, sizeof(hostname)) < 0) {
9737c478bd9Sstevel@tonic-gate 		perror("gethostname");
9747c478bd9Sstevel@tonic-gate 		exit(1);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
977442d23f4Sjp161948 #define GETOPT_ARGS "BcdeHilpqxXyb:C:f:F:N:P:R:t:"
978ea01bd62SHuie-Ying Lee 
979442d23f4Sjp161948 	while ((opt = getopt(argc, argv, GETOPT_ARGS)) != -1) {
9807c478bd9Sstevel@tonic-gate 		switch (opt) {
9817c478bd9Sstevel@tonic-gate 		case 'b':
9827c478bd9Sstevel@tonic-gate 			bits = atoi(optarg);
9837c478bd9Sstevel@tonic-gate 			if (bits < 512 || bits > 32768) {
984442d23f4Sjp161948 				printf(gettext("Bits has bad value.\n"));
9857c478bd9Sstevel@tonic-gate 				exit(1);
9867c478bd9Sstevel@tonic-gate 			}
9877c478bd9Sstevel@tonic-gate 			break;
988442d23f4Sjp161948 		case 'F':
989442d23f4Sjp161948 			find_host = 1;
990442d23f4Sjp161948 			rr_hostname = optarg;
991442d23f4Sjp161948 			break;
992442d23f4Sjp161948 		case 'H':
993442d23f4Sjp161948 			hash_hosts = 1;
994442d23f4Sjp161948 			break;
995442d23f4Sjp161948 		case 'R':
996442d23f4Sjp161948 			delete_host = 1;
997442d23f4Sjp161948 			rr_hostname = optarg;
998442d23f4Sjp161948 			break;
9997c478bd9Sstevel@tonic-gate 		case 'l':
10007c478bd9Sstevel@tonic-gate 			print_fingerprint = 1;
10017c478bd9Sstevel@tonic-gate 			break;
10027c478bd9Sstevel@tonic-gate 		case 'B':
10037c478bd9Sstevel@tonic-gate 			print_bubblebabble = 1;
10047c478bd9Sstevel@tonic-gate 			break;
10057c478bd9Sstevel@tonic-gate 		case 'p':
10067c478bd9Sstevel@tonic-gate 			change_passphrase = 1;
10077c478bd9Sstevel@tonic-gate 			break;
10087c478bd9Sstevel@tonic-gate 		case 'c':
10097c478bd9Sstevel@tonic-gate 			change_comment = 1;
10107c478bd9Sstevel@tonic-gate 			break;
10117c478bd9Sstevel@tonic-gate 		case 'f':
1012442d23f4Sjp161948 			strlcpy(identity_file, optarg, sizeof(identity_file));
10137c478bd9Sstevel@tonic-gate 			have_identity = 1;
10147c478bd9Sstevel@tonic-gate 			break;
10157c478bd9Sstevel@tonic-gate 		case 'P':
10167c478bd9Sstevel@tonic-gate 			identity_passphrase = optarg;
10177c478bd9Sstevel@tonic-gate 			break;
10187c478bd9Sstevel@tonic-gate 		case 'N':
10197c478bd9Sstevel@tonic-gate 			identity_new_passphrase = optarg;
10207c478bd9Sstevel@tonic-gate 			break;
10217c478bd9Sstevel@tonic-gate 		case 'C':
10227c478bd9Sstevel@tonic-gate 			identity_comment = optarg;
10237c478bd9Sstevel@tonic-gate 			break;
10247c478bd9Sstevel@tonic-gate 		case 'q':
10257c478bd9Sstevel@tonic-gate 			quiet = 1;
10267c478bd9Sstevel@tonic-gate 			break;
10277c478bd9Sstevel@tonic-gate 		case 'e':
10287c478bd9Sstevel@tonic-gate 		case 'x':
10297c478bd9Sstevel@tonic-gate 			/* export key */
10307c478bd9Sstevel@tonic-gate 			convert_to_ssh2 = 1;
10317c478bd9Sstevel@tonic-gate 			break;
10327c478bd9Sstevel@tonic-gate 		case 'i':
10337c478bd9Sstevel@tonic-gate 		case 'X':
10347c478bd9Sstevel@tonic-gate 			/* import key */
10357c478bd9Sstevel@tonic-gate 			convert_from_ssh2 = 1;
10367c478bd9Sstevel@tonic-gate 			break;
10377c478bd9Sstevel@tonic-gate 		case 'y':
10387c478bd9Sstevel@tonic-gate 			print_public = 1;
10397c478bd9Sstevel@tonic-gate 			break;
10407c478bd9Sstevel@tonic-gate 		case 'd':
10417c478bd9Sstevel@tonic-gate 			key_type_name = "dsa";
10427c478bd9Sstevel@tonic-gate 			break;
10437c478bd9Sstevel@tonic-gate 		case 't':
10447c478bd9Sstevel@tonic-gate 			key_type_name = optarg;
10457c478bd9Sstevel@tonic-gate 			break;
10467c478bd9Sstevel@tonic-gate 		case '?':
10477c478bd9Sstevel@tonic-gate 		default:
10487c478bd9Sstevel@tonic-gate 			usage();
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 	}
1051442d23f4Sjp161948 	if (optind < argc) {
1052442d23f4Sjp161948 		printf(gettext("Too many arguments.\n"));
10537c478bd9Sstevel@tonic-gate 		usage();
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 	if (change_passphrase && change_comment) {
1056442d23f4Sjp161948 		printf(gettext("Can only have one of -p and -c.\n"));
10577c478bd9Sstevel@tonic-gate 		usage();
10587c478bd9Sstevel@tonic-gate 	}
1059442d23f4Sjp161948 	if (delete_host || hash_hosts || find_host)
1060442d23f4Sjp161948 		do_known_hosts(pw, rr_hostname);
10617c478bd9Sstevel@tonic-gate 	if (print_fingerprint || print_bubblebabble)
10627c478bd9Sstevel@tonic-gate 		do_fingerprint(pw);
10637c478bd9Sstevel@tonic-gate 	if (change_passphrase)
10647c478bd9Sstevel@tonic-gate 		do_change_passphrase(pw);
10657c478bd9Sstevel@tonic-gate 	if (change_comment)
10667c478bd9Sstevel@tonic-gate 		do_change_comment(pw);
10677c478bd9Sstevel@tonic-gate 	if (convert_to_ssh2)
10687c478bd9Sstevel@tonic-gate 		do_convert_to_ssh2(pw);
10697c478bd9Sstevel@tonic-gate 	if (convert_from_ssh2)
10707c478bd9Sstevel@tonic-gate 		do_convert_from_ssh2(pw);
10717c478bd9Sstevel@tonic-gate 	if (print_public)
10727c478bd9Sstevel@tonic-gate 		do_print_public(pw);
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	arc4random_stir();
10757c478bd9Sstevel@tonic-gate 
10767c478bd9Sstevel@tonic-gate 	if (key_type_name == NULL) {
1077442d23f4Sjp161948 		printf(gettext("You must specify a key type (-t).\n"));
10787c478bd9Sstevel@tonic-gate 		usage();
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 	type = key_type_from_name(key_type_name);
10817c478bd9Sstevel@tonic-gate 	if (type == KEY_UNSPEC) {
1082442d23f4Sjp161948 		fprintf(stderr, gettext("unknown key type %s\n"),
10837c478bd9Sstevel@tonic-gate 		    key_type_name);
10847c478bd9Sstevel@tonic-gate 		exit(1);
10857c478bd9Sstevel@tonic-gate 	}
1086*e7b3b5cfSHai-May Chao 	if (bits == 0)
1087*e7b3b5cfSHai-May Chao 		bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS_RSA;
1088*e7b3b5cfSHai-May Chao 
10897c478bd9Sstevel@tonic-gate 	if (!quiet)
1090442d23f4Sjp161948 		printf(gettext("Generating public/private %s key pair.\n"),
10917c478bd9Sstevel@tonic-gate 		    key_type_name);
10927c478bd9Sstevel@tonic-gate 	private = key_generate(type, bits);
10937c478bd9Sstevel@tonic-gate 	if (private == NULL) {
1094442d23f4Sjp161948 		fprintf(stderr, gettext("key_generate failed"));
10957c478bd9Sstevel@tonic-gate 		exit(1);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 	public  = key_from_private(private);
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 	if (!have_identity)
11007c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which to save the key"));
11017c478bd9Sstevel@tonic-gate 
1102442d23f4Sjp161948 	/* Create ~/.ssh directory if it doesn't already exist. */
1103442d23f4Sjp161948 	snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
11047c478bd9Sstevel@tonic-gate 	if (strstr(identity_file, dotsshdir) != NULL &&
11057c478bd9Sstevel@tonic-gate 	    stat(dotsshdir, &st) < 0) {
11067c478bd9Sstevel@tonic-gate 		if (mkdir(dotsshdir, 0700) < 0)
11077c478bd9Sstevel@tonic-gate 			error("Could not create directory '%s'.", dotsshdir);
11087c478bd9Sstevel@tonic-gate 		else if (!quiet)
1109442d23f4Sjp161948 			printf(gettext("Created directory '%s'.\n"), dotsshdir);
11107c478bd9Sstevel@tonic-gate 	}
11117c478bd9Sstevel@tonic-gate 	/* If the file already exists, ask the user to confirm. */
11127c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) >= 0) {
11137c478bd9Sstevel@tonic-gate 		char yesno[128];
1114442d23f4Sjp161948 		printf(gettext("%s already exists.\n"), identity_file);
1115442d23f4Sjp161948 		printf(gettext("Overwrite (%s/%s)? "),
11167c478bd9Sstevel@tonic-gate 		    nl_langinfo(YESSTR), nl_langinfo(NOSTR));
1117442d23f4Sjp161948 		fflush(stdout);
11187c478bd9Sstevel@tonic-gate 		if (fgets(yesno, sizeof(yesno), stdin) == NULL)
11197c478bd9Sstevel@tonic-gate 			exit(1);
112026ba1984Sjp161948 		if (strcasecmp(chop(yesno), nl_langinfo(YESSTR)) != 0)
11217c478bd9Sstevel@tonic-gate 			exit(1);
11227c478bd9Sstevel@tonic-gate 	}
11237c478bd9Sstevel@tonic-gate 	/* Ask for a passphrase (twice). */
11247c478bd9Sstevel@tonic-gate 	if (identity_passphrase)
11257c478bd9Sstevel@tonic-gate 		passphrase1 = xstrdup(identity_passphrase);
11267c478bd9Sstevel@tonic-gate 	else if (identity_new_passphrase)
11277c478bd9Sstevel@tonic-gate 		passphrase1 = xstrdup(identity_new_passphrase);
11287c478bd9Sstevel@tonic-gate 	else {
11297c478bd9Sstevel@tonic-gate passphrase_again:
11307c478bd9Sstevel@tonic-gate 		passphrase1 =
11317c478bd9Sstevel@tonic-gate 			read_passphrase(gettext("Enter passphrase (empty "
11327c478bd9Sstevel@tonic-gate 			"for no passphrase): "), RP_ALLOW_STDIN);
11337c478bd9Sstevel@tonic-gate 		passphrase2 = read_passphrase(gettext("Enter same "
11347c478bd9Sstevel@tonic-gate 			    "passphrase again: "), RP_ALLOW_STDIN);
11357c478bd9Sstevel@tonic-gate 		if (strcmp(passphrase1, passphrase2) != 0) {
11367c478bd9Sstevel@tonic-gate 			/*
11377c478bd9Sstevel@tonic-gate 			 * The passphrases do not match.  Clear them and
11387c478bd9Sstevel@tonic-gate 			 * retry.
11397c478bd9Sstevel@tonic-gate 			 */
1140442d23f4Sjp161948 			memset(passphrase1, 0, strlen(passphrase1));
1141442d23f4Sjp161948 			memset(passphrase2, 0, strlen(passphrase2));
11427c478bd9Sstevel@tonic-gate 			xfree(passphrase1);
11437c478bd9Sstevel@tonic-gate 			xfree(passphrase2);
1144442d23f4Sjp161948 			printf(gettext("Passphrases do not match.  Try "
11457c478bd9Sstevel@tonic-gate 			    "again.\n"));
11467c478bd9Sstevel@tonic-gate 			goto passphrase_again;
11477c478bd9Sstevel@tonic-gate 		}
11487c478bd9Sstevel@tonic-gate 		/* Clear the other copy of the passphrase. */
1149442d23f4Sjp161948 		memset(passphrase2, 0, strlen(passphrase2));
11507c478bd9Sstevel@tonic-gate 		xfree(passphrase2);
11517c478bd9Sstevel@tonic-gate 	}
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	if (identity_comment) {
1154442d23f4Sjp161948 		strlcpy(comment, identity_comment, sizeof(comment));
11557c478bd9Sstevel@tonic-gate 	} else {
11567c478bd9Sstevel@tonic-gate 		/* Create default commend field for the passphrase. */
1157442d23f4Sjp161948 		snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 	/* Save the key with the given passphrase and comment. */
11617c478bd9Sstevel@tonic-gate 	if (!key_save_private(private, identity_file, passphrase1, comment)) {
1162442d23f4Sjp161948 		printf(gettext("Saving the key failed: %s.\n"), identity_file);
1163442d23f4Sjp161948 		memset(passphrase1, 0, strlen(passphrase1));
11647c478bd9Sstevel@tonic-gate 		xfree(passphrase1);
11657c478bd9Sstevel@tonic-gate 		exit(1);
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	/* Clear the passphrase. */
1168442d23f4Sjp161948 	memset(passphrase1, 0, strlen(passphrase1));
11697c478bd9Sstevel@tonic-gate 	xfree(passphrase1);
11707c478bd9Sstevel@tonic-gate 
11717c478bd9Sstevel@tonic-gate 	/* Clear the private key and the random number generator. */
11727c478bd9Sstevel@tonic-gate 	key_free(private);
11737c478bd9Sstevel@tonic-gate 	arc4random_stir();
11747c478bd9Sstevel@tonic-gate 
11757c478bd9Sstevel@tonic-gate 	if (!quiet)
1176442d23f4Sjp161948 		printf(gettext("Your identification has been saved in %s.\n"),
11777c478bd9Sstevel@tonic-gate 		    identity_file);
11787c478bd9Sstevel@tonic-gate 
1179442d23f4Sjp161948 	strlcat(identity_file, ".pub", sizeof(identity_file));
11807c478bd9Sstevel@tonic-gate 	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
11817c478bd9Sstevel@tonic-gate 	if (fd == -1) {
1182442d23f4Sjp161948 		printf(gettext("Could not save your public key in %s\n"),
11837c478bd9Sstevel@tonic-gate 		    identity_file);
11847c478bd9Sstevel@tonic-gate 		exit(1);
11857c478bd9Sstevel@tonic-gate 	}
11867c478bd9Sstevel@tonic-gate 	f = fdopen(fd, "w");
11877c478bd9Sstevel@tonic-gate 	if (f == NULL) {
1188442d23f4Sjp161948 		printf(gettext("fdopen %s failed"), identity_file);
11897c478bd9Sstevel@tonic-gate 		exit(1);
11907c478bd9Sstevel@tonic-gate 	}
11917c478bd9Sstevel@tonic-gate 	if (!key_write(public, f))
1192442d23f4Sjp161948 		fprintf(stderr, gettext("write key failed"));
1193442d23f4Sjp161948 	fprintf(f, " %s\n", comment);
1194442d23f4Sjp161948 	fclose(f);
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate 	if (!quiet) {
11977c478bd9Sstevel@tonic-gate 		char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
1198442d23f4Sjp161948 		printf(gettext("Your public key has been saved in %s.\n"),
11997c478bd9Sstevel@tonic-gate 		    identity_file);
1200442d23f4Sjp161948 		printf(gettext("The key fingerprint is:\n"));
1201442d23f4Sjp161948 		printf("%s %s\n", fp, comment);
12027c478bd9Sstevel@tonic-gate 		xfree(fp);
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	key_free(public);
12067c478bd9Sstevel@tonic-gate 	return(0);
12077c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
12087c478bd9Sstevel@tonic-gate }
1209