File indexing completed on 2026-04-17 08:35:03
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #ifndef _THRIFT_TRANSPORT_TSSLSOCKET_H_
0021 #define _THRIFT_TRANSPORT_TSSLSOCKET_H_ 1
0022
0023
0024 #include <thrift/transport/TSocket.h>
0025
0026 #include <openssl/ssl.h>
0027 #include <string>
0028 #include <thrift/concurrency/Mutex.h>
0029
0030 namespace apache {
0031 namespace thrift {
0032 namespace transport {
0033
0034 class AccessManager;
0035 class SSLContext;
0036
0037 enum SSLProtocol {
0038 SSLTLS = 0,
0039
0040 SSLv3 = 2,
0041 TLSv1_0 = 3,
0042 TLSv1_1 = 4,
0043 TLSv1_2 = 5,
0044 LATEST = TLSv1_2
0045 };
0046
0047 #define TSSL_EINTR 0
0048 #define TSSL_DATA 1
0049
0050
0051
0052
0053
0054
0055
0056
0057 void initializeOpenSSL();
0058
0059
0060
0061
0062
0063
0064
0065 void cleanupOpenSSL();
0066
0067
0068
0069
0070 class TSSLSocket : public TSocket {
0071 public:
0072 ~TSSLSocket() override;
0073
0074
0075
0076 bool isOpen() const override;
0077 bool peek() override;
0078 void open() override;
0079 void close() override;
0080 bool hasPendingDataToRead() override;
0081 uint32_t read(uint8_t* buf, uint32_t len) override;
0082 void write(const uint8_t* buf, uint32_t len) override;
0083 uint32_t write_partial(const uint8_t* buf, uint32_t len) override;
0084 void flush() override;
0085
0086
0087
0088
0089
0090 void server(bool flag) { server_ = flag; }
0091
0092
0093
0094 bool server() const { return server_; }
0095
0096
0097
0098
0099
0100 virtual void access(std::shared_ptr<AccessManager> manager) { access_ = manager; }
0101
0102
0103
0104 void setLibeventSafe() { eventSafe_ = true; }
0105
0106
0107
0108 bool isLibeventSafe() const { return eventSafe_; }
0109
0110 protected:
0111
0112
0113
0114 TSSLSocket(std::shared_ptr<SSLContext> ctx, std::shared_ptr<TConfiguration> config = nullptr);
0115
0116
0117
0118 TSSLSocket(std::shared_ptr<SSLContext> ctx, std::shared_ptr<THRIFT_SOCKET> interruptListener,
0119 std::shared_ptr<TConfiguration> config = nullptr);
0120
0121
0122
0123
0124
0125 TSSLSocket(std::shared_ptr<SSLContext> ctx, THRIFT_SOCKET socket, std::shared_ptr<TConfiguration> config = nullptr);
0126
0127
0128
0129
0130
0131 TSSLSocket(std::shared_ptr<SSLContext> ctx, THRIFT_SOCKET socket, std::shared_ptr<THRIFT_SOCKET> interruptListener,
0132 std::shared_ptr<TConfiguration> config = nullptr);
0133
0134
0135
0136
0137
0138
0139 TSSLSocket(std::shared_ptr<SSLContext> ctx, std::string host, int port, std::shared_ptr<TConfiguration> config = nullptr);
0140
0141
0142
0143
0144
0145
0146 TSSLSocket(std::shared_ptr<SSLContext> ctx, std::string host, int port, std::shared_ptr<THRIFT_SOCKET> interruptListener,
0147 std::shared_ptr<TConfiguration> config = nullptr);
0148
0149
0150
0151 virtual void authorize();
0152
0153
0154
0155 void initializeHandshake();
0156
0157
0158
0159 void initializeHandshakeParams();
0160
0161
0162
0163 bool checkHandshake();
0164
0165
0166
0167
0168
0169
0170
0171
0172 unsigned int waitForEvent(bool wantRead);
0173
0174 bool server_;
0175 SSL* ssl_;
0176 std::shared_ptr<SSLContext> ctx_;
0177 std::shared_ptr<AccessManager> access_;
0178 friend class TSSLSocketFactory;
0179
0180 private:
0181 bool handshakeCompleted_;
0182 int readRetryCount_;
0183 bool eventSafe_;
0184
0185 void init();
0186 };
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 class TSSLSocketFactory {
0206 public:
0207
0208
0209
0210
0211
0212 TSSLSocketFactory(SSLProtocol protocol = SSLTLS);
0213 virtual ~TSSLSocketFactory();
0214
0215
0216
0217 virtual std::shared_ptr<TSSLSocket> createSocket();
0218
0219
0220
0221 virtual std::shared_ptr<TSSLSocket> createSocket(std::shared_ptr<THRIFT_SOCKET> interruptListener);
0222
0223
0224
0225
0226
0227 virtual std::shared_ptr<TSSLSocket> createSocket(THRIFT_SOCKET socket);
0228
0229
0230
0231
0232
0233 virtual std::shared_ptr<TSSLSocket> createSocket(THRIFT_SOCKET socket, std::shared_ptr<THRIFT_SOCKET> interruptListener);
0234
0235
0236
0237
0238
0239
0240 virtual std::shared_ptr<TSSLSocket> createSocket(const std::string& host, int port);
0241
0242
0243
0244
0245
0246
0247 virtual std::shared_ptr<TSSLSocket> createSocket(const std::string& host, int port, std::shared_ptr<THRIFT_SOCKET> interruptListener);
0248
0249
0250
0251
0252
0253 virtual void ciphers(const std::string& enable);
0254
0255
0256
0257
0258
0259 virtual void authenticate(bool required);
0260
0261
0262
0263
0264
0265
0266 virtual void loadCertificate(const char* path, const char* format = "PEM");
0267 virtual void loadCertificateFromBuffer(const char* aCertificate, const char* format = "PEM");
0268
0269
0270
0271
0272
0273
0274 virtual void loadPrivateKey(const char* path, const char* format = "PEM");
0275 virtual void loadPrivateKeyFromBuffer(const char* aPrivateKey, const char* format = "PEM");
0276
0277
0278
0279
0280
0281 virtual void loadTrustedCertificates(const char* path, const char* capath = nullptr);
0282 virtual void loadTrustedCertificatesFromBuffer(const char* aCertificate, const char* aChain = nullptr);
0283
0284
0285
0286 virtual void randomize();
0287
0288
0289
0290 void overrideDefaultPasswordCallback();
0291
0292
0293
0294
0295
0296 virtual void server(bool flag) { server_ = flag; }
0297
0298
0299
0300
0301
0302 virtual bool server() const { return server_; }
0303
0304
0305
0306
0307
0308 virtual void access(std::shared_ptr<AccessManager> manager) { access_ = manager; }
0309 static void setManualOpenSSLInitialization(bool manualOpenSSLInitialization) {
0310 manualOpenSSLInitialization_ = manualOpenSSLInitialization;
0311 }
0312
0313 protected:
0314 std::shared_ptr<SSLContext> ctx_;
0315
0316
0317
0318
0319
0320
0321
0322
0323 virtual void getPassword(std::string& , int ) {}
0324
0325 private:
0326 bool server_;
0327 std::shared_ptr<AccessManager> access_;
0328 static concurrency::Mutex mutex_;
0329 static uint64_t count_;
0330 THRIFT_EXPORT static bool manualOpenSSLInitialization_;
0331 void setup(std::shared_ptr<TSSLSocket> ssl);
0332 static int passwordCallback(char* password, int size, int, void* data);
0333 };
0334
0335
0336
0337
0338 class TSSLException : public TTransportException {
0339 public:
0340 TSSLException(const std::string& message)
0341 : TTransportException(TTransportException::INTERNAL_ERROR, message) {}
0342
0343 const char* what() const noexcept override {
0344 if (message_.empty()) {
0345 return "TSSLException";
0346 } else {
0347 return message_.c_str();
0348 }
0349 }
0350 };
0351
0352
0353
0354
0355 class SSLContext {
0356 public:
0357 SSLContext(const SSLProtocol& protocol = SSLTLS);
0358 virtual ~SSLContext();
0359 SSL* createSSL();
0360 SSL_CTX* get() { return ctx_; }
0361
0362 private:
0363 SSL_CTX* ctx_;
0364 };
0365
0366
0367
0368
0369
0370
0371
0372 class AccessManager {
0373 public:
0374 enum Decision {
0375 DENY = -1,
0376 SKIP = 0,
0377 ALLOW = 1
0378 };
0379
0380
0381
0382 virtual ~AccessManager() = default;
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394 virtual Decision verify(const sockaddr_storage& ) noexcept { return DENY; }
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406
0407
0408 virtual Decision verify(const std::string& ,
0409 const char* ,
0410 int ) noexcept {
0411 return DENY;
0412 }
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422 virtual Decision verify(const sockaddr_storage& ,
0423 const char* ,
0424 int ) noexcept {
0425 return DENY;
0426 }
0427 };
0428
0429 typedef AccessManager::Decision Decision;
0430
0431 class DefaultClientAccessManager : public AccessManager {
0432 public:
0433
0434 Decision verify(const sockaddr_storage& sa) noexcept override;
0435 Decision verify(const std::string& host, const char* name, int size) noexcept override;
0436 Decision verify(const sockaddr_storage& sa, const char* data, int size) noexcept override;
0437 };
0438 }
0439 }
0440 }
0441
0442 #endif