Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef mozilla_test__TLSServer_h |
michael@0 | 6 | #define mozilla_test__TLSServer_h |
michael@0 | 7 | |
michael@0 | 8 | // This is a standalone server for testing SSL features of Gecko. |
michael@0 | 9 | // The client is expected to connect and initiate an SSL handshake (with SNI |
michael@0 | 10 | // to indicate which "server" to connect to). If all is good, the client then |
michael@0 | 11 | // sends one encrypted byte and receives that same byte back. |
michael@0 | 12 | // This server also has the ability to "call back" another process waiting on |
michael@0 | 13 | // it. That is, when the server is all set up and ready to receive connections, |
michael@0 | 14 | // it will connect to a specified port and issue a simple HTTP request. |
michael@0 | 15 | |
michael@0 | 16 | #include <stdint.h> |
michael@0 | 17 | #include "prio.h" |
michael@0 | 18 | #include "ScopedNSSTypes.h" |
michael@0 | 19 | #include "secerr.h" |
michael@0 | 20 | #include "ssl.h" |
michael@0 | 21 | |
michael@0 | 22 | namespace mozilla { namespace test { |
michael@0 | 23 | |
michael@0 | 24 | enum DebugLevel |
michael@0 | 25 | { |
michael@0 | 26 | DEBUG_ERRORS = 1, |
michael@0 | 27 | DEBUG_WARNINGS = 2, |
michael@0 | 28 | DEBUG_VERBOSE = 3 |
michael@0 | 29 | }; |
michael@0 | 30 | |
michael@0 | 31 | extern DebugLevel gDebugLevel; |
michael@0 | 32 | |
michael@0 | 33 | void PrintPRError(const char *aPrefix); |
michael@0 | 34 | |
michael@0 | 35 | // The default certificate is trusted for localhost and *.example.com |
michael@0 | 36 | extern const char DEFAULT_CERT_NICKNAME[]; |
michael@0 | 37 | |
michael@0 | 38 | // Pass DEFAULT_CERT_NICKNAME as certName unless you need a specific |
michael@0 | 39 | // certificate. |
michael@0 | 40 | SECStatus |
michael@0 | 41 | ConfigSecureServerWithNamedCert(PRFileDesc *fd, const char *certName, |
michael@0 | 42 | /*optional*/ ScopedCERTCertificate *cert, |
michael@0 | 43 | /*optional*/ SSLKEAType *kea); |
michael@0 | 44 | |
michael@0 | 45 | int |
michael@0 | 46 | StartServer(const char *nssCertDBDir, SSLSNISocketConfig sniSocketConfig, |
michael@0 | 47 | void *sniSocketConfigArg); |
michael@0 | 48 | |
michael@0 | 49 | template <typename Host> |
michael@0 | 50 | inline const Host * |
michael@0 | 51 | GetHostForSNI(const SECItem *aSrvNameArr, uint32_t aSrvNameArrSize, |
michael@0 | 52 | const Host *hosts) |
michael@0 | 53 | { |
michael@0 | 54 | for (uint32_t i = 0; i < aSrvNameArrSize; i++) { |
michael@0 | 55 | for (const Host *host = hosts; host->mHostName; ++host) { |
michael@0 | 56 | SECItem hostName; |
michael@0 | 57 | hostName.data = reinterpret_cast<uint8_t*>(const_cast<char*>(host->mHostName)); |
michael@0 | 58 | hostName.len = strlen(host->mHostName); |
michael@0 | 59 | if (SECITEM_ItemsAreEqual(&hostName, &aSrvNameArr[i])) { |
michael@0 | 60 | if (gDebugLevel >= DEBUG_VERBOSE) { |
michael@0 | 61 | fprintf(stderr, "found pre-defined host '%s'\n", host->mHostName); |
michael@0 | 62 | } |
michael@0 | 63 | return host; |
michael@0 | 64 | } |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | if (gDebugLevel >= DEBUG_VERBOSE) { |
michael@0 | 69 | fprintf(stderr, "could not find host info from SNI\n"); |
michael@0 | 70 | } |
michael@0 | 71 | |
michael@0 | 72 | PR_SetError(SEC_ERROR_INVALID_ARGS, 0); |
michael@0 | 73 | return nullptr; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | } } // namespace mozilla::test |
michael@0 | 77 | |
michael@0 | 78 | #endif // mozilla_test__TLSServer_h |