34 #ifdef HAVE_XKEY_PROVIDER
38 #include <openssl/bio.h>
39 #include <openssl/pem.h>
40 #include <openssl/core_names.h>
41 #include <openssl/evp.h>
46 static int mgmt_callback_called;
49 #define _countof(x) sizeof((x))/sizeof(*(x))
55 static const char pubkey1[] =
"-----BEGIN PUBLIC KEY-----\n"
56 "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7GWP6RLCGlvmVioIqYI6\n"
57 "LUR4owA7sJ/nJxBAk+/xzD6gqgSigBsTqeb+gdZwkKjY1N4w2DUA0r5i8Eja/BWN\n"
58 "xMZtC5nxK4MACtMqIwvlzfk130NhFXKtlZj2cyFBXqDdRyeg1ZrUQagcHVcgcReP\n"
59 "9yiePgfO7NUOQk8edEeOR53SFCgnLBQQ9dGWtZN0hO/5BN6NSm/fd6vq0VjTRP5a\n"
60 "BAH/BnqX9/3jV0jh8N9AE59mI1rjVVQ9VDnuAPkS8dLfdC661/CNxt0YWByTIgt1\n"
61 "+qjW4LUvLbnU/rlPhuJ1SBZg+z/JtDBCKfs7syu5WYFqRvNFg7/91Rr/NwxvW/1h\n"
63 "-----END PUBLIC KEY-----\n";
65 static const char pubkey2[] =
"-----BEGIN PUBLIC KEY-----\n"
66 "MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEO85iXW+HgnUkwlj1DohNVw0GsnGIh1gZ\n"
67 "u95ff1JiUaJIkYNIkZA+hwIPFVH5aJcSCv3SPIeDS2VUAESNKHZJBQ==\n"
68 "-----END PUBLIC KEY-----\n";
70 static const char pubkey3[] =
"-----BEGIN PUBLIC KEY-----\n"
71 "MCowBQYDK2VwAyEA+q5xjF5hGyyqYZidJdz/0saEQabL3N4wIZJBxNGbgJE=\n"
72 "-----END PUBLIC KEY-----";
74 static const char *pubkeys[] = {pubkey1, pubkey2, pubkey3};
76 static const char *prov_name =
"ovpn.xkey";
78 static const char *test_msg =
"Lorem ipsum dolor sit amet, consectetur "
79 "adipisici elit, sed eiusmod tempor incidunt "
80 "ut labore et dolore magna aliqua.";
82 static const char *test_msg_b64 =
83 "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2ljaS"
84 "BlbGl0LCBzZWQgZWl1c21vZCB0ZW1wb3IgaW5jaWR1bnQgdXQgbGFib3JlIGV0IGRv"
85 "bG9yZSBtYWduYSBhbGlxdWEu";
88 static const uint8_t test_digest[] = {
89 0x77, 0x38, 0x65, 0x00, 0x1e, 0x96, 0x48, 0xc6, 0x57, 0x0b, 0xae,
90 0xc0, 0xb7, 0x96, 0xf9, 0x66, 0x4d, 0x5f, 0xd0, 0xb7, 0xdb, 0xf3,
91 0x3a, 0xbf, 0x02, 0xcc, 0x78, 0x61, 0x83, 0x20, 0x20, 0xee
94 static const char *test_digest_b64 =
"dzhlAB6WSMZXC67At5b5Zk1f0Lfb8zq/Asx4YYMgIO4=";
101 static const uint8_t good_sig[] = {
102 0xd8, 0xa7, 0xd9, 0x81, 0xd8, 0xaa, 0xd8, 0xad, 0x20, 0xd9, 0x8a, 0xd8,
103 0xa7, 0x20, 0xd8, 0xb3, 0xd9, 0x85, 0xd8, 0xb3, 0xd9, 0x85, 0x0
106 static const char *good_sig_b64 =
"2KfZgdiq2K0g2YrYpyDYs9mF2LPZhQA=";
109 load_pubkey(
const char *pem)
111 BIO *in = BIO_new_mem_buf(pem, -1);
114 EVP_PKEY *pkey = PEM_read_bio_PUBKEY(in, NULL, NULL, NULL);
115 assert_non_null(pkey);
125 prov[0] = OSSL_PROVIDER_load(NULL,
"default");
126 OSSL_PROVIDER_add_builtin(NULL, prov_name, xkey_provider_init);
127 prov[1] = OSSL_PROVIDER_load(NULL, prov_name);
130 EVP_set_default_properties(NULL,
"?provider!=ovpn.xkey");
132 #ifdef ENABLE_MANAGEMENT
140 for (
size_t i = 0; i < _countof(prov); i++)
144 OSSL_PROVIDER_unload(prov[i]);
158 const char *algorithm)
163 mgmt_callback_called = 1;
165 const char *expected_tbs = test_digest_b64;
166 if (strstr(algorithm,
"data=message"))
168 expected_tbs = test_msg_b64;
171 if (!strstr(algorithm,
"ED25519"))
173 assert_non_null(strstr(algorithm,
"hashalg=SHA256"));
176 assert_string_equal(b64_data, expected_tbs);
179 if (!strstr(algorithm,
"ECDSA") && !strstr(algorithm,
"ED25519"))
181 assert_non_null(strstr(algorithm,
"RSA_PKCS1_PSS_PADDING,hashalg=SHA256,saltlen=digest"));
187 out = strdup(good_sig_b64);
188 assert_non_null(out);
195 xkey_provider_test_fetch(
void **state)
197 assert_true(OSSL_PROVIDER_available(NULL, prov_name));
199 const char *algs[] = {
"RSA",
"ECDSA"};
201 for (
size_t i = 0; i < _countof(algs); i++)
203 EVP_SIGNATURE *sig = EVP_SIGNATURE_fetch(NULL, algs[i],
"provider=ovpn.xkey");
204 assert_non_null(sig);
205 assert_string_equal(OSSL_PROVIDER_get0_name(EVP_SIGNATURE_get0_provider(sig)), prov_name);
207 EVP_SIGNATURE_free(sig);
210 const char *names[] = {
"RSA",
"EC"};
212 for (
size_t i = 0; i < _countof(names); i++)
214 EVP_KEYMGMT *km = EVP_KEYMGMT_fetch(NULL, names[i],
"provider=ovpn.xkey");
216 assert_string_equal(OSSL_PROVIDER_get0_name(EVP_KEYMGMT_get0_provider(km)), prov_name);
218 EVP_KEYMGMT_free(km);
224 digest_sign(EVP_PKEY *pkey)
229 OSSL_PARAM params[6] = {OSSL_PARAM_END};
231 const char *mdname =
"SHA256";
232 const char *padmode =
"pss";
233 const char *saltlen =
"digest";
235 if (EVP_PKEY_get_id(pkey) == EVP_PKEY_RSA)
237 params[0] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, (
char *)mdname, 0);
238 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PAD_MODE, (
char *)padmode, 0);
239 params[2] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, (
char *)saltlen, 0);
241 params[3] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_MGF1_DIGEST, (
char *)saltlen, 0);
242 params[4] = OSSL_PARAM_construct_end();
244 else if (EVP_PKEY_get_id(pkey) == EVP_PKEY_ED25519)
247 params[0] = OSSL_PARAM_construct_end();
251 EVP_PKEY_CTX *pctx = NULL;
252 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
255 || EVP_DigestSignInit_ex(mctx, &pctx, mdname, NULL, NULL, pkey, params) <= 0)
257 fail_msg(
"Failed to initialize EVP_DigestSignInit_ex()");
262 assert_int_equal(EVP_DigestSign(mctx, sig, &siglen, (uint8_t *)test_msg, strlen(test_msg)), 1);
263 assert_true(siglen > 0);
265 if ((sig = test_calloc(1, siglen)) == NULL)
267 fail_msg(
"Out of memory");
269 assert_int_equal(EVP_DigestSign(mctx, sig, &siglen, (uint8_t *)test_msg, strlen(test_msg)), 1);
274 EVP_MD_CTX_free(mctx);
279 #ifdef ENABLE_MANAGEMENT
285 xkey_provider_test_mgmt_sign_cb(
void **state)
288 for (
size_t i = 0; i < _countof(pubkeys); i++)
290 pubkey = load_pubkey(pubkeys[i]);
291 assert_true(pubkey != NULL);
292 EVP_PKEY *privkey = xkey_load_management_key(NULL, pubkey);
293 assert_true(privkey != NULL);
299 mgmt_callback_called = 0;
300 uint8_t *sig = digest_sign(privkey);
301 assert_non_null(sig);
304 assert_int_equal(mgmt_callback_called, 1);
305 assert_memory_equal(sig, good_sig,
sizeof(good_sig));
314 EVP_PKEY_free(pubkey);
315 EVP_PKEY_free(privkey);
321 static int xkey_free_called;
322 static int xkey_sign_called;
324 xkey_free(
void *handle)
326 xkey_free_called = 1;
328 assert_string_equal(handle,
"xkey_handle");
332 xkey_sign(
void *handle,
unsigned char *sig,
size_t *siglen,
333 const unsigned char *tbs,
size_t tbslen, XKEY_SIGALG s)
341 xkey_sign_called = 1;
343 if (!strcmp(s.op,
"DigestSign"))
345 assert_memory_equal(tbs, test_msg, strlen(test_msg));
349 assert_memory_equal(tbs, test_digest,
sizeof(test_digest));
353 if (!strcmp(s.keytype,
"ED25519"))
355 assert_string_equal(s.mdname,
"none");
359 assert_int_equal(OBJ_sn2nid(s.mdname), NID_sha256);
361 if (!strcmp(s.keytype,
"RSA"))
363 assert_string_equal(s.padmode,
"pss");
365 else if (strcmp(s.keytype,
"EC") && strcmp(s.keytype,
"ED25519"))
367 fail_msg(
"Unknown keytype: %s", s.keytype);
371 memcpy(sig, good_sig,
min_int(
sizeof(good_sig), *siglen));
380 xkey_provider_test_generic_sign_cb(
void **state)
383 const char *dummy =
"xkey_handle";
385 for (
size_t i = 0; i < _countof(pubkeys); i++)
387 pubkey = load_pubkey(pubkeys[i]);
388 assert_true(pubkey != NULL);
390 EVP_PKEY *privkey = xkey_load_generic_key(NULL, (
void *)dummy, pubkey, xkey_sign, xkey_free);
391 assert_true(privkey != NULL);
393 xkey_sign_called = 0;
394 xkey_free_called = 0;
395 uint8_t *sig = digest_sign(privkey);
396 assert_non_null(sig);
399 assert_int_equal(xkey_sign_called, 1);
400 assert_memory_equal(sig, good_sig,
sizeof(good_sig));
403 EVP_PKEY_free(pubkey);
404 EVP_PKEY_free(privkey);
407 assert_int_equal(xkey_free_called, 1);
416 const struct CMUnitTest tests[] = {
417 cmocka_unit_test(xkey_provider_test_fetch),
418 #ifdef ENABLE_MANAGEMENT
419 cmocka_unit_test(xkey_provider_test_mgmt_sign_cb),
421 cmocka_unit_test(xkey_provider_test_generic_sign_cb),
424 int ret = cmocka_run_group_tests_name(
"xkey provider tests", tests, NULL, NULL);