Implementation of Key class.
[platform/core/security/key-manager.git] / src / manager / client / client-manager-impl.h
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 #pragma once
22
23 #include <ckm/key-manager.h>
24
25 namespace CKM {
26
27 class Manager::ManagerImpl {
28 public:
29     ManagerImpl()
30       : m_counter(0)
31     {}
32     virtual ~ManagerImpl(){}
33
34     int saveKey(const Alias &alias, const Key &key, const Policy &policy) {
35         m_counter++;
36
37         return try_catch([&] {
38             if (user.empty() || key.empty())
39                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
40
41             Message send, recv;
42             Serialization::Serialization(send, static_cast<int>(StorageCommand::SAVE_KEY));
43             Serialization::Serialize(send, m_counter);
44             Serialization::Serialize(send, alias);
45             Serialization::Serialize(send, key.getImpl());
46             Serialization::Serialize(send, policy);
47
48             int retCode = sendToServer(
49                 SERVICE_SOCKET_CKM_STORAGE,
50                 send.Pop(),
51                 recv);
52
53             if (KEY_MANAGER_API_SUCCESS != retCode) {
54                 return retCode;
55             }
56
57             int id;
58             Deserialization::Deserialize(recv, id);
59             Deserialization::Deserialize(recv, retCode);
60
61             if (id != m_counter) {
62                 return KEY_MANAGER_API_ERROR_UNKNOWN;
63             }
64
65             return retCode;
66         });
67     }
68
69     int removeKey(const Alias &alias) {
70         return try_catch([&] {
71             if (user.empty() || key.empty())
72                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
73
74             Message send, recv;
75             Serialization::Serialization(send, static_cast<int>(StorageCommand::REMOVE_KEY));
76             Serialization::Serialize(send, m_counter);
77             Serialization::Serialize(send, alias);
78
79             int retCode = sendToServer(
80                 SERVICE_SOCKET_CKM_STORAGE,
81                 send.Pop(),
82                 recv);
83
84             if (KEY_MANAGER_API_SUCCESS != retCode) {
85                 return retCode;
86             }
87
88             int id;
89             Deserialization::Deserialize(recv, id);
90             Deserialization::Deserialize(recv, retCode);
91
92             if (id != m_counter) {
93                 return KEY_MANAGER_API_ERROR_UNKNOWN;
94             }
95
96             return retCode;
97     }
98
99     int getKey(const Alias &alias, const RawData &password, Key &key) {
100         return try_catch([&] {
101             if (user.empty() || key.empty())
102                 return KEY_MANAGER_API_ERROR_INPUT_PARAM;
103
104             Message send, recv;
105             Serialization::Serialization(send, static_cast<int>(StorageCommand::REMOVE_KEY));
106             Serialization::Serialize(send, m_counter);
107             Serialization::Serialize(send, alias);
108             Serialization::Serialize(send, password);
109             Serialization::Serialize(send, key.getImpl());
110
111             int retCode = sendToServer(
112                 SERVICE_SOCKET_CKM_STORAGE,
113                 send.Pop(),
114                 recv);
115
116             if (KEY_MANAGER_API_SUCCESS != retCode) {
117                 return retCode;
118             }
119
120             int id;
121             Deserialization::Deserialize(recv, id);
122             Deserialization::Deserialize(recv, retCode);
123
124             if (id != m_counter) {
125                 return KEY_MANAGER_API_ERROR_UNKNOWN;
126             }
127
128             return retCode;
129     }
130
131 private:
132     int m_counter;
133 };
134
135