security/manager/ssl/tests/unit/tlsserver/lib/TLSServer.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/manager/ssl/tests/unit/tlsserver/lib/TLSServer.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#ifndef mozilla_test__TLSServer_h
     1.9 +#define mozilla_test__TLSServer_h
    1.10 +
    1.11 +// This is a standalone server for testing SSL features of Gecko.
    1.12 +// The client is expected to connect and initiate an SSL handshake (with SNI
    1.13 +// to indicate which "server" to connect to). If all is good, the client then
    1.14 +// sends one encrypted byte and receives that same byte back.
    1.15 +// This server also has the ability to "call back" another process waiting on
    1.16 +// it. That is, when the server is all set up and ready to receive connections,
    1.17 +// it will connect to a specified port and issue a simple HTTP request.
    1.18 +
    1.19 +#include <stdint.h>
    1.20 +#include "prio.h"
    1.21 +#include "ScopedNSSTypes.h"
    1.22 +#include "secerr.h"
    1.23 +#include "ssl.h"
    1.24 +
    1.25 +namespace mozilla { namespace test {
    1.26 +
    1.27 +enum DebugLevel
    1.28 +{
    1.29 +  DEBUG_ERRORS = 1,
    1.30 +  DEBUG_WARNINGS  = 2,
    1.31 +  DEBUG_VERBOSE = 3
    1.32 +};
    1.33 +
    1.34 +extern DebugLevel gDebugLevel;
    1.35 +
    1.36 +void PrintPRError(const char *aPrefix);
    1.37 +
    1.38 +// The default certificate is trusted for localhost and *.example.com
    1.39 +extern const char DEFAULT_CERT_NICKNAME[];
    1.40 +
    1.41 +// Pass DEFAULT_CERT_NICKNAME as certName unless you need a specific
    1.42 +// certificate.
    1.43 +SECStatus
    1.44 +ConfigSecureServerWithNamedCert(PRFileDesc *fd, const char *certName,
    1.45 +                                /*optional*/ ScopedCERTCertificate *cert,
    1.46 +                                /*optional*/ SSLKEAType *kea);
    1.47 +
    1.48 +int
    1.49 +StartServer(const char *nssCertDBDir, SSLSNISocketConfig sniSocketConfig,
    1.50 +            void *sniSocketConfigArg);
    1.51 +
    1.52 +template <typename Host>
    1.53 +inline const Host *
    1.54 +GetHostForSNI(const SECItem *aSrvNameArr, uint32_t aSrvNameArrSize,
    1.55 +              const Host *hosts)
    1.56 +{
    1.57 +  for (uint32_t i = 0; i < aSrvNameArrSize; i++) {
    1.58 +    for (const Host *host = hosts; host->mHostName; ++host) {
    1.59 +      SECItem hostName;
    1.60 +      hostName.data = reinterpret_cast<uint8_t*>(const_cast<char*>(host->mHostName));
    1.61 +      hostName.len = strlen(host->mHostName);
    1.62 +      if (SECITEM_ItemsAreEqual(&hostName, &aSrvNameArr[i])) {
    1.63 +        if (gDebugLevel >= DEBUG_VERBOSE) {
    1.64 +          fprintf(stderr, "found pre-defined host '%s'\n", host->mHostName);
    1.65 +        }
    1.66 +        return host;
    1.67 +      }
    1.68 +    }
    1.69 +  }
    1.70 +
    1.71 +  if (gDebugLevel >= DEBUG_VERBOSE) {
    1.72 +    fprintf(stderr, "could not find host info from SNI\n");
    1.73 +  }
    1.74 +
    1.75 +  PR_SetError(SEC_ERROR_INVALID_ARGS, 0);
    1.76 +  return nullptr;
    1.77 +}
    1.78 +
    1.79 +} } // namespace mozilla::test
    1.80 +
    1.81 +#endif // mozilla_test__TLSServer_h

mercurial