Skip to content

Commit ad4a73b

Browse files
committed
Wt HTTP client: use Windows CA certificates
1 parent dbeaed4 commit ad4a73b

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

src/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,9 @@ ENDIF(ENABLE_LIBWTTEST)
547547
IF(HAVE_SSL)
548548
TARGET_LINK_LIBRARIES(wt PRIVATE ${SSL_LIBRARIES})
549549
INCLUDE_DIRECTORIES(${SSL_INCLUDE_DIRS})
550+
IF(WIN32)
551+
TARGET_LINK_LIBRARIES(wt PRIVATE Crypt32.lib)
552+
ENDIF(WIN32)
550553
ELSE(HAVE_SSL)
551554
MESSAGE("** Disabling crypto support (Auth::SHA1HashFunction, HTTPS support): requires OpenSSL.")
552555
IF(ENABLE_SSL)

src/Wt/Http/Client.C

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626

2727
#define VERIFY_CERTIFICATE
2828

29+
#ifdef WT_WIN32
30+
#include "web/SslUtils.h"
31+
#endif // WT_WIN32
32+
2933
#endif // WT_WITH_SSL
3034

3135
#ifdef WT_WIN32
@@ -972,10 +976,13 @@ bool Client::request(Http::Method method, const std::string& url,
972976

973977
context.set_options(sslOptions);
974978

975-
976979
#ifdef VERIFY_CERTIFICATE
977-
if (verifyEnabled_)
980+
if (verifyEnabled_) {
978981
context.set_default_verify_paths();
982+
#ifdef WT_WIN32
983+
Ssl::addWindowsCACertificates(context);
984+
#endif // WT_WIN32
985+
}
979986

980987
if (!verifyFile_.empty() || !verifyPath_.empty()) {
981988
if (!verifyFile_.empty())

src/web/SslUtils.C

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,22 @@
33
*
44
* See the LICENSE file for terms of use.
55
*/
6+
#include <Wt/WConfig.h>
7+
8+
#ifdef WT_WITH_SSL
9+
#ifdef WT_WIN32
10+
#include <Wt/AsioWrapper/ssl.hpp>
11+
#endif // WT_WIN32
12+
#endif // WT_WITH_SSL
613

714
#include "SslUtils.h"
815

916
#ifdef WT_WITH_SSL
1017
#include <openssl/ssl.h>
18+
19+
#ifdef WT_WIN32
20+
#include <wincrypt.h>
21+
#endif // WT_WIN32
1122
#endif //WT_WITH_SSL
1223

1324
#ifdef WT_WITH_SSL
@@ -166,6 +177,32 @@ namespace Wt {
166177

167178
return certificate;
168179
}
180+
181+
#ifdef WT_WIN32
182+
void addWindowsCACertificates(asio::ssl::context &ctx) {
183+
HCERTSTORE hStore = CertOpenSystemStore(0, "ROOT");
184+
if (hStore == NULL) {
185+
return;
186+
}
187+
188+
X509_STORE *store = X509_STORE_new();
189+
PCCERT_CONTEXT pContext = NULL;
190+
while ((pContext = CertEnumCertificatesInStore(hStore, pContext)) != NULL) {
191+
X509 *x509 = d2i_X509(NULL,
192+
(const unsigned char **)&pContext->pbCertEncoded,
193+
pContext->cbCertEncoded);
194+
if (x509 != NULL) {
195+
X509_STORE_add_cert(store, x509);
196+
X509_free(x509);
197+
}
198+
}
199+
200+
CertFreeCertificateContext(pContext);
201+
CertCloseStore(hStore, 0);
202+
203+
SSL_CTX_set_cert_store(ctx.native_handle(), store);
204+
}
205+
#endif // WT_WIN32
169206
}
170207
}
171208
#endif //WT_WITH_SSL

src/web/SslUtils.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#ifndef SSLUTILS_H_
88
#define SSLUTILS_H_
99

10+
#include <Wt/WConfig.h>
11+
1012
#include <string>
1113
#include <vector>
1214

@@ -16,6 +18,25 @@
1618
#ifdef WT_WITH_SSL
1719
#include <openssl/ssl.h>
1820

21+
#ifdef WT_WIN32
22+
#ifdef WT_ASIO_IS_BOOST_ASIO
23+
namespace boost {
24+
namespace asio {
25+
namespace ssl {
26+
class context;
27+
}
28+
}
29+
}
30+
namespace asio = boost::asio;
31+
#else // WT_ASIO_IS_STANDALONE_ASIO
32+
namespace asio {
33+
namespace ssl {
34+
class context;
35+
}
36+
}
37+
#endif // WT_ASIO_IS_STANDALONE_ASIO
38+
#endif // WT_WIN32
39+
1940
namespace Wt {
2041
namespace Ssl {
2142
std::vector<Wt::WSslCertificate::DnAttribute>
@@ -28,6 +49,10 @@ namespace Wt {
2849
std::string exportToPem(struct x509_st *x509);
2950

3051
struct x509_st *readFromPem(const std::string &pem);
52+
53+
#ifdef WT_WIN32
54+
extern void addWindowsCACertificates(asio::ssl::context &ctx);
55+
#endif // WT_WIN32
3156
}
3257
}
3358
#endif //WT_WITH_SSL

0 commit comments

Comments
 (0)