]> andersk Git - openssh.git/blame - ssh-keygen.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / ssh-keygen.c
CommitLineData
c23cfd0d 1/* $OpenBSD: ssh-keygen.c,v 1.176 2010/01/11 10:51:07 djm Exp $ */
8efc0c15 2/*
5260325f 3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
5260325f 6 * Identity and host key generation and maintenance.
bcbf86ec 7 *
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".
5260325f 13 */
8efc0c15 14
15#include "includes.h"
4095f623 16
17#include <sys/types.h>
5b04a8bf 18#include <sys/socket.h>
4095f623 19#include <sys/stat.h>
536c14e8 20#include <sys/param.h>
8efc0c15 21
a306f2dd 22#include <openssl/evp.h>
23#include <openssl/pem.h>
f5f25d17 24#include "openbsd-compat/openssl-compat.h"
a306f2dd 25
028094f4 26#include <errno.h>
d3221cca 27#include <fcntl.h>
28cb0a43 28#include <netdb.h>
42685763 29#ifdef HAVE_PATHS_H
30# include <paths.h>
31#endif
b1842393 32#include <pwd.h>
24436b92 33#include <stdarg.h>
cf851879 34#include <stdio.h>
ffa517a8 35#include <stdlib.h>
00146caa 36#include <string.h>
5188ba17 37#include <unistd.h>
b1842393 38
8efc0c15 39#include "xmalloc.h"
a306f2dd 40#include "key.h"
457fc0c6 41#include "rsa.h"
a306f2dd 42#include "authfile.h"
43#include "uuencode.h"
94ec8c6b 44#include "buffer.h"
42f11eb2 45#include "pathnames.h"
46#include "log.h"
58ae9cb8 47#include "misc.h"
bdffbcdc 48#include "match.h"
49#include "hostfile.h"
0cbe25f0 50#include "dns.h"
94ec8c6b 51
93a56445 52#ifdef SMARTCARD
93a56445 53#include "scard.h"
dd2495cb 54#endif
2b30154a 55
255d3e00 56/* Number of bits in the RSA/DSA key. This value can be set on the command line. */
57#define DEFAULT_BITS 2048
58#define DEFAULT_BITS_DSA 1024
59u_int32_t bits = 0;
8efc0c15 60
aa3378df 61/*
62 * Flag indicating that we just want to change the passphrase. This can be
63 * set on the command line.
64 */
8efc0c15 65int change_passphrase = 0;
66
aa3378df 67/*
68 * Flag indicating that we just want to change the comment. This can be set
69 * on the command line.
70 */
8efc0c15 71int change_comment = 0;
72
73int quiet = 0;
74
639211b7 75int log_level = SYSLOG_LEVEL_INFO;
76
bdffbcdc 77/* Flag indicating that we want to hash a known_hosts file */
78int hash_hosts = 0;
79/* Flag indicating that we want lookup a host in known_hosts file */
80int find_host = 0;
81/* Flag indicating that we want to delete a host from a known_hosts file */
82int delete_host = 0;
83
f095fcc7 84/* Flag indicating that we just want to see the key fingerprint */
85int print_fingerprint = 0;
aaf45d87 86int print_bubblebabble = 0;
f095fcc7 87
5ad13cd7 88/* The identity file name, given on the command line or entered by the user. */
89char identity_file[1024];
90int have_identity = 0;
8efc0c15 91
92/* This is set to the passphrase if given on the command line. */
93char *identity_passphrase = NULL;
94
95/* This is set to the new passphrase if given on the command line. */
96char *identity_new_passphrase = NULL;
97
98/* This is set to the new comment if given on the command line. */
99char *identity_comment = NULL;
100
a306f2dd 101/* Dump public key file in format used by real and the original SSH 2 */
102int convert_to_ssh2 = 0;
103int convert_from_ssh2 = 0;
104int print_public = 0;
21289cd0 105int print_generic = 0;
fa08c86b 106
3dc93cd8 107char *key_type_name = NULL;
a306f2dd 108
5ad13cd7 109/* argv0 */
110extern char *__progname;
8efc0c15 111
a306f2dd 112char hostname[MAXHOSTNAMELEN];
113
20eea1d7 114/* moduli.c */
c784ae09 115int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
20eea1d7 116int prime_test(FILE *, FILE *, u_int32_t, u_int32_t);
117
396c147e 118static void
5ad13cd7 119ask_filename(struct passwd *pw, const char *prompt)
8efc0c15 120{
5260325f 121 char buf[1024];
c523303b 122 char *name = NULL;
123
8d22d775 124 if (key_type_name == NULL)
42f11eb2 125 name = _PATH_SSH_CLIENT_ID_RSA;
32596c7b 126 else {
8d22d775 127 switch (key_type_from_name(key_type_name)) {
128 case KEY_RSA1:
129 name = _PATH_SSH_CLIENT_IDENTITY;
130 break;
131 case KEY_DSA:
132 name = _PATH_SSH_CLIENT_ID_DSA;
133 break;
134 case KEY_RSA:
135 name = _PATH_SSH_CLIENT_ID_RSA;
136 break;
137 default:
7eec82ab 138 fprintf(stderr, "bad key type\n");
8d22d775 139 exit(1);
140 break;
141 }
32596c7b 142 }
c523303b 143 snprintf(identity_file, sizeof(identity_file), "%s/%s", pw->pw_dir, name);
8d88011e 144 fprintf(stderr, "%s (%s): ", prompt, identity_file);
5260325f 145 if (fgets(buf, sizeof(buf), stdin) == NULL)
146 exit(1);
b05117ac 147 buf[strcspn(buf, "\n")] = '\0';
5260325f 148 if (strcmp(buf, "") != 0)
149 strlcpy(identity_file, buf, sizeof(identity_file));
150 have_identity = 1;
f095fcc7 151}
8efc0c15 152
396c147e 153static Key *
abe0fb9f 154load_identity(char *filename)
a306f2dd 155{
aeaa3d9e 156 char *pass;
157 Key *prv;
158
cd332296 159 prv = key_load_private(filename, "", NULL);
aeaa3d9e 160 if (prv == NULL) {
abe0fb9f 161 if (identity_passphrase)
162 pass = xstrdup(identity_passphrase);
163 else
1843a425 164 pass = read_passphrase("Enter passphrase: ",
165 RP_ALLOW_STDIN);
aeaa3d9e 166 prv = key_load_private(filename, pass, NULL);
a306f2dd 167 memset(pass, 0, strlen(pass));
168 xfree(pass);
169 }
aeaa3d9e 170 return prv;
a306f2dd 171}
172
94ec8c6b 173#define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
7203d6bb 174#define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
94ec8c6b 175#define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
2b87da3b 176#define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
a306f2dd 177
396c147e 178static void
a306f2dd 179do_convert_to_ssh2(struct passwd *pw)
180{
b587c165 181 Key *k;
21b30f38 182 u_int len;
1e3b8b07 183 u_char *blob;
c23cfd0d 184 char comment[61];
a306f2dd 185 struct stat st;
186
187 if (!have_identity)
188 ask_filename(pw, "Enter file in which the key is");
189 if (stat(identity_file, &st) < 0) {
190 perror(identity_file);
191 exit(1);
192 }
b587c165 193 if ((k = key_load_public(identity_file, NULL)) == NULL) {
abe0fb9f 194 if ((k = load_identity(identity_file)) == NULL) {
b587c165 195 fprintf(stderr, "load failed\n");
196 exit(1);
197 }
a306f2dd 198 }
3566c73c 199 if (k->type == KEY_RSA1) {
200 fprintf(stderr, "version 1 keys are not supported\n");
201 exit(1);
202 }
f7436b8c 203 if (key_to_blob(k, &blob, &len) <= 0) {
204 fprintf(stderr, "key_to_blob failed\n");
205 exit(1);
206 }
c23cfd0d 207 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
208 snprintf(comment, sizeof(comment),
209 "%u-bit %s, converted by %s@%s from OpenSSH",
b587c165 210 key_size(k), key_type(k),
a306f2dd 211 pw->pw_name, hostname);
c23cfd0d 212
213 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
214 fprintf(stdout, "Comment: \"%s\"\n", comment);
a306f2dd 215 dump_base64(stdout, blob, len);
94ec8c6b 216 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
b587c165 217 key_free(k);
a306f2dd 218 xfree(blob);
219 exit(0);
220}
221
396c147e 222static void
94ec8c6b 223buffer_get_bignum_bits(Buffer *b, BIGNUM *value)
224{
ca75d7de 225 u_int bignum_bits = buffer_get_int(b);
226 u_int bytes = (bignum_bits + 7) / 8;
457fc0c6 227
94ec8c6b 228 if (buffer_len(b) < bytes)
457fc0c6 229 fatal("buffer_get_bignum_bits: input buffer too small: "
230 "need %d have %d", bytes, buffer_len(b));
e516451d 231 if (BN_bin2bn(buffer_ptr(b), bytes, value) == NULL)
232 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
94ec8c6b 233 buffer_consume(b, bytes);
234}
235
396c147e 236static Key *
c66f9d0e 237do_convert_private_ssh2_from_blob(u_char *blob, u_int blen)
94ec8c6b 238{
239 Buffer b;
94ec8c6b 240 Key *key = NULL;
a599bd06 241 char *type, *cipher;
d3260e12 242 u_char *sig, data[] = "abcde12345";
2e0becb6 243 int magic, rlen, ktype, i1, i2, i3, i4;
a599bd06 244 u_int slen;
2e0becb6 245 u_long e;
94ec8c6b 246
247 buffer_init(&b);
248 buffer_append(&b, blob, blen);
249
803c4226 250 magic = buffer_get_int(&b);
94ec8c6b 251 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
252 error("bad magic 0x%x != 0x%x", magic, SSH_COM_PRIVATE_KEY_MAGIC);
253 buffer_free(&b);
254 return NULL;
255 }
2e0becb6 256 i1 = buffer_get_int(&b);
94ec8c6b 257 type = buffer_get_string(&b, NULL);
258 cipher = buffer_get_string(&b, NULL);
2e0becb6 259 i2 = buffer_get_int(&b);
260 i3 = buffer_get_int(&b);
261 i4 = buffer_get_int(&b);
4f6e2ba9 262 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
94ec8c6b 263 if (strcmp(cipher, "none") != 0) {
264 error("unsupported cipher %s", cipher);
265 xfree(cipher);
266 buffer_free(&b);
457fc0c6 267 xfree(type);
94ec8c6b 268 return NULL;
269 }
270 xfree(cipher);
271
457fc0c6 272 if (strstr(type, "dsa")) {
273 ktype = KEY_DSA;
274 } else if (strstr(type, "rsa")) {
275 ktype = KEY_RSA;
276 } else {
3c460ede 277 buffer_free(&b);
457fc0c6 278 xfree(type);
94ec8c6b 279 return NULL;
280 }
457fc0c6 281 key = key_new_private(ktype);
282 xfree(type);
283
284 switch (key->type) {
285 case KEY_DSA:
286 buffer_get_bignum_bits(&b, key->dsa->p);
287 buffer_get_bignum_bits(&b, key->dsa->g);
288 buffer_get_bignum_bits(&b, key->dsa->q);
289 buffer_get_bignum_bits(&b, key->dsa->pub_key);
290 buffer_get_bignum_bits(&b, key->dsa->priv_key);
291 break;
292 case KEY_RSA:
803c4226 293 e = buffer_get_char(&b);
2e0becb6 294 debug("e %lx", e);
295 if (e < 30) {
296 e <<= 8;
297 e += buffer_get_char(&b);
298 debug("e %lx", e);
299 e <<= 8;
300 e += buffer_get_char(&b);
301 debug("e %lx", e);
302 }
303 if (!BN_set_word(key->rsa->e, e)) {
457fc0c6 304 buffer_free(&b);
305 key_free(key);
306 return NULL;
307 }
308 buffer_get_bignum_bits(&b, key->rsa->d);
309 buffer_get_bignum_bits(&b, key->rsa->n);
310 buffer_get_bignum_bits(&b, key->rsa->iqmp);
311 buffer_get_bignum_bits(&b, key->rsa->q);
312 buffer_get_bignum_bits(&b, key->rsa->p);
2b63e803 313 rsa_generate_additional_parameters(key->rsa);
457fc0c6 314 break;
315 }
94ec8c6b 316 rlen = buffer_len(&b);
6aacefa7 317 if (rlen != 0)
457fc0c6 318 error("do_convert_private_ssh2_from_blob: "
319 "remaining bytes in key blob %d", rlen);
94ec8c6b 320 buffer_free(&b);
a599bd06 321
322 /* try the key */
323 key_sign(key, &sig, &slen, data, sizeof(data));
324 key_verify(key, sig, slen, data, sizeof(data));
325 xfree(sig);
94ec8c6b 326 return key;
327}
328
15b81af3 329static int
330get_line(FILE *fp, char *line, size_t len)
331{
332 int c;
333 size_t pos = 0;
334
335 line[0] = '\0';
336 while ((c = fgetc(fp)) != EOF) {
337 if (pos >= len - 1) {
338 fprintf(stderr, "input line too long.\n");
339 exit(1);
340 }
32596c7b 341 switch (c) {
15b81af3 342 case '\r':
343 c = fgetc(fp);
344 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF) {
345 fprintf(stderr, "unget: %s\n", strerror(errno));
346 exit(1);
347 }
348 return pos;
349 case '\n':
350 return pos;
351 }
352 line[pos++] = c;
353 line[pos] = '\0';
354 }
cbaa43c0 355 /* We reached EOF */
356 return -1;
15b81af3 357}
358
396c147e 359static void
a306f2dd 360do_convert_from_ssh2(struct passwd *pw)
361{
362 Key *k;
363 int blen;
783dbbdc 364 u_int len;
15b81af3 365 char line[1024];
cf54363d 366 u_char blob[8096];
a306f2dd 367 char encoded[8096];
368 struct stat st;
94ec8c6b 369 int escaped = 0, private = 0, ok;
a306f2dd 370 FILE *fp;
371
372 if (!have_identity)
373 ask_filename(pw, "Enter file in which the key is");
374 if (stat(identity_file, &st) < 0) {
375 perror(identity_file);
376 exit(1);
377 }
378 fp = fopen(identity_file, "r");
379 if (fp == NULL) {
380 perror(identity_file);
381 exit(1);
382 }
383 encoded[0] = '\0';
15b81af3 384 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
385 if (line[blen - 1] == '\\')
d0c832f3 386 escaped++;
a306f2dd 387 if (strncmp(line, "----", 4) == 0 ||
388 strstr(line, ": ") != NULL) {
94ec8c6b 389 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
390 private = 1;
a599bd06 391 if (strstr(line, " END ") != NULL) {
392 break;
393 }
012bc0e1 394 /* fprintf(stderr, "ignore: %s", line); */
a306f2dd 395 continue;
396 }
d0c832f3 397 if (escaped) {
398 escaped--;
012bc0e1 399 /* fprintf(stderr, "escaped: %s", line); */
d0c832f3 400 continue;
a306f2dd 401 }
a306f2dd 402 strlcat(encoded, line, sizeof(encoded));
403 }
783dbbdc 404 len = strlen(encoded);
405 if (((len % 4) == 3) &&
406 (encoded[len-1] == '=') &&
407 (encoded[len-2] == '=') &&
408 (encoded[len-3] == '='))
409 encoded[len-3] = '\0';
e6207598 410 blen = uudecode(encoded, blob, sizeof(blob));
a306f2dd 411 if (blen < 0) {
412 fprintf(stderr, "uudecode failed.\n");
413 exit(1);
414 }
94ec8c6b 415 k = private ?
416 do_convert_private_ssh2_from_blob(blob, blen) :
fa08c86b 417 key_from_blob(blob, blen);
94ec8c6b 418 if (k == NULL) {
419 fprintf(stderr, "decode blob failed.\n");
420 exit(1);
421 }
422 ok = private ?
457fc0c6 423 (k->type == KEY_DSA ?
424 PEM_write_DSAPrivateKey(stdout, k->dsa, NULL, NULL, 0, NULL, NULL) :
425 PEM_write_RSAPrivateKey(stdout, k->rsa, NULL, NULL, 0, NULL, NULL)) :
94ec8c6b 426 key_write(k, stdout);
427 if (!ok) {
7eec82ab 428 fprintf(stderr, "key write failed\n");
94ec8c6b 429 exit(1);
430 }
a306f2dd 431 key_free(k);
4ccb828d 432 if (!private)
433 fprintf(stdout, "\n");
a306f2dd 434 fclose(fp);
435 exit(0);
436}
437
396c147e 438static void
a306f2dd 439do_print_public(struct passwd *pw)
440{
aeaa3d9e 441 Key *prv;
a306f2dd 442 struct stat st;
443
444 if (!have_identity)
445 ask_filename(pw, "Enter file in which the key is");
446 if (stat(identity_file, &st) < 0) {
447 perror(identity_file);
448 exit(1);
449 }
abe0fb9f 450 prv = load_identity(identity_file);
aeaa3d9e 451 if (prv == NULL) {
a306f2dd 452 fprintf(stderr, "load failed\n");
453 exit(1);
454 }
aeaa3d9e 455 if (!key_write(prv, stdout))
a306f2dd 456 fprintf(stderr, "key_write failed");
aeaa3d9e 457 key_free(prv);
a306f2dd 458 fprintf(stdout, "\n");
459 exit(0);
460}
461
4f831fd7 462#ifdef SMARTCARD
2b30154a 463static void
93a56445 464do_upload(struct passwd *pw, const char *sc_reader_id)
2b30154a 465{
2b30154a 466 Key *prv = NULL;
467 struct stat st;
b9f62352 468 int ret;
469
2b30154a 470 if (!have_identity)
471 ask_filename(pw, "Enter file in which the key is");
472 if (stat(identity_file, &st) < 0) {
473 perror(identity_file);
b9f62352 474 exit(1);
2b30154a 475 }
476 prv = load_identity(identity_file);
477 if (prv == NULL) {
478 error("load failed");
b9f62352 479 exit(1);
49ccba9c 480 }
b9f62352 481 ret = sc_put_key(prv, sc_reader_id);
482 key_free(prv);
483 if (ret < 0)
484 exit(1);
bbe88b6d 485 logit("loading key done");
b9f62352 486 exit(0);
2b30154a 487}
93a56445 488
489static void
490do_download(struct passwd *pw, const char *sc_reader_id)
491{
0659cace 492 Key **keys = NULL;
493 int i;
93a56445 494
0659cace 495 keys = sc_get_keys(sc_reader_id, NULL);
496 if (keys == NULL)
93a56445 497 fatal("cannot read public key from smartcard");
0659cace 498 for (i = 0; keys[i]; i++) {
499 key_write(keys[i], stdout);
500 key_free(keys[i]);
501 fprintf(stdout, "\n");
502 }
503 xfree(keys);
93a56445 504 exit(0);
505}
3e984472 506#endif /* SMARTCARD */
2b30154a 507
396c147e 508static void
f095fcc7 509do_fingerprint(struct passwd *pw)
510{
c8d54615 511 FILE *f;
a306f2dd 512 Key *public;
aff73c5f 513 char *comment = NULL, *cp, *ep, line[16*1024], *fp, *ra;
f8f89bae 514 int i, skip = 0, num = 0, invalid = 1;
17a3011c 515 enum fp_rep rep;
516 enum fp_type fptype;
5260325f 517 struct stat st;
518
aeaa3d9e 519 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
520 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
aaf45d87 521
5260325f 522 if (!have_identity)
523 ask_filename(pw, "Enter file in which the key is");
524 if (stat(identity_file, &st) < 0) {
525 perror(identity_file);
526 exit(1);
527 }
aeaa3d9e 528 public = key_load_public(identity_file, &comment);
529 if (public != NULL) {
530 fp = key_fingerprint(public, fptype, rep);
bf3290be 531 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
31de76cc 532 printf("%u %s %s (%s)\n", key_size(public), fp, comment,
533 key_type(public));
639211b7 534 if (log_level >= SYSLOG_LEVEL_VERBOSE)
535 printf("%s\n", ra);
a306f2dd 536 key_free(public);
fa08c86b 537 xfree(comment);
aff73c5f 538 xfree(ra);
aaf45d87 539 xfree(fp);
c8d54615 540 exit(0);
541 }
2b8dc5e3 542 if (comment) {
aeaa3d9e 543 xfree(comment);
2b8dc5e3 544 comment = NULL;
545 }
c8d54615 546
547 f = fopen(identity_file, "r");
548 if (f != NULL) {
c8d54615 549 while (fgets(line, sizeof(line), f)) {
3e2d9453 550 if ((cp = strchr(line, '\n')) == NULL) {
f8f89bae 551 error("line %d too long: %.40s...",
552 num + 1, line);
c8d54615 553 skip = 1;
554 continue;
555 }
556 num++;
557 if (skip) {
558 skip = 0;
559 continue;
560 }
3e2d9453 561 *cp = '\0';
c8d54615 562
563 /* Skip leading whitespace, empty and comment lines. */
564 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
565 ;
566 if (!*cp || *cp == '\n' || *cp == '#')
4f6e2ba9 567 continue;
c8d54615 568 i = strtol(cp, &ep, 10);
569 if (i == 0 || ep == NULL || (*ep != ' ' && *ep != '\t')) {
570 int quoted = 0;
571 comment = cp;
512df038 572 for (; *cp && (quoted || (*cp != ' ' &&
573 *cp != '\t')); cp++) {
c8d54615 574 if (*cp == '\\' && cp[1] == '"')
575 cp++; /* Skip both */
576 else if (*cp == '"')
577 quoted = !quoted;
578 }
579 if (!*cp)
580 continue;
581 *cp++ = '\0';
582 }
583 ep = cp;
efcae5b1 584 public = key_new(KEY_RSA1);
585 if (key_read(public, &cp) != 1) {
586 cp = ep;
587 key_free(public);
588 public = key_new(KEY_UNSPEC);
589 if (key_read(public, &cp) != 1) {
590 key_free(public);
591 continue;
592 }
5260325f 593 }
efcae5b1 594 comment = *cp ? cp : comment;
aeaa3d9e 595 fp = key_fingerprint(public, fptype, rep);
bf3290be 596 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
31de76cc 597 printf("%u %s %s (%s)\n", key_size(public), fp,
598 comment ? comment : "no comment", key_type(public));
639211b7 599 if (log_level >= SYSLOG_LEVEL_VERBOSE)
600 printf("%s\n", ra);
aff73c5f 601 xfree(ra);
aaf45d87 602 xfree(fp);
aeaa3d9e 603 key_free(public);
efcae5b1 604 invalid = 0;
5260325f 605 }
c8d54615 606 fclose(f);
607 }
608 if (invalid) {
f564d016 609 printf("%s is not a public key file.\n", identity_file);
c8d54615 610 exit(1);
5260325f 611 }
5260325f 612 exit(0);
f095fcc7 613}
614
bdffbcdc 615static void
ccc6beea 616print_host(FILE *f, const char *name, Key *public, int hash)
bdffbcdc 617{
048acbeb 618 if (print_fingerprint) {
619 enum fp_rep rep;
620 enum fp_type fptype;
aff73c5f 621 char *fp, *ra;
048acbeb 622
623 fptype = print_bubblebabble ? SSH_FP_SHA1 : SSH_FP_MD5;
624 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_HEX;
625 fp = key_fingerprint(public, fptype, rep);
bf3290be 626 ra = key_fingerprint(public, SSH_FP_MD5, SSH_FP_RANDOMART);
322b3f02 627 printf("%u %s %s (%s)\n", key_size(public), fp, name,
628 key_type(public));
629 if (log_level >= SYSLOG_LEVEL_VERBOSE)
630 printf("%s\n", ra);
aff73c5f 631 xfree(ra);
048acbeb 632 xfree(fp);
633 } else {
634 if (hash && (name = host_hash(name, NULL, 0)) == NULL)
635 fatal("hash_host failed");
636 fprintf(f, "%s ", name);
637 if (!key_write(public, f))
638 fatal("key_write failed");
639 fprintf(f, "\n");
640 }
bdffbcdc 641}
642
643static void
644do_known_hosts(struct passwd *pw, const char *name)
645{
646 FILE *in, *out = stdout;
647 Key *public;
648 char *cp, *cp2, *kp, *kp2;
649 char line[16*1024], tmp[MAXPATHLEN], old[MAXPATHLEN];
f8f89bae 650 int c, skip = 0, inplace = 0, num = 0, invalid = 0, has_unhashed = 0;
bdffbcdc 651
652 if (!have_identity) {
653 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
654 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
655 sizeof(identity_file))
656 fatal("Specified known hosts path too long");
657 xfree(cp);
658 have_identity = 1;
659 }
660 if ((in = fopen(identity_file, "r")) == NULL)
661 fatal("fopen: %s", strerror(errno));
662
663 /*
664 * Find hosts goes to stdout, hash and deletions happen in-place
665 * A corner case is ssh-keygen -HF foo, which should go to stdout
666 */
667 if (!find_host && (hash_hosts || delete_host)) {
668 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
669 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
670 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
671 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
672 fatal("known_hosts path too long");
673 umask(077);
674 if ((c = mkstemp(tmp)) == -1)
675 fatal("mkstemp: %s", strerror(errno));
676 if ((out = fdopen(c, "w")) == NULL) {
677 c = errno;
678 unlink(tmp);
679 fatal("fdopen: %s", strerror(c));
680 }
681 inplace = 1;
682 }
683
684 while (fgets(line, sizeof(line), in)) {
3e2d9453 685 if ((cp = strchr(line, '\n')) == NULL) {
f8f89bae 686 error("line %d too long: %.40s...", num + 1, line);
bdffbcdc 687 skip = 1;
688 invalid = 1;
689 continue;
690 }
3e2d9453 691 num++;
bdffbcdc 692 if (skip) {
693 skip = 0;
694 continue;
695 }
3e2d9453 696 *cp = '\0';
bdffbcdc 697
698 /* Skip leading whitespace, empty and comment lines. */
699 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
700 ;
701 if (!*cp || *cp == '\n' || *cp == '#') {
702 if (inplace)
703 fprintf(out, "%s\n", cp);
704 continue;
705 }
706 /* Find the end of the host name portion. */
707 for (kp = cp; *kp && *kp != ' ' && *kp != '\t'; kp++)
708 ;
709 if (*kp == '\0' || *(kp + 1) == '\0') {
710 error("line %d missing key: %.40s...",
711 num, line);
712 invalid = 1;
713 continue;
714 }
715 *kp++ = '\0';
716 kp2 = kp;
717
718 public = key_new(KEY_RSA1);
719 if (key_read(public, &kp) != 1) {
720 kp = kp2;
721 key_free(public);
722 public = key_new(KEY_UNSPEC);
723 if (key_read(public, &kp) != 1) {
724 error("line %d invalid key: %.40s...",
725 num, line);
726 key_free(public);
727 invalid = 1;
728 continue;
729 }
730 }
731
732 if (*cp == HASH_DELIM) {
733 if (find_host || delete_host) {
734 cp2 = host_hash(name, cp, strlen(cp));
735 if (cp2 == NULL) {
736 error("line %d: invalid hashed "
737 "name: %.64s...", num, line);
738 invalid = 1;
739 continue;
740 }
741 c = (strcmp(cp2, cp) == 0);
742 if (find_host && c) {
743 printf("# Host %s found: "
744 "line %d type %s\n", name,
745 num, key_type(public));
746 print_host(out, cp, public, 0);
747 }
748 if (delete_host && !c)
749 print_host(out, cp, public, 0);
750 } else if (hash_hosts)
751 print_host(out, cp, public, 0);
752 } else {
753 if (find_host || delete_host) {
754 c = (match_hostname(name, cp,
755 strlen(cp)) == 1);
756 if (find_host && c) {
757 printf("# Host %s found: "
758 "line %d type %s\n", name,
759 num, key_type(public));
ccc6beea 760 print_host(out, name, public,
761 hash_hosts);
bdffbcdc 762 }
763 if (delete_host && !c)
764 print_host(out, cp, public, 0);
765 } else if (hash_hosts) {
f8cc7664 766 for (cp2 = strsep(&cp, ",");
bdffbcdc 767 cp2 != NULL && *cp2 != '\0';
bc7119ba 768 cp2 = strsep(&cp, ",")) {
769 if (strcspn(cp2, "*?!") != strlen(cp2))
770 fprintf(stderr, "Warning: "
771 "ignoring host name with "
772 "metacharacters: %.64s\n",
773 cp2);
774 else
775 print_host(out, cp2, public, 1);
776 }
bdffbcdc 777 has_unhashed = 1;
778 }
779 }
780 key_free(public);
781 }
782 fclose(in);
783
784 if (invalid) {
f8f89bae 785 fprintf(stderr, "%s is not a valid known_hosts file.\n",
bdffbcdc 786 identity_file);
787 if (inplace) {
788 fprintf(stderr, "Not replacing existing known_hosts "
604dac32 789 "file because of errors\n");
bdffbcdc 790 fclose(out);
791 unlink(tmp);
792 }
793 exit(1);
794 }
795
796 if (inplace) {
797 fclose(out);
798
799 /* Backup existing file */
800 if (unlink(old) == -1 && errno != ENOENT)
801 fatal("unlink %.100s: %s", old, strerror(errno));
802 if (link(identity_file, old) == -1)
803 fatal("link %.100s to %.100s: %s", identity_file, old,
804 strerror(errno));
805 /* Move new one into place */
806 if (rename(tmp, identity_file) == -1) {
807 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
808 strerror(errno));
809 unlink(tmp);
810 unlink(old);
811 exit(1);
812 }
813
814 fprintf(stderr, "%s updated.\n", identity_file);
815 fprintf(stderr, "Original contents retained as %s\n", old);
816 if (has_unhashed) {
817 fprintf(stderr, "WARNING: %s contains unhashed "
818 "entries\n", old);
819 fprintf(stderr, "Delete this file to ensure privacy "
4e2e5cfd 820 "of hostnames\n");
bdffbcdc 821 }
822 }
823
824 exit(0);
825}
826
5260325f 827/*
828 * Perform changing a passphrase. The argument is the passwd structure
829 * for the current user.
830 */
396c147e 831static void
f095fcc7 832do_change_passphrase(struct passwd *pw)
833{
5260325f 834 char *comment;
835 char *old_passphrase, *passphrase1, *passphrase2;
836 struct stat st;
a306f2dd 837 Key *private;
5260325f 838
839 if (!have_identity)
840 ask_filename(pw, "Enter file in which the key is");
5260325f 841 if (stat(identity_file, &st) < 0) {
842 perror(identity_file);
843 exit(1);
844 }
5260325f 845 /* Try to load the file with empty passphrase. */
aeaa3d9e 846 private = key_load_private(identity_file, "", &comment);
847 if (private == NULL) {
5260325f 848 if (identity_passphrase)
849 old_passphrase = xstrdup(identity_passphrase);
850 else
1843a425 851 old_passphrase =
852 read_passphrase("Enter old passphrase: ",
853 RP_ALLOW_STDIN);
854 private = key_load_private(identity_file, old_passphrase,
855 &comment);
aeaa3d9e 856 memset(old_passphrase, 0, strlen(old_passphrase));
857 xfree(old_passphrase);
858 if (private == NULL) {
5260325f 859 printf("Bad passphrase.\n");
860 exit(1);
861 }
5260325f 862 }
863 printf("Key has comment '%s'\n", comment);
864
865 /* Ask the new passphrase (twice). */
866 if (identity_new_passphrase) {
867 passphrase1 = xstrdup(identity_new_passphrase);
868 passphrase2 = NULL;
869 } else {
870 passphrase1 =
1843a425 871 read_passphrase("Enter new passphrase (empty for no "
872 "passphrase): ", RP_ALLOW_STDIN);
873 passphrase2 = read_passphrase("Enter same passphrase again: ",
184eed6a 874 RP_ALLOW_STDIN);
5260325f 875
876 /* Verify that they are the same. */
877 if (strcmp(passphrase1, passphrase2) != 0) {
878 memset(passphrase1, 0, strlen(passphrase1));
879 memset(passphrase2, 0, strlen(passphrase2));
880 xfree(passphrase1);
881 xfree(passphrase2);
882 printf("Pass phrases do not match. Try again.\n");
883 exit(1);
884 }
885 /* Destroy the other copy. */
886 memset(passphrase2, 0, strlen(passphrase2));
887 xfree(passphrase2);
8efc0c15 888 }
8efc0c15 889
5260325f 890 /* Save the file using the new passphrase. */
aeaa3d9e 891 if (!key_save_private(private, identity_file, passphrase1, comment)) {
58cfa257 892 printf("Saving the key failed: %s.\n", identity_file);
5260325f 893 memset(passphrase1, 0, strlen(passphrase1));
894 xfree(passphrase1);
a306f2dd 895 key_free(private);
5260325f 896 xfree(comment);
897 exit(1);
898 }
899 /* Destroy the passphrase and the copy of the key in memory. */
900 memset(passphrase1, 0, strlen(passphrase1));
901 xfree(passphrase1);
a306f2dd 902 key_free(private); /* Destroys contents */
5260325f 903 xfree(comment);
904
905 printf("Your identification has been saved with the new passphrase.\n");
906 exit(0);
907}
8efc0c15 908
21289cd0 909/*
910 * Print the SSHFP RR.
911 */
3eff92ec 912static int
913do_print_resource_record(struct passwd *pw, char *fname, char *hname)
21289cd0 914{
915 Key *public;
916 char *comment = NULL;
917 struct stat st;
918
3eff92ec 919 if (fname == NULL)
21289cd0 920 ask_filename(pw, "Enter file in which the key is");
3eff92ec 921 if (stat(fname, &st) < 0) {
922 if (errno == ENOENT)
923 return 0;
924 perror(fname);
21289cd0 925 exit(1);
926 }
3eff92ec 927 public = key_load_public(fname, &comment);
21289cd0 928 if (public != NULL) {
ca75d7de 929 export_dns_rr(hname, public, stdout, print_generic);
21289cd0 930 key_free(public);
931 xfree(comment);
3eff92ec 932 return 1;
21289cd0 933 }
934 if (comment)
935 xfree(comment);
936
3eff92ec 937 printf("failed to read v2 public key from %s.\n", fname);
21289cd0 938 exit(1);
939}
21289cd0 940
5260325f 941/*
942 * Change the comment of a private key file.
943 */
396c147e 944static void
8efc0c15 945do_change_comment(struct passwd *pw)
946{
1c9a907f 947 char new_comment[1024], *comment, *passphrase;
aeaa3d9e 948 Key *private;
949 Key *public;
5260325f 950 struct stat st;
951 FILE *f;
1c9a907f 952 int fd;
5260325f 953
954 if (!have_identity)
955 ask_filename(pw, "Enter file in which the key is");
5260325f 956 if (stat(identity_file, &st) < 0) {
957 perror(identity_file);
958 exit(1);
959 }
aeaa3d9e 960 private = key_load_private(identity_file, "", &comment);
961 if (private == NULL) {
5260325f 962 if (identity_passphrase)
963 passphrase = xstrdup(identity_passphrase);
964 else if (identity_new_passphrase)
965 passphrase = xstrdup(identity_new_passphrase);
966 else
1843a425 967 passphrase = read_passphrase("Enter passphrase: ",
968 RP_ALLOW_STDIN);
5260325f 969 /* Try to load using the passphrase. */
aeaa3d9e 970 private = key_load_private(identity_file, passphrase, &comment);
971 if (private == NULL) {
5260325f 972 memset(passphrase, 0, strlen(passphrase));
973 xfree(passphrase);
974 printf("Bad passphrase.\n");
975 exit(1);
976 }
aeaa3d9e 977 } else {
978 passphrase = xstrdup("");
5260325f 979 }
aeaa3d9e 980 if (private->type != KEY_RSA1) {
981 fprintf(stderr, "Comments are only supported for RSA1 keys.\n");
982 key_free(private);
983 exit(1);
184eed6a 984 }
5260325f 985 printf("Key now has comment '%s'\n", comment);
986
987 if (identity_comment) {
988 strlcpy(new_comment, identity_comment, sizeof(new_comment));
989 } else {
990 printf("Enter new comment: ");
991 fflush(stdout);
992 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
993 memset(passphrase, 0, strlen(passphrase));
a306f2dd 994 key_free(private);
5260325f 995 exit(1);
996 }
b05117ac 997 new_comment[strcspn(new_comment, "\n")] = '\0';
5260325f 998 }
999
1000 /* Save the file using the new passphrase. */
aeaa3d9e 1001 if (!key_save_private(private, identity_file, passphrase, new_comment)) {
58cfa257 1002 printf("Saving the key failed: %s.\n", identity_file);
5260325f 1003 memset(passphrase, 0, strlen(passphrase));
1004 xfree(passphrase);
a306f2dd 1005 key_free(private);
5260325f 1006 xfree(comment);
1007 exit(1);
8efc0c15 1008 }
5260325f 1009 memset(passphrase, 0, strlen(passphrase));
1010 xfree(passphrase);
aeaa3d9e 1011 public = key_from_private(private);
a306f2dd 1012 key_free(private);
5260325f 1013
5260325f 1014 strlcat(identity_file, ".pub", sizeof(identity_file));
1c9a907f 1015 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1016 if (fd == -1) {
5260325f 1017 printf("Could not save your public key in %s\n", identity_file);
1018 exit(1);
1019 }
1c9a907f 1020 f = fdopen(fd, "w");
1021 if (f == NULL) {
7eec82ab 1022 printf("fdopen %s failed\n", identity_file);
1c9a907f 1023 exit(1);
1024 }
a306f2dd 1025 if (!key_write(public, f))
7eec82ab 1026 fprintf(stderr, "write key failed\n");
a306f2dd 1027 key_free(public);
1028 fprintf(f, " %s\n", new_comment);
5260325f 1029 fclose(f);
1030
1031 xfree(comment);
1032
1033 printf("The comment in your key file has been changed.\n");
1034 exit(0);
8efc0c15 1035}
1036
396c147e 1037static void
5ad13cd7 1038usage(void)
1039{
269cfc8a 1040 fprintf(stderr, "usage: %s [options]\n", __progname);
58153e34 1041 fprintf(stderr, "Options:\n");
4feb61af 1042 fprintf(stderr, " -a trials Number of trials for screening DH-GEX moduli.\n");
1043 fprintf(stderr, " -B Show bubblebabble digest of key file.\n");
58153e34 1044 fprintf(stderr, " -b bits Number of bits in the key to create.\n");
4feb61af 1045 fprintf(stderr, " -C comment Provide new comment.\n");
58153e34 1046 fprintf(stderr, " -c Change comment in private and public key files.\n");
4feb61af 1047#ifdef SMARTCARD
1048 fprintf(stderr, " -D reader Download public key from smartcard.\n");
1049#endif /* SMARTCARD */
e91a8c3f 1050 fprintf(stderr, " -e Convert OpenSSH to RFC 4716 key file.\n");
4feb61af 1051 fprintf(stderr, " -F hostname Find hostname in known hosts file.\n");
58153e34 1052 fprintf(stderr, " -f filename Filename of the key file.\n");
4feb61af 1053 fprintf(stderr, " -G file Generate candidates for DH-GEX moduli.\n");
21289cd0 1054 fprintf(stderr, " -g Use generic DNS resource record format.\n");
4feb61af 1055 fprintf(stderr, " -H Hash names in known_hosts file.\n");
e91a8c3f 1056 fprintf(stderr, " -i Convert RFC 4716 to OpenSSH key file.\n");
58153e34 1057 fprintf(stderr, " -l Show fingerprint of key file.\n");
4feb61af 1058 fprintf(stderr, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
58153e34 1059 fprintf(stderr, " -N phrase Provide new passphrase.\n");
1060 fprintf(stderr, " -P phrase Provide old passphrase.\n");
4feb61af 1061 fprintf(stderr, " -p Change passphrase of private key file.\n");
1062 fprintf(stderr, " -q Quiet.\n");
1063 fprintf(stderr, " -R hostname Remove host from known_hosts file.\n");
21289cd0 1064 fprintf(stderr, " -r hostname Print DNS resource record.\n");
4feb61af 1065 fprintf(stderr, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1066 fprintf(stderr, " -T file Screen candidates for DH-GEX moduli.\n");
1067 fprintf(stderr, " -t type Specify type of key to create.\n");
58153e34 1068#ifdef SMARTCARD
58153e34 1069 fprintf(stderr, " -U reader Upload private key to smartcard.\n");
1070#endif /* SMARTCARD */
4feb61af 1071 fprintf(stderr, " -v Verbose.\n");
1072 fprintf(stderr, " -W gen Generator to use for generating DH-GEX moduli.\n");
1073 fprintf(stderr, " -y Read private key file and print public key.\n");
c35a6dc5 1074
5260325f 1075 exit(1);
5ad13cd7 1076}
1077
5260325f 1078/*
1079 * Main program for key management.
1080 */
8efc0c15 1081int
4b4810d4 1082main(int argc, char **argv)
8efc0c15 1083{
3dc93cd8 1084 char dotsshdir[MAXPATHLEN], comment[1024], *passphrase1, *passphrase2;
8f52069e 1085 char out_file[MAXPATHLEN], *reader_id = NULL;
bdffbcdc 1086 char *rr_hostname = NULL;
1c9a907f 1087 Key *private, *public;
5260325f 1088 struct passwd *pw;
5260325f 1089 struct stat st;
c784ae09 1090 int opt, type, fd, download = 0;
2a1995a3 1091 u_int32_t memory = 0, generator_wanted = 0, trials = 100;
c35a6dc5 1092 int do_gen_candidates = 0, do_screen_candidates = 0;
1093 BIGNUM *start = NULL;
5260325f 1094 FILE *f;
de4feb6b 1095 const char *errstr;
fa08c86b 1096
5260325f 1097 extern int optind;
1098 extern char *optarg;
1099
fd6168c1 1100 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1101 sanitise_stdfd();
1102
b05ff3f5 1103 __progname = ssh_get_progname(argv[0]);
264dce47 1104
fa649821 1105 SSLeay_add_all_algorithms();
4b4810d4 1106 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
c35a6dc5 1107
ce88d9df 1108 init_rng();
1109 seed_rng();
a306f2dd 1110
aa3378df 1111 /* we need this for the home * directory. */
5260325f 1112 pw = getpwuid(getuid());
1113 if (!pw) {
1114 printf("You don't exist, go away!\n");
1115 exit(1);
1116 }
a306f2dd 1117 if (gethostname(hostname, sizeof(hostname)) < 0) {
1118 perror("gethostname");
1119 exit(1);
1120 }
aa3378df 1121
4b4810d4 1122 while ((opt = getopt(argc, argv,
bdffbcdc 1123 "degiqpclBHvxXyF:b:f:t:U:D:P:N:C:r:g:R:T:G:M:S:a:W:")) != -1) {
5260325f 1124 switch (opt) {
1125 case 'b':
0fe9892f 1126 bits = (u_int32_t)strtonum(optarg, 768, 32768, &errstr);
de4feb6b 1127 if (errstr)
1128 fatal("Bits has bad value %s (%s)",
1129 optarg, errstr);
5260325f 1130 break;
bdffbcdc 1131 case 'F':
1132 find_host = 1;
1133 rr_hostname = optarg;
1134 break;
1135 case 'H':
1136 hash_hosts = 1;
1137 break;
1138 case 'R':
1139 delete_host = 1;
1140 rr_hostname = optarg;
1141 break;
5260325f 1142 case 'l':
1143 print_fingerprint = 1;
1144 break;
aaf45d87 1145 case 'B':
1146 print_bubblebabble = 1;
1147 break;
5260325f 1148 case 'p':
1149 change_passphrase = 1;
1150 break;
5260325f 1151 case 'c':
1152 change_comment = 1;
1153 break;
5260325f 1154 case 'f':
c784ae09 1155 if (strlcpy(identity_file, optarg, sizeof(identity_file)) >=
1156 sizeof(identity_file))
1157 fatal("Identity filename too long");
5260325f 1158 have_identity = 1;
1159 break;
21289cd0 1160 case 'g':
1161 print_generic = 1;
1162 break;
5260325f 1163 case 'P':
1164 identity_passphrase = optarg;
1165 break;
5260325f 1166 case 'N':
1167 identity_new_passphrase = optarg;
1168 break;
5260325f 1169 case 'C':
1170 identity_comment = optarg;
1171 break;
5260325f 1172 case 'q':
1173 quiet = 1;
1174 break;
5deceabb 1175 case 'e':
a306f2dd 1176 case 'x':
5deceabb 1177 /* export key */
a306f2dd 1178 convert_to_ssh2 = 1;
1179 break;
5deceabb 1180 case 'i':
a306f2dd 1181 case 'X':
5deceabb 1182 /* import key */
a306f2dd 1183 convert_from_ssh2 = 1;
1184 break;
a306f2dd 1185 case 'y':
1186 print_public = 1;
1187 break;
a306f2dd 1188 case 'd':
fa08c86b 1189 key_type_name = "dsa";
a306f2dd 1190 break;
fa08c86b 1191 case 't':
1192 key_type_name = optarg;
fa08c86b 1193 break;
93a56445 1194 case 'D':
1195 download = 1;
32596c7b 1196 /*FALLTHROUGH*/
285d2b15 1197 case 'U':
93a56445 1198 reader_id = optarg;
2b30154a 1199 break;
c6fbc95a 1200 case 'v':
1201 if (log_level == SYSLOG_LEVEL_INFO)
1202 log_level = SYSLOG_LEVEL_DEBUG1;
1203 else {
f2107e97 1204 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
c6fbc95a 1205 log_level < SYSLOG_LEVEL_DEBUG3)
1206 log_level++;
1207 }
1208 break;
21289cd0 1209 case 'r':
bdffbcdc 1210 rr_hostname = optarg;
21289cd0 1211 break;
c35a6dc5 1212 case 'W':
0fe9892f 1213 generator_wanted = (u_int32_t)strtonum(optarg, 1,
1214 UINT_MAX, &errstr);
c784ae09 1215 if (errstr)
1216 fatal("Desired generator has bad value: %s (%s)",
1217 optarg, errstr);
c35a6dc5 1218 break;
1219 case 'a':
0fe9892f 1220 trials = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
c784ae09 1221 if (errstr)
1222 fatal("Invalid number of trials: %s (%s)",
1223 optarg, errstr);
c35a6dc5 1224 break;
1225 case 'M':
0fe9892f 1226 memory = (u_int32_t)strtonum(optarg, 1, UINT_MAX, &errstr);
c784ae09 1227 if (errstr) {
1228 fatal("Memory limit is %s: %s", errstr, optarg);
1229 }
c35a6dc5 1230 break;
1231 case 'G':
1232 do_gen_candidates = 1;
c784ae09 1233 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1234 sizeof(out_file))
1235 fatal("Output filename too long");
c35a6dc5 1236 break;
1237 case 'T':
1238 do_screen_candidates = 1;
c784ae09 1239 if (strlcpy(out_file, optarg, sizeof(out_file)) >=
1240 sizeof(out_file))
1241 fatal("Output filename too long");
c35a6dc5 1242 break;
1243 case 'S':
1244 /* XXX - also compare length against bits */
1245 if (BN_hex2bn(&start, optarg) == 0)
1246 fatal("Invalid start point.");
1247 break;
5260325f 1248 case '?':
1249 default:
1250 usage();
1251 }
1252 }
c6fbc95a 1253
1254 /* reinit */
4b4810d4 1255 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
c6fbc95a 1256
4b4810d4 1257 if (optind < argc) {
5260325f 1258 printf("Too many arguments.\n");
1259 usage();
1260 }
1261 if (change_passphrase && change_comment) {
1262 printf("Can only have one of -p and -c.\n");
1263 usage();
1264 }
048acbeb 1265 if (print_fingerprint && (delete_host || hash_hosts)) {
1266 printf("Cannot use -l with -D or -R.\n");
1267 usage();
1268 }
bdffbcdc 1269 if (delete_host || hash_hosts || find_host)
1270 do_known_hosts(pw, rr_hostname);
aaf45d87 1271 if (print_fingerprint || print_bubblebabble)
5260325f 1272 do_fingerprint(pw);
5260325f 1273 if (change_passphrase)
1274 do_change_passphrase(pw);
633151a3 1275 if (change_comment)
1276 do_change_comment(pw);
ce88d9df 1277 if (convert_to_ssh2)
1278 do_convert_to_ssh2(pw);
1279 if (convert_from_ssh2)
1280 do_convert_from_ssh2(pw);
a306f2dd 1281 if (print_public)
1282 do_print_public(pw);
bdffbcdc 1283 if (rr_hostname != NULL) {
3eff92ec 1284 unsigned int n = 0;
1285
1286 if (have_identity) {
1287 n = do_print_resource_record(pw,
1288 identity_file, rr_hostname);
1289 if (n == 0) {
1290 perror(identity_file);
1291 exit(1);
1292 }
1293 exit(0);
1294 } else {
1295
1296 n += do_print_resource_record(pw,
1297 _PATH_HOST_RSA_KEY_FILE, rr_hostname);
1298 n += do_print_resource_record(pw,
1299 _PATH_HOST_DSA_KEY_FILE, rr_hostname);
1300
1301 if (n == 0)
1302 fatal("no keys found.");
1303 exit(0);
1304 }
21289cd0 1305 }
93a56445 1306 if (reader_id != NULL) {
4f831fd7 1307#ifdef SMARTCARD
93a56445 1308 if (download)
1309 do_download(pw, reader_id);
1310 else
1311 do_upload(pw, reader_id);
3e984472 1312#else /* SMARTCARD */
4f831fd7 1313 fatal("no support for smartcards.");
3e984472 1314#endif /* SMARTCARD */
93a56445 1315 }
5260325f 1316
c35a6dc5 1317 if (do_gen_candidates) {
1318 FILE *out = fopen(out_file, "w");
b6453d99 1319
c35a6dc5 1320 if (out == NULL) {
1321 error("Couldn't open modulus candidate file \"%s\": %s",
1322 out_file, strerror(errno));
1323 return (1);
1324 }
255d3e00 1325 if (bits == 0)
1326 bits = DEFAULT_BITS;
c35a6dc5 1327 if (gen_candidates(out, memory, bits, start) != 0)
17318dd6 1328 fatal("modulus candidate generation failed");
c35a6dc5 1329
1330 return (0);
1331 }
1332
1333 if (do_screen_candidates) {
1334 FILE *in;
1335 FILE *out = fopen(out_file, "w");
1336
1337 if (have_identity && strcmp(identity_file, "-") != 0) {
1338 if ((in = fopen(identity_file, "r")) == NULL) {
1339 fatal("Couldn't open modulus candidate "
aff51935 1340 "file \"%s\": %s", identity_file,
c35a6dc5 1341 strerror(errno));
1342 }
1343 } else
1344 in = stdin;
1345
1346 if (out == NULL) {
1347 fatal("Couldn't open moduli file \"%s\": %s",
1348 out_file, strerror(errno));
1349 }
1350 if (prime_test(in, out, trials, generator_wanted) != 0)
17318dd6 1351 fatal("modulus screening failed");
08d035b6 1352 return (0);
c35a6dc5 1353 }
1354
5260325f 1355 arc4random_stir();
1356
882c9d5a 1357 if (key_type_name == NULL)
1358 key_type_name = "rsa";
1359
c523303b 1360 type = key_type_from_name(key_type_name);
1361 if (type == KEY_UNSPEC) {
1362 fprintf(stderr, "unknown key type %s\n", key_type_name);
1363 exit(1);
a306f2dd 1364 }
255d3e00 1365 if (bits == 0)
1366 bits = (type == KEY_DSA) ? DEFAULT_BITS_DSA : DEFAULT_BITS;
6e94bd72 1367 if (type == KEY_DSA && bits != 1024)
1368 fatal("DSA keys must be 1024 bits");
60dc0294 1369 if (!quiet)
1370 printf("Generating public/private %s key pair.\n", key_type_name);
fa08c86b 1371 private = key_generate(type, bits);
1372 if (private == NULL) {
7eec82ab 1373 fprintf(stderr, "key_generate failed\n");
fa08c86b 1374 exit(1);
1375 }
1376 public = key_from_private(private);
5260325f 1377
1378 if (!have_identity)
1379 ask_filename(pw, "Enter file in which to save the key");
1380
7b9b0103 1381 /* Create ~/.ssh directory if it doesn't already exist. */
42f11eb2 1382 snprintf(dotsshdir, sizeof dotsshdir, "%s/%s", pw->pw_dir, _PATH_SSH_USER_DIR);
5260325f 1383 if (strstr(identity_file, dotsshdir) != NULL &&
1384 stat(dotsshdir, &st) < 0) {
704b1659 1385 if (mkdir(dotsshdir, 0700) < 0)
5260325f 1386 error("Could not create directory '%s'.", dotsshdir);
1387 else if (!quiet)
1388 printf("Created directory '%s'.\n", dotsshdir);
1389 }
1390 /* If the file already exists, ask the user to confirm. */
1391 if (stat(identity_file, &st) >= 0) {
1392 char yesno[3];
1393 printf("%s already exists.\n", identity_file);
1394 printf("Overwrite (y/n)? ");
1395 fflush(stdout);
1396 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
1397 exit(1);
1398 if (yesno[0] != 'y' && yesno[0] != 'Y')
1399 exit(1);
1400 }
1401 /* Ask for a passphrase (twice). */
1402 if (identity_passphrase)
1403 passphrase1 = xstrdup(identity_passphrase);
1404 else if (identity_new_passphrase)
1405 passphrase1 = xstrdup(identity_new_passphrase);
1406 else {
1407passphrase_again:
1408 passphrase1 =
1843a425 1409 read_passphrase("Enter passphrase (empty for no "
1410 "passphrase): ", RP_ALLOW_STDIN);
1411 passphrase2 = read_passphrase("Enter same passphrase again: ",
1412 RP_ALLOW_STDIN);
5260325f 1413 if (strcmp(passphrase1, passphrase2) != 0) {
1843a425 1414 /*
1415 * The passphrases do not match. Clear them and
1416 * retry.
1417 */
5260325f 1418 memset(passphrase1, 0, strlen(passphrase1));
1419 memset(passphrase2, 0, strlen(passphrase2));
1420 xfree(passphrase1);
1421 xfree(passphrase2);
1422 printf("Passphrases do not match. Try again.\n");
1423 goto passphrase_again;
1424 }
1425 /* Clear the other copy of the passphrase. */
1426 memset(passphrase2, 0, strlen(passphrase2));
1427 xfree(passphrase2);
1428 }
1429
5260325f 1430 if (identity_comment) {
1431 strlcpy(comment, identity_comment, sizeof(comment));
1432 } else {
1890bf8b 1433 /* Create default comment field for the passphrase. */
5260325f 1434 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
1435 }
1436
1437 /* Save the key with the given passphrase and comment. */
aeaa3d9e 1438 if (!key_save_private(private, identity_file, passphrase1, comment)) {
58cfa257 1439 printf("Saving the key failed: %s.\n", identity_file);
5260325f 1440 memset(passphrase1, 0, strlen(passphrase1));
1441 xfree(passphrase1);
1442 exit(1);
1443 }
1444 /* Clear the passphrase. */
1445 memset(passphrase1, 0, strlen(passphrase1));
1446 xfree(passphrase1);
1447
1448 /* Clear the private key and the random number generator. */
fa08c86b 1449 key_free(private);
5260325f 1450 arc4random_stir();
1451
1452 if (!quiet)
1453 printf("Your identification has been saved in %s.\n", identity_file);
1454
5260325f 1455 strlcat(identity_file, ".pub", sizeof(identity_file));
1c9a907f 1456 fd = open(identity_file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1457 if (fd == -1) {
5260325f 1458 printf("Could not save your public key in %s\n", identity_file);
1459 exit(1);
1460 }
1c9a907f 1461 f = fdopen(fd, "w");
1462 if (f == NULL) {
7eec82ab 1463 printf("fdopen %s failed\n", identity_file);
1c9a907f 1464 exit(1);
1465 }
a306f2dd 1466 if (!key_write(public, f))
7eec82ab 1467 fprintf(stderr, "write key failed\n");
a306f2dd 1468 fprintf(f, " %s\n", comment);
5260325f 1469 fclose(f);
1470
1471 if (!quiet) {
22138a36 1472 char *fp = key_fingerprint(public, SSH_FP_MD5, SSH_FP_HEX);
aff73c5f 1473 char *ra = key_fingerprint(public, SSH_FP_MD5,
1474 SSH_FP_RANDOMART);
a306f2dd 1475 printf("Your public key has been saved in %s.\n",
1476 identity_file);
5260325f 1477 printf("The key fingerprint is:\n");
22138a36 1478 printf("%s %s\n", fp, comment);
aff73c5f 1479 printf("The key's randomart image is:\n");
1480 printf("%s\n", ra);
1481 xfree(ra);
22138a36 1482 xfree(fp);
8efc0c15 1483 }
a306f2dd 1484
1485 key_free(public);
5260325f 1486 exit(0);
8efc0c15 1487}
This page took 0.572949 seconds and 5 git commands to generate.