Disable default build with tz-backend
[platform/core/security/key-manager.git] / src / manager / crypto / platform / decider.cpp
1 /*
2  *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       decider.cpp
18  * @author     BartÅ‚omiej Grzelewski (b.grzelewski@samsung.com)
19  * @author     Lukasz Kostyra (l.kostyra@samsung.com)
20  * @version    1.0
21  */
22 #include <dpl/log/log.h>
23
24 #include <crypto-backend.h>
25
26 #include <platform/decider.h>
27
28 #include <generic-backend/exception.h>
29 #include <sw-backend/store.h>
30
31 #ifdef TZ_BACKEND_ENABLED
32 #include <tz-backend/store.h>
33 #include <tz-backend/tz-context.h>
34
35 #include <tee_client_api.h>
36 #include <km_ta_defines.h>
37 #endif // TZ_BACKEND_ENABLED
38
39 #include <sstream>
40 #include <fstream>
41 #include <iomanip>
42
43 namespace CKM {
44 namespace Crypto {
45
46 namespace {
47
48 template <typename T>
49 std::string ValueToString(const T& value)
50 {
51         std::stringstream str;
52         // we need to re-cast because otherwise stringstream
53         // will write our value incorrectly
54         str << std::setfill('0') << std::setw(2 * sizeof(T)) << std::hex
55                 << static_cast<uint64_t>(value);
56         return str.str();
57 }
58
59 CryptoBackend chooseCryptoBackend(DataType data,
60                                   const Policy &policy,
61                                   bool encrypted)
62 {
63 #ifdef TZ_BACKEND_ENABLED
64         // user directly point proper backend - we will not discuss with it
65         if (policy.backend == CKM::PolicyBackend::FORCE_SOFTWARE)
66                 return CryptoBackend::OpenSSL;
67
68         // user directly point proper backend - we will not discuss with it
69         if (policy.backend == CKM::PolicyBackend::FORCE_HARDWARE)
70                 return CryptoBackend::TrustZone;
71
72         // For now only software backend supports device encyption key
73         // TODO tz-backend could support the master key, but it would require
74         //      hardcoding a known key ID and querying TA whether the key is
75         //      reachable
76         if (encrypted)
77                 return CryptoBackend::OpenSSL;
78
79         // Only software backend allows for key export
80         if (policy.extractable)
81                 return CryptoBackend::OpenSSL;
82
83         // Use TrustZone only with symmetric keys until asymmetric
84         // cryptography is implemented
85         if (!data.isSKey())
86                 return CryptoBackend::OpenSSL;
87
88         try {
89                 LogDebug("Trying to open TA session...");
90                 TZ::Internals::TrustZoneContext::Instance();
91         } catch (const Exc::Crypto::InternalError& e) {
92                 LogDebug("...failed. Selecting SW backend.");
93                 return CryptoBackend::OpenSSL;
94         }
95
96         LogDebug("...succeeded. Selecting TZ backend.");
97         return CryptoBackend::TrustZone;
98 #else // TZ_BACKEND_ENABLED
99     (void) data;
100     (void) policy;
101     (void) encrypted;
102     return CryptoBackend::OpenSSL;
103 #endif // TZ_BACKEND_ENABLED
104 }
105
106 } // namespace
107
108 Decider::Decider()
109         : m_swStore(new SW::Store(CryptoBackend::OpenSSL))
110 #ifdef TZ_BACKEND_ENABLED
111         , m_tzStore(new TZ::Store(CryptoBackend::TrustZone))
112 #endif
113 {
114 }
115
116 GStore &Decider::getStore(const Token &token) const
117 {
118         return getStore(token.backendId);
119 };
120
121 GStore &Decider::getStore(CryptoBackend cryptoBackend) const
122 {
123         GStore *gStore = NULL;
124
125         if (cryptoBackend == CryptoBackend::OpenSSL)
126                 gStore = m_swStore.get();
127 #ifdef TZ_BACKEND_ENABLED
128         if (cryptoBackend == CryptoBackend::TrustZone)
129                 gStore = m_tzStore.get();
130 #endif
131         if (gStore)
132                 return *gStore;
133
134         ThrowErr(Exc::Crypto::InternalError,
135                          "Backend not available. BackendId: ", (int)cryptoBackend);
136 }
137
138 GStore &Decider::getStore(DataType data, const Policy &policy, bool encrypted) const
139 {
140         return getStore(chooseCryptoBackend(data, policy, encrypted));
141 }
142
143 } // namespace Crypto
144 } // namespace CKM
145