check_encryption_basic128rsa15.c 5.8 KB

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