4046a1d95c5f88ba27a9552eef4f0e60106cec38
[platform/core/security/key-manager.git] / src / manager / client-async / client-manager-async-impl.h
1 /*
2  *  Copyright (c) 2000 - 2014 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
33 {
34 public:
35     Impl();
36
37     NONCOPYABLE(Impl);
38
39     virtual ~Impl();
40
41     void saveKey(
42             const ObserverPtr& observer,
43             const Alias& alias,
44             const KeyShPtr& key,
45             const Policy& policy);
46     void saveCertificate(
47             const ObserverPtr& observer,
48             const Alias& alias,
49             const CertificateShPtr& cert,
50             const Policy& policy);
51     void saveData(
52             const ObserverPtr& observer,
53             const Alias& alias,
54             const RawBuffer& data,
55             const Policy& policy);
56     void savePKCS12(
57             const ObserverPtr& observer,
58             const Alias &alias,
59             const PKCS12ShPtr &pkcs,
60             const Policy &keyPolicy,
61             const Policy &certPolicy);
62
63     void createSignature(
64             const ObserverPtr& observer,
65             const Alias& privateKeyAlias,
66             const Password& password,
67             const RawBuffer& message,
68             const HashAlgorithm hash,
69             const RSAPaddingAlgorithm padding);
70     void verifySignature(
71             const ObserverPtr& observer,
72             const Alias& publicKeyOrCertAlias,
73             const Password& password,
74             const RawBuffer& message,
75             const RawBuffer& signature,
76             const HashAlgorithm hash,
77             const RSAPaddingAlgorithm padding);
78
79     void ocspCheck(
80             const ObserverPtr& observer,
81             const CertificateShPtrVector& certificateChainVector);
82
83     void setPermission(
84             const ObserverPtr& observer,
85             const Alias& alias,
86             const Label& accessor,
87             PermissionMask permissionMask);
88
89     // generic methods
90     void saveBinaryData(
91             const ManagerAsync::ObserverPtr& observer,
92             const Alias& alias,
93             DataType dataType,
94             const RawBuffer& rawData,
95             const Policy& policy);
96
97     void removeAlias(
98             const ManagerAsync::ObserverPtr& observer,
99             const Alias &alias);
100
101     void getBinaryData(
102             const ManagerAsync::ObserverPtr& observer,
103             const Alias &alias,
104             DataType sendDataType,
105             const Password &password);
106
107     void getPKCS12(
108             const ManagerAsync::ObserverPtr& observer,
109             const Alias &alias,
110             const Password &keyPassword,
111             const Password &certPassword);
112
113     void getBinaryDataAliasVector(
114             const ManagerAsync::ObserverPtr& observer,
115             DataType dataType);
116
117     void createKeyPair(
118             const ManagerAsync::ObserverPtr& observer,
119             const KeyType key_type,
120             const int     additional_param,
121             const Alias  &privateKeyAlias,
122             const Alias  &publicKeyAlias,
123             const Policy &policyPrivateKey,
124             const Policy &policyPublicKey);
125
126     template <typename T>
127     void getCertChain(
128             const ManagerAsync::ObserverPtr& observer,
129             LogicCommand command,
130             const CertificateShPtr &certificate,
131             const T &untrusted,
132             const T &trusted,
133             bool useSystemTrustedCertificates)
134     {
135         observerCheck(observer);
136         if (!certificate) {
137             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
138             return;
139         }
140         try_catch_async([&] {
141             sendToStorage(observer,
142                           static_cast<int>(command),
143                           m_counter,
144                           certificate->getDER(),
145                           untrusted,
146                           trusted,
147                           useSystemTrustedCertificates);
148         }, [&observer](int error){ observer->ReceivedError(error); } );
149     }
150
151 private:
152
153     template <typename... Args>
154     void sendToStorage(const ManagerAsync::ObserverPtr& observer, const Args&... args)
155     {
156         m_counter++; // yes, it changes m_counter argument passed in args
157
158         auto send = MessageBuffer::Serialize(args...);
159         thread()->sendMessage(AsyncRequest(observer,
160                                            SERVICE_SOCKET_CKM_STORAGE,
161                                            send.Pop(),
162                                            m_counter));
163     }
164
165     void observerCheck(const ManagerAsync::ObserverPtr& observer);
166
167     typedef std::unique_ptr<ConnectionThread> ConnectionThreadPtr;
168
169     ConnectionThreadPtr& thread() {
170         if (!m_thread || m_thread->finished()) {
171             m_thread.reset(new ConnectionThread());
172             m_thread->run();
173         }
174         return m_thread;
175     }
176
177     ConnectionThreadPtr m_thread;
178
179     static int m_counter;
180 };
181
182 } // namespace CKM