Properly detect the presence of TA
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 2 Mar 2018 14:35:04 +0000 (15:35 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 12 Mar 2018 09:06:38 +0000 (10:06 +0100)
Tef-simulator and optee use different TA file name formats. Key-manager was
detecting the presence of TA by checking the existence of TA file with
hardcoded format. It worked with tef-simulator but it failed to detect the TA
presence in case of optee.

This commit replaces the TA file presence checking with an attempt to open a
session using libteec. If an attempt succeeds the decider selects TZ backend.
Otherwise, it falls back to SW backend.

Change-Id: I840d6b58a1ffa39885a4b8ded0ff70f4147c3de0

src/manager/crypto/platform/decider.cpp

index d5a5506..2a93729 100644 (file)
@@ -28,6 +28,7 @@
 #include <generic-backend/exception.h>
 #include <sw-backend/store.h>
 #include <tz-backend/store.h>
+#include <tz-backend/tz-context.h>
 
 #include <tee_client_api.h>
 #include <km_ta_defines.h>
@@ -41,8 +42,6 @@ namespace Crypto {
 
 namespace {
 
-const std::string TA_STORE_PATH = "/usr/lib/tastore";
-
 template <typename T>
 std::string ValueToString(const T& value)
 {
@@ -54,18 +53,6 @@ std::string ValueToString(const T& value)
        return str.str();
 }
 
-std::string convertTeecUUIDToString(TEEC_UUID uuid)
-{
-       std::string uuidStr;
-       uuidStr += ValueToString(uuid.timeLow);
-       uuidStr += ValueToString(uuid.timeMid);
-       uuidStr += ValueToString(uuid.timeHiAndVersion);
-       for (auto& c: uuid.clockSeqAndNode)
-               uuidStr += ValueToString(c);
-
-       return uuidStr;
-}
-
 CryptoBackend chooseCryptoBackend(DataType data,
                                   const Policy &policy,
                                   bool encrypted)
@@ -94,16 +81,16 @@ CryptoBackend chooseCryptoBackend(DataType data,
        if (!data.isSKey())
                return CryptoBackend::OpenSSL;
 
-       // Check if key-manager TA exists
-       std::string taUUIDStr = convertTeecUUIDToString(KM_TA_UUID);
-
-       LogDebug("Checking for " << TA_STORE_PATH << "/" << taUUIDStr);
-       std::ifstream taFile(TA_STORE_PATH + "/" + taUUIDStr);
-       if (taFile)
-               return CryptoBackend::TrustZone;
+       try {
+               LogDebug("Trying to open TA session...");
+               TZ::Internals::TrustZoneContext::Instance();
+       } catch (const Exc::Crypto::InternalError& e) {
+               LogDebug("...failed. Selecting SW backend.");
+               return CryptoBackend::OpenSSL;
+       }
 
-       // no TA available - fallback to OpenSSL
-       return CryptoBackend::OpenSSL;
+       LogDebug("...succeeded. Selecting TZ backend.");
+       return CryptoBackend::TrustZone;
 }
 
 } // namespace