Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | #include "dns/DnsTlsTransport.h" |
| 18 | |
| 19 | #include <arpa/inet.h> |
| 20 | #include <arpa/nameser.h> |
| 21 | #include <errno.h> |
| 22 | #include <openssl/err.h> |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 23 | |
| 24 | #define LOG_TAG "DnsTlsTransport" |
| 25 | #define DBG 0 |
| 26 | |
| 27 | #include "log/log.h" |
| 28 | #include "Fwmark.h" |
| 29 | #undef ADD // already defined in nameser.h |
| 30 | #include "NetdConstants.h" |
| 31 | #include "Permission.h" |
| 32 | |
| 33 | |
| 34 | namespace android { |
| 35 | namespace net { |
| 36 | |
| 37 | namespace { |
| 38 | |
| 39 | bool setNonBlocking(int fd, bool enabled) { |
| 40 | int flags = fcntl(fd, F_GETFL); |
| 41 | if (flags < 0) return false; |
| 42 | |
| 43 | if (enabled) { |
| 44 | flags |= O_NONBLOCK; |
| 45 | } else { |
| 46 | flags &= ~O_NONBLOCK; |
| 47 | } |
| 48 | return (fcntl(fd, F_SETFL, flags) == 0); |
| 49 | } |
| 50 | |
| 51 | int waitForReading(int fd) { |
| 52 | fd_set fds; |
| 53 | FD_ZERO(&fds); |
| 54 | FD_SET(fd, &fds); |
| 55 | const int ret = TEMP_FAILURE_RETRY(select(fd + 1, &fds, nullptr, nullptr, nullptr)); |
| 56 | if (DBG && ret <= 0) { |
| 57 | ALOGD("select"); |
| 58 | } |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | int waitForWriting(int fd) { |
| 63 | fd_set fds; |
| 64 | FD_ZERO(&fds); |
| 65 | FD_SET(fd, &fds); |
| 66 | const int ret = TEMP_FAILURE_RETRY(select(fd + 1, nullptr, &fds, nullptr, nullptr)); |
| 67 | if (DBG && ret <= 0) { |
| 68 | ALOGD("select"); |
| 69 | } |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | } // namespace |
| 74 | |
| 75 | android::base::unique_fd DnsTlsTransport::makeConnectedSocket() const { |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 76 | if (DBG) { |
| 77 | ALOGD("%u connecting TCP socket", mMark); |
| 78 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 79 | android::base::unique_fd fd; |
| 80 | int type = SOCK_NONBLOCK | SOCK_CLOEXEC; |
Ben Schwartz | 5250462 | 2017-07-11 12:21:13 -0400 | [diff] [blame] | 81 | switch (mServer.protocol) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 82 | case IPPROTO_TCP: |
| 83 | type |= SOCK_STREAM; |
| 84 | break; |
| 85 | default: |
| 86 | errno = EPROTONOSUPPORT; |
| 87 | return fd; |
| 88 | } |
| 89 | |
Ben Schwartz | 5250462 | 2017-07-11 12:21:13 -0400 | [diff] [blame] | 90 | fd.reset(socket(mServer.ss.ss_family, type, mServer.protocol)); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 91 | if (fd.get() == -1) { |
| 92 | return fd; |
| 93 | } |
| 94 | |
| 95 | const socklen_t len = sizeof(mMark); |
| 96 | if (setsockopt(fd.get(), SOL_SOCKET, SO_MARK, &mMark, len) == -1) { |
| 97 | fd.reset(); |
| 98 | } else if (connect(fd.get(), |
Ben Schwartz | 5250462 | 2017-07-11 12:21:13 -0400 | [diff] [blame] | 99 | reinterpret_cast<const struct sockaddr *>(&mServer.ss), sizeof(mServer.ss)) != 0 |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 100 | && errno != EINPROGRESS) { |
| 101 | fd.reset(); |
| 102 | } |
| 103 | |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 104 | if (!setNonBlocking(fd, false)) { |
| 105 | ALOGE("Failed to disable nonblocking status on DNS-over-TLS fd"); |
| 106 | fd.reset(); |
| 107 | } |
| 108 | |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 109 | return fd; |
| 110 | } |
| 111 | |
| 112 | bool getSPKIDigest(const X509* cert, std::vector<uint8_t>* out) { |
| 113 | int spki_len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), NULL); |
| 114 | unsigned char spki[spki_len]; |
| 115 | unsigned char* temp = spki; |
| 116 | if (spki_len != i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &temp)) { |
| 117 | ALOGW("SPKI length mismatch"); |
| 118 | return false; |
| 119 | } |
| 120 | out->resize(SHA256_SIZE); |
| 121 | unsigned int digest_len = 0; |
| 122 | int ret = EVP_Digest(spki, spki_len, out->data(), &digest_len, EVP_sha256(), NULL); |
| 123 | if (ret != 1) { |
| 124 | ALOGW("Server cert digest extraction failed"); |
| 125 | return false; |
| 126 | } |
| 127 | if (digest_len != out->size()) { |
| 128 | ALOGW("Wrong digest length: %d", digest_len); |
| 129 | return false; |
| 130 | } |
| 131 | return true; |
| 132 | } |
| 133 | |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 134 | bool DnsTlsTransport::initialize() { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 135 | // This method should only be called once, at the beginning, so locking should be |
| 136 | // unnecessary. This lock only serves to help catch bugs in code that calls this method. |
| 137 | std::lock_guard<std::mutex> guard(mLock); |
| 138 | if (mSslCtx) { |
| 139 | // This is a bug in the caller. |
| 140 | return false; |
| 141 | } |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 142 | mSslCtx.reset(SSL_CTX_new(TLS_method())); |
| 143 | if (!mSslCtx) { |
| 144 | return false; |
| 145 | } |
| 146 | SSL_CTX_sess_set_new_cb(mSslCtx.get(), DnsTlsTransport::newSessionCallback); |
| 147 | SSL_CTX_sess_set_remove_cb(mSslCtx.get(), DnsTlsTransport::removeSessionCallback); |
| 148 | return true; |
| 149 | } |
| 150 | |
| 151 | bssl::UniquePtr<SSL> DnsTlsTransport::sslConnect(int fd) { |
| 152 | // Check TLS context. |
| 153 | if (!mSslCtx) { |
| 154 | ALOGE("Internal error: context is null in ssl connect"); |
| 155 | return nullptr; |
| 156 | } |
| 157 | if (!SSL_CTX_set_max_proto_version(mSslCtx.get(), TLS1_3_VERSION) || |
| 158 | !SSL_CTX_set_min_proto_version(mSslCtx.get(), TLS1_2_VERSION)) { |
| 159 | ALOGE("failed to min/max TLS versions"); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 160 | return nullptr; |
| 161 | } |
| 162 | |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 163 | bssl::UniquePtr<SSL> ssl(SSL_new(mSslCtx.get())); |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 164 | // This file descriptor is owned by a unique_fd, so don't let libssl close it. |
| 165 | bssl::UniquePtr<BIO> bio(BIO_new_socket(fd, BIO_NOCLOSE)); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 166 | SSL_set_bio(ssl.get(), bio.get(), bio.get()); |
| 167 | bio.release(); |
| 168 | |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 169 | // Add this transport as the 0-index extra data for the socket. |
| 170 | // This is used by newSessionCallback. |
| 171 | if (SSL_set_ex_data(ssl.get(), 0, this) != 1) { |
| 172 | ALOGE("failed to associate SSL socket to transport"); |
| 173 | return nullptr; |
| 174 | } |
| 175 | |
| 176 | // Add this transport as the 0-index extra data for the context. |
| 177 | // This is used by removeSessionCallback. |
| 178 | if (SSL_CTX_set_ex_data(mSslCtx.get(), 0, this) != 1) { |
| 179 | ALOGE("failed to associate SSL context to transport"); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 180 | return nullptr; |
| 181 | } |
| 182 | |
Ben Schwartz | 1691bc4 | 2017-08-16 12:53:09 -0400 | [diff] [blame] | 183 | if (!mServer.name.empty()) { |
| 184 | if (SSL_set_tlsext_host_name(ssl.get(), mServer.name.c_str()) != 1) { |
| 185 | ALOGE("Failed to set SNI to %s", mServer.name.c_str()); |
| 186 | return nullptr; |
| 187 | } |
| 188 | X509_VERIFY_PARAM* param = SSL_get0_param(ssl.get()); |
| 189 | X509_VERIFY_PARAM_set1_host(param, mServer.name.c_str(), 0); |
| 190 | // This will cause the handshake to fail if certificate verification fails. |
| 191 | SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, nullptr); |
| 192 | } |
| 193 | |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 194 | bssl::UniquePtr<SSL_SESSION> session; |
| 195 | { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 196 | std::lock_guard<std::mutex> guard(mLock); |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 197 | if (!mSessions.empty()) { |
| 198 | session = std::move(mSessions.front()); |
| 199 | mSessions.pop_front(); |
| 200 | } else if (DBG) { |
| 201 | ALOGD("Starting without session ticket."); |
| 202 | } |
| 203 | } |
| 204 | if (session) { |
| 205 | SSL_set_session(ssl.get(), session.get()); |
| 206 | } |
| 207 | |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 208 | for (;;) { |
| 209 | if (DBG) { |
| 210 | ALOGD("%u Calling SSL_connect", mMark); |
| 211 | } |
| 212 | int ret = SSL_connect(ssl.get()); |
| 213 | if (DBG) { |
| 214 | ALOGD("%u SSL_connect returned %d", mMark, ret); |
| 215 | } |
| 216 | if (ret == 1) break; // SSL handshake complete; |
| 217 | |
| 218 | const int ssl_err = SSL_get_error(ssl.get(), ret); |
| 219 | switch (ssl_err) { |
| 220 | case SSL_ERROR_WANT_READ: |
| 221 | if (waitForReading(fd) != 1) { |
| 222 | ALOGW("SSL_connect read error"); |
| 223 | return nullptr; |
| 224 | } |
| 225 | break; |
| 226 | case SSL_ERROR_WANT_WRITE: |
| 227 | if (waitForWriting(fd) != 1) { |
| 228 | ALOGW("SSL_connect write error"); |
| 229 | return nullptr; |
| 230 | } |
| 231 | break; |
| 232 | default: |
| 233 | ALOGW("SSL_connect error %d, errno=%d", ssl_err, errno); |
| 234 | return nullptr; |
| 235 | } |
| 236 | } |
| 237 | |
Ben Schwartz | 5250462 | 2017-07-11 12:21:13 -0400 | [diff] [blame] | 238 | if (!mServer.fingerprints.empty()) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 239 | if (DBG) { |
| 240 | ALOGD("Checking DNS over TLS fingerprint"); |
| 241 | } |
Ben Schwartz | f028d39 | 2017-07-10 15:07:12 -0400 | [diff] [blame] | 242 | |
| 243 | // We only care that the chain is internally self-consistent, not that |
| 244 | // it chains to a trusted root, so we can ignore some kinds of errors. |
| 245 | // TODO: Add a CA root verification mode that respects these errors. |
| 246 | int verify_result = SSL_get_verify_result(ssl.get()); |
| 247 | switch (verify_result) { |
| 248 | case X509_V_OK: |
| 249 | case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT: |
| 250 | case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN: |
| 251 | case X509_V_ERR_CERT_UNTRUSTED: |
| 252 | break; |
| 253 | default: |
| 254 | ALOGW("Invalid certificate chain, error %d", verify_result); |
| 255 | return nullptr; |
| 256 | } |
| 257 | |
| 258 | STACK_OF(X509) *chain = SSL_get_peer_cert_chain(ssl.get()); |
| 259 | if (!chain) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 260 | ALOGW("Server has null certificate"); |
| 261 | return nullptr; |
| 262 | } |
Ben Schwartz | f028d39 | 2017-07-10 15:07:12 -0400 | [diff] [blame] | 263 | // Chain and its contents are owned by ssl, so we don't need to free explicitly. |
| 264 | bool matched = false; |
| 265 | for (size_t i = 0; i < sk_X509_num(chain); ++i) { |
| 266 | // This appears to be O(N^2), but there doesn't seem to be a straightforward |
| 267 | // way to walk a STACK_OF nondestructively in linear time. |
| 268 | X509* cert = sk_X509_value(chain, i); |
| 269 | std::vector<uint8_t> digest; |
| 270 | if (!getSPKIDigest(cert, &digest)) { |
| 271 | ALOGE("Digest computation failed"); |
| 272 | return nullptr; |
| 273 | } |
| 274 | |
| 275 | if (mServer.fingerprints.count(digest) > 0) { |
| 276 | matched = true; |
| 277 | break; |
| 278 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 279 | } |
| 280 | |
Ben Schwartz | f028d39 | 2017-07-10 15:07:12 -0400 | [diff] [blame] | 281 | if (!matched) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 282 | ALOGW("No matching fingerprint"); |
| 283 | return nullptr; |
| 284 | } |
Ben Schwartz | f028d39 | 2017-07-10 15:07:12 -0400 | [diff] [blame] | 285 | |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 286 | if (DBG) { |
| 287 | ALOGD("DNS over TLS fingerprint is correct"); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | if (DBG) { |
| 292 | ALOGD("%u handshake complete", mMark); |
| 293 | } |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 294 | |
| 295 | return ssl; |
| 296 | } |
| 297 | |
| 298 | // static |
| 299 | int DnsTlsTransport::newSessionCallback(SSL* ssl, SSL_SESSION* session) { |
| 300 | if (!session) { |
| 301 | return 0; |
| 302 | } |
| 303 | if (DBG) { |
| 304 | ALOGD("Recording session ticket"); |
| 305 | } |
| 306 | DnsTlsTransport* xport = reinterpret_cast<DnsTlsTransport*>( |
| 307 | SSL_get_ex_data(ssl, 0)); |
| 308 | if (!xport) { |
| 309 | ALOGE("null transport in new session callback"); |
| 310 | return 0; |
| 311 | } |
| 312 | xport->recordSession(session); |
| 313 | return 1; |
| 314 | } |
| 315 | |
| 316 | void DnsTlsTransport::removeSessionCallback(SSL_CTX* ssl_ctx, SSL_SESSION* session) { |
| 317 | if (DBG) { |
| 318 | ALOGD("Removing session ticket"); |
| 319 | } |
| 320 | DnsTlsTransport* xport = reinterpret_cast<DnsTlsTransport*>( |
| 321 | SSL_CTX_get_ex_data(ssl_ctx, 0)); |
| 322 | if (!xport) { |
| 323 | ALOGE("null transport in remove session callback"); |
| 324 | return; |
| 325 | } |
| 326 | xport->removeSession(session); |
| 327 | } |
| 328 | |
| 329 | void DnsTlsTransport::recordSession(SSL_SESSION* session) { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 330 | std::lock_guard<std::mutex> guard(mLock); |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 331 | mSessions.emplace_front(session); |
| 332 | if (mSessions.size() > 5) { |
| 333 | if (DBG) { |
| 334 | ALOGD("Too many sessions; trimming"); |
| 335 | } |
| 336 | mSessions.pop_back(); |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | void DnsTlsTransport::removeSession(SSL_SESSION* session) { |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 341 | std::lock_guard<std::mutex> guard(mLock); |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 342 | if (session) { |
| 343 | // TODO: Consider implementing targeted removal. |
| 344 | mSessions.clear(); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | void DnsTlsTransport::sslDisconnect(bssl::UniquePtr<SSL> ssl, base::unique_fd fd) { |
| 349 | if (ssl) { |
| 350 | SSL_shutdown(ssl.get()); |
| 351 | ssl.reset(); |
| 352 | } |
| 353 | fd.reset(); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | bool DnsTlsTransport::sslWrite(int fd, SSL *ssl, const uint8_t *buffer, int len) { |
| 357 | if (DBG) { |
| 358 | ALOGD("%u Writing %d bytes", mMark, len); |
| 359 | } |
| 360 | for (;;) { |
| 361 | int ret = SSL_write(ssl, buffer, len); |
| 362 | if (ret == len) break; // SSL write complete; |
| 363 | |
| 364 | if (ret < 1) { |
| 365 | const int ssl_err = SSL_get_error(ssl, ret); |
| 366 | switch (ssl_err) { |
| 367 | case SSL_ERROR_WANT_WRITE: |
| 368 | if (waitForWriting(fd) != 1) { |
| 369 | if (DBG) { |
| 370 | ALOGW("SSL_write error"); |
| 371 | } |
| 372 | return false; |
| 373 | } |
| 374 | continue; |
| 375 | case 0: |
| 376 | break; // SSL write complete; |
| 377 | default: |
| 378 | if (DBG) { |
| 379 | ALOGW("SSL_write error %d", ssl_err); |
| 380 | } |
| 381 | return false; |
| 382 | } |
| 383 | } |
| 384 | } |
| 385 | if (DBG) { |
| 386 | ALOGD("%u Wrote %d bytes", mMark, len); |
| 387 | } |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | // Read exactly len bytes into buffer or fail |
| 392 | bool DnsTlsTransport::sslRead(int fd, SSL *ssl, uint8_t *buffer, int len) { |
| 393 | int remaining = len; |
| 394 | while (remaining > 0) { |
| 395 | int ret = SSL_read(ssl, buffer + (len - remaining), remaining); |
| 396 | if (ret == 0) { |
| 397 | ALOGE("SSL socket closed with %i of %i bytes remaining", remaining, len); |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | if (ret < 0) { |
| 402 | const int ssl_err = SSL_get_error(ssl, ret); |
| 403 | if (ssl_err == SSL_ERROR_WANT_READ) { |
| 404 | if (waitForReading(fd) != 1) { |
| 405 | if (DBG) { |
| 406 | ALOGW("SSL_read error"); |
| 407 | } |
| 408 | return false; |
| 409 | } |
| 410 | continue; |
| 411 | } else { |
| 412 | if (DBG) { |
| 413 | ALOGW("SSL_read error %d", ssl_err); |
| 414 | } |
| 415 | return false; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | remaining -= ret; |
| 420 | } |
| 421 | return true; |
| 422 | } |
| 423 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 424 | DnsTlsTransport::Response DnsTlsTransport::query(const uint8_t *query, size_t qlen, |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 425 | uint8_t *response, size_t limit, int *resplen) { |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 426 | android::base::unique_fd fd = makeConnectedSocket(); |
| 427 | if (fd.get() < 0) { |
| 428 | ALOGD("%u makeConnectedSocket() failed with: %s", mMark, strerror(errno)); |
| 429 | return Response::network_error; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 430 | } |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 431 | bssl::UniquePtr<SSL> ssl = sslConnect(fd.get()); |
| 432 | if (!ssl) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 433 | return Response::network_error; |
| 434 | } |
| 435 | |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 436 | Response res = sendQuery(fd.get(), ssl.get(), query, qlen); |
| 437 | if (res == Response::success) { |
| 438 | res = readResponse(fd.get(), ssl.get(), query, response, limit, resplen); |
| 439 | } |
| 440 | |
| 441 | sslDisconnect(std::move(ssl), std::move(fd)); |
| 442 | return res; |
| 443 | } |
| 444 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 445 | DnsTlsTransport::Response DnsTlsTransport::sendQuery(int fd, SSL* ssl, |
| 446 | const uint8_t *query, size_t qlen) { |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 447 | if (DBG) { |
| 448 | ALOGD("sending query"); |
| 449 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 450 | uint8_t queryHeader[2]; |
| 451 | queryHeader[0] = qlen >> 8; |
| 452 | queryHeader[1] = qlen; |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 453 | if (!sslWrite(fd, ssl, queryHeader, 2)) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 454 | return Response::network_error; |
| 455 | } |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 456 | if (!sslWrite(fd, ssl, query, qlen)) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 457 | return Response::network_error; |
| 458 | } |
| 459 | if (DBG) { |
| 460 | ALOGD("%u SSL_write complete", mMark); |
| 461 | } |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 462 | return Response::success; |
| 463 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 464 | |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 465 | DnsTlsTransport::Response DnsTlsTransport::readResponse(int fd, SSL* ssl, |
| 466 | const uint8_t *query, uint8_t *response, size_t limit, int *resplen) { |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 467 | if (DBG) { |
| 468 | ALOGD("reading response"); |
| 469 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 470 | uint8_t responseHeader[2]; |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 471 | if (!sslRead(fd, ssl, responseHeader, 2)) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 472 | if (DBG) { |
| 473 | ALOGW("%u Failed to read 2-byte length header", mMark); |
| 474 | } |
| 475 | return Response::network_error; |
| 476 | } |
| 477 | const uint16_t responseSize = (responseHeader[0] << 8) | responseHeader[1]; |
| 478 | if (DBG) { |
| 479 | ALOGD("%u Expecting response of size %i", mMark, responseSize); |
| 480 | } |
| 481 | if (responseSize > limit) { |
| 482 | ALOGE("%u Response doesn't fit in output buffer: %i", mMark, responseSize); |
| 483 | return Response::limit_error; |
| 484 | } |
Ben Schwartz | a13c23a | 2017-10-02 12:06:21 -0400 | [diff] [blame] | 485 | if (!sslRead(fd, ssl, response, responseSize)) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 486 | if (DBG) { |
| 487 | ALOGW("%u Failed to read %i bytes", mMark, responseSize); |
| 488 | } |
| 489 | return Response::network_error; |
| 490 | } |
| 491 | if (DBG) { |
| 492 | ALOGD("%u SSL_read complete", mMark); |
| 493 | } |
| 494 | |
| 495 | if (response[0] != query[0] || response[1] != query[1]) { |
| 496 | ALOGE("reply query ID != query ID"); |
| 497 | return Response::internal_error; |
| 498 | } |
| 499 | |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 500 | *resplen = responseSize; |
| 501 | return Response::success; |
| 502 | } |
| 503 | |
Ben Schwartz | 5250462 | 2017-07-11 12:21:13 -0400 | [diff] [blame] | 504 | // static |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 505 | bool DnsTlsTransport::validate(const DnsTlsServer& server, unsigned netid) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 506 | if (DBG) { |
| 507 | ALOGD("Beginning validation on %u", netid); |
| 508 | } |
| 509 | // Generate "<random>-dnsotls-ds.metric.gstatic.com", which we will lookup through |ss| in |
| 510 | // order to prove that it is actually a working DNS over TLS server. |
| 511 | static const char kDnsSafeChars[] = |
| 512 | "abcdefhijklmnopqrstuvwxyz" |
| 513 | "ABCDEFHIJKLMNOPQRSTUVWXYZ" |
| 514 | "0123456789"; |
| 515 | const auto c = [](uint8_t rnd) -> uint8_t { |
| 516 | return kDnsSafeChars[(rnd % ARRAY_SIZE(kDnsSafeChars))]; |
| 517 | }; |
| 518 | uint8_t rnd[8]; |
| 519 | arc4random_buf(rnd, ARRAY_SIZE(rnd)); |
| 520 | // We could try to use res_mkquery() here, but it's basically the same. |
| 521 | uint8_t query[] = { |
| 522 | rnd[6], rnd[7], // [0-1] query ID |
| 523 | 1, 0, // [2-3] flags; query[2] = 1 for recursion desired (RD). |
| 524 | 0, 1, // [4-5] QDCOUNT (number of queries) |
| 525 | 0, 0, // [6-7] ANCOUNT (number of answers) |
| 526 | 0, 0, // [8-9] NSCOUNT (number of name server records) |
| 527 | 0, 0, // [10-11] ARCOUNT (number of additional records) |
| 528 | 17, c(rnd[0]), c(rnd[1]), c(rnd[2]), c(rnd[3]), c(rnd[4]), c(rnd[5]), |
| 529 | '-', 'd', 'n', 's', 'o', 't', 'l', 's', '-', 'd', 's', |
| 530 | 6, 'm', 'e', 't', 'r', 'i', 'c', |
| 531 | 7, 'g', 's', 't', 'a', 't', 'i', 'c', |
| 532 | 3, 'c', 'o', 'm', |
| 533 | 0, // null terminator of FQDN (root TLD) |
| 534 | 0, ns_t_aaaa, // QTYPE |
| 535 | 0, ns_c_in // QCLASS |
| 536 | }; |
| 537 | const int qlen = ARRAY_SIZE(query); |
| 538 | |
| 539 | const int kRecvBufSize = 4 * 1024; |
| 540 | uint8_t recvbuf[kRecvBufSize]; |
| 541 | |
| 542 | // At validation time, we only know the netId, so we have to guess/compute the |
| 543 | // corresponding socket mark. |
| 544 | Fwmark fwmark; |
| 545 | fwmark.permission = PERMISSION_SYSTEM; |
| 546 | fwmark.explicitlySelected = true; |
| 547 | fwmark.protectedFromVpn = true; |
| 548 | fwmark.netId = netid; |
| 549 | unsigned mark = fwmark.intValue; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 550 | int replylen = 0; |
Ben Schwartz | 66810f6 | 2017-10-16 19:27:46 -0400 | [diff] [blame^] | 551 | DnsTlsTransport transport(server, mark); |
| 552 | if (!transport.initialize()) { |
| 553 | return false; |
| 554 | } |
| 555 | transport.query(query, qlen, recvbuf, kRecvBufSize, &replylen); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 556 | if (replylen == 0) { |
| 557 | if (DBG) { |
Ben Schwartz | 5250462 | 2017-07-11 12:21:13 -0400 | [diff] [blame] | 558 | ALOGD("query failed"); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 559 | } |
| 560 | return false; |
| 561 | } |
| 562 | |
| 563 | if (replylen < NS_HFIXEDSZ) { |
| 564 | if (DBG) { |
| 565 | ALOGW("short response: %d", replylen); |
| 566 | } |
| 567 | return false; |
| 568 | } |
| 569 | |
| 570 | const int qdcount = (recvbuf[4] << 8) | recvbuf[5]; |
| 571 | if (qdcount != 1) { |
| 572 | ALOGW("reply query count != 1: %d", qdcount); |
| 573 | return false; |
| 574 | } |
| 575 | |
| 576 | const int ancount = (recvbuf[6] << 8) | recvbuf[7]; |
| 577 | if (DBG) { |
| 578 | ALOGD("%u answer count: %d", netid, ancount); |
| 579 | } |
| 580 | |
| 581 | // TODO: Further validate the response contents (check for valid AAAA record, ...). |
| 582 | // Note that currently, integration tests rely on this function accepting a |
| 583 | // response with zero records. |
| 584 | #if 0 |
| 585 | for (int i = 0; i < resplen; i++) { |
| 586 | ALOGD("recvbuf[%d] = %d %c", i, recvbuf[i], recvbuf[i]); |
| 587 | } |
| 588 | #endif |
| 589 | return true; |
| 590 | } |
| 591 | |
| 592 | } // namespace net |
| 593 | } // namespace android |