check_encryption_basic128rsa15.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_config_default.h>
  5. #include <open62541/plugin/securitypolicy_default.h>
  6. #include <open62541/server_config_default.h>
  7. #include "client/ua_client_internal.h"
  8. #include "ua_network_tcp.h"
  9. #include "ua_server_internal.h"
  10. #include <check.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include "certificates.h"
  14. #include "testing_clock.h"
  15. #include "testing_networklayers.h"
  16. #include "thread_wrapper.h"
  17. UA_Server *server;
  18. UA_ServerConfig *config;
  19. UA_Boolean running;
  20. UA_ServerNetworkLayer nl;
  21. THREAD_HANDLE server_thread;
  22. THREAD_CALLBACK(serverloop) {
  23. while(running)
  24. UA_Server_run_iterate(server, true);
  25. return 0;
  26. }
  27. static void setup(void) {
  28. running = true;
  29. /* Load certificate and private key */
  30. UA_ByteString certificate;
  31. certificate.length = CERT_DER_LENGTH;
  32. certificate.data = CERT_DER_DATA;
  33. UA_ByteString privateKey;
  34. privateKey.length = KEY_DER_LENGTH;
  35. privateKey.data = KEY_DER_DATA;
  36. /* Load the trustlist */
  37. size_t trustListSize = 0;
  38. UA_ByteString *trustList = NULL;
  39. /* TODO test trustList
  40. if(argc > 3)
  41. trustListSize = (size_t)argc-3;
  42. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  43. for(size_t i = 0; i < trustListSize; i++)
  44. trustList[i] = loadFile(argv[i+3]);
  45. */
  46. /* Loading of a revocation list currently unsupported */
  47. UA_ByteString *revocationList = NULL;
  48. size_t revocationListSize = 0;
  49. config = UA_ServerConfig_new_basic128rsa15(4840, &certificate, &privateKey,
  50. trustList, trustListSize,
  51. revocationList, revocationListSize);
  52. for(size_t i = 0; i < trustListSize; i++)
  53. UA_ByteString_deleteMembers(&trustList[i]);
  54. server = UA_Server_new(config);
  55. UA_Server_run_startup(server);
  56. THREAD_CREATE(server_thread, serverloop);
  57. }
  58. static void teardown(void) {
  59. running = false;
  60. THREAD_JOIN(server_thread);
  61. UA_Server_run_shutdown(server);
  62. UA_Server_delete(server);
  63. UA_ServerConfig_delete(config);
  64. }
  65. START_TEST(encryption_connect) {
  66. UA_Client *client = NULL;
  67. UA_EndpointDescription* endpointArray = NULL;
  68. size_t endpointArraySize = 0;
  69. UA_ByteString *trustList = NULL;
  70. size_t trustListSize = 0;
  71. UA_ByteString *revocationList = NULL;
  72. size_t revocationListSize = 0;
  73. UA_ByteString *remoteCertificate = NULL;
  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_msg(client != NULL);
  89. remoteCertificate = UA_ByteString_new();
  90. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  91. &endpointArraySize, &endpointArray);
  92. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  93. for(size_t endPointCount = 0; endPointCount < endpointArraySize; endPointCount++) {
  94. if(endpointArray[endPointCount].securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  95. UA_ByteString_copy(&endpointArray[endPointCount].serverCertificate, remoteCertificate);
  96. }
  97. if(UA_ByteString_equal(remoteCertificate, &UA_BYTESTRING_NULL)) {
  98. ck_abort_msg("Server does not support Security Mode of UA_MESSAGESECURITYMODE_SIGNANDENCRYPT");
  99. }
  100. UA_Array_delete(endpointArray, endpointArraySize,
  101. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  102. /* TODO test trustList Load revocationList is not supported now
  103. if(argc > MIN_ARGS) {
  104. trustListSize = (size_t)argc-MIN_ARGS;
  105. retval = UA_ByteString_allocBuffer(trustList, trustListSize);
  106. if(retval != UA_STATUSCODE_GOOD) {
  107. cleanupClient(client, remoteCertificate);
  108. return (int)retval;
  109. }
  110. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  111. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  112. }
  113. }
  114. */
  115. UA_Client_delete(client);
  116. /* Secure client initialization */
  117. client = UA_Client_new();
  118. UA_ClientConfig *cc = UA_Client_getConfig(client);
  119. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  120. trustList, trustListSize,
  121. revocationList, revocationListSize);
  122. cc->securityPolicyUri =
  123. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  124. ck_assert_msg(client != NULL);
  125. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  126. UA_ByteString_deleteMembers(&trustList[deleteCount]);
  127. }
  128. /* Secure client connect */
  129. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  130. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  131. UA_Variant val;
  132. UA_Variant_init(&val);
  133. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  134. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  135. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  136. UA_Variant_deleteMembers(&val);
  137. UA_ByteString_delete(remoteCertificate);
  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 basic128rsa15");
  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. }