Skip to content
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

feat: support load ssl cert from plain text #1328

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 20 additions & 2 deletions src/MQTTAsync.c
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
}
if (options->struct_version != 0 && options->ssl) /* check validity of SSL options structure */
{
if (strncmp(options->ssl->struct_id, "MQTS", 4) != 0 || options->ssl->struct_version < 0 || options->ssl->struct_version > 5)
if (strncmp(options->ssl->struct_id, "MQTS", 4) != 0 || options->ssl->struct_version < 0 || options->ssl->struct_version > 6)
{
rc = MQTTASYNC_BAD_STRUCTURE;
goto exit;
Expand Down Expand Up @@ -750,14 +750,23 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
if (m->c->sslopts->privateKey)
free((void*)m->c->sslopts->privateKey);
if (m->c->sslopts->privateKeyPassword)
free((void*)m->c->sslopts->privateKeyPassword);
free((void*)m->c->sslopts->privateKeyPassword);
if (m->c->sslopts->enabledCipherSuites)
free((void*)m->c->sslopts->enabledCipherSuites);
if (m->c->sslopts->struct_version >= 2)
{
if (m->c->sslopts->CApath)
free((void*)m->c->sslopts->CApath);
}
if (m->c->sslopts->struct_version >= 6)
{
if (m->c->sslopts->pemRootCerts)
free((void*)m->c->sslopts->pemRootCerts);
if (m->c->sslopts->pemCertChain)
free((void*)m->c->sslopts->pemCertChain);
if (m->c->sslopts->pemPrivateKey)
free((void*)m->c->sslopts->pemPrivateKey);
}
free((void*)m->c->sslopts);
m->c->sslopts = NULL;
}
Expand Down Expand Up @@ -807,6 +816,15 @@ int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions* options)
m->c->sslopts->protos = (const unsigned char*)MQTTStrdup((const char*)options->ssl->protos);
m->c->sslopts->protos_len = options->ssl->protos_len;
}
if (m->c->sslopts->struct_version >= 6)
{
if (options->ssl->pemRootCerts)
m->c->sslopts->pemRootCerts = MQTTStrdup(options->ssl->pemRootCerts);
if (options->ssl->pemCertChain)
m->c->sslopts->pemCertChain = MQTTStrdup(options->ssl->pemCertChain);
if (options->ssl->pemPrivateKey)
m->c->sslopts->pemPrivateKey = MQTTStrdup(options->ssl->pemPrivateKey);
}
}
#else
if (options->struct_version != 0 && options->ssl)
Expand Down
19 changes: 17 additions & 2 deletions src/MQTTAsync.h
Original file line number Diff line number Diff line change
Expand Up @@ -1125,7 +1125,7 @@ typedef struct
* From the OpenSSL documentation:
* If CApath is not NULL, it points to a directory containing CA certificates in PEM format.
* Exists only if struct_version >= 2
*/
*/
const char* CApath;

/**
Expand Down Expand Up @@ -1174,9 +1174,24 @@ typedef struct
* Exists only if struct_version >= 5
*/
unsigned int protos_len;

/**
* document
*/
const char* pemRootCerts;

/**
* document
*/
const char* pemCertChain;

/**
* document
*/
const char* pemPrivateKey;
} MQTTAsync_SSLOptions;

#define MQTTAsync_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0 }
#define MQTTAsync_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 6, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL }

/** Utility structure where name/value pairs are needed */
typedef struct
Expand Down
20 changes: 19 additions & 1 deletion src/MQTTClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -1601,6 +1601,15 @@ static MQTTResponse MQTTClient_connectURI(MQTTClient handle, MQTTClient_connectO
if (m->c->sslopts->CApath)
free((void*)m->c->sslopts->CApath);
}
if (m->c->sslopts->struct_version >= 6)
{
if (m->c->sslopts->pemRootCerts)
free((void*)m->c->sslopts->pemRootCerts);
if (m->c->sslopts->pemCertChain)
free((void*)m->c->sslopts->pemCertChain);
if (m->c->sslopts->pemPrivateKey)
free((void*)m->c->sslopts->pemPrivateKey);
}
free(m->c->sslopts);
m->c->sslopts = NULL;
}
Expand Down Expand Up @@ -1649,6 +1658,15 @@ static MQTTResponse MQTTClient_connectURI(MQTTClient handle, MQTTClient_connectO
m->c->sslopts->protos = options->ssl->protos;
m->c->sslopts->protos_len = options->ssl->protos_len;
}
if (m->c->sslopts->struct_version >= 6)
{
if (options->ssl->pemRootCerts)
m->c->sslopts->pemRootCerts = MQTTStrdup(options->ssl->pemRootCerts);
if (options->ssl->pemCertChain)
m->c->sslopts->pemCertChain = MQTTStrdup(options->ssl->pemCertChain);
if (options->ssl->pemPrivateKey)
m->c->sslopts->pemPrivateKey = MQTTStrdup(options->ssl->pemPrivateKey);
}
}
#endif

Expand Down Expand Up @@ -1801,7 +1819,7 @@ MQTTResponse MQTTClient_connectAll(MQTTClient handle, MQTTClient_connectOptions*
#if defined(OPENSSL)
if (options->struct_version != 0 && options->ssl) /* check validity of SSL options structure */
{
if (strncmp(options->ssl->struct_id, "MQTS", 4) != 0 || options->ssl->struct_version < 0 || options->ssl->struct_version > 5)
if (strncmp(options->ssl->struct_id, "MQTS", 4) != 0 || options->ssl->struct_version < 0 || options->ssl->struct_version > 6)
{
rc.reasonCode = MQTTCLIENT_BAD_STRUCTURE;
goto exit;
Expand Down
17 changes: 16 additions & 1 deletion src/MQTTClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,24 @@ typedef struct
* Exists only if struct_version >= 5
*/
unsigned int protos_len;

/**
* document
*/
const char* pemRootCerts;

/**
* document
*/
const char* pemCertChain;

/**
* document
*/
const char* pemPrivateKey;
} MQTTClient_SSLOptions;

#define MQTTClient_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0 }
#define MQTTClient_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 6, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0, NULL, NULL, NULL }

/**
* MQTTClient_libraryInfo is used to store details relating to the currently used
Expand Down
41 changes: 18 additions & 23 deletions src/MQTTPacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ int MQTTPacket_decode(networkHandles* net, size_t* value)
*/
int readInt(char** pptr)
{
char* ptr = *pptr;
int len = 256*((unsigned char)(*ptr)) + (unsigned char)(*(ptr+1));
char *ptr = *pptr;
int val = ((((uint32_t)ptr[0]) << 8) | ((uint32_t)ptr[1]));
*pptr += 2;
return len;
return val;
}


Expand Down Expand Up @@ -452,10 +452,10 @@ void writeChar(char** pptr, char c)
*/
void writeInt(char** pptr, int anInt)
{
**pptr = (char)(anInt / 256);
(*pptr)++;
**pptr = (char)(anInt % 256);
(*pptr)++;
char* ptr = *pptr;
ptr[0] = (uint8_t) ((anInt >> 8) & 0xFF);
ptr[1] = (uint8_t) (anInt & 0xFF);
*pptr += 2;
}


Expand Down Expand Up @@ -944,33 +944,28 @@ void MQTTPacket_free_packet(MQTTPacket* pack)
*/
void writeInt4(char** pptr, int anInt)
{
**pptr = (char)(anInt / 16777216);
(*pptr)++;
anInt %= 16777216;
**pptr = (char)(anInt / 65536);
(*pptr)++;
anInt %= 65536;
**pptr = (char)(anInt / 256);
(*pptr)++;
**pptr = (char)(anInt % 256);
(*pptr)++;
unsigned char* ptr = (unsigned char*)*pptr;
ptr[0] = (uint8_t) ((anInt >> 24) & 0xFF);
ptr[1] = (uint8_t) ((anInt >> 16) & 0xFF);
ptr[2] = (uint8_t) ((anInt >> 8) & 0xFF);
ptr[3] = (uint8_t) (anInt & 0xFF);
*pptr += 4;
}


/**
* Calculates an integer from two bytes read from the input buffer
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
* @param pptr pointer to the input buffer - incremented by the number of bytes
* used & returned
* @return the integer value calculated
*/
int readInt4(char** pptr)
{
unsigned char* ptr = (unsigned char*)*pptr;
int value = 16777216*(*ptr) + 65536*(*(ptr+1)) + 256*(*(ptr+2)) + (*(ptr+3));
unsigned char *ptr = (unsigned char *)*pptr;
int val = ((((uint32_t)ptr[0]) << 24) | (((uint32_t)ptr[1]) << 16) | (((uint32_t)ptr[2]) << 8) | ((uint32_t)ptr[3]));
*pptr += 4;
return value;
return val;
}


void writeMQTTLenString(char** pptr, MQTTLenString lenstring)
{
writeInt(pptr, lenstring.len);
Expand Down
9 changes: 9 additions & 0 deletions src/MQTTProtocolClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,15 @@ void MQTTProtocol_freeClient(Clients* client)
if (client->sslopts->protos)
free((void*)client->sslopts->protos);
}
if (client->sslopts->struct_version >= 6)
{
if (client->sslopts->pemRootCerts)
free((void*)client->sslopts->pemRootCerts);
if (client->sslopts->pemCertChain)
free((void*)client->sslopts->pemCertChain);
if (client->sslopts->pemPrivateKey)
free((void*)client->sslopts->pemPrivateKey);
}
free(client->sslopts);
client->sslopts = NULL;
}
Expand Down
Loading