CKMC API: Add option to list aliases with information about password protection
[platform/core/security/key-manager.git] / src / manager / client / client-common.h
1 /*
2  *  Copyright (c) 2000 - 2019 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-common.h
18  * @author      Bartlomiej Grzelewski (b.grzelewski@samsung.com)
19  * @version     1.0
20  * @brief       This file contains implementation of common types
21  *              used in Central Key Manager.
22  */
23
24 #ifndef _KEY_MANAGER_CLIENT_
25 #define _KEY_MANAGER_CLIENT_
26
27 #include <unistd.h>
28
29 #include <vector>
30 #include <functional>
31
32 #include <noncopyable.h>
33 #include <ckm/ckm-type.h>
34 #include <ckmc/ckmc-error.h>
35 #include <message-buffer.h>
36 #include <protocols.h>
37
38 #define EXCEPTION_GUARD_START_CPPAPI return CKM::try_catch([&]()->int {
39 #define EXCEPTION_GUARD_START_CAPI   return CKM::try_catch_enclosure([&]()->int {
40 #define EXCEPTION_GUARD_END          });
41
42 extern "C" {
43         struct msghdr;
44
45         struct ckmc_alias_info_s {
46                 char* alias;
47                 bool is_password_protected;
48         };
49
50 }
51
52 namespace CKM {
53
54 class AliasSupport {
55 public:
56         AliasSupport(const Alias &alias);
57
58         const ClientId &getOwner() const;
59         const Name &getName() const;
60         bool isOwnerEmpty() const;
61
62         static Alias merge(const ClientId &owner, const Name &alias);
63
64 private:
65         Name m_name;
66         ClientId m_owner;
67 };
68
69 class SockRAII {
70 public:
71         SockRAII();
72
73         NONCOPYABLE(SockRAII);
74
75         virtual ~SockRAII();
76
77         int connect(const char *interface);
78         void disconnect();
79         bool isConnected() const;
80         int get() const;
81         int waitForSocket(int event, int timeout);
82
83 protected:
84         int connectWrapper(int socket, const char *interface);
85         int m_sock;
86 };
87
88 class ServiceConnection {
89 public:
90         ServiceConnection(const char *service_interface);
91
92         // roundtrip: send and receive
93         int processRequest(const CKM::RawBuffer &send_buf,
94                                            CKM::MessageBuffer &recv_buf);
95
96         // blocking
97         int send(const CKM::RawBuffer &send_buf);
98         int receive(CKM::MessageBuffer &recv_buf);
99
100         virtual ~ServiceConnection();
101
102 protected:
103         int prepareConnection();
104
105         SockRAII m_socket;
106         std::string m_serviceInterface;
107 };
108
109 /*
110  * Decorator function that performs frequently repeated exception handling in
111  * SS client API functions. Accepts lambda expression as an argument.
112  */
113 // for c++ api layer
114 int try_catch(const std::function<int()> &func);
115 // for c api layer
116 int try_catch_enclosure(const std::function<int()> &func);
117
118 // for c++ async api layer
119 void try_catch_async(const std::function<void()> &func,
120                                          const std::function<void(int)> &error);
121
122 } // namespace CKM
123
124 #endif // _KEY_MANAGER_CLIENT_