/******************************************************************
*
- * Copyright 2017 - 2018 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2020 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 <inttypes.h>
#include <mbedtls/ssl.h>
-#ifdef USE_DLOG_LOGGING
#define LOG_TAG "DCM_CLIENT"
#include <dlog.h>
-#endif
static_assert(MD_NONE == (unsigned int)MBEDTLS_MD_NONE, "MBEDTLS_MD_NONE mismatch");
static_assert(MD_MD2 == (unsigned int)MBEDTLS_MD_MD2, "MBEDTLS_MD_MD2 mismatch");
std::lock_guard<std::mutex> locker(fLock);
if(fCookie) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Cookie has already been requested for session %p", __FUNCTION__, this);
-#endif
// Already created
return false;
}
try {
ensureSocketConnected();
} catch(std::exception& ex) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Caught exception \"%s\" when connecting socket for session %p", __FUNCTION__, ex.what(), this);
-#endif
return false;
} catch(...) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Caught unknown exception when connecting socket for session %p", __FUNCTION__, this);
-#endif
return false;
}
}
sendReceive(request, response);
if(!response.has_associate_context()) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: received response is not context association message in context %p", __FUNCTION__, this);
-#endif
return false;
}
auto& assoc_message(response.associate_context());
if(assoc_message.result() != 0) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Received context association message with error %d in %p", __FUNCTION__, assoc_message.result(), this);
-#endif
return false;
}
fKeyType = assoc_message.key_type();
fKeyLength = assoc_message.key_length();
} catch(std::exception& ex) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Caught exception \"%s\" when establishing cookie for session %p", __FUNCTION__, ex.what(), this);
-#endif
fSocket.reset();
return false;
} catch(...) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Caught unknown exception when establishing cookie for session %p", __FUNCTION__, this);
-#endif
fSocket.reset();
return false;
}
std::lock_guard<std::mutex> locker(fLock);
if(!fCookie) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Trying to request certificate in session %p without connection", __FUNCTION__, this);
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
sendReceive(request, response);
if(!response.has_request_chain()) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Response from server is not certificate chain response on session %p", __FUNCTION__, this);
-#endif
return DCM_ERROR_NO_DATA;
}
auto& cert_resp(response.request_chain());
if(cert_resp.result() != 0) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Server can't respond with certificate chain: error %d in session %p", __FUNCTION__,
cert_resp.result(), this);
-#endif
return DCM_ERROR_NO_DATA;
}
if(cert_resp.cert_chain().size() == 0) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Server can't respond with certificate chain: certificate empty", __FUNCTION__);
-#endif
return DCM_ERROR_NO_DATA;
}
chain.push_back(0);
}
} catch(std::bad_alloc&) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Out of memory when requesting certificate for session %p", __FUNCTION__, this);
-#endif
return DCM_ERROR_OUT_OF_MEMORY;
} catch(std::invalid_argument&) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Invalid argument passed for certificate request for session %p", __FUNCTION__, this);
-#endif
return DCM_ERROR_INVALID_PARAMETER;
} catch(std::exception& ex) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: When requesting certificate for session %p received exception : %s", __FUNCTION__, this, ex.what());
-#endif
return DCM_ERROR_UNKNOWN;
} catch(...) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: When requesting certificate for session %p received exception : %s", __FUNCTION__, this, "Unknown error");
-#endif
return DCM_ERROR_UNKNOWN;
}
std::lock_guard<std::mutex> locker(fLock);
if(!fCookie) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Trying to request data signing in object %p but there is no connection", __FUNCTION__, this);
-#endif
return DCM_ERROR_SOCKET;
}
*/
if(hash_size == 0) {
if(digestType == MBEDTLS_MD_NONE) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Digest type is NONE and hash size is 0", __FUNCTION__);
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
const mbedtls_md_info_t * md_info = mbedtls_md_info_from_type(digestType);
if(!md_info) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Can't find hash data for digest type %d", __FUNCTION__, digestType);
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
const mbedtls_md_info_t * md_info = mbedtls_md_info_from_type(digestType);
if(!md_info) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Can't find hash data for digest type %d", __FUNCTION__, digestType);
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
if(hash_size != mbedtls_md_get_size(md_info)) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Hash size mismatch. Expected %zd but got %zd", __FUNCTION__, hash_size, (size_t)mbedtls_md_get_size(md_info));
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
}
sendReceive(request, response);
if(!response.has_sign_data()) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Response for hash signature has no signature data", __FUNCTION__);
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
auto& sign_resp(response.sign_data());
if(sign_resp.result() != 0) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Signature request for session %p received error %d", __FUNCTION__, this, sign_resp.result());
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
const auto& signature = sign_resp.signature();
if(signature.empty()) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Received signature object is empty for session %p", __FUNCTION__, this);
-#endif
return DCM_ERROR_INVALID_PARAMETER;
}
digest.resize(signature.size());
memcpy(&digest[0], signature.c_str(), signature.size());
} catch(std::bad_alloc&) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: Out of memory when processing sign request for session %p", __FUNCTION__, this);
-#endif
return DCM_ERROR_OUT_OF_MEMORY;
} catch(...) {
-#ifdef USE_DLOG_LOGGING
LOGE("%s: When processing signature for session %p got exception : %s", __FUNCTION__, this, "Unknown error");
-#endif
return DCM_ERROR_UNKNOWN;
}
/******************************************************************
*
- * Copyright 2017 - 2019 Samsung Electronics All Rights Reserved.
+ * Copyright 2017 - 2020 Samsung Electronics All Rights Reserved.
*
* Author: Jaroslaw Pelczar <j.pelczar@samsung.com>
*
#include <boost/log/sinks.hpp>
#include <functional>
-#ifdef USE_DLOG_LOGGING
#include <dlog.h>
-#else
-typedef enum {
- DLOG_UNKNOWN = 0, /**< Keep this always at the start */
- DLOG_DEFAULT, /**< Default */
- DLOG_VERBOSE, /**< Verbose */
- DLOG_DEBUG, /**< Debug */
- DLOG_INFO, /**< Info */
- DLOG_WARN, /**< Warning */
- DLOG_ERROR, /**< Error */
- DLOG_FATAL, /**< Fatal */
- DLOG_SILENT, /**< Silent */
- DLOG_PRIO_MAX /**< Keep this always at the end. */
-} log_priority;
-#endif
template<typename AttributeValueT = int> class dlog_direct_severity_mapping :
public boost::log::sinks::basic_direct_mapping<log_priority, AttributeValueT>
severity_mapper_type level_mapper_;
inline void send(log_priority level, string_type const& formatted_message) {
-#ifdef USE_DLOG_LOGGING
dlog_print(level, log_domain_.c_str(), "%s", formatted_message.c_str());
-#else
- fprintf(stderr, "%d/(%s): %s\n", level, log_domain_.c_str(), formatted_message.c_str());
-#endif
}
public: