check_encryption_basic256sha256.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2019 (c) Kalycito Infotech Private Limited
  6. *
  7. */
  8. #include <open62541/client.h>
  9. #include <open62541/client_config_default.h>
  10. #include <open62541/client_highlevel.h>
  11. #include <open62541/plugin/securitypolicy.h>
  12. #include <open62541/server.h>
  13. #include <open62541/server_config_default.h>
  14. #include "client/ua_client_internal.h"
  15. #include "ua_server_internal.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "certificates.h"
  19. #include "check.h"
  20. #include "testing_clock.h"
  21. #include "testing_networklayers.h"
  22. #include "thread_wrapper.h"
  23. UA_Server *server;
  24. UA_Boolean running;
  25. UA_ServerNetworkLayer nl;
  26. THREAD_HANDLE server_thread;
  27. THREAD_CALLBACK(serverloop) {
  28. while(running)
  29. UA_Server_run_iterate(server, true);
  30. return 0;
  31. }
  32. static void setup(void) {
  33. running = true;
  34. /* Load certificate and private key */
  35. UA_ByteString certificate;
  36. certificate.length = CERT_DER_LENGTH;
  37. certificate.data = CERT_DER_DATA;
  38. UA_ByteString privateKey;
  39. privateKey.length = KEY_DER_LENGTH;
  40. privateKey.data = KEY_DER_DATA;
  41. /* Load the trustlist */
  42. size_t trustListSize = 0;
  43. UA_ByteString *trustList = NULL;
  44. /* Load the issuerList */
  45. size_t issuerListSize = 0;
  46. UA_ByteString *issuerList = NULL;
  47. /* TODO test trustList
  48. if(argc > 3)
  49. trustListSize = (size_t)argc-3;
  50. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  51. for(size_t i = 0; i < trustListSize; i++)
  52. trustList[i] = loadFile(argv[i+3]);
  53. */
  54. /* Loading of a revocation list currently unsupported */
  55. UA_ByteString *revocationList = NULL;
  56. size_t revocationListSize = 0;
  57. server = UA_Server_new();
  58. UA_ServerConfig *config = UA_Server_getConfig(server);
  59. UA_ServerConfig_setDefaultWithSecurityPolicies(config, 4840, &certificate, &privateKey,
  60. trustList, trustListSize,
  61. issuerList, issuerListSize,
  62. revocationList, revocationListSize);
  63. /* Set the ApplicationUri used in the certificate */
  64. UA_String_clear(&config->applicationDescription.applicationUri);
  65. config->applicationDescription.applicationUri =
  66. UA_STRING_ALLOC("urn:unconfigured:application");
  67. for(size_t i = 0; i < trustListSize; i++)
  68. UA_ByteString_deleteMembers(&trustList[i]);
  69. UA_Server_run_startup(server);
  70. THREAD_CREATE(server_thread, serverloop);
  71. }
  72. static void teardown(void) {
  73. running = false;
  74. THREAD_JOIN(server_thread);
  75. UA_Server_run_shutdown(server);
  76. UA_Server_delete(server);
  77. }
  78. START_TEST(encryption_connect) {
  79. UA_Client *client = NULL;
  80. UA_EndpointDescription* endpointArray = NULL;
  81. size_t endpointArraySize = 0;
  82. UA_ByteString *trustList = NULL;
  83. size_t trustListSize = 0;
  84. UA_ByteString *revocationList = NULL;
  85. size_t revocationListSize = 0;
  86. /* Load certificate and private key */
  87. UA_ByteString certificate;
  88. certificate.length = CERT_DER_LENGTH;
  89. certificate.data = CERT_DER_DATA;
  90. ck_assert_int_ne(certificate.length, 0);
  91. UA_ByteString privateKey;
  92. privateKey.length = KEY_DER_LENGTH;
  93. privateKey.data = KEY_DER_DATA;
  94. ck_assert_int_ne(privateKey.length, 0);
  95. /* The Get endpoint (discovery service) is done with
  96. * security mode as none to see the server's capability
  97. * and certificate */
  98. client = UA_Client_new();
  99. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  100. ck_assert(client != NULL);
  101. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  102. &endpointArraySize, &endpointArray);
  103. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  104. ck_assert(endpointArraySize > 0);
  105. UA_Array_delete(endpointArray, endpointArraySize,
  106. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  107. /* TODO test trustList Load revocationList is not supported now
  108. if(argc > MIN_ARGS) {
  109. trustListSize = (size_t)argc-MIN_ARGS;
  110. retval = UA_ByteString_allocBuffer(trustList, trustListSize);
  111. if(retval != UA_STATUSCODE_GOOD) {
  112. cleanupClient(client, remoteCertificate);
  113. return (int)retval;
  114. }
  115. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  116. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  117. }
  118. }
  119. */
  120. UA_Client_delete(client);
  121. /* Secure client initialization */
  122. client = UA_Client_new();
  123. UA_ClientConfig *cc = UA_Client_getConfig(client);
  124. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  125. trustList, trustListSize,
  126. revocationList, revocationListSize);
  127. cc->securityPolicyUri =
  128. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
  129. ck_assert(client != NULL);
  130. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  131. UA_ByteString_deleteMembers(&trustList[deleteCount]);
  132. }
  133. /* Secure client connect */
  134. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  135. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  136. UA_Variant val;
  137. UA_Variant_init(&val);
  138. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  139. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  140. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  141. UA_Variant_deleteMembers(&val);
  142. UA_Client_disconnect(client);
  143. UA_Client_delete(client);
  144. }
  145. END_TEST
  146. static Suite* testSuite_encryption(void) {
  147. Suite *s = suite_create("Encryption");
  148. TCase *tc_encryption = tcase_create("Encryption basic256sha256");
  149. tcase_add_checked_fixture(tc_encryption, setup, teardown);
  150. #ifdef UA_ENABLE_ENCRYPTION
  151. tcase_add_test(tc_encryption, encryption_connect);
  152. #endif /* UA_ENABLE_ENCRYPTION */
  153. suite_add_tcase(s,tc_encryption);
  154. return s;
  155. }
  156. int main(void) {
  157. Suite *s = testSuite_encryption();
  158. SRunner *sr = srunner_create(s);
  159. srunner_set_fork_status(sr, CK_NOFORK);
  160. srunner_run_all(sr,CK_NORMAL);
  161. int number_failed = srunner_ntests_failed(sr);
  162. srunner_free(sr);
  163. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  164. }