client_encryption.c 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 <stdlib.h>
  10. #define MIN_ARGS 4
  11. int main(int argc, char* argv[]) {
  12. if(argc < MIN_ARGS) {
  13. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  14. "Arguments are missing. The required arguments are "
  15. "<opc.tcp://host:port> "
  16. "<client-certificate.der> <client-private-key.der> "
  17. "[<trustlist1.crl>, ...]");
  18. return EXIT_FAILURE;
  19. }
  20. const char *endpointUrl = argv[1];
  21. /* Load certificate and private key */
  22. UA_ByteString certificate = loadFile(argv[2]);
  23. UA_ByteString privateKey = loadFile(argv[3]);
  24. /* Load the trustList. Load revocationList is not supported now */
  25. size_t trustListSize = 0;
  26. if(argc > MIN_ARGS)
  27. trustListSize = (size_t)argc-MIN_ARGS;
  28. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  29. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++)
  30. trustList[trustListCount] = loadFile(argv[trustListCount+4]);
  31. UA_ByteString *revocationList = NULL;
  32. size_t revocationListSize = 0;
  33. UA_Client *client = UA_Client_new();
  34. UA_ClientConfig *cc = UA_Client_getConfig(client);
  35. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  36. trustList, trustListSize,
  37. revocationList, revocationListSize);
  38. UA_ByteString_clear(&certificate);
  39. UA_ByteString_clear(&privateKey);
  40. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  41. UA_ByteString_clear(&trustList[deleteCount]);
  42. }
  43. /* Secure client connect */
  44. cc->securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT; /* require encryption */
  45. UA_StatusCode retval = UA_Client_connect(client, endpointUrl);
  46. if(retval != UA_STATUSCODE_GOOD) {
  47. UA_Client_delete(client);
  48. return EXIT_FAILURE;
  49. }
  50. UA_Variant value;
  51. UA_Variant_init(&value);
  52. /* NodeId of the variable holding the current time */
  53. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  54. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  55. if(retval == UA_STATUSCODE_GOOD &&
  56. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  57. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  58. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  59. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  60. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  61. }
  62. /* Clean up */
  63. UA_Variant_clear(&value);
  64. UA_Client_delete(client);
  65. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  66. }