Serialize AES key size as int
[platform/core/security/key-manager.git] / src / manager / client-async / client-manager-async-impl.h
1 /*
2  *  Copyright (c) 2014-2020 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       client-manager-async-impl.h
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #pragma once
23
24 #include <ckm/ckm-manager-async.h>
25 #include <memory>
26 #include <connection-thread.h>
27 #include <protocols.h>
28 #include <noncopyable.h>
29
30 namespace CKM {
31
32 class ManagerAsync::Impl final {
33 public:
34         Impl() {}
35
36         NONCOPYABLE(Impl);
37
38         void saveKey(
39                 const ObserverPtr &observer,
40                 const Alias &alias,
41                 const KeyShPtr &key,
42                 const Policy &policy);
43         void saveCertificate(
44                 const ObserverPtr &observer,
45                 const Alias &alias,
46                 const CertificateShPtr &cert,
47                 const Policy &policy);
48         void saveData(
49                 const ObserverPtr &observer,
50                 const Alias &alias,
51                 const RawBuffer &data,
52                 const Policy &policy);
53         void savePKCS12(
54                 const ObserverPtr &observer,
55                 const Alias &alias,
56                 const PKCS12ShPtr &pkcs,
57                 const Policy &keyPolicy,
58                 const Policy &certPolicy);
59
60         void createSignature(
61                 const ObserverPtr &observer,
62                 const Alias &privateKeyAlias,
63                 const Password &password,
64                 const RawBuffer &message,
65                 const CryptoAlgorithm &cAlgorithm);
66         void verifySignature(
67                 const ObserverPtr &observer,
68                 const Alias &publicKeyOrCertAlias,
69                 const Password &password,
70                 const RawBuffer &message,
71                 const RawBuffer &signature,
72                 const CryptoAlgorithm &cAlgorithm);
73
74         void ocspCheck(
75                 const ObserverPtr &observer,
76                 const CertificateShPtrVector &certificateChainVector);
77
78         void setPermission(
79                 const ObserverPtr &observer,
80                 const Alias &alias,
81                 const ClientId &accessor,
82                 PermissionMask permissionMask);
83
84         // generic methods
85         void saveBinaryData(
86                 const ObserverPtr &observer,
87                 const Alias &alias,
88                 DataType dataType,
89                 const RawBuffer &rawData,
90                 const Policy &policy);
91
92         void removeAlias(
93                 const ObserverPtr &observer,
94                 const Alias &alias);
95
96         void getBinaryData(
97                 const ObserverPtr &observer,
98                 const Alias &alias,
99                 DataType sendDataType,
100                 const Password &password);
101
102         void getPKCS12(
103                 const ObserverPtr &observer,
104                 const Alias &alias,
105                 const Password &keyPassword,
106                 const Password &certPassword);
107
108         void getBinaryDataAliasVector(
109                 const ObserverPtr &observer,
110                 DataType dataType);
111
112         void createKeyPair(
113                 const ObserverPtr &observer,
114                 const KeyType key_type,
115                 const int     additional_param,
116                 const Alias  &privateKeyAlias,
117                 const Alias  &publicKeyAlias,
118                 const Policy &policyPrivateKey,
119                 const Policy &policyPublicKey);
120
121         void createKeyAES(
122                 const ObserverPtr &observer,
123                 const int  size,
124                 const Alias  &keyAlias,
125                 const Policy &policyKey);
126
127         template <typename T>
128         void getCertChain(
129                 const ObserverPtr &observer,
130                 LogicCommand command,
131                 const CertificateShPtr &certificate,
132                 const T &untrusted,
133                 const T &trusted,
134                 bool useSystemTrustedCertificates)
135         {
136                 if (!certificate || certificate->empty())
137                         ThrowMsg(Exc::InputParam, "Empty certificate");
138
139                 sendToStorage(command, observer,
140                                           certificate->getDER(), untrusted, trusted, useSystemTrustedCertificates);
141         }
142
143         void crypt(
144                 const ObserverPtr &observer,
145                 const CryptoAlgorithm &algo,
146                 const Alias &keyAlias,
147                 const Password &password,
148                 const RawBuffer &input,
149                 bool encryption);
150
151         static void observerCheck(const ObserverPtr &observer);
152
153 private:
154         template <typename... Args>
155         void sendToStorage(LogicCommand command, const ObserverPtr &observer, Args &&... args)
156         {
157                 m_counter++;
158                 auto send = SerializeMessage(command, m_counter, std::forward<Args>(args)...);
159                 thread()->sendMessage(AsyncRequest(observer, SERVICE_SOCKET_CKM_STORAGE,
160                                                                                    std::move(send), m_counter, static_cast<int>(command)));
161         }
162
163         typedef std::unique_ptr<ConnectionThread> ConnectionThreadPtr;
164
165         ConnectionThreadPtr &thread()
166         {
167                 if (!m_thread || m_thread->finished()) {
168                         m_thread.reset(new ConnectionThread());
169                         m_thread->run();
170                 }
171
172                 return m_thread;
173         }
174
175         ConnectionThreadPtr m_thread;
176
177         static inline int m_counter = 0;
178 };
179
180 } // namespace CKM