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 verifications before calling callbacks #725

Merged
merged 1 commit into from
Sep 8, 2024
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
72 changes: 62 additions & 10 deletions lib/ocpp/common/websocket/websocket_libwebsockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,15 @@ void WebsocketTlsTPM::reconnect(long delay) {

{
std::lock_guard<std::mutex> lk(this->reconnect_mutex);
this->reconnect_timer_tpm.timeout([this]() { this->reconnect_callback(); }, std::chrono::milliseconds(delay));
this->reconnect_timer_tpm.timeout(
[this]() {
if (this->reconnect_callback) {
this->reconnect_callback();
} else {
EVLOG_error << "Invalid reconnect callback!";
}
},
std::chrono::milliseconds(delay));
}
}

Expand Down Expand Up @@ -824,8 +832,17 @@ void WebsocketTlsTPM::close(const WebsocketCloseReason code, const std::string&
recv_buffered_message.clear();

this->push_deferred_callback([this]() {
this->closed_callback(WebsocketCloseReason::Normal);
this->disconnected_callback();
if (this->closed_callback) {
this->closed_callback(WebsocketCloseReason::Normal);
} else {
EVLOG_error << "Closed callback not registered!";
}

if (this->disconnected_callback) {
this->disconnected_callback();
} else {
EVLOG_error << "Disconnected callback not registered!";
}
});
}

Expand All @@ -839,7 +856,13 @@ void WebsocketTlsTPM::on_conn_connected() {
// Clear any irrelevant data after a DC
recv_buffered_message.clear();

this->push_deferred_callback([this]() { this->connected_callback(this->connection_options.security_profile); });
this->push_deferred_callback([this]() {
if (connected_callback) {
this->connected_callback(this->connection_options.security_profile);
} else {
EVLOG_error << "Connected callback not registered!";
}
});
}

void WebsocketTlsTPM::on_conn_close() {
Expand All @@ -856,8 +879,17 @@ void WebsocketTlsTPM::on_conn_close() {
recv_buffered_message.clear();

this->push_deferred_callback([this]() {
this->closed_callback(WebsocketCloseReason::Normal);
this->disconnected_callback();
if (this->closed_callback) {
this->closed_callback(WebsocketCloseReason::Normal);
} else {
EVLOG_error << "Closed callback not registered!";
}

if (this->disconnected_callback) {
this->disconnected_callback();
} else {
EVLOG_error << "Disconnected callback not registered!";
}
});
}

Expand All @@ -866,7 +898,13 @@ void WebsocketTlsTPM::on_conn_fail() {

std::lock_guard<std::mutex> lk(this->connection_mutex);
if (this->m_is_connected) {
this->push_deferred_callback([this]() { this->disconnected_callback(); });
this->push_deferred_callback([this]() {
if (this->disconnected_callback) {
this->disconnected_callback();
} else {
EVLOG_error << "Disconnected callback not registered!";
}
});
}

this->m_is_connected = false;
Expand Down Expand Up @@ -1155,8 +1193,13 @@ int WebsocketTlsTPM::process_callback(void* wsi_ptr, int callback_reason, void*
if (!verify_csms_cn(this->connection_options.csms_uri.get_hostname(), (len == 1),
reinterpret_cast<X509_STORE_CTX*>(user),
this->connection_options.verify_csms_allow_wildcards)) {
this->push_deferred_callback(
[this]() { this->connection_failed_callback(ConnectionFailedReason::InvalidCSMSCertificate); });
this->push_deferred_callback([this]() {
if (this->connection_failed_callback) {
this->connection_failed_callback(ConnectionFailedReason::InvalidCSMSCertificate);
} else {
EVLOG_error << "Connection failed callback not registered!";
}
});
// Return 1 to fail the cert
return 1;
}
Expand Down Expand Up @@ -1363,6 +1406,11 @@ int WebsocketTlsTPM::process_callback(void* wsi_ptr, int callback_reason, void*
}

void WebsocketTlsTPM::push_deferred_callback(const std::function<void()>& callback) {
if (!callback) {
EVLOG_error << "Attempting to push stale callback in deferred queue!";
return;
}

std::scoped_lock tmp_lock(this->deferred_callback_mutex);
this->deferred_callback_queue.push(callback);
this->deferred_callback_cv.notify_one();
Expand All @@ -1385,7 +1433,11 @@ void WebsocketTlsTPM::handle_deferred_callback_queue() {
}
// This needs to be out of lock scope otherwise we still keep the mutex locked while executing the callback.
// This would block the callers of push_deferred_callback()
callback();
if (callback) {
callback();
} else {
EVLOG_error << "Stale callback in deferred queue!";
}
}
}

Expand Down
14 changes: 12 additions & 2 deletions lib/ocpp/v201/charge_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1966,8 +1966,18 @@ void ChargePoint::sign_certificate_req(const ocpp::CertificateSigningUseEnum& ce
this->device_model->get_optional_value<std::string>(ControllerComponentVariables::ISO15118CtrlrCountryName);
}

if (!common.has_value() or !country.has_value() or !organization.has_value()) {
EVLOG_warning << "Missing configuration of either organizationName, commonName or country to generate CSR";
if (!common.has_value()) {
EVLOG_warning << "Missing configuration of commonName to generate CSR";
return;
}

if (!country.has_value()) {
EVLOG_warning << "Missing configuration country to generate CSR";
return;
}

if (!organization.has_value()) {
EVLOG_warning << "Missing configuration of organizationName to generate CSR";
return;
}

Expand Down
Loading