1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
|
/**
* @file test_cert_exp_notif.c
* @author Roman Janota <janota@cesnet.cz>
* @brief libnetconf2 certificate expiration notification test
*
* @copyright
* Copyright (c) 2024 CESNET, z.s.p.o.
*
* This source code is licensed under BSD 3-Clause License (the "License").
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*/
#define _GNU_SOURCE
#include <errno.h>
#include <pthread.h>
#include <setjmp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <cmocka.h>
#include "ln2_test.h"
#ifdef HAVE_MBEDTLS
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#include <mbedtls/error.h>
#include <mbedtls/pk.h>
#include <mbedtls/x509.h>
#include <mbedtls/x509_crt.h>
#else
#include <openssl/asn1.h>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/x509.h>
#endif
#define VALID_CERT_ADD_SEC 15 /* should be enough even with valgrind on slower machines */
#define EXPIRED_CERT_ADD_SEC -10
/* leave a 2 second leeway at most for both sending and receiving */
#define NC_RECV_NOTIF_TIMEOUT (VALID_CERT_ADD_SEC + 2) * 1000
#define NC_SEND_NOTIF_TIMEOUT (VALID_CERT_ADD_SEC + 2) * 1000
struct ly_ctx *server_ctx, *client_ctx;
struct test_state {
pthread_barrier_t barrier;
pthread_barrier_t ntf_barrier;
struct nc_server_notif *ntf;
struct lyd_node *tree;
};
int TEST_PORT = 10050;
const char *TEST_PORT_STR = "10050";
#ifdef HAVE_MBEDTLS
static const char *
mbedtls_strerr(int err)
{
const char *err_str;
err_str = mbedtls_high_level_strerr(err);
if (err_str) {
return err_str;
}
err_str = mbedtls_low_level_strerr(err);
if (err_str) {
return err_str;
}
return "unknown error";
}
static int
custom_exp_date_cert_create(long offset_sec, char cert_path[12])
{
int ret = 0, fd;
mbedtls_pk_context pkey;
mbedtls_x509write_cert cert;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
time_t exp_time;
struct tm *exp_tm;
const char *not_before = "20000101000000";
char not_after[15];
unsigned char output_buf[4096] = {0};
mode_t umode;
/* init */
mbedtls_ctr_drbg_init(&ctr_drbg);
mbedtls_entropy_init(&entropy);
mbedtls_pk_init(&pkey);
mbedtls_x509write_crt_init(&cert);
ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, NULL, 0);
if (ret) {
fprintf(stderr, "mbedtls_ctr_drbg_seed() failed (%s)\n", mbedtls_strerr(ret));
goto cleanup;
}
/* parse the private key */
ret = mbedtls_pk_parse_keyfile(&pkey, TESTS_DIR "/data/client.key", NULL, mbedtls_ctr_drbg_random, &ctr_drbg);
if (ret) {
fprintf(stderr, "mbedtls_pk_parse_keyfile() failed (%s)\n", mbedtls_strerr(ret));
goto cleanup;
}
/* cert will be self signed, so the subject's and issuer's key can be the same */
mbedtls_x509write_crt_set_subject_key(&cert, &pkey);
mbedtls_x509write_crt_set_issuer_key(&cert, &pkey);
/* likewise for their CNs */
ret = mbedtls_x509write_crt_set_subject_name(&cert, "CN=cert_exp_test");
if (ret) {
fprintf(stderr, "mbedtls_x509write_crt_set_subject_name() failed (%s)\n", mbedtls_strerr(ret));
goto cleanup;
}
ret = mbedtls_x509write_crt_set_issuer_name(&cert, "CN=cert_exp_test");
if (ret) {
fprintf(stderr, "mbedtls_x509write_crt_set_issuer_name() failed (%s)\n", mbedtls_strerr(ret));
goto cleanup;
}
/* set SN to 1 */
ret = mbedtls_x509write_crt_set_serial_raw(&cert, (unsigned char *)"1", 1);
if (ret) {
fprintf(stderr, "mbedtls_x509write_crt_set_serial_raw() failed (%s)\n", mbedtls_strerr(ret));
goto cleanup;
}
/* generate the expiration date in GMT */
exp_time = time(NULL) + offset_sec;
exp_tm = gmtime(&exp_time);
ret = strftime(not_after, 15, "%Y%m%d%H%M%S", exp_tm);
if (ret != 14) {
fprintf(stderr, "strftime() failed (%s)\n", strerror(errno));
ret = 1;
goto cleanup;
}
/* set the validity dates */
ret = mbedtls_x509write_crt_set_validity(&cert, not_before, not_after);
if (ret) {
fprintf(stderr, "mbedtls_x509write_crt_set_validity() failed (%s)\n", mbedtls_strerr(ret));
goto cleanup;
}
mbedtls_x509write_crt_set_md_alg(&cert, MBEDTLS_MD_SHA256);
/* write the cert to mem */
ret = mbedtls_x509write_crt_pem(&cert, output_buf, 4096, mbedtls_ctr_drbg_random, &ctr_drbg);
if (ret < 0) {
fprintf(stderr, "mbedtls_x509write_crt_pem() failed (%s)\n", mbedtls_strerr(ret));
goto cleanup;
}
/* then create a tmp file and write it from mem to this file */
umode = umask(0177);
fd = mkstemp(cert_path);
if (fd < 0) {
fprintf(stderr, "mkstemp() failed (%s)\n", strerror(errno));
ret = 1;
goto cleanup;
}
umask(umode);
if (write(fd, output_buf, strlen((char *)output_buf)) < 0) {
fprintf(stderr, "write() failed (%s)\n", strerror(errno));
ret = 1;
goto cleanup;
}
cleanup:
mbedtls_x509write_crt_free(&cert);
mbedtls_pk_free(&pkey);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
return ret;
}
#else
static const char *
openssl_strerr(void)
{
return ERR_reason_error_string(ERR_get_error());
}
static int
custom_exp_date_cert_create(long offset_sec, char cert_path[12])
{
int ret = 0;
EVP_PKEY *pkey = NULL;
X509 *cert = NULL;
X509_NAME *name;
FILE *f;
mode_t umode;
BIO *out_bio = NULL;
int fd;
/* get the private key */
f = fopen(TESTS_DIR "/data/client.key", "r");
if (!f) {
fprintf(stderr, "fopen() failed (%s)\n", strerror(errno));
ret = 1;
goto cleanup;
}
pkey = PEM_read_PrivateKey(f, NULL, NULL, NULL);
if (!pkey) {
fprintf(stderr, "PEM_read_PrivateKey() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
/* new cert */
cert = X509_new();
if (!cert) {
fprintf(stderr, "X509_new() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
/* set the public key */
if (!X509_set_pubkey(cert, pkey)) {
fprintf(stderr, "X509_set_pubkey() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
/* set the issuer's CN */
name = X509_get_subject_name(cert);
if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char *)"cert_exp_test", -1, -1, 0)) {
fprintf(stderr, "X509_NAME_add_entry_by_txt() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
if (!X509_set_issuer_name(cert, name)) {
fprintf(stderr, "X509_set_issuer_name() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
/* set SN to 1 */
if (!ASN1_INTEGER_set(X509_get_serialNumber(cert), 1)) {
fprintf(stderr, "ASN1_INTEGER_set() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
/* set the validity dates */
if (!X509_gmtime_adj(X509_get_notBefore(cert), 0) || !X509_gmtime_adj(X509_get_notAfter(cert), offset_sec)) {
fprintf(stderr, "X509_gmtime_adj() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
/* sign it using the private key */
if (!X509_sign(cert, pkey, EVP_sha256())) {
fprintf(stderr, "X509_sign() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
/* write the cert to a file */
umode = umask(0177);
fd = mkstemp(cert_path);
if (fd < 0) {
fprintf(stderr, "mkstemp() failed (%s)\n", strerror(errno));
ret = 1;
goto cleanup;
}
umask(umode);
out_bio = BIO_new_fd(fd, BIO_NOCLOSE);
if (!out_bio) {
fprintf(stderr, "BIO_new_fd() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
if (!PEM_write_bio_X509(out_bio, cert)) {
fprintf(stderr, "PEM_write_bio_X509() failed (%s)\n", openssl_strerr());
ret = 1;
goto cleanup;
}
cleanup:
if (f) {
fclose(f);
}
X509_free(cert);
EVP_PKEY_free(pkey);
BIO_free(out_bio);
return ret;
}
#endif
static void *
server_thread(void *arg)
{
NC_MSG_TYPE msgtype;
struct nc_session *session;
struct test_state *state = arg;
struct nc_pollsession *ps;
int ret;
ps = nc_ps_new();
assert_non_null(ps);
/* wait until the client is ready to connect */
pthread_barrier_wait(&state->barrier);
msgtype = nc_accept(NC_ACCEPT_TIMEOUT, server_ctx, &session);
assert_int_equal(msgtype, NC_MSG_HELLO);
/* add sess to ps */
ret = nc_ps_add_session(ps, session);
assert_int_equal(ret, 0);
/* serve all the RPCs */
do {
ret = nc_ps_poll(ps, NC_PS_POLL_TIMEOUT, NULL);
} while (ret & NC_PSPOLL_RPC);
/* increase session notification subscription flag count */
nc_session_inc_notif_status(session);
/* wait until the notif is ready to be sent */
printf("Server waiting for the certificate to expire...\n");
pthread_barrier_wait(&state->ntf_barrier);
/* send the notif */
msgtype = nc_server_notif_send(session, state->ntf, NC_SEND_NOTIF_TIMEOUT);
assert_int_equal(msgtype, NC_MSG_NOTIF);
/* wait until the client has received the notif and closed the session */
pthread_barrier_wait(&state->barrier);
nc_session_dec_notif_status(session);
nc_server_notif_free(state->ntf);
nc_ps_clear(ps, 1, NULL);
nc_ps_free(ps);
return NULL;
}
static void *
client_thread(void *arg)
{
int ret;
struct nc_session *session = NULL;
struct test_state *state = arg;
NC_MSG_TYPE msgtype;
struct lyd_node *envp, *op, *node;
/* set schema search path */
ret = nc_client_set_schema_searchpath(MODULES_DIR);
assert_int_equal(ret, 0);
/* set client cert */
ret = nc_client_tls_set_cert_key_paths(TESTS_DIR "/data/client.crt", TESTS_DIR "/data/client.key");
assert_int_equal(ret, 0);
/* set client ca */
ret = nc_client_tls_set_trusted_ca_paths(NULL, TESTS_DIR "/data");
assert_int_equal(ret, 0);
/* wait until the server is ready to accept the connection */
pthread_barrier_wait(&state->barrier);
session = nc_connect_tls("127.0.0.1", TEST_PORT, client_ctx);
assert_non_null(session);
/* receive the notif */
msgtype = nc_recv_notif(session, NC_RECV_NOTIF_TIMEOUT, &envp, &op);
assert_int_equal(msgtype, NC_MSG_NOTIF);
/* check the notif content and print the expiration date */
ret = lyd_find_path(op, "expiration-date", 0, &node);
assert_int_equal(ret, 0);
printf("Certificate expires on :%s\n", lyd_get_value(node));
/* close the session and signal the server */
lyd_free_all(envp);
lyd_free_all(op);
nc_session_free(session, NULL);
pthread_barrier_wait(&state->barrier);
return NULL;
}
static void
nc_cert_exp_notif_cb(const char *exp_time, const char *xpath, void *user_data)
{
int ret;
struct nc_server_notif *ntf = NULL;
struct lyd_node *ntf_data = NULL;
time_t ntf_time;
char *ntf_time_str = NULL;
struct test_state *state = user_data;
/* create the notification data */
ret = lyd_new_path(NULL, server_ctx, xpath, exp_time, 0, &ntf_data);
assert_int_equal(ret, 0);
/* yang time str from time_t */
ntf_time = time(NULL);
ret = ly_time_time2str(ntf_time, NULL, &ntf_time_str);
assert_int_equal(ret, 0);
/* create the notification */
ntf = nc_server_notif_new(ntf_data, ntf_time_str, NC_PARAMTYPE_FREE);
assert_non_null(ntf);
state->ntf = ntf;
/* signal the server that the notif is ready to be sent */
pthread_barrier_wait(&state->ntf_barrier);
}
static void
test_nc_cert_exp_notif_valid_cert(void **state)
{
int ret, i;
pthread_t tids[2];
struct test_state *st = *state;
char cert_path[12] = "/tmp/XXXXXX";
assert_non_null(state);
/* create a soon expiring cert and get a path to it */
ret = custom_exp_date_cert_create(VALID_CERT_ADD_SEC, cert_path);
assert_int_equal(ret, 0);
/* create new end entity client cert data */
ret = nc_server_config_add_tls_client_cert(server_ctx, "endpt", "exp_cert_test", cert_path, &st->tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
ret = nc_server_config_setup_data(st->tree);
assert_int_equal(ret, 0);
/* start the cert exp notification thread */
ret = nc_server_notif_cert_expiration_thread_start(nc_cert_exp_notif_cb, st, NULL);
assert_int_equal(ret, 0);
/* start the client and server threads */
ret = pthread_create(&tids[0], NULL, client_thread, *state);
assert_int_equal(ret, 0);
ret = pthread_create(&tids[1], NULL, server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
pthread_join(tids[i], NULL);
}
/* stop the cert exp notif thread */
nc_server_notif_cert_expiration_thread_stop(1);
}
static void
test_nc_cert_exp_notif_expired_cert(void **state)
{
int ret, i;
pthread_t tids[2];
struct test_state *st = *state;
char cert_path[12] = "/tmp/XXXXXX";
assert_non_null(state);
/* create an expired cert and get a path to it */
ret = custom_exp_date_cert_create(EXPIRED_CERT_ADD_SEC, cert_path);
assert_int_equal(ret, 0);
/* create new end entity client cert data from it */
ret = nc_server_config_add_tls_client_cert(server_ctx, "endpt", "exp_cert_test", cert_path, &st->tree);
assert_int_equal(ret, 0);
/* configure the server based on the data */
ret = nc_server_config_setup_data(st->tree);
assert_int_equal(ret, 0);
/* start the cert exp notification thread */
ret = nc_server_notif_cert_expiration_thread_start(nc_cert_exp_notif_cb, st, NULL);
assert_int_equal(ret, 0);
/* start the client and server threads */
ret = pthread_create(&tids[0], NULL, client_thread, *state);
assert_int_equal(ret, 0);
ret = pthread_create(&tids[1], NULL, server_thread, *state);
assert_int_equal(ret, 0);
for (i = 0; i < 2; i++) {
pthread_join(tids[i], NULL);
}
/* stop the cert exp notif thread */
nc_server_notif_cert_expiration_thread_stop(1);
}
static void
test_nc_cert_exp_notif_bad_interval_period(void **state)
{
int ret;
struct lyd_node *tree = NULL;
const char *invalid_data =
"<ln2-netconf-server xmlns=\"urn:cesnet:libnetconf2-netconf-server\">\n"
" <certificate-expiration-notif-intervals>\n"
" <interval>\n"
" <anchor>5d</anchor>\n"
" <period>1w</period>\n"
" </interval>\n"
" </certificate-expiration-notif-intervals>\n"
"</ln2-netconf-server>";
(void) state;
/* validating this data should fail because of a must condition, the period
* must not be bigger than the anchor (at least unit wise) */
ret = lyd_parse_data_mem(server_ctx, invalid_data, LYD_XML, 0, LYD_VALIDATE_PRESENT, &tree);
assert_int_not_equal(ret, 0);
}
static void
init_test_ctx(struct ly_ctx **ctx)
{
int ret;
struct lys_module *mod;
const char *ietf_ct_features[] = {"cleartext-passwords", "cleartext-private-keys", "certificate-expiration-notification", NULL};
ret = ly_ctx_new(MODULES_DIR, 0, ctx);
assert_int_equal(ret, 0);
ret = nc_server_init_ctx(ctx);
assert_int_equal(ret, 0);
ret = nc_server_config_load_modules(ctx);
assert_int_equal(ret, 0);
mod = ly_ctx_get_module_implemented(*ctx, "ietf-crypto-types");
assert_non_null(mod);
/* enable the certificate-expiration-notification feature */
ret = lys_set_implemented(mod, ietf_ct_features);
assert_int_equal(ret, 0);
}
static int
setup_f(void **state)
{
int ret;
struct test_state *st;
nc_verbosity(NC_VERB_VERBOSE);
/* init barriers */
st = malloc(sizeof *st);
assert_non_null(st);
ret = pthread_barrier_init(&st->barrier, NULL, 2);
assert_int_equal(ret, 0);
ret = pthread_barrier_init(&st->ntf_barrier, NULL, 2);
assert_int_equal(ret, 0);
st->tree = NULL;
st->ntf = NULL;
*state = st;
/* init server */
ret = nc_server_init();
assert_int_equal(ret, 0);
/* init client */
ret = nc_client_init();
assert_int_equal(ret, 0);
/* init server ctx */
init_test_ctx(&server_ctx);
/* init client ctx to avoid the need to implement get-schema */
init_test_ctx(&client_ctx);
/* create new address and port data */
ret = nc_server_config_add_address_port(server_ctx, "endpt", NC_TI_TLS, "127.0.0.1", TEST_PORT, &st->tree);
assert_int_equal(ret, 0);
/* create new server certificate data */
ret = nc_server_config_add_tls_server_cert(server_ctx, "endpt", TESTS_DIR "/data/server.key", NULL, TESTS_DIR "/data/server.crt", &st->tree);
assert_int_equal(ret, 0);
/* create new end entity client cert data */
ret = nc_server_config_add_tls_client_cert(server_ctx, "endpt", "client_cert", TESTS_DIR "/data/client.crt", &st->tree);
assert_int_equal(ret, 0);
/* create new client ca data */
ret = nc_server_config_add_tls_ca_cert(server_ctx, "endpt", "client_ca", TESTS_DIR "/data/serverca.pem", &st->tree);
assert_int_equal(ret, 0);
/* create new cert-to-name */
ret = nc_server_config_add_tls_ctn(server_ctx, "endpt", 1,
"04:85:6B:75:D1:1A:86:E0:D8:FE:5B:BD:72:F5:73:1D:07:EA:32:BF:09:11:21:6A:6E:23:78:8E:B6:D5:73:C3:2D",
NC_TLS_CTN_SPECIFIED, "client", &st->tree);
assert_int_equal(ret, 0);
return 0;
}
static int
teardown_f(void **state)
{
int ret = 0;
struct test_state *test_state;
assert_non_null(state);
test_state = *state;
ret = pthread_barrier_destroy(&test_state->barrier);
assert_int_equal(ret, 0);
ret = pthread_barrier_destroy(&test_state->ntf_barrier);
assert_int_equal(ret, 0);
lyd_free_all(test_state->tree);
free(*state);
nc_client_destroy();
nc_server_destroy();
ly_ctx_destroy(server_ctx);
ly_ctx_destroy(client_ctx);
return 0;
}
int
main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup_teardown(test_nc_cert_exp_notif_valid_cert, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_nc_cert_exp_notif_expired_cert, setup_f, teardown_f),
cmocka_unit_test_setup_teardown(test_nc_cert_exp_notif_bad_interval_period, setup_f, teardown_f),
};
/* try to get ports from the environment, otherwise use the default */
if (ln2_glob_test_get_ports(1, &TEST_PORT, &TEST_PORT_STR)) {
return 1;
}
setenv("CMOCKA_TEST_ABORT", "1", 1);
return cmocka_run_group_tests(tests, NULL, NULL);
}
|