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