client_basic128rsa15.c 6.3 KB

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