Skip to content

Extend HTTPClient to allow connecting with a client certificate #788

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions libraries/HTTPClient/src/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class TransportTraits
class TLSTraits : public TransportTraits
{
public:
TLSTraits(const char* CAcert) :
_cacert(CAcert)
TLSTraits(const char* CAcert, const char* clicert = nullptr, const char* clikey = nullptr) :
_cacert(CAcert), _clicert(clicert), _clikey(clikey)
{
}

Expand All @@ -67,12 +67,16 @@ class TLSTraits : public TransportTraits
bool verify(WiFiClient& client, const char* host) override
{
WiFiClientSecure& wcs = static_cast<WiFiClientSecure&>(client);
wcs.setCACert(_cacert);
wcs.setCACert(_cacert);
wcs.setCertificate(_clicert);
wcs.setPrivateKey(_clikey);
return true;
}

protected:
const char* _cacert;
const char* _clicert;
const char* _clikey;
};

/**
Expand Down Expand Up @@ -203,6 +207,20 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcer
return true;
}

bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcert, const char* cli_cert, const char* cli_key)
{
clear();
_host = host;
_port = port;
_uri = uri;

if (strlen(CAcert) == 0) {
return false;
}
_transportTraits = TransportTraitsPtr(new TLSTraits(CAcert, cli_cert, cli_key));
return true;
}

/**
* end
* called after the payload is handled
Expand Down
1 change: 1 addition & 0 deletions libraries/HTTPClient/src/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ class HTTPClient
bool begin(String url, const char* CAcert);
bool begin(String host, uint16_t port, String uri = "/");
bool begin(String host, uint16_t port, String uri, const char* CAcert);
bool begin(String host, uint16_t port, String uri, const char* CAcert, const char* cli_cert, const char* cli_key);

void end(void);

Expand Down