feat(mqtt-intf): add keypass setting

This commit is contained in:
gc87
2022-05-12 23:25:59 +08:00
parent 31b5ee634a
commit 241aeb8654
6 changed files with 47 additions and 11 deletions
+1 -1
View File
@@ -75,7 +75,7 @@ ${MQTT_CONFIG_HOST} 127.0.0.1
${MQTT_CONFIG_PORT} ${1883}
${MQTT_CONFIG} {"client-id": "${MQTT_CLIENTID}", "upload-topic": "", "format": 1,
... "ssl": false, "host": "${MQTT_CONFIG_HOST}", "port": ${MQTT_CONFIG_PORT},
... "username": "", "password": "", "ca":"", "cert":"", "key":""}
... "username": "", "password": "", "ca":"", "cert":"", "key":"", "keypass":""}
${MODBUS_TCP_CONFIG} {"host": "127.0.0.1", "port": 60502, "connection_mode": 0}
+1 -1
View File
@@ -93,7 +93,7 @@ Get the setted adapters information after stopping and restarting Neuron, it sho
Node Setting ${modbus_tcp2_id} {"host": "127.0.0.1", "port": 502, "connection_mode": 0}
${mqtt_id} = Get Node ID ${NODE_MQTT} mqtt-1
Node Setting ${mqtt_id} {"client-id":"upload123", "upload-topic":"", "format": 0, "ssl":false,"host":"127.0.0.1","port":1883,"username":"admin","password":"0000","ca":"", "cert":"", "key":""}
Node Setting ${mqtt_id} {"client-id":"upload123", "upload-topic":"", "format": 0, "ssl":false,"host":"127.0.0.1","port":1883,"username":"admin","password":"0000","ca":"", "cert":"", "key":"", "keypass":""}
Neuron Stop Running ${process}
${process} = Neuron Start Running
+14
View File
@@ -128,5 +128,19 @@
"valid": {
"length": 81960
}
},
"keypass": {
"name": "keypass",
"description": "key password",
"attribute": "optional",
"type": "file",
"condition": {
"field": "ssl",
"value": true
},
"default": "",
"valid": {
"length": 256
}
}
}
+10 -2
View File
@@ -80,6 +80,11 @@ void mqtt_option_uninit(neu_mqtt_option_t *option)
free(option->key);
option->key = NULL;
}
if (NULL != option->keypass) {
free(option->keypass);
option->keypass = NULL;
}
}
int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option)
@@ -101,9 +106,11 @@ int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option)
neu_json_elem_t ca = { .name = "ca", .t = NEU_JSON_STR };
neu_json_elem_t cert = { .name = "cert", .t = NEU_JSON_STR };
neu_json_elem_t key = { .name = "key", .t = NEU_JSON_STR };
neu_json_elem_t keypass = { .name = "keypass", .t = NEU_JSON_STR };
ret = neu_parse_param(config->buf, &error, 11, &id, &upload, &format, &ssl,
&host, &port, &username, &password, &ca, &cert, &key);
ret = neu_parse_param(config->buf, &error, 12, &id, &upload, &format, &ssl,
&host, &port, &username, &password, &ca, &cert, &key,
&keypass);
if (0 != ret) {
return ret;
}
@@ -139,5 +146,6 @@ int mqtt_option_init(neu_config_t *config, neu_mqtt_option_t *option)
option->clean_session = 1;
option->will_topic = NULL;
option->will_payload = NULL;
option->keypass = keypass.v.val_str;
return 0;
}
+19 -6
View File
@@ -51,6 +51,7 @@ struct reconnect_state {
char * ca_path;
char * cert_file;
char * key_file;
char * keypass;
mqtt_c_client_t *client;
uint8_t send_buf[SEND_BUF_SIZE];
uint8_t recv_buf[RECV_BUF_SIZE];
@@ -90,7 +91,7 @@ static neu_err_code_e client_destroy(mqtt_c_client_t *client);
static neu_err_code_e client_subscribe_send(mqtt_c_client_t * client,
struct subscribe_tuple *tuple);
static X509 *ssl_ctx_load_cert(const char *b64)
static X509 *ssl_ctx_load_cert(const char *b64, const char *password)
{
unsigned char *bin = NULL;
int len = 0;
@@ -102,13 +103,13 @@ static X509 *ssl_ctx_load_cert(const char *b64)
X509 *cert = NULL;
BIO * bio = NULL;
bio = BIO_new_mem_buf((void *) bin, len);
cert = PEM_read_bio_X509(bio, NULL, 0, NULL);
cert = PEM_read_bio_X509(bio, NULL, 0, (void *) password);
BIO_free_all(bio);
free(bin);
return cert;
}
static EVP_PKEY *ssl_ctx_load_key(const char *b64)
static EVP_PKEY *ssl_ctx_load_key(const char *b64, const char *password)
{
unsigned char *bin = NULL;
int len = 0;
@@ -120,7 +121,7 @@ static EVP_PKEY *ssl_ctx_load_key(const char *b64)
EVP_PKEY *key = NULL;
BIO * bio = NULL;
bio = BIO_new_mem_buf((void *) bin, len);
key = PEM_read_bio_PrivateKey(bio, NULL, 0, NULL);
key = PEM_read_bio_PrivateKey(bio, NULL, 0, (void *) password);
BIO_free_all(bio);
free(bin);
return key;
@@ -196,7 +197,7 @@ static void ssl_ctx_init(struct reconnect_state *state)
// setup cert
if (NULL != cert_file && 0 < strlen(cert_file)) {
state->cert = ssl_ctx_load_cert(cert_file);
state->cert = ssl_ctx_load_cert(cert_file, NULL);
if (NULL != state->cert) {
if (0 >= SSL_CTX_use_certificate(state->ssl_ctx, state->cert)) {
log_error("failed to use certificate");
@@ -208,7 +209,8 @@ static void ssl_ctx_init(struct reconnect_state *state)
// setup key
if (NULL != key_file && 0 < strlen(key_file)) {
state->key = ssl_ctx_load_key(key_file);
const char *keypass = state->keypass;
state->key = ssl_ctx_load_key(key_file, keypass);
if (NULL != state->key) {
if (0 >= SSL_CTX_use_PrivateKey(state->ssl_ctx, state->key)) {
log_error("failed to use privatekey");
@@ -389,6 +391,10 @@ static void reconnect_state_uninit(struct reconnect_state *state)
if (NULL != state->will_payload) {
free(state->will_payload);
}
if (NULL != state->keypass) {
free(state->keypass);
}
}
static void subscribe_on_reconnect(mqtt_c_client_t *client)
@@ -574,6 +580,13 @@ static mqtt_c_client_t *client_create(const neu_mqtt_option_t *option,
}
}
if (NULL != client->option->keypass &&
0 < strlen(client->option->keypass)) {
state->keypass = strdup(client->option->keypass);
} else {
state->keypass = NULL;
}
client->mqtt.publish_response_callback_state = state;
return client;
}
+2 -1
View File
@@ -13,7 +13,8 @@ TEST(MQTTTest, mqtt_option_init_by_config)
"\"upload123\", \"upload-topic\":\"\", \"format\": "
"0,\"ssl\":false,\"host\":\"192.168.10."
"116\",\"port\":1883,\"username\":\"\",\"password\":"
"\"\",\"ca\":\"\", \"cert\":\"\", \"key\":\"\"}}";
"\"\",\"ca\":\"\", \"cert\":\"\", \"key\":\"\", "
"\"keypass\":\"\"}}";
int rc = mqtt_option_init(&config, &option);
EXPECT_EQ(0, rc);