check_encryption_basic128rsa15.c 6.3 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. *
  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 *config = UA_Server_getConfig(server);
  56. UA_ServerConfig_setDefaultWithSecurityPolicies(config, 4840, &certificate, &privateKey,
  57. trustList, trustListSize,
  58. issuerList, issuerListSize,
  59. revocationList, revocationListSize);
  60. /* Set the ApplicationUri used in the certificate */
  61. UA_String_clear(&config->applicationDescription.applicationUri);
  62. config->applicationDescription.applicationUri =
  63. UA_STRING_ALLOC("urn:unconfigured:application");
  64. for(size_t i = 0; i < trustListSize; i++)
  65. UA_ByteString_deleteMembers(&trustList[i]);
  66. UA_Server_run_startup(server);
  67. THREAD_CREATE(server_thread, serverloop);
  68. }
  69. static void teardown(void) {
  70. running = false;
  71. THREAD_JOIN(server_thread);
  72. UA_Server_run_shutdown(server);
  73. UA_Server_delete(server);
  74. }
  75. START_TEST(encryption_connect) {
  76. UA_Client *client = NULL;
  77. UA_EndpointDescription* endpointArray = NULL;
  78. size_t endpointArraySize = 0;
  79. UA_ByteString *trustList = NULL;
  80. size_t trustListSize = 0;
  81. UA_ByteString *revocationList = NULL;
  82. size_t revocationListSize = 0;
  83. /* Load certificate and private key */
  84. UA_ByteString certificate;
  85. certificate.length = CERT_DER_LENGTH;
  86. certificate.data = CERT_DER_DATA;
  87. ck_assert_int_ne(certificate.length, 0);
  88. UA_ByteString privateKey;
  89. privateKey.length = KEY_DER_LENGTH;
  90. privateKey.data = KEY_DER_DATA;
  91. ck_assert_int_ne(privateKey.length, 0);
  92. /* The Get endpoint (discovery service) is done with
  93. * security mode as none to see the server's capability
  94. * and certificate */
  95. client = UA_Client_new();
  96. UA_ClientConfig_setDefault(UA_Client_getConfig(client));
  97. ck_assert(client != NULL);
  98. UA_StatusCode retval = UA_Client_getEndpoints(client, "opc.tcp://localhost:4840",
  99. &endpointArraySize, &endpointArray);
  100. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  101. ck_assert(endpointArraySize > 0);
  102. UA_Array_delete(endpointArray, endpointArraySize,
  103. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  104. /* TODO test trustList Load revocationList is not supported now
  105. if(argc > MIN_ARGS) {
  106. trustListSize = (size_t)argc-MIN_ARGS;
  107. retval = UA_ByteString_allocBuffer(trustList, trustListSize);
  108. if(retval != UA_STATUSCODE_GOOD) {
  109. cleanupClient(client, remoteCertificate);
  110. return (int)retval;
  111. }
  112. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  113. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  114. }
  115. }
  116. */
  117. UA_Client_delete(client);
  118. /* Secure client initialization */
  119. client = UA_Client_new();
  120. UA_ClientConfig *cc = UA_Client_getConfig(client);
  121. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  122. trustList, trustListSize,
  123. revocationList, revocationListSize);
  124. cc->securityPolicyUri =
  125. UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  126. ck_assert(client != NULL);
  127. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  128. UA_ByteString_deleteMembers(&trustList[deleteCount]);
  129. }
  130. /* Secure client connect */
  131. retval = UA_Client_connect(client, "opc.tcp://localhost:4840");
  132. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  133. UA_Variant val;
  134. UA_Variant_init(&val);
  135. UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_STATE);
  136. retval = UA_Client_readValueAttribute(client, nodeId, &val);
  137. ck_assert_uint_eq(retval, UA_STATUSCODE_GOOD);
  138. UA_Variant_deleteMembers(&val);
  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. }