Turn backends into Decider members
[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
30 #ifdef TZ_BACKEND_ENABLED
31 #include <tz-backend/tz-context.h>
32
33 #include <tee_client_api.h>
34 #include <km_ta_defines.h>
35 #endif // TZ_BACKEND_ENABLED
36
37 #include <sstream>
38 #include <fstream>
39 #include <iomanip>
40
41 namespace CKM {
42 namespace Crypto {
43
44 namespace {
45
46 template <typename T>
47 std::string ValueToString(const T& value)
48 {
49         std::stringstream str;
50         // we need to re-cast because otherwise stringstream
51         // will write our value incorrectly
52         str << std::setfill('0') << std::setw(2 * sizeof(T)) << std::hex
53                 << static_cast<uint64_t>(value);
54         return str.str();
55 }
56
57 CryptoBackend chooseCryptoBackend(const DataParams& params)
58 {
59 #ifdef TZ_BACKEND_ENABLED
60         if (params.size() != 1 && params.size() != 2) {
61                 ThrowErr(Exc::Crypto::InternalError, "Invalid number of key parameters provided to decider");
62         }
63
64         // user directly point proper backend - we will not discuss with it
65         if (params[0].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 (params[0].policy.backend == CKM::PolicyBackend::FORCE_HARDWARE)
70                 return CryptoBackend::TrustZone;
71
72         if (params.size() == 1) {
73                 // For now only software backend supports device encyption key
74                 // TODO tz-backend could support the master key, but it would require
75                 //      hardcoding a known key ID and querying TA whether the key is
76                 //      reachable
77                 if (params[0].encrypted) {
78                         return CryptoBackend::OpenSSL;
79                 }
80
81                 // tz-backend allows only for data binary export
82                 if (params[0].policy.extractable && !params[0].data.isBinaryData()) {
83                         return CryptoBackend::OpenSSL;
84                 }
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                 }
91         } else if (params.size() == 2) {
92                 // extractable private key can only be handled by OpenSSL
93                 if (params[0].policy.extractable) {
94                         return CryptoBackend::OpenSSL;
95                 }
96
97                 // ECDSA algorithm is unsupported by GP API 1.0
98                 if (params[0].data.isEllipticCurve() || params[1].data.isEllipticCurve()) {
99                         return CryptoBackend::OpenSSL;
100                 }
101         }
102
103         try {
104                 LogDebug("Trying to open TA session...");
105                 TZ::Internals::TrustZoneContext::Instance();
106         } catch (const Exc::Crypto::InternalError& e) {
107                 LogDebug("...failed. Selecting SW backend.");
108                 return CryptoBackend::OpenSSL;
109         }
110
111         LogDebug("...succeeded. Selecting TZ backend.");
112         return CryptoBackend::TrustZone;
113
114 #else // TZ_BACKEND_ENABLED
115     (void) params;
116     return CryptoBackend::OpenSSL;
117 #endif // TZ_BACKEND_ENABLED
118 }
119
120 } // namespace
121
122 Decider::Decider()
123         : m_swStore(CryptoBackend::OpenSSL)
124 #ifdef TZ_BACKEND_ENABLED
125         , m_tzStore(CryptoBackend::TrustZone)
126 #endif
127 {
128 }
129
130 GStore &Decider::getStore(const Token &token)
131 {
132         return getStore(token.backendId);
133 };
134
135 GStore &Decider::getStore(CryptoBackend cryptoBackend)
136 {
137         GStore *gStore = NULL;
138
139         if (cryptoBackend == CryptoBackend::OpenSSL)
140                 gStore = &m_swStore;
141 #ifdef TZ_BACKEND_ENABLED
142         if (cryptoBackend == CryptoBackend::TrustZone)
143                 gStore = &m_tzStore;
144 #endif
145         if (gStore)
146                 return *gStore;
147
148         ThrowErr(Exc::Crypto::InternalError,
149                          "Backend not available. BackendId: ", (int)cryptoBackend);
150 }
151
152 GStore &Decider::getStore(DataType data, const Policy &policy, bool encrypted)
153 {
154         DataParams params{
155                 DataParam(data, policy, encrypted)
156         };
157
158         return getStore(chooseCryptoBackend(params));
159 }
160
161 GStore &Decider::getStore(const DataParams& params)
162 {
163         return getStore(chooseCryptoBackend(params));
164 }
165
166 } // namespace Crypto
167 } // namespace CKM
168