client_basic128rsa15.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
  3. #include <ua_server.h>
  4. #include <ua_config_default.h>
  5. #include <ua_log_stdout.h>
  6. #include <ua_securitypolicies.h>
  7. #include <ua_client_highlevel.h>
  8. #include "common.h"
  9. #include <signal.h>
  10. #define MIN_ARGS 3
  11. #define FAILURE 1
  12. #define CONNECTION_STRING "opc.tcp://localhost:4840"
  13. /* cleanupClient deletes the memory allocated for client configuration.
  14. *
  15. * @param client client configuration that need to be deleted
  16. * @param remoteCertificate server certificate */
  17. static void cleanupClient(UA_Client* client, UA_ByteString* remoteCertificate) {
  18. UA_ByteString_delete(remoteCertificate); /* Dereference the memory */
  19. UA_Client_delete(client); /* Disconnects the client internally */
  20. }
  21. /* main function for secure client implementation.
  22. *
  23. * @param argc count of command line variable provided
  24. * @param argv[] array of strings include certificate, private key,
  25. * trust list and revocation list
  26. * @return Return an integer representing success or failure of application */
  27. int main(int argc, char* argv[]) {
  28. UA_Client* client = NULL;
  29. UA_ByteString* remoteCertificate = NULL;
  30. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  31. size_t trustListSize = 0;
  32. UA_ByteString* revocationList = NULL;
  33. size_t revocationListSize = 0;
  34. /* endpointArray is used to hold the available endpoints in the server
  35. * endpointArraySize is used to hold the number of endpoints available */
  36. UA_EndpointDescription* endpointArray = NULL;
  37. size_t endpointArraySize = 0;
  38. if(argc < MIN_ARGS) {
  39. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  40. "The Certificate and key is missing."
  41. "The required arguments are "
  42. "<client-certificate.der> <client-private-key.der> "
  43. "[<trustlist1.crl>, ...]");
  44. return FAILURE;
  45. }
  46. /* Load certificate and private key */
  47. UA_ByteString certificate = loadFile(argv[1]);
  48. UA_ByteString privateKey = loadFile(argv[2]);
  49. /* The Get endpoint (discovery service) is done with
  50. * security mode as none to see the server's capability
  51. * and certificate */
  52. client = UA_Client_new(UA_ClientConfig_default);
  53. remoteCertificate = UA_ByteString_new();
  54. retval = UA_Client_getEndpoints(client, CONNECTION_STRING,
  55. &endpointArraySize, &endpointArray);
  56. if(retval != UA_STATUSCODE_GOOD) {
  57. UA_Array_delete(endpointArray, endpointArraySize,
  58. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  59. cleanupClient(client, remoteCertificate);
  60. return (int)retval;
  61. }
  62. UA_String securityPolicyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15");
  63. printf("%i endpoints found\n", (int)endpointArraySize);
  64. for(size_t endPointCount = 0; endPointCount < endpointArraySize; endPointCount++) {
  65. printf("URL of endpoint %i is %.*s / %.*s\n", (int)endPointCount,
  66. (int)endpointArray[endPointCount].endpointUrl.length,
  67. endpointArray[endPointCount].endpointUrl.data,
  68. (int)endpointArray[endPointCount].securityPolicyUri.length,
  69. endpointArray[endPointCount].securityPolicyUri.data);
  70. if(endpointArray[endPointCount].securityMode != UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  71. continue;
  72. if(UA_String_equal(&endpointArray[endPointCount].securityPolicyUri, &securityPolicyUri)) {
  73. UA_ByteString_copy(&endpointArray[endPointCount].serverCertificate, remoteCertificate);
  74. break;
  75. }
  76. }
  77. if(UA_ByteString_equal(remoteCertificate, &UA_BYTESTRING_NULL)) {
  78. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  79. "Server does not support Security Basic128Rsa15 Mode of"
  80. " UA_MESSAGESECURITYMODE_SIGNANDENCRYPT");
  81. cleanupClient(client, remoteCertificate);
  82. return FAILURE;
  83. }
  84. UA_Array_delete(endpointArray, endpointArraySize,
  85. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  86. UA_Client_delete(client); /* Disconnects the client internally */
  87. /* Load the trustList. Load revocationList is not supported now */
  88. if(argc > MIN_ARGS)
  89. trustListSize = (size_t)argc-MIN_ARGS;
  90. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  91. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  92. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  93. }
  94. /* Secure client initialization */
  95. client = UA_Client_secure_new(UA_ClientConfig_default,
  96. certificate, privateKey,
  97. remoteCertificate,
  98. trustList, trustListSize,
  99. revocationList, revocationListSize,
  100. UA_SecurityPolicy_Basic128Rsa15);
  101. if(client == NULL) {
  102. UA_ByteString_delete(remoteCertificate); /* Dereference the memory */
  103. return FAILURE;
  104. }
  105. UA_ByteString_clear(&certificate);
  106. UA_ByteString_clear(&privateKey);
  107. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  108. UA_ByteString_clear(&trustList[deleteCount]);
  109. }
  110. /* Secure client connect */
  111. retval = UA_Client_connect(client, CONNECTION_STRING);
  112. if(retval != UA_STATUSCODE_GOOD) {
  113. cleanupClient(client, remoteCertificate);
  114. return (int)retval;
  115. }
  116. UA_Variant value;
  117. UA_Variant_init(&value);
  118. /* NodeId of the variable holding the current time */
  119. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  120. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  121. if(retval == UA_STATUSCODE_GOOD &&
  122. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  123. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  124. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  125. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  126. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  127. }
  128. /* Clean up */
  129. UA_Variant_clear(&value);
  130. cleanupClient(client, remoteCertificate);
  131. return (int)retval;
  132. }