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