1ac26c
From 23985bac83fd50c8e29431009302b5442f985096 Mon Sep 17 00:00:00 2001
1ac26c
From: slontis <shane.lontis@oracle.com>
1ac26c
Date: Wed, 11 Jan 2023 11:05:04 +1000
1ac26c
Subject: [PATCH 10/18] Fix NULL deference when validating FFC public key.
1ac26c
1ac26c
Fixes CVE-2023-0217
1ac26c
1ac26c
When attempting to do a BN_Copy of params->p there was no NULL check.
1ac26c
Since BN_copy does not check for NULL this is a NULL reference.
1ac26c
1ac26c
As an aside BN_cmp() does do a NULL check, so there are other checks
1ac26c
that fail because a NULL is passed. A more general check for NULL params
1ac26c
has been added for both FFC public and private key validation instead.
1ac26c
1ac26c
Reviewed-by: Matt Caswell <matt@openssl.org>
1ac26c
Reviewed-by: Paul Dale <pauli@openssl.org>
1ac26c
Reviewed-by: Tomas Mraz <tomas@openssl.org>
1ac26c
---
1ac26c
 crypto/ffc/ffc_key_validate.c |  9 +++++++++
1ac26c
 include/internal/ffc.h        |  1 +
1ac26c
 test/ffc_internal_test.c      | 31 +++++++++++++++++++++++++++++++
1ac26c
 3 files changed, 41 insertions(+)
1ac26c
1ac26c
diff --git a/crypto/ffc/ffc_key_validate.c b/crypto/ffc/ffc_key_validate.c
1ac26c
index 9f6525a2c8..442303e4b3 100644
1ac26c
--- a/crypto/ffc/ffc_key_validate.c
1ac26c
+++ b/crypto/ffc/ffc_key_validate.c
1ac26c
@@ -24,6 +24,11 @@ int ossl_ffc_validate_public_key_partial(const FFC_PARAMS *params,
1ac26c
     BN_CTX *ctx = NULL;
1ac26c
 
1ac26c
     *ret = 0;
1ac26c
+    if (params == NULL || pub_key == NULL || params->p == NULL) {
1ac26c
+        *ret = FFC_ERROR_PASSED_NULL_PARAM;
1ac26c
+        return 0;
1ac26c
+    }
1ac26c
+
1ac26c
     ctx = BN_CTX_new_ex(NULL);
1ac26c
     if (ctx == NULL)
1ac26c
         goto err;
1ac26c
@@ -107,6 +112,10 @@ int ossl_ffc_validate_private_key(const BIGNUM *upper, const BIGNUM *priv,
1ac26c
 
1ac26c
     *ret = 0;
1ac26c
 
1ac26c
+    if (priv == NULL || upper == NULL) {
1ac26c
+        *ret = FFC_ERROR_PASSED_NULL_PARAM;
1ac26c
+        goto err;
1ac26c
+    }
1ac26c
     if (BN_cmp(priv, BN_value_one()) < 0) {
1ac26c
         *ret |= FFC_ERROR_PRIVKEY_TOO_SMALL;
1ac26c
         goto err;
1ac26c
diff --git a/include/internal/ffc.h b/include/internal/ffc.h
1ac26c
index 732514a6c2..b8b7140857 100644
1ac26c
--- a/include/internal/ffc.h
1ac26c
+++ b/include/internal/ffc.h
1ac26c
@@ -76,6 +76,7 @@
1ac26c
 # define FFC_ERROR_NOT_SUITABLE_GENERATOR 0x08
1ac26c
 # define FFC_ERROR_PRIVKEY_TOO_SMALL      0x10
1ac26c
 # define FFC_ERROR_PRIVKEY_TOO_LARGE      0x20
1ac26c
+# define FFC_ERROR_PASSED_NULL_PARAM      0x40
1ac26c
 
1ac26c
 /*
1ac26c
  * Finite field cryptography (FFC) domain parameters are used by DH and DSA.
1ac26c
diff --git a/test/ffc_internal_test.c b/test/ffc_internal_test.c
1ac26c
index 2c97293573..9f67bd29b9 100644
1ac26c
--- a/test/ffc_internal_test.c
1ac26c
+++ b/test/ffc_internal_test.c
1ac26c
@@ -510,6 +510,27 @@ static int ffc_public_validate_test(void)
1ac26c
     if (!TEST_true(ossl_ffc_validate_public_key(params, pub, &res)))
1ac26c
         goto err;
1ac26c
 
1ac26c
+    /* Fail if params is NULL */
1ac26c
+    if (!TEST_false(ossl_ffc_validate_public_key(NULL, pub, &res)))
1ac26c
+        goto err;
1ac26c
+    if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
1ac26c
+        goto err;
1ac26c
+    res = -1;
1ac26c
+    /* Fail if pubkey is NULL */
1ac26c
+    if (!TEST_false(ossl_ffc_validate_public_key(params, NULL, &res)))
1ac26c
+        goto err;
1ac26c
+    if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
1ac26c
+        goto err;
1ac26c
+    res = -1;
1ac26c
+
1ac26c
+    BN_free(params->p);
1ac26c
+    params->p = NULL;
1ac26c
+    /* Fail if params->p is NULL */
1ac26c
+    if (!TEST_false(ossl_ffc_validate_public_key(params, pub, &res)))
1ac26c
+        goto err;
1ac26c
+    if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
1ac26c
+        goto err;
1ac26c
+
1ac26c
     ret = 1;
1ac26c
 err:
1ac26c
     DH_free(dh);
1ac26c
@@ -567,6 +588,16 @@ static int ffc_private_validate_test(void)
1ac26c
     if (!TEST_true(ossl_ffc_validate_private_key(params->q, priv, &res)))
1ac26c
         goto err;
1ac26c
 
1ac26c
+    if (!TEST_false(ossl_ffc_validate_private_key(NULL, priv, &res)))
1ac26c
+        goto err;
1ac26c
+    if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
1ac26c
+        goto err;
1ac26c
+    res = -1;
1ac26c
+    if (!TEST_false(ossl_ffc_validate_private_key(params->q, NULL, &res)))
1ac26c
+        goto err;
1ac26c
+    if (!TEST_int_eq(FFC_ERROR_PASSED_NULL_PARAM, res))
1ac26c
+        goto err;
1ac26c
+
1ac26c
     ret = 1;
1ac26c
 err:
1ac26c
     DH_free(dh);
1ac26c
-- 
1ac26c
2.39.1
1ac26c
1ac26c
From c1b4467a7cc129a74fc5205b80a5c47556b99416 Mon Sep 17 00:00:00 2001
1ac26c
From: Tomas Mraz <tomas@openssl.org>
1ac26c
Date: Fri, 13 Jan 2023 17:57:59 +0100
1ac26c
Subject: [PATCH 11/18] Prevent creating DSA and DH keys without parameters
1ac26c
 through import
1ac26c
1ac26c
Reviewed-by: Matt Caswell <matt@openssl.org>
1ac26c
Reviewed-by: Paul Dale <pauli@openssl.org>
1ac26c
---
1ac26c
 providers/implementations/keymgmt/dh_kmgmt.c  | 4 ++--
1ac26c
 providers/implementations/keymgmt/dsa_kmgmt.c | 5 +++--
1ac26c
 2 files changed, 5 insertions(+), 4 deletions(-)
1ac26c
1ac26c
diff --git a/providers/implementations/keymgmt/dh_kmgmt.c b/providers/implementations/keymgmt/dh_kmgmt.c
1ac26c
index 58a5fd009f..c2d87b4a7f 100644
1ac26c
--- a/providers/implementations/keymgmt/dh_kmgmt.c
1ac26c
+++ b/providers/implementations/keymgmt/dh_kmgmt.c
1ac26c
@@ -198,8 +198,8 @@ static int dh_import(void *keydata, int selection, const OSSL_PARAM params[])
1ac26c
     if ((selection & DH_POSSIBLE_SELECTIONS) == 0)
1ac26c
         return 0;
1ac26c
 
1ac26c
-    if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
1ac26c
-        ok = ok && ossl_dh_params_fromdata(dh, params);
1ac26c
+    /* a key without parameters is meaningless */
1ac26c
+    ok = ok && ossl_dh_params_fromdata(dh, params);
1ac26c
 
1ac26c
     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
1ac26c
         int include_private =
1ac26c
diff --git a/providers/implementations/keymgmt/dsa_kmgmt.c b/providers/implementations/keymgmt/dsa_kmgmt.c
1ac26c
index 100e917167..881680c085 100644
1ac26c
--- a/providers/implementations/keymgmt/dsa_kmgmt.c
1ac26c
+++ b/providers/implementations/keymgmt/dsa_kmgmt.c
1ac26c
@@ -199,8 +199,9 @@ static int dsa_import(void *keydata, int selection, const OSSL_PARAM params[])
1ac26c
     if ((selection & DSA_POSSIBLE_SELECTIONS) == 0)
1ac26c
         return 0;
1ac26c
 
1ac26c
-    if ((selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0)
1ac26c
-        ok = ok && ossl_dsa_ffc_params_fromdata(dsa, params);
1ac26c
+    /* a key without parameters is meaningless */
1ac26c
+    ok = ok && ossl_dsa_ffc_params_fromdata(dsa, params);
1ac26c
+
1ac26c
     if ((selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
1ac26c
         int include_private =
1ac26c
             selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY ? 1 : 0;
1ac26c
-- 
1ac26c
2.39.1
1ac26c
1ac26c
From fab4973801bdc11c29c4c8ccf65cf39cbc63ce9b Mon Sep 17 00:00:00 2001
1ac26c
From: Tomas Mraz <tomas@openssl.org>
1ac26c
Date: Fri, 13 Jan 2023 17:59:52 +0100
1ac26c
Subject: [PATCH 12/18] Do not create DSA keys without parameters by decoder
1ac26c
1ac26c
Reviewed-by: Matt Caswell <matt@openssl.org>
1ac26c
Reviewed-by: Paul Dale <pauli@openssl.org>
1ac26c
---
1ac26c
 crypto/x509/x_pubkey.c                        | 24 +++++++++++++++++++
1ac26c
 include/crypto/x509.h                         |  3 +++
1ac26c
 .../encode_decode/decode_der2key.c            |  2 +-
1ac26c
 3 files changed, 28 insertions(+), 1 deletion(-)
1ac26c
1ac26c
diff --git a/crypto/x509/x_pubkey.c b/crypto/x509/x_pubkey.c
1ac26c
index bc90ddd89b..77790faa1f 100644
1ac26c
--- a/crypto/x509/x_pubkey.c
1ac26c
+++ b/crypto/x509/x_pubkey.c
1ac26c
@@ -745,6 +745,30 @@ DSA *d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
1ac26c
     return key;
1ac26c
 }
1ac26c
 
1ac26c
+/* Called from decoders; disallows provided DSA keys without parameters. */
1ac26c
+DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length)
1ac26c
+{
1ac26c
+    DSA *key = NULL;
1ac26c
+    const unsigned char *data;
1ac26c
+    const BIGNUM *p, *q, *g;
1ac26c
+
1ac26c
+    data = *pp;
1ac26c
+    key = d2i_DSA_PUBKEY(NULL, &data, length);
1ac26c
+    if (key == NULL)
1ac26c
+        return NULL;
1ac26c
+    DSA_get0_pqg(key, &p, &q, &g);
1ac26c
+    if (p == NULL || q == NULL || g == NULL) {
1ac26c
+        DSA_free(key);
1ac26c
+        return NULL;
1ac26c
+    }
1ac26c
+    *pp = data;
1ac26c
+    if (a != NULL) {
1ac26c
+        DSA_free(*a);
1ac26c
+        *a = key;
1ac26c
+    }
1ac26c
+    return key;
1ac26c
+}
1ac26c
+
1ac26c
 int i2d_DSA_PUBKEY(const DSA *a, unsigned char **pp)
1ac26c
 {
1ac26c
     EVP_PKEY *pktmp;
1ac26c
diff --git a/include/crypto/x509.h b/include/crypto/x509.h
1ac26c
index 1f00178e89..0c42730ee9 100644
1ac26c
--- a/include/crypto/x509.h
1ac26c
+++ b/include/crypto/x509.h
1ac26c
@@ -339,6 +339,9 @@ void ossl_X509_PUBKEY_INTERNAL_free(X509_PUBKEY *xpub);
1ac26c
 
1ac26c
 RSA *ossl_d2i_RSA_PSS_PUBKEY(RSA **a, const unsigned char **pp, long length);
1ac26c
 int ossl_i2d_RSA_PSS_PUBKEY(const RSA *a, unsigned char **pp);
1ac26c
+# ifndef OPENSSL_NO_DSA
1ac26c
+DSA *ossl_d2i_DSA_PUBKEY(DSA **a, const unsigned char **pp, long length);
1ac26c
+# endif /* OPENSSL_NO_DSA */
1ac26c
 # ifndef OPENSSL_NO_DH
1ac26c
 DH *ossl_d2i_DH_PUBKEY(DH **a, const unsigned char **pp, long length);
1ac26c
 int ossl_i2d_DH_PUBKEY(const DH *a, unsigned char **pp);
1ac26c
diff --git a/providers/implementations/encode_decode/decode_der2key.c b/providers/implementations/encode_decode/decode_der2key.c
1ac26c
index ebc2d24833..d6ad738ef3 100644
1ac26c
--- a/providers/implementations/encode_decode/decode_der2key.c
1ac26c
+++ b/providers/implementations/encode_decode/decode_der2key.c
1ac26c
@@ -374,7 +374,7 @@ static void *dsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
1ac26c
                              (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8);
1ac26c
 }
1ac26c
 
1ac26c
-# define dsa_d2i_PUBKEY                 (d2i_of_void *)d2i_DSA_PUBKEY
1ac26c
+# define dsa_d2i_PUBKEY                 (d2i_of_void *)ossl_d2i_DSA_PUBKEY
1ac26c
 # define dsa_free                       (free_key_fn *)DSA_free
1ac26c
 # define dsa_check                      NULL
1ac26c
 
1ac26c
-- 
1ac26c
2.39.1
1ac26c
1ac26c
From 7e37185582995b35f885fec9dcc3670af9ffcbef Mon Sep 17 00:00:00 2001
1ac26c
From: Tomas Mraz <tomas@openssl.org>
1ac26c
Date: Fri, 13 Jan 2023 18:46:15 +0100
1ac26c
Subject: [PATCH 13/18] Add test for DSA pubkey without param import and check
1ac26c
1ac26c
Reviewed-by: Matt Caswell <matt@openssl.org>
1ac26c
Reviewed-by: Paul Dale <pauli@openssl.org>
1ac26c
---
1ac26c
 test/recipes/91-test_pkey_check.t             |  48 ++++++++++++++----
1ac26c
 .../91-test_pkey_check_data/dsapub.pem        |  12 +++++
1ac26c
 .../dsapub_noparam.der                        | Bin 0 -> 108 bytes
1ac26c
 3 files changed, 49 insertions(+), 11 deletions(-)
1ac26c
 create mode 100644 test/recipes/91-test_pkey_check_data/dsapub.pem
1ac26c
 create mode 100644 test/recipes/91-test_pkey_check_data/dsapub_noparam.der
1ac26c
1ac26c
diff --git a/test/recipes/91-test_pkey_check.t b/test/recipes/91-test_pkey_check.t
1ac26c
index 612a3e3d6c..015d7805db 100644
1ac26c
--- a/test/recipes/91-test_pkey_check.t
1ac26c
+++ b/test/recipes/91-test_pkey_check.t
1ac26c
@@ -11,19 +11,24 @@ use strict;
1ac26c
 use warnings;
1ac26c
 
1ac26c
 use File::Spec;
1ac26c
-use OpenSSL::Test qw/:DEFAULT data_file/;
1ac26c
+use OpenSSL::Test qw/:DEFAULT data_file with/;
1ac26c
 use OpenSSL::Test::Utils;
1ac26c
 
1ac26c
 sub pkey_check {
1ac26c
     my $f = shift;
1ac26c
+    my $pubcheck = shift;
1ac26c
+    my @checkopt = ('-check');
1ac26c
 
1ac26c
-    return run(app(['openssl', 'pkey', '-check', '-text',
1ac26c
+    @checkopt = ('-pubcheck', '-pubin') if $pubcheck;
1ac26c
+
1ac26c
+    return run(app(['openssl', 'pkey', @checkopt, '-text',
1ac26c
                     '-in', $f]));
1ac26c
 }
1ac26c
 
1ac26c
 sub check_key {
1ac26c
     my $f = shift;
1ac26c
     my $should_fail = shift;
1ac26c
+    my $pubcheck = shift;
1ac26c
     my $str;
1ac26c
 
1ac26c
 
1ac26c
@@ -33,11 +38,10 @@ sub check_key {
1ac26c
     $f = data_file($f);
1ac26c
 
1ac26c
     if ( -s $f ) {
1ac26c
-        if ($should_fail) {
1ac26c
-            ok(!pkey_check($f), $str);
1ac26c
-        } else {
1ac26c
-            ok(pkey_check($f), $str);
1ac26c
-        }
1ac26c
+        with({ exit_checker => sub { return shift == $should_fail; } },
1ac26c
+            sub {
1ac26c
+                ok(pkey_check($f, $pubcheck), $str);
1ac26c
+            });
1ac26c
     } else {
1ac26c
         fail("Missing file $f");
1ac26c
     }
1ac26c
@@ -66,15 +70,37 @@ push(@positive_tests, (
1ac26c
     "dhpkey.pem"
1ac26c
     )) unless disabled("dh");
1ac26c
 
1ac26c
+my @negative_pubtests = ();
1ac26c
+
1ac26c
+push(@negative_pubtests, (
1ac26c
+    "dsapub_noparam.der"
1ac26c
+    )) unless disabled("dsa");
1ac26c
+
1ac26c
+my @positive_pubtests = ();
1ac26c
+
1ac26c
+push(@positive_pubtests, (
1ac26c
+    "dsapub.pem"
1ac26c
+    )) unless disabled("dsa");
1ac26c
+
1ac26c
 plan skip_all => "No tests within the current enabled feature set"
1ac26c
-    unless @negative_tests && @positive_tests;
1ac26c
+    unless @negative_tests && @positive_tests
1ac26c
+           && @negative_pubtests && @positive_pubtests;
1ac26c
 
1ac26c
-plan tests => scalar(@negative_tests) + scalar(@positive_tests);
1ac26c
+plan tests => scalar(@negative_tests) + scalar(@positive_tests)
1ac26c
+              + scalar(@negative_pubtests) + scalar(@positive_pubtests);
1ac26c
 
1ac26c
 foreach my $t (@negative_tests) {
1ac26c
-    check_key($t, 1);
1ac26c
+    check_key($t, 1, 0);
1ac26c
 }
1ac26c
 
1ac26c
 foreach my $t (@positive_tests) {
1ac26c
-    check_key($t, 0);
1ac26c
+    check_key($t, 0, 0);
1ac26c
+}
1ac26c
+
1ac26c
+foreach my $t (@negative_pubtests) {
1ac26c
+    check_key($t, 1, 1);
1ac26c
+}
1ac26c
+
1ac26c
+foreach my $t (@positive_pubtests) {
1ac26c
+    check_key($t, 0, 1);
1ac26c
 }
1ac26c
diff --git a/test/recipes/91-test_pkey_check_data/dsapub.pem b/test/recipes/91-test_pkey_check_data/dsapub.pem
1ac26c
new file mode 100644
1ac26c
index 0000000000..0ff4bd83ed
1ac26c
--- /dev/null
1ac26c
+++ b/test/recipes/91-test_pkey_check_data/dsapub.pem
1ac26c
@@ -0,0 +1,12 @@
1ac26c
+-----BEGIN PUBLIC KEY-----
1ac26c
+MIIBvzCCATQGByqGSM44BAEwggEnAoGBAIjbXpOVVciVNuagg26annKkghIIZFI4
1ac26c
+4WdMomnV+I/oXyxHbZTBBBpW9xy/E1+yMjbp4GmX+VxyDj3WxUWxXllzL+miEkzD
1ac26c
+9Xz638VzIBhjFbMvk1/N4kS4bKVUd9yk7HfvYzAdnRphk0WI+RoDiDrBNPPxSoQD
1ac26c
+CEWgvwgsLIDhAh0A6dbz1IQpQwGF4+Ca28x6OO+UfJJv3ggeZ++fNwKBgQCA9XKV
1ac26c
+lRrTY8ALBxS0KbZjpaIXuUj5nr3i1lIDyP3ISksDF0ekyLtn6eK9VijX6Pm65Np+
1ac26c
+4ic9Nr5WKLKhPaUSpLNRx1gDqo3sd92hYgiEUifzEuhLYfK/CsgFED+l2hDXtJUq
1ac26c
+bISNSHVwI5lsyNXLu7HI1Fk8F5UO3LqsboFAngOBhAACgYATxFY89nEYcUhgHGgr
1ac26c
+YDHhXBQfMKnTKYdvon4DN7WQ9ip+t4VUsLpTD1ZE9zrM2R/B04+8C6KGoViwyeER
1ac26c
+kS4dxWOkX71x4X2DlNpYevcR53tNcTDqmMD7YKfDDmrb0lftMyfW8aESaiymVMys
1ac26c
+DRjhKHBjdo0rZeSM8DAk3ctrXA==
1ac26c
+-----END PUBLIC KEY-----
1ac26c
diff --git a/test/recipes/91-test_pkey_check_data/dsapub_noparam.der b/test/recipes/91-test_pkey_check_data/dsapub_noparam.der
1ac26c
new file mode 100644
1ac26c
index 0000000000000000000000000000000000000000..b8135f1ca94da914b6829421e0c13f6daa731862
1ac26c
GIT binary patch
1ac26c
literal 108
1ac26c
zcmXpIGT>xm*J|@PXTieE%*wz71
1ac26c
U3o&W4F|x9<gY>|F5F-Nv0Bz9(=Kufz
1ac26c
1ac26c
literal 0
1ac26c
HcmV?d00001
1ac26c
1ac26c
-- 
1ac26c
2.39.1
1ac26c
1ac26c
From 2ad9928170768653d19d81881deabc5f9c1665c0 Mon Sep 17 00:00:00 2001
1ac26c
From: Tomas Mraz <tomas@openssl.org>
1ac26c
Date: Fri, 3 Feb 2023 14:57:04 +0100
1ac26c
Subject: [PATCH 18/18] Internaly declare the DSA type for no-deprecated builds
1ac26c
1ac26c
Reviewed-by: Hugo Landau <hlandau@openssl.org>
1ac26c
Reviewed-by: Richard Levitte <levitte@openssl.org>
1ac26c
(cherry picked from commit 7a21a1b5fa2dac438892cf3292d1f9c445d870d9)
1ac26c
---
1ac26c
 include/crypto/types.h | 3 +++
1ac26c
 1 file changed, 3 insertions(+)
1ac26c
1ac26c
diff --git a/include/crypto/types.h b/include/crypto/types.h
1ac26c
index 0d81404091..0a75f03a3f 100644
1ac26c
--- a/include/crypto/types.h
1ac26c
+++ b/include/crypto/types.h
1ac26c
@@ -20,6 +20,9 @@ typedef struct rsa_meth_st RSA_METHOD;
1ac26c
 typedef struct ec_key_st EC_KEY;
1ac26c
 typedef struct ec_key_method_st EC_KEY_METHOD;
1ac26c
 #  endif
1ac26c
+#  ifndef OPENSSL_NO_DSA
1ac26c
+typedef struct dsa_st DSA;
1ac26c
+#  endif
1ac26c
 # endif
1ac26c
 
1ac26c
 # ifndef OPENSSL_NO_EC
1ac26c
-- 
1ac26c
2.39.1
1ac26c