blob: 9407321d5ab97307041fa45cf31a1998d311da5d [file] [log] [blame]
Damien Miller3bde12a2012-06-20 21:51:11 +10001/* $OpenBSD: ssh-keygen.c,v 1.214 2012/05/23 03:28:28 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Identity and host key generation and maintenance.
Damien Millere4340be2000-09-16 13:29:08 +11007 *
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Miller95def091999-11-25 00:26:21 +110013 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100014
15#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110016
17#include <sys/types.h>
Damien Millere3b60b52006-07-10 21:08:03 +100018#include <sys/socket.h>
Damien Millerf17883e2006-03-15 11:45:54 +110019#include <sys/stat.h>
Damien Miller8dbffe72006-08-05 11:02:17 +100020#include <sys/param.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Damien Millereba71ba2000-04-29 23:57:08 +100022#include <openssl/evp.h>
23#include <openssl/pem.h>
Darren Tuckerbfaaf962008-02-28 19:13:52 +110024#include "openbsd-compat/openssl-compat.h"
Damien Millereba71ba2000-04-29 23:57:08 +100025
Darren Tucker39972492006-07-12 22:22:46 +100026#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100027#include <fcntl.h>
Damien Millerb8fe89c2006-07-24 14:51:00 +100028#include <netdb.h>
Darren Tucker2ee50c52006-07-11 18:55:05 +100029#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millerded319c2006-09-01 15:38:36 +100033#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100034#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100035#include <stdlib.h>
Damien Millere3476ed2006-07-24 14:13:33 +100036#include <string.h>
Damien Millere6b3b612006-07-24 14:01:23 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd4a8b7e1999-10-27 13:42:43 +100039#include "xmalloc.h"
Damien Millereba71ba2000-04-29 23:57:08 +100040#include "key.h"
Ben Lindstromd09fcf52001-03-29 00:29:54 +000041#include "rsa.h"
Damien Millereba71ba2000-04-29 23:57:08 +100042#include "authfile.h"
43#include "uuencode.h"
Damien Miller874d77b2000-10-14 16:23:11 +110044#include "buffer.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000045#include "pathnames.h"
46#include "log.h"
Darren Tuckere608ca22004-05-13 16:15:47 +100047#include "misc.h"
Damien Miller4b42d7f2005-03-01 21:48:35 +110048#include "match.h"
49#include "hostfile.h"
Damien Miller69996102006-07-10 20:53:31 +100050#include "dns.h"
Damien Miller0a80ca12010-02-27 07:55:05 +110051#include "ssh2.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110052#include "ssh-pkcs11.h"
Ben Lindstromcd392282001-07-04 03:44:03 +000053
Damien Miller3f54a9f2005-11-05 14:52:18 +110054/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
55#define DEFAULT_BITS 2048
56#define DEFAULT_BITS_DSA 1024
Damien Miller6e9f6802010-09-10 11:17:38 +100057#define DEFAULT_BITS_ECDSA 256
Damien Miller3f54a9f2005-11-05 14:52:18 +110058u_int32_t bits = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller5428f641999-11-25 11:54:57 +110060/*
61 * Flag indicating that we just want to change the passphrase. This can be
62 * set on the command line.
63 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064int change_passphrase = 0;
65
Damien Miller5428f641999-11-25 11:54:57 +110066/*
67 * Flag indicating that we just want to change the comment. This can be set
68 * on the command line.
69 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070int change_comment = 0;
71
72int quiet = 0;
73
Darren Tucker35c45532008-06-13 04:43:15 +100074int log_level = SYSLOG_LEVEL_INFO;
75
Damien Miller4b42d7f2005-03-01 21:48:35 +110076/* Flag indicating that we want to hash a known_hosts file */
77int hash_hosts = 0;
78/* Flag indicating that we want lookup a host in known_hosts file */
79int find_host = 0;
80/* Flag indicating that we want to delete a host from a known_hosts file */
81int delete_host = 0;
82
Damien Millerf2b70ca2010-03-05 07:39:35 +110083/* Flag indicating that we want to show the contents of a certificate */
84int show_cert = 0;
85
Damien Miller10f6f6b1999-11-17 17:29:08 +110086/* Flag indicating that we just want to see the key fingerprint */
87int print_fingerprint = 0;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +000088int print_bubblebabble = 0;
Damien Miller10f6f6b1999-11-17 17:29:08 +110089
Damien Miller431f66b1999-11-21 18:31:57 +110090/* The identity file name, given on the command line or entered by the user. */
91char identity_file[1024];
92int have_identity = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
94/* This is set to the passphrase if given on the command line. */
95char *identity_passphrase = NULL;
96
97/* This is set to the new passphrase if given on the command line. */
98char *identity_new_passphrase = NULL;
99
100/* This is set to the new comment if given on the command line. */
101char *identity_comment = NULL;
102
Damien Miller0a80ca12010-02-27 07:55:05 +1100103/* Path to CA key when certifying keys. */
104char *ca_key_path = NULL;
105
Damien Miller4e270b02010-04-16 15:56:21 +1000106/* Certificate serial number */
107long long cert_serial = 0;
108
Damien Miller0a80ca12010-02-27 07:55:05 +1100109/* Key type when certifying */
110u_int cert_key_type = SSH2_CERT_TYPE_USER;
111
112/* "key ID" of signed key */
113char *cert_key_id = NULL;
114
115/* Comma-separated list of principal names for certifying keys */
116char *cert_principals = NULL;
117
118/* Validity period for certificates */
119u_int64_t cert_valid_from = 0;
120u_int64_t cert_valid_to = ~0ULL;
121
Damien Miller4e270b02010-04-16 15:56:21 +1000122/* Certificate options */
Damien Millerd0e4a8e2010-05-21 14:58:32 +1000123#define CERTOPT_X_FWD (1)
124#define CERTOPT_AGENT_FWD (1<<1)
125#define CERTOPT_PORT_FWD (1<<2)
126#define CERTOPT_PTY (1<<3)
127#define CERTOPT_USER_RC (1<<4)
128#define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
129 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
130u_int32_t certflags_flags = CERTOPT_DEFAULT;
131char *certflags_command = NULL;
132char *certflags_src_addr = NULL;
Damien Miller0a80ca12010-02-27 07:55:05 +1100133
Damien Miller44b25042010-07-02 13:35:01 +1000134/* Conversion to/from various formats */
135int convert_to = 0;
136int convert_from = 0;
137enum {
138 FMT_RFC4716,
139 FMT_PKCS8,
140 FMT_PEM
141} convert_format = FMT_RFC4716;
Damien Millereba71ba2000-04-29 23:57:08 +1000142int print_public = 0;
Damien Miller37876e92003-05-15 10:19:46 +1000143int print_generic = 0;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100144
Damien Millera41c8b12002-01-22 23:05:08 +1100145char *key_type_name = NULL;
Damien Millereba71ba2000-04-29 23:57:08 +1000146
Damien Miller757f34e2010-08-05 13:05:31 +1000147/* Load key from this PKCS#11 provider */
148char *pkcs11provider = NULL;
Damien Miller44b25042010-07-02 13:35:01 +1000149
Damien Miller431f66b1999-11-21 18:31:57 +1100150/* argv0 */
151extern char *__progname;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Millereba71ba2000-04-29 23:57:08 +1000153char hostname[MAXHOSTNAMELEN];
154
Darren Tucker770fc012004-05-13 16:24:32 +1000155/* moduli.c */
Damien Millerb089fb52005-05-26 12:16:18 +1000156int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
Damien Miller390d0562011-10-18 16:05:19 +1100157int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *);
Darren Tucker770fc012004-05-13 16:24:32 +1000158
Ben Lindstrombba81212001-06-25 05:01:22 +0000159static void
Damien Miller884b63a2011-05-05 14:14:52 +1000160type_bits_valid(int type, u_int32_t *bitsp)
Damien Miller58f1baf2011-05-05 14:06:15 +1000161{
162 u_int maxbits;
163
164 if (type == KEY_UNSPEC) {
165 fprintf(stderr, "unknown key type %s\n", key_type_name);
166 exit(1);
167 }
Damien Miller884b63a2011-05-05 14:14:52 +1000168 if (*bitsp == 0) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000169 if (type == KEY_DSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000170 *bitsp = DEFAULT_BITS_DSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000171 else if (type == KEY_ECDSA)
Damien Miller884b63a2011-05-05 14:14:52 +1000172 *bitsp = DEFAULT_BITS_ECDSA;
Damien Miller58f1baf2011-05-05 14:06:15 +1000173 else
Damien Miller884b63a2011-05-05 14:14:52 +1000174 *bitsp = DEFAULT_BITS;
Damien Miller58f1baf2011-05-05 14:06:15 +1000175 }
176 maxbits = (type == KEY_DSA) ?
177 OPENSSL_DSA_MAX_MODULUS_BITS : OPENSSL_RSA_MAX_MODULUS_BITS;
Damien Miller884b63a2011-05-05 14:14:52 +1000178 if (*bitsp > maxbits) {
Damien Miller58f1baf2011-05-05 14:06:15 +1000179 fprintf(stderr, "key bits exceeds maximum %d\n", maxbits);
180 exit(1);
181 }
Damien Miller884b63a2011-05-05 14:14:52 +1000182 if (type == KEY_DSA && *bitsp != 1024)
Damien Miller58f1baf2011-05-05 14:06:15 +1000183 fatal("DSA keys must be 1024 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000184 else if (type != KEY_ECDSA && *bitsp < 768)
Damien Miller58f1baf2011-05-05 14:06:15 +1000185 fatal("Key must at least be 768 bits");
Damien Miller884b63a2011-05-05 14:14:52 +1000186 else if (type == KEY_ECDSA && key_ecdsa_bits_to_nid(*bitsp) == -1)
Damien Miller58f1baf2011-05-05 14:06:15 +1000187 fatal("Invalid ECDSA key length - valid lengths are "
188 "256, 384 or 521 bits");
189}
190
191static void
Damien Miller431f66b1999-11-21 18:31:57 +1100192ask_filename(struct passwd *pw, const char *prompt)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193{
Damien Miller95def091999-11-25 00:26:21 +1100194 char buf[1024];
Damien Millere39cacc2000-11-29 12:18:44 +1100195 char *name = NULL;
196
Damien Miller993dd552002-02-19 15:22:47 +1100197 if (key_type_name == NULL)
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000198 name = _PATH_SSH_CLIENT_ID_RSA;
Damien Miller90967402006-03-26 14:07:26 +1100199 else {
Damien Miller993dd552002-02-19 15:22:47 +1100200 switch (key_type_from_name(key_type_name)) {
201 case KEY_RSA1:
202 name = _PATH_SSH_CLIENT_IDENTITY;
203 break;
Damien Miller4e270b02010-04-16 15:56:21 +1000204 case KEY_DSA_CERT:
205 case KEY_DSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100206 case KEY_DSA:
207 name = _PATH_SSH_CLIENT_ID_DSA;
208 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100209#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000210 case KEY_ECDSA_CERT:
211 case KEY_ECDSA:
212 name = _PATH_SSH_CLIENT_ID_ECDSA;
213 break;
Damien Millerdd190dd2010-11-11 14:17:02 +1100214#endif
Damien Miller4e270b02010-04-16 15:56:21 +1000215 case KEY_RSA_CERT:
216 case KEY_RSA_CERT_V00:
Damien Miller993dd552002-02-19 15:22:47 +1100217 case KEY_RSA:
218 name = _PATH_SSH_CLIENT_ID_RSA;
219 break;
220 default:
Damien Miller9eab9562009-02-22 08:47:02 +1100221 fprintf(stderr, "bad key type\n");
Damien Miller993dd552002-02-19 15:22:47 +1100222 exit(1);
223 break;
224 }
Damien Miller90967402006-03-26 14:07:26 +1100225 }
Damien Millere39cacc2000-11-29 12:18:44 +1100226 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
Ben Lindstrom3deda8b2000-12-22 20:27:43 +0000227 fprintf(stderr, "%s (%s): ", prompt, identity_file);
Damien Miller95def091999-11-25 00:26:21 +1100228 if (fgets(buf, sizeof(buf), stdin) == NULL)
229 exit(1);
Damien Miller14b017d2007-09-17 16:09:15 +1000230 buf[strcspn(buf, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +1100231 if (strcmp(buf, "") != 0)
232 strlcpy(identity_file, buf, sizeof(identity_file));
233 have_identity = 1;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100234}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000235
Ben Lindstrombba81212001-06-25 05:01:22 +0000236static Key *
Ben Lindstromd78ae762001-06-05 20:35:09 +0000237load_identity(char *filename)
Damien Millereba71ba2000-04-29 23:57:08 +1000238{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000239 char *pass;
240 Key *prv;
241
Ben Lindstroma3700052001-04-05 23:26:32 +0000242 prv = key_load_private(filename, "", NULL);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000243 if (prv == NULL) {
Ben Lindstromd78ae762001-06-05 20:35:09 +0000244 if (identity_passphrase)
245 pass = xstrdup(identity_passphrase);
246 else
Ben Lindstrom949974b2001-06-25 05:20:31 +0000247 pass = read_passphrase("Enter passphrase: ",
248 RP_ALLOW_STDIN);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000249 prv = key_load_private(filename, pass, NULL);
Damien Millereba71ba2000-04-29 23:57:08 +1000250 memset(pass, 0, strlen(pass));
251 xfree(pass);
252 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000253 return prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000254}
255
Damien Miller874d77b2000-10-14 16:23:11 +1100256#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
Ben Lindstromcb72e4f2002-06-21 00:41:51 +0000257#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
Damien Miller874d77b2000-10-14 16:23:11 +1100258#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
Kevin Stevesef4eea92001-02-05 12:42:17 +0000259#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
Damien Millereba71ba2000-04-29 23:57:08 +1000260
Ben Lindstrombba81212001-06-25 05:01:22 +0000261static void
Damien Miller44b25042010-07-02 13:35:01 +1000262do_convert_to_ssh2(struct passwd *pw, Key *k)
Damien Millereba71ba2000-04-29 23:57:08 +1000263{
Ben Lindstromc58ab022002-02-26 18:15:09 +0000264 u_int len;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000265 u_char *blob;
Darren Tuckerd04758d2010-01-12 19:41:57 +1100266 char comment[61];
Damien Millereba71ba2000-04-29 23:57:08 +1000267
Damien Millera563cce2012-04-22 11:07:28 +1000268 if (k->type == KEY_RSA1) {
269 fprintf(stderr, "version 1 keys are not supported\n");
270 exit(1);
271 }
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000272 if (key_to_blob(k, &blob, &len) <= 0) {
273 fprintf(stderr, "key_to_blob failed\n");
274 exit(1);
275 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100276 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
277 snprintf(comment, sizeof(comment),
278 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000279 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000280 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100281
282 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
283 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000284 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100285 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000286 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000287 xfree(blob);
288 exit(0);
289}
290
Ben Lindstrombba81212001-06-25 05:01:22 +0000291static void
Damien Miller44b25042010-07-02 13:35:01 +1000292do_convert_to_pkcs8(Key *k)
293{
294 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000295 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000296 case KEY_RSA:
297 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
298 fatal("PEM_write_RSA_PUBKEY failed");
299 break;
300 case KEY_DSA:
301 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
302 fatal("PEM_write_DSA_PUBKEY failed");
303 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000304#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000305 case KEY_ECDSA:
306 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
307 fatal("PEM_write_EC_PUBKEY failed");
308 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000309#endif
Damien Miller44b25042010-07-02 13:35:01 +1000310 default:
311 fatal("%s: unsupported key type %s", __func__, key_type(k));
312 }
313 exit(0);
314}
315
316static void
317do_convert_to_pem(Key *k)
318{
319 switch (key_type_plain(k->type)) {
Damien Millera563cce2012-04-22 11:07:28 +1000320 case KEY_RSA1:
Damien Miller44b25042010-07-02 13:35:01 +1000321 case KEY_RSA:
322 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
323 fatal("PEM_write_RSAPublicKey failed");
324 break;
325#if notyet /* OpenSSH 0.9.8 lacks this function */
326 case KEY_DSA:
327 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
328 fatal("PEM_write_DSAPublicKey failed");
329 break;
330#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000331 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000332 default:
333 fatal("%s: unsupported key type %s", __func__, key_type(k));
334 }
335 exit(0);
336}
337
338static void
339do_convert_to(struct passwd *pw)
340{
341 Key *k;
342 struct stat st;
343
344 if (!have_identity)
345 ask_filename(pw, "Enter file in which the key is");
346 if (stat(identity_file, &st) < 0)
347 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
348 if ((k = key_load_public(identity_file, NULL)) == NULL) {
349 if ((k = load_identity(identity_file)) == NULL) {
350 fprintf(stderr, "load failed\n");
351 exit(1);
352 }
353 }
Damien Miller44b25042010-07-02 13:35:01 +1000354
355 switch (convert_format) {
356 case FMT_RFC4716:
357 do_convert_to_ssh2(pw, k);
358 break;
359 case FMT_PKCS8:
360 do_convert_to_pkcs8(k);
361 break;
362 case FMT_PEM:
363 do_convert_to_pem(k);
364 break;
365 default:
366 fatal("%s: unknown key format %d", __func__, convert_format);
367 }
368 exit(0);
369}
370
371static void
Damien Miller874d77b2000-10-14 16:23:11 +1100372buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
373{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000374 u_int bignum_bits = buffer_get_int(b);
375 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000376
Damien Miller874d77b2000-10-14 16:23:11 +1100377 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000378 fatal("buffer_get_bignum_bits: input buffer too small: "
379 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100380 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
381 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100382 buffer_consume(b, bytes);
383}
384
Ben Lindstrombba81212001-06-25 05:01:22 +0000385static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000386do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100387{
388 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100389 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100390 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000391 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000392 int magic, rlen, ktype, i1, i2, i3, i4;
393 u_int slen;
394 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100395
396 buffer_init(&b);
397 buffer_append(&b, blob, blen);
398
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100399 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100400 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
401 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
402 buffer_free(&b);
403 return NULL;
404 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000405 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100406 type = buffer_get_string(&b, NULL);
407 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000408 i2 = buffer_get_int(&b);
409 i3 = buffer_get_int(&b);
410 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100411 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100412 if (strcmp(cipher, "none") != 0) {
413 error("unsupported cipher %s", cipher);
414 xfree(cipher);
415 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000416 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100417 return NULL;
418 }
419 xfree(cipher);
420
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000421 if (strstr(type, "dsa")) {
422 ktype = KEY_DSA;
423 } else if (strstr(type, "rsa")) {
424 ktype = KEY_RSA;
425 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100426 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000427 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100428 return NULL;
429 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000430 key = key_new_private(ktype);
431 xfree(type);
432
433 switch (key->type) {
434 case KEY_DSA:
435 buffer_get_bignum_bits(&b, key->dsa->p);
436 buffer_get_bignum_bits(&b, key->dsa->g);
437 buffer_get_bignum_bits(&b, key->dsa->q);
438 buffer_get_bignum_bits(&b, key->dsa->pub_key);
439 buffer_get_bignum_bits(&b, key->dsa->priv_key);
440 break;
441 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100442 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000443 debug("e %lx", e);
444 if (e < 30) {
445 e <<= 8;
446 e += buffer_get_char(&b);
447 debug("e %lx", e);
448 e <<= 8;
449 e += buffer_get_char(&b);
450 debug("e %lx", e);
451 }
452 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000453 buffer_free(&b);
454 key_free(key);
455 return NULL;
456 }
457 buffer_get_bignum_bits(&b, key->rsa->d);
458 buffer_get_bignum_bits(&b, key->rsa->n);
459 buffer_get_bignum_bits(&b, key->rsa->iqmp);
460 buffer_get_bignum_bits(&b, key->rsa->q);
461 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000462 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000463 break;
464 }
Damien Miller874d77b2000-10-14 16:23:11 +1100465 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000466 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000467 error("do_convert_private_ssh2_from_blob: "
468 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100469 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000470
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000471 /* try the key */
472 key_sign(key, &sig, &slen, data, sizeof(data));
473 key_verify(key, sig, slen, data, sizeof(data));
474 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100475 return key;
476}
477
Damien Miller8056a9d2006-03-15 12:05:40 +1100478static int
479get_line(FILE *fp, char *line, size_t len)
480{
481 int c;
482 size_t pos = 0;
483
484 line[0] = '\0';
485 while ((c = fgetc(fp)) != EOF) {
486 if (pos >= len - 1) {
487 fprintf(stderr, "input line too long.\n");
488 exit(1);
489 }
Damien Miller90967402006-03-26 14:07:26 +1100490 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100491 case '\r':
492 c = fgetc(fp);
493 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
494 fprintf(stderr, "unget: %s\n", strerror(errno));
495 exit(1);
496 }
497 return pos;
498 case '\n':
499 return pos;
500 }
501 line[pos++] = c;
502 line[pos] = '\0';
503 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100504 /* We reached EOF */
505 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100506}
507
Ben Lindstrombba81212001-06-25 05:01:22 +0000508static void
Damien Miller44b25042010-07-02 13:35:01 +1000509do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000510{
Damien Millereba71ba2000-04-29 23:57:08 +1000511 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000512 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100513 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000514 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000515 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000516 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000517 FILE *fp;
518
Damien Millerba3420a2010-06-26 09:39:07 +1000519 if ((fp = fopen(identity_file, "r")) == NULL)
520 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000521 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100522 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
523 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000524 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000525 if (strncmp(line, "----", 4) == 0 ||
526 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100527 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000528 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000529 if (strstr(line, " END ") != NULL) {
530 break;
531 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000532 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000533 continue;
534 }
Damien Miller30c3d422000-05-09 11:02:59 +1000535 if (escaped) {
536 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000537 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000538 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000539 }
Damien Millereba71ba2000-04-29 23:57:08 +1000540 strlcat(encoded, line, sizeof(encoded));
541 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000542 len = strlen(encoded);
543 if (((len % 4) == 3) &&
544 (encoded[len-1] == '=') &&
545 (encoded[len-2] == '=') &&
546 (encoded[len-3] == '='))
547 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100548 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000549 if (blen < 0) {
550 fprintf(stderr, "uudecode failed.\n");
551 exit(1);
552 }
Damien Miller44b25042010-07-02 13:35:01 +1000553 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100554 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100555 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000556 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100557 fprintf(stderr, "decode blob failed.\n");
558 exit(1);
559 }
Damien Miller44b25042010-07-02 13:35:01 +1000560 fclose(fp);
561}
562
563static void
564do_convert_from_pkcs8(Key **k, int *private)
565{
566 EVP_PKEY *pubkey;
567 FILE *fp;
568
569 if ((fp = fopen(identity_file, "r")) == NULL)
570 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
571 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
572 fatal("%s: %s is not a recognised public key format", __func__,
573 identity_file);
574 }
575 fclose(fp);
576 switch (EVP_PKEY_type(pubkey->type)) {
577 case EVP_PKEY_RSA:
578 *k = key_new(KEY_UNSPEC);
579 (*k)->type = KEY_RSA;
580 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
581 break;
582 case EVP_PKEY_DSA:
583 *k = key_new(KEY_UNSPEC);
584 (*k)->type = KEY_DSA;
585 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
586 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000587#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000588 case EVP_PKEY_EC:
589 *k = key_new(KEY_UNSPEC);
590 (*k)->type = KEY_ECDSA;
591 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100592 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000593 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000594#endif
Damien Miller44b25042010-07-02 13:35:01 +1000595 default:
596 fatal("%s: unsupported pubkey type %d", __func__,
597 EVP_PKEY_type(pubkey->type));
598 }
599 EVP_PKEY_free(pubkey);
600 return;
601}
602
603static void
604do_convert_from_pem(Key **k, int *private)
605{
606 FILE *fp;
607 RSA *rsa;
608#ifdef notyet
609 DSA *dsa;
610#endif
611
612 if ((fp = fopen(identity_file, "r")) == NULL)
613 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
614 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
615 *k = key_new(KEY_UNSPEC);
616 (*k)->type = KEY_RSA;
617 (*k)->rsa = rsa;
618 fclose(fp);
619 return;
620 }
621#if notyet /* OpenSSH 0.9.8 lacks this function */
622 rewind(fp);
623 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
624 *k = key_new(KEY_UNSPEC);
625 (*k)->type = KEY_DSA;
626 (*k)->dsa = dsa;
627 fclose(fp);
628 return;
629 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000630 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000631#endif
632 fatal("%s: unrecognised raw private key format", __func__);
633}
634
635static void
636do_convert_from(struct passwd *pw)
637{
638 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000639 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000640 struct stat st;
641
642 if (!have_identity)
643 ask_filename(pw, "Enter file in which the key is");
644 if (stat(identity_file, &st) < 0)
645 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
646
647 switch (convert_format) {
648 case FMT_RFC4716:
649 do_convert_from_ssh2(pw, &k, &private);
650 break;
651 case FMT_PKCS8:
652 do_convert_from_pkcs8(&k, &private);
653 break;
654 case FMT_PEM:
655 do_convert_from_pem(&k, &private);
656 break;
657 default:
658 fatal("%s: unknown key format %d", __func__, convert_format);
659 }
660
661 if (!private)
662 ok = key_write(k, stdout);
663 if (ok)
664 fprintf(stdout, "\n");
665 else {
666 switch (k->type) {
667 case KEY_DSA:
668 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
669 NULL, 0, NULL, NULL);
670 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000671#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000672 case KEY_ECDSA:
673 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
674 NULL, 0, NULL, NULL);
675 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000676#endif
Damien Miller44b25042010-07-02 13:35:01 +1000677 case KEY_RSA:
678 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
679 NULL, 0, NULL, NULL);
680 break;
681 default:
682 fatal("%s: unsupported key type %s", __func__,
683 key_type(k));
684 }
685 }
686
Damien Miller874d77b2000-10-14 16:23:11 +1100687 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100688 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100689 exit(1);
690 }
Damien Millereba71ba2000-04-29 23:57:08 +1000691 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000692 exit(0);
693}
694
Ben Lindstrombba81212001-06-25 05:01:22 +0000695static void
Damien Millereba71ba2000-04-29 23:57:08 +1000696do_print_public(struct passwd *pw)
697{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000698 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000699 struct stat st;
700
701 if (!have_identity)
702 ask_filename(pw, "Enter file in which the key is");
703 if (stat(identity_file, &st) < 0) {
704 perror(identity_file);
705 exit(1);
706 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000707 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000708 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000709 fprintf(stderr, "load failed\n");
710 exit(1);
711 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000712 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000713 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000714 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000715 fprintf(stdout, "\n");
716 exit(0);
717}
718
Ben Lindstromcd392282001-07-04 03:44:03 +0000719static void
Damien Miller757f34e2010-08-05 13:05:31 +1000720do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000721{
Damien Miller7ea845e2010-02-12 09:21:02 +1100722#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000723 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100724 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000725
Damien Miller7ea845e2010-02-12 09:21:02 +1100726 pkcs11_init(0);
727 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
728 if (nkeys <= 0)
729 fatal("cannot read public key from pkcs11");
730 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000731 key_write(keys[i], stdout);
732 key_free(keys[i]);
733 fprintf(stdout, "\n");
734 }
735 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100736 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000737 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100738#else
739 fatal("no pkcs11 support");
740#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000741}
Ben Lindstromcd392282001-07-04 03:44:03 +0000742
Ben Lindstrombba81212001-06-25 05:01:22 +0000743static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100744do_fingerprint(struct passwd *pw)
745{
Damien Miller98c7ad62000-03-09 21:27:49 +1100746 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000747 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000748 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100749 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000750 enum fp_rep rep;
751 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100752 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100753
Ben Lindstromd0fca422001-03-26 13:44:06 +0000754 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
755 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000756
Damien Miller95def091999-11-25 00:26:21 +1100757 if (!have_identity)
758 ask_filename(pw, "Enter file in which the key is");
759 if (stat(identity_file, &st) < 0) {
760 perror(identity_file);
761 exit(1);
762 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000763 public = key_load_public(identity_file, &comment);
764 if (public != NULL) {
765 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100766 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000767 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
768 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000769 if (log_level >= SYSLOG_LEVEL_VERBOSE)
770 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100771 key_free(public);
772 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000773 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000774 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100775 exit(0);
776 }
Damien Miller40b59852006-06-13 13:00:25 +1000777 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000778 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000779 comment = NULL;
780 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100781
Damien Millerba3420a2010-06-26 09:39:07 +1000782 if ((f = fopen(identity_file, "r")) == NULL)
783 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100784
Damien Millerba3420a2010-06-26 09:39:07 +1000785 while (fgets(line, sizeof(line), f)) {
786 if ((cp = strchr(line, '\n')) == NULL) {
787 error("line %d too long: %.40s...",
788 num + 1, line);
789 skip = 1;
790 continue;
Damien Miller95def091999-11-25 00:26:21 +1100791 }
Damien Millerba3420a2010-06-26 09:39:07 +1000792 num++;
793 if (skip) {
794 skip = 0;
795 continue;
796 }
797 *cp = '\0';
798
799 /* Skip leading whitespace, empty and comment lines. */
800 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
801 ;
802 if (!*cp || *cp == '\n' || *cp == '#')
803 continue;
804 i = strtol(cp, &ep, 10);
805 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
806 int quoted = 0;
807 comment = cp;
808 for (; *cp && (quoted || (*cp != ' ' &&
809 *cp != '\t')); cp++) {
810 if (*cp == '\\' && cp[1] == '"')
811 cp++; /* Skip both */
812 else if (*cp == '"')
813 quoted = !quoted;
814 }
815 if (!*cp)
816 continue;
817 *cp++ = '\0';
818 }
819 ep = cp;
820 public = key_new(KEY_RSA1);
821 if (key_read(public, &cp) != 1) {
822 cp = ep;
823 key_free(public);
824 public = key_new(KEY_UNSPEC);
825 if (key_read(public, &cp) != 1) {
826 key_free(public);
827 continue;
828 }
829 }
830 comment = *cp ? cp : comment;
831 fp = key_fingerprint(public, fptype, rep);
832 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
833 printf("%u %s %s (%s)\n", key_size(public), fp,
834 comment ? comment : "no comment", key_type(public));
835 if (log_level >= SYSLOG_LEVEL_VERBOSE)
836 printf("%s\n", ra);
837 xfree(ra);
838 xfree(fp);
839 key_free(public);
840 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100841 }
Damien Millerba3420a2010-06-26 09:39:07 +1000842 fclose(f);
843
Damien Miller98c7ad62000-03-09 21:27:49 +1100844 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100845 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100846 exit(1);
847 }
Damien Miller95def091999-11-25 00:26:21 +1100848 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100849}
850
Damien Miller4b42d7f2005-03-01 21:48:35 +1100851static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000852do_gen_all_hostkeys(struct passwd *pw)
853{
854 struct {
855 char *key_type;
856 char *key_type_display;
857 char *path;
858 } key_types[] = {
859 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
860 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
861 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100862#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000863 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100864#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000865 { NULL, NULL, NULL }
866 };
867
868 int first = 0;
869 struct stat st;
870 Key *private, *public;
871 char comment[1024];
872 int i, type, fd;
873 FILE *f;
874
875 for (i = 0; key_types[i].key_type; i++) {
876 if (stat(key_types[i].path, &st) == 0)
877 continue;
878 if (errno != ENOENT) {
879 printf("Could not stat %s: %s", key_types[i].path,
880 strerror(errno));
881 first = 0;
882 continue;
883 }
884
885 if (first == 0) {
886 first = 1;
887 printf("%s: generating new host keys: ", __progname);
888 }
889 printf("%s ", key_types[i].key_type_display);
890 fflush(stdout);
891 arc4random_stir();
892 type = key_type_from_name(key_types[i].key_type);
893 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
894 bits = 0;
895 type_bits_valid(type, &bits);
896 private = key_generate(type, bits);
897 if (private == NULL) {
898 fprintf(stderr, "key_generate failed\n");
899 first = 0;
900 continue;
901 }
902 public = key_from_private(private);
903 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
904 hostname);
905 if (!key_save_private(private, identity_file, "", comment)) {
906 printf("Saving the key failed: %s.\n", identity_file);
907 key_free(private);
908 key_free(public);
909 first = 0;
910 continue;
911 }
912 key_free(private);
913 arc4random_stir();
914 strlcat(identity_file, ".pub", sizeof(identity_file));
915 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
916 if (fd == -1) {
917 printf("Could not save your public key in %s\n",
918 identity_file);
919 key_free(public);
920 first = 0;
921 continue;
922 }
923 f = fdopen(fd, "w");
924 if (f == NULL) {
925 printf("fdopen %s failed\n", identity_file);
926 key_free(public);
927 first = 0;
928 continue;
929 }
930 if (!key_write(public, f)) {
931 fprintf(stderr, "write key failed\n");
932 key_free(public);
933 first = 0;
934 continue;
935 }
936 fprintf(f, " %s\n", comment);
937 fclose(f);
938 key_free(public);
939
940 }
941 if (first != 0)
942 printf("\n");
943}
944
945static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100946printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100947{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000948 if (print_fingerprint) {
949 enum fp_rep rep;
950 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000951 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000952
953 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
954 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
955 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100956 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000957 printf("%u %s %s (%s)\n", key_size(public), fp, name,
958 key_type(public));
959 if (log_level >= SYSLOG_LEVEL_VERBOSE)
960 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000961 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000962 xfree(fp);
963 } else {
964 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
965 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100966 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000967 if (!key_write(public, f))
968 fatal("key_write failed");
969 fprintf(f, "\n");
970 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100971}
972
973static void
974do_known_hosts(struct passwd *pw, const char *name)
975{
976 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100977 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100978 char *cp, *cp2, *kp, *kp2;
979 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100980 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100981 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100982
983 if (!have_identity) {
984 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
985 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
986 sizeof(identity_file))
987 fatal("Specified known hosts path too long");
988 xfree(cp);
989 have_identity = 1;
990 }
991 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +1000992 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +1100993
994 /*
995 * Find hosts goes to stdout, hash and deletions happen in-place
996 * A corner case is ssh-keygen -HF foo, which should go to stdout
997 */
998 if (!find_host && (hash_hosts || delete_host)) {
999 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1000 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1001 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1002 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1003 fatal("known_hosts path too long");
1004 umask(077);
1005 if ((c = mkstemp(tmp)) == -1)
1006 fatal("mkstemp: %s", strerror(errno));
1007 if ((out = fdopen(c, "w")) == NULL) {
1008 c = errno;
1009 unlink(tmp);
1010 fatal("fdopen: %s", strerror(c));
1011 }
1012 inplace = 1;
1013 }
1014
1015 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001016 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001017 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001018 skip = 1;
1019 invalid = 1;
1020 continue;
1021 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001022 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001023 if (skip) {
1024 skip = 0;
1025 continue;
1026 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001027 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001028
1029 /* Skip leading whitespace, empty and comment lines. */
1030 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1031 ;
1032 if (!*cp || *cp == '\n' || *cp == '#') {
1033 if (inplace)
1034 fprintf(out, "%s\n", cp);
1035 continue;
1036 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001037 /* Check whether this is a CA key */
1038 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1039 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1040 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1041 ca = 1;
1042 cp += sizeof(CA_MARKER);
1043 } else
1044 ca = 0;
1045
Damien Miller4b42d7f2005-03-01 21:48:35 +11001046 /* Find the end of the host name portion. */
1047 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1048 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001049
Damien Miller4b42d7f2005-03-01 21:48:35 +11001050 if (*kp == '\0' || *(kp + 1) == '\0') {
1051 error("line %d missing key: %.40s...",
1052 num, line);
1053 invalid = 1;
1054 continue;
1055 }
1056 *kp++ = '\0';
1057 kp2 = kp;
1058
Damien Miller0a80ca12010-02-27 07:55:05 +11001059 pub = key_new(KEY_RSA1);
1060 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001061 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001062 key_free(pub);
1063 pub = key_new(KEY_UNSPEC);
1064 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001065 error("line %d invalid key: %.40s...",
1066 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001067 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001068 invalid = 1;
1069 continue;
1070 }
1071 }
1072
1073 if (*cp == HASH_DELIM) {
1074 if (find_host || delete_host) {
1075 cp2 = host_hash(name, cp, strlen(cp));
1076 if (cp2 == NULL) {
1077 error("line %d: invalid hashed "
1078 "name: %.64s...", num, line);
1079 invalid = 1;
1080 continue;
1081 }
1082 c = (strcmp(cp2, cp) == 0);
1083 if (find_host && c) {
1084 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001085 "line %d type %s%s\n", name,
1086 num, key_type(pub),
1087 ca ? " (CA key)" : "");
1088 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001089 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001090 if (delete_host && !c && !ca)
1091 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001092 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +11001093 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001094 } else {
1095 if (find_host || delete_host) {
1096 c = (match_hostname(name, cp,
1097 strlen(cp)) == 1);
1098 if (find_host && c) {
1099 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001100 "line %d type %s%s\n", name,
1101 num, key_type(pub),
1102 ca ? " (CA key)" : "");
1103 printhost(out, name, pub,
1104 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001105 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001106 if (delete_host && !c && !ca)
1107 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001108 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +11001109 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001110 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001111 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001112 if (ca) {
1113 fprintf(stderr, "Warning: "
1114 "ignoring CA key for host: "
1115 "%.64s\n", cp2);
1116 printhost(out, cp2, pub, ca, 0);
1117 } else if (strcspn(cp2, "*?!") !=
1118 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001119 fprintf(stderr, "Warning: "
1120 "ignoring host name with "
1121 "metacharacters: %.64s\n",
1122 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +11001123 printhost(out, cp2, pub, ca, 0);
1124 } else
1125 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001126 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001127 has_unhashed = 1;
1128 }
1129 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001130 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001131 }
1132 fclose(in);
1133
1134 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001135 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001136 identity_file);
1137 if (inplace) {
1138 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001139 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001140 fclose(out);
1141 unlink(tmp);
1142 }
1143 exit(1);
1144 }
1145
1146 if (inplace) {
1147 fclose(out);
1148
1149 /* Backup existing file */
1150 if (unlink(old) == -1 && errno != ENOENT)
1151 fatal("unlink %.100s: %s", old, strerror(errno));
1152 if (link(identity_file, old) == -1)
1153 fatal("link %.100s to %.100s: %s", identity_file, old,
1154 strerror(errno));
1155 /* Move new one into place */
1156 if (rename(tmp, identity_file) == -1) {
1157 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1158 strerror(errno));
1159 unlink(tmp);
1160 unlink(old);
1161 exit(1);
1162 }
1163
1164 fprintf(stderr, "%s updated.\n", identity_file);
1165 fprintf(stderr, "Original contents retained as %s\n", old);
1166 if (has_unhashed) {
1167 fprintf(stderr, "WARNING: %s contains unhashed "
1168 "entries\n", old);
1169 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001170 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001171 }
1172 }
1173
1174 exit(0);
1175}
1176
Damien Miller95def091999-11-25 00:26:21 +11001177/*
1178 * Perform changing a passphrase. The argument is the passwd structure
1179 * for the current user.
1180 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001181static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001182do_change_passphrase(struct passwd *pw)
1183{
Damien Miller95def091999-11-25 00:26:21 +11001184 char *comment;
1185 char *old_passphrase, *passphrase1, *passphrase2;
1186 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001187 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001188
Damien Miller95def091999-11-25 00:26:21 +11001189 if (!have_identity)
1190 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001191 if (stat(identity_file, &st) < 0) {
1192 perror(identity_file);
1193 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001194 }
Damien Miller95def091999-11-25 00:26:21 +11001195 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001196 private = key_load_private(identity_file, "", &comment);
1197 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001198 if (identity_passphrase)
1199 old_passphrase = xstrdup(identity_passphrase);
1200 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001201 old_passphrase =
1202 read_passphrase("Enter old passphrase: ",
1203 RP_ALLOW_STDIN);
1204 private = key_load_private(identity_file, old_passphrase,
1205 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001206 memset(old_passphrase, 0, strlen(old_passphrase));
1207 xfree(old_passphrase);
1208 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001209 printf("Bad passphrase.\n");
1210 exit(1);
1211 }
Damien Miller95def091999-11-25 00:26:21 +11001212 }
1213 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001214
Damien Miller95def091999-11-25 00:26:21 +11001215 /* Ask the new passphrase (twice). */
1216 if (identity_new_passphrase) {
1217 passphrase1 = xstrdup(identity_new_passphrase);
1218 passphrase2 = NULL;
1219 } else {
1220 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001221 read_passphrase("Enter new passphrase (empty for no "
1222 "passphrase): ", RP_ALLOW_STDIN);
1223 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001224 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001225
1226 /* Verify that they are the same. */
1227 if (strcmp(passphrase1, passphrase2) != 0) {
1228 memset(passphrase1, 0, strlen(passphrase1));
1229 memset(passphrase2, 0, strlen(passphrase2));
1230 xfree(passphrase1);
1231 xfree(passphrase2);
1232 printf("Pass phrases do not match. Try again.\n");
1233 exit(1);
1234 }
1235 /* Destroy the other copy. */
1236 memset(passphrase2, 0, strlen(passphrase2));
1237 xfree(passphrase2);
1238 }
1239
1240 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001241 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001242 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001243 memset(passphrase1, 0, strlen(passphrase1));
1244 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001245 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001246 xfree(comment);
1247 exit(1);
1248 }
1249 /* Destroy the passphrase and the copy of the key in memory. */
1250 memset(passphrase1, 0, strlen(passphrase1));
1251 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001252 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +11001253 xfree(comment);
1254
1255 printf("Your identification has been saved with the new passphrase.\n");
1256 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001257}
1258
Damien Miller37876e92003-05-15 10:19:46 +10001259/*
1260 * Print the SSHFP RR.
1261 */
Damien Millercb314822006-03-26 13:48:01 +11001262static int
1263do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001264{
1265 Key *public;
1266 char *comment = NULL;
1267 struct stat st;
1268
Damien Millercb314822006-03-26 13:48:01 +11001269 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +10001270 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +11001271 if (stat(fname, &st) < 0) {
1272 if (errno == ENOENT)
1273 return 0;
1274 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001275 exit(1);
1276 }
Damien Millercb314822006-03-26 13:48:01 +11001277 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001278 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001279 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001280 key_free(public);
1281 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +11001282 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001283 }
1284 if (comment)
1285 xfree(comment);
1286
Damien Millercb314822006-03-26 13:48:01 +11001287 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001288 exit(1);
1289}
Damien Miller37876e92003-05-15 10:19:46 +10001290
Damien Miller95def091999-11-25 00:26:21 +11001291/*
1292 * Change the comment of a private key file.
1293 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001294static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001295do_change_comment(struct passwd *pw)
1296{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001297 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001298 Key *private;
1299 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001300 struct stat st;
1301 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001302 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001303
Damien Miller95def091999-11-25 00:26:21 +11001304 if (!have_identity)
1305 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001306 if (stat(identity_file, &st) < 0) {
1307 perror(identity_file);
1308 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001309 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001310 private = key_load_private(identity_file, "", &comment);
1311 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001312 if (identity_passphrase)
1313 passphrase = xstrdup(identity_passphrase);
1314 else if (identity_new_passphrase)
1315 passphrase = xstrdup(identity_new_passphrase);
1316 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001317 passphrase = read_passphrase("Enter passphrase: ",
1318 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001319 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001320 private = key_load_private(identity_file, passphrase, &comment);
1321 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001322 memset(passphrase, 0, strlen(passphrase));
1323 xfree(passphrase);
1324 printf("Bad passphrase.\n");
1325 exit(1);
1326 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001327 } else {
1328 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001329 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001330 if (private->type != KEY_RSA1) {
1331 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1332 key_free(private);
1333 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001334 }
Damien Miller95def091999-11-25 00:26:21 +11001335 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001336
Damien Miller95def091999-11-25 00:26:21 +11001337 if (identity_comment) {
1338 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1339 } else {
1340 printf("Enter new comment: ");
1341 fflush(stdout);
1342 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1343 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001344 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001345 exit(1);
1346 }
Damien Miller14b017d2007-09-17 16:09:15 +10001347 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001348 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001349
Damien Miller95def091999-11-25 00:26:21 +11001350 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001351 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001352 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001353 memset(passphrase, 0, strlen(passphrase));
1354 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001355 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001356 xfree(comment);
1357 exit(1);
1358 }
Damien Miller95def091999-11-25 00:26:21 +11001359 memset(passphrase, 0, strlen(passphrase));
1360 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001361 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001362 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001363
Damien Miller95def091999-11-25 00:26:21 +11001364 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001365 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1366 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001367 printf("Could not save your public key in %s\n", identity_file);
1368 exit(1);
1369 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001370 f = fdopen(fd, "w");
1371 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001372 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001373 exit(1);
1374 }
Damien Millereba71ba2000-04-29 23:57:08 +10001375 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001376 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001377 key_free(public);
1378 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001379 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001380
Damien Miller95def091999-11-25 00:26:21 +11001381 xfree(comment);
1382
1383 printf("The comment in your key file has been changed.\n");
1384 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001385}
1386
Damien Miller0a80ca12010-02-27 07:55:05 +11001387static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001388fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001389{
1390 char from[32], to[32];
1391 static char ret[64];
1392 time_t tt;
1393 struct tm *tm;
1394
1395 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001396 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001397 return "forever";
1398
Damien Millerf2b70ca2010-03-05 07:39:35 +11001399 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001400 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001401 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001402 tm = localtime(&tt);
1403 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1404 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001405 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001406 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001407 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001408 tm = localtime(&tt);
1409 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1410 }
1411
Damien Millerf2b70ca2010-03-05 07:39:35 +11001412 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001413 snprintf(ret, sizeof(ret), "before %s", to);
1414 return ret;
1415 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001416 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001417 snprintf(ret, sizeof(ret), "after %s", from);
1418 return ret;
1419 }
1420
1421 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1422 return ret;
1423}
1424
1425static void
Damien Miller4e270b02010-04-16 15:56:21 +10001426add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001427{
1428 debug3("%s: %s", __func__, name);
1429 buffer_put_cstring(c, name);
1430 buffer_put_string(c, NULL, 0);
1431}
1432
1433static void
Damien Miller4e270b02010-04-16 15:56:21 +10001434add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001435{
1436 Buffer b;
1437
1438 debug3("%s: %s=%s", __func__, name, value);
1439 buffer_init(&b);
1440 buffer_put_cstring(&b, value);
1441
1442 buffer_put_cstring(c, name);
1443 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1444
1445 buffer_free(&b);
1446}
1447
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001448#define OPTIONS_CRITICAL 1
1449#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001450static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001451prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001452{
Damien Miller0a80ca12010-02-27 07:55:05 +11001453 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001454 if ((which & OPTIONS_CRITICAL) != 0 &&
1455 certflags_command != NULL)
1456 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001457 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001458 (certflags_flags & CERTOPT_X_FWD) != 0)
1459 add_flag_option(c, "permit-X11-forwarding");
1460 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001461 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001462 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001463 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1464 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001465 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001466 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1467 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001468 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001469 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1470 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001471 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001472 if ((which & OPTIONS_CRITICAL) != 0 &&
1473 certflags_src_addr != NULL)
1474 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001475}
1476
Damien Miller757f34e2010-08-05 13:05:31 +10001477static Key *
1478load_pkcs11_key(char *path)
1479{
1480#ifdef ENABLE_PKCS11
1481 Key **keys = NULL, *public, *private = NULL;
1482 int i, nkeys;
1483
1484 if ((public = key_load_public(path, NULL)) == NULL)
1485 fatal("Couldn't load CA public key \"%s\"", path);
1486
1487 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1488 debug3("%s: %d keys", __func__, nkeys);
1489 if (nkeys <= 0)
1490 fatal("cannot read public key from pkcs11");
1491 for (i = 0; i < nkeys; i++) {
1492 if (key_equal_public(public, keys[i])) {
1493 private = keys[i];
1494 continue;
1495 }
1496 key_free(keys[i]);
1497 }
1498 xfree(keys);
1499 key_free(public);
1500 return private;
1501#else
1502 fatal("no pkcs11 support");
1503#endif /* ENABLE_PKCS11 */
1504}
1505
Damien Miller0a80ca12010-02-27 07:55:05 +11001506static void
1507do_ca_sign(struct passwd *pw, int argc, char **argv)
1508{
1509 int i, fd;
1510 u_int n;
1511 Key *ca, *public;
1512 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1513 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001514 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001515
Damien Miller4e270b02010-04-16 15:56:21 +10001516 if (key_type_name != NULL) {
1517 switch (key_type_from_name(key_type_name)) {
1518 case KEY_RSA_CERT_V00:
1519 case KEY_DSA_CERT_V00:
1520 v00 = 1;
1521 break;
1522 case KEY_UNSPEC:
1523 if (strcasecmp(key_type_name, "v00") == 0) {
1524 v00 = 1;
1525 break;
1526 } else if (strcasecmp(key_type_name, "v01") == 0)
1527 break;
1528 /* FALLTHROUGH */
1529 default:
1530 fprintf(stderr, "unknown key type %s\n", key_type_name);
1531 exit(1);
1532 }
1533 }
1534
Damien Miller757f34e2010-08-05 13:05:31 +10001535 pkcs11_init(1);
1536 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1537 if (pkcs11provider != NULL) {
1538 if ((ca = load_pkcs11_key(tmp)) == NULL)
1539 fatal("No PKCS#11 key matching %s found", ca_key_path);
1540 } else if ((ca = load_identity(tmp)) == NULL)
1541 fatal("Couldn't load CA key \"%s\"", tmp);
1542 xfree(tmp);
1543
Damien Miller0a80ca12010-02-27 07:55:05 +11001544 for (i = 0; i < argc; i++) {
1545 /* Split list of principals */
1546 n = 0;
1547 if (cert_principals != NULL) {
1548 otmp = tmp = xstrdup(cert_principals);
1549 plist = NULL;
1550 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1551 plist = xrealloc(plist, n + 1, sizeof(*plist));
1552 if (*(plist[n] = xstrdup(cp)) == '\0')
1553 fatal("Empty principal name");
1554 }
1555 xfree(otmp);
1556 }
1557
1558 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1559 if ((public = key_load_public(tmp, &comment)) == NULL)
1560 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001561 if (public->type != KEY_RSA && public->type != KEY_DSA &&
1562 public->type != KEY_ECDSA)
Damien Miller0a80ca12010-02-27 07:55:05 +11001563 fatal("%s: key \"%s\" type %s cannot be certified",
1564 __func__, tmp, key_type(public));
1565
1566 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001567 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001568 fatal("Could not upgrade key %s to certificate", tmp);
1569 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001570 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001571 public->cert->key_id = xstrdup(cert_key_id);
1572 public->cert->nprincipals = n;
1573 public->cert->principals = plist;
1574 public->cert->valid_after = cert_valid_from;
1575 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001576 if (v00) {
1577 prepare_options_buf(&public->cert->critical,
1578 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1579 } else {
1580 prepare_options_buf(&public->cert->critical,
1581 OPTIONS_CRITICAL);
1582 prepare_options_buf(&public->cert->extensions,
1583 OPTIONS_EXTENSIONS);
1584 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001585 public->cert->signature_key = key_from_private(ca);
1586
1587 if (key_certify(public, ca) != 0)
1588 fatal("Couldn't not certify key %s", tmp);
1589
1590 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1591 *cp = '\0';
1592 xasprintf(&out, "%s-cert.pub", tmp);
1593 xfree(tmp);
1594
1595 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1596 fatal("Could not open \"%s\" for writing: %s", out,
1597 strerror(errno));
1598 if ((f = fdopen(fd, "w")) == NULL)
1599 fatal("%s: fdopen: %s", __func__, strerror(errno));
1600 if (!key_write(public, f))
1601 fatal("Could not write certified key to %s", out);
1602 fprintf(f, " %s\n", comment);
1603 fclose(f);
1604
Damien Miller4e270b02010-04-16 15:56:21 +10001605 if (!quiet) {
1606 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1607 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001608 out, public->cert->key_id,
1609 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001610 cert_principals != NULL ? " for " : "",
1611 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001612 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001613 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001614
1615 key_free(public);
1616 xfree(out);
1617 }
Damien Miller757f34e2010-08-05 13:05:31 +10001618 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001619 exit(0);
1620}
1621
1622static u_int64_t
1623parse_relative_time(const char *s, time_t now)
1624{
1625 int64_t mul, secs;
1626
1627 mul = *s == '-' ? -1 : 1;
1628
1629 if ((secs = convtime(s + 1)) == -1)
1630 fatal("Invalid relative certificate time %s", s);
1631 if (mul == -1 && secs > now)
1632 fatal("Certificate time %s cannot be represented", s);
1633 return now + (u_int64_t)(secs * mul);
1634}
1635
1636static u_int64_t
1637parse_absolute_time(const char *s)
1638{
1639 struct tm tm;
1640 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001641 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001642
Damien Miller2ca342b2010-03-03 12:14:15 +11001643 /*
1644 * POSIX strptime says "The application shall ensure that there
1645 * is white-space or other non-alphanumeric characters between
1646 * any two conversion specifications" so arrange things this way.
1647 */
1648 switch (strlen(s)) {
1649 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001650 fmt = "%Y-%m-%d";
1651 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001652 break;
1653 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001654 fmt = "%Y-%m-%dT%H:%M:%S";
1655 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001656 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1657 break;
1658 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001659 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001660 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001661
1662 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001663 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001664 fatal("Invalid certificate time %s", s);
1665 if ((tt = mktime(&tm)) < 0)
1666 fatal("Certificate time %s cannot be represented", s);
1667 return (u_int64_t)tt;
1668}
1669
1670static void
1671parse_cert_times(char *timespec)
1672{
1673 char *from, *to;
1674 time_t now = time(NULL);
1675 int64_t secs;
1676
1677 /* +timespec relative to now */
1678 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1679 if ((secs = convtime(timespec + 1)) == -1)
1680 fatal("Invalid relative certificate life %s", timespec);
1681 cert_valid_to = now + secs;
1682 /*
1683 * Backdate certificate one minute to avoid problems on hosts
1684 * with poorly-synchronised clocks.
1685 */
1686 cert_valid_from = ((now - 59)/ 60) * 60;
1687 return;
1688 }
1689
1690 /*
1691 * from:to, where
1692 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1693 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1694 */
1695 from = xstrdup(timespec);
1696 to = strchr(from, ':');
1697 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001698 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001699 *to++ = '\0';
1700
1701 if (*from == '-' || *from == '+')
1702 cert_valid_from = parse_relative_time(from, now);
1703 else
1704 cert_valid_from = parse_absolute_time(from);
1705
1706 if (*to == '-' || *to == '+')
1707 cert_valid_to = parse_relative_time(to, cert_valid_from);
1708 else
1709 cert_valid_to = parse_absolute_time(to);
1710
1711 if (cert_valid_to <= cert_valid_from)
1712 fatal("Empty certificate validity interval");
1713 xfree(from);
1714}
1715
1716static void
Damien Miller4e270b02010-04-16 15:56:21 +10001717add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001718{
1719 char *val;
1720
Damien Miller044f4a62011-05-05 14:14:08 +10001721 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001722 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001723 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001724 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001725 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001726 certflags_flags |= CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001727 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001728 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001729 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001730 certflags_flags |= CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001731 else if (strcasecmp(opt, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001732 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001733 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001734 certflags_flags |= CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001735 else if (strcasecmp(opt, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001736 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001737 else if (strcasecmp(opt, "permit-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001738 certflags_flags |= CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001739 else if (strcasecmp(opt, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001740 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001741 else if (strcasecmp(opt, "permit-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001742 certflags_flags |= CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001743 else if (strncasecmp(opt, "force-command=", 14) == 0) {
1744 val = opt + 14;
1745 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001746 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001747 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001748 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001749 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001750 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1751 val = opt + 15;
1752 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001753 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001754 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001755 fatal("source-address already specified");
1756 if (addr_match_cidr_list(NULL, val) != 0)
1757 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001758 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001759 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001760 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001761}
1762
Ben Lindstrombba81212001-06-25 05:01:22 +00001763static void
Damien Millerd834d352010-06-26 09:48:02 +10001764show_options(const Buffer *optbuf, int v00, int in_critical)
1765{
1766 u_char *name, *data;
1767 u_int dlen;
1768 Buffer options, option;
1769
1770 buffer_init(&options);
1771 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1772
1773 buffer_init(&option);
1774 while (buffer_len(&options) != 0) {
1775 name = buffer_get_string(&options, NULL);
1776 data = buffer_get_string_ptr(&options, &dlen);
1777 buffer_append(&option, data, dlen);
1778 printf(" %s", name);
1779 if ((v00 || !in_critical) &&
1780 (strcmp(name, "permit-X11-forwarding") == 0 ||
1781 strcmp(name, "permit-agent-forwarding") == 0 ||
1782 strcmp(name, "permit-port-forwarding") == 0 ||
1783 strcmp(name, "permit-pty") == 0 ||
1784 strcmp(name, "permit-user-rc") == 0))
1785 printf("\n");
1786 else if ((v00 || in_critical) &&
1787 (strcmp(name, "force-command") == 0 ||
1788 strcmp(name, "source-address") == 0)) {
1789 data = buffer_get_string(&option, NULL);
1790 printf(" %s\n", data);
1791 xfree(data);
1792 } else {
1793 printf(" UNKNOWN OPTION (len %u)\n",
1794 buffer_len(&option));
1795 buffer_clear(&option);
1796 }
1797 xfree(name);
1798 if (buffer_len(&option) != 0)
1799 fatal("Option corrupt: extra data at end");
1800 }
1801 buffer_free(&option);
1802 buffer_free(&options);
1803}
1804
1805static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001806do_show_cert(struct passwd *pw)
1807{
1808 Key *key;
1809 struct stat st;
1810 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001811 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001812
1813 if (!have_identity)
1814 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001815 if (stat(identity_file, &st) < 0)
1816 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001817 if ((key = key_load_public(identity_file, NULL)) == NULL)
1818 fatal("%s is not a public key", identity_file);
1819 if (!key_is_cert(key))
1820 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001821 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1822
Damien Millerf2b70ca2010-03-05 07:39:35 +11001823 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1824 ca_fp = key_fingerprint(key->cert->signature_key,
1825 SSH_FP_MD5, SSH_FP_HEX);
1826
1827 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001828 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1829 key_cert_type(key));
1830 printf(" Public key: %s %s\n", key_type(key), key_fp);
1831 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001832 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001833 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001834 if (!v00) {
1835 printf(" Serial: %llu\n",
1836 (unsigned long long)key->cert->serial);
1837 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001838 printf(" Valid: %s\n",
1839 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1840 printf(" Principals: ");
1841 if (key->cert->nprincipals == 0)
1842 printf("(none)\n");
1843 else {
1844 for (i = 0; i < key->cert->nprincipals; i++)
1845 printf("\n %s",
1846 key->cert->principals[i]);
1847 printf("\n");
1848 }
Damien Miller4e270b02010-04-16 15:56:21 +10001849 printf(" Critical Options: ");
1850 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001851 printf("(none)\n");
1852 else {
1853 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001854 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001855 }
Damien Miller4e270b02010-04-16 15:56:21 +10001856 if (!v00) {
1857 printf(" Extensions: ");
1858 if (buffer_len(&key->cert->extensions) == 0)
1859 printf("(none)\n");
1860 else {
1861 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001862 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001863 }
1864 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001865 exit(0);
1866}
1867
1868static void
Damien Miller431f66b1999-11-21 18:31:57 +11001869usage(void)
1870{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001871 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001872 fprintf(stderr, "Options:\n");
Damien Miller58f1baf2011-05-05 14:06:15 +10001873 fprintf(stderr, " -A Generate non-existent host keys for all key types.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001874 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001875 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001876 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001877 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001878 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001879#ifdef ENABLE_PKCS11
1880 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1881#endif
Damien Miller44b25042010-07-02 13:35:01 +10001882 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001883 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1884 fprintf(stderr, " -f filename Filename of the key file.\n");
1885 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1886 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1887 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001888 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1889 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10001890 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Miller927d82b2011-10-18 16:05:38 +11001891 fprintf(stderr, " -K checkpt Write checkpoints to this file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001892 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001893 fprintf(stderr, " -l Show fingerprint of key file.\n");
1894 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001895 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001896 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001897 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10001898 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001899 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1900 fprintf(stderr, " -p Change passphrase of private key file.\n");
1901 fprintf(stderr, " -q Quiet.\n");
1902 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1903 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1904 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001905 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001906 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1907 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001908 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001909 fprintf(stderr, " -v Verbose.\n");
1910 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1911 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001912 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001913
Damien Miller95def091999-11-25 00:26:21 +11001914 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001915}
1916
Damien Miller95def091999-11-25 00:26:21 +11001917/*
1918 * Main program for key management.
1919 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001921main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001922{
Damien Millera41c8b12002-01-22 23:05:08 +11001923 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11001924 char *checkpoint = NULL;
Damien Miller757f34e2010-08-05 13:05:31 +10001925 char out_file[MAXPATHLEN], *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001926 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001927 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001928 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001929 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001930 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001931 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Miller58f1baf2011-05-05 14:06:15 +10001932 int gen_all_hostkeys = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10001933 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001934 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001935 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001936
Damien Miller95def091999-11-25 00:26:21 +11001937 extern int optind;
1938 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001939
Darren Tuckerce321d82005-10-03 18:11:24 +10001940 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1941 sanitise_stdfd();
1942
Darren Tucker9ac56e92007-01-14 10:19:59 +11001943 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001944
Damien Miller4314c2b2010-09-10 11:12:09 +10001945 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001946 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001947
Kevin Steves3a881912002-07-20 19:05:40 +00001948 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001949
Damien Miller5428f641999-11-25 11:54:57 +11001950 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001951 pw = getpwuid(getuid());
1952 if (!pw) {
1953 printf("You don't exist, go away!\n");
1954 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001955 }
Damien Millereba71ba2000-04-29 23:57:08 +10001956 if (gethostname(hostname, sizeof(hostname)) < 0) {
1957 perror("gethostname");
1958 exit(1);
1959 }
Damien Miller5428f641999-11-25 11:54:57 +11001960
Damien Miller390d0562011-10-18 16:05:19 +11001961 while ((opt = getopt(argc, argv, "AegiqpclBHLhvxXyF:b:f:t:D:I:K:P:m:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001962 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001963 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10001964 case 'A':
1965 gen_all_hostkeys = 1;
1966 break;
Damien Miller95def091999-11-25 00:26:21 +11001967 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10001968 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001969 if (errstr)
1970 fatal("Bits has bad value %s (%s)",
1971 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001972 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001973 case 'F':
1974 find_host = 1;
1975 rr_hostname = optarg;
1976 break;
1977 case 'H':
1978 hash_hosts = 1;
1979 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001980 case 'I':
1981 cert_key_id = optarg;
1982 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001983 case 'R':
1984 delete_host = 1;
1985 rr_hostname = optarg;
1986 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001987 case 'L':
1988 show_cert = 1;
1989 break;
Damien Miller95def091999-11-25 00:26:21 +11001990 case 'l':
1991 print_fingerprint = 1;
1992 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001993 case 'B':
1994 print_bubblebabble = 1;
1995 break;
Damien Miller44b25042010-07-02 13:35:01 +10001996 case 'm':
1997 if (strcasecmp(optarg, "RFC4716") == 0 ||
1998 strcasecmp(optarg, "ssh2") == 0) {
1999 convert_format = FMT_RFC4716;
2000 break;
2001 }
2002 if (strcasecmp(optarg, "PKCS8") == 0) {
2003 convert_format = FMT_PKCS8;
2004 break;
2005 }
2006 if (strcasecmp(optarg, "PEM") == 0) {
2007 convert_format = FMT_PEM;
2008 break;
2009 }
2010 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002011 case 'n':
2012 cert_principals = optarg;
2013 break;
Damien Miller95def091999-11-25 00:26:21 +11002014 case 'p':
2015 change_passphrase = 1;
2016 break;
Damien Miller95def091999-11-25 00:26:21 +11002017 case 'c':
2018 change_comment = 1;
2019 break;
Damien Miller95def091999-11-25 00:26:21 +11002020 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002021 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2022 sizeof(identity_file))
2023 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002024 have_identity = 1;
2025 break;
Damien Miller37876e92003-05-15 10:19:46 +10002026 case 'g':
2027 print_generic = 1;
2028 break;
Damien Miller95def091999-11-25 00:26:21 +11002029 case 'P':
2030 identity_passphrase = optarg;
2031 break;
Damien Miller95def091999-11-25 00:26:21 +11002032 case 'N':
2033 identity_new_passphrase = optarg;
2034 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002035 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002036 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002037 break;
Damien Miller95def091999-11-25 00:26:21 +11002038 case 'C':
2039 identity_comment = optarg;
2040 break;
Damien Miller95def091999-11-25 00:26:21 +11002041 case 'q':
2042 quiet = 1;
2043 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002044 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002045 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002046 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002047 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002048 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002049 case 'h':
2050 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002051 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002052 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002053 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002054 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002055 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002056 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002057 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002058 case 'y':
2059 print_public = 1;
2060 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002061 case 's':
2062 ca_key_path = optarg;
2063 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002064 case 't':
2065 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002066 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002067 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002068 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002069 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002070 case 'v':
2071 if (log_level == SYSLOG_LEVEL_INFO)
2072 log_level = SYSLOG_LEVEL_DEBUG1;
2073 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002074 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002075 log_level < SYSLOG_LEVEL_DEBUG3)
2076 log_level++;
2077 }
2078 break;
Damien Miller37876e92003-05-15 10:19:46 +10002079 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002080 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002081 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002082 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002083 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2084 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002085 if (errstr)
2086 fatal("Desired generator has bad value: %s (%s)",
2087 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002088 break;
2089 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11002090 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002091 if (errstr)
2092 fatal("Invalid number of trials: %s (%s)",
2093 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002094 break;
2095 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002096 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002097 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002098 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002099 break;
2100 case 'G':
2101 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002102 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2103 sizeof(out_file))
2104 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002105 break;
2106 case 'T':
2107 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002108 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2109 sizeof(out_file))
2110 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002111 break;
Damien Miller390d0562011-10-18 16:05:19 +11002112 case 'K':
2113 if (strlen(optarg) >= MAXPATHLEN)
2114 fatal("Checkpoint filename too long");
2115 checkpoint = xstrdup(optarg);
2116 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002117 case 'S':
2118 /* XXX - also compare length against bits */
2119 if (BN_hex2bn(&start, optarg) == 0)
2120 fatal("Invalid start point.");
2121 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002122 case 'V':
2123 parse_cert_times(optarg);
2124 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002125 case 'z':
2126 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
2127 if (errstr)
2128 fatal("Invalid serial number: %s", errstr);
2129 break;
Damien Miller95def091999-11-25 00:26:21 +11002130 case '?':
2131 default:
2132 usage();
2133 }
2134 }
Darren Tucker06930c72003-12-31 11:34:51 +11002135
2136 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002137 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002138
Damien Miller0a80ca12010-02-27 07:55:05 +11002139 argv += optind;
2140 argc -= optind;
2141
2142 if (ca_key_path != NULL) {
2143 if (argc < 1) {
2144 printf("Too few arguments.\n");
2145 usage();
2146 }
2147 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11002148 printf("Too many arguments.\n");
2149 usage();
2150 }
2151 if (change_passphrase && change_comment) {
2152 printf("Can only have one of -p and -c.\n");
2153 usage();
2154 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002155 if (print_fingerprint && (delete_host || hash_hosts)) {
2156 printf("Cannot use -l with -D or -R.\n");
2157 usage();
2158 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002159 if (ca_key_path != NULL) {
2160 if (cert_key_id == NULL)
2161 fatal("Must specify key id (-I) when certifying");
2162 do_ca_sign(pw, argc, argv);
2163 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002164 if (show_cert)
2165 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002166 if (delete_host || hash_hosts || find_host)
2167 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002168 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002169 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002170 if (change_passphrase)
2171 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002172 if (change_comment)
2173 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002174 if (convert_to)
2175 do_convert_to(pw);
2176 if (convert_from)
2177 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002178 if (print_public)
2179 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002180 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002181 unsigned int n = 0;
2182
2183 if (have_identity) {
2184 n = do_print_resource_record(pw,
2185 identity_file, rr_hostname);
2186 if (n == 0) {
2187 perror(identity_file);
2188 exit(1);
2189 }
2190 exit(0);
2191 } else {
2192
2193 n += do_print_resource_record(pw,
2194 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2195 n += do_print_resource_record(pw,
2196 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
Damien Miller3bde12a2012-06-20 21:51:11 +10002197 n += do_print_resource_record(pw,
2198 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname);
Damien Millercb314822006-03-26 13:48:01 +11002199
2200 if (n == 0)
2201 fatal("no keys found.");
2202 exit(0);
2203 }
Damien Miller37876e92003-05-15 10:19:46 +10002204 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002205 if (pkcs11provider != NULL)
Damien Miller757f34e2010-08-05 13:05:31 +10002206 do_download(pw);
Damien Miller95def091999-11-25 00:26:21 +11002207
Darren Tucker019cefe2003-08-02 22:40:07 +10002208 if (do_gen_candidates) {
2209 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002210
Darren Tucker019cefe2003-08-02 22:40:07 +10002211 if (out == NULL) {
2212 error("Couldn't open modulus candidate file \"%s\": %s",
2213 out_file, strerror(errno));
2214 return (1);
2215 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002216 if (bits == 0)
2217 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002218 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002219 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002220
2221 return (0);
2222 }
2223
2224 if (do_screen_candidates) {
2225 FILE *in;
2226 FILE *out = fopen(out_file, "w");
2227
2228 if (have_identity && strcmp(identity_file, "-") != 0) {
2229 if ((in = fopen(identity_file, "r")) == NULL) {
2230 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002231 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002232 strerror(errno));
2233 }
2234 } else
2235 in = stdin;
2236
2237 if (out == NULL) {
2238 fatal("Couldn't open moduli file \"%s\": %s",
2239 out_file, strerror(errno));
2240 }
Damien Miller390d0562011-10-18 16:05:19 +11002241 if (prime_test(in, out, trials, generator_wanted, checkpoint)
2242 != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002243 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002244 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002245 }
2246
Damien Miller58f1baf2011-05-05 14:06:15 +10002247 if (gen_all_hostkeys) {
2248 do_gen_all_hostkeys(pw);
2249 return (0);
2250 }
2251
Damien Miller95def091999-11-25 00:26:21 +11002252 arc4random_stir();
2253
Damien Millerf14be5c2005-11-05 15:15:49 +11002254 if (key_type_name == NULL)
2255 key_type_name = "rsa";
2256
Damien Millere39cacc2000-11-29 12:18:44 +11002257 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002258 type_bits_valid(type, &bits);
2259
Darren Tucker3af2ac52005-11-29 13:10:24 +11002260 if (!quiet)
2261 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002262 private = key_generate(type, bits);
2263 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002264 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002265 exit(1);
2266 }
2267 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002268
2269 if (!have_identity)
2270 ask_filename(pw, "Enter file in which to save the key");
2271
Damien Miller788f2122005-11-05 15:14:59 +11002272 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002273 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2274 pw->pw_dir, _PATH_SSH_USER_DIR);
2275 if (strstr(identity_file, dotsshdir) != NULL) {
2276 if (stat(dotsshdir, &st) < 0) {
2277 if (errno != ENOENT) {
2278 error("Could not stat %s: %s", dotsshdir,
2279 strerror(errno));
2280 } else if (mkdir(dotsshdir, 0700) < 0) {
2281 error("Could not create directory '%s': %s",
2282 dotsshdir, strerror(errno));
2283 } else if (!quiet)
2284 printf("Created directory '%s'.\n", dotsshdir);
2285 }
Damien Miller95def091999-11-25 00:26:21 +11002286 }
2287 /* If the file already exists, ask the user to confirm. */
2288 if (stat(identity_file, &st) >= 0) {
2289 char yesno[3];
2290 printf("%s already exists.\n", identity_file);
2291 printf("Overwrite (y/n)? ");
2292 fflush(stdout);
2293 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2294 exit(1);
2295 if (yesno[0] != 'y' && yesno[0] != 'Y')
2296 exit(1);
2297 }
2298 /* Ask for a passphrase (twice). */
2299 if (identity_passphrase)
2300 passphrase1 = xstrdup(identity_passphrase);
2301 else if (identity_new_passphrase)
2302 passphrase1 = xstrdup(identity_new_passphrase);
2303 else {
2304passphrase_again:
2305 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002306 read_passphrase("Enter passphrase (empty for no "
2307 "passphrase): ", RP_ALLOW_STDIN);
2308 passphrase2 = read_passphrase("Enter same passphrase again: ",
2309 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002310 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002311 /*
2312 * The passphrases do not match. Clear them and
2313 * retry.
2314 */
Damien Miller95def091999-11-25 00:26:21 +11002315 memset(passphrase1, 0, strlen(passphrase1));
2316 memset(passphrase2, 0, strlen(passphrase2));
2317 xfree(passphrase1);
2318 xfree(passphrase2);
2319 printf("Passphrases do not match. Try again.\n");
2320 goto passphrase_again;
2321 }
2322 /* Clear the other copy of the passphrase. */
2323 memset(passphrase2, 0, strlen(passphrase2));
2324 xfree(passphrase2);
2325 }
2326
Damien Miller95def091999-11-25 00:26:21 +11002327 if (identity_comment) {
2328 strlcpy(comment, identity_comment, sizeof(comment));
2329 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002330 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002331 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2332 }
2333
2334 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00002335 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002336 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002337 memset(passphrase1, 0, strlen(passphrase1));
2338 xfree(passphrase1);
2339 exit(1);
2340 }
2341 /* Clear the passphrase. */
2342 memset(passphrase1, 0, strlen(passphrase1));
2343 xfree(passphrase1);
2344
2345 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002346 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002347 arc4random_stir();
2348
2349 if (!quiet)
2350 printf("Your identification has been saved in %s.\n", identity_file);
2351
Damien Miller95def091999-11-25 00:26:21 +11002352 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002353 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2354 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002355 printf("Could not save your public key in %s\n", identity_file);
2356 exit(1);
2357 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002358 f = fdopen(fd, "w");
2359 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002360 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002361 exit(1);
2362 }
Damien Millereba71ba2000-04-29 23:57:08 +10002363 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002364 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002365 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002366 fclose(f);
2367
2368 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002369 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002370 char *ra = key_fingerprint(public, SSH_FP_MD5,
2371 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002372 printf("Your public key has been saved in %s.\n",
2373 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002374 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002375 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002376 printf("The key's randomart image is:\n");
2377 printf("%s\n", ra);
2378 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002379 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002380 }
Damien Millereba71ba2000-04-29 23:57:08 +10002381
2382 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002383 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002384}