1 /* $OpenBSD: ssh-keygen.c,v 1.203 2010/09/02 17:21:50 naddy 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
60 #define DEFAULT_BITS_ECDSA 256
64 * Flag indicating that we just want to change the passphrase. This can be
65 * set on the command line.
67 int change_passphrase
= 0;
70 * Flag indicating that we just want to change the comment. This can be set
71 * on the command line.
73 int change_comment
= 0;
77 int log_level
= SYSLOG_LEVEL_INFO
;
79 /* Flag indicating that we want to hash a known_hosts file */
81 /* Flag indicating that we want lookup a host in known_hosts file */
83 /* Flag indicating that we want to delete a host from a known_hosts file */
86 /* Flag indicating that we want to show the contents of a certificate */
89 /* Flag indicating that we just want to see the key fingerprint */
90 int print_fingerprint
= 0;
91 int print_bubblebabble
= 0;
93 /* The identity file name, given on the command line or entered by the user. */
94 char identity_file
[1024];
95 int have_identity
= 0;
97 /* This is set to the passphrase if given on the command line. */
98 char *identity_passphrase
= NULL
;
100 /* This is set to the new passphrase if given on the command line. */
101 char *identity_new_passphrase
= NULL
;
103 /* This is set to the new comment if given on the command line. */
104 char *identity_comment
= NULL
;
106 /* Path to CA key when certifying keys. */
107 char *ca_key_path
= NULL
;
109 /* Certificate serial number */
110 long long cert_serial
= 0;
112 /* Key type when certifying */
113 u_int cert_key_type
= SSH2_CERT_TYPE_USER
;
115 /* "key ID" of signed key */
116 char *cert_key_id
= NULL
;
118 /* Comma-separated list of principal names for certifying keys */
119 char *cert_principals
= NULL
;
121 /* Validity period for certificates */
122 u_int64_t cert_valid_from
= 0;
123 u_int64_t cert_valid_to
= ~0ULL;
125 /* Certificate options */
126 #define CERTOPT_X_FWD (1)
127 #define CERTOPT_AGENT_FWD (1<<1)
128 #define CERTOPT_PORT_FWD (1<<2)
129 #define CERTOPT_PTY (1<<3)
130 #define CERTOPT_USER_RC (1<<4)
131 #define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
132 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
133 u_int32_t certflags_flags
= CERTOPT_DEFAULT
;
134 char *certflags_command
= NULL
;
135 char *certflags_src_addr
= NULL
;
137 /* Conversion to/from various formats */
139 int convert_from
= 0;
144 } convert_format
= FMT_RFC4716
;
145 int print_public
= 0;
146 int print_generic
= 0;
148 char *key_type_name
= NULL
;
150 /* Load key from this PKCS#11 provider */
151 char *pkcs11provider
= NULL
;
154 extern char *__progname
;
156 char hostname
[MAXHOSTNAMELEN
];
159 int gen_candidates(FILE *, u_int32_t
, u_int32_t
, BIGNUM
*);
160 int prime_test(FILE *, FILE *, u_int32_t
, u_int32_t
);
163 ask_filename(struct passwd
*pw
, const char *prompt
)
168 if (key_type_name
== NULL
)
169 name
= _PATH_SSH_CLIENT_ID_RSA
;
171 switch (key_type_from_name(key_type_name
)) {
173 name
= _PATH_SSH_CLIENT_IDENTITY
;
176 case KEY_DSA_CERT_V00
:
178 name
= _PATH_SSH_CLIENT_ID_DSA
;
182 name
= _PATH_SSH_CLIENT_ID_ECDSA
;
185 case KEY_RSA_CERT_V00
:
187 name
= _PATH_SSH_CLIENT_ID_RSA
;
190 fprintf(stderr
, "bad key type\n");
195 snprintf(identity_file
, sizeof(identity_file
), "%s/%s", pw
->pw_dir
, name
);
196 fprintf(stderr
, "%s (%s): ", prompt
, identity_file
);
197 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
)
199 buf
[strcspn(buf
, "\n")] = '\0';
200 if (strcmp(buf
, "") != 0)
201 strlcpy(identity_file
, buf
, sizeof(identity_file
));
206 load_identity(char *filename
)
211 prv
= key_load_private(filename
, "", NULL
);
213 if (identity_passphrase
)
214 pass
= xstrdup(identity_passphrase
);
216 pass
= read_passphrase("Enter passphrase: ",
218 prv
= key_load_private(filename
, pass
, NULL
);
219 memset(pass
, 0, strlen(pass
));
225 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
226 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
227 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
228 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
231 do_convert_to_ssh2(struct passwd
*pw
, Key
*k
)
237 if (key_to_blob(k
, &blob
, &len
) <= 0) {
238 fprintf(stderr
, "key_to_blob failed\n");
241 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
242 snprintf(comment
, sizeof(comment
),
243 "%u-bit %s, converted by %s@%s from OpenSSH",
244 key_size(k
), key_type(k
),
245 pw
->pw_name
, hostname
);
247 fprintf(stdout
, "%s\n", SSH_COM_PUBLIC_BEGIN
);
248 fprintf(stdout
, "Comment: \"%s\"\n", comment
);
249 dump_base64(stdout
, blob
, len
);
250 fprintf(stdout
, "%s\n", SSH_COM_PUBLIC_END
);
257 do_convert_to_pkcs8(Key
*k
)
259 switch (key_type_plain(k
->type
)) {
261 if (!PEM_write_RSA_PUBKEY(stdout
, k
->rsa
))
262 fatal("PEM_write_RSA_PUBKEY failed");
265 if (!PEM_write_DSA_PUBKEY(stdout
, k
->dsa
))
266 fatal("PEM_write_DSA_PUBKEY failed");
268 #ifdef OPENSSL_HAS_ECC
270 if (!PEM_write_EC_PUBKEY(stdout
, k
->ecdsa
))
271 fatal("PEM_write_EC_PUBKEY failed");
275 fatal("%s: unsupported key type %s", __func__
, key_type(k
));
281 do_convert_to_pem(Key
*k
)
283 switch (key_type_plain(k
->type
)) {
285 if (!PEM_write_RSAPublicKey(stdout
, k
->rsa
))
286 fatal("PEM_write_RSAPublicKey failed");
288 #if notyet /* OpenSSH 0.9.8 lacks this function */
290 if (!PEM_write_DSAPublicKey(stdout
, k
->dsa
))
291 fatal("PEM_write_DSAPublicKey failed");
296 fatal("%s: unsupported key type %s", __func__
, key_type(k
));
302 do_convert_to(struct passwd
*pw
)
308 ask_filename(pw
, "Enter file in which the key is");
309 if (stat(identity_file
, &st
) < 0)
310 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
311 if ((k
= key_load_public(identity_file
, NULL
)) == NULL
) {
312 if ((k
= load_identity(identity_file
)) == NULL
) {
313 fprintf(stderr
, "load failed\n");
317 if (k
->type
== KEY_RSA1
) {
318 fprintf(stderr
, "version 1 keys are not supported\n");
322 switch (convert_format
) {
324 do_convert_to_ssh2(pw
, k
);
327 do_convert_to_pkcs8(k
);
330 do_convert_to_pem(k
);
333 fatal("%s: unknown key format %d", __func__
, convert_format
);
339 buffer_get_bignum_bits(Buffer
*b
, BIGNUM
*value
)
341 u_int bignum_bits
= buffer_get_int(b
);
342 u_int bytes
= (bignum_bits
+ 7) / 8;
344 if (buffer_len(b
) < bytes
)
345 fatal("buffer_get_bignum_bits: input buffer too small: "
346 "need %d have %d", bytes
, buffer_len(b
));
347 if (BN_bin2bn(buffer_ptr(b
), bytes
, value
) == NULL
)
348 fatal("buffer_get_bignum_bits: BN_bin2bn failed");
349 buffer_consume(b
, bytes
);
353 do_convert_private_ssh2_from_blob(u_char
*blob
, u_int blen
)
358 u_char
*sig
, data
[] = "abcde12345";
359 int magic
, rlen
, ktype
, i1
, i2
, i3
, i4
;
364 buffer_append(&b
, blob
, blen
);
366 magic
= buffer_get_int(&b
);
367 if (magic
!= SSH_COM_PRIVATE_KEY_MAGIC
) {
368 error("bad magic 0x%x != 0x%x", magic
, SSH_COM_PRIVATE_KEY_MAGIC
);
372 i1
= buffer_get_int(&b
);
373 type
= buffer_get_string(&b
, NULL
);
374 cipher
= buffer_get_string(&b
, NULL
);
375 i2
= buffer_get_int(&b
);
376 i3
= buffer_get_int(&b
);
377 i4
= buffer_get_int(&b
);
378 debug("ignore (%d %d %d %d)", i1
, i2
, i3
, i4
);
379 if (strcmp(cipher
, "none") != 0) {
380 error("unsupported cipher %s", cipher
);
388 if (strstr(type
, "dsa")) {
390 } else if (strstr(type
, "rsa")) {
397 key
= key_new_private(ktype
);
402 buffer_get_bignum_bits(&b
, key
->dsa
->p
);
403 buffer_get_bignum_bits(&b
, key
->dsa
->g
);
404 buffer_get_bignum_bits(&b
, key
->dsa
->q
);
405 buffer_get_bignum_bits(&b
, key
->dsa
->pub_key
);
406 buffer_get_bignum_bits(&b
, key
->dsa
->priv_key
);
409 e
= buffer_get_char(&b
);
413 e
+= buffer_get_char(&b
);
416 e
+= buffer_get_char(&b
);
419 if (!BN_set_word(key
->rsa
->e
, e
)) {
424 buffer_get_bignum_bits(&b
, key
->rsa
->d
);
425 buffer_get_bignum_bits(&b
, key
->rsa
->n
);
426 buffer_get_bignum_bits(&b
, key
->rsa
->iqmp
);
427 buffer_get_bignum_bits(&b
, key
->rsa
->q
);
428 buffer_get_bignum_bits(&b
, key
->rsa
->p
);
429 rsa_generate_additional_parameters(key
->rsa
);
432 rlen
= buffer_len(&b
);
434 error("do_convert_private_ssh2_from_blob: "
435 "remaining bytes in key blob %d", rlen
);
439 key_sign(key
, &sig
, &slen
, data
, sizeof(data
));
440 key_verify(key
, sig
, slen
, data
, sizeof(data
));
446 get_line(FILE *fp
, char *line
, size_t len
)
452 while ((c
= fgetc(fp
)) != EOF
) {
453 if (pos
>= len
- 1) {
454 fprintf(stderr
, "input line too long.\n");
460 if (c
!= EOF
&& c
!= '\n' && ungetc(c
, fp
) == EOF
) {
461 fprintf(stderr
, "unget: %s\n", strerror(errno
));
476 do_convert_from_ssh2(struct passwd
*pw
, Key
**k
, int *private)
486 if ((fp
= fopen(identity_file
, "r")) == NULL
)
487 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
489 while ((blen
= get_line(fp
, line
, sizeof(line
))) != -1) {
490 if (line
[blen
- 1] == '\\')
492 if (strncmp(line
, "----", 4) == 0 ||
493 strstr(line
, ": ") != NULL
) {
494 if (strstr(line
, SSH_COM_PRIVATE_BEGIN
) != NULL
)
496 if (strstr(line
, " END ") != NULL
) {
499 /* fprintf(stderr, "ignore: %s", line); */
504 /* fprintf(stderr, "escaped: %s", line); */
507 strlcat(encoded
, line
, sizeof(encoded
));
509 len
= strlen(encoded
);
510 if (((len
% 4) == 3) &&
511 (encoded
[len
-1] == '=') &&
512 (encoded
[len
-2] == '=') &&
513 (encoded
[len
-3] == '='))
514 encoded
[len
-3] = '\0';
515 blen
= uudecode(encoded
, blob
, sizeof(blob
));
517 fprintf(stderr
, "uudecode failed.\n");
521 do_convert_private_ssh2_from_blob(blob
, blen
) :
522 key_from_blob(blob
, blen
);
524 fprintf(stderr
, "decode blob failed.\n");
531 do_convert_from_pkcs8(Key
**k
, int *private)
536 if ((fp
= fopen(identity_file
, "r")) == NULL
)
537 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
538 if ((pubkey
= PEM_read_PUBKEY(fp
, NULL
, NULL
, NULL
)) == NULL
) {
539 fatal("%s: %s is not a recognised public key format", __func__
,
543 switch (EVP_PKEY_type(pubkey
->type
)) {
545 *k
= key_new(KEY_UNSPEC
);
546 (*k
)->type
= KEY_RSA
;
547 (*k
)->rsa
= EVP_PKEY_get1_RSA(pubkey
);
550 *k
= key_new(KEY_UNSPEC
);
551 (*k
)->type
= KEY_DSA
;
552 (*k
)->dsa
= EVP_PKEY_get1_DSA(pubkey
);
554 #ifdef OPENSSL_HAS_ECC
556 *k
= key_new(KEY_UNSPEC
);
557 (*k
)->type
= KEY_ECDSA
;
558 (*k
)->ecdsa
= EVP_PKEY_get1_EC_KEY(pubkey
);
559 (*k
)->ecdsa_nid
= key_ecdsa_group_to_nid(
560 EC_KEY_get0_group((*k
)->ecdsa
));
564 fatal("%s: unsupported pubkey type %d", __func__
,
565 EVP_PKEY_type(pubkey
->type
));
567 EVP_PKEY_free(pubkey
);
572 do_convert_from_pem(Key
**k
, int *private)
580 if ((fp
= fopen(identity_file
, "r")) == NULL
)
581 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
582 if ((rsa
= PEM_read_RSAPublicKey(fp
, NULL
, NULL
, NULL
)) != NULL
) {
583 *k
= key_new(KEY_UNSPEC
);
584 (*k
)->type
= KEY_RSA
;
589 #if notyet /* OpenSSH 0.9.8 lacks this function */
591 if ((dsa
= PEM_read_DSAPublicKey(fp
, NULL
, NULL
, NULL
)) != NULL
) {
592 *k
= key_new(KEY_UNSPEC
);
593 (*k
)->type
= KEY_DSA
;
600 fatal("%s: unrecognised raw private key format", __func__
);
604 do_convert_from(struct passwd
*pw
)
607 int private = 0, ok
= 0;
611 ask_filename(pw
, "Enter file in which the key is");
612 if (stat(identity_file
, &st
) < 0)
613 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
615 switch (convert_format
) {
617 do_convert_from_ssh2(pw
, &k
, &private);
620 do_convert_from_pkcs8(&k
, &private);
623 do_convert_from_pem(&k
, &private);
626 fatal("%s: unknown key format %d", __func__
, convert_format
);
630 ok
= key_write(k
, stdout
);
632 fprintf(stdout
, "\n");
636 ok
= PEM_write_DSAPrivateKey(stdout
, k
->dsa
, NULL
,
637 NULL
, 0, NULL
, NULL
);
639 #ifdef OPENSSL_HAS_ECC
641 ok
= PEM_write_ECPrivateKey(stdout
, k
->ecdsa
, NULL
,
642 NULL
, 0, NULL
, NULL
);
646 ok
= PEM_write_RSAPrivateKey(stdout
, k
->rsa
, NULL
,
647 NULL
, 0, NULL
, NULL
);
650 fatal("%s: unsupported key type %s", __func__
,
656 fprintf(stderr
, "key write failed\n");
664 do_print_public(struct passwd
*pw
)
670 ask_filename(pw
, "Enter file in which the key is");
671 if (stat(identity_file
, &st
) < 0) {
672 perror(identity_file
);
675 prv
= load_identity(identity_file
);
677 fprintf(stderr
, "load failed\n");
680 if (!key_write(prv
, stdout
))
681 fprintf(stderr
, "key_write failed");
683 fprintf(stdout
, "\n");
688 do_download(struct passwd
*pw
)
695 nkeys
= pkcs11_add_provider(pkcs11provider
, NULL
, &keys
);
697 fatal("cannot read public key from pkcs11");
698 for (i
= 0; i
< nkeys
; i
++) {
699 key_write(keys
[i
], stdout
);
701 fprintf(stdout
, "\n");
707 fatal("no pkcs11 support");
708 #endif /* ENABLE_PKCS11 */
712 do_fingerprint(struct passwd
*pw
)
716 char *comment
= NULL
, *cp
, *ep
, line
[16*1024], *fp
, *ra
;
717 int i
, skip
= 0, num
= 0, invalid
= 1;
722 fptype
= print_bubblebabble
? SSH_FP_SHA1
: SSH_FP_MD5
;
723 rep
= print_bubblebabble
? SSH_FP_BUBBLEBABBLE
: SSH_FP_HEX
;
726 ask_filename(pw
, "Enter file in which the key is");
727 if (stat(identity_file
, &st
) < 0) {
728 perror(identity_file
);
731 public = key_load_public(identity_file
, &comment
);
732 if (public != NULL
) {
733 fp
= key_fingerprint(public, fptype
, rep
);
734 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
735 printf("%u %s %s (%s)\n", key_size(public), fp
, comment
,
737 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
750 if ((f
= fopen(identity_file
, "r")) == NULL
)
751 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
753 while (fgets(line
, sizeof(line
), f
)) {
754 if ((cp
= strchr(line
, '\n')) == NULL
) {
755 error("line %d too long: %.40s...",
767 /* Skip leading whitespace, empty and comment lines. */
768 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
770 if (!*cp
|| *cp
== '\n' || *cp
== '#')
772 i
= strtol(cp
, &ep
, 10);
773 if (i
== 0 || ep
== NULL
|| (*ep
!= ' ' && *ep
!= '\t')) {
776 for (; *cp
&& (quoted
|| (*cp
!= ' ' &&
777 *cp
!= '\t')); cp
++) {
778 if (*cp
== '\\' && cp
[1] == '"')
779 cp
++; /* Skip both */
788 public = key_new(KEY_RSA1
);
789 if (key_read(public, &cp
) != 1) {
792 public = key_new(KEY_UNSPEC
);
793 if (key_read(public, &cp
) != 1) {
798 comment
= *cp
? cp
: comment
;
799 fp
= key_fingerprint(public, fptype
, rep
);
800 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
801 printf("%u %s %s (%s)\n", key_size(public), fp
,
802 comment
? comment
: "no comment", key_type(public));
803 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
813 printf("%s is not a public key file.\n", identity_file
);
820 printhost(FILE *f
, const char *name
, Key
*public, int ca
, int hash
)
822 if (print_fingerprint
) {
827 fptype
= print_bubblebabble
? SSH_FP_SHA1
: SSH_FP_MD5
;
828 rep
= print_bubblebabble
? SSH_FP_BUBBLEBABBLE
: SSH_FP_HEX
;
829 fp
= key_fingerprint(public, fptype
, rep
);
830 ra
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_RANDOMART
);
831 printf("%u %s %s (%s)\n", key_size(public), fp
, name
,
833 if (log_level
>= SYSLOG_LEVEL_VERBOSE
)
838 if (hash
&& (name
= host_hash(name
, NULL
, 0)) == NULL
)
839 fatal("hash_host failed");
840 fprintf(f
, "%s%s%s ", ca
? CA_MARKER
: "", ca
? " " : "", name
);
841 if (!key_write(public, f
))
842 fatal("key_write failed");
848 do_known_hosts(struct passwd
*pw
, const char *name
)
850 FILE *in
, *out
= stdout
;
852 char *cp
, *cp2
, *kp
, *kp2
;
853 char line
[16*1024], tmp
[MAXPATHLEN
], old
[MAXPATHLEN
];
854 int c
, skip
= 0, inplace
= 0, num
= 0, invalid
= 0, has_unhashed
= 0;
857 if (!have_identity
) {
858 cp
= tilde_expand_filename(_PATH_SSH_USER_HOSTFILE
, pw
->pw_uid
);
859 if (strlcpy(identity_file
, cp
, sizeof(identity_file
)) >=
860 sizeof(identity_file
))
861 fatal("Specified known hosts path too long");
865 if ((in
= fopen(identity_file
, "r")) == NULL
)
866 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
869 * Find hosts goes to stdout, hash and deletions happen in-place
870 * A corner case is ssh-keygen -HF foo, which should go to stdout
872 if (!find_host
&& (hash_hosts
|| delete_host
)) {
873 if (strlcpy(tmp
, identity_file
, sizeof(tmp
)) >= sizeof(tmp
) ||
874 strlcat(tmp
, ".XXXXXXXXXX", sizeof(tmp
)) >= sizeof(tmp
) ||
875 strlcpy(old
, identity_file
, sizeof(old
)) >= sizeof(old
) ||
876 strlcat(old
, ".old", sizeof(old
)) >= sizeof(old
))
877 fatal("known_hosts path too long");
879 if ((c
= mkstemp(tmp
)) == -1)
880 fatal("mkstemp: %s", strerror(errno
));
881 if ((out
= fdopen(c
, "w")) == NULL
) {
884 fatal("fdopen: %s", strerror(c
));
889 while (fgets(line
, sizeof(line
), in
)) {
890 if ((cp
= strchr(line
, '\n')) == NULL
) {
891 error("line %d too long: %.40s...", num
+ 1, line
);
903 /* Skip leading whitespace, empty and comment lines. */
904 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
906 if (!*cp
|| *cp
== '\n' || *cp
== '#') {
908 fprintf(out
, "%s\n", cp
);
911 /* Check whether this is a CA key */
912 if (strncasecmp(cp
, CA_MARKER
, sizeof(CA_MARKER
) - 1) == 0 &&
913 (cp
[sizeof(CA_MARKER
) - 1] == ' ' ||
914 cp
[sizeof(CA_MARKER
) - 1] == '\t')) {
916 cp
+= sizeof(CA_MARKER
);
920 /* Find the end of the host name portion. */
921 for (kp
= cp
; *kp
&& *kp
!= ' ' && *kp
!= '\t'; kp
++)
924 if (*kp
== '\0' || *(kp
+ 1) == '\0') {
925 error("line %d missing key: %.40s...",
933 pub
= key_new(KEY_RSA1
);
934 if (key_read(pub
, &kp
) != 1) {
937 pub
= key_new(KEY_UNSPEC
);
938 if (key_read(pub
, &kp
) != 1) {
939 error("line %d invalid key: %.40s...",
947 if (*cp
== HASH_DELIM
) {
948 if (find_host
|| delete_host
) {
949 cp2
= host_hash(name
, cp
, strlen(cp
));
951 error("line %d: invalid hashed "
952 "name: %.64s...", num
, line
);
956 c
= (strcmp(cp2
, cp
) == 0);
957 if (find_host
&& c
) {
958 printf("# Host %s found: "
959 "line %d type %s%s\n", name
,
961 ca
? " (CA key)" : "");
962 printhost(out
, cp
, pub
, ca
, 0);
964 if (delete_host
&& !c
&& !ca
)
965 printhost(out
, cp
, pub
, ca
, 0);
966 } else if (hash_hosts
)
967 printhost(out
, cp
, pub
, ca
, 0);
969 if (find_host
|| delete_host
) {
970 c
= (match_hostname(name
, cp
,
972 if (find_host
&& c
) {
973 printf("# Host %s found: "
974 "line %d type %s%s\n", name
,
976 ca
? " (CA key)" : "");
977 printhost(out
, name
, pub
,
978 ca
, hash_hosts
&& !ca
);
980 if (delete_host
&& !c
&& !ca
)
981 printhost(out
, cp
, pub
, ca
, 0);
982 } else if (hash_hosts
) {
983 for (cp2
= strsep(&cp
, ",");
984 cp2
!= NULL
&& *cp2
!= '\0';
985 cp2
= strsep(&cp
, ",")) {
987 fprintf(stderr
, "Warning: "
988 "ignoring CA key for host: "
990 printhost(out
, cp2
, pub
, ca
, 0);
991 } else if (strcspn(cp2
, "*?!") !=
993 fprintf(stderr
, "Warning: "
994 "ignoring host name with "
995 "metacharacters: %.64s\n",
997 printhost(out
, cp2
, pub
, ca
, 0);
999 printhost(out
, cp2
, pub
, ca
, 1);
1009 fprintf(stderr
, "%s is not a valid known_hosts file.\n",
1012 fprintf(stderr
, "Not replacing existing known_hosts "
1013 "file because of errors\n");
1023 /* Backup existing file */
1024 if (unlink(old
) == -1 && errno
!= ENOENT
)
1025 fatal("unlink %.100s: %s", old
, strerror(errno
));
1026 if (link(identity_file
, old
) == -1)
1027 fatal("link %.100s to %.100s: %s", identity_file
, old
,
1029 /* Move new one into place */
1030 if (rename(tmp
, identity_file
) == -1) {
1031 error("rename\"%s\" to \"%s\": %s", tmp
, identity_file
,
1038 fprintf(stderr
, "%s updated.\n", identity_file
);
1039 fprintf(stderr
, "Original contents retained as %s\n", old
);
1041 fprintf(stderr
, "WARNING: %s contains unhashed "
1043 fprintf(stderr
, "Delete this file to ensure privacy "
1052 * Perform changing a passphrase. The argument is the passwd structure
1053 * for the current user.
1056 do_change_passphrase(struct passwd
*pw
)
1059 char *old_passphrase
, *passphrase1
, *passphrase2
;
1064 ask_filename(pw
, "Enter file in which the key is");
1065 if (stat(identity_file
, &st
) < 0) {
1066 perror(identity_file
);
1069 /* Try to load the file with empty passphrase. */
1070 private = key_load_private(identity_file
, "", &comment
);
1071 if (private == NULL
) {
1072 if (identity_passphrase
)
1073 old_passphrase
= xstrdup(identity_passphrase
);
1076 read_passphrase("Enter old passphrase: ",
1078 private = key_load_private(identity_file
, old_passphrase
,
1080 memset(old_passphrase
, 0, strlen(old_passphrase
));
1081 xfree(old_passphrase
);
1082 if (private == NULL
) {
1083 printf("Bad passphrase.\n");
1087 printf("Key has comment '%s'\n", comment
);
1089 /* Ask the new passphrase (twice). */
1090 if (identity_new_passphrase
) {
1091 passphrase1
= xstrdup(identity_new_passphrase
);
1095 read_passphrase("Enter new passphrase (empty for no "
1096 "passphrase): ", RP_ALLOW_STDIN
);
1097 passphrase2
= read_passphrase("Enter same passphrase again: ",
1100 /* Verify that they are the same. */
1101 if (strcmp(passphrase1
, passphrase2
) != 0) {
1102 memset(passphrase1
, 0, strlen(passphrase1
));
1103 memset(passphrase2
, 0, strlen(passphrase2
));
1106 printf("Pass phrases do not match. Try again.\n");
1109 /* Destroy the other copy. */
1110 memset(passphrase2
, 0, strlen(passphrase2
));
1114 /* Save the file using the new passphrase. */
1115 if (!key_save_private(private, identity_file
, passphrase1
, comment
)) {
1116 printf("Saving the key failed: %s.\n", identity_file
);
1117 memset(passphrase1
, 0, strlen(passphrase1
));
1123 /* Destroy the passphrase and the copy of the key in memory. */
1124 memset(passphrase1
, 0, strlen(passphrase1
));
1126 key_free(private); /* Destroys contents */
1129 printf("Your identification has been saved with the new passphrase.\n");
1134 * Print the SSHFP RR.
1137 do_print_resource_record(struct passwd
*pw
, char *fname
, char *hname
)
1140 char *comment
= NULL
;
1144 ask_filename(pw
, "Enter file in which the key is");
1145 if (stat(fname
, &st
) < 0) {
1146 if (errno
== ENOENT
)
1151 public = key_load_public(fname
, &comment
);
1152 if (public != NULL
) {
1153 export_dns_rr(hname
, public, stdout
, print_generic
);
1161 printf("failed to read v2 public key from %s.\n", fname
);
1166 * Change the comment of a private key file.
1169 do_change_comment(struct passwd
*pw
)
1171 char new_comment
[1024], *comment
, *passphrase
;
1179 ask_filename(pw
, "Enter file in which the key is");
1180 if (stat(identity_file
, &st
) < 0) {
1181 perror(identity_file
);
1184 private = key_load_private(identity_file
, "", &comment
);
1185 if (private == NULL
) {
1186 if (identity_passphrase
)
1187 passphrase
= xstrdup(identity_passphrase
);
1188 else if (identity_new_passphrase
)
1189 passphrase
= xstrdup(identity_new_passphrase
);
1191 passphrase
= read_passphrase("Enter passphrase: ",
1193 /* Try to load using the passphrase. */
1194 private = key_load_private(identity_file
, passphrase
, &comment
);
1195 if (private == NULL
) {
1196 memset(passphrase
, 0, strlen(passphrase
));
1198 printf("Bad passphrase.\n");
1202 passphrase
= xstrdup("");
1204 if (private->type
!= KEY_RSA1
) {
1205 fprintf(stderr
, "Comments are only supported for RSA1 keys.\n");
1209 printf("Key now has comment '%s'\n", comment
);
1211 if (identity_comment
) {
1212 strlcpy(new_comment
, identity_comment
, sizeof(new_comment
));
1214 printf("Enter new comment: ");
1216 if (!fgets(new_comment
, sizeof(new_comment
), stdin
)) {
1217 memset(passphrase
, 0, strlen(passphrase
));
1221 new_comment
[strcspn(new_comment
, "\n")] = '\0';
1224 /* Save the file using the new passphrase. */
1225 if (!key_save_private(private, identity_file
, passphrase
, new_comment
)) {
1226 printf("Saving the key failed: %s.\n", identity_file
);
1227 memset(passphrase
, 0, strlen(passphrase
));
1233 memset(passphrase
, 0, strlen(passphrase
));
1235 public = key_from_private(private);
1238 strlcat(identity_file
, ".pub", sizeof(identity_file
));
1239 fd
= open(identity_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
1241 printf("Could not save your public key in %s\n", identity_file
);
1244 f
= fdopen(fd
, "w");
1246 printf("fdopen %s failed\n", identity_file
);
1249 if (!key_write(public, f
))
1250 fprintf(stderr
, "write key failed\n");
1252 fprintf(f
, " %s\n", new_comment
);
1257 printf("The comment in your key file has been changed.\n");
1262 fmt_validity(u_int64_t valid_from
, u_int64_t valid_to
)
1264 char from
[32], to
[32];
1265 static char ret
[64];
1270 if (valid_from
== 0 && valid_to
== 0xffffffffffffffffULL
)
1273 if (valid_from
!= 0) {
1274 /* XXX revisit INT_MAX in 2038 :) */
1275 tt
= valid_from
> INT_MAX
? INT_MAX
: valid_from
;
1276 tm
= localtime(&tt
);
1277 strftime(from
, sizeof(from
), "%Y-%m-%dT%H:%M:%S", tm
);
1279 if (valid_to
!= 0xffffffffffffffffULL
) {
1280 /* XXX revisit INT_MAX in 2038 :) */
1281 tt
= valid_to
> INT_MAX
? INT_MAX
: valid_to
;
1282 tm
= localtime(&tt
);
1283 strftime(to
, sizeof(to
), "%Y-%m-%dT%H:%M:%S", tm
);
1286 if (valid_from
== 0) {
1287 snprintf(ret
, sizeof(ret
), "before %s", to
);
1290 if (valid_to
== 0xffffffffffffffffULL
) {
1291 snprintf(ret
, sizeof(ret
), "after %s", from
);
1295 snprintf(ret
, sizeof(ret
), "from %s to %s", from
, to
);
1300 add_flag_option(Buffer
*c
, const char *name
)
1302 debug3("%s: %s", __func__
, name
);
1303 buffer_put_cstring(c
, name
);
1304 buffer_put_string(c
, NULL
, 0);
1308 add_string_option(Buffer
*c
, const char *name
, const char *value
)
1312 debug3("%s: %s=%s", __func__
, name
, value
);
1314 buffer_put_cstring(&b
, value
);
1316 buffer_put_cstring(c
, name
);
1317 buffer_put_string(c
, buffer_ptr(&b
), buffer_len(&b
));
1322 #define OPTIONS_CRITICAL 1
1323 #define OPTIONS_EXTENSIONS 2
1325 prepare_options_buf(Buffer
*c
, int which
)
1328 if ((which
& OPTIONS_CRITICAL
) != 0 &&
1329 certflags_command
!= NULL
)
1330 add_string_option(c
, "force-command", certflags_command
);
1331 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1332 (certflags_flags
& CERTOPT_AGENT_FWD
) != 0)
1333 add_flag_option(c
, "permit-agent-forwarding");
1334 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1335 (certflags_flags
& CERTOPT_PORT_FWD
) != 0)
1336 add_flag_option(c
, "permit-port-forwarding");
1337 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1338 (certflags_flags
& CERTOPT_PTY
) != 0)
1339 add_flag_option(c
, "permit-pty");
1340 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1341 (certflags_flags
& CERTOPT_USER_RC
) != 0)
1342 add_flag_option(c
, "permit-user-rc");
1343 if ((which
& OPTIONS_EXTENSIONS
) != 0 &&
1344 (certflags_flags
& CERTOPT_X_FWD
) != 0)
1345 add_flag_option(c
, "permit-X11-forwarding");
1346 if ((which
& OPTIONS_CRITICAL
) != 0 &&
1347 certflags_src_addr
!= NULL
)
1348 add_string_option(c
, "source-address", certflags_src_addr
);
1352 load_pkcs11_key(char *path
)
1354 #ifdef ENABLE_PKCS11
1355 Key
**keys
= NULL
, *public, *private = NULL
;
1358 if ((public = key_load_public(path
, NULL
)) == NULL
)
1359 fatal("Couldn't load CA public key \"%s\"", path
);
1361 nkeys
= pkcs11_add_provider(pkcs11provider
, identity_passphrase
, &keys
);
1362 debug3("%s: %d keys", __func__
, nkeys
);
1364 fatal("cannot read public key from pkcs11");
1365 for (i
= 0; i
< nkeys
; i
++) {
1366 if (key_equal_public(public, keys
[i
])) {
1376 fatal("no pkcs11 support");
1377 #endif /* ENABLE_PKCS11 */
1381 do_ca_sign(struct passwd
*pw
, int argc
, char **argv
)
1386 char *otmp
, *tmp
, *cp
, *out
, *comment
, **plist
= NULL
;
1388 int v00
= 0; /* legacy keys */
1390 if (key_type_name
!= NULL
) {
1391 switch (key_type_from_name(key_type_name
)) {
1392 case KEY_RSA_CERT_V00
:
1393 case KEY_DSA_CERT_V00
:
1397 if (strcasecmp(key_type_name
, "v00") == 0) {
1400 } else if (strcasecmp(key_type_name
, "v01") == 0)
1404 fprintf(stderr
, "unknown key type %s\n", key_type_name
);
1410 tmp
= tilde_expand_filename(ca_key_path
, pw
->pw_uid
);
1411 if (pkcs11provider
!= NULL
) {
1412 if ((ca
= load_pkcs11_key(tmp
)) == NULL
)
1413 fatal("No PKCS#11 key matching %s found", ca_key_path
);
1414 } else if ((ca
= load_identity(tmp
)) == NULL
)
1415 fatal("Couldn't load CA key \"%s\"", tmp
);
1418 for (i
= 0; i
< argc
; i
++) {
1419 /* Split list of principals */
1421 if (cert_principals
!= NULL
) {
1422 otmp
= tmp
= xstrdup(cert_principals
);
1424 for (; (cp
= strsep(&tmp
, ",")) != NULL
; n
++) {
1425 plist
= xrealloc(plist
, n
+ 1, sizeof(*plist
));
1426 if (*(plist
[n
] = xstrdup(cp
)) == '\0')
1427 fatal("Empty principal name");
1432 tmp
= tilde_expand_filename(argv
[i
], pw
->pw_uid
);
1433 if ((public = key_load_public(tmp
, &comment
)) == NULL
)
1434 fatal("%s: unable to open \"%s\"", __func__
, tmp
);
1435 if (public->type
!= KEY_RSA
&& public->type
!= KEY_DSA
&&
1436 public->type
!= KEY_ECDSA
)
1437 fatal("%s: key \"%s\" type %s cannot be certified",
1438 __func__
, tmp
, key_type(public));
1440 /* Prepare certificate to sign */
1441 if (key_to_certified(public, v00
) != 0)
1442 fatal("Could not upgrade key %s to certificate", tmp
);
1443 public->cert
->type
= cert_key_type
;
1444 public->cert
->serial
= (u_int64_t
)cert_serial
;
1445 public->cert
->key_id
= xstrdup(cert_key_id
);
1446 public->cert
->nprincipals
= n
;
1447 public->cert
->principals
= plist
;
1448 public->cert
->valid_after
= cert_valid_from
;
1449 public->cert
->valid_before
= cert_valid_to
;
1451 prepare_options_buf(&public->cert
->critical
,
1452 OPTIONS_CRITICAL
|OPTIONS_EXTENSIONS
);
1454 prepare_options_buf(&public->cert
->critical
,
1456 prepare_options_buf(&public->cert
->extensions
,
1457 OPTIONS_EXTENSIONS
);
1459 public->cert
->signature_key
= key_from_private(ca
);
1461 if (key_certify(public, ca
) != 0)
1462 fatal("Couldn't not certify key %s", tmp
);
1464 if ((cp
= strrchr(tmp
, '.')) != NULL
&& strcmp(cp
, ".pub") == 0)
1466 xasprintf(&out
, "%s-cert.pub", tmp
);
1469 if ((fd
= open(out
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644)) == -1)
1470 fatal("Could not open \"%s\" for writing: %s", out
,
1472 if ((f
= fdopen(fd
, "w")) == NULL
)
1473 fatal("%s: fdopen: %s", __func__
, strerror(errno
));
1474 if (!key_write(public, f
))
1475 fatal("Could not write certified key to %s", out
);
1476 fprintf(f
, " %s\n", comment
);
1480 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1481 "valid %s", key_cert_type(public),
1482 out
, public->cert
->key_id
, public->cert
->serial
,
1483 cert_principals
!= NULL
? " for " : "",
1484 cert_principals
!= NULL
? cert_principals
: "",
1485 fmt_validity(cert_valid_from
, cert_valid_to
));
1496 parse_relative_time(const char *s
, time_t now
)
1500 mul
= *s
== '-' ? -1 : 1;
1502 if ((secs
= convtime(s
+ 1)) == -1)
1503 fatal("Invalid relative certificate time %s", s
);
1504 if (mul
== -1 && secs
> now
)
1505 fatal("Certificate time %s cannot be represented", s
);
1506 return now
+ (u_int64_t
)(secs
* mul
);
1510 parse_absolute_time(const char *s
)
1517 * POSIX strptime says "The application shall ensure that there
1518 * is white-space or other non-alphanumeric characters between
1519 * any two conversion specifications" so arrange things this way.
1521 switch (strlen(s
)) {
1524 snprintf(buf
, sizeof(buf
), "%.4s-%.2s-%.2s", s
, s
+ 4, s
+ 6);
1527 fmt
= "%Y-%m-%dT%H:%M:%S";
1528 snprintf(buf
, sizeof(buf
), "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s",
1529 s
, s
+ 4, s
+ 6, s
+ 8, s
+ 10, s
+ 12);
1532 fatal("Invalid certificate time format %s", s
);
1535 bzero(&tm
, sizeof(tm
));
1536 if (strptime(buf
, fmt
, &tm
) == NULL
)
1537 fatal("Invalid certificate time %s", s
);
1538 if ((tt
= mktime(&tm
)) < 0)
1539 fatal("Certificate time %s cannot be represented", s
);
1540 return (u_int64_t
)tt
;
1544 parse_cert_times(char *timespec
)
1547 time_t now
= time(NULL
);
1550 /* +timespec relative to now */
1551 if (*timespec
== '+' && strchr(timespec
, ':') == NULL
) {
1552 if ((secs
= convtime(timespec
+ 1)) == -1)
1553 fatal("Invalid relative certificate life %s", timespec
);
1554 cert_valid_to
= now
+ secs
;
1556 * Backdate certificate one minute to avoid problems on hosts
1557 * with poorly-synchronised clocks.
1559 cert_valid_from
= ((now
- 59)/ 60) * 60;
1565 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1566 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS
1568 from
= xstrdup(timespec
);
1569 to
= strchr(from
, ':');
1570 if (to
== NULL
|| from
== to
|| *(to
+ 1) == '\0')
1571 fatal("Invalid certificate life specification %s", timespec
);
1574 if (*from
== '-' || *from
== '+')
1575 cert_valid_from
= parse_relative_time(from
, now
);
1577 cert_valid_from
= parse_absolute_time(from
);
1579 if (*to
== '-' || *to
== '+')
1580 cert_valid_to
= parse_relative_time(to
, cert_valid_from
);
1582 cert_valid_to
= parse_absolute_time(to
);
1584 if (cert_valid_to
<= cert_valid_from
)
1585 fatal("Empty certificate validity interval");
1590 add_cert_option(char *opt
)
1594 if (strcmp(opt
, "clear") == 0)
1595 certflags_flags
= 0;
1596 else if (strcasecmp(opt
, "no-x11-forwarding") == 0)
1597 certflags_flags
&= ~CERTOPT_X_FWD
;
1598 else if (strcasecmp(opt
, "permit-x11-forwarding") == 0)
1599 certflags_flags
|= CERTOPT_X_FWD
;
1600 else if (strcasecmp(opt
, "no-agent-forwarding") == 0)
1601 certflags_flags
&= ~CERTOPT_AGENT_FWD
;
1602 else if (strcasecmp(opt
, "permit-agent-forwarding") == 0)
1603 certflags_flags
|= CERTOPT_AGENT_FWD
;
1604 else if (strcasecmp(opt
, "no-port-forwarding") == 0)
1605 certflags_flags
&= ~CERTOPT_PORT_FWD
;
1606 else if (strcasecmp(opt
, "permit-port-forwarding") == 0)
1607 certflags_flags
|= CERTOPT_PORT_FWD
;
1608 else if (strcasecmp(opt
, "no-pty") == 0)
1609 certflags_flags
&= ~CERTOPT_PTY
;
1610 else if (strcasecmp(opt
, "permit-pty") == 0)
1611 certflags_flags
|= CERTOPT_PTY
;
1612 else if (strcasecmp(opt
, "no-user-rc") == 0)
1613 certflags_flags
&= ~CERTOPT_USER_RC
;
1614 else if (strcasecmp(opt
, "permit-user-rc") == 0)
1615 certflags_flags
|= CERTOPT_USER_RC
;
1616 else if (strncasecmp(opt
, "force-command=", 14) == 0) {
1619 fatal("Empty force-command option");
1620 if (certflags_command
!= NULL
)
1621 fatal("force-command already specified");
1622 certflags_command
= xstrdup(val
);
1623 } else if (strncasecmp(opt
, "source-address=", 15) == 0) {
1626 fatal("Empty source-address option");
1627 if (certflags_src_addr
!= NULL
)
1628 fatal("source-address already specified");
1629 if (addr_match_cidr_list(NULL
, val
) != 0)
1630 fatal("Invalid source-address list");
1631 certflags_src_addr
= xstrdup(val
);
1633 fatal("Unsupported certificate option \"%s\"", opt
);
1637 show_options(const Buffer
*optbuf
, int v00
, int in_critical
)
1639 u_char
*name
, *data
;
1641 Buffer options
, option
;
1643 buffer_init(&options
);
1644 buffer_append(&options
, buffer_ptr(optbuf
), buffer_len(optbuf
));
1646 buffer_init(&option
);
1647 while (buffer_len(&options
) != 0) {
1648 name
= buffer_get_string(&options
, NULL
);
1649 data
= buffer_get_string_ptr(&options
, &dlen
);
1650 buffer_append(&option
, data
, dlen
);
1651 printf(" %s", name
);
1652 if ((v00
|| !in_critical
) &&
1653 (strcmp(name
, "permit-X11-forwarding") == 0 ||
1654 strcmp(name
, "permit-agent-forwarding") == 0 ||
1655 strcmp(name
, "permit-port-forwarding") == 0 ||
1656 strcmp(name
, "permit-pty") == 0 ||
1657 strcmp(name
, "permit-user-rc") == 0))
1659 else if ((v00
|| in_critical
) &&
1660 (strcmp(name
, "force-command") == 0 ||
1661 strcmp(name
, "source-address") == 0)) {
1662 data
= buffer_get_string(&option
, NULL
);
1663 printf(" %s\n", data
);
1666 printf(" UNKNOWN OPTION (len %u)\n",
1667 buffer_len(&option
));
1668 buffer_clear(&option
);
1671 if (buffer_len(&option
) != 0)
1672 fatal("Option corrupt: extra data at end");
1674 buffer_free(&option
);
1675 buffer_free(&options
);
1679 do_show_cert(struct passwd
*pw
)
1683 char *key_fp
, *ca_fp
;
1687 ask_filename(pw
, "Enter file in which the key is");
1688 if (stat(identity_file
, &st
) < 0)
1689 fatal("%s: %s: %s", __progname
, identity_file
, strerror(errno
));
1690 if ((key
= key_load_public(identity_file
, NULL
)) == NULL
)
1691 fatal("%s is not a public key", identity_file
);
1692 if (!key_is_cert(key
))
1693 fatal("%s is not a certificate", identity_file
);
1694 v00
= key
->type
== KEY_RSA_CERT_V00
|| key
->type
== KEY_DSA_CERT_V00
;
1696 key_fp
= key_fingerprint(key
, SSH_FP_MD5
, SSH_FP_HEX
);
1697 ca_fp
= key_fingerprint(key
->cert
->signature_key
,
1698 SSH_FP_MD5
, SSH_FP_HEX
);
1700 printf("%s:\n", identity_file
);
1701 printf(" Type: %s %s certificate\n", key_ssh_name(key
),
1702 key_cert_type(key
));
1703 printf(" Public key: %s %s\n", key_type(key
), key_fp
);
1704 printf(" Signing CA: %s %s\n",
1705 key_type(key
->cert
->signature_key
), ca_fp
);
1706 printf(" Key ID: \"%s\"\n", key
->cert
->key_id
);
1708 printf(" Serial: %llu\n", key
->cert
->serial
);
1709 printf(" Valid: %s\n",
1710 fmt_validity(key
->cert
->valid_after
, key
->cert
->valid_before
));
1711 printf(" Principals: ");
1712 if (key
->cert
->nprincipals
== 0)
1715 for (i
= 0; i
< key
->cert
->nprincipals
; i
++)
1717 key
->cert
->principals
[i
]);
1720 printf(" Critical Options: ");
1721 if (buffer_len(&key
->cert
->critical
) == 0)
1725 show_options(&key
->cert
->critical
, v00
, 1);
1728 printf(" Extensions: ");
1729 if (buffer_len(&key
->cert
->extensions
) == 0)
1733 show_options(&key
->cert
->extensions
, v00
, 0);
1742 fprintf(stderr
, "usage: %s [options]\n", __progname
);
1743 fprintf(stderr
, "Options:\n");
1744 fprintf(stderr
, " -a trials Number of trials for screening DH-GEX moduli.\n");
1745 fprintf(stderr
, " -B Show bubblebabble digest of key file.\n");
1746 fprintf(stderr
, " -b bits Number of bits in the key to create.\n");
1747 fprintf(stderr
, " -C comment Provide new comment.\n");
1748 fprintf(stderr
, " -c Change comment in private and public key files.\n");
1749 #ifdef ENABLE_PKCS11
1750 fprintf(stderr
, " -D pkcs11 Download public key from pkcs11 token.\n");
1752 fprintf(stderr
, " -e Export OpenSSH to foreign format key file.\n");
1753 fprintf(stderr
, " -F hostname Find hostname in known hosts file.\n");
1754 fprintf(stderr
, " -f filename Filename of the key file.\n");
1755 fprintf(stderr
, " -G file Generate candidates for DH-GEX moduli.\n");
1756 fprintf(stderr
, " -g Use generic DNS resource record format.\n");
1757 fprintf(stderr
, " -H Hash names in known_hosts file.\n");
1758 fprintf(stderr
, " -h Generate host certificate instead of a user certificate.\n");
1759 fprintf(stderr
, " -I key_id Key identifier to include in certificate.\n");
1760 fprintf(stderr
, " -i Import foreign format to OpenSSH key file.\n");
1761 fprintf(stderr
, " -L Print the contents of a certificate.\n");
1762 fprintf(stderr
, " -l Show fingerprint of key file.\n");
1763 fprintf(stderr
, " -M memory Amount of memory (MB) to use for generating DH-GEX moduli.\n");
1764 fprintf(stderr
, " -m key_fmt Conversion format for -e/-i (PEM|PKCS8|RFC4716).\n");
1765 fprintf(stderr
, " -N phrase Provide new passphrase.\n");
1766 fprintf(stderr
, " -n name,... User/host principal names to include in certificate\n");
1767 fprintf(stderr
, " -O option Specify a certificate option.\n");
1768 fprintf(stderr
, " -P phrase Provide old passphrase.\n");
1769 fprintf(stderr
, " -p Change passphrase of private key file.\n");
1770 fprintf(stderr
, " -q Quiet.\n");
1771 fprintf(stderr
, " -R hostname Remove host from known_hosts file.\n");
1772 fprintf(stderr
, " -r hostname Print DNS resource record.\n");
1773 fprintf(stderr
, " -S start Start point (hex) for generating DH-GEX moduli.\n");
1774 fprintf(stderr
, " -s ca_key Certify keys with CA key.\n");
1775 fprintf(stderr
, " -T file Screen candidates for DH-GEX moduli.\n");
1776 fprintf(stderr
, " -t type Specify type of key to create.\n");
1777 fprintf(stderr
, " -V from:to Specify certificate validity interval.\n");
1778 fprintf(stderr
, " -v Verbose.\n");
1779 fprintf(stderr
, " -W gen Generator to use for generating DH-GEX moduli.\n");
1780 fprintf(stderr
, " -y Read private key file and print public key.\n");
1781 fprintf(stderr
, " -z serial Specify a serial number.\n");
1787 * Main program for key management.
1790 main(int argc
, char **argv
)
1792 char dotsshdir
[MAXPATHLEN
], comment
[1024], *passphrase1
, *passphrase2
;
1793 char out_file
[MAXPATHLEN
], *rr_hostname
= NULL
;
1794 Key
*private, *public;
1799 u_int32_t memory
= 0, generator_wanted
= 0, trials
= 100;
1800 int do_gen_candidates
= 0, do_screen_candidates
= 0;
1801 BIGNUM
*start
= NULL
;
1806 extern char *optarg
;
1808 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
1811 __progname
= ssh_get_progname(argv
[0]);
1813 OpenSSL_add_all_algorithms();
1814 log_init(argv
[0], SYSLOG_LEVEL_INFO
, SYSLOG_FACILITY_USER
, 1);
1819 /* we need this for the home * directory. */
1820 pw
= getpwuid(getuid());
1822 printf("You don't exist, go away!\n");
1825 if (gethostname(hostname
, sizeof(hostname
)) < 0) {
1826 perror("gethostname");
1830 while ((opt
= getopt(argc
, argv
, "degiqpclBHLhvxXyF:b:f:t:D:I:P:m:N:n:"
1831 "O:C:r:g:R:T:G:M:S:s:a:V:W:z:")) != -1) {
1834 bits
= (u_int32_t
)strtonum(optarg
, 256, 32768, &errstr
);
1836 fatal("Bits has bad value %s (%s)",
1841 rr_hostname
= optarg
;
1847 cert_key_id
= optarg
;
1851 rr_hostname
= optarg
;
1857 print_fingerprint
= 1;
1860 print_bubblebabble
= 1;
1863 if (strcasecmp(optarg
, "RFC4716") == 0 ||
1864 strcasecmp(optarg
, "ssh2") == 0) {
1865 convert_format
= FMT_RFC4716
;
1868 if (strcasecmp(optarg
, "PKCS8") == 0) {
1869 convert_format
= FMT_PKCS8
;
1872 if (strcasecmp(optarg
, "PEM") == 0) {
1873 convert_format
= FMT_PEM
;
1876 fatal("Unsupported conversion format \"%s\"", optarg
);
1878 cert_principals
= optarg
;
1881 change_passphrase
= 1;
1887 if (strlcpy(identity_file
, optarg
, sizeof(identity_file
)) >=
1888 sizeof(identity_file
))
1889 fatal("Identity filename too long");
1896 identity_passphrase
= optarg
;
1899 identity_new_passphrase
= optarg
;
1902 add_cert_option(optarg
);
1905 identity_comment
= optarg
;
1916 cert_key_type
= SSH2_CERT_TYPE_HOST
;
1917 certflags_flags
= 0;
1928 key_type_name
= "dsa";
1931 ca_key_path
= optarg
;
1934 key_type_name
= optarg
;
1937 pkcs11provider
= optarg
;
1940 if (log_level
== SYSLOG_LEVEL_INFO
)
1941 log_level
= SYSLOG_LEVEL_DEBUG1
;
1943 if (log_level
>= SYSLOG_LEVEL_DEBUG1
&&
1944 log_level
< SYSLOG_LEVEL_DEBUG3
)
1949 rr_hostname
= optarg
;
1952 generator_wanted
= (u_int32_t
)strtonum(optarg
, 1,
1955 fatal("Desired generator has bad value: %s (%s)",
1959 trials
= (u_int32_t
)strtonum(optarg
, 1, UINT_MAX
, &errstr
);
1961 fatal("Invalid number of trials: %s (%s)",
1965 memory
= (u_int32_t
)strtonum(optarg
, 1, UINT_MAX
, &errstr
);
1967 fatal("Memory limit is %s: %s", errstr
, optarg
);
1970 do_gen_candidates
= 1;
1971 if (strlcpy(out_file
, optarg
, sizeof(out_file
)) >=
1973 fatal("Output filename too long");
1976 do_screen_candidates
= 1;
1977 if (strlcpy(out_file
, optarg
, sizeof(out_file
)) >=
1979 fatal("Output filename too long");
1982 /* XXX - also compare length against bits */
1983 if (BN_hex2bn(&start
, optarg
) == 0)
1984 fatal("Invalid start point.");
1987 parse_cert_times(optarg
);
1990 cert_serial
= strtonum(optarg
, 0, LLONG_MAX
, &errstr
);
1992 fatal("Invalid serial number: %s", errstr
);
2001 log_init(argv
[0], log_level
, SYSLOG_FACILITY_USER
, 1);
2006 if (ca_key_path
!= NULL
) {
2008 printf("Too few arguments.\n");
2011 } else if (argc
> 0) {
2012 printf("Too many arguments.\n");
2015 if (change_passphrase
&& change_comment
) {
2016 printf("Can only have one of -p and -c.\n");
2019 if (print_fingerprint
&& (delete_host
|| hash_hosts
)) {
2020 printf("Cannot use -l with -D or -R.\n");
2023 if (ca_key_path
!= NULL
) {
2024 if (cert_key_id
== NULL
)
2025 fatal("Must specify key id (-I) when certifying");
2026 do_ca_sign(pw
, argc
, argv
);
2030 if (delete_host
|| hash_hosts
|| find_host
)
2031 do_known_hosts(pw
, rr_hostname
);
2032 if (print_fingerprint
|| print_bubblebabble
)
2034 if (change_passphrase
)
2035 do_change_passphrase(pw
);
2037 do_change_comment(pw
);
2041 do_convert_from(pw
);
2043 do_print_public(pw
);
2044 if (rr_hostname
!= NULL
) {
2047 if (have_identity
) {
2048 n
= do_print_resource_record(pw
,
2049 identity_file
, rr_hostname
);
2051 perror(identity_file
);
2057 n
+= do_print_resource_record(pw
,
2058 _PATH_HOST_RSA_KEY_FILE
, rr_hostname
);
2059 n
+= do_print_resource_record(pw
,
2060 _PATH_HOST_DSA_KEY_FILE
, rr_hostname
);
2063 fatal("no keys found.");
2067 if (pkcs11provider
!= NULL
)
2070 if (do_gen_candidates
) {
2071 FILE *out
= fopen(out_file
, "w");
2074 error("Couldn't open modulus candidate file \"%s\": %s",
2075 out_file
, strerror(errno
));
2079 bits
= DEFAULT_BITS
;
2080 if (gen_candidates(out
, memory
, bits
, start
) != 0)
2081 fatal("modulus candidate generation failed");
2086 if (do_screen_candidates
) {
2088 FILE *out
= fopen(out_file
, "w");
2090 if (have_identity
&& strcmp(identity_file
, "-") != 0) {
2091 if ((in
= fopen(identity_file
, "r")) == NULL
) {
2092 fatal("Couldn't open modulus candidate "
2093 "file \"%s\": %s", identity_file
,
2100 fatal("Couldn't open moduli file \"%s\": %s",
2101 out_file
, strerror(errno
));
2103 if (prime_test(in
, out
, trials
, generator_wanted
) != 0)
2104 fatal("modulus screening failed");
2110 if (key_type_name
== NULL
)
2111 key_type_name
= "rsa";
2113 type
= key_type_from_name(key_type_name
);
2114 if (type
== KEY_UNSPEC
) {
2115 fprintf(stderr
, "unknown key type %s\n", key_type_name
);
2119 if (type
== KEY_DSA
)
2120 bits
= DEFAULT_BITS_DSA
;
2121 else if (type
== KEY_ECDSA
)
2122 bits
= DEFAULT_BITS_ECDSA
;
2124 bits
= DEFAULT_BITS
;
2126 maxbits
= (type
== KEY_DSA
) ?
2127 OPENSSL_DSA_MAX_MODULUS_BITS
: OPENSSL_RSA_MAX_MODULUS_BITS
;
2128 if (bits
> maxbits
) {
2129 fprintf(stderr
, "key bits exceeds maximum %d\n", maxbits
);
2132 if (type
== KEY_DSA
&& bits
!= 1024)
2133 fatal("DSA keys must be 1024 bits");
2134 else if (type
!= KEY_ECDSA
&& bits
< 768)
2135 fatal("Key must at least be 768 bits");
2136 else if (type
== KEY_ECDSA
&& key_ecdsa_bits_to_nid(bits
) == -1)
2137 fatal("Invalid ECDSA key length - valid lengths are "
2138 "256, 384 or 521 bits");
2140 printf("Generating public/private %s key pair.\n", key_type_name
);
2141 private = key_generate(type
, bits
);
2142 if (private == NULL
) {
2143 fprintf(stderr
, "key_generate failed\n");
2146 public = key_from_private(private);
2149 ask_filename(pw
, "Enter file in which to save the key");
2151 /* Create ~/.ssh directory if it doesn't already exist. */
2152 snprintf(dotsshdir
, sizeof dotsshdir
, "%s/%s",
2153 pw
->pw_dir
, _PATH_SSH_USER_DIR
);
2154 if (strstr(identity_file
, dotsshdir
) != NULL
) {
2155 if (stat(dotsshdir
, &st
) < 0) {
2156 if (errno
!= ENOENT
) {
2157 error("Could not stat %s: %s", dotsshdir
,
2159 } else if (mkdir(dotsshdir
, 0700) < 0) {
2160 error("Could not create directory '%s': %s",
2161 dotsshdir
, strerror(errno
));
2163 printf("Created directory '%s'.\n", dotsshdir
);
2166 /* If the file already exists, ask the user to confirm. */
2167 if (stat(identity_file
, &st
) >= 0) {
2169 printf("%s already exists.\n", identity_file
);
2170 printf("Overwrite (y/n)? ");
2172 if (fgets(yesno
, sizeof(yesno
), stdin
) == NULL
)
2174 if (yesno
[0] != 'y' && yesno
[0] != 'Y')
2177 /* Ask for a passphrase (twice). */
2178 if (identity_passphrase
)
2179 passphrase1
= xstrdup(identity_passphrase
);
2180 else if (identity_new_passphrase
)
2181 passphrase1
= xstrdup(identity_new_passphrase
);
2185 read_passphrase("Enter passphrase (empty for no "
2186 "passphrase): ", RP_ALLOW_STDIN
);
2187 passphrase2
= read_passphrase("Enter same passphrase again: ",
2189 if (strcmp(passphrase1
, passphrase2
) != 0) {
2191 * The passphrases do not match. Clear them and
2194 memset(passphrase1
, 0, strlen(passphrase1
));
2195 memset(passphrase2
, 0, strlen(passphrase2
));
2198 printf("Passphrases do not match. Try again.\n");
2199 goto passphrase_again
;
2201 /* Clear the other copy of the passphrase. */
2202 memset(passphrase2
, 0, strlen(passphrase2
));
2206 if (identity_comment
) {
2207 strlcpy(comment
, identity_comment
, sizeof(comment
));
2209 /* Create default comment field for the passphrase. */
2210 snprintf(comment
, sizeof comment
, "%s@%s", pw
->pw_name
, hostname
);
2213 /* Save the key with the given passphrase and comment. */
2214 if (!key_save_private(private, identity_file
, passphrase1
, comment
)) {
2215 printf("Saving the key failed: %s.\n", identity_file
);
2216 memset(passphrase1
, 0, strlen(passphrase1
));
2220 /* Clear the passphrase. */
2221 memset(passphrase1
, 0, strlen(passphrase1
));
2224 /* Clear the private key and the random number generator. */
2229 printf("Your identification has been saved in %s.\n", identity_file
);
2231 strlcat(identity_file
, ".pub", sizeof(identity_file
));
2232 fd
= open(identity_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0644);
2234 printf("Could not save your public key in %s\n", identity_file
);
2237 f
= fdopen(fd
, "w");
2239 printf("fdopen %s failed\n", identity_file
);
2242 if (!key_write(public, f
))
2243 fprintf(stderr
, "write key failed\n");
2244 fprintf(f
, " %s\n", comment
);
2248 char *fp
= key_fingerprint(public, SSH_FP_MD5
, SSH_FP_HEX
);
2249 char *ra
= key_fingerprint(public, SSH_FP_MD5
,
2251 printf("Your public key has been saved in %s.\n",
2253 printf("The key fingerprint is:\n");
2254 printf("%s %s\n", fp
, comment
);
2255 printf("The key's randomart image is:\n");