check_encryption_basic256sha256.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_setDefaultWithSecurityPolicies(UA_Server_getConfig(server),
  59. 4840, &certificate, &privateKey,
  60. trustList, trustListSize,
  61. issuerList, issuerListSize,
  62. revocationList, revocationListSize);
  63. for(size_t i = 0; i < trustListSize; i++)
  64. UA_ByteString_deleteMembers(&trustList[i]);
  65. UA_Server_run_startup(server);
  66. THREAD_CREATE(server_thread, serverloop);
  67. }
  68. static void teardown(void) {
  69. running = false;
  70. THREAD_JOIN(server_thread);
  71. UA_Server_run_shutdown(server);
  72. UA_Server_delete(server);
  73. }
  74. START_TEST(encryption_connect) {
  75. UA_Client *client = NULL;
  76. UA_EndpointDescription* endpointArray = NULL;
  77. size_t endpointArraySize = 0;
  78. UA_ByteString *trustList = NULL;
  79. size_t trustListSize = 0;
  80. UA_ByteString *revocationList = NULL;
  81. size_t revocationListSize = 0;
  82. /* Load certificate and private key */
  83. UA_ByteString certificate;
  84. certificate.length = CERT_DER_LENGTH;
  85. certificate.data = CERT_DER_DATA;
  86. ck_assert_int_ne(certificate.length, 0);
  87. UA_ByteString privateKey;
  88. privateKey.length = KEY_DER_LENGTH;
  89. privateKey.data = KEY_DER_DATA;
  90. ck_assert_int_ne(privateKey.length, 0);
  91. /* The Get endpoint (discovery service) is done with
  92. * security mode as none to see the server's capability
  93. * and certificate */
  94. client = UA_Client_new();
  95. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  96. ck_assert(client != NULL);
  97. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  98. &endpointArraySize, &endpointArray);
  99. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  100. ck_assert(endpointArraySize > 0);
  101. UA_Array_delete(endpointArray, endpointArraySize,
  102. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  103. /* TODO test trustList Load revocationList is not supported now
  104. if(argc > MIN_ARGS) {
  105. trustListSize = (size_t)argc-MIN_ARGS;
  106. retval = UA_ByteString_allocBuffer(trustList, trustListSize);
  107. if(retval != UA_STATUSCODE_GOOD) {
  108. cleanupClient(client, remoteCertificate);
  109. return (int)retval;
  110. }
  111. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  112. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  113. }
  114. }
  115. */
  116. UA_Client_delete(client);
  117. /* Secure client initialization */
  118. client = UA_Client_new();
  119. UA_ClientConfig *cc = UA_Client_getConfig(client);
  120. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  121. trustList, trustListSize,
  122. revocationList, revocationListSize);
  123. cc->securityPolicyUri =
  124. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
  125. ck_assert(client != NULL);
  126. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  127. UA_ByteString_deleteMembers(&trustList[deleteCount]);
  128. }
  129. /* Secure client connect */
  130. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  131. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  132. UA_Variant val;
  133. UA_Variant_init(&val);
  134. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  135. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  136. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  137. UA_Variant_deleteMembers(&val);
  138. UA_Client_disconnect(client);
  139. UA_Client_delete(client);
  140. }
  141. END_TEST
  142. static Suite* testSuite_encryption(void) {
  143. Suite *s = suite_create("Encryption");
  144. TCase *tc_encryption = tcase_create("Encryption basic256sha256");
  145. tcase_add_checked_fixture(tc_encryption, setup, teardown);
  146. #ifdef UA_ENABLE_ENCRYPTION
  147. tcase_add_test(tc_encryption, encryption_connect);
  148. #endif /* UA_ENABLE_ENCRYPTION */
  149. suite_add_tcase(s,tc_encryption);
  150. return s;
  151. }
  152. int main(void) {
  153. Suite *s = testSuite_encryption();
  154. SRunner *sr = srunner_create(s);
  155. srunner_set_fork_status(sr, CK_NOFORK);
  156. srunner_run_all(sr,CK_NORMAL);
  157. int number_failed = srunner_ntests_failed(sr);
  158. srunner_free(sr);
  159. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  160. }