decider: Allow multiple policies for more complex logic
[platform/core/security/key-manager.git] / src / manager / crypto / platform / decider.cpp
1 /*
2  *  Copyright (c) 2015 - 2019 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(const DataParams& params)
60 {
61 #ifdef TZ_BACKEND_ENABLED
62         if (params.size() != 1 && params.size() != 2) {
63                 ThrowErr(Exc::Crypto::InternalError, "Invalid number of key parameters provided to decider");
64         }
65
66         // user directly point proper backend - we will not discuss with it
67         if (params[0].policy.backend == CKM::PolicyBackend::FORCE_SOFTWARE)
68                 return CryptoBackend::OpenSSL;
69
70         // user directly point proper backend - we will not discuss with it
71         if (params[0].policy.backend == CKM::PolicyBackend::FORCE_HARDWARE)
72                 return CryptoBackend::TrustZone;
73
74         if (params.size() == 1) {
75                 // For now only software backend supports device encyption key
76                 // TODO tz-backend could support the master key, but it would require
77                 //      hardcoding a known key ID and querying TA whether the key is
78                 //      reachable
79                 if (params[0].encrypted)
80                         return CryptoBackend::OpenSSL;
81
82                 // tz-backend allows only for data binary export
83                 if (params[0].policy.extractable && !params[0].data.isBinaryData())
84                         return CryptoBackend::OpenSSL;
85
86                 // Use TrustZone only with symmetric keys or unencrypted binary
87                 // data until asymmetric cryptography is implemented
88                 if (!params[0].data.isSKey() && !params[0].data.isBinaryData())
89                         return CryptoBackend::OpenSSL;
90         } else if (params.size() == 2) {
91                 LogDebug("2 keys - asymmetric encryption not yet supported, selecting OpenSSL");
92                 return CryptoBackend::OpenSSL;
93         }
94
95         try {
96                 LogDebug("Trying to open TA session...");
97                 TZ::Internals::TrustZoneContext::Instance();
98         } catch (const Exc::Crypto::InternalError& e) {
99                 LogDebug("...failed. Selecting SW backend.");
100                 return CryptoBackend::OpenSSL;
101         }
102
103         LogDebug("...succeeded. Selecting TZ backend.");
104         return CryptoBackend::TrustZone;
105
106 #else // TZ_BACKEND_ENABLED
107     (void) params;
108     return CryptoBackend::OpenSSL;
109 #endif // TZ_BACKEND_ENABLED
110 }
111
112 } // namespace
113
114 Decider::Decider()
115         : m_swStore(new SW::Store(CryptoBackend::OpenSSL))
116 #ifdef TZ_BACKEND_ENABLED
117         , m_tzStore(new TZ::Store(CryptoBackend::TrustZone))
118 #endif
119 {
120 }
121
122 GStore &Decider::getStore(const Token &token) const
123 {
124         return getStore(token.backendId);
125 };
126
127 GStore &Decider::getStore(CryptoBackend cryptoBackend) const
128 {
129         GStore *gStore = NULL;
130
131         if (cryptoBackend == CryptoBackend::OpenSSL)
132                 gStore = m_swStore.get();
133 #ifdef TZ_BACKEND_ENABLED
134         if (cryptoBackend == CryptoBackend::TrustZone)
135                 gStore = m_tzStore.get();
136 #endif
137         if (gStore)
138                 return *gStore;
139
140         ThrowErr(Exc::Crypto::InternalError,
141                          "Backend not available. BackendId: ", (int)cryptoBackend);
142 }
143
144 GStore &Decider::getStore(DataType data, const Policy &policy, bool encrypted) const
145 {
146         DataParams params{
147                 DataParam(data, policy, encrypted)
148         };
149
150         return getStore(chooseCryptoBackend(params));
151 }
152
153 GStore &Decider::getStore(const DataParams& params) const
154 {
155         return getStore(chooseCryptoBackend(params));
156 }
157
158 } // namespace Crypto
159 } // namespace CKM
160