Service implementation.
[platform/core/security/key-manager.git] / src / manager / client / client-manager-impl.cpp
1 /* Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd All Rights Reserved
2  *
3  *  Licensed under the Apache License, Version 2.0 (the "License");
4  *  you may not use this file except in compliance with the License.
5  *  You may obtain a copy of the License at
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
9  *  Unless required by applicable law or agreed to in writing, software
10  *  distributed under the License is distributed on an "AS IS" BASIS,
11  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  *  See the License for the specific language governing permissions and
13  *  limitations under the License
14  *
15  *
16  * @file        client-manager-impl.cpp
17  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
18  * @version     1.0
19  * @brief       Manager implementation.
20  */
21 #include <dpl/serialization.h>
22 #include <dpl/log/log.h>
23
24 #include <client-manager-impl.h>
25 #include <client-common.h>
26 #include <message-buffer.h>
27 #include <protocols.h>
28
29 namespace CKM {
30
31 int Manager::ManagerImpl::saveBinaryData(
32     const Alias &alias,
33     DBDataType dataType,
34     const RawBuffer &rawData,
35     const Policy &policy)
36 {
37     m_counter++;
38
39     return try_catch([&] {
40         if (alias.empty() || rawData.empty())
41             return CKM_API_ERROR_INPUT_PARAM;
42
43         MessageBuffer send, recv;
44         Serialization::Serialize(send, static_cast<int>(LogicCommand::SAVE));
45         Serialization::Serialize(send, m_counter);
46         Serialization::Serialize(send, static_cast<int>(dataType));
47         Serialization::Serialize(send, alias);
48         Serialization::Serialize(send, rawData);
49         Serialization::Serialize(send, PolicySerializable(policy));
50
51         int retCode = sendToServer(
52             SERVICE_SOCKET_CKM_STORAGE,
53             send.Pop(),
54             recv);
55
56         if (CKM_API_SUCCESS != retCode) {
57             return retCode;
58         }
59
60         int command;
61         int counter;
62         int opType;
63         Deserialization::Deserialize(recv, command);
64         Deserialization::Deserialize(recv, counter);
65         Deserialization::Deserialize(recv, retCode);
66         Deserialization::Deserialize(recv, opType);
67
68         if (counter != m_counter) {
69             return CKM_API_ERROR_UNKNOWN;
70         }
71
72         return retCode;
73     });
74 }
75
76 int Manager::ManagerImpl::saveKey(const Alias &alias, const Key &key, const Policy &policy) {
77     if (key.empty())
78         return CKM_API_ERROR_INPUT_PARAM;
79     return saveBinaryData(alias, toDBDataType(key.getType()), key.getDER(), policy);
80 }
81
82 int Manager::ManagerImpl::saveCertificate(
83     const Alias &alias,
84     const Certificate &cert,
85     const Policy &policy)
86 {
87     return saveBinaryData(alias, DBDataType::CERTIFICATE, cert.getDER(), policy);
88 }
89
90 int Manager::ManagerImpl::saveData(const Alias &alias, const RawBuffer &rawData, const Policy &policy) {
91     if (!policy.extractable)
92         return CKM_API_ERROR_INPUT_PARAM;
93     return saveBinaryData(alias, DBDataType::BINARY_DATA, rawData, policy);
94 }
95
96 int Manager::ManagerImpl::removeBinaryData(const Alias &alias, DBDataType dataType)
97 {
98     return try_catch([&] {
99         if (alias.empty())
100             return CKM_API_ERROR_INPUT_PARAM;
101
102         MessageBuffer send, recv;
103         Serialization::Serialize(send, static_cast<int>(LogicCommand::REMOVE));
104         Serialization::Serialize(send, m_counter);
105         Serialization::Serialize(send, static_cast<int>(dataType));
106         Serialization::Serialize(send, alias);
107
108         int retCode = sendToServer(
109             SERVICE_SOCKET_CKM_STORAGE,
110             send.Pop(),
111             recv);
112
113         if (CKM_API_SUCCESS != retCode) {
114             return retCode;
115         }
116
117         int command;
118         int counter;
119         int opType;
120         Deserialization::Deserialize(recv, command);
121         Deserialization::Deserialize(recv, counter);
122         Deserialization::Deserialize(recv, retCode);
123         Deserialization::Deserialize(recv, opType);
124
125         if (counter != m_counter) {
126             return CKM_API_ERROR_UNKNOWN;
127         }
128
129         return retCode;
130     });
131 }
132
133 int Manager::ManagerImpl::removeKey(const Alias &alias) {
134     return removeBinaryData(alias, DBDataType::KEY_RSA_PUBLIC);
135 }
136
137 int Manager::ManagerImpl::removeCertificate(const Alias &alias) {
138     return removeBinaryData(alias, DBDataType::CERTIFICATE);
139 }
140
141 int Manager::ManagerImpl::removeData(const Alias &alias) {
142     return removeBinaryData(alias, DBDataType::BINARY_DATA);
143 }
144
145 int Manager::ManagerImpl::getBinaryData(
146     const Alias &alias,
147     DBDataType sendDataType,
148     const std::string &password,
149     DBDataType &recvDataType,
150     RawBuffer &rawData)
151 {
152     return try_catch([&] {
153         if (alias.empty())
154             return CKM_API_ERROR_INPUT_PARAM;
155
156         MessageBuffer send, recv;
157         Serialization::Serialize(send, static_cast<int>(LogicCommand::GET));
158         Serialization::Serialize(send, m_counter);
159         Serialization::Serialize(send, static_cast<int>(sendDataType));
160         Serialization::Serialize(send, alias);
161         Serialization::Serialize(send, password);
162
163         int retCode = sendToServer(
164             SERVICE_SOCKET_CKM_STORAGE,
165             send.Pop(),
166             recv);
167
168         if (CKM_API_SUCCESS != retCode) {
169             return retCode;
170         }
171
172         int command;
173         int counter;
174         int tmpDataType;
175         Deserialization::Deserialize(recv, command);
176         Deserialization::Deserialize(recv, counter);
177         Deserialization::Deserialize(recv, retCode);
178         Deserialization::Deserialize(recv, tmpDataType);
179         Deserialization::Deserialize(recv, rawData);
180         recvDataType = static_cast<DBDataType>(tmpDataType);
181
182         if (counter != m_counter) {
183             return CKM_API_ERROR_UNKNOWN;
184         }
185
186         return retCode;
187     });
188 }
189
190 int Manager::ManagerImpl::getKey(const Alias &alias, const std::string &password, Key &key) {
191     DBDataType recvDataType;
192     RawBuffer rawData;
193
194     int retCode = getBinaryData(
195         alias,
196         DBDataType::KEY_RSA_PUBLIC,
197         password,
198         recvDataType,
199         rawData);
200
201     if (retCode != CKM_API_SUCCESS)
202         return retCode;
203
204     Key keyParsed(rawData);
205
206     if (keyParsed.empty()) {
207         LogDebug("Key empty - failed to parse!");
208         return CKM_API_ERROR_BAD_RESPONSE;
209     }
210
211     key = keyParsed;
212
213     return CKM_API_SUCCESS;
214 }
215
216 int Manager::ManagerImpl::getCertificate(const Alias &alias, const std::string &password, Certificate &cert)
217 {
218     DBDataType recvDataType;
219     RawBuffer rawData;
220
221     int retCode = getBinaryData(
222         alias,
223         DBDataType::CERTIFICATE,
224         password,
225         recvDataType,
226         rawData);
227
228     if (retCode != CKM_API_SUCCESS)
229         return retCode;
230
231     if (recvDataType != DBDataType::CERTIFICATE)
232         return CKM_API_ERROR_BAD_RESPONSE;
233
234     Certificate certParsed(rawData, DataFormat::FORM_DER);
235
236     if (certParsed.empty())
237         return CKM_API_ERROR_BAD_RESPONSE;
238
239     cert = certParsed;
240
241     return CKM_API_SUCCESS;
242 }
243
244 int Manager::ManagerImpl::getData(const Alias &alias, const std::string &password, RawBuffer &rawData)
245 {
246     DBDataType recvDataType;
247
248     int retCode = getBinaryData(
249         alias,
250         DBDataType::BINARY_DATA,
251         password,
252         recvDataType,
253         rawData);
254
255     if (retCode != CKM_API_SUCCESS)
256         return retCode;
257
258     if (recvDataType != DBDataType::BINARY_DATA)
259         return CKM_API_ERROR_BAD_RESPONSE;
260
261     return CKM_API_SUCCESS;
262 }
263
264 int Manager::ManagerImpl::getBinaryDataAliasVector(DBDataType dataType, AliasVector &aliasVector)
265 {
266     return try_catch([&] {
267
268         MessageBuffer send, recv;
269         Serialization::Serialize(send, static_cast<int>(LogicCommand::GET_LIST));
270         Serialization::Serialize(send, m_counter);
271         Serialization::Serialize(send, static_cast<int>(dataType));
272
273         int retCode = sendToServer(
274             SERVICE_SOCKET_CKM_STORAGE,
275             send.Pop(),
276             recv);
277
278         if (CKM_API_SUCCESS != retCode) {
279             return retCode;
280         }
281
282         int command;
283         int counter;
284         int tmpDataType;
285
286         Deserialization::Deserialize(recv, command);
287         Deserialization::Deserialize(recv, counter);
288         Deserialization::Deserialize(recv, retCode);
289         Deserialization::Deserialize(recv, tmpDataType);
290         Deserialization::Deserialize(recv, aliasVector);
291         if ((command != static_cast<int>(LogicCommand::GET_LIST)) || (counter != m_counter)) {
292             return CKM_API_ERROR_UNKNOWN;
293         }
294
295         return retCode;
296     });
297 }
298
299 int Manager::ManagerImpl::getKeyAliasVector(AliasVector &aliasVector) {
300     return getBinaryDataAliasVector(DBDataType::KEY_RSA_PUBLIC, aliasVector);
301 }
302
303 int Manager::ManagerImpl::getCertificateAliasVector(AliasVector &aliasVector) {
304     return getBinaryDataAliasVector(DBDataType::CERTIFICATE, aliasVector);
305 }
306
307 int Manager::ManagerImpl::getDataAliasVector(AliasVector &aliasVector) {
308     return getBinaryDataAliasVector(DBDataType::BINARY_DATA, aliasVector);
309 }
310
311 int Manager::ManagerImpl::createKeyPairRSA(
312     const int size,              // size in bits [1024, 2048, 4096]
313     const Alias &privateKeyAlias,
314     const Alias &publicKeyAlias,
315     const Policy &policyPrivateKey,
316     const Policy &policyPublicKey)
317 {
318     m_counter++;
319     int my_counter = m_counter;
320     return try_catch([&] {
321
322         MessageBuffer send, recv;
323         Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_RSA));
324         Serialization::Serialize(send, my_counter);
325         Serialization::Serialize(send, static_cast<int>(size));
326         Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
327         Serialization::Serialize(send, PolicySerializable(policyPublicKey));
328         Serialization::Serialize(send, privateKeyAlias);
329         Serialization::Serialize(send, publicKeyAlias);
330
331         int retCode = sendToServer(
332             SERVICE_SOCKET_CKM_STORAGE,
333             send.Pop(),
334             recv);
335
336         if (CKM_API_SUCCESS != retCode) {
337             return retCode;
338         }
339
340         int command;
341         int counter;
342
343         Deserialization::Deserialize(recv, command);
344         Deserialization::Deserialize(recv, counter);
345         Deserialization::Deserialize(recv, retCode);
346         if (counter != my_counter) {
347             return CKM_API_ERROR_UNKNOWN;
348         }
349
350         return retCode;
351     });
352 }
353
354 int Manager::ManagerImpl::createKeyPairECDSA(
355     ElipticCurve type,
356     const Alias &privateKeyAlias,
357     const Alias &publicKeyAlias,
358     const Policy &policyPrivateKey,
359     const Policy &policyPublicKey)
360 {
361     m_counter++;
362     int my_counter = m_counter;
363     return try_catch([&] {
364
365         MessageBuffer send, recv;
366         Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_KEY_PAIR_ECDSA));
367         Serialization::Serialize(send, my_counter);
368         Serialization::Serialize(send, static_cast<unsigned int>(type));
369         Serialization::Serialize(send, PolicySerializable(policyPrivateKey));
370         Serialization::Serialize(send, PolicySerializable(policyPublicKey));
371         Serialization::Serialize(send, privateKeyAlias);
372         Serialization::Serialize(send, publicKeyAlias);
373
374         int retCode = sendToServer(
375             SERVICE_SOCKET_CKM_STORAGE,
376             send.Pop(),
377             recv);
378
379         if (CKM_API_SUCCESS != retCode) {
380             return retCode;
381         }
382
383         int command;
384         int counter;
385
386         Deserialization::Deserialize(recv, command);
387         Deserialization::Deserialize(recv, counter);
388         Deserialization::Deserialize(recv, retCode);
389
390         if (counter != my_counter) {
391             return CKM_API_ERROR_UNKNOWN;
392         }
393
394         return retCode;
395     });
396 }
397
398 template <class T>
399 int getCertChain(
400     LogicCommand command,
401     int counter,
402     const Certificate &certificate,
403     const T &sendData,
404     CertificateVector &certificateChainVector)
405 {
406     return try_catch([&] {
407
408         MessageBuffer send, recv;
409         Serialization::Serialize(send, static_cast<int>(command));
410         Serialization::Serialize(send, counter);
411         Serialization::Serialize(send, certificate.getDER());
412         Serialization::Serialize(send, sendData);
413         int retCode = sendToServer(
414             SERVICE_SOCKET_CKM_STORAGE,
415             send.Pop(),
416             recv);
417
418         if (CKM_API_SUCCESS != retCode) {
419             return retCode;
420         }
421
422         int retCommand;
423         int retCounter;
424         RawBufferVector rawBufferVector;
425
426         Deserialization::Deserialize(recv, retCommand);
427         Deserialization::Deserialize(recv, retCounter);
428         Deserialization::Deserialize(recv, retCode);
429         Deserialization::Deserialize(recv, rawBufferVector);
430
431         if ((counter != retCounter) || (static_cast<int>(command) != retCommand)) {
432             return CKM_API_ERROR_UNKNOWN;
433         }
434
435         if (retCode != CKM_API_SUCCESS) {
436             return retCode;
437         }
438
439         for (auto &e: rawBufferVector)
440             certificateChainVector.push_back(Certificate(e, DataFormat::FORM_DER));
441
442         return retCode;
443     });
444 }
445
446
447 int Manager::ManagerImpl::getCertificateChain(
448     const Certificate &certificate,
449     const CertificateVector &untrustedCertificates,
450     CertificateVector &certificateChainVector)
451 {
452     RawBufferVector rawBufferVector;
453
454     for (auto &e: untrustedCertificates) {
455         rawBufferVector.push_back(e.getDER());
456     }
457
458     return getCertChain(
459         LogicCommand::GET_CHAIN_CERT,
460         ++m_counter,
461         certificate,
462         rawBufferVector,
463         certificateChainVector);
464 }
465
466 int Manager::ManagerImpl::getCertificateChain(
467     const Certificate &certificate,
468     const AliasVector &untrustedCertificates,
469     CertificateVector &certificateChainVector)
470 {
471     (void) certificate;
472     (void) untrustedCertificates;
473     (void) certificateChainVector;
474     return CKM_API_ERROR_UNKNOWN;
475 }
476
477 int Manager::ManagerImpl::createSignature(
478     const Alias &privateKeyAlias,
479     const std::string &password,           // password for private_key
480     const RawBuffer &message,
481     const HashAlgorithm hash,
482     const RSAPaddingAlgorithm padding,
483     RawBuffer &signature)
484 {
485     m_counter++;
486     int my_counter = m_counter;
487     return try_catch([&] {
488
489         MessageBuffer send, recv;
490         Serialization::Serialize(send, static_cast<int>(LogicCommand::CREATE_SIGNATURE));
491         Serialization::Serialize(send, my_counter);
492         Serialization::Serialize(send, privateKeyAlias);
493         Serialization::Serialize(send, password);
494         Serialization::Serialize(send, message);
495         Serialization::Serialize(send, static_cast<int>(hash));
496         Serialization::Serialize(send, static_cast<int>(padding));
497
498         int retCode = sendToServer(
499             SERVICE_SOCKET_CKM_STORAGE,
500             send.Pop(),
501             recv);
502
503         if (CKM_API_SUCCESS != retCode) {
504             return retCode;
505         }
506
507         int command;
508         int counter;
509
510         Deserialization::Deserialize(recv, command);
511         Deserialization::Deserialize(recv, counter);
512         Deserialization::Deserialize(recv, signature);
513
514         if ((command != static_cast<int>(LogicCommand::CREATE_SIGNATURE)) || (counter != my_counter)) {
515             return CKM_API_ERROR_UNKNOWN;
516         }
517
518         return retCode;
519     });
520 }
521
522 int Manager::ManagerImpl::verifySignature(
523     const Alias &publicKeyOrCertAlias,
524     const std::string &password,           // password for public_key (optional)
525     const RawBuffer &message,
526     const RawBuffer &signature,
527     const HashAlgorithm hash,
528     const RSAPaddingAlgorithm padding)
529 {
530     m_counter++;
531     int my_counter = m_counter;
532     return try_catch([&] {
533
534         MessageBuffer send, recv;
535         Serialization::Serialize(send, static_cast<int>(LogicCommand::VERIFY_SIGNATURE));
536         Serialization::Serialize(send, my_counter);
537         Serialization::Serialize(send, publicKeyOrCertAlias);
538         Serialization::Serialize(send, password);
539         Serialization::Serialize(send, message);
540         Serialization::Serialize(send, signature);
541         Serialization::Serialize(send, static_cast<int>(hash));
542         Serialization::Serialize(send, static_cast<int>(padding));
543
544         int retCode = sendToServer(
545             SERVICE_SOCKET_CKM_STORAGE,
546             send.Pop(),
547             recv);
548
549         if (CKM_API_SUCCESS != retCode) {
550             return retCode;
551         }
552
553         int command;
554         int counter;
555
556         Deserialization::Deserialize(recv, command);
557         Deserialization::Deserialize(recv, counter);
558
559         if ((command != static_cast<int>(LogicCommand::VERIFY_SIGNATURE)) || (counter != my_counter)) {
560             return CKM_API_ERROR_UNKNOWN;
561         }
562
563         return retCode;
564     });
565 }
566
567 } // namespace CKM
568