dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0046-tests-add-PKCS-11-URI-tests.patch

71e593
From b2a979e5e66f463d9567165fa7a46a39a9e6ae18 Mon Sep 17 00:00:00 2001
71e593
From: Sumit Bose <sbose@redhat.com>
71e593
Date: Tue, 9 Oct 2018 10:46:43 +0200
71e593
Subject: [PATCH 46/47] tests: add PKCS#11 URI tests
71e593
71e593
Related to https://pagure.io/SSSD/sssd/issue/3814
71e593
71e593
Reviewed-by: Jakub Hrozek <jhrozek@redhat.com>
71e593
(cherry picked from commit 4a22fb6bba6662ad628f6e17203e8ccf20eb9666)
71e593
---
71e593
 src/tests/cmocka/test_pam_srv.c | 120 ++++++++++++++++++++++++++++++++++++++++
71e593
 src/tests/test_CA/Makefile.am   |  16 +++++-
71e593
 2 files changed, 135 insertions(+), 1 deletion(-)
71e593
71e593
diff --git a/src/tests/cmocka/test_pam_srv.c b/src/tests/cmocka/test_pam_srv.c
71e593
index 2b02ac27b7356c5bce9e11dae785ecdbddd31aa3..7fc9224e152b8c206faf5d0cd9b6778099c6605c 100644
71e593
--- a/src/tests/cmocka/test_pam_srv.c
71e593
+++ b/src/tests/cmocka/test_pam_srv.c
71e593
@@ -65,6 +65,7 @@
71e593
 #endif
71e593
 
71e593
 #define TEST_TOKEN_NAME "SSSD Test Token"
71e593
+#define TEST_TOKEN2_NAME "SSSD Test Token Number 2"
71e593
 #define TEST_KEY_ID "C554C9F82C2A9D58B70921C143304153A8A42F17"
71e593
 #ifdef HAVE_NSS
71e593
 #define TEST_MODULE_NAME "NSS-Internal"
71e593
@@ -961,6 +962,54 @@ static int test_pam_cert_check_ex(uint32_t status, uint8_t *body, size_t blen,
71e593
     return EOK;
71e593
 }
71e593
 
71e593
+static int test_pam_cert2_token2_check_ex(uint32_t status, uint8_t *body,
71e593
+                                          size_t blen, enum response_type type,
71e593
+                                          const char *name)
71e593
+{
71e593
+    size_t rp = 0;
71e593
+    uint32_t val;
71e593
+    size_t check2_len = 0;
71e593
+    char const *check2_strings[] = { NULL,
71e593
+                                     TEST_TOKEN2_NAME,
71e593
+                                     TEST_MODULE_NAME,
71e593
+                                     TEST2_KEY_ID,
71e593
+                                     TEST2_PROMPT,
71e593
+                                     NULL };
71e593
+
71e593
+    assert_int_equal(status, 0);
71e593
+
71e593
+    check2_strings[0] = name;
71e593
+    check2_len = check_string_array_len(check2_strings);
71e593
+
71e593
+    SAFEALIGN_COPY_UINT32(&val, body + rp, &rp);
71e593
+    assert_int_equal(val, pam_test_ctx->exp_pam_status);
71e593
+
71e593
+    SAFEALIGN_COPY_UINT32(&val, body + rp, &rp);
71e593
+    assert_int_equal(val, 2);
71e593
+
71e593
+    SAFEALIGN_COPY_UINT32(&val, body + rp, &rp);
71e593
+    assert_int_equal(val, SSS_PAM_DOMAIN_NAME);
71e593
+
71e593
+    SAFEALIGN_COPY_UINT32(&val, body + rp, &rp);
71e593
+    assert_int_equal(val, 9);
71e593
+
71e593
+    assert_int_equal(*(body + rp + val - 1), 0);
71e593
+    assert_string_equal(body + rp, TEST_DOM_NAME);
71e593
+    rp += val;
71e593
+
71e593
+    SAFEALIGN_COPY_UINT32(&val, body + rp, &rp);
71e593
+    assert_int_equal(val, type);
71e593
+
71e593
+    SAFEALIGN_COPY_UINT32(&val, body + rp, &rp);
71e593
+    assert_int_equal(val, check2_len);
71e593
+
71e593
+    check_string_array(check2_strings, body, &rp);
71e593
+
71e593
+    assert_int_equal(rp, blen);
71e593
+
71e593
+    return EOK;
71e593
+}
71e593
+
71e593
 static int test_pam_cert_check(uint32_t status, uint8_t *body, size_t blen)
71e593
 {
71e593
     return test_pam_cert_check_ex(status, body, blen,
71e593
@@ -968,6 +1017,12 @@ static int test_pam_cert_check(uint32_t status, uint8_t *body, size_t blen)
71e593
                                   NULL);
71e593
 }
71e593
 
71e593
+static int test_pam_cert2_check(uint32_t status, uint8_t *body, size_t blen)
71e593
+{
71e593
+    return test_pam_cert2_token2_check_ex(status, body, blen, SSS_PAM_CERT_INFO,
71e593
+                                          "pamuser@"TEST_DOM_NAME);
71e593
+}
71e593
+
71e593
 static int test_pam_cert_check_auth_success(uint32_t status, uint8_t *body,
71e593
                                             size_t blen)
71e593
 {
71e593
@@ -2476,6 +2531,65 @@ void test_pam_cert_auth_2certs_one_mapping(void **state)
71e593
     assert_int_equal(ret, EOK);
71e593
 }
71e593
 
71e593
+void test_pam_cert_preauth_uri_token1(void **state)
71e593
+{
71e593
+    int ret;
71e593
+
71e593
+    struct sss_test_conf_param pam_params[] = {
71e593
+        { CONFDB_PAM_P11_URI, "pkcs11:token=SSSD%20Test%20Token" },
71e593
+        { NULL, NULL },             /* Sentinel */
71e593
+    };
71e593
+
71e593
+    ret = add_pam_params(pam_params, pam_test_ctx->rctx->cdb);
71e593
+    assert_int_equal(ret, EOK);
71e593
+    set_cert_auth_param(pam_test_ctx->pctx, CA_DB);
71e593
+    putenv(discard_const("SOFTHSM2_CONF=" ABS_BUILD_DIR "/src/tests/test_CA/softhsm2_2tokens.conf"));
71e593
+
71e593
+    mock_input_pam_cert(pam_test_ctx, "pamuser", NULL, NULL, NULL, NULL, NULL,
71e593
+                        test_lookup_by_cert_cb, SSSD_TEST_CERT_0001, false);
71e593
+
71e593
+    will_return(__wrap_sss_packet_get_cmd, SSS_PAM_PREAUTH);
71e593
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
71e593
+
71e593
+    set_cmd_cb(test_pam_cert_check);
71e593
+    ret = sss_cmd_execute(pam_test_ctx->cctx, SSS_PAM_PREAUTH,
71e593
+                          pam_test_ctx->pam_cmds);
71e593
+    assert_int_equal(ret, EOK);
71e593
+
71e593
+    /* Wait until the test finishes with EOK */
71e593
+    ret = test_ev_loop(pam_test_ctx->tctx);
71e593
+    assert_int_equal(ret, EOK);
71e593
+}
71e593
+
71e593
+void test_pam_cert_preauth_uri_token2(void **state)
71e593
+{
71e593
+    int ret;
71e593
+
71e593
+    struct sss_test_conf_param pam_params[] = {
71e593
+        { CONFDB_PAM_P11_URI, "pkcs11:token=SSSD%20Test%20Token%20Number%202" },
71e593
+        { NULL, NULL },             /* Sentinel */
71e593
+    };
71e593
+
71e593
+    ret = add_pam_params(pam_params, pam_test_ctx->rctx->cdb);
71e593
+    assert_int_equal(ret, EOK);
71e593
+    set_cert_auth_param(pam_test_ctx->pctx, CA_DB);
71e593
+    putenv(discard_const("SOFTHSM2_CONF=" ABS_BUILD_DIR "/src/tests/test_CA/softhsm2_2tokens.conf"));
71e593
+
71e593
+    mock_input_pam_cert(pam_test_ctx, "pamuser", NULL, NULL, NULL, NULL, NULL,
71e593
+                        test_lookup_by_cert_cb, SSSD_TEST_CERT_0002, false);
71e593
+
71e593
+    will_return(__wrap_sss_packet_get_cmd, SSS_PAM_PREAUTH);
71e593
+    will_return(__wrap_sss_packet_get_body, WRAP_CALL_REAL);
71e593
+
71e593
+    set_cmd_cb(test_pam_cert2_check);
71e593
+    ret = sss_cmd_execute(pam_test_ctx->cctx, SSS_PAM_PREAUTH,
71e593
+                          pam_test_ctx->pam_cmds);
71e593
+    assert_int_equal(ret, EOK);
71e593
+
71e593
+    /* Wait until the test finishes with EOK */
71e593
+    ret = test_ev_loop(pam_test_ctx->tctx);
71e593
+    assert_int_equal(ret, EOK);
71e593
+}
71e593
 
71e593
 void test_filter_response(void **state)
71e593
 {
71e593
@@ -2915,6 +3029,12 @@ int main(int argc, const char *argv[])
71e593
                                         pam_test_setup, pam_test_teardown),
71e593
         cmocka_unit_test_setup_teardown(test_pam_cert_auth_no_logon_name_no_key_id,
71e593
                                         pam_test_setup, pam_test_teardown),
71e593
+#ifndef HAVE_NSS
71e593
+        cmocka_unit_test_setup_teardown(test_pam_cert_preauth_uri_token1,
71e593
+                                        pam_test_setup, pam_test_teardown),
71e593
+        cmocka_unit_test_setup_teardown(test_pam_cert_preauth_uri_token2,
71e593
+                                        pam_test_setup, pam_test_teardown),
71e593
+#endif /* ! HAVE_NSS */
71e593
 #endif /* HAVE_TEST_CA */
71e593
 
71e593
         cmocka_unit_test_setup_teardown(test_filter_response,
71e593
diff --git a/src/tests/test_CA/Makefile.am b/src/tests/test_CA/Makefile.am
71e593
index 1bce2c36633b2d1df65c29258f8ee163a4bfce9e..b574c76111560ba3fce453254e74c267fc680681 100644
71e593
--- a/src/tests/test_CA/Makefile.am
71e593
+++ b/src/tests/test_CA/Makefile.am
71e593
@@ -24,7 +24,7 @@ pkcs12 = $(addprefix SSSD_test_cert_pkcs12_,$(addsuffix .pem,$(ids)))
71e593
 if HAVE_NSS
71e593
 extra = p11_nssdb p11_nssdb_2certs
71e593
 else
71e593
-extra = softhsm2_none softhsm2_one softhsm2_two
71e593
+extra = softhsm2_none softhsm2_one softhsm2_two softhsm2_2tokens
71e593
 endif
71e593
 
71e593
 # If openssl is run in parallel there might be conflicts with the serial
71e593
@@ -114,6 +114,20 @@ softhsm2_two.conf:
71e593
 	@echo "objectstore.backend = file" >> $@
71e593
 	@echo "slots.removable = true" >> $@
71e593
 
71e593
+softhsm2_2tokens: softhsm2_2tokens.conf
71e593
+	mkdir $@
71e593
+	SOFTHSM2_CONF=./$< $(SOFTHSM2_UTIL) --init-token  --label "SSSD Test Token" --pin 123456 --so-pin 123456 --free
71e593
+	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 'C554C9F82C2A9D58B70921C143304153A8A42F17' pkcs11:token=SSSD%20Test%20Token
71e593
+	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 'C554C9F82C2A9D58B70921C143304153A8A42F17' pkcs11:token=SSSD%20Test%20Token
71e593
+	SOFTHSM2_CONF=./$< $(SOFTHSM2_UTIL) --init-token  --label "SSSD Test Token Number 2" --pin 654321 --so-pin 654321 --free
71e593
+	GNUTLS_PIN=654321 SOFTHSM2_CONF=./$< $(P11TOOL) --provider=$(SOFTHSM2_PATH) --write --no-mark-private --load-certificate=SSSD_test_cert_x509_0002.pem --login  --label 'SSSD test cert 0002' --id '5405842D56CF31F0BB025A695C5F3E907051C5B9' pkcs11:token=SSSD%20Test%20Token%20Number%202
71e593
+	GNUTLS_PIN=654321 SOFTHSM2_CONF=./$< $(P11TOOL) --provider=$(SOFTHSM2_PATH) --write --load-privkey=$(srcdir)/SSSD_test_cert_key_0002.pem --login  --label 'SSSD test cert 0002' --id '5405842D56CF31F0BB025A695C5F3E907051C5B9' pkcs11:token=SSSD%20Test%20Token%20Number%202
71e593
+
71e593
+softhsm2_2tokens.conf:
71e593
+	@echo "directories.tokendir = "$(abs_top_builddir)"/src/tests/test_CA/softhsm2_2tokens" > $@
71e593
+	@echo "objectstore.backend = file" >> $@
71e593
+	@echo "slots.removable = true" >> $@
71e593
+
71e593
 CLEANFILES = \
71e593
     index.txt  index.txt.attr \
71e593
     index.txt.attr.old  index.txt.old \
71e593
-- 
71e593
2.14.4
71e593