client_encryption.c 3.2 KB

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