Blame SOURCES/0016-add-tests-multiple-certs-same-id.patch

8ad293
From f633f37e712cb0f7524a2ee257e15f34468149b4 Mon Sep 17 00:00:00 2001
8ad293
From: Sumit Bose <sbose@redhat.com>
8ad293
Date: Tue, 3 Nov 2020 09:58:52 +0100
8ad293
Subject: [PATCH 16/16] add tests multiple certs same id
8ad293
8ad293
Add unit test for the case that two certificates use the same key.
8ad293
8ad293
Resolves: https://github.com/SSSD/sssd/issues/5400
8ad293
8ad293
Reviewed-by: Alexey Tikhonov <atikhono@redhat.com>
8ad293
---
8ad293
 src/tests/cmocka/test_pam_srv.c              | 116 +++++++++++++++++++
8ad293
 src/tests/test_CA/Makefile.am                |  26 ++++-
8ad293
 src/tests/test_CA/SSSD_test_cert_0006.config |  20 ++++
8ad293
 3 files changed, 161 insertions(+), 1 deletion(-)
8ad293
 create mode 100644 src/tests/test_CA/SSSD_test_cert_0006.config
8ad293
8ad293
diff --git a/src/tests/cmocka/test_pam_srv.c b/src/tests/cmocka/test_pam_srv.c
8ad293
index 5506fbf34..8ca5abd43 100644
8ad293
--- a/src/tests/cmocka/test_pam_srv.c
8ad293
+++ b/src/tests/cmocka/test_pam_srv.c
8ad293
@@ -40,12 +40,14 @@
8ad293
 #include "tests/test_CA/SSSD_test_cert_x509_0001.h"
8ad293
 #include "tests/test_CA/SSSD_test_cert_x509_0002.h"
8ad293
 #include "tests/test_CA/SSSD_test_cert_x509_0005.h"
8ad293
+#include "tests/test_CA/SSSD_test_cert_x509_0006.h"
8ad293
 
8ad293
 #include "tests/test_ECC_CA/SSSD_test_ECC_cert_x509_0001.h"
8ad293
 #else
8ad293
 #define SSSD_TEST_CERT_0001 ""
8ad293
 #define SSSD_TEST_CERT_0002 ""
8ad293
 #define SSSD_TEST_CERT_0005 ""
8ad293
+#define SSSD_TEST_CERT_0006 ""
8ad293
 
8ad293
 #define SSSD_TEST_ECC_CERT_0001 ""
8ad293
 #endif
8ad293
@@ -1093,6 +1095,13 @@ static int test_pam_creds_insufficient_check(uint32_t status,
8ad293
     return EOK;
8ad293
 }
8ad293
 
8ad293
+static int test_pam_auth_err_check(uint32_t status, uint8_t *body, size_t blen)
8ad293
+{
8ad293
+    /* PAM_AUTH_ERR is returned for different types of error, we use different
8ad293
+     * names for the check functions to make the purpose more clear. */
8ad293
+    return test_pam_wrong_pw_offline_auth_check(status, body, blen);
8ad293
+}
8ad293
+
8ad293
 static int test_pam_user_unknown_check(uint32_t status,
8ad293
                                        uint8_t *body, size_t blen)
8ad293
 {
8ad293
@@ -2500,6 +2509,107 @@ void test_pam_cert_auth_2certs_one_mapping(void **state)
8ad293
     assert_int_equal(ret, EOK);
8ad293
 }
8ad293
 
8ad293
+/* The following three tests cover a use case where multiple certificates are
8ad293
+ * using the same key-pair. According to PKCS#11 specs "The CKA_ID field is
8ad293
+ * intended to distinguish among multiple keys. In the case of public and
8ad293
+ * private keys, this field assists in handling multiple keys held by the same
8ad293
+ * subject; the key identifier for a public key and its corresponding private
8ad293
+ * key should be the same. The key identifier should also be the same as for
8ad293
+ * the corresponding certificate, if one exists. Cryptoki does not enforce
8ad293
+ * these associations, however." As a result certificates sharing the same
8ad293
+ * key-pair will have the same id on the Smartcard. This means a second
8ad293
+ * parameter is needed to distinguish them. We use the label here.
8ad293
+ *
8ad293
+ * The first test makes sure authentication fails is the label is missing, the
8ad293
+ * second and third test make sure that each certificate can be selected with
8ad293
+ * the proper label. */
8ad293
+void test_pam_cert_auth_2certs_same_id_no_label(void **state)
8ad293
+{
8ad293
+    int ret;
8ad293
+
8ad293
+    set_cert_auth_param(pam_test_ctx->pctx, CA_DB);
8ad293
+    putenv(discard_const("SOFTHSM2_CONF=" ABS_BUILD_DIR "/src/tests/test_CA/softhsm2_2certs_same_id.conf"));
8ad293
+
8ad293
+    mock_input_pam_cert(pam_test_ctx, "pamuser", "123456", "SSSD Test Token",
8ad293
+                        TEST_MODULE_NAME,
8ad293
+                        "11111111",
8ad293
+                        NULL, NULL,
8ad293
+                        NULL, SSSD_TEST_CERT_0001);
8ad293
+
8ad293
+    will_return(__wrap_sss_packet_get_cmd, SSS_PAM_AUTHENTICATE);
8ad293
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
8ad293
+
8ad293
+    /* Assume backend cannot handle Smartcard credentials */
8ad293
+    pam_test_ctx->exp_pam_status = PAM_BAD_ITEM;
8ad293
+
8ad293
+    set_cmd_cb(test_pam_auth_err_check);
8ad293
+    ret = sss_cmd_execute(pam_test_ctx->cctx, SSS_PAM_AUTHENTICATE,
8ad293
+                          pam_test_ctx->pam_cmds);
8ad293
+    assert_int_equal(ret, EOK);
8ad293
+
8ad293
+    /* Wait until the test finishes with EOK */
8ad293
+    ret = test_ev_loop(pam_test_ctx->tctx);
8ad293
+    assert_int_equal(ret, EOK);
8ad293
+}
8ad293
+
8ad293
+void test_pam_cert_auth_2certs_same_id_with_label_1(void **state)
8ad293
+{
8ad293
+    int ret;
8ad293
+
8ad293
+    set_cert_auth_param(pam_test_ctx->pctx, CA_DB);
8ad293
+    putenv(discard_const("SOFTHSM2_CONF=" ABS_BUILD_DIR "/src/tests/test_CA/softhsm2_2certs_same_id.conf"));
8ad293
+
8ad293
+    mock_input_pam_cert(pam_test_ctx, "pamuser", "123456", "SSSD Test Token",
8ad293
+                        TEST_MODULE_NAME,
8ad293
+                        "11111111",
8ad293
+                        "SSSD test cert 0001", NULL,
8ad293
+                        test_lookup_by_cert_double_cb, SSSD_TEST_CERT_0001);
8ad293
+
8ad293
+    will_return(__wrap_sss_packet_get_cmd, SSS_PAM_AUTHENTICATE);
8ad293
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
8ad293
+
8ad293
+    /* Assume backend cannot handle Smartcard credentials */
8ad293
+    pam_test_ctx->exp_pam_status = PAM_BAD_ITEM;
8ad293
+
8ad293
+    set_cmd_cb(test_pam_simple_check_success);
8ad293
+    ret = sss_cmd_execute(pam_test_ctx->cctx, SSS_PAM_AUTHENTICATE,
8ad293
+                          pam_test_ctx->pam_cmds);
8ad293
+    assert_int_equal(ret, EOK);
8ad293
+
8ad293
+    /* Wait until the test finishes with EOK */
8ad293
+    ret = test_ev_loop(pam_test_ctx->tctx);
8ad293
+    assert_int_equal(ret, EOK);
8ad293
+}
8ad293
+
8ad293
+void test_pam_cert_auth_2certs_same_id_with_label_6(void **state)
8ad293
+{
8ad293
+    int ret;
8ad293
+
8ad293
+    set_cert_auth_param(pam_test_ctx->pctx, CA_DB);
8ad293
+    putenv(discard_const("SOFTHSM2_CONF=" ABS_BUILD_DIR "/src/tests/test_CA/softhsm2_2certs_same_id.conf"));
8ad293
+
8ad293
+    mock_input_pam_cert(pam_test_ctx, "pamuser", "123456", "SSSD Test Token",
8ad293
+                        TEST_MODULE_NAME,
8ad293
+                        "11111111",
8ad293
+                        "SSSD test cert 0006", NULL,
8ad293
+                        test_lookup_by_cert_double_cb, SSSD_TEST_CERT_0006);
8ad293
+
8ad293
+    will_return(__wrap_sss_packet_get_cmd, SSS_PAM_AUTHENTICATE);
8ad293
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
8ad293
+
8ad293
+    /* Assume backend cannot handle Smartcard credentials */
8ad293
+    pam_test_ctx->exp_pam_status = PAM_BAD_ITEM;
8ad293
+
8ad293
+    set_cmd_cb(test_pam_simple_check_success);
8ad293
+    ret = sss_cmd_execute(pam_test_ctx->cctx, SSS_PAM_AUTHENTICATE,
8ad293
+                          pam_test_ctx->pam_cmds);
8ad293
+    assert_int_equal(ret, EOK);
8ad293
+
8ad293
+    /* Wait until the test finishes with EOK */
8ad293
+    ret = test_ev_loop(pam_test_ctx->tctx);
8ad293
+    assert_int_equal(ret, EOK);
8ad293
+}
8ad293
+
8ad293
 void test_pam_cert_preauth_uri_token1(void **state)
8ad293
 {
8ad293
     int ret;
8ad293
@@ -3179,6 +3289,12 @@ int main(int argc, const char *argv[])
8ad293
                                         pam_test_setup, pam_test_teardown),
8ad293
         cmocka_unit_test_setup_teardown(test_pam_cert_auth_2certs_one_mapping,
8ad293
                                         pam_test_setup, pam_test_teardown),
8ad293
+        cmocka_unit_test_setup_teardown(test_pam_cert_auth_2certs_same_id_no_label,
8ad293
+                                        pam_test_setup, pam_test_teardown),
8ad293
+        cmocka_unit_test_setup_teardown(test_pam_cert_auth_2certs_same_id_with_label_1,
8ad293
+                                        pam_test_setup, pam_test_teardown),
8ad293
+        cmocka_unit_test_setup_teardown(test_pam_cert_auth_2certs_same_id_with_label_6,
8ad293
+                                        pam_test_setup, pam_test_teardown),
8ad293
         cmocka_unit_test_setup_teardown(test_pam_cert_auth_no_logon_name,
8ad293
                                         pam_test_setup, pam_test_teardown),
8ad293
         cmocka_unit_test_setup_teardown(test_pam_cert_auth_no_logon_name_no_key_id,
8ad293
diff --git a/src/tests/test_CA/Makefile.am b/src/tests/test_CA/Makefile.am
8ad293
index 0e0122737..8765d0fd6 100644
8ad293
--- a/src/tests/test_CA/Makefile.am
8ad293
+++ b/src/tests/test_CA/Makefile.am
8ad293
@@ -6,6 +6,7 @@ dist_noinst_DATA = \
8ad293
     SSSD_test_cert_0003.config \
8ad293
     SSSD_test_cert_0004.config \
8ad293
     SSSD_test_cert_0005.config \
8ad293
+    SSSD_test_cert_0006.config \
8ad293
     SSSD_test_cert_key_0001.pem \
8ad293
     SSSD_test_cert_key_0002.pem \
8ad293
     SSSD_test_cert_key_0003.pem \
8ad293
@@ -25,7 +26,7 @@ pubkeys = $(addprefix SSSD_test_cert_pubsshkey_,$(addsuffix .pub,$(ids)))
8ad293
 pubkeys_h = $(addprefix SSSD_test_cert_pubsshkey_,$(addsuffix .h,$(ids)))
8ad293
 pkcs12 = $(addprefix SSSD_test_cert_pkcs12_,$(addsuffix .pem,$(ids)))
8ad293
 
8ad293
-extra = softhsm2_none softhsm2_one softhsm2_two softhsm2_2tokens softhsm2_ocsp
8ad293
+extra = softhsm2_none softhsm2_one softhsm2_two softhsm2_2tokens softhsm2_ocsp softhsm2_2certs_same_id
8ad293
 if HAVE_FAKETIME
8ad293
 extra += SSSD_test_CA_expired_crl.pem
8ad293
 endif
8ad293
@@ -41,6 +42,14 @@ $(pwdfile):
8ad293
 SSSD_test_CA.pem: $(openssl_ca_key) $(openssl_ca_config) serial
8ad293
 	$(OPENSSL) req -batch -config ${openssl_ca_config} -x509 -new -nodes -key $< -sha256 -days 1024 -set_serial 0 -extensions v3_ca -out $@
8ad293
 
8ad293
+# SSSD_test_cert_0006 should use the same key as SSSD_test_cert_0001
8ad293
+.INTERMEDIATE: SSSD_test_cert_req_0006.pem
8ad293
+SSSD_test_cert_req_0006.pem: $(srcdir)/SSSD_test_cert_key_0001.pem $(srcdir)/SSSD_test_cert_0006.config
8ad293
+	if [ $(shell grep -c req_exts $(srcdir)/SSSD_test_cert_0006.config) -eq 0 ]; then \
8ad293
+		$(OPENSSL) req -new -nodes -key $< -config $(srcdir)/SSSD_test_cert_0006.config -out $@ ; \
8ad293
+	else \
8ad293
+		$(OPENSSL) req -new -nodes -key $< -reqexts req_exts -config $(srcdir)/SSSD_test_cert_0006.config -out $@ ; \
8ad293
+	fi
8ad293
 
8ad293
 SSSD_test_cert_req_%.pem: $(srcdir)/SSSD_test_cert_key_%.pem $(srcdir)/SSSD_test_cert_%.config
8ad293
 	if [ $(shell grep -c req_exts $(srcdir)/SSSD_test_cert_$*.config) -eq 0 ]; then \
8ad293
@@ -52,6 +61,9 @@ SSSD_test_cert_req_%.pem: $(srcdir)/SSSD_test_cert_key_%.pem $(srcdir)/SSSD_test
8ad293
 SSSD_test_cert_x509_%.pem: SSSD_test_cert_req_%.pem $(openssl_ca_config) SSSD_test_CA.pem
8ad293
 	$(OPENSSL) ca -config ${openssl_ca_config} -batch -notext -keyfile $(openssl_ca_key) -in $< -days 200 -extensions usr_cert -out $@
8ad293
 
8ad293
+SSSD_test_cert_pkcs12_0006.pem: SSSD_test_cert_x509_0006.pem $(srcdir)/SSSD_test_cert_key_0001.pem $(pwdfile)
8ad293
+	$(OPENSSL) pkcs12 -export -in SSSD_test_cert_x509_0006.pem -inkey $(srcdir)/SSSD_test_cert_key_0001.pem -nodes -passout file:$(pwdfile) -out $@
8ad293
+
8ad293
 SSSD_test_cert_pkcs12_%.pem: SSSD_test_cert_x509_%.pem $(srcdir)/SSSD_test_cert_key_%.pem $(pwdfile)
8ad293
 	$(OPENSSL) pkcs12 -export -in SSSD_test_cert_x509_$*.pem -inkey $(srcdir)/SSSD_test_cert_key_$*.pem -nodes -passout file:$(pwdfile) -out $@
8ad293
 
8ad293
@@ -130,6 +142,18 @@ softhsm2_ocsp.conf:
8ad293
 	@echo "objectstore.backend = file" >> $@
8ad293
 	@echo "slots.removable = true" >> $@
8ad293
 
8ad293
+softhsm2_2certs_same_id: softhsm2_2certs_same_id.conf SSSD_test_cert_x509_0001.pem SSSD_test_cert_x509_0006.pem
8ad293
+	mkdir $@
8ad293
+	SOFTHSM2_CONF=./$< $(SOFTHSM2_UTIL) --init-token  --label "SSSD Test Token" --pin 123456 --so-pin 123456 --free
8ad293
+	GNUTLS_PIN=123456 SOFTHSM2_CONF=./$< $(P11TOOL) --provider=$(SOFTHSM2_PATH) --write --no-mark-private --load-certificate=SSSD_test_cert_x509_0006.pem --login  --label 'SSSD test cert 0006' --id '11111111'
8ad293
+	GNUTLS_PIN=123456 SOFTHSM2_CONF=./$< $(P11TOOL) --provider=$(SOFTHSM2_PATH) --write --no-mark-private --load-certificate=SSSD_test_cert_x509_0001.pem --login  --label 'SSSD test cert 0001' --id '11111111'
8ad293
+	GNUTLS_PIN=123456 SOFTHSM2_CONF=./$< $(P11TOOL) --provider=$(SOFTHSM2_PATH) --write --load-privkey=$(srcdir)/SSSD_test_cert_key_0001.pem --login  --label 'SSSD test cert 0001' --id '11111111'
8ad293
+
8ad293
+softhsm2_2certs_same_id.conf:
8ad293
+	@echo "directories.tokendir = "$(abs_top_builddir)"/src/tests/test_CA/softhsm2_2certs_same_id" > $@
8ad293
+	@echo "objectstore.backend = file" >> $@
8ad293
+	@echo "slots.removable = true" >> $@
8ad293
+
8ad293
 CLEANFILES = \
8ad293
     index.txt  index.txt.attr \
8ad293
     index.txt.attr.old  index.txt.old \
8ad293
diff --git a/src/tests/test_CA/SSSD_test_cert_0006.config b/src/tests/test_CA/SSSD_test_cert_0006.config
8ad293
new file mode 100644
8ad293
index 000000000..762de55cd
8ad293
--- /dev/null
8ad293
+++ b/src/tests/test_CA/SSSD_test_cert_0006.config
8ad293
@@ -0,0 +1,20 @@
8ad293
+# This certificate is used in
8ad293
+# - src/tests/cmocka/test_pam_srv.c
8ad293
+# and should use the same key-pair as SSSD_test_cert_0001
8ad293
+[ req ]
8ad293
+distinguished_name = req_distinguished_name
8ad293
+prompt = no
8ad293
+
8ad293
+[ req_distinguished_name ]
8ad293
+O = SSSD
8ad293
+OU = SSSD test
8ad293
+CN = SSSD test cert 0006
8ad293
+
8ad293
+[ req_exts ]
8ad293
+basicConstraints = CA:FALSE
8ad293
+nsCertType = client, email
8ad293
+nsComment = "SSSD test Certificate"
8ad293
+subjectKeyIdentifier = hash
8ad293
+keyUsage = critical, nonRepudiation, digitalSignature, keyEncipherment
8ad293
+extendedKeyUsage = clientAuth, emailProtection
8ad293
+subjectAltName = email:sssd-devel@lists.fedorahosted.org,URI:https://github.com/SSSD/sssd//
8ad293
-- 
8ad293
2.21.3
8ad293