Added Gtest for smartcard-service
[platform/core/connectivity/smartcard-service.git] / server / ServerSession.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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 /* standard library header */
18 #include <stdio.h>
19 #include <dlfcn.h>
20
21 /* SLP library header */
22
23 /* local header */
24 #include "Debug.h"
25 #include "ServerSession.h"
26 #include "ServerReader.h"
27 #include "ServerChannel.h"
28 #include "APDUHelper.h"
29 #include "GPACE.h"
30
31 namespace smartcard_service_api
32 {
33         /* LCOV_EXCL_START */
34         ServerSession::ServerSession(ServerReader *reader,
35                 const vector<ByteArray> &certHashes,
36                 void *caller, Terminal *terminal) : SessionHelper(reader)
37         {
38                 this->terminal = NULL;
39
40                 if (terminal == NULL)
41                 {
42                         _ERR("invalid param");
43
44                         return;
45                 }
46
47                 this->terminal = terminal;
48                 this->certHashes = certHashes;
49         }
50
51         ServerSession::~ServerSession()
52         {
53                 if (isClosed() == false)
54                         closeSync();
55         }
56
57         const ByteArray ServerSession::getATRSync()
58                 throw(ErrorIO &, ErrorIllegalState &)
59         {
60                 /* call get atr to terminal */
61                 if (atr.isEmpty()) {
62                         if (terminal != NULL) {
63                                 if (terminal->open() == true) {
64                                         int error = terminal->getATRSync(atr);
65
66                                         if (error < SCARD_ERROR_OK) {
67                                                 _ERR("getATRSync failed, [%d]", error);
68                                         }
69
70                                         terminal->close();
71                                 } else {
72                                         _ERR("terminal->open failed");
73                                 }
74                         } else {
75                                 _ERR("terminal is null.");
76                         }
77                 }
78
79                 return atr;
80         }
81
82         void ServerSession::closeSync()
83                 throw(ErrorIO &, ErrorIllegalState &)
84         {
85                 if (isClosed() == false)
86                 {
87                         closed = true;
88                         closeChannels();
89                 }
90         }
91
92         void ServerSession::closeChannels()
93                 throw(ErrorIO &, ErrorIllegalState &)
94         {
95                 size_t i;
96
97                 for (i = 0; i < channels.size(); i++)
98                 {
99                         if (channels[i] != NULL)
100                                 channels[i]->closeSync();
101                 }
102
103                 channels.clear();
104         }
105
106         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid)
107                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
108         {
109                 return openBasicChannelSync(aid, (void *)NULL);
110         }
111
112         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, unsigned char P2)
113                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
114         {
115                 return openBasicChannelSync(aid, (void *)NULL);
116         }
117
118         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, void *caller)
119                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
120         {
121                 ServerChannel *channel = NULL;
122 #if 0
123                 AccessControlList *acList = NULL;
124                 ByteArray command, result;
125                 int channelID = 0;
126                 int rv = 0;
127
128                 _BEGIN();
129
130                 acList = ((ServerReader *)reader)->getAccessControlList();
131                 if (acList == NULL) {
132                         _ERR("acList is null");
133
134                         return channel;
135                 }
136
137                 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
138                         _ERR("unauthorized access, aid : %s", aid.toString().c_str());
139
140                         return channel;
141                 }
142
143                 /* select aid */
144                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, channelID, aid);
145                 rv = terminal->transmitSync(command, result);
146                 if (rv == 0 && result.size() >= 2) {
147                         ResponseHelper resp(result);
148
149                         if (resp.getStatus() == 0) {
150                                 channel = new ServerChannel(this, caller, channelID, terminal);
151                                 if (channel != NULL) {
152                                         channel->selectResponse = result;
153
154                                         channels.push_back(channel);
155                                 } else {
156                                         _ERR("alloc failed");
157                                 }
158                         } else {
159                                 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
160                         }
161                 } else {
162                         _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
163                 }
164 #endif
165                 return channel;
166         }
167
168         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length)
169                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
170         {
171                 unsigned char P2 = 0x00;
172                 ByteArray temp(aid, length);
173
174                 return openBasicChannelSync(temp, P2);
175         }
176
177         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
178                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
179         {
180                 ByteArray temp(aid, length);
181
182                 return openBasicChannelSync(temp, P2);
183         }
184
185         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, void *caller)
186                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
187         {
188                 ByteArray temp(aid, length);
189
190                 return openBasicChannelSync(temp, caller);
191         }
192
193         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid)
194                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
195         {
196                 void* caller = NULL;
197                 return openLogicalChannelSync(aid, caller);
198         }
199
200         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, unsigned char P2)
201                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
202         {
203                 void* caller = NULL;
204                 return openLogicalChannelSync(aid, caller);
205         }
206
207         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, void *caller)
208                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
209         {
210                 ServerChannel *channel = NULL;
211 #if 0
212                 AccessControlList *acList = NULL;
213                 ByteArray command, result;
214                 int channelID = 1;
215                 int rv;
216
217                 acList = ((ServerReader *)reader)->getAccessControlList();
218                 if (acList == NULL) {
219                         _ERR("unauthorized access, aid %s, hash %s");
220
221                         return channel;
222                 }
223
224                 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
225                         _ERR("unauthorized access, aid : %s", aid.toString().c_str());
226
227                         return channel;
228                 }
229
230                 /* open channel */
231                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_OPEN_LOGICAL_CHANNEL, 0, ByteArray::EMPTY);
232                 rv = terminal->transmitSync(command, result);
233
234                 if (rv == 0 && result.size() >= 2) {
235                         ResponseHelper resp(result);
236
237                         if (resp.getStatus() == 0) {
238                                 channelID = resp.getDataField()[0];
239                         } else {
240                                 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
241
242                                 return channel;
243                         }
244                 } else {
245                         _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
246
247                         return channel;
248                 }
249
250                 /* select aid */
251                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, channelID, aid);
252                 rv = terminal->transmitSync(command, result);
253
254                 if (rv == 0 && result.size() >= 2) {
255                         ResponseHelper resp(result);
256
257                         if (resp.getStatus() == 0) {
258                                 channel = new ServerChannel(this, caller, channelID, terminal);
259                                 if (channel == NULL) {
260                                         _ERR("alloc failed");
261
262                                         return NULL;
263                                 }
264
265                                 channel->selectResponse = result;
266
267                                 channels.push_back(channel);
268                         } else {
269                                 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
270                         }
271                 } else {
272                         _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
273                 }
274 #endif
275                 return channel;
276         }
277
278         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length)
279                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
280         {
281                 unsigned char P2 = 0x00;
282                 ByteArray temp(aid, length);
283
284                 return openLogicalChannelSync(temp, P2);
285         }
286
287         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
288                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
289         {
290                 ByteArray temp(aid, length);
291
292                 return openLogicalChannelSync(temp, P2);
293         }
294
295         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, void *caller)
296                 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
297         {
298                 ByteArray temp(aid, length);
299
300                 return openLogicalChannelSync(temp, caller);
301         }
302
303         /* LCOV_EXCL_STOP */
304 } /* namespace smartcard_service_api */