Skip to content

Commit c4b7d05

Browse files
authored
Add proxy support (#53)
Signed-off-by: Raul Metsma <[email protected]>
1 parent 82881c2 commit c4b7d05

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

cdoc/NetworkBackend.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,28 @@ setPeerCertificates(httplib::SSLClient& cli, libcdoc::NetworkBackend *network, c
197197
return libcdoc::OK;
198198
}
199199

200+
//
201+
// Set proxy parameters
202+
//
203+
static libcdoc::result_t
204+
setProxy(httplib::SSLClient& cli, libcdoc::NetworkBackend *network)
205+
{
206+
libcdoc::NetworkBackend::ProxyCredentials cred;
207+
switch (auto result = network->getProxyCredentials(cred)) {
208+
case libcdoc::NOT_IMPLEMENTED:
209+
return libcdoc::OK;
210+
case libcdoc::OK:
211+
if (!cred.host.empty()) {
212+
cli.set_proxy(cred.host, cred.port);
213+
}
214+
if (!cred.username.empty()) {
215+
cli.set_proxy_basic_auth(cred.username, cred.password);
216+
}
217+
return libcdoc::OK;
218+
default: return result;
219+
}
220+
}
221+
200222
//
201223
// Post request and fetch response
202224
//
@@ -262,6 +284,7 @@ libcdoc::NetworkBackend::sendKey (CapsuleInfo& dst, const std::string& url, cons
262284
httplib::SSLClient cli(host, port);
263285
result = setPeerCertificates(cli, this, buildURL(host, port));
264286
if (result != OK) return result;
287+
if (result = setProxy(cli, this); result != OK) return result;
265288

266289
std::string full = path + "/key-capsules";
267290
httplib::Response rsp;
@@ -322,6 +345,7 @@ libcdoc::NetworkBackend::sendShare(std::vector<uint8_t>& dst, const std::string&
322345
httplib::SSLClient cli(host, port);
323346
result = setPeerCertificates(cli, this, buildURL(host, port));
324347
if (result != OK) return result;
348+
if (result = setProxy(cli, this); result != OK) return result;
325349

326350
std::string full = path + "/key-shares";
327351
httplib::Response rsp;
@@ -360,6 +384,7 @@ libcdoc::NetworkBackend::fetchKey (std::vector<uint8_t>& dst, const std::string&
360384
httplib::SSLClient cli(host, port, d->x509.get(), d->pkey);
361385
result = setPeerCertificates(cli, this, buildURL(host, port));
362386
if (result != OK) return result;
387+
if (result = setProxy(cli, this); result != OK) return result;
363388

364389
std::string full = path + "/key-capsules/" + transaction_id;
365390
httplib::Headers hdrs;
@@ -395,6 +420,7 @@ libcdoc::NetworkBackend::fetchNonce(std::vector<uint8_t>& dst, const std::string
395420
httplib::SSLClient cli(host, port);
396421
result = setPeerCertificates(cli, this, buildURL(host, port));
397422
if (result != OK) return result;
423+
if (result = setProxy(cli, this); result != OK) return result;
398424

399425
std::string full = path + "/key-shares/" + share_id + "/nonce";
400426
httplib::Response rsp;
@@ -430,6 +456,7 @@ libcdoc::NetworkBackend::fetchShare(ShareInfo& share, const std::string& url, co
430456

431457
result = setPeerCertificates(cli, this, buildURL(host, port));
432458
if (result != OK) return result;
459+
if (result = setProxy(cli, this); result != OK) return result;
433460

434461
std::string full = path + "/key-shares/" + share_id;
435462
LOG_DBG("Share url: {}", full);
@@ -680,6 +707,7 @@ libcdoc::NetworkBackend::signSID(std::vector<uint8_t>& dst, std::vector<uint8_t>
680707
httplib::SSLClient cli(host, port);
681708
result = setPeerCertificates(cli, this, buildURL(host, port));
682709
if (result != OK) return result;
710+
if (result = setProxy(cli, this); result != OK) return result;
683711

684712
//
685713
// Let user choose certificate (if multiple)
@@ -801,6 +829,7 @@ libcdoc::NetworkBackend::signMID(std::vector<uint8_t>& dst, std::vector<uint8_t>
801829
httplib::SSLClient cli(host, port);
802830
result = setPeerCertificates(cli, this, buildURL(host, port));
803831
if (result != OK) return result;
832+
if (result = setProxy(cli, this); result != OK) return result;
804833

805834
//
806835
// Authenticate

cdoc/NetworkBackend.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,29 @@ struct CDOC_EXPORT NetworkBackend {
9696
std::string recipient;
9797
};
9898

99+
/**
100+
* @brief Proxy credentials used for network access
101+
*
102+
*/
103+
struct ProxyCredentials {
104+
/**
105+
* @brief Proxy host
106+
*/
107+
std::string host;
108+
/**
109+
* @brief Proxy port
110+
*/
111+
uint16_t port;
112+
/**
113+
* @brief Proxy username
114+
*/
115+
std::string username;
116+
/**
117+
* @brief Proxy password
118+
*/
119+
std::string password;
120+
};
121+
99122
NetworkBackend() = default;
100123
virtual ~NetworkBackend() noexcept = default;
101124
NetworkBackend(const NetworkBackend&) = delete;
@@ -191,6 +214,14 @@ struct CDOC_EXPORT NetworkBackend {
191214
return getPeerTLSCertificates(dst);
192215
}
193216

217+
/**
218+
* @brief Get proxy configuration currently set
219+
* @param credentials output for proxy credentials
220+
*/
221+
virtual result_t getProxyCredentials(ProxyCredentials& credentials) const {
222+
return NOT_IMPLEMENTED;
223+
}
224+
194225
/**
195226
* @brief sign TLS digest with client's private key
196227
* @param dst a destination container for signature

libcdoc.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
%apply long long { libcdoc::result_t }
9090
%apply long long { int64_t }
9191
%apply long long { uint64_t }
92+
%apply int { uint16_t }
9293
%apply int { int32_t }
9394
%apply int { unsigned int }
9495

0 commit comments

Comments
 (0)