| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: ServerTLS.h |
|---|
| 5 | // Purpose: Implementation of a server using TLS streams |
|---|
| 6 | // Created: 2003/08/06 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef SERVERTLS__H |
|---|
| 11 | #define SERVERTLS__H |
|---|
| 12 | |
|---|
| 13 | #include "ServerStream.h" |
|---|
| 14 | #include "SocketStreamTLS.h" |
|---|
| 15 | #include "SSLLib.h" |
|---|
| 16 | #include "TLSContext.h" |
|---|
| 17 | |
|---|
| 18 | // -------------------------------------------------------------------------- |
|---|
| 19 | // |
|---|
| 20 | // Class |
|---|
| 21 | // Name: ServerTLS |
|---|
| 22 | // Purpose: Implementation of a server using TLS streams |
|---|
| 23 | // Created: 2003/08/06 |
|---|
| 24 | // |
|---|
| 25 | // -------------------------------------------------------------------------- |
|---|
| 26 | template<int Port, int ListenBacklog = 128, bool ForkToHandleRequests = true> |
|---|
| 27 | class ServerTLS : public ServerStream<SocketStreamTLS, Port, ListenBacklog, ForkToHandleRequests> |
|---|
| 28 | { |
|---|
| 29 | public: |
|---|
| 30 | ServerTLS() |
|---|
| 31 | { |
|---|
| 32 | // Safe to call this here, as the Daemon class makes sure there is only one instance every of a Daemon. |
|---|
| 33 | SSLLib::Initialise(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | ~ServerTLS() |
|---|
| 37 | { |
|---|
| 38 | } |
|---|
| 39 | private: |
|---|
| 40 | ServerTLS(const ServerTLS &) |
|---|
| 41 | { |
|---|
| 42 | } |
|---|
| 43 | public: |
|---|
| 44 | |
|---|
| 45 | virtual void Run2(bool &rChildExit) |
|---|
| 46 | { |
|---|
| 47 | // First, set up the SSL context. |
|---|
| 48 | // Get parameters from the configuration |
|---|
| 49 | // this-> in next line required to build under some gcc versions |
|---|
| 50 | const Configuration &conf(this->GetConfiguration()); |
|---|
| 51 | const Configuration &serverconf(conf.GetSubConfiguration("Server")); |
|---|
| 52 | std::string certFile(serverconf.GetKeyValue("CertificateFile")); |
|---|
| 53 | std::string keyFile(serverconf.GetKeyValue("PrivateKeyFile")); |
|---|
| 54 | std::string caFile(serverconf.GetKeyValue("TrustedCAsFile")); |
|---|
| 55 | mContext.Initialise(true /* as server */, certFile.c_str(), keyFile.c_str(), caFile.c_str()); |
|---|
| 56 | |
|---|
| 57 | // Then do normal stream server stuff |
|---|
| 58 | ServerStream<SocketStreamTLS, Port, ListenBacklog, |
|---|
| 59 | ForkToHandleRequests>::Run2(rChildExit); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | virtual void HandleConnection(SocketStreamTLS &rStream) |
|---|
| 63 | { |
|---|
| 64 | rStream.Handshake(mContext, true /* is server */); |
|---|
| 65 | // this-> in next line required to build under some gcc versions |
|---|
| 66 | this->Connection(rStream); |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | private: |
|---|
| 70 | TLSContext mContext; |
|---|
| 71 | }; |
|---|
| 72 | |
|---|
| 73 | #define SERVERTLS_VERIFY_SERVER_KEYS(DEFAULT_ADDRESSES) \ |
|---|
| 74 | ConfigurationVerifyKey("CertificateFile", ConfigTest_Exists), \ |
|---|
| 75 | ConfigurationVerifyKey("PrivateKeyFile", ConfigTest_Exists), \ |
|---|
| 76 | ConfigurationVerifyKey("TrustedCAsFile", ConfigTest_Exists), \ |
|---|
| 77 | SERVERSTREAM_VERIFY_SERVER_KEYS(DEFAULT_ADDRESSES) |
|---|
| 78 | |
|---|
| 79 | #endif // SERVERTLS__H |
|---|
| 80 | |
|---|