xref: /titanic_50/usr/src/cmd/ssh/ssh-keygen/ssh-keygen.c (revision 26ba198477055398633f319757f934b7ce73784e)
17c478bd9Sstevel@tonic-gate /*
2*26ba1984Sjp161948  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
37c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate /*
67c478bd9Sstevel@tonic-gate  * Author: Tatu Ylonen <ylo@cs.hut.fi>
77c478bd9Sstevel@tonic-gate  * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
87c478bd9Sstevel@tonic-gate  *                    All rights reserved
97c478bd9Sstevel@tonic-gate  * Identity and host key generation and maintenance.
107c478bd9Sstevel@tonic-gate  *
117c478bd9Sstevel@tonic-gate  * As far as I am concerned, the code I have written for this software
127c478bd9Sstevel@tonic-gate  * can be used freely for any purpose.  Any derived versions of this
137c478bd9Sstevel@tonic-gate  * software must be clearly marked as such, and if the derived work is
147c478bd9Sstevel@tonic-gate  * incompatible with the protocol description in the RFC file, it must be
157c478bd9Sstevel@tonic-gate  * called by a name other than "ssh" or "Secure Shell".
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include "includes.h"
197c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: ssh-keygen.c,v 1.101 2002/06/23 09:39:55 deraadt Exp $");
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate #include <openssl/evp.h>
247c478bd9Sstevel@tonic-gate #include <openssl/pem.h>
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include "xmalloc.h"
277c478bd9Sstevel@tonic-gate #include "key.h"
287c478bd9Sstevel@tonic-gate #include "rsa.h"
297c478bd9Sstevel@tonic-gate #include "authfile.h"
307c478bd9Sstevel@tonic-gate #include "uuencode.h"
317c478bd9Sstevel@tonic-gate #include "buffer.h"
327c478bd9Sstevel@tonic-gate #include "bufaux.h"
337c478bd9Sstevel@tonic-gate #include "pathnames.h"
347c478bd9Sstevel@tonic-gate #include "log.h"
357c478bd9Sstevel@tonic-gate #include "readpass.h"
36*26ba1984Sjp161948 #include "misc.h"
377c478bd9Sstevel@tonic-gate #include <langinfo.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifdef SMARTCARD
407c478bd9Sstevel@tonic-gate #include "scard.h"
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /* Number of bits in the RSA/DSA key.  This value can be changed on the command line. */
447c478bd9Sstevel@tonic-gate int bits = 1024;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Flag indicating that we just want to change the passphrase.  This can be
487c478bd9Sstevel@tonic-gate  * set on the command line.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate int change_passphrase = 0;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * Flag indicating that we just want to change the comment.  This can be set
547c478bd9Sstevel@tonic-gate  * on the command line.
557c478bd9Sstevel@tonic-gate  */
567c478bd9Sstevel@tonic-gate int change_comment = 0;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate int quiet = 0;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /* Flag indicating that we just want to see the key fingerprint */
617c478bd9Sstevel@tonic-gate int print_fingerprint = 0;
627c478bd9Sstevel@tonic-gate int print_bubblebabble = 0;
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate /* The identity file name, given on the command line or entered by the user. */
657c478bd9Sstevel@tonic-gate char identity_file[1024];
667c478bd9Sstevel@tonic-gate int have_identity = 0;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /* This is set to the passphrase if given on the command line. */
697c478bd9Sstevel@tonic-gate char *identity_passphrase = NULL;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate /* This is set to the new passphrase if given on the command line. */
727c478bd9Sstevel@tonic-gate char *identity_new_passphrase = NULL;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* This is set to the new comment if given on the command line. */
757c478bd9Sstevel@tonic-gate char *identity_comment = NULL;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /* Dump public key file in format used by real and the original SSH 2 */
787c478bd9Sstevel@tonic-gate int convert_to_ssh2 = 0;
797c478bd9Sstevel@tonic-gate int convert_from_ssh2 = 0;
807c478bd9Sstevel@tonic-gate int print_public = 0;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate char *key_type_name = NULL;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate /* argv0 */
857c478bd9Sstevel@tonic-gate #ifdef HAVE___PROGNAME
867c478bd9Sstevel@tonic-gate extern char *__progname;
877c478bd9Sstevel@tonic-gate #else
887c478bd9Sstevel@tonic-gate char *__progname;
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate char hostname[MAXHOSTNAMELEN];
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate static void
947c478bd9Sstevel@tonic-gate ask_filename(struct passwd *pw, const char *prompt)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	char buf[1024];
977c478bd9Sstevel@tonic-gate 	char *name = NULL;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if (key_type_name == NULL)
1007c478bd9Sstevel@tonic-gate 		name = _PATH_SSH_CLIENT_ID_RSA;
1017c478bd9Sstevel@tonic-gate 	else
1027c478bd9Sstevel@tonic-gate 		switch (key_type_from_name(key_type_name)) {
1037c478bd9Sstevel@tonic-gate 		case KEY_RSA1:
1047c478bd9Sstevel@tonic-gate 			name = _PATH_SSH_CLIENT_IDENTITY;
1057c478bd9Sstevel@tonic-gate 			break;
1067c478bd9Sstevel@tonic-gate 		case KEY_DSA:
1077c478bd9Sstevel@tonic-gate 			name = _PATH_SSH_CLIENT_ID_DSA;
1087c478bd9Sstevel@tonic-gate 			break;
1097c478bd9Sstevel@tonic-gate 		case KEY_RSA:
1107c478bd9Sstevel@tonic-gate 			name = _PATH_SSH_CLIENT_ID_RSA;
1117c478bd9Sstevel@tonic-gate 			break;
1127c478bd9Sstevel@tonic-gate 		default:
1137c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("bad key type"));
1147c478bd9Sstevel@tonic-gate 			exit(1);
1157c478bd9Sstevel@tonic-gate 			break;
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	(void) snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
1197c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%s (%s): ", gettext(prompt), identity_file);
1207c478bd9Sstevel@tonic-gate 	(void) fflush(stderr);
1217c478bd9Sstevel@tonic-gate 	if (fgets(buf, sizeof(buf), stdin) == NULL)
1227c478bd9Sstevel@tonic-gate 		exit(1);
1237c478bd9Sstevel@tonic-gate 	if (strchr(buf, '\n'))
1247c478bd9Sstevel@tonic-gate 		*strchr(buf, '\n') = 0;
1257c478bd9Sstevel@tonic-gate 	if (strcmp(buf, "") != 0)
1267c478bd9Sstevel@tonic-gate 		(void) strlcpy(identity_file, buf, sizeof(identity_file));
1277c478bd9Sstevel@tonic-gate 	have_identity = 1;
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate static Key *
1317c478bd9Sstevel@tonic-gate load_identity(char *filename)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	char *pass;
1347c478bd9Sstevel@tonic-gate 	Key *prv;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	prv = key_load_private(filename, "", NULL);
1377c478bd9Sstevel@tonic-gate 	if (prv == NULL) {
1387c478bd9Sstevel@tonic-gate 		if (identity_passphrase)
1397c478bd9Sstevel@tonic-gate 			pass = xstrdup(identity_passphrase);
1407c478bd9Sstevel@tonic-gate 		else
1417c478bd9Sstevel@tonic-gate 			pass = read_passphrase(gettext("Enter passphrase: "),
1427c478bd9Sstevel@tonic-gate 			    RP_ALLOW_STDIN);
1437c478bd9Sstevel@tonic-gate 		prv = key_load_private(filename, pass, NULL);
1447c478bd9Sstevel@tonic-gate 		(void) memset(pass, 0, strlen(pass));
1457c478bd9Sstevel@tonic-gate 		xfree(pass);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	return prv;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #define SSH_COM_PUBLIC_BEGIN		"---- BEGIN SSH2 PUBLIC KEY ----"
1517c478bd9Sstevel@tonic-gate #define SSH_COM_PUBLIC_END		"---- END SSH2 PUBLIC KEY ----"
1527c478bd9Sstevel@tonic-gate #define SSH_COM_PRIVATE_BEGIN		"---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
1537c478bd9Sstevel@tonic-gate #define	SSH_COM_PRIVATE_KEY_MAGIC	0x3f6ff9eb
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate static void
1567c478bd9Sstevel@tonic-gate do_convert_to_ssh2(struct passwd *pw)
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	Key *k;
1597c478bd9Sstevel@tonic-gate 	u_int len;
1607c478bd9Sstevel@tonic-gate 	u_char *blob;
1617c478bd9Sstevel@tonic-gate 	struct stat st;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if (!have_identity)
1647c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
1657c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
1667c478bd9Sstevel@tonic-gate 		perror(identity_file);
1677c478bd9Sstevel@tonic-gate 		exit(1);
1687c478bd9Sstevel@tonic-gate 	}
1697c478bd9Sstevel@tonic-gate 	if ((k = key_load_public(identity_file, NULL)) == NULL) {
1707c478bd9Sstevel@tonic-gate 		if ((k = load_identity(identity_file)) == NULL) {
1717c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("load failed\n"));
1727c478bd9Sstevel@tonic-gate 			exit(1);
1737c478bd9Sstevel@tonic-gate 		}
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	if (key_to_blob(k, &blob, &len) <= 0) {
1767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("key_to_blob failed\n"));
1777c478bd9Sstevel@tonic-gate 		exit(1);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
1807c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, gettext(
1817c478bd9Sstevel@tonic-gate 	    "Comment: \"%u-bit %s, converted from OpenSSH by %s@%s\"\n"),
1827c478bd9Sstevel@tonic-gate 	    key_size(k), key_type(k),
1837c478bd9Sstevel@tonic-gate 	    pw->pw_name, hostname);
1847c478bd9Sstevel@tonic-gate 	dump_base64(stdout, blob, len);
1857c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
1867c478bd9Sstevel@tonic-gate 	key_free(k);
1877c478bd9Sstevel@tonic-gate 	xfree(blob);
1887c478bd9Sstevel@tonic-gate 	exit(0);
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate static void
1927c478bd9Sstevel@tonic-gate buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
1937c478bd9Sstevel@tonic-gate {
1947c478bd9Sstevel@tonic-gate 	int bits = buffer_get_int(b);
1957c478bd9Sstevel@tonic-gate 	int bytes = (bits + 7) / 8;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (buffer_len(b) < bytes)
1987c478bd9Sstevel@tonic-gate 		fatal("buffer_get_bignum_bits: input buffer too small: "
1997c478bd9Sstevel@tonic-gate 		    "need %d have %d", bytes, buffer_len(b));
2007c478bd9Sstevel@tonic-gate 	(void) BN_bin2bn(buffer_ptr(b), bytes, value);
2017c478bd9Sstevel@tonic-gate 	buffer_consume(b, bytes);
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate static Key *
2057c478bd9Sstevel@tonic-gate do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	Buffer b;
2087c478bd9Sstevel@tonic-gate 	Key *key = NULL;
2097c478bd9Sstevel@tonic-gate 	char *type, *cipher;
2107c478bd9Sstevel@tonic-gate 	u_char *sig, data[] = "abcde12345";
2117c478bd9Sstevel@tonic-gate 	int magic, rlen, ktype, i1, i2, i3, i4;
2127c478bd9Sstevel@tonic-gate 	u_int slen;
2137c478bd9Sstevel@tonic-gate 	u_long e;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	buffer_init(&b);
2167c478bd9Sstevel@tonic-gate 	buffer_append(&b, blob, blen);
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	magic  = buffer_get_int(&b);
2197c478bd9Sstevel@tonic-gate 	if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
2207c478bd9Sstevel@tonic-gate 		error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
2217c478bd9Sstevel@tonic-gate 		buffer_free(&b);
2227c478bd9Sstevel@tonic-gate 		return NULL;
2237c478bd9Sstevel@tonic-gate 	}
2247c478bd9Sstevel@tonic-gate 	i1 = buffer_get_int(&b);
2257c478bd9Sstevel@tonic-gate 	type   = buffer_get_string(&b, NULL);
2267c478bd9Sstevel@tonic-gate 	cipher = buffer_get_string(&b, NULL);
2277c478bd9Sstevel@tonic-gate 	i2 = buffer_get_int(&b);
2287c478bd9Sstevel@tonic-gate 	i3 = buffer_get_int(&b);
2297c478bd9Sstevel@tonic-gate 	i4 = buffer_get_int(&b);
2307c478bd9Sstevel@tonic-gate 	debug("ignore (%d %d %d %d)", i1,i2,i3,i4);
2317c478bd9Sstevel@tonic-gate 	if (strcmp(cipher, "none") != 0) {
2327c478bd9Sstevel@tonic-gate 		error("unsupported cipher %s", cipher);
2337c478bd9Sstevel@tonic-gate 		xfree(cipher);
2347c478bd9Sstevel@tonic-gate 		buffer_free(&b);
2357c478bd9Sstevel@tonic-gate 		xfree(type);
2367c478bd9Sstevel@tonic-gate 		return NULL;
2377c478bd9Sstevel@tonic-gate 	}
2387c478bd9Sstevel@tonic-gate 	xfree(cipher);
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (strstr(type, "dsa")) {
2417c478bd9Sstevel@tonic-gate 		ktype = KEY_DSA;
2427c478bd9Sstevel@tonic-gate 	} else if (strstr(type, "rsa")) {
2437c478bd9Sstevel@tonic-gate 		ktype = KEY_RSA;
2447c478bd9Sstevel@tonic-gate 	} else {
2457c478bd9Sstevel@tonic-gate 		xfree(type);
2467c478bd9Sstevel@tonic-gate 		return NULL;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	key = key_new_private(ktype);
2497c478bd9Sstevel@tonic-gate 	xfree(type);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	switch (key->type) {
2527c478bd9Sstevel@tonic-gate 	case KEY_DSA:
2537c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->p);
2547c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->g);
2557c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->q);
2567c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->pub_key);
2577c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->dsa->priv_key);
2587c478bd9Sstevel@tonic-gate 		break;
2597c478bd9Sstevel@tonic-gate 	case KEY_RSA:
2607c478bd9Sstevel@tonic-gate 		e  = buffer_get_char(&b);
2617c478bd9Sstevel@tonic-gate 		debug("e %lx", e);
2627c478bd9Sstevel@tonic-gate 		if (e < 30) {
2637c478bd9Sstevel@tonic-gate 			e <<= 8;
2647c478bd9Sstevel@tonic-gate 			e += buffer_get_char(&b);
2657c478bd9Sstevel@tonic-gate 			debug("e %lx", e);
2667c478bd9Sstevel@tonic-gate 			e <<= 8;
2677c478bd9Sstevel@tonic-gate 			e += buffer_get_char(&b);
2687c478bd9Sstevel@tonic-gate 			debug("e %lx", e);
2697c478bd9Sstevel@tonic-gate 		}
2707c478bd9Sstevel@tonic-gate 		if (!BN_set_word(key->rsa->e, e)) {
2717c478bd9Sstevel@tonic-gate 			buffer_free(&b);
2727c478bd9Sstevel@tonic-gate 			key_free(key);
2737c478bd9Sstevel@tonic-gate 			return NULL;
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->d);
2767c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->n);
2777c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->iqmp);
2787c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->q);
2797c478bd9Sstevel@tonic-gate 		buffer_get_bignum_bits(&b, key->rsa->p);
2807c478bd9Sstevel@tonic-gate 		rsa_generate_additional_parameters(key->rsa);
2817c478bd9Sstevel@tonic-gate 		break;
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 	rlen = buffer_len(&b);
2847c478bd9Sstevel@tonic-gate 	if (rlen != 0)
2857c478bd9Sstevel@tonic-gate 		error("do_convert_private_ssh2_from_blob: "
2867c478bd9Sstevel@tonic-gate 		    "remaining bytes in key blob %d", rlen);
2877c478bd9Sstevel@tonic-gate 	buffer_free(&b);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* try the key */
2907c478bd9Sstevel@tonic-gate 	(void) key_sign(key, &sig, &slen, data, sizeof(data));
2917c478bd9Sstevel@tonic-gate 	key_verify(key, sig, slen, data, sizeof(data));
2927c478bd9Sstevel@tonic-gate 	xfree(sig);
2937c478bd9Sstevel@tonic-gate 	return key;
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate static void
2977c478bd9Sstevel@tonic-gate do_convert_from_ssh2(struct passwd *pw)
2987c478bd9Sstevel@tonic-gate {
2997c478bd9Sstevel@tonic-gate 	Key *k;
3007c478bd9Sstevel@tonic-gate 	int blen;
3017c478bd9Sstevel@tonic-gate 	u_int len;
3027c478bd9Sstevel@tonic-gate 	char line[1024], *p;
3037c478bd9Sstevel@tonic-gate 	u_char blob[8096];
3047c478bd9Sstevel@tonic-gate 	char encoded[8096];
3057c478bd9Sstevel@tonic-gate 	struct stat st;
3067c478bd9Sstevel@tonic-gate 	int escaped = 0, private = 0, ok;
3077c478bd9Sstevel@tonic-gate 	FILE *fp;
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (!have_identity)
3107c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
3117c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
3127c478bd9Sstevel@tonic-gate 		perror(identity_file);
3137c478bd9Sstevel@tonic-gate 		exit(1);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 	fp = fopen(identity_file, "r");
3167c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
3177c478bd9Sstevel@tonic-gate 		perror(identity_file);
3187c478bd9Sstevel@tonic-gate 		exit(1);
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 	encoded[0] = '\0';
3217c478bd9Sstevel@tonic-gate 	while (fgets(line, sizeof(line), fp)) {
3227c478bd9Sstevel@tonic-gate 		if (!(p = strchr(line, '\n'))) {
3237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("input line too long.\n"));
3247c478bd9Sstevel@tonic-gate 			exit(1);
3257c478bd9Sstevel@tonic-gate 		}
3267c478bd9Sstevel@tonic-gate 		if (p > line && p[-1] == '\\')
3277c478bd9Sstevel@tonic-gate 			escaped++;
3287c478bd9Sstevel@tonic-gate 		if (strncmp(line, "----", 4) == 0 ||
3297c478bd9Sstevel@tonic-gate 		    strstr(line, ": ") != NULL) {
3307c478bd9Sstevel@tonic-gate 			if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
3317c478bd9Sstevel@tonic-gate 				private = 1;
3327c478bd9Sstevel@tonic-gate 			if (strstr(line, " END ") != NULL) {
3337c478bd9Sstevel@tonic-gate 				break;
3347c478bd9Sstevel@tonic-gate 			}
3357c478bd9Sstevel@tonic-gate 			/* fprintf(stderr, "ignore: %s", line); */
3367c478bd9Sstevel@tonic-gate 			continue;
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 		if (escaped) {
3397c478bd9Sstevel@tonic-gate 			escaped--;
3407c478bd9Sstevel@tonic-gate 			/* fprintf(stderr, "escaped: %s", line); */
3417c478bd9Sstevel@tonic-gate 			continue;
3427c478bd9Sstevel@tonic-gate 		}
3437c478bd9Sstevel@tonic-gate 		*p = '\0';
3447c478bd9Sstevel@tonic-gate 		(void) strlcat(encoded, line, sizeof(encoded));
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 	len = strlen(encoded);
3477c478bd9Sstevel@tonic-gate 	if (((len % 4) == 3) &&
3487c478bd9Sstevel@tonic-gate 	    (encoded[len-1] == '=') &&
3497c478bd9Sstevel@tonic-gate 	    (encoded[len-2] == '=') &&
3507c478bd9Sstevel@tonic-gate 	    (encoded[len-3] == '='))
3517c478bd9Sstevel@tonic-gate 		encoded[len-3] = '\0';
3527c478bd9Sstevel@tonic-gate 	blen = uudecode(encoded, blob, sizeof(blob));
3537c478bd9Sstevel@tonic-gate 	if (blen < 0) {
3547c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("uudecode failed.\n"));
3557c478bd9Sstevel@tonic-gate 		exit(1);
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	k = private ?
3587c478bd9Sstevel@tonic-gate 	    do_convert_private_ssh2_from_blob(blob, blen) :
3597c478bd9Sstevel@tonic-gate 	    key_from_blob(blob, blen);
3607c478bd9Sstevel@tonic-gate 	if (k == NULL) {
3617c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("decode blob failed.\n"));
3627c478bd9Sstevel@tonic-gate 		exit(1);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 	ok = private ?
3657c478bd9Sstevel@tonic-gate 	    (k->type == KEY_DSA ?
3667c478bd9Sstevel@tonic-gate 		 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
3677c478bd9Sstevel@tonic-gate 		 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
3687c478bd9Sstevel@tonic-gate 	    key_write(k, stdout);
3697c478bd9Sstevel@tonic-gate 	if (!ok) {
3707c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("key write failed"));
3717c478bd9Sstevel@tonic-gate 		exit(1);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	key_free(k);
3747c478bd9Sstevel@tonic-gate 	if (!private)
3757c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
3767c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
3777c478bd9Sstevel@tonic-gate 	exit(0);
3787c478bd9Sstevel@tonic-gate }
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate static void
3817c478bd9Sstevel@tonic-gate do_print_public(struct passwd *pw)
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	Key *prv;
3847c478bd9Sstevel@tonic-gate 	struct stat st;
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	if (!have_identity)
3877c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
3887c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
3897c478bd9Sstevel@tonic-gate 		perror(identity_file);
3907c478bd9Sstevel@tonic-gate 		exit(1);
3917c478bd9Sstevel@tonic-gate 	}
3927c478bd9Sstevel@tonic-gate 	prv = load_identity(identity_file);
3937c478bd9Sstevel@tonic-gate 	if (prv == NULL) {
3947c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("load failed\n"));
3957c478bd9Sstevel@tonic-gate 		exit(1);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	if (!key_write(prv, stdout))
3987c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("key_write failed"));
3997c478bd9Sstevel@tonic-gate 	key_free(prv);
4007c478bd9Sstevel@tonic-gate 	(void) fprintf(stdout, "\n");
4017c478bd9Sstevel@tonic-gate 	exit(0);
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate #ifdef SMARTCARD
4057c478bd9Sstevel@tonic-gate static void
4067c478bd9Sstevel@tonic-gate do_upload(struct passwd *pw, const char *sc_reader_id)
4077c478bd9Sstevel@tonic-gate {
4087c478bd9Sstevel@tonic-gate 	Key *prv = NULL;
4097c478bd9Sstevel@tonic-gate 	struct stat st;
4107c478bd9Sstevel@tonic-gate 	int ret;
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 	if (!have_identity)
4137c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
4147c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
4157c478bd9Sstevel@tonic-gate 		perror(identity_file);
4167c478bd9Sstevel@tonic-gate 		exit(1);
4177c478bd9Sstevel@tonic-gate 	}
4187c478bd9Sstevel@tonic-gate 	prv = load_identity(identity_file);
4197c478bd9Sstevel@tonic-gate 	if (prv == NULL) {
4207c478bd9Sstevel@tonic-gate 		error("load failed");
4217c478bd9Sstevel@tonic-gate 		exit(1);
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	ret = sc_put_key(prv, sc_reader_id);
4247c478bd9Sstevel@tonic-gate 	key_free(prv);
4257c478bd9Sstevel@tonic-gate 	if (ret < 0)
4267c478bd9Sstevel@tonic-gate 		exit(1);
4277c478bd9Sstevel@tonic-gate 	log("loading key done");
4287c478bd9Sstevel@tonic-gate 	exit(0);
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate static void
4327c478bd9Sstevel@tonic-gate do_download(struct passwd *pw, const char *sc_reader_id)
4337c478bd9Sstevel@tonic-gate {
4347c478bd9Sstevel@tonic-gate 	Key **keys = NULL;
4357c478bd9Sstevel@tonic-gate 	int i;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	keys = sc_get_keys(sc_reader_id, NULL);
4387c478bd9Sstevel@tonic-gate 	if (keys == NULL)
4397c478bd9Sstevel@tonic-gate 		fatal("cannot read public key from smartcard");
4407c478bd9Sstevel@tonic-gate 	for (i = 0; keys[i]; i++) {
4417c478bd9Sstevel@tonic-gate 		key_write(keys[i], stdout);
4427c478bd9Sstevel@tonic-gate 		key_free(keys[i]);
4437c478bd9Sstevel@tonic-gate 		(void) fprintf(stdout, "\n");
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	xfree(keys);
4467c478bd9Sstevel@tonic-gate 	exit(0);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate #endif /* SMARTCARD */
4497c478bd9Sstevel@tonic-gate 
4507c478bd9Sstevel@tonic-gate static void
4517c478bd9Sstevel@tonic-gate do_fingerprint(struct passwd *pw)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate 	FILE *f;
4547c478bd9Sstevel@tonic-gate 	Key *public;
4557c478bd9Sstevel@tonic-gate 	char *comment = NULL, *cp, *ep, line[16*1024], *fp;
4567c478bd9Sstevel@tonic-gate 	int i, skip = 0, num = 1, invalid = 1;
4577c478bd9Sstevel@tonic-gate 	enum fp_rep rep;
4587c478bd9Sstevel@tonic-gate 	enum fp_type fptype;
4597c478bd9Sstevel@tonic-gate 	struct stat st;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
4627c478bd9Sstevel@tonic-gate 	rep =    print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	if (!have_identity)
4657c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
4667c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
4677c478bd9Sstevel@tonic-gate 		perror(identity_file);
4687c478bd9Sstevel@tonic-gate 		exit(1);
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 	public = key_load_public(identity_file, &comment);
4717c478bd9Sstevel@tonic-gate 	if (public != NULL) {
4727c478bd9Sstevel@tonic-gate 		fp = key_fingerprint(public, fptype, rep);
4737c478bd9Sstevel@tonic-gate 		(void) printf("%u %s %s\n", key_size(public), fp, comment);
4747c478bd9Sstevel@tonic-gate 		key_free(public);
4757c478bd9Sstevel@tonic-gate 		xfree(comment);
4767c478bd9Sstevel@tonic-gate 		xfree(fp);
4777c478bd9Sstevel@tonic-gate 		exit(0);
4787c478bd9Sstevel@tonic-gate 	}
4797c478bd9Sstevel@tonic-gate 	if (comment)
4807c478bd9Sstevel@tonic-gate 		xfree(comment);
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	f = fopen(identity_file, "r");
4837c478bd9Sstevel@tonic-gate 	if (f != NULL) {
4847c478bd9Sstevel@tonic-gate 		while (fgets(line, sizeof(line), f)) {
4857c478bd9Sstevel@tonic-gate 			i = strlen(line) - 1;
4867c478bd9Sstevel@tonic-gate 			if (line[i] != '\n') {
4877c478bd9Sstevel@tonic-gate 				error("line %d too long: %.40s...", num, line);
4887c478bd9Sstevel@tonic-gate 				skip = 1;
4897c478bd9Sstevel@tonic-gate 				continue;
4907c478bd9Sstevel@tonic-gate 			}
4917c478bd9Sstevel@tonic-gate 			num++;
4927c478bd9Sstevel@tonic-gate 			if (skip) {
4937c478bd9Sstevel@tonic-gate 				skip = 0;
4947c478bd9Sstevel@tonic-gate 				continue;
4957c478bd9Sstevel@tonic-gate 			}
4967c478bd9Sstevel@tonic-gate 			line[i] = '\0';
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 			/* Skip leading whitespace, empty and comment lines. */
4997c478bd9Sstevel@tonic-gate 			for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
5007c478bd9Sstevel@tonic-gate 				;
5017c478bd9Sstevel@tonic-gate 			if (!*cp || *cp == '\n' || *cp == '#')
5027c478bd9Sstevel@tonic-gate 				continue ;
5037c478bd9Sstevel@tonic-gate 			i = strtol(cp, &ep, 10);
5047c478bd9Sstevel@tonic-gate 			if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
5057c478bd9Sstevel@tonic-gate 				int quoted = 0;
5067c478bd9Sstevel@tonic-gate 				comment = cp;
5077c478bd9Sstevel@tonic-gate 				for (; *cp && (quoted || (*cp != ' ' &&
5087c478bd9Sstevel@tonic-gate 				    *cp != '\t')); cp++) {
5097c478bd9Sstevel@tonic-gate 					if (*cp == '\\' && cp[1] == '"')
5107c478bd9Sstevel@tonic-gate 						cp++;	/* Skip both */
5117c478bd9Sstevel@tonic-gate 					else if (*cp == '"')
5127c478bd9Sstevel@tonic-gate 						quoted = !quoted;
5137c478bd9Sstevel@tonic-gate 				}
5147c478bd9Sstevel@tonic-gate 				if (!*cp)
5157c478bd9Sstevel@tonic-gate 					continue;
5167c478bd9Sstevel@tonic-gate 				*cp++ = '\0';
5177c478bd9Sstevel@tonic-gate 			}
5187c478bd9Sstevel@tonic-gate 			ep = cp;
5197c478bd9Sstevel@tonic-gate 			public = key_new(KEY_RSA1);
5207c478bd9Sstevel@tonic-gate 			if (key_read(public, &cp) != 1) {
5217c478bd9Sstevel@tonic-gate 				cp = ep;
5227c478bd9Sstevel@tonic-gate 				key_free(public);
5237c478bd9Sstevel@tonic-gate 				public = key_new(KEY_UNSPEC);
5247c478bd9Sstevel@tonic-gate 				if (key_read(public, &cp) != 1) {
5257c478bd9Sstevel@tonic-gate 					key_free(public);
5267c478bd9Sstevel@tonic-gate 					continue;
5277c478bd9Sstevel@tonic-gate 				}
5287c478bd9Sstevel@tonic-gate 			}
5297c478bd9Sstevel@tonic-gate 			comment = *cp ? cp : comment;
5307c478bd9Sstevel@tonic-gate 			fp = key_fingerprint(public, fptype, rep);
5317c478bd9Sstevel@tonic-gate 			(void) printf("%u %s %s\n", key_size(public), fp,
5327c478bd9Sstevel@tonic-gate 			    comment ? comment : gettext("no comment"));
5337c478bd9Sstevel@tonic-gate 			xfree(fp);
5347c478bd9Sstevel@tonic-gate 			key_free(public);
5357c478bd9Sstevel@tonic-gate 			invalid = 0;
5367c478bd9Sstevel@tonic-gate 		}
5377c478bd9Sstevel@tonic-gate 		(void) fclose(f);
5387c478bd9Sstevel@tonic-gate 	}
5397c478bd9Sstevel@tonic-gate 	if (invalid) {
5407c478bd9Sstevel@tonic-gate 		(void) printf(gettext("%s is not a public key file.\n"),
5417c478bd9Sstevel@tonic-gate 		       identity_file);
5427c478bd9Sstevel@tonic-gate 		exit(1);
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate 	exit(0);
5457c478bd9Sstevel@tonic-gate }
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate /*
5487c478bd9Sstevel@tonic-gate  * Perform changing a passphrase.  The argument is the passwd structure
5497c478bd9Sstevel@tonic-gate  * for the current user.
5507c478bd9Sstevel@tonic-gate  */
5517c478bd9Sstevel@tonic-gate static void
5527c478bd9Sstevel@tonic-gate do_change_passphrase(struct passwd *pw)
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	char *comment;
5557c478bd9Sstevel@tonic-gate 	char *old_passphrase, *passphrase1, *passphrase2;
5567c478bd9Sstevel@tonic-gate 	struct stat st;
5577c478bd9Sstevel@tonic-gate 	Key *private;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (!have_identity)
5607c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
5617c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
5627c478bd9Sstevel@tonic-gate 		perror(identity_file);
5637c478bd9Sstevel@tonic-gate 		exit(1);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	/* Try to load the file with empty passphrase. */
5667c478bd9Sstevel@tonic-gate 	private = key_load_private(identity_file, "", &comment);
5677c478bd9Sstevel@tonic-gate 	if (private == NULL) {
5687c478bd9Sstevel@tonic-gate 		if (identity_passphrase)
5697c478bd9Sstevel@tonic-gate 			old_passphrase = xstrdup(identity_passphrase);
5707c478bd9Sstevel@tonic-gate 		else
5717c478bd9Sstevel@tonic-gate 			old_passphrase =
5727c478bd9Sstevel@tonic-gate 			    read_passphrase(gettext("Enter old passphrase: "),
5737c478bd9Sstevel@tonic-gate 			    RP_ALLOW_STDIN);
5747c478bd9Sstevel@tonic-gate 		private = key_load_private(identity_file, old_passphrase,
5757c478bd9Sstevel@tonic-gate 		    &comment);
5767c478bd9Sstevel@tonic-gate 		(void) memset(old_passphrase, 0, strlen(old_passphrase));
5777c478bd9Sstevel@tonic-gate 		xfree(old_passphrase);
5787c478bd9Sstevel@tonic-gate 		if (private == NULL) {
5797c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Bad passphrase.\n"));
5807c478bd9Sstevel@tonic-gate 			exit(1);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 	}
5837c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Key has comment '%s'\n"), comment);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	/* Ask the new passphrase (twice). */
5867c478bd9Sstevel@tonic-gate 	if (identity_new_passphrase) {
5877c478bd9Sstevel@tonic-gate 		passphrase1 = xstrdup(identity_new_passphrase);
5887c478bd9Sstevel@tonic-gate 		passphrase2 = NULL;
5897c478bd9Sstevel@tonic-gate 	} else {
5907c478bd9Sstevel@tonic-gate 		passphrase1 =
5917c478bd9Sstevel@tonic-gate 			read_passphrase(gettext("Enter new passphrase (empty"
5927c478bd9Sstevel@tonic-gate 			    " for no passphrase): "), RP_ALLOW_STDIN);
5937c478bd9Sstevel@tonic-gate 		passphrase2 = read_passphrase(gettext("Enter same "
5947c478bd9Sstevel@tonic-gate 			    "passphrase again: "), RP_ALLOW_STDIN);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		/* Verify that they are the same. */
5977c478bd9Sstevel@tonic-gate 		if (strcmp(passphrase1, passphrase2) != 0) {
5987c478bd9Sstevel@tonic-gate 			(void) memset(passphrase1, 0, strlen(passphrase1));
5997c478bd9Sstevel@tonic-gate 			(void) memset(passphrase2, 0, strlen(passphrase2));
6007c478bd9Sstevel@tonic-gate 			xfree(passphrase1);
6017c478bd9Sstevel@tonic-gate 			xfree(passphrase2);
6027c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Pass phrases do not match.  Try "
6037c478bd9Sstevel@tonic-gate 				       "again.\n"));
6047c478bd9Sstevel@tonic-gate 			exit(1);
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		/* Destroy the other copy. */
6077c478bd9Sstevel@tonic-gate 		(void) memset(passphrase2, 0, strlen(passphrase2));
6087c478bd9Sstevel@tonic-gate 		xfree(passphrase2);
6097c478bd9Sstevel@tonic-gate 	}
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 	/* Save the file using the new passphrase. */
6127c478bd9Sstevel@tonic-gate 	if (!key_save_private(private, identity_file, passphrase1, comment)) {
6137c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Saving the key failed: %s.\n"), identity_file);
6147c478bd9Sstevel@tonic-gate 		(void) memset(passphrase1, 0, strlen(passphrase1));
6157c478bd9Sstevel@tonic-gate 		xfree(passphrase1);
6167c478bd9Sstevel@tonic-gate 		key_free(private);
6177c478bd9Sstevel@tonic-gate 		xfree(comment);
6187c478bd9Sstevel@tonic-gate 		exit(1);
6197c478bd9Sstevel@tonic-gate 	}
6207c478bd9Sstevel@tonic-gate 	/* Destroy the passphrase and the copy of the key in memory. */
6217c478bd9Sstevel@tonic-gate 	(void) memset(passphrase1, 0, strlen(passphrase1));
6227c478bd9Sstevel@tonic-gate 	xfree(passphrase1);
6237c478bd9Sstevel@tonic-gate 	key_free(private);		 /* Destroys contents */
6247c478bd9Sstevel@tonic-gate 	xfree(comment);
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Your identification has been saved with the new "
6277c478bd9Sstevel@tonic-gate 		       "passphrase.\n"));
6287c478bd9Sstevel@tonic-gate 	exit(0);
6297c478bd9Sstevel@tonic-gate }
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate /*
6327c478bd9Sstevel@tonic-gate  * Change the comment of a private key file.
6337c478bd9Sstevel@tonic-gate  */
6347c478bd9Sstevel@tonic-gate static void
6357c478bd9Sstevel@tonic-gate do_change_comment(struct passwd *pw)
6367c478bd9Sstevel@tonic-gate {
6377c478bd9Sstevel@tonic-gate 	char new_comment[1024], *comment, *passphrase;
6387c478bd9Sstevel@tonic-gate 	Key *private;
6397c478bd9Sstevel@tonic-gate 	Key *public;
6407c478bd9Sstevel@tonic-gate 	struct stat st;
6417c478bd9Sstevel@tonic-gate 	FILE *f;
6427c478bd9Sstevel@tonic-gate 	int fd;
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	if (!have_identity)
6457c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which the key is"));
6467c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) < 0) {
6477c478bd9Sstevel@tonic-gate 		perror(identity_file);
6487c478bd9Sstevel@tonic-gate 		exit(1);
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 	private = key_load_private(identity_file, "", &comment);
6517c478bd9Sstevel@tonic-gate 	if (private == NULL) {
6527c478bd9Sstevel@tonic-gate 		if (identity_passphrase)
6537c478bd9Sstevel@tonic-gate 			passphrase = xstrdup(identity_passphrase);
6547c478bd9Sstevel@tonic-gate 		else if (identity_new_passphrase)
6557c478bd9Sstevel@tonic-gate 			passphrase = xstrdup(identity_new_passphrase);
6567c478bd9Sstevel@tonic-gate 		else
6577c478bd9Sstevel@tonic-gate 			passphrase =
6587c478bd9Sstevel@tonic-gate 			    read_passphrase(gettext("Enter passphrase: "),
6597c478bd9Sstevel@tonic-gate 				    RP_ALLOW_STDIN);
6607c478bd9Sstevel@tonic-gate 		/* Try to load using the passphrase. */
6617c478bd9Sstevel@tonic-gate 		private = key_load_private(identity_file, passphrase, &comment);
6627c478bd9Sstevel@tonic-gate 		if (private == NULL) {
6637c478bd9Sstevel@tonic-gate 			(void) memset(passphrase, 0, strlen(passphrase));
6647c478bd9Sstevel@tonic-gate 			xfree(passphrase);
6657c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Bad passphrase.\n"));
6667c478bd9Sstevel@tonic-gate 			exit(1);
6677c478bd9Sstevel@tonic-gate 		}
6687c478bd9Sstevel@tonic-gate 	} else {
6697c478bd9Sstevel@tonic-gate 		passphrase = xstrdup("");
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 	if (private->type != KEY_RSA1) {
6727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("Comments are only supported for "
6737c478bd9Sstevel@tonic-gate 				    "RSA1 keys.\n"));
6747c478bd9Sstevel@tonic-gate 		key_free(private);
6757c478bd9Sstevel@tonic-gate 		exit(1);
6767c478bd9Sstevel@tonic-gate 	}
6777c478bd9Sstevel@tonic-gate 	(void) printf(gettext("Key now has comment '%s'\n"), comment);
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	if (identity_comment) {
6807c478bd9Sstevel@tonic-gate 		(void) strlcpy(new_comment, identity_comment, sizeof(new_comment));
6817c478bd9Sstevel@tonic-gate 	} else {
6827c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Enter new comment: "));
6837c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
6847c478bd9Sstevel@tonic-gate 		if (!fgets(new_comment, sizeof(new_comment), stdin)) {
6857c478bd9Sstevel@tonic-gate 			(void) memset(passphrase, 0, strlen(passphrase));
6867c478bd9Sstevel@tonic-gate 			key_free(private);
6877c478bd9Sstevel@tonic-gate 			exit(1);
6887c478bd9Sstevel@tonic-gate 		}
6897c478bd9Sstevel@tonic-gate 		if (strchr(new_comment, '\n'))
6907c478bd9Sstevel@tonic-gate 			*strchr(new_comment, '\n') = 0;
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	/* Save the file using the new passphrase. */
6947c478bd9Sstevel@tonic-gate 	if (!key_save_private(private, identity_file, passphrase, new_comment)) {
6957c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Saving the key failed: %s.\n"), identity_file);
6967c478bd9Sstevel@tonic-gate 		(void) memset(passphrase, 0, strlen(passphrase));
6977c478bd9Sstevel@tonic-gate 		xfree(passphrase);
6987c478bd9Sstevel@tonic-gate 		key_free(private);
6997c478bd9Sstevel@tonic-gate 		xfree(comment);
7007c478bd9Sstevel@tonic-gate 		exit(1);
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	(void) memset(passphrase, 0, strlen(passphrase));
7037c478bd9Sstevel@tonic-gate 	xfree(passphrase);
7047c478bd9Sstevel@tonic-gate 	public = key_from_private(private);
7057c478bd9Sstevel@tonic-gate 	key_free(private);
7067c478bd9Sstevel@tonic-gate 
7077c478bd9Sstevel@tonic-gate 	(void) strlcat(identity_file, ".pub", sizeof(identity_file));
7087c478bd9Sstevel@tonic-gate 	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
7097c478bd9Sstevel@tonic-gate 	if (fd == -1) {
7107c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Could not save your public key in %s\n"),
7117c478bd9Sstevel@tonic-gate 		       identity_file);
7127c478bd9Sstevel@tonic-gate 		exit(1);
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	f = fdopen(fd, "w");
7157c478bd9Sstevel@tonic-gate 	if (f == NULL) {
7167c478bd9Sstevel@tonic-gate 		(void) printf(gettext("fdopen %s failed"), identity_file);
7177c478bd9Sstevel@tonic-gate 		exit(1);
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	if (!key_write(public, f))
7207c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("write key failed"));
7217c478bd9Sstevel@tonic-gate 	key_free(public);
7227c478bd9Sstevel@tonic-gate 	(void) fprintf(f, " %s\n", new_comment);
7237c478bd9Sstevel@tonic-gate 	(void) fclose(f);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	xfree(comment);
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 	(void) printf(gettext("The comment in your key file has been changed.\n"));
7287c478bd9Sstevel@tonic-gate 	exit(0);
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static void
7327c478bd9Sstevel@tonic-gate usage(void)
7337c478bd9Sstevel@tonic-gate {
7347c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
7357c478bd9Sstevel@tonic-gate 	"Usage: %s [options]\n"
7367c478bd9Sstevel@tonic-gate 	"Options:\n"
7377c478bd9Sstevel@tonic-gate 	"  -b bits     Number of bits in the key to create.\n"
7387c478bd9Sstevel@tonic-gate 	"  -c          Change comment in private and public key files.\n"
7397c478bd9Sstevel@tonic-gate 	"  -e          Convert OpenSSH to IETF SECSH key file.\n"
7407c478bd9Sstevel@tonic-gate 	"  -f filename Filename of the key file.\n"
7417c478bd9Sstevel@tonic-gate 	"  -i          Convert IETF SECSH to OpenSSH key file.\n"
7427c478bd9Sstevel@tonic-gate 	"  -l          Show fingerprint of key file.\n"
7437c478bd9Sstevel@tonic-gate 	"  -p          Change passphrase of private key file.\n"
7447c478bd9Sstevel@tonic-gate 	"  -q          Quiet.\n"
7457c478bd9Sstevel@tonic-gate 	"  -y          Read private key file and print public key.\n"
7467c478bd9Sstevel@tonic-gate 	"  -t type     Specify type of key to create.\n"
7477c478bd9Sstevel@tonic-gate 	"  -B          Show bubblebabble digest of key file.\n"
7487c478bd9Sstevel@tonic-gate 	"  -C comment  Provide new comment.\n"
7497c478bd9Sstevel@tonic-gate 	"  -N phrase   Provide new passphrase.\n"
7507c478bd9Sstevel@tonic-gate 	"  -P phrase   Provide old passphrase.\n"
7517c478bd9Sstevel@tonic-gate #ifdef SMARTCARD
7527c478bd9Sstevel@tonic-gate 	"  -D reader   Download public key from smartcard.\n"
7537c478bd9Sstevel@tonic-gate 	"  -U reader   Upload private key to smartcard.\n"
7547c478bd9Sstevel@tonic-gate #endif /* SMARTCARD */
7557c478bd9Sstevel@tonic-gate 	), __progname);
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	exit(1);
7587c478bd9Sstevel@tonic-gate }
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate /*
7617c478bd9Sstevel@tonic-gate  * Main program for key management.
7627c478bd9Sstevel@tonic-gate  */
7637c478bd9Sstevel@tonic-gate int
7647c478bd9Sstevel@tonic-gate main(int ac, char **av)
7657c478bd9Sstevel@tonic-gate {
7667c478bd9Sstevel@tonic-gate 	char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
7677c478bd9Sstevel@tonic-gate 	char *reader_id = NULL;
7687c478bd9Sstevel@tonic-gate 	Key *private, *public;
7697c478bd9Sstevel@tonic-gate 	struct passwd *pw;
7707c478bd9Sstevel@tonic-gate 	struct stat st;
7717c478bd9Sstevel@tonic-gate 	int opt, type, fd;
7727c478bd9Sstevel@tonic-gate #ifdef SMARTCARD
7737c478bd9Sstevel@tonic-gate 	int download = 0;
7747c478bd9Sstevel@tonic-gate #endif /* SMARTCARD */
7757c478bd9Sstevel@tonic-gate 	FILE *f;
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	extern int optind;
7787c478bd9Sstevel@tonic-gate 	extern char *optarg;
7797c478bd9Sstevel@tonic-gate 
7807c478bd9Sstevel@tonic-gate 	__progname = get_progname(av[0]);
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	(void) g11n_setlocale(LC_ALL, "");
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	SSLeay_add_all_algorithms();
7857c478bd9Sstevel@tonic-gate 	init_rng();
7867c478bd9Sstevel@tonic-gate 	seed_rng();
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	/* we need this for the home * directory.  */
7897c478bd9Sstevel@tonic-gate 	pw = getpwuid(getuid());
7907c478bd9Sstevel@tonic-gate 	if (!pw) {
7917c478bd9Sstevel@tonic-gate 		(void) printf(gettext("You don't exist, go away!\n"));
7927c478bd9Sstevel@tonic-gate 		exit(1);
7937c478bd9Sstevel@tonic-gate 	}
7947c478bd9Sstevel@tonic-gate 	if (gethostname(hostname, sizeof(hostname)) < 0) {
7957c478bd9Sstevel@tonic-gate 		perror("gethostname");
7967c478bd9Sstevel@tonic-gate 		exit(1);
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate #ifdef SMARTCARD
8007c478bd9Sstevel@tonic-gate #define GETOPT_ARGS "deiqpclBRxXyb:f:t:U:D:P:N:C:"
8017c478bd9Sstevel@tonic-gate #else
8027c478bd9Sstevel@tonic-gate #define GETOPT_ARGS "deiqpclBRxXyb:f:t:P:N:C:"
8037c478bd9Sstevel@tonic-gate #endif /* SMARTCARD */
8047c478bd9Sstevel@tonic-gate 	while ((opt = getopt(ac, av, GETOPT_ARGS)) != -1) {
8057c478bd9Sstevel@tonic-gate 		switch (opt) {
8067c478bd9Sstevel@tonic-gate 		case 'b':
8077c478bd9Sstevel@tonic-gate 			bits = atoi(optarg);
8087c478bd9Sstevel@tonic-gate 			if (bits < 512 || bits > 32768) {
8097c478bd9Sstevel@tonic-gate 				(void) printf(gettext("Bits has bad value.\n"));
8107c478bd9Sstevel@tonic-gate 				exit(1);
8117c478bd9Sstevel@tonic-gate 			}
8127c478bd9Sstevel@tonic-gate 			break;
8137c478bd9Sstevel@tonic-gate 		case 'l':
8147c478bd9Sstevel@tonic-gate 			print_fingerprint = 1;
8157c478bd9Sstevel@tonic-gate 			break;
8167c478bd9Sstevel@tonic-gate 		case 'B':
8177c478bd9Sstevel@tonic-gate 			print_bubblebabble = 1;
8187c478bd9Sstevel@tonic-gate 			break;
8197c478bd9Sstevel@tonic-gate 		case 'p':
8207c478bd9Sstevel@tonic-gate 			change_passphrase = 1;
8217c478bd9Sstevel@tonic-gate 			break;
8227c478bd9Sstevel@tonic-gate 		case 'c':
8237c478bd9Sstevel@tonic-gate 			change_comment = 1;
8247c478bd9Sstevel@tonic-gate 			break;
8257c478bd9Sstevel@tonic-gate 		case 'f':
8267c478bd9Sstevel@tonic-gate 			(void) strlcpy(identity_file, optarg, sizeof(identity_file));
8277c478bd9Sstevel@tonic-gate 			have_identity = 1;
8287c478bd9Sstevel@tonic-gate 			break;
8297c478bd9Sstevel@tonic-gate 		case 'P':
8307c478bd9Sstevel@tonic-gate 			identity_passphrase = optarg;
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case 'N':
8337c478bd9Sstevel@tonic-gate 			identity_new_passphrase = optarg;
8347c478bd9Sstevel@tonic-gate 			break;
8357c478bd9Sstevel@tonic-gate 		case 'C':
8367c478bd9Sstevel@tonic-gate 			identity_comment = optarg;
8377c478bd9Sstevel@tonic-gate 			break;
8387c478bd9Sstevel@tonic-gate 		case 'q':
8397c478bd9Sstevel@tonic-gate 			quiet = 1;
8407c478bd9Sstevel@tonic-gate 			break;
8417c478bd9Sstevel@tonic-gate 		case 'R':
8427c478bd9Sstevel@tonic-gate 			/* unused */
8437c478bd9Sstevel@tonic-gate 			exit(0);
8447c478bd9Sstevel@tonic-gate 			break;
8457c478bd9Sstevel@tonic-gate 		case 'e':
8467c478bd9Sstevel@tonic-gate 		case 'x':
8477c478bd9Sstevel@tonic-gate 			/* export key */
8487c478bd9Sstevel@tonic-gate 			convert_to_ssh2 = 1;
8497c478bd9Sstevel@tonic-gate 			break;
8507c478bd9Sstevel@tonic-gate 		case 'i':
8517c478bd9Sstevel@tonic-gate 		case 'X':
8527c478bd9Sstevel@tonic-gate 			/* import key */
8537c478bd9Sstevel@tonic-gate 			convert_from_ssh2 = 1;
8547c478bd9Sstevel@tonic-gate 			break;
8557c478bd9Sstevel@tonic-gate 		case 'y':
8567c478bd9Sstevel@tonic-gate 			print_public = 1;
8577c478bd9Sstevel@tonic-gate 			break;
8587c478bd9Sstevel@tonic-gate 		case 'd':
8597c478bd9Sstevel@tonic-gate 			key_type_name = "dsa";
8607c478bd9Sstevel@tonic-gate 			break;
8617c478bd9Sstevel@tonic-gate 		case 't':
8627c478bd9Sstevel@tonic-gate 			key_type_name = optarg;
8637c478bd9Sstevel@tonic-gate 			break;
8647c478bd9Sstevel@tonic-gate #ifdef SMARTCARD
8657c478bd9Sstevel@tonic-gate 		case 'D':
8667c478bd9Sstevel@tonic-gate 			download = 1;
8677c478bd9Sstevel@tonic-gate 		case 'U':
8687c478bd9Sstevel@tonic-gate 			reader_id = optarg;
8697c478bd9Sstevel@tonic-gate 			break;
8707c478bd9Sstevel@tonic-gate #endif
8717c478bd9Sstevel@tonic-gate 		case '?':
8727c478bd9Sstevel@tonic-gate 		default:
8737c478bd9Sstevel@tonic-gate 			usage();
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 	if (optind < ac) {
8777c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Too many arguments.\n"));
8787c478bd9Sstevel@tonic-gate 		usage();
8797c478bd9Sstevel@tonic-gate 	}
8807c478bd9Sstevel@tonic-gate 	if (change_passphrase && change_comment) {
8817c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Can only have one of -p and -c.\n"));
8827c478bd9Sstevel@tonic-gate 		usage();
8837c478bd9Sstevel@tonic-gate 	}
8847c478bd9Sstevel@tonic-gate 	if (print_fingerprint || print_bubblebabble)
8857c478bd9Sstevel@tonic-gate 		do_fingerprint(pw);
8867c478bd9Sstevel@tonic-gate 	if (change_passphrase)
8877c478bd9Sstevel@tonic-gate 		do_change_passphrase(pw);
8887c478bd9Sstevel@tonic-gate 	if (change_comment)
8897c478bd9Sstevel@tonic-gate 		do_change_comment(pw);
8907c478bd9Sstevel@tonic-gate 	if (convert_to_ssh2)
8917c478bd9Sstevel@tonic-gate 		do_convert_to_ssh2(pw);
8927c478bd9Sstevel@tonic-gate 	if (convert_from_ssh2)
8937c478bd9Sstevel@tonic-gate 		do_convert_from_ssh2(pw);
8947c478bd9Sstevel@tonic-gate 	if (print_public)
8957c478bd9Sstevel@tonic-gate 		do_print_public(pw);
8967c478bd9Sstevel@tonic-gate 	if (reader_id != NULL) {
8977c478bd9Sstevel@tonic-gate #ifdef SMARTCARD
8987c478bd9Sstevel@tonic-gate 		if (download)
8997c478bd9Sstevel@tonic-gate 			do_download(pw, reader_id);
9007c478bd9Sstevel@tonic-gate 		else
9017c478bd9Sstevel@tonic-gate 			do_upload(pw, reader_id);
9027c478bd9Sstevel@tonic-gate #else /* SMARTCARD */
9037c478bd9Sstevel@tonic-gate 		fatal("no support for smartcards.");
9047c478bd9Sstevel@tonic-gate #endif /* SMARTCARD */
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	arc4random_stir();
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	if (key_type_name == NULL) {
9107c478bd9Sstevel@tonic-gate 		(void) printf(gettext("You must specify a key type (-t).\n"));
9117c478bd9Sstevel@tonic-gate 		usage();
9127c478bd9Sstevel@tonic-gate 	}
9137c478bd9Sstevel@tonic-gate 	type = key_type_from_name(key_type_name);
9147c478bd9Sstevel@tonic-gate 	if (type == KEY_UNSPEC) {
9157c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("unknown key type %s\n"),
9167c478bd9Sstevel@tonic-gate 			key_type_name);
9177c478bd9Sstevel@tonic-gate 		exit(1);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	if (!quiet)
9207c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Generating public/private %s key pair.\n"),
9217c478bd9Sstevel@tonic-gate 			key_type_name);
9227c478bd9Sstevel@tonic-gate 	private = key_generate(type, bits);
9237c478bd9Sstevel@tonic-gate 	if (private == NULL) {
9247c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("key_generate failed"));
9257c478bd9Sstevel@tonic-gate 		exit(1);
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	public  = key_from_private(private);
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (!have_identity)
9307c478bd9Sstevel@tonic-gate 		ask_filename(pw, gettext("Enter file in which to save the key"));
9317c478bd9Sstevel@tonic-gate 
9327c478bd9Sstevel@tonic-gate 	/* Create ~/.ssh directory if it doesn\'t already exist. */
9337c478bd9Sstevel@tonic-gate 	(void) snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
9347c478bd9Sstevel@tonic-gate 	if (strstr(identity_file, dotsshdir) != NULL &&
9357c478bd9Sstevel@tonic-gate 	    stat(dotsshdir, &st) < 0) {
9367c478bd9Sstevel@tonic-gate 		if (mkdir(dotsshdir, 0700) < 0)
9377c478bd9Sstevel@tonic-gate 			error("Could not create directory '%s'.", dotsshdir);
9387c478bd9Sstevel@tonic-gate 		else if (!quiet)
9397c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Created directory '%s'.\n"), dotsshdir);
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 	/* If the file already exists, ask the user to confirm. */
9427c478bd9Sstevel@tonic-gate 	if (stat(identity_file, &st) >= 0) {
9437c478bd9Sstevel@tonic-gate 		char yesno[128];
9447c478bd9Sstevel@tonic-gate 		(void) printf(gettext("%s already exists.\n"), identity_file);
9457c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Overwrite (%s/%s)? "),
9467c478bd9Sstevel@tonic-gate 				      nl_langinfo(YESSTR), nl_langinfo(NOSTR));
9477c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
9487c478bd9Sstevel@tonic-gate 		if (fgets(yesno, sizeof(yesno), stdin) == NULL)
9497c478bd9Sstevel@tonic-gate 			exit(1);
950*26ba1984Sjp161948 		if (strcasecmp(chop(yesno), nl_langinfo(YESSTR)) != 0)
9517c478bd9Sstevel@tonic-gate 			exit(1);
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 	/* Ask for a passphrase (twice). */
9547c478bd9Sstevel@tonic-gate 	if (identity_passphrase)
9557c478bd9Sstevel@tonic-gate 		passphrase1 = xstrdup(identity_passphrase);
9567c478bd9Sstevel@tonic-gate 	else if (identity_new_passphrase)
9577c478bd9Sstevel@tonic-gate 		passphrase1 = xstrdup(identity_new_passphrase);
9587c478bd9Sstevel@tonic-gate 	else {
9597c478bd9Sstevel@tonic-gate passphrase_again:
9607c478bd9Sstevel@tonic-gate 		passphrase1 =
9617c478bd9Sstevel@tonic-gate 			read_passphrase(gettext("Enter passphrase (empty "
9627c478bd9Sstevel@tonic-gate 			"for no passphrase): "), RP_ALLOW_STDIN);
9637c478bd9Sstevel@tonic-gate 		passphrase2 = read_passphrase(gettext("Enter same "
9647c478bd9Sstevel@tonic-gate 			    "passphrase again: "), RP_ALLOW_STDIN);
9657c478bd9Sstevel@tonic-gate 		if (strcmp(passphrase1, passphrase2) != 0) {
9667c478bd9Sstevel@tonic-gate 			/*
9677c478bd9Sstevel@tonic-gate 			 * The passphrases do not match.  Clear them and
9687c478bd9Sstevel@tonic-gate 			 * retry.
9697c478bd9Sstevel@tonic-gate 			 */
9707c478bd9Sstevel@tonic-gate 			(void) memset(passphrase1, 0, strlen(passphrase1));
9717c478bd9Sstevel@tonic-gate 			(void) memset(passphrase2, 0, strlen(passphrase2));
9727c478bd9Sstevel@tonic-gate 			xfree(passphrase1);
9737c478bd9Sstevel@tonic-gate 			xfree(passphrase2);
9747c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Passphrases do not match.  Try "
9757c478bd9Sstevel@tonic-gate 				    "again.\n"));
9767c478bd9Sstevel@tonic-gate 			goto passphrase_again;
9777c478bd9Sstevel@tonic-gate 		}
9787c478bd9Sstevel@tonic-gate 		/* Clear the other copy of the passphrase. */
9797c478bd9Sstevel@tonic-gate 		(void) memset(passphrase2, 0, strlen(passphrase2));
9807c478bd9Sstevel@tonic-gate 		xfree(passphrase2);
9817c478bd9Sstevel@tonic-gate 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	if (identity_comment) {
9847c478bd9Sstevel@tonic-gate 		(void) strlcpy(comment, identity_comment, sizeof(comment));
9857c478bd9Sstevel@tonic-gate 	} else {
9867c478bd9Sstevel@tonic-gate 		/* Create default commend field for the passphrase. */
9877c478bd9Sstevel@tonic-gate 		(void) snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
9887c478bd9Sstevel@tonic-gate 	}
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	/* Save the key with the given passphrase and comment. */
9917c478bd9Sstevel@tonic-gate 	if (!key_save_private(private, identity_file, passphrase1, comment)) {
9927c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Saving the key failed: %s.\n"), identity_file);
9937c478bd9Sstevel@tonic-gate 		(void) memset(passphrase1, 0, strlen(passphrase1));
9947c478bd9Sstevel@tonic-gate 		xfree(passphrase1);
9957c478bd9Sstevel@tonic-gate 		exit(1);
9967c478bd9Sstevel@tonic-gate 	}
9977c478bd9Sstevel@tonic-gate 	/* Clear the passphrase. */
9987c478bd9Sstevel@tonic-gate 	(void) memset(passphrase1, 0, strlen(passphrase1));
9997c478bd9Sstevel@tonic-gate 	xfree(passphrase1);
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	/* Clear the private key and the random number generator. */
10027c478bd9Sstevel@tonic-gate 	key_free(private);
10037c478bd9Sstevel@tonic-gate 	arc4random_stir();
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if (!quiet)
10067c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Your identification has been saved in %s.\n"),
10077c478bd9Sstevel@tonic-gate 			identity_file);
10087c478bd9Sstevel@tonic-gate 
10097c478bd9Sstevel@tonic-gate 	(void) strlcat(identity_file, ".pub", sizeof(identity_file));
10107c478bd9Sstevel@tonic-gate 	fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
10117c478bd9Sstevel@tonic-gate 	if (fd == -1) {
10127c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Could not save your public key in %s\n"),
10137c478bd9Sstevel@tonic-gate 			identity_file);
10147c478bd9Sstevel@tonic-gate 		exit(1);
10157c478bd9Sstevel@tonic-gate 	}
10167c478bd9Sstevel@tonic-gate 	f = fdopen(fd, "w");
10177c478bd9Sstevel@tonic-gate 	if (f == NULL) {
10187c478bd9Sstevel@tonic-gate 		(void) printf(gettext("fdopen %s failed"), identity_file);
10197c478bd9Sstevel@tonic-gate 		exit(1);
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 	if (!key_write(public, f))
10227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("write key failed"));
10237c478bd9Sstevel@tonic-gate 	(void) fprintf(f, " %s\n", comment);
10247c478bd9Sstevel@tonic-gate 	(void) fclose(f);
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 	if (!quiet) {
10277c478bd9Sstevel@tonic-gate 		char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
10287c478bd9Sstevel@tonic-gate 		(void) printf(gettext("Your public key has been saved in %s.\n"),
10297c478bd9Sstevel@tonic-gate 		    identity_file);
10307c478bd9Sstevel@tonic-gate 		(void) printf(gettext("The key fingerprint is:\n"));
10317c478bd9Sstevel@tonic-gate 		(void) printf("%s %s\n", fp, comment);
10327c478bd9Sstevel@tonic-gate 		xfree(fp);
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	key_free(public);
10367c478bd9Sstevel@tonic-gate 	return(0);
10377c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
10387c478bd9Sstevel@tonic-gate }
1039