check_encryption_basic128rsa15.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. *
  5. * Copyright 2019 (c) Kalycito Infotech Private Limited
  6. *
  7. */
  8. #include <open62541/client_config_default.h>
  9. #include <open62541/plugin/securitypolicy_default.h>
  10. #include <open62541/server_config_default.h>
  11. #include "client/ua_client_internal.h"
  12. #include "ua_server_internal.h"
  13. #include <check.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "certificates.h"
  17. #include "testing_clock.h"
  18. #include "testing_networklayers.h"
  19. #include "thread_wrapper.h"
  20. UA_Server *server;
  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. /* Load the issuerList */
  42. size_t issuerListSize = 0;
  43. UA_ByteString *issuerList = NULL;
  44. /* TODO test trustList
  45. if(argc > 3)
  46. trustListSize = (size_t)argc-3;
  47. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  48. for(size_t i = 0; i < trustListSize; i++)
  49. trustList[i] = loadFile(argv[i+3]);
  50. */
  51. /* Loading of a revocation list currently unsupported */
  52. UA_ByteString *revocationList = NULL;
  53. size_t revocationListSize = 0;
  54. server = UA_Server_new();
  55. UA_ServerConfig_setDefaultWithSecurityPolicies(UA_Server_getConfig(server),
  56. 4840, &certificate, &privateKey,
  57. trustList, trustListSize,
  58. issuerList, issuerListSize,
  59. revocationList, revocationListSize);
  60. for(size_t i = 0; i < trustListSize; i++)
  61. UA_ByteString_deleteMembers(&trustList[i]);
  62. UA_Server_run_startup(server);
  63. THREAD_CREATE(server_thread, serverloop);
  64. }
  65. static void teardown(void) {
  66. running = false;
  67. THREAD_JOIN(server_thread);
  68. UA_Server_run_shutdown(server);
  69. UA_Server_delete(server);
  70. }
  71. START_TEST(encryption_connect) {
  72. UA_Client *client = NULL;
  73. UA_EndpointDescription* endpointArray = NULL;
  74. size_t endpointArraySize = 0;
  75. UA_ByteString *trustList = NULL;
  76. size_t trustListSize = 0;
  77. UA_ByteString *revocationList = NULL;
  78. size_t revocationListSize = 0;
  79. /* Load certificate and private key */
  80. UA_ByteString certificate;
  81. certificate.length = CERT_DER_LENGTH;
  82. certificate.data = CERT_DER_DATA;
  83. ck_assert_int_ne(certificate.length, 0);
  84. UA_ByteString privateKey;
  85. privateKey.length = KEY_DER_LENGTH;
  86. privateKey.data = KEY_DER_DATA;
  87. ck_assert_int_ne(privateKey.length, 0);
  88. /* The Get endpoint (discovery service) is done with
  89. * security mode as none to see the server's capability
  90. * and certificate */
  91. client = UA_Client_new();
  92. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  93. ck_assert(client != NULL);
  94. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  95. &endpointArraySize, &endpointArray);
  96. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  97. ck_assert(endpointArraySize > 0);
  98. UA_Array_delete(endpointArray, endpointArraySize,
  99. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  100. /* TODO test trustList Load revocationList is not supported now
  101. if(argc > MIN_ARGS) {
  102. trustListSize = (size_t)argc-MIN_ARGS;
  103. retval = UA_ByteString_allocBuffer(trustList, trustListSize);
  104. if(retval != UA_STATUSCODE_GOOD) {
  105. cleanupClient(client, remoteCertificate);
  106. return (int)retval;
  107. }
  108. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  109. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  110. }
  111. }
  112. */
  113. UA_Client_delete(client);
  114. /* Secure client initialization */
  115. client = UA_Client_new();
  116. UA_ClientConfig *cc = UA_Client_getConfig(client);
  117. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  118. trustList, trustListSize,
  119. revocationList, revocationListSize);
  120. cc->securityPolicyUri =
  121. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  122. ck_assert(client != NULL);
  123. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  124. UA_ByteString_deleteMembers(&trustList[deleteCount]);
  125. }
  126. /* Secure client connect */
  127. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  128. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  129. UA_Variant val;
  130. UA_Variant_init(&val);
  131. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  132. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  133. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  134. UA_Variant_deleteMembers(&val);
  135. UA_Client_disconnect(client);
  136. UA_Client_delete(client);
  137. }
  138. END_TEST
  139. static Suite* testSuite_encryption(void) {
  140. Suite *s = suite_create("Encryption");
  141. TCase *tc_encryption = tcase_create("Encryption basic128rsa15");
  142. tcase_add_checked_fixture(tc_encryption, setup, teardown);
  143. #ifdef UA_ENABLE_ENCRYPTION
  144. tcase_add_test(tc_encryption, encryption_connect);
  145. #endif /* UA_ENABLE_ENCRYPTION */
  146. suite_add_tcase(s,tc_encryption);
  147. return s;
  148. }
  149. int main(void) {
  150. Suite *s = testSuite_encryption();
  151. SRunner *sr = srunner_create(s);
  152. srunner_set_fork_status(sr, CK_NOFORK);
  153. srunner_run_all(sr,CK_NORMAL);
  154. int number_failed = srunner_ntests_failed(sr);
  155. srunner_free(sr);
  156. return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
  157. }