client_basic256sha256.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. size_t trustListSize = 0;
  29. UA_ByteString* revocationList = NULL;
  30. size_t revocationListSize = 0;
  31. /* endpointArray is used to hold the available endpoints in the server
  32. * endpointArraySize is used to hold the number of endpoints available */
  33. UA_EndpointDescription* endpointArray = NULL;
  34. size_t endpointArraySize = 0;
  35. if(argc < MIN_ARGS) {
  36. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  37. "The Certificate and key is missing."
  38. "The required arguments are "
  39. "<client-certificate.der> <client-private-key.der> "
  40. "[<trustlist1.crl>, ...]");
  41. return FAILURE;
  42. }
  43. /* Load certificate and private key */
  44. UA_ByteString certificate = loadFile(argv[1]);
  45. UA_ByteString privateKey = loadFile(argv[2]);
  46. /* The Get endpoint (discovery service) is done with
  47. * security mode as none to see the server's capability
  48. * and certificate */
  49. client = UA_Client_new(UA_ClientConfig_default);
  50. remoteCertificate = UA_ByteString_new();
  51. retval = UA_Client_getEndpoints(client, CONNECTION_STRING,
  52. &endpointArraySize, &endpointArray);
  53. if(retval != UA_STATUSCODE_GOOD) {
  54. UA_Array_delete(endpointArray, endpointArraySize,
  55. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  56. cleanupClient(client, remoteCertificate);
  57. return (int)retval;
  58. }
  59. UA_String securityPolicyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256");
  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 / %.*s\n", (int)endPointCount,
  63. (int)endpointArray[endPointCount].endpointUrl.length,
  64. endpointArray[endPointCount].endpointUrl.data,
  65. (int)endpointArray[endPointCount].securityPolicyUri.length,
  66. endpointArray[endPointCount].securityPolicyUri.data);
  67. if(endpointArray[endPointCount].securityMode != UA_MESSAGESECURITYMODE_SIGNANDENCRYPT)
  68. continue;
  69. if(UA_String_equal(&endpointArray[endPointCount].securityPolicyUri, &securityPolicyUri)) {
  70. UA_ByteString_copy(&endpointArray[endPointCount].serverCertificate, remoteCertificate);
  71. break;
  72. }
  73. }
  74. if(UA_ByteString_equal(remoteCertificate, &UA_BYTESTRING_NULL)) {
  75. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  76. "Server does not support Security Basic256Sha256 Mode of"
  77. " UA_MESSAGESECURITYMODE_SIGNANDENCRYPT");
  78. cleanupClient(client, remoteCertificate);
  79. return FAILURE;
  80. }
  81. UA_Array_delete(endpointArray, endpointArraySize,
  82. &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
  83. UA_Client_delete(client); /* Disconnects the client internally */
  84. /* Load the trustList. Load revocationList is not supported now */
  85. if(argc > MIN_ARGS)
  86. trustListSize = (size_t)argc-MIN_ARGS;
  87. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  88. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++) {
  89. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  90. }
  91. /* Secure client initialization */
  92. client = UA_Client_secure_new(UA_ClientConfig_default,
  93. certificate, privateKey,
  94. remoteCertificate,
  95. trustList, trustListSize,
  96. revocationList, revocationListSize,
  97. UA_SecurityPolicy_Basic256Sha256);
  98. if(client == NULL) {
  99. UA_ByteString_delete(remoteCertificate); /* Dereference the memory */
  100. return FAILURE;
  101. }
  102. UA_ByteString_deleteMembers(&certificate);
  103. UA_ByteString_deleteMembers(&privateKey);
  104. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  105. UA_ByteString_deleteMembers(&trustList[deleteCount]);
  106. }
  107. /* Secure client connect */
  108. retval = UA_Client_connect(client, CONNECTION_STRING);
  109. if(retval != UA_STATUSCODE_GOOD) {
  110. cleanupClient(client, remoteCertificate);
  111. return (int)retval;
  112. }
  113. UA_Variant value;
  114. UA_Variant_init(&value);
  115. /* NodeId of the variable holding the current time */
  116. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  117. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  118. if(retval == UA_STATUSCODE_GOOD &&
  119. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  120. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  121. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  122. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  123. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  124. }
  125. /* Clean up */
  126. UA_Variant_deleteMembers(&value);
  127. cleanupClient(client, remoteCertificate);
  128. return (int)retval;
  129. }