Go to the documentation of this file.
42 #define token_name "Test Token"
54 while ((e = ERR_get_error()))
56 msg(flags,
"OpenSSL error %lu: %s\n", e, ERR_error_string(e, NULL));
62 parse_line(
const char *line,
char **p,
const int n,
const char *file,
63 const int line_num,
int msglevel,
struct gc_arena *
gc)
78 #if defined(ENABLE_SYSTEMD)
80 query_user_exec_systemd(
void)
93 query_user_add(
char *prompt,
size_t prompt_len,
char *resp,
size_t resp_len,
bool echo)
111 const char *algorithm)
154 assert(
sizeof(certs_local) ==
sizeof(
certs));
155 memcpy(
certs, certs_local,
sizeof(certs_local));
161 const unsigned int flags,
const char *unused)
176 msg(
M_NONFATAL,
"ERROR: get_user_pass called with unknown request <%s> ignored\n", prefix);
187 assert_true(capacity >= EVP_MD_size(EVP_sha1()));
188 assert_int_equal(X509_digest(x509, EVP_sha1(),
hash, NULL), 1);
191 #if defined(HAVE_XKEY_PROVIDER)
213 fail_msg(
"make tmpfile using template <%s> failed (error = %d)",
softhsm2_conf_path, errno);
215 snprintf(config,
sizeof(config),
"directories.tokendir=%s/",
217 assert_int_equal(
write(fd, config, strlen(config)), strlen(config));
228 argv_printf(&a,
"%s --init-token --free --label \"%s\" --so-pin %s --pin %s",
233 char cert[] =
"cert_XXXXXX";
234 char key[] =
"key_XXXXXX";
235 int cert_fd = mkstemp(cert);
236 int key_fd = mkstemp(
key);
237 if (cert_fd < 0 || key_fd < 0)
239 fail_msg(
"make tmpfile for certificate or key data failed (error = %d)", errno);
245 BIO *buf = BIO_new_mem_buf(c->cert, -1);
249 x509 = PEM_read_bio_X509(buf, NULL, NULL, NULL);
252 assert_non_null(x509);
258 assert_int_equal(
write(cert_fd, c->cert, strlen(c->cert)), strlen(c->cert));
259 assert_int_equal(
write(key_fd, c->key, strlen(c->key)), strlen(c->key));
264 argv_printf(&a,
"%s --provider %s --load-certificate %s --label \"%s\" --id %08x --login --write",
265 P11TOOL_PATH, SOFTHSM2_MODULE_PATH, cert, c->friendly_name,
num_certs+1);
270 argv_printf(&a,
"%s --provider %s --load-privkey %s --label \"%s\" --id %08x --login --write",
271 P11TOOL_PATH, SOFTHSM2_MODULE_PATH,
key, c->friendly_name,
num_certs+1);
274 assert_int_equal(ftruncate(cert_fd, 0), 0);
275 assert_int_equal(ftruncate(key_fd, 0), 0);
276 assert_int_equal(lseek(cert_fd, 0, SEEK_SET), 0);
277 assert_int_equal(lseek(key_fd, 0, SEEK_SET), 0);
313 #if defined(HAVE_XKEY_PROVIDER)
316 prov[0] = OSSL_PROVIDER_load(
tls_libctx,
"default");
317 OSSL_PROVIDER_add_builtin(
tls_libctx,
"ovpn.xkey", xkey_provider_init);
318 prov[1] = OSSL_PROVIDER_load(
tls_libctx,
"ovpn.xkey");
319 assert_non_null(prov[1]);
322 EVP_set_default_properties(
tls_libctx,
"?provider!=ovpn.xkey");
324 pkcs11_initialize(
true, 0);
325 pkcs11_addProvider(SOFTHSM2_MODULE_PATH,
false, 0,
false);
333 #if defined(HAVE_XKEY_PROVIDER)
334 for (
size_t i = 0; i <
SIZE(prov); i++)
338 OSSL_PROVIDER_unload(prov[i]);
355 return c->
cert ? c : NULL;
365 int n = pkcs11_management_id_count();
368 for (
int i = 0; i < n; i++)
375 if (!pkcs11_management_id_get(i, &
p11_id, &base64))
377 fail_msg(
"Failed to get pkcs11-id for index (%d) from pkcs11-helper", i);
380 unsigned char *der = malloc(strlen(base64));
381 assert_non_null(der);
384 assert_true(derlen > 0);
386 const unsigned char *ppin = der;
387 assert_non_null(d2i_X509(&x509, &ppin, derlen));
403 fail_msg(
"Certificate <%s> not enumerated by pkcs11-helper", c->friendly_name);
419 #ifdef HAVE_XKEY_PROVIDER
422 tls_ctx.
ctx = SSL_CTX_new(SSLv23_client_method());
430 tls_ctx_use_pkcs11(&tls_ctx, 1, NULL);
435 tls_ctx_use_pkcs11(&tls_ctx, 0, c->p11_id);
439 X509 *x509 = SSL_CTX_get0_certificate(tls_ctx.
ctx);
440 assert_non_null(x509);
442 assert_memory_equal(sha1, c->hash,
HASHSIZE);
445 EVP_PKEY *pubkey = X509_get0_pubkey(x509);
446 EVP_PKEY *privkey = SSL_CTX_get0_privatekey(tls_ctx.
ctx);
447 assert_non_null(pubkey);
448 assert_non_null(privkey);
449 #ifdef HAVE_XKEY_PROVIDER
453 if (!SSL_CTX_check_private_key(tls_ctx.
ctx))
455 fail_msg(
"Certificate and private key in ssl_ctx do not match for <%s>", c->friendly_name);
459 SSL_CTX_free(tls_ctx.
ctx);
475 const struct CMUnitTest tests[] = {
483 int ret = cmocka_run_group_tests_name(
"pkcs11_tests", tests,
init,
cleanup);
void query_user_clear(void)
Wipes all data put into all of the query_user structs.
static const char *const cert2
int openvpn_base64_decode(const char *str, void *data, int size)
static int setup_pkcs11(void **state)
static const char *const cert1
char * x509_get_subject(openvpn_x509_cert_t *cert, struct gc_arena *gc)
static const char * pkcs11_id_current
void env_set_destroy(struct env_set *es)
static const char *const key1
void init_cert_data(void)
void argv_free(struct argv *a)
Frees all memory allocations allocated by the struct argv related functions.
int parse_line(const char *line, char **p, const int n, const char *file, const int line_num, int msglevel, struct gc_arena *gc)
const char *const friendly_name
static char softhsm2_tokens_path[]
Container for unidirectional cipher and HMAC key material.
static void sha1_fingerprint(X509 *x509, uint8_t *hash, int capacity)
int openvpn_execve_check(const struct argv *a, const struct env_set *es, const unsigned int flags, const char *error_message)
static void secure_memzero(void *data, size_t len)
Securely zeroise memory.
static struct test_cert certs[5]
#define SSL_CTX_new_ex(libctx, propq, method)
Reduce SSL_CTX_new_ex() to SSL_CTX_new() for OpenSSL < 3.
static int teardown_pkcs11(void **state)
struct management * management
void crypto_print_openssl_errors(const unsigned int flags)
Retrieve any occurred OpenSSL errors and print those errors.
static const char *const cname2
static const char *const cname3
static const char *const key3
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.
struct env_set * env_set_create(struct gc_arena *gc)
char * management_query_pk_sig(struct management *man, const char *b64_data, const char *algorithm)
static void test_pkcs11_ids(void **state)
static const char *const cname1
Garbage collection arena used to keep track of dynamically allocated memory.
void setenv_str(struct env_set *es, const char *name, const char *value)
static int init(void **state)
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...
static void strncpynt(char *dest, const char *src, size_t maxlen)
struct argv argv_new(void)
Allocates a new struct argv and ensures it is initialised.
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.
bool argv_printf(struct argv *argres, const char *format,...)
printf() variant which populates a struct argv.
int digest_sign_verify(EVP_PKEY *privkey, EVP_PKEY *pubkey)
Structure that wraps the TLS context.
static void test_tls_ctx_use_pkcs11(void **state)
#define GET_USER_PASS_PASSWORD_ONLY
static struct test_cert * lookup_cert_byhash(uint8_t *sha1)
OSSL_LIB_CTX * tls_libctx
static char softhsm2_conf_path[]
char password[USER_PASS_LEN]
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)
#define GET_USER_PASS_NEED_STR
static bool pkcs11_id_management
static void test_tls_ctx_use_pkcs11__management(void **state)
static const char *const cert4
static const char *const cert3
static int cleanup(void **state)