From 94f3898f43a7ef0c53dd50c60ce6d6f884de28e1 Mon Sep 17 00:00:00 2001 Date: Tue, 20 Aug 2024 20:17:01 +0800 Subject: [PATCH] backport-openssh-7.7p1-fips Reference:https://src.fedoraproject.org/rpms/openssh/blob/rawhide/f/backport-openssh-7.7p1-fips.patch Conflict:NA --- cipher-aes.c | 3 +- dh.c | 41 +++++++++++++++++++++++++++ dh.h | 1 + kex.c | 6 +++- kexgexc.c | 5 ++++ myproposal.h | 33 ++++++++++++++++++++++ readconf.c | 16 +++++++---- sandbox-seccomp-filter.c | 3 ++ servconf.c | 16 +++++++---- ssh-keygen.c | 17 ++++++++++- ssh-rsa.c | 3 ++ ssh.c | 5 ++++ sshconnect2.c | 61 ++++++++++++++++++++++------------------ sshd.c | 19 ++++++++++--- sshkey.c | 1 + 15 files changed, 186 insertions(+), 44 deletions(-) diff --git a/cipher-aes.c b/cipher-aes.c index 8b10172..1a07697 100644 --- a/cipher-aes.c +++ b/cipher-aes.c @@ -154,7 +154,8 @@ evp_rijndael(void) rijndal_cbc.do_cipher = ssh_rijndael_cbc; #ifndef SSH_OLD_EVP rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | - EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV; + EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV | + EVP_CIPH_FLAG_FIPS; #endif return (&rijndal_cbc); } diff --git a/dh.c b/dh.c index ce2eb47..166cb02 100644 --- a/dh.c +++ b/dh.c @@ -36,6 +36,7 @@ #include #include +#include #include "dh.h" #include "pathnames.h" @@ -164,6 +165,12 @@ choose_dh(int min, int wantbits, int max) int best, bestcount, which, linenum; struct dhgroup dhg; + if (FIPS_mode()) { + logit("Using arbitrary primes is not allowed in FIPS mode." + " Falling back to known groups."); + return (dh_new_group_fallback(max)); + } + if ((f = fopen(get_moduli_filename(), "r")) == NULL) { logit("WARNING: could not open %s (%s), using fixed modulus", get_moduli_filename(), strerror(errno)); @@ -502,4 +509,38 @@ dh_estimate(int bits) return 8192; } +/* + * Compares the received DH parameters with known-good groups, + * which might be either from group14, group16 or group18. + */ +int +dh_is_known_group(const DH *dh) +{ + const BIGNUM *p, *g; + const BIGNUM *known_p, *known_g; + DH *known = NULL; + int bits = 0, rv = 0; + + DH_get0_pqg(dh, &p, NULL, &g); + bits = BN_num_bits(p); + + if (bits <= 3072) { + known = dh_new_group14(); + } else if (bits <= 6144) { + known = dh_new_group16(); + } else { + known = dh_new_group18(); + } + + DH_get0_pqg(known, &known_p, NULL, &known_g); + + if (BN_cmp(g, known_g) == 0 && + BN_cmp(p, known_p) == 0) { + rv = 1; + } + + DH_free(known); + return rv; +} + #endif /* WITH_OPENSSL */ diff --git a/dh.h b/dh.h index c6326a3..e51e292 100644 --- a/dh.h +++ b/dh.h @@ -45,6 +45,7 @@ DH *dh_new_group_fallback(int); int dh_gen_key(DH *, int); int dh_pub_is_valid(const DH *, const BIGNUM *); +int dh_is_known_group(const DH *); u_int dh_estimate(int); void dh_set_moduli_file(const char *); diff --git a/kex.c b/kex.c index 36ae36c..1636f25 100644 --- a/kex.c +++ b/kex.c @@ -40,6 +40,7 @@ #ifdef WITH_OPENSSL #include #include +#include # ifdef HAVE_EVP_KDF_CTX_NEW_ID # include # endif @@ -205,7 +206,10 @@ kex_names_valid(const char *names) for ((p = strsep(&cp, ",")); p && *p != '\0'; (p = strsep(&cp, ","))) { if (kex_alg_by_name(p) == NULL) { - error("Unsupported KEX algorithm \"%.100s\"", p); + if (FIPS_mode()) + error("\"%.100s\" is not allowed in FIPS mode", p); + else + error("Unsupported KEX algorithm \"%.100s\"", p); free(s); return 0; } diff --git a/kexgexc.c b/kexgexc.c index e99e0cf..4c3feae 100644 --- a/kexgexc.c +++ b/kexgexc.c @@ -28,6 +28,7 @@ #ifdef WITH_OPENSSL +#include #include #include @@ -115,6 +116,10 @@ input_kex_dh_gex_group(int type, u_int32_t seq, struct ssh *ssh) r = SSH_ERR_ALLOC_FAIL; goto out; } + if (FIPS_mode() && dh_is_known_group(kex->dh) == 0) { + r = SSH_ERR_INVALID_ARGUMENT; + goto out; + } p = g = NULL; /* belong to kex->dh now */ /* generate and send 'e', client DH public key */ diff --git a/myproposal.h b/myproposal.h index ee6e9f7..ff8dfa8 100644 --- a/myproposal.h +++ b/myproposal.h @@ -56,6 +56,18 @@ "rsa-sha2-512," \ "rsa-sha2-256" +#define KEX_FIPS_PK_ALG \ + "ecdsa-sha2-nistp256-cert-v01@openssh.com," \ + "ecdsa-sha2-nistp384-cert-v01@openssh.com," \ + "ecdsa-sha2-nistp521-cert-v01@openssh.com," \ + "rsa-sha2-512-cert-v01@openssh.com," \ + "rsa-sha2-256-cert-v01@openssh.com," \ + "ecdsa-sha2-nistp256," \ + "ecdsa-sha2-nistp384," \ + "ecdsa-sha2-nistp521," \ + "rsa-sha2-512," \ + "rsa-sha2-256," \ + #define KEX_SERVER_ENCRYPT \ "chacha20-poly1305@openssh.com," \ "aes128-ctr,aes192-ctr,aes256-ctr," \ @@ -77,6 +89,27 @@ #define KEX_CLIENT_MAC KEX_SERVER_MAC +#define KEX_FIPS_ENCRYPT \ + "aes128-ctr,aes192-ctr,aes256-ctr," \ + "aes128-cbc,3des-cbc," \ + "aes192-cbc,aes256-cbc,rijndael-cbc@lysator.liu.se," \ + "aes128-gcm@openssh.com,aes256-gcm@openssh.com" +#define KEX_DEFAULT_KEX_FIPS \ + "ecdh-sha2-nistp256," \ + "ecdh-sha2-nistp384," \ + "ecdh-sha2-nistp521," \ + "diffie-hellman-group-exchange-sha256," \ + "diffie-hellman-group16-sha512," \ + "diffie-hellman-group18-sha512," \ + "diffie-hellman-group14-sha256" +#define KEX_FIPS_MAC \ + "hmac-sha1," \ + "hmac-sha2-256," \ + "hmac-sha2-512," \ + "hmac-sha1-etm@openssh.com," \ + "hmac-sha2-256-etm@openssh.com," \ + "hmac-sha2-512-etm@openssh.com" + /* Not a KEX value, but here so all the algorithm defaults are together */ #define SSH_ALLOWED_CA_SIGALGS \ "ssh-ed25519," \ diff --git a/readconf.c b/readconf.c index bd8627c..dd22c3c 100644 --- a/readconf.c +++ b/readconf.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -2711,11 +2712,16 @@ fill_default_options(Options * options) all_key = sshkey_alg_list(0, 0, 1, ','); all_sig = sshkey_alg_list(0, 1, 1, ','); /* remove unsupported algos from default lists */ - def_cipher = match_filter_allowlist(KEX_CLIENT_ENCRYPT, all_cipher); - def_mac = match_filter_allowlist(KEX_CLIENT_MAC, all_mac); - def_kex = match_filter_allowlist(KEX_CLIENT_KEX, all_kex); - def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key); - def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig); + def_cipher = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_ENCRYPT : KEX_CLIENT_ENCRYPT), all_cipher); + def_mac = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_MAC : KEX_CLIENT_MAC), all_mac); + def_kex = match_filter_allowlist((FIPS_mode() ? + KEX_DEFAULT_KEX_FIPS : KEX_CLIENT_KEX), all_kex); + def_key = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key); + def_sig = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig); #define ASSEMBLE(what, defaults, all) \ do { \ if ((r = kex_assemble_names(&options->what, \ diff --git a/sandbox-seccomp-filter.c b/sandbox-seccomp-filter.c index 139b6fb..5376800 100644 --- a/sandbox-seccomp-filter.c +++ b/sandbox-seccomp-filter.c @@ -230,6 +230,9 @@ static const struct sock_filter preauth_insns[] = { #ifdef __NR_open SC_DENY(__NR_open, EACCES), #endif +#ifdef __NR_socket + SC_DENY(__NR_socket, EACCES), +#endif #ifdef __NR_openat SC_DENY(__NR_openat, EACCES), #endif diff --git a/servconf.c b/servconf.c index 0dbf90c..24db0cc 100644 --- a/servconf.c +++ b/servconf.c @@ -26,6 +26,7 @@ #ifdef HAVE_NET_ROUTE_H #include #endif +#include #include #include @@ -230,11 +231,16 @@ assemble_algorithms(ServerOptions *o) all_key = sshkey_alg_list(0, 0, 1, ','); all_sig = sshkey_alg_list(0, 1, 1, ','); /* remove unsupported algos from default lists */ - def_cipher = match_filter_allowlist(KEX_SERVER_ENCRYPT, all_cipher); - def_mac = match_filter_allowlist(KEX_SERVER_MAC, all_mac); - def_kex = match_filter_allowlist(KEX_SERVER_KEX, all_kex); - def_key = match_filter_allowlist(KEX_DEFAULT_PK_ALG, all_key); - def_sig = match_filter_allowlist(SSH_ALLOWED_CA_SIGALGS, all_sig); + def_cipher = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_ENCRYPT : KEX_SERVER_ENCRYPT), all_cipher); + def_mac = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_MAC : KEX_SERVER_MAC), all_mac); + def_kex = match_filter_allowlist((FIPS_mode() ? + KEX_DEFAULT_KEX_FIPS : KEX_SERVER_KEX), all_kex); + def_key = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_PK_ALG : KEX_DEFAULT_PK_ALG), all_key); + def_sig = match_filter_allowlist((FIPS_mode() ? + KEX_FIPS_PK_ALG : SSH_ALLOWED_CA_SIGALGS), all_sig); #define ASSEMBLE(what, defaults, all) \ do { \ if ((r = kex_assemble_names(&o->what, defaults, all)) != 0) \ diff --git a/ssh-keygen.c b/ssh-keygen.c index 46f4998..5c48ee0 100644 --- a/ssh-keygen.c +++ b/ssh-keygen.c @@ -23,6 +23,7 @@ #include #include "openbsd-compat/openssl-compat.h" #endif +#include #ifdef HAVE_STDINT_H # include @@ -207,6 +208,12 @@ type_bits_valid(int type, const char *name, u_int32_t *bitsp) #endif } #ifdef WITH_OPENSSL + if (FIPS_mode()) { + if (type == KEY_DSA) + fatal("DSA keys are not allowed in FIPS mode"); + if (type == KEY_ED25519) + fatal("ED25519 keys are not allowed in FIPS mode"); + } switch (type) { case KEY_DSA: if (*bitsp != 1024) @@ -1113,9 +1120,17 @@ do_gen_all_hostkeys(struct passwd *pw) first = 1; printf("%s: generating new host keys: ", __progname); } + type = sshkey_type_from_name(key_types[i].key_type); + + /* Skip the keys that are not supported in FIPS mode */ + if (FIPS_mode() && (type == KEY_DSA || type == KEY_ED25519)) { + logit("Skipping %s key in FIPS mode", + key_types[i].key_type_display); + goto next; + } + printf("%s ", key_types[i].key_type_display); fflush(stdout); - type = sshkey_type_from_name(key_types[i].key_type); if ((fd = mkstemp(prv_tmp)) == -1) { error("Could not save your private key in %s: %s", prv_tmp, strerror(errno)); diff --git a/ssh-rsa.c b/ssh-rsa.c index 88a98fd..17662be 100644 --- a/ssh-rsa.c +++ b/ssh-rsa.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -162,6 +163,8 @@ ssh_rsa_generate(struct sshkey *k, int bits) goto out; if (EVP_PKEY_keygen(ctx, &res) <= 0) { + if (FIPS_mode()) + logit_f("the key length might be unsupported by FIPS mode approved key generation method"); ret = SSH_ERR_LIBCRYPTO_ERROR; goto out; } diff --git a/ssh.c b/ssh.c index 4caa6e1..d144a25 100644 --- a/ssh.c +++ b/ssh.c @@ -77,6 +77,7 @@ #include #include #endif +#include #include "openbsd-compat/openssl-compat.h" #include "openbsd-compat/sys-queue.h" @@ -1564,6 +1565,10 @@ main(int ac, char **av) exit(0); } + if (FIPS_mode()) { + debug("FIPS mode initialized"); + } + /* Expand SecurityKeyProvider if it refers to an environment variable */ if (options.sk_provider != NULL && *options.sk_provider == '$' && strlen(options.sk_provider) > 1) { diff --git a/sshconnect2.c b/sshconnect2.c index 1e217e4..1d72a91 100644 --- a/sshconnect2.c +++ b/sshconnect2.c @@ -45,6 +45,8 @@ #include #endif +#include + #include "openbsd-compat/sys-queue.h" #include "xmalloc.h" @@ -276,36 +278,41 @@ ssh_kex2(struct ssh *ssh, char *host, struct sockaddr *hostaddr, u_short port, #if defined(GSSAPI) && defined(WITH_OPENSSL) if (options.gss_keyex) { - /* Add the GSSAPI mechanisms currently supported on this - * client to the key exchange algorithm proposal */ - orig = myproposal[PROPOSAL_KEX_ALGS]; - - if (options.gss_server_identity) { - gss_host = xstrdup(options.gss_server_identity); - } else if (options.gss_trust_dns) { - gss_host = remote_hostname(ssh); - /* Fall back to specified host if we are using proxy command - * and can not use DNS on that socket */ - if (strcmp(gss_host, "UNKNOWN") == 0) { - free(gss_host); + if (FIPS_mode()) { + logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode"); + options.gss_keyex = 0; + } else { + /* Add the GSSAPI mechanisms currently supported on this + * client to the key exchange algorithm proposal */ + orig = myproposal[PROPOSAL_KEX_ALGS]; + + if (options.gss_server_identity) { + gss_host = xstrdup(options.gss_server_identity); + } else if (options.gss_trust_dns) { + gss_host = remote_hostname(ssh); + /* Fall back to specified host if we are using proxy command + * and can not use DNS on that socket */ + if (strcmp(gss_host, "UNKNOWN") == 0) { + free(gss_host); + gss_host = xstrdup(host); + } + } else { gss_host = xstrdup(host); } - } else { - gss_host = xstrdup(host); - } - gss = ssh_gssapi_client_mechanisms(gss_host, - options.gss_client_identity, options.gss_kex_algorithms); - if (gss) { - debug("Offering GSSAPI proposal: %s", gss); - xasprintf(&myproposal[PROPOSAL_KEX_ALGS], - "%s,%s", gss, orig); - - /* If we've got GSSAPI algorithms, then we also support the - * 'null' hostkey, as a last resort */ - orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]; - xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], - "%s,null", orig); + gss = ssh_gssapi_client_mechanisms(gss_host, + options.gss_client_identity, options.gss_kex_algorithms); + if (gss) { + debug("Offering GSSAPI proposal: %s", gss); + xasprintf(&myproposal[PROPOSAL_KEX_ALGS], + "%s,%s", gss, orig); + + /* If we've got GSSAPI algorithms, then we also support the + * 'null' hostkey, as a last resort */ + orig = myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]; + xasprintf(&myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS], + "%s,null", orig); + } } } #endif diff --git a/sshd.c b/sshd.c index 595e44a..f49e2e5 100644 --- a/sshd.c +++ b/sshd.c @@ -69,6 +69,7 @@ #endif #include #include +#include #include #include #include @@ -80,6 +81,7 @@ #include #include #include +#include #include "openbsd-compat/openssl-compat.h" #endif @@ -1665,6 +1667,7 @@ main(int ac, char **av) sigemptyset(&sigmask); sigprocmask(SIG_SETMASK, &sigmask, NULL); + OpenSSL_add_all_algorithms(); /* Save argv. Duplicate so setproctitle emulation doesn't clobber it */ saved_argc = ac; rexec_argc = ac; @@ -2160,6 +2163,10 @@ main(int ac, char **av) /* Reinitialize the log (because of the fork above). */ log_init(__progname, options.log_level, options.log_facility, log_stderr); + if (FIPS_mode()) { + debug("FIPS mode initialized"); + } + /* * Chdir to the root directory so that the current disk can be * unmounted if desired. @@ -2535,10 +2542,14 @@ do_ssh2_kex(struct ssh *ssh) if (strlen(myproposal[PROPOSAL_SERVER_HOST_KEY_ALGS]) == 0) orig = NULL; - if (options.gss_keyex) - gss = ssh_gssapi_server_mechanisms(); - else - gss = NULL; + if (options.gss_keyex) { + if (FIPS_mode()) { + logit("Disabling GSSAPIKeyExchange. Not usable in FIPS mode"); + options.gss_keyex = 0; + } else { + gss = ssh_gssapi_server_mechanisms(); + } + } if (gss && orig) xasprintf(&newstr, "%s,%s", gss, orig); diff --git a/sshkey.c b/sshkey.c index 1aee244..be2c399 100644 --- a/sshkey.c +++ b/sshkey.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #endif -- 2.33.0