#include <bundle.h>
#include <message-port.h>
+#include <package_manager.h>
#include <FBaseStringComparer.h>
#include <FBaseSysLog.h>
+#include <FBase_StringConverter.h>
#include "FIo_MessagePortStub.h"
#include "FIo_MessagePortService.h"
using namespace Tizen::Io;
using namespace Tizen::App;
+using namespace std;
+
static const char LOCAL_APPID[] = "LOCAL_APPID";
static const char LOCAL_PORT[] = "LOCAL_PORT";
static const char TRUSTED_LOCAL[] = "TRUSTED_LOCAL";
{
static _StringHashProvider hashProvider;
static _StringComparer stringComparer;
- result r = E_SUCCESS;
__pPorts = new HashMapT <String, int>();
__pPorts->Construct(0, 0, hashProvider, stringComparer);
bool out = false;
- if (trusted.Equals(L"FALSE", false))
+ if (trusted.Equals(L"TRUE", false))
{
- __pPorts->ContainsKey(key, out);
- SysTryReturnResult(NID_IO, !out, MESSAGEPORT_ERROR_IO_ERROR, "The port (%ls) has already registered", key.GetPointer());
-
- __pPorts->Add(key, clientId);
+ __pTrustedPorts->ContainsKey(key, out);
}
else
{
- __pTrustedPorts->ContainsKey(key, out);
- SysTryReturnResult(NID_IO, !out, MESSAGEPORT_ERROR_IO_ERROR, "The trusted port (%ls) has already registered", key.GetPointer());
+ __pPorts->ContainsKey(key, out);
+ }
+
+ SysTryReturn(NID_IO, !out, MESSAGEPORT_ERROR_IO_ERROR, E_SYSTEM,
+ "[E_SYSTEM] The local message port (%ls) has already registered", key.GetPointer());
+ if (trusted.Equals(L"TRUE", false))
+ {
__pTrustedPorts->Add(key, clientId);
}
+ else
+ {
+ __pPorts->Add(key, clientId);
+ }
return 0;
}
bool out = false;
- if (trusted.Equals(L"FALSE", false))
+ if (trusted.Equals(L"TRUE", false))
{
- __pPorts->ContainsKey(key, out);
- if (!out)
- {
- SysLogException(NID_IO, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The remote message port [%ls] is not found.", key.GetPointer());
- return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
- }
+ __pTrustedPorts->ContainsKey(key, out);
}
else
{
- __pTrustedPorts->ContainsKey(key, out);
- if (!out)
+ __pPorts->ContainsKey(key, out);
+ }
+
+ SysTryReturn(NID_IO, out == true, MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND, E_OBJ_NOT_FOUND,
+ "[E_OBJ_NOT_FOUND] The remote message port (%ls) is not found.", key.GetPointer());
+
+ if (trusted.Equals(L"TRUE", false))
+ {
+ String localAppId = bundle_get_val(buffer.b, LOCAL_APPID);
+ String remoteAppId = bundle_get_val(buffer.b, REMOTE_APPID);
+
+ // Check the preloaded
+ if (!IsPreloaded(localAppId, remoteAppId))
{
- SysLogException(NID_IO, E_OBJ_NOT_FOUND, "[E_OBJ_NOT_FOUND] The trusted remote message port [%ls] is not found.", key.GetPointer());
- return MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND;
+ // Check the certificate
+ return CheckCertificate(localAppId, remoteAppId);
}
}
return r;
}
-result
+int
_MessagePortService::SendMessage(const BundleBuffer& buffer)
{
result r = E_SUCCESS;
SysLog(NID_IO, "Sends a message to a remote message port [%ls]", key.GetPointer());
String trustedMessage(bundle_get_val(b, TRUSTED_MESSAGE));
- if (trustedMessage.Equals(L"FALSE", false))
+ if (trustedMessage.Equals(L"TRUE", false))
{
- r = __pPorts->GetValue(key, clientId);
+ r = __pTrustedPorts->GetValue(key, clientId);
}
else
{
- r = __pTrustedPorts->GetValue(key, clientId);
+ r = __pPorts->GetValue(key, clientId);
}
- SysTryReturnResult(NID_IO, r == E_SUCCESS, E_OBJ_NOT_FOUND,
- "The destination message port is not found.");
+ SysTryReturn(NID_IO, r == E_SUCCESS, MESSAGEPORT_ERROR_MESSAGEPORT_NOT_FOUND, E_OBJ_NOT_FOUND,
+ "[E_OBJ_NOT_FOUND] The destination message port (%ls) is not found.", key.GetPointer());
+
+ if (trustedMessage.Equals(L"TRUE", false))
+ {
+ String localAppId = bundle_get_val(buffer.b, LOCAL_APPID);
+ String remoteAppId = bundle_get_val(buffer.b, REMOTE_APPID);
+
+ // Check the preloaded
+ if (!IsPreloaded(localAppId, remoteAppId))
+ {
+ // Check the certificate
+ int ret = CheckCertificate(localAppId, remoteAppId);
+ if (ret < 0)
+ {
+ return ret;
+ }
+ }
+ }
r = __pStub->SendMessage(clientId, buffer);
- SysTryReturnResult(NID_IO, r == E_SUCCESS, E_SYSTEM, "Failed to send a message");
+ SysTryReturn(NID_IO, r == E_SUCCESS, MESSAGEPORT_ERROR_IO_ERROR, E_SYSTEM,
+ "[E_SYSTEM] Failed to send a message.");
return E_SUCCESS;
}
return key;
}
+bool
+_MessagePortService::IsPreloaded(const String& localAppId, const String& remoteAppId) const
+{
+ bool preload_local = false;
+ bool preload_remote = false;
+
+ unique_ptr<char[]> pLocalAppId(_StringConverter::CopyToCharArrayN(localAppId));
+ unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
+
+ int ret = package_manager_is_preload_package_by_app_id(pLocalAppId.get(), &preload_local);
+ if (ret == 0)
+ {
+ ret = package_manager_is_preload_package_by_app_id(pRemoteAppId.get(), &preload_remote);
+ if (ret == 0)
+ {
+ if (preload_local && preload_remote)
+ {
+ return true;
+ }
+ }
+ else
+ {
+ SysLog(NID_IO, "Failed to check the preloaded application. %d", ret);
+ }
+ }
+ else
+ {
+ SysLog(NID_IO, "Failed to check the preloaded application. %d", ret);
+ }
+
+ return false;
+}
+
+int
+_MessagePortService::CheckCertificate(const String& localAppId, const String& remoteAppId) const
+{
+ package_manager_compare_result_type_e res;
+
+ unique_ptr<char[]> pLocalAppId(_StringConverter::CopyToCharArrayN(localAppId));
+ unique_ptr<char[]> pRemoteAppId(_StringConverter::CopyToCharArrayN(remoteAppId));
+
+ int ret = package_manager_compare_app_cert_info(pLocalAppId.get(), pRemoteAppId.get(), &res);
+ SysTryReturn(NID_IO, ret == 0, MESSAGEPORT_ERROR_IO_ERROR, E_SYSTEM,
+ "[E_SYSTEM] Failed to check the certificate: %d", ret);
+
+ SysTryReturn(NID_IO, res == PACAKGE_MANAGER_COMPARE_MATCH, MESSAGEPORT_ERROR_CERTIFICATE_NOT_MATCH, E_CERTIFICATE_VERIFICATION_FAILED,
+ "[E_CERTIFICATE_VERIFICATION_FAILED] Both applications are not signed with the same certificate: %d", res);
+
+ return 0;
+}
+