| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 1 | Thrift C++ Software Library |
| 2 | |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 3 | # License |
| Bryan Duxbury | def30a6 | 2009-04-08 00:19:37 +0000 | [diff] [blame] | 4 | |
| 5 | Licensed to the Apache Software Foundation (ASF) under one |
| 6 | or more contributor license agreements. See the NOTICE file |
| 7 | distributed with this work for additional information |
| 8 | regarding copyright ownership. The ASF licenses this file |
| 9 | to you under the Apache License, Version 2.0 (the |
| 10 | "License"); you may not use this file except in compliance |
| 11 | with the License. You may obtain a copy of the License at |
| 12 | |
| 13 | http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | |
| 15 | Unless required by applicable law or agreed to in writing, |
| 16 | software distributed under the License is distributed on an |
| 17 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 | KIND, either express or implied. See the License for the |
| 19 | specific language governing permissions and limitations |
| 20 | under the License. |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 21 | |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 22 | |
| 23 | # Using Thrift with C++ |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 24 | |
| 25 | The Thrift C++ libraries are built using the GNU tools. Follow the instructions |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 26 | in the top-level README.md |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 27 | |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 28 | In case you do not want to open another README.md file, do this thrift src: |
| 29 | |
| 30 | ./bootstrap.sh |
| 31 | ./configure (--with-boost=/usr/local) |
| 32 | make |
| 33 | sudo make install |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 34 | |
| 35 | Thrift is divided into two libraries. |
| 36 | |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 37 | * libthrift - The core Thrift library contains all the core Thrift code. It requires |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 38 | boost shared pointers, pthreads, and librt. |
| 39 | |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 40 | * libthriftnb - This library contains the Thrift nonblocking server, which uses libevent. |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 41 | To link this library you will also need to link libevent. |
| 42 | |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 43 | ## Linking Against Thrift |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 44 | |
| 45 | After you build and install Thrift the libraries are installed to |
| 46 | /usr/local/lib by default. Make sure this is in your LDPATH. |
| 47 | |
| 48 | On Linux, the best way to do this is to ensure that /usr/local/lib is in |
| 49 | your /etc/ld.so.conf and then run /sbin/ldconfig. |
| 50 | |
| 51 | Depending upon whether you are linking dynamically or statically and how |
| 52 | your build environment it set up, you may need to include additional |
| 53 | libraries when linking against thrift, such as librt and/or libpthread. If |
| 54 | you are using libthriftnb you will also need libevent. |
| 55 | |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 56 | ## Dependencies |
| Mark Slee | 54b7ab9 | 2007-03-06 00:06:27 +0000 | [diff] [blame] | 57 | |
| 58 | boost shared pointers |
| 59 | http://www.boost.org/libs/smart_ptr/smart_ptr.htm |
| 60 | |
| 61 | libevent (for libthriftnb only) |
| 62 | http://monkey.org/~provos/libevent/ |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 63 | |
| 64 | # Using Thrift with C++ on Windows |
| 65 | |
| Roger Meier | faf52db | 2014-06-09 22:41:06 +0200 | [diff] [blame] | 66 | You need to define an environment variables for 3rd party components separately: |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 67 | |
| Roger Meier | faf52db | 2014-06-09 22:41:06 +0200 | [diff] [blame] | 68 | BOOST_ROOT : For boost, e.g. D:\boost_1_55_0 |
| 69 | OPENSSL_ROOT_DIR : For OpenSSL, e.g. D:\OpenSSL-Win32 |
| 70 | |
| 71 | only required by libthriftnb: |
| 72 | |
| 73 | LIBEVENT_ROOT_DIR : For Libevent e.g. D:\libevent-2.0.21-stable |
| 74 | |
| 75 | See /3rdparty.user for more details. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 76 | |
| 77 | Thrift is divided into two libraries. |
| 78 | |
| 79 | * libthrift - The core Thrift library contains all the core Thrift code. It requires |
| 80 | boost shared pointers, pthreads, and librt. |
| 81 | |
| 82 | * libthriftnb - This library contains the Thrift nonblocking server, which uses libevent. |
| 83 | To link this library you will also need to link libevent. |
| 84 | |
| 85 | ## Linking Against Thrift |
| 86 | |
| 87 | You need to link your project that uses thrift against all the thrift |
| Roger Meier | faf52db | 2014-06-09 22:41:06 +0200 | [diff] [blame] | 88 | dependencies; in the case of libthrift, boost and for |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 89 | libthriftnb, libevent. |
| 90 | |
| 91 | In the project properties you must also set HAVE_CONFIG_H as force include |
| 92 | the config header: "windows/confg.h" |
| 93 | |
| 94 | ## Dependencies |
| 95 | |
| 96 | boost shared pointers |
| 97 | http://www.boost.org/libs/smart_ptr/smart_ptr.htm |
| 98 | |
| 99 | boost thread |
| 100 | http://www.boost.org/doc/libs/release/doc/html/thread.html |
| 101 | |
| 102 | libevent (for libthriftnb only) |
| 103 | http://monkey.org/~provos/libevent/ |
| 104 | |
| 105 | ## Notes on boost thread (static vs shared): |
| 106 | |
| 107 | By default lib/cpp/windows/force_inc.h defines: |
| 108 | |
| 109 | #define BOOST_ALL_NO_LIB 1 |
| 110 | #define BOOST_THREAD_NO_LIB 1 |
| 111 | |
| 112 | This has for effect to have the host application linking against Thrift |
| 113 | to have to link with boost thread as a static library. |
| 114 | |
| 115 | If you wanted instead to link with boost thread as a shared library, |
| 116 | you'll need to uncomment those two lines, and recompile. |
| 117 | |
| 118 | ## Windows version compatibility |
| 119 | |
| 120 | The Thrift library targets Windows XP for broadest compatbility. A notable |
| 121 | difference is in the Windows-specific implementation of the socket poll |
| 122 | function. To target Vista, Win7 or other versions, comment out the line |
| 123 | |
| 124 | #define TARGET_WIN_XP. |
| 125 | |
| 126 | ## Named Pipes |
| 127 | |
| 128 | Named Pipe transport has been added in the TPipe and TPipeServer classes. This |
| 129 | is currently Windows-only. Named pipe transport for *NIX has not been |
| 130 | implemented. Domain sockets are a better choice for local IPC under non-Windows |
| 131 | OS's. *NIX named pipes only support 1:1 client-server connection. |
| 132 | |
| 133 | # Thrift/SSL |
| 134 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 135 | ## Scope |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 136 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 137 | This SSL only supports blocking mode socket I/O. It can only be used with |
| 138 | TSimpleServer, TThreadedServer, and TThreadPoolServer. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 139 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 140 | ## Implementation |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 141 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 142 | There're two main classes TSSLSocketFactory and TSSLSocket. Instances of |
| 143 | TSSLSocket are always created from TSSLSocketFactory. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 144 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 145 | PosixSSLThreadFactory creates PosixSSLThread. The only difference from the |
| 146 | PthreadThread type is that it cleanups OpenSSL error queue upon exiting |
| 147 | the thread. Ideally, OpenSSL APIs should only be called from PosixSSLThread. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 148 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 149 | ## How to use SSL APIs |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 150 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 151 | This is for demo. In real code, typically only one TSSLSocketFactory |
| 152 | instance is needed. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 153 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 154 | shared_ptr<TSSLSocketFactory> getSSLSocketFactory() { |
| 155 | shared_ptr<TSSLSocketFactory> factory(new TSSLSocketFactory()); |
| 156 | // client: load trusted certificates |
| 157 | factory->loadTrustedCertificates("my-trusted-ca-certificates.pem"); |
| 158 | // client: optionally set your own access manager, otherwise, |
| 159 | // the default client access manager will be loaded. |
| 160 | |
| 161 | factory->loadCertificate("my-certificate-signed-by-ca.pem"); |
| 162 | factory->loadPrivateKey("my-private-key.pem"); |
| 163 | // server: optionally setup access manager |
| 164 | // shared_ptr<AccessManager> accessManager(new MyAccessManager); |
| 165 | // factory->access(accessManager); |
| 166 | ... |
| 167 | } |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 168 | |
| 169 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 170 | client code sample |
| 171 | |
| 172 | shared_ptr<TSSLSocketFactory> factory = getSSLSocketFactory(); |
| 173 | shared_ptr<TSocket> socket = factory.createSocket(host, port); |
| 174 | shared_ptr<TBufferedTransport> transport(new TBufferedTransport(socket)); |
| 175 | ... |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 176 | |
| 177 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 178 | server code sample |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 179 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 180 | shared_ptr<TSSLSocketFactory> factory = getSSLSocketFactory(); |
| 181 | shared_ptr<TSSLServerSocket> socket(new TSSLServerSocket(port, factory)); |
| 182 | shared_ptr<TTransportFactory> transportFactory(new TBufferedTransportFactory)); |
| 183 | ... |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 184 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 185 | ## AccessManager |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 186 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 187 | AccessManager defines a callback interface. It has three callback methods: |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 188 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 189 | (a) Decision verify(const sockaddr_storage& sa); |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 190 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 191 | (b) Decision verify(const string& host, const char* name, int size); |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 192 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 193 | (c) Decision verify(const sockaddr_storage& sa, const char* data, int size); |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 194 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 195 | After SSL handshake completes, additional checks are conducted. Application |
| 196 | is given the chance to decide whether or not to continue the conversation |
| 197 | with the remote. Application is queried through the above three "verify" |
| 198 | method. They are called at different points of the verification process. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 199 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 200 | Decisions can be one of ALLOW, DENY, and SKIP. ALLOW and DENY means the |
| 201 | conversation should be continued or disconnected, respectively. ALLOW and |
| 202 | DENY decision stops the verification process. SKIP means there's no decision |
| 203 | based on the given input, continue the verification process. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 204 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 205 | First, (a) is called with the remote IP. It is called once at the beginning. |
| 206 | "sa" is the IP address of the remote peer. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 207 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 208 | Then, the certificate of remote peer is loaded. SubjectAltName extensions |
| 209 | are extracted and sent to application for verification. When a DNS |
| 210 | subjectAltName field is extracted, (b) is called. When an IP subjectAltName |
| 211 | field is extracted, (c) is called. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 212 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 213 | The "host" in (b) is the value from TSocket::getHost() if this is a client |
| 214 | side socket, or TSocket::getPeerHost() if this is a server side socket. The |
| 215 | reason is client side socket initiates the connection. TSocket::getHost() |
| 216 | is the remote host name. On server side, the remote host name is unknown |
| 217 | unless it's retrieved through TSocket::getPeerHost(). Either way, "host" |
| 218 | should be the remote host name. Keep in mind, if TSocket::getPeerHost() |
| 219 | failed, it would return the remote host name in numeric format. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 220 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 221 | If all subjectAltName extensions were "skipped", the common name field would |
| 222 | be checked. It is sent to application through (c), where "sa" is the remote |
| 223 | IP address. "data" is the IP address extracted from subjectAltName IP |
| 224 | extension, and "size" is the length of the extension data. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 225 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 226 | If any of the above "verify" methods returned a decision ALLOW or DENY, the |
| 227 | verification process would be stopped. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 228 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 229 | If any of the above "verify" methods returned SKIP, that decision would be |
| 230 | ignored and the verification process would move on till the last item is |
| 231 | examined. At that point, if there's still no decision, the connection is |
| 232 | terminated. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 233 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 234 | Thread safety, an access manager should not store state information if it's |
| 235 | to be used by many SSL sockets. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 236 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 237 | ## SIGPIPE signal |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 238 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 239 | Applications running OpenSSL over network connections may crash if SIGPIPE |
| 240 | is not ignored. This happens when they receive a connection reset by remote |
| 241 | peer exception, which somehow triggers a SIGPIPE signal. If not handled, |
| 242 | this signal would kill the application. |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 243 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 244 | ## How to run test client/server in SSL mode |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 245 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 246 | The server and client expects the followings from the directory /test/ |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 247 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 248 | - keys/server.crt |
| 249 | - keys/server.key |
| 250 | - keys/CA.pem |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 251 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 252 | The file names are hard coded in the source code. You need to create these |
| 253 | certificates before you can run the test code in SSL mode. Make sure at least |
| 254 | one of the followings is included in "keys/server.crt", |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 255 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 256 | - subjectAltName, DNS localhost |
| 257 | - subjectAltName, IP 127.0.0.1 |
| 258 | - common name, localhost |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 259 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 260 | Run within /test/ folder, |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 261 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 262 | ./cpp/TestServer --ssl & |
| 263 | ./cpp/TestClient --ssl |
| Roger Meier | 6370cfd | 2014-05-04 22:21:58 +0200 | [diff] [blame] | 264 | |
| Roger Meier | 4fba9d2 | 2014-05-04 22:34:44 +0200 | [diff] [blame] | 265 | If "-h <host>" is used to run client, the above "localhost" in the above |
| 266 | keys/server.crt has to be replaced with that host name. |
| 267 | |
| 268 | ## TSSLSocketFactory::randomize() |
| 269 | |
| 270 | The default implementation of OpenSSLSocketFactory::randomize() simply calls |
| 271 | OpenSSL's RAND_poll() when OpenSSL library is first initialized. |
| 272 | |
| 273 | The PRNG seed is key to the application security. This method should be |
| 274 | overridden if it's not strong enough for you. |
| James E. King, III | 7bc9431 | 2017-03-31 21:25:20 -0400 | [diff] [blame] | 275 | |
| 276 | # Breaking Changes |
| 277 | |
| 278 | ## 0.11.0 |
| 279 | |
| 280 | In the pthread mutex implementation, the contention profiling code was enabled |
| 281 | by default in all builds. This changed to be disabled by default. (THRIFT-4151) |
| James E. King, III | 7f5a8c2 | 2017-04-04 09:36:38 -0400 | [diff] [blame^] | 282 | |
| 283 | In older releases, if a TSSLSocketFactory's lifetime was not at least as long |
| 284 | as the TSSLSockets it created, we silently reverted openssl to unsafe multithread behavior |
| 285 | and so the results were undefined. Changes were made in 0.11.0 that cause either an |
| 286 | assertion or a core instead of undefined behavior. The lifetime of a TSSLSocketFactory |
| 287 | *must* be longer than any TSSLSocket that it creates, otherwise openssl will be cleaned |
| 288 | up too early. If the static boolean is set to disable openssl initialization and |
| 289 | cleanup and leave it up to the consuming application, this requirement is not needed. |
| 290 | (THRIFT-4164) |
| 291 | |