1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 // This is a standalone server for testing SSL features of Gecko.
9 // The client is expected to connect and initiate an SSL handshake (with SNI
10 // to indicate which "server" to connect to). If all is good, the client then
11 // sends one encrypted byte and receives that same byte back.
12 // This server also has the ability to "call back" another process waiting on
13 // it. That is, when the server is all set up and ready to receive connections,
14 // it will connect to a specified port and issue a simple HTTP request.
18 #include "ScopedNSSTypes.h"
19 #include "mozilla/Casting.h"
26 MOZ_TYPE_SPECIFIC_UNIQUE_PTR_TEMPLATE(UniquePRDir
, PRDir
, PR_CloseDir
);
28 } // namespace mozilla
33 typedef SECStatus (*ServerConfigFunc
)(PRFileDesc
* fd
);
35 enum DebugLevel
{ DEBUG_ERRORS
= 1, DEBUG_WARNINGS
= 2, DEBUG_VERBOSE
= 3 };
37 extern DebugLevel gDebugLevel
;
39 void PrintPRError(const char* aPrefix
);
41 // The default certificate is trusted for localhost and *.example.com
42 extern const char DEFAULT_CERT_NICKNAME
[];
44 // ConfigSecureServerWithNamedCert sets up the hostname name provided. If the
45 // extraData parameter is presented, extraData->certChain will be automatically
46 // filled in using database information.
47 // Pass DEFAULT_CERT_NICKNAME as certName unless you need a specific
49 SECStatus
ConfigSecureServerWithNamedCert(
50 PRFileDesc
* fd
, const char* certName
,
51 /*optional*/ UniqueCERTCertificate
* cert
,
52 /*optional*/ SSLKEAType
* kea
,
53 /*optional*/ SSLExtraServerCertData
* extraData
);
55 SECStatus
InitializeNSS(const char* nssCertDBDir
);
57 // StartServer initializes NSS, sockets, the SNI callback, and a default
58 // certificate. configFunc (optional) is a pointer to an implementation-
59 // defined configuration function, which is called on the model socket
60 // prior to handling any connections.
61 int StartServer(int argc
, char* argv
[], SSLSNISocketConfig sniSocketConfig
,
62 void* sniSocketConfigArg
,
63 ServerConfigFunc configFunc
= nullptr);
65 template <typename Host
>
66 inline const Host
* GetHostForSNI(const SECItem
* aSrvNameArr
,
67 uint32_t aSrvNameArrSize
, const Host
* hosts
) {
68 for (uint32_t i
= 0; i
< aSrvNameArrSize
; i
++) {
69 for (const Host
* host
= hosts
; host
->mHostName
; ++host
) {
71 hostName
.data
= BitwiseCast
<unsigned char*, const char*>(host
->mHostName
);
72 hostName
.len
= strlen(host
->mHostName
);
73 if (SECITEM_ItemsAreEqual(&hostName
, &aSrvNameArr
[i
])) {
74 if (gDebugLevel
>= DEBUG_VERBOSE
) {
75 fprintf(stderr
, "found pre-defined host '%s'\n", host
->mHostName
);
82 if (gDebugLevel
>= DEBUG_VERBOSE
) {
83 fprintf(stderr
, "could not find host info from SNI\n");
86 PR_SetError(SEC_ERROR_INVALID_ARGS
, 0);
91 } // namespace mozilla