summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar comex2023-06-25 19:23:23 -0700
committerGravatar comex2023-06-25 19:24:49 -0700
commitd885dd5b642807d0587acad43668cfccfdf06d1e (patch)
treeb3f4ac530883702313901f9d48453577ce060175 /src
parentnetwork.cpp: include expected.h (diff)
downloadyuzu-d885dd5b642807d0587acad43668cfccfdf06d1e.tar.gz
yuzu-d885dd5b642807d0587acad43668cfccfdf06d1e.tar.xz
yuzu-d885dd5b642807d0587acad43668cfccfdf06d1e.zip
PR feedback + constification
Diffstat (limited to 'src')
-rw-r--r--src/core/hle/service/sockets/bsd.cpp8
-rw-r--r--src/core/hle/service/sockets/nsd.cpp6
-rw-r--r--src/core/hle/service/sockets/sfdnsres.cpp22
-rw-r--r--src/core/hle/service/ssl/ssl.cpp20
-rw-r--r--src/core/hle/service/ssl/ssl_backend.h4
-rw-r--r--src/core/hle/service/ssl/ssl_backend_none.cpp3
-rw-r--r--src/core/hle/service/ssl/ssl_backend_openssl.cpp14
-rw-r--r--src/core/hle/service/ssl/ssl_backend_schannel.cpp45
8 files changed, 62 insertions, 60 deletions
diff --git a/src/core/hle/service/sockets/bsd.cpp b/src/core/hle/service/sockets/bsd.cpp
index 6677689dc..6034cc0b5 100644
--- a/src/core/hle/service/sockets/bsd.cpp
+++ b/src/core/hle/service/sockets/bsd.cpp
@@ -270,10 +270,10 @@ void BSD::GetSockOpt(HLERequestContext& ctx) {
270 270
271 std::vector<u8> optval(ctx.GetWriteBufferSize()); 271 std::vector<u8> optval(ctx.GetWriteBufferSize());
272 272
273 LOG_WARNING(Service, "called. fd={} level={} optname=0x{:x} len=0x{:x}", fd, level, optname, 273 LOG_DEBUG(Service, "called. fd={} level={} optname=0x{:x} len=0x{:x}", fd, level, optname,
274 optval.size()); 274 optval.size());
275 275
276 Errno err = GetSockOptImpl(fd, level, optname, optval); 276 const Errno err = GetSockOptImpl(fd, level, optname, optval);
277 277
278 ctx.WriteBuffer(optval); 278 ctx.WriteBuffer(optval);
279 279
@@ -447,7 +447,7 @@ void BSD::DuplicateSocket(HLERequestContext& ctx) {
447 const s32 fd = rp.Pop<s32>(); 447 const s32 fd = rp.Pop<s32>();
448 [[maybe_unused]] const u64 unused = rp.Pop<u64>(); 448 [[maybe_unused]] const u64 unused = rp.Pop<u64>();
449 449
450 Common::Expected<s32, Errno> res = DuplicateSocketImpl(fd); 450 Expected<s32, Errno> res = DuplicateSocketImpl(fd);
451 IPC::ResponseBuilder rb{ctx, 4}; 451 IPC::ResponseBuilder rb{ctx, 4};
452 rb.Push(ResultSuccess); 452 rb.Push(ResultSuccess);
453 rb.Push(res.value_or(0)); // ret 453 rb.Push(res.value_or(0)); // ret
diff --git a/src/core/hle/service/sockets/nsd.cpp b/src/core/hle/service/sockets/nsd.cpp
index 22c3a31a0..0dfb0f166 100644
--- a/src/core/hle/service/sockets/nsd.cpp
+++ b/src/core/hle/service/sockets/nsd.cpp
@@ -49,7 +49,7 @@ static ResultVal<std::string> ResolveImpl(const std::string& fqdn_in) {
49 // The real implementation makes various substitutions. 49 // The real implementation makes various substitutions.
50 // For now we just return the string as-is, which is good enough when not 50 // For now we just return the string as-is, which is good enough when not
51 // connecting to real Nintendo servers. 51 // connecting to real Nintendo servers.
52 LOG_WARNING(Service, "(STUBBED) called({})", fqdn_in); 52 LOG_WARNING(Service, "(STUBBED) called, fqdn_in={}", fqdn_in);
53 return fqdn_in; 53 return fqdn_in;
54} 54}
55 55
@@ -69,7 +69,7 @@ void NSD::Resolve(HLERequestContext& ctx) {
69 const std::string fqdn_in = Common::StringFromBuffer(ctx.ReadBuffer(0)); 69 const std::string fqdn_in = Common::StringFromBuffer(ctx.ReadBuffer(0));
70 70
71 std::array<char, 0x100> fqdn_out{}; 71 std::array<char, 0x100> fqdn_out{};
72 Result res = ResolveCommon(fqdn_in, fqdn_out); 72 const Result res = ResolveCommon(fqdn_in, fqdn_out);
73 73
74 ctx.WriteBuffer(fqdn_out); 74 ctx.WriteBuffer(fqdn_out);
75 IPC::ResponseBuilder rb{ctx, 2}; 75 IPC::ResponseBuilder rb{ctx, 2};
@@ -80,7 +80,7 @@ void NSD::ResolveEx(HLERequestContext& ctx) {
80 const std::string fqdn_in = Common::StringFromBuffer(ctx.ReadBuffer(0)); 80 const std::string fqdn_in = Common::StringFromBuffer(ctx.ReadBuffer(0));
81 81
82 std::array<char, 0x100> fqdn_out; 82 std::array<char, 0x100> fqdn_out;
83 Result res = ResolveCommon(fqdn_in, fqdn_out); 83 const Result res = ResolveCommon(fqdn_in, fqdn_out);
84 84
85 if (res.IsError()) { 85 if (res.IsError()) {
86 IPC::ResponseBuilder rb{ctx, 2}; 86 IPC::ResponseBuilder rb{ctx, 2};
diff --git a/src/core/hle/service/sockets/sfdnsres.cpp b/src/core/hle/service/sockets/sfdnsres.cpp
index c5eaec920..45f0f526a 100644
--- a/src/core/hle/service/sockets/sfdnsres.cpp
+++ b/src/core/hle/service/sockets/sfdnsres.cpp
@@ -88,15 +88,15 @@ static Errno GetAddrInfoErrorToErrno(GetAddrInfoError result) {
88 88
89template <typename T> 89template <typename T>
90static void Append(std::vector<u8>& vec, T t) { 90static void Append(std::vector<u8>& vec, T t) {
91 size_t off = vec.size(); 91 const size_t offset = vec.size();
92 vec.resize(off + sizeof(T)); 92 vec.resize(offset + sizeof(T));
93 std::memcpy(vec.data() + off, &t, sizeof(T)); 93 std::memcpy(vec.data() + offset, &t, sizeof(T));
94} 94}
95 95
96static void AppendNulTerminated(std::vector<u8>& vec, std::string_view str) { 96static void AppendNulTerminated(std::vector<u8>& vec, std::string_view str) {
97 size_t off = vec.size(); 97 const size_t offset = vec.size();
98 vec.resize(off + str.size() + 1); 98 vec.resize(offset + str.size() + 1);
99 std::memmove(vec.data() + off, str.data(), str.size()); 99 std::memmove(vec.data() + offset, str.data(), str.size());
100} 100}
101 101
102// We implement gethostbyname using the host's getaddrinfo rather than the 102// We implement gethostbyname using the host's getaddrinfo rather than the
@@ -154,8 +154,8 @@ static std::pair<u32, GetAddrInfoError> GetHostByNameRequestImpl(HLERequestConte
154 return {0, Translate(res.error())}; 154 return {0, Translate(res.error())};
155 } 155 }
156 156
157 std::vector<u8> data = SerializeAddrInfoAsHostEnt(res.value(), host); 157 const std::vector<u8> data = SerializeAddrInfoAsHostEnt(res.value(), host);
158 u32 data_size = static_cast<u32>(data.size()); 158 const u32 data_size = static_cast<u32>(data.size());
159 ctx.WriteBuffer(data, 0); 159 ctx.WriteBuffer(data, 0);
160 160
161 return {data_size, GetAddrInfoError::SUCCESS}; 161 return {data_size, GetAddrInfoError::SUCCESS};
@@ -243,7 +243,7 @@ static std::pair<u32, GetAddrInfoError> GetAddrInfoRequestImpl(HLERequestContext
243 243
244 std::optional<std::string> service = std::nullopt; 244 std::optional<std::string> service = std::nullopt;
245 if (ctx.CanReadBuffer(1)) { 245 if (ctx.CanReadBuffer(1)) {
246 std::span<const u8> service_buffer = ctx.ReadBuffer(1); 246 const std::span<const u8> service_buffer = ctx.ReadBuffer(1);
247 service = Common::StringFromBuffer(service_buffer); 247 service = Common::StringFromBuffer(service_buffer);
248 } 248 }
249 249
@@ -254,8 +254,8 @@ static std::pair<u32, GetAddrInfoError> GetAddrInfoRequestImpl(HLERequestContext
254 return {0, Translate(res.error())}; 254 return {0, Translate(res.error())};
255 } 255 }
256 256
257 std::vector<u8> data = SerializeAddrInfo(res.value(), host); 257 const std::vector<u8> data = SerializeAddrInfo(res.value(), host);
258 u32 data_size = static_cast<u32>(data.size()); 258 const u32 data_size = static_cast<u32>(data.size());
259 ctx.WriteBuffer(data, 0); 259 ctx.WriteBuffer(data, 0);
260 260
261 return {data_size, GetAddrInfoError::SUCCESS}; 261 return {data_size, GetAddrInfoError::SUCCESS};
diff --git a/src/core/hle/service/ssl/ssl.cpp b/src/core/hle/service/ssl/ssl.cpp
index a3b54c7f0..5638dd693 100644
--- a/src/core/hle/service/ssl/ssl.cpp
+++ b/src/core/hle/service/ssl/ssl.cpp
@@ -114,7 +114,7 @@ public:
114 ~ISslConnection() { 114 ~ISslConnection() {
115 shared_data_->connection_count--; 115 shared_data_->connection_count--;
116 if (fd_to_close_.has_value()) { 116 if (fd_to_close_.has_value()) {
117 s32 fd = *fd_to_close_; 117 const s32 fd = *fd_to_close_;
118 if (!do_not_close_socket_) { 118 if (!do_not_close_socket_) {
119 LOG_ERROR(Service_SSL, 119 LOG_ERROR(Service_SSL,
120 "do_not_close_socket was changed after setting socket; is this right?"); 120 "do_not_close_socket was changed after setting socket; is this right?");
@@ -123,7 +123,7 @@ public:
123 if (bsd) { 123 if (bsd) {
124 auto err = bsd->CloseImpl(fd); 124 auto err = bsd->CloseImpl(fd);
125 if (err != Service::Sockets::Errno::SUCCESS) { 125 if (err != Service::Sockets::Errno::SUCCESS) {
126 LOG_ERROR(Service_SSL, "failed to close duplicated socket: {}", err); 126 LOG_ERROR(Service_SSL, "Failed to close duplicated socket: {}", err);
127 } 127 }
128 } 128 }
129 } 129 }
@@ -151,7 +151,7 @@ private:
151 if (do_not_close_socket_) { 151 if (do_not_close_socket_) {
152 auto res = bsd->DuplicateSocketImpl(fd); 152 auto res = bsd->DuplicateSocketImpl(fd);
153 if (!res.has_value()) { 153 if (!res.has_value()) {
154 LOG_ERROR(Service_SSL, "failed to duplicate socket"); 154 LOG_ERROR(Service_SSL, "Failed to duplicate socket with fd {}", fd);
155 return ResultInvalidSocket; 155 return ResultInvalidSocket;
156 } 156 }
157 fd = *res; 157 fd = *res;
@@ -171,7 +171,7 @@ private:
171 } 171 }
172 172
173 Result SetHostNameImpl(const std::string& hostname) { 173 Result SetHostNameImpl(const std::string& hostname) {
174 LOG_DEBUG(Service_SSL, "SetHostNameImpl({})", hostname); 174 LOG_DEBUG(Service_SSL, "called. hostname={}", hostname);
175 ASSERT(!did_handshake_); 175 ASSERT(!did_handshake_);
176 Result res = backend_->SetHostName(hostname); 176 Result res = backend_->SetHostName(hostname);
177 if (res == ResultSuccess) { 177 if (res == ResultSuccess) {
@@ -191,9 +191,9 @@ private:
191 ASSERT(mode == IoMode::Blocking || mode == IoMode::NonBlocking); 191 ASSERT(mode == IoMode::Blocking || mode == IoMode::NonBlocking);
192 ASSERT_OR_EXECUTE(socket_, { return ResultNoSocket; }); 192 ASSERT_OR_EXECUTE(socket_, { return ResultNoSocket; });
193 193
194 bool non_block = mode == IoMode::NonBlocking; 194 const bool non_block = mode == IoMode::NonBlocking;
195 Network::Errno e = socket_->SetNonBlock(non_block); 195 const Network::Errno error = socket_->SetNonBlock(non_block);
196 if (e != Network::Errno::SUCCESS) { 196 if (error != Network::Errno::SUCCESS) {
197 LOG_ERROR(Service_SSL, "Failed to set native socket non-block flag to {}", non_block); 197 LOG_ERROR(Service_SSL, "Failed to set native socket non-block flag to {}", non_block);
198 } 198 }
199 return ResultSuccess; 199 return ResultSuccess;
@@ -307,13 +307,13 @@ private:
307 } 307 }
308 308
309 void DoHandshakeGetServerCert(HLERequestContext& ctx) { 309 void DoHandshakeGetServerCert(HLERequestContext& ctx) {
310 Result res = DoHandshakeImpl(); 310 const Result res = DoHandshakeImpl();
311 u32 certs_count = 0; 311 u32 certs_count = 0;
312 u32 certs_size = 0; 312 u32 certs_size = 0;
313 if (res == ResultSuccess) { 313 if (res == ResultSuccess) {
314 auto certs = backend_->GetServerCerts(); 314 auto certs = backend_->GetServerCerts();
315 if (certs.Succeeded()) { 315 if (certs.Succeeded()) {
316 std::vector<u8> certs_buf = SerializeServerCerts(*certs); 316 const std::vector<u8> certs_buf = SerializeServerCerts(*certs);
317 ctx.WriteBuffer(certs_buf); 317 ctx.WriteBuffer(certs_buf);
318 certs_count = static_cast<u32>(certs->size()); 318 certs_count = static_cast<u32>(certs->size());
319 certs_size = static_cast<u32>(certs_buf.size()); 319 certs_size = static_cast<u32>(certs_buf.size());
@@ -377,7 +377,7 @@ private:
377 get_server_cert_chain_ = static_cast<bool>(parameters.value); 377 get_server_cert_chain_ = static_cast<bool>(parameters.value);
378 break; 378 break;
379 default: 379 default:
380 LOG_WARNING(Service_SSL, "unrecognized option={}, value={}", parameters.option, 380 LOG_WARNING(Service_SSL, "Unknown option={}, value={}", parameters.option,
381 parameters.value); 381 parameters.value);
382 } 382 }
383 383
diff --git a/src/core/hle/service/ssl/ssl_backend.h b/src/core/hle/service/ssl/ssl_backend.h
index 0dd8d9118..25c16bcc1 100644
--- a/src/core/hle/service/ssl/ssl_backend.h
+++ b/src/core/hle/service/ssl/ssl_backend.h
@@ -23,11 +23,11 @@ constexpr Result ResultInvalidSocket{ErrorModule::SSLSrv, 106};
23constexpr Result ResultTimeout{ErrorModule::SSLSrv, 205}; 23constexpr Result ResultTimeout{ErrorModule::SSLSrv, 205};
24constexpr Result ResultInternalError{ErrorModule::SSLSrv, 999}; // made up 24constexpr Result ResultInternalError{ErrorModule::SSLSrv, 999}; // made up
25 25
26constexpr Result ResultWouldBlock{ErrorModule::SSLSrv, 204}; 26// ResultWouldBlock is returned from Read and Write, and oddly, DoHandshake,
27// ^ ResultWouldBlock is returned from Read and Write, and oddly, DoHandshake,
28// with no way in the latter case to distinguish whether the client should poll 27// with no way in the latter case to distinguish whether the client should poll
29// for read or write. The one official client I've seen handles this by always 28// for read or write. The one official client I've seen handles this by always
30// polling for read (with a timeout). 29// polling for read (with a timeout).
30constexpr Result ResultWouldBlock{ErrorModule::SSLSrv, 204};
31 31
32class SSLConnectionBackend { 32class SSLConnectionBackend {
33public: 33public:
diff --git a/src/core/hle/service/ssl/ssl_backend_none.cpp b/src/core/hle/service/ssl/ssl_backend_none.cpp
index eb01561e2..f2f0ef706 100644
--- a/src/core/hle/service/ssl/ssl_backend_none.cpp
+++ b/src/core/hle/service/ssl/ssl_backend_none.cpp
@@ -8,7 +8,8 @@
8namespace Service::SSL { 8namespace Service::SSL {
9 9
10ResultVal<std::unique_ptr<SSLConnectionBackend>> CreateSSLConnectionBackend() { 10ResultVal<std::unique_ptr<SSLConnectionBackend>> CreateSSLConnectionBackend() {
11 LOG_ERROR(Service_SSL, "No SSL backend on this platform"); 11 LOG_ERROR(Service_SSL,
12 "Can't create SSL connection because no SSL backend is available on this platform");
12 return ResultInternalError; 13 return ResultInternalError;
13} 14}
14 15
diff --git a/src/core/hle/service/ssl/ssl_backend_openssl.cpp b/src/core/hle/service/ssl/ssl_backend_openssl.cpp
index bc797b76b..e7d5801fd 100644
--- a/src/core/hle/service/ssl/ssl_backend_openssl.cpp
+++ b/src/core/hle/service/ssl/ssl_backend_openssl.cpp
@@ -90,15 +90,15 @@ public:
90 90
91 Result DoHandshake() override { 91 Result DoHandshake() override {
92 SSL_set_verify_result(ssl_, X509_V_OK); 92 SSL_set_verify_result(ssl_, X509_V_OK);
93 int ret = SSL_do_handshake(ssl_); 93 const int ret = SSL_do_handshake(ssl_);
94 long verify_result = SSL_get_verify_result(ssl_); 94 const long verify_result = SSL_get_verify_result(ssl_);
95 if (verify_result != X509_V_OK) { 95 if (verify_result != X509_V_OK) {
96 LOG_ERROR(Service_SSL, "SSL cert verification failed because: {}", 96 LOG_ERROR(Service_SSL, "SSL cert verification failed because: {}",
97 X509_verify_cert_error_string(verify_result)); 97 X509_verify_cert_error_string(verify_result));
98 return CheckOpenSSLErrors(); 98 return CheckOpenSSLErrors();
99 } 99 }
100 if (ret <= 0) { 100 if (ret <= 0) {
101 int ssl_err = SSL_get_error(ssl_, ret); 101 const int ssl_err = SSL_get_error(ssl_, ret);
102 if (ssl_err == SSL_ERROR_ZERO_RETURN || 102 if (ssl_err == SSL_ERROR_ZERO_RETURN ||
103 (ssl_err == SSL_ERROR_SYSCALL && got_read_eof_)) { 103 (ssl_err == SSL_ERROR_SYSCALL && got_read_eof_)) {
104 LOG_ERROR(Service_SSL, "SSL handshake failed because server hung up"); 104 LOG_ERROR(Service_SSL, "SSL handshake failed because server hung up");
@@ -110,18 +110,18 @@ public:
110 110
111 ResultVal<size_t> Read(std::span<u8> data) override { 111 ResultVal<size_t> Read(std::span<u8> data) override {
112 size_t actual; 112 size_t actual;
113 int ret = SSL_read_ex(ssl_, data.data(), data.size(), &actual); 113 const int ret = SSL_read_ex(ssl_, data.data(), data.size(), &actual);
114 return HandleReturn("SSL_read_ex", actual, ret); 114 return HandleReturn("SSL_read_ex", actual, ret);
115 } 115 }
116 116
117 ResultVal<size_t> Write(std::span<const u8> data) override { 117 ResultVal<size_t> Write(std::span<const u8> data) override {
118 size_t actual; 118 size_t actual;
119 int ret = SSL_write_ex(ssl_, data.data(), data.size(), &actual); 119 const int ret = SSL_write_ex(ssl_, data.data(), data.size(), &actual);
120 return HandleReturn("SSL_write_ex", actual, ret); 120 return HandleReturn("SSL_write_ex", actual, ret);
121 } 121 }
122 122
123 ResultVal<size_t> HandleReturn(const char* what, size_t actual, int ret) { 123 ResultVal<size_t> HandleReturn(const char* what, size_t actual, int ret) {
124 int ssl_err = SSL_get_error(ssl_, ret); 124 const int ssl_err = SSL_get_error(ssl_, ret);
125 CheckOpenSSLErrors(); 125 CheckOpenSSLErrors();
126 switch (ssl_err) { 126 switch (ssl_err) {
127 case SSL_ERROR_NONE: 127 case SSL_ERROR_NONE:
@@ -255,7 +255,7 @@ public:
255 255
256ResultVal<std::unique_ptr<SSLConnectionBackend>> CreateSSLConnectionBackend() { 256ResultVal<std::unique_ptr<SSLConnectionBackend>> CreateSSLConnectionBackend() {
257 auto conn = std::make_unique<SSLConnectionBackendOpenSSL>(); 257 auto conn = std::make_unique<SSLConnectionBackendOpenSSL>();
258 Result res = conn->Init(); 258 const Result res = conn->Init();
259 if (res.IsFailure()) { 259 if (res.IsFailure()) {
260 return res; 260 return res;
261 } 261 }
diff --git a/src/core/hle/service/ssl/ssl_backend_schannel.cpp b/src/core/hle/service/ssl/ssl_backend_schannel.cpp
index d293adcf7..775d5cc07 100644
--- a/src/core/hle/service/ssl/ssl_backend_schannel.cpp
+++ b/src/core/hle/service/ssl/ssl_backend_schannel.cpp
@@ -38,7 +38,7 @@ static void OneTimeInit() {
38 // certificate, and presenting one to some arbitrary server 38 // certificate, and presenting one to some arbitrary server
39 // might be a privacy concern? Who knows, though. 39 // might be a privacy concern? Who knows, though.
40 40
41 SECURITY_STATUS ret = 41 const SECURITY_STATUS ret =
42 AcquireCredentialsHandle(nullptr, const_cast<LPTSTR>(UNISP_NAME), SECPKG_CRED_OUTBOUND, 42 AcquireCredentialsHandle(nullptr, const_cast<LPTSTR>(UNISP_NAME), SECPKG_CRED_OUTBOUND,
43 nullptr, &schannel_cred, nullptr, nullptr, &cred_handle, nullptr); 43 nullptr, &schannel_cred, nullptr, nullptr, &cred_handle, nullptr);
44 if (ret != SEC_E_OK) { 44 if (ret != SEC_E_OK) {
@@ -121,14 +121,14 @@ public:
121 } 121 }
122 122
123 Result FillCiphertextReadBuf() { 123 Result FillCiphertextReadBuf() {
124 size_t fill_size = read_buf_fill_size_ ? read_buf_fill_size_ : 4096; 124 const size_t fill_size = read_buf_fill_size_ ? read_buf_fill_size_ : 4096;
125 read_buf_fill_size_ = 0; 125 read_buf_fill_size_ = 0;
126 // This unnecessarily zeroes the buffer; oh well. 126 // This unnecessarily zeroes the buffer; oh well.
127 size_t offset = ciphertext_read_buf_.size(); 127 const size_t offset = ciphertext_read_buf_.size();
128 ASSERT_OR_EXECUTE(offset + fill_size >= offset, { return ResultInternalError; }); 128 ASSERT_OR_EXECUTE(offset + fill_size >= offset, { return ResultInternalError; });
129 ciphertext_read_buf_.resize(offset + fill_size, 0); 129 ciphertext_read_buf_.resize(offset + fill_size, 0);
130 auto read_span = std::span(ciphertext_read_buf_).subspan(offset, fill_size); 130 const auto read_span = std::span(ciphertext_read_buf_).subspan(offset, fill_size);
131 auto [actual, err] = socket_->Recv(0, read_span); 131 const auto [actual, err] = socket_->Recv(0, read_span);
132 switch (err) { 132 switch (err) {
133 case Network::Errno::SUCCESS: 133 case Network::Errno::SUCCESS:
134 ASSERT(static_cast<size_t>(actual) <= fill_size); 134 ASSERT(static_cast<size_t>(actual) <= fill_size);
@@ -147,7 +147,7 @@ public:
147 // Returns success if the write buffer has been completely emptied. 147 // Returns success if the write buffer has been completely emptied.
148 Result FlushCiphertextWriteBuf() { 148 Result FlushCiphertextWriteBuf() {
149 while (!ciphertext_write_buf_.empty()) { 149 while (!ciphertext_write_buf_.empty()) {
150 auto [actual, err] = socket_->Send(ciphertext_write_buf_, 0); 150 const auto [actual, err] = socket_->Send(ciphertext_write_buf_, 0);
151 switch (err) { 151 switch (err) {
152 case Network::Errno::SUCCESS: 152 case Network::Errno::SUCCESS:
153 ASSERT(static_cast<size_t>(actual) <= ciphertext_write_buf_.size()); 153 ASSERT(static_cast<size_t>(actual) <= ciphertext_write_buf_.size());
@@ -165,9 +165,10 @@ public:
165 } 165 }
166 166
167 Result CallInitializeSecurityContext() { 167 Result CallInitializeSecurityContext() {
168 unsigned long req = ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONFIDENTIALITY | ISC_REQ_INTEGRITY | 168 const unsigned long req = ISC_REQ_ALLOCATE_MEMORY | ISC_REQ_CONFIDENTIALITY |
169 ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT | ISC_REQ_STREAM | 169 ISC_REQ_INTEGRITY | ISC_REQ_REPLAY_DETECT |
170 ISC_REQ_USE_SUPPLIED_CREDS; 170 ISC_REQ_SEQUENCE_DETECT | ISC_REQ_STREAM |
171 ISC_REQ_USE_SUPPLIED_CREDS;
171 unsigned long attr; 172 unsigned long attr;
172 // https://learn.microsoft.com/en-us/windows/win32/secauthn/initializesecuritycontext--schannel 173 // https://learn.microsoft.com/en-us/windows/win32/secauthn/initializesecuritycontext--schannel
173 std::array<SecBuffer, 2> input_buffers{{ 174 std::array<SecBuffer, 2> input_buffers{{
@@ -219,7 +220,7 @@ public:
219 ciphertext_read_buf_.size()); 220 ciphertext_read_buf_.size());
220 } 221 }
221 222
222 SECURITY_STATUS ret = 223 const SECURITY_STATUS ret =
223 InitializeSecurityContextA(&cred_handle, initial_call_done ? &ctxt_ : nullptr, 224 InitializeSecurityContextA(&cred_handle, initial_call_done ? &ctxt_ : nullptr,
224 // Caller ensured we have set a hostname: 225 // Caller ensured we have set a hostname:
225 const_cast<char*>(hostname_.value().c_str()), req, 226 const_cast<char*>(hostname_.value().c_str()), req,
@@ -231,15 +232,15 @@ public:
231 nullptr); // ptsExpiry 232 nullptr); // ptsExpiry
232 233
233 if (output_buffers[0].pvBuffer) { 234 if (output_buffers[0].pvBuffer) {
234 std::span span(static_cast<u8*>(output_buffers[0].pvBuffer), 235 const std::span span(static_cast<u8*>(output_buffers[0].pvBuffer),
235 output_buffers[0].cbBuffer); 236 output_buffers[0].cbBuffer);
236 ciphertext_write_buf_.insert(ciphertext_write_buf_.end(), span.begin(), span.end()); 237 ciphertext_write_buf_.insert(ciphertext_write_buf_.end(), span.begin(), span.end());
237 FreeContextBuffer(output_buffers[0].pvBuffer); 238 FreeContextBuffer(output_buffers[0].pvBuffer);
238 } 239 }
239 240
240 if (output_buffers[1].pvBuffer) { 241 if (output_buffers[1].pvBuffer) {
241 std::span span(static_cast<u8*>(output_buffers[1].pvBuffer), 242 const std::span span(static_cast<u8*>(output_buffers[1].pvBuffer),
242 output_buffers[1].cbBuffer); 243 output_buffers[1].cbBuffer);
243 // The documentation doesn't explain what format this data is in. 244 // The documentation doesn't explain what format this data is in.
244 LOG_DEBUG(Service_SSL, "Got a {}-byte alert buffer: {}", span.size(), 245 LOG_DEBUG(Service_SSL, "Got a {}-byte alert buffer: {}", span.size(),
245 Common::HexToString(span)); 246 Common::HexToString(span));
@@ -280,7 +281,7 @@ public:
280 } 281 }
281 282
282 Result GrabStreamSizes() { 283 Result GrabStreamSizes() {
283 SECURITY_STATUS ret = 284 const SECURITY_STATUS ret =
284 QueryContextAttributes(&ctxt_, SECPKG_ATTR_STREAM_SIZES, &stream_sizes_); 285 QueryContextAttributes(&ctxt_, SECPKG_ATTR_STREAM_SIZES, &stream_sizes_);
285 if (ret != SEC_E_OK) { 286 if (ret != SEC_E_OK) {
286 LOG_ERROR(Service_SSL, "QueryContextAttributes(SECPKG_ATTR_STREAM_SIZES) failed: {}", 287 LOG_ERROR(Service_SSL, "QueryContextAttributes(SECPKG_ATTR_STREAM_SIZES) failed: {}",
@@ -301,7 +302,7 @@ public:
301 } 302 }
302 while (1) { 303 while (1) {
303 if (!cleartext_read_buf_.empty()) { 304 if (!cleartext_read_buf_.empty()) {
304 size_t read_size = std::min(cleartext_read_buf_.size(), data.size()); 305 const size_t read_size = std::min(cleartext_read_buf_.size(), data.size());
305 std::memcpy(data.data(), cleartext_read_buf_.data(), read_size); 306 std::memcpy(data.data(), cleartext_read_buf_.data(), read_size);
306 cleartext_read_buf_.erase(cleartext_read_buf_.begin(), 307 cleartext_read_buf_.erase(cleartext_read_buf_.begin(),
307 cleartext_read_buf_.begin() + read_size); 308 cleartext_read_buf_.begin() + read_size);
@@ -366,7 +367,7 @@ public:
366 return ResultInternalError; 367 return ResultInternalError;
367 } 368 }
368 } 369 }
369 Result r = FillCiphertextReadBuf(); 370 const Result r = FillCiphertextReadBuf();
370 if (r != ResultSuccess) { 371 if (r != ResultSuccess) {
371 return r; 372 return r;
372 } 373 }
@@ -430,7 +431,7 @@ public:
430 .pBuffers = buffers.data(), 431 .pBuffers = buffers.data(),
431 }; 432 };
432 433
433 SECURITY_STATUS ret = EncryptMessage(&ctxt_, /*fQOP*/ 0, &desc, /*MessageSeqNo*/ 0); 434 const SECURITY_STATUS ret = EncryptMessage(&ctxt_, /*fQOP*/ 0, &desc, /*MessageSeqNo*/ 0);
434 if (ret != SEC_E_OK) { 435 if (ret != SEC_E_OK) {
435 LOG_ERROR(Service_SSL, "EncryptMessage failed: {}", Common::NativeErrorToString(ret)); 436 LOG_ERROR(Service_SSL, "EncryptMessage failed: {}", Common::NativeErrorToString(ret));
436 return ResultInternalError; 437 return ResultInternalError;
@@ -445,19 +446,19 @@ public:
445 } 446 }
446 447
447 ResultVal<size_t> WriteAlreadyEncryptedData() { 448 ResultVal<size_t> WriteAlreadyEncryptedData() {
448 Result r = FlushCiphertextWriteBuf(); 449 const Result r = FlushCiphertextWriteBuf();
449 if (r != ResultSuccess) { 450 if (r != ResultSuccess) {
450 return r; 451 return r;
451 } 452 }
452 // write buf is empty 453 // write buf is empty
453 size_t cleartext_bytes_written = cleartext_write_buf_.size(); 454 const size_t cleartext_bytes_written = cleartext_write_buf_.size();
454 cleartext_write_buf_.clear(); 455 cleartext_write_buf_.clear();
455 return cleartext_bytes_written; 456 return cleartext_bytes_written;
456 } 457 }
457 458
458 ResultVal<std::vector<std::vector<u8>>> GetServerCerts() override { 459 ResultVal<std::vector<std::vector<u8>>> GetServerCerts() override {
459 PCCERT_CONTEXT returned_cert = nullptr; 460 PCCERT_CONTEXT returned_cert = nullptr;
460 SECURITY_STATUS ret = 461 const SECURITY_STATUS ret =
461 QueryContextAttributes(&ctxt_, SECPKG_ATTR_REMOTE_CERT_CONTEXT, &returned_cert); 462 QueryContextAttributes(&ctxt_, SECPKG_ATTR_REMOTE_CERT_CONTEXT, &returned_cert);
462 if (ret != SEC_E_OK) { 463 if (ret != SEC_E_OK) {
463 LOG_ERROR(Service_SSL, 464 LOG_ERROR(Service_SSL,
@@ -527,7 +528,7 @@ public:
527 528
528ResultVal<std::unique_ptr<SSLConnectionBackend>> CreateSSLConnectionBackend() { 529ResultVal<std::unique_ptr<SSLConnectionBackend>> CreateSSLConnectionBackend() {
529 auto conn = std::make_unique<SSLConnectionBackendSchannel>(); 530 auto conn = std::make_unique<SSLConnectionBackendSchannel>();
530 Result res = conn->Init(); 531 const Result res = conn->Init();
531 if (res.IsFailure()) { 532 if (res.IsFailure()) {
532 return res; 533 return res;
533 } 534 }