blob: 5fcd3a1596ae6b937143980660c1042eacf97066 [file] [log] [blame]
Damien Miller927d82b2011-10-18 16:05:38 +11001/* $OpenBSD: ssh-keygen.c,v 1.212 2011/10/16 15:02:41 jmc 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
Ben Lindstrom99a30f12001-09-18 05:49:14 +0000268 if (key_to_blob(k, &blob, &len) <= 0) {
269 fprintf(stderr, "key_to_blob failed\n");
270 exit(1);
271 }
Darren Tuckerd04758d2010-01-12 19:41:57 +1100272 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
273 snprintf(comment, sizeof(comment),
274 "%u-bit %s, converted by %s@%s from OpenSSH",
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000275 key_size(k), key_type(k),
Damien Millereba71ba2000-04-29 23:57:08 +1000276 pw->pw_name, hostname);
Darren Tuckerd04758d2010-01-12 19:41:57 +1100277
278 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
279 fprintf(stdout, "Comment: \"%s\"\n", comment);
Damien Millereba71ba2000-04-29 23:57:08 +1000280 dump_base64(stdout, blob, len);
Damien Miller874d77b2000-10-14 16:23:11 +1100281 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
Ben Lindstrom46c264f2001-04-24 16:56:58 +0000282 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000283 xfree(blob);
284 exit(0);
285}
286
Ben Lindstrombba81212001-06-25 05:01:22 +0000287static void
Damien Miller44b25042010-07-02 13:35:01 +1000288do_convert_to_pkcs8(Key *k)
289{
290 switch (key_type_plain(k->type)) {
291 case KEY_RSA:
292 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
293 fatal("PEM_write_RSA_PUBKEY failed");
294 break;
295 case KEY_DSA:
296 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
297 fatal("PEM_write_DSA_PUBKEY failed");
298 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000299#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000300 case KEY_ECDSA:
301 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
302 fatal("PEM_write_EC_PUBKEY failed");
303 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000304#endif
Damien Miller44b25042010-07-02 13:35:01 +1000305 default:
306 fatal("%s: unsupported key type %s", __func__, key_type(k));
307 }
308 exit(0);
309}
310
311static void
312do_convert_to_pem(Key *k)
313{
314 switch (key_type_plain(k->type)) {
315 case KEY_RSA:
316 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
317 fatal("PEM_write_RSAPublicKey failed");
318 break;
319#if notyet /* OpenSSH 0.9.8 lacks this function */
320 case KEY_DSA:
321 if (!PEM_write_DSAPublicKey(stdout, k->dsa))
322 fatal("PEM_write_DSAPublicKey failed");
323 break;
324#endif
Damien Millereb8b60e2010-08-31 22:41:14 +1000325 /* XXX ECDSA? */
Damien Miller44b25042010-07-02 13:35:01 +1000326 default:
327 fatal("%s: unsupported key type %s", __func__, key_type(k));
328 }
329 exit(0);
330}
331
332static void
333do_convert_to(struct passwd *pw)
334{
335 Key *k;
336 struct stat st;
337
338 if (!have_identity)
339 ask_filename(pw, "Enter file in which the key is");
340 if (stat(identity_file, &st) < 0)
341 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
342 if ((k = key_load_public(identity_file, NULL)) == NULL) {
343 if ((k = load_identity(identity_file)) == NULL) {
344 fprintf(stderr, "load failed\n");
345 exit(1);
346 }
347 }
348 if (k->type == KEY_RSA1) {
349 fprintf(stderr, "version 1 keys are not supported\n");
350 exit(1);
351 }
352
353 switch (convert_format) {
354 case FMT_RFC4716:
355 do_convert_to_ssh2(pw, k);
356 break;
357 case FMT_PKCS8:
358 do_convert_to_pkcs8(k);
359 break;
360 case FMT_PEM:
361 do_convert_to_pem(k);
362 break;
363 default:
364 fatal("%s: unknown key format %d", __func__, convert_format);
365 }
366 exit(0);
367}
368
369static void
Damien Miller874d77b2000-10-14 16:23:11 +1100370buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
371{
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000372 u_int bignum_bits = buffer_get_int(b);
373 u_int bytes = (bignum_bits + 7) / 8;
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000374
Damien Miller874d77b2000-10-14 16:23:11 +1100375 if (buffer_len(b) < bytes)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000376 fatal("buffer_get_bignum_bits: input buffer too small: "
377 "need %d have %d", bytes, buffer_len(b));
Darren Tucker0bc85572006-11-07 23:14:41 +1100378 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
379 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
Damien Miller874d77b2000-10-14 16:23:11 +1100380 buffer_consume(b, bytes);
381}
382
Ben Lindstrombba81212001-06-25 05:01:22 +0000383static Key *
Ben Lindstrom90fd8142002-02-26 18:09:42 +0000384do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
Damien Miller874d77b2000-10-14 16:23:11 +1100385{
386 Buffer b;
Damien Miller874d77b2000-10-14 16:23:11 +1100387 Key *key = NULL;
Damien Miller874d77b2000-10-14 16:23:11 +1100388 char *type, *cipher;
Ben Lindstrom511d69e2001-07-04 05:05:27 +0000389 u_char *sig, data[] = "abcde12345";
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000390 int magic, rlen, ktype, i1, i2, i3, i4;
391 u_int slen;
392 u_long e;
Damien Miller874d77b2000-10-14 16:23:11 +1100393
394 buffer_init(&b);
395 buffer_append(&b, blob, blen);
396
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100397 magic = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100398 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
399 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
400 buffer_free(&b);
401 return NULL;
402 }
Ben Lindstrom34f91882001-06-25 04:47:54 +0000403 i1 = buffer_get_int(&b);
Damien Miller874d77b2000-10-14 16:23:11 +1100404 type = buffer_get_string(&b, NULL);
405 cipher = buffer_get_string(&b, NULL);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000406 i2 = buffer_get_int(&b);
407 i3 = buffer_get_int(&b);
408 i4 = buffer_get_int(&b);
Damien Miller80163902007-01-05 16:30:16 +1100409 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
Damien Miller874d77b2000-10-14 16:23:11 +1100410 if (strcmp(cipher, "none") != 0) {
411 error("unsupported cipher %s", cipher);
412 xfree(cipher);
413 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000414 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100415 return NULL;
416 }
417 xfree(cipher);
418
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000419 if (strstr(type, "dsa")) {
420 ktype = KEY_DSA;
421 } else if (strstr(type, "rsa")) {
422 ktype = KEY_RSA;
423 } else {
Darren Tucker7cfeecf2005-01-20 10:56:31 +1100424 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000425 xfree(type);
Damien Miller874d77b2000-10-14 16:23:11 +1100426 return NULL;
427 }
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000428 key = key_new_private(ktype);
429 xfree(type);
430
431 switch (key->type) {
432 case KEY_DSA:
433 buffer_get_bignum_bits(&b, key->dsa->p);
434 buffer_get_bignum_bits(&b, key->dsa->g);
435 buffer_get_bignum_bits(&b, key->dsa->q);
436 buffer_get_bignum_bits(&b, key->dsa->pub_key);
437 buffer_get_bignum_bits(&b, key->dsa->priv_key);
438 break;
439 case KEY_RSA:
Darren Tucker82a3d2b2007-02-19 22:10:25 +1100440 e = buffer_get_char(&b);
Ben Lindstrom34f91882001-06-25 04:47:54 +0000441 debug("e %lx", e);
442 if (e < 30) {
443 e <<= 8;
444 e += buffer_get_char(&b);
445 debug("e %lx", e);
446 e <<= 8;
447 e += buffer_get_char(&b);
448 debug("e %lx", e);
449 }
450 if (!BN_set_word(key->rsa->e, e)) {
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000451 buffer_free(&b);
452 key_free(key);
453 return NULL;
454 }
455 buffer_get_bignum_bits(&b, key->rsa->d);
456 buffer_get_bignum_bits(&b, key->rsa->n);
457 buffer_get_bignum_bits(&b, key->rsa->iqmp);
458 buffer_get_bignum_bits(&b, key->rsa->q);
459 buffer_get_bignum_bits(&b, key->rsa->p);
Ben Lindstromf7297dd2001-07-04 05:02:23 +0000460 rsa_generate_additional_parameters(key->rsa);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000461 break;
462 }
Damien Miller874d77b2000-10-14 16:23:11 +1100463 rlen = buffer_len(&b);
Ben Lindstrom1c37c6a2001-12-06 18:00:18 +0000464 if (rlen != 0)
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000465 error("do_convert_private_ssh2_from_blob: "
466 "remaining bytes in key blob %d", rlen);
Damien Miller874d77b2000-10-14 16:23:11 +1100467 buffer_free(&b);
Ben Lindstromd09fcf52001-03-29 00:29:54 +0000468
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000469 /* try the key */
470 key_sign(key, &sig, &slen, data, sizeof(data));
471 key_verify(key, sig, slen, data, sizeof(data));
472 xfree(sig);
Damien Miller874d77b2000-10-14 16:23:11 +1100473 return key;
474}
475
Damien Miller8056a9d2006-03-15 12:05:40 +1100476static int
477get_line(FILE *fp, char *line, size_t len)
478{
479 int c;
480 size_t pos = 0;
481
482 line[0] = '\0';
483 while ((c = fgetc(fp)) != EOF) {
484 if (pos >= len - 1) {
485 fprintf(stderr, "input line too long.\n");
486 exit(1);
487 }
Damien Miller90967402006-03-26 14:07:26 +1100488 switch (c) {
Damien Miller8056a9d2006-03-15 12:05:40 +1100489 case '\r':
490 c = fgetc(fp);
491 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
492 fprintf(stderr, "unget: %s\n", strerror(errno));
493 exit(1);
494 }
495 return pos;
496 case '\n':
497 return pos;
498 }
499 line[pos++] = c;
500 line[pos] = '\0';
501 }
Damien Miller6c7439f2007-01-05 16:29:55 +1100502 /* We reached EOF */
503 return -1;
Damien Miller8056a9d2006-03-15 12:05:40 +1100504}
505
Ben Lindstrombba81212001-06-25 05:01:22 +0000506static void
Damien Miller44b25042010-07-02 13:35:01 +1000507do_convert_from_ssh2(struct passwd *pw, Key **k, int *private)
Damien Millereba71ba2000-04-29 23:57:08 +1000508{
Damien Millereba71ba2000-04-29 23:57:08 +1000509 int blen;
Ben Lindstrom155b9812002-04-02 20:26:26 +0000510 u_int len;
Damien Miller8056a9d2006-03-15 12:05:40 +1100511 char line[1024];
Ben Lindstrom9e0ddd42001-09-18 05:41:19 +0000512 u_char blob[8096];
Damien Millereba71ba2000-04-29 23:57:08 +1000513 char encoded[8096];
Damien Miller44b25042010-07-02 13:35:01 +1000514 int escaped = 0;
Damien Millereba71ba2000-04-29 23:57:08 +1000515 FILE *fp;
516
Damien Millerba3420a2010-06-26 09:39:07 +1000517 if ((fp = fopen(identity_file, "r")) == NULL)
518 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millereba71ba2000-04-29 23:57:08 +1000519 encoded[0] = '\0';
Damien Miller8056a9d2006-03-15 12:05:40 +1100520 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
521 if (line[blen - 1] == '\\')
Damien Miller30c3d422000-05-09 11:02:59 +1000522 escaped++;
Damien Millereba71ba2000-04-29 23:57:08 +1000523 if (strncmp(line, "----", 4) == 0 ||
524 strstr(line, ": ") != NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100525 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
Damien Miller44b25042010-07-02 13:35:01 +1000526 *private = 1;
Ben Lindstrome586c4c2001-06-25 05:04:58 +0000527 if (strstr(line, " END ") != NULL) {
528 break;
529 }
Ben Lindstrom30358602001-04-24 16:59:28 +0000530 /* fprintf(stderr, "ignore: %s", line); */
Damien Millereba71ba2000-04-29 23:57:08 +1000531 continue;
532 }
Damien Miller30c3d422000-05-09 11:02:59 +1000533 if (escaped) {
534 escaped--;
Ben Lindstrom30358602001-04-24 16:59:28 +0000535 /* fprintf(stderr, "escaped: %s", line); */
Damien Miller30c3d422000-05-09 11:02:59 +1000536 continue;
Damien Millereba71ba2000-04-29 23:57:08 +1000537 }
Damien Millereba71ba2000-04-29 23:57:08 +1000538 strlcat(encoded, line, sizeof(encoded));
539 }
Ben Lindstrom155b9812002-04-02 20:26:26 +0000540 len = strlen(encoded);
541 if (((len % 4) == 3) &&
542 (encoded[len-1] == '=') &&
543 (encoded[len-2] == '=') &&
544 (encoded[len-3] == '='))
545 encoded[len-3] = '\0';
Damien Miller4a8ed542002-01-22 23:33:31 +1100546 blen = uudecode(encoded, blob, sizeof(blob));
Damien Millereba71ba2000-04-29 23:57:08 +1000547 if (blen < 0) {
548 fprintf(stderr, "uudecode failed.\n");
549 exit(1);
550 }
Damien Miller44b25042010-07-02 13:35:01 +1000551 *k = *private ?
Damien Miller874d77b2000-10-14 16:23:11 +1100552 do_convert_private_ssh2_from_blob(blob, blen) :
Damien Miller0bc1bd82000-11-13 22:57:25 +1100553 key_from_blob(blob, blen);
Damien Miller44b25042010-07-02 13:35:01 +1000554 if (*k == NULL) {
Damien Miller874d77b2000-10-14 16:23:11 +1100555 fprintf(stderr, "decode blob failed.\n");
556 exit(1);
557 }
Damien Miller44b25042010-07-02 13:35:01 +1000558 fclose(fp);
559}
560
561static void
562do_convert_from_pkcs8(Key **k, int *private)
563{
564 EVP_PKEY *pubkey;
565 FILE *fp;
566
567 if ((fp = fopen(identity_file, "r")) == NULL)
568 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
569 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
570 fatal("%s: %s is not a recognised public key format", __func__,
571 identity_file);
572 }
573 fclose(fp);
574 switch (EVP_PKEY_type(pubkey->type)) {
575 case EVP_PKEY_RSA:
576 *k = key_new(KEY_UNSPEC);
577 (*k)->type = KEY_RSA;
578 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
579 break;
580 case EVP_PKEY_DSA:
581 *k = key_new(KEY_UNSPEC);
582 (*k)->type = KEY_DSA;
583 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
584 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000585#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000586 case EVP_PKEY_EC:
587 *k = key_new(KEY_UNSPEC);
588 (*k)->type = KEY_ECDSA;
589 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
Damien Millerb472a902010-11-05 10:19:49 +1100590 (*k)->ecdsa_nid = key_ecdsa_key_to_nid((*k)->ecdsa);
Damien Millereb8b60e2010-08-31 22:41:14 +1000591 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000592#endif
Damien Miller44b25042010-07-02 13:35:01 +1000593 default:
594 fatal("%s: unsupported pubkey type %d", __func__,
595 EVP_PKEY_type(pubkey->type));
596 }
597 EVP_PKEY_free(pubkey);
598 return;
599}
600
601static void
602do_convert_from_pem(Key **k, int *private)
603{
604 FILE *fp;
605 RSA *rsa;
606#ifdef notyet
607 DSA *dsa;
608#endif
609
610 if ((fp = fopen(identity_file, "r")) == NULL)
611 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
612 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
613 *k = key_new(KEY_UNSPEC);
614 (*k)->type = KEY_RSA;
615 (*k)->rsa = rsa;
616 fclose(fp);
617 return;
618 }
619#if notyet /* OpenSSH 0.9.8 lacks this function */
620 rewind(fp);
621 if ((dsa = PEM_read_DSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
622 *k = key_new(KEY_UNSPEC);
623 (*k)->type = KEY_DSA;
624 (*k)->dsa = dsa;
625 fclose(fp);
626 return;
627 }
Damien Millereb8b60e2010-08-31 22:41:14 +1000628 /* XXX ECDSA */
Damien Miller44b25042010-07-02 13:35:01 +1000629#endif
630 fatal("%s: unrecognised raw private key format", __func__);
631}
632
633static void
634do_convert_from(struct passwd *pw)
635{
636 Key *k = NULL;
Damien Miller844cccf2010-08-03 16:03:29 +1000637 int private = 0, ok = 0;
Damien Miller44b25042010-07-02 13:35:01 +1000638 struct stat st;
639
640 if (!have_identity)
641 ask_filename(pw, "Enter file in which the key is");
642 if (stat(identity_file, &st) < 0)
643 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
644
645 switch (convert_format) {
646 case FMT_RFC4716:
647 do_convert_from_ssh2(pw, &k, &private);
648 break;
649 case FMT_PKCS8:
650 do_convert_from_pkcs8(&k, &private);
651 break;
652 case FMT_PEM:
653 do_convert_from_pem(&k, &private);
654 break;
655 default:
656 fatal("%s: unknown key format %d", __func__, convert_format);
657 }
658
659 if (!private)
660 ok = key_write(k, stdout);
661 if (ok)
662 fprintf(stdout, "\n");
663 else {
664 switch (k->type) {
665 case KEY_DSA:
666 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
667 NULL, 0, NULL, NULL);
668 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000669#ifdef OPENSSL_HAS_ECC
Damien Millereb8b60e2010-08-31 22:41:14 +1000670 case KEY_ECDSA:
671 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
672 NULL, 0, NULL, NULL);
673 break;
Damien Miller6af914a2010-09-10 11:39:26 +1000674#endif
Damien Miller44b25042010-07-02 13:35:01 +1000675 case KEY_RSA:
676 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
677 NULL, 0, NULL, NULL);
678 break;
679 default:
680 fatal("%s: unsupported key type %s", __func__,
681 key_type(k));
682 }
683 }
684
Damien Miller874d77b2000-10-14 16:23:11 +1100685 if (!ok) {
Damien Miller9eab9562009-02-22 08:47:02 +1100686 fprintf(stderr, "key write failed\n");
Damien Miller874d77b2000-10-14 16:23:11 +1100687 exit(1);
688 }
Damien Millereba71ba2000-04-29 23:57:08 +1000689 key_free(k);
Damien Millereba71ba2000-04-29 23:57:08 +1000690 exit(0);
691}
692
Ben Lindstrombba81212001-06-25 05:01:22 +0000693static void
Damien Millereba71ba2000-04-29 23:57:08 +1000694do_print_public(struct passwd *pw)
695{
Ben Lindstromd0fca422001-03-26 13:44:06 +0000696 Key *prv;
Damien Millereba71ba2000-04-29 23:57:08 +1000697 struct stat st;
698
699 if (!have_identity)
700 ask_filename(pw, "Enter file in which the key is");
701 if (stat(identity_file, &st) < 0) {
702 perror(identity_file);
703 exit(1);
704 }
Ben Lindstromd78ae762001-06-05 20:35:09 +0000705 prv = load_identity(identity_file);
Ben Lindstromd0fca422001-03-26 13:44:06 +0000706 if (prv == NULL) {
Damien Millereba71ba2000-04-29 23:57:08 +1000707 fprintf(stderr, "load failed\n");
708 exit(1);
709 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000710 if (!key_write(prv, stdout))
Damien Millereba71ba2000-04-29 23:57:08 +1000711 fprintf(stderr, "key_write failed");
Ben Lindstromd0fca422001-03-26 13:44:06 +0000712 key_free(prv);
Damien Millereba71ba2000-04-29 23:57:08 +1000713 fprintf(stdout, "\n");
714 exit(0);
715}
716
Ben Lindstromcd392282001-07-04 03:44:03 +0000717static void
Damien Miller757f34e2010-08-05 13:05:31 +1000718do_download(struct passwd *pw)
Ben Lindstromcd392282001-07-04 03:44:03 +0000719{
Damien Miller7ea845e2010-02-12 09:21:02 +1100720#ifdef ENABLE_PKCS11
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000721 Key **keys = NULL;
Damien Miller7ea845e2010-02-12 09:21:02 +1100722 int i, nkeys;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000723
Damien Miller7ea845e2010-02-12 09:21:02 +1100724 pkcs11_init(0);
725 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
726 if (nkeys <= 0)
727 fatal("cannot read public key from pkcs11");
728 for (i = 0; i < nkeys; i++) {
Ben Lindstrom0936a5b2002-03-26 03:17:42 +0000729 key_write(keys[i], stdout);
730 key_free(keys[i]);
731 fprintf(stdout, "\n");
732 }
733 xfree(keys);
Damien Miller7ea845e2010-02-12 09:21:02 +1100734 pkcs11_terminate();
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000735 exit(0);
Damien Miller7ea845e2010-02-12 09:21:02 +1100736#else
737 fatal("no pkcs11 support");
738#endif /* ENABLE_PKCS11 */
Ben Lindstrom8282d6a2001-08-06 21:44:05 +0000739}
Ben Lindstromcd392282001-07-04 03:44:03 +0000740
Ben Lindstrombba81212001-06-25 05:01:22 +0000741static void
Damien Miller10f6f6b1999-11-17 17:29:08 +1100742do_fingerprint(struct passwd *pw)
743{
Damien Miller98c7ad62000-03-09 21:27:49 +1100744 FILE *f;
Damien Millereba71ba2000-04-29 23:57:08 +1000745 Key *public;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000746 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
Damien Millercb2fbb22008-02-10 22:24:55 +1100747 int i, skip = 0, num = 0, invalid = 1;
Ben Lindstrom65366a82001-12-06 16:32:47 +0000748 enum fp_rep rep;
749 enum fp_type fptype;
Damien Miller95def091999-11-25 00:26:21 +1100750 struct stat st;
Damien Miller10f6f6b1999-11-17 17:29:08 +1100751
Ben Lindstromd0fca422001-03-26 13:44:06 +0000752 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
753 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000754
Damien Miller95def091999-11-25 00:26:21 +1100755 if (!have_identity)
756 ask_filename(pw, "Enter file in which the key is");
757 if (stat(identity_file, &st) < 0) {
758 perror(identity_file);
759 exit(1);
760 }
Ben Lindstromd0fca422001-03-26 13:44:06 +0000761 public = key_load_public(identity_file, &comment);
762 if (public != NULL) {
763 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100764 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Darren Tuckerb68fb4a2008-06-13 08:57:27 +1000765 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
766 key_type(public));
Darren Tucker35c45532008-06-13 04:43:15 +1000767 if (log_level >= SYSLOG_LEVEL_VERBOSE)
768 printf("%s\n", ra);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100769 key_free(public);
770 xfree(comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000771 xfree(ra);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +0000772 xfree(fp);
Damien Miller98c7ad62000-03-09 21:27:49 +1100773 exit(0);
774 }
Damien Miller40b59852006-06-13 13:00:25 +1000775 if (comment) {
Ben Lindstromd0fca422001-03-26 13:44:06 +0000776 xfree(comment);
Damien Miller40b59852006-06-13 13:00:25 +1000777 comment = NULL;
778 }
Damien Miller98c7ad62000-03-09 21:27:49 +1100779
Damien Millerba3420a2010-06-26 09:39:07 +1000780 if ((f = fopen(identity_file, "r")) == NULL)
781 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller98c7ad62000-03-09 21:27:49 +1100782
Damien Millerba3420a2010-06-26 09:39:07 +1000783 while (fgets(line, sizeof(line), f)) {
784 if ((cp = strchr(line, '\n')) == NULL) {
785 error("line %d too long: %.40s...",
786 num + 1, line);
787 skip = 1;
788 continue;
Damien Miller95def091999-11-25 00:26:21 +1100789 }
Damien Millerba3420a2010-06-26 09:39:07 +1000790 num++;
791 if (skip) {
792 skip = 0;
793 continue;
794 }
795 *cp = '\0';
796
797 /* Skip leading whitespace, empty and comment lines. */
798 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
799 ;
800 if (!*cp || *cp == '\n' || *cp == '#')
801 continue;
802 i = strtol(cp, &ep, 10);
803 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
804 int quoted = 0;
805 comment = cp;
806 for (; *cp && (quoted || (*cp != ' ' &&
807 *cp != '\t')); cp++) {
808 if (*cp == '\\' && cp[1] == '"')
809 cp++; /* Skip both */
810 else if (*cp == '"')
811 quoted = !quoted;
812 }
813 if (!*cp)
814 continue;
815 *cp++ = '\0';
816 }
817 ep = cp;
818 public = key_new(KEY_RSA1);
819 if (key_read(public, &cp) != 1) {
820 cp = ep;
821 key_free(public);
822 public = key_new(KEY_UNSPEC);
823 if (key_read(public, &cp) != 1) {
824 key_free(public);
825 continue;
826 }
827 }
828 comment = *cp ? cp : comment;
829 fp = key_fingerprint(public, fptype, rep);
830 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
831 printf("%u %s %s (%s)\n", key_size(public), fp,
832 comment ? comment : "no comment", key_type(public));
833 if (log_level >= SYSLOG_LEVEL_VERBOSE)
834 printf("%s\n", ra);
835 xfree(ra);
836 xfree(fp);
837 key_free(public);
838 invalid = 0;
Damien Miller95def091999-11-25 00:26:21 +1100839 }
Damien Millerba3420a2010-06-26 09:39:07 +1000840 fclose(f);
841
Damien Miller98c7ad62000-03-09 21:27:49 +1100842 if (invalid) {
Damien Millereb5fec62001-11-12 10:52:44 +1100843 printf("%s is not a public key file.\n", identity_file);
Damien Miller98c7ad62000-03-09 21:27:49 +1100844 exit(1);
845 }
Damien Miller95def091999-11-25 00:26:21 +1100846 exit(0);
Damien Miller10f6f6b1999-11-17 17:29:08 +1100847}
848
Damien Miller4b42d7f2005-03-01 21:48:35 +1100849static void
Damien Miller58f1baf2011-05-05 14:06:15 +1000850do_gen_all_hostkeys(struct passwd *pw)
851{
852 struct {
853 char *key_type;
854 char *key_type_display;
855 char *path;
856 } key_types[] = {
857 { "rsa1", "RSA1", _PATH_HOST_KEY_FILE },
858 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
859 { "dsa", "DSA", _PATH_HOST_DSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100860#ifdef OPENSSL_HAS_ECC
Damien Miller58f1baf2011-05-05 14:06:15 +1000861 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
Damien Millerb56e4932012-02-06 07:41:27 +1100862#endif
Damien Miller58f1baf2011-05-05 14:06:15 +1000863 { NULL, NULL, NULL }
864 };
865
866 int first = 0;
867 struct stat st;
868 Key *private, *public;
869 char comment[1024];
870 int i, type, fd;
871 FILE *f;
872
873 for (i = 0; key_types[i].key_type; i++) {
874 if (stat(key_types[i].path, &st) == 0)
875 continue;
876 if (errno != ENOENT) {
877 printf("Could not stat %s: %s", key_types[i].path,
878 strerror(errno));
879 first = 0;
880 continue;
881 }
882
883 if (first == 0) {
884 first = 1;
885 printf("%s: generating new host keys: ", __progname);
886 }
887 printf("%s ", key_types[i].key_type_display);
888 fflush(stdout);
889 arc4random_stir();
890 type = key_type_from_name(key_types[i].key_type);
891 strlcpy(identity_file, key_types[i].path, sizeof(identity_file));
892 bits = 0;
893 type_bits_valid(type, &bits);
894 private = key_generate(type, bits);
895 if (private == NULL) {
896 fprintf(stderr, "key_generate failed\n");
897 first = 0;
898 continue;
899 }
900 public = key_from_private(private);
901 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
902 hostname);
903 if (!key_save_private(private, identity_file, "", comment)) {
904 printf("Saving the key failed: %s.\n", identity_file);
905 key_free(private);
906 key_free(public);
907 first = 0;
908 continue;
909 }
910 key_free(private);
911 arc4random_stir();
912 strlcat(identity_file, ".pub", sizeof(identity_file));
913 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
914 if (fd == -1) {
915 printf("Could not save your public key in %s\n",
916 identity_file);
917 key_free(public);
918 first = 0;
919 continue;
920 }
921 f = fdopen(fd, "w");
922 if (f == NULL) {
923 printf("fdopen %s failed\n", identity_file);
924 key_free(public);
925 first = 0;
926 continue;
927 }
928 if (!key_write(public, f)) {
929 fprintf(stderr, "write key failed\n");
930 key_free(public);
931 first = 0;
932 continue;
933 }
934 fprintf(f, " %s\n", comment);
935 fclose(f);
936 key_free(public);
937
938 }
939 if (first != 0)
940 printf("\n");
941}
942
943static void
Damien Miller0a80ca12010-02-27 07:55:05 +1100944printhost(FILE *f, const char *name, Key *public, int ca, int hash)
Damien Miller4b42d7f2005-03-01 21:48:35 +1100945{
Darren Tucker0f7e9102008-06-08 12:54:29 +1000946 if (print_fingerprint) {
947 enum fp_rep rep;
948 enum fp_type fptype;
Darren Tucker9c16ac92008-06-13 04:40:35 +1000949 char *fp, *ra;
Darren Tucker0f7e9102008-06-08 12:54:29 +1000950
951 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
952 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
953 fp = key_fingerprint(public, fptype, rep);
Darren Tucker9bcd25b2009-10-07 08:45:48 +1100954 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
Damien Miller81dec052008-07-14 11:28:29 +1000955 printf("%u %s %s (%s)\n", key_size(public), fp, name,
956 key_type(public));
957 if (log_level >= SYSLOG_LEVEL_VERBOSE)
958 printf("%s\n", ra);
Darren Tucker9c16ac92008-06-13 04:40:35 +1000959 xfree(ra);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000960 xfree(fp);
961 } else {
962 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
963 fatal("hash_host failed");
Damien Miller0a80ca12010-02-27 07:55:05 +1100964 fprintf(f, "%s%s%s ", ca ? CA_MARKER : "", ca ? " " : "", name);
Darren Tucker0f7e9102008-06-08 12:54:29 +1000965 if (!key_write(public, f))
966 fatal("key_write failed");
967 fprintf(f, "\n");
968 }
Damien Miller4b42d7f2005-03-01 21:48:35 +1100969}
970
971static void
972do_known_hosts(struct passwd *pw, const char *name)
973{
974 FILE *in, *out = stdout;
Damien Miller0a80ca12010-02-27 07:55:05 +1100975 Key *pub;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100976 char *cp, *cp2, *kp, *kp2;
977 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
Damien Millercb2fbb22008-02-10 22:24:55 +1100978 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100979 int ca;
Damien Miller4b42d7f2005-03-01 21:48:35 +1100980
981 if (!have_identity) {
982 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
983 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
984 sizeof(identity_file))
985 fatal("Specified known hosts path too long");
986 xfree(cp);
987 have_identity = 1;
988 }
989 if ((in = fopen(identity_file, "r")) == NULL)
Damien Millerba3420a2010-06-26 09:39:07 +1000990 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Miller4b42d7f2005-03-01 21:48:35 +1100991
992 /*
993 * Find hosts goes to stdout, hash and deletions happen in-place
994 * A corner case is ssh-keygen -HF foo, which should go to stdout
995 */
996 if (!find_host && (hash_hosts || delete_host)) {
997 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
998 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
999 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1000 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1001 fatal("known_hosts path too long");
1002 umask(077);
1003 if ((c = mkstemp(tmp)) == -1)
1004 fatal("mkstemp: %s", strerror(errno));
1005 if ((out = fdopen(c, "w")) == NULL) {
1006 c = errno;
1007 unlink(tmp);
1008 fatal("fdopen: %s", strerror(c));
1009 }
1010 inplace = 1;
1011 }
1012
1013 while (fgets(line, sizeof(line), in)) {
Damien Miller0f4ed692007-10-26 14:26:32 +10001014 if ((cp = strchr(line, '\n')) == NULL) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001015 error("line %d too long: %.40s...", num + 1, line);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001016 skip = 1;
1017 invalid = 1;
1018 continue;
1019 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001020 num++;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001021 if (skip) {
1022 skip = 0;
1023 continue;
1024 }
Damien Miller0f4ed692007-10-26 14:26:32 +10001025 *cp = '\0';
Damien Miller4b42d7f2005-03-01 21:48:35 +11001026
1027 /* Skip leading whitespace, empty and comment lines. */
1028 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
1029 ;
1030 if (!*cp || *cp == '\n' || *cp == '#') {
1031 if (inplace)
1032 fprintf(out, "%s\n", cp);
1033 continue;
1034 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001035 /* Check whether this is a CA key */
1036 if (strncasecmp(cp, CA_MARKER, sizeof(CA_MARKER) - 1) == 0 &&
1037 (cp[sizeof(CA_MARKER) - 1] == ' ' ||
1038 cp[sizeof(CA_MARKER) - 1] == '\t')) {
1039 ca = 1;
1040 cp += sizeof(CA_MARKER);
1041 } else
1042 ca = 0;
1043
Damien Miller4b42d7f2005-03-01 21:48:35 +11001044 /* Find the end of the host name portion. */
1045 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
1046 ;
Damien Miller0a80ca12010-02-27 07:55:05 +11001047
Damien Miller4b42d7f2005-03-01 21:48:35 +11001048 if (*kp == '\0' || *(kp + 1) == '\0') {
1049 error("line %d missing key: %.40s...",
1050 num, line);
1051 invalid = 1;
1052 continue;
1053 }
1054 *kp++ = '\0';
1055 kp2 = kp;
1056
Damien Miller0a80ca12010-02-27 07:55:05 +11001057 pub = key_new(KEY_RSA1);
1058 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001059 kp = kp2;
Damien Miller0a80ca12010-02-27 07:55:05 +11001060 key_free(pub);
1061 pub = key_new(KEY_UNSPEC);
1062 if (key_read(pub, &kp) != 1) {
Damien Miller4b42d7f2005-03-01 21:48:35 +11001063 error("line %d invalid key: %.40s...",
1064 num, line);
Damien Miller0a80ca12010-02-27 07:55:05 +11001065 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001066 invalid = 1;
1067 continue;
1068 }
1069 }
1070
1071 if (*cp == HASH_DELIM) {
1072 if (find_host || delete_host) {
1073 cp2 = host_hash(name, cp, strlen(cp));
1074 if (cp2 == NULL) {
1075 error("line %d: invalid hashed "
1076 "name: %.64s...", num, line);
1077 invalid = 1;
1078 continue;
1079 }
1080 c = (strcmp(cp2, cp) == 0);
1081 if (find_host && c) {
1082 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001083 "line %d type %s%s\n", name,
1084 num, key_type(pub),
1085 ca ? " (CA key)" : "");
1086 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001087 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001088 if (delete_host && !c && !ca)
1089 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001090 } else if (hash_hosts)
Damien Miller0a80ca12010-02-27 07:55:05 +11001091 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001092 } else {
1093 if (find_host || delete_host) {
1094 c = (match_hostname(name, cp,
1095 strlen(cp)) == 1);
1096 if (find_host && c) {
1097 printf("# Host %s found: "
Damien Miller0a80ca12010-02-27 07:55:05 +11001098 "line %d type %s%s\n", name,
1099 num, key_type(pub),
1100 ca ? " (CA key)" : "");
1101 printhost(out, name, pub,
1102 ca, hash_hosts && !ca);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001103 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001104 if (delete_host && !c && !ca)
1105 printhost(out, cp, pub, ca, 0);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001106 } else if (hash_hosts) {
Darren Tucker47eede72005-03-14 23:08:12 +11001107 for (cp2 = strsep(&cp, ",");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001108 cp2 != NULL && *cp2 != '\0';
Damien Miller89eac802005-03-02 12:33:04 +11001109 cp2 = strsep(&cp, ",")) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001110 if (ca) {
1111 fprintf(stderr, "Warning: "
1112 "ignoring CA key for host: "
1113 "%.64s\n", cp2);
1114 printhost(out, cp2, pub, ca, 0);
1115 } else if (strcspn(cp2, "*?!") !=
1116 strlen(cp2)) {
Damien Miller89eac802005-03-02 12:33:04 +11001117 fprintf(stderr, "Warning: "
1118 "ignoring host name with "
1119 "metacharacters: %.64s\n",
1120 cp2);
Damien Miller0a80ca12010-02-27 07:55:05 +11001121 printhost(out, cp2, pub, ca, 0);
1122 } else
1123 printhost(out, cp2, pub, ca, 1);
Damien Miller89eac802005-03-02 12:33:04 +11001124 }
Damien Miller4b42d7f2005-03-01 21:48:35 +11001125 has_unhashed = 1;
1126 }
1127 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001128 key_free(pub);
Damien Miller4b42d7f2005-03-01 21:48:35 +11001129 }
1130 fclose(in);
1131
1132 if (invalid) {
Damien Millercb2fbb22008-02-10 22:24:55 +11001133 fprintf(stderr, "%s is not a valid known_hosts file.\n",
Damien Miller4b42d7f2005-03-01 21:48:35 +11001134 identity_file);
1135 if (inplace) {
1136 fprintf(stderr, "Not replacing existing known_hosts "
Darren Tucker9f438a92005-03-14 23:09:18 +11001137 "file because of errors\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001138 fclose(out);
1139 unlink(tmp);
1140 }
1141 exit(1);
1142 }
1143
1144 if (inplace) {
1145 fclose(out);
1146
1147 /* Backup existing file */
1148 if (unlink(old) == -1 && errno != ENOENT)
1149 fatal("unlink %.100s: %s", old, strerror(errno));
1150 if (link(identity_file, old) == -1)
1151 fatal("link %.100s to %.100s: %s", identity_file, old,
1152 strerror(errno));
1153 /* Move new one into place */
1154 if (rename(tmp, identity_file) == -1) {
1155 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1156 strerror(errno));
1157 unlink(tmp);
1158 unlink(old);
1159 exit(1);
1160 }
1161
1162 fprintf(stderr, "%s updated.\n", identity_file);
1163 fprintf(stderr, "Original contents retained as %s\n", old);
1164 if (has_unhashed) {
1165 fprintf(stderr, "WARNING: %s contains unhashed "
1166 "entries\n", old);
1167 fprintf(stderr, "Delete this file to ensure privacy "
Damien Miller0dc1bef2005-07-17 17:22:45 +10001168 "of hostnames\n");
Damien Miller4b42d7f2005-03-01 21:48:35 +11001169 }
1170 }
1171
1172 exit(0);
1173}
1174
Damien Miller95def091999-11-25 00:26:21 +11001175/*
1176 * Perform changing a passphrase. The argument is the passwd structure
1177 * for the current user.
1178 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001179static void
Damien Miller10f6f6b1999-11-17 17:29:08 +11001180do_change_passphrase(struct passwd *pw)
1181{
Damien Miller95def091999-11-25 00:26:21 +11001182 char *comment;
1183 char *old_passphrase, *passphrase1, *passphrase2;
1184 struct stat st;
Damien Millereba71ba2000-04-29 23:57:08 +10001185 Key *private;
Damien Miller10f6f6b1999-11-17 17:29:08 +11001186
Damien Miller95def091999-11-25 00:26:21 +11001187 if (!have_identity)
1188 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001189 if (stat(identity_file, &st) < 0) {
1190 perror(identity_file);
1191 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001192 }
Damien Miller95def091999-11-25 00:26:21 +11001193 /* Try to load the file with empty passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001194 private = key_load_private(identity_file, "", &comment);
1195 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001196 if (identity_passphrase)
1197 old_passphrase = xstrdup(identity_passphrase);
1198 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001199 old_passphrase =
1200 read_passphrase("Enter old passphrase: ",
1201 RP_ALLOW_STDIN);
1202 private = key_load_private(identity_file, old_passphrase,
1203 &comment);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001204 memset(old_passphrase, 0, strlen(old_passphrase));
1205 xfree(old_passphrase);
1206 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001207 printf("Bad passphrase.\n");
1208 exit(1);
1209 }
Damien Miller95def091999-11-25 00:26:21 +11001210 }
1211 printf("Key has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001212
Damien Miller95def091999-11-25 00:26:21 +11001213 /* Ask the new passphrase (twice). */
1214 if (identity_new_passphrase) {
1215 passphrase1 = xstrdup(identity_new_passphrase);
1216 passphrase2 = NULL;
1217 } else {
1218 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00001219 read_passphrase("Enter new passphrase (empty for no "
1220 "passphrase): ", RP_ALLOW_STDIN);
1221 passphrase2 = read_passphrase("Enter same passphrase again: ",
Damien Miller9f0f5c62001-12-21 14:45:46 +11001222 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001223
1224 /* Verify that they are the same. */
1225 if (strcmp(passphrase1, passphrase2) != 0) {
1226 memset(passphrase1, 0, strlen(passphrase1));
1227 memset(passphrase2, 0, strlen(passphrase2));
1228 xfree(passphrase1);
1229 xfree(passphrase2);
1230 printf("Pass phrases do not match. Try again.\n");
1231 exit(1);
1232 }
1233 /* Destroy the other copy. */
1234 memset(passphrase2, 0, strlen(passphrase2));
1235 xfree(passphrase2);
1236 }
1237
1238 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001239 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001240 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001241 memset(passphrase1, 0, strlen(passphrase1));
1242 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001243 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001244 xfree(comment);
1245 exit(1);
1246 }
1247 /* Destroy the passphrase and the copy of the key in memory. */
1248 memset(passphrase1, 0, strlen(passphrase1));
1249 xfree(passphrase1);
Damien Millereba71ba2000-04-29 23:57:08 +10001250 key_free(private); /* Destroys contents */
Damien Miller95def091999-11-25 00:26:21 +11001251 xfree(comment);
1252
1253 printf("Your identification has been saved with the new passphrase.\n");
1254 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001255}
1256
Damien Miller37876e92003-05-15 10:19:46 +10001257/*
1258 * Print the SSHFP RR.
1259 */
Damien Millercb314822006-03-26 13:48:01 +11001260static int
1261do_print_resource_record(struct passwd *pw, char *fname, char *hname)
Damien Miller37876e92003-05-15 10:19:46 +10001262{
1263 Key *public;
1264 char *comment = NULL;
1265 struct stat st;
1266
Damien Millercb314822006-03-26 13:48:01 +11001267 if (fname == NULL)
Damien Miller37876e92003-05-15 10:19:46 +10001268 ask_filename(pw, "Enter file in which the key is");
Damien Millercb314822006-03-26 13:48:01 +11001269 if (stat(fname, &st) < 0) {
1270 if (errno == ENOENT)
1271 return 0;
1272 perror(fname);
Damien Miller37876e92003-05-15 10:19:46 +10001273 exit(1);
1274 }
Damien Millercb314822006-03-26 13:48:01 +11001275 public = key_load_public(fname, &comment);
Damien Miller37876e92003-05-15 10:19:46 +10001276 if (public != NULL) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +10001277 export_dns_rr(hname, public, stdout, print_generic);
Damien Miller37876e92003-05-15 10:19:46 +10001278 key_free(public);
1279 xfree(comment);
Damien Millercb314822006-03-26 13:48:01 +11001280 return 1;
Damien Miller37876e92003-05-15 10:19:46 +10001281 }
1282 if (comment)
1283 xfree(comment);
1284
Damien Millercb314822006-03-26 13:48:01 +11001285 printf("failed to read v2 public key from %s.\n", fname);
Damien Miller37876e92003-05-15 10:19:46 +10001286 exit(1);
1287}
Damien Miller37876e92003-05-15 10:19:46 +10001288
Damien Miller95def091999-11-25 00:26:21 +11001289/*
1290 * Change the comment of a private key file.
1291 */
Ben Lindstrombba81212001-06-25 05:01:22 +00001292static void
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001293do_change_comment(struct passwd *pw)
1294{
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001295 char new_comment[1024], *comment, *passphrase;
Ben Lindstromd0fca422001-03-26 13:44:06 +00001296 Key *private;
1297 Key *public;
Damien Miller95def091999-11-25 00:26:21 +11001298 struct stat st;
1299 FILE *f;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001300 int fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001301
Damien Miller95def091999-11-25 00:26:21 +11001302 if (!have_identity)
1303 ask_filename(pw, "Enter file in which the key is");
Damien Miller95def091999-11-25 00:26:21 +11001304 if (stat(identity_file, &st) < 0) {
1305 perror(identity_file);
1306 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001307 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001308 private = key_load_private(identity_file, "", &comment);
1309 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001310 if (identity_passphrase)
1311 passphrase = xstrdup(identity_passphrase);
1312 else if (identity_new_passphrase)
1313 passphrase = xstrdup(identity_new_passphrase);
1314 else
Ben Lindstrom949974b2001-06-25 05:20:31 +00001315 passphrase = read_passphrase("Enter passphrase: ",
1316 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11001317 /* Try to load using the passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001318 private = key_load_private(identity_file, passphrase, &comment);
1319 if (private == NULL) {
Damien Miller95def091999-11-25 00:26:21 +11001320 memset(passphrase, 0, strlen(passphrase));
1321 xfree(passphrase);
1322 printf("Bad passphrase.\n");
1323 exit(1);
1324 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001325 } else {
1326 passphrase = xstrdup("");
Damien Miller95def091999-11-25 00:26:21 +11001327 }
Ben Lindstromd0fca422001-03-26 13:44:06 +00001328 if (private->type != KEY_RSA1) {
1329 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
1330 key_free(private);
1331 exit(1);
Damien Miller9f0f5c62001-12-21 14:45:46 +11001332 }
Damien Miller95def091999-11-25 00:26:21 +11001333 printf("Key now has comment '%s'\n", comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001334
Damien Miller95def091999-11-25 00:26:21 +11001335 if (identity_comment) {
1336 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1337 } else {
1338 printf("Enter new comment: ");
1339 fflush(stdout);
1340 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1341 memset(passphrase, 0, strlen(passphrase));
Damien Millereba71ba2000-04-29 23:57:08 +10001342 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001343 exit(1);
1344 }
Damien Miller14b017d2007-09-17 16:09:15 +10001345 new_comment[strcspn(new_comment, "\n")] = '\0';
Damien Miller95def091999-11-25 00:26:21 +11001346 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001347
Damien Miller95def091999-11-25 00:26:21 +11001348 /* Save the file using the new passphrase. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00001349 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00001350 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11001351 memset(passphrase, 0, strlen(passphrase));
1352 xfree(passphrase);
Damien Millereba71ba2000-04-29 23:57:08 +10001353 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11001354 xfree(comment);
1355 exit(1);
1356 }
Damien Miller95def091999-11-25 00:26:21 +11001357 memset(passphrase, 0, strlen(passphrase));
1358 xfree(passphrase);
Ben Lindstromd0fca422001-03-26 13:44:06 +00001359 public = key_from_private(private);
Damien Millereba71ba2000-04-29 23:57:08 +10001360 key_free(private);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001361
Damien Miller95def091999-11-25 00:26:21 +11001362 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001363 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1364 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11001365 printf("Could not save your public key in %s\n", identity_file);
1366 exit(1);
1367 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001368 f = fdopen(fd, "w");
1369 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11001370 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001371 exit(1);
1372 }
Damien Millereba71ba2000-04-29 23:57:08 +10001373 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11001374 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10001375 key_free(public);
1376 fprintf(f, " %s\n", new_comment);
Damien Miller95def091999-11-25 00:26:21 +11001377 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001378
Damien Miller95def091999-11-25 00:26:21 +11001379 xfree(comment);
1380
1381 printf("The comment in your key file has been changed.\n");
1382 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001383}
1384
Damien Miller0a80ca12010-02-27 07:55:05 +11001385static const char *
Damien Millerf2b70ca2010-03-05 07:39:35 +11001386fmt_validity(u_int64_t valid_from, u_int64_t valid_to)
Damien Miller0a80ca12010-02-27 07:55:05 +11001387{
1388 char from[32], to[32];
1389 static char ret[64];
1390 time_t tt;
1391 struct tm *tm;
1392
1393 *from = *to = '\0';
Damien Millerf2b70ca2010-03-05 07:39:35 +11001394 if (valid_from == 0 && valid_to == 0xffffffffffffffffULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001395 return "forever";
1396
Damien Millerf2b70ca2010-03-05 07:39:35 +11001397 if (valid_from != 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001398 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001399 tt = valid_from > INT_MAX ? INT_MAX : valid_from;
Damien Miller0a80ca12010-02-27 07:55:05 +11001400 tm = localtime(&tt);
1401 strftime(from, sizeof(from), "%Y-%m-%dT%H:%M:%S", tm);
1402 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001403 if (valid_to != 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001404 /* XXX revisit INT_MAX in 2038 :) */
Damien Millerf2b70ca2010-03-05 07:39:35 +11001405 tt = valid_to > INT_MAX ? INT_MAX : valid_to;
Damien Miller0a80ca12010-02-27 07:55:05 +11001406 tm = localtime(&tt);
1407 strftime(to, sizeof(to), "%Y-%m-%dT%H:%M:%S", tm);
1408 }
1409
Damien Millerf2b70ca2010-03-05 07:39:35 +11001410 if (valid_from == 0) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001411 snprintf(ret, sizeof(ret), "before %s", to);
1412 return ret;
1413 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001414 if (valid_to == 0xffffffffffffffffULL) {
Damien Miller0a80ca12010-02-27 07:55:05 +11001415 snprintf(ret, sizeof(ret), "after %s", from);
1416 return ret;
1417 }
1418
1419 snprintf(ret, sizeof(ret), "from %s to %s", from, to);
1420 return ret;
1421}
1422
1423static void
Damien Miller4e270b02010-04-16 15:56:21 +10001424add_flag_option(Buffer *c, const char *name)
Damien Miller0a80ca12010-02-27 07:55:05 +11001425{
1426 debug3("%s: %s", __func__, name);
1427 buffer_put_cstring(c, name);
1428 buffer_put_string(c, NULL, 0);
1429}
1430
1431static void
Damien Miller4e270b02010-04-16 15:56:21 +10001432add_string_option(Buffer *c, const char *name, const char *value)
Damien Miller0a80ca12010-02-27 07:55:05 +11001433{
1434 Buffer b;
1435
1436 debug3("%s: %s=%s", __func__, name, value);
1437 buffer_init(&b);
1438 buffer_put_cstring(&b, value);
1439
1440 buffer_put_cstring(c, name);
1441 buffer_put_string(c, buffer_ptr(&b), buffer_len(&b));
1442
1443 buffer_free(&b);
1444}
1445
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001446#define OPTIONS_CRITICAL 1
1447#define OPTIONS_EXTENSIONS 2
Damien Miller0a80ca12010-02-27 07:55:05 +11001448static void
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001449prepare_options_buf(Buffer *c, int which)
Damien Miller0a80ca12010-02-27 07:55:05 +11001450{
Damien Miller0a80ca12010-02-27 07:55:05 +11001451 buffer_clear(c);
Damien Miller1da63882010-08-05 13:03:51 +10001452 if ((which & OPTIONS_CRITICAL) != 0 &&
1453 certflags_command != NULL)
1454 add_string_option(c, "force-command", certflags_command);
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001455 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Miller2ce12ef2011-05-05 14:17:18 +10001456 (certflags_flags & CERTOPT_X_FWD) != 0)
1457 add_flag_option(c, "permit-X11-forwarding");
1458 if ((which & OPTIONS_EXTENSIONS) != 0 &&
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001459 (certflags_flags & CERTOPT_AGENT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001460 add_flag_option(c, "permit-agent-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001461 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1462 (certflags_flags & CERTOPT_PORT_FWD) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001463 add_flag_option(c, "permit-port-forwarding");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001464 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1465 (certflags_flags & CERTOPT_PTY) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001466 add_flag_option(c, "permit-pty");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001467 if ((which & OPTIONS_EXTENSIONS) != 0 &&
1468 (certflags_flags & CERTOPT_USER_RC) != 0)
Damien Miller4e270b02010-04-16 15:56:21 +10001469 add_flag_option(c, "permit-user-rc");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001470 if ((which & OPTIONS_CRITICAL) != 0 &&
1471 certflags_src_addr != NULL)
1472 add_string_option(c, "source-address", certflags_src_addr);
Damien Miller0a80ca12010-02-27 07:55:05 +11001473}
1474
Damien Miller757f34e2010-08-05 13:05:31 +10001475static Key *
1476load_pkcs11_key(char *path)
1477{
1478#ifdef ENABLE_PKCS11
1479 Key **keys = NULL, *public, *private = NULL;
1480 int i, nkeys;
1481
1482 if ((public = key_load_public(path, NULL)) == NULL)
1483 fatal("Couldn't load CA public key \"%s\"", path);
1484
1485 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
1486 debug3("%s: %d keys", __func__, nkeys);
1487 if (nkeys <= 0)
1488 fatal("cannot read public key from pkcs11");
1489 for (i = 0; i < nkeys; i++) {
1490 if (key_equal_public(public, keys[i])) {
1491 private = keys[i];
1492 continue;
1493 }
1494 key_free(keys[i]);
1495 }
1496 xfree(keys);
1497 key_free(public);
1498 return private;
1499#else
1500 fatal("no pkcs11 support");
1501#endif /* ENABLE_PKCS11 */
1502}
1503
Damien Miller0a80ca12010-02-27 07:55:05 +11001504static void
1505do_ca_sign(struct passwd *pw, int argc, char **argv)
1506{
1507 int i, fd;
1508 u_int n;
1509 Key *ca, *public;
1510 char *otmp, *tmp, *cp, *out, *comment, **plist = NULL;
1511 FILE *f;
Damien Miller4e270b02010-04-16 15:56:21 +10001512 int v00 = 0; /* legacy keys */
Damien Miller0a80ca12010-02-27 07:55:05 +11001513
Damien Miller4e270b02010-04-16 15:56:21 +10001514 if (key_type_name != NULL) {
1515 switch (key_type_from_name(key_type_name)) {
1516 case KEY_RSA_CERT_V00:
1517 case KEY_DSA_CERT_V00:
1518 v00 = 1;
1519 break;
1520 case KEY_UNSPEC:
1521 if (strcasecmp(key_type_name, "v00") == 0) {
1522 v00 = 1;
1523 break;
1524 } else if (strcasecmp(key_type_name, "v01") == 0)
1525 break;
1526 /* FALLTHROUGH */
1527 default:
1528 fprintf(stderr, "unknown key type %s\n", key_type_name);
1529 exit(1);
1530 }
1531 }
1532
Damien Miller757f34e2010-08-05 13:05:31 +10001533 pkcs11_init(1);
1534 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1535 if (pkcs11provider != NULL) {
1536 if ((ca = load_pkcs11_key(tmp)) == NULL)
1537 fatal("No PKCS#11 key matching %s found", ca_key_path);
1538 } else if ((ca = load_identity(tmp)) == NULL)
1539 fatal("Couldn't load CA key \"%s\"", tmp);
1540 xfree(tmp);
1541
Damien Miller0a80ca12010-02-27 07:55:05 +11001542 for (i = 0; i < argc; i++) {
1543 /* Split list of principals */
1544 n = 0;
1545 if (cert_principals != NULL) {
1546 otmp = tmp = xstrdup(cert_principals);
1547 plist = NULL;
1548 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1549 plist = xrealloc(plist, n + 1, sizeof(*plist));
1550 if (*(plist[n] = xstrdup(cp)) == '\0')
1551 fatal("Empty principal name");
1552 }
1553 xfree(otmp);
1554 }
1555
1556 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1557 if ((public = key_load_public(tmp, &comment)) == NULL)
1558 fatal("%s: unable to open \"%s\"", __func__, tmp);
Damien Millereb8b60e2010-08-31 22:41:14 +10001559 if (public->type != KEY_RSA && public->type != KEY_DSA &&
1560 public->type != KEY_ECDSA)
Damien Miller0a80ca12010-02-27 07:55:05 +11001561 fatal("%s: key \"%s\" type %s cannot be certified",
1562 __func__, tmp, key_type(public));
1563
1564 /* Prepare certificate to sign */
Damien Miller4e270b02010-04-16 15:56:21 +10001565 if (key_to_certified(public, v00) != 0)
Damien Miller0a80ca12010-02-27 07:55:05 +11001566 fatal("Could not upgrade key %s to certificate", tmp);
1567 public->cert->type = cert_key_type;
Damien Miller4e270b02010-04-16 15:56:21 +10001568 public->cert->serial = (u_int64_t)cert_serial;
Damien Miller0a80ca12010-02-27 07:55:05 +11001569 public->cert->key_id = xstrdup(cert_key_id);
1570 public->cert->nprincipals = n;
1571 public->cert->principals = plist;
1572 public->cert->valid_after = cert_valid_from;
1573 public->cert->valid_before = cert_valid_to;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001574 if (v00) {
1575 prepare_options_buf(&public->cert->critical,
1576 OPTIONS_CRITICAL|OPTIONS_EXTENSIONS);
1577 } else {
1578 prepare_options_buf(&public->cert->critical,
1579 OPTIONS_CRITICAL);
1580 prepare_options_buf(&public->cert->extensions,
1581 OPTIONS_EXTENSIONS);
1582 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001583 public->cert->signature_key = key_from_private(ca);
1584
1585 if (key_certify(public, ca) != 0)
1586 fatal("Couldn't not certify key %s", tmp);
1587
1588 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1589 *cp = '\0';
1590 xasprintf(&out, "%s-cert.pub", tmp);
1591 xfree(tmp);
1592
1593 if ((fd = open(out, O_WRONLY|O_CREAT|O_TRUNC, 0644)) == -1)
1594 fatal("Could not open \"%s\" for writing: %s", out,
1595 strerror(errno));
1596 if ((f = fdopen(fd, "w")) == NULL)
1597 fatal("%s: fdopen: %s", __func__, strerror(errno));
1598 if (!key_write(public, f))
1599 fatal("Could not write certified key to %s", out);
1600 fprintf(f, " %s\n", comment);
1601 fclose(f);
1602
Damien Miller4e270b02010-04-16 15:56:21 +10001603 if (!quiet) {
1604 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1605 "valid %s", key_cert_type(public),
Damien Miller821de0a2011-01-11 17:20:29 +11001606 out, public->cert->key_id,
1607 (unsigned long long)public->cert->serial,
Damien Miller0a80ca12010-02-27 07:55:05 +11001608 cert_principals != NULL ? " for " : "",
1609 cert_principals != NULL ? cert_principals : "",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001610 fmt_validity(cert_valid_from, cert_valid_to));
Damien Miller4e270b02010-04-16 15:56:21 +10001611 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001612
1613 key_free(public);
1614 xfree(out);
1615 }
Damien Miller757f34e2010-08-05 13:05:31 +10001616 pkcs11_terminate();
Damien Miller0a80ca12010-02-27 07:55:05 +11001617 exit(0);
1618}
1619
1620static u_int64_t
1621parse_relative_time(const char *s, time_t now)
1622{
1623 int64_t mul, secs;
1624
1625 mul = *s == '-' ? -1 : 1;
1626
1627 if ((secs = convtime(s + 1)) == -1)
1628 fatal("Invalid relative certificate time %s", s);
1629 if (mul == -1 && secs > now)
1630 fatal("Certificate time %s cannot be represented", s);
1631 return now + (u_int64_t)(secs * mul);
1632}
1633
1634static u_int64_t
1635parse_absolute_time(const char *s)
1636{
1637 struct tm tm;
1638 time_t tt;
Damien Miller2ca342b2010-03-03 12:14:15 +11001639 char buf[32], *fmt;
Damien Miller0a80ca12010-02-27 07:55:05 +11001640
Damien Miller2ca342b2010-03-03 12:14:15 +11001641 /*
1642 * POSIX strptime says "The application shall ensure that there
1643 * is white-space or other non-alphanumeric characters between
1644 * any two conversion specifications" so arrange things this way.
1645 */
1646 switch (strlen(s)) {
1647 case 8:
Damien Miller3e1ee492010-03-08 09:24:11 +11001648 fmt = "%Y-%m-%d";
1649 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2s", s, s + 4, s + 6);
Damien Miller2ca342b2010-03-03 12:14:15 +11001650 break;
1651 case 14:
Damien Miller3e1ee492010-03-08 09:24:11 +11001652 fmt = "%Y-%m-%dT%H:%M:%S";
1653 snprintf(buf, sizeof(buf), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
Damien Miller2ca342b2010-03-03 12:14:15 +11001654 s, s + 4, s + 6, s + 8, s + 10, s + 12);
1655 break;
1656 default:
Damien Miller0a80ca12010-02-27 07:55:05 +11001657 fatal("Invalid certificate time format %s", s);
Damien Miller2ca342b2010-03-03 12:14:15 +11001658 }
Damien Miller0a80ca12010-02-27 07:55:05 +11001659
1660 bzero(&tm, sizeof(tm));
Damien Miller2ca342b2010-03-03 12:14:15 +11001661 if (strptime(buf, fmt, &tm) == NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001662 fatal("Invalid certificate time %s", s);
1663 if ((tt = mktime(&tm)) < 0)
1664 fatal("Certificate time %s cannot be represented", s);
1665 return (u_int64_t)tt;
1666}
1667
1668static void
1669parse_cert_times(char *timespec)
1670{
1671 char *from, *to;
1672 time_t now = time(NULL);
1673 int64_t secs;
1674
1675 /* +timespec relative to now */
1676 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1677 if ((secs = convtime(timespec + 1)) == -1)
1678 fatal("Invalid relative certificate life %s", timespec);
1679 cert_valid_to = now + secs;
1680 /*
1681 * Backdate certificate one minute to avoid problems on hosts
1682 * with poorly-synchronised clocks.
1683 */
1684 cert_valid_from = ((now - 59)/ 60) * 60;
1685 return;
1686 }
1687
1688 /*
1689 * from:to, where
1690 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1691 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1692 */
1693 from = xstrdup(timespec);
1694 to = strchr(from, ':');
1695 if (to == NULL || from == to || *(to + 1) == '\0')
Damien Miller910f2092010-03-04 14:17:22 +11001696 fatal("Invalid certificate life specification %s", timespec);
Damien Miller0a80ca12010-02-27 07:55:05 +11001697 *to++ = '\0';
1698
1699 if (*from == '-' || *from == '+')
1700 cert_valid_from = parse_relative_time(from, now);
1701 else
1702 cert_valid_from = parse_absolute_time(from);
1703
1704 if (*to == '-' || *to == '+')
1705 cert_valid_to = parse_relative_time(to, cert_valid_from);
1706 else
1707 cert_valid_to = parse_absolute_time(to);
1708
1709 if (cert_valid_to <= cert_valid_from)
1710 fatal("Empty certificate validity interval");
1711 xfree(from);
1712}
1713
1714static void
Damien Miller4e270b02010-04-16 15:56:21 +10001715add_cert_option(char *opt)
Damien Miller0a80ca12010-02-27 07:55:05 +11001716{
1717 char *val;
1718
Damien Miller044f4a62011-05-05 14:14:08 +10001719 if (strcasecmp(opt, "clear") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001720 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11001721 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001722 certflags_flags &= ~CERTOPT_X_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001723 else if (strcasecmp(opt, "permit-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, "no-agent-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001726 certflags_flags &= ~CERTOPT_AGENT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001727 else if (strcasecmp(opt, "permit-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, "no-port-forwarding") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001730 certflags_flags &= ~CERTOPT_PORT_FWD;
Damien Miller0a80ca12010-02-27 07:55:05 +11001731 else if (strcasecmp(opt, "permit-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, "no-pty") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001734 certflags_flags &= ~CERTOPT_PTY;
Damien Miller0a80ca12010-02-27 07:55:05 +11001735 else if (strcasecmp(opt, "permit-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, "no-user-rc") == 0)
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001738 certflags_flags &= ~CERTOPT_USER_RC;
Damien Miller0a80ca12010-02-27 07:55:05 +11001739 else if (strcasecmp(opt, "permit-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 (strncasecmp(opt, "force-command=", 14) == 0) {
1742 val = opt + 14;
1743 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001744 fatal("Empty force-command option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001745 if (certflags_command != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001746 fatal("force-command already specified");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001747 certflags_command = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001748 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
1749 val = opt + 15;
1750 if (*val == '\0')
Damien Miller4e270b02010-04-16 15:56:21 +10001751 fatal("Empty source-address option");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001752 if (certflags_src_addr != NULL)
Damien Miller0a80ca12010-02-27 07:55:05 +11001753 fatal("source-address already specified");
1754 if (addr_match_cidr_list(NULL, val) != 0)
1755 fatal("Invalid source-address list");
Damien Millerd0e4a8e2010-05-21 14:58:32 +10001756 certflags_src_addr = xstrdup(val);
Damien Miller0a80ca12010-02-27 07:55:05 +11001757 } else
Damien Miller4e270b02010-04-16 15:56:21 +10001758 fatal("Unsupported certificate option \"%s\"", opt);
Damien Miller0a80ca12010-02-27 07:55:05 +11001759}
1760
Ben Lindstrombba81212001-06-25 05:01:22 +00001761static void
Damien Millerd834d352010-06-26 09:48:02 +10001762show_options(const Buffer *optbuf, int v00, int in_critical)
1763{
1764 u_char *name, *data;
1765 u_int dlen;
1766 Buffer options, option;
1767
1768 buffer_init(&options);
1769 buffer_append(&options, buffer_ptr(optbuf), buffer_len(optbuf));
1770
1771 buffer_init(&option);
1772 while (buffer_len(&options) != 0) {
1773 name = buffer_get_string(&options, NULL);
1774 data = buffer_get_string_ptr(&options, &dlen);
1775 buffer_append(&option, data, dlen);
1776 printf(" %s", name);
1777 if ((v00 || !in_critical) &&
1778 (strcmp(name, "permit-X11-forwarding") == 0 ||
1779 strcmp(name, "permit-agent-forwarding") == 0 ||
1780 strcmp(name, "permit-port-forwarding") == 0 ||
1781 strcmp(name, "permit-pty") == 0 ||
1782 strcmp(name, "permit-user-rc") == 0))
1783 printf("\n");
1784 else if ((v00 || in_critical) &&
1785 (strcmp(name, "force-command") == 0 ||
1786 strcmp(name, "source-address") == 0)) {
1787 data = buffer_get_string(&option, NULL);
1788 printf(" %s\n", data);
1789 xfree(data);
1790 } else {
1791 printf(" UNKNOWN OPTION (len %u)\n",
1792 buffer_len(&option));
1793 buffer_clear(&option);
1794 }
1795 xfree(name);
1796 if (buffer_len(&option) != 0)
1797 fatal("Option corrupt: extra data at end");
1798 }
1799 buffer_free(&option);
1800 buffer_free(&options);
1801}
1802
1803static void
Damien Millerf2b70ca2010-03-05 07:39:35 +11001804do_show_cert(struct passwd *pw)
1805{
1806 Key *key;
1807 struct stat st;
1808 char *key_fp, *ca_fp;
Damien Millerd834d352010-06-26 09:48:02 +10001809 u_int i, v00;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001810
1811 if (!have_identity)
1812 ask_filename(pw, "Enter file in which the key is");
Damien Millerba3420a2010-06-26 09:39:07 +10001813 if (stat(identity_file, &st) < 0)
1814 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
Damien Millerf2b70ca2010-03-05 07:39:35 +11001815 if ((key = key_load_public(identity_file, NULL)) == NULL)
1816 fatal("%s is not a public key", identity_file);
1817 if (!key_is_cert(key))
1818 fatal("%s is not a certificate", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001819 v00 = key->type == KEY_RSA_CERT_V00 || key->type == KEY_DSA_CERT_V00;
1820
Damien Millerf2b70ca2010-03-05 07:39:35 +11001821 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
1822 ca_fp = key_fingerprint(key->cert->signature_key,
1823 SSH_FP_MD5, SSH_FP_HEX);
1824
1825 printf("%s:\n", identity_file);
Damien Miller4e270b02010-04-16 15:56:21 +10001826 printf(" Type: %s %s certificate\n", key_ssh_name(key),
1827 key_cert_type(key));
1828 printf(" Public key: %s %s\n", key_type(key), key_fp);
1829 printf(" Signing CA: %s %s\n",
Damien Millerf2b70ca2010-03-05 07:39:35 +11001830 key_type(key->cert->signature_key), ca_fp);
Damien Miller4e270b02010-04-16 15:56:21 +10001831 printf(" Key ID: \"%s\"\n", key->cert->key_id);
Damien Miller821de0a2011-01-11 17:20:29 +11001832 if (!v00) {
1833 printf(" Serial: %llu\n",
1834 (unsigned long long)key->cert->serial);
1835 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001836 printf(" Valid: %s\n",
1837 fmt_validity(key->cert->valid_after, key->cert->valid_before));
1838 printf(" Principals: ");
1839 if (key->cert->nprincipals == 0)
1840 printf("(none)\n");
1841 else {
1842 for (i = 0; i < key->cert->nprincipals; i++)
1843 printf("\n %s",
1844 key->cert->principals[i]);
1845 printf("\n");
1846 }
Damien Miller4e270b02010-04-16 15:56:21 +10001847 printf(" Critical Options: ");
1848 if (buffer_len(&key->cert->critical) == 0)
Damien Millerf2b70ca2010-03-05 07:39:35 +11001849 printf("(none)\n");
1850 else {
1851 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001852 show_options(&key->cert->critical, v00, 1);
Damien Millerf2b70ca2010-03-05 07:39:35 +11001853 }
Damien Miller4e270b02010-04-16 15:56:21 +10001854 if (!v00) {
1855 printf(" Extensions: ");
1856 if (buffer_len(&key->cert->extensions) == 0)
1857 printf("(none)\n");
1858 else {
1859 printf("\n");
Damien Millerd834d352010-06-26 09:48:02 +10001860 show_options(&key->cert->extensions, v00, 0);
Damien Miller4e270b02010-04-16 15:56:21 +10001861 }
1862 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11001863 exit(0);
1864}
1865
1866static void
Damien Miller431f66b1999-11-21 18:31:57 +11001867usage(void)
1868{
Damien Miller5cbe7ca2007-09-17 16:05:50 +10001869 fprintf(stderr, "usage: %s [options]\n", __progname);
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001870 fprintf(stderr, "Options:\n");
Damien Miller58f1baf2011-05-05 14:06:15 +10001871 fprintf(stderr, " -A Generate non-existent host keys for all key types.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001872 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001873 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001874 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
Ben Lindstrom97be31e2001-08-06 21:49:06 +00001875 fprintf(stderr, " -C comment Provide new comment.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001876 fprintf(stderr, " -c Change comment in private and public key files.\n");
Damien Miller7ea845e2010-02-12 09:21:02 +11001877#ifdef ENABLE_PKCS11
1878 fprintf(stderr, " -D pkcs11 Download public key from pkcs11 token.\n");
1879#endif
Damien Miller44b25042010-07-02 13:35:01 +10001880 fprintf(stderr, " -e Export OpenSSH to foreign format key file.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001881 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
1882 fprintf(stderr, " -f filename Filename of the key file.\n");
1883 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
1884 fprintf(stderr, " -g Use generic DNS resource record format.\n");
1885 fprintf(stderr, " -H Hash names in known_hosts file.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001886 fprintf(stderr, " -h Generate host certificate instead of a user certificate.\n");
1887 fprintf(stderr, " -I key_id Key identifier to include in certificate.\n");
Damien Miller44b25042010-07-02 13:35:01 +10001888 fprintf(stderr, " -i Import foreign format to OpenSSH key file.\n");
Damien Miller927d82b2011-10-18 16:05:38 +11001889 fprintf(stderr, " -K checkpt Write checkpoints to this file.\n");
Damien Millerf2b70ca2010-03-05 07:39:35 +11001890 fprintf(stderr, " -L Print the contents of a certificate.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001891 fprintf(stderr, " -l Show fingerprint of key file.\n");
1892 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001893 fprintf(stderr, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001894 fprintf(stderr, " -N phrase Provide new passphrase.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001895 fprintf(stderr, " -n name,... User/host principal names to include in certificate\n");
Damien Miller1f181422010-04-18 08:08:03 +10001896 fprintf(stderr, " -O option Specify a certificate option.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001897 fprintf(stderr, " -P phrase Provide old passphrase.\n");
1898 fprintf(stderr, " -p Change passphrase of private key file.\n");
1899 fprintf(stderr, " -q Quiet.\n");
1900 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
1901 fprintf(stderr, " -r hostname Print DNS resource record.\n");
1902 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
Damien Miller6022f582010-07-02 13:37:01 +10001903 fprintf(stderr, " -s ca_key Certify keys with CA key.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001904 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1905 fprintf(stderr, " -t type Specify type of key to create.\n");
Damien Miller0a80ca12010-02-27 07:55:05 +11001906 fprintf(stderr, " -V from:to Specify certificate validity interval.\n");
Damien Miller9278ffa2005-05-26 11:59:06 +10001907 fprintf(stderr, " -v Verbose.\n");
1908 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1909 fprintf(stderr, " -y Read private key file and print public key.\n");
Damien Miller1f181422010-04-18 08:08:03 +10001910 fprintf(stderr, " -z serial Specify a serial number.\n");
Darren Tucker019cefe2003-08-02 22:40:07 +10001911
Damien Miller95def091999-11-25 00:26:21 +11001912 exit(1);
Damien Miller431f66b1999-11-21 18:31:57 +11001913}
1914
Damien Miller95def091999-11-25 00:26:21 +11001915/*
1916 * Main program for key management.
1917 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001918int
Damien Millerdf8b7db2007-01-05 16:22:57 +11001919main(int argc, char **argv)
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001920{
Damien Millera41c8b12002-01-22 23:05:08 +11001921 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
Damien Miller390d0562011-10-18 16:05:19 +11001922 char *checkpoint = NULL;
Damien Miller757f34e2010-08-05 13:05:31 +10001923 char out_file[MAXPATHLEN], *rr_hostname = NULL;
Ben Lindstrom5fc62702001-03-09 18:19:24 +00001924 Key *private, *public;
Damien Miller95def091999-11-25 00:26:21 +11001925 struct passwd *pw;
Damien Miller95def091999-11-25 00:26:21 +11001926 struct stat st;
Damien Miller7ea845e2010-02-12 09:21:02 +11001927 int opt, type, fd;
Darren Tucker2db8ae62005-06-01 23:02:25 +10001928 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
Darren Tucker019cefe2003-08-02 22:40:07 +10001929 int do_gen_candidates = 0, do_screen_candidates = 0;
Damien Miller58f1baf2011-05-05 14:06:15 +10001930 int gen_all_hostkeys = 0;
Darren Tucker019cefe2003-08-02 22:40:07 +10001931 BIGNUM *start = NULL;
Damien Miller95def091999-11-25 00:26:21 +11001932 FILE *f;
Damien Miller02e754f2005-05-26 12:19:39 +10001933 const char *errstr;
Damien Miller0bc1bd82000-11-13 22:57:25 +11001934
Damien Miller95def091999-11-25 00:26:21 +11001935 extern int optind;
1936 extern char *optarg;
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001937
Darren Tuckerce321d82005-10-03 18:11:24 +10001938 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1939 sanitise_stdfd();
1940
Darren Tucker9ac56e92007-01-14 10:19:59 +11001941 __progname = ssh_get_progname(argv[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +10001942
Damien Miller4314c2b2010-09-10 11:12:09 +10001943 OpenSSL_add_all_algorithms();
Damien Millerdf8b7db2007-01-05 16:22:57 +11001944 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
Darren Tucker019cefe2003-08-02 22:40:07 +10001945
Kevin Steves3a881912002-07-20 19:05:40 +00001946 seed_rng();
Damien Millereba71ba2000-04-29 23:57:08 +10001947
Damien Miller5428f641999-11-25 11:54:57 +11001948 /* we need this for the home * directory. */
Damien Miller95def091999-11-25 00:26:21 +11001949 pw = getpwuid(getuid());
1950 if (!pw) {
1951 printf("You don't exist, go away!\n");
1952 exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001953 }
Damien Millereba71ba2000-04-29 23:57:08 +10001954 if (gethostname(hostname, sizeof(hostname)) < 0) {
1955 perror("gethostname");
1956 exit(1);
1957 }
Damien Miller5428f641999-11-25 11:54:57 +11001958
Damien Miller390d0562011-10-18 16:05:19 +11001959 while ((opt = getopt(argc, argv, "AegiqpclBHLhvxXyF:b:f:t:D:I:K:P:m:N:n:"
Damien Miller4e270b02010-04-16 15:56:21 +10001960 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
Damien Miller95def091999-11-25 00:26:21 +11001961 switch (opt) {
Damien Miller58f1baf2011-05-05 14:06:15 +10001962 case 'A':
1963 gen_all_hostkeys = 1;
1964 break;
Damien Miller95def091999-11-25 00:26:21 +11001965 case 'b':
Damien Miller57737942010-09-10 11:16:37 +10001966 bits = (u_int32_t)strtonum(optarg, 256, 32768, &errstr);
Damien Miller02e754f2005-05-26 12:19:39 +10001967 if (errstr)
1968 fatal("Bits has bad value %s (%s)",
1969 optarg, errstr);
Damien Miller95def091999-11-25 00:26:21 +11001970 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001971 case 'F':
1972 find_host = 1;
1973 rr_hostname = optarg;
1974 break;
1975 case 'H':
1976 hash_hosts = 1;
1977 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11001978 case 'I':
1979 cert_key_id = optarg;
1980 break;
Damien Miller4b42d7f2005-03-01 21:48:35 +11001981 case 'R':
1982 delete_host = 1;
1983 rr_hostname = optarg;
1984 break;
Damien Millerf2b70ca2010-03-05 07:39:35 +11001985 case 'L':
1986 show_cert = 1;
1987 break;
Damien Miller95def091999-11-25 00:26:21 +11001988 case 'l':
1989 print_fingerprint = 1;
1990 break;
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00001991 case 'B':
1992 print_bubblebabble = 1;
1993 break;
Damien Miller44b25042010-07-02 13:35:01 +10001994 case 'm':
1995 if (strcasecmp(optarg, "RFC4716") == 0 ||
1996 strcasecmp(optarg, "ssh2") == 0) {
1997 convert_format = FMT_RFC4716;
1998 break;
1999 }
2000 if (strcasecmp(optarg, "PKCS8") == 0) {
2001 convert_format = FMT_PKCS8;
2002 break;
2003 }
2004 if (strcasecmp(optarg, "PEM") == 0) {
2005 convert_format = FMT_PEM;
2006 break;
2007 }
2008 fatal("Unsupported conversion format \"%s\"", optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002009 case 'n':
2010 cert_principals = optarg;
2011 break;
Damien Miller95def091999-11-25 00:26:21 +11002012 case 'p':
2013 change_passphrase = 1;
2014 break;
Damien Miller95def091999-11-25 00:26:21 +11002015 case 'c':
2016 change_comment = 1;
2017 break;
Damien Miller95def091999-11-25 00:26:21 +11002018 case 'f':
Damien Millerb089fb52005-05-26 12:16:18 +10002019 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
2020 sizeof(identity_file))
2021 fatal("Identity filename too long");
Damien Miller95def091999-11-25 00:26:21 +11002022 have_identity = 1;
2023 break;
Damien Miller37876e92003-05-15 10:19:46 +10002024 case 'g':
2025 print_generic = 1;
2026 break;
Damien Miller95def091999-11-25 00:26:21 +11002027 case 'P':
2028 identity_passphrase = optarg;
2029 break;
Damien Miller95def091999-11-25 00:26:21 +11002030 case 'N':
2031 identity_new_passphrase = optarg;
2032 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002033 case 'O':
Damien Miller4e270b02010-04-16 15:56:21 +10002034 add_cert_option(optarg);
Damien Miller0a80ca12010-02-27 07:55:05 +11002035 break;
Damien Miller95def091999-11-25 00:26:21 +11002036 case 'C':
2037 identity_comment = optarg;
2038 break;
Damien Miller95def091999-11-25 00:26:21 +11002039 case 'q':
2040 quiet = 1;
2041 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002042 case 'e':
Damien Millereba71ba2000-04-29 23:57:08 +10002043 case 'x':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002044 /* export key */
Damien Miller44b25042010-07-02 13:35:01 +10002045 convert_to = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002046 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002047 case 'h':
2048 cert_key_type = SSH2_CERT_TYPE_HOST;
Damien Millerd0e4a8e2010-05-21 14:58:32 +10002049 certflags_flags = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +11002050 break;
Ben Lindstrom5a707822001-04-22 17:15:46 +00002051 case 'i':
Damien Millereba71ba2000-04-29 23:57:08 +10002052 case 'X':
Ben Lindstrom5a707822001-04-22 17:15:46 +00002053 /* import key */
Damien Miller44b25042010-07-02 13:35:01 +10002054 convert_from = 1;
Damien Millereba71ba2000-04-29 23:57:08 +10002055 break;
Damien Millereba71ba2000-04-29 23:57:08 +10002056 case 'y':
2057 print_public = 1;
2058 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002059 case 's':
2060 ca_key_path = optarg;
2061 break;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002062 case 't':
2063 key_type_name = optarg;
Damien Miller0bc1bd82000-11-13 22:57:25 +11002064 break;
Ben Lindstrom8282d6a2001-08-06 21:44:05 +00002065 case 'D':
Damien Miller7ea845e2010-02-12 09:21:02 +11002066 pkcs11provider = optarg;
Ben Lindstromcd392282001-07-04 03:44:03 +00002067 break;
Darren Tucker06930c72003-12-31 11:34:51 +11002068 case 'v':
2069 if (log_level == SYSLOG_LEVEL_INFO)
2070 log_level = SYSLOG_LEVEL_DEBUG1;
2071 else {
Darren Tuckerfc959702004-07-17 16:12:08 +10002072 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
Darren Tucker06930c72003-12-31 11:34:51 +11002073 log_level < SYSLOG_LEVEL_DEBUG3)
2074 log_level++;
2075 }
2076 break;
Damien Miller37876e92003-05-15 10:19:46 +10002077 case 'r':
Damien Miller4b42d7f2005-03-01 21:48:35 +11002078 rr_hostname = optarg;
Damien Miller37876e92003-05-15 10:19:46 +10002079 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002080 case 'W':
Damien Miller5f340062006-03-26 14:27:57 +11002081 generator_wanted = (u_int32_t)strtonum(optarg, 1,
2082 UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002083 if (errstr)
2084 fatal("Desired generator has bad value: %s (%s)",
2085 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002086 break;
2087 case 'a':
Damien Miller5f340062006-03-26 14:27:57 +11002088 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Millerb089fb52005-05-26 12:16:18 +10002089 if (errstr)
2090 fatal("Invalid number of trials: %s (%s)",
2091 optarg, errstr);
Darren Tucker019cefe2003-08-02 22:40:07 +10002092 break;
2093 case 'M':
Damien Miller5f340062006-03-26 14:27:57 +11002094 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
Damien Miller4e270b02010-04-16 15:56:21 +10002095 if (errstr)
Damien Millerb089fb52005-05-26 12:16:18 +10002096 fatal("Memory limit is %s: %s", errstr, optarg);
Darren Tucker019cefe2003-08-02 22:40:07 +10002097 break;
2098 case 'G':
2099 do_gen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002100 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2101 sizeof(out_file))
2102 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002103 break;
2104 case 'T':
2105 do_screen_candidates = 1;
Damien Millerb089fb52005-05-26 12:16:18 +10002106 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
2107 sizeof(out_file))
2108 fatal("Output filename too long");
Darren Tucker019cefe2003-08-02 22:40:07 +10002109 break;
Damien Miller390d0562011-10-18 16:05:19 +11002110 case 'K':
2111 if (strlen(optarg) >= MAXPATHLEN)
2112 fatal("Checkpoint filename too long");
2113 checkpoint = xstrdup(optarg);
2114 break;
Darren Tucker019cefe2003-08-02 22:40:07 +10002115 case 'S':
2116 /* XXX - also compare length against bits */
2117 if (BN_hex2bn(&start, optarg) == 0)
2118 fatal("Invalid start point.");
2119 break;
Damien Miller0a80ca12010-02-27 07:55:05 +11002120 case 'V':
2121 parse_cert_times(optarg);
2122 break;
Damien Miller4e270b02010-04-16 15:56:21 +10002123 case 'z':
2124 cert_serial = strtonum(optarg, 0, LLONG_MAX, &errstr);
2125 if (errstr)
2126 fatal("Invalid serial number: %s", errstr);
2127 break;
Damien Miller95def091999-11-25 00:26:21 +11002128 case '?':
2129 default:
2130 usage();
2131 }
2132 }
Darren Tucker06930c72003-12-31 11:34:51 +11002133
2134 /* reinit */
Damien Millerdf8b7db2007-01-05 16:22:57 +11002135 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
Darren Tucker06930c72003-12-31 11:34:51 +11002136
Damien Miller0a80ca12010-02-27 07:55:05 +11002137 argv += optind;
2138 argc -= optind;
2139
2140 if (ca_key_path != NULL) {
2141 if (argc < 1) {
2142 printf("Too few arguments.\n");
2143 usage();
2144 }
2145 } else if (argc > 0) {
Damien Miller95def091999-11-25 00:26:21 +11002146 printf("Too many arguments.\n");
2147 usage();
2148 }
2149 if (change_passphrase && change_comment) {
2150 printf("Can only have one of -p and -c.\n");
2151 usage();
2152 }
Darren Tucker0f7e9102008-06-08 12:54:29 +10002153 if (print_fingerprint && (delete_host || hash_hosts)) {
2154 printf("Cannot use -l with -D or -R.\n");
2155 usage();
2156 }
Damien Miller0a80ca12010-02-27 07:55:05 +11002157 if (ca_key_path != NULL) {
2158 if (cert_key_id == NULL)
2159 fatal("Must specify key id (-I) when certifying");
2160 do_ca_sign(pw, argc, argv);
2161 }
Damien Millerf2b70ca2010-03-05 07:39:35 +11002162 if (show_cert)
2163 do_show_cert(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002164 if (delete_host || hash_hosts || find_host)
2165 do_known_hosts(pw, rr_hostname);
Ben Lindstrom8fd372b2001-03-12 03:02:17 +00002166 if (print_fingerprint || print_bubblebabble)
Damien Miller95def091999-11-25 00:26:21 +11002167 do_fingerprint(pw);
Damien Miller95def091999-11-25 00:26:21 +11002168 if (change_passphrase)
2169 do_change_passphrase(pw);
Damien Miller4a10d2e2002-03-11 22:53:29 +11002170 if (change_comment)
2171 do_change_comment(pw);
Damien Miller44b25042010-07-02 13:35:01 +10002172 if (convert_to)
2173 do_convert_to(pw);
2174 if (convert_from)
2175 do_convert_from(pw);
Damien Millereba71ba2000-04-29 23:57:08 +10002176 if (print_public)
2177 do_print_public(pw);
Damien Miller4b42d7f2005-03-01 21:48:35 +11002178 if (rr_hostname != NULL) {
Damien Millercb314822006-03-26 13:48:01 +11002179 unsigned int n = 0;
2180
2181 if (have_identity) {
2182 n = do_print_resource_record(pw,
2183 identity_file, rr_hostname);
2184 if (n == 0) {
2185 perror(identity_file);
2186 exit(1);
2187 }
2188 exit(0);
2189 } else {
2190
2191 n += do_print_resource_record(pw,
2192 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
2193 n += do_print_resource_record(pw,
2194 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
2195
2196 if (n == 0)
2197 fatal("no keys found.");
2198 exit(0);
2199 }
Damien Miller37876e92003-05-15 10:19:46 +10002200 }
Damien Miller7ea845e2010-02-12 09:21:02 +11002201 if (pkcs11provider != NULL)
Damien Miller757f34e2010-08-05 13:05:31 +10002202 do_download(pw);
Damien Miller95def091999-11-25 00:26:21 +11002203
Darren Tucker019cefe2003-08-02 22:40:07 +10002204 if (do_gen_candidates) {
2205 FILE *out = fopen(out_file, "w");
Damien Miller787b2ec2003-11-21 23:56:47 +11002206
Darren Tucker019cefe2003-08-02 22:40:07 +10002207 if (out == NULL) {
2208 error("Couldn't open modulus candidate file \"%s\": %s",
2209 out_file, strerror(errno));
2210 return (1);
2211 }
Damien Miller3f54a9f2005-11-05 14:52:18 +11002212 if (bits == 0)
2213 bits = DEFAULT_BITS;
Darren Tucker019cefe2003-08-02 22:40:07 +10002214 if (gen_candidates(out, memory, bits, start) != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002215 fatal("modulus candidate generation failed");
Darren Tucker019cefe2003-08-02 22:40:07 +10002216
2217 return (0);
2218 }
2219
2220 if (do_screen_candidates) {
2221 FILE *in;
2222 FILE *out = fopen(out_file, "w");
2223
2224 if (have_identity && strcmp(identity_file, "-") != 0) {
2225 if ((in = fopen(identity_file, "r")) == NULL) {
2226 fatal("Couldn't open modulus candidate "
Damien Millera8e06ce2003-11-21 23:48:55 +11002227 "file \"%s\": %s", identity_file,
Darren Tucker019cefe2003-08-02 22:40:07 +10002228 strerror(errno));
2229 }
2230 } else
2231 in = stdin;
2232
2233 if (out == NULL) {
2234 fatal("Couldn't open moduli file \"%s\": %s",
2235 out_file, strerror(errno));
2236 }
Damien Miller390d0562011-10-18 16:05:19 +11002237 if (prime_test(in, out, trials, generator_wanted, checkpoint)
2238 != 0)
Damien Miller15d72a02005-11-05 15:07:33 +11002239 fatal("modulus screening failed");
Darren Tuckerf4220e62003-08-21 16:44:07 +10002240 return (0);
Darren Tucker019cefe2003-08-02 22:40:07 +10002241 }
2242
Damien Miller58f1baf2011-05-05 14:06:15 +10002243 if (gen_all_hostkeys) {
2244 do_gen_all_hostkeys(pw);
2245 return (0);
2246 }
2247
Damien Miller95def091999-11-25 00:26:21 +11002248 arc4random_stir();
2249
Damien Millerf14be5c2005-11-05 15:15:49 +11002250 if (key_type_name == NULL)
2251 key_type_name = "rsa";
2252
Damien Millere39cacc2000-11-29 12:18:44 +11002253 type = key_type_from_name(key_type_name);
Damien Miller58f1baf2011-05-05 14:06:15 +10002254 type_bits_valid(type, &bits);
2255
Darren Tucker3af2ac52005-11-29 13:10:24 +11002256 if (!quiet)
2257 printf("Generating public/private %s key pair.\n", key_type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +11002258 private = key_generate(type, bits);
2259 if (private == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002260 fprintf(stderr, "key_generate failed\n");
Damien Miller0bc1bd82000-11-13 22:57:25 +11002261 exit(1);
2262 }
2263 public = key_from_private(private);
Damien Miller95def091999-11-25 00:26:21 +11002264
2265 if (!have_identity)
2266 ask_filename(pw, "Enter file in which to save the key");
2267
Damien Miller788f2122005-11-05 15:14:59 +11002268 /* Create ~/.ssh directory if it doesn't already exist. */
Damien Miller50af79b2010-05-10 11:52:00 +10002269 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s",
2270 pw->pw_dir, _PATH_SSH_USER_DIR);
2271 if (strstr(identity_file, dotsshdir) != NULL) {
2272 if (stat(dotsshdir, &st) < 0) {
2273 if (errno != ENOENT) {
2274 error("Could not stat %s: %s", dotsshdir,
2275 strerror(errno));
2276 } else if (mkdir(dotsshdir, 0700) < 0) {
2277 error("Could not create directory '%s': %s",
2278 dotsshdir, strerror(errno));
2279 } else if (!quiet)
2280 printf("Created directory '%s'.\n", dotsshdir);
2281 }
Damien Miller95def091999-11-25 00:26:21 +11002282 }
2283 /* If the file already exists, ask the user to confirm. */
2284 if (stat(identity_file, &st) >= 0) {
2285 char yesno[3];
2286 printf("%s already exists.\n", identity_file);
2287 printf("Overwrite (y/n)? ");
2288 fflush(stdout);
2289 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
2290 exit(1);
2291 if (yesno[0] != 'y' && yesno[0] != 'Y')
2292 exit(1);
2293 }
2294 /* Ask for a passphrase (twice). */
2295 if (identity_passphrase)
2296 passphrase1 = xstrdup(identity_passphrase);
2297 else if (identity_new_passphrase)
2298 passphrase1 = xstrdup(identity_new_passphrase);
2299 else {
2300passphrase_again:
2301 passphrase1 =
Ben Lindstrom949974b2001-06-25 05:20:31 +00002302 read_passphrase("Enter passphrase (empty for no "
2303 "passphrase): ", RP_ALLOW_STDIN);
2304 passphrase2 = read_passphrase("Enter same passphrase again: ",
2305 RP_ALLOW_STDIN);
Damien Miller95def091999-11-25 00:26:21 +11002306 if (strcmp(passphrase1, passphrase2) != 0) {
Ben Lindstrom949974b2001-06-25 05:20:31 +00002307 /*
2308 * The passphrases do not match. Clear them and
2309 * retry.
2310 */
Damien Miller95def091999-11-25 00:26:21 +11002311 memset(passphrase1, 0, strlen(passphrase1));
2312 memset(passphrase2, 0, strlen(passphrase2));
2313 xfree(passphrase1);
2314 xfree(passphrase2);
2315 printf("Passphrases do not match. Try again.\n");
2316 goto passphrase_again;
2317 }
2318 /* Clear the other copy of the passphrase. */
2319 memset(passphrase2, 0, strlen(passphrase2));
2320 xfree(passphrase2);
2321 }
2322
Damien Miller95def091999-11-25 00:26:21 +11002323 if (identity_comment) {
2324 strlcpy(comment, identity_comment, sizeof(comment));
2325 } else {
Darren Tuckere15fb092008-11-11 16:31:43 +11002326 /* Create default comment field for the passphrase. */
Damien Miller95def091999-11-25 00:26:21 +11002327 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
2328 }
2329
2330 /* Save the key with the given passphrase and comment. */
Ben Lindstromd0fca422001-03-26 13:44:06 +00002331 if (!key_save_private(private, identity_file, passphrase1, comment)) {
Ben Lindstrom15f33862001-04-16 02:00:02 +00002332 printf("Saving the key failed: %s.\n", identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002333 memset(passphrase1, 0, strlen(passphrase1));
2334 xfree(passphrase1);
2335 exit(1);
2336 }
2337 /* Clear the passphrase. */
2338 memset(passphrase1, 0, strlen(passphrase1));
2339 xfree(passphrase1);
2340
2341 /* Clear the private key and the random number generator. */
Damien Miller0bc1bd82000-11-13 22:57:25 +11002342 key_free(private);
Damien Miller95def091999-11-25 00:26:21 +11002343 arc4random_stir();
2344
2345 if (!quiet)
2346 printf("Your identification has been saved in %s.\n", identity_file);
2347
Damien Miller95def091999-11-25 00:26:21 +11002348 strlcat(identity_file, ".pub", sizeof(identity_file));
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002349 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
2350 if (fd == -1) {
Damien Miller95def091999-11-25 00:26:21 +11002351 printf("Could not save your public key in %s\n", identity_file);
2352 exit(1);
2353 }
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002354 f = fdopen(fd, "w");
2355 if (f == NULL) {
Damien Miller9eab9562009-02-22 08:47:02 +11002356 printf("fdopen %s failed\n", identity_file);
Ben Lindstrom5fc62702001-03-09 18:19:24 +00002357 exit(1);
2358 }
Damien Millereba71ba2000-04-29 23:57:08 +10002359 if (!key_write(public, f))
Damien Miller9eab9562009-02-22 08:47:02 +11002360 fprintf(stderr, "write key failed\n");
Damien Millereba71ba2000-04-29 23:57:08 +10002361 fprintf(f, " %s\n", comment);
Damien Miller95def091999-11-25 00:26:21 +11002362 fclose(f);
2363
2364 if (!quiet) {
Ben Lindstromcfccef92001-03-13 04:57:58 +00002365 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002366 char *ra = key_fingerprint(public, SSH_FP_MD5,
2367 SSH_FP_RANDOMART);
Damien Millereba71ba2000-04-29 23:57:08 +10002368 printf("Your public key has been saved in %s.\n",
2369 identity_file);
Damien Miller95def091999-11-25 00:26:21 +11002370 printf("The key fingerprint is:\n");
Ben Lindstromcfccef92001-03-13 04:57:58 +00002371 printf("%s %s\n", fp, comment);
Darren Tucker9c16ac92008-06-13 04:40:35 +10002372 printf("The key's randomart image is:\n");
2373 printf("%s\n", ra);
2374 xfree(ra);
Ben Lindstromcfccef92001-03-13 04:57:58 +00002375 xfree(fp);
Damien Miller95def091999-11-25 00:26:21 +11002376 }
Damien Millereba71ba2000-04-29 23:57:08 +10002377
2378 key_free(public);
Damien Miller95def091999-11-25 00:26:21 +11002379 exit(0);
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002380}