blob: 49e4eee10ff58a65f2a7865c70578c5089f65bf1 [file] [log] [blame]
Damien Miller2ce12ef2011-05-05 14:17:18 +10001/* $OpenBSD: ssh-keygen.c,v 1.210 2011/04/18 00:46:05 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 Miller874d77b2000-10-14 16:23:11 +110052
Damien Miller7ea845e2010-02-12 09:21:02 +110053#ifdef ENABLE_PKCS11
54#include "ssh-pkcs11.h"
Ben Lindstrombcc18082001-08-06 21:59:25 +000055#endif
Ben Lindstromcd392282001-07-04 03:44:03 +000056
Damien Miller3f54a9f2005-11-05 14:52:18 +110057/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
58#define DEFAULT_BITS 2048
59#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100060#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110061u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
Damien Miller5428f641999-11-25 11:54:57 +110063/*
64 * Flag indicating that we just want to change the passphrase. This can be
65 * set on the command line.
66 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067int change_passphrase = 0;
68
Damien Miller5428f641999-11-25 11:54:57 +110069/*
70 * Flag indicating that we just want to change the comment. This can be set
71 * on the command line.
72 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073int change_comment = 0;
74
75int quiet = 0;
76
Darren Tucker35c45532008-06-13 04:43:15 +100077int log_level = SYSLOG_LEVEL_INFO;
78
Damien Miller4b42d7f2005-03-01 21:48:35 +110079/* Flag indicating that we want to hash a known_hosts file */
80int hash_hosts = 0;
81/* Flag indicating that we want lookup a host in known_hosts file */
82int find_host = 0;
83/* Flag indicating that we want to delete a host from a known_hosts file */
84int delete_host = 0;
85
Damien Millerf2b70ca2010-03-05 07:39:35 +110086/* Flag indicating that we want to show the contents of a certificate */
87int show_cert = 0;
88
Damien Miller10f6f6b1999-11-17 17:29:08 +110089/* Flag indicating that we just want to see the key fingerprint */
90int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000091int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110092
Damien Miller431f66b1999-11-21 18:31:57 +110093/* The identity file name, given on the command line or entered by the user. */
94char identity_file[1024];
95int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100096
97/* This is set to the passphrase if given on the command line. */
98char *identity_passphrase = NULL;
99
100/* This is set to the new passphrase if given on the command line. */
101char *identity_new_passphrase = NULL;
102
103/* This is set to the new comment if given on the command line. */
104char *identity_comment = NULL;
105
Damien Miller0a80ca12010-02-27 07:55:05 +1100106/* Path to CA key when certifying keys. */
107char *ca_key_path = NULL;
108
Damien Miller4e270b02010-04-16 15:56:21 +1000109/* Certificate serial number */
110long long cert_serial = 0;
111
Damien Miller0a80ca12010-02-27 07:55:05 +1100112/* Key type when certifying */
113u_int cert_key_type = SSH2_CERT_TYPE_USER;
114
115/* "key ID" of signed key */
116char *cert_key_id = NULL;
117
118/* Comma-separated list of principal names for certifying keys */
119char *cert_principals = NULL;
120
121/* Validity period for certificates */
122u_int64_t cert_valid_from = 0;
123u_int64_t cert_valid_to = ~0ULL;
124
Damien Miller4e270b02010-04-16 15:56:21 +1000125/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000126#define CERTOPT_X_FWD (1)
127#define CERTOPT_AGENT_FWD (1<<1)
128#define CERTOPT_PORT_FWD (1<<2)
129#define CERTOPT_PTY (1<<3)
130#define CERTOPT_USER_RC (1<<4)
131#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
132 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
133u_int32_t certflags_flags = CERTOPT_DEFAULT;
134char *certflags_command = NULL;
135char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100136
Damien Miller44b25042010-07-02 13:35:01 +1000137/* Conversion to/from various formats */
138int convert_to = 0;
139int convert_from = 0;
140enum {
141 FMT_RFC4716,
142 FMT_PKCS8,
143 FMT_PEM
144} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000145int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000146int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100147
Damien Millera41c8b12002-01-22 23:05:08 +1100148char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000149
Damien Miller757f34e2010-08-05 13:05:31 +1000150/* Load key from this PKCS#11 provider */
151char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000152
Damien Miller431f66b1999-11-21 18:31:57 +1100153/* argv0 */
154extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Millereba71ba2000-04-29 23:57:08 +1000156char hostname[MAXHOSTNAMELEN];
157
Darren Tucker770fc012004-05-13 16:24:32 +1000158/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000159int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Darren Tucker770fc012004-05-13 16:24:32 +1000160int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
161
Ben Lindstrombba81212001-06-25 05:01:22 +0000162static void
Damien Miller884b63a2011-05-05 14:14:52 +1000163type_bits_valid(int type, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000164{
165 u_int maxbits;
166
167 if (type == KEY_UNSPEC) {
168 fprintf(stderr, "unknown key type %s\n", key_type_name);
169 exit(1);
170 }
Damien Miller884b63a2011-05-05 14:14:52 +1000171 if (*bitsp == 0) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000172 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000173 *bitsp = DEFAULT_BITS_DSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000174 else if (type == KEY_ECDSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000175 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000176 else
Damien Miller884b63a2011-05-05 14:14:52 +1000177 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000178 }
179 maxbits = (type == KEY_DSA) ?
180 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Damien Miller884b63a2011-05-05 14:14:52 +1000181 if (*bitsp > maxbits) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000182 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
183 exit(1);
184 }
Damien Miller884b63a2011-05-05 14:14:52 +1000185 if (type == KEY_DSA && *bitsp != 1024)
Damien Miller58f1baf2011-05-05 14:06:15 +1000186 fatal("DSA keys must be 1024 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000187 else if (type != KEY_ECDSA && *bitsp < 768)
Damien Miller58f1baf2011-05-05 14:06:15 +1000188 fatal("Key must at least be 768 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000189 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(*bitsp) == -1)
Damien Miller58f1baf2011-05-05 14:06:15 +1000190 fatal("Invalid ECDSA key length - valid lengths are "
191 "256, 384 or 521 bits");
192}
193
194static void
Damien Miller431f66b1999-11-21 18:31:57 +1100195ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196{
Damien Miller95def091999-11-25 00:26:21 +1100197 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100198 char *name = NULL;
199
Damien Miller993dd552002-02-19 15:22:47 +1100200 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000201 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100202 else {
Damien Miller993dd552002-02-19 15:22:47 +1100203 switch (key_type_from_name(key_type_name)) {
204 case KEY_RSA1:
205 name = _PATH_SSH_CLIENT_IDENTITY;
206 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000207 case KEY_DSA_CERT:
208 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100209 case KEY_DSA:
210 name = _PATH_SSH_CLIENT_ID_DSA;
211 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100212#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000213 case KEY_ECDSA_CERT:
214 case KEY_ECDSA:
215 name = _PATH_SSH_CLIENT_ID_ECDSA;
216 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100217#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000218 case KEY_RSA_CERT:
219 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100220 case KEY_RSA:
221 name = _PATH_SSH_CLIENT_ID_RSA;
222 break;
223 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100224 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100225 exit(1);
226 break;
227 }
Damien Miller90967402006-03-26 14:07:26 +1100228 }
Damien Millere39cacc2000-11-29 12:18:44 +1100229 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000230 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100231 if (fgets(buf, sizeof(buf), stdin) == NULL)
232 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000233 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100234 if (strcmp(buf, "") != 0)
235 strlcpy(identity_file, buf, sizeof(identity_file));
236 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100237}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238
Ben Lindstrombba81212001-06-25 05:01:22 +0000239static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000240load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000241{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000242 char *pass;
243 Key *prv;
244
Ben Lindstroma3700052001-04-05 23:26:32 +0000245 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000246 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000247 if (identity_passphrase)
248 pass = xstrdup(identity_passphrase);
249 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000250 pass = read_passphrase("Enter passphrase: ",
251 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000252 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000253 memset(pass, 0, strlen(pass));
254 xfree(pass);
255 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000256 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000257}
258
Damien Miller874d77b2000-10-14 16:23:11 +1100259#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000260#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100261#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000262#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000263
Ben Lindstrombba81212001-06-25 05:01:22 +0000264static void
Damien Miller44b25042010-07-02 13:35:01 +1000265do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000266{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000267 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000268 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100269 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000270
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000271 if (key_to_blob(k, &blob, &len) <= 0) {
272 fprintf(stderr, "key_to_blob failed\n");
273 exit(1);
274 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100275 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
276 snprintf(comment, sizeof(comment),
277 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000278 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000279 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100280
281 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
282 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000283 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100284 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000285 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000286 xfree(blob);
287 exit(0);
288}
289
Ben Lindstrombba81212001-06-25 05:01:22 +0000290static void
Damien Miller44b25042010-07-02 13:35:01 +1000291do_convert_to_pkcs8(Key *k)
292{
293 switch (key_type_plain(k->type)) {
294 case KEY_RSA:
295 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
296 fatal("PEM_write_RSA_PUBKEY failed");
297 break;
298 case KEY_DSA:
299 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
300 fatal("PEM_write_DSA_PUBKEY failed");
301 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000302#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000303 case KEY_ECDSA:
304 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
305 fatal("PEM_write_EC_PUBKEY failed");
306 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000307#endif
Damien Miller44b25042010-07-02 13:35:01 +1000308 default:
309 fatal("%s: unsupported key type %s", __func__, key_type(k));
310 }
311 exit(0);
312}
313
314static void
315do_convert_to_pem(Key *k)
316{
317 switch (key_type_plain(k->type)) {
318 case KEY_RSA:
319 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
320 fatal("PEM_write_RSAPublicKey failed");
321 break;
322#if notyet /* OpenSSH 0.9.8 lacks this function */
323 case KEY_DSA:
324 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
325 fatal("PEM_write_DSAPublicKey failed");
326 break;
327#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000328 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000329 default:
330 fatal("%s: unsupported key type %s", __func__, key_type(k));
331 }
332 exit(0);
333}
334
335static void
336do_convert_to(struct passwd *pw)
337{
338 Key *k;
339 struct stat st;
340
341 if (!have_identity)
342 ask_filename(pw, "Enter file in which the key is");
343 if (stat(identity_file, &st) < 0)
344 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
345 if ((k = key_load_public(identity_file, NULL)) == NULL) {
346 if ((k = load_identity(identity_file)) == NULL) {
347 fprintf(stderr, "load failed\n");
348 exit(1);
349 }
350 }
351 if (k->type == KEY_RSA1) {
352 fprintf(stderr, "version 1 keys are not supported\n");
353 exit(1);
354 }
355
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;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000726
Damien Miller7ea845e2010-02-12 09:21:02 +1100727 pkcs11_init(0);
728 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
729 if (nkeys <= 0)
730 fatal("cannot read public key from pkcs11");
731 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000732 key_write(keys[i], stdout);
733 key_free(keys[i]);
734 fprintf(stdout, "\n");
735 }
736 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100737 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000738 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100739#else
740 fatal("no pkcs11 support");
741#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000742}
Ben Lindstromcd392282001-07-04 03:44:03 +0000743
Ben Lindstrombba81212001-06-25 05:01:22 +0000744static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100745do_fingerprint(struct passwd *pw)
746{
Damien Miller98c7ad62000-03-09 21:27:49 +1100747 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000748 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000749 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100750 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000751 enum fp_rep rep;
752 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100753 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100754
Ben Lindstromd0fca422001-03-26 13:44:06 +0000755 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
756 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000757
Damien Miller95def091999-11-25 00:26:21 +1100758 if (!have_identity)
759 ask_filename(pw, "Enter file in which the key is");
760 if (stat(identity_file, &st) < 0) {
761 perror(identity_file);
762 exit(1);
763 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000764 public = key_load_public(identity_file, &comment);
765 if (public != NULL) {
766 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100767 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000768 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
769 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000770 if (log_level >= SYSLOG_LEVEL_VERBOSE)
771 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100772 key_free(public);
773 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000774 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000775 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100776 exit(0);
777 }
Damien Miller40b59852006-06-13 13:00:25 +1000778 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000779 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000780 comment = NULL;
781 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100782
Damien Millerba3420a2010-06-26 09:39:07 +1000783 if ((f = fopen(identity_file, "r")) == NULL)
784 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100785
Damien Millerba3420a2010-06-26 09:39:07 +1000786 while (fgets(line, sizeof(line), f)) {
787 if ((cp = strchr(line, '\n')) == NULL) {
788 error("line %d too long: %.40s...",
789 num + 1, line);
790 skip = 1;
791 continue;
Damien Miller95def091999-11-25 00:26:21 +1100792 }
Damien Millerba3420a2010-06-26 09:39:07 +1000793 num++;
794 if (skip) {
795 skip = 0;
796 continue;
797 }
798 *cp = '\0';
799
800 /* Skip leading whitespace, empty and comment lines. */
801 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
802 ;
803 if (!*cp || *cp == '\n' || *cp == '#')
804 continue;
805 i = strtol(cp, &ep, 10);
806 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
807 int quoted = 0;
808 comment = cp;
809 for (; *cp && (quoted || (*cp != ' ' &&
810 *cp != '\t')); cp++) {
811 if (*cp == '\\' && cp[1] == '"')
812 cp++; /* Skip both */
813 else if (*cp == '"')
814 quoted = !quoted;
815 }
816 if (!*cp)
817 continue;
818 *cp++ = '\0';
819 }
820 ep = cp;
821 public = key_new(KEY_RSA1);
822 if (key_read(public, &cp) != 1) {
823 cp = ep;
824 key_free(public);
825 public = key_new(KEY_UNSPEC);
826 if (key_read(public, &cp) != 1) {
827 key_free(public);
828 continue;
829 }
830 }
831 comment = *cp ? cp : comment;
832 fp = key_fingerprint(public, fptype, rep);
833 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
834 printf("%u %s %s (%s)\n", key_size(public), fp,
835 comment ? comment : "no comment", key_type(public));
836 if (log_level >= SYSLOG_LEVEL_VERBOSE)
837 printf("%s\n", ra);
838 xfree(ra);
839 xfree(fp);
840 key_free(public);
841 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100842 }
Damien Millerba3420a2010-06-26 09:39:07 +1000843 fclose(f);
844
Damien Miller98c7ad62000-03-09 21:27:49 +1100845 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100846 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100847 exit(1);
848 }
Damien Miller95def091999-11-25 00:26:21 +1100849 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100850}
851
Damien Miller4b42d7f2005-03-01 21:48:35 +1100852static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000853do_gen_all_hostkeys(struct passwd *pw)
854{
855 struct {
856 char *key_type;
857 char *key_type_display;
858 char *path;
859 } key_types[] = {
860 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
861 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
862 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
863 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
864 { NULL, NULL, NULL }
865 };
866
867 int first = 0;
868 struct stat st;
869 Key *private, *public;
870 char comment[1024];
871 int i, type, fd;
872 FILE *f;
873
874 for (i = 0; key_types[i].key_type; i++) {
875 if (stat(key_types[i].path, &st) == 0)
876 continue;
877 if (errno != ENOENT) {
878 printf("Could not stat %s: %s", key_types[i].path,
879 strerror(errno));
880 first = 0;
881 continue;
882 }
883
884 if (first == 0) {
885 first = 1;
886 printf("%s: generating new host keys: ", __progname);
887 }
888 printf("%s ", key_types[i].key_type_display);
889 fflush(stdout);
890 arc4random_stir();
891 type = key_type_from_name(key_types[i].key_type);
892 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
893 bits = 0;
894 type_bits_valid(type, &bits);
895 private = key_generate(type, bits);
896 if (private == NULL) {
897 fprintf(stderr, "key_generate failed\n");
898 first = 0;
899 continue;
900 }
901 public = key_from_private(private);
902 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
903 hostname);
904 if (!key_save_private(private, identity_file, "", comment)) {
905 printf("Saving the key failed: %s.\n", identity_file);
906 key_free(private);
907 key_free(public);
908 first = 0;
909 continue;
910 }
911 key_free(private);
912 arc4random_stir();
913 strlcat(identity_file, ".pub", sizeof(identity_file));
914 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
915 if (fd == -1) {
916 printf("Could not save your public key in %s\n",
917 identity_file);
918 key_free(public);
919 first = 0;
920 continue;
921 }
922 f = fdopen(fd, "w");
923 if (f == NULL) {
924 printf("fdopen %s failed\n", identity_file);
925 key_free(public);
926 first = 0;
927 continue;
928 }
929 if (!key_write(public, f)) {
930 fprintf(stderr, "write key failed\n");
931 key_free(public);
932 first = 0;
933 continue;
934 }
935 fprintf(f, " %s\n", comment);
936 fclose(f);
937 key_free(public);
938
939 }
940 if (first != 0)
941 printf("\n");
942}
943
944static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100945printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100946{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000947 if (print_fingerprint) {
948 enum fp_rep rep;
949 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000950 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000951
952 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
953 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
954 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100955 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000956 printf("%u %s %s (%s)\n", key_size(public), fp, name,
957 key_type(public));
958 if (log_level >= SYSLOG_LEVEL_VERBOSE)
959 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000960 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000961 xfree(fp);
962 } else {
963 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
964 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100965 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000966 if (!key_write(public, f))
967 fatal("key_write failed");
968 fprintf(f, "\n");
969 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100970}
971
972static void
973do_known_hosts(struct passwd *pw, const char *name)
974{
975 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100976 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100977 char *cp, *cp2, *kp, *kp2;
978 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100979 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100980 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100981
982 if (!have_identity) {
983 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
984 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
985 sizeof(identity_file))
986 fatal("Specified known hosts path too long");
987 xfree(cp);
988 have_identity = 1;
989 }
990 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +1000991 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +1100992
993 /*
994 * Find hosts goes to stdout, hash and deletions happen in-place
995 * A corner case is ssh-keygen -HF foo, which should go to stdout
996 */
997 if (!find_host && (hash_hosts || delete_host)) {
998 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
999 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1000 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1001 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1002 fatal("known_hosts path too long");
1003 umask(077);
1004 if ((c = mkstemp(tmp)) == -1)
1005 fatal("mkstemp: %s", strerror(errno));
1006 if ((out = fdopen(c, "w")) == NULL) {
1007 c = errno;
1008 unlink(tmp);
1009 fatal("fdopen: %s", strerror(c));
1010 }
1011 inplace = 1;
1012 }
1013
1014 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001015 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001016 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001017 skip = 1;
1018 invalid = 1;
1019 continue;
1020 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001021 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001022 if (skip) {
1023 skip = 0;
1024 continue;
1025 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001026 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001027
1028 /* Skip leading whitespace, empty and comment lines. */
1029 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1030 ;
1031 if (!*cp || *cp == '\n' || *cp == '#') {
1032 if (inplace)
1033 fprintf(out, "%s\n", cp);
1034 continue;
1035 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001036 /* Check whether this is a CA key */
1037 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1038 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1039 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1040 ca = 1;
1041 cp += sizeof(CA_MARKER);
1042 } else
1043 ca = 0;
1044
Damien Miller4b42d7f2005-03-01 21:48:35 +11001045 /* Find the end of the host name portion. */
1046 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1047 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001048
Damien Miller4b42d7f2005-03-01 21:48:35 +11001049 if (*kp == '\0' || *(kp + 1) == '\0') {
1050 error("line %d missing key: %.40s...",
1051 num, line);
1052 invalid = 1;
1053 continue;
1054 }
1055 *kp++ = '\0';
1056 kp2 = kp;
1057
Damien Miller0a80ca12010-02-27 07:55:05 +11001058 pub = key_new(KEY_RSA1);
1059 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001060 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001061 key_free(pub);
1062 pub = key_new(KEY_UNSPEC);
1063 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001064 error("line %d invalid key: %.40s...",
1065 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001066 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001067 invalid = 1;
1068 continue;
1069 }
1070 }
1071
1072 if (*cp == HASH_DELIM) {
1073 if (find_host || delete_host) {
1074 cp2 = host_hash(name, cp, strlen(cp));
1075 if (cp2 == NULL) {
1076 error("line %d: invalid hashed "
1077 "name: %.64s...", num, line);
1078 invalid = 1;
1079 continue;
1080 }
1081 c = (strcmp(cp2, cp) == 0);
1082 if (find_host && c) {
1083 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001084 "line %d type %s%s\n", name,
1085 num, key_type(pub),
1086 ca ? " (CA key)" : "");
1087 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001088 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001089 if (delete_host && !c && !ca)
1090 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001091 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +11001092 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001093 } else {
1094 if (find_host || delete_host) {
1095 c = (match_hostname(name, cp,
1096 strlen(cp)) == 1);
1097 if (find_host && c) {
1098 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001099 "line %d type %s%s\n", name,
1100 num, key_type(pub),
1101 ca ? " (CA key)" : "");
1102 printhost(out, name, pub,
1103 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001104 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001105 if (delete_host && !c && !ca)
1106 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001107 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +11001108 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001109 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001110 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001111 if (ca) {
1112 fprintf(stderr, "Warning: "
1113 "ignoring CA key for host: "
1114 "%.64s\n", cp2);
1115 printhost(out, cp2, pub, ca, 0);
1116 } else if (strcspn(cp2, "*?!") !=
1117 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001118 fprintf(stderr, "Warning: "
1119 "ignoring host name with "
1120 "metacharacters: %.64s\n",
1121 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +11001122 printhost(out, cp2, pub, ca, 0);
1123 } else
1124 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001125 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001126 has_unhashed = 1;
1127 }
1128 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001129 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001130 }
1131 fclose(in);
1132
1133 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001134 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001135 identity_file);
1136 if (inplace) {
1137 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001138 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001139 fclose(out);
1140 unlink(tmp);
1141 }
1142 exit(1);
1143 }
1144
1145 if (inplace) {
1146 fclose(out);
1147
1148 /* Backup existing file */
1149 if (unlink(old) == -1 && errno != ENOENT)
1150 fatal("unlink %.100s: %s", old, strerror(errno));
1151 if (link(identity_file, old) == -1)
1152 fatal("link %.100s to %.100s: %s", identity_file, old,
1153 strerror(errno));
1154 /* Move new one into place */
1155 if (rename(tmp, identity_file) == -1) {
1156 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1157 strerror(errno));
1158 unlink(tmp);
1159 unlink(old);
1160 exit(1);
1161 }
1162
1163 fprintf(stderr, "%s updated.\n", identity_file);
1164 fprintf(stderr, "Original contents retained as %s\n", old);
1165 if (has_unhashed) {
1166 fprintf(stderr, "WARNING: %s contains unhashed "
1167 "entries\n", old);
1168 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001169 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001170 }
1171 }
1172
1173 exit(0);
1174}
1175
Damien Miller95def091999-11-25 00:26:21 +11001176/*
1177 * Perform changing a passphrase. The argument is the passwd structure
1178 * for the current user.
1179 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001180static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001181do_change_passphrase(struct passwd *pw)
1182{
Damien Miller95def091999-11-25 00:26:21 +11001183 char *comment;
1184 char *old_passphrase, *passphrase1, *passphrase2;
1185 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001186 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001187
Damien Miller95def091999-11-25 00:26:21 +11001188 if (!have_identity)
1189 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001190 if (stat(identity_file, &st) < 0) {
1191 perror(identity_file);
1192 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001193 }
Damien Miller95def091999-11-25 00:26:21 +11001194 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001195 private = key_load_private(identity_file, "", &comment);
1196 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001197 if (identity_passphrase)
1198 old_passphrase = xstrdup(identity_passphrase);
1199 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001200 old_passphrase =
1201 read_passphrase("Enter old passphrase: ",
1202 RP_ALLOW_STDIN);
1203 private = key_load_private(identity_file, old_passphrase,
1204 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001205 memset(old_passphrase, 0, strlen(old_passphrase));
1206 xfree(old_passphrase);
1207 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001208 printf("Bad passphrase.\n");
1209 exit(1);
1210 }
Damien Miller95def091999-11-25 00:26:21 +11001211 }
1212 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001213
Damien Miller95def091999-11-25 00:26:21 +11001214 /* Ask the new passphrase (twice). */
1215 if (identity_new_passphrase) {
1216 passphrase1 = xstrdup(identity_new_passphrase);
1217 passphrase2 = NULL;
1218 } else {
1219 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001220 read_passphrase("Enter new passphrase (empty for no "
1221 "passphrase): ", RP_ALLOW_STDIN);
1222 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001223 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001224
1225 /* Verify that they are the same. */
1226 if (strcmp(passphrase1, passphrase2) != 0) {
1227 memset(passphrase1, 0, strlen(passphrase1));
1228 memset(passphrase2, 0, strlen(passphrase2));
1229 xfree(passphrase1);
1230 xfree(passphrase2);
1231 printf("Pass phrases do not match. Try again.\n");
1232 exit(1);
1233 }
1234 /* Destroy the other copy. */
1235 memset(passphrase2, 0, strlen(passphrase2));
1236 xfree(passphrase2);
1237 }
1238
1239 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001240 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001241 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001242 memset(passphrase1, 0, strlen(passphrase1));
1243 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001244 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001245 xfree(comment);
1246 exit(1);
1247 }
1248 /* Destroy the passphrase and the copy of the key in memory. */
1249 memset(passphrase1, 0, strlen(passphrase1));
1250 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001251 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +11001252 xfree(comment);
1253
1254 printf("Your identification has been saved with the new passphrase.\n");
1255 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001256}
1257
Damien Miller37876e92003-05-15 10:19:46 +10001258/*
1259 * Print the SSHFP RR.
1260 */
Damien Millercb314822006-03-26 13:48:01 +11001261static int
1262do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001263{
1264 Key *public;
1265 char *comment = NULL;
1266 struct stat st;
1267
Damien Millercb314822006-03-26 13:48:01 +11001268 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +10001269 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +11001270 if (stat(fname, &st) < 0) {
1271 if (errno == ENOENT)
1272 return 0;
1273 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001274 exit(1);
1275 }
Damien Millercb314822006-03-26 13:48:01 +11001276 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001277 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001278 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001279 key_free(public);
1280 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +11001281 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001282 }
1283 if (comment)
1284 xfree(comment);
1285
Damien Millercb314822006-03-26 13:48:01 +11001286 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001287 exit(1);
1288}
Damien Miller37876e92003-05-15 10:19:46 +10001289
Damien Miller95def091999-11-25 00:26:21 +11001290/*
1291 * Change the comment of a private key file.
1292 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001293static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001294do_change_comment(struct passwd *pw)
1295{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001296 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001297 Key *private;
1298 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001299 struct stat st;
1300 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001301 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001302
Damien Miller95def091999-11-25 00:26:21 +11001303 if (!have_identity)
1304 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001305 if (stat(identity_file, &st) < 0) {
1306 perror(identity_file);
1307 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001308 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001309 private = key_load_private(identity_file, "", &comment);
1310 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001311 if (identity_passphrase)
1312 passphrase = xstrdup(identity_passphrase);
1313 else if (identity_new_passphrase)
1314 passphrase = xstrdup(identity_new_passphrase);
1315 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001316 passphrase = read_passphrase("Enter passphrase: ",
1317 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001318 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001319 private = key_load_private(identity_file, passphrase, &comment);
1320 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001321 memset(passphrase, 0, strlen(passphrase));
1322 xfree(passphrase);
1323 printf("Bad passphrase.\n");
1324 exit(1);
1325 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001326 } else {
1327 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001328 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001329 if (private->type != KEY_RSA1) {
1330 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1331 key_free(private);
1332 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001333 }
Damien Miller95def091999-11-25 00:26:21 +11001334 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001335
Damien Miller95def091999-11-25 00:26:21 +11001336 if (identity_comment) {
1337 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1338 } else {
1339 printf("Enter new comment: ");
1340 fflush(stdout);
1341 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1342 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001343 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001344 exit(1);
1345 }
Damien Miller14b017d2007-09-17 16:09:15 +10001346 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001347 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001348
Damien Miller95def091999-11-25 00:26:21 +11001349 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001350 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001351 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001352 memset(passphrase, 0, strlen(passphrase));
1353 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001354 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001355 xfree(comment);
1356 exit(1);
1357 }
Damien Miller95def091999-11-25 00:26:21 +11001358 memset(passphrase, 0, strlen(passphrase));
1359 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001360 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001361 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001362
Damien Miller95def091999-11-25 00:26:21 +11001363 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001364 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1365 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001366 printf("Could not save your public key in %s\n", identity_file);
1367 exit(1);
1368 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001369 f = fdopen(fd, "w");
1370 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001371 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001372 exit(1);
1373 }
Damien Millereba71ba2000-04-29 23:57:08 +10001374 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001375 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001376 key_free(public);
1377 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001378 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001379
Damien Miller95def091999-11-25 00:26:21 +11001380 xfree(comment);
1381
1382 printf("The comment in your key file has been changed.\n");
1383 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001384}
1385
Damien Miller0a80ca12010-02-27 07:55:05 +11001386static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001387fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001388{
1389 char from[32], to[32];
1390 static char ret[64];
1391 time_t tt;
1392 struct tm *tm;
1393
1394 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001395 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001396 return "forever";
1397
Damien Millerf2b70ca2010-03-05 07:39:35 +11001398 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001399 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001400 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001401 tm = localtime(&tt);
1402 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1403 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001404 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001405 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001406 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001407 tm = localtime(&tt);
1408 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1409 }
1410
Damien Millerf2b70ca2010-03-05 07:39:35 +11001411 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001412 snprintf(ret, sizeof(ret), "before %s", to);
1413 return ret;
1414 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001415 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001416 snprintf(ret, sizeof(ret), "after %s", from);
1417 return ret;
1418 }
1419
1420 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1421 return ret;
1422}
1423
1424static void
Damien Miller4e270b02010-04-16 15:56:21 +10001425add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001426{
1427 debug3("%s: %s", __func__, name);
1428 buffer_put_cstring(c, name);
1429 buffer_put_string(c, NULL, 0);
1430}
1431
1432static void
Damien Miller4e270b02010-04-16 15:56:21 +10001433add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001434{
1435 Buffer b;
1436
1437 debug3("%s: %s=%s", __func__, name, value);
1438 buffer_init(&b);
1439 buffer_put_cstring(&b, value);
1440
1441 buffer_put_cstring(c, name);
1442 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1443
1444 buffer_free(&b);
1445}
1446
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001447#define OPTIONS_CRITICAL 1
1448#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001449static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001450prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001451{
Damien Miller0a80ca12010-02-27 07:55:05 +11001452 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001453 if ((which & OPTIONS_CRITICAL) != 0 &&
1454 certflags_command != NULL)
1455 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001456 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001457 (certflags_flags & CERTOPT_X_FWD) != 0)
1458 add_flag_option(c, "permit-X11-forwarding");
1459 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001460 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001461 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001462 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1463 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001464 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001465 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1466 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001467 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001468 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1469 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001470 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001471 if ((which & OPTIONS_CRITICAL) != 0 &&
1472 certflags_src_addr != NULL)
1473 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001474}
1475
Damien Miller757f34e2010-08-05 13:05:31 +10001476static Key *
1477load_pkcs11_key(char *path)
1478{
1479#ifdef ENABLE_PKCS11
1480 Key **keys = NULL, *public, *private = NULL;
1481 int i, nkeys;
1482
1483 if ((public = key_load_public(path, NULL)) == NULL)
1484 fatal("Couldn't load CA public key \"%s\"", path);
1485
1486 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1487 debug3("%s: %d keys", __func__, nkeys);
1488 if (nkeys <= 0)
1489 fatal("cannot read public key from pkcs11");
1490 for (i = 0; i < nkeys; i++) {
1491 if (key_equal_public(public, keys[i])) {
1492 private = keys[i];
1493 continue;
1494 }
1495 key_free(keys[i]);
1496 }
1497 xfree(keys);
1498 key_free(public);
1499 return private;
1500#else
1501 fatal("no pkcs11 support");
1502#endif /* ENABLE_PKCS11 */
1503}
1504
Damien Miller0a80ca12010-02-27 07:55:05 +11001505static void
1506do_ca_sign(struct passwd *pw, int argc, char **argv)
1507{
1508 int i, fd;
1509 u_int n;
1510 Key *ca, *public;
1511 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1512 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001513 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001514
Damien Miller4e270b02010-04-16 15:56:21 +10001515 if (key_type_name != NULL) {
1516 switch (key_type_from_name(key_type_name)) {
1517 case KEY_RSA_CERT_V00:
1518 case KEY_DSA_CERT_V00:
1519 v00 = 1;
1520 break;
1521 case KEY_UNSPEC:
1522 if (strcasecmp(key_type_name, "v00") == 0) {
1523 v00 = 1;
1524 break;
1525 } else if (strcasecmp(key_type_name, "v01") == 0)
1526 break;
1527 /* FALLTHROUGH */
1528 default:
1529 fprintf(stderr, "unknown key type %s\n", key_type_name);
1530 exit(1);
1531 }
1532 }
1533
Damien Miller757f34e2010-08-05 13:05:31 +10001534 pkcs11_init(1);
1535 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1536 if (pkcs11provider != NULL) {
1537 if ((ca = load_pkcs11_key(tmp)) == NULL)
1538 fatal("No PKCS#11 key matching %s found", ca_key_path);
1539 } else if ((ca = load_identity(tmp)) == NULL)
1540 fatal("Couldn't load CA key \"%s\"", tmp);
1541 xfree(tmp);
1542
Damien Miller0a80ca12010-02-27 07:55:05 +11001543 for (i = 0; i < argc; i++) {
1544 /* Split list of principals */
1545 n = 0;
1546 if (cert_principals != NULL) {
1547 otmp = tmp = xstrdup(cert_principals);
1548 plist = NULL;
1549 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1550 plist = xrealloc(plist, n + 1, sizeof(*plist));
1551 if (*(plist[n] = xstrdup(cp)) == '\0')
1552 fatal("Empty principal name");
1553 }
1554 xfree(otmp);
1555 }
1556
1557 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1558 if ((public = key_load_public(tmp, &comment)) == NULL)
1559 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001560 if (public->type != KEY_RSA && public->type != KEY_DSA &&
1561 public->type != KEY_ECDSA)
Damien Miller0a80ca12010-02-27 07:55:05 +11001562 fatal("%s: key \"%s\" type %s cannot be certified",
1563 __func__, tmp, key_type(public));
1564
1565 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001566 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001567 fatal("Could not upgrade key %s to certificate", tmp);
1568 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001569 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001570 public->cert->key_id = xstrdup(cert_key_id);
1571 public->cert->nprincipals = n;
1572 public->cert->principals = plist;
1573 public->cert->valid_after = cert_valid_from;
1574 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001575 if (v00) {
1576 prepare_options_buf(&public->cert->critical,
1577 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1578 } else {
1579 prepare_options_buf(&public->cert->critical,
1580 OPTIONS_CRITICAL);
1581 prepare_options_buf(&public->cert->extensions,
1582 OPTIONS_EXTENSIONS);
1583 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001584 public->cert->signature_key = key_from_private(ca);
1585
1586 if (key_certify(public, ca) != 0)
1587 fatal("Couldn't not certify key %s", tmp);
1588
1589 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1590 *cp = '\0';
1591 xasprintf(&out, "%s-cert.pub", tmp);
1592 xfree(tmp);
1593
1594 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1595 fatal("Could not open \"%s\" for writing: %s", out,
1596 strerror(errno));
1597 if ((f = fdopen(fd, "w")) == NULL)
1598 fatal("%s: fdopen: %s", __func__, strerror(errno));
1599 if (!key_write(public, f))
1600 fatal("Could not write certified key to %s", out);
1601 fprintf(f, " %s\n", comment);
1602 fclose(f);
1603
Damien Miller4e270b02010-04-16 15:56:21 +10001604 if (!quiet) {
1605 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1606 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001607 out, public->cert->key_id,
1608 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001609 cert_principals != NULL ? " for " : "",
1610 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001611 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001612 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001613
1614 key_free(public);
1615 xfree(out);
1616 }
Damien Miller757f34e2010-08-05 13:05:31 +10001617 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001618 exit(0);
1619}
1620
1621static u_int64_t
1622parse_relative_time(const char *s, time_t now)
1623{
1624 int64_t mul, secs;
1625
1626 mul = *s == '-' ? -1 : 1;
1627
1628 if ((secs = convtime(s + 1)) == -1)
1629 fatal("Invalid relative certificate time %s", s);
1630 if (mul == -1 && secs > now)
1631 fatal("Certificate time %s cannot be represented", s);
1632 return now + (u_int64_t)(secs * mul);
1633}
1634
1635static u_int64_t
1636parse_absolute_time(const char *s)
1637{
1638 struct tm tm;
1639 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001640 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001641
Damien Miller2ca342b2010-03-03 12:14:15 +11001642 /*
1643 * POSIX strptime says "The application shall ensure that there
1644 * is white-space or other non-alphanumeric characters between
1645 * any two conversion specifications" so arrange things this way.
1646 */
1647 switch (strlen(s)) {
1648 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001649 fmt = "%Y-%m-%d";
1650 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001651 break;
1652 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001653 fmt = "%Y-%m-%dT%H:%M:%S";
1654 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001655 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1656 break;
1657 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001658 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001659 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001660
1661 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001662 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001663 fatal("Invalid certificate time %s", s);
1664 if ((tt = mktime(&tm)) < 0)
1665 fatal("Certificate time %s cannot be represented", s);
1666 return (u_int64_t)tt;
1667}
1668
1669static void
1670parse_cert_times(char *timespec)
1671{
1672 char *from, *to;
1673 time_t now = time(NULL);
1674 int64_t secs;
1675
1676 /* +timespec relative to now */
1677 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1678 if ((secs = convtime(timespec + 1)) == -1)
1679 fatal("Invalid relative certificate life %s", timespec);
1680 cert_valid_to = now + secs;
1681 /*
1682 * Backdate certificate one minute to avoid problems on hosts
1683 * with poorly-synchronised clocks.
1684 */
1685 cert_valid_from = ((now - 59)/ 60) * 60;
1686 return;
1687 }
1688
1689 /*
1690 * from:to, where
1691 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1692 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1693 */
1694 from = xstrdup(timespec);
1695 to = strchr(from, ':');
1696 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001697 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001698 *to++ = '\0';
1699
1700 if (*from == '-' || *from == '+')
1701 cert_valid_from = parse_relative_time(from, now);
1702 else
1703 cert_valid_from = parse_absolute_time(from);
1704
1705 if (*to == '-' || *to == '+')
1706 cert_valid_to = parse_relative_time(to, cert_valid_from);
1707 else
1708 cert_valid_to = parse_absolute_time(to);
1709
1710 if (cert_valid_to <= cert_valid_from)
1711 fatal("Empty certificate validity interval");
1712 xfree(from);
1713}
1714
1715static void
Damien Miller4e270b02010-04-16 15:56:21 +10001716add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001717{
1718 char *val;
1719
Damien Miller044f4a62011-05-05 14:14:08 +10001720 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001721 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001722 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001723 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001724 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001725 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001726 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001727 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001728 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001729 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001730 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001731 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001732 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001733 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001734 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001735 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001736 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001737 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001738 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001739 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001740 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001741 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001742 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1743 val = opt + 14;
1744 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001745 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001746 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001747 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001748 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001749 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1750 val = opt + 15;
1751 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001752 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001753 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001754 fatal("source-address already specified");
1755 if (addr_match_cidr_list(NULL, val) != 0)
1756 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001757 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001758 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001759 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001760}
1761
Ben Lindstrombba81212001-06-25 05:01:22 +00001762static void
Damien Millerd834d352010-06-26 09:48:02 +10001763show_options(const Buffer *optbuf, int v00, int in_critical)
1764{
1765 u_char *name, *data;
1766 u_int dlen;
1767 Buffer options, option;
1768
1769 buffer_init(&options);
1770 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1771
1772 buffer_init(&option);
1773 while (buffer_len(&options) != 0) {
1774 name = buffer_get_string(&options, NULL);
1775 data = buffer_get_string_ptr(&options, &dlen);
1776 buffer_append(&option, data, dlen);
1777 printf(" %s", name);
1778 if ((v00 || !in_critical) &&
1779 (strcmp(name, "permit-X11-forwarding") == 0 ||
1780 strcmp(name, "permit-agent-forwarding") == 0 ||
1781 strcmp(name, "permit-port-forwarding") == 0 ||
1782 strcmp(name, "permit-pty") == 0 ||
1783 strcmp(name, "permit-user-rc") == 0))
1784 printf("\n");
1785 else if ((v00 || in_critical) &&
1786 (strcmp(name, "force-command") == 0 ||
1787 strcmp(name, "source-address") == 0)) {
1788 data = buffer_get_string(&option, NULL);
1789 printf(" %s\n", data);
1790 xfree(data);
1791 } else {
1792 printf(" UNKNOWN OPTION (len %u)\n",
1793 buffer_len(&option));
1794 buffer_clear(&option);
1795 }
1796 xfree(name);
1797 if (buffer_len(&option) != 0)
1798 fatal("Option corrupt: extra data at end");
1799 }
1800 buffer_free(&option);
1801 buffer_free(&options);
1802}
1803
1804static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001805do_show_cert(struct passwd *pw)
1806{
1807 Key *key;
1808 struct stat st;
1809 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001810 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001811
1812 if (!have_identity)
1813 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001814 if (stat(identity_file, &st) < 0)
1815 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001816 if ((key = key_load_public(identity_file, NULL)) == NULL)
1817 fatal("%s is not a public key", identity_file);
1818 if (!key_is_cert(key))
1819 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001820 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1821
Damien Millerf2b70ca2010-03-05 07:39:35 +11001822 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1823 ca_fp = key_fingerprint(key->cert->signature_key,
1824 SSH_FP_MD5, SSH_FP_HEX);
1825
1826 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001827 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1828 key_cert_type(key));
1829 printf(" Public key: %s %s\n", key_type(key), key_fp);
1830 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001831 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001832 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001833 if (!v00) {
1834 printf(" Serial: %llu\n",
1835 (unsigned long long)key->cert->serial);
1836 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001837 printf(" Valid: %s\n",
1838 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1839 printf(" Principals: ");
1840 if (key->cert->nprincipals == 0)
1841 printf("(none)\n");
1842 else {
1843 for (i = 0; i < key->cert->nprincipals; i++)
1844 printf("\n %s",
1845 key->cert->principals[i]);
1846 printf("\n");
1847 }
Damien Miller4e270b02010-04-16 15:56:21 +10001848 printf(" Critical Options: ");
1849 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001850 printf("(none)\n");
1851 else {
1852 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001853 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001854 }
Damien Miller4e270b02010-04-16 15:56:21 +10001855 if (!v00) {
1856 printf(" Extensions: ");
1857 if (buffer_len(&key->cert->extensions) == 0)
1858 printf("(none)\n");
1859 else {
1860 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001861 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001862 }
1863 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001864 exit(0);
1865}
1866
1867static void
Damien Miller431f66b1999-11-21 18:31:57 +11001868usage(void)
1869{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001870 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001871 fprintf(stderr, "Options:\n");
Damien Miller58f1baf2011-05-05 14:06:15 +10001872 fprintf(stderr, " -A Generate non-existent host keys for all key types.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001873 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001874 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001875 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001876 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001877 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001878#ifdef ENABLE_PKCS11
1879 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1880#endif
Damien Miller44b25042010-07-02 13:35:01 +10001881 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001882 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1883 fprintf(stderr, " -f filename Filename of the key file.\n");
1884 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1885 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1886 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001887 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1888 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10001889 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001890 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001891 fprintf(stderr, " -l Show fingerprint of key file.\n");
1892 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001893 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001894 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001895 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10001896 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001897 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1898 fprintf(stderr, " -p Change passphrase of private key file.\n");
1899 fprintf(stderr, " -q Quiet.\n");
1900 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1901 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1902 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001903 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001904 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1905 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001906 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001907 fprintf(stderr, " -v Verbose.\n");
1908 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1909 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001910 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001911
Damien Miller95def091999-11-25 00:26:21 +11001912 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001913}
1914
Damien Miller95def091999-11-25 00:26:21 +11001915/*
1916 * Main program for key management.
1917 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001918int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001919main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920{
Damien Millera41c8b12002-01-22 23:05:08 +11001921 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller757f34e2010-08-05 13:05:31 +10001922 char out_file[MAXPATHLEN], *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001923 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001924 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001925 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001926 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001927 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001928 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Miller58f1baf2011-05-05 14:06:15 +10001929 int gen_all_hostkeys = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10001930 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001931 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001932 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001933
Damien Miller95def091999-11-25 00:26:21 +11001934 extern int optind;
1935 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001936
Darren Tuckerce321d82005-10-03 18:11:24 +10001937 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1938 sanitise_stdfd();
1939
Darren Tucker9ac56e92007-01-14 10:19:59 +11001940 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001941
Damien Miller4314c2b2010-09-10 11:12:09 +10001942 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001943 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001944
Kevin Steves3a881912002-07-20 19:05:40 +00001945 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001946
Damien Miller5428f641999-11-25 11:54:57 +11001947 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001948 pw = getpwuid(getuid());
1949 if (!pw) {
1950 printf("You don't exist, go away!\n");
1951 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001952 }
Damien Millereba71ba2000-04-29 23:57:08 +10001953 if (gethostname(hostname, sizeof(hostname)) < 0) {
1954 perror("gethostname");
1955 exit(1);
1956 }
Damien Miller5428f641999-11-25 11:54:57 +11001957
Damien Miller11143192011-05-05 14:13:25 +10001958 while ((opt = getopt(argc, argv, "AegiqpclBHLhvxXyF:b:f:t:D:I:P:m:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001959 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001960 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10001961 case 'A':
1962 gen_all_hostkeys = 1;
1963 break;
Damien Miller95def091999-11-25 00:26:21 +11001964 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10001965 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001966 if (errstr)
1967 fatal("Bits has bad value %s (%s)",
1968 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001969 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001970 case 'F':
1971 find_host = 1;
1972 rr_hostname = optarg;
1973 break;
1974 case 'H':
1975 hash_hosts = 1;
1976 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001977 case 'I':
1978 cert_key_id = optarg;
1979 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001980 case 'R':
1981 delete_host = 1;
1982 rr_hostname = optarg;
1983 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001984 case 'L':
1985 show_cert = 1;
1986 break;
Damien Miller95def091999-11-25 00:26:21 +11001987 case 'l':
1988 print_fingerprint = 1;
1989 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001990 case 'B':
1991 print_bubblebabble = 1;
1992 break;
Damien Miller44b25042010-07-02 13:35:01 +10001993 case 'm':
1994 if (strcasecmp(optarg, "RFC4716") == 0 ||
1995 strcasecmp(optarg, "ssh2") == 0) {
1996 convert_format = FMT_RFC4716;
1997 break;
1998 }
1999 if (strcasecmp(optarg, "PKCS8") == 0) {
2000 convert_format = FMT_PKCS8;
2001 break;
2002 }
2003 if (strcasecmp(optarg, "PEM") == 0) {
2004 convert_format = FMT_PEM;
2005 break;
2006 }
2007 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002008 case 'n':
2009 cert_principals = optarg;
2010 break;
Damien Miller95def091999-11-25 00:26:21 +11002011 case 'p':
2012 change_passphrase = 1;
2013 break;
Damien Miller95def091999-11-25 00:26:21 +11002014 case 'c':
2015 change_comment = 1;
2016 break;
Damien Miller95def091999-11-25 00:26:21 +11002017 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002018 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2019 sizeof(identity_file))
2020 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002021 have_identity = 1;
2022 break;
Damien Miller37876e92003-05-15 10:19:46 +10002023 case 'g':
2024 print_generic = 1;
2025 break;
Damien Miller95def091999-11-25 00:26:21 +11002026 case 'P':
2027 identity_passphrase = optarg;
2028 break;
Damien Miller95def091999-11-25 00:26:21 +11002029 case 'N':
2030 identity_new_passphrase = optarg;
2031 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002032 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002033 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002034 break;
Damien Miller95def091999-11-25 00:26:21 +11002035 case 'C':
2036 identity_comment = optarg;
2037 break;
Damien Miller95def091999-11-25 00:26:21 +11002038 case 'q':
2039 quiet = 1;
2040 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002041 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002042 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002043 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002044 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002045 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002046 case 'h':
2047 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002048 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002049 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002050 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002051 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002052 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002053 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002054 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002055 case 'y':
2056 print_public = 1;
2057 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002058 case 's':
2059 ca_key_path = optarg;
2060 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002061 case 't':
2062 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002063 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002064 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002065 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002066 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002067 case 'v':
2068 if (log_level == SYSLOG_LEVEL_INFO)
2069 log_level = SYSLOG_LEVEL_DEBUG1;
2070 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002071 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002072 log_level < SYSLOG_LEVEL_DEBUG3)
2073 log_level++;
2074 }
2075 break;
Damien Miller37876e92003-05-15 10:19:46 +10002076 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002077 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002078 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002079 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002080 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2081 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002082 if (errstr)
2083 fatal("Desired generator has bad value: %s (%s)",
2084 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002085 break;
2086 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11002087 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002088 if (errstr)
2089 fatal("Invalid number of trials: %s (%s)",
2090 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002091 break;
2092 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002093 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002094 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002095 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002096 break;
2097 case 'G':
2098 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002099 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2100 sizeof(out_file))
2101 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002102 break;
2103 case 'T':
2104 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002105 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2106 sizeof(out_file))
2107 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002108 break;
2109 case 'S':
2110 /* XXX - also compare length against bits */
2111 if (BN_hex2bn(&start, optarg) == 0)
2112 fatal("Invalid start point.");
2113 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002114 case 'V':
2115 parse_cert_times(optarg);
2116 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002117 case 'z':
2118 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
2119 if (errstr)
2120 fatal("Invalid serial number: %s", errstr);
2121 break;
Damien Miller95def091999-11-25 00:26:21 +11002122 case '?':
2123 default:
2124 usage();
2125 }
2126 }
Darren Tucker06930c72003-12-31 11:34:51 +11002127
2128 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002129 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002130
Damien Miller0a80ca12010-02-27 07:55:05 +11002131 argv += optind;
2132 argc -= optind;
2133
2134 if (ca_key_path != NULL) {
2135 if (argc < 1) {
2136 printf("Too few arguments.\n");
2137 usage();
2138 }
2139 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11002140 printf("Too many arguments.\n");
2141 usage();
2142 }
2143 if (change_passphrase && change_comment) {
2144 printf("Can only have one of -p and -c.\n");
2145 usage();
2146 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002147 if (print_fingerprint && (delete_host || hash_hosts)) {
2148 printf("Cannot use -l with -D or -R.\n");
2149 usage();
2150 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002151 if (ca_key_path != NULL) {
2152 if (cert_key_id == NULL)
2153 fatal("Must specify key id (-I) when certifying");
2154 do_ca_sign(pw, argc, argv);
2155 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002156 if (show_cert)
2157 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002158 if (delete_host || hash_hosts || find_host)
2159 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002160 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002161 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002162 if (change_passphrase)
2163 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002164 if (change_comment)
2165 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002166 if (convert_to)
2167 do_convert_to(pw);
2168 if (convert_from)
2169 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002170 if (print_public)
2171 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002172 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002173 unsigned int n = 0;
2174
2175 if (have_identity) {
2176 n = do_print_resource_record(pw,
2177 identity_file, rr_hostname);
2178 if (n == 0) {
2179 perror(identity_file);
2180 exit(1);
2181 }
2182 exit(0);
2183 } else {
2184
2185 n += do_print_resource_record(pw,
2186 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2187 n += do_print_resource_record(pw,
2188 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
2189
2190 if (n == 0)
2191 fatal("no keys found.");
2192 exit(0);
2193 }
Damien Miller37876e92003-05-15 10:19:46 +10002194 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002195 if (pkcs11provider != NULL)
Damien Miller757f34e2010-08-05 13:05:31 +10002196 do_download(pw);
Damien Miller95def091999-11-25 00:26:21 +11002197
Darren Tucker019cefe2003-08-02 22:40:07 +10002198 if (do_gen_candidates) {
2199 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002200
Darren Tucker019cefe2003-08-02 22:40:07 +10002201 if (out == NULL) {
2202 error("Couldn't open modulus candidate file \"%s\": %s",
2203 out_file, strerror(errno));
2204 return (1);
2205 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002206 if (bits == 0)
2207 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002208 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002209 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002210
2211 return (0);
2212 }
2213
2214 if (do_screen_candidates) {
2215 FILE *in;
2216 FILE *out = fopen(out_file, "w");
2217
2218 if (have_identity && strcmp(identity_file, "-") != 0) {
2219 if ((in = fopen(identity_file, "r")) == NULL) {
2220 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002221 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002222 strerror(errno));
2223 }
2224 } else
2225 in = stdin;
2226
2227 if (out == NULL) {
2228 fatal("Couldn't open moduli file \"%s\": %s",
2229 out_file, strerror(errno));
2230 }
2231 if (prime_test(in, out, trials, generator_wanted) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002232 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002233 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002234 }
2235
Damien Miller58f1baf2011-05-05 14:06:15 +10002236 if (gen_all_hostkeys) {
2237 do_gen_all_hostkeys(pw);
2238 return (0);
2239 }
2240
Damien Miller95def091999-11-25 00:26:21 +11002241 arc4random_stir();
2242
Damien Millerf14be5c2005-11-05 15:15:49 +11002243 if (key_type_name == NULL)
2244 key_type_name = "rsa";
2245
Damien Millere39cacc2000-11-29 12:18:44 +11002246 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002247 type_bits_valid(type, &bits);
2248
Darren Tucker3af2ac52005-11-29 13:10:24 +11002249 if (!quiet)
2250 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002251 private = key_generate(type, bits);
2252 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002253 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002254 exit(1);
2255 }
2256 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002257
2258 if (!have_identity)
2259 ask_filename(pw, "Enter file in which to save the key");
2260
Damien Miller788f2122005-11-05 15:14:59 +11002261 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002262 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2263 pw->pw_dir, _PATH_SSH_USER_DIR);
2264 if (strstr(identity_file, dotsshdir) != NULL) {
2265 if (stat(dotsshdir, &st) < 0) {
2266 if (errno != ENOENT) {
2267 error("Could not stat %s: %s", dotsshdir,
2268 strerror(errno));
2269 } else if (mkdir(dotsshdir, 0700) < 0) {
2270 error("Could not create directory '%s': %s",
2271 dotsshdir, strerror(errno));
2272 } else if (!quiet)
2273 printf("Created directory '%s'.\n", dotsshdir);
2274 }
Damien Miller95def091999-11-25 00:26:21 +11002275 }
2276 /* If the file already exists, ask the user to confirm. */
2277 if (stat(identity_file, &st) >= 0) {
2278 char yesno[3];
2279 printf("%s already exists.\n", identity_file);
2280 printf("Overwrite (y/n)? ");
2281 fflush(stdout);
2282 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2283 exit(1);
2284 if (yesno[0] != 'y' && yesno[0] != 'Y')
2285 exit(1);
2286 }
2287 /* Ask for a passphrase (twice). */
2288 if (identity_passphrase)
2289 passphrase1 = xstrdup(identity_passphrase);
2290 else if (identity_new_passphrase)
2291 passphrase1 = xstrdup(identity_new_passphrase);
2292 else {
2293passphrase_again:
2294 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002295 read_passphrase("Enter passphrase (empty for no "
2296 "passphrase): ", RP_ALLOW_STDIN);
2297 passphrase2 = read_passphrase("Enter same passphrase again: ",
2298 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002299 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002300 /*
2301 * The passphrases do not match. Clear them and
2302 * retry.
2303 */
Damien Miller95def091999-11-25 00:26:21 +11002304 memset(passphrase1, 0, strlen(passphrase1));
2305 memset(passphrase2, 0, strlen(passphrase2));
2306 xfree(passphrase1);
2307 xfree(passphrase2);
2308 printf("Passphrases do not match. Try again.\n");
2309 goto passphrase_again;
2310 }
2311 /* Clear the other copy of the passphrase. */
2312 memset(passphrase2, 0, strlen(passphrase2));
2313 xfree(passphrase2);
2314 }
2315
Damien Miller95def091999-11-25 00:26:21 +11002316 if (identity_comment) {
2317 strlcpy(comment, identity_comment, sizeof(comment));
2318 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002319 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002320 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2321 }
2322
2323 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00002324 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002325 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002326 memset(passphrase1, 0, strlen(passphrase1));
2327 xfree(passphrase1);
2328 exit(1);
2329 }
2330 /* Clear the passphrase. */
2331 memset(passphrase1, 0, strlen(passphrase1));
2332 xfree(passphrase1);
2333
2334 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002335 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002336 arc4random_stir();
2337
2338 if (!quiet)
2339 printf("Your identification has been saved in %s.\n", identity_file);
2340
Damien Miller95def091999-11-25 00:26:21 +11002341 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002342 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2343 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002344 printf("Could not save your public key in %s\n", identity_file);
2345 exit(1);
2346 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002347 f = fdopen(fd, "w");
2348 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002349 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002350 exit(1);
2351 }
Damien Millereba71ba2000-04-29 23:57:08 +10002352 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002353 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002354 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002355 fclose(f);
2356
2357 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002358 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002359 char *ra = key_fingerprint(public, SSH_FP_MD5,
2360 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002361 printf("Your public key has been saved in %s.\n",
2362 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002363 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002364 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002365 printf("The key's randomart image is:\n");
2366 printf("%s\n", ra);
2367 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002368 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002369 }
Damien Millereba71ba2000-04-29 23:57:08 +10002370
2371 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002372 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002373}