check_encryption_basic256sha256.c 5.9 KB

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