Skip to content

Commit 1e495f1

Browse files
committed
WiFiClientSecure: add support for PSK (pre-shared key) ciphers
1 parent a15b7e9 commit 1e495f1

File tree

4 files changed

+68
-3
lines changed

4 files changed

+68
-3
lines changed

libraries/WiFiClientSecure/src/WiFiClientSecure.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ WiFiClientSecure::WiFiClientSecure()
3939
_CA_cert = NULL;
4040
_cert = NULL;
4141
_private_key = NULL;
42+
_pskIdent = NULL;
43+
_psKey = NULL;
4244
next = NULL;
4345
}
4446

@@ -59,6 +61,8 @@ WiFiClientSecure::WiFiClientSecure(int sock)
5961
_CA_cert = NULL;
6062
_cert = NULL;
6163
_private_key = NULL;
64+
_pskIdent = NULL;
65+
_psKey = NULL;
6266
next = NULL;
6367
}
6468

@@ -89,11 +93,15 @@ void WiFiClientSecure::stop()
8993

9094
int WiFiClientSecure::connect(IPAddress ip, uint16_t port)
9195
{
96+
if (_pskIdent && _psKey)
97+
return connect(ip, port, _pskIdent, _psKey);
9298
return connect(ip, port, _CA_cert, _cert, _private_key);
9399
}
94100

95101
int WiFiClientSecure::connect(const char *host, uint16_t port)
96102
{
103+
if (_pskIdent && _psKey)
104+
return connect(host, port, _pskIdent, _psKey);
97105
return connect(host, port, _CA_cert, _cert, _private_key);
98106
}
99107

@@ -104,7 +112,24 @@ int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *_CA_cert,
104112

105113
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *_CA_cert, const char *_cert, const char *_private_key)
106114
{
107-
int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key);
115+
int ret = start_ssl_client(sslclient, host, port, _CA_cert, _cert, _private_key, NULL, NULL);
116+
_lastError = ret;
117+
if (ret < 0) {
118+
log_e("start_ssl_client: %d", ret);
119+
stop();
120+
return 0;
121+
}
122+
_connected = true;
123+
return 1;
124+
}
125+
126+
int WiFiClientSecure::connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey) {
127+
return connect(ip.toString().c_str(), port,_pskIdent, _psKey);
128+
}
129+
130+
int WiFiClientSecure::connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey) {
131+
log_v("start_ssl_client with PSK");
132+
int ret = start_ssl_client(sslclient, host, port, NULL, NULL, NULL, _pskIdent, _psKey);
108133
_lastError = ret;
109134
if (ret < 0) {
110135
log_e("start_ssl_client: %d", ret);
@@ -223,6 +248,11 @@ void WiFiClientSecure::setPrivateKey (const char *private_key)
223248
_private_key = private_key;
224249
}
225250

251+
void WiFiClientSecure::setPreSharedKey(const char *pskIdent, const char *psKey) {
252+
_pskIdent = pskIdent;
253+
_psKey = psKey;
254+
}
255+
226256
bool WiFiClientSecure::verify(const char* fp, const char* domain_name)
227257
{
228258
if (!sslclient)

libraries/WiFiClientSecure/src/WiFiClientSecure.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class WiFiClientSecure : public WiFiClient
3535
const char *_CA_cert;
3636
const char *_cert;
3737
const char *_private_key;
38+
const char *_pskIdent; // identity for PSK cipher suites
39+
const char *_psKey; // key in hex for PSK cipher suites
3840

3941
public:
4042
WiFiClientSecure *next;
@@ -45,6 +47,8 @@ class WiFiClientSecure : public WiFiClient
4547
int connect(const char *host, uint16_t port);
4648
int connect(IPAddress ip, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
4749
int connect(const char *host, uint16_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
50+
int connect(IPAddress ip, uint16_t port, const char *pskIdent, const char *psKey);
51+
int connect(const char *host, uint16_t port, const char *pskIdent, const char *psKey);
4852
int peek();
4953
size_t write(uint8_t data);
5054
size_t write(const uint8_t *buf, size_t size);
@@ -55,6 +59,7 @@ class WiFiClientSecure : public WiFiClient
5559
void stop();
5660
uint8_t connected();
5761
int lastError(char *buf, const size_t size);
62+
void setPreSharedKey(const char *pskIdent, const char *psKey); // psKey in Hex
5863
void setCACert(const char *rootCA);
5964
void setCertificate(const char *client_ca);
6065
void setPrivateKey (const char *private_key);

libraries/WiFiClientSecure/src/ssl_client.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void ssl_init(sslclient_context *ssl_client)
4545
}
4646

4747

48-
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key)
48+
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey)
4949
{
5050
char buf[512];
5151
int ret, flags, timeout;
@@ -116,6 +116,36 @@ int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t p
116116
if (ret < 0) {
117117
return handle_error(ret);
118118
}
119+
} else if (pskIdent != NULL && psKey != NULL) {
120+
log_v("Setting up PSK");
121+
// convert PSK from hex to binary
122+
if ((strlen(psKey) & 1) != 0 || strlen(psKey) > 2*MBEDTLS_PSK_MAX_LEN) {
123+
log_e("pre-shared key not valid hex or too long");
124+
return -1;
125+
}
126+
unsigned char psk[MBEDTLS_PSK_MAX_LEN];
127+
size_t psk_len = strlen(psKey)/2;
128+
for (int j=0; j<strlen(psKey); j+= 2) {
129+
char c = psKey[j];
130+
if (c >= '0' && c <= '9') c -= '0';
131+
else if (c >= 'A' && c <= 'F') c -= 'A' - 10;
132+
else if (c >= 'a' && c <= 'f') c -= 'a' - 10;
133+
else return -1;
134+
psk[j/2] = c<<4;
135+
c = psKey[j+1];
136+
if (c >= '0' && c <= '9') c -= '0';
137+
else if (c >= 'A' && c <= 'F') c -= 'A' - 10;
138+
else if (c >= 'a' && c <= 'f') c -= 'a' - 10;
139+
else return -1;
140+
psk[j/2] |= c;
141+
}
142+
// set mbedtls config
143+
ret = mbedtls_ssl_conf_psk(&ssl_client->ssl_conf, psk, psk_len,
144+
(const unsigned char *)pskIdent, strlen(pskIdent));
145+
if (ret != 0) {
146+
log_e("mbedtls_ssl_conf_psk returned %d", ret);
147+
return handle_error(ret);
148+
}
119149
} else {
120150
mbedtls_ssl_conf_authmode(&ssl_client->ssl_conf, MBEDTLS_SSL_VERIFY_NONE);
121151
log_i("WARNING: Use certificates for a more secure communication!");

libraries/WiFiClientSecure/src/ssl_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct sslclient_context {
2929

3030

3131
void ssl_init(sslclient_context *ssl_client);
32-
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key);
32+
int start_ssl_client(sslclient_context *ssl_client, const char *host, uint32_t port, const char *rootCABuff, const char *cli_cert, const char *cli_key, const char *pskIdent, const char *psKey);
3333
void stop_ssl_socket(sslclient_context *ssl_client, const char *rootCABuff, const char *cli_cert, const char *cli_key);
3434
int data_to_read(sslclient_context *ssl_client);
3535
int send_ssl_data(sslclient_context *ssl_client, const uint8_t *data, uint16_t len);

0 commit comments

Comments
 (0)