blob: 425aa170867498fe3a1e97038f623b024da60f0c [file] [log] [blame]
Ben Schwartzded1b702017-10-25 14:41:02 -04001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "DnsTlsSocket"
Ben Schwartz33860762017-10-25 14:41:02 -040018//#define LOG_NDEBUG 0
Ben Schwartzded1b702017-10-25 14:41:02 -040019
Mike Yu5ae61542018-10-19 22:11:43 +080020#include "netd_resolv/DnsTlsSocket.h"
Ben Schwartzded1b702017-10-25 14:41:02 -040021
22#include <algorithm>
23#include <arpa/inet.h>
24#include <arpa/nameser.h>
25#include <errno.h>
Erik Klined1503072018-02-22 23:55:40 -080026#include <linux/tcp.h>
Ben Schwartzded1b702017-10-25 14:41:02 -040027#include <openssl/err.h>
Mike Yu5ae61542018-10-19 22:11:43 +080028#include <openssl/sha.h>
Bernie Innocentif944a9c2018-05-18 20:50:25 +090029#include <sys/poll.h>
Ben Schwartzded1b702017-10-25 14:41:02 -040030
Mike Yu5ae61542018-10-19 22:11:43 +080031#include "netd_resolv/DnsTlsSessionCache.h"
32#include "netd_resolv/IDnsTlsSocketObserver.h"
Ben Schwartzded1b702017-10-25 14:41:02 -040033
34#include "log/log.h"
Erik Klined1503072018-02-22 23:55:40 -080035#include "netdutils/SocketOption.h"
Ben Schwartzded1b702017-10-25 14:41:02 -040036#include "Permission.h"
37
Ben Schwartzded1b702017-10-25 14:41:02 -040038namespace android {
Ben Schwartzded1b702017-10-25 14:41:02 -040039
Erik Klined1503072018-02-22 23:55:40 -080040using netdutils::enableSockopt;
41using netdutils::enableTcpKeepAlives;
42using netdutils::isOk;
Ben Schwartzded1b702017-10-25 14:41:02 -040043using netdutils::Status;
44
Erik Klined1503072018-02-22 23:55:40 -080045namespace net {
Ben Schwartzded1b702017-10-25 14:41:02 -040046namespace {
47
48constexpr const char kCaCertDir[] = "/system/etc/security/cacerts";
Mike Yu5ae61542018-10-19 22:11:43 +080049constexpr size_t SHA256_SIZE = SHA256_DIGEST_LENGTH;
Ben Schwartzded1b702017-10-25 14:41:02 -040050
51int waitForReading(int fd) {
Bernie Innocentif944a9c2018-05-18 20:50:25 +090052 struct pollfd fds = { .fd = fd, .events = POLLIN };
53 const int ret = TEMP_FAILURE_RETRY(poll(&fds, 1, -1));
Ben Schwartzded1b702017-10-25 14:41:02 -040054 return ret;
55}
56
57int waitForWriting(int fd) {
Bernie Innocentif944a9c2018-05-18 20:50:25 +090058 struct pollfd fds = { .fd = fd, .events = POLLOUT };
59 const int ret = TEMP_FAILURE_RETRY(poll(&fds, 1, -1));
Ben Schwartzded1b702017-10-25 14:41:02 -040060 return ret;
61}
62
63} // namespace
64
65Status DnsTlsSocket::tcpConnect() {
66 ALOGV("%u connecting TCP socket", mMark);
67 int type = SOCK_NONBLOCK | SOCK_CLOEXEC;
68 switch (mServer.protocol) {
69 case IPPROTO_TCP:
70 type |= SOCK_STREAM;
71 break;
72 default:
73 return Status(EPROTONOSUPPORT);
74 }
75
76 mSslFd.reset(socket(mServer.ss.ss_family, type, mServer.protocol));
77 if (mSslFd.get() == -1) {
78 ALOGE("Failed to create socket");
79 return Status(errno);
80 }
81
82 const socklen_t len = sizeof(mMark);
83 if (setsockopt(mSslFd.get(), SOL_SOCKET, SO_MARK, &mMark, len) == -1) {
84 ALOGE("Failed to set socket mark");
85 mSslFd.reset();
86 return Status(errno);
87 }
Erik Klined1503072018-02-22 23:55:40 -080088
89 const Status tfo = enableSockopt(mSslFd.get(), SOL_TCP, TCP_FASTOPEN_CONNECT);
90 if (!isOk(tfo) && tfo.code() != ENOPROTOOPT) {
91 ALOGI("Failed to enable TFO: %s", tfo.msg().c_str());
92 }
93
94 // Send 5 keepalives, 3 seconds apart, after 15 seconds of inactivity.
Bernie Innocenti6f9fd902018-10-11 20:50:23 +090095 enableTcpKeepAlives(mSslFd.get(), 15U, 5U, 3U).ignoreError();
Erik Klined1503072018-02-22 23:55:40 -080096
Ben Schwartzded1b702017-10-25 14:41:02 -040097 if (connect(mSslFd.get(), reinterpret_cast<const struct sockaddr *>(&mServer.ss),
98 sizeof(mServer.ss)) != 0 &&
99 errno != EINPROGRESS) {
100 ALOGV("Socket failed to connect");
101 mSslFd.reset();
102 return Status(errno);
103 }
104
105 return netdutils::status::ok;
106}
107
108bool getSPKIDigest(const X509* cert, std::vector<uint8_t>* out) {
Yi Kongbdfd57e2018-07-25 13:26:10 -0700109 int spki_len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), nullptr);
Ben Schwartzded1b702017-10-25 14:41:02 -0400110 unsigned char spki[spki_len];
111 unsigned char* temp = spki;
112 if (spki_len != i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &temp)) {
113 ALOGW("SPKI length mismatch");
114 return false;
115 }
116 out->resize(SHA256_SIZE);
117 unsigned int digest_len = 0;
Yi Kongbdfd57e2018-07-25 13:26:10 -0700118 int ret = EVP_Digest(spki, spki_len, out->data(), &digest_len, EVP_sha256(), nullptr);
Ben Schwartzded1b702017-10-25 14:41:02 -0400119 if (ret != 1) {
120 ALOGW("Server cert digest extraction failed");
121 return false;
122 }
123 if (digest_len != out->size()) {
124 ALOGW("Wrong digest length: %d", digest_len);
125 return false;
126 }
127 return true;
128}
129
130bool DnsTlsSocket::initialize() {
131 // This method should only be called once, at the beginning, so locking should be
132 // unnecessary. This lock only serves to help catch bugs in code that calls this method.
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900133 std::lock_guard guard(mLock);
Ben Schwartzded1b702017-10-25 14:41:02 -0400134 if (mSslCtx) {
135 // This is a bug in the caller.
136 return false;
137 }
138 mSslCtx.reset(SSL_CTX_new(TLS_method()));
139 if (!mSslCtx) {
140 return false;
141 }
142
143 // Load system CA certs for hostname verification.
144 //
145 // For discussion of alternative, sustainable approaches see b/71909242.
146 if (SSL_CTX_load_verify_locations(mSslCtx.get(), nullptr, kCaCertDir) != 1) {
147 ALOGE("Failed to load CA cert dir: %s", kCaCertDir);
148 return false;
149 }
150
151 // Enable TLS false start
152 SSL_CTX_set_false_start_allowed_without_alpn(mSslCtx.get(), 1);
153 SSL_CTX_set_mode(mSslCtx.get(), SSL_MODE_ENABLE_FALSE_START);
154
155 // Enable session cache
156 mCache->prepareSslContext(mSslCtx.get());
157
158 // Connect
159 Status status = tcpConnect();
160 if (!status.ok()) {
161 return false;
162 }
163 mSsl = sslConnect(mSslFd.get());
164 if (!mSsl) {
165 return false;
166 }
Ben Schwartz33860762017-10-25 14:41:02 -0400167 int sv[2];
168 if (socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, sv)) {
169 return false;
170 }
171 // The two sockets are perfectly symmetrical, so the choice of which one is
172 // "in" and which one is "out" is arbitrary.
173 mIpcInFd.reset(sv[0]);
174 mIpcOutFd.reset(sv[1]);
175
176 // Start the I/O loop.
177 mLoopThread.reset(new std::thread(&DnsTlsSocket::loop, this));
Ben Schwartzded1b702017-10-25 14:41:02 -0400178
179 return true;
180}
181
182bssl::UniquePtr<SSL> DnsTlsSocket::sslConnect(int fd) {
183 if (!mSslCtx) {
184 ALOGE("Internal error: context is null in sslConnect");
185 return nullptr;
186 }
187 if (!SSL_CTX_set_min_proto_version(mSslCtx.get(), TLS1_2_VERSION)) {
188 ALOGE("Failed to set minimum TLS version");
189 return nullptr;
190 }
191
192 bssl::UniquePtr<SSL> ssl(SSL_new(mSslCtx.get()));
193 // This file descriptor is owned by mSslFd, so don't let libssl close it.
194 bssl::UniquePtr<BIO> bio(BIO_new_socket(fd, BIO_NOCLOSE));
195 SSL_set_bio(ssl.get(), bio.get(), bio.get());
196 bio.release();
197
198 if (!mCache->prepareSsl(ssl.get())) {
199 return nullptr;
200 }
201
202 if (!mServer.name.empty()) {
203 if (SSL_set_tlsext_host_name(ssl.get(), mServer.name.c_str()) != 1) {
204 ALOGE("Failed to set SNI to %s", mServer.name.c_str());
205 return nullptr;
206 }
207 X509_VERIFY_PARAM* param = SSL_get0_param(ssl.get());
Erik Klineefc13632018-03-22 11:19:02 -0700208 if (X509_VERIFY_PARAM_set1_host(param, mServer.name.data(), mServer.name.size()) != 1) {
209 ALOGE("Failed to set verify host param to %s", mServer.name.c_str());
210 return nullptr;
211 }
Ben Schwartzded1b702017-10-25 14:41:02 -0400212 // This will cause the handshake to fail if certificate verification fails.
213 SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, nullptr);
214 }
215
216 bssl::UniquePtr<SSL_SESSION> session = mCache->getSession();
217 if (session) {
218 ALOGV("Setting session");
219 SSL_set_session(ssl.get(), session.get());
220 } else {
221 ALOGV("No session available");
222 }
223
224 for (;;) {
225 ALOGV("%u Calling SSL_connect", mMark);
226 int ret = SSL_connect(ssl.get());
227 ALOGV("%u SSL_connect returned %d", mMark, ret);
228 if (ret == 1) break; // SSL handshake complete;
229
230 const int ssl_err = SSL_get_error(ssl.get(), ret);
231 switch (ssl_err) {
232 case SSL_ERROR_WANT_READ:
233 if (waitForReading(fd) != 1) {
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900234 ALOGW("SSL_connect read error: %d", errno);
Ben Schwartzded1b702017-10-25 14:41:02 -0400235 return nullptr;
236 }
237 break;
238 case SSL_ERROR_WANT_WRITE:
239 if (waitForWriting(fd) != 1) {
240 ALOGW("SSL_connect write error");
241 return nullptr;
242 }
243 break;
244 default:
245 ALOGW("SSL_connect error %d, errno=%d", ssl_err, errno);
246 return nullptr;
247 }
248 }
249
250 // TODO: Call SSL_shutdown before discarding the session if validation fails.
251 if (!mServer.fingerprints.empty()) {
252 ALOGV("Checking DNS over TLS fingerprint");
253
254 // We only care that the chain is internally self-consistent, not that
255 // it chains to a trusted root, so we can ignore some kinds of errors.
256 // TODO: Add a CA root verification mode that respects these errors.
257 int verify_result = SSL_get_verify_result(ssl.get());
258 switch (verify_result) {
259 case X509_V_OK:
260 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
261 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
262 case X509_V_ERR_CERT_UNTRUSTED:
263 break;
264 default:
265 ALOGW("Invalid certificate chain, error %d", verify_result);
266 return nullptr;
267 }
268
269 STACK_OF(X509) *chain = SSL_get_peer_cert_chain(ssl.get());
270 if (!chain) {
271 ALOGW("Server has null certificate");
272 return nullptr;
273 }
274 // Chain and its contents are owned by ssl, so we don't need to free explicitly.
275 bool matched = false;
276 for (size_t i = 0; i < sk_X509_num(chain); ++i) {
277 // This appears to be O(N^2), but there doesn't seem to be a straightforward
278 // way to walk a STACK_OF nondestructively in linear time.
279 X509* cert = sk_X509_value(chain, i);
280 std::vector<uint8_t> digest;
281 if (!getSPKIDigest(cert, &digest)) {
282 ALOGE("Digest computation failed");
283 return nullptr;
284 }
285
286 if (mServer.fingerprints.count(digest) > 0) {
287 matched = true;
288 break;
289 }
290 }
291
292 if (!matched) {
293 ALOGW("No matching fingerprint");
294 return nullptr;
295 }
296
297 ALOGV("DNS over TLS fingerprint is correct");
298 }
299
300 ALOGV("%u handshake complete", mMark);
301
302 return ssl;
303}
304
305void DnsTlsSocket::sslDisconnect() {
306 if (mSsl) {
307 SSL_shutdown(mSsl.get());
308 mSsl.reset();
309 }
310 mSslFd.reset();
311}
312
313bool DnsTlsSocket::sslWrite(const Slice buffer) {
314 ALOGV("%u Writing %zu bytes", mMark, buffer.size());
315 for (;;) {
316 int ret = SSL_write(mSsl.get(), buffer.base(), buffer.size());
317 if (ret == int(buffer.size())) break; // SSL write complete;
318
319 if (ret < 1) {
320 const int ssl_err = SSL_get_error(mSsl.get(), ret);
321 switch (ssl_err) {
322 case SSL_ERROR_WANT_WRITE:
323 if (waitForWriting(mSslFd.get()) != 1) {
324 ALOGV("SSL_write error");
325 return false;
326 }
327 continue;
328 case 0:
329 break; // SSL write complete;
330 default:
331 ALOGV("SSL_write error %d", ssl_err);
332 return false;
333 }
334 }
335 }
336 ALOGV("%u Wrote %zu bytes", mMark, buffer.size());
337 return true;
338}
339
Ben Schwartz33860762017-10-25 14:41:02 -0400340void DnsTlsSocket::loop() {
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900341 std::lock_guard guard(mLock);
Ben Schwartz33860762017-10-25 14:41:02 -0400342 // Buffer at most one query.
343 Query q;
344
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900345 const int timeout_msecs = DnsTlsSocket::kIdleTimeout.count() * 1000;
Ben Schwartz33860762017-10-25 14:41:02 -0400346 while (true) {
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900347 // poll() ignores negative fds
348 struct pollfd fds[2] = { { .fd = -1 }, { .fd = -1 } };
349 enum { SSLFD = 0, IPCFD = 1 };
350
Ben Schwartz33860762017-10-25 14:41:02 -0400351 // Always listen for a response from server.
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900352 fds[SSLFD].fd = mSslFd.get();
353 fds[SSLFD].events = POLLIN;
354
Ben Schwartz33860762017-10-25 14:41:02 -0400355 // If we have a pending query, also wait for space
356 // to write it, otherwise listen for a new query.
357 if (!q.query.empty()) {
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900358 fds[SSLFD].events |= POLLOUT;
Ben Schwartz33860762017-10-25 14:41:02 -0400359 } else {
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900360 fds[IPCFD].fd = mIpcOutFd.get();
361 fds[IPCFD].events = POLLIN;
Ben Schwartz33860762017-10-25 14:41:02 -0400362 }
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900363
Mike Yu5ae61542018-10-19 22:11:43 +0800364 const int s = TEMP_FAILURE_RETRY(poll(fds, std::size(fds), timeout_msecs));
Ben Schwartz33860762017-10-25 14:41:02 -0400365 if (s == 0) {
366 ALOGV("Idle timeout");
367 break;
368 }
369 if (s < 0) {
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900370 ALOGV("Poll failed: %d", errno);
Ben Schwartz33860762017-10-25 14:41:02 -0400371 break;
372 }
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900373 if (fds[SSLFD].revents & (POLLIN | POLLERR)) {
Ben Schwartz33860762017-10-25 14:41:02 -0400374 if (!readResponse()) {
375 ALOGV("SSL remote close or read error.");
376 break;
377 }
378 }
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900379 if (fds[IPCFD].revents & (POLLIN | POLLERR)) {
Ben Schwartz33860762017-10-25 14:41:02 -0400380 int res = read(mIpcOutFd.get(), &q, sizeof(q));
381 if (res < 0) {
382 ALOGW("Error during IPC read");
383 break;
384 } else if (res == 0) {
385 ALOGV("IPC channel closed; disconnecting");
386 break;
387 } else if (res != sizeof(q)) {
388 ALOGE("Struct size mismatch: %d != %zu", res, sizeof(q));
389 break;
390 }
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900391 } else if (fds[SSLFD].revents & POLLOUT) {
Ben Schwartz33860762017-10-25 14:41:02 -0400392 // query cannot be null here.
393 if (!sendQuery(q)) {
394 break;
395 }
396 q = Query(); // Reset q to empty
397 }
Ben Schwartzded1b702017-10-25 14:41:02 -0400398 }
Ben Schwartz33860762017-10-25 14:41:02 -0400399 ALOGV("Closing IPC read FD");
400 mIpcOutFd.reset();
401 ALOGV("Disconnecting");
402 sslDisconnect();
403 ALOGV("Calling onClosed");
404 mObserver->onClosed();
405 ALOGV("Ending loop");
Ben Schwartzded1b702017-10-25 14:41:02 -0400406}
407
Ben Schwartz33860762017-10-25 14:41:02 -0400408DnsTlsSocket::~DnsTlsSocket() {
409 ALOGV("Destructor");
410 // This will trigger an orderly shutdown in loop().
411 mIpcInFd.reset();
412 {
413 // Wait for the orderly shutdown to complete.
Bernie Innocentiabf8a342018-08-10 15:17:16 +0900414 std::lock_guard guard(mLock);
Ben Schwartz33860762017-10-25 14:41:02 -0400415 if (mLoopThread && std::this_thread::get_id() == mLoopThread->get_id()) {
416 ALOGE("Violation of re-entrance precondition");
417 return;
418 }
419 }
420 if (mLoopThread) {
421 ALOGV("Waiting for loop thread to terminate");
422 mLoopThread->join();
423 mLoopThread.reset();
424 }
425 ALOGV("Destructor completed");
426}
427
428bool DnsTlsSocket::query(uint16_t id, const Slice query) {
429 const Query q = { .id = id, .query = query };
430 if (!mIpcInFd) {
431 return false;
432 }
433 int written = write(mIpcInFd.get(), &q, sizeof(q));
434 return written == sizeof(q);
435}
436
437// Read exactly len bytes into buffer or fail with an SSL error code
438int DnsTlsSocket::sslRead(const Slice buffer, bool wait) {
Ben Schwartzded1b702017-10-25 14:41:02 -0400439 size_t remaining = buffer.size();
440 while (remaining > 0) {
441 int ret = SSL_read(mSsl.get(), buffer.limit() - remaining, remaining);
442 if (ret == 0) {
443 ALOGW_IF(remaining < buffer.size(), "SSL closed with %zu of %zu bytes remaining",
444 remaining, buffer.size());
Ben Schwartz33860762017-10-25 14:41:02 -0400445 return SSL_ERROR_ZERO_RETURN;
Ben Schwartzded1b702017-10-25 14:41:02 -0400446 }
447
448 if (ret < 0) {
449 const int ssl_err = SSL_get_error(mSsl.get(), ret);
Ben Schwartz33860762017-10-25 14:41:02 -0400450 if (wait && ssl_err == SSL_ERROR_WANT_READ) {
Ben Schwartzded1b702017-10-25 14:41:02 -0400451 if (waitForReading(mSslFd.get()) != 1) {
Bernie Innocentif944a9c2018-05-18 20:50:25 +0900452 ALOGV("Poll failed in sslRead: %d", errno);
Ben Schwartz33860762017-10-25 14:41:02 -0400453 return SSL_ERROR_SYSCALL;
Ben Schwartzded1b702017-10-25 14:41:02 -0400454 }
455 continue;
456 } else {
457 ALOGV("SSL_read error %d", ssl_err);
Ben Schwartz33860762017-10-25 14:41:02 -0400458 return ssl_err;
Ben Schwartzded1b702017-10-25 14:41:02 -0400459 }
460 }
461
462 remaining -= ret;
Ben Schwartz33860762017-10-25 14:41:02 -0400463 wait = true; // Once a read is started, try to finish.
Ben Schwartzded1b702017-10-25 14:41:02 -0400464 }
Ben Schwartz33860762017-10-25 14:41:02 -0400465 return SSL_ERROR_NONE;
Ben Schwartzded1b702017-10-25 14:41:02 -0400466}
467
468bool DnsTlsSocket::sendQuery(const Query& q) {
469 ALOGV("sending query");
470 // Compose the entire message in a single buffer, so that it can be
471 // sent as a single TLS record.
472 std::vector<uint8_t> buf(q.query.size() + 4);
473 // Write 2-byte length
474 uint16_t len = q.query.size() + 2; // + 2 for the ID.
475 buf[0] = len >> 8;
476 buf[1] = len;
477 // Write 2-byte ID
478 buf[2] = q.id >> 8;
479 buf[3] = q.id;
480 // Copy body
481 std::memcpy(buf.data() + 4, q.query.base(), q.query.size());
482 if (!sslWrite(netdutils::makeSlice(buf))) {
483 return false;
484 }
485 ALOGV("%u SSL_write complete", mMark);
486 return true;
487}
488
Ben Schwartz33860762017-10-25 14:41:02 -0400489bool DnsTlsSocket::readResponse() {
Ben Schwartzded1b702017-10-25 14:41:02 -0400490 ALOGV("reading response");
491 uint8_t responseHeader[2];
Ben Schwartz33860762017-10-25 14:41:02 -0400492 int err = sslRead(Slice(responseHeader, 2), false);
493 if (err == SSL_ERROR_WANT_READ) {
494 ALOGV("Ignoring spurious wakeup from server");
495 return true;
496 }
497 if (err != SSL_ERROR_NONE) {
498 return false;
Ben Schwartzded1b702017-10-25 14:41:02 -0400499 }
500 // Truncate responses larger than MAX_SIZE. This is safe because a DNS packet is
501 // always invalid when truncated, so the response will be treated as an error.
502 constexpr uint16_t MAX_SIZE = 8192;
503 const uint16_t responseSize = (responseHeader[0] << 8) | responseHeader[1];
504 ALOGV("%u Expecting response of size %i", mMark, responseSize);
505 std::vector<uint8_t> response(std::min(responseSize, MAX_SIZE));
Ben Schwartz33860762017-10-25 14:41:02 -0400506 if (sslRead(netdutils::makeSlice(response), true) != SSL_ERROR_NONE) {
Ben Schwartzded1b702017-10-25 14:41:02 -0400507 ALOGV("%u Failed to read %zu bytes", mMark, response.size());
Ben Schwartz33860762017-10-25 14:41:02 -0400508 return false;
Ben Schwartzded1b702017-10-25 14:41:02 -0400509 }
510 uint16_t remainingBytes = responseSize - response.size();
511 while (remainingBytes > 0) {
512 constexpr uint16_t CHUNK_SIZE = 2048;
513 std::vector<uint8_t> discard(std::min(remainingBytes, CHUNK_SIZE));
Ben Schwartz33860762017-10-25 14:41:02 -0400514 if (sslRead(netdutils::makeSlice(discard), true) != SSL_ERROR_NONE) {
Ben Schwartzded1b702017-10-25 14:41:02 -0400515 ALOGV("%u Failed to discard %zu bytes", mMark, discard.size());
Ben Schwartz33860762017-10-25 14:41:02 -0400516 return false;
Ben Schwartzded1b702017-10-25 14:41:02 -0400517 }
518 remainingBytes -= discard.size();
519 }
520 ALOGV("%u SSL_read complete", mMark);
521
Ben Schwartz33860762017-10-25 14:41:02 -0400522 mObserver->onResponse(std::move(response));
523 return true;
Ben Schwartzded1b702017-10-25 14:41:02 -0400524}
525
526} // end of namespace net
527} // end of namespace android