| 115 | */ |
| 116 | |
| 117 | Client::Client(std::shared_ptr<core::logging::Logger> logger, const std::string& applicationURI, |
| 118 | const std::vector<char>& certBuffer, const std::vector<char>& keyBuffer, |
| 119 | const std::vector<std::vector<char>>& trustBuffers) { |
| 120 | |
| 121 | client_ = UA_Client_new(); |
| 122 | if (certBuffer.empty()) { |
| 123 | UA_ClientConfig_setDefault(UA_Client_getConfig(client_)); |
| 124 | } else { |
| 125 | UA_ClientConfig *cc = UA_Client_getConfig(client_); |
| 126 | cc->securityMode = UA_MESSAGESECURITYMODE_SIGNANDENCRYPT; |
| 127 | |
| 128 | // Certificate |
| 129 | UA_ByteString certByteString = UA_STRING_NULL; |
| 130 | certByteString.length = certBuffer.size(); |
| 131 | certByteString.data = (UA_Byte*)UA_malloc(certByteString.length * sizeof(UA_Byte)); |
| 132 | memcpy(certByteString.data, certBuffer.data(), certByteString.length); |
| 133 | |
| 134 | // Key |
| 135 | UA_ByteString keyByteString = UA_STRING_NULL; |
| 136 | keyByteString.length = keyBuffer.size(); |
| 137 | keyByteString.data = (UA_Byte*)UA_malloc(keyByteString.length * sizeof(UA_Byte)); |
| 138 | memcpy(keyByteString.data, keyBuffer.data(), keyByteString.length); |
| 139 | |
| 140 | // Trusted certificates |
| 141 | UA_STACKARRAY(UA_ByteString, trustList, trustBuffers.size()); |
| 142 | for (size_t i = 0; i < trustBuffers.size(); i++) { |
| 143 | trustList[i] = UA_STRING_NULL; |
| 144 | trustList[i].length = trustBuffers[i].size(); |
| 145 | trustList[i].data = (UA_Byte*)UA_malloc(trustList[i].length * sizeof(UA_Byte)); |
| 146 | memcpy(trustList[i].data, trustBuffers[i].data(), trustList[i].length); |
| 147 | } |
| 148 | UA_StatusCode sc = UA_ClientConfig_setDefaultEncryption(cc, certByteString, keyByteString, |
| 149 | trustList, trustBuffers.size(), |
| 150 | nullptr, 0); |
| 151 | UA_ByteString_clear(&certByteString); |
| 152 | UA_ByteString_clear(&keyByteString); |
| 153 | for (size_t i = 0; i < trustBuffers.size(); i++) { |
| 154 | UA_ByteString_clear(&trustList[i]); |
| 155 | } |
| 156 | if (sc != UA_STATUSCODE_GOOD) { |
| 157 | logger->log_error("Configuring the client for encryption failed: %s", UA_StatusCode_name(sc)); |
| 158 | UA_Client_delete(client_); |
| 159 | throw OPCException(GENERAL_EXCEPTION, std::string("Failed to created client with the provided encryption settings: ") + UA_StatusCode_name(sc)); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | const UA_Logger MinifiUALogger = {logFunc, logger.get(), logClear}; |
| 164 | |
| 165 | UA_ClientConfig *configPtr = UA_Client_getConfig(client_); |
| 166 | configPtr->logger = MinifiUALogger; |
| 167 | |
| 168 | if(applicationURI.length() > 0) { |
| 169 | UA_String_clear(&configPtr->clientDescription.applicationUri); |
| 170 | configPtr->clientDescription.applicationUri = UA_STRING_ALLOC(applicationURI.c_str()); |
| 171 | } |
| 172 | |
| 173 | logger_ = logger; |
| 174 | } |