Add PKCS12 support.
[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             Permission newPermission);
88
89     // generic methods
90     void saveBinaryData(
91             const ManagerAsync::ObserverPtr& observer,
92             const Alias& alias,
93             DBDataType 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             DBDataType sendDataType,
105             const Password &password);
106
107     void getPKCS12(
108             const ManagerAsync::ObserverPtr& observer,
109             const Alias &alias);
110
111     void getBinaryDataAliasVector(
112             const ManagerAsync::ObserverPtr& observer,
113             DBDataType dataType);
114
115     void createKeyPair(
116             const ManagerAsync::ObserverPtr& observer,
117             const KeyType key_type,
118             const int     additional_param,
119             const Alias  &privateKeyAlias,
120             const Alias  &publicKeyAlias,
121             const Policy &policyPrivateKey,
122             const Policy &policyPublicKey);
123
124     template <typename T>
125     void getCertChain(
126             const ManagerAsync::ObserverPtr& observer,
127             LogicCommand command,
128             const CertificateShPtr &certificate,
129             const T &sendData)
130     {
131         observerCheck(observer);
132         if (!certificate || sendData.empty()) {
133             observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
134             return;
135         }
136         try_catch_async([&] {
137             sendToStorage(observer,
138                           static_cast<int>(command),
139                           m_counter,
140                           certificate->getDER(),
141                           sendData);
142         }, [&observer](int error){ observer->ReceivedError(error); } );
143     }
144
145 private:
146
147     template <typename... Args>
148     void sendToStorage(const ManagerAsync::ObserverPtr& observer, const Args&... args)
149     {
150         m_counter++; // yes, it changes m_counter argument passed in args
151
152         auto send = MessageBuffer::Serialize(args...);
153         thread()->sendMessage(AsyncRequest(observer,
154                                            SERVICE_SOCKET_CKM_STORAGE,
155                                            send.Pop(),
156                                            m_counter));
157     }
158
159     void observerCheck(const ManagerAsync::ObserverPtr& observer);
160
161     typedef std::unique_ptr<ConnectionThread> ConnectionThreadPtr;
162
163     ConnectionThreadPtr& thread() {
164         if (!m_thread || m_thread->finished()) {
165             m_thread.reset(new ConnectionThread());
166             m_thread->run();
167         }
168         return m_thread;
169     }
170
171     ConnectionThreadPtr m_thread;
172
173     static int m_counter;
174 };
175
176 } // namespace CKM