blob: 106f1536da07a1e188b57c92a22936ec8cfaba46 [file] [log] [blame]
Damien Millerec77c952013-01-09 15:58:00 +11001/* $OpenBSD: ssh-keygen.c,v 1.221 2013/01/03 23:22:58 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110051#include "ssh2.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110052#include "ssh-pkcs11.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000053
Damien Miller3f54a9f2005-11-05 14:52:18 +110054/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
55#define DEFAULT_BITS 2048
56#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100057#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110058u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller5428f641999-11-25 11:54:57 +110060/*
61 * Flag indicating that we just want to change the passphrase. This can be
62 * set on the command line.
63 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064int change_passphrase = 0;
65
Damien Miller5428f641999-11-25 11:54:57 +110066/*
67 * Flag indicating that we just want to change the comment. This can be set
68 * on the command line.
69 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070int change_comment = 0;
71
72int quiet = 0;
73
Darren Tucker35c45532008-06-13 04:43:15 +100074int log_level = SYSLOG_LEVEL_INFO;
75
Damien Miller4b42d7f2005-03-01 21:48:35 +110076/* Flag indicating that we want to hash a known_hosts file */
77int hash_hosts = 0;
78/* Flag indicating that we want lookup a host in known_hosts file */
79int find_host = 0;
80/* Flag indicating that we want to delete a host from a known_hosts file */
81int delete_host = 0;
82
Damien Millerf2b70ca2010-03-05 07:39:35 +110083/* Flag indicating that we want to show the contents of a certificate */
84int show_cert = 0;
85
Damien Miller10f6f6b1999-11-17 17:29:08 +110086/* Flag indicating that we just want to see the key fingerprint */
87int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000088int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110089
Damien Miller431f66b1999-11-21 18:31:57 +110090/* The identity file name, given on the command line or entered by the user. */
91char identity_file[1024];
92int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* This is set to the passphrase if given on the command line. */
95char *identity_passphrase = NULL;
96
97/* This is set to the new passphrase if given on the command line. */
98char *identity_new_passphrase = NULL;
99
100/* This is set to the new comment if given on the command line. */
101char *identity_comment = NULL;
102
Damien Miller0a80ca12010-02-27 07:55:05 +1100103/* Path to CA key when certifying keys. */
104char *ca_key_path = NULL;
105
Damien Miller4e270b02010-04-16 15:56:21 +1000106/* Certificate serial number */
Damien Miller55aca022012-12-03 11:25:30 +1100107unsigned long long cert_serial = 0;
Damien Miller4e270b02010-04-16 15:56:21 +1000108
Damien Miller0a80ca12010-02-27 07:55:05 +1100109/* Key type when certifying */
110u_int cert_key_type = SSH2_CERT_TYPE_USER;
111
112/* "key ID" of signed key */
113char *cert_key_id = NULL;
114
115/* Comma-separated list of principal names for certifying keys */
116char *cert_principals = NULL;
117
118/* Validity period for certificates */
119u_int64_t cert_valid_from = 0;
120u_int64_t cert_valid_to = ~0ULL;
121
Damien Miller4e270b02010-04-16 15:56:21 +1000122/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000123#define CERTOPT_X_FWD (1)
124#define CERTOPT_AGENT_FWD (1<<1)
125#define CERTOPT_PORT_FWD (1<<2)
126#define CERTOPT_PTY (1<<3)
127#define CERTOPT_USER_RC (1<<4)
128#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
129 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
130u_int32_t certflags_flags = CERTOPT_DEFAULT;
131char *certflags_command = NULL;
132char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100133
Damien Miller44b25042010-07-02 13:35:01 +1000134/* Conversion to/from various formats */
135int convert_to = 0;
136int convert_from = 0;
137enum {
138 FMT_RFC4716,
139 FMT_PKCS8,
140 FMT_PEM
141} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000142int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000143int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100144
Damien Millera41c8b12002-01-22 23:05:08 +1100145char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000146
Damien Miller757f34e2010-08-05 13:05:31 +1000147/* Load key from this PKCS#11 provider */
148char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000149
Damien Miller431f66b1999-11-21 18:31:57 +1100150/* argv0 */
151extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Millereba71ba2000-04-29 23:57:08 +1000153char hostname[MAXHOSTNAMELEN];
154
Darren Tucker770fc012004-05-13 16:24:32 +1000155/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000156int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Millerdfceafe2012-07-06 13:44:19 +1000157int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
158 unsigned long);
Darren Tucker770fc012004-05-13 16:24:32 +1000159
Ben Lindstrombba81212001-06-25 05:01:22 +0000160static void
Damien Miller884b63a2011-05-05 14:14:52 +1000161type_bits_valid(int type, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000162{
163 u_int maxbits;
164
165 if (type == KEY_UNSPEC) {
166 fprintf(stderr, "unknown key type %s\n", key_type_name);
167 exit(1);
168 }
Damien Miller884b63a2011-05-05 14:14:52 +1000169 if (*bitsp == 0) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000170 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000171 *bitsp = DEFAULT_BITS_DSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000172 else if (type == KEY_ECDSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000173 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000174 else
Damien Miller884b63a2011-05-05 14:14:52 +1000175 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000176 }
177 maxbits = (type == KEY_DSA) ?
178 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Damien Miller884b63a2011-05-05 14:14:52 +1000179 if (*bitsp > maxbits) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000180 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
181 exit(1);
182 }
Damien Miller884b63a2011-05-05 14:14:52 +1000183 if (type == KEY_DSA && *bitsp != 1024)
Damien Miller58f1baf2011-05-05 14:06:15 +1000184 fatal("DSA keys must be 1024 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000185 else if (type != KEY_ECDSA && *bitsp < 768)
Damien Miller58f1baf2011-05-05 14:06:15 +1000186 fatal("Key must at least be 768 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000187 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(*bitsp) == -1)
Damien Miller58f1baf2011-05-05 14:06:15 +1000188 fatal("Invalid ECDSA key length - valid lengths are "
189 "256, 384 or 521 bits");
190}
191
192static void
Damien Miller431f66b1999-11-21 18:31:57 +1100193ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000194{
Damien Miller95def091999-11-25 00:26:21 +1100195 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100196 char *name = NULL;
197
Damien Miller993dd552002-02-19 15:22:47 +1100198 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000199 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100200 else {
Damien Miller993dd552002-02-19 15:22:47 +1100201 switch (key_type_from_name(key_type_name)) {
202 case KEY_RSA1:
203 name = _PATH_SSH_CLIENT_IDENTITY;
204 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000205 case KEY_DSA_CERT:
206 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100207 case KEY_DSA:
208 name = _PATH_SSH_CLIENT_ID_DSA;
209 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100210#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000211 case KEY_ECDSA_CERT:
212 case KEY_ECDSA:
213 name = _PATH_SSH_CLIENT_ID_ECDSA;
214 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100215#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000216 case KEY_RSA_CERT:
217 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100218 case KEY_RSA:
219 name = _PATH_SSH_CLIENT_ID_RSA;
220 break;
221 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100222 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100223 exit(1);
224 break;
225 }
Damien Miller90967402006-03-26 14:07:26 +1100226 }
Damien Millere39cacc2000-11-29 12:18:44 +1100227 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000228 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100229 if (fgets(buf, sizeof(buf), stdin) == NULL)
230 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000231 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100232 if (strcmp(buf, "") != 0)
233 strlcpy(identity_file, buf, sizeof(identity_file));
234 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100235}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236
Ben Lindstrombba81212001-06-25 05:01:22 +0000237static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000238load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000239{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000240 char *pass;
241 Key *prv;
242
Ben Lindstroma3700052001-04-05 23:26:32 +0000243 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000244 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000245 if (identity_passphrase)
246 pass = xstrdup(identity_passphrase);
247 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000248 pass = read_passphrase("Enter passphrase: ",
249 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000250 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000251 memset(pass, 0, strlen(pass));
252 xfree(pass);
253 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000254 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000255}
256
Damien Miller874d77b2000-10-14 16:23:11 +1100257#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000258#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100259#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000260#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000261
Ben Lindstrombba81212001-06-25 05:01:22 +0000262static void
Damien Miller44b25042010-07-02 13:35:01 +1000263do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000264{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000265 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000266 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100267 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000268
Damien Millera563cce2012-04-22 11:07:28 +1000269 if (k->type == KEY_RSA1) {
270 fprintf(stderr, "version 1 keys are not supported\n");
271 exit(1);
272 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000273 if (key_to_blob(k, &blob, &len) <= 0) {
274 fprintf(stderr, "key_to_blob failed\n");
275 exit(1);
276 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100277 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
278 snprintf(comment, sizeof(comment),
279 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000280 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000281 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100282
283 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
284 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000285 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100286 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000287 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000288 xfree(blob);
289 exit(0);
290}
291
Ben Lindstrombba81212001-06-25 05:01:22 +0000292static void
Damien Miller44b25042010-07-02 13:35:01 +1000293do_convert_to_pkcs8(Key *k)
294{
295 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000296 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000297 case KEY_RSA:
298 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
299 fatal("PEM_write_RSA_PUBKEY failed");
300 break;
301 case KEY_DSA:
302 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
303 fatal("PEM_write_DSA_PUBKEY failed");
304 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000305#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000306 case KEY_ECDSA:
307 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
308 fatal("PEM_write_EC_PUBKEY failed");
309 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000310#endif
Damien Miller44b25042010-07-02 13:35:01 +1000311 default:
312 fatal("%s: unsupported key type %s", __func__, key_type(k));
313 }
314 exit(0);
315}
316
317static void
318do_convert_to_pem(Key *k)
319{
320 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000321 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000322 case KEY_RSA:
323 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
324 fatal("PEM_write_RSAPublicKey failed");
325 break;
326#if notyet /* OpenSSH 0.9.8 lacks this function */
327 case KEY_DSA:
328 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
329 fatal("PEM_write_DSAPublicKey failed");
330 break;
331#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000332 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000333 default:
334 fatal("%s: unsupported key type %s", __func__, key_type(k));
335 }
336 exit(0);
337}
338
339static void
340do_convert_to(struct passwd *pw)
341{
342 Key *k;
343 struct stat st;
344
345 if (!have_identity)
346 ask_filename(pw, "Enter file in which the key is");
347 if (stat(identity_file, &st) < 0)
348 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
349 if ((k = key_load_public(identity_file, NULL)) == NULL) {
350 if ((k = load_identity(identity_file)) == NULL) {
351 fprintf(stderr, "load failed\n");
352 exit(1);
353 }
354 }
Damien Miller44b25042010-07-02 13:35:01 +1000355
356 switch (convert_format) {
357 case FMT_RFC4716:
358 do_convert_to_ssh2(pw, k);
359 break;
360 case FMT_PKCS8:
361 do_convert_to_pkcs8(k);
362 break;
363 case FMT_PEM:
364 do_convert_to_pem(k);
365 break;
366 default:
367 fatal("%s: unknown key format %d", __func__, convert_format);
368 }
369 exit(0);
370}
371
372static void
Damien Miller874d77b2000-10-14 16:23:11 +1100373buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
374{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000375 u_int bignum_bits = buffer_get_int(b);
376 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000377
Damien Miller874d77b2000-10-14 16:23:11 +1100378 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000379 fatal("buffer_get_bignum_bits: input buffer too small: "
380 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100381 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
382 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100383 buffer_consume(b, bytes);
384}
385
Ben Lindstrombba81212001-06-25 05:01:22 +0000386static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000387do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100388{
389 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100390 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100391 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000392 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000393 int magic, rlen, ktype, i1, i2, i3, i4;
394 u_int slen;
395 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100396
397 buffer_init(&b);
398 buffer_append(&b, blob, blen);
399
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100400 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100401 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
402 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
403 buffer_free(&b);
404 return NULL;
405 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000406 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100407 type = buffer_get_string(&b, NULL);
408 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000409 i2 = buffer_get_int(&b);
410 i3 = buffer_get_int(&b);
411 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100412 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100413 if (strcmp(cipher, "none") != 0) {
414 error("unsupported cipher %s", cipher);
415 xfree(cipher);
416 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000417 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100418 return NULL;
419 }
420 xfree(cipher);
421
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000422 if (strstr(type, "dsa")) {
423 ktype = KEY_DSA;
424 } else if (strstr(type, "rsa")) {
425 ktype = KEY_RSA;
426 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100427 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000428 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100429 return NULL;
430 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000431 key = key_new_private(ktype);
432 xfree(type);
433
434 switch (key->type) {
435 case KEY_DSA:
436 buffer_get_bignum_bits(&b, key->dsa->p);
437 buffer_get_bignum_bits(&b, key->dsa->g);
438 buffer_get_bignum_bits(&b, key->dsa->q);
439 buffer_get_bignum_bits(&b, key->dsa->pub_key);
440 buffer_get_bignum_bits(&b, key->dsa->priv_key);
441 break;
442 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100443 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000444 debug("e %lx", e);
445 if (e < 30) {
446 e <<= 8;
447 e += buffer_get_char(&b);
448 debug("e %lx", e);
449 e <<= 8;
450 e += buffer_get_char(&b);
451 debug("e %lx", e);
452 }
453 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000454 buffer_free(&b);
455 key_free(key);
456 return NULL;
457 }
458 buffer_get_bignum_bits(&b, key->rsa->d);
459 buffer_get_bignum_bits(&b, key->rsa->n);
460 buffer_get_bignum_bits(&b, key->rsa->iqmp);
461 buffer_get_bignum_bits(&b, key->rsa->q);
462 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000463 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000464 break;
465 }
Damien Miller874d77b2000-10-14 16:23:11 +1100466 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000467 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000468 error("do_convert_private_ssh2_from_blob: "
469 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100470 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000471
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000472 /* try the key */
473 key_sign(key, &sig, &slen, data, sizeof(data));
474 key_verify(key, sig, slen, data, sizeof(data));
475 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100476 return key;
477}
478
Damien Miller8056a9d2006-03-15 12:05:40 +1100479static int
480get_line(FILE *fp, char *line, size_t len)
481{
482 int c;
483 size_t pos = 0;
484
485 line[0] = '\0';
486 while ((c = fgetc(fp)) != EOF) {
487 if (pos >= len - 1) {
488 fprintf(stderr, "input line too long.\n");
489 exit(1);
490 }
Damien Miller90967402006-03-26 14:07:26 +1100491 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100492 case '\r':
493 c = fgetc(fp);
494 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
495 fprintf(stderr, "unget: %s\n", strerror(errno));
496 exit(1);
497 }
498 return pos;
499 case '\n':
500 return pos;
501 }
502 line[pos++] = c;
503 line[pos] = '\0';
504 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100505 /* We reached EOF */
506 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100507}
508
Ben Lindstrombba81212001-06-25 05:01:22 +0000509static void
Damien Miller44b25042010-07-02 13:35:01 +1000510do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000511{
Damien Millereba71ba2000-04-29 23:57:08 +1000512 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000513 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100514 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000515 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000516 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000517 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000518 FILE *fp;
519
Damien Millerba3420a2010-06-26 09:39:07 +1000520 if ((fp = fopen(identity_file, "r")) == NULL)
521 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000522 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100523 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
524 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000525 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000526 if (strncmp(line, "----", 4) == 0 ||
527 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100528 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000529 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000530 if (strstr(line, " END ") != NULL) {
531 break;
532 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000533 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000534 continue;
535 }
Damien Miller30c3d422000-05-09 11:02:59 +1000536 if (escaped) {
537 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000538 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000539 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000540 }
Damien Millereba71ba2000-04-29 23:57:08 +1000541 strlcat(encoded, line, sizeof(encoded));
542 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000543 len = strlen(encoded);
544 if (((len % 4) == 3) &&
545 (encoded[len-1] == '=') &&
546 (encoded[len-2] == '=') &&
547 (encoded[len-3] == '='))
548 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100549 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000550 if (blen < 0) {
551 fprintf(stderr, "uudecode failed.\n");
552 exit(1);
553 }
Damien Miller44b25042010-07-02 13:35:01 +1000554 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100555 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100556 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000557 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100558 fprintf(stderr, "decode blob failed.\n");
559 exit(1);
560 }
Damien Miller44b25042010-07-02 13:35:01 +1000561 fclose(fp);
562}
563
564static void
565do_convert_from_pkcs8(Key **k, int *private)
566{
567 EVP_PKEY *pubkey;
568 FILE *fp;
569
570 if ((fp = fopen(identity_file, "r")) == NULL)
571 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
572 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
573 fatal("%s: %s is not a recognised public key format", __func__,
574 identity_file);
575 }
576 fclose(fp);
577 switch (EVP_PKEY_type(pubkey->type)) {
578 case EVP_PKEY_RSA:
579 *k = key_new(KEY_UNSPEC);
580 (*k)->type = KEY_RSA;
581 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
582 break;
583 case EVP_PKEY_DSA:
584 *k = key_new(KEY_UNSPEC);
585 (*k)->type = KEY_DSA;
586 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
587 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000588#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000589 case EVP_PKEY_EC:
590 *k = key_new(KEY_UNSPEC);
591 (*k)->type = KEY_ECDSA;
592 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100593 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000594 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000595#endif
Damien Miller44b25042010-07-02 13:35:01 +1000596 default:
597 fatal("%s: unsupported pubkey type %d", __func__,
598 EVP_PKEY_type(pubkey->type));
599 }
600 EVP_PKEY_free(pubkey);
601 return;
602}
603
604static void
605do_convert_from_pem(Key **k, int *private)
606{
607 FILE *fp;
608 RSA *rsa;
609#ifdef notyet
610 DSA *dsa;
611#endif
612
613 if ((fp = fopen(identity_file, "r")) == NULL)
614 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
615 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
616 *k = key_new(KEY_UNSPEC);
617 (*k)->type = KEY_RSA;
618 (*k)->rsa = rsa;
619 fclose(fp);
620 return;
621 }
622#if notyet /* OpenSSH 0.9.8 lacks this function */
623 rewind(fp);
624 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
625 *k = key_new(KEY_UNSPEC);
626 (*k)->type = KEY_DSA;
627 (*k)->dsa = dsa;
628 fclose(fp);
629 return;
630 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000631 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000632#endif
633 fatal("%s: unrecognised raw private key format", __func__);
634}
635
636static void
637do_convert_from(struct passwd *pw)
638{
639 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000640 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000641 struct stat st;
642
643 if (!have_identity)
644 ask_filename(pw, "Enter file in which the key is");
645 if (stat(identity_file, &st) < 0)
646 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
647
648 switch (convert_format) {
649 case FMT_RFC4716:
650 do_convert_from_ssh2(pw, &k, &private);
651 break;
652 case FMT_PKCS8:
653 do_convert_from_pkcs8(&k, &private);
654 break;
655 case FMT_PEM:
656 do_convert_from_pem(&k, &private);
657 break;
658 default:
659 fatal("%s: unknown key format %d", __func__, convert_format);
660 }
661
662 if (!private)
663 ok = key_write(k, stdout);
664 if (ok)
665 fprintf(stdout, "\n");
666 else {
667 switch (k->type) {
668 case KEY_DSA:
669 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
670 NULL, 0, NULL, NULL);
671 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000672#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000673 case KEY_ECDSA:
674 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
675 NULL, 0, NULL, NULL);
676 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000677#endif
Damien Miller44b25042010-07-02 13:35:01 +1000678 case KEY_RSA:
679 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
680 NULL, 0, NULL, NULL);
681 break;
682 default:
683 fatal("%s: unsupported key type %s", __func__,
684 key_type(k));
685 }
686 }
687
Damien Miller874d77b2000-10-14 16:23:11 +1100688 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100689 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100690 exit(1);
691 }
Damien Millereba71ba2000-04-29 23:57:08 +1000692 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000693 exit(0);
694}
695
Ben Lindstrombba81212001-06-25 05:01:22 +0000696static void
Damien Millereba71ba2000-04-29 23:57:08 +1000697do_print_public(struct passwd *pw)
698{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000699 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000700 struct stat st;
701
702 if (!have_identity)
703 ask_filename(pw, "Enter file in which the key is");
704 if (stat(identity_file, &st) < 0) {
705 perror(identity_file);
706 exit(1);
707 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000708 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000709 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000710 fprintf(stderr, "load failed\n");
711 exit(1);
712 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000713 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000714 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000715 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000716 fprintf(stdout, "\n");
717 exit(0);
718}
719
Ben Lindstromcd392282001-07-04 03:44:03 +0000720static void
Damien Miller757f34e2010-08-05 13:05:31 +1000721do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000722{
Damien Miller7ea845e2010-02-12 09:21:02 +1100723#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000724 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100725 int i, nkeys;
Damien Millerec77c952013-01-09 15:58:00 +1100726 enum fp_rep rep;
727 enum fp_type fptype;
728 char *fp, *ra;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000729
Damien Miller7ea845e2010-02-12 09:21:02 +1100730 pkcs11_init(0);
731 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
732 if (nkeys <= 0)
733 fatal("cannot read public key from pkcs11");
734 for (i = 0; i < nkeys; i++) {
Damien Millerec77c952013-01-09 15:58:00 +1100735 if (print_fingerprint) {
736 fp = key_fingerprint(keys[i], fptype, rep);
737 ra = key_fingerprint(keys[i], SSH_FP_MD5,
738 SSH_FP_RANDOMART);
739 printf("%u %s %s (PKCS11 key)\n", key_size(keys[i]),
740 fp, key_type(keys[i]));
741 if (log_level >= SYSLOG_LEVEL_VERBOSE)
742 printf("%s\n", ra);
743 xfree(ra);
744 xfree(fp);
745 } else {
746 key_write(keys[i], stdout);
747 fprintf(stdout, "\n");
748 }
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000749 key_free(keys[i]);
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000750 }
751 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100752 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000753 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100754#else
755 fatal("no pkcs11 support");
756#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000757}
Ben Lindstromcd392282001-07-04 03:44:03 +0000758
Ben Lindstrombba81212001-06-25 05:01:22 +0000759static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100760do_fingerprint(struct passwd *pw)
761{
Damien Miller98c7ad62000-03-09 21:27:49 +1100762 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000763 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000764 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100765 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000766 enum fp_rep rep;
767 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100768 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100769
Ben Lindstromd0fca422001-03-26 13:44:06 +0000770 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
771 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000772
Damien Miller95def091999-11-25 00:26:21 +1100773 if (!have_identity)
774 ask_filename(pw, "Enter file in which the key is");
775 if (stat(identity_file, &st) < 0) {
776 perror(identity_file);
777 exit(1);
778 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000779 public = key_load_public(identity_file, &comment);
780 if (public != NULL) {
781 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100782 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000783 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
784 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000785 if (log_level >= SYSLOG_LEVEL_VERBOSE)
786 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100787 key_free(public);
788 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000789 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000790 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100791 exit(0);
792 }
Damien Miller40b59852006-06-13 13:00:25 +1000793 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000794 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000795 comment = NULL;
796 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100797
Damien Millerba3420a2010-06-26 09:39:07 +1000798 if ((f = fopen(identity_file, "r")) == NULL)
799 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100800
Damien Millerba3420a2010-06-26 09:39:07 +1000801 while (fgets(line, sizeof(line), f)) {
802 if ((cp = strchr(line, '\n')) == NULL) {
803 error("line %d too long: %.40s...",
804 num + 1, line);
805 skip = 1;
806 continue;
Damien Miller95def091999-11-25 00:26:21 +1100807 }
Damien Millerba3420a2010-06-26 09:39:07 +1000808 num++;
809 if (skip) {
810 skip = 0;
811 continue;
812 }
813 *cp = '\0';
814
815 /* Skip leading whitespace, empty and comment lines. */
816 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
817 ;
818 if (!*cp || *cp == '\n' || *cp == '#')
819 continue;
820 i = strtol(cp, &ep, 10);
821 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
822 int quoted = 0;
823 comment = cp;
824 for (; *cp && (quoted || (*cp != ' ' &&
825 *cp != '\t')); cp++) {
826 if (*cp == '\\' && cp[1] == '"')
827 cp++; /* Skip both */
828 else if (*cp == '"')
829 quoted = !quoted;
830 }
831 if (!*cp)
832 continue;
833 *cp++ = '\0';
834 }
835 ep = cp;
836 public = key_new(KEY_RSA1);
837 if (key_read(public, &cp) != 1) {
838 cp = ep;
839 key_free(public);
840 public = key_new(KEY_UNSPEC);
841 if (key_read(public, &cp) != 1) {
842 key_free(public);
843 continue;
844 }
845 }
846 comment = *cp ? cp : comment;
847 fp = key_fingerprint(public, fptype, rep);
848 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
849 printf("%u %s %s (%s)\n", key_size(public), fp,
850 comment ? comment : "no comment", key_type(public));
851 if (log_level >= SYSLOG_LEVEL_VERBOSE)
852 printf("%s\n", ra);
853 xfree(ra);
854 xfree(fp);
855 key_free(public);
856 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100857 }
Damien Millerba3420a2010-06-26 09:39:07 +1000858 fclose(f);
859
Damien Miller98c7ad62000-03-09 21:27:49 +1100860 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100861 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100862 exit(1);
863 }
Damien Miller95def091999-11-25 00:26:21 +1100864 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100865}
866
Damien Miller4b42d7f2005-03-01 21:48:35 +1100867static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000868do_gen_all_hostkeys(struct passwd *pw)
869{
870 struct {
871 char *key_type;
872 char *key_type_display;
873 char *path;
874 } key_types[] = {
875 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
876 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
877 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100878#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000879 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100880#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000881 { NULL, NULL, NULL }
882 };
883
884 int first = 0;
885 struct stat st;
886 Key *private, *public;
887 char comment[1024];
888 int i, type, fd;
889 FILE *f;
890
891 for (i = 0; key_types[i].key_type; i++) {
892 if (stat(key_types[i].path, &st) == 0)
893 continue;
894 if (errno != ENOENT) {
895 printf("Could not stat %s: %s", key_types[i].path,
896 strerror(errno));
897 first = 0;
898 continue;
899 }
900
901 if (first == 0) {
902 first = 1;
903 printf("%s: generating new host keys: ", __progname);
904 }
905 printf("%s ", key_types[i].key_type_display);
906 fflush(stdout);
907 arc4random_stir();
908 type = key_type_from_name(key_types[i].key_type);
909 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
910 bits = 0;
911 type_bits_valid(type, &bits);
912 private = key_generate(type, bits);
913 if (private == NULL) {
914 fprintf(stderr, "key_generate failed\n");
915 first = 0;
916 continue;
917 }
918 public = key_from_private(private);
919 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
920 hostname);
921 if (!key_save_private(private, identity_file, "", comment)) {
922 printf("Saving the key failed: %s.\n", identity_file);
923 key_free(private);
924 key_free(public);
925 first = 0;
926 continue;
927 }
928 key_free(private);
929 arc4random_stir();
930 strlcat(identity_file, ".pub", sizeof(identity_file));
931 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
932 if (fd == -1) {
933 printf("Could not save your public key in %s\n",
934 identity_file);
935 key_free(public);
936 first = 0;
937 continue;
938 }
939 f = fdopen(fd, "w");
940 if (f == NULL) {
941 printf("fdopen %s failed\n", identity_file);
942 key_free(public);
943 first = 0;
944 continue;
945 }
946 if (!key_write(public, f)) {
947 fprintf(stderr, "write key failed\n");
948 key_free(public);
949 first = 0;
950 continue;
951 }
952 fprintf(f, " %s\n", comment);
953 fclose(f);
954 key_free(public);
955
956 }
957 if (first != 0)
958 printf("\n");
959}
960
961static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100962printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100963{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000964 if (print_fingerprint) {
965 enum fp_rep rep;
966 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000967 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000968
969 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
970 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
971 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100972 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000973 printf("%u %s %s (%s)\n", key_size(public), fp, name,
974 key_type(public));
975 if (log_level >= SYSLOG_LEVEL_VERBOSE)
976 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000977 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000978 xfree(fp);
979 } else {
980 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
981 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100982 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000983 if (!key_write(public, f))
984 fatal("key_write failed");
985 fprintf(f, "\n");
986 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100987}
988
989static void
990do_known_hosts(struct passwd *pw, const char *name)
991{
992 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100993 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100994 char *cp, *cp2, *kp, *kp2;
995 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100996 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100997 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100998
999 if (!have_identity) {
1000 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1001 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1002 sizeof(identity_file))
1003 fatal("Specified known hosts path too long");
1004 xfree(cp);
1005 have_identity = 1;
1006 }
1007 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +10001008 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +11001009
1010 /*
1011 * Find hosts goes to stdout, hash and deletions happen in-place
1012 * A corner case is ssh-keygen -HF foo, which should go to stdout
1013 */
1014 if (!find_host && (hash_hosts || delete_host)) {
1015 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1016 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1017 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1018 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1019 fatal("known_hosts path too long");
1020 umask(077);
1021 if ((c = mkstemp(tmp)) == -1)
1022 fatal("mkstemp: %s", strerror(errno));
1023 if ((out = fdopen(c, "w")) == NULL) {
1024 c = errno;
1025 unlink(tmp);
1026 fatal("fdopen: %s", strerror(c));
1027 }
1028 inplace = 1;
1029 }
1030
1031 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001032 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001033 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001034 skip = 1;
1035 invalid = 1;
1036 continue;
1037 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001038 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001039 if (skip) {
1040 skip = 0;
1041 continue;
1042 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001043 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001044
1045 /* Skip leading whitespace, empty and comment lines. */
1046 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1047 ;
1048 if (!*cp || *cp == '\n' || *cp == '#') {
1049 if (inplace)
1050 fprintf(out, "%s\n", cp);
1051 continue;
1052 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001053 /* Check whether this is a CA key */
1054 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1055 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1056 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1057 ca = 1;
1058 cp += sizeof(CA_MARKER);
1059 } else
1060 ca = 0;
1061
Damien Miller4b42d7f2005-03-01 21:48:35 +11001062 /* Find the end of the host name portion. */
1063 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1064 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001065
Damien Miller4b42d7f2005-03-01 21:48:35 +11001066 if (*kp == '\0' || *(kp + 1) == '\0') {
1067 error("line %d missing key: %.40s...",
1068 num, line);
1069 invalid = 1;
1070 continue;
1071 }
1072 *kp++ = '\0';
1073 kp2 = kp;
1074
Damien Miller0a80ca12010-02-27 07:55:05 +11001075 pub = key_new(KEY_RSA1);
1076 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001077 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001078 key_free(pub);
1079 pub = key_new(KEY_UNSPEC);
1080 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001081 error("line %d invalid key: %.40s...",
1082 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001083 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001084 invalid = 1;
1085 continue;
1086 }
1087 }
1088
1089 if (*cp == HASH_DELIM) {
1090 if (find_host || delete_host) {
1091 cp2 = host_hash(name, cp, strlen(cp));
1092 if (cp2 == NULL) {
1093 error("line %d: invalid hashed "
1094 "name: %.64s...", num, line);
1095 invalid = 1;
1096 continue;
1097 }
1098 c = (strcmp(cp2, cp) == 0);
1099 if (find_host && c) {
1100 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001101 "line %d type %s%s\n", name,
1102 num, key_type(pub),
1103 ca ? " (CA key)" : "");
1104 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001105 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001106 if (delete_host) {
1107 if (!c && !ca)
1108 printhost(out, cp, pub, ca, 0);
1109 else
1110 printf("# Host %s found: "
1111 "line %d type %s\n", name,
1112 num, key_type(pub));
1113 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001114 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +11001115 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001116 } else {
1117 if (find_host || delete_host) {
1118 c = (match_hostname(name, cp,
1119 strlen(cp)) == 1);
1120 if (find_host && c) {
1121 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001122 "line %d type %s%s\n", name,
1123 num, key_type(pub),
1124 ca ? " (CA key)" : "");
1125 printhost(out, name, pub,
1126 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001127 }
Darren Tuckerf09a8a62012-09-06 21:20:39 +10001128 if (delete_host) {
1129 if (!c && !ca)
1130 printhost(out, cp, pub, ca, 0);
1131 else
1132 printf("# Host %s found: "
1133 "line %d type %s\n", name,
1134 num, key_type(pub));
1135 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001136 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +11001137 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001138 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001139 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001140 if (ca) {
1141 fprintf(stderr, "Warning: "
1142 "ignoring CA key for host: "
1143 "%.64s\n", cp2);
1144 printhost(out, cp2, pub, ca, 0);
1145 } else if (strcspn(cp2, "*?!") !=
1146 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001147 fprintf(stderr, "Warning: "
1148 "ignoring host name with "
1149 "metacharacters: %.64s\n",
1150 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +11001151 printhost(out, cp2, pub, ca, 0);
1152 } else
1153 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001154 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001155 has_unhashed = 1;
1156 }
1157 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001158 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001159 }
1160 fclose(in);
1161
1162 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001163 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001164 identity_file);
1165 if (inplace) {
1166 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001167 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001168 fclose(out);
1169 unlink(tmp);
1170 }
1171 exit(1);
1172 }
1173
1174 if (inplace) {
1175 fclose(out);
1176
1177 /* Backup existing file */
1178 if (unlink(old) == -1 && errno != ENOENT)
1179 fatal("unlink %.100s: %s", old, strerror(errno));
1180 if (link(identity_file, old) == -1)
1181 fatal("link %.100s to %.100s: %s", identity_file, old,
1182 strerror(errno));
1183 /* Move new one into place */
1184 if (rename(tmp, identity_file) == -1) {
1185 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1186 strerror(errno));
1187 unlink(tmp);
1188 unlink(old);
1189 exit(1);
1190 }
1191
1192 fprintf(stderr, "%s updated.\n", identity_file);
1193 fprintf(stderr, "Original contents retained as %s\n", old);
1194 if (has_unhashed) {
1195 fprintf(stderr, "WARNING: %s contains unhashed "
1196 "entries\n", old);
1197 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001198 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001199 }
1200 }
1201
1202 exit(0);
1203}
1204
Damien Miller95def091999-11-25 00:26:21 +11001205/*
1206 * Perform changing a passphrase. The argument is the passwd structure
1207 * for the current user.
1208 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001209static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001210do_change_passphrase(struct passwd *pw)
1211{
Damien Miller95def091999-11-25 00:26:21 +11001212 char *comment;
1213 char *old_passphrase, *passphrase1, *passphrase2;
1214 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001215 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001216
Damien Miller95def091999-11-25 00:26:21 +11001217 if (!have_identity)
1218 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001219 if (stat(identity_file, &st) < 0) {
1220 perror(identity_file);
1221 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001222 }
Damien Miller95def091999-11-25 00:26:21 +11001223 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001224 private = key_load_private(identity_file, "", &comment);
1225 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001226 if (identity_passphrase)
1227 old_passphrase = xstrdup(identity_passphrase);
1228 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001229 old_passphrase =
1230 read_passphrase("Enter old passphrase: ",
1231 RP_ALLOW_STDIN);
1232 private = key_load_private(identity_file, old_passphrase,
1233 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001234 memset(old_passphrase, 0, strlen(old_passphrase));
1235 xfree(old_passphrase);
1236 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001237 printf("Bad passphrase.\n");
1238 exit(1);
1239 }
Damien Miller95def091999-11-25 00:26:21 +11001240 }
1241 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001242
Damien Miller95def091999-11-25 00:26:21 +11001243 /* Ask the new passphrase (twice). */
1244 if (identity_new_passphrase) {
1245 passphrase1 = xstrdup(identity_new_passphrase);
1246 passphrase2 = NULL;
1247 } else {
1248 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001249 read_passphrase("Enter new passphrase (empty for no "
1250 "passphrase): ", RP_ALLOW_STDIN);
1251 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001252 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001253
1254 /* Verify that they are the same. */
1255 if (strcmp(passphrase1, passphrase2) != 0) {
1256 memset(passphrase1, 0, strlen(passphrase1));
1257 memset(passphrase2, 0, strlen(passphrase2));
1258 xfree(passphrase1);
1259 xfree(passphrase2);
1260 printf("Pass phrases do not match. Try again.\n");
1261 exit(1);
1262 }
1263 /* Destroy the other copy. */
1264 memset(passphrase2, 0, strlen(passphrase2));
1265 xfree(passphrase2);
1266 }
1267
1268 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001269 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001270 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001271 memset(passphrase1, 0, strlen(passphrase1));
1272 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001273 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001274 xfree(comment);
1275 exit(1);
1276 }
1277 /* Destroy the passphrase and the copy of the key in memory. */
1278 memset(passphrase1, 0, strlen(passphrase1));
1279 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001280 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +11001281 xfree(comment);
1282
1283 printf("Your identification has been saved with the new passphrase.\n");
1284 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001285}
1286
Damien Miller37876e92003-05-15 10:19:46 +10001287/*
1288 * Print the SSHFP RR.
1289 */
Damien Millercb314822006-03-26 13:48:01 +11001290static int
1291do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001292{
1293 Key *public;
1294 char *comment = NULL;
1295 struct stat st;
1296
Damien Millercb314822006-03-26 13:48:01 +11001297 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +10001298 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +11001299 if (stat(fname, &st) < 0) {
1300 if (errno == ENOENT)
1301 return 0;
1302 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001303 exit(1);
1304 }
Damien Millercb314822006-03-26 13:48:01 +11001305 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001306 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001307 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001308 key_free(public);
1309 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +11001310 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001311 }
1312 if (comment)
1313 xfree(comment);
1314
Damien Millercb314822006-03-26 13:48:01 +11001315 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001316 exit(1);
1317}
Damien Miller37876e92003-05-15 10:19:46 +10001318
Damien Miller95def091999-11-25 00:26:21 +11001319/*
1320 * Change the comment of a private key file.
1321 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001322static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001323do_change_comment(struct passwd *pw)
1324{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001325 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001326 Key *private;
1327 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001328 struct stat st;
1329 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001330 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001331
Damien Miller95def091999-11-25 00:26:21 +11001332 if (!have_identity)
1333 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001334 if (stat(identity_file, &st) < 0) {
1335 perror(identity_file);
1336 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001337 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001338 private = key_load_private(identity_file, "", &comment);
1339 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001340 if (identity_passphrase)
1341 passphrase = xstrdup(identity_passphrase);
1342 else if (identity_new_passphrase)
1343 passphrase = xstrdup(identity_new_passphrase);
1344 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001345 passphrase = read_passphrase("Enter passphrase: ",
1346 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001347 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001348 private = key_load_private(identity_file, passphrase, &comment);
1349 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001350 memset(passphrase, 0, strlen(passphrase));
1351 xfree(passphrase);
1352 printf("Bad passphrase.\n");
1353 exit(1);
1354 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001355 } else {
1356 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001357 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001358 if (private->type != KEY_RSA1) {
1359 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1360 key_free(private);
1361 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001362 }
Damien Miller95def091999-11-25 00:26:21 +11001363 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001364
Damien Miller95def091999-11-25 00:26:21 +11001365 if (identity_comment) {
1366 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1367 } else {
1368 printf("Enter new comment: ");
1369 fflush(stdout);
1370 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1371 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001372 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001373 exit(1);
1374 }
Damien Miller14b017d2007-09-17 16:09:15 +10001375 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001376 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001377
Damien Miller95def091999-11-25 00:26:21 +11001378 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001379 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001380 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001381 memset(passphrase, 0, strlen(passphrase));
1382 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001383 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001384 xfree(comment);
1385 exit(1);
1386 }
Damien Miller95def091999-11-25 00:26:21 +11001387 memset(passphrase, 0, strlen(passphrase));
1388 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001389 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001390 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001391
Damien Miller95def091999-11-25 00:26:21 +11001392 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001393 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1394 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001395 printf("Could not save your public key in %s\n", identity_file);
1396 exit(1);
1397 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001398 f = fdopen(fd, "w");
1399 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001400 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001401 exit(1);
1402 }
Damien Millereba71ba2000-04-29 23:57:08 +10001403 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001404 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001405 key_free(public);
1406 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001407 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001408
Damien Miller95def091999-11-25 00:26:21 +11001409 xfree(comment);
1410
1411 printf("The comment in your key file has been changed.\n");
1412 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001413}
1414
Damien Miller0a80ca12010-02-27 07:55:05 +11001415static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001416fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001417{
1418 char from[32], to[32];
1419 static char ret[64];
1420 time_t tt;
1421 struct tm *tm;
1422
1423 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001424 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001425 return "forever";
1426
Damien Millerf2b70ca2010-03-05 07:39:35 +11001427 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001428 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001429 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001430 tm = localtime(&tt);
1431 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1432 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001433 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001434 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001435 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001436 tm = localtime(&tt);
1437 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1438 }
1439
Damien Millerf2b70ca2010-03-05 07:39:35 +11001440 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001441 snprintf(ret, sizeof(ret), "before %s", to);
1442 return ret;
1443 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001444 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001445 snprintf(ret, sizeof(ret), "after %s", from);
1446 return ret;
1447 }
1448
1449 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1450 return ret;
1451}
1452
1453static void
Damien Miller4e270b02010-04-16 15:56:21 +10001454add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001455{
1456 debug3("%s: %s", __func__, name);
1457 buffer_put_cstring(c, name);
1458 buffer_put_string(c, NULL, 0);
1459}
1460
1461static void
Damien Miller4e270b02010-04-16 15:56:21 +10001462add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001463{
1464 Buffer b;
1465
1466 debug3("%s: %s=%s", __func__, name, value);
1467 buffer_init(&b);
1468 buffer_put_cstring(&b, value);
1469
1470 buffer_put_cstring(c, name);
1471 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1472
1473 buffer_free(&b);
1474}
1475
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001476#define OPTIONS_CRITICAL 1
1477#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001478static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001479prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001480{
Damien Miller0a80ca12010-02-27 07:55:05 +11001481 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001482 if ((which & OPTIONS_CRITICAL) != 0 &&
1483 certflags_command != NULL)
1484 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001485 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001486 (certflags_flags & CERTOPT_X_FWD) != 0)
1487 add_flag_option(c, "permit-X11-forwarding");
1488 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001489 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001490 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001491 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1492 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001493 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001494 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1495 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001496 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001497 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1498 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001499 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001500 if ((which & OPTIONS_CRITICAL) != 0 &&
1501 certflags_src_addr != NULL)
1502 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001503}
1504
Damien Miller757f34e2010-08-05 13:05:31 +10001505static Key *
1506load_pkcs11_key(char *path)
1507{
1508#ifdef ENABLE_PKCS11
1509 Key **keys = NULL, *public, *private = NULL;
1510 int i, nkeys;
1511
1512 if ((public = key_load_public(path, NULL)) == NULL)
1513 fatal("Couldn't load CA public key \"%s\"", path);
1514
1515 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1516 debug3("%s: %d keys", __func__, nkeys);
1517 if (nkeys <= 0)
1518 fatal("cannot read public key from pkcs11");
1519 for (i = 0; i < nkeys; i++) {
1520 if (key_equal_public(public, keys[i])) {
1521 private = keys[i];
1522 continue;
1523 }
1524 key_free(keys[i]);
1525 }
1526 xfree(keys);
1527 key_free(public);
1528 return private;
1529#else
1530 fatal("no pkcs11 support");
1531#endif /* ENABLE_PKCS11 */
1532}
1533
Damien Miller0a80ca12010-02-27 07:55:05 +11001534static void
1535do_ca_sign(struct passwd *pw, int argc, char **argv)
1536{
1537 int i, fd;
1538 u_int n;
1539 Key *ca, *public;
1540 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1541 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001542 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001543
Damien Miller4e270b02010-04-16 15:56:21 +10001544 if (key_type_name != NULL) {
1545 switch (key_type_from_name(key_type_name)) {
1546 case KEY_RSA_CERT_V00:
1547 case KEY_DSA_CERT_V00:
1548 v00 = 1;
1549 break;
1550 case KEY_UNSPEC:
1551 if (strcasecmp(key_type_name, "v00") == 0) {
1552 v00 = 1;
1553 break;
1554 } else if (strcasecmp(key_type_name, "v01") == 0)
1555 break;
1556 /* FALLTHROUGH */
1557 default:
1558 fprintf(stderr, "unknown key type %s\n", key_type_name);
1559 exit(1);
1560 }
1561 }
1562
Damien Miller757f34e2010-08-05 13:05:31 +10001563 pkcs11_init(1);
1564 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1565 if (pkcs11provider != NULL) {
1566 if ((ca = load_pkcs11_key(tmp)) == NULL)
1567 fatal("No PKCS#11 key matching %s found", ca_key_path);
1568 } else if ((ca = load_identity(tmp)) == NULL)
1569 fatal("Couldn't load CA key \"%s\"", tmp);
1570 xfree(tmp);
1571
Damien Miller0a80ca12010-02-27 07:55:05 +11001572 for (i = 0; i < argc; i++) {
1573 /* Split list of principals */
1574 n = 0;
1575 if (cert_principals != NULL) {
1576 otmp = tmp = xstrdup(cert_principals);
1577 plist = NULL;
1578 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1579 plist = xrealloc(plist, n + 1, sizeof(*plist));
1580 if (*(plist[n] = xstrdup(cp)) == '\0')
1581 fatal("Empty principal name");
1582 }
1583 xfree(otmp);
1584 }
1585
1586 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1587 if ((public = key_load_public(tmp, &comment)) == NULL)
1588 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001589 if (public->type != KEY_RSA && public->type != KEY_DSA &&
1590 public->type != KEY_ECDSA)
Damien Miller0a80ca12010-02-27 07:55:05 +11001591 fatal("%s: key \"%s\" type %s cannot be certified",
1592 __func__, tmp, key_type(public));
1593
1594 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001595 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001596 fatal("Could not upgrade key %s to certificate", tmp);
1597 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001598 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001599 public->cert->key_id = xstrdup(cert_key_id);
1600 public->cert->nprincipals = n;
1601 public->cert->principals = plist;
1602 public->cert->valid_after = cert_valid_from;
1603 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001604 if (v00) {
1605 prepare_options_buf(&public->cert->critical,
1606 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1607 } else {
1608 prepare_options_buf(&public->cert->critical,
1609 OPTIONS_CRITICAL);
1610 prepare_options_buf(&public->cert->extensions,
1611 OPTIONS_EXTENSIONS);
1612 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001613 public->cert->signature_key = key_from_private(ca);
1614
1615 if (key_certify(public, ca) != 0)
1616 fatal("Couldn't not certify key %s", tmp);
1617
1618 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1619 *cp = '\0';
1620 xasprintf(&out, "%s-cert.pub", tmp);
1621 xfree(tmp);
1622
1623 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1624 fatal("Could not open \"%s\" for writing: %s", out,
1625 strerror(errno));
1626 if ((f = fdopen(fd, "w")) == NULL)
1627 fatal("%s: fdopen: %s", __func__, strerror(errno));
1628 if (!key_write(public, f))
1629 fatal("Could not write certified key to %s", out);
1630 fprintf(f, " %s\n", comment);
1631 fclose(f);
1632
Damien Miller4e270b02010-04-16 15:56:21 +10001633 if (!quiet) {
1634 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1635 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001636 out, public->cert->key_id,
1637 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001638 cert_principals != NULL ? " for " : "",
1639 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001640 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001641 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001642
1643 key_free(public);
1644 xfree(out);
1645 }
Damien Miller757f34e2010-08-05 13:05:31 +10001646 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001647 exit(0);
1648}
1649
1650static u_int64_t
1651parse_relative_time(const char *s, time_t now)
1652{
1653 int64_t mul, secs;
1654
1655 mul = *s == '-' ? -1 : 1;
1656
1657 if ((secs = convtime(s + 1)) == -1)
1658 fatal("Invalid relative certificate time %s", s);
1659 if (mul == -1 && secs > now)
1660 fatal("Certificate time %s cannot be represented", s);
1661 return now + (u_int64_t)(secs * mul);
1662}
1663
1664static u_int64_t
1665parse_absolute_time(const char *s)
1666{
1667 struct tm tm;
1668 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001669 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001670
Damien Miller2ca342b2010-03-03 12:14:15 +11001671 /*
1672 * POSIX strptime says "The application shall ensure that there
1673 * is white-space or other non-alphanumeric characters between
1674 * any two conversion specifications" so arrange things this way.
1675 */
1676 switch (strlen(s)) {
1677 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001678 fmt = "%Y-%m-%d";
1679 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001680 break;
1681 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001682 fmt = "%Y-%m-%dT%H:%M:%S";
1683 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001684 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1685 break;
1686 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001687 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001688 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001689
1690 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001691 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001692 fatal("Invalid certificate time %s", s);
1693 if ((tt = mktime(&tm)) < 0)
1694 fatal("Certificate time %s cannot be represented", s);
1695 return (u_int64_t)tt;
1696}
1697
1698static void
1699parse_cert_times(char *timespec)
1700{
1701 char *from, *to;
1702 time_t now = time(NULL);
1703 int64_t secs;
1704
1705 /* +timespec relative to now */
1706 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1707 if ((secs = convtime(timespec + 1)) == -1)
1708 fatal("Invalid relative certificate life %s", timespec);
1709 cert_valid_to = now + secs;
1710 /*
1711 * Backdate certificate one minute to avoid problems on hosts
1712 * with poorly-synchronised clocks.
1713 */
1714 cert_valid_from = ((now - 59)/ 60) * 60;
1715 return;
1716 }
1717
1718 /*
1719 * from:to, where
1720 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1721 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1722 */
1723 from = xstrdup(timespec);
1724 to = strchr(from, ':');
1725 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001726 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001727 *to++ = '\0';
1728
1729 if (*from == '-' || *from == '+')
1730 cert_valid_from = parse_relative_time(from, now);
1731 else
1732 cert_valid_from = parse_absolute_time(from);
1733
1734 if (*to == '-' || *to == '+')
1735 cert_valid_to = parse_relative_time(to, cert_valid_from);
1736 else
1737 cert_valid_to = parse_absolute_time(to);
1738
1739 if (cert_valid_to <= cert_valid_from)
1740 fatal("Empty certificate validity interval");
1741 xfree(from);
1742}
1743
1744static void
Damien Miller4e270b02010-04-16 15:56:21 +10001745add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001746{
1747 char *val;
1748
Damien Miller044f4a62011-05-05 14:14:08 +10001749 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001750 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001751 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001752 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001753 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001754 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001755 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001756 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001757 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001758 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001759 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001760 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001761 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001762 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001763 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001764 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001765 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001766 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001767 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001768 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001769 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001770 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001771 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1772 val = opt + 14;
1773 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001774 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001775 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001776 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001777 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001778 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1779 val = opt + 15;
1780 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001781 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001782 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001783 fatal("source-address already specified");
1784 if (addr_match_cidr_list(NULL, val) != 0)
1785 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001786 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001787 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001788 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001789}
1790
Ben Lindstrombba81212001-06-25 05:01:22 +00001791static void
Damien Millerd834d352010-06-26 09:48:02 +10001792show_options(const Buffer *optbuf, int v00, int in_critical)
1793{
1794 u_char *name, *data;
1795 u_int dlen;
1796 Buffer options, option;
1797
1798 buffer_init(&options);
1799 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1800
1801 buffer_init(&option);
1802 while (buffer_len(&options) != 0) {
1803 name = buffer_get_string(&options, NULL);
1804 data = buffer_get_string_ptr(&options, &dlen);
1805 buffer_append(&option, data, dlen);
1806 printf(" %s", name);
1807 if ((v00 || !in_critical) &&
1808 (strcmp(name, "permit-X11-forwarding") == 0 ||
1809 strcmp(name, "permit-agent-forwarding") == 0 ||
1810 strcmp(name, "permit-port-forwarding") == 0 ||
1811 strcmp(name, "permit-pty") == 0 ||
1812 strcmp(name, "permit-user-rc") == 0))
1813 printf("\n");
1814 else if ((v00 || in_critical) &&
1815 (strcmp(name, "force-command") == 0 ||
1816 strcmp(name, "source-address") == 0)) {
1817 data = buffer_get_string(&option, NULL);
1818 printf(" %s\n", data);
1819 xfree(data);
1820 } else {
1821 printf(" UNKNOWN OPTION (len %u)\n",
1822 buffer_len(&option));
1823 buffer_clear(&option);
1824 }
1825 xfree(name);
1826 if (buffer_len(&option) != 0)
1827 fatal("Option corrupt: extra data at end");
1828 }
1829 buffer_free(&option);
1830 buffer_free(&options);
1831}
1832
1833static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001834do_show_cert(struct passwd *pw)
1835{
1836 Key *key;
1837 struct stat st;
1838 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001839 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001840
1841 if (!have_identity)
1842 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001843 if (stat(identity_file, &st) < 0)
1844 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001845 if ((key = key_load_public(identity_file, NULL)) == NULL)
1846 fatal("%s is not a public key", identity_file);
1847 if (!key_is_cert(key))
1848 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001849 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1850
Damien Millerf2b70ca2010-03-05 07:39:35 +11001851 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1852 ca_fp = key_fingerprint(key->cert->signature_key,
1853 SSH_FP_MD5, SSH_FP_HEX);
1854
1855 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001856 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1857 key_cert_type(key));
1858 printf(" Public key: %s %s\n", key_type(key), key_fp);
1859 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001860 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001861 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001862 if (!v00) {
1863 printf(" Serial: %llu\n",
1864 (unsigned long long)key->cert->serial);
1865 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001866 printf(" Valid: %s\n",
1867 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1868 printf(" Principals: ");
1869 if (key->cert->nprincipals == 0)
1870 printf("(none)\n");
1871 else {
1872 for (i = 0; i < key->cert->nprincipals; i++)
1873 printf("\n %s",
1874 key->cert->principals[i]);
1875 printf("\n");
1876 }
Damien Miller4e270b02010-04-16 15:56:21 +10001877 printf(" Critical Options: ");
1878 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001879 printf("(none)\n");
1880 else {
1881 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001882 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001883 }
Damien Miller4e270b02010-04-16 15:56:21 +10001884 if (!v00) {
1885 printf(" Extensions: ");
1886 if (buffer_len(&key->cert->extensions) == 0)
1887 printf("(none)\n");
1888 else {
1889 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001890 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001891 }
1892 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001893 exit(0);
1894}
1895
1896static void
Damien Miller431f66b1999-11-21 18:31:57 +11001897usage(void)
1898{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001899 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001900 fprintf(stderr, "Options:\n");
Damien Miller58f1baf2011-05-05 14:06:15 +10001901 fprintf(stderr, " -A Generate non-existent host keys for all key types.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001902 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001903 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001904 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001905 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001906 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001907#ifdef ENABLE_PKCS11
1908 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1909#endif
Damien Miller44b25042010-07-02 13:35:01 +10001910 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001911 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1912 fprintf(stderr, " -f filename Filename of the key file.\n");
1913 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1914 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1915 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001916 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1917 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10001918 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Miller709a1e92012-07-31 12:20:43 +10001919 fprintf(stderr, " -J number Screen this number of moduli lines.\n");
Damien Millerdfceafe2012-07-06 13:44:19 +10001920 fprintf(stderr, " -j number Start screening moduli at specified line.\n");
Damien Miller927d82b2011-10-18 16:05:38 +11001921 fprintf(stderr, " -K checkpt Write checkpoints to this file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001922 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001923 fprintf(stderr, " -l Show fingerprint of key file.\n");
1924 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001925 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001926 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001927 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10001928 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001929 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1930 fprintf(stderr, " -p Change passphrase of private key file.\n");
1931 fprintf(stderr, " -q Quiet.\n");
1932 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1933 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1934 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001935 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001936 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1937 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001938 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001939 fprintf(stderr, " -v Verbose.\n");
1940 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1941 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001942 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001943
Damien Miller95def091999-11-25 00:26:21 +11001944 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001945}
1946
Damien Miller95def091999-11-25 00:26:21 +11001947/*
1948 * Main program for key management.
1949 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001950int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001951main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001952{
Damien Millera41c8b12002-01-22 23:05:08 +11001953 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11001954 char *checkpoint = NULL;
Damien Miller6f3b3622012-11-14 19:04:33 +11001955 char out_file[MAXPATHLEN], *rr_hostname = NULL, *ep;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001956 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001957 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001958 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001959 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001960 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001961 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Miller58f1baf2011-05-05 14:06:15 +10001962 int gen_all_hostkeys = 0;
Damien Millerdfceafe2012-07-06 13:44:19 +10001963 unsigned long start_lineno = 0, lines_to_process = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10001964 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001965 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001966 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001967
Damien Miller95def091999-11-25 00:26:21 +11001968 extern int optind;
1969 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001970
Darren Tuckerce321d82005-10-03 18:11:24 +10001971 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1972 sanitise_stdfd();
1973
Darren Tucker9ac56e92007-01-14 10:19:59 +11001974 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001975
Damien Miller4314c2b2010-09-10 11:12:09 +10001976 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001977 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001978
Kevin Steves3a881912002-07-20 19:05:40 +00001979 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001980
Damien Miller5428f641999-11-25 11:54:57 +11001981 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001982 pw = getpwuid(getuid());
1983 if (!pw) {
1984 printf("You don't exist, go away!\n");
1985 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001986 }
Damien Millereba71ba2000-04-29 23:57:08 +10001987 if (gethostname(hostname, sizeof(hostname)) < 0) {
1988 perror("gethostname");
1989 exit(1);
1990 }
Damien Miller5428f641999-11-25 11:54:57 +11001991
Damien Millerdfceafe2012-07-06 13:44:19 +10001992 while ((opt = getopt(argc, argv, "AegiqpclBHLhvxXyF:b:f:t:D:I:J:j:K:P:"
Darren Tucker0dc283b2012-10-05 10:52:51 +10001993 "m:N:n:O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001994 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10001995 case 'A':
1996 gen_all_hostkeys = 1;
1997 break;
Damien Miller95def091999-11-25 00:26:21 +11001998 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10001999 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10002000 if (errstr)
2001 fatal("Bits has bad value %s (%s)",
2002 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11002003 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002004 case 'F':
2005 find_host = 1;
2006 rr_hostname = optarg;
2007 break;
2008 case 'H':
2009 hash_hosts = 1;
2010 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002011 case 'I':
2012 cert_key_id = optarg;
2013 break;
Damien Millerdfceafe2012-07-06 13:44:19 +10002014 case 'J':
2015 lines_to_process = strtoul(optarg, NULL, 10);
2016 break;
2017 case 'j':
2018 start_lineno = strtoul(optarg, NULL, 10);
2019 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11002020 case 'R':
2021 delete_host = 1;
2022 rr_hostname = optarg;
2023 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11002024 case 'L':
2025 show_cert = 1;
2026 break;
Damien Miller95def091999-11-25 00:26:21 +11002027 case 'l':
2028 print_fingerprint = 1;
2029 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002030 case 'B':
2031 print_bubblebabble = 1;
2032 break;
Damien Miller44b25042010-07-02 13:35:01 +10002033 case 'm':
2034 if (strcasecmp(optarg, "RFC4716") == 0 ||
2035 strcasecmp(optarg, "ssh2") == 0) {
2036 convert_format = FMT_RFC4716;
2037 break;
2038 }
2039 if (strcasecmp(optarg, "PKCS8") == 0) {
2040 convert_format = FMT_PKCS8;
2041 break;
2042 }
2043 if (strcasecmp(optarg, "PEM") == 0) {
2044 convert_format = FMT_PEM;
2045 break;
2046 }
2047 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002048 case 'n':
2049 cert_principals = optarg;
2050 break;
Damien Miller95def091999-11-25 00:26:21 +11002051 case 'p':
2052 change_passphrase = 1;
2053 break;
Damien Miller95def091999-11-25 00:26:21 +11002054 case 'c':
2055 change_comment = 1;
2056 break;
Damien Miller95def091999-11-25 00:26:21 +11002057 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002058 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2059 sizeof(identity_file))
2060 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002061 have_identity = 1;
2062 break;
Damien Miller37876e92003-05-15 10:19:46 +10002063 case 'g':
2064 print_generic = 1;
2065 break;
Damien Miller95def091999-11-25 00:26:21 +11002066 case 'P':
2067 identity_passphrase = optarg;
2068 break;
Damien Miller95def091999-11-25 00:26:21 +11002069 case 'N':
2070 identity_new_passphrase = optarg;
2071 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002072 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002073 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002074 break;
Damien Miller95def091999-11-25 00:26:21 +11002075 case 'C':
2076 identity_comment = optarg;
2077 break;
Damien Miller95def091999-11-25 00:26:21 +11002078 case 'q':
2079 quiet = 1;
2080 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002081 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002082 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002083 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002084 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002085 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002086 case 'h':
2087 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002088 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002089 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002090 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002091 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002092 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002093 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002094 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002095 case 'y':
2096 print_public = 1;
2097 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002098 case 's':
2099 ca_key_path = optarg;
2100 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002101 case 't':
2102 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002103 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002104 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002105 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002106 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002107 case 'v':
2108 if (log_level == SYSLOG_LEVEL_INFO)
2109 log_level = SYSLOG_LEVEL_DEBUG1;
2110 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002111 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002112 log_level < SYSLOG_LEVEL_DEBUG3)
2113 log_level++;
2114 }
2115 break;
Damien Miller37876e92003-05-15 10:19:46 +10002116 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002117 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002118 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002119 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002120 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2121 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002122 if (errstr)
2123 fatal("Desired generator has bad value: %s (%s)",
2124 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002125 break;
2126 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11002127 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002128 if (errstr)
2129 fatal("Invalid number of trials: %s (%s)",
2130 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002131 break;
2132 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002133 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002134 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002135 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002136 break;
2137 case 'G':
2138 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002139 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2140 sizeof(out_file))
2141 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002142 break;
2143 case 'T':
2144 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002145 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2146 sizeof(out_file))
2147 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002148 break;
Damien Miller390d0562011-10-18 16:05:19 +11002149 case 'K':
2150 if (strlen(optarg) >= MAXPATHLEN)
2151 fatal("Checkpoint filename too long");
2152 checkpoint = xstrdup(optarg);
2153 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002154 case 'S':
2155 /* XXX - also compare length against bits */
2156 if (BN_hex2bn(&start, optarg) == 0)
2157 fatal("Invalid start point.");
2158 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002159 case 'V':
2160 parse_cert_times(optarg);
2161 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002162 case 'z':
Damien Miller6f3b3622012-11-14 19:04:33 +11002163 errno = 0;
2164 cert_serial = strtoull(optarg, &ep, 10);
2165 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
2166 (errno == ERANGE && cert_serial == ULLONG_MAX))
2167 fatal("Invalid serial number \"%s\"", optarg);
Damien Miller4e270b02010-04-16 15:56:21 +10002168 break;
Damien Miller95def091999-11-25 00:26:21 +11002169 case '?':
2170 default:
2171 usage();
2172 }
2173 }
Darren Tucker06930c72003-12-31 11:34:51 +11002174
2175 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002176 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002177
Damien Miller0a80ca12010-02-27 07:55:05 +11002178 argv += optind;
2179 argc -= optind;
2180
2181 if (ca_key_path != NULL) {
2182 if (argc < 1) {
2183 printf("Too few arguments.\n");
2184 usage();
2185 }
2186 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11002187 printf("Too many arguments.\n");
2188 usage();
2189 }
2190 if (change_passphrase && change_comment) {
2191 printf("Can only have one of -p and -c.\n");
2192 usage();
2193 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002194 if (print_fingerprint && (delete_host || hash_hosts)) {
Damien Millerec77c952013-01-09 15:58:00 +11002195 printf("Cannot use -l with -H or -R.\n");
Darren Tucker0f7e9102008-06-08 12:54:29 +10002196 usage();
2197 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002198 if (ca_key_path != NULL) {
2199 if (cert_key_id == NULL)
2200 fatal("Must specify key id (-I) when certifying");
2201 do_ca_sign(pw, argc, argv);
2202 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002203 if (show_cert)
2204 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002205 if (delete_host || hash_hosts || find_host)
2206 do_known_hosts(pw, rr_hostname);
Damien Millerec77c952013-01-09 15:58:00 +11002207 if (pkcs11provider != NULL)
2208 do_download(pw);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002209 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002210 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002211 if (change_passphrase)
2212 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002213 if (change_comment)
2214 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002215 if (convert_to)
2216 do_convert_to(pw);
2217 if (convert_from)
2218 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002219 if (print_public)
2220 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002221 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002222 unsigned int n = 0;
2223
2224 if (have_identity) {
2225 n = do_print_resource_record(pw,
2226 identity_file, rr_hostname);
2227 if (n == 0) {
2228 perror(identity_file);
2229 exit(1);
2230 }
2231 exit(0);
2232 } else {
2233
2234 n += do_print_resource_record(pw,
2235 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2236 n += do_print_resource_record(pw,
2237 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002238 n += do_print_resource_record(pw,
2239 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002240
2241 if (n == 0)
2242 fatal("no keys found.");
2243 exit(0);
2244 }
Damien Miller37876e92003-05-15 10:19:46 +10002245 }
Damien Miller95def091999-11-25 00:26:21 +11002246
Darren Tucker019cefe2003-08-02 22:40:07 +10002247 if (do_gen_candidates) {
2248 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002249
Darren Tucker019cefe2003-08-02 22:40:07 +10002250 if (out == NULL) {
2251 error("Couldn't open modulus candidate file \"%s\": %s",
2252 out_file, strerror(errno));
2253 return (1);
2254 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002255 if (bits == 0)
2256 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002257 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002258 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002259
2260 return (0);
2261 }
2262
2263 if (do_screen_candidates) {
2264 FILE *in;
2265 FILE *out = fopen(out_file, "w");
2266
2267 if (have_identity && strcmp(identity_file, "-") != 0) {
2268 if ((in = fopen(identity_file, "r")) == NULL) {
2269 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002270 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002271 strerror(errno));
2272 }
2273 } else
2274 in = stdin;
2275
2276 if (out == NULL) {
2277 fatal("Couldn't open moduli file \"%s\": %s",
2278 out_file, strerror(errno));
2279 }
Damien Millerdfceafe2012-07-06 13:44:19 +10002280 if (prime_test(in, out, trials, generator_wanted, checkpoint,
2281 start_lineno, lines_to_process) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002282 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002283 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002284 }
2285
Damien Miller58f1baf2011-05-05 14:06:15 +10002286 if (gen_all_hostkeys) {
2287 do_gen_all_hostkeys(pw);
2288 return (0);
2289 }
2290
Damien Miller95def091999-11-25 00:26:21 +11002291 arc4random_stir();
2292
Damien Millerf14be5c2005-11-05 15:15:49 +11002293 if (key_type_name == NULL)
2294 key_type_name = "rsa";
2295
Damien Millere39cacc2000-11-29 12:18:44 +11002296 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002297 type_bits_valid(type, &bits);
2298
Darren Tucker3af2ac52005-11-29 13:10:24 +11002299 if (!quiet)
2300 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002301 private = key_generate(type, bits);
2302 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002303 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002304 exit(1);
2305 }
2306 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002307
2308 if (!have_identity)
2309 ask_filename(pw, "Enter file in which to save the key");
2310
Damien Miller788f2122005-11-05 15:14:59 +11002311 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002312 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2313 pw->pw_dir, _PATH_SSH_USER_DIR);
2314 if (strstr(identity_file, dotsshdir) != NULL) {
2315 if (stat(dotsshdir, &st) < 0) {
2316 if (errno != ENOENT) {
2317 error("Could not stat %s: %s", dotsshdir,
2318 strerror(errno));
2319 } else if (mkdir(dotsshdir, 0700) < 0) {
2320 error("Could not create directory '%s': %s",
2321 dotsshdir, strerror(errno));
2322 } else if (!quiet)
2323 printf("Created directory '%s'.\n", dotsshdir);
2324 }
Damien Miller95def091999-11-25 00:26:21 +11002325 }
2326 /* If the file already exists, ask the user to confirm. */
2327 if (stat(identity_file, &st) >= 0) {
2328 char yesno[3];
2329 printf("%s already exists.\n", identity_file);
2330 printf("Overwrite (y/n)? ");
2331 fflush(stdout);
2332 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2333 exit(1);
2334 if (yesno[0] != 'y' && yesno[0] != 'Y')
2335 exit(1);
2336 }
2337 /* Ask for a passphrase (twice). */
2338 if (identity_passphrase)
2339 passphrase1 = xstrdup(identity_passphrase);
2340 else if (identity_new_passphrase)
2341 passphrase1 = xstrdup(identity_new_passphrase);
2342 else {
2343passphrase_again:
2344 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002345 read_passphrase("Enter passphrase (empty for no "
2346 "passphrase): ", RP_ALLOW_STDIN);
2347 passphrase2 = read_passphrase("Enter same passphrase again: ",
2348 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002349 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002350 /*
2351 * The passphrases do not match. Clear them and
2352 * retry.
2353 */
Damien Miller95def091999-11-25 00:26:21 +11002354 memset(passphrase1, 0, strlen(passphrase1));
2355 memset(passphrase2, 0, strlen(passphrase2));
2356 xfree(passphrase1);
2357 xfree(passphrase2);
2358 printf("Passphrases do not match. Try again.\n");
2359 goto passphrase_again;
2360 }
2361 /* Clear the other copy of the passphrase. */
2362 memset(passphrase2, 0, strlen(passphrase2));
2363 xfree(passphrase2);
2364 }
2365
Damien Miller95def091999-11-25 00:26:21 +11002366 if (identity_comment) {
2367 strlcpy(comment, identity_comment, sizeof(comment));
2368 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002369 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002370 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2371 }
2372
2373 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00002374 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002375 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002376 memset(passphrase1, 0, strlen(passphrase1));
2377 xfree(passphrase1);
2378 exit(1);
2379 }
2380 /* Clear the passphrase. */
2381 memset(passphrase1, 0, strlen(passphrase1));
2382 xfree(passphrase1);
2383
2384 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002385 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002386 arc4random_stir();
2387
2388 if (!quiet)
2389 printf("Your identification has been saved in %s.\n", identity_file);
2390
Damien Miller95def091999-11-25 00:26:21 +11002391 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002392 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2393 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002394 printf("Could not save your public key in %s\n", identity_file);
2395 exit(1);
2396 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002397 f = fdopen(fd, "w");
2398 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002399 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002400 exit(1);
2401 }
Damien Millereba71ba2000-04-29 23:57:08 +10002402 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002403 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002404 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002405 fclose(f);
2406
2407 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002408 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002409 char *ra = key_fingerprint(public, SSH_FP_MD5,
2410 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002411 printf("Your public key has been saved in %s.\n",
2412 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002413 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002414 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002415 printf("The key's randomart image is:\n");
2416 printf("%s\n", ra);
2417 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002418 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002419 }
Damien Millereba71ba2000-04-29 23:57:08 +10002420
2421 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002422 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002423}