From 81f51b20958c94473149beda8ee7fbac3952b160 Mon Sep 17 00:00:00 2001 From: ZitongLi Date: Thu, 21 Aug 2025 16:28:35 +0800 Subject: [PATCH] Change type of wskey from string to char Change type of wskey from string to char Issue: https://gitee.com/openharmony/arkcompiler_toolchain/issues/ICU1K1 Signed-off-by: zitongli --- websocket/client/websocket_client.cpp | 5 ++++- websocket/client/websocket_client.h | 6 +++++- websocket/handshake_helper.cpp | 14 ++++++++------ websocket/handshake_helper.h | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/websocket/client/websocket_client.cpp b/websocket/client/websocket_client.cpp index c8a629a8..88d027d0 100644 --- a/websocket/client/websocket_client.cpp +++ b/websocket/client/websocket_client.cpp @@ -178,7 +178,10 @@ bool WebSocketClient::ClientSendWSUpgradeReq() return true; } - secWebSocketKey_ = WebSocketKeyEncoder::GenerateRandomSecWSKey(); + if (!WebSocketKeyEncoder::GenerateRandomSecWSKey(secWebSocketKey_)) { + LOGE("ClientSendWSUpgradeReq::client failed to generate Sec-WebSocket-Key"); + return false; + } std::string upgradeReq = std::string(CLIENT_WS_UPGRADE_REQ_BEFORE_KEY) + secWebSocketKey_ + std::string(CLIENT_WS_UPGRADE_REQ_AFTER_KEY); if (!Send(GetConnectionSocket(), upgradeReq.data(), upgradeReq.size(), 0)) { diff --git a/websocket/client/websocket_client.h b/websocket/client/websocket_client.h index 35302fdf..e1b57498 100644 --- a/websocket/client/websocket_client.h +++ b/websocket/client/websocket_client.h @@ -18,6 +18,7 @@ #include "websocket_base.h" #include "http.h" +#include "network.h" #include #include #include @@ -27,6 +28,9 @@ namespace OHOS::ArkCompiler::Toolchain { class WebSocketClient final : public WebSocketBase { public: + static constexpr size_t SEC_WEBSOCKET_KEY_BYTES_LEN = 16; + static constexpr size_t KEY_LENGTH = GetBase64EncodingLength(SEC_WEBSOCKET_KEY_BYTES_LEN); + ~WebSocketClient() noexcept override = default; void Close(); @@ -70,7 +74,7 @@ private: static constexpr int NET_SUCCESS = 1; static constexpr uint8_t MASK_KEY[] = {0xa, 0xb, 0xc, 0xd}; - std::string secWebSocketKey_; + char secWebSocketKey_[KEY_LENGTH + 1] = {}; }; } // namespace OHOS::ArkCompiler::Toolchain diff --git a/websocket/handshake_helper.cpp b/websocket/handshake_helper.cpp index b8c843a1..ceb77c6b 100644 --- a/websocket/handshake_helper.cpp +++ b/websocket/handshake_helper.cpp @@ -18,22 +18,24 @@ namespace OHOS::ArkCompiler::Toolchain { /* static */ -std::string WebSocketKeyEncoder::GenerateRandomSecWSKey() +bool WebSocketKeyEncoder::GenerateRandomSecWSKey(char (&destination)[KEY_LENGTH + 1]) { std::array bytes {}; if (RAND_bytes(bytes.data(), bytes.size()) != 1) { LOGE("RAND_bytes failed to generate secure random bytes"); - return ""; + return false; } - std::array encoded {}; // base64-encoding is done via EVP_EncodeBlock, which writes a null-terminated string. - int encodedBytes = EVP_EncodeBlock(encoded.data(), bytes.data(), SEC_WEBSOCKET_KEY_BYTES_LEN); + int encodedBytes = EVP_EncodeBlock(reinterpret_cast(destination), + bytes.data(), SEC_WEBSOCKET_KEY_BYTES_LEN); if (encodedBytes != KEY_LENGTH) { LOGE("EVP_EncodeBlock failed to encode Sec-WebSocket-Key bytes, encodedBytes = %{public}d", encodedBytes); - return ""; + destination[0] = '\0'; + return false; } - return std::string(reinterpret_cast(encoded.data()), KEY_LENGTH); + destination[KEY_LENGTH] = '\0'; + return true; } /* static */ diff --git a/websocket/handshake_helper.h b/websocket/handshake_helper.h index 4c56ac4d..3d0fe5cb 100644 --- a/websocket/handshake_helper.h +++ b/websocket/handshake_helper.h @@ -34,7 +34,7 @@ public: // SHA1 will write SHA_DIGEST_LENGTH == 20 bytes of output static constexpr size_t ENCODED_KEY_LEN = GetBase64EncodingLength(SHA_DIGEST_LENGTH); - static std::string GenerateRandomSecWSKey(); + static bool GenerateRandomSecWSKey(char (&destination)[KEY_LENGTH + 1]); static bool EncodeKey(std::string_view key, unsigned char (&destination)[ENCODED_KEY_LEN + 1]); static bool EncodeKey(const unsigned char(&key)[KEY_LENGTH + 1], unsigned char (&destination)[ENCODED_KEY_LEN + 1]); -- Gitee