1 /* $OpenBSD: ssh-keygen.c,v 1.197 2010/08/04 06:07:11 djm 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 /* Certificate serial number */
109 long long cert_serial
= 0;
111 /* Key type when certifying */
112 u_int cert_key_type
= SSH2_CERT_TYPE_USER
;
114 /* "key ID" of signed key */
115 char *cert_key_id
= NULL
;
117 /* Comma-separated list of principal names for certifying keys */
118 char *cert_principals
= NULL
;
120 /* Validity period for certificates */
121 u_int64_t cert_valid_from
= 0;
122 u_int64_t cert_valid_to
= ~0ULL;
124 /* Certificate options */
125 #define CERTOPT_X_FWD (1)
126 #define CERTOPT_AGENT_FWD (1<<1)
127 #define CERTOPT_PORT_FWD (1<<2)
128 #define CERTOPT_PTY (1<<3)
129 #define CERTOPT_USER_RC (1<<4)
130 #define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
131 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
132 u_int32_t certflags_flags
= CERTOPT_DEFAULT
;
133 char *certflags_command
= NULL
;
134 char *certflags_src_addr
= NULL
;
136 /* Conversion to/from various formats */
138 int convert_from
= 0;
143 } convert_format
= FMT_RFC4716
;
144 int print_public
= 0;
145 int print_generic
= 0;
147 char *key_type_name
= NULL
;
149 /* Load key from this PKCS#11 provider */
150 char *pkcs11provider
= NULL
;
153 extern char *__progname
;
155 char hostname
[MAXHOSTNAMELEN
];
158 int gen_candidates(FILE *, u_int32_t
, u_int32_t
, BIGNUM
*);
159 int prime_test(FILE *, FILE *, u_int32_t
, u_int32_t
);
162 ask_filename(struct passwd
*pw
, const char *prompt
)
167 if (key_type_name
== NULL
)
168 name
= _PATH_SSH_CLIENT_ID_RSA
;
170 switch (key_type_from_name(key_type_name
)) {
172 name
= _PATH_SSH_CLIENT_IDENTITY
;
175 case KEY_DSA_CERT_V00
:
177 name
= _PATH_SSH_CLIENT_ID_DSA
;
180 case KEY_RSA_CERT_V00
:
182 name
= _PATH_SSH_CLIENT_ID_RSA
;
185 fprintf(stderr
, "bad key type\n");
190 snprintf(identity_file
, sizeof(identity_file
), "%s/%s", pw
->pw_dir
, name
);
191 fprintf(stderr
, "%s (%s): ", prompt
, identity_file
);
192 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
194 buf
[strcspn(buf
, "\n")] = '\0';
195 if (strcmp(buf
, "") != 0)
196 strlcpy(identity_file
, buf
, sizeof(identity_file
));
201 load_identity(char *filename
)
206 prv
= key_load_private(filename
, "", NULL
);
208 if (identity_passphrase
)
209 pass
= xstrdup(identity_passphrase
);
211 pass
= read_passphrase("Enter passphrase: ",
213 prv
= key_load_private(filename
, pass
, NULL
);
214 memset(pass
, 0, strlen(pass
));
220 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
221 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
222 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
223 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
226 do_convert_to_ssh2(struct passwd
*pw
, Key
*k
)
232 if (key_to_blob(k
, &blob
, &len
) <= 0) {
233 fprintf(stderr
, "key_to_blob failed\n");
236 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
237 snprintf(comment
, sizeof(comment
),
238 "%u-bit %s, converted by %s@%s from OpenSSH",
239 key_size(k
), key_type(k
),
240 pw
->pw_name
, hostname
);
242 fprintf(stdout
, "%s\n", SSH_COM_PUBLIC_BEGIN
);
243 fprintf(stdout
, "Comment: \"%s\"\n", comment
);
244 dump_base64(stdout
, blob
, len
);
245 fprintf(stdout
, "%s\n", SSH_COM_PUBLIC_END
);
252 do_convert_to_pkcs8(Key
*k
)
254 switch (key_type_plain(k
->type
)) {
256 if (!PEM_write_RSA_PUBKEY(stdout
, k
->rsa
))
257 fatal("PEM_write_RSA_PUBKEY failed");
260 if (!PEM_write_DSA_PUBKEY(stdout
, k
->dsa
))
261 fatal("PEM_write_DSA_PUBKEY failed");
264 fatal("%s: unsupported key type %s", __func__
, key_type(k
));
270 do_convert_to_pem(Key
*k
)
272 switch (key_type_plain(k
->type
)) {
274 if (!PEM_write_RSAPublicKey(stdout
, k
->rsa
))
275 fatal("PEM_write_RSAPublicKey failed");
277 #if notyet /* OpenSSH 0.9.8 lacks this function */
279 if (!PEM_write_DSAPublicKey(stdout
, k
->dsa
))
280 fatal("PEM_write_DSAPublicKey failed");
284 fatal("%s: unsupported key type %s", __func__
, key_type(k
));
290 do_convert_to(struct passwd
*pw
)
296 ask_filename(pw
, "Enter file in which the key is");
297 if (stat(identity_file
, &st
) < 0)
298 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
299 if ((k
= key_load_public(identity_file
, NULL
)) == NULL
) {
300 if ((k
= load_identity(identity_file
)) == NULL
) {
301 fprintf(stderr
, "load failed\n");
305 if (k
->type
== KEY_RSA1
) {
306 fprintf(stderr
, "version 1 keys are not supported\n");
310 switch (convert_format
) {
312 do_convert_to_ssh2(pw
, k
);
315 do_convert_to_pkcs8(k
);
318 do_convert_to_pem(k
);
321 fatal("%s: unknown key format %d", __func__
, convert_format
);
327 buffer_get_bignum_bits(Buffer
*b
, BIGNUM
*value
)
329 u_int bignum_bits
= buffer_get_int(b
);
330 u_int bytes
= (bignum_bits
+ 7) / 8;
332 if (buffer_len(b
) < bytes
)
333 fatal("buffer_get_bignum_bits: input buffer too small: "
334 "need %d have %d", bytes
, buffer_len(b
));
335 if (BN_bin2bn(buffer_ptr(b
), bytes
, value
) == NULL
)
336 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
337 buffer_consume(b
, bytes
);
341 do_convert_private_ssh2_from_blob(u_char
*blob
, u_int blen
)
346 u_char
*sig
, data
[] = "abcde12345";
347 int magic
, rlen
, ktype
, i1
, i2
, i3
, i4
;
352 buffer_append(&b
, blob
, blen
);
354 magic
= buffer_get_int(&b
);
355 if (magic
!= SSH_COM_PRIVATE_KEY_MAGIC
) {
356 error("bad magic 0x%x != 0x%x", magic
, SSH_COM_PRIVATE_KEY_MAGIC
);
360 i1
= buffer_get_int(&b
);
361 type
= buffer_get_string(&b
, NULL
);
362 cipher
= buffer_get_string(&b
, NULL
);
363 i2
= buffer_get_int(&b
);
364 i3
= buffer_get_int(&b
);
365 i4
= buffer_get_int(&b
);
366 debug("ignore (%d %d %d %d)", i1
, i2
, i3
, i4
);
367 if (strcmp(cipher
, "none") != 0) {
368 error("unsupported cipher %s", cipher
);
376 if (strstr(type
, "dsa")) {
378 } else if (strstr(type
, "rsa")) {
385 key
= key_new_private(ktype
);
390 buffer_get_bignum_bits(&b
, key
->dsa
->p
);
391 buffer_get_bignum_bits(&b
, key
->dsa
->g
);
392 buffer_get_bignum_bits(&b
, key
->dsa
->q
);
393 buffer_get_bignum_bits(&b
, key
->dsa
->pub_key
);
394 buffer_get_bignum_bits(&b
, key
->dsa
->priv_key
);
397 e
= buffer_get_char(&b
);
401 e
+= buffer_get_char(&b
);
404 e
+= buffer_get_char(&b
);
407 if (!BN_set_word(key
->rsa
->e
, e
)) {
412 buffer_get_bignum_bits(&b
, key
->rsa
->d
);
413 buffer_get_bignum_bits(&b
, key
->rsa
->n
);
414 buffer_get_bignum_bits(&b
, key
->rsa
->iqmp
);
415 buffer_get_bignum_bits(&b
, key
->rsa
->q
);
416 buffer_get_bignum_bits(&b
, key
->rsa
->p
);
417 rsa_generate_additional_parameters(key
->rsa
);
420 rlen
= buffer_len(&b
);
422 error("do_convert_private_ssh2_from_blob: "
423 "remaining bytes in key blob %d", rlen
);
427 key_sign(key
, &sig
, &slen
, data
, sizeof(data
));
428 key_verify(key
, sig
, slen
, data
, sizeof(data
));
434 get_line(FILE *fp
, char *line
, size_t len
)
440 while ((c
= fgetc(fp
)) != EOF
) {
441 if (pos
>= len
- 1) {
442 fprintf(stderr
, "input line too long.\n");
448 if (c
!= EOF
&& c
!= '\n' && ungetc(c
, fp
) == EOF
) {
449 fprintf(stderr
, "unget: %s\n", strerror(errno
));
464 do_convert_from_ssh2(struct passwd
*pw
, Key
**k
, int *private)
474 if ((fp
= fopen(identity_file
, "r")) == NULL
)
475 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
477 while ((blen
= get_line(fp
, line
, sizeof(line
))) != -1) {
478 if (line
[blen
- 1] == '\\')
480 if (strncmp(line
, "----", 4) == 0 ||
481 strstr(line
, ": ") != NULL
) {
482 if (strstr(line
, SSH_COM_PRIVATE_BEGIN
) != NULL
)
484 if (strstr(line
, " END ") != NULL
) {
487 /* fprintf(stderr, "ignore: %s", line); */
492 /* fprintf(stderr, "escaped: %s", line); */
495 strlcat(encoded
, line
, sizeof(encoded
));
497 len
= strlen(encoded
);
498 if (((len
% 4) == 3) &&
499 (encoded
[len
-1] == '=') &&
500 (encoded
[len
-2] == '=') &&
501 (encoded
[len
-3] == '='))
502 encoded
[len
-3] = '\0';
503 blen
= uudecode(encoded
, blob
, sizeof(blob
));
505 fprintf(stderr
, "uudecode failed.\n");
509 do_convert_private_ssh2_from_blob(blob
, blen
) :
510 key_from_blob(blob
, blen
);
512 fprintf(stderr
, "decode blob failed.\n");
519 do_convert_from_pkcs8(Key
**k
, int *private)
524 if ((fp
= fopen(identity_file
, "r")) == NULL
)
525 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
526 if ((pubkey
= PEM_read_PUBKEY(fp
, NULL
, NULL
, NULL
)) == NULL
) {
527 fatal("%s: %s is not a recognised public key format", __func__
,
531 switch (EVP_PKEY_type(pubkey
->type
)) {
533 *k
= key_new(KEY_UNSPEC
);
534 (*k
)->type
= KEY_RSA
;
535 (*k
)->rsa
= EVP_PKEY_get1_RSA(pubkey
);
538 *k
= key_new(KEY_UNSPEC
);
539 (*k
)->type
= KEY_DSA
;
540 (*k
)->dsa
= EVP_PKEY_get1_DSA(pubkey
);
543 fatal("%s: unsupported pubkey type %d", __func__
,
544 EVP_PKEY_type(pubkey
->type
));
546 EVP_PKEY_free(pubkey
);
551 do_convert_from_pem(Key
**k
, int *private)
559 if ((fp
= fopen(identity_file
, "r")) == NULL
)
560 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
561 if ((rsa
= PEM_read_RSAPublicKey(fp
, NULL
, NULL
, NULL
)) != NULL
) {
562 *k
= key_new(KEY_UNSPEC
);
563 (*k
)->type
= KEY_RSA
;
568 #if notyet /* OpenSSH 0.9.8 lacks this function */
570 if ((dsa
= PEM_read_DSAPublicKey(fp
, NULL
, NULL
, NULL
)) != NULL
) {
571 *k
= key_new(KEY_UNSPEC
);
572 (*k
)->type
= KEY_DSA
;
578 fatal("%s: unrecognised raw private key format", __func__
);
582 do_convert_from(struct passwd
*pw
)
585 int private = 0, ok
= 0;
589 ask_filename(pw
, "Enter file in which the key is");
590 if (stat(identity_file
, &st
) < 0)
591 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
593 switch (convert_format
) {
595 do_convert_from_ssh2(pw
, &k
, &private);
598 do_convert_from_pkcs8(&k
, &private);
601 do_convert_from_pem(&k
, &private);
604 fatal("%s: unknown key format %d", __func__
, convert_format
);
608 ok
= key_write(k
, stdout
);
610 fprintf(stdout
, "\n");
614 ok
= PEM_write_DSAPrivateKey(stdout
, k
->dsa
, NULL
,
615 NULL
, 0, NULL
, NULL
);
618 ok
= PEM_write_RSAPrivateKey(stdout
, k
->rsa
, NULL
,
619 NULL
, 0, NULL
, NULL
);
622 fatal("%s: unsupported key type %s", __func__
,
628 fprintf(stderr
, "key write failed\n");
636 do_print_public(struct passwd
*pw
)
642 ask_filename(pw
, "Enter file in which the key is");
643 if (stat(identity_file
, &st
) < 0) {
644 perror(identity_file
);
647 prv
= load_identity(identity_file
);
649 fprintf(stderr
, "load failed\n");
652 if (!key_write(prv
, stdout
))
653 fprintf(stderr
, "key_write failed");
655 fprintf(stdout
, "\n");
660 do_download(struct passwd
*pw
)
667 nkeys
= pkcs11_add_provider(pkcs11provider
, NULL
, &keys
);
669 fatal("cannot read public key from pkcs11");
670 for (i
= 0; i
< nkeys
; i
++) {
671 key_write(keys
[i
], stdout
);
673 fprintf(stdout
, "\n");
679 fatal("no pkcs11 support");
680 #endif /* ENABLE_PKCS11 */
684 do_fingerprint(struct passwd
*pw
)
688 char *comment
= NULL
, *cp
, *ep
, line
[16*1024], *fp
, *ra
;
689 int i
, skip
= 0, num
= 0, invalid
= 1;
694 fptype
= print_bubblebabble
? SSH_FP_SHA1
: SSH_FP_MD5
;
695 rep
= print_bubblebabble
? SSH_FP_BUBBLEBABBLE
: SSH_FP_HEX
;
698 ask_filename(pw
, "Enter file in which the key is");
699 if (stat(identity_file
, &st
) < 0) {
700 perror(identity_file
);
703 public = key_load_public(identity_file
, &comment
);
704 if (public != NULL
) {
705 fp
= key_fingerprint(public, fptype
, rep
);
706 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
707 printf("%u %s %s (%s)\n", key_size(public), fp
, comment
,
709 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
722 if ((f
= fopen(identity_file
, "r")) == NULL
)
723 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
725 while (fgets(line
, sizeof(line
), f
)) {
726 if ((cp
= strchr(line
, '\n')) == NULL
) {
727 error("line %d too long: %.40s...",
739 /* Skip leading whitespace, empty and comment lines. */
740 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
742 if (!*cp
|| *cp
== '\n' || *cp
== '#')
744 i
= strtol(cp
, &ep
, 10);
745 if (i
== 0 || ep
== NULL
|| (*ep
!= ' ' && *ep
!= '\t')) {
748 for (; *cp
&& (quoted
|| (*cp
!= ' ' &&
749 *cp
!= '\t')); cp
++) {
750 if (*cp
== '\\' && cp
[1] == '"')
751 cp
++; /* Skip both */
760 public = key_new(KEY_RSA1
);
761 if (key_read(public, &cp
) != 1) {
764 public = key_new(KEY_UNSPEC
);
765 if (key_read(public, &cp
) != 1) {
770 comment
= *cp
? cp
: comment
;
771 fp
= key_fingerprint(public, fptype
, rep
);
772 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
773 printf("%u %s %s (%s)\n", key_size(public), fp
,
774 comment
? comment
: "no comment", key_type(public));
775 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
785 printf("%s is not a public key file.\n", identity_file
);
792 printhost(FILE *f
, const char *name
, Key
*public, int ca
, int hash
)
794 if (print_fingerprint
) {
799 fptype
= print_bubblebabble
? SSH_FP_SHA1
: SSH_FP_MD5
;
800 rep
= print_bubblebabble
? SSH_FP_BUBBLEBABBLE
: SSH_FP_HEX
;
801 fp
= key_fingerprint(public, fptype
, rep
);
802 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
803 printf("%u %s %s (%s)\n", key_size(public), fp
, name
,
805 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
810 if (hash
&& (name
= host_hash(name
, NULL
, 0)) == NULL
)
811 fatal("hash_host failed");
812 fprintf(f
, "%s%s%s ", ca
? CA_MARKER
: "", ca
? " " : "", name
);
813 if (!key_write(public, f
))
814 fatal("key_write failed");
820 do_known_hosts(struct passwd
*pw
, const char *name
)
822 FILE *in
, *out
= stdout
;
824 char *cp
, *cp2
, *kp
, *kp2
;
825 char line
[16*1024], tmp
[MAXPATHLEN
], old
[MAXPATHLEN
];
826 int c
, skip
= 0, inplace
= 0, num
= 0, invalid
= 0, has_unhashed
= 0;
829 if (!have_identity
) {
830 cp
= tilde_expand_filename(_PATH_SSH_USER_HOSTFILE
, pw
->pw_uid
);
831 if (strlcpy(identity_file
, cp
, sizeof(identity_file
)) >=
832 sizeof(identity_file
))
833 fatal("Specified known hosts path too long");
837 if ((in
= fopen(identity_file
, "r")) == NULL
)
838 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
841 * Find hosts goes to stdout, hash and deletions happen in-place
842 * A corner case is ssh-keygen -HF foo, which should go to stdout
844 if (!find_host
&& (hash_hosts
|| delete_host
)) {
845 if (strlcpy(tmp
, identity_file
, sizeof(tmp
)) >= sizeof(tmp
) ||
846 strlcat(tmp
, ".XXXXXXXXXX", sizeof(tmp
)) >= sizeof(tmp
) ||
847 strlcpy(old
, identity_file
, sizeof(old
)) >= sizeof(old
) ||
848 strlcat(old
, ".old", sizeof(old
)) >= sizeof(old
))
849 fatal("known_hosts path too long");
851 if ((c
= mkstemp(tmp
)) == -1)
852 fatal("mkstemp: %s", strerror(errno
));
853 if ((out
= fdopen(c
, "w")) == NULL
) {
856 fatal("fdopen: %s", strerror(c
));
861 while (fgets(line
, sizeof(line
), in
)) {
862 if ((cp
= strchr(line
, '\n')) == NULL
) {
863 error("line %d too long: %.40s...", num
+ 1, line
);
875 /* Skip leading whitespace, empty and comment lines. */
876 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
878 if (!*cp
|| *cp
== '\n' || *cp
== '#') {
880 fprintf(out
, "%s\n", cp
);
883 /* Check whether this is a CA key */
884 if (strncasecmp(cp
, CA_MARKER
, sizeof(CA_MARKER
) - 1) == 0 &&
885 (cp
[sizeof(CA_MARKER
) - 1] == ' ' ||
886 cp
[sizeof(CA_MARKER
) - 1] == '\t')) {
888 cp
+= sizeof(CA_MARKER
);
892 /* Find the end of the host name portion. */
893 for (kp
= cp
; *kp
&& *kp
!= ' ' && *kp
!= '\t'; kp
++)
896 if (*kp
== '\0' || *(kp
+ 1) == '\0') {
897 error("line %d missing key: %.40s...",
905 pub
= key_new(KEY_RSA1
);
906 if (key_read(pub
, &kp
) != 1) {
909 pub
= key_new(KEY_UNSPEC
);
910 if (key_read(pub
, &kp
) != 1) {
911 error("line %d invalid key: %.40s...",
919 if (*cp
== HASH_DELIM
) {
920 if (find_host
|| delete_host
) {
921 cp2
= host_hash(name
, cp
, strlen(cp
));
923 error("line %d: invalid hashed "
924 "name: %.64s...", num
, line
);
928 c
= (strcmp(cp2
, cp
) == 0);
929 if (find_host
&& c
) {
930 printf("# Host %s found: "
931 "line %d type %s%s\n", name
,
933 ca
? " (CA key)" : "");
934 printhost(out
, cp
, pub
, ca
, 0);
936 if (delete_host
&& !c
&& !ca
)
937 printhost(out
, cp
, pub
, ca
, 0);
938 } else if (hash_hosts
)
939 printhost(out
, cp
, pub
, ca
, 0);
941 if (find_host
|| delete_host
) {
942 c
= (match_hostname(name
, cp
,
944 if (find_host
&& c
) {
945 printf("# Host %s found: "
946 "line %d type %s%s\n", name
,
948 ca
? " (CA key)" : "");
949 printhost(out
, name
, pub
,
950 ca
, hash_hosts
&& !ca
);
952 if (delete_host
&& !c
&& !ca
)
953 printhost(out
, cp
, pub
, ca
, 0);
954 } else if (hash_hosts
) {
955 for (cp2
= strsep(&cp
, ",");
956 cp2
!= NULL
&& *cp2
!= '\0';
957 cp2
= strsep(&cp
, ",")) {
959 fprintf(stderr
, "Warning: "
960 "ignoring CA key for host: "
962 printhost(out
, cp2
, pub
, ca
, 0);
963 } else if (strcspn(cp2
, "*?!") !=
965 fprintf(stderr
, "Warning: "
966 "ignoring host name with "
967 "metacharacters: %.64s\n",
969 printhost(out
, cp2
, pub
, ca
, 0);
971 printhost(out
, cp2
, pub
, ca
, 1);
981 fprintf(stderr
, "%s is not a valid known_hosts file.\n",
984 fprintf(stderr
, "Not replacing existing known_hosts "
985 "file because of errors\n");
995 /* Backup existing file */
996 if (unlink(old
) == -1 && errno
!= ENOENT
)
997 fatal("unlink %.100s: %s", old
, strerror(errno
));
998 if (link(identity_file
, old
) == -1)
999 fatal("link %.100s to %.100s: %s", identity_file
, old
,
1001 /* Move new one into place */
1002 if (rename(tmp
, identity_file
) == -1) {
1003 error("rename\"%s\" to \"%s\": %s", tmp
, identity_file
,
1010 fprintf(stderr
, "%s updated.\n", identity_file
);
1011 fprintf(stderr
, "Original contents retained as %s\n", old
);
1013 fprintf(stderr
, "WARNING: %s contains unhashed "
1015 fprintf(stderr
, "Delete this file to ensure privacy "
1024 * Perform changing a passphrase. The argument is the passwd structure
1025 * for the current user.
1028 do_change_passphrase(struct passwd
*pw
)
1031 char *old_passphrase
, *passphrase1
, *passphrase2
;
1036 ask_filename(pw
, "Enter file in which the key is");
1037 if (stat(identity_file
, &st
) < 0) {
1038 perror(identity_file
);
1041 /* Try to load the file with empty passphrase. */
1042 private = key_load_private(identity_file
, "", &comment
);
1043 if (private == NULL
) {
1044 if (identity_passphrase
)
1045 old_passphrase
= xstrdup(identity_passphrase
);
1048 read_passphrase("Enter old passphrase: ",
1050 private = key_load_private(identity_file
, old_passphrase
,
1052 memset(old_passphrase
, 0, strlen(old_passphrase
));
1053 xfree(old_passphrase
);
1054 if (private == NULL
) {
1055 printf("Bad passphrase.\n");
1059 printf("Key has comment '%s'\n", comment
);
1061 /* Ask the new passphrase (twice). */
1062 if (identity_new_passphrase
) {
1063 passphrase1
= xstrdup(identity_new_passphrase
);
1067 read_passphrase("Enter new passphrase (empty for no "
1068 "passphrase): ", RP_ALLOW_STDIN
);
1069 passphrase2
= read_passphrase("Enter same passphrase again: ",
1072 /* Verify that they are the same. */
1073 if (strcmp(passphrase1
, passphrase2
) != 0) {
1074 memset(passphrase1
, 0, strlen(passphrase1
));
1075 memset(passphrase2
, 0, strlen(passphrase2
));
1078 printf("Pass phrases do not match. Try again.\n");
1081 /* Destroy the other copy. */
1082 memset(passphrase2
, 0, strlen(passphrase2
));
1086 /* Save the file using the new passphrase. */
1087 if (!key_save_private(private, identity_file
, passphrase1
, comment
)) {
1088 printf("Saving the key failed: %s.\n", identity_file
);
1089 memset(passphrase1
, 0, strlen(passphrase1
));
1095 /* Destroy the passphrase and the copy of the key in memory. */
1096 memset(passphrase1
, 0, strlen(passphrase1
));
1098 key_free(private); /* Destroys contents */
1101 printf("Your identification has been saved with the new passphrase.\n");
1106 * Print the SSHFP RR.
1109 do_print_resource_record(struct passwd
*pw
, char *fname
, char *hname
)
1112 char *comment
= NULL
;
1116 ask_filename(pw
, "Enter file in which the key is");
1117 if (stat(fname
, &st
) < 0) {
1118 if (errno
== ENOENT
)
1123 public = key_load_public(fname
, &comment
);
1124 if (public != NULL
) {
1125 export_dns_rr(hname
, public, stdout
, print_generic
);
1133 printf("failed to read v2 public key from %s.\n", fname
);
1138 * Change the comment of a private key file.
1141 do_change_comment(struct passwd
*pw
)
1143 char new_comment
[1024], *comment
, *passphrase
;
1151 ask_filename(pw
, "Enter file in which the key is");
1152 if (stat(identity_file
, &st
) < 0) {
1153 perror(identity_file
);
1156 private = key_load_private(identity_file
, "", &comment
);
1157 if (private == NULL
) {
1158 if (identity_passphrase
)
1159 passphrase
= xstrdup(identity_passphrase
);
1160 else if (identity_new_passphrase
)
1161 passphrase
= xstrdup(identity_new_passphrase
);
1163 passphrase
= read_passphrase("Enter passphrase: ",
1165 /* Try to load using the passphrase. */
1166 private = key_load_private(identity_file
, passphrase
, &comment
);
1167 if (private == NULL
) {
1168 memset(passphrase
, 0, strlen(passphrase
));
1170 printf("Bad passphrase.\n");
1174 passphrase
= xstrdup("");
1176 if (private->type
!= KEY_RSA1
) {
1177 fprintf(stderr
, "Comments are only supported for RSA1 keys.\n");
1181 printf("Key now has comment '%s'\n", comment
);
1183 if (identity_comment
) {
1184 strlcpy(new_comment
, identity_comment
, sizeof(new_comment
));
1186 printf("Enter new comment: ");
1188 if (!fgets(new_comment
, sizeof(new_comment
), stdin
)) {
1189 memset(passphrase
, 0, strlen(passphrase
));
1193 new_comment
[strcspn(new_comment
, "\n")] = '\0';
1196 /* Save the file using the new passphrase. */
1197 if (!key_save_private(private, identity_file
, passphrase
, new_comment
)) {
1198 printf("Saving the key failed: %s.\n", identity_file
);
1199 memset(passphrase
, 0, strlen(passphrase
));
1205 memset(passphrase
, 0, strlen(passphrase
));
1207 public = key_from_private(private);
1210 strlcat(identity_file
, ".pub", sizeof(identity_file
));
1211 fd
= open(identity_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
1213 printf("Could not save your public key in %s\n", identity_file
);
1216 f
= fdopen(fd
, "w");
1218 printf("fdopen %s failed\n", identity_file
);
1221 if (!key_write(public, f
))
1222 fprintf(stderr
, "write key failed\n");
1224 fprintf(f
, " %s\n", new_comment
);
1229 printf("The comment in your key file has been changed.\n");
1234 fmt_validity(u_int64_t valid_from
, u_int64_t valid_to
)
1236 char from
[32], to
[32];
1237 static char ret
[64];
1242 if (valid_from
== 0 && valid_to
== 0xffffffffffffffffULL
)
1245 if (valid_from
!= 0) {
1246 /* XXX revisit INT_MAX in 2038 :) */
1247 tt
= valid_from
> INT_MAX
? INT_MAX
: valid_from
;
1248 tm
= localtime(&tt
);
1249 strftime(from
, sizeof(from
), "%Y-%m-%dT%H:%M:%S", tm
);
1251 if (valid_to
!= 0xffffffffffffffffULL
) {
1252 /* XXX revisit INT_MAX in 2038 :) */
1253 tt
= valid_to
> INT_MAX
? INT_MAX
: valid_to
;
1254 tm
= localtime(&tt
);
1255 strftime(to
, sizeof(to
), "%Y-%m-%dT%H:%M:%S", tm
);
1258 if (valid_from
== 0) {
1259 snprintf(ret
, sizeof(ret
), "before %s", to
);
1262 if (valid_to
== 0xffffffffffffffffULL
) {
1263 snprintf(ret
, sizeof(ret
), "after %s", from
);
1267 snprintf(ret
, sizeof(ret
), "from %s to %s", from
, to
);
1272 add_flag_option(Buffer
*c
, const char *name
)
1274 debug3("%s: %s", __func__
, name
);
1275 buffer_put_cstring(c
, name
);
1276 buffer_put_string(c
, NULL
, 0);
1280 add_string_option(Buffer
*c
, const char *name
, const char *value
)
1284 debug3("%s: %s=%s", __func__
, name
, value
);
1286 buffer_put_cstring(&b
, value
);
1288 buffer_put_cstring(c
, name
);
1289 buffer_put_string(c
, buffer_ptr(&b
), buffer_len(&b
));
1294 #define OPTIONS_CRITICAL 1
1295 #define OPTIONS_EXTENSIONS 2
1297 prepare_options_buf(Buffer
*c
, int which
)
1300 if ((which
& OPTIONS_CRITICAL
) != 0 &&
1301 certflags_command
!= NULL
)
1302 add_string_option(c
, "force-command", certflags_command
);
1303 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1304 (certflags_flags
& CERTOPT_AGENT_FWD
) != 0)
1305 add_flag_option(c
, "permit-agent-forwarding");
1306 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1307 (certflags_flags
& CERTOPT_PORT_FWD
) != 0)
1308 add_flag_option(c
, "permit-port-forwarding");
1309 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1310 (certflags_flags
& CERTOPT_PTY
) != 0)
1311 add_flag_option(c
, "permit-pty");
1312 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1313 (certflags_flags
& CERTOPT_USER_RC
) != 0)
1314 add_flag_option(c
, "permit-user-rc");
1315 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1316 (certflags_flags
& CERTOPT_X_FWD
) != 0)
1317 add_flag_option(c
, "permit-X11-forwarding");
1318 if ((which
& OPTIONS_CRITICAL
) != 0 &&
1319 certflags_src_addr
!= NULL
)
1320 add_string_option(c
, "source-address", certflags_src_addr
);
1324 load_pkcs11_key(char *path
)
1326 #ifdef ENABLE_PKCS11
1327 Key
**keys
= NULL
, *public, *private = NULL
;
1330 if ((public = key_load_public(path
, NULL
)) == NULL
)
1331 fatal("Couldn't load CA public key \"%s\"", path
);
1333 nkeys
= pkcs11_add_provider(pkcs11provider
, identity_passphrase
, &keys
);
1334 debug3("%s: %d keys", __func__
, nkeys
);
1336 fatal("cannot read public key from pkcs11");
1337 for (i
= 0; i
< nkeys
; i
++) {
1338 if (key_equal_public(public, keys
[i
])) {
1348 fatal("no pkcs11 support");
1349 #endif /* ENABLE_PKCS11 */
1353 do_ca_sign(struct passwd
*pw
, int argc
, char **argv
)
1358 char *otmp
, *tmp
, *cp
, *out
, *comment
, **plist
= NULL
;
1360 int v00
= 0; /* legacy keys */
1362 if (key_type_name
!= NULL
) {
1363 switch (key_type_from_name(key_type_name
)) {
1364 case KEY_RSA_CERT_V00
:
1365 case KEY_DSA_CERT_V00
:
1369 if (strcasecmp(key_type_name
, "v00") == 0) {
1372 } else if (strcasecmp(key_type_name
, "v01") == 0)
1376 fprintf(stderr
, "unknown key type %s\n", key_type_name
);
1382 tmp
= tilde_expand_filename(ca_key_path
, pw
->pw_uid
);
1383 if (pkcs11provider
!= NULL
) {
1384 if ((ca
= load_pkcs11_key(tmp
)) == NULL
)
1385 fatal("No PKCS#11 key matching %s found", ca_key_path
);
1386 } else if ((ca
= load_identity(tmp
)) == NULL
)
1387 fatal("Couldn't load CA key \"%s\"", tmp
);
1390 for (i
= 0; i
< argc
; i
++) {
1391 /* Split list of principals */
1393 if (cert_principals
!= NULL
) {
1394 otmp
= tmp
= xstrdup(cert_principals
);
1396 for (; (cp
= strsep(&tmp
, ",")) != NULL
; n
++) {
1397 plist
= xrealloc(plist
, n
+ 1, sizeof(*plist
));
1398 if (*(plist
[n
] = xstrdup(cp
)) == '\0')
1399 fatal("Empty principal name");
1404 tmp
= tilde_expand_filename(argv
[i
], pw
->pw_uid
);
1405 if ((public = key_load_public(tmp
, &comment
)) == NULL
)
1406 fatal("%s: unable to open \"%s\"", __func__
, tmp
);
1407 if (public->type
!= KEY_RSA
&& public->type
!= KEY_DSA
)
1408 fatal("%s: key \"%s\" type %s cannot be certified",
1409 __func__
, tmp
, key_type(public));
1411 /* Prepare certificate to sign */
1412 if (key_to_certified(public, v00
) != 0)
1413 fatal("Could not upgrade key %s to certificate", tmp
);
1414 public->cert
->type
= cert_key_type
;
1415 public->cert
->serial
= (u_int64_t
)cert_serial
;
1416 public->cert
->key_id
= xstrdup(cert_key_id
);
1417 public->cert
->nprincipals
= n
;
1418 public->cert
->principals
= plist
;
1419 public->cert
->valid_after
= cert_valid_from
;
1420 public->cert
->valid_before
= cert_valid_to
;
1422 prepare_options_buf(&public->cert
->critical
,
1423 OPTIONS_CRITICAL
|OPTIONS_EXTENSIONS
);
1425 prepare_options_buf(&public->cert
->critical
,
1427 prepare_options_buf(&public->cert
->extensions
,
1428 OPTIONS_EXTENSIONS
);
1430 public->cert
->signature_key
= key_from_private(ca
);
1432 if (key_certify(public, ca
) != 0)
1433 fatal("Couldn't not certify key %s", tmp
);
1435 if ((cp
= strrchr(tmp
, '.')) != NULL
&& strcmp(cp
, ".pub") == 0)
1437 xasprintf(&out
, "%s-cert.pub", tmp
);
1440 if ((fd
= open(out
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) == -1)
1441 fatal("Could not open \"%s\" for writing: %s", out
,
1443 if ((f
= fdopen(fd
, "w")) == NULL
)
1444 fatal("%s: fdopen: %s", __func__
, strerror(errno
));
1445 if (!key_write(public, f
))
1446 fatal("Could not write certified key to %s", out
);
1447 fprintf(f
, " %s\n", comment
);
1451 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1452 "valid %s", key_cert_type(public),
1453 out
, public->cert
->key_id
, public->cert
->serial
,
1454 cert_principals
!= NULL
? " for " : "",
1455 cert_principals
!= NULL
? cert_principals
: "",
1456 fmt_validity(cert_valid_from
, cert_valid_to
));
1467 parse_relative_time(const char *s
, time_t now
)
1471 mul
= *s
== '-' ? -1 : 1;
1473 if ((secs
= convtime(s
+ 1)) == -1)
1474 fatal("Invalid relative certificate time %s", s
);
1475 if (mul
== -1 && secs
> now
)
1476 fatal("Certificate time %s cannot be represented", s
);
1477 return now
+ (u_int64_t
)(secs
* mul
);
1481 parse_absolute_time(const char *s
)
1488 * POSIX strptime says "The application shall ensure that there
1489 * is white-space or other non-alphanumeric characters between
1490 * any two conversion specifications" so arrange things this way.
1492 switch (strlen(s
)) {
1495 snprintf(buf
, sizeof(buf
), "%.4s-%.2s-%.2s", s
, s
+ 4, s
+ 6);
1498 fmt
= "%Y-%m-%dT%H:%M:%S";
1499 snprintf(buf
, sizeof(buf
), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
1500 s
, s
+ 4, s
+ 6, s
+ 8, s
+ 10, s
+ 12);
1503 fatal("Invalid certificate time format %s", s
);
1506 bzero(&tm
, sizeof(tm
));
1507 if (strptime(buf
, fmt
, &tm
) == NULL
)
1508 fatal("Invalid certificate time %s", s
);
1509 if ((tt
= mktime(&tm
)) < 0)
1510 fatal("Certificate time %s cannot be represented", s
);
1511 return (u_int64_t
)tt
;
1515 parse_cert_times(char *timespec
)
1518 time_t now
= time(NULL
);
1521 /* +timespec relative to now */
1522 if (*timespec
== '+' && strchr(timespec
, ':') == NULL
) {
1523 if ((secs
= convtime(timespec
+ 1)) == -1)
1524 fatal("Invalid relative certificate life %s", timespec
);
1525 cert_valid_to
= now
+ secs
;
1527 * Backdate certificate one minute to avoid problems on hosts
1528 * with poorly-synchronised clocks.
1530 cert_valid_from
= ((now
- 59)/ 60) * 60;
1536 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1537 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1539 from
= xstrdup(timespec
);
1540 to
= strchr(from
, ':');
1541 if (to
== NULL
|| from
== to
|| *(to
+ 1) == '\0')
1542 fatal("Invalid certificate life specification %s", timespec
);
1545 if (*from
== '-' || *from
== '+')
1546 cert_valid_from
= parse_relative_time(from
, now
);
1548 cert_valid_from
= parse_absolute_time(from
);
1550 if (*to
== '-' || *to
== '+')
1551 cert_valid_to
= parse_relative_time(to
, cert_valid_from
);
1553 cert_valid_to
= parse_absolute_time(to
);
1555 if (cert_valid_to
<= cert_valid_from
)
1556 fatal("Empty certificate validity interval");
1561 add_cert_option(char *opt
)
1565 if (strcmp(opt
, "clear") == 0)
1566 certflags_flags
= 0;
1567 else if (strcasecmp(opt
, "no-x11-forwarding") == 0)
1568 certflags_flags
&= ~CERTOPT_X_FWD
;
1569 else if (strcasecmp(opt
, "permit-x11-forwarding") == 0)
1570 certflags_flags
|= CERTOPT_X_FWD
;
1571 else if (strcasecmp(opt
, "no-agent-forwarding") == 0)
1572 certflags_flags
&= ~CERTOPT_AGENT_FWD
;
1573 else if (strcasecmp(opt
, "permit-agent-forwarding") == 0)
1574 certflags_flags
|= CERTOPT_AGENT_FWD
;
1575 else if (strcasecmp(opt
, "no-port-forwarding") == 0)
1576 certflags_flags
&= ~CERTOPT_PORT_FWD
;
1577 else if (strcasecmp(opt
, "permit-port-forwarding") == 0)
1578 certflags_flags
|= CERTOPT_PORT_FWD
;
1579 else if (strcasecmp(opt
, "no-pty") == 0)
1580 certflags_flags
&= ~CERTOPT_PTY
;
1581 else if (strcasecmp(opt
, "permit-pty") == 0)
1582 certflags_flags
|= CERTOPT_PTY
;
1583 else if (strcasecmp(opt
, "no-user-rc") == 0)
1584 certflags_flags
&= ~CERTOPT_USER_RC
;
1585 else if (strcasecmp(opt
, "permit-user-rc") == 0)
1586 certflags_flags
|= CERTOPT_USER_RC
;
1587 else if (strncasecmp(opt
, "force-command=", 14) == 0) {
1590 fatal("Empty force-command option");
1591 if (certflags_command
!= NULL
)
1592 fatal("force-command already specified");
1593 certflags_command
= xstrdup(val
);
1594 } else if (strncasecmp(opt
, "source-address=", 15) == 0) {
1597 fatal("Empty source-address option");
1598 if (certflags_src_addr
!= NULL
)
1599 fatal("source-address already specified");
1600 if (addr_match_cidr_list(NULL
, val
) != 0)
1601 fatal("Invalid source-address list");
1602 certflags_src_addr
= xstrdup(val
);
1604 fatal("Unsupported certificate option \"%s\"", opt
);
1608 show_options(const Buffer
*optbuf
, int v00
, int in_critical
)
1610 u_char
*name
, *data
;
1612 Buffer options
, option
;
1614 buffer_init(&options
);
1615 buffer_append(&options
, buffer_ptr(optbuf
), buffer_len(optbuf
));
1617 buffer_init(&option
);
1618 while (buffer_len(&options
) != 0) {
1619 name
= buffer_get_string(&options
, NULL
);
1620 data
= buffer_get_string_ptr(&options
, &dlen
);
1621 buffer_append(&option
, data
, dlen
);
1622 printf(" %s", name
);
1623 if ((v00
|| !in_critical
) &&
1624 (strcmp(name
, "permit-X11-forwarding") == 0 ||
1625 strcmp(name
, "permit-agent-forwarding") == 0 ||
1626 strcmp(name
, "permit-port-forwarding") == 0 ||
1627 strcmp(name
, "permit-pty") == 0 ||
1628 strcmp(name
, "permit-user-rc") == 0))
1630 else if ((v00
|| in_critical
) &&
1631 (strcmp(name
, "force-command") == 0 ||
1632 strcmp(name
, "source-address") == 0)) {
1633 data
= buffer_get_string(&option
, NULL
);
1634 printf(" %s\n", data
);
1637 printf(" UNKNOWN OPTION (len %u)\n",
1638 buffer_len(&option
));
1639 buffer_clear(&option
);
1642 if (buffer_len(&option
) != 0)
1643 fatal("Option corrupt: extra data at end");
1645 buffer_free(&option
);
1646 buffer_free(&options
);
1650 do_show_cert(struct passwd
*pw
)
1654 char *key_fp
, *ca_fp
;
1658 ask_filename(pw
, "Enter file in which the key is");
1659 if (stat(identity_file
, &st
) < 0)
1660 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
1661 if ((key
= key_load_public(identity_file
, NULL
)) == NULL
)
1662 fatal("%s is not a public key", identity_file
);
1663 if (!key_is_cert(key
))
1664 fatal("%s is not a certificate", identity_file
);
1665 v00
= key
->type
== KEY_RSA_CERT_V00
|| key
->type
== KEY_DSA_CERT_V00
;
1667 key_fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
1668 ca_fp
= key_fingerprint(key
->cert
->signature_key
,
1669 SSH_FP_MD5
, SSH_FP_HEX
);
1671 printf("%s:\n", identity_file
);
1672 printf(" Type: %s %s certificate\n", key_ssh_name(key
),
1673 key_cert_type(key
));
1674 printf(" Public key: %s %s\n", key_type(key
), key_fp
);
1675 printf(" Signing CA: %s %s\n",
1676 key_type(key
->cert
->signature_key
), ca_fp
);
1677 printf(" Key ID: \"%s\"\n", key
->cert
->key_id
);
1679 printf(" Serial: %llu\n", key
->cert
->serial
);
1680 printf(" Valid: %s\n",
1681 fmt_validity(key
->cert
->valid_after
, key
->cert
->valid_before
));
1682 printf(" Principals: ");
1683 if (key
->cert
->nprincipals
== 0)
1686 for (i
= 0; i
< key
->cert
->nprincipals
; i
++)
1688 key
->cert
->principals
[i
]);
1691 printf(" Critical Options: ");
1692 if (buffer_len(&key
->cert
->critical
) == 0)
1696 show_options(&key
->cert
->critical
, v00
, 1);
1699 printf(" Extensions: ");
1700 if (buffer_len(&key
->cert
->extensions
) == 0)
1704 show_options(&key
->cert
->extensions
, v00
, 0);
1713 fprintf(stderr
, "usage: %s [options]\n", __progname
);
1714 fprintf(stderr
, "Options:\n");
1715 fprintf(stderr
, " -a trials Number of trials for screening DH-GEX moduli.\n");
1716 fprintf(stderr
, " -B Show bubblebabble digest of key file.\n");
1717 fprintf(stderr
, " -b bits Number of bits in the key to create.\n");
1718 fprintf(stderr
, " -C comment Provide new comment.\n");
1719 fprintf(stderr
, " -c Change comment in private and public key files.\n");
1720 #ifdef ENABLE_PKCS11
1721 fprintf(stderr
, " -D pkcs11 Download public key from pkcs11 token.\n");
1723 fprintf(stderr
, " -e Export OpenSSH to foreign format key file.\n");
1724 fprintf(stderr
, " -F hostname Find hostname in known hosts file.\n");
1725 fprintf(stderr
, " -f filename Filename of the key file.\n");
1726 fprintf(stderr
, " -G file Generate candidates for DH-GEX moduli.\n");
1727 fprintf(stderr
, " -g Use generic DNS resource record format.\n");
1728 fprintf(stderr
, " -H Hash names in known_hosts file.\n");
1729 fprintf(stderr
, " -h Generate host certificate instead of a user certificate.\n");
1730 fprintf(stderr
, " -I key_id Key identifier to include in certificate.\n");
1731 fprintf(stderr
, " -i Import foreign format to OpenSSH key file.\n");
1732 fprintf(stderr
, " -L Print the contents of a certificate.\n");
1733 fprintf(stderr
, " -l Show fingerprint of key file.\n");
1734 fprintf(stderr
, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1735 fprintf(stderr
, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
1736 fprintf(stderr
, " -N phrase Provide new passphrase.\n");
1737 fprintf(stderr
, " -n name,... User/host principal names to include in certificate\n");
1738 fprintf(stderr
, " -O option Specify a certificate option.\n");
1739 fprintf(stderr
, " -P phrase Provide old passphrase.\n");
1740 fprintf(stderr
, " -p Change passphrase of private key file.\n");
1741 fprintf(stderr
, " -q Quiet.\n");
1742 fprintf(stderr
, " -R hostname Remove host from known_hosts file.\n");
1743 fprintf(stderr
, " -r hostname Print DNS resource record.\n");
1744 fprintf(stderr
, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1745 fprintf(stderr
, " -s ca_key Certify keys with CA key.\n");
1746 fprintf(stderr
, " -T file Screen candidates for DH-GEX moduli.\n");
1747 fprintf(stderr
, " -t type Specify type of key to create.\n");
1748 fprintf(stderr
, " -V from:to Specify certificate validity interval.\n");
1749 fprintf(stderr
, " -v Verbose.\n");
1750 fprintf(stderr
, " -W gen Generator to use for generating DH-GEX moduli.\n");
1751 fprintf(stderr
, " -y Read private key file and print public key.\n");
1752 fprintf(stderr
, " -z serial Specify a serial number.\n");
1758 * Main program for key management.
1761 main(int argc
, char **argv
)
1763 char dotsshdir
[MAXPATHLEN
], comment
[1024], *passphrase1
, *passphrase2
;
1764 char out_file
[MAXPATHLEN
], *rr_hostname
= NULL
;
1765 Key
*private, *public;
1770 u_int32_t memory
= 0, generator_wanted
= 0, trials
= 100;
1771 int do_gen_candidates
= 0, do_screen_candidates
= 0;
1772 BIGNUM
*start
= NULL
;
1777 extern char *optarg
;
1779 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1782 __progname
= ssh_get_progname(argv
[0]);
1784 SSLeay_add_all_algorithms();
1785 log_init(argv
[0], SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_USER
, 1);
1790 /* we need this for the home * directory. */
1791 pw
= getpwuid(getuid());
1793 printf("You don't exist, go away!\n");
1796 if (gethostname(hostname
, sizeof(hostname
)) < 0) {
1797 perror("gethostname");
1801 while ((opt
= getopt(argc
, argv
, "degiqpclBHLhvxXyF:b:f:t:D:I:P:m:N:n:"
1802 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
1805 bits
= (u_int32_t
)strtonum(optarg
, 768, 32768, &errstr
);
1807 fatal("Bits has bad value %s (%s)",
1812 rr_hostname
= optarg
;
1818 cert_key_id
= optarg
;
1822 rr_hostname
= optarg
;
1828 print_fingerprint
= 1;
1831 print_bubblebabble
= 1;
1834 if (strcasecmp(optarg
, "RFC4716") == 0 ||
1835 strcasecmp(optarg
, "ssh2") == 0) {
1836 convert_format
= FMT_RFC4716
;
1839 if (strcasecmp(optarg
, "PKCS8") == 0) {
1840 convert_format
= FMT_PKCS8
;
1843 if (strcasecmp(optarg
, "PEM") == 0) {
1844 convert_format
= FMT_PEM
;
1847 fatal("Unsupported conversion format \"%s\"", optarg
);
1849 cert_principals
= optarg
;
1852 change_passphrase
= 1;
1858 if (strlcpy(identity_file
, optarg
, sizeof(identity_file
)) >=
1859 sizeof(identity_file
))
1860 fatal("Identity filename too long");
1867 identity_passphrase
= optarg
;
1870 identity_new_passphrase
= optarg
;
1873 add_cert_option(optarg
);
1876 identity_comment
= optarg
;
1887 cert_key_type
= SSH2_CERT_TYPE_HOST
;
1888 certflags_flags
= 0;
1899 key_type_name
= "dsa";
1902 ca_key_path
= optarg
;
1905 key_type_name
= optarg
;
1908 pkcs11provider
= optarg
;
1911 if (log_level
== SYSLOG_LEVEL_INFO
)
1912 log_level
= SYSLOG_LEVEL_DEBUG1
;
1914 if (log_level
>= SYSLOG_LEVEL_DEBUG1
&&
1915 log_level
< SYSLOG_LEVEL_DEBUG3
)
1920 rr_hostname
= optarg
;
1923 generator_wanted
= (u_int32_t
)strtonum(optarg
, 1,
1926 fatal("Desired generator has bad value: %s (%s)",
1930 trials
= (u_int32_t
)strtonum(optarg
, 1, UINT_MAX
, &errstr
);
1932 fatal("Invalid number of trials: %s (%s)",
1936 memory
= (u_int32_t
)strtonum(optarg
, 1, UINT_MAX
, &errstr
);
1938 fatal("Memory limit is %s: %s", errstr
, optarg
);
1941 do_gen_candidates
= 1;
1942 if (strlcpy(out_file
, optarg
, sizeof(out_file
)) >=
1944 fatal("Output filename too long");
1947 do_screen_candidates
= 1;
1948 if (strlcpy(out_file
, optarg
, sizeof(out_file
)) >=
1950 fatal("Output filename too long");
1953 /* XXX - also compare length against bits */
1954 if (BN_hex2bn(&start
, optarg
) == 0)
1955 fatal("Invalid start point.");
1958 parse_cert_times(optarg
);
1961 cert_serial
= strtonum(optarg
, 0, LLONG_MAX
, &errstr
);
1963 fatal("Invalid serial number: %s", errstr
);
1972 log_init(argv
[0], log_level
, SYSLOG_FACILITY_USER
, 1);
1977 if (ca_key_path
!= NULL
) {
1979 printf("Too few arguments.\n");
1982 } else if (argc
> 0) {
1983 printf("Too many arguments.\n");
1986 if (change_passphrase
&& change_comment
) {
1987 printf("Can only have one of -p and -c.\n");
1990 if (print_fingerprint
&& (delete_host
|| hash_hosts
)) {
1991 printf("Cannot use -l with -D or -R.\n");
1994 if (ca_key_path
!= NULL
) {
1995 if (cert_key_id
== NULL
)
1996 fatal("Must specify key id (-I) when certifying");
1997 do_ca_sign(pw
, argc
, argv
);
2001 if (delete_host
|| hash_hosts
|| find_host
)
2002 do_known_hosts(pw
, rr_hostname
);
2003 if (print_fingerprint
|| print_bubblebabble
)
2005 if (change_passphrase
)
2006 do_change_passphrase(pw
);
2008 do_change_comment(pw
);
2012 do_convert_from(pw
);
2014 do_print_public(pw
);
2015 if (rr_hostname
!= NULL
) {
2018 if (have_identity
) {
2019 n
= do_print_resource_record(pw
,
2020 identity_file
, rr_hostname
);
2022 perror(identity_file
);
2028 n
+= do_print_resource_record(pw
,
2029 _PATH_HOST_RSA_KEY_FILE
, rr_hostname
);
2030 n
+= do_print_resource_record(pw
,
2031 _PATH_HOST_DSA_KEY_FILE
, rr_hostname
);
2034 fatal("no keys found.");
2038 if (pkcs11provider
!= NULL
)
2041 if (do_gen_candidates
) {
2042 FILE *out
= fopen(out_file
, "w");
2045 error("Couldn't open modulus candidate file \"%s\": %s",
2046 out_file
, strerror(errno
));
2050 bits
= DEFAULT_BITS
;
2051 if (gen_candidates(out
, memory
, bits
, start
) != 0)
2052 fatal("modulus candidate generation failed");
2057 if (do_screen_candidates
) {
2059 FILE *out
= fopen(out_file
, "w");
2061 if (have_identity
&& strcmp(identity_file
, "-") != 0) {
2062 if ((in
= fopen(identity_file
, "r")) == NULL
) {
2063 fatal("Couldn't open modulus candidate "
2064 "file \"%s\": %s", identity_file
,
2071 fatal("Couldn't open moduli file \"%s\": %s",
2072 out_file
, strerror(errno
));
2074 if (prime_test(in
, out
, trials
, generator_wanted
) != 0)
2075 fatal("modulus screening failed");
2081 if (key_type_name
== NULL
)
2082 key_type_name
= "rsa";
2084 type
= key_type_from_name(key_type_name
);
2085 if (type
== KEY_UNSPEC
) {
2086 fprintf(stderr
, "unknown key type %s\n", key_type_name
);
2090 bits
= (type
== KEY_DSA
) ? DEFAULT_BITS_DSA
: DEFAULT_BITS
;
2091 maxbits
= (type
== KEY_DSA
) ?
2092 OPENSSL_DSA_MAX_MODULUS_BITS
: OPENSSL_RSA_MAX_MODULUS_BITS
;
2093 if (bits
> maxbits
) {
2094 fprintf(stderr
, "key bits exceeds maximum %d\n", maxbits
);
2097 if (type
== KEY_DSA
&& bits
!= 1024)
2098 fatal("DSA keys must be 1024 bits");
2100 printf("Generating public/private %s key pair.\n", key_type_name
);
2101 private = key_generate(type
, bits
);
2102 if (private == NULL
) {
2103 fprintf(stderr
, "key_generate failed\n");
2106 public = key_from_private(private);
2109 ask_filename(pw
, "Enter file in which to save the key");
2111 /* Create ~/.ssh directory if it doesn't already exist. */
2112 snprintf(dotsshdir
, sizeof dotsshdir
, "%s/%s",
2113 pw
->pw_dir
, _PATH_SSH_USER_DIR
);
2114 if (strstr(identity_file
, dotsshdir
) != NULL
) {
2115 if (stat(dotsshdir
, &st
) < 0) {
2116 if (errno
!= ENOENT
) {
2117 error("Could not stat %s: %s", dotsshdir
,
2119 } else if (mkdir(dotsshdir
, 0700) < 0) {
2120 error("Could not create directory '%s': %s",
2121 dotsshdir
, strerror(errno
));
2123 printf("Created directory '%s'.\n", dotsshdir
);
2126 /* If the file already exists, ask the user to confirm. */
2127 if (stat(identity_file
, &st
) >= 0) {
2129 printf("%s already exists.\n", identity_file
);
2130 printf("Overwrite (y/n)? ");
2132 if (fgets(yesno
, sizeof(yesno
), stdin
) == NULL
)
2134 if (yesno
[0] != 'y' && yesno
[0] != 'Y')
2137 /* Ask for a passphrase (twice). */
2138 if (identity_passphrase
)
2139 passphrase1
= xstrdup(identity_passphrase
);
2140 else if (identity_new_passphrase
)
2141 passphrase1
= xstrdup(identity_new_passphrase
);
2145 read_passphrase("Enter passphrase (empty for no "
2146 "passphrase): ", RP_ALLOW_STDIN
);
2147 passphrase2
= read_passphrase("Enter same passphrase again: ",
2149 if (strcmp(passphrase1
, passphrase2
) != 0) {
2151 * The passphrases do not match. Clear them and
2154 memset(passphrase1
, 0, strlen(passphrase1
));
2155 memset(passphrase2
, 0, strlen(passphrase2
));
2158 printf("Passphrases do not match. Try again.\n");
2159 goto passphrase_again
;
2161 /* Clear the other copy of the passphrase. */
2162 memset(passphrase2
, 0, strlen(passphrase2
));
2166 if (identity_comment
) {
2167 strlcpy(comment
, identity_comment
, sizeof(comment
));
2169 /* Create default comment field for the passphrase. */
2170 snprintf(comment
, sizeof comment
, "%s@%s", pw
->pw_name
, hostname
);
2173 /* Save the key with the given passphrase and comment. */
2174 if (!key_save_private(private, identity_file
, passphrase1
, comment
)) {
2175 printf("Saving the key failed: %s.\n", identity_file
);
2176 memset(passphrase1
, 0, strlen(passphrase1
));
2180 /* Clear the passphrase. */
2181 memset(passphrase1
, 0, strlen(passphrase1
));
2184 /* Clear the private key and the random number generator. */
2189 printf("Your identification has been saved in %s.\n", identity_file
);
2191 strlcat(identity_file
, ".pub", sizeof(identity_file
));
2192 fd
= open(identity_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
2194 printf("Could not save your public key in %s\n", identity_file
);
2197 f
= fdopen(fd
, "w");
2199 printf("fdopen %s failed\n", identity_file
);
2202 if (!key_write(public, f
))
2203 fprintf(stderr
, "write key failed\n");
2204 fprintf(f
, " %s\n", comment
);
2208 char *fp
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_HEX
);
2209 char *ra
= key_fingerprint(public, SSH_FP_MD5
,
2211 printf("Your public key has been saved in %s.\n",
2213 printf("The key fingerprint is:\n");
2214 printf("%s %s\n", fp
, comment
);
2215 printf("The key's randomart image is:\n");