return DCM_ERROR_NONE;
}
+int dcm_client_connection::close_context() noexcept
+{
+ std::lock_guard<std::mutex> locker(fLock);
+
+ if(!fCookie) {
+ LOGE("Trying to close key context in session %p without connection", this);
+ return DCM_ERROR_SOCKET;
+ }
+
+ try {
+ RequestMessage request;
+ ResponseMessage response;
+
+ auto* close_req = request.mutable_close_context();
+ close_req->set_context_cookie(fCookie);
+
+ send_receive(request, response);
+ auto& close_resp(response.close_context());
+ if(close_resp.result() != 0) {
+ LOGE("Close key context for session %p received error %d", this, close_resp.result());
+ return DCM_ERROR_INVALID_PARAMETER;
+ }
+ } catch(std::bad_alloc&) {
+ LOGE("Out of memory when requesting close key context for session %p", this);
+ return DCM_ERROR_OUT_OF_MEMORY;
+ } catch(std::exception& ex) {
+ LOGE("When requesting close key context for session %p received exception : %s", this, ex.what());
+ return DCM_ERROR_UNKNOWN;
+ } catch(...) {
+ LOGE("When requesting close key context for session %p received exception : %s", this, "Unknown error");
+ return DCM_ERROR_UNKNOWN;
+ }
+
+ return DCM_ERROR_NONE;
+}
+
int dcm_client_connection::get_certificate_chain(std::vector<uint8_t>& chain) noexcept
{
std::lock_guard<std::mutex> locker(fLock);
const std::string& usage,
const std::string& key_type) noexcept;
+ int close_context() noexcept;
+
int get_certificate_chain(std::vector<uint8_t>& chain) noexcept;
const std::string& key_type() const noexcept;
optional bytes output_data = 2;
}
+message CloseKeyContext
+{
+ required uint64 context_cookie = 1;
+}
+
+message CloseKeyContextResponse
+{
+ required int32 result = 1;
+}
+
message RequestMessage {
oneof request_oneof {
AssociateKeyContext associate_context = 1;
RequestCertificateChain request_chain = 2;
SignRequest sign_data = 3;
ExtCallRequest ext_call = 4;
+ CloseKeyContext close_context = 5;
}
}
RequestCertificateChainResponse request_chain = 2;
SignResponse sign_data = 3;
ExtCallResponse ext_call = 4;
+ CloseKeyContextResponse close_context = 5;
}
}
if(!key_ctx)
return DCM_ERROR_NONE;
+ const dcm_key_context_internal *context =
+ reinterpret_cast<const dcm_key_context_internal *>(key_ctx);
+
+ context->connection->close_context();
delete reinterpret_cast<dcm_key_context_internal *>(key_ctx);
return DCM_ERROR_NONE;
case RequestMessage::kExtCall:
handle_ext_call_request(requestMessage.ext_call());
break;
+ case RequestMessage::kCloseContext:
+ handle_close_context(requestMessage.close_context());
+ break;
default:
LOGE("Incorrect request message type");
// This will terminate connection
}
reply(msg);
}
+
+void dcm_session::handle_close_context(const CloseKeyContext& message)
+{
+ LOGD("Request close key context");
+
+ if(!verify_privileges(fSocket.native_handle(), DCM_DEFAULT_PRIVILEGE)) {
+ LOGE("Client privilege check failure. Disconnect");
+ return;
+ }
+
+ ResponseMessage msg;
+ auto* closeResponse = msg.mutable_close_context();
+
+ if(message.context_cookie() != fCookie) {
+ LOGE("Received unknown context cookie");
+ closeResponse->set_result(-EINVAL);
+ reply(msg);
+ return;
+ }
+
+ if(!fBackendContext) {
+ LOGE("Context not associated with connection");
+ closeResponse->set_result(-EINVAL);
+ reply(msg);
+ return;
+ }
+
+ int error = 0;
+ try {
+ fSoResolver->invoke<void, dcm_backend_context&>(
+ "dcm_backend_free_key_context", *fBackendContext);
+ } catch(std::bad_alloc&) {
+ error = -ENOMEM;
+ } catch(std::exception&) {
+ error = -EINVAL;
+ } catch(...) {
+ error = -EFAULT;
+ }
+
+ closeResponse->set_result(error);
+ reply(msg);
+}
void handle_cert_chain(const RequestCertificateChain& message);
void handle_sign_request(const SignRequest& message);
void handle_ext_call_request(const ExtCallRequest& message);
+ void handle_close_context(const CloseKeyContext& message);
int sign(MessageDigestType digest_type, const std::string& message, std::string& signature);
int sign_request_check(const SignRequest& message);