Add Manager 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 <client-manager-impl.h>
22 #include <message-buffer.h>
23 #include <client-common.h>
24 #include <dpl/serialization.h>
25 #include <protocols.h>
26 #include <client-key-impl.h>
27
28 namespace CKM {
29
30 int Manager::ManagerImpl::saveKey(const Alias &alias, const Key &key, const Policy &policy) {
31     m_counter++;
32
33     return try_catch([&] {
34         if (alias.empty() || key.empty())
35             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
36
37         MessageBuffer send, recv;
38         Serialization::Serialize(send, static_cast<int>(StorageCommand::SAVE));
39         Serialization::Serialize(send, m_counter);
40         Serialization::Serialize(send, static_cast<int>(toDBDataType(key.getType())));
41         Serialization::Serialize(send, alias);
42         Serialization::Serialize(send, key.getImpl());
43         Serialization::Serialize(send, PolicySerializable(policy));
44
45         int retCode = sendToServer(
46             SERVICE_SOCKET_CKM_STORAGE,
47             send.Pop(),
48             recv);
49
50         if (KEY_MANAGER_API_SUCCESS != retCode) {
51             return retCode;
52         }
53
54         int command;
55         int counter;
56         int opType;
57         Deserialization::Deserialize(recv, command);
58         Deserialization::Deserialize(recv, counter);
59         Deserialization::Deserialize(recv, opType);
60         Deserialization::Deserialize(recv, retCode);
61
62         if (counter != m_counter) {
63             return KEY_MANAGER_API_ERROR_UNKNOWN;
64         }
65
66         return retCode;
67     });
68 }
69
70 int Manager::ManagerImpl::removeKey(const Alias &alias) {
71     return try_catch([&] {
72         if (alias.empty())
73             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
74
75         MessageBuffer send, recv;
76         Serialization::Serialize(send, static_cast<int>(StorageCommand::REMOVE));
77         Serialization::Serialize(send, m_counter);
78         Serialization::Serialize(send, static_cast<int>(DBDataType::KEY_RSA_PUBLIC));
79         Serialization::Serialize(send, alias);
80
81         int retCode = sendToServer(
82             SERVICE_SOCKET_CKM_STORAGE,
83             send.Pop(),
84             recv);
85
86         if (KEY_MANAGER_API_SUCCESS != retCode) {
87             return retCode;
88         }
89
90         int command;
91         int counter;
92         int opType;
93         Deserialization::Deserialize(recv, command);
94         Deserialization::Deserialize(recv, counter);
95         Deserialization::Deserialize(recv, opType);
96         Deserialization::Deserialize(recv, retCode);
97
98         if (counter != m_counter) {
99             return KEY_MANAGER_API_ERROR_UNKNOWN;
100         }
101
102         return retCode;
103     });
104 }
105
106 int Manager::ManagerImpl::getKey(const Alias &alias, const RawData &password, Key &key) {
107     return try_catch([&] {
108         if (alias.empty())
109             return KEY_MANAGER_API_ERROR_INPUT_PARAM;
110
111         MessageBuffer send, recv;
112         Serialization::Serialize(send, static_cast<int>(StorageCommand::GET));
113         Serialization::Serialize(send, m_counter);
114         Serialization::Serialize(send, static_cast<int>(DBDataType::KEY_RSA_PUBLIC));
115         Serialization::Serialize(send, alias);
116         Serialization::Serialize(send, password);
117
118         int retCode = sendToServer(
119             SERVICE_SOCKET_CKM_STORAGE,
120             send.Pop(),
121             recv);
122
123         if (KEY_MANAGER_API_SUCCESS != retCode) {
124             return retCode;
125         }
126
127         int command;
128         int counter;
129         int opType;
130         Deserialization::Deserialize(recv, command);
131         Deserialization::Deserialize(recv, counter);
132         Deserialization::Deserialize(recv, opType);
133         Deserialization::Deserialize(recv, retCode);
134
135         if (retCode == KEY_MANAGER_API_SUCCESS)
136             Deserialization::Deserialize(recv, *(key.getImpl()));
137
138         if (counter != m_counter) {
139             return KEY_MANAGER_API_ERROR_UNKNOWN;
140         }
141
142         return retCode;
143     });
144 }
145
146 } // namespace CKM
147