client_access_control_encrypt.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. *
  4. * Copyright (c) 2019 Kalycito Infotech Private Limited
  5. *
  6. */
  7. #include <open62541/client_config_default.h>
  8. #include <open62541/client_highlevel.h>
  9. #include <open62541/plugin/log_stdout.h>
  10. #include <open62541/plugin/securitypolicy.h>
  11. #include <stdlib.h>
  12. #include "common.h"
  13. #define MIN_ARGS 4
  14. int main(int argc, char* argv[]) {
  15. if(argc < MIN_ARGS) {
  16. UA_LOG_FATAL(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
  17. "Arguments are missing. The required arguments are "
  18. "<opc.tcp://host:port> "
  19. "<client-certificate.der> <client-private-key.der> "
  20. "[<trustlist1.crl>, ...]");
  21. return EXIT_FAILURE;
  22. }
  23. const char *endpointUrl = argv[1];
  24. /* Load certificate and private key */
  25. UA_ByteString certificate = loadFile(argv[2]);
  26. UA_ByteString privateKey = loadFile(argv[3]);
  27. /* Load the trustList. Load revocationList is not supported now */
  28. size_t trustListSize = 0;
  29. if(argc > MIN_ARGS)
  30. trustListSize = (size_t)argc-MIN_ARGS;
  31. UA_STACKARRAY(UA_ByteString, trustList, trustListSize);
  32. for(size_t trustListCount = 0; trustListCount < trustListSize; trustListCount++)
  33. trustList[trustListCount] = loadFile(argv[trustListCount+4]);
  34. UA_ByteString *revocationList = NULL;
  35. size_t revocationListSize = 0;
  36. UA_Client *client = UA_Client_new();
  37. UA_ClientConfig *config = UA_Client_getConfig(client);
  38. config->securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT;
  39. UA_ClientConfig_setDefaultEncryption(config, certificate, privateKey,
  40. trustList, trustListSize,
  41. revocationList, revocationListSize);
  42. UA_ByteString_clear(&certificate);
  43. UA_ByteString_clear(&privateKey);
  44. for(size_t deleteCount = 0; deleteCount < trustListSize; deleteCount++) {
  45. UA_ByteString_clear(&trustList[deleteCount]);
  46. }
  47. UA_StatusCode retval = UA_Client_connect_username(client, endpointUrl, "paula", "paula123");
  48. if(retval != UA_STATUSCODE_GOOD) {
  49. UA_Client_delete(client);
  50. return EXIT_FAILURE;
  51. }
  52. UA_NodeId newVariableIdRequest = UA_NODEID_NUMERIC(1, 1001);
  53. UA_NodeId newVariableId = UA_NODEID_NULL;
  54. UA_VariableAttributes newVariableAttributes = UA_VariableAttributes_default;
  55. newVariableAttributes.accessLevel = UA_ACCESSLEVELMASK_READ;
  56. newVariableAttributes.description = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable desc");
  57. newVariableAttributes.displayName = UA_LOCALIZEDTEXT_ALLOC("en-US", "NewVariable");
  58. newVariableAttributes.dataType = UA_TYPES[UA_TYPES_UINT32].typeId;
  59. UA_UInt32 value = 50;
  60. UA_Variant_setScalarCopy(&newVariableAttributes.value, &value, &UA_TYPES[UA_TYPES_UINT32]);
  61. UA_StatusCode retCode;
  62. retCode = UA_Client_addVariableNode(client, newVariableIdRequest,
  63. UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  64. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
  65. UA_QUALIFIEDNAME(1, "newVariable"),
  66. UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE),
  67. newVariableAttributes, &newVariableId);
  68. printf("addVariable returned: %s\n", UA_StatusCode_name(retCode));
  69. UA_ExpandedNodeId extNodeId = UA_EXPANDEDNODEID_NUMERIC(0, 0);
  70. extNodeId.nodeId = newVariableId;
  71. retCode = UA_Client_addReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  72. UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), UA_TRUE,
  73. UA_STRING_NULL, extNodeId, UA_NODECLASS_VARIABLE);
  74. printf("addReference returned: %s\n", UA_StatusCode_name(retCode));
  75. retCode = UA_Client_deleteReference(client, UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
  76. UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES), UA_TRUE, extNodeId,
  77. UA_TRUE);
  78. printf("deleteReference returned: %s\n", UA_StatusCode_name(retCode));
  79. retCode = UA_Client_deleteNode(client, newVariableId, UA_TRUE);
  80. printf("deleteNode returned: %s\n", UA_StatusCode_name(retCode));
  81. /* Clean up */
  82. UA_VariableAttributes_clear(&newVariableAttributes);
  83. UA_Client_delete(client); /* Disconnects the client internally */
  84. return EXIT_SUCCESS;
  85. }