client_encryption.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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://192.168.56.1:51510"
  13. /* main function for secure client implementation.
  14. *
  15. * @param argc count of command line variable provided
  16. * @param argv[] array of strings include certificate, private key,
  17. * trust list and revocation list
  18. * @return Return an integer representing success or failure of application */
  19. int main(int argc, char* argv[]) {
  20. UA_Client* client = NULL;
  21. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  22. UA_ByteString* revocationList = NULL;
  23. size_t revocationListSize = 0;
  24. size_t trustListSize = 0;
  25. if(argc > MIN_ARGS)
  26. trustListSize = (size_t)argc-MIN_ARGS;
  27. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  28. if(argc < MIN_ARGS) {
  29. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  30. "The Certificate and key is missing."
  31. "The required arguments are "
  32. "<client-certificate.der> <client-private-key.der> "
  33. "[<trustlist1.crl>, ...]");
  34. return FAILURE;
  35. }
  36. /* Load certificate and private key */
  37. UA_ByteString certificate = loadFile(argv[1]);
  38. UA_ByteString privateKey = loadFile(argv[2]);
  39. /* Load the trustList. Load revocationList is not supported now */
  40. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++)
  41. trustList[trustListCount] = loadFile(argv[trustListCount+3]);
  42. client = UA_Client_new();
  43. UA_ClientConfig_setDefaultEncryption(UA_Client_getConfig(client),
  44. certificate, privateKey,
  45. trustList, trustListSize,
  46. revocationList, revocationListSize);
  47. UA_ByteString_clear(&certificate);
  48. UA_ByteString_clear(&privateKey);
  49. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  50. UA_ByteString_clear(&trustList[deleteCount]);
  51. }
  52. /* Secure client connect */
  53. //retval = UA_Client_connect(client, CONNECTION_STRING);
  54. retval = UA_Client_connect_username(client, CONNECTION_STRING, "usr", "pwd");
  55. if(retval != UA_STATUSCODE_GOOD) {
  56. UA_Client_delete(client);
  57. return (int)retval;
  58. }
  59. UA_Variant value;
  60. UA_Variant_init(&value);
  61. /* NodeId of the variable holding the current time */
  62. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  63. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  64. if(retval == UA_STATUSCODE_GOOD &&
  65. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  66. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  67. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  68. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  69. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  70. }
  71. /* Clean up */
  72. UA_Variant_clear(&value);
  73. UA_Client_delete(client);
  74. return (int)retval;
  75. }