check_encryption_basic256sha256.c 6.5 KB

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