blob: bbd434b0b2224ca26b7ffa74eda2a64a771a09ee [file] [log] [blame]
Damien Miller6e9f6802010-09-10 11:17:38 +10001/* $OpenBSD: ssh-keygen.c,v 1.203 2010/09/02 17:21:50 naddy Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110051#include "ssh2.h"
Damien Miller874d77b2000-10-14 16:23:11 +110052
Damien Miller7ea845e2010-02-12 09:21:02 +110053#ifdef ENABLE_PKCS11
54#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000055#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000056
Damien Miller3f54a9f2005-11-05 14:52:18 +110057/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
58#define DEFAULT_BITS 2048
59#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100060#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110061u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Flag indicating that we just want to change the passphrase. This can be
65 * set on the command line.
66 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067int change_passphrase = 0;
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * Flag indicating that we just want to change the comment. This can be set
71 * on the command line.
72 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073int change_comment = 0;
74
75int quiet = 0;
76
Darren Tucker35c45532008-06-13 04:43:15 +100077int log_level = SYSLOG_LEVEL_INFO;
78
Damien Miller4b42d7f2005-03-01 21:48:35 +110079/* Flag indicating that we want to hash a known_hosts file */
80int hash_hosts = 0;
81/* Flag indicating that we want lookup a host in known_hosts file */
82int find_host = 0;
83/* Flag indicating that we want to delete a host from a known_hosts file */
84int delete_host = 0;
85
Damien Millerf2b70ca2010-03-05 07:39:35 +110086/* Flag indicating that we want to show the contents of a certificate */
87int show_cert = 0;
88
Damien Miller10f6f6b1999-11-17 17:29:08 +110089/* Flag indicating that we just want to see the key fingerprint */
90int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000091int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110092
Damien Miller431f66b1999-11-21 18:31:57 +110093/* The identity file name, given on the command line or entered by the user. */
94char identity_file[1024];
95int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* This is set to the passphrase if given on the command line. */
98char *identity_passphrase = NULL;
99
100/* This is set to the new passphrase if given on the command line. */
101char *identity_new_passphrase = NULL;
102
103/* This is set to the new comment if given on the command line. */
104char *identity_comment = NULL;
105
Damien Miller0a80ca12010-02-27 07:55:05 +1100106/* Path to CA key when certifying keys. */
107char *ca_key_path = NULL;
108
Damien Miller4e270b02010-04-16 15:56:21 +1000109/* Certificate serial number */
110long long cert_serial = 0;
111
Damien Miller0a80ca12010-02-27 07:55:05 +1100112/* Key type when certifying */
113u_int cert_key_type = SSH2_CERT_TYPE_USER;
114
115/* "key ID" of signed key */
116char *cert_key_id = NULL;
117
118/* Comma-separated list of principal names for certifying keys */
119char *cert_principals = NULL;
120
121/* Validity period for certificates */
122u_int64_t cert_valid_from = 0;
123u_int64_t cert_valid_to = ~0ULL;
124
Damien Miller4e270b02010-04-16 15:56:21 +1000125/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000126#define CERTOPT_X_FWD (1)
127#define CERTOPT_AGENT_FWD (1<<1)
128#define CERTOPT_PORT_FWD (1<<2)
129#define CERTOPT_PTY (1<<3)
130#define CERTOPT_USER_RC (1<<4)
131#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
132 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
133u_int32_t certflags_flags = CERTOPT_DEFAULT;
134char *certflags_command = NULL;
135char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100136
Damien Miller44b25042010-07-02 13:35:01 +1000137/* Conversion to/from various formats */
138int convert_to = 0;
139int convert_from = 0;
140enum {
141 FMT_RFC4716,
142 FMT_PKCS8,
143 FMT_PEM
144} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000145int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000146int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100147
Damien Millera41c8b12002-01-22 23:05:08 +1100148char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000149
Damien Miller757f34e2010-08-05 13:05:31 +1000150/* Load key from this PKCS#11 provider */
151char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000152
Damien Miller431f66b1999-11-21 18:31:57 +1100153/* argv0 */
154extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Millereba71ba2000-04-29 23:57:08 +1000156char hostname[MAXHOSTNAMELEN];
157
Darren Tucker770fc012004-05-13 16:24:32 +1000158/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000159int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000160int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
161
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void
Damien Miller431f66b1999-11-21 18:31:57 +1100163ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000164{
Damien Miller95def091999-11-25 00:26:21 +1100165 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100166 char *name = NULL;
167
Damien Miller993dd552002-02-19 15:22:47 +1100168 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000169 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100170 else {
Damien Miller993dd552002-02-19 15:22:47 +1100171 switch (key_type_from_name(key_type_name)) {
172 case KEY_RSA1:
173 name = _PATH_SSH_CLIENT_IDENTITY;
174 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000175 case KEY_DSA_CERT:
176 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100177 case KEY_DSA:
178 name = _PATH_SSH_CLIENT_ID_DSA;
179 break;
Damien Millereb8b60e2010-08-31 22:41:14 +1000180 case KEY_ECDSA_CERT:
181 case KEY_ECDSA:
182 name = _PATH_SSH_CLIENT_ID_ECDSA;
183 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000184 case KEY_RSA_CERT:
185 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100186 case KEY_RSA:
187 name = _PATH_SSH_CLIENT_ID_RSA;
188 break;
189 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100190 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100191 exit(1);
192 break;
193 }
Damien Miller90967402006-03-26 14:07:26 +1100194 }
Damien Millere39cacc2000-11-29 12:18:44 +1100195 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000196 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100197 if (fgets(buf, sizeof(buf), stdin) == NULL)
198 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000199 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100200 if (strcmp(buf, "") != 0)
201 strlcpy(identity_file, buf, sizeof(identity_file));
202 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100203}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
Ben Lindstrombba81212001-06-25 05:01:22 +0000205static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000206load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000207{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000208 char *pass;
209 Key *prv;
210
Ben Lindstroma3700052001-04-05 23:26:32 +0000211 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000212 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000213 if (identity_passphrase)
214 pass = xstrdup(identity_passphrase);
215 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000216 pass = read_passphrase("Enter passphrase: ",
217 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000218 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000219 memset(pass, 0, strlen(pass));
220 xfree(pass);
221 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000222 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000223}
224
Damien Miller874d77b2000-10-14 16:23:11 +1100225#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000226#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100227#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000228#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000229
Ben Lindstrombba81212001-06-25 05:01:22 +0000230static void
Damien Miller44b25042010-07-02 13:35:01 +1000231do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000232{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000233 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000234 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100235 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000236
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000237 if (key_to_blob(k, &blob, &len) <= 0) {
238 fprintf(stderr, "key_to_blob failed\n");
239 exit(1);
240 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100241 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
242 snprintf(comment, sizeof(comment),
243 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000244 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000245 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100246
247 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
248 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000249 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100250 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000251 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000252 xfree(blob);
253 exit(0);
254}
255
Ben Lindstrombba81212001-06-25 05:01:22 +0000256static void
Damien Miller44b25042010-07-02 13:35:01 +1000257do_convert_to_pkcs8(Key *k)
258{
259 switch (key_type_plain(k->type)) {
260 case KEY_RSA:
261 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
262 fatal("PEM_write_RSA_PUBKEY failed");
263 break;
264 case KEY_DSA:
265 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
266 fatal("PEM_write_DSA_PUBKEY failed");
267 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000268#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000269 case KEY_ECDSA:
270 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
271 fatal("PEM_write_EC_PUBKEY failed");
272 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000273#endif
Damien Miller44b25042010-07-02 13:35:01 +1000274 default:
275 fatal("%s: unsupported key type %s", __func__, key_type(k));
276 }
277 exit(0);
278}
279
280static void
281do_convert_to_pem(Key *k)
282{
283 switch (key_type_plain(k->type)) {
284 case KEY_RSA:
285 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
286 fatal("PEM_write_RSAPublicKey failed");
287 break;
288#if notyet /* OpenSSH 0.9.8 lacks this function */
289 case KEY_DSA:
290 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
291 fatal("PEM_write_DSAPublicKey failed");
292 break;
293#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000294 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000295 default:
296 fatal("%s: unsupported key type %s", __func__, key_type(k));
297 }
298 exit(0);
299}
300
301static void
302do_convert_to(struct passwd *pw)
303{
304 Key *k;
305 struct stat st;
306
307 if (!have_identity)
308 ask_filename(pw, "Enter file in which the key is");
309 if (stat(identity_file, &st) < 0)
310 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
311 if ((k = key_load_public(identity_file, NULL)) == NULL) {
312 if ((k = load_identity(identity_file)) == NULL) {
313 fprintf(stderr, "load failed\n");
314 exit(1);
315 }
316 }
317 if (k->type == KEY_RSA1) {
318 fprintf(stderr, "version 1 keys are not supported\n");
319 exit(1);
320 }
321
322 switch (convert_format) {
323 case FMT_RFC4716:
324 do_convert_to_ssh2(pw, k);
325 break;
326 case FMT_PKCS8:
327 do_convert_to_pkcs8(k);
328 break;
329 case FMT_PEM:
330 do_convert_to_pem(k);
331 break;
332 default:
333 fatal("%s: unknown key format %d", __func__, convert_format);
334 }
335 exit(0);
336}
337
338static void
Damien Miller874d77b2000-10-14 16:23:11 +1100339buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
340{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000341 u_int bignum_bits = buffer_get_int(b);
342 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000343
Damien Miller874d77b2000-10-14 16:23:11 +1100344 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000345 fatal("buffer_get_bignum_bits: input buffer too small: "
346 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100347 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
348 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100349 buffer_consume(b, bytes);
350}
351
Ben Lindstrombba81212001-06-25 05:01:22 +0000352static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000353do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100354{
355 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100356 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100357 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000358 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000359 int magic, rlen, ktype, i1, i2, i3, i4;
360 u_int slen;
361 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100362
363 buffer_init(&b);
364 buffer_append(&b, blob, blen);
365
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100366 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100367 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
368 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
369 buffer_free(&b);
370 return NULL;
371 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000372 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100373 type = buffer_get_string(&b, NULL);
374 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000375 i2 = buffer_get_int(&b);
376 i3 = buffer_get_int(&b);
377 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100378 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100379 if (strcmp(cipher, "none") != 0) {
380 error("unsupported cipher %s", cipher);
381 xfree(cipher);
382 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000383 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100384 return NULL;
385 }
386 xfree(cipher);
387
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000388 if (strstr(type, "dsa")) {
389 ktype = KEY_DSA;
390 } else if (strstr(type, "rsa")) {
391 ktype = KEY_RSA;
392 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100393 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000394 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100395 return NULL;
396 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000397 key = key_new_private(ktype);
398 xfree(type);
399
400 switch (key->type) {
401 case KEY_DSA:
402 buffer_get_bignum_bits(&b, key->dsa->p);
403 buffer_get_bignum_bits(&b, key->dsa->g);
404 buffer_get_bignum_bits(&b, key->dsa->q);
405 buffer_get_bignum_bits(&b, key->dsa->pub_key);
406 buffer_get_bignum_bits(&b, key->dsa->priv_key);
407 break;
408 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100409 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000410 debug("e %lx", e);
411 if (e < 30) {
412 e <<= 8;
413 e += buffer_get_char(&b);
414 debug("e %lx", e);
415 e <<= 8;
416 e += buffer_get_char(&b);
417 debug("e %lx", e);
418 }
419 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000420 buffer_free(&b);
421 key_free(key);
422 return NULL;
423 }
424 buffer_get_bignum_bits(&b, key->rsa->d);
425 buffer_get_bignum_bits(&b, key->rsa->n);
426 buffer_get_bignum_bits(&b, key->rsa->iqmp);
427 buffer_get_bignum_bits(&b, key->rsa->q);
428 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000429 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000430 break;
431 }
Damien Miller874d77b2000-10-14 16:23:11 +1100432 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000433 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000434 error("do_convert_private_ssh2_from_blob: "
435 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100436 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000437
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000438 /* try the key */
439 key_sign(key, &sig, &slen, data, sizeof(data));
440 key_verify(key, sig, slen, data, sizeof(data));
441 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100442 return key;
443}
444
Damien Miller8056a9d2006-03-15 12:05:40 +1100445static int
446get_line(FILE *fp, char *line, size_t len)
447{
448 int c;
449 size_t pos = 0;
450
451 line[0] = '\0';
452 while ((c = fgetc(fp)) != EOF) {
453 if (pos >= len - 1) {
454 fprintf(stderr, "input line too long.\n");
455 exit(1);
456 }
Damien Miller90967402006-03-26 14:07:26 +1100457 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100458 case '\r':
459 c = fgetc(fp);
460 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
461 fprintf(stderr, "unget: %s\n", strerror(errno));
462 exit(1);
463 }
464 return pos;
465 case '\n':
466 return pos;
467 }
468 line[pos++] = c;
469 line[pos] = '\0';
470 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100471 /* We reached EOF */
472 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100473}
474
Ben Lindstrombba81212001-06-25 05:01:22 +0000475static void
Damien Miller44b25042010-07-02 13:35:01 +1000476do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000477{
Damien Millereba71ba2000-04-29 23:57:08 +1000478 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000479 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100480 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000481 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000482 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000483 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000484 FILE *fp;
485
Damien Millerba3420a2010-06-26 09:39:07 +1000486 if ((fp = fopen(identity_file, "r")) == NULL)
487 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000488 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100489 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
490 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000491 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000492 if (strncmp(line, "----", 4) == 0 ||
493 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100494 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000495 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000496 if (strstr(line, " END ") != NULL) {
497 break;
498 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000499 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000500 continue;
501 }
Damien Miller30c3d422000-05-09 11:02:59 +1000502 if (escaped) {
503 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000504 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000505 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000506 }
Damien Millereba71ba2000-04-29 23:57:08 +1000507 strlcat(encoded, line, sizeof(encoded));
508 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000509 len = strlen(encoded);
510 if (((len % 4) == 3) &&
511 (encoded[len-1] == '=') &&
512 (encoded[len-2] == '=') &&
513 (encoded[len-3] == '='))
514 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100515 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000516 if (blen < 0) {
517 fprintf(stderr, "uudecode failed.\n");
518 exit(1);
519 }
Damien Miller44b25042010-07-02 13:35:01 +1000520 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100521 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100522 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000523 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100524 fprintf(stderr, "decode blob failed.\n");
525 exit(1);
526 }
Damien Miller44b25042010-07-02 13:35:01 +1000527 fclose(fp);
528}
529
530static void
531do_convert_from_pkcs8(Key **k, int *private)
532{
533 EVP_PKEY *pubkey;
534 FILE *fp;
535
536 if ((fp = fopen(identity_file, "r")) == NULL)
537 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
538 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
539 fatal("%s: %s is not a recognised public key format", __func__,
540 identity_file);
541 }
542 fclose(fp);
543 switch (EVP_PKEY_type(pubkey->type)) {
544 case EVP_PKEY_RSA:
545 *k = key_new(KEY_UNSPEC);
546 (*k)->type = KEY_RSA;
547 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
548 break;
549 case EVP_PKEY_DSA:
550 *k = key_new(KEY_UNSPEC);
551 (*k)->type = KEY_DSA;
552 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
553 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000554#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000555 case EVP_PKEY_EC:
556 *k = key_new(KEY_UNSPEC);
557 (*k)->type = KEY_ECDSA;
558 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
559 (*k)->ecdsa_nid = key_ecdsa_group_to_nid(
560 EC_KEY_get0_group((*k)->ecdsa));
561 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000562#endif
Damien Miller44b25042010-07-02 13:35:01 +1000563 default:
564 fatal("%s: unsupported pubkey type %d", __func__,
565 EVP_PKEY_type(pubkey->type));
566 }
567 EVP_PKEY_free(pubkey);
568 return;
569}
570
571static void
572do_convert_from_pem(Key **k, int *private)
573{
574 FILE *fp;
575 RSA *rsa;
576#ifdef notyet
577 DSA *dsa;
578#endif
579
580 if ((fp = fopen(identity_file, "r")) == NULL)
581 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
582 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
583 *k = key_new(KEY_UNSPEC);
584 (*k)->type = KEY_RSA;
585 (*k)->rsa = rsa;
586 fclose(fp);
587 return;
588 }
589#if notyet /* OpenSSH 0.9.8 lacks this function */
590 rewind(fp);
591 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
592 *k = key_new(KEY_UNSPEC);
593 (*k)->type = KEY_DSA;
594 (*k)->dsa = dsa;
595 fclose(fp);
596 return;
597 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000598 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000599#endif
600 fatal("%s: unrecognised raw private key format", __func__);
601}
602
603static void
604do_convert_from(struct passwd *pw)
605{
606 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000607 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000608 struct stat st;
609
610 if (!have_identity)
611 ask_filename(pw, "Enter file in which the key is");
612 if (stat(identity_file, &st) < 0)
613 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
614
615 switch (convert_format) {
616 case FMT_RFC4716:
617 do_convert_from_ssh2(pw, &k, &private);
618 break;
619 case FMT_PKCS8:
620 do_convert_from_pkcs8(&k, &private);
621 break;
622 case FMT_PEM:
623 do_convert_from_pem(&k, &private);
624 break;
625 default:
626 fatal("%s: unknown key format %d", __func__, convert_format);
627 }
628
629 if (!private)
630 ok = key_write(k, stdout);
631 if (ok)
632 fprintf(stdout, "\n");
633 else {
634 switch (k->type) {
635 case KEY_DSA:
636 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
637 NULL, 0, NULL, NULL);
638 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000639#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000640 case KEY_ECDSA:
641 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
642 NULL, 0, NULL, NULL);
643 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000644#endif
Damien Miller44b25042010-07-02 13:35:01 +1000645 case KEY_RSA:
646 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
647 NULL, 0, NULL, NULL);
648 break;
649 default:
650 fatal("%s: unsupported key type %s", __func__,
651 key_type(k));
652 }
653 }
654
Damien Miller874d77b2000-10-14 16:23:11 +1100655 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100656 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100657 exit(1);
658 }
Damien Millereba71ba2000-04-29 23:57:08 +1000659 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000660 exit(0);
661}
662
Ben Lindstrombba81212001-06-25 05:01:22 +0000663static void
Damien Millereba71ba2000-04-29 23:57:08 +1000664do_print_public(struct passwd *pw)
665{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000666 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000667 struct stat st;
668
669 if (!have_identity)
670 ask_filename(pw, "Enter file in which the key is");
671 if (stat(identity_file, &st) < 0) {
672 perror(identity_file);
673 exit(1);
674 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000675 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000676 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000677 fprintf(stderr, "load failed\n");
678 exit(1);
679 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000680 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000681 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000682 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000683 fprintf(stdout, "\n");
684 exit(0);
685}
686
Ben Lindstromcd392282001-07-04 03:44:03 +0000687static void
Damien Miller757f34e2010-08-05 13:05:31 +1000688do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000689{
Damien Miller7ea845e2010-02-12 09:21:02 +1100690#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000691 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100692 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000693
Damien Miller7ea845e2010-02-12 09:21:02 +1100694 pkcs11_init(0);
695 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
696 if (nkeys <= 0)
697 fatal("cannot read public key from pkcs11");
698 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000699 key_write(keys[i], stdout);
700 key_free(keys[i]);
701 fprintf(stdout, "\n");
702 }
703 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100704 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000705 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100706#else
707 fatal("no pkcs11 support");
708#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000709}
Ben Lindstromcd392282001-07-04 03:44:03 +0000710
Ben Lindstrombba81212001-06-25 05:01:22 +0000711static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100712do_fingerprint(struct passwd *pw)
713{
Damien Miller98c7ad62000-03-09 21:27:49 +1100714 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000715 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000716 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100717 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000718 enum fp_rep rep;
719 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100720 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100721
Ben Lindstromd0fca422001-03-26 13:44:06 +0000722 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
723 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000724
Damien Miller95def091999-11-25 00:26:21 +1100725 if (!have_identity)
726 ask_filename(pw, "Enter file in which the key is");
727 if (stat(identity_file, &st) < 0) {
728 perror(identity_file);
729 exit(1);
730 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000731 public = key_load_public(identity_file, &comment);
732 if (public != NULL) {
733 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100734 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000735 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
736 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000737 if (log_level >= SYSLOG_LEVEL_VERBOSE)
738 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100739 key_free(public);
740 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000741 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000742 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100743 exit(0);
744 }
Damien Miller40b59852006-06-13 13:00:25 +1000745 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000746 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000747 comment = NULL;
748 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100749
Damien Millerba3420a2010-06-26 09:39:07 +1000750 if ((f = fopen(identity_file, "r")) == NULL)
751 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100752
Damien Millerba3420a2010-06-26 09:39:07 +1000753 while (fgets(line, sizeof(line), f)) {
754 if ((cp = strchr(line, '\n')) == NULL) {
755 error("line %d too long: %.40s...",
756 num + 1, line);
757 skip = 1;
758 continue;
Damien Miller95def091999-11-25 00:26:21 +1100759 }
Damien Millerba3420a2010-06-26 09:39:07 +1000760 num++;
761 if (skip) {
762 skip = 0;
763 continue;
764 }
765 *cp = '\0';
766
767 /* Skip leading whitespace, empty and comment lines. */
768 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
769 ;
770 if (!*cp || *cp == '\n' || *cp == '#')
771 continue;
772 i = strtol(cp, &ep, 10);
773 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
774 int quoted = 0;
775 comment = cp;
776 for (; *cp && (quoted || (*cp != ' ' &&
777 *cp != '\t')); cp++) {
778 if (*cp == '\\' && cp[1] == '"')
779 cp++; /* Skip both */
780 else if (*cp == '"')
781 quoted = !quoted;
782 }
783 if (!*cp)
784 continue;
785 *cp++ = '\0';
786 }
787 ep = cp;
788 public = key_new(KEY_RSA1);
789 if (key_read(public, &cp) != 1) {
790 cp = ep;
791 key_free(public);
792 public = key_new(KEY_UNSPEC);
793 if (key_read(public, &cp) != 1) {
794 key_free(public);
795 continue;
796 }
797 }
798 comment = *cp ? cp : comment;
799 fp = key_fingerprint(public, fptype, rep);
800 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
801 printf("%u %s %s (%s)\n", key_size(public), fp,
802 comment ? comment : "no comment", key_type(public));
803 if (log_level >= SYSLOG_LEVEL_VERBOSE)
804 printf("%s\n", ra);
805 xfree(ra);
806 xfree(fp);
807 key_free(public);
808 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100809 }
Damien Millerba3420a2010-06-26 09:39:07 +1000810 fclose(f);
811
Damien Miller98c7ad62000-03-09 21:27:49 +1100812 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100813 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100814 exit(1);
815 }
Damien Miller95def091999-11-25 00:26:21 +1100816 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100817}
818
Damien Miller4b42d7f2005-03-01 21:48:35 +1100819static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100820printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100821{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000822 if (print_fingerprint) {
823 enum fp_rep rep;
824 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000825 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000826
827 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
828 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
829 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100830 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000831 printf("%u %s %s (%s)\n", key_size(public), fp, name,
832 key_type(public));
833 if (log_level >= SYSLOG_LEVEL_VERBOSE)
834 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000835 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000836 xfree(fp);
837 } else {
838 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
839 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100840 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000841 if (!key_write(public, f))
842 fatal("key_write failed");
843 fprintf(f, "\n");
844 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100845}
846
847static void
848do_known_hosts(struct passwd *pw, const char *name)
849{
850 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100851 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100852 char *cp, *cp2, *kp, *kp2;
853 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100854 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100855 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100856
857 if (!have_identity) {
858 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
859 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
860 sizeof(identity_file))
861 fatal("Specified known hosts path too long");
862 xfree(cp);
863 have_identity = 1;
864 }
865 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +1000866 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +1100867
868 /*
869 * Find hosts goes to stdout, hash and deletions happen in-place
870 * A corner case is ssh-keygen -HF foo, which should go to stdout
871 */
872 if (!find_host && (hash_hosts || delete_host)) {
873 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
874 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
875 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
876 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
877 fatal("known_hosts path too long");
878 umask(077);
879 if ((c = mkstemp(tmp)) == -1)
880 fatal("mkstemp: %s", strerror(errno));
881 if ((out = fdopen(c, "w")) == NULL) {
882 c = errno;
883 unlink(tmp);
884 fatal("fdopen: %s", strerror(c));
885 }
886 inplace = 1;
887 }
888
889 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +1000890 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +1100891 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100892 skip = 1;
893 invalid = 1;
894 continue;
895 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000896 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100897 if (skip) {
898 skip = 0;
899 continue;
900 }
Damien Miller0f4ed692007-10-26 14:26:32 +1000901 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +1100902
903 /* Skip leading whitespace, empty and comment lines. */
904 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
905 ;
906 if (!*cp || *cp == '\n' || *cp == '#') {
907 if (inplace)
908 fprintf(out, "%s\n", cp);
909 continue;
910 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100911 /* Check whether this is a CA key */
912 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
913 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
914 cp[sizeof(CA_MARKER) - 1] == '\t')) {
915 ca = 1;
916 cp += sizeof(CA_MARKER);
917 } else
918 ca = 0;
919
Damien Miller4b42d7f2005-03-01 21:48:35 +1100920 /* Find the end of the host name portion. */
921 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
922 ;
Damien Miller0a80ca12010-02-27 07:55:05 +1100923
Damien Miller4b42d7f2005-03-01 21:48:35 +1100924 if (*kp == '\0' || *(kp + 1) == '\0') {
925 error("line %d missing key: %.40s...",
926 num, line);
927 invalid = 1;
928 continue;
929 }
930 *kp++ = '\0';
931 kp2 = kp;
932
Damien Miller0a80ca12010-02-27 07:55:05 +1100933 pub = key_new(KEY_RSA1);
934 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100935 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +1100936 key_free(pub);
937 pub = key_new(KEY_UNSPEC);
938 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +1100939 error("line %d invalid key: %.40s...",
940 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +1100941 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100942 invalid = 1;
943 continue;
944 }
945 }
946
947 if (*cp == HASH_DELIM) {
948 if (find_host || delete_host) {
949 cp2 = host_hash(name, cp, strlen(cp));
950 if (cp2 == NULL) {
951 error("line %d: invalid hashed "
952 "name: %.64s...", num, line);
953 invalid = 1;
954 continue;
955 }
956 c = (strcmp(cp2, cp) == 0);
957 if (find_host && c) {
958 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100959 "line %d type %s%s\n", name,
960 num, key_type(pub),
961 ca ? " (CA key)" : "");
962 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100963 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100964 if (delete_host && !c && !ca)
965 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100966 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +1100967 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100968 } else {
969 if (find_host || delete_host) {
970 c = (match_hostname(name, cp,
971 strlen(cp)) == 1);
972 if (find_host && c) {
973 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +1100974 "line %d type %s%s\n", name,
975 num, key_type(pub),
976 ca ? " (CA key)" : "");
977 printhost(out, name, pub,
978 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100979 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100980 if (delete_host && !c && !ca)
981 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +1100982 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +1100983 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +1100984 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +1100985 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100986 if (ca) {
987 fprintf(stderr, "Warning: "
988 "ignoring CA key for host: "
989 "%.64s\n", cp2);
990 printhost(out, cp2, pub, ca, 0);
991 } else if (strcspn(cp2, "*?!") !=
992 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +1100993 fprintf(stderr, "Warning: "
994 "ignoring host name with "
995 "metacharacters: %.64s\n",
996 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +1100997 printhost(out, cp2, pub, ca, 0);
998 } else
999 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001000 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001001 has_unhashed = 1;
1002 }
1003 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001004 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001005 }
1006 fclose(in);
1007
1008 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001009 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001010 identity_file);
1011 if (inplace) {
1012 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001013 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001014 fclose(out);
1015 unlink(tmp);
1016 }
1017 exit(1);
1018 }
1019
1020 if (inplace) {
1021 fclose(out);
1022
1023 /* Backup existing file */
1024 if (unlink(old) == -1 && errno != ENOENT)
1025 fatal("unlink %.100s: %s", old, strerror(errno));
1026 if (link(identity_file, old) == -1)
1027 fatal("link %.100s to %.100s: %s", identity_file, old,
1028 strerror(errno));
1029 /* Move new one into place */
1030 if (rename(tmp, identity_file) == -1) {
1031 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1032 strerror(errno));
1033 unlink(tmp);
1034 unlink(old);
1035 exit(1);
1036 }
1037
1038 fprintf(stderr, "%s updated.\n", identity_file);
1039 fprintf(stderr, "Original contents retained as %s\n", old);
1040 if (has_unhashed) {
1041 fprintf(stderr, "WARNING: %s contains unhashed "
1042 "entries\n", old);
1043 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001044 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001045 }
1046 }
1047
1048 exit(0);
1049}
1050
Damien Miller95def091999-11-25 00:26:21 +11001051/*
1052 * Perform changing a passphrase. The argument is the passwd structure
1053 * for the current user.
1054 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001055static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001056do_change_passphrase(struct passwd *pw)
1057{
Damien Miller95def091999-11-25 00:26:21 +11001058 char *comment;
1059 char *old_passphrase, *passphrase1, *passphrase2;
1060 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001061 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001062
Damien Miller95def091999-11-25 00:26:21 +11001063 if (!have_identity)
1064 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001065 if (stat(identity_file, &st) < 0) {
1066 perror(identity_file);
1067 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001068 }
Damien Miller95def091999-11-25 00:26:21 +11001069 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001070 private = key_load_private(identity_file, "", &comment);
1071 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001072 if (identity_passphrase)
1073 old_passphrase = xstrdup(identity_passphrase);
1074 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001075 old_passphrase =
1076 read_passphrase("Enter old passphrase: ",
1077 RP_ALLOW_STDIN);
1078 private = key_load_private(identity_file, old_passphrase,
1079 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001080 memset(old_passphrase, 0, strlen(old_passphrase));
1081 xfree(old_passphrase);
1082 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001083 printf("Bad passphrase.\n");
1084 exit(1);
1085 }
Damien Miller95def091999-11-25 00:26:21 +11001086 }
1087 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001088
Damien Miller95def091999-11-25 00:26:21 +11001089 /* Ask the new passphrase (twice). */
1090 if (identity_new_passphrase) {
1091 passphrase1 = xstrdup(identity_new_passphrase);
1092 passphrase2 = NULL;
1093 } else {
1094 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001095 read_passphrase("Enter new passphrase (empty for no "
1096 "passphrase): ", RP_ALLOW_STDIN);
1097 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001098 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001099
1100 /* Verify that they are the same. */
1101 if (strcmp(passphrase1, passphrase2) != 0) {
1102 memset(passphrase1, 0, strlen(passphrase1));
1103 memset(passphrase2, 0, strlen(passphrase2));
1104 xfree(passphrase1);
1105 xfree(passphrase2);
1106 printf("Pass phrases do not match. Try again.\n");
1107 exit(1);
1108 }
1109 /* Destroy the other copy. */
1110 memset(passphrase2, 0, strlen(passphrase2));
1111 xfree(passphrase2);
1112 }
1113
1114 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001115 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001116 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001117 memset(passphrase1, 0, strlen(passphrase1));
1118 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001119 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001120 xfree(comment);
1121 exit(1);
1122 }
1123 /* Destroy the passphrase and the copy of the key in memory. */
1124 memset(passphrase1, 0, strlen(passphrase1));
1125 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001126 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +11001127 xfree(comment);
1128
1129 printf("Your identification has been saved with the new passphrase.\n");
1130 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001131}
1132
Damien Miller37876e92003-05-15 10:19:46 +10001133/*
1134 * Print the SSHFP RR.
1135 */
Damien Millercb314822006-03-26 13:48:01 +11001136static int
1137do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001138{
1139 Key *public;
1140 char *comment = NULL;
1141 struct stat st;
1142
Damien Millercb314822006-03-26 13:48:01 +11001143 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +10001144 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +11001145 if (stat(fname, &st) < 0) {
1146 if (errno == ENOENT)
1147 return 0;
1148 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001149 exit(1);
1150 }
Damien Millercb314822006-03-26 13:48:01 +11001151 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001152 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001153 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001154 key_free(public);
1155 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +11001156 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001157 }
1158 if (comment)
1159 xfree(comment);
1160
Damien Millercb314822006-03-26 13:48:01 +11001161 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001162 exit(1);
1163}
Damien Miller37876e92003-05-15 10:19:46 +10001164
Damien Miller95def091999-11-25 00:26:21 +11001165/*
1166 * Change the comment of a private key file.
1167 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001168static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001169do_change_comment(struct passwd *pw)
1170{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001171 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001172 Key *private;
1173 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001174 struct stat st;
1175 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001176 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001177
Damien Miller95def091999-11-25 00:26:21 +11001178 if (!have_identity)
1179 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001180 if (stat(identity_file, &st) < 0) {
1181 perror(identity_file);
1182 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001183 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001184 private = key_load_private(identity_file, "", &comment);
1185 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001186 if (identity_passphrase)
1187 passphrase = xstrdup(identity_passphrase);
1188 else if (identity_new_passphrase)
1189 passphrase = xstrdup(identity_new_passphrase);
1190 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001191 passphrase = read_passphrase("Enter passphrase: ",
1192 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001193 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001194 private = key_load_private(identity_file, passphrase, &comment);
1195 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001196 memset(passphrase, 0, strlen(passphrase));
1197 xfree(passphrase);
1198 printf("Bad passphrase.\n");
1199 exit(1);
1200 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001201 } else {
1202 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001203 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001204 if (private->type != KEY_RSA1) {
1205 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1206 key_free(private);
1207 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001208 }
Damien Miller95def091999-11-25 00:26:21 +11001209 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001210
Damien Miller95def091999-11-25 00:26:21 +11001211 if (identity_comment) {
1212 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1213 } else {
1214 printf("Enter new comment: ");
1215 fflush(stdout);
1216 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1217 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001218 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001219 exit(1);
1220 }
Damien Miller14b017d2007-09-17 16:09:15 +10001221 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001222 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001223
Damien Miller95def091999-11-25 00:26:21 +11001224 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001225 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001226 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001227 memset(passphrase, 0, strlen(passphrase));
1228 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001229 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001230 xfree(comment);
1231 exit(1);
1232 }
Damien Miller95def091999-11-25 00:26:21 +11001233 memset(passphrase, 0, strlen(passphrase));
1234 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001235 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001236 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001237
Damien Miller95def091999-11-25 00:26:21 +11001238 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001239 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1240 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001241 printf("Could not save your public key in %s\n", identity_file);
1242 exit(1);
1243 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001244 f = fdopen(fd, "w");
1245 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001246 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001247 exit(1);
1248 }
Damien Millereba71ba2000-04-29 23:57:08 +10001249 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001250 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001251 key_free(public);
1252 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001253 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001254
Damien Miller95def091999-11-25 00:26:21 +11001255 xfree(comment);
1256
1257 printf("The comment in your key file has been changed.\n");
1258 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001259}
1260
Damien Miller0a80ca12010-02-27 07:55:05 +11001261static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001262fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001263{
1264 char from[32], to[32];
1265 static char ret[64];
1266 time_t tt;
1267 struct tm *tm;
1268
1269 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001270 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001271 return "forever";
1272
Damien Millerf2b70ca2010-03-05 07:39:35 +11001273 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001274 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001275 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001276 tm = localtime(&tt);
1277 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1278 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001279 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001280 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001281 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001282 tm = localtime(&tt);
1283 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1284 }
1285
Damien Millerf2b70ca2010-03-05 07:39:35 +11001286 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001287 snprintf(ret, sizeof(ret), "before %s", to);
1288 return ret;
1289 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001290 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001291 snprintf(ret, sizeof(ret), "after %s", from);
1292 return ret;
1293 }
1294
1295 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1296 return ret;
1297}
1298
1299static void
Damien Miller4e270b02010-04-16 15:56:21 +10001300add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001301{
1302 debug3("%s: %s", __func__, name);
1303 buffer_put_cstring(c, name);
1304 buffer_put_string(c, NULL, 0);
1305}
1306
1307static void
Damien Miller4e270b02010-04-16 15:56:21 +10001308add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001309{
1310 Buffer b;
1311
1312 debug3("%s: %s=%s", __func__, name, value);
1313 buffer_init(&b);
1314 buffer_put_cstring(&b, value);
1315
1316 buffer_put_cstring(c, name);
1317 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1318
1319 buffer_free(&b);
1320}
1321
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001322#define OPTIONS_CRITICAL 1
1323#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001324static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001325prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001326{
Damien Miller0a80ca12010-02-27 07:55:05 +11001327 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001328 if ((which & OPTIONS_CRITICAL) != 0 &&
1329 certflags_command != NULL)
1330 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001331 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1332 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001333 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001334 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1335 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001336 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001337 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1338 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001339 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001340 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1341 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001342 add_flag_option(c, "permit-user-rc");
Damien Miller1da63882010-08-05 13:03:51 +10001343 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1344 (certflags_flags & CERTOPT_X_FWD) != 0)
1345 add_flag_option(c, "permit-X11-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001346 if ((which & OPTIONS_CRITICAL) != 0 &&
1347 certflags_src_addr != NULL)
1348 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001349}
1350
Damien Miller757f34e2010-08-05 13:05:31 +10001351static Key *
1352load_pkcs11_key(char *path)
1353{
1354#ifdef ENABLE_PKCS11
1355 Key **keys = NULL, *public, *private = NULL;
1356 int i, nkeys;
1357
1358 if ((public = key_load_public(path, NULL)) == NULL)
1359 fatal("Couldn't load CA public key \"%s\"", path);
1360
1361 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1362 debug3("%s: %d keys", __func__, nkeys);
1363 if (nkeys <= 0)
1364 fatal("cannot read public key from pkcs11");
1365 for (i = 0; i < nkeys; i++) {
1366 if (key_equal_public(public, keys[i])) {
1367 private = keys[i];
1368 continue;
1369 }
1370 key_free(keys[i]);
1371 }
1372 xfree(keys);
1373 key_free(public);
1374 return private;
1375#else
1376 fatal("no pkcs11 support");
1377#endif /* ENABLE_PKCS11 */
1378}
1379
Damien Miller0a80ca12010-02-27 07:55:05 +11001380static void
1381do_ca_sign(struct passwd *pw, int argc, char **argv)
1382{
1383 int i, fd;
1384 u_int n;
1385 Key *ca, *public;
1386 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1387 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001388 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001389
Damien Miller4e270b02010-04-16 15:56:21 +10001390 if (key_type_name != NULL) {
1391 switch (key_type_from_name(key_type_name)) {
1392 case KEY_RSA_CERT_V00:
1393 case KEY_DSA_CERT_V00:
1394 v00 = 1;
1395 break;
1396 case KEY_UNSPEC:
1397 if (strcasecmp(key_type_name, "v00") == 0) {
1398 v00 = 1;
1399 break;
1400 } else if (strcasecmp(key_type_name, "v01") == 0)
1401 break;
1402 /* FALLTHROUGH */
1403 default:
1404 fprintf(stderr, "unknown key type %s\n", key_type_name);
1405 exit(1);
1406 }
1407 }
1408
Damien Miller757f34e2010-08-05 13:05:31 +10001409 pkcs11_init(1);
1410 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1411 if (pkcs11provider != NULL) {
1412 if ((ca = load_pkcs11_key(tmp)) == NULL)
1413 fatal("No PKCS#11 key matching %s found", ca_key_path);
1414 } else if ((ca = load_identity(tmp)) == NULL)
1415 fatal("Couldn't load CA key \"%s\"", tmp);
1416 xfree(tmp);
1417
Damien Miller0a80ca12010-02-27 07:55:05 +11001418 for (i = 0; i < argc; i++) {
1419 /* Split list of principals */
1420 n = 0;
1421 if (cert_principals != NULL) {
1422 otmp = tmp = xstrdup(cert_principals);
1423 plist = NULL;
1424 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1425 plist = xrealloc(plist, n + 1, sizeof(*plist));
1426 if (*(plist[n] = xstrdup(cp)) == '\0')
1427 fatal("Empty principal name");
1428 }
1429 xfree(otmp);
1430 }
1431
1432 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1433 if ((public = key_load_public(tmp, &comment)) == NULL)
1434 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001435 if (public->type != KEY_RSA && public->type != KEY_DSA &&
1436 public->type != KEY_ECDSA)
Damien Miller0a80ca12010-02-27 07:55:05 +11001437 fatal("%s: key \"%s\" type %s cannot be certified",
1438 __func__, tmp, key_type(public));
1439
1440 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001441 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001442 fatal("Could not upgrade key %s to certificate", tmp);
1443 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001444 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001445 public->cert->key_id = xstrdup(cert_key_id);
1446 public->cert->nprincipals = n;
1447 public->cert->principals = plist;
1448 public->cert->valid_after = cert_valid_from;
1449 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001450 if (v00) {
1451 prepare_options_buf(&public->cert->critical,
1452 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1453 } else {
1454 prepare_options_buf(&public->cert->critical,
1455 OPTIONS_CRITICAL);
1456 prepare_options_buf(&public->cert->extensions,
1457 OPTIONS_EXTENSIONS);
1458 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001459 public->cert->signature_key = key_from_private(ca);
1460
1461 if (key_certify(public, ca) != 0)
1462 fatal("Couldn't not certify key %s", tmp);
1463
1464 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1465 *cp = '\0';
1466 xasprintf(&out, "%s-cert.pub", tmp);
1467 xfree(tmp);
1468
1469 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1470 fatal("Could not open \"%s\" for writing: %s", out,
1471 strerror(errno));
1472 if ((f = fdopen(fd, "w")) == NULL)
1473 fatal("%s: fdopen: %s", __func__, strerror(errno));
1474 if (!key_write(public, f))
1475 fatal("Could not write certified key to %s", out);
1476 fprintf(f, " %s\n", comment);
1477 fclose(f);
1478
Damien Miller4e270b02010-04-16 15:56:21 +10001479 if (!quiet) {
1480 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1481 "valid %s", key_cert_type(public),
1482 out, public->cert->key_id, public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001483 cert_principals != NULL ? " for " : "",
1484 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001485 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001486 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001487
1488 key_free(public);
1489 xfree(out);
1490 }
Damien Miller757f34e2010-08-05 13:05:31 +10001491 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001492 exit(0);
1493}
1494
1495static u_int64_t
1496parse_relative_time(const char *s, time_t now)
1497{
1498 int64_t mul, secs;
1499
1500 mul = *s == '-' ? -1 : 1;
1501
1502 if ((secs = convtime(s + 1)) == -1)
1503 fatal("Invalid relative certificate time %s", s);
1504 if (mul == -1 && secs > now)
1505 fatal("Certificate time %s cannot be represented", s);
1506 return now + (u_int64_t)(secs * mul);
1507}
1508
1509static u_int64_t
1510parse_absolute_time(const char *s)
1511{
1512 struct tm tm;
1513 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001514 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001515
Damien Miller2ca342b2010-03-03 12:14:15 +11001516 /*
1517 * POSIX strptime says "The application shall ensure that there
1518 * is white-space or other non-alphanumeric characters between
1519 * any two conversion specifications" so arrange things this way.
1520 */
1521 switch (strlen(s)) {
1522 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001523 fmt = "%Y-%m-%d";
1524 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001525 break;
1526 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001527 fmt = "%Y-%m-%dT%H:%M:%S";
1528 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001529 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1530 break;
1531 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001532 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001533 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001534
1535 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001536 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001537 fatal("Invalid certificate time %s", s);
1538 if ((tt = mktime(&tm)) < 0)
1539 fatal("Certificate time %s cannot be represented", s);
1540 return (u_int64_t)tt;
1541}
1542
1543static void
1544parse_cert_times(char *timespec)
1545{
1546 char *from, *to;
1547 time_t now = time(NULL);
1548 int64_t secs;
1549
1550 /* +timespec relative to now */
1551 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1552 if ((secs = convtime(timespec + 1)) == -1)
1553 fatal("Invalid relative certificate life %s", timespec);
1554 cert_valid_to = now + secs;
1555 /*
1556 * Backdate certificate one minute to avoid problems on hosts
1557 * with poorly-synchronised clocks.
1558 */
1559 cert_valid_from = ((now - 59)/ 60) * 60;
1560 return;
1561 }
1562
1563 /*
1564 * from:to, where
1565 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1566 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1567 */
1568 from = xstrdup(timespec);
1569 to = strchr(from, ':');
1570 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001571 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001572 *to++ = '\0';
1573
1574 if (*from == '-' || *from == '+')
1575 cert_valid_from = parse_relative_time(from, now);
1576 else
1577 cert_valid_from = parse_absolute_time(from);
1578
1579 if (*to == '-' || *to == '+')
1580 cert_valid_to = parse_relative_time(to, cert_valid_from);
1581 else
1582 cert_valid_to = parse_absolute_time(to);
1583
1584 if (cert_valid_to <= cert_valid_from)
1585 fatal("Empty certificate validity interval");
1586 xfree(from);
1587}
1588
1589static void
Damien Miller4e270b02010-04-16 15:56:21 +10001590add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001591{
1592 char *val;
1593
1594 if (strcmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001595 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001596 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001597 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001598 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001599 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001600 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001601 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001602 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001603 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001604 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001605 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001606 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001607 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001608 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001609 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001610 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001611 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001612 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001613 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001614 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001615 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001616 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1617 val = opt + 14;
1618 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001619 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001620 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001621 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001622 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001623 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1624 val = opt + 15;
1625 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001626 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001627 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001628 fatal("source-address already specified");
1629 if (addr_match_cidr_list(NULL, val) != 0)
1630 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001631 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001632 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001633 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001634}
1635
Ben Lindstrombba81212001-06-25 05:01:22 +00001636static void
Damien Millerd834d352010-06-26 09:48:02 +10001637show_options(const Buffer *optbuf, int v00, int in_critical)
1638{
1639 u_char *name, *data;
1640 u_int dlen;
1641 Buffer options, option;
1642
1643 buffer_init(&options);
1644 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1645
1646 buffer_init(&option);
1647 while (buffer_len(&options) != 0) {
1648 name = buffer_get_string(&options, NULL);
1649 data = buffer_get_string_ptr(&options, &dlen);
1650 buffer_append(&option, data, dlen);
1651 printf(" %s", name);
1652 if ((v00 || !in_critical) &&
1653 (strcmp(name, "permit-X11-forwarding") == 0 ||
1654 strcmp(name, "permit-agent-forwarding") == 0 ||
1655 strcmp(name, "permit-port-forwarding") == 0 ||
1656 strcmp(name, "permit-pty") == 0 ||
1657 strcmp(name, "permit-user-rc") == 0))
1658 printf("\n");
1659 else if ((v00 || in_critical) &&
1660 (strcmp(name, "force-command") == 0 ||
1661 strcmp(name, "source-address") == 0)) {
1662 data = buffer_get_string(&option, NULL);
1663 printf(" %s\n", data);
1664 xfree(data);
1665 } else {
1666 printf(" UNKNOWN OPTION (len %u)\n",
1667 buffer_len(&option));
1668 buffer_clear(&option);
1669 }
1670 xfree(name);
1671 if (buffer_len(&option) != 0)
1672 fatal("Option corrupt: extra data at end");
1673 }
1674 buffer_free(&option);
1675 buffer_free(&options);
1676}
1677
1678static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001679do_show_cert(struct passwd *pw)
1680{
1681 Key *key;
1682 struct stat st;
1683 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001684 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001685
1686 if (!have_identity)
1687 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001688 if (stat(identity_file, &st) < 0)
1689 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001690 if ((key = key_load_public(identity_file, NULL)) == NULL)
1691 fatal("%s is not a public key", identity_file);
1692 if (!key_is_cert(key))
1693 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001694 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1695
Damien Millerf2b70ca2010-03-05 07:39:35 +11001696 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1697 ca_fp = key_fingerprint(key->cert->signature_key,
1698 SSH_FP_MD5, SSH_FP_HEX);
1699
1700 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001701 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1702 key_cert_type(key));
1703 printf(" Public key: %s %s\n", key_type(key), key_fp);
1704 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001705 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001706 printf(" Key ID: \"%s\"\n", key->cert->key_id);
1707 if (!v00)
1708 printf(" Serial: %llu\n", key->cert->serial);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001709 printf(" Valid: %s\n",
1710 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1711 printf(" Principals: ");
1712 if (key->cert->nprincipals == 0)
1713 printf("(none)\n");
1714 else {
1715 for (i = 0; i < key->cert->nprincipals; i++)
1716 printf("\n %s",
1717 key->cert->principals[i]);
1718 printf("\n");
1719 }
Damien Miller4e270b02010-04-16 15:56:21 +10001720 printf(" Critical Options: ");
1721 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001722 printf("(none)\n");
1723 else {
1724 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001725 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001726 }
Damien Miller4e270b02010-04-16 15:56:21 +10001727 if (!v00) {
1728 printf(" Extensions: ");
1729 if (buffer_len(&key->cert->extensions) == 0)
1730 printf("(none)\n");
1731 else {
1732 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001733 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001734 }
1735 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001736 exit(0);
1737}
1738
1739static void
Damien Miller431f66b1999-11-21 18:31:57 +11001740usage(void)
1741{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001742 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001743 fprintf(stderr, "Options:\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001744 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001745 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001746 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001747 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001748 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001749#ifdef ENABLE_PKCS11
1750 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1751#endif
Damien Miller44b25042010-07-02 13:35:01 +10001752 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001753 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1754 fprintf(stderr, " -f filename Filename of the key file.\n");
1755 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1756 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1757 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001758 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1759 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10001760 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001761 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001762 fprintf(stderr, " -l Show fingerprint of key file.\n");
1763 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001764 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001765 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001766 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10001767 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001768 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1769 fprintf(stderr, " -p Change passphrase of private key file.\n");
1770 fprintf(stderr, " -q Quiet.\n");
1771 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1772 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1773 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001774 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001775 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1776 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001777 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001778 fprintf(stderr, " -v Verbose.\n");
1779 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1780 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001781 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001782
Damien Miller95def091999-11-25 00:26:21 +11001783 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001784}
1785
Damien Miller95def091999-11-25 00:26:21 +11001786/*
1787 * Main program for key management.
1788 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001789int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001790main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001791{
Damien Millera41c8b12002-01-22 23:05:08 +11001792 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller757f34e2010-08-05 13:05:31 +10001793 char out_file[MAXPATHLEN], *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001794 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001795 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001796 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001797 int opt, type, fd;
Damien Millerbebbb7e2010-05-10 11:54:38 +10001798 u_int maxbits;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001799 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001800 int do_gen_candidates = 0, do_screen_candidates = 0;
1801 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001802 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001803 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001804
Damien Miller95def091999-11-25 00:26:21 +11001805 extern int optind;
1806 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001807
Darren Tuckerce321d82005-10-03 18:11:24 +10001808 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1809 sanitise_stdfd();
1810
Darren Tucker9ac56e92007-01-14 10:19:59 +11001811 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001812
Damien Miller4314c2b2010-09-10 11:12:09 +10001813 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001814 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001815
Kevin Steves3a881912002-07-20 19:05:40 +00001816 init_rng();
1817 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001818
Damien Miller5428f641999-11-25 11:54:57 +11001819 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001820 pw = getpwuid(getuid());
1821 if (!pw) {
1822 printf("You don't exist, go away!\n");
1823 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001824 }
Damien Millereba71ba2000-04-29 23:57:08 +10001825 if (gethostname(hostname, sizeof(hostname)) < 0) {
1826 perror("gethostname");
1827 exit(1);
1828 }
Damien Miller5428f641999-11-25 11:54:57 +11001829
Damien Miller44b25042010-07-02 13:35:01 +10001830 while ((opt = getopt(argc, argv, "degiqpclBHLhvxXyF:b:f:t:D:I:P:m:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001831 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001832 switch (opt) {
1833 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10001834 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001835 if (errstr)
1836 fatal("Bits has bad value %s (%s)",
1837 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001838 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001839 case 'F':
1840 find_host = 1;
1841 rr_hostname = optarg;
1842 break;
1843 case 'H':
1844 hash_hosts = 1;
1845 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001846 case 'I':
1847 cert_key_id = optarg;
1848 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001849 case 'R':
1850 delete_host = 1;
1851 rr_hostname = optarg;
1852 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001853 case 'L':
1854 show_cert = 1;
1855 break;
Damien Miller95def091999-11-25 00:26:21 +11001856 case 'l':
1857 print_fingerprint = 1;
1858 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001859 case 'B':
1860 print_bubblebabble = 1;
1861 break;
Damien Miller44b25042010-07-02 13:35:01 +10001862 case 'm':
1863 if (strcasecmp(optarg, "RFC4716") == 0 ||
1864 strcasecmp(optarg, "ssh2") == 0) {
1865 convert_format = FMT_RFC4716;
1866 break;
1867 }
1868 if (strcasecmp(optarg, "PKCS8") == 0) {
1869 convert_format = FMT_PKCS8;
1870 break;
1871 }
1872 if (strcasecmp(optarg, "PEM") == 0) {
1873 convert_format = FMT_PEM;
1874 break;
1875 }
1876 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001877 case 'n':
1878 cert_principals = optarg;
1879 break;
Damien Miller95def091999-11-25 00:26:21 +11001880 case 'p':
1881 change_passphrase = 1;
1882 break;
Damien Miller95def091999-11-25 00:26:21 +11001883 case 'c':
1884 change_comment = 1;
1885 break;
Damien Miller95def091999-11-25 00:26:21 +11001886 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10001887 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1888 sizeof(identity_file))
1889 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11001890 have_identity = 1;
1891 break;
Damien Miller37876e92003-05-15 10:19:46 +10001892 case 'g':
1893 print_generic = 1;
1894 break;
Damien Miller95def091999-11-25 00:26:21 +11001895 case 'P':
1896 identity_passphrase = optarg;
1897 break;
Damien Miller95def091999-11-25 00:26:21 +11001898 case 'N':
1899 identity_new_passphrase = optarg;
1900 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001901 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10001902 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11001903 break;
Damien Miller95def091999-11-25 00:26:21 +11001904 case 'C':
1905 identity_comment = optarg;
1906 break;
Damien Miller95def091999-11-25 00:26:21 +11001907 case 'q':
1908 quiet = 1;
1909 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001910 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10001911 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001912 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10001913 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001914 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001915 case 'h':
1916 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001917 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001918 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00001919 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10001920 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00001921 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10001922 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10001923 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001924 case 'y':
1925 print_public = 1;
1926 break;
Damien Millereba71ba2000-04-29 23:57:08 +10001927 case 'd':
Damien Miller0bc1bd82000-11-13 22:57:25 +11001928 key_type_name = "dsa";
Damien Millereba71ba2000-04-29 23:57:08 +10001929 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001930 case 's':
1931 ca_key_path = optarg;
1932 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001933 case 't':
1934 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001935 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00001936 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11001937 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00001938 break;
Darren Tucker06930c72003-12-31 11:34:51 +11001939 case 'v':
1940 if (log_level == SYSLOG_LEVEL_INFO)
1941 log_level = SYSLOG_LEVEL_DEBUG1;
1942 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10001943 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11001944 log_level < SYSLOG_LEVEL_DEBUG3)
1945 log_level++;
1946 }
1947 break;
Damien Miller37876e92003-05-15 10:19:46 +10001948 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11001949 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10001950 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10001951 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11001952 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1953 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001954 if (errstr)
1955 fatal("Desired generator has bad value: %s (%s)",
1956 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001957 break;
1958 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11001959 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10001960 if (errstr)
1961 fatal("Invalid number of trials: %s (%s)",
1962 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10001963 break;
1964 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11001965 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10001966 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10001967 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10001968 break;
1969 case 'G':
1970 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001971 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1972 sizeof(out_file))
1973 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001974 break;
1975 case 'T':
1976 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10001977 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1978 sizeof(out_file))
1979 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10001980 break;
1981 case 'S':
1982 /* XXX - also compare length against bits */
1983 if (BN_hex2bn(&start, optarg) == 0)
1984 fatal("Invalid start point.");
1985 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001986 case 'V':
1987 parse_cert_times(optarg);
1988 break;
Damien Miller4e270b02010-04-16 15:56:21 +10001989 case 'z':
1990 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
1991 if (errstr)
1992 fatal("Invalid serial number: %s", errstr);
1993 break;
Damien Miller95def091999-11-25 00:26:21 +11001994 case '?':
1995 default:
1996 usage();
1997 }
1998 }
Darren Tucker06930c72003-12-31 11:34:51 +11001999
2000 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002001 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002002
Damien Miller0a80ca12010-02-27 07:55:05 +11002003 argv += optind;
2004 argc -= optind;
2005
2006 if (ca_key_path != NULL) {
2007 if (argc < 1) {
2008 printf("Too few arguments.\n");
2009 usage();
2010 }
2011 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11002012 printf("Too many arguments.\n");
2013 usage();
2014 }
2015 if (change_passphrase && change_comment) {
2016 printf("Can only have one of -p and -c.\n");
2017 usage();
2018 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002019 if (print_fingerprint && (delete_host || hash_hosts)) {
2020 printf("Cannot use -l with -D or -R.\n");
2021 usage();
2022 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002023 if (ca_key_path != NULL) {
2024 if (cert_key_id == NULL)
2025 fatal("Must specify key id (-I) when certifying");
2026 do_ca_sign(pw, argc, argv);
2027 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002028 if (show_cert)
2029 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002030 if (delete_host || hash_hosts || find_host)
2031 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002032 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002033 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002034 if (change_passphrase)
2035 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002036 if (change_comment)
2037 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002038 if (convert_to)
2039 do_convert_to(pw);
2040 if (convert_from)
2041 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002042 if (print_public)
2043 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002044 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002045 unsigned int n = 0;
2046
2047 if (have_identity) {
2048 n = do_print_resource_record(pw,
2049 identity_file, rr_hostname);
2050 if (n == 0) {
2051 perror(identity_file);
2052 exit(1);
2053 }
2054 exit(0);
2055 } else {
2056
2057 n += do_print_resource_record(pw,
2058 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2059 n += do_print_resource_record(pw,
2060 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
2061
2062 if (n == 0)
2063 fatal("no keys found.");
2064 exit(0);
2065 }
Damien Miller37876e92003-05-15 10:19:46 +10002066 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002067 if (pkcs11provider != NULL)
Damien Miller757f34e2010-08-05 13:05:31 +10002068 do_download(pw);
Damien Miller95def091999-11-25 00:26:21 +11002069
Darren Tucker019cefe2003-08-02 22:40:07 +10002070 if (do_gen_candidates) {
2071 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002072
Darren Tucker019cefe2003-08-02 22:40:07 +10002073 if (out == NULL) {
2074 error("Couldn't open modulus candidate file \"%s\": %s",
2075 out_file, strerror(errno));
2076 return (1);
2077 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002078 if (bits == 0)
2079 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002080 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002081 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002082
2083 return (0);
2084 }
2085
2086 if (do_screen_candidates) {
2087 FILE *in;
2088 FILE *out = fopen(out_file, "w");
2089
2090 if (have_identity && strcmp(identity_file, "-") != 0) {
2091 if ((in = fopen(identity_file, "r")) == NULL) {
2092 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002093 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002094 strerror(errno));
2095 }
2096 } else
2097 in = stdin;
2098
2099 if (out == NULL) {
2100 fatal("Couldn't open moduli file \"%s\": %s",
2101 out_file, strerror(errno));
2102 }
2103 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002104 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002105 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002106 }
2107
Damien Miller95def091999-11-25 00:26:21 +11002108 arc4random_stir();
2109
Damien Millerf14be5c2005-11-05 15:15:49 +11002110 if (key_type_name == NULL)
2111 key_type_name = "rsa";
2112
Damien Millere39cacc2000-11-29 12:18:44 +11002113 type = key_type_from_name(key_type_name);
2114 if (type == KEY_UNSPEC) {
2115 fprintf(stderr, "unknown key type %s\n", key_type_name);
2116 exit(1);
Damien Millereba71ba2000-04-29 23:57:08 +10002117 }
Damien Millereb8b60e2010-08-31 22:41:14 +10002118 if (bits == 0) {
2119 if (type == KEY_DSA)
2120 bits = DEFAULT_BITS_DSA;
2121 else if (type == KEY_ECDSA)
2122 bits = DEFAULT_BITS_ECDSA;
2123 else
2124 bits = DEFAULT_BITS;
2125 }
Damien Millerbebbb7e2010-05-10 11:54:38 +10002126 maxbits = (type == KEY_DSA) ?
2127 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
2128 if (bits > maxbits) {
2129 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
2130 exit(1);
2131 }
Tim Rice660c3402005-11-28 17:45:32 -08002132 if (type == KEY_DSA && bits != 1024)
2133 fatal("DSA keys must be 1024 bits");
Damien Miller57737942010-09-10 11:16:37 +10002134 else if (type != KEY_ECDSA && bits < 768)
2135 fatal("Key must at least be 768 bits");
Damien Millereb8b60e2010-08-31 22:41:14 +10002136 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(bits) == -1)
2137 fatal("Invalid ECDSA key length - valid lengths are "
2138 "256, 384 or 521 bits");
Darren Tucker3af2ac52005-11-29 13:10:24 +11002139 if (!quiet)
2140 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002141 private = key_generate(type, bits);
2142 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002143 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002144 exit(1);
2145 }
2146 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002147
2148 if (!have_identity)
2149 ask_filename(pw, "Enter file in which to save the key");
2150
Damien Miller788f2122005-11-05 15:14:59 +11002151 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002152 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2153 pw->pw_dir, _PATH_SSH_USER_DIR);
2154 if (strstr(identity_file, dotsshdir) != NULL) {
2155 if (stat(dotsshdir, &st) < 0) {
2156 if (errno != ENOENT) {
2157 error("Could not stat %s: %s", dotsshdir,
2158 strerror(errno));
2159 } else if (mkdir(dotsshdir, 0700) < 0) {
2160 error("Could not create directory '%s': %s",
2161 dotsshdir, strerror(errno));
2162 } else if (!quiet)
2163 printf("Created directory '%s'.\n", dotsshdir);
2164 }
Damien Miller95def091999-11-25 00:26:21 +11002165 }
2166 /* If the file already exists, ask the user to confirm. */
2167 if (stat(identity_file, &st) >= 0) {
2168 char yesno[3];
2169 printf("%s already exists.\n", identity_file);
2170 printf("Overwrite (y/n)? ");
2171 fflush(stdout);
2172 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2173 exit(1);
2174 if (yesno[0] != 'y' && yesno[0] != 'Y')
2175 exit(1);
2176 }
2177 /* Ask for a passphrase (twice). */
2178 if (identity_passphrase)
2179 passphrase1 = xstrdup(identity_passphrase);
2180 else if (identity_new_passphrase)
2181 passphrase1 = xstrdup(identity_new_passphrase);
2182 else {
2183passphrase_again:
2184 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002185 read_passphrase("Enter passphrase (empty for no "
2186 "passphrase): ", RP_ALLOW_STDIN);
2187 passphrase2 = read_passphrase("Enter same passphrase again: ",
2188 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002189 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002190 /*
2191 * The passphrases do not match. Clear them and
2192 * retry.
2193 */
Damien Miller95def091999-11-25 00:26:21 +11002194 memset(passphrase1, 0, strlen(passphrase1));
2195 memset(passphrase2, 0, strlen(passphrase2));
2196 xfree(passphrase1);
2197 xfree(passphrase2);
2198 printf("Passphrases do not match. Try again.\n");
2199 goto passphrase_again;
2200 }
2201 /* Clear the other copy of the passphrase. */
2202 memset(passphrase2, 0, strlen(passphrase2));
2203 xfree(passphrase2);
2204 }
2205
Damien Miller95def091999-11-25 00:26:21 +11002206 if (identity_comment) {
2207 strlcpy(comment, identity_comment, sizeof(comment));
2208 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002209 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002210 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2211 }
2212
2213 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00002214 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002215 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002216 memset(passphrase1, 0, strlen(passphrase1));
2217 xfree(passphrase1);
2218 exit(1);
2219 }
2220 /* Clear the passphrase. */
2221 memset(passphrase1, 0, strlen(passphrase1));
2222 xfree(passphrase1);
2223
2224 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002225 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002226 arc4random_stir();
2227
2228 if (!quiet)
2229 printf("Your identification has been saved in %s.\n", identity_file);
2230
Damien Miller95def091999-11-25 00:26:21 +11002231 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002232 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2233 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002234 printf("Could not save your public key in %s\n", identity_file);
2235 exit(1);
2236 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002237 f = fdopen(fd, "w");
2238 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002239 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002240 exit(1);
2241 }
Damien Millereba71ba2000-04-29 23:57:08 +10002242 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002243 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002244 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002245 fclose(f);
2246
2247 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002248 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002249 char *ra = key_fingerprint(public, SSH_FP_MD5,
2250 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002251 printf("Your public key has been saved in %s.\n",
2252 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002253 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002254 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002255 printf("The key's randomart image is:\n");
2256 printf("%s\n", ra);
2257 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002258 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002259 }
Damien Millereba71ba2000-04-29 23:57:08 +10002260
2261 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002262 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002263}