Skip to content

Commit afc7620

Browse files
olszomalmtrojnar
authored andcommitted
libp11 PKCS#11 provider tests
1 parent 8ff7952 commit afc7620

24 files changed

Lines changed: 2340 additions & 50 deletions

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,17 @@ tests/rsa-pss-sign
7474
tests/check-privkey
7575
tests/dup-key
7676
tests/store-cert
77+
tests/check-privkey-prov
78+
tests/dup-key-prov
79+
tests/evp-sign-prov
80+
tests/fork-change-slot-prov
81+
tests/rsa-oaep-prov
82+
tests/rsa-pss-sign-prov
83+
tests/store-cert-prov
84+
7785
tests/*.log
7886
tests/*.trs
87+
tests/*.a
7988
tests/output.*
8089

8190
doc/doxygen.conf

tests/Makefile.am

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ LDADD = ../src/libp11.la $(OPENSSL_LIBS)
1111

1212
check_PROGRAMS = \
1313
openssl_version \
14-
fork-test evp-sign \
14+
evp-sign \
15+
evp-sign-prov \
16+
fork-test \
1517
fork-change-slot \
18+
fork-change-slot-prov \
1619
list-tokens \
1720
rsa-pss-sign \
21+
rsa-pss-sign-prov \
1822
rsa-oaep \
23+
rsa-oaep-prov \
1924
check-privkey \
25+
check-privkey-prov \
2026
store-cert \
21-
dup-key
27+
store-cert-prov \
28+
dup-key \
29+
dup-key-prov
2230
dist_check_SCRIPTS = \
2331
rsa-testpkcs11.softhsm \
2432
rsa-testfork.softhsm \
@@ -36,11 +44,31 @@ dist_check_SCRIPTS = \
3644
fork-change-slot.softhsm \
3745
case-insensitive.softhsm \
3846
pkcs11-uri-without-token.softhsm \
39-
search-all-matching-tokens.softhsm
47+
search-all-matching-tokens.softhsm \
48+
provider-rsa-evp-sign.softhsm \
49+
provider-rsa-pss-sign.softhsm \
50+
provider-rsa-oaep.softhsm \
51+
provider-rsa-check-privkey.softhsm \
52+
provider-ec-evp-sign.softhsm \
53+
provider-ec-check-privkey.softhsm \
54+
provider-ec-cert-store.softhsm \
55+
provider-ec-copy.softhsm \
56+
provider-fork-change-slot.softhsm \
57+
provider-case-insensitive.softhsm \
58+
provider-pkcs11-uri-without-token.softhsm \
59+
provider-search-all-matching-tokens.softhsm
4060
dist_check_DATA = \
4161
rsa-cert.der rsa-privkey.der rsa-pubkey.der \
4262
ec-cert.der ec-privkey.der ec-pubkey.der
4363

64+
evp_sign_prov_SOURCES = evp-sign-prov.c helpers_prov.c
65+
fork_change_slot_prov_SOURCES = fork-change-slot-prov.c helpers_prov.c
66+
dup_key_prov_SOURCES = dup-key-prov.c helpers_prov.c
67+
check_privkey_prov_SOURCES = check-privkey-prov.c helpers_prov.c
68+
rsa_pss_sign_prov_SOURCES = rsa-pss-sign-prov.c helpers_prov.c
69+
rsa_oaep_prov_SOURCES = rsa-oaep-prov.c helpers_prov.c
70+
store_cert_prov_SOURCES = store-cert-prov.c helpers_prov.c
71+
4472
TESTS = $(dist_check_SCRIPTS)
4573

4674
TESTS_ENVIRONMENT = \

tests/check-privkey-prov.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* Copyright © 2025 Mobi - Com Polska Sp. z o.o.
3+
* Author: Małgorzata Olszówka <Malgorzata.Olszowka@stunnel.org>
4+
* All rights reserved.
5+
*
6+
* PKCS#11 provider test
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*/
29+
30+
#include "helpers_prov.h"
31+
32+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
33+
34+
int main(int argc, char *argv[])
35+
{
36+
EVP_PKEY *private_key = NULL;
37+
X509 *cert = NULL;
38+
int ret = EXIT_FAILURE;
39+
40+
if (argc < 2) {
41+
fprintf(stderr, "usage: %s [certificate (PEM or URL)] [private key URL]n", argv[0]);
42+
return ret;
43+
}
44+
45+
/* Load pkcs11prov and default providers */
46+
if (!providers_load()) {
47+
display_openssl_errors();
48+
return ret;
49+
}
50+
51+
/* Load certificate */
52+
cert = load_cert(argv[1]);
53+
if (!cert) {
54+
fprintf(stderr, "Cannot load certificate: %s\n", argv[1]);
55+
display_openssl_errors();
56+
goto cleanup;
57+
}
58+
printf("Certificate found: %s\n", argv[1]);
59+
60+
/* Load private key */
61+
private_key = load_pkey(argv[2], NULL);
62+
if (!private_key) {
63+
fprintf(stderr, "Cannot load private key: %s\n", argv[1]);
64+
display_openssl_errors();
65+
goto cleanup;
66+
}
67+
printf("Private key found.\n");
68+
69+
ret = X509_check_private_key(cert, private_key);
70+
if (!ret) {
71+
printf("Could not check private key.\n");
72+
display_openssl_errors();
73+
goto cleanup;
74+
}
75+
76+
printf("Key and certificate matched.\n");
77+
ret = EXIT_SUCCESS;
78+
79+
cleanup:
80+
X509_free(cert);
81+
EVP_PKEY_free(private_key);
82+
providers_cleanup();
83+
printf("\n");
84+
return ret;
85+
}
86+
87+
#else
88+
89+
int main() {
90+
return 0;
91+
}
92+
93+
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
94+
95+
/* vim: set noexpandtab: */

tests/dup-key-prov.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright © 2025 Mobi - Com Polska Sp. z o.o.
3+
* Author: Małgorzata Olszówka <Malgorzata.Olszowka@stunnel.org>
4+
* All rights reserved.
5+
*
6+
* PKCS#11 provider test
7+
*
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions
10+
* are met:
11+
* 1. Redistributions of source code must retain the above copyright
12+
* notice, this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright
14+
* notice, this list of conditions and the following disclaimer in the
15+
* documentation and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27+
* SUCH DAMAGE.
28+
*/
29+
30+
#include "helpers_prov.h"
31+
32+
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
33+
34+
int main(int argc, char *argv[])
35+
{
36+
EVP_PKEY *private_key = NULL;
37+
EVP_PKEY *private_key_dup = NULL;
38+
int ret = EXIT_FAILURE;
39+
40+
if (argc < 1) {
41+
fprintf(stderr, "Usage: %s [private key URL]\n", argv[0]);
42+
return ret;
43+
}
44+
45+
/* Load pkcs11prov and default providers */
46+
if (!providers_load()) {
47+
display_openssl_errors();
48+
return ret;
49+
}
50+
51+
/* Load private key */
52+
private_key = load_pkey(argv[1], NULL);
53+
if (!private_key) {
54+
fprintf(stderr, "Cannot load private key: %s\n", argv[1]);
55+
display_openssl_errors();
56+
goto cleanup;
57+
}
58+
printf("Private key found.\n");
59+
60+
private_key_dup = EVP_PKEY_dup(private_key);
61+
if (!private_key_dup) {
62+
fprintf(stderr, "Cannot duplicate private key\n");
63+
display_openssl_errors();
64+
goto cleanup;
65+
}
66+
printf("Duplicate private key created.\n");
67+
68+
EVP_PKEY_free(private_key_dup);
69+
EVP_PKEY_free(private_key);
70+
71+
/* Do it one more time */
72+
private_key = load_pkey(argv[1], NULL);
73+
if (!private_key) {
74+
fprintf(stderr, "Cannot load private key: %s\n", argv[1]);
75+
display_openssl_errors();
76+
goto cleanup;
77+
}
78+
printf("Private key found.\n");
79+
ret = EXIT_SUCCESS;
80+
81+
cleanup:
82+
EVP_PKEY_free(private_key);
83+
providers_cleanup();
84+
printf("\n");
85+
return ret;
86+
}
87+
88+
#else
89+
90+
int main() {
91+
return 0;
92+
}
93+
94+
#endif /* OPENSSL_VERSION_NUMBER >= 0x30000000L */
95+
96+
/* vim: set noexpandtab: */

0 commit comments

Comments
 (0)