/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * Copyright 2019 (c) Kalycito Infotech Private Limited * */ #include #include #include #include #include #include #include #include "client/ua_client_internal.h" #include "ua_server_internal.h" #include #include #include "../encryption/certificates.h" #include "check.h" #include "test_helpers.h" #include "testing_clock.h" #include "testing_networklayers.h" #include "thread_wrapper.h" UA_Server *server; UA_atomic(uintptr_t) running; THREAD_HANDLE server_thread; THREAD_CALLBACK(serverloop) { while(UA_atomic_load(&running)) UA_Server_run_iterate(server, true); return 0; } static void setup(void) { UA_atomic_store(&running, true); /* Load certificate and private key */ UA_ByteString certificate; certificate.length = CERT_DER_LENGTH; certificate.data = CERT_DER_DATA; UA_ByteString privateKey; privateKey.length = KEY_DER_LENGTH; privateKey.data = KEY_DER_DATA; size_t trustListSize = 0; UA_ByteString *trustList = NULL; size_t issuerListSize = 0; UA_ByteString *issuerList = NULL; UA_ByteString *revocationList = NULL; size_t revocationListSize = 0; server = UA_Server_newForUnitTestWithSecurityPolicies(4840, &certificate, &privateKey, trustList, trustListSize, issuerList, issuerListSize, revocationList, revocationListSize); ck_assert(server != NULL); UA_ServerConfig *config = UA_Server_getConfig(server); UA_CertificateGroup_AcceptAll(&config->secureChannelPKI); UA_CertificateGroup_AcceptAll(&config->sessionPKI); /* Set the ApplicationUri used in the certificate */ UA_String_clear(&config->applicationDescription.applicationUri); config->applicationDescription.applicationUri = UA_STRING_ALLOC("urn:open62541.unconfigured.application"); UA_Server_run_startup(server); THREAD_CREATE(server_thread, serverloop); } static void teardown(void) { UA_atomic_store(&running, false); THREAD_JOIN(server_thread); UA_Server_run_shutdown(server); UA_Server_delete(server); } /* An empty trust list must still result in a real (trust-nothing) certificate * verifier, not a no-op that leaves whatever verifier was previously * configured (the accept-all default) in place. */ START_TEST(encryption_emptyTrustList_rejectsUntrustedCert) { UA_ByteString certificate; certificate.length = CERT_DER_LENGTH; certificate.data = CERT_DER_DATA; UA_ByteString privateKey; privateKey.length = KEY_DER_LENGTH; privateKey.data = KEY_DER_DATA; UA_Client *client = UA_Client_newForUnitTest(); UA_ClientConfig *cc = UA_Client_getConfig(client); UA_StatusCode retval = UA_ClientConfig_setDefaultEncryption( cc, certificate, privateKey, NULL, 0, NULL, 0); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); /* Some other certificate that is not part of the (empty) trust list. */ UA_ByteString otherCert; otherCert.length = APPLICATION_CERT_DER_LENGTH; otherCert.data = APPLICATION_CERT_DER_DATA; ck_assert(cc->certificateVerification.verifyCertificate != NULL); retval = cc->certificateVerification.verifyCertificate(&cc->certificateVerification, &otherCert); ck_assert_uint_ne(retval, UA_STATUSCODE_GOOD); UA_Client_delete(client); } END_TEST /* Test re-activating a Session on a new SecureChannel */ START_TEST(encryption_reconnect_session) { UA_Client *client = NULL; UA_ByteString *trustList = NULL; size_t trustListSize = 0; UA_ByteString *revocationList = NULL; size_t revocationListSize = 0; /* Load certificate and private key */ UA_ByteString certificate; certificate.length = CERT_DER_LENGTH; certificate.data = CERT_DER_DATA; ck_assert_uint_ne(certificate.length, 0); UA_ByteString privateKey; privateKey.length = KEY_DER_LENGTH; privateKey.data = KEY_DER_DATA; ck_assert_uint_ne(privateKey.length, 0); /* Secure client initialization */ client = UA_Client_newForUnitTest(); UA_ClientConfig *cc = UA_Client_getConfig(client); UA_StatusCode retval = UA_ClientConfig_setDefaultEncryption( cc, certificate, privateKey, trustList, trustListSize, revocationList, revocationListSize); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_CertificateGroup_AcceptAll(&cc->certificateVerification); cc->securityPolicyUri = UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256"); ck_assert(client != NULL); /* Secure client connect */ retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_Variant val; UA_Variant_init(&val); UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE); retval = UA_Client_readValueAttribute(client, nodeId, &val); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_Variant_clear(&val); UA_NodeId oldAuthToken = client->authenticationToken; /* Close the SecureChannel without closing the session */ UA_Client_disconnectSecureChannel(client); /* Reconnect. This reuses the previous session. */ retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); /* Read the variable. That reactivates the session first. */ retval = UA_Client_readValueAttribute(client, nodeId, &val); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_Variant_clear(&val); ck_assert(UA_NodeId_equal(&oldAuthToken, &client->authenticationToken)); UA_Client_disconnect(client); UA_Client_delete(client); } END_TEST /* Replacing channel and authentication policies must preserve the policies * and contexts used by the current connection, including on repeated calls. */ START_TEST(encryption_setDefaultEncryption_whileConnected_doesNotCrash) { /* Use the server's test clock and allow for slow Valgrind handshakes. */ UA_Client *client = UA_Client_newForUnitTest(); ck_assert(client != NULL); /* Connect first with the default (unencrypted) configuration */ UA_StatusCode retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); /* Load certificate and private key */ UA_ByteString certificate; certificate.length = CERT_DER_LENGTH; certificate.data = CERT_DER_DATA; UA_ByteString privateKey; privateKey.length = KEY_DER_LENGTH; privateKey.data = KEY_DER_DATA; /* Replace configured policies while retaining the active policy instances. */ UA_ClientConfig *cc = UA_Client_getConfig(client); retval = UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_CertificateGroup_AcceptAll(&cc->certificateVerification); retval = UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); /* The (empty) trust list rejects every server certificate, accept the * certificate of the test server again */ UA_CertificateGroup_AcceptAll(&cc->certificateVerification); /* The still-open (unencrypted) channel must remain fully usable */ UA_Variant val; UA_Variant_init(&val); UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE); retval = UA_Client_readValueAttribute(client, nodeId, &val); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_Variant_clear(&val); /* A fresh channel using the newly configured encryption must also work */ UA_String_clear(&cc->securityPolicyUri); cc->securityPolicyUri = UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256"); UA_Client_disconnect(client); retval = UA_Client_connect(client, "opc.tcp://localhost:4840"); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); retval = UA_Client_readValueAttribute(client, nodeId, &val); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_Variant_clear(&val); /* Reconfiguration also preserves an encrypted channel and its session. */ retval = UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); retval = UA_Client_readValueAttribute(client, nodeId, &val); ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD); UA_Variant_clear(&val); UA_Client_disconnect(client); UA_Client_delete(client); } END_TEST static Suite* testSuite_encryption(void) { Suite *s = suite_create("Encryption"); TCase *tc_encryption = tcase_create("Encryption basic256sha256"); tcase_add_checked_fixture(tc_encryption, setup, teardown); tcase_add_test(tc_encryption, encryption_emptyTrustList_rejectsUntrustedCert); tcase_add_test(tc_encryption, encryption_reconnect_session); tcase_add_test(tc_encryption, encryption_setDefaultEncryption_whileConnected_doesNotCrash); suite_add_tcase(s,tc_encryption); return s; } int main(void) { Suite *s = testSuite_encryption(); SRunner *sr = srunner_create(s); srunner_set_fork_status(sr, CK_NOFORK); srunner_run_all(sr,CK_NORMAL); int number_failed = srunner_ntests_failed(sr); srunner_free(sr); return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE; }