michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_test__TLSServer_h michael@0: #define mozilla_test__TLSServer_h michael@0: michael@0: // This is a standalone server for testing SSL features of Gecko. michael@0: // The client is expected to connect and initiate an SSL handshake (with SNI michael@0: // to indicate which "server" to connect to). If all is good, the client then michael@0: // sends one encrypted byte and receives that same byte back. michael@0: // This server also has the ability to "call back" another process waiting on michael@0: // it. That is, when the server is all set up and ready to receive connections, michael@0: // it will connect to a specified port and issue a simple HTTP request. michael@0: michael@0: #include michael@0: #include "prio.h" michael@0: #include "ScopedNSSTypes.h" michael@0: #include "secerr.h" michael@0: #include "ssl.h" michael@0: michael@0: namespace mozilla { namespace test { michael@0: michael@0: enum DebugLevel michael@0: { michael@0: DEBUG_ERRORS = 1, michael@0: DEBUG_WARNINGS = 2, michael@0: DEBUG_VERBOSE = 3 michael@0: }; michael@0: michael@0: extern DebugLevel gDebugLevel; michael@0: michael@0: void PrintPRError(const char *aPrefix); michael@0: michael@0: // The default certificate is trusted for localhost and *.example.com michael@0: extern const char DEFAULT_CERT_NICKNAME[]; michael@0: michael@0: // Pass DEFAULT_CERT_NICKNAME as certName unless you need a specific michael@0: // certificate. michael@0: SECStatus michael@0: ConfigSecureServerWithNamedCert(PRFileDesc *fd, const char *certName, michael@0: /*optional*/ ScopedCERTCertificate *cert, michael@0: /*optional*/ SSLKEAType *kea); michael@0: michael@0: int michael@0: StartServer(const char *nssCertDBDir, SSLSNISocketConfig sniSocketConfig, michael@0: void *sniSocketConfigArg); michael@0: michael@0: template michael@0: inline const Host * michael@0: GetHostForSNI(const SECItem *aSrvNameArr, uint32_t aSrvNameArrSize, michael@0: const Host *hosts) michael@0: { michael@0: for (uint32_t i = 0; i < aSrvNameArrSize; i++) { michael@0: for (const Host *host = hosts; host->mHostName; ++host) { michael@0: SECItem hostName; michael@0: hostName.data = reinterpret_cast(const_cast(host->mHostName)); michael@0: hostName.len = strlen(host->mHostName); michael@0: if (SECITEM_ItemsAreEqual(&hostName, &aSrvNameArr[i])) { michael@0: if (gDebugLevel >= DEBUG_VERBOSE) { michael@0: fprintf(stderr, "found pre-defined host '%s'\n", host->mHostName); michael@0: } michael@0: return host; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (gDebugLevel >= DEBUG_VERBOSE) { michael@0: fprintf(stderr, "could not find host info from SNI\n"); michael@0: } michael@0: michael@0: PR_SetError(SEC_ERROR_INVALID_ARGS, 0); michael@0: return nullptr; michael@0: } michael@0: michael@0: } } // namespace mozilla::test michael@0: michael@0: #endif // mozilla_test__TLSServer_h