41#define token_name "Test Token"
53 while ((e = ERR_get_error()))
55 msg(flags,
"OpenSSL error %lu: %s", e, ERR_error_string(e, NULL));
61parse_line(
const char *line,
char **p,
const int n,
const char *file,
const int line_num,
77#if defined(ENABLE_SYSTEMD)
79query_user_exec_systemd(
void)
92query_user_add(
char *prompt,
size_t prompt_len,
char *resp,
size_t resp_len,
bool echo)
145 {
cert1,
key1,
cname1,
"OVPN TEST CA1",
"OVPN Test Cert 1", { 0 }, NULL },
146 {
cert2,
key2,
cname2,
"OVPN TEST CA2",
"OVPN Test Cert 2", { 0 }, NULL },
147 {
cert3,
key3,
cname3,
"OVPN TEST CA1",
"OVPN Test Cert 3", { 0 }, NULL },
148 {
cert4,
key4,
cname4,
"OVPN TEST CA2",
"OVPN Test Cert 4", { 0 }, NULL },
151 assert(
sizeof(certs_local) ==
sizeof(
certs));
152 memcpy(
certs, certs_local,
sizeof(certs_local));
158 const unsigned int flags,
const char *unused)
173 msg(
M_NONFATAL,
"ERROR: get_user_pass called with unknown request <%s> ignored", prefix);
184 assert_true(capacity >= EVP_MD_size(EVP_sha1()));
185 assert_int_equal(X509_digest(x509, EVP_sha1(),
hash, NULL), 1);
188#if defined(HAVE_XKEY_PROVIDER)
211 fail_msg(
"make tmpfile using template <%s> failed (error = %d)",
softhsm2_conf_path, errno);
214 assert_int_equal(
write(fd, config, strlen(config)), strlen(config));
225 argv_printf(&a,
"%s --init-token --free --label \"%s\" --so-pin %s --pin %s",
230 char cert[] =
"cert_XXXXXX";
231 char key[] =
"key_XXXXXX";
232 int cert_fd = mkstemp(cert);
233 int key_fd = mkstemp(
key);
234 if (cert_fd < 0 || key_fd < 0)
236 fail_msg(
"make tmpfile for certificate or key data failed (error = %d)", errno);
242 BIO *buf = BIO_new_mem_buf(c->cert, -1);
246 x509 = PEM_read_bio_X509(buf, NULL, NULL, NULL);
249 assert_non_null(x509);
255 assert_int_equal(
write(cert_fd, c->cert, strlen(c->cert)), strlen(c->cert));
256 assert_int_equal(
write(key_fd, c->key, strlen(c->key)), strlen(c->key));
262 &a,
"%s --provider %s --load-certificate %s --label \"%s\" --id %08x --login --write",
263 P11TOOL_PATH, SOFTHSM2_MODULE_PATH, cert, c->friendly_name,
num_certs + 1);
269 "%s --provider %s --load-privkey %s --label \"%s\" --id %08x --login --write",
270 P11TOOL_PATH, SOFTHSM2_MODULE_PATH,
key, c->friendly_name,
num_certs + 1);
273 assert_int_equal(ftruncate(cert_fd, 0), 0);
274 assert_int_equal(ftruncate(key_fd, 0), 0);
275 assert_int_equal(lseek(cert_fd, 0, SEEK_SET), 0);
276 assert_int_equal(lseek(key_fd, 0, SEEK_SET), 0);
312#if defined(HAVE_XKEY_PROVIDER)
315 prov[0] = OSSL_PROVIDER_load(
tls_libctx,
"default");
316 OSSL_PROVIDER_add_builtin(
tls_libctx,
"ovpn.xkey", xkey_provider_init);
317 prov[1] = OSSL_PROVIDER_load(
tls_libctx,
"ovpn.xkey");
318 assert_non_null(prov[1]);
321 EVP_set_default_properties(
tls_libctx,
"?provider!=ovpn.xkey");
323 pkcs11_initialize(
true, 0);
324 pkcs11_addProvider(SOFTHSM2_MODULE_PATH,
false, 0,
false);
332#if defined(HAVE_XKEY_PROVIDER)
333 for (
size_t i = 0;
i <
SIZE(prov);
i++)
337 OSSL_PROVIDER_unload(prov[
i]);
354 return c->
cert ? c : NULL;
364 int n = pkcs11_management_id_count();
367 for (
int i = 0;
i < n;
i++)
374 if (!pkcs11_management_id_get(
i, &
p11_id, &base64))
376 fail_msg(
"Failed to get pkcs11-id for index (%d) from pkcs11-helper",
i);
379 unsigned char *der = malloc(strlen(base64));
380 assert_non_null(der);
383 assert_true(derlen > 0);
385 const unsigned char *ppin = der;
386 assert_non_null(d2i_X509(&x509, &ppin, derlen));
402 fail_msg(
"Certificate <%s> not enumerated by pkcs11-helper", c->friendly_name);
418#ifdef HAVE_XKEY_PROVIDER
421 tls_ctx.
ctx = SSL_CTX_new(SSLv23_client_method());
429 tls_ctx_use_pkcs11(&tls_ctx, 1, NULL);
434 tls_ctx_use_pkcs11(&tls_ctx, 0, c->p11_id);
438 X509 *x509 = SSL_CTX_get0_certificate(tls_ctx.
ctx);
439 assert_non_null(x509);
441 assert_memory_equal(sha1, c->hash,
HASHSIZE);
444 EVP_PKEY *pubkey = X509_get0_pubkey(x509);
445 EVP_PKEY *privkey = SSL_CTX_get0_privatekey(tls_ctx.
ctx);
446 assert_non_null(pubkey);
447 assert_non_null(privkey);
448#ifdef HAVE_XKEY_PROVIDER
452 if (!SSL_CTX_check_private_key(tls_ctx.
ctx))
454 fail_msg(
"Certificate and private key in ssl_ctx do not match for <%s>",
459 SSL_CTX_free(tls_ctx.
ctx);
475 const struct CMUnitTest tests[] = {
481 int ret = cmocka_run_group_tests_name(
"pkcs11_tests", tests,
init,
cleanup);
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
static void strncpynt(char *dest, const char *src, size_t maxlen)
static const char *const cert2
static const char *const cert3
static const char *const key3
static const char *const cert4
static const char *const cname2
static const char *const cname1
static const char *const cname3
static const char *const cert1
static const char *const key1
void env_set_destroy(struct env_set *es)
void setenv_str(struct env_set *es, const char *name, const char *value)
struct env_set * env_set_create(struct gc_arena *gc)
#define GET_USER_PASS_PASSWORD_ONLY
#define GET_USER_PASS_NEED_STR
#define SSL_CTX_new_ex(libctx, propq, method)
Reduce SSL_CTX_new_ex() to SSL_CTX_new() for OpenSSL < 3.
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
int openvpn_base64_decode(const char *str, void *data, int size)
Control Channel SSL/Data channel negotiation module.
OSSL_LIB_CTX * tls_libctx
mbedtls_x509_crt openvpn_x509_cert_t
Garbage collection arena used to keep track of dynamically allocated memory.
Container for unidirectional cipher and HMAC key material.
const char *const friendly_name
Structure that wraps the TLS context.
char password[USER_PASS_LEN]
static void openvpn_unit_test_setup(void)
Sets up the environment for unit tests like making both stderr and stdout non-buffered to avoid messa...
void crypto_print_openssl_errors(const unsigned int flags)
Retrieve any occurred OpenSSL errors and print those errors.
struct management * management
static struct test_cert certs[5]
static void sha1_fingerprint(X509 *x509, uint8_t *hash, int capacity)
void query_user_clear(void)
Wipes all data put into all of the query_user structs.
bool query_user_exec_builtin(void)
Loop through configured query_user slots, using the built-in method for querying the user.
void purge_user_pass(struct user_pass *up, const bool force)
static void test_tls_ctx_use_pkcs11__management(void **state)
static void test_tls_ctx_use_pkcs11(void **state)
static int cleanup(void **state)
void init_cert_data(void)
static bool pkcs11_id_management
static int teardown_pkcs11(void **state)
static void test_pkcs11_ids(void **state)
char * x509_get_subject(openvpn_x509_cert_t *cert, struct gc_arena *gc)
static int setup_pkcs11(void **state)
static const char * pkcs11_id_current
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
static struct test_cert * lookup_cert_byhash(uint8_t *sha1)
static int init(void **state)
bool get_user_pass_cr(struct user_pass *up, const char *auth_file, const char *prefix, const unsigned int flags, const char *unused)
Retrieves the user credentials from various sources depending on the flags.
static char softhsm2_tokens_path[]
static char softhsm2_conf_path[]
void query_user_add(char *prompt, size_t prompt_len, char *resp, size_t resp_len, bool echo)
Adds an item to ask the user for.
int parse_line(const char *line, char **p, const int n, const char *file, const int line_num, int msglevel, struct gc_arena *gc)
int digest_sign_verify(EVP_PKEY *privkey, EVP_PKEY *pubkey)