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