/******************************************************************
*
- * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2024 Samsung Electronics All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "protobuf_asio.h"
#include "log.h"
+#define LOGE_return(error, str...)\
+{\
+ LOGE(str);\
+ return error;\
+}
+
+
size_t get_digest_size(dcm_digest_algorithm_e md)
{
switch(md) {
- case DCM_DIGEST_NONE:
- return 0;
case DCM_DIGEST_MD2:
case DCM_DIGEST_MD4:
case DCM_DIGEST_MD5:
return 64;
case DCM_DIGEST_RIPEMD160:
return 20;
+ case DCM_DIGEST_NONE:
default:
return 0;
}
std::lock_guard<std::mutex> locker(fLock);
if(fCookie) {
- LOGE("Cookie has already been requested for session %p", this);
- // Already created
- return DCM_ERROR_SOCKET;
+ LOGE_return(DCM_ERROR_SOCKET, "Cookie has already been requested for session %p", this);
}
if(!fSocket) {
boost::asio::local::stream_protocol::endpoint endpoint(DCM_UNIX_SOCKET_PATH);
fSocket->connect(endpoint);
} catch(std::exception& ex) {
- LOGE("Caught exception \"%s\" when connecting socket for session %p", ex.what(), this);
- return DCM_ERROR_SOCKET;
+ LOGE_return(DCM_ERROR_SOCKET,
+ "Caught exception \"%s\" when connecting socket for session %p",
+ ex.what(), this);
} catch(...) {
- LOGE("Caught unknown exception when connecting socket for session %p", this);
- return DCM_ERROR_SOCKET;
+ LOGE_return(DCM_ERROR_SOCKET,
+ "Caught unknown exception when connecting socket for session %p",
+ this);
}
}
send_receive(request, response);
if(!response.has_associate_context()) {
- LOGE("Received response is not context association message in context %p", this);
- return DCM_ERROR_NO_DATA;
+ LOGE_return(DCM_ERROR_NO_DATA,
+ "Received response is not context association message in context %p",
+ this);
}
auto& assoc_message(response.associate_context());
if(assoc_message.result() != 0) {
- LOGE("Received context association message with error %d in %p", assoc_message.result(), this);
- return DCM_ERROR_NO_DATA;
+ LOGE_return(DCM_ERROR_NO_DATA,
+ "Received context association message with error %d in %p",
+ assoc_message.result(),
+ this);
}
fCookie = assoc_message.context_cookie();
fKeyType = assoc_message.key_type();
fKeyLength = assoc_message.key_length();
} catch(std::exception& ex) {
- LOGE("Caught exception \"%s\" when establishing cookie for session %p", ex.what(), this);
fSocket.reset();
- return DCM_ERROR_SOCKET;
+ LOGE_return(DCM_ERROR_SOCKET,
+ "Caught exception \"%s\" when establishing cookie for session %p",
+ ex.what(),
+ this);
} catch(...) {
- LOGE("Caught unknown exception when establishing cookie for session %p", this);
fSocket.reset();
- return DCM_ERROR_SOCKET;
+ LOGE_return(DCM_ERROR_SOCKET,
+ "Caught unknown exception when establishing cookie for session %p",
+ this);
}
return DCM_ERROR_NONE;
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;
+ LOGE_return(DCM_ERROR_SOCKET,
+ "Trying to close key context in session %p without connection",
+ this);
}
try {
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;
+ LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+ "Close key context for session %p received error %d",
+ this,
+ close_resp.result());
}
} catch(std::bad_alloc&) {
- LOGE("Out of memory when requesting close key context for session %p", this);
- return DCM_ERROR_OUT_OF_MEMORY;
+ LOGE_return(DCM_ERROR_OUT_OF_MEMORY,
+ "Out of memory when requesting close key context for session %p",
+ this);
} catch(std::exception& ex) {
- LOGE("When requesting close key context for session %p received exception : %s", this, ex.what());
- return DCM_ERROR_UNKNOWN;
+ LOGE_return(DCM_ERROR_UNKNOWN,
+ "When requesting close key context for session %p received exception : %s",
+ this,
+ ex.what());
} catch(...) {
- LOGE("When requesting close key context for session %p received exception : %s", this, "Unknown error");
- return DCM_ERROR_UNKNOWN;
+ LOGE_return(DCM_ERROR_UNKNOWN,
+ "When requesting close key context for session %p received exception : %s",
+ this,
+ "Unknown error");
}
return DCM_ERROR_NONE;
std::lock_guard<std::mutex> locker(fLock);
if(!fCookie) {
- LOGE("Trying to request certificate in session %p without connection", this);
- return DCM_ERROR_SOCKET;
+ LOGE_return(DCM_ERROR_SOCKET,
+ "Trying to request certificate in session %p without connection",
+ this);
}
try {
send_receive(request, response);
if(!response.has_request_chain()) {
- LOGE("Response from server is not certificate chain response on session %p", this);
- return DCM_ERROR_NO_DATA;
+ LOGE_return(DCM_ERROR_NO_DATA,
+ "Response from server is not certificate chain response on session %p",
+ this);
}
auto& cert_resp(response.request_chain());
if(cert_resp.result() != 0) {
- LOGE("Server can't respond with certificate chain: error %d in session %p", cert_resp.result(), this);
- return DCM_ERROR_NO_DATA;
+ LOGE_return(DCM_ERROR_NO_DATA,
+ "Server can't respond with certificate chain: error %d in session %p",
+ cert_resp.result(),
+ this);
}
if(cert_resp.cert_chain().size() == 0) {
- LOGE("Server can't respond with certificate chain: certificate empty");
- return DCM_ERROR_NO_DATA;
+ LOGE_return(DCM_ERROR_NO_DATA,
+ "Server can't respond with certificate chain: certificate empty");
}
// Add space for last zero byte if needed
chain.push_back(0);
}
} catch(std::bad_alloc&) {
- LOGE("Out of memory when requesting certificate for session %p", this);
- return DCM_ERROR_OUT_OF_MEMORY;
+ LOGE_return(DCM_ERROR_OUT_OF_MEMORY,
+ "Out of memory when requesting certificate for session %p",
+ this);
} catch(std::invalid_argument&) {
- LOGE("Invalid argument passed for certificate request for session %p", this);
- return DCM_ERROR_INVALID_PARAMETER;
+ LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+ "Invalid argument passed for certificate request for session %p",
+ this);
} catch(std::exception& ex) {
- LOGE("When requesting certificate for session %p received exception : %s", this, ex.what());
- return DCM_ERROR_UNKNOWN;
+ LOGE_return(DCM_ERROR_UNKNOWN,
+ "When requesting certificate for session %p received exception : %s",
+ this,
+ ex.what());
} catch(...) {
- LOGE("When requesting certificate for session %p received exception : %s", this, "Unknown error");
- return DCM_ERROR_UNKNOWN;
+ LOGE_return(DCM_ERROR_UNKNOWN,
+ "When requesting certificate for session %p received exception : %s",
+ this,
+ "Unknown error");
}
return DCM_ERROR_NONE;
std::lock_guard<std::mutex> locker(fLock);
if(!fCookie) {
- LOGE("Trying to request data signing in object %p but there is no connection", this);
- return DCM_ERROR_SOCKET;
+ LOGE_return(DCM_ERROR_SOCKET,
+ "Trying to request data signing in object %p but there is no connection",
+ this);
}
// If hash_size == 0 then hash type must be known
if(hash_size == 0) {
if(md == DCM_DIGEST_NONE) {
- LOGE("Digest type is NONE and hash size is 0");
- return DCM_ERROR_INVALID_PARAMETER;
+ LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+ "Digest type is NONE and hash size is 0");
}
hash_size = get_digest_size(md);
} else if(hash_size != 0 && md != DCM_DIGEST_NONE) {
// If hash_size != 0 then hash type can be specified
if(hash_size != get_digest_size(md)) {
- LOGE("Hash size mismatch. Expected %zd but got %zd", hash_size, get_digest_size(md));
- return DCM_ERROR_INVALID_PARAMETER;
+ LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+ "Hash size mismatch. Expected %zd but got %zd",
+ hash_size,
+ get_digest_size(md));
}
}
send_receive(request, response);
if(!response.has_sign_data()) {
- LOGE("Response for hash signature has no signature data");
- return DCM_ERROR_NO_DATA;
+ LOGE_return(DCM_ERROR_NO_DATA,
+ "Response for hash signature has no signature data");
}
auto& sign_resp(response.sign_data());
if(sign_resp.result() != 0) {
- LOGE("Signature request for session %p received error %d", this, sign_resp.result());
- return DCM_ERROR_INVALID_PARAMETER;
+ LOGE_return(DCM_ERROR_INVALID_PARAMETER,
+ "Signature request for session %p received error %d",
+ this,
+ sign_resp.result());
}
const auto& signature = sign_resp.signature();
if(signature.empty()) {
- LOGE("Received signature object is empty for session %p", this);
- return DCM_ERROR_NO_DATA;
+ LOGE_return(DCM_ERROR_NO_DATA,
+ "Received signature object is empty for session %p",
+ this);
}
digest.resize(signature.size());
memcpy(&digest[0], signature.c_str(), signature.size());
} catch(std::bad_alloc&) {
- LOGE("Out of memory when processing sign request for session %p", this);
- return DCM_ERROR_OUT_OF_MEMORY;
+ LOGE_return(DCM_ERROR_OUT_OF_MEMORY,
+ "Out of memory when processing sign request for session %p",
+ this);
} catch(...) {
- LOGE("When processing signature for session %p got exception : %s", this, "Unknown error");
- return DCM_ERROR_UNKNOWN;
+ LOGE_return(DCM_ERROR_UNKNOWN,
+ "When processing signature for session %p got exception : %s",
+ this,
+ "Unknown error");
}
return DCM_ERROR_NONE;