check_encryption_basic128rsa15.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 <stdio.h>
  5. #include <stdlib.h>
  6. #include "check.h"
  7. #include "ua_types.h"
  8. #include "ua_server.h"
  9. #include "ua_server_internal.h"
  10. #include "ua_client.h"
  11. #include "client/ua_client_internal.h"
  12. #include "ua_securitypolicies.h"
  13. #include "ua_config_default.h"
  14. #include "ua_client_highlevel.h"
  15. #include "ua_network_tcp.h"
  16. #include "testing_clock.h"
  17. #include "testing_networklayers.h"
  18. #include "thread_wrapper.h"
  19. #include "certificates.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_basic128rsa15(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. ck_assert_msg(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_ByteString_delete(remoteCertificate);
  139. UA_Client_disconnect(client);
  140. UA_Client_delete(client);
  141. }
  142. END_TEST
  143. static Suite* testSuite_encryption(void) {
  144. Suite *s = suite_create("Encryption");
  145. TCase *tc_encryption = tcase_create("Encryption basic128rsa15");
  146. tcase_add_checked_fixture(tc_encryption, setup, teardown);
  147. #ifdef UA_ENABLE_ENCRYPTION
  148. tcase_add_test(tc_encryption, encryption_connect);
  149. #endif /* UA_ENABLE_ENCRYPTION */
  150. suite_add_tcase(s,tc_encryption);
  151. return s;
  152. }
  153. int main(void) {
  154. Suite *s = testSuite_encryption();
  155. SRunner *sr = srunner_create(s);
  156. srunner_set_fork_status(sr, CK_NOFORK);
  157. srunner_run_all(sr,CK_NORMAL);
  158. int number_failed = srunner_ntests_failed(sr);
  159. srunner_free(sr);
  160. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  161. }