client_encryption.c 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. cc->securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT;
  36. UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey,
  37. trustList, trustListSize,
  38. revocationList, revocationListSize);
  39. UA_ByteString_clear(&certificate);
  40. UA_ByteString_clear(&privateKey);
  41. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  42. UA_ByteString_clear(&trustList[deleteCount]);
  43. }
  44. /* Secure client connect */
  45. cc->securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT; /* require encryption */
  46. UA_StatusCode retval = UA_Client_connect(client, endpointUrl);
  47. if(retval != UA_STATUSCODE_GOOD) {
  48. UA_Client_delete(client);
  49. return EXIT_FAILURE;
  50. }
  51. UA_Variant value;
  52. UA_Variant_init(&value);
  53. /* NodeId of the variable holding the current time */
  54. const UA_NodeId nodeId = UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
  55. retval = UA_Client_readValueAttribute(client, nodeId, &value);
  56. if(retval == UA_STATUSCODE_GOOD &&
  57. UA_Variant_hasScalarType(&value, &UA_TYPES[UA_TYPES_DATETIME])) {
  58. UA_DateTime raw_date = *(UA_DateTime *) value.data;
  59. UA_DateTimeStruct dts = UA_DateTime_toStruct(raw_date);
  60. UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "date is: %u-%u-%u %u:%u:%u.%03u\n",
  61. dts.day, dts.month, dts.year, dts.hour, dts.min, dts.sec, dts.milliSec);
  62. }
  63. /* Clean up */
  64. UA_Variant_clear(&value);
  65. UA_Client_delete(client);
  66. return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
  67. }