Skip to content

Commit 3d34274

Browse files
authored
Read and pass cacert buffer to lws_config instead of path (#2149)
- Avoids libwebsockets taking control over using raw fread/nvs APIs - The SDK, hence, do not need to rely on libwebsockets method
1 parent 16c40d4 commit 3d34274

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/source/Signaling/Signaling.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,37 @@
66
extern StateMachineState SIGNALING_STATE_MACHINE_STATES[];
77
extern UINT32 SIGNALING_STATE_MACHINE_STATE_COUNT;
88

9+
// Allocate memory and read the CA certificate from the path
10+
PRIVATE_API STATUS readCACertificate(PCHAR pCaCertPath, PBYTE* ppCaCertBuf, PUINT32 pCaCertBufLen)
11+
{
12+
ENTERS();
13+
STATUS retStatus = STATUS_SUCCESS;
14+
UINT64 cert_len = 0;
15+
PBYTE cert_buf = NULL;
16+
17+
CHK(pCaCertPath != NULL && ppCaCertBuf != NULL && pCaCertBufLen != NULL, STATUS_NULL_ARG);
18+
19+
*ppCaCertBuf = NULL;
20+
*pCaCertBufLen = 0;
21+
22+
CHK_STATUS(readFile(pCaCertPath, FALSE, NULL, &cert_len));
23+
CHK(cert_len > 0, STATUS_INVALID_CERT_PATH_LENGTH);
24+
cert_buf = (PBYTE) MEMCALLOC(1, cert_len + 1); // +1 for the null terminator
25+
CHK(cert_buf != NULL, STATUS_NOT_ENOUGH_MEMORY);
26+
CHK_STATUS(readFile(pCaCertPath, FALSE, cert_buf, &cert_len));
27+
28+
*ppCaCertBuf = cert_buf;
29+
*pCaCertBufLen = (UINT32) cert_len;
30+
cert_buf = NULL; // So that it is not freed by SAFE_MEMFREE
31+
32+
CleanUp:
33+
CHK_LOG_ERR(retStatus);
34+
SAFE_MEMFREE(cert_buf);
35+
36+
LEAVES();
37+
return retStatus;
38+
}
39+
940
STATUS createSignalingSync(PSignalingClientInfoInternal pClientInfo, PChannelInfo pChannelInfo, PSignalingClientCallbacks pCallbacks,
1041
PAwsCredentialProvider pCredentialProvider, PSignalingClient* ppSignalingClient)
1142
{
@@ -14,6 +45,8 @@ STATUS createSignalingSync(PSignalingClientInfoInternal pClientInfo, PChannelInf
1445
PSignalingClient pSignalingClient = NULL;
1546
PCHAR userLogLevelStr = NULL;
1647
UINT32 userLogLevel;
48+
PBYTE caCertBuf = NULL;
49+
UINT32 caCertBufLen = 0;
1750
struct lws_context_creation_info creationInfo;
1851
const lws_retry_bo_t retryPolicy = {
1952
.secs_since_valid_ping = SIGNALING_SERVICE_WSS_PING_PONG_INTERVAL_IN_SECONDS,
@@ -121,7 +154,9 @@ STATUS createSignalingSync(PSignalingClientInfoInternal pClientInfo, PChannelInf
121154
creationInfo.timeout_secs = SIGNALING_SERVICE_API_CALL_TIMEOUT_IN_SECONDS;
122155
creationInfo.gid = -1;
123156
creationInfo.uid = -1;
124-
creationInfo.client_ssl_ca_filepath = pChannelInfo->pCertPath;
157+
CHK_STATUS(readCACertificate(pChannelInfo->pCertPath, &caCertBuf, &caCertBufLen));
158+
creationInfo.client_ssl_ca_mem = caCertBuf;
159+
creationInfo.client_ssl_ca_mem_len = caCertBufLen;
125160
creationInfo.client_ssl_cipher_list = "HIGH:!PSK:!RSP:!eNULL:!aNULL:!RC4:!MD5:!DES:!3DES:!aDH:!kDH:!DSS";
126161
creationInfo.ka_time = SIGNALING_SERVICE_TCP_KEEPALIVE_IN_SECONDS;
127162
creationInfo.ka_probes = SIGNALING_SERVICE_TCP_KEEPALIVE_PROBE_COUNT;
@@ -204,6 +239,7 @@ STATUS createSignalingSync(PSignalingClientInfoInternal pClientInfo, PChannelInf
204239
SIGNALING_STATE_GET_TOKEN));
205240

206241
CleanUp:
242+
SAFE_MEMFREE(caCertBuf);
207243
if (pClientInfo != NULL && pSignalingClient != NULL) {
208244
pClientInfo->signalingClientInfo.stateMachineRetryCountReadOnly = pSignalingClient->diagnostics.stateMachineRetryCount;
209245
}

0 commit comments

Comments
 (0)