1 /* $OpenBSD: ssh-keygen.c,v 1.185 2010/03/15 19:40:02 stevesk Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1994 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * Identity and host key generation and maintenance.
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".
17 #include <sys/types.h>
18 #include <sys/socket.h>
20 #include <sys/param.h>
22 #include <openssl/evp.h>
23 #include <openssl/pem.h>
24 #include "openbsd-compat/openssl-compat.h"
45 #include "pathnames.h"
54 #include "ssh-pkcs11.h"
57 /* Number of bits in the RSA/DSA key. This value can be set on the command line. */
58 #define DEFAULT_BITS 2048
59 #define DEFAULT_BITS_DSA 1024
63 * Flag indicating that we just want to change the passphrase. This can be
64 * set on the command line.
66 int change_passphrase
= 0;
69 * Flag indicating that we just want to change the comment. This can be set
70 * on the command line.
72 int change_comment
= 0;
76 int log_level
= SYSLOG_LEVEL_INFO
;
78 /* Flag indicating that we want to hash a known_hosts file */
80 /* Flag indicating that we want lookup a host in known_hosts file */
82 /* Flag indicating that we want to delete a host from a known_hosts file */
85 /* Flag indicating that we want to show the contents of a certificate */
88 /* Flag indicating that we just want to see the key fingerprint */
89 int print_fingerprint
= 0;
90 int print_bubblebabble
= 0;
92 /* The identity file name, given on the command line or entered by the user. */
93 char identity_file
[1024];
94 int have_identity
= 0;
96 /* This is set to the passphrase if given on the command line. */
97 char *identity_passphrase
= NULL
;
99 /* This is set to the new passphrase if given on the command line. */
100 char *identity_new_passphrase
= NULL
;
102 /* This is set to the new comment if given on the command line. */
103 char *identity_comment
= NULL
;
105 /* Path to CA key when certifying keys. */
106 char *ca_key_path
= NULL
;
108 /* Key type when certifying */
109 u_int cert_key_type
= SSH2_CERT_TYPE_USER
;
111 /* "key ID" of signed key */
112 char *cert_key_id
= NULL
;
114 /* Comma-separated list of principal names for certifying keys */
115 char *cert_principals
= NULL
;
117 /* Validity period for certificates */
118 u_int64_t cert_valid_from
= 0;
119 u_int64_t cert_valid_to
= ~0ULL;
121 /* Certificate constraints */
122 #define CONSTRAINT_X_FWD (1)
123 #define CONSTRAINT_AGENT_FWD (1<<1)
124 #define CONSTRAINT_PORT_FWD (1<<2)
125 #define CONSTRAINT_PTY (1<<3)
126 #define CONSTRAINT_USER_RC (1<<4)
127 #define CONSTRAINT_DEFAULT (CONSTRAINT_X_FWD|CONSTRAINT_AGENT_FWD| \
128 CONSTRAINT_PORT_FWD|CONSTRAINT_PTY| \
130 u_int32_t constraint_flags
= CONSTRAINT_DEFAULT
;
131 char *constraint_command
= NULL
;
132 char *constraint_src_addr
= NULL
;
134 /* Dump public key file in format used by real and the original SSH 2 */
135 int convert_to_ssh2
= 0;
136 int convert_from_ssh2
= 0;
137 int print_public
= 0;
138 int print_generic
= 0;
140 char *key_type_name
= NULL
;
143 extern char *__progname
;
145 char hostname
[MAXHOSTNAMELEN
];
148 int gen_candidates(FILE *, u_int32_t
, u_int32_t
, BIGNUM
*);
149 int prime_test(FILE *, FILE *, u_int32_t
, u_int32_t
);
152 ask_filename(struct passwd
*pw
, const char *prompt
)
157 if (key_type_name
== NULL
)
158 name
= _PATH_SSH_CLIENT_ID_RSA
;
160 switch (key_type_from_name(key_type_name
)) {
162 name
= _PATH_SSH_CLIENT_IDENTITY
;
165 name
= _PATH_SSH_CLIENT_ID_DSA
;
168 name
= _PATH_SSH_CLIENT_ID_RSA
;
171 fprintf(stderr
, "bad key type\n");
176 snprintf(identity_file
, sizeof(identity_file
), "%s/%s", pw
->pw_dir
, name
);
177 fprintf(stderr
, "%s (%s): ", prompt
, identity_file
);
178 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
180 buf
[strcspn(buf
, "\n")] = '\0';
181 if (strcmp(buf
, "") != 0)
182 strlcpy(identity_file
, buf
, sizeof(identity_file
));
187 load_identity(char *filename
)
192 prv
= key_load_private(filename
, "", NULL
);
194 if (identity_passphrase
)
195 pass
= xstrdup(identity_passphrase
);
197 pass
= read_passphrase("Enter passphrase: ",
199 prv
= key_load_private(filename
, pass
, NULL
);
200 memset(pass
, 0, strlen(pass
));
206 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
207 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
208 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
209 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
212 do_convert_to_ssh2(struct passwd
*pw
)
221 ask_filename(pw
, "Enter file in which the key is");
222 if (stat(identity_file
, &st
) < 0) {
223 perror(identity_file
);
226 if ((k
= key_load_public(identity_file
, NULL
)) == NULL
) {
227 if ((k
= load_identity(identity_file
)) == NULL
) {
228 fprintf(stderr
, "load failed\n");
232 if (k
->type
== KEY_RSA1
) {
233 fprintf(stderr
, "version 1 keys are not supported\n");
236 if (key_to_blob(k
, &blob
, &len
) <= 0) {
237 fprintf(stderr
, "key_to_blob failed\n");
240 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
241 snprintf(comment
, sizeof(comment
),
242 "%u-bit %s, converted by %s@%s from OpenSSH",
243 key_size(k
), key_type(k
),
244 pw
->pw_name
, hostname
);
246 fprintf(stdout
, "%s\n", SSH_COM_PUBLIC_BEGIN
);
247 fprintf(stdout
, "Comment: \"%s\"\n", comment
);
248 dump_base64(stdout
, blob
, len
);
249 fprintf(stdout
, "%s\n", SSH_COM_PUBLIC_END
);
256 buffer_get_bignum_bits(Buffer
*b
, BIGNUM
*value
)
258 u_int bignum_bits
= buffer_get_int(b
);
259 u_int bytes
= (bignum_bits
+ 7) / 8;
261 if (buffer_len(b
) < bytes
)
262 fatal("buffer_get_bignum_bits: input buffer too small: "
263 "need %d have %d", bytes
, buffer_len(b
));
264 if (BN_bin2bn(buffer_ptr(b
), bytes
, value
) == NULL
)
265 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
266 buffer_consume(b
, bytes
);
270 do_convert_private_ssh2_from_blob(u_char
*blob
, u_int blen
)
275 u_char
*sig
, data
[] = "abcde12345";
276 int magic
, rlen
, ktype
, i1
, i2
, i3
, i4
;
281 buffer_append(&b
, blob
, blen
);
283 magic
= buffer_get_int(&b
);
284 if (magic
!= SSH_COM_PRIVATE_KEY_MAGIC
) {
285 error("bad magic 0x%x != 0x%x", magic
, SSH_COM_PRIVATE_KEY_MAGIC
);
289 i1
= buffer_get_int(&b
);
290 type
= buffer_get_string(&b
, NULL
);
291 cipher
= buffer_get_string(&b
, NULL
);
292 i2
= buffer_get_int(&b
);
293 i3
= buffer_get_int(&b
);
294 i4
= buffer_get_int(&b
);
295 debug("ignore (%d %d %d %d)", i1
, i2
, i3
, i4
);
296 if (strcmp(cipher
, "none") != 0) {
297 error("unsupported cipher %s", cipher
);
305 if (strstr(type
, "dsa")) {
307 } else if (strstr(type
, "rsa")) {
314 key
= key_new_private(ktype
);
319 buffer_get_bignum_bits(&b
, key
->dsa
->p
);
320 buffer_get_bignum_bits(&b
, key
->dsa
->g
);
321 buffer_get_bignum_bits(&b
, key
->dsa
->q
);
322 buffer_get_bignum_bits(&b
, key
->dsa
->pub_key
);
323 buffer_get_bignum_bits(&b
, key
->dsa
->priv_key
);
326 e
= buffer_get_char(&b
);
330 e
+= buffer_get_char(&b
);
333 e
+= buffer_get_char(&b
);
336 if (!BN_set_word(key
->rsa
->e
, e
)) {
341 buffer_get_bignum_bits(&b
, key
->rsa
->d
);
342 buffer_get_bignum_bits(&b
, key
->rsa
->n
);
343 buffer_get_bignum_bits(&b
, key
->rsa
->iqmp
);
344 buffer_get_bignum_bits(&b
, key
->rsa
->q
);
345 buffer_get_bignum_bits(&b
, key
->rsa
->p
);
346 rsa_generate_additional_parameters(key
->rsa
);
349 rlen
= buffer_len(&b
);
351 error("do_convert_private_ssh2_from_blob: "
352 "remaining bytes in key blob %d", rlen
);
356 key_sign(key
, &sig
, &slen
, data
, sizeof(data
));
357 key_verify(key
, sig
, slen
, data
, sizeof(data
));
363 get_line(FILE *fp
, char *line
, size_t len
)
369 while ((c
= fgetc(fp
)) != EOF
) {
370 if (pos
>= len
- 1) {
371 fprintf(stderr
, "input line too long.\n");
377 if (c
!= EOF
&& c
!= '\n' && ungetc(c
, fp
) == EOF
) {
378 fprintf(stderr
, "unget: %s\n", strerror(errno
));
393 do_convert_from_ssh2(struct passwd
*pw
)
402 int escaped
= 0, private = 0, ok
;
406 ask_filename(pw
, "Enter file in which the key is");
407 if (stat(identity_file
, &st
) < 0) {
408 perror(identity_file
);
411 fp
= fopen(identity_file
, "r");
413 perror(identity_file
);
417 while ((blen
= get_line(fp
, line
, sizeof(line
))) != -1) {
418 if (line
[blen
- 1] == '\\')
420 if (strncmp(line
, "----", 4) == 0 ||
421 strstr(line
, ": ") != NULL
) {
422 if (strstr(line
, SSH_COM_PRIVATE_BEGIN
) != NULL
)
424 if (strstr(line
, " END ") != NULL
) {
427 /* fprintf(stderr, "ignore: %s", line); */
432 /* fprintf(stderr, "escaped: %s", line); */
435 strlcat(encoded
, line
, sizeof(encoded
));
437 len
= strlen(encoded
);
438 if (((len
% 4) == 3) &&
439 (encoded
[len
-1] == '=') &&
440 (encoded
[len
-2] == '=') &&
441 (encoded
[len
-3] == '='))
442 encoded
[len
-3] = '\0';
443 blen
= uudecode(encoded
, blob
, sizeof(blob
));
445 fprintf(stderr
, "uudecode failed.\n");
449 do_convert_private_ssh2_from_blob(blob
, blen
) :
450 key_from_blob(blob
, blen
);
452 fprintf(stderr
, "decode blob failed.\n");
456 (k
->type
== KEY_DSA
?
457 PEM_write_DSAPrivateKey(stdout
, k
->dsa
, NULL
, NULL
, 0, NULL
, NULL
) :
458 PEM_write_RSAPrivateKey(stdout
, k
->rsa
, NULL
, NULL
, 0, NULL
, NULL
)) :
459 key_write(k
, stdout
);
461 fprintf(stderr
, "key write failed\n");
466 fprintf(stdout
, "\n");
472 do_print_public(struct passwd
*pw
)
478 ask_filename(pw
, "Enter file in which the key is");
479 if (stat(identity_file
, &st
) < 0) {
480 perror(identity_file
);
483 prv
= load_identity(identity_file
);
485 fprintf(stderr
, "load failed\n");
488 if (!key_write(prv
, stdout
))
489 fprintf(stderr
, "key_write failed");
491 fprintf(stdout
, "\n");
496 do_download(struct passwd
*pw
, char *pkcs11provider
)
503 nkeys
= pkcs11_add_provider(pkcs11provider
, NULL
, &keys
);
505 fatal("cannot read public key from pkcs11");
506 for (i
= 0; i
< nkeys
; i
++) {
507 key_write(keys
[i
], stdout
);
509 fprintf(stdout
, "\n");
515 fatal("no pkcs11 support");
516 #endif /* ENABLE_PKCS11 */
520 do_fingerprint(struct passwd
*pw
)
524 char *comment
= NULL
, *cp
, *ep
, line
[16*1024], *fp
, *ra
;
525 int i
, skip
= 0, num
= 0, invalid
= 1;
530 fptype
= print_bubblebabble
? SSH_FP_SHA1
: SSH_FP_MD5
;
531 rep
= print_bubblebabble
? SSH_FP_BUBBLEBABBLE
: SSH_FP_HEX
;
534 ask_filename(pw
, "Enter file in which the key is");
535 if (stat(identity_file
, &st
) < 0) {
536 perror(identity_file
);
539 public = key_load_public(identity_file
, &comment
);
540 if (public != NULL
) {
541 fp
= key_fingerprint(public, fptype
, rep
);
542 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
543 printf("%u %s %s (%s)\n", key_size(public), fp
, comment
,
545 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
558 f
= fopen(identity_file
, "r");
560 while (fgets(line
, sizeof(line
), f
)) {
561 if ((cp
= strchr(line
, '\n')) == NULL
) {
562 error("line %d too long: %.40s...",
574 /* Skip leading whitespace, empty and comment lines. */
575 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
577 if (!*cp
|| *cp
== '\n' || *cp
== '#')
579 i
= strtol(cp
, &ep
, 10);
580 if (i
== 0 || ep
== NULL
|| (*ep
!= ' ' && *ep
!= '\t')) {
583 for (; *cp
&& (quoted
|| (*cp
!= ' ' &&
584 *cp
!= '\t')); cp
++) {
585 if (*cp
== '\\' && cp
[1] == '"')
586 cp
++; /* Skip both */
595 public = key_new(KEY_RSA1
);
596 if (key_read(public, &cp
) != 1) {
599 public = key_new(KEY_UNSPEC
);
600 if (key_read(public, &cp
) != 1) {
605 comment
= *cp
? cp
: comment
;
606 fp
= key_fingerprint(public, fptype
, rep
);
607 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
608 printf("%u %s %s (%s)\n", key_size(public), fp
,
609 comment
? comment
: "no comment", key_type(public));
610 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
620 printf("%s is not a public key file.\n", identity_file
);
627 printhost(FILE *f
, const char *name
, Key
*public, int ca
, int hash
)
629 if (print_fingerprint
) {
634 fptype
= print_bubblebabble
? SSH_FP_SHA1
: SSH_FP_MD5
;
635 rep
= print_bubblebabble
? SSH_FP_BUBBLEBABBLE
: SSH_FP_HEX
;
636 fp
= key_fingerprint(public, fptype
, rep
);
637 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
638 printf("%u %s %s (%s)\n", key_size(public), fp
, name
,
640 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
645 if (hash
&& (name
= host_hash(name
, NULL
, 0)) == NULL
)
646 fatal("hash_host failed");
647 fprintf(f
, "%s%s%s ", ca
? CA_MARKER
: "", ca
? " " : "", name
);
648 if (!key_write(public, f
))
649 fatal("key_write failed");
655 do_known_hosts(struct passwd
*pw
, const char *name
)
657 FILE *in
, *out
= stdout
;
659 char *cp
, *cp2
, *kp
, *kp2
;
660 char line
[16*1024], tmp
[MAXPATHLEN
], old
[MAXPATHLEN
];
661 int c
, skip
= 0, inplace
= 0, num
= 0, invalid
= 0, has_unhashed
= 0;
664 if (!have_identity
) {
665 cp
= tilde_expand_filename(_PATH_SSH_USER_HOSTFILE
, pw
->pw_uid
);
666 if (strlcpy(identity_file
, cp
, sizeof(identity_file
)) >=
667 sizeof(identity_file
))
668 fatal("Specified known hosts path too long");
672 if ((in
= fopen(identity_file
, "r")) == NULL
)
673 fatal("fopen: %s", strerror(errno
));
676 * Find hosts goes to stdout, hash and deletions happen in-place
677 * A corner case is ssh-keygen -HF foo, which should go to stdout
679 if (!find_host
&& (hash_hosts
|| delete_host
)) {
680 if (strlcpy(tmp
, identity_file
, sizeof(tmp
)) >= sizeof(tmp
) ||
681 strlcat(tmp
, ".XXXXXXXXXX", sizeof(tmp
)) >= sizeof(tmp
) ||
682 strlcpy(old
, identity_file
, sizeof(old
)) >= sizeof(old
) ||
683 strlcat(old
, ".old", sizeof(old
)) >= sizeof(old
))
684 fatal("known_hosts path too long");
686 if ((c
= mkstemp(tmp
)) == -1)
687 fatal("mkstemp: %s", strerror(errno
));
688 if ((out
= fdopen(c
, "w")) == NULL
) {
691 fatal("fdopen: %s", strerror(c
));
696 while (fgets(line
, sizeof(line
), in
)) {
697 if ((cp
= strchr(line
, '\n')) == NULL
) {
698 error("line %d too long: %.40s...", num
+ 1, line
);
710 /* Skip leading whitespace, empty and comment lines. */
711 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
713 if (!*cp
|| *cp
== '\n' || *cp
== '#') {
715 fprintf(out
, "%s\n", cp
);
718 /* Check whether this is a CA key */
719 if (strncasecmp(cp
, CA_MARKER
, sizeof(CA_MARKER
) - 1) == 0 &&
720 (cp
[sizeof(CA_MARKER
) - 1] == ' ' ||
721 cp
[sizeof(CA_MARKER
) - 1] == '\t')) {
723 cp
+= sizeof(CA_MARKER
);
727 /* Find the end of the host name portion. */
728 for (kp
= cp
; *kp
&& *kp
!= ' ' && *kp
!= '\t'; kp
++)
731 if (*kp
== '\0' || *(kp
+ 1) == '\0') {
732 error("line %d missing key: %.40s...",
740 pub
= key_new(KEY_RSA1
);
741 if (key_read(pub
, &kp
) != 1) {
744 pub
= key_new(KEY_UNSPEC
);
745 if (key_read(pub
, &kp
) != 1) {
746 error("line %d invalid key: %.40s...",
754 if (*cp
== HASH_DELIM
) {
755 if (find_host
|| delete_host
) {
756 cp2
= host_hash(name
, cp
, strlen(cp
));
758 error("line %d: invalid hashed "
759 "name: %.64s...", num
, line
);
763 c
= (strcmp(cp2
, cp
) == 0);
764 if (find_host
&& c
) {
765 printf("# Host %s found: "
766 "line %d type %s%s\n", name
,
768 ca
? " (CA key)" : "");
769 printhost(out
, cp
, pub
, ca
, 0);
771 if (delete_host
&& !c
&& !ca
)
772 printhost(out
, cp
, pub
, ca
, 0);
773 } else if (hash_hosts
)
774 printhost(out
, cp
, pub
, ca
, 0);
776 if (find_host
|| delete_host
) {
777 c
= (match_hostname(name
, cp
,
779 if (find_host
&& c
) {
780 printf("# Host %s found: "
781 "line %d type %s%s\n", name
,
783 ca
? " (CA key)" : "");
784 printhost(out
, name
, pub
,
785 ca
, hash_hosts
&& !ca
);
787 if (delete_host
&& !c
&& !ca
)
788 printhost(out
, cp
, pub
, ca
, 0);
789 } else if (hash_hosts
) {
790 for (cp2
= strsep(&cp
, ",");
791 cp2
!= NULL
&& *cp2
!= '\0';
792 cp2
= strsep(&cp
, ",")) {
794 fprintf(stderr
, "Warning: "
795 "ignoring CA key for host: "
797 printhost(out
, cp2
, pub
, ca
, 0);
798 } else if (strcspn(cp2
, "*?!") !=
800 fprintf(stderr
, "Warning: "
801 "ignoring host name with "
802 "metacharacters: %.64s\n",
804 printhost(out
, cp2
, pub
, ca
, 0);
806 printhost(out
, cp2
, pub
, ca
, 1);
816 fprintf(stderr
, "%s is not a valid known_hosts file.\n",
819 fprintf(stderr
, "Not replacing existing known_hosts "
820 "file because of errors\n");
830 /* Backup existing file */
831 if (unlink(old
) == -1 && errno
!= ENOENT
)
832 fatal("unlink %.100s: %s", old
, strerror(errno
));
833 if (link(identity_file
, old
) == -1)
834 fatal("link %.100s to %.100s: %s", identity_file
, old
,
836 /* Move new one into place */
837 if (rename(tmp
, identity_file
) == -1) {
838 error("rename\"%s\" to \"%s\": %s", tmp
, identity_file
,
845 fprintf(stderr
, "%s updated.\n", identity_file
);
846 fprintf(stderr
, "Original contents retained as %s\n", old
);
848 fprintf(stderr
, "WARNING: %s contains unhashed "
850 fprintf(stderr
, "Delete this file to ensure privacy "
859 * Perform changing a passphrase. The argument is the passwd structure
860 * for the current user.
863 do_change_passphrase(struct passwd
*pw
)
866 char *old_passphrase
, *passphrase1
, *passphrase2
;
871 ask_filename(pw
, "Enter file in which the key is");
872 if (stat(identity_file
, &st
) < 0) {
873 perror(identity_file
);
876 /* Try to load the file with empty passphrase. */
877 private = key_load_private(identity_file
, "", &comment
);
878 if (private == NULL
) {
879 if (identity_passphrase
)
880 old_passphrase
= xstrdup(identity_passphrase
);
883 read_passphrase("Enter old passphrase: ",
885 private = key_load_private(identity_file
, old_passphrase
,
887 memset(old_passphrase
, 0, strlen(old_passphrase
));
888 xfree(old_passphrase
);
889 if (private == NULL
) {
890 printf("Bad passphrase.\n");
894 printf("Key has comment '%s'\n", comment
);
896 /* Ask the new passphrase (twice). */
897 if (identity_new_passphrase
) {
898 passphrase1
= xstrdup(identity_new_passphrase
);
902 read_passphrase("Enter new passphrase (empty for no "
903 "passphrase): ", RP_ALLOW_STDIN
);
904 passphrase2
= read_passphrase("Enter same passphrase again: ",
907 /* Verify that they are the same. */
908 if (strcmp(passphrase1
, passphrase2
) != 0) {
909 memset(passphrase1
, 0, strlen(passphrase1
));
910 memset(passphrase2
, 0, strlen(passphrase2
));
913 printf("Pass phrases do not match. Try again.\n");
916 /* Destroy the other copy. */
917 memset(passphrase2
, 0, strlen(passphrase2
));
921 /* Save the file using the new passphrase. */
922 if (!key_save_private(private, identity_file
, passphrase1
, comment
)) {
923 printf("Saving the key failed: %s.\n", identity_file
);
924 memset(passphrase1
, 0, strlen(passphrase1
));
930 /* Destroy the passphrase and the copy of the key in memory. */
931 memset(passphrase1
, 0, strlen(passphrase1
));
933 key_free(private); /* Destroys contents */
936 printf("Your identification has been saved with the new passphrase.\n");
941 * Print the SSHFP RR.
944 do_print_resource_record(struct passwd
*pw
, char *fname
, char *hname
)
947 char *comment
= NULL
;
951 ask_filename(pw
, "Enter file in which the key is");
952 if (stat(fname
, &st
) < 0) {
958 public = key_load_public(fname
, &comment
);
959 if (public != NULL
) {
960 export_dns_rr(hname
, public, stdout
, print_generic
);
968 printf("failed to read v2 public key from %s.\n", fname
);
973 * Change the comment of a private key file.
976 do_change_comment(struct passwd
*pw
)
978 char new_comment
[1024], *comment
, *passphrase
;
986 ask_filename(pw
, "Enter file in which the key is");
987 if (stat(identity_file
, &st
) < 0) {
988 perror(identity_file
);
991 private = key_load_private(identity_file
, "", &comment
);
992 if (private == NULL
) {
993 if (identity_passphrase
)
994 passphrase
= xstrdup(identity_passphrase
);
995 else if (identity_new_passphrase
)
996 passphrase
= xstrdup(identity_new_passphrase
);
998 passphrase
= read_passphrase("Enter passphrase: ",
1000 /* Try to load using the passphrase. */
1001 private = key_load_private(identity_file
, passphrase
, &comment
);
1002 if (private == NULL
) {
1003 memset(passphrase
, 0, strlen(passphrase
));
1005 printf("Bad passphrase.\n");
1009 passphrase
= xstrdup("");
1011 if (private->type
!= KEY_RSA1
) {
1012 fprintf(stderr
, "Comments are only supported for RSA1 keys.\n");
1016 printf("Key now has comment '%s'\n", comment
);
1018 if (identity_comment
) {
1019 strlcpy(new_comment
, identity_comment
, sizeof(new_comment
));
1021 printf("Enter new comment: ");
1023 if (!fgets(new_comment
, sizeof(new_comment
), stdin
)) {
1024 memset(passphrase
, 0, strlen(passphrase
));
1028 new_comment
[strcspn(new_comment
, "\n")] = '\0';
1031 /* Save the file using the new passphrase. */
1032 if (!key_save_private(private, identity_file
, passphrase
, new_comment
)) {
1033 printf("Saving the key failed: %s.\n", identity_file
);
1034 memset(passphrase
, 0, strlen(passphrase
));
1040 memset(passphrase
, 0, strlen(passphrase
));
1042 public = key_from_private(private);
1045 strlcat(identity_file
, ".pub", sizeof(identity_file
));
1046 fd
= open(identity_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
1048 printf("Could not save your public key in %s\n", identity_file
);
1051 f
= fdopen(fd
, "w");
1053 printf("fdopen %s failed\n", identity_file
);
1056 if (!key_write(public, f
))
1057 fprintf(stderr
, "write key failed\n");
1059 fprintf(f
, " %s\n", new_comment
);
1064 printf("The comment in your key file has been changed.\n");
1069 fmt_validity(u_int64_t valid_from
, u_int64_t valid_to
)
1071 char from
[32], to
[32];
1072 static char ret
[64];
1077 if (valid_from
== 0 && valid_to
== 0xffffffffffffffffULL
)
1080 if (valid_from
!= 0) {
1081 /* XXX revisit INT_MAX in 2038 :) */
1082 tt
= valid_from
> INT_MAX
? INT_MAX
: valid_from
;
1083 tm
= localtime(&tt
);
1084 strftime(from
, sizeof(from
), "%Y-%m-%dT%H:%M:%S", tm
);
1086 if (valid_to
!= 0xffffffffffffffffULL
) {
1087 /* XXX revisit INT_MAX in 2038 :) */
1088 tt
= valid_to
> INT_MAX
? INT_MAX
: valid_to
;
1089 tm
= localtime(&tt
);
1090 strftime(to
, sizeof(to
), "%Y-%m-%dT%H:%M:%S", tm
);
1093 if (valid_from
== 0) {
1094 snprintf(ret
, sizeof(ret
), "before %s", to
);
1097 if (valid_to
== 0xffffffffffffffffULL
) {
1098 snprintf(ret
, sizeof(ret
), "after %s", from
);
1102 snprintf(ret
, sizeof(ret
), "from %s to %s", from
, to
);
1107 add_flag_constraint(Buffer
*c
, const char *name
)
1109 debug3("%s: %s", __func__
, name
);
1110 buffer_put_cstring(c
, name
);
1111 buffer_put_string(c
, NULL
, 0);
1115 add_string_constraint(Buffer
*c
, const char *name
, const char *value
)
1119 debug3("%s: %s=%s", __func__
, name
, value
);
1121 buffer_put_cstring(&b
, value
);
1123 buffer_put_cstring(c
, name
);
1124 buffer_put_string(c
, buffer_ptr(&b
), buffer_len(&b
));
1130 prepare_constraint_buf(Buffer
*c
)
1134 if ((constraint_flags
& CONSTRAINT_X_FWD
) != 0)
1135 add_flag_constraint(c
, "permit-X11-forwarding");
1136 if ((constraint_flags
& CONSTRAINT_AGENT_FWD
) != 0)
1137 add_flag_constraint(c
, "permit-agent-forwarding");
1138 if ((constraint_flags
& CONSTRAINT_PORT_FWD
) != 0)
1139 add_flag_constraint(c
, "permit-port-forwarding");
1140 if ((constraint_flags
& CONSTRAINT_PTY
) != 0)
1141 add_flag_constraint(c
, "permit-pty");
1142 if ((constraint_flags
& CONSTRAINT_USER_RC
) != 0)
1143 add_flag_constraint(c
, "permit-user-rc");
1144 if (constraint_command
!= NULL
)
1145 add_string_constraint(c
, "force-command", constraint_command
);
1146 if (constraint_src_addr
!= NULL
)
1147 add_string_constraint(c
, "source-address", constraint_src_addr
);
1151 do_ca_sign(struct passwd
*pw
, int argc
, char **argv
)
1156 char *otmp
, *tmp
, *cp
, *out
, *comment
, **plist
= NULL
;
1159 tmp
= tilde_expand_filename(ca_key_path
, pw
->pw_uid
);
1160 if ((ca
= load_identity(tmp
)) == NULL
)
1161 fatal("Couldn't load CA key \"%s\"", tmp
);
1164 for (i
= 0; i
< argc
; i
++) {
1165 /* Split list of principals */
1167 if (cert_principals
!= NULL
) {
1168 otmp
= tmp
= xstrdup(cert_principals
);
1170 for (; (cp
= strsep(&tmp
, ",")) != NULL
; n
++) {
1171 plist
= xrealloc(plist
, n
+ 1, sizeof(*plist
));
1172 if (*(plist
[n
] = xstrdup(cp
)) == '\0')
1173 fatal("Empty principal name");
1178 tmp
= tilde_expand_filename(argv
[i
], pw
->pw_uid
);
1179 if ((public = key_load_public(tmp
, &comment
)) == NULL
)
1180 fatal("%s: unable to open \"%s\"", __func__
, tmp
);
1181 if (public->type
!= KEY_RSA
&& public->type
!= KEY_DSA
)
1182 fatal("%s: key \"%s\" type %s cannot be certified",
1183 __func__
, tmp
, key_type(public));
1185 /* Prepare certificate to sign */
1186 if (key_to_certified(public) != 0)
1187 fatal("Could not upgrade key %s to certificate", tmp
);
1188 public->cert
->type
= cert_key_type
;
1189 public->cert
->key_id
= xstrdup(cert_key_id
);
1190 public->cert
->nprincipals
= n
;
1191 public->cert
->principals
= plist
;
1192 public->cert
->valid_after
= cert_valid_from
;
1193 public->cert
->valid_before
= cert_valid_to
;
1194 prepare_constraint_buf(&public->cert
->constraints
);
1195 public->cert
->signature_key
= key_from_private(ca
);
1197 if (key_certify(public, ca
) != 0)
1198 fatal("Couldn't not certify key %s", tmp
);
1200 if ((cp
= strrchr(tmp
, '.')) != NULL
&& strcmp(cp
, ".pub") == 0)
1202 xasprintf(&out
, "%s-cert.pub", tmp
);
1205 if ((fd
= open(out
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) == -1)
1206 fatal("Could not open \"%s\" for writing: %s", out
,
1208 if ((f
= fdopen(fd
, "w")) == NULL
)
1209 fatal("%s: fdopen: %s", __func__
, strerror(errno
));
1210 if (!key_write(public, f
))
1211 fatal("Could not write certified key to %s", out
);
1212 fprintf(f
, " %s\n", comment
);
1216 logit("Signed %s key %s: id \"%s\"%s%s valid %s",
1217 cert_key_type
== SSH2_CERT_TYPE_USER
?"user":"host",
1219 cert_principals
!= NULL
? " for " : "",
1220 cert_principals
!= NULL
? cert_principals
: "",
1221 fmt_validity(cert_valid_from
, cert_valid_to
));
1230 parse_relative_time(const char *s
, time_t now
)
1234 mul
= *s
== '-' ? -1 : 1;
1236 if ((secs
= convtime(s
+ 1)) == -1)
1237 fatal("Invalid relative certificate time %s", s
);
1238 if (mul
== -1 && secs
> now
)
1239 fatal("Certificate time %s cannot be represented", s
);
1240 return now
+ (u_int64_t
)(secs
* mul
);
1244 parse_absolute_time(const char *s
)
1251 * POSIX strptime says "The application shall ensure that there
1252 * is white-space or other non-alphanumeric characters between
1253 * any two conversion specifications" so arrange things this way.
1255 switch (strlen(s
)) {
1258 snprintf(buf
, sizeof(buf
), "%.4s-%.2s-%.2s", s
, s
+ 4, s
+ 6);
1261 fmt
= "%Y-%m-%dT%H:%M:%S";
1262 snprintf(buf
, sizeof(buf
), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
1263 s
, s
+ 4, s
+ 6, s
+ 8, s
+ 10, s
+ 12);
1266 fatal("Invalid certificate time format %s", s
);
1269 bzero(&tm
, sizeof(tm
));
1270 if (strptime(buf
, fmt
, &tm
) == NULL
)
1271 fatal("Invalid certificate time %s", s
);
1272 if ((tt
= mktime(&tm
)) < 0)
1273 fatal("Certificate time %s cannot be represented", s
);
1274 return (u_int64_t
)tt
;
1278 parse_cert_times(char *timespec
)
1281 time_t now
= time(NULL
);
1284 /* +timespec relative to now */
1285 if (*timespec
== '+' && strchr(timespec
, ':') == NULL
) {
1286 if ((secs
= convtime(timespec
+ 1)) == -1)
1287 fatal("Invalid relative certificate life %s", timespec
);
1288 cert_valid_to
= now
+ secs
;
1290 * Backdate certificate one minute to avoid problems on hosts
1291 * with poorly-synchronised clocks.
1293 cert_valid_from
= ((now
- 59)/ 60) * 60;
1299 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1300 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1302 from
= xstrdup(timespec
);
1303 to
= strchr(from
, ':');
1304 if (to
== NULL
|| from
== to
|| *(to
+ 1) == '\0')
1305 fatal("Invalid certificate life specification %s", timespec
);
1308 if (*from
== '-' || *from
== '+')
1309 cert_valid_from
= parse_relative_time(from
, now
);
1311 cert_valid_from
= parse_absolute_time(from
);
1313 if (*to
== '-' || *to
== '+')
1314 cert_valid_to
= parse_relative_time(to
, cert_valid_from
);
1316 cert_valid_to
= parse_absolute_time(to
);
1318 if (cert_valid_to
<= cert_valid_from
)
1319 fatal("Empty certificate validity interval");
1324 add_cert_constraint(char *opt
)
1328 if (strcmp(opt
, "clear") == 0)
1329 constraint_flags
= 0;
1330 else if (strcasecmp(opt
, "no-x11-forwarding") == 0)
1331 constraint_flags
&= ~CONSTRAINT_X_FWD
;
1332 else if (strcasecmp(opt
, "permit-x11-forwarding") == 0)
1333 constraint_flags
|= CONSTRAINT_X_FWD
;
1334 else if (strcasecmp(opt
, "no-agent-forwarding") == 0)
1335 constraint_flags
&= ~CONSTRAINT_AGENT_FWD
;
1336 else if (strcasecmp(opt
, "permit-agent-forwarding") == 0)
1337 constraint_flags
|= CONSTRAINT_AGENT_FWD
;
1338 else if (strcasecmp(opt
, "no-port-forwarding") == 0)
1339 constraint_flags
&= ~CONSTRAINT_PORT_FWD
;
1340 else if (strcasecmp(opt
, "permit-port-forwarding") == 0)
1341 constraint_flags
|= CONSTRAINT_PORT_FWD
;
1342 else if (strcasecmp(opt
, "no-pty") == 0)
1343 constraint_flags
&= ~CONSTRAINT_PTY
;
1344 else if (strcasecmp(opt
, "permit-pty") == 0)
1345 constraint_flags
|= CONSTRAINT_PTY
;
1346 else if (strcasecmp(opt
, "no-user-rc") == 0)
1347 constraint_flags
&= ~CONSTRAINT_USER_RC
;
1348 else if (strcasecmp(opt
, "permit-user-rc") == 0)
1349 constraint_flags
|= CONSTRAINT_USER_RC
;
1350 else if (strncasecmp(opt
, "force-command=", 14) == 0) {
1353 fatal("Empty force-command constraint");
1354 if (constraint_command
!= NULL
)
1355 fatal("force-command already specified");
1356 constraint_command
= xstrdup(val
);
1357 } else if (strncasecmp(opt
, "source-address=", 15) == 0) {
1360 fatal("Empty source-address constraint");
1361 if (constraint_src_addr
!= NULL
)
1362 fatal("source-address already specified");
1363 if (addr_match_cidr_list(NULL
, val
) != 0)
1364 fatal("Invalid source-address list");
1365 constraint_src_addr
= xstrdup(val
);
1367 fatal("Unsupported certificate constraint \"%s\"", opt
);
1371 do_show_cert(struct passwd
*pw
)
1375 char *key_fp
, *ca_fp
;
1376 Buffer constraints
, constraint
;
1377 u_char
*name
, *data
;
1381 ask_filename(pw
, "Enter file in which the key is");
1382 if (stat(identity_file
, &st
) < 0) {
1383 perror(identity_file
);
1386 if ((key
= key_load_public(identity_file
, NULL
)) == NULL
)
1387 fatal("%s is not a public key", identity_file
);
1388 if (!key_is_cert(key
))
1389 fatal("%s is not a certificate", identity_file
);
1391 key_fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
1392 ca_fp
= key_fingerprint(key
->cert
->signature_key
,
1393 SSH_FP_MD5
, SSH_FP_HEX
);
1395 printf("%s:\n", identity_file
);
1396 printf(" %s %s certificate %s\n", key_type(key
),
1397 key_cert_type(key
), key_fp
);
1398 printf(" Signed by %s CA %s\n",
1399 key_type(key
->cert
->signature_key
), ca_fp
);
1400 printf(" Key ID \"%s\"\n", key
->cert
->key_id
);
1401 printf(" Valid: %s\n",
1402 fmt_validity(key
->cert
->valid_after
, key
->cert
->valid_before
));
1403 printf(" Principals: ");
1404 if (key
->cert
->nprincipals
== 0)
1407 for (i
= 0; i
< key
->cert
->nprincipals
; i
++)
1409 key
->cert
->principals
[i
]);
1412 printf(" Constraints: ");
1413 if (buffer_len(&key
->cert
->constraints
) == 0)
1417 buffer_init(&constraints
);
1418 buffer_append(&constraints
,
1419 buffer_ptr(&key
->cert
->constraints
),
1420 buffer_len(&key
->cert
->constraints
));
1421 buffer_init(&constraint
);
1422 while (buffer_len(&constraints
) != 0) {
1423 name
= buffer_get_string(&constraints
, NULL
);
1424 data
= buffer_get_string_ptr(&constraints
, &dlen
);
1425 buffer_append(&constraint
, data
, dlen
);
1426 printf(" %s", name
);
1427 if (strcmp(name
, "permit-X11-forwarding") == 0 ||
1428 strcmp(name
, "permit-agent-forwarding") == 0 ||
1429 strcmp(name
, "permit-port-forwarding") == 0 ||
1430 strcmp(name
, "permit-pty") == 0 ||
1431 strcmp(name
, "permit-user-rc") == 0)
1433 else if (strcmp(name
, "force-command") == 0 ||
1434 strcmp(name
, "source-address") == 0) {
1435 data
= buffer_get_string(&constraint
, NULL
);
1436 printf(" %s\n", data
);
1439 printf(" UNKNOWN CONSTRAINT (len %u)\n",
1440 buffer_len(&constraint
));
1441 buffer_clear(&constraint
);
1444 if (buffer_len(&constraint
) != 0)
1445 fatal("Constraint corrupt: extra data at end");
1447 buffer_free(&constraint
);
1448 buffer_free(&constraints
);
1457 fprintf(stderr
, "usage: %s [options]\n", __progname
);
1458 fprintf(stderr
, "Options:\n");
1459 fprintf(stderr
, " -a trials Number of trials for screening DH-GEX moduli.\n");
1460 fprintf(stderr
, " -B Show bubblebabble digest of key file.\n");
1461 fprintf(stderr
, " -b bits Number of bits in the key to create.\n");
1462 fprintf(stderr
, " -C comment Provide new comment.\n");
1463 fprintf(stderr
, " -c Change comment in private and public key files.\n");
1464 #ifdef ENABLE_PKCS11
1465 fprintf(stderr
, " -D pkcs11 Download public key from pkcs11 token.\n");
1467 fprintf(stderr
, " -e Convert OpenSSH to RFC 4716 key file.\n");
1468 fprintf(stderr
, " -F hostname Find hostname in known hosts file.\n");
1469 fprintf(stderr
, " -f filename Filename of the key file.\n");
1470 fprintf(stderr
, " -G file Generate candidates for DH-GEX moduli.\n");
1471 fprintf(stderr
, " -g Use generic DNS resource record format.\n");
1472 fprintf(stderr
, " -H Hash names in known_hosts file.\n");
1473 fprintf(stderr
, " -h Generate host certificate instead of a user certificate.\n");
1474 fprintf(stderr
, " -I key_id Key identifier to include in certificate.\n");
1475 fprintf(stderr
, " -i Convert RFC 4716 to OpenSSH key file.\n");
1476 fprintf(stderr
, " -L Print the contents of a certificate.\n");
1477 fprintf(stderr
, " -l Show fingerprint of key file.\n");
1478 fprintf(stderr
, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1479 fprintf(stderr
, " -n name,... User/host principal names to include in certificate\n");
1480 fprintf(stderr
, " -N phrase Provide new passphrase.\n");
1481 fprintf(stderr
, " -O cnstr Specify a certificate constraint.\n");
1482 fprintf(stderr
, " -P phrase Provide old passphrase.\n");
1483 fprintf(stderr
, " -p Change passphrase of private key file.\n");
1484 fprintf(stderr
, " -q Quiet.\n");
1485 fprintf(stderr
, " -R hostname Remove host from known_hosts file.\n");
1486 fprintf(stderr
, " -r hostname Print DNS resource record.\n");
1487 fprintf(stderr
, " -s ca_key Certify keys with CA key.\n");
1488 fprintf(stderr
, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1489 fprintf(stderr
, " -T file Screen candidates for DH-GEX moduli.\n");
1490 fprintf(stderr
, " -t type Specify type of key to create.\n");
1491 fprintf(stderr
, " -V from:to Specify certificate validity interval.\n");
1492 fprintf(stderr
, " -v Verbose.\n");
1493 fprintf(stderr
, " -W gen Generator to use for generating DH-GEX moduli.\n");
1494 fprintf(stderr
, " -y Read private key file and print public key.\n");
1500 * Main program for key management.
1503 main(int argc
, char **argv
)
1505 char dotsshdir
[MAXPATHLEN
], comment
[1024], *passphrase1
, *passphrase2
;
1506 char out_file
[MAXPATHLEN
], *pkcs11provider
= NULL
;
1507 char *rr_hostname
= NULL
;
1508 Key
*private, *public;
1512 u_int32_t memory
= 0, generator_wanted
= 0, trials
= 100;
1513 int do_gen_candidates
= 0, do_screen_candidates
= 0;
1514 BIGNUM
*start
= NULL
;
1519 extern char *optarg
;
1521 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1524 __progname
= ssh_get_progname(argv
[0]);
1526 SSLeay_add_all_algorithms();
1527 log_init(argv
[0], SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_USER
, 1);
1532 /* we need this for the home * directory. */
1533 pw
= getpwuid(getuid());
1535 printf("You don't exist, go away!\n");
1538 if (gethostname(hostname
, sizeof(hostname
)) < 0) {
1539 perror("gethostname");
1543 while ((opt
= getopt(argc
, argv
, "degiqpclBHLhvxXyF:b:f:t:D:I:P:N:n:"
1544 "O:C:r:g:R:T:G:M:S:s:a:V:W:")) != -1) {
1547 bits
= (u_int32_t
)strtonum(optarg
, 768, 32768, &errstr
);
1549 fatal("Bits has bad value %s (%s)",
1554 rr_hostname
= optarg
;
1560 cert_key_id
= optarg
;
1564 rr_hostname
= optarg
;
1570 print_fingerprint
= 1;
1573 print_bubblebabble
= 1;
1576 cert_principals
= optarg
;
1579 change_passphrase
= 1;
1585 if (strlcpy(identity_file
, optarg
, sizeof(identity_file
)) >=
1586 sizeof(identity_file
))
1587 fatal("Identity filename too long");
1594 identity_passphrase
= optarg
;
1597 identity_new_passphrase
= optarg
;
1600 add_cert_constraint(optarg
);
1603 identity_comment
= optarg
;
1611 convert_to_ssh2
= 1;
1614 cert_key_type
= SSH2_CERT_TYPE_HOST
;
1615 constraint_flags
= 0;
1620 convert_from_ssh2
= 1;
1626 key_type_name
= "dsa";
1629 ca_key_path
= optarg
;
1632 key_type_name
= optarg
;
1635 pkcs11provider
= optarg
;
1638 if (log_level
== SYSLOG_LEVEL_INFO
)
1639 log_level
= SYSLOG_LEVEL_DEBUG1
;
1641 if (log_level
>= SYSLOG_LEVEL_DEBUG1
&&
1642 log_level
< SYSLOG_LEVEL_DEBUG3
)
1647 rr_hostname
= optarg
;
1650 generator_wanted
= (u_int32_t
)strtonum(optarg
, 1,
1653 fatal("Desired generator has bad value: %s (%s)",
1657 trials
= (u_int32_t
)strtonum(optarg
, 1, UINT_MAX
, &errstr
);
1659 fatal("Invalid number of trials: %s (%s)",
1663 memory
= (u_int32_t
)strtonum(optarg
, 1, UINT_MAX
, &errstr
);
1665 fatal("Memory limit is %s: %s", errstr
, optarg
);
1669 do_gen_candidates
= 1;
1670 if (strlcpy(out_file
, optarg
, sizeof(out_file
)) >=
1672 fatal("Output filename too long");
1675 do_screen_candidates
= 1;
1676 if (strlcpy(out_file
, optarg
, sizeof(out_file
)) >=
1678 fatal("Output filename too long");
1681 /* XXX - also compare length against bits */
1682 if (BN_hex2bn(&start
, optarg
) == 0)
1683 fatal("Invalid start point.");
1686 parse_cert_times(optarg
);
1695 log_init(argv
[0], log_level
, SYSLOG_FACILITY_USER
, 1);
1700 if (ca_key_path
!= NULL
) {
1702 printf("Too few arguments.\n");
1705 } else if (argc
> 0) {
1706 printf("Too many arguments.\n");
1709 if (change_passphrase
&& change_comment
) {
1710 printf("Can only have one of -p and -c.\n");
1713 if (print_fingerprint
&& (delete_host
|| hash_hosts
)) {
1714 printf("Cannot use -l with -D or -R.\n");
1717 if (ca_key_path
!= NULL
) {
1718 if (cert_key_id
== NULL
)
1719 fatal("Must specify key id (-I) when certifying");
1720 do_ca_sign(pw
, argc
, argv
);
1724 if (delete_host
|| hash_hosts
|| find_host
)
1725 do_known_hosts(pw
, rr_hostname
);
1726 if (print_fingerprint
|| print_bubblebabble
)
1728 if (change_passphrase
)
1729 do_change_passphrase(pw
);
1731 do_change_comment(pw
);
1732 if (convert_to_ssh2
)
1733 do_convert_to_ssh2(pw
);
1734 if (convert_from_ssh2
)
1735 do_convert_from_ssh2(pw
);
1737 do_print_public(pw
);
1738 if (rr_hostname
!= NULL
) {
1741 if (have_identity
) {
1742 n
= do_print_resource_record(pw
,
1743 identity_file
, rr_hostname
);
1745 perror(identity_file
);
1751 n
+= do_print_resource_record(pw
,
1752 _PATH_HOST_RSA_KEY_FILE
, rr_hostname
);
1753 n
+= do_print_resource_record(pw
,
1754 _PATH_HOST_DSA_KEY_FILE
, rr_hostname
);
1757 fatal("no keys found.");
1761 if (pkcs11provider
!= NULL
)
1762 do_download(pw
, pkcs11provider
);
1764 if (do_gen_candidates
) {
1765 FILE *out
= fopen(out_file
, "w");
1768 error("Couldn't open modulus candidate file \"%s\": %s",
1769 out_file
, strerror(errno
));
1773 bits
= DEFAULT_BITS
;
1774 if (gen_candidates(out
, memory
, bits
, start
) != 0)
1775 fatal("modulus candidate generation failed");
1780 if (do_screen_candidates
) {
1782 FILE *out
= fopen(out_file
, "w");
1784 if (have_identity
&& strcmp(identity_file
, "-") != 0) {
1785 if ((in
= fopen(identity_file
, "r")) == NULL
) {
1786 fatal("Couldn't open modulus candidate "
1787 "file \"%s\": %s", identity_file
,
1794 fatal("Couldn't open moduli file \"%s\": %s",
1795 out_file
, strerror(errno
));
1797 if (prime_test(in
, out
, trials
, generator_wanted
) != 0)
1798 fatal("modulus screening failed");
1804 if (key_type_name
== NULL
)
1805 key_type_name
= "rsa";
1807 type
= key_type_from_name(key_type_name
);
1808 if (type
== KEY_UNSPEC
) {
1809 fprintf(stderr
, "unknown key type %s\n", key_type_name
);
1813 bits
= (type
== KEY_DSA
) ? DEFAULT_BITS_DSA
: DEFAULT_BITS
;
1814 if (type
== KEY_DSA
&& bits
!= 1024)
1815 fatal("DSA keys must be 1024 bits");
1817 printf("Generating public/private %s key pair.\n", key_type_name
);
1818 private = key_generate(type
, bits
);
1819 if (private == NULL
) {
1820 fprintf(stderr
, "key_generate failed\n");
1823 public = key_from_private(private);
1826 ask_filename(pw
, "Enter file in which to save the key");
1828 /* Create ~/.ssh directory if it doesn't already exist. */
1829 snprintf(dotsshdir
, sizeof dotsshdir
, "%s/%s", pw
->pw_dir
, _PATH_SSH_USER_DIR
);
1830 if (strstr(identity_file
, dotsshdir
) != NULL
&&
1831 stat(dotsshdir
, &st
) < 0) {
1832 if (mkdir(dotsshdir
, 0700) < 0)
1833 error("Could not create directory '%s'.", dotsshdir
);
1835 printf("Created directory '%s'.\n", dotsshdir
);
1837 /* If the file already exists, ask the user to confirm. */
1838 if (stat(identity_file
, &st
) >= 0) {
1840 printf("%s already exists.\n", identity_file
);
1841 printf("Overwrite (y/n)? ");
1843 if (fgets(yesno
, sizeof(yesno
), stdin
) == NULL
)
1845 if (yesno
[0] != 'y' && yesno
[0] != 'Y')
1848 /* Ask for a passphrase (twice). */
1849 if (identity_passphrase
)
1850 passphrase1
= xstrdup(identity_passphrase
);
1851 else if (identity_new_passphrase
)
1852 passphrase1
= xstrdup(identity_new_passphrase
);
1856 read_passphrase("Enter passphrase (empty for no "
1857 "passphrase): ", RP_ALLOW_STDIN
);
1858 passphrase2
= read_passphrase("Enter same passphrase again: ",
1860 if (strcmp(passphrase1
, passphrase2
) != 0) {
1862 * The passphrases do not match. Clear them and
1865 memset(passphrase1
, 0, strlen(passphrase1
));
1866 memset(passphrase2
, 0, strlen(passphrase2
));
1869 printf("Passphrases do not match. Try again.\n");
1870 goto passphrase_again
;
1872 /* Clear the other copy of the passphrase. */
1873 memset(passphrase2
, 0, strlen(passphrase2
));
1877 if (identity_comment
) {
1878 strlcpy(comment
, identity_comment
, sizeof(comment
));
1880 /* Create default comment field for the passphrase. */
1881 snprintf(comment
, sizeof comment
, "%s@%s", pw
->pw_name
, hostname
);
1884 /* Save the key with the given passphrase and comment. */
1885 if (!key_save_private(private, identity_file
, passphrase1
, comment
)) {
1886 printf("Saving the key failed: %s.\n", identity_file
);
1887 memset(passphrase1
, 0, strlen(passphrase1
));
1891 /* Clear the passphrase. */
1892 memset(passphrase1
, 0, strlen(passphrase1
));
1895 /* Clear the private key and the random number generator. */
1900 printf("Your identification has been saved in %s.\n", identity_file
);
1902 strlcat(identity_file
, ".pub", sizeof(identity_file
));
1903 fd
= open(identity_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
1905 printf("Could not save your public key in %s\n", identity_file
);
1908 f
= fdopen(fd
, "w");
1910 printf("fdopen %s failed\n", identity_file
);
1913 if (!key_write(public, f
))
1914 fprintf(stderr
, "write key failed\n");
1915 fprintf(f
, " %s\n", comment
);
1919 char *fp
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_HEX
);
1920 char *ra
= key_fingerprint(public, SSH_FP_MD5
,
1922 printf("Your public key has been saved in %s.\n",
1924 printf("The key fingerprint is:\n");
1925 printf("%s %s\n", fp
, comment
);
1926 printf("The key's randomart image is:\n");