Skip to content

Commit fec3925

Browse files
committed
Added feature: loading DH params from the file
1 parent 5f0fc3f commit fec3925

File tree

8 files changed

+80
-13
lines changed

8 files changed

+80
-13
lines changed

samples/apps.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ server {
1414
# tls_certificate /media/projects/sites/servertest/cert/cert1.pem;
1515
# tls_certificate_key /media/projects/sites/servertest/cert/privkey1.pem;
1616

17+
# [Optional]
1718
# tls_certificate_chain /media/projects/sites/servertest/cert/chain1.pem;
1819
# tls_certificate_crl /media/projects/sites/servertest/cert/crl.pem;
1920

2021
# tls_stapling_file /media/projects/sites/servertest/cert/ocsp.der;
22+
# tls_dh_params_file /media/projects/sites/servertest/cert/dh.pem;
2123
}

src/ConfigParser.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ namespace HttpServer
137137
std::string chain_file;
138138
std::string crl_file;
139139
std::string stapling_file;
140+
std::string dh_file;
140141

141142
if (false == tls_ports.empty() )
142143
{
@@ -161,6 +162,13 @@ namespace HttpServer
161162
stapling_file = it_stapling_file->second;
162163
}
163164

165+
auto const it_dh_params_file = app.find("tls_dh_params_file");
166+
167+
if (app.cend() != it_dh_params_file)
168+
{
169+
dh_file = it_dh_params_file->second;
170+
}
171+
164172
auto const it_cert_file = app.find("tls_certificate");
165173

166174
if (app.cend() == it_cert_file)
@@ -283,7 +291,7 @@ namespace HttpServer
283291
std::string module_update = app.cend() != it_module_update ? it_module_update->second : "";
284292

285293
// Calculate module index
286-
size_t module_index = ~0;
294+
size_t module_index = std::numeric_limits<size_t>::max();
287295

288296
for (size_t i = 0; i < modules.size(); ++i)
289297
{
@@ -294,7 +302,7 @@ namespace HttpServer
294302
}
295303
}
296304

297-
if (module_index == ~0)
305+
if (std::numeric_limits<size_t>::max() == module_index)
298306
{
299307
module_index = modules.size();
300308
modules.emplace_back(std::move(module) );
@@ -333,6 +341,7 @@ namespace HttpServer
333341
std::move(chain_file),
334342
std::move(crl_file),
335343
std::move(stapling_file),
344+
std::move(dh_file),
336345

337346
std::move(app_call),
338347
std::move(app_clear),

src/Server.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,9 +1508,49 @@ namespace HttpServer
15081508

15091509
::gnutls_dh_params_init(&dh_params);
15101510

1511-
const unsigned int bits = ::gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_LEGACY);
1511+
if (app.dh_file.empty() )
1512+
{
1513+
const unsigned int bits = ::gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_HIGH);
1514+
1515+
ret = ::gnutls_dh_params_generate2(dh_params, bits);
1516+
}
1517+
else
1518+
{
1519+
std::ifstream dh_file(app.dh_file);
1520+
1521+
if (dh_file)
1522+
{
1523+
const size_t max_file_size = 1024 * 1024;
15121524

1513-
::gnutls_dh_params_generate2(dh_params, bits);
1525+
std::vector<char> buf(max_file_size);
1526+
1527+
dh_file.read(buf.data(), buf.size() );
1528+
1529+
gnutls_datum_t datum {
1530+
reinterpret_cast<unsigned char *>(buf.data() ),
1531+
static_cast<unsigned int>(dh_file.gcount() )
1532+
};
1533+
1534+
ret = ::gnutls_dh_params_import_pkcs3(dh_params, &datum, GNUTLS_X509_FMT_PEM);
1535+
}
1536+
else
1537+
{
1538+
ret = -1;
1539+
1540+
std::cout << "Error: DH params file has not been opened;" << std::endl;;
1541+
}
1542+
1543+
dh_file.close();
1544+
}
1545+
1546+
if (ret < 0)
1547+
{
1548+
::gnutls_certificate_free_credentials(x509_cred);
1549+
1550+
std::cout << "Error: failed tls DH params get;" << std::endl;
1551+
1552+
return false;
1553+
}
15141554

15151555
::gnutls_certificate_set_dh_params(x509_cred, dh_params);
15161556

src/ServerApplicationSettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace HttpServer
2828
std::string chain_file;
2929
std::string crl_file;
3030
std::string stapling_file;
31+
std::string dh_file;
3132

3233
std::function<int(server_request *, server_response *)> application_call;
3334
std::function<void(Utils::raw_pair [], const size_t)> application_clear;

src/SignalHandlers.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
#include <thread>
88

99
static std::thread threadMessageLoop;
10-
extern ::TCHAR myWndClassName[];
1110
#endif
1211

1312
#include <csignal>
1413

1514
static HttpServer::Server *globalServerPtr = nullptr;
1615

16+
/**
17+
* Terminate signal
18+
*/
1719
static void handlerSigTerm(const int sig)
1820
{
1921
if (globalServerPtr)
@@ -22,6 +24,9 @@ static void handlerSigTerm(const int sig)
2224
}
2325
}
2426

27+
/**
28+
* Interrupt signal
29+
*/
2530
static void handlerSigInt(const int sig)
2631
{
2732
if (globalServerPtr)
@@ -30,6 +35,9 @@ static void handlerSigInt(const int sig)
3035
}
3136
}
3237

38+
/**
39+
* Signal to restart
40+
*/
3341
static void handlerSigUsr1(const int sig)
3442
{
3543
if (globalServerPtr)
@@ -39,6 +47,9 @@ static void handlerSigUsr1(const int sig)
3947
}
4048
}
4149

50+
/**
51+
* Signal to update modules
52+
*/
4253
static void handlerSigUsr2(const int sig)
4354
{
4455
if (globalServerPtr)
@@ -60,6 +71,7 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const
6071
switch (message)
6172
{
6273
case SIGTERM:
74+
case WM_CLOSE:
6375
{
6476
handlerSigTerm(message);
6577
::PostMessage(hWnd, WM_QUIT, 0, 0); // Fuck ::PostQuitMessage(0);
@@ -96,11 +108,11 @@ static ::LRESULT CALLBACK WndProc(const ::HWND hWnd, const ::UINT message, const
96108
return 0;
97109
}
98110

99-
static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event *pCreatedWindow)
111+
static ::WPARAM mainMessageLoop(const ::HINSTANCE hInstance, HttpServer::Event * const eventWindowCreation)
100112
{
101113
const ::HWND hWnd = ::CreateWindow(myWndClassName, nullptr, 0, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, nullptr, nullptr, hInstance, nullptr);
102114

103-
pCreatedWindow->notify();
115+
eventWindowCreation->notify(); // After this action, eventWindowCreation will be destroyed (in the other thread)
104116

105117
if (0 == hWnd)
106118
{
@@ -166,11 +178,11 @@ bool bindSignalHandlers(HttpServer::Server *server)
166178
return false;
167179
}
168180

169-
HttpServer::Event createdWindow;
181+
HttpServer::Event eventWindowCreation;
170182

171-
threadMessageLoop = std::thread(mainMessageLoop, hInstance, &createdWindow);
183+
threadMessageLoop = std::thread(mainMessageLoop, hInstance, &eventWindowCreation);
172184

173-
createdWindow.wait();
185+
eventWindowCreation.wait();
174186

175187
#elif POSIX
176188

src/SocketAdapterTls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace HttpServer
5555

5656
while (total < length)
5757
{
58-
::gnutls_record_set_timeout(this->session, timeout.count() );
58+
::gnutls_record_set_timeout(this->session, static_cast<unsigned int>(timeout.count() ) );
5959

6060
if (record_size > length - total)
6161
{
@@ -92,7 +92,7 @@ namespace HttpServer
9292

9393
long SocketAdapterTls::nonblock_recv(std::vector<std::string::value_type> &buf, const std::chrono::milliseconds &timeout) const
9494
{
95-
::gnutls_record_set_timeout(this->session, timeout.count() );
95+
::gnutls_record_set_timeout(this->session, static_cast<unsigned int>(timeout.count() ) );
9696
return ::gnutls_record_recv(this->session, buf.data(), buf.size() );
9797
}
9898

src/System.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
#ifdef WIN32
44
#include <WS2tcpip.h>
55
#include <Windows.h>
6+
#undef min
7+
#undef max
8+
69
::TCHAR myWndClassName[];
710

811
#ifdef SIGTERM

src/Utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ namespace Utils
280280

281281
// Parse RFC 822
282282
#ifdef WIN32
283-
if (~0 != ::sscanf_s(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon.data(), s_mon.size(), &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
283+
if (~0 != ::sscanf_s(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon.data(), static_cast<unsigned int>(s_mon.size() ), &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
284284
#else
285285
if (~0 != ::sscanf(strTime.c_str(), "%*s %d %3s %d %d:%d:%d", &tc.tm_mday, s_mon.data(), &tc.tm_year, &tc.tm_hour, &tc.tm_min, &tc.tm_sec) )
286286
#endif

0 commit comments

Comments
 (0)