1 /* $OpenBSD: ssh-keygen.c,v 1.471 2023/09/04 10:29:58 job 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>
22 #include <openssl/evp.h>
23 #include <openssl/pem.h>
24 #include "openbsd-compat/openssl-compat.h"
50 #include "pathnames.h"
59 #include "ssh-pkcs11.h"
67 #include "sk-api.h" /* XXX for SSH_SK_USER_PRESENCE_REQD; remove */
70 #define DEFAULT_KEY_TYPE_NAME "ed25519"
73 * Default number of bits in the RSA, DSA and ECDSA keys. These value can be
74 * overridden on the command line.
76 * These values, with the exception of DSA, provide security equivalent to at
77 * least 128 bits of security according to NIST Special Publication 800-57:
78 * Recommendation for Key Management Part 1 rev 4 section 5.6.1.
79 * For DSA it (and FIPS-186-4 section 4.2) specifies that the only size for
80 * which a 160bit hash is acceptable is 1kbit, and since ssh-dss specifies only
81 * SHA1 we limit the DSA key size 1k bits.
83 #define DEFAULT_BITS 3072
84 #define DEFAULT_BITS_DSA 1024
85 #define DEFAULT_BITS_ECDSA 256
89 /* Flag indicating that we just want to see the key fingerprint */
90 static int print_fingerprint = 0;
91 static int print_bubblebabble = 0;
93 /* Hash algorithm to use for fingerprints. */
94 static int fingerprint_hash = SSH_FP_HASH_DEFAULT;
96 /* The identity file name, given on the command line or entered by the user. */
97 static char identity_file[PATH_MAX];
98 static int have_identity = 0;
100 /* This is set to the passphrase if given on the command line. */
101 static char *identity_passphrase = NULL;
103 /* This is set to the new passphrase if given on the command line. */
104 static char *identity_new_passphrase = NULL;
106 /* Key type when certifying */
107 static u_int cert_key_type = SSH2_CERT_TYPE_USER;
109 /* "key ID" of signed key */
110 static char *cert_key_id = NULL;
112 /* Comma-separated list of principal names for certifying keys */
113 static char *cert_principals = NULL;
115 /* Validity period for certificates */
116 static u_int64_t cert_valid_from = 0;
117 static u_int64_t cert_valid_to = ~0ULL;
119 /* Certificate options */
120 #define CERTOPT_X_FWD (1)
121 #define CERTOPT_AGENT_FWD (1<<1)
122 #define CERTOPT_PORT_FWD (1<<2)
123 #define CERTOPT_PTY (1<<3)
124 #define CERTOPT_USER_RC (1<<4)
125 #define CERTOPT_NO_REQUIRE_USER_PRESENCE (1<<5)
126 #define CERTOPT_REQUIRE_VERIFY (1<<6)
127 #define CERTOPT_DEFAULT (CERTOPT_X_FWD|CERTOPT_AGENT_FWD| \
128 CERTOPT_PORT_FWD|CERTOPT_PTY|CERTOPT_USER_RC)
129 static u_int32_t certflags_flags = CERTOPT_DEFAULT;
130 static char *certflags_command = NULL;
131 static char *certflags_src_addr = NULL;
133 /* Arbitrary extensions specified by user */
139 static struct cert_ext *cert_ext;
140 static size_t ncert_ext;
142 /* Conversion to/from various formats */
147 } convert_format = FMT_RFC4716;
149 static char *key_type_name = NULL;
151 /* Load key from this PKCS#11 provider */
152 static char *pkcs11provider = NULL;
154 /* FIDO/U2F provider to use */
155 static char *sk_provider = NULL;
157 /* Format for writing private keys */
158 static int private_key_format = SSHKEY_PRIVATE_OPENSSH;
160 /* Cipher for new-format private keys */
161 static char *openssh_format_cipher = NULL;
163 /* Number of KDF rounds to derive new format keys. */
164 static int rounds = 0;
167 extern char *__progname;
169 static char hostname[NI_MAXHOST];
173 int gen_candidates(FILE *, u_int32_t, u_int32_t, BIGNUM *);
174 int prime_test(FILE *, FILE *, u_int32_t, u_int32_t, char *, unsigned long,
179 type_bits_valid(int type, const char *name, u_int32_t *bitsp)
181 if (type == KEY_UNSPEC)
182 fatal("unknown key type %s", key_type_name);
189 *bitsp = DEFAULT_BITS_DSA;
193 (nid = sshkey_ecdsa_nid_from_name(name)) > 0)
194 *bitsp = sshkey_curve_nid_to_bits(nid);
196 *bitsp = DEFAULT_BITS_ECDSA;
199 *bitsp = DEFAULT_BITS;
208 fatal("Invalid DSA key length: must be 1024 bits");
211 if (*bitsp < SSH_RSA_MINIMUM_MODULUS_SIZE)
212 fatal("Invalid RSA key length: minimum is %d bits",
213 SSH_RSA_MINIMUM_MODULUS_SIZE);
214 else if (*bitsp > OPENSSL_RSA_MAX_MODULUS_BITS)
215 fatal("Invalid RSA key length: maximum is %d bits",
216 OPENSSL_RSA_MAX_MODULUS_BITS);
219 if (sshkey_ecdsa_bits_to_nid(*bitsp) == -1)
220 #ifdef OPENSSL_HAS_NISTP521
221 fatal("Invalid ECDSA key length: valid lengths are "
222 "256, 384 or 521 bits");
224 fatal("Invalid ECDSA key length: valid lengths are "
232 * Checks whether a file exists and, if so, asks the user whether they wish
234 * Returns nonzero if the file does not already exist or if the user agrees to
235 * overwrite, or zero otherwise.
238 confirm_overwrite(const char *filename)
243 if (stat(filename, &st) != 0)
245 printf("%s already exists.\n", filename);
246 printf("Overwrite (y/n)? ");
248 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
250 if (yesno[0] != 'y' && yesno[0] != 'Y')
256 ask_filename(struct passwd *pw, const char *prompt)
261 if (key_type_name == NULL)
262 name = _PATH_SSH_CLIENT_ID_ED25519;
264 switch (sshkey_type_from_name(key_type_name)) {
267 name = _PATH_SSH_CLIENT_ID_DSA;
269 #ifdef OPENSSL_HAS_ECC
272 name = _PATH_SSH_CLIENT_ID_ECDSA;
274 case KEY_ECDSA_SK_CERT:
276 name = _PATH_SSH_CLIENT_ID_ECDSA_SK;
281 name = _PATH_SSH_CLIENT_ID_RSA;
284 case KEY_ED25519_CERT:
285 name = _PATH_SSH_CLIENT_ID_ED25519;
288 case KEY_ED25519_SK_CERT:
289 name = _PATH_SSH_CLIENT_ID_ED25519_SK;
293 name = _PATH_SSH_CLIENT_ID_XMSS;
296 fatal("bad key type");
299 snprintf(identity_file, sizeof(identity_file),
300 "%s/%s", pw->pw_dir, name);
301 printf("%s (%s): ", prompt, identity_file);
303 if (fgets(buf, sizeof(buf), stdin) == NULL)
305 buf[strcspn(buf, "\n")] = '\0';
306 if (strcmp(buf, "") != 0)
307 strlcpy(identity_file, buf, sizeof(identity_file));
311 static struct sshkey *
312 load_identity(const char *filename, char **commentp)
318 if (commentp != NULL)
320 if ((r = sshkey_load_private(filename, "", &prv, commentp)) == 0)
322 if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
323 fatal_r(r, "Load key \"%s\"", filename);
324 if (identity_passphrase)
325 pass = xstrdup(identity_passphrase);
327 pass = read_passphrase("Enter passphrase: ", RP_ALLOW_STDIN);
328 r = sshkey_load_private(filename, pass, &prv, commentp);
329 freezero(pass, strlen(pass));
331 fatal_r(r, "Load key \"%s\"", filename);
335 #define SSH_COM_PUBLIC_BEGIN "---- BEGIN SSH2 PUBLIC KEY ----"
336 #define SSH_COM_PUBLIC_END "---- END SSH2 PUBLIC KEY ----"
337 #define SSH_COM_PRIVATE_BEGIN "---- BEGIN SSH2 ENCRYPTED PRIVATE KEY ----"
338 #define SSH_COM_PRIVATE_KEY_MAGIC 0x3f6ff9eb
342 do_convert_to_ssh2(struct passwd *pw, struct sshkey *k)
345 char comment[61], *b64;
348 if ((b = sshbuf_new()) == NULL)
349 fatal_f("sshbuf_new failed");
350 if ((r = sshkey_putb(k, b)) != 0)
351 fatal_fr(r, "put key");
352 if ((b64 = sshbuf_dtob64_string(b, 1)) == NULL)
353 fatal_f("sshbuf_dtob64_string failed");
355 /* Comment + surrounds must fit into 72 chars (RFC 4716 sec 3.3) */
356 snprintf(comment, sizeof(comment),
357 "%u-bit %s, converted by %s@%s from OpenSSH",
358 sshkey_size(k), sshkey_type(k),
359 pw->pw_name, hostname);
364 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_BEGIN);
365 fprintf(stdout, "Comment: \"%s\"\n%s", comment, b64);
366 fprintf(stdout, "%s\n", SSH_COM_PUBLIC_END);
372 do_convert_to_pkcs8(struct sshkey *k)
374 switch (sshkey_type_plain(k->type)) {
376 if (!PEM_write_RSA_PUBKEY(stdout, k->rsa))
377 fatal("PEM_write_RSA_PUBKEY failed");
380 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
381 fatal("PEM_write_DSA_PUBKEY failed");
383 #ifdef OPENSSL_HAS_ECC
385 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
386 fatal("PEM_write_EC_PUBKEY failed");
390 fatal_f("unsupported key type %s", sshkey_type(k));
396 do_convert_to_pem(struct sshkey *k)
398 switch (sshkey_type_plain(k->type)) {
400 if (!PEM_write_RSAPublicKey(stdout, k->rsa))
401 fatal("PEM_write_RSAPublicKey failed");
404 if (!PEM_write_DSA_PUBKEY(stdout, k->dsa))
405 fatal("PEM_write_DSA_PUBKEY failed");
407 #ifdef OPENSSL_HAS_ECC
409 if (!PEM_write_EC_PUBKEY(stdout, k->ecdsa))
410 fatal("PEM_write_EC_PUBKEY failed");
414 fatal_f("unsupported key type %s", sshkey_type(k));
420 do_convert_to(struct passwd *pw)
427 ask_filename(pw, "Enter file in which the key is");
428 if (stat(identity_file, &st) == -1)
429 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
430 if ((r = sshkey_load_public(identity_file, &k, NULL)) != 0)
431 k = load_identity(identity_file, NULL);
432 switch (convert_format) {
434 do_convert_to_ssh2(pw, k);
437 do_convert_to_pkcs8(k);
440 do_convert_to_pem(k);
443 fatal_f("unknown key format %d", convert_format);
449 * This is almost exactly the bignum1 encoding, but with 32 bit for length
453 buffer_get_bignum_bits(struct sshbuf *b, BIGNUM *value)
455 u_int bytes, bignum_bits;
458 if ((r = sshbuf_get_u32(b, &bignum_bits)) != 0)
459 fatal_fr(r, "parse");
460 bytes = (bignum_bits + 7) / 8;
461 if (sshbuf_len(b) < bytes)
462 fatal_f("input buffer too small: need %d have %zu",
463 bytes, sshbuf_len(b));
464 if (BN_bin2bn(sshbuf_ptr(b), bytes, value) == NULL)
465 fatal_f("BN_bin2bn failed");
466 if ((r = sshbuf_consume(b, bytes)) != 0)
467 fatal_fr(r, "consume");
470 static struct sshkey *
471 do_convert_private_ssh2(struct sshbuf *b)
473 struct sshkey *key = NULL;
475 const char *alg = NULL;
476 u_char e1, e2, e3, *sig = NULL, data[] = "abcde12345";
478 u_int magic, i1, i2, i3, i4;
481 BIGNUM *dsa_p = NULL, *dsa_q = NULL, *dsa_g = NULL;
482 BIGNUM *dsa_pub_key = NULL, *dsa_priv_key = NULL;
483 BIGNUM *rsa_n = NULL, *rsa_e = NULL, *rsa_d = NULL;
484 BIGNUM *rsa_p = NULL, *rsa_q = NULL, *rsa_iqmp = NULL;
486 if ((r = sshbuf_get_u32(b, &magic)) != 0)
487 fatal_fr(r, "parse magic");
489 if (magic != SSH_COM_PRIVATE_KEY_MAGIC) {
490 error("bad magic 0x%x != 0x%x", magic,
491 SSH_COM_PRIVATE_KEY_MAGIC);
494 if ((r = sshbuf_get_u32(b, &i1)) != 0 ||
495 (r = sshbuf_get_cstring(b, &type, NULL)) != 0 ||
496 (r = sshbuf_get_cstring(b, &cipher, NULL)) != 0 ||
497 (r = sshbuf_get_u32(b, &i2)) != 0 ||
498 (r = sshbuf_get_u32(b, &i3)) != 0 ||
499 (r = sshbuf_get_u32(b, &i4)) != 0)
500 fatal_fr(r, "parse");
501 debug("ignore (%d %d %d %d)", i1, i2, i3, i4);
502 if (strcmp(cipher, "none") != 0) {
503 error("unsupported cipher %s", cipher);
510 if (strstr(type, "dsa")) {
512 } else if (strstr(type, "rsa")) {
518 if ((key = sshkey_new(ktype)) == NULL)
519 fatal("sshkey_new failed");
524 if ((dsa_p = BN_new()) == NULL ||
525 (dsa_q = BN_new()) == NULL ||
526 (dsa_g = BN_new()) == NULL ||
527 (dsa_pub_key = BN_new()) == NULL ||
528 (dsa_priv_key = BN_new()) == NULL)
530 buffer_get_bignum_bits(b, dsa_p);
531 buffer_get_bignum_bits(b, dsa_g);
532 buffer_get_bignum_bits(b, dsa_q);
533 buffer_get_bignum_bits(b, dsa_pub_key);
534 buffer_get_bignum_bits(b, dsa_priv_key);
535 if (!DSA_set0_pqg(key->dsa, dsa_p, dsa_q, dsa_g))
536 fatal_f("DSA_set0_pqg failed");
537 dsa_p = dsa_q = dsa_g = NULL; /* transferred */
538 if (!DSA_set0_key(key->dsa, dsa_pub_key, dsa_priv_key))
539 fatal_f("DSA_set0_key failed");
540 dsa_pub_key = dsa_priv_key = NULL; /* transferred */
543 if ((r = sshbuf_get_u8(b, &e1)) != 0 ||
544 (e1 < 30 && (r = sshbuf_get_u8(b, &e2)) != 0) ||
545 (e1 < 30 && (r = sshbuf_get_u8(b, &e3)) != 0))
546 fatal_fr(r, "parse RSA");
557 if ((rsa_e = BN_new()) == NULL)
559 if (!BN_set_word(rsa_e, e)) {
560 BN_clear_free(rsa_e);
564 if ((rsa_n = BN_new()) == NULL ||
565 (rsa_d = BN_new()) == NULL ||
566 (rsa_p = BN_new()) == NULL ||
567 (rsa_q = BN_new()) == NULL ||
568 (rsa_iqmp = BN_new()) == NULL)
570 buffer_get_bignum_bits(b, rsa_d);
571 buffer_get_bignum_bits(b, rsa_n);
572 buffer_get_bignum_bits(b, rsa_iqmp);
573 buffer_get_bignum_bits(b, rsa_q);
574 buffer_get_bignum_bits(b, rsa_p);
575 if (!RSA_set0_key(key->rsa, rsa_n, rsa_e, rsa_d))
576 fatal_f("RSA_set0_key failed");
577 rsa_n = rsa_e = rsa_d = NULL; /* transferred */
578 if (!RSA_set0_factors(key->rsa, rsa_p, rsa_q))
579 fatal_f("RSA_set0_factors failed");
580 rsa_p = rsa_q = NULL; /* transferred */
581 if ((r = ssh_rsa_complete_crt_parameters(key, rsa_iqmp)) != 0)
582 fatal_fr(r, "generate RSA parameters");
583 BN_clear_free(rsa_iqmp);
584 alg = "rsa-sha2-256";
587 rlen = sshbuf_len(b);
589 error_f("remaining bytes in key blob %d", rlen);
592 if ((r = sshkey_sign(key, &sig, &slen, data, sizeof(data),
593 alg, NULL, NULL, 0)) != 0)
594 error_fr(r, "signing with converted key failed");
595 else if ((r = sshkey_verify(key, sig, slen, data, sizeof(data),
597 error_fr(r, "verification with converted key failed");
608 get_line(FILE *fp, char *line, size_t len)
614 while ((c = fgetc(fp)) != EOF) {
616 fatal("input line too long.");
620 if (c != EOF && c != '\n' && ungetc(c, fp) == EOF)
621 fatal("unget: %s", strerror(errno));
634 do_convert_from_ssh2(struct passwd *pw, struct sshkey **k, int *private)
636 int r, blen, escaped = 0;
643 if ((buf = sshbuf_new()) == NULL)
644 fatal("sshbuf_new failed");
645 if ((fp = fopen(identity_file, "r")) == NULL)
646 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
648 while ((blen = get_line(fp, line, sizeof(line))) != -1) {
649 if (blen > 0 && line[blen - 1] == '\\')
651 if (strncmp(line, "----", 4) == 0 ||
652 strstr(line, ": ") != NULL) {
653 if (strstr(line, SSH_COM_PRIVATE_BEGIN) != NULL)
655 if (strstr(line, " END ") != NULL) {
658 /* fprintf(stderr, "ignore: %s", line); */
663 /* fprintf(stderr, "escaped: %s", line); */
666 strlcat(encoded, line, sizeof(encoded));
668 len = strlen(encoded);
669 if (((len % 4) == 3) &&
670 (encoded[len-1] == '=') &&
671 (encoded[len-2] == '=') &&
672 (encoded[len-3] == '='))
673 encoded[len-3] = '\0';
674 if ((r = sshbuf_b64tod(buf, encoded)) != 0)
675 fatal_fr(r, "base64 decode");
677 if ((*k = do_convert_private_ssh2(buf)) == NULL)
678 fatal_f("private key conversion failed");
679 } else if ((r = sshkey_fromb(buf, k)) != 0)
680 fatal_fr(r, "parse key");
686 do_convert_from_pkcs8(struct sshkey **k, int *private)
691 if ((fp = fopen(identity_file, "r")) == NULL)
692 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
693 if ((pubkey = PEM_read_PUBKEY(fp, NULL, NULL, NULL)) == NULL) {
694 fatal_f("%s is not a recognised public key format",
698 switch (EVP_PKEY_base_id(pubkey)) {
700 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
701 fatal("sshkey_new failed");
702 (*k)->type = KEY_RSA;
703 (*k)->rsa = EVP_PKEY_get1_RSA(pubkey);
706 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
707 fatal("sshkey_new failed");
708 (*k)->type = KEY_DSA;
709 (*k)->dsa = EVP_PKEY_get1_DSA(pubkey);
711 #ifdef OPENSSL_HAS_ECC
713 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
714 fatal("sshkey_new failed");
715 (*k)->type = KEY_ECDSA;
716 (*k)->ecdsa = EVP_PKEY_get1_EC_KEY(pubkey);
717 (*k)->ecdsa_nid = sshkey_ecdsa_key_to_nid((*k)->ecdsa);
721 fatal_f("unsupported pubkey type %d",
722 EVP_PKEY_base_id(pubkey));
724 EVP_PKEY_free(pubkey);
729 do_convert_from_pem(struct sshkey **k, int *private)
734 if ((fp = fopen(identity_file, "r")) == NULL)
735 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
736 if ((rsa = PEM_read_RSAPublicKey(fp, NULL, NULL, NULL)) != NULL) {
737 if ((*k = sshkey_new(KEY_UNSPEC)) == NULL)
738 fatal("sshkey_new failed");
739 (*k)->type = KEY_RSA;
744 fatal_f("unrecognised raw private key format");
748 do_convert_from(struct passwd *pw)
750 struct sshkey *k = NULL;
751 int r, private = 0, ok = 0;
755 ask_filename(pw, "Enter file in which the key is");
756 if (stat(identity_file, &st) == -1)
757 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
759 switch (convert_format) {
761 do_convert_from_ssh2(pw, &k, &private);
764 do_convert_from_pkcs8(&k, &private);
767 do_convert_from_pem(&k, &private);
770 fatal_f("unknown key format %d", convert_format);
774 if ((r = sshkey_write(k, stdout)) == 0)
777 fprintf(stdout, "\n");
781 ok = PEM_write_DSAPrivateKey(stdout, k->dsa, NULL,
782 NULL, 0, NULL, NULL);
784 #ifdef OPENSSL_HAS_ECC
786 ok = PEM_write_ECPrivateKey(stdout, k->ecdsa, NULL,
787 NULL, 0, NULL, NULL);
791 ok = PEM_write_RSAPrivateKey(stdout, k->rsa, NULL,
792 NULL, 0, NULL, NULL);
795 fatal_f("unsupported key type %s", sshkey_type(k));
800 fatal("key write failed");
807 do_print_public(struct passwd *pw)
812 char *comment = NULL;
815 ask_filename(pw, "Enter file in which the key is");
816 if (stat(identity_file, &st) == -1)
817 fatal("%s: %s", identity_file, strerror(errno));
818 prv = load_identity(identity_file, &comment);
819 if ((r = sshkey_write(prv, stdout)) != 0)
820 fatal_fr(r, "write key");
821 if (comment != NULL && *comment != '\0')
822 fprintf(stdout, " %s", comment);
823 fprintf(stdout, "\n");
824 if (sshkey_is_sk(prv)) {
825 debug("sk_application: \"%s\", sk_flags 0x%02x",
826 prv->sk_application, prv->sk_flags);
834 do_download(struct passwd *pw)
837 struct sshkey **keys = NULL;
839 enum sshkey_fp_rep rep;
841 char *fp, *ra, **comments = NULL;
843 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
844 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
847 nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys, &comments);
849 fatal("cannot read public key from pkcs11");
850 for (i = 0; i < nkeys; i++) {
851 if (print_fingerprint) {
852 fp = sshkey_fingerprint(keys[i], fptype, rep);
853 ra = sshkey_fingerprint(keys[i], fingerprint_hash,
855 if (fp == NULL || ra == NULL)
856 fatal_f("sshkey_fingerprint fail");
857 printf("%u %s %s (PKCS11 key)\n", sshkey_size(keys[i]),
858 fp, sshkey_type(keys[i]));
859 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
864 (void) sshkey_write(keys[i], stdout); /* XXX check */
865 fprintf(stdout, "%s%s\n",
866 *(comments[i]) == '\0' ? "" : " ", comments[i]);
869 sshkey_free(keys[i]);
876 fatal("no pkcs11 support");
877 #endif /* ENABLE_PKCS11 */
880 static struct sshkey *
881 try_read_key(char **cpp)
886 if ((ret = sshkey_new(KEY_UNSPEC)) == NULL)
887 fatal("sshkey_new failed");
888 if ((r = sshkey_read(ret, cpp)) == 0)
896 fingerprint_one_key(const struct sshkey *public, const char *comment)
898 char *fp = NULL, *ra = NULL;
899 enum sshkey_fp_rep rep;
902 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
903 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
904 fp = sshkey_fingerprint(public, fptype, rep);
905 ra = sshkey_fingerprint(public, fingerprint_hash, SSH_FP_RANDOMART);
906 if (fp == NULL || ra == NULL)
907 fatal_f("sshkey_fingerprint failed");
908 mprintf("%u %s %s (%s)\n", sshkey_size(public), fp,
909 comment ? comment : "no comment", sshkey_type(public));
910 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
917 fingerprint_private(const char *path)
920 char *comment = NULL;
921 struct sshkey *privkey = NULL, *pubkey = NULL;
924 if (stat(identity_file, &st) == -1)
925 fatal("%s: %s", path, strerror(errno));
926 if ((r = sshkey_load_public(path, &pubkey, &comment)) != 0)
927 debug_r(r, "load public \"%s\"", path);
928 if (pubkey == NULL || comment == NULL || *comment == '\0') {
930 if ((r = sshkey_load_private(path, NULL,
931 &privkey, &comment)) != 0)
932 debug_r(r, "load private \"%s\"", path);
934 if (pubkey == NULL && privkey == NULL)
935 fatal("%s is not a key file.", path);
937 fingerprint_one_key(pubkey == NULL ? privkey : pubkey, comment);
939 sshkey_free(privkey);
944 do_fingerprint(struct passwd *pw)
947 struct sshkey *public = NULL;
948 char *comment = NULL, *cp, *ep, *line = NULL;
955 ask_filename(pw, "Enter file in which the key is");
956 path = identity_file;
958 if (strcmp(identity_file, "-") == 0) {
961 } else if ((f = fopen(path, "r")) == NULL)
962 fatal("%s: %s: %s", __progname, path, strerror(errno));
964 while (getline(&line, &linesize, f) != -1) {
967 cp[strcspn(cp, "\n")] = '\0';
968 /* Trim leading space and comments */
969 cp = line + strspn(line, " \t");
970 if (*cp == '#' || *cp == '\0')
974 * Input may be plain keys, private keys, authorized_keys
979 * Try private keys first. Assume a key is private if
980 * "SSH PRIVATE KEY" appears on the first line and we're
981 * not reading from stdin (XXX support private keys on stdin).
983 if (lnum == 1 && strcmp(identity_file, "-") != 0 &&
984 strstr(cp, "PRIVATE KEY") != NULL) {
987 fingerprint_private(path);
992 * If it's not a private key, then this must be prepared to
993 * accept a public key prefixed with a hostname or options.
994 * Try a bare key first, otherwise skip the leading stuff.
997 if ((public = try_read_key(&cp)) == NULL) {
998 i = strtol(cp, &ep, 10);
999 if (i == 0 || ep == NULL ||
1000 (*ep != ' ' && *ep != '\t')) {
1004 for (; *cp && (quoted || (*cp != ' ' &&
1005 *cp != '\t')); cp++) {
1006 if (*cp == '\\' && cp[1] == '"')
1007 cp++; /* Skip both */
1008 else if (*cp == '"')
1016 /* Retry after parsing leading hostname/key options */
1017 if (public == NULL && (public = try_read_key(&cp)) == NULL) {
1018 debug("%s:%lu: not a public key", path, lnum);
1022 /* Find trailing comment, if any */
1023 for (; *cp == ' ' || *cp == '\t'; cp++)
1025 if (*cp != '\0' && *cp != '#')
1028 fingerprint_one_key(public, comment);
1029 sshkey_free(public);
1030 invalid = 0; /* One good key in the file is sufficient */
1036 fatal("%s is not a public key file.", path);
1041 do_gen_all_hostkeys(struct passwd *pw)
1045 char *key_type_display;
1049 { "rsa", "RSA" ,_PATH_HOST_RSA_KEY_FILE },
1050 #ifdef OPENSSL_HAS_ECC
1051 { "ecdsa", "ECDSA",_PATH_HOST_ECDSA_KEY_FILE },
1052 #endif /* OPENSSL_HAS_ECC */
1053 #endif /* WITH_OPENSSL */
1054 { "ed25519", "ED25519",_PATH_HOST_ED25519_KEY_FILE },
1056 { "xmss", "XMSS",_PATH_HOST_XMSS_KEY_FILE },
1057 #endif /* WITH_XMSS */
1058 { NULL, NULL, NULL }
1064 struct sshkey *private, *public;
1065 char comment[1024], *prv_tmp, *pub_tmp, *prv_file, *pub_file;
1068 for (i = 0; key_types[i].key_type; i++) {
1069 public = private = NULL;
1070 prv_tmp = pub_tmp = prv_file = pub_file = NULL;
1072 xasprintf(&prv_file, "%s%s",
1073 identity_file, key_types[i].path);
1075 /* Check whether private key exists and is not zero-length */
1076 if (stat(prv_file, &st) == 0) {
1077 if (st.st_size != 0)
1079 } else if (errno != ENOENT) {
1080 error("Could not stat %s: %s", key_types[i].path,
1086 * Private key doesn't exist or is invalid; proceed with
1089 xasprintf(&prv_tmp, "%s%s.XXXXXXXXXX",
1090 identity_file, key_types[i].path);
1091 xasprintf(&pub_tmp, "%s%s.pub.XXXXXXXXXX",
1092 identity_file, key_types[i].path);
1093 xasprintf(&pub_file, "%s%s.pub",
1094 identity_file, key_types[i].path);
1098 printf("%s: generating new host keys: ", __progname);
1100 printf("%s ", key_types[i].key_type_display);
1102 type = sshkey_type_from_name(key_types[i].key_type);
1103 if ((fd = mkstemp(prv_tmp)) == -1) {
1104 error("Could not save your private key in %s: %s",
1105 prv_tmp, strerror(errno));
1108 (void)close(fd); /* just using mkstemp() to reserve a name */
1110 type_bits_valid(type, NULL, &bits);
1111 if ((r = sshkey_generate(type, bits, &private)) != 0) {
1112 error_r(r, "sshkey_generate failed");
1115 if ((r = sshkey_from_private(private, &public)) != 0)
1116 fatal_fr(r, "sshkey_from_private");
1117 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name,
1119 if ((r = sshkey_save_private(private, prv_tmp, "",
1120 comment, private_key_format, openssh_format_cipher,
1122 error_r(r, "Saving key \"%s\" failed", prv_tmp);
1125 if ((fd = mkstemp(pub_tmp)) == -1) {
1126 error("Could not save your public key in %s: %s",
1127 pub_tmp, strerror(errno));
1130 (void)fchmod(fd, 0644);
1132 if ((r = sshkey_save_public(public, pub_tmp, comment)) != 0) {
1133 error_r(r, "Unable to save public key to %s",
1138 /* Rename temporary files to their permanent locations. */
1139 if (rename(pub_tmp, pub_file) != 0) {
1140 error("Unable to move %s into position: %s",
1141 pub_file, strerror(errno));
1144 if (rename(prv_tmp, prv_file) != 0) {
1145 error("Unable to move %s into position: %s",
1146 key_types[i].path, strerror(errno));
1152 sshkey_free(private);
1153 sshkey_free(public);
1163 struct known_hosts_ctx {
1164 const char *host; /* Hostname searched for in find/delete case */
1165 FILE *out; /* Output file, stdout for find_hosts case */
1166 int has_unhashed; /* When hashing, original had unhashed hosts */
1167 int found_key; /* For find/delete, host was found */
1168 int invalid; /* File contained invalid items; don't delete */
1169 int hash_hosts; /* Hash hostnames as we go */
1170 int find_host; /* Search for specific hostname */
1171 int delete_host; /* Delete host from known_hosts */
1175 known_hosts_hash(struct hostkey_foreach_line *l, void *_ctx)
1177 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1178 char *hashed, *cp, *hosts, *ohosts;
1179 int has_wild = l->hosts && strcspn(l->hosts, "*?!") != strlen(l->hosts);
1180 int was_hashed = l->hosts && l->hosts[0] == HASH_DELIM;
1182 switch (l->status) {
1184 case HKF_STATUS_MATCHED:
1186 * Don't hash hosts already hashed, with wildcard
1187 * characters or a CA/revocation marker.
1189 if (was_hashed || has_wild || l->marker != MRK_NONE) {
1190 fprintf(ctx->out, "%s\n", l->line);
1191 if (has_wild && !ctx->find_host) {
1192 logit("%s:%lu: ignoring host name "
1193 "with wildcard: %.64s", l->path,
1194 l->linenum, l->hosts);
1199 * Split any comma-separated hostnames from the host list,
1200 * hash and store separately.
1202 ohosts = hosts = xstrdup(l->hosts);
1203 while ((cp = strsep(&hosts, ",")) != NULL && *cp != '\0') {
1205 if ((hashed = host_hash(cp, NULL, 0)) == NULL)
1206 fatal("hash_host failed");
1207 fprintf(ctx->out, "%s %s\n", hashed, l->rawkey);
1209 ctx->has_unhashed = 1;
1213 case HKF_STATUS_INVALID:
1214 /* Retain invalid lines, but mark file as invalid. */
1216 logit("%s:%lu: invalid line", l->path, l->linenum);
1219 fprintf(ctx->out, "%s\n", l->line);
1227 known_hosts_find_delete(struct hostkey_foreach_line *l, void *_ctx)
1229 struct known_hosts_ctx *ctx = (struct known_hosts_ctx *)_ctx;
1230 enum sshkey_fp_rep rep;
1232 char *fp = NULL, *ra = NULL;
1234 fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
1235 rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;
1237 if (l->status == HKF_STATUS_MATCHED) {
1238 if (ctx->delete_host) {
1239 if (l->marker != MRK_NONE) {
1240 /* Don't remove CA and revocation lines */
1241 fprintf(ctx->out, "%s\n", l->line);
1244 * Hostname matches and has no CA/revoke
1245 * marker, delete it by *not* writing the
1250 printf("# Host %s found: line %lu\n",
1251 ctx->host, l->linenum);
1254 } else if (ctx->find_host) {
1257 printf("# Host %s found: line %lu %s\n",
1259 l->linenum, l->marker == MRK_CA ? "CA" :
1260 (l->marker == MRK_REVOKE ? "REVOKED" : ""));
1262 if (ctx->hash_hosts)
1263 known_hosts_hash(l, ctx);
1264 else if (print_fingerprint) {
1265 fp = sshkey_fingerprint(l->key, fptype, rep);
1266 ra = sshkey_fingerprint(l->key,
1267 fingerprint_hash, SSH_FP_RANDOMART);
1268 if (fp == NULL || ra == NULL)
1269 fatal_f("sshkey_fingerprint failed");
1270 mprintf("%s %s %s%s%s\n", ctx->host,
1271 sshkey_type(l->key), fp,
1272 l->comment[0] ? " " : "",
1274 if (log_level_get() >= SYSLOG_LEVEL_VERBOSE)
1279 fprintf(ctx->out, "%s\n", l->line);
1282 } else if (ctx->delete_host) {
1283 /* Retain non-matching hosts when deleting */
1284 if (l->status == HKF_STATUS_INVALID) {
1286 logit("%s:%lu: invalid line", l->path, l->linenum);
1288 fprintf(ctx->out, "%s\n", l->line);
1294 do_known_hosts(struct passwd *pw, const char *name, int find_host,
1295 int delete_host, int hash_hosts)
1297 char *cp, tmp[PATH_MAX], old[PATH_MAX];
1298 int r, fd, oerrno, inplace = 0;
1299 struct known_hosts_ctx ctx;
1300 u_int foreach_options;
1303 if (!have_identity) {
1304 cp = tilde_expand_filename(_PATH_SSH_USER_HOSTFILE, pw->pw_uid);
1305 if (strlcpy(identity_file, cp, sizeof(identity_file)) >=
1306 sizeof(identity_file))
1307 fatal("Specified known hosts path too long");
1311 if (stat(identity_file, &sb) != 0)
1312 fatal("Cannot stat %s: %s", identity_file, strerror(errno));
1314 memset(&ctx, 0, sizeof(ctx));
1317 ctx.hash_hosts = hash_hosts;
1318 ctx.find_host = find_host;
1319 ctx.delete_host = delete_host;
1322 * Find hosts goes to stdout, hash and deletions happen in-place
1323 * A corner case is ssh-keygen -HF foo, which should go to stdout
1325 if (!find_host && (hash_hosts || delete_host)) {
1326 if (strlcpy(tmp, identity_file, sizeof(tmp)) >= sizeof(tmp) ||
1327 strlcat(tmp, ".XXXXXXXXXX", sizeof(tmp)) >= sizeof(tmp) ||
1328 strlcpy(old, identity_file, sizeof(old)) >= sizeof(old) ||
1329 strlcat(old, ".old", sizeof(old)) >= sizeof(old))
1330 fatal("known_hosts path too long");
1332 if ((fd = mkstemp(tmp)) == -1)
1333 fatal("mkstemp: %s", strerror(errno));
1334 if ((ctx.out = fdopen(fd, "w")) == NULL) {
1337 fatal("fdopen: %s", strerror(oerrno));
1339 (void)fchmod(fd, sb.st_mode & 0644);
1342 /* XXX support identity_file == "-" for stdin */
1343 foreach_options = find_host ? HKF_WANT_MATCH : 0;
1344 foreach_options |= print_fingerprint ? HKF_WANT_PARSE_KEY : 0;
1345 if ((r = hostkeys_foreach(identity_file, (find_host || !hash_hosts) ?
1346 known_hosts_find_delete : known_hosts_hash, &ctx, name, NULL,
1347 foreach_options, 0)) != 0) {
1350 fatal_fr(r, "hostkeys_foreach");
1357 error("%s is not a valid known_hosts file.", identity_file);
1359 error("Not replacing existing known_hosts "
1360 "file because of errors");
1364 } else if (delete_host && !ctx.found_key) {
1365 logit("Host %s not found in %s", name, identity_file);
1368 } else if (inplace) {
1369 /* Backup existing file */
1370 if (unlink(old) == -1 && errno != ENOENT)
1371 fatal("unlink %.100s: %s", old, strerror(errno));
1372 if (link(identity_file, old) == -1)
1373 fatal("link %.100s to %.100s: %s", identity_file, old,
1375 /* Move new one into place */
1376 if (rename(tmp, identity_file) == -1) {
1377 error("rename\"%s\" to \"%s\": %s", tmp, identity_file,
1384 printf("%s updated.\n", identity_file);
1385 printf("Original contents retained as %s\n", old);
1386 if (ctx.has_unhashed) {
1387 logit("WARNING: %s contains unhashed entries", old);
1388 logit("Delete this file to ensure privacy "
1393 exit (find_host && !ctx.found_key);
1397 * Perform changing a passphrase. The argument is the passwd structure
1398 * for the current user.
1401 do_change_passphrase(struct passwd *pw)
1404 char *old_passphrase, *passphrase1, *passphrase2;
1406 struct sshkey *private;
1410 ask_filename(pw, "Enter file in which the key is");
1411 if (stat(identity_file, &st) == -1)
1412 fatal("%s: %s", identity_file, strerror(errno));
1413 /* Try to load the file with empty passphrase. */
1414 r = sshkey_load_private(identity_file, "", &private, &comment);
1415 if (r == SSH_ERR_KEY_WRONG_PASSPHRASE) {
1416 if (identity_passphrase)
1417 old_passphrase = xstrdup(identity_passphrase);
1420 read_passphrase("Enter old passphrase: ",
1422 r = sshkey_load_private(identity_file, old_passphrase,
1423 &private, &comment);
1424 freezero(old_passphrase, strlen(old_passphrase));
1427 } else if (r != 0) {
1429 fatal_r(r, "Failed to load key %s", identity_file);
1432 mprintf("Key has comment '%s'\n", comment);
1434 /* Ask the new passphrase (twice). */
1435 if (identity_new_passphrase) {
1436 passphrase1 = xstrdup(identity_new_passphrase);
1440 read_passphrase("Enter new passphrase (empty for no "
1441 "passphrase): ", RP_ALLOW_STDIN);
1442 passphrase2 = read_passphrase("Enter same passphrase again: ",
1445 /* Verify that they are the same. */
1446 if (strcmp(passphrase1, passphrase2) != 0) {
1447 explicit_bzero(passphrase1, strlen(passphrase1));
1448 explicit_bzero(passphrase2, strlen(passphrase2));
1451 printf("Pass phrases do not match. Try again.\n");
1454 /* Destroy the other copy. */
1455 freezero(passphrase2, strlen(passphrase2));
1458 /* Save the file using the new passphrase. */
1459 if ((r = sshkey_save_private(private, identity_file, passphrase1,
1460 comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
1461 error_r(r, "Saving key \"%s\" failed", identity_file);
1462 freezero(passphrase1, strlen(passphrase1));
1463 sshkey_free(private);
1467 /* Destroy the passphrase and the copy of the key in memory. */
1468 freezero(passphrase1, strlen(passphrase1));
1469 sshkey_free(private); /* Destroys contents */
1472 printf("Your identification has been saved with the new passphrase.\n");
1477 * Print the SSHFP RR.
1480 do_print_resource_record(struct passwd *pw, char *fname, char *hname,
1481 int print_generic, char * const *opts, size_t nopts)
1483 struct sshkey *public;
1484 char *comment = NULL;
1489 for (i = 0; i < nopts; i++) {
1490 if (strncasecmp(opts[i], "hashalg=", 8) == 0) {
1491 if ((hash = ssh_digest_alg_by_name(opts[i] + 8)) == -1)
1492 fatal("Unsupported hash algorithm");
1494 error("Invalid option \"%s\"", opts[i]);
1495 return SSH_ERR_INVALID_ARGUMENT;
1499 fatal_f("no filename");
1500 if (stat(fname, &st) == -1) {
1501 if (errno == ENOENT)
1503 fatal("%s: %s", fname, strerror(errno));
1505 if ((r = sshkey_load_public(fname, &public, &comment)) != 0)
1506 fatal_r(r, "Failed to read v2 public key from \"%s\"", fname);
1507 export_dns_rr(hname, public, stdout, print_generic, hash);
1508 sshkey_free(public);
1514 * Change the comment of a private key file.
1517 do_change_comment(struct passwd *pw, const char *identity_comment)
1519 char new_comment[1024], *comment, *passphrase;
1520 struct sshkey *private;
1521 struct sshkey *public;
1526 ask_filename(pw, "Enter file in which the key is");
1527 if (stat(identity_file, &st) == -1)
1528 fatal("%s: %s", identity_file, strerror(errno));
1529 if ((r = sshkey_load_private(identity_file, "",
1530 &private, &comment)) == 0)
1531 passphrase = xstrdup("");
1532 else if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
1533 fatal_r(r, "Cannot load private key \"%s\"", identity_file);
1535 if (identity_passphrase)
1536 passphrase = xstrdup(identity_passphrase);
1537 else if (identity_new_passphrase)
1538 passphrase = xstrdup(identity_new_passphrase);
1540 passphrase = read_passphrase("Enter passphrase: ",
1542 /* Try to load using the passphrase. */
1543 if ((r = sshkey_load_private(identity_file, passphrase,
1544 &private, &comment)) != 0) {
1545 freezero(passphrase, strlen(passphrase));
1546 fatal_r(r, "Cannot load private key \"%s\"",
1551 if (private->type != KEY_ED25519 && private->type != KEY_XMSS &&
1552 private_key_format != SSHKEY_PRIVATE_OPENSSH) {
1553 error("Comments are only supported for keys stored in "
1554 "the new format (-o).");
1555 explicit_bzero(passphrase, strlen(passphrase));
1556 sshkey_free(private);
1560 printf("Old comment: %s\n", comment);
1562 printf("No existing comment\n");
1564 if (identity_comment) {
1565 strlcpy(new_comment, identity_comment, sizeof(new_comment));
1567 printf("New comment: ");
1569 if (!fgets(new_comment, sizeof(new_comment), stdin)) {
1570 explicit_bzero(passphrase, strlen(passphrase));
1571 sshkey_free(private);
1574 new_comment[strcspn(new_comment, "\n")] = '\0';
1576 if (comment != NULL && strcmp(comment, new_comment) == 0) {
1577 printf("No change to comment\n");
1579 sshkey_free(private);
1584 /* Save the file using the new passphrase. */
1585 if ((r = sshkey_save_private(private, identity_file, passphrase,
1586 new_comment, private_key_format, openssh_format_cipher,
1588 error_r(r, "Saving key \"%s\" failed", identity_file);
1589 freezero(passphrase, strlen(passphrase));
1590 sshkey_free(private);
1594 freezero(passphrase, strlen(passphrase));
1595 if ((r = sshkey_from_private(private, &public)) != 0)
1596 fatal_fr(r, "sshkey_from_private");
1597 sshkey_free(private);
1599 strlcat(identity_file, ".pub", sizeof(identity_file));
1600 if ((r = sshkey_save_public(public, identity_file, new_comment)) != 0)
1601 fatal_r(r, "Unable to save public key to %s", identity_file);
1602 sshkey_free(public);
1605 if (strlen(new_comment) > 0)
1606 printf("Comment '%s' applied\n", new_comment);
1608 printf("Comment removed\n");
1614 cert_ext_add(const char *key, const char *value, int iscrit)
1616 cert_ext = xreallocarray(cert_ext, ncert_ext + 1, sizeof(*cert_ext));
1617 cert_ext[ncert_ext].key = xstrdup(key);
1618 cert_ext[ncert_ext].val = value == NULL ? NULL : xstrdup(value);
1619 cert_ext[ncert_ext].crit = iscrit;
1623 /* qsort(3) comparison function for certificate extensions */
1625 cert_ext_cmp(const void *_a, const void *_b)
1627 const struct cert_ext *a = (const struct cert_ext *)_a;
1628 const struct cert_ext *b = (const struct cert_ext *)_b;
1631 if (a->crit != b->crit)
1632 return (a->crit < b->crit) ? -1 : 1;
1633 if ((r = strcmp(a->key, b->key)) != 0)
1635 if ((a->val == NULL) != (b->val == NULL))
1636 return (a->val == NULL) ? -1 : 1;
1637 if (a->val != NULL && (r = strcmp(a->val, b->val)) != 0)
1642 #define OPTIONS_CRITICAL 1
1643 #define OPTIONS_EXTENSIONS 2
1645 prepare_options_buf(struct sshbuf *c, int which)
1650 const struct cert_ext *ext;
1652 if ((b = sshbuf_new()) == NULL)
1653 fatal_f("sshbuf_new failed");
1655 for (i = 0; i < ncert_ext; i++) {
1657 if ((ext->crit && (which & OPTIONS_EXTENSIONS)) ||
1658 (!ext->crit && (which & OPTIONS_CRITICAL)))
1660 if (ext->val == NULL) {
1662 debug3_f("%s", ext->key);
1663 if ((r = sshbuf_put_cstring(c, ext->key)) != 0 ||
1664 (r = sshbuf_put_string(c, NULL, 0)) != 0)
1665 fatal_fr(r, "prepare flag");
1667 /* key/value option */
1668 debug3_f("%s=%s", ext->key, ext->val);
1670 if ((r = sshbuf_put_cstring(c, ext->key)) != 0 ||
1671 (r = sshbuf_put_cstring(b, ext->val)) != 0 ||
1672 (r = sshbuf_put_stringb(c, b)) != 0)
1673 fatal_fr(r, "prepare k/v");
1680 finalise_cert_exts(void)
1682 /* critical options */
1683 if (certflags_command != NULL)
1684 cert_ext_add("force-command", certflags_command, 1);
1685 if (certflags_src_addr != NULL)
1686 cert_ext_add("source-address", certflags_src_addr, 1);
1687 if ((certflags_flags & CERTOPT_REQUIRE_VERIFY) != 0)
1688 cert_ext_add("verify-required", NULL, 1);
1690 if ((certflags_flags & CERTOPT_X_FWD) != 0)
1691 cert_ext_add("permit-X11-forwarding", NULL, 0);
1692 if ((certflags_flags & CERTOPT_AGENT_FWD) != 0)
1693 cert_ext_add("permit-agent-forwarding", NULL, 0);
1694 if ((certflags_flags & CERTOPT_PORT_FWD) != 0)
1695 cert_ext_add("permit-port-forwarding", NULL, 0);
1696 if ((certflags_flags & CERTOPT_PTY) != 0)
1697 cert_ext_add("permit-pty", NULL, 0);
1698 if ((certflags_flags & CERTOPT_USER_RC) != 0)
1699 cert_ext_add("permit-user-rc", NULL, 0);
1700 if ((certflags_flags & CERTOPT_NO_REQUIRE_USER_PRESENCE) != 0)
1701 cert_ext_add("no-touch-required", NULL, 0);
1702 /* order lexically by key */
1704 qsort(cert_ext, ncert_ext, sizeof(*cert_ext), cert_ext_cmp);
1707 static struct sshkey *
1708 load_pkcs11_key(char *path)
1710 #ifdef ENABLE_PKCS11
1711 struct sshkey **keys = NULL, *public, *private = NULL;
1714 if ((r = sshkey_load_public(path, &public, NULL)) != 0)
1715 fatal_r(r, "Couldn't load CA public key \"%s\"", path);
1717 nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase,
1719 debug3_f("%d keys", nkeys);
1721 fatal("cannot read public key from pkcs11");
1722 for (i = 0; i < nkeys; i++) {
1723 if (sshkey_equal_public(public, keys[i])) {
1727 sshkey_free(keys[i]);
1730 sshkey_free(public);
1733 fatal("no pkcs11 support");
1734 #endif /* ENABLE_PKCS11 */
1737 /* Signer for sshkey_certify_custom that uses the agent */
1739 agent_signer(struct sshkey *key, u_char **sigp, size_t *lenp,
1740 const u_char *data, size_t datalen,
1741 const char *alg, const char *provider, const char *pin,
1742 u_int compat, void *ctx)
1744 int *agent_fdp = (int *)ctx;
1746 return ssh_agent_sign(*agent_fdp, key, sigp, lenp,
1747 data, datalen, alg, compat);
1751 do_ca_sign(struct passwd *pw, const char *ca_key_path, int prefer_agent,
1752 unsigned long long cert_serial, int cert_serial_autoinc,
1753 int argc, char **argv)
1755 int r, i, found, agent_fd = -1;
1757 struct sshkey *ca, *public;
1758 char valid[64], *otmp, *tmp, *cp, *out, *comment;
1759 char *ca_fp = NULL, **plist = NULL, *pin = NULL;
1760 struct ssh_identitylist *agent_ids;
1762 struct notifier_ctx *notifier = NULL;
1764 #ifdef ENABLE_PKCS11
1767 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
1768 if (pkcs11provider != NULL) {
1769 /* If a PKCS#11 token was specified then try to use it */
1770 if ((ca = load_pkcs11_key(tmp)) == NULL)
1771 fatal("No PKCS#11 key matching %s found", ca_key_path);
1772 } else if (prefer_agent) {
1774 * Agent signature requested. Try to use agent after making
1775 * sure the public key specified is actually present in the
1778 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
1779 fatal_r(r, "Cannot load CA public key %s", tmp);
1780 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0)
1781 fatal_r(r, "Cannot use public key for CA signature");
1782 if ((r = ssh_fetch_identitylist(agent_fd, &agent_ids)) != 0)
1783 fatal_r(r, "Retrieve agent key list");
1785 for (j = 0; j < agent_ids->nkeys; j++) {
1786 if (sshkey_equal(ca, agent_ids->keys[j])) {
1792 fatal("CA key %s not found in agent", tmp);
1793 ssh_free_identitylist(agent_ids);
1794 ca->flags |= SSHKEY_FLAG_EXT;
1796 /* CA key is assumed to be a private key on the filesystem */
1797 ca = load_identity(tmp, NULL);
1798 if (sshkey_is_sk(ca) &&
1799 (ca->sk_flags & SSH_SK_USER_VERIFICATION_REQD)) {
1800 if ((pin = read_passphrase("Enter PIN for CA key: ",
1801 RP_ALLOW_STDIN)) == NULL)
1802 fatal_f("couldn't read PIN");
1807 if (key_type_name != NULL) {
1808 if (sshkey_type_from_name(key_type_name) != ca->type) {
1809 fatal("CA key type %s doesn't match specified %s",
1810 sshkey_ssh_name(ca), key_type_name);
1812 } else if (ca->type == KEY_RSA) {
1813 /* Default to a good signature algorithm */
1814 key_type_name = "rsa-sha2-512";
1816 ca_fp = sshkey_fingerprint(ca, fingerprint_hash, SSH_FP_DEFAULT);
1818 finalise_cert_exts();
1819 for (i = 0; i < argc; i++) {
1820 /* Split list of principals */
1822 if (cert_principals != NULL) {
1823 otmp = tmp = xstrdup(cert_principals);
1825 for (; (cp = strsep(&tmp, ",")) != NULL; n++) {
1826 plist = xreallocarray(plist, n + 1, sizeof(*plist));
1827 if (*(plist[n] = xstrdup(cp)) == '\0')
1828 fatal("Empty principal name");
1832 if (n > SSHKEY_CERT_MAX_PRINCIPALS)
1833 fatal("Too many certificate principals specified");
1835 tmp = tilde_expand_filename(argv[i], pw->pw_uid);
1836 if ((r = sshkey_load_public(tmp, &public, &comment)) != 0)
1837 fatal_r(r, "load pubkey \"%s\"", tmp);
1838 if (sshkey_is_cert(public))
1839 fatal_f("key \"%s\" type %s cannot be certified",
1840 tmp, sshkey_type(public));
1842 /* Prepare certificate to sign */
1843 if ((r = sshkey_to_certified(public)) != 0)
1844 fatal_r(r, "Could not upgrade key %s to certificate", tmp);
1845 public->cert->type = cert_key_type;
1846 public->cert->serial = (u_int64_t)cert_serial;
1847 public->cert->key_id = xstrdup(cert_key_id);
1848 public->cert->nprincipals = n;
1849 public->cert->principals = plist;
1850 public->cert->valid_after = cert_valid_from;
1851 public->cert->valid_before = cert_valid_to;
1852 prepare_options_buf(public->cert->critical, OPTIONS_CRITICAL);
1853 prepare_options_buf(public->cert->extensions,
1854 OPTIONS_EXTENSIONS);
1855 if ((r = sshkey_from_private(ca,
1856 &public->cert->signature_key)) != 0)
1857 fatal_r(r, "sshkey_from_private (ca key)");
1859 if (agent_fd != -1 && (ca->flags & SSHKEY_FLAG_EXT) != 0) {
1860 if ((r = sshkey_certify_custom(public, ca,
1861 key_type_name, sk_provider, NULL, agent_signer,
1863 fatal_r(r, "Couldn't certify %s via agent", tmp);
1865 if (sshkey_is_sk(ca) &&
1866 (ca->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
1867 notifier = notify_start(0,
1868 "Confirm user presence for key %s %s",
1869 sshkey_type(ca), ca_fp);
1871 r = sshkey_certify(public, ca, key_type_name,
1873 notify_complete(notifier, "User presence confirmed");
1875 fatal_r(r, "Couldn't certify key %s", tmp);
1878 if ((cp = strrchr(tmp, '.')) != NULL && strcmp(cp, ".pub") == 0)
1880 xasprintf(&out, "%s-cert.pub", tmp);
1883 if ((r = sshkey_save_public(public, out, comment)) != 0) {
1884 fatal_r(r, "Unable to save public key to %s",
1889 sshkey_format_cert_validity(public->cert,
1890 valid, sizeof(valid));
1891 logit("Signed %s key %s: id \"%s\" serial %llu%s%s "
1892 "valid %s", sshkey_cert_type(public),
1893 out, public->cert->key_id,
1894 (unsigned long long)public->cert->serial,
1895 cert_principals != NULL ? " for " : "",
1896 cert_principals != NULL ? cert_principals : "",
1900 sshkey_free(public);
1902 if (cert_serial_autoinc)
1906 freezero(pin, strlen(pin));
1908 #ifdef ENABLE_PKCS11
1915 parse_relative_time(const char *s, time_t now)
1919 mul = *s == '-' ? -1 : 1;
1921 if ((secs = convtime(s + 1)) == -1)
1922 fatal("Invalid relative certificate time %s", s);
1923 if (mul == -1 && secs > now)
1924 fatal("Certificate time %s cannot be represented", s);
1925 return now + (u_int64_t)(secs * mul);
1929 parse_hex_u64(const char *s, uint64_t *up)
1932 unsigned long long ull;
1935 ull = strtoull(s, &ep, 16);
1936 if (*s == '\0' || *ep != '\0')
1937 fatal("Invalid certificate time: not a number");
1938 if (errno == ERANGE && ull == ULONG_MAX)
1939 fatal_fr(SSH_ERR_SYSTEM_ERROR, "Invalid certificate time");
1940 *up = (uint64_t)ull;
1944 parse_cert_times(char *timespec)
1947 time_t now = time(NULL);
1950 /* +timespec relative to now */
1951 if (*timespec == '+' && strchr(timespec, ':') == NULL) {
1952 if ((secs = convtime(timespec + 1)) == -1)
1953 fatal("Invalid relative certificate life %s", timespec);
1954 cert_valid_to = now + secs;
1956 * Backdate certificate one minute to avoid problems on hosts
1957 * with poorly-synchronised clocks.
1959 cert_valid_from = ((now - 59)/ 60) * 60;
1965 * from := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | 0x... | "always"
1966 * to := [+-]timespec | YYYYMMDD | YYYYMMDDHHMMSS | 0x... | "forever"
1968 from = xstrdup(timespec);
1969 to = strchr(from, ':');
1970 if (to == NULL || from == to || *(to + 1) == '\0')
1971 fatal("Invalid certificate life specification %s", timespec);
1974 if (*from == '-' || *from == '+')
1975 cert_valid_from = parse_relative_time(from, now);
1976 else if (strcmp(from, "always") == 0)
1977 cert_valid_from = 0;
1978 else if (strncmp(from, "0x", 2) == 0)
1979 parse_hex_u64(from, &cert_valid_from);
1980 else if (parse_absolute_time(from, &cert_valid_from) != 0)
1981 fatal("Invalid from time \"%s\"", from);
1983 if (*to == '-' || *to == '+')
1984 cert_valid_to = parse_relative_time(to, now);
1985 else if (strcmp(to, "forever") == 0)
1986 cert_valid_to = ~(u_int64_t)0;
1987 else if (strncmp(to, "0x", 2) == 0)
1988 parse_hex_u64(to, &cert_valid_to);
1989 else if (parse_absolute_time(to, &cert_valid_to) != 0)
1990 fatal("Invalid to time \"%s\"", to);
1992 if (cert_valid_to <= cert_valid_from)
1993 fatal("Empty certificate validity interval");
1998 add_cert_option(char *opt)
2003 if (strcasecmp(opt, "clear") == 0)
2004 certflags_flags = 0;
2005 else if (strcasecmp(opt, "no-x11-forwarding") == 0)
2006 certflags_flags &= ~CERTOPT_X_FWD;
2007 else if (strcasecmp(opt, "permit-x11-forwarding") == 0)
2008 certflags_flags |= CERTOPT_X_FWD;
2009 else if (strcasecmp(opt, "no-agent-forwarding") == 0)
2010 certflags_flags &= ~CERTOPT_AGENT_FWD;
2011 else if (strcasecmp(opt, "permit-agent-forwarding") == 0)
2012 certflags_flags |= CERTOPT_AGENT_FWD;
2013 else if (strcasecmp(opt, "no-port-forwarding") == 0)
2014 certflags_flags &= ~CERTOPT_PORT_FWD;
2015 else if (strcasecmp(opt, "permit-port-forwarding") == 0)
2016 certflags_flags |= CERTOPT_PORT_FWD;
2017 else if (strcasecmp(opt, "no-pty") == 0)
2018 certflags_flags &= ~CERTOPT_PTY;
2019 else if (strcasecmp(opt, "permit-pty") == 0)
2020 certflags_flags |= CERTOPT_PTY;
2021 else if (strcasecmp(opt, "no-user-rc") == 0)
2022 certflags_flags &= ~CERTOPT_USER_RC;
2023 else if (strcasecmp(opt, "permit-user-rc") == 0)
2024 certflags_flags |= CERTOPT_USER_RC;
2025 else if (strcasecmp(opt, "touch-required") == 0)
2026 certflags_flags &= ~CERTOPT_NO_REQUIRE_USER_PRESENCE;
2027 else if (strcasecmp(opt, "no-touch-required") == 0)
2028 certflags_flags |= CERTOPT_NO_REQUIRE_USER_PRESENCE;
2029 else if (strcasecmp(opt, "no-verify-required") == 0)
2030 certflags_flags &= ~CERTOPT_REQUIRE_VERIFY;
2031 else if (strcasecmp(opt, "verify-required") == 0)
2032 certflags_flags |= CERTOPT_REQUIRE_VERIFY;
2033 else if (strncasecmp(opt, "force-command=", 14) == 0) {
2036 fatal("Empty force-command option");
2037 if (certflags_command != NULL)
2038 fatal("force-command already specified");
2039 certflags_command = xstrdup(val);
2040 } else if (strncasecmp(opt, "source-address=", 15) == 0) {
2043 fatal("Empty source-address option");
2044 if (certflags_src_addr != NULL)
2045 fatal("source-address already specified");
2046 if (addr_match_cidr_list(NULL, val) != 0)
2047 fatal("Invalid source-address list");
2048 certflags_src_addr = xstrdup(val);
2049 } else if (strncasecmp(opt, "extension:", 10) == 0 ||
2050 (iscrit = (strncasecmp(opt, "critical:", 9) == 0))) {
2051 val = xstrdup(strchr(opt, ':') + 1);
2052 if ((cp = strchr(val, '=')) != NULL)
2054 cert_ext_add(val, cp, iscrit);
2057 fatal("Unsupported certificate option \"%s\"", opt);
2061 show_options(struct sshbuf *optbuf, int in_critical)
2063 char *name, *arg, *hex;
2064 struct sshbuf *options, *option = NULL;
2067 if ((options = sshbuf_fromb(optbuf)) == NULL)
2068 fatal_f("sshbuf_fromb failed");
2069 while (sshbuf_len(options) != 0) {
2070 sshbuf_free(option);
2072 if ((r = sshbuf_get_cstring(options, &name, NULL)) != 0 ||
2073 (r = sshbuf_froms(options, &option)) != 0)
2074 fatal_fr(r, "parse option");
2075 printf(" %s", name);
2077 (strcmp(name, "permit-X11-forwarding") == 0 ||
2078 strcmp(name, "permit-agent-forwarding") == 0 ||
2079 strcmp(name, "permit-port-forwarding") == 0 ||
2080 strcmp(name, "permit-pty") == 0 ||
2081 strcmp(name, "permit-user-rc") == 0 ||
2082 strcmp(name, "no-touch-required") == 0)) {
2084 } else if (in_critical &&
2085 (strcmp(name, "force-command") == 0 ||
2086 strcmp(name, "source-address") == 0)) {
2087 if ((r = sshbuf_get_cstring(option, &arg, NULL)) != 0)
2088 fatal_fr(r, "parse critical");
2089 printf(" %s\n", arg);
2091 } else if (in_critical &&
2092 strcmp(name, "verify-required") == 0) {
2094 } else if (sshbuf_len(option) > 0) {
2095 hex = sshbuf_dtob16(option);
2096 printf(" UNKNOWN OPTION: %s (len %zu)\n",
2097 hex, sshbuf_len(option));
2098 sshbuf_reset(option);
2101 printf(" UNKNOWN FLAG OPTION\n");
2103 if (sshbuf_len(option) != 0)
2104 fatal("Option corrupt: extra data at end");
2106 sshbuf_free(option);
2107 sshbuf_free(options);
2111 print_cert(struct sshkey *key)
2113 char valid[64], *key_fp, *ca_fp;
2116 key_fp = sshkey_fingerprint(key, fingerprint_hash, SSH_FP_DEFAULT);
2117 ca_fp = sshkey_fingerprint(key->cert->signature_key,
2118 fingerprint_hash, SSH_FP_DEFAULT);
2119 if (key_fp == NULL || ca_fp == NULL)
2120 fatal_f("sshkey_fingerprint fail");
2121 sshkey_format_cert_validity(key->cert, valid, sizeof(valid));
2123 printf(" Type: %s %s certificate\n", sshkey_ssh_name(key),
2124 sshkey_cert_type(key));
2125 printf(" Public key: %s %s\n", sshkey_type(key), key_fp);
2126 printf(" Signing CA: %s %s (using %s)\n",
2127 sshkey_type(key->cert->signature_key), ca_fp,
2128 key->cert->signature_type);
2129 printf(" Key ID: \"%s\"\n", key->cert->key_id);
2130 printf(" Serial: %llu\n", (unsigned long long)key->cert->serial);
2131 printf(" Valid: %s\n", valid);
2132 printf(" Principals: ");
2133 if (key->cert->nprincipals == 0)
2136 for (i = 0; i < key->cert->nprincipals; i++)
2138 key->cert->principals[i]);
2141 printf(" Critical Options: ");
2142 if (sshbuf_len(key->cert->critical) == 0)
2146 show_options(key->cert->critical, 1);
2148 printf(" Extensions: ");
2149 if (sshbuf_len(key->cert->extensions) == 0)
2153 show_options(key->cert->extensions, 0);
2158 do_show_cert(struct passwd *pw)
2160 struct sshkey *key = NULL;
2162 int r, is_stdin = 0, ok = 0;
2164 char *cp, *line = NULL;
2166 size_t linesize = 0;
2170 ask_filename(pw, "Enter file in which the key is");
2171 if (strcmp(identity_file, "-") != 0 && stat(identity_file, &st) == -1)
2172 fatal("%s: %s: %s", __progname, identity_file, strerror(errno));
2174 path = identity_file;
2175 if (strcmp(path, "-") == 0) {
2179 } else if ((f = fopen(identity_file, "r")) == NULL)
2180 fatal("fopen %s: %s", identity_file, strerror(errno));
2182 while (getline(&line, &linesize, f) != -1) {
2186 /* Trim leading space and comments */
2187 cp = line + strspn(line, " \t");
2188 if (*cp == '#' || *cp == '\0')
2190 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
2191 fatal("sshkey_new");
2192 if ((r = sshkey_read(key, &cp)) != 0) {
2193 error_r(r, "%s:%lu: invalid key", path, lnum);
2196 if (!sshkey_is_cert(key)) {
2197 error("%s:%lu is not a certificate", path, lnum);
2201 if (!is_stdin && lnum == 1)
2202 printf("%s:\n", path);
2204 printf("%s:%lu:\n", path, lnum);
2214 load_krl(const char *path, struct ssh_krl **krlp)
2216 struct sshbuf *krlbuf;
2219 if ((r = sshbuf_load_file(path, &krlbuf)) != 0)
2220 fatal_r(r, "Unable to load KRL %s", path);
2221 /* XXX check sigs */
2222 if ((r = ssh_krl_from_blob(krlbuf, krlp)) != 0 ||
2224 fatal_r(r, "Invalid KRL file %s", path);
2225 sshbuf_free(krlbuf);
2229 hash_to_blob(const char *cp, u_char **blobp, size_t *lenp,
2230 const char *file, u_long lnum)
2237 if (strncmp(cp, "SHA256:", 7) != 0)
2238 fatal("%s:%lu: unsupported hash algorithm", file, lnum);
2242 * OpenSSH base64 hashes omit trailing '='
2243 * characters; put them back for decode.
2245 if ((tlen = strlen(cp)) >= SIZE_MAX - 5)
2246 fatal_f("hash too long: %zu bytes", tlen);
2247 tmp = xmalloc(tlen + 4 + 1);
2248 strlcpy(tmp, cp, tlen + 1);
2249 while ((tlen % 4) != 0) {
2253 if ((b = sshbuf_new()) == NULL)
2254 fatal_f("sshbuf_new failed");
2255 if ((r = sshbuf_b64tod(b, tmp)) != 0)
2256 fatal_r(r, "%s:%lu: decode hash failed", file, lnum);
2258 *lenp = sshbuf_len(b);
2259 *blobp = xmalloc(*lenp);
2260 memcpy(*blobp, sshbuf_ptr(b), *lenp);
2265 update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
2266 const struct sshkey *ca, struct ssh_krl *krl)
2268 struct sshkey *key = NULL;
2270 char *path, *cp, *ep, *line = NULL;
2271 u_char *blob = NULL;
2272 size_t blen = 0, linesize = 0;
2273 unsigned long long serial, serial2;
2274 int i, was_explicit_key, was_sha1, was_sha256, was_hash, r;
2277 path = tilde_expand_filename(file, pw->pw_uid);
2278 if (strcmp(path, "-") == 0) {
2281 path = xstrdup("(standard input)");
2282 } else if ((krl_spec = fopen(path, "r")) == NULL)
2283 fatal("fopen %s: %s", path, strerror(errno));
2286 printf("Revoking from %s\n", path);
2287 while (getline(&line, &linesize, krl_spec) != -1) {
2288 if (linesize >= INT_MAX) {
2289 fatal_f("%s contains unparsable line, len=%zu",
2293 was_explicit_key = was_sha1 = was_sha256 = was_hash = 0;
2294 cp = line + strspn(line, " \t");
2295 /* Trim trailing space, comments and strip \n */
2296 for (i = 0, r = -1; cp[i] != '\0'; i++) {
2297 if (cp[i] == '#' || cp[i] == '\n') {
2301 if (cp[i] == ' ' || cp[i] == '\t') {
2302 /* Remember the start of a span of whitespace */
2312 if (strncasecmp(cp, "serial:", 7) == 0) {
2313 if (ca == NULL && !wild_ca) {
2314 fatal("revoking certificates by serial number "
2315 "requires specification of a CA key");
2318 cp = cp + strspn(cp, " \t");
2320 serial = strtoull(cp, &ep, 0);
2321 if (*cp == '\0' || (*ep != '\0' && *ep != '-'))
2322 fatal("%s:%lu: invalid serial \"%s\"",
2324 if (errno == ERANGE && serial == ULLONG_MAX)
2325 fatal("%s:%lu: serial out of range",
2331 serial2 = strtoull(cp, &ep, 0);
2332 if (*cp == '\0' || *ep != '\0')
2333 fatal("%s:%lu: invalid serial \"%s\"",
2335 if (errno == ERANGE && serial2 == ULLONG_MAX)
2336 fatal("%s:%lu: serial out of range",
2338 if (serial2 <= serial)
2339 fatal("%s:%lu: invalid serial range "
2340 "%llu:%llu", path, lnum,
2341 (unsigned long long)serial,
2342 (unsigned long long)serial2);
2344 if (ssh_krl_revoke_cert_by_serial_range(krl,
2345 ca, serial, serial2) != 0) {
2346 fatal_f("revoke serial failed");
2348 } else if (strncasecmp(cp, "id:", 3) == 0) {
2349 if (ca == NULL && !wild_ca) {
2350 fatal("revoking certificates by key ID "
2351 "requires specification of a CA key");
2354 cp = cp + strspn(cp, " \t");
2355 if (ssh_krl_revoke_cert_by_key_id(krl, ca, cp) != 0)
2356 fatal_f("revoke key ID failed");
2357 } else if (strncasecmp(cp, "hash:", 5) == 0) {
2359 cp = cp + strspn(cp, " \t");
2360 hash_to_blob(cp, &blob, &blen, file, lnum);
2361 r = ssh_krl_revoke_key_sha256(krl, blob, blen);
2363 fatal_fr(r, "revoke key failed");
2365 if (strncasecmp(cp, "key:", 4) == 0) {
2367 cp = cp + strspn(cp, " \t");
2368 was_explicit_key = 1;
2369 } else if (strncasecmp(cp, "sha1:", 5) == 0) {
2371 cp = cp + strspn(cp, " \t");
2373 } else if (strncasecmp(cp, "sha256:", 7) == 0) {
2375 cp = cp + strspn(cp, " \t");
2378 * Just try to process the line as a key.
2379 * Parsing will fail if it isn't.
2382 if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
2383 fatal("sshkey_new");
2384 if ((r = sshkey_read(key, &cp)) != 0)
2385 fatal_r(r, "%s:%lu: invalid key", path, lnum);
2386 if (was_explicit_key)
2387 r = ssh_krl_revoke_key_explicit(krl, key);
2388 else if (was_sha1) {
2389 if (sshkey_fingerprint_raw(key,
2390 SSH_DIGEST_SHA1, &blob, &blen) != 0) {
2391 fatal("%s:%lu: fingerprint failed",
2394 r = ssh_krl_revoke_key_sha1(krl, blob, blen);
2395 } else if (was_sha256) {
2396 if (sshkey_fingerprint_raw(key,
2397 SSH_DIGEST_SHA256, &blob, &blen) != 0) {
2398 fatal("%s:%lu: fingerprint failed",
2401 r = ssh_krl_revoke_key_sha256(krl, blob, blen);
2403 r = ssh_krl_revoke_key(krl, key);
2405 fatal_fr(r, "revoke key failed");
2406 freezero(blob, blen);
2412 if (strcmp(path, "-") != 0)
2419 do_gen_krl(struct passwd *pw, int updating, const char *ca_key_path,
2420 unsigned long long krl_version, const char *krl_comment,
2421 int argc, char **argv)
2423 struct ssh_krl *krl;
2425 struct sshkey *ca = NULL;
2426 int i, r, wild_ca = 0;
2428 struct sshbuf *kbuf;
2430 if (*identity_file == '\0')
2431 fatal("KRL generation requires an output file");
2432 if (stat(identity_file, &sb) == -1) {
2433 if (errno != ENOENT)
2434 fatal("Cannot access KRL \"%s\": %s",
2435 identity_file, strerror(errno));
2437 fatal("KRL \"%s\" does not exist", identity_file);
2439 if (ca_key_path != NULL) {
2440 if (strcasecmp(ca_key_path, "none") == 0)
2443 tmp = tilde_expand_filename(ca_key_path, pw->pw_uid);
2444 if ((r = sshkey_load_public(tmp, &ca, NULL)) != 0)
2445 fatal_r(r, "Cannot load CA public key %s", tmp);
2451 load_krl(identity_file, &krl);
2452 else if ((krl = ssh_krl_init()) == NULL)
2453 fatal("couldn't create KRL");
2455 if (krl_version != 0)
2456 ssh_krl_set_version(krl, krl_version);
2457 if (krl_comment != NULL)
2458 ssh_krl_set_comment(krl, krl_comment);
2460 for (i = 0; i < argc; i++)
2461 update_krl_from_file(pw, argv[i], wild_ca, ca, krl);
2463 if ((kbuf = sshbuf_new()) == NULL)
2464 fatal("sshbuf_new failed");
2465 if (ssh_krl_to_blob(krl, kbuf) != 0)
2466 fatal("Couldn't generate KRL");
2467 if ((r = sshbuf_write_file(identity_file, kbuf)) != 0)
2468 fatal("write %s: %s", identity_file, strerror(errno));
2475 do_check_krl(struct passwd *pw, int print_krl, int argc, char **argv)
2479 struct ssh_krl *krl;
2482 if (*identity_file == '\0')
2483 fatal("KRL checking requires an input file");
2484 load_krl(identity_file, &krl);
2486 krl_dump(krl, stdout);
2487 for (i = 0; i < argc; i++) {
2488 if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
2489 fatal_r(r, "Cannot load public key %s", argv[i]);
2490 r = ssh_krl_check_key(krl, k);
2491 printf("%s%s%s%s: %s\n", argv[i],
2492 *comment ? " (" : "", comment, *comment ? ")" : "",
2493 r == 0 ? "ok" : "REVOKED");
2503 static struct sshkey *
2504 load_sign_key(const char *keypath, const struct sshkey *pubkey)
2506 size_t i, slen, plen = strlen(keypath);
2507 char *privpath = xstrdup(keypath);
2508 static const char * const suffixes[] = { "-cert.pub", ".pub", NULL };
2509 struct sshkey *ret = NULL, *privkey = NULL;
2514 * If passed a public key filename, then try to locate the corresponding
2515 * private key. This lets us specify certificates on the command-line
2516 * and have ssh-keygen find the appropriate private key.
2518 for (i = 0; suffixes[i]; i++) {
2519 slen = strlen(suffixes[i]);
2521 strcmp(privpath + plen - slen, suffixes[i]) != 0)
2523 privpath[plen - slen] = '\0';
2524 debug_f("%s looks like a public key, using private key "
2525 "path %s instead", keypath, privpath);
2528 if (waspub && stat(privpath, &st) != 0 && errno == ENOENT)
2529 fatal("No private key found for public key \"%s\"", keypath);
2530 if ((r = sshkey_load_private(privpath, "", &privkey, NULL)) != 0 &&
2531 (r != SSH_ERR_KEY_WRONG_PASSPHRASE)) {
2532 debug_fr(r, "load private key \"%s\"", privpath);
2533 fatal("No private key found for \"%s\"", privpath);
2534 } else if (privkey == NULL)
2535 privkey = load_identity(privpath, NULL);
2537 if (!sshkey_equal_public(pubkey, privkey)) {
2538 error("Public key %s doesn't match private %s",
2542 if (sshkey_is_cert(pubkey) && !sshkey_is_cert(privkey)) {
2544 * Graft the certificate onto the private key to make
2545 * it capable of signing.
2547 if ((r = sshkey_to_certified(privkey)) != 0) {
2548 error_fr(r, "sshkey_to_certified");
2551 if ((r = sshkey_cert_copy(pubkey, privkey)) != 0) {
2552 error_fr(r, "sshkey_cert_copy");
2560 sshkey_free(privkey);
2566 sign_one(struct sshkey *signkey, const char *filename, int fd,
2567 const char *sig_namespace, const char *hashalg, sshsig_signer *signer,
2570 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2571 int r = SSH_ERR_INTERNAL_ERROR, wfd = -1, oerrno;
2572 char *wfile = NULL, *asig = NULL, *fp = NULL;
2573 char *pin = NULL, *prompt = NULL;
2576 if (fd == STDIN_FILENO)
2577 fprintf(stderr, "Signing data on standard input\n");
2579 fprintf(stderr, "Signing file %s\n", filename);
2581 if (signer == NULL && sshkey_is_sk(signkey)) {
2582 if ((signkey->sk_flags & SSH_SK_USER_VERIFICATION_REQD)) {
2583 xasprintf(&prompt, "Enter PIN for %s key: ",
2584 sshkey_type(signkey));
2585 if ((pin = read_passphrase(prompt,
2586 RP_ALLOW_STDIN)) == NULL)
2587 fatal_f("couldn't read PIN");
2589 if ((signkey->sk_flags & SSH_SK_USER_PRESENCE_REQD)) {
2590 if ((fp = sshkey_fingerprint(signkey, fingerprint_hash,
2591 SSH_FP_DEFAULT)) == NULL)
2592 fatal_f("fingerprint failed");
2593 fprintf(stderr, "Confirm user presence for key %s %s\n",
2594 sshkey_type(signkey), fp);
2598 if ((r = sshsig_sign_fd(signkey, hashalg, sk_provider, pin,
2599 fd, sig_namespace, &sigbuf, signer, signer_ctx)) != 0) {
2600 error_r(r, "Signing %s failed", filename);
2603 if ((r = sshsig_armor(sigbuf, &abuf)) != 0) {
2604 error_fr(r, "sshsig_armor");
2607 if ((asig = sshbuf_dup_string(abuf)) == NULL) {
2608 error_f("buffer error");
2609 r = SSH_ERR_ALLOC_FAIL;
2613 if (fd == STDIN_FILENO) {
2614 fputs(asig, stdout);
2617 xasprintf(&wfile, "%s.sig", filename);
2618 if (confirm_overwrite(wfile)) {
2619 if ((wfd = open(wfile, O_WRONLY|O_CREAT|O_TRUNC,
2622 error("Cannot open %s: %s",
2623 wfile, strerror(errno));
2625 r = SSH_ERR_SYSTEM_ERROR;
2628 if (atomicio(vwrite, wfd, asig,
2629 strlen(asig)) != strlen(asig)) {
2631 error("Cannot write to %s: %s",
2632 wfile, strerror(errno));
2634 r = SSH_ERR_SYSTEM_ERROR;
2638 fprintf(stderr, "Write signature to %s\n",
2650 freezero(pin, strlen(pin));
2652 sshbuf_free(sigbuf);
2659 sig_process_opts(char * const *opts, size_t nopts, char **hashalgp,
2660 uint64_t *verify_timep, int *print_pubkey)
2665 if (verify_timep != NULL)
2667 if (print_pubkey != NULL)
2669 if (hashalgp != NULL)
2671 for (i = 0; i < nopts; i++) {
2672 if (hashalgp != NULL &&
2673 strncasecmp(opts[i], "hashalg=", 8) == 0) {
2674 *hashalgp = xstrdup(opts[i] + 8);
2675 } else if (verify_timep &&
2676 strncasecmp(opts[i], "verify-time=", 12) == 0) {
2677 if (parse_absolute_time(opts[i] + 12,
2678 verify_timep) != 0 || *verify_timep == 0) {
2679 error("Invalid \"verify-time\" option");
2680 return SSH_ERR_INVALID_ARGUMENT;
2682 } else if (print_pubkey &&
2683 strcasecmp(opts[i], "print-pubkey") == 0) {
2686 error("Invalid option \"%s\"", opts[i]);
2687 return SSH_ERR_INVALID_ARGUMENT;
2690 if (verify_timep && *verify_timep == 0) {
2691 if ((now = time(NULL)) < 0) {
2692 error("Time is before epoch");
2693 return SSH_ERR_INVALID_ARGUMENT;
2695 *verify_timep = (uint64_t)now;
2702 sig_sign(const char *keypath, const char *sig_namespace, int require_agent,
2703 int argc, char **argv, char * const *opts, size_t nopts)
2705 int i, fd = -1, r, ret = -1;
2707 struct sshkey *pubkey = NULL, *privkey = NULL, *signkey = NULL;
2708 sshsig_signer *signer = NULL;
2709 char *hashalg = NULL;
2711 /* Check file arguments. */
2712 for (i = 0; i < argc; i++) {
2713 if (strcmp(argv[i], "-") != 0)
2715 if (i > 0 || argc > 1)
2716 fatal("Cannot sign mix of paths and standard input");
2719 if (sig_process_opts(opts, nopts, &hashalg, NULL, NULL) != 0)
2720 goto done; /* error already logged */
2722 if ((r = sshkey_load_public(keypath, &pubkey, NULL)) != 0) {
2723 error_r(r, "Couldn't load public key %s", keypath);
2727 if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) {
2729 fatal("Couldn't get agent socket");
2730 debug_r(r, "Couldn't get agent socket");
2732 if ((r = ssh_agent_has_key(agent_fd, pubkey)) == 0)
2733 signer = agent_signer;
2736 fatal("Couldn't find key in agent");
2737 debug_r(r, "Couldn't find key in agent");
2741 if (signer == NULL) {
2742 /* Not using agent - try to load private key */
2743 if ((privkey = load_sign_key(keypath, pubkey)) == NULL)
2747 /* Will use key in agent */
2752 if ((r = sign_one(signkey, "(stdin)", STDIN_FILENO,
2753 sig_namespace, hashalg, signer, &agent_fd)) != 0)
2756 for (i = 0; i < argc; i++) {
2757 if (strcmp(argv[i], "-") == 0)
2759 else if ((fd = open(argv[i], O_RDONLY)) == -1) {
2760 error("Cannot open %s for signing: %s",
2761 argv[i], strerror(errno));
2764 if ((r = sign_one(signkey, argv[i], fd, sig_namespace,
2765 hashalg, signer, &agent_fd)) != 0)
2767 if (fd != STDIN_FILENO)
2775 if (fd != -1 && fd != STDIN_FILENO)
2777 sshkey_free(pubkey);
2778 sshkey_free(privkey);
2784 sig_verify(const char *signature, const char *sig_namespace,
2785 const char *principal, const char *allowed_keys, const char *revoked_keys,
2786 char * const *opts, size_t nopts)
2789 int print_pubkey = 0;
2790 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2791 struct sshkey *sign_key = NULL;
2793 struct sshkey_sig_details *sig_details = NULL;
2794 uint64_t verify_time = 0;
2796 if (sig_process_opts(opts, nopts, NULL, &verify_time,
2797 &print_pubkey) != 0)
2798 goto done; /* error already logged */
2800 memset(&sig_details, 0, sizeof(sig_details));
2801 if ((r = sshbuf_load_file(signature, &abuf)) != 0) {
2802 error_r(r, "Couldn't read signature file");
2806 if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) {
2807 error_fr(r, "sshsig_armor");
2810 if ((r = sshsig_verify_fd(sigbuf, STDIN_FILENO, sig_namespace,
2811 &sign_key, &sig_details)) != 0)
2812 goto done; /* sshsig_verify() prints error */
2814 if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
2815 SSH_FP_DEFAULT)) == NULL)
2816 fatal_f("sshkey_fingerprint failed");
2817 debug("Valid (unverified) signature from key %s", fp);
2818 if (sig_details != NULL) {
2819 debug2_f("signature details: counter = %u, flags = 0x%02x",
2820 sig_details->sk_counter, sig_details->sk_flags);
2825 if (revoked_keys != NULL) {
2826 if ((r = sshkey_check_revoked(sign_key, revoked_keys)) != 0) {
2827 debug3_fr(r, "sshkey_check_revoked");
2832 if (allowed_keys != NULL && (r = sshsig_check_allowed_keys(allowed_keys,
2833 sign_key, principal, sig_namespace, verify_time)) != 0) {
2834 debug3_fr(r, "sshsig_check_allowed_keys");
2842 if ((fp = sshkey_fingerprint(sign_key, fingerprint_hash,
2843 SSH_FP_DEFAULT)) == NULL)
2844 fatal_f("sshkey_fingerprint failed");
2845 if (principal == NULL) {
2846 printf("Good \"%s\" signature with %s key %s\n",
2847 sig_namespace, sshkey_type(sign_key), fp);
2850 printf("Good \"%s\" signature for %s with %s key %s\n",
2851 sig_namespace, principal,
2852 sshkey_type(sign_key), fp);
2855 printf("Could not verify signature.\n");
2858 /* Print the signature key if requested */
2859 if (ret == 0 && print_pubkey && sign_key != NULL) {
2860 if ((r = sshkey_write(sign_key, stdout)) == 0)
2861 fputc('\n', stdout);
2863 error_r(r, "Could not print public key.\n");
2867 sshbuf_free(sigbuf);
2869 sshkey_free(sign_key);
2870 sshkey_sig_details_free(sig_details);
2876 sig_find_principals(const char *signature, const char *allowed_keys,
2877 char * const *opts, size_t nopts)
2880 struct sshbuf *sigbuf = NULL, *abuf = NULL;
2881 struct sshkey *sign_key = NULL;
2882 char *principals = NULL, *cp, *tmp;
2883 uint64_t verify_time = 0;
2885 if (sig_process_opts(opts, nopts, NULL, &verify_time, NULL) != 0)
2886 goto done; /* error already logged */
2888 if ((r = sshbuf_load_file(signature, &abuf)) != 0) {
2889 error_r(r, "Couldn't read signature file");
2892 if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) {
2893 error_fr(r, "sshsig_armor");
2896 if ((r = sshsig_get_pubkey(sigbuf, &sign_key)) != 0) {
2897 error_fr(r, "sshsig_get_pubkey");
2900 if ((r = sshsig_find_principals(allowed_keys, sign_key,
2901 verify_time, &principals)) != 0) {
2902 if (r != SSH_ERR_KEY_NOT_FOUND)
2903 error_fr(r, "sshsig_find_principal");
2909 /* Emit matching principals one per line */
2911 while ((cp = strsep(&tmp, ",")) != NULL && *cp != '\0')
2914 fprintf(stderr, "No principal matched.\n");
2916 sshbuf_free(sigbuf);
2918 sshkey_free(sign_key);
2924 sig_match_principals(const char *allowed_keys, char *principal,
2925 char * const *opts, size_t nopts)
2928 char **principals = NULL;
2929 size_t i, nprincipals = 0;
2931 if ((r = sig_process_opts(opts, nopts, NULL, NULL, NULL)) != 0)
2932 return r; /* error already logged */
2934 if ((r = sshsig_match_principals(allowed_keys, principal,
2935 &principals, &nprincipals)) != 0) {
2936 debug_f("match: %s", ssh_err(r));
2937 fprintf(stderr, "No principal matched.\n");
2940 for (i = 0; i < nprincipals; i++) {
2941 printf("%s\n", principals[i]);
2942 free(principals[i]);
2950 do_moduli_gen(const char *out_file, char **opts, size_t nopts)
2953 /* Moduli generation/screening */
2954 u_int32_t memory = 0;
2955 BIGNUM *start = NULL;
2956 int moduli_bits = 0;
2962 for (i = 0; i < nopts; i++) {
2963 if (strncmp(opts[i], "memory=", 7) == 0) {
2964 memory = (u_int32_t)strtonum(opts[i]+7, 1,
2967 fatal("Memory limit is %s: %s",
2970 } else if (strncmp(opts[i], "start=", 6) == 0) {
2971 /* XXX - also compare length against bits */
2972 if (BN_hex2bn(&start, opts[i]+6) == 0)
2973 fatal("Invalid start point.");
2974 } else if (strncmp(opts[i], "bits=", 5) == 0) {
2975 moduli_bits = (int)strtonum(opts[i]+5, 1,
2978 fatal("Invalid number: %s (%s)",
2979 opts[i]+12, errstr);
2982 fatal("Option \"%s\" is unsupported for moduli "
2983 "generation", opts[i]);
2987 if ((out = fopen(out_file, "w")) == NULL) {
2988 fatal("Couldn't open modulus candidate file \"%s\": %s",
2989 out_file, strerror(errno));
2991 setvbuf(out, NULL, _IOLBF, 0);
2993 if (moduli_bits == 0)
2994 moduli_bits = DEFAULT_BITS;
2995 if (gen_candidates(out, memory, moduli_bits, start) != 0)
2996 fatal("modulus candidate generation failed");
2997 #else /* WITH_OPENSSL */
2998 fatal("Moduli generation is not supported");
2999 #endif /* WITH_OPENSSL */
3003 do_moduli_screen(const char *out_file, char **opts, size_t nopts)
3006 /* Moduli generation/screening */
3007 char *checkpoint = NULL;
3008 u_int32_t generator_wanted = 0;
3009 unsigned long start_lineno = 0, lines_to_process = 0;
3010 int prime_tests = 0;
3011 FILE *out, *in = stdin;
3016 for (i = 0; i < nopts; i++) {
3017 if (strncmp(opts[i], "lines=", 6) == 0) {
3018 lines_to_process = strtoul(opts[i]+6, NULL, 10);
3019 } else if (strncmp(opts[i], "start-line=", 11) == 0) {
3020 start_lineno = strtoul(opts[i]+11, NULL, 10);
3021 } else if (strncmp(opts[i], "checkpoint=", 11) == 0) {
3023 checkpoint = xstrdup(opts[i]+11);
3024 } else if (strncmp(opts[i], "generator=", 10) == 0) {
3025 generator_wanted = (u_int32_t)strtonum(
3026 opts[i]+10, 1, UINT_MAX, &errstr);
3027 if (errstr != NULL) {
3028 fatal("Generator invalid: %s (%s)",
3029 opts[i]+10, errstr);
3031 } else if (strncmp(opts[i], "prime-tests=", 12) == 0) {
3032 prime_tests = (int)strtonum(opts[i]+12, 1,
3035 fatal("Invalid number: %s (%s)",
3036 opts[i]+12, errstr);
3039 fatal("Option \"%s\" is unsupported for moduli "
3040 "screening", opts[i]);
3044 if (have_identity && strcmp(identity_file, "-") != 0) {
3045 if ((in = fopen(identity_file, "r")) == NULL) {
3046 fatal("Couldn't open modulus candidate "
3047 "file \"%s\": %s", identity_file,
3052 if ((out = fopen(out_file, "a")) == NULL) {
3053 fatal("Couldn't open moduli file \"%s\": %s",
3054 out_file, strerror(errno));
3056 setvbuf(out, NULL, _IOLBF, 0);
3057 if (prime_test(in, out, prime_tests == 0 ? 100 : prime_tests,
3058 generator_wanted, checkpoint,
3059 start_lineno, lines_to_process) != 0)
3060 fatal("modulus screening failed");
3064 #else /* WITH_OPENSSL */
3065 fatal("Moduli screening is not supported");
3066 #endif /* WITH_OPENSSL */
3069 /* Read and confirm a passphrase */
3071 read_check_passphrase(const char *prompt1, const char *prompt2,
3072 const char *retry_prompt)
3074 char *passphrase1, *passphrase2;
3077 passphrase1 = read_passphrase(prompt1, RP_ALLOW_STDIN);
3078 passphrase2 = read_passphrase(prompt2, RP_ALLOW_STDIN);
3079 if (strcmp(passphrase1, passphrase2) == 0) {
3080 freezero(passphrase2, strlen(passphrase2));
3083 /* The passphrases do not match. Clear them and retry. */
3084 freezero(passphrase1, strlen(passphrase1));
3085 freezero(passphrase2, strlen(passphrase2));
3086 fputs(retry_prompt, stdout);
3087 fputc('\n', stdout);
3095 private_key_passphrase(void)
3097 if (identity_passphrase)
3098 return xstrdup(identity_passphrase);
3099 if (identity_new_passphrase)
3100 return xstrdup(identity_new_passphrase);
3102 return read_check_passphrase(
3103 "Enter passphrase (empty for no passphrase): ",
3104 "Enter same passphrase again: ",
3105 "Passphrases do not match. Try again.");
3109 sk_suffix(const char *application, const uint8_t *user, size_t userlen)
3114 /* Trim off URL-like preamble */
3115 if (strncmp(application, "ssh://", 6) == 0)
3116 ret = xstrdup(application + 6);
3117 else if (strncmp(application, "ssh:", 4) == 0)
3118 ret = xstrdup(application + 4);
3120 ret = xstrdup(application);
3122 /* Count trailing zeros in user */
3123 for (i = 0; i < userlen; i++) {
3124 if (user[userlen - i - 1] != 0)
3128 return ret; /* user-id was default all-zeros */
3130 /* Append user-id, escaping non-UTF-8 characters */
3132 if (asmprintf(&cp, INT_MAX, NULL, "%.*s", (int)slen, user) == -1)
3133 fatal_f("asmprintf failed");
3134 /* Don't emit a user-id that contains path or control characters */
3135 if (strchr(cp, '/') != NULL || strstr(cp, "..") != NULL ||
3136 strchr(cp, '\\') != NULL) {
3138 cp = tohex(user, slen);
3140 xextendf(&ret, "_", "%s", cp);
3146 do_download_sk(const char *skprovider, const char *device)
3148 struct sshsk_resident_key **srks;
3151 char *fp, *pin = NULL, *pass = NULL, *path, *pubpath;
3155 if (skprovider == NULL)
3156 fatal("Cannot download keys without provider");
3158 pin = read_passphrase("Enter PIN for authenticator: ", RP_ALLOW_STDIN);
3160 printf("You may need to touch your authenticator "
3161 "to authorize key download.\n");
3163 if ((r = sshsk_load_resident(skprovider, device, pin, 0,
3164 &srks, &nsrks)) != 0) {
3166 freezero(pin, strlen(pin));
3167 error_r(r, "Unable to load resident keys");
3171 logit("No keys to download");
3173 freezero(pin, strlen(pin));
3175 for (i = 0; i < nsrks; i++) {
3177 if (key->type != KEY_ECDSA_SK && key->type != KEY_ED25519_SK) {
3178 error("Unsupported key type %s (%d)",
3179 sshkey_type(key), key->type);
3182 if ((fp = sshkey_fingerprint(key, fingerprint_hash,
3183 SSH_FP_DEFAULT)) == NULL)
3184 fatal_f("sshkey_fingerprint failed");
3185 debug_f("key %zu: %s %s %s (flags 0x%02x)", i,
3186 sshkey_type(key), fp, key->sk_application, key->sk_flags);
3187 ext = sk_suffix(key->sk_application,
3188 srks[i]->user_id, srks[i]->user_id_len);
3189 xasprintf(&path, "id_%s_rk%s%s",
3190 key->type == KEY_ECDSA_SK ? "ecdsa_sk" : "ed25519_sk",
3191 *ext == '\0' ? "" : "_", ext);
3193 /* If the file already exists, ask the user to confirm. */
3194 if (!confirm_overwrite(path)) {
3199 /* Save the key with the application string as the comment */
3201 pass = private_key_passphrase();
3202 if ((r = sshkey_save_private(key, path, pass,
3203 key->sk_application, private_key_format,
3204 openssh_format_cipher, rounds)) != 0) {
3205 error_r(r, "Saving key \"%s\" failed", path);
3210 printf("Saved %s key%s%s to %s\n", sshkey_type(key),
3211 *ext != '\0' ? " " : "",
3212 *ext != '\0' ? key->sk_application : "",
3216 /* Save public key too */
3217 xasprintf(&pubpath, "%s.pub", path);
3219 if ((r = sshkey_save_public(key, pubpath,
3220 key->sk_application)) != 0) {
3221 error_r(r, "Saving public key \"%s\" failed", pubpath);
3229 ret = 0; /* success */
3231 freezero(pass, strlen(pass));
3232 sshsk_free_resident_keys(srks, nsrks);
3237 save_attestation(struct sshbuf *attest, const char *path)
3243 return; /* nothing to do */
3244 if (attest == NULL || sshbuf_len(attest) == 0)
3245 fatal("Enrollment did not return attestation data");
3247 r = sshbuf_write_file(path, attest);
3250 fatal_r(r, "Unable to write attestation data \"%s\"", path);
3252 printf("Your FIDO attestation certificate has been saved in "
3257 confirm_sk_overwrite(const char *application, const char *user)
3261 printf("A resident key scoped to '%s' with user id '%s' already "
3262 "exists.\n", application == NULL ? "ssh:" : application,
3263 user == NULL ? "null" : user);
3264 printf("Overwrite key in token (y/n)? ");
3266 if (fgets(yesno, sizeof(yesno), stdin) == NULL)
3268 if (yesno[0] != 'y' && yesno[0] != 'Y')
3277 "usage: ssh-keygen [-q] [-a rounds] [-b bits] [-C comment] [-f output_keyfile]\n"
3278 " [-m format] [-N new_passphrase] [-O option]\n"
3279 " [-t dsa | ecdsa | ecdsa-sk | ed25519 | ed25519-sk | rsa]\n"
3280 " [-w provider] [-Z cipher]\n"
3281 " ssh-keygen -p [-a rounds] [-f keyfile] [-m format] [-N new_passphrase]\n"
3282 " [-P old_passphrase] [-Z cipher]\n"
3284 " ssh-keygen -i [-f input_keyfile] [-m key_format]\n"
3285 " ssh-keygen -e [-f input_keyfile] [-m key_format]\n"
3287 " ssh-keygen -y [-f input_keyfile]\n"
3288 " ssh-keygen -c [-a rounds] [-C comment] [-f keyfile] [-P passphrase]\n"
3289 " ssh-keygen -l [-v] [-E fingerprint_hash] [-f input_keyfile]\n"
3290 " ssh-keygen -B [-f input_keyfile]\n");
3291 #ifdef ENABLE_PKCS11
3293 " ssh-keygen -D pkcs11\n");
3296 " ssh-keygen -F hostname [-lv] [-f known_hosts_file]\n"
3297 " ssh-keygen -H [-f known_hosts_file]\n"
3298 " ssh-keygen -K [-a rounds] [-w provider]\n"
3299 " ssh-keygen -R hostname [-f known_hosts_file]\n"
3300 " ssh-keygen -r hostname [-g] [-f input_keyfile]\n"
3302 " ssh-keygen -M generate [-O option] output_file\n"
3303 " ssh-keygen -M screen [-f input_file] [-O option] output_file\n"
3305 " ssh-keygen -I certificate_identity -s ca_key [-hU] [-D pkcs11_provider]\n"
3306 " [-n principals] [-O option] [-V validity_interval]\n"
3307 " [-z serial_number] file ...\n"
3308 " ssh-keygen -L [-f input_keyfile]\n"
3309 " ssh-keygen -A [-a rounds] [-f prefix_path]\n"
3310 " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
3312 " ssh-keygen -Q [-l] -f krl_file [file ...]\n"
3313 " ssh-keygen -Y find-principals -s signature_file -f allowed_signers_file\n"
3314 " ssh-keygen -Y match-principals -I signer_identity -f allowed_signers_file\n"
3315 " ssh-keygen -Y check-novalidate -n namespace -s signature_file\n"
3316 " ssh-keygen -Y sign -f key_file -n namespace file [-O option] ...\n"
3317 " ssh-keygen -Y verify -f allowed_signers_file -I signer_identity\n"
3318 " -n namespace -s signature_file [-r krl_file] [-O option]\n");
3323 * Main program for key management.
3326 main(int argc, char **argv)
3328 char comment[1024], *passphrase = NULL;
3329 char *rr_hostname = NULL, *ep, *fp, *ra;
3330 struct sshkey *private, *public;
3333 int change_passphrase = 0, change_comment = 0, show_cert = 0;
3334 int find_host = 0, delete_host = 0, hash_hosts = 0;
3335 int gen_all_hostkeys = 0, gen_krl = 0, update_krl = 0, check_krl = 0;
3336 int prefer_agent = 0, convert_to = 0, convert_from = 0;
3337 int print_public = 0, print_generic = 0, cert_serial_autoinc = 0;
3338 int do_gen_candidates = 0, do_screen_candidates = 0, download_sk = 0;
3339 unsigned long long cert_serial = 0;
3340 char *identity_comment = NULL, *ca_key_path = NULL, **opts = NULL;
3341 char *sk_application = NULL, *sk_device = NULL, *sk_user = NULL;
3342 char *sk_attestation_path = NULL;
3343 struct sshbuf *challenge = NULL, *attest = NULL;
3344 size_t i, nopts = 0;
3346 uint8_t sk_flags = SSH_SK_USER_PRESENCE_REQD;
3348 int log_level = SYSLOG_LEVEL_INFO;
3349 char *sign_op = NULL;
3352 extern char *optarg;
3354 /* Ensure that fds 0, 1 and 2 are open or directed to /dev/null */
3357 __progname = ssh_get_progname(argv[0]);
3361 log_init(argv[0], SYSLOG_LEVEL_INFO, SYSLOG_FACILITY_USER, 1);
3365 /* we need this for the home * directory. */
3366 pw = getpwuid(getuid());
3368 fatal("No user exists for uid %lu", (u_long)getuid());
3370 if (gethostname(hostname, sizeof(hostname)) == -1)
3371 fatal("gethostname: %s", strerror(errno));
3373 sk_provider = getenv("SSH_SK_PROVIDER");
3375 /* Remaining characters: dGjJSTWx */
3376 while ((opt = getopt(argc, argv, "ABHKLQUXceghiklopquvy"
3377 "C:D:E:F:I:M:N:O:P:R:V:Y:Z:"
3378 "a:b:f:g:m:n:r:s:t:w:z:")) != -1) {
3381 gen_all_hostkeys = 1;
3384 bits = (u_int32_t)strtonum(optarg, 1, UINT32_MAX,
3387 fatal("Bits has bad value %s (%s)",
3391 fingerprint_hash = ssh_digest_alg_by_name(optarg);
3392 if (fingerprint_hash == -1)
3393 fatal("Invalid hash algorithm \"%s\"", optarg);
3397 rr_hostname = optarg;
3403 cert_key_id = optarg;
3407 rr_hostname = optarg;
3413 print_fingerprint = 1;
3416 print_bubblebabble = 1;
3419 if (strcasecmp(optarg, "RFC4716") == 0 ||
3420 strcasecmp(optarg, "ssh2") == 0) {
3421 convert_format = FMT_RFC4716;
3424 if (strcasecmp(optarg, "PKCS8") == 0) {
3425 convert_format = FMT_PKCS8;
3426 private_key_format = SSHKEY_PRIVATE_PKCS8;
3429 if (strcasecmp(optarg, "PEM") == 0) {
3430 convert_format = FMT_PEM;
3431 private_key_format = SSHKEY_PRIVATE_PEM;
3434 fatal("Unsupported conversion format \"%s\"", optarg);
3436 cert_principals = optarg;
3439 /* no-op; new format is already the default */
3442 change_passphrase = 1;
3448 if (strlcpy(identity_file, optarg,
3449 sizeof(identity_file)) >= sizeof(identity_file))
3450 fatal("Identity filename too long");
3460 identity_passphrase = optarg;
3463 identity_new_passphrase = optarg;
3469 opts = xrecallocarray(opts, nopts, nopts + 1,
3471 opts[nopts++] = xstrdup(optarg);
3474 openssh_format_cipher = optarg;
3475 if (cipher_by_name(openssh_format_cipher) == NULL)
3476 fatal("Invalid OpenSSH-format cipher '%s'",
3477 openssh_format_cipher);
3480 identity_comment = optarg;
3490 cert_key_type = SSH2_CERT_TYPE_HOST;
3491 certflags_flags = 0;
3505 ca_key_path = optarg;
3508 key_type_name = optarg;
3511 pkcs11provider = optarg;
3520 if (log_level == SYSLOG_LEVEL_INFO)
3521 log_level = SYSLOG_LEVEL_DEBUG1;
3523 if (log_level >= SYSLOG_LEVEL_DEBUG1 &&
3524 log_level < SYSLOG_LEVEL_DEBUG3)
3529 rr_hostname = optarg;
3532 rounds = (int)strtonum(optarg, 1, INT_MAX, &errstr);
3534 fatal("Invalid number: %s (%s)",
3538 parse_cert_times(optarg);
3544 sk_provider = optarg;
3548 if (*optarg == '+') {
3549 cert_serial_autoinc = 1;
3552 cert_serial = strtoull(optarg, &ep, 10);
3553 if (*optarg < '0' || *optarg > '9' || *ep != '\0' ||
3554 (errno == ERANGE && cert_serial == ULLONG_MAX))
3555 fatal("Invalid serial number \"%s\"", optarg);
3558 if (strcmp(optarg, "generate") == 0)
3559 do_gen_candidates = 1;
3560 else if (strcmp(optarg, "screen") == 0)
3561 do_screen_candidates = 1;
3563 fatal("Unsupported moduli option %s", optarg);
3570 #ifdef ENABLE_SK_INTERNAL
3571 if (sk_provider == NULL)
3572 sk_provider = "internal";
3576 log_init(argv[0], log_level, SYSLOG_FACILITY_USER, 1);
3581 if (sign_op != NULL) {
3582 if (strncmp(sign_op, "find-principals", 15) == 0) {
3583 if (ca_key_path == NULL) {
3584 error("Too few arguments for find-principals:"
3585 "missing signature file");
3588 if (!have_identity) {
3589 error("Too few arguments for find-principals:"
3590 "missing allowed keys file");
3593 return sig_find_principals(ca_key_path, identity_file,
3595 } else if (strncmp(sign_op, "match-principals", 16) == 0) {
3596 if (!have_identity) {
3597 error("Too few arguments for match-principals:"
3598 "missing allowed keys file");
3601 if (cert_key_id == NULL) {
3602 error("Too few arguments for match-principals: "
3603 "missing principal ID");
3606 return sig_match_principals(identity_file, cert_key_id,
3608 } else if (strncmp(sign_op, "sign", 4) == 0) {
3609 /* NB. cert_principals is actually namespace, via -n */
3610 if (cert_principals == NULL ||
3611 *cert_principals == '\0') {
3612 error("Too few arguments for sign: "
3613 "missing namespace");
3616 if (!have_identity) {
3617 error("Too few arguments for sign: "
3621 return sig_sign(identity_file, cert_principals,
3622 prefer_agent, argc, argv, opts, nopts);
3623 } else if (strncmp(sign_op, "check-novalidate", 16) == 0) {
3624 /* NB. cert_principals is actually namespace, via -n */
3625 if (cert_principals == NULL ||
3626 *cert_principals == '\0') {
3627 error("Too few arguments for check-novalidate: "
3628 "missing namespace");
3631 if (ca_key_path == NULL) {
3632 error("Too few arguments for check-novalidate: "
3633 "missing signature file");
3636 return sig_verify(ca_key_path, cert_principals,
3637 NULL, NULL, NULL, opts, nopts);
3638 } else if (strncmp(sign_op, "verify", 6) == 0) {
3639 /* NB. cert_principals is actually namespace, via -n */
3640 if (cert_principals == NULL ||
3641 *cert_principals == '\0') {
3642 error("Too few arguments for verify: "
3643 "missing namespace");
3646 if (ca_key_path == NULL) {
3647 error("Too few arguments for verify: "
3648 "missing signature file");
3651 if (!have_identity) {
3652 error("Too few arguments for sign: "
3653 "missing allowed keys file");
3656 if (cert_key_id == NULL) {
3657 error("Too few arguments for verify: "
3658 "missing principal identity");
3661 return sig_verify(ca_key_path, cert_principals,
3662 cert_key_id, identity_file, rr_hostname,
3665 error("Unsupported operation for -Y: \"%s\"", sign_op);
3670 if (ca_key_path != NULL) {
3671 if (argc < 1 && !gen_krl) {
3672 error("Too few arguments.");
3675 } else if (argc > 0 && !gen_krl && !check_krl &&
3676 !do_gen_candidates && !do_screen_candidates) {
3677 error("Too many arguments.");
3680 if (change_passphrase && change_comment) {
3681 error("Can only have one of -p and -c.");
3684 if (print_fingerprint && (delete_host || hash_hosts)) {
3685 error("Cannot use -l with -H or -R.");
3689 do_gen_krl(pw, update_krl, ca_key_path,
3690 cert_serial, identity_comment, argc, argv);
3694 do_check_krl(pw, print_fingerprint, argc, argv);
3697 if (ca_key_path != NULL) {
3698 if (cert_key_id == NULL)
3699 fatal("Must specify key id (-I) when certifying");
3700 for (i = 0; i < nopts; i++)
3701 add_cert_option(opts[i]);
3702 do_ca_sign(pw, ca_key_path, prefer_agent,
3703 cert_serial, cert_serial_autoinc, argc, argv);
3707 if (delete_host || hash_hosts || find_host) {
3708 do_known_hosts(pw, rr_hostname, find_host,
3709 delete_host, hash_hosts);
3711 if (pkcs11provider != NULL)
3714 for (i = 0; i < nopts; i++) {
3715 if (strncasecmp(opts[i], "device=", 7) == 0) {
3716 sk_device = xstrdup(opts[i] + 7);
3718 fatal("Option \"%s\" is unsupported for "
3719 "FIDO authenticator download", opts[i]);
3722 return do_download_sk(sk_provider, sk_device);
3724 if (print_fingerprint || print_bubblebabble)
3726 if (change_passphrase)
3727 do_change_passphrase(pw);
3729 do_change_comment(pw, identity_comment);
3734 do_convert_from(pw);
3735 #else /* WITH_OPENSSL */
3736 if (convert_to || convert_from)
3737 fatal("key conversion disabled at compile time");
3738 #endif /* WITH_OPENSSL */
3740 do_print_public(pw);
3741 if (rr_hostname != NULL) {
3744 if (have_identity) {
3745 n = do_print_resource_record(pw, identity_file,
3746 rr_hostname, print_generic, opts, nopts);
3748 fatal("%s: %s", identity_file, strerror(errno));
3752 n += do_print_resource_record(pw,
3753 _PATH_HOST_RSA_KEY_FILE, rr_hostname,
3754 print_generic, opts, nopts);
3755 n += do_print_resource_record(pw,
3756 _PATH_HOST_DSA_KEY_FILE, rr_hostname,
3757 print_generic, opts, nopts);
3758 n += do_print_resource_record(pw,
3759 _PATH_HOST_ECDSA_KEY_FILE, rr_hostname,
3760 print_generic, opts, nopts);
3761 n += do_print_resource_record(pw,
3762 _PATH_HOST_ED25519_KEY_FILE, rr_hostname,
3763 print_generic, opts, nopts);
3764 n += do_print_resource_record(pw,
3765 _PATH_HOST_XMSS_KEY_FILE, rr_hostname,
3766 print_generic, opts, nopts);
3768 fatal("no keys found.");
3773 if (do_gen_candidates || do_screen_candidates) {
3775 fatal("No output file specified");
3777 fatal("Too many output files specified");
3779 if (do_gen_candidates) {
3780 do_moduli_gen(argv[0], opts, nopts);
3783 if (do_screen_candidates) {
3784 do_moduli_screen(argv[0], opts, nopts);
3788 if (gen_all_hostkeys) {
3789 do_gen_all_hostkeys(pw);
3793 if (key_type_name == NULL)
3794 key_type_name = DEFAULT_KEY_TYPE_NAME;
3796 type = sshkey_type_from_name(key_type_name);
3797 type_bits_valid(type, key_type_name, &bits);
3800 printf("Generating public/private %s key pair.\n",
3804 case KEY_ED25519_SK:
3805 for (i = 0; i < nopts; i++) {
3806 if (strcasecmp(opts[i], "no-touch-required") == 0) {
3807 sk_flags &= ~SSH_SK_USER_PRESENCE_REQD;
3808 } else if (strcasecmp(opts[i], "verify-required") == 0) {
3809 sk_flags |= SSH_SK_USER_VERIFICATION_REQD;
3810 } else if (strcasecmp(opts[i], "resident") == 0) {
3811 sk_flags |= SSH_SK_RESIDENT_KEY;
3812 } else if (strncasecmp(opts[i], "device=", 7) == 0) {
3813 sk_device = xstrdup(opts[i] + 7);
3814 } else if (strncasecmp(opts[i], "user=", 5) == 0) {
3815 sk_user = xstrdup(opts[i] + 5);
3816 } else if (strncasecmp(opts[i], "challenge=", 10) == 0) {
3817 if ((r = sshbuf_load_file(opts[i] + 10,
3818 &challenge)) != 0) {
3819 fatal_r(r, "Unable to load FIDO "
3820 "enrollment challenge \"%s\"",
3823 } else if (strncasecmp(opts[i],
3824 "write-attestation=", 18) == 0) {
3825 sk_attestation_path = opts[i] + 18;
3826 } else if (strncasecmp(opts[i],
3827 "application=", 12) == 0) {
3828 sk_application = xstrdup(opts[i] + 12);
3829 if (strncmp(sk_application, "ssh:", 4) != 0) {
3830 fatal("FIDO application string must "
3831 "begin with \"ssh:\"");
3834 fatal("Option \"%s\" is unsupported for "
3835 "FIDO authenticator enrollment", opts[i]);
3838 if ((attest = sshbuf_new()) == NULL)
3839 fatal("sshbuf_new failed");
3843 printf("You may need to touch your "
3844 "authenticator%s to authorize key "
3846 r == 0 ? "" : " again");
3849 r = sshsk_enroll(type, sk_provider, sk_device,
3850 sk_application == NULL ? "ssh:" : sk_application,
3851 sk_user, sk_flags, passphrase, challenge,
3855 if (r == SSH_ERR_KEY_BAD_PERMISSIONS &&
3856 (sk_flags & SSH_SK_RESIDENT_KEY) != 0 &&
3857 (sk_flags & SSH_SK_FORCE_OPERATION) == 0 &&
3858 confirm_sk_overwrite(sk_application, sk_user)) {
3859 sk_flags |= SSH_SK_FORCE_OPERATION;
3862 if (r != SSH_ERR_KEY_WRONG_PASSPHRASE)
3863 fatal_r(r, "Key enrollment failed");
3864 else if (passphrase != NULL) {
3865 error("PIN incorrect");
3866 freezero(passphrase, strlen(passphrase));
3870 fatal("Too many incorrect PINs");
3871 passphrase = read_passphrase("Enter PIN for "
3872 "authenticator: ", RP_ALLOW_STDIN);
3874 if (passphrase != NULL) {
3875 freezero(passphrase, strlen(passphrase));
3880 if ((r = sshkey_generate(type, bits, &private)) != 0)
3881 fatal("sshkey_generate failed");
3884 if ((r = sshkey_from_private(private, &public)) != 0)
3885 fatal_r(r, "sshkey_from_private");
3888 ask_filename(pw, "Enter file in which to save the key");
3890 /* Create ~/.ssh directory if it doesn't already exist. */
3891 hostfile_create_user_ssh_dir(identity_file, !quiet);
3893 /* If the file already exists, ask the user to confirm. */
3894 if (!confirm_overwrite(identity_file))
3897 /* Determine the passphrase for the private key */
3898 passphrase = private_key_passphrase();
3899 if (identity_comment) {
3900 strlcpy(comment, identity_comment, sizeof(comment));
3902 /* Create default comment field for the passphrase. */
3903 snprintf(comment, sizeof comment, "%s@%s", pw->pw_name, hostname);
3906 /* Save the key with the given passphrase and comment. */
3907 if ((r = sshkey_save_private(private, identity_file, passphrase,
3908 comment, private_key_format, openssh_format_cipher, rounds)) != 0) {
3909 error_r(r, "Saving key \"%s\" failed", identity_file);
3910 freezero(passphrase, strlen(passphrase));
3913 freezero(passphrase, strlen(passphrase));
3914 sshkey_free(private);
3917 printf("Your identification has been saved in %s\n",
3921 strlcat(identity_file, ".pub", sizeof(identity_file));
3922 if ((r = sshkey_save_public(public, identity_file, comment)) != 0)
3923 fatal_r(r, "Unable to save public key to %s", identity_file);
3926 fp = sshkey_fingerprint(public, fingerprint_hash,
3928 ra = sshkey_fingerprint(public, fingerprint_hash,
3930 if (fp == NULL || ra == NULL)
3931 fatal("sshkey_fingerprint failed");
3932 printf("Your public key has been saved in %s\n",
3934 printf("The key fingerprint is:\n");
3935 printf("%s %s\n", fp, comment);
3936 printf("The key's randomart image is:\n");
3942 if (sk_attestation_path != NULL)
3943 save_attestation(attest, sk_attestation_path);
3945 sshbuf_free(attest);
3946 sshkey_free(public);