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

Added client certificate options. #920

Merged
merged 1 commit into from
Mar 27, 2022
Merged
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
8 changes: 7 additions & 1 deletion example/cli.conf
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ mqtt_version=v5
clean_start=true

# CA certificate file. it is used only protocol mqtts and wss
#cacert=cacert.pem
#verify_file=cacert.pem

# Client Certificate (chain) file
#certificate=client.crt.pem

# Client Certificate key file
#private_key=client.key.pem

# Web-Scoket path. it is used only protocol ws and wss
#ws_path=/
Expand Down
50 changes: 46 additions & 4 deletions example/client_cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,10 +305,20 @@ int main(int argc, char* argv[]) {
"(optional) client_id"
)
(
"cacert",
"verify_file",
boost::program_options::value<std::string>(),
"CA Certificate file to verify server certificate for mqtts and wss connections"
)
(
"certificate",
boost::program_options::value<std::string>(),
"Client certificate (chain) file"
)
(
"private_key",
boost::program_options::value<std::string>(),
"Client certificate key file"
)
(
"ws_path",
boost::program_options::value<std::string>(),
Expand Down Expand Up @@ -430,10 +440,24 @@ int main(int argc, char* argv[]) {
}
return std::string();
} ();
auto cacert =
auto verify_file =
[&] () -> MQTT_NS::optional<std::string> {
if (vm.count("verify_file")) {
return vm["verify_file"].as<std::string>();
}
return MQTT_NS::nullopt;
} ();
auto certificate =
[&] () -> MQTT_NS::optional<std::string> {
if (vm.count("cacert")) {
return vm["cacert"].as<std::string>();
if (vm.count("certificate")) {
return vm["certificate"].as<std::string>();
}
return MQTT_NS::nullopt;
} ();
auto private_key =
[&] () -> MQTT_NS::optional<std::string> {
if (vm.count("private_key")) {
return vm["private_key"].as<std::string>();
}
return MQTT_NS::nullopt;
} ();
Expand Down Expand Up @@ -803,6 +827,15 @@ int main(int argc, char* argv[]) {
port,
version
);
if (verify_file) {
client->get_ssl_context().load_verify_file(*verify_file);
}
if (certificate) {
client->get_ssl_context().use_certificate_chain_file(*certificate);
}
if (private_key) {
client->get_ssl_context().use_private_key_file(*private_key, boost::asio::ssl::context::pem);
}
setup(*client);
return 0;
#else // defined(MQTT_USE_TLS)
Expand Down Expand Up @@ -835,6 +868,15 @@ int main(int argc, char* argv[]) {
ws_path ? ws_path.value() : std::string(),
version
);
if (verify_file) {
client->get_ssl_context().load_verify_file(*verify_file);
}
if (certificate) {
client->get_ssl_context().use_certificate_chain_file(*certificate);
}
if (private_key) {
client->get_ssl_context().use_private_key_file(*private_key, boost::asio::ssl::context::pem);
}
setup(*client);
return 0;
#else // defined(MQTT_USE_TLS) && defined(MQTT_USE_WS)
Expand Down