Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 1 | /* |
| 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 | #ifndef _DNS_DNSTLSSOCKET_H |
| 18 | #define _DNS_DNSTLSSOCKET_H |
| 19 | |
| 20 | #include <future> |
| 21 | #include <mutex> |
| 22 | #include <openssl/ssl.h> |
| 23 | |
| 24 | #include <android-base/thread_annotations.h> |
| 25 | #include <android-base/unique_fd.h> |
| 26 | #include <netdutils/Slice.h> |
| 27 | #include <netdutils/Status.h> |
| 28 | |
| 29 | #include "dns/DnsTlsServer.h" |
| 30 | #include "dns/IDnsTlsSocket.h" |
| 31 | |
| 32 | namespace android { |
| 33 | namespace net { |
| 34 | |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 35 | class IDnsTlsSocketObserver; |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 36 | class DnsTlsSessionCache; |
| 37 | |
| 38 | using netdutils::Slice; |
| 39 | |
| 40 | // A class for managing a TLS socket that sends and receives messages in |
| 41 | // [length][value] format, with a 2-byte length (i.e. DNS-over-TCP format). |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 42 | // This class is not aware of query-response pairing or anything else about DNS. |
| 43 | // For the observer: |
| 44 | // This class is not re-entrant: the observer is not permitted to wait for a call to query() |
| 45 | // or the destructor in a callback. Doing so will result in deadlocks. |
| 46 | // This class may call the observer at any time after initialize(), until the destructor |
| 47 | // returns (but not after). |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 48 | class DnsTlsSocket : public IDnsTlsSocket { |
| 49 | public: |
| 50 | DnsTlsSocket(const DnsTlsServer& server, unsigned mark, |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 51 | IDnsTlsSocketObserver* _Nonnull observer, |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 52 | DnsTlsSessionCache* _Nonnull cache) : |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 53 | mMark(mark), mServer(server), mObserver(observer), mCache(cache) {} |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 54 | ~DnsTlsSocket(); |
| 55 | |
| 56 | // Creates the SSL context for this session and connect. Returns false on failure. |
| 57 | // This method should be called after construction and before use of a DnsTlsSocket. |
| 58 | // Only call this method once per DnsTlsSocket. |
| 59 | bool initialize() EXCLUDES(mLock); |
| 60 | |
| 61 | // Send a query on the provided SSL socket. |query| contains |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 62 | // the body of a query, not including the ID header. This function will typically return before |
| 63 | // the query is actually sent. If this function fails, DnsTlsSocketObserver will be |
| 64 | // notified that the socket is closed. |
| 65 | // Note that success here indicates successful sending, not receipt of a response. |
| 66 | // Thread-safe. |
| 67 | bool query(uint16_t id, const Slice query) override; |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 68 | |
| 69 | private: |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 70 | // Lock to be held by the SSL event loop thread. This is not normally in contention. |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 71 | std::mutex mLock; |
| 72 | |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 73 | // Forwards queries and receives responses. Blocks until the idle timeout. |
| 74 | void loop() EXCLUDES(mLock); |
| 75 | std::unique_ptr<std::thread> mLoopThread GUARDED_BY(mLock); |
| 76 | |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 77 | // On success, sets mSslFd to a socket connected to mAddr (the |
| 78 | // connection will likely be in progress if mProtocol is IPPROTO_TCP). |
| 79 | // On error, returns the errno. |
| 80 | netdutils::Status tcpConnect() REQUIRES(mLock); |
| 81 | |
| 82 | // Connect an SSL session on the provided socket. If connection fails, closing the |
| 83 | // socket remains the caller's responsibility. |
| 84 | bssl::UniquePtr<SSL> sslConnect(int fd) REQUIRES(mLock); |
| 85 | |
| 86 | // Disconnect the SSL session and close the socket. |
| 87 | void sslDisconnect() REQUIRES(mLock); |
| 88 | |
| 89 | // Writes a buffer to the socket. |
| 90 | bool sslWrite(const Slice buffer) REQUIRES(mLock); |
| 91 | |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 92 | // Reads exactly the specified number of bytes from the socket, or fails. |
| 93 | // Returns SSL_ERROR_NONE on success. |
| 94 | // If |wait| is true, then this function always blocks. Otherwise, it |
| 95 | // will return SSL_ERROR_WANT_READ if there is no data from the server to read. |
| 96 | int sslRead(const Slice buffer, bool wait) REQUIRES(mLock); |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 97 | |
| 98 | struct Query { |
| 99 | uint16_t id; |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 100 | Slice query; |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | bool sendQuery(const Query& q) REQUIRES(mLock); |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 104 | bool readResponse() REQUIRES(mLock); |
| 105 | |
| 106 | // SOCK_SEQPACKET socket pair used for sending queries from myriad query |
| 107 | // threads to the SSL thread. EOF indicates a close request. |
| 108 | // We have to use a socket pair (i.e. a pipe) because the SSL thread needs to wait in |
| 109 | // select() for input from either a remote server or a query thread. |
| 110 | base::unique_fd mIpcInFd; |
| 111 | base::unique_fd mIpcOutFd GUARDED_BY(mLock); |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 112 | |
| 113 | // SSL Socket fields. |
| 114 | bssl::UniquePtr<SSL_CTX> mSslCtx GUARDED_BY(mLock); |
| 115 | base::unique_fd mSslFd GUARDED_BY(mLock); |
| 116 | bssl::UniquePtr<SSL> mSsl GUARDED_BY(mLock); |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 117 | static constexpr std::chrono::seconds kIdleTimeout = std::chrono::seconds(20); |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 118 | |
| 119 | const unsigned mMark; // Socket mark |
| 120 | const DnsTlsServer mServer; |
Ben Schwartz | 8d1e7a6 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 121 | IDnsTlsSocketObserver* _Nonnull const mObserver; |
Ben Schwartz | ded1b70 | 2017-10-25 14:41:02 -0400 | [diff] [blame] | 122 | DnsTlsSessionCache* _Nonnull const mCache; |
| 123 | }; |
| 124 | |
| 125 | } // end of namespace net |
| 126 | } // end of namespace android |
| 127 | |
| 128 | #endif // _DNS_DNSTLSSOCKET_H |