Skip to content

Commit

Permalink
Merge pull request #987 from pennam/sni-fix
Browse files Browse the repository at this point in the history
Fix and simplify sni setting
  • Loading branch information
pennam authored Nov 5, 2024
2 parents 26ff735 + 4081c6b commit 11c3c83
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 14 deletions.
4 changes: 2 additions & 2 deletions libraries/SocketWrapper/src/AClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ int arduino::AClient::connectSSL(IPAddress ip, uint16_t port) {
return client->connectSSL(ip, port);
}

int arduino::AClient::connectSSL(const char *host, uint16_t port, bool disableSNI) {
int arduino::AClient::connectSSL(const char *host, uint16_t port) {
if (!client) {
newMbedClient();
}
return client->connectSSL(host, port, disableSNI);
return client->connectSSL(host, port);
}

void arduino::AClient::stop() {
Expand Down
2 changes: 1 addition & 1 deletion libraries/SocketWrapper/src/AClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class AClient : public Client {
virtual int connect(IPAddress ip, uint16_t port);
virtual int connect(const char *host, uint16_t port);
int connectSSL(IPAddress ip, uint16_t port);
int connectSSL(const char* host, uint16_t port, bool disableSNI = false);
int connectSSL(const char* host, uint16_t port);
virtual void stop();

virtual explicit operator bool();
Expand Down
10 changes: 1 addition & 9 deletions libraries/SocketWrapper/src/MbedClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,7 @@ int arduino::MbedClient::connectSSL(IPAddress ip, uint16_t port) {
return connectSSL(SocketHelpers::socketAddressFromIpAddress(ip, port));
}

int arduino::MbedClient::connectSSL(const char *host, uint16_t port, bool disableSNI) {
if (!disableSNI) {
if (sock == nullptr) {
sock = new TLSSocket();
_own_socket = true;
}
static_cast<TLSSocket *>(sock)->set_hostname(host);
}

int arduino::MbedClient::connectSSL(const char *host, uint16_t port) {
SocketAddress socketAddress = SocketAddress();
socketAddress.set_port(port);
SocketHelpers::gethostbyname(getNetwork(), host, &socketAddress);
Expand Down
2 changes: 1 addition & 1 deletion libraries/SocketWrapper/src/MbedClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class MbedClient {
virtual int connect(const char* host, uint16_t port);
int connectSSL(SocketAddress socketAddress);
int connectSSL(IPAddress ip, uint16_t port);
int connectSSL(const char* host, uint16_t port, bool disableSNI = false);
int connectSSL(const char* host, uint16_t port);
size_t write(uint8_t);
size_t write(const uint8_t* buf, size_t size);
int available();
Expand Down
8 changes: 7 additions & 1 deletion libraries/SocketWrapper/src/MbedSSLClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class MbedSSLClient : public arduino::MbedClient {
return connectSSL(ip, port);
}
int connect(const char* host, uint16_t port) {
return connectSSL(host, port, _disableSNI);
_hostname = host;
return connectSSL(host, port);
}
void disableSNI(bool statusSNI) {
_disableSNI = statusSNI;
Expand All @@ -53,6 +54,7 @@ class MbedSSLClient : public arduino::MbedClient {

protected:
const char* _ca_cert_custom = NULL;
const char* _hostname = NULL;

private:
int setRootCA() {
Expand All @@ -79,6 +81,10 @@ class MbedSSLClient : public arduino::MbedClient {
}
#endif

if(_hostname && !_disableSNI) {
((TLSSocket*)sock)->set_hostname(_hostname);
}

if(_ca_cert_custom != NULL) {
err = ((TLSSocket*)sock)->append_root_ca_cert(_ca_cert_custom);
}
Expand Down

0 comments on commit 11c3c83

Please sign in to comment.