Remove gcov rpm package
[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         {
59                 /* call get atr to terminal */
60                 if (atr.isEmpty()) {
61                         if (terminal != NULL) {
62                                 if (terminal->open() == true) {
63                                         int error = terminal->getATRSync(atr);
64
65                                         if (error < SCARD_ERROR_OK) {
66                                                 _ERR("getATRSync failed, [%d]", error);
67                                         }
68
69                                         terminal->close();
70                                 } else {
71                                         _ERR("terminal->open failed");
72                                 }
73                         } else {
74                                 _ERR("terminal is null.");
75                         }
76                 }
77
78                 return atr;
79         }
80
81         void ServerSession::closeSync()
82         {
83                 if (isClosed() == false)
84                 {
85                         closed = true;
86                         closeChannels();
87                 }
88         }
89
90         void ServerSession::closeChannels()
91         {
92                 size_t i;
93
94                 for (i = 0; i < channels.size(); i++)
95                 {
96                         if (channels[i] != NULL)
97                                 channels[i]->closeSync();
98                 }
99
100                 channels.clear();
101         }
102
103         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid)
104         {
105                 return openBasicChannelSync(aid, (void *)NULL);
106         }
107
108         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, unsigned char P2)
109         {
110                 return openBasicChannelSync(aid, (void *)NULL);
111         }
112
113         Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, void *caller)
114         {
115                 ServerChannel *channel = NULL;
116 #if 0
117                 AccessControlList *acList = NULL;
118                 ByteArray command, result;
119                 int channelID = 0;
120                 int rv = 0;
121
122                 _BEGIN();
123
124                 acList = ((ServerReader *)reader)->getAccessControlList();
125                 if (acList == NULL) {
126                         _ERR("acList is null");
127
128                         return channel;
129                 }
130
131                 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
132                         _ERR("unauthorized access, aid : %s", aid.toString().c_str());
133
134                         return channel;
135                 }
136
137                 /* select aid */
138                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, channelID, aid);
139                 rv = terminal->transmitSync(command, result);
140                 if (rv == 0 && result.size() >= 2) {
141                         ResponseHelper resp(result);
142
143                         if (resp.getStatus() == 0) {
144                                 channel = new ServerChannel(this, caller, channelID, terminal);
145                                 if (channel != NULL) {
146                                         channel->selectResponse = result;
147
148                                         channels.push_back(channel);
149                                 } else {
150                                         _ERR("alloc failed");
151                                 }
152                         } else {
153                                 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
154                         }
155                 } else {
156                         _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
157                 }
158 #endif
159                 return channel;
160         }
161
162         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length)
163         {
164                 unsigned char P2 = 0x00;
165                 ByteArray temp(aid, length);
166
167                 return openBasicChannelSync(temp, P2);
168         }
169
170         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
171         {
172                 ByteArray temp(aid, length);
173
174                 return openBasicChannelSync(temp, P2);
175         }
176
177         Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, void *caller)
178         {
179                 ByteArray temp(aid, length);
180
181                 return openBasicChannelSync(temp, caller);
182         }
183
184         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid)
185         {
186                 void* caller = NULL;
187                 return openLogicalChannelSync(aid, caller);
188         }
189
190         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, unsigned char P2)
191         {
192                 void* caller = NULL;
193                 return openLogicalChannelSync(aid, caller);
194         }
195
196         Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, void *caller)
197         {
198                 ServerChannel *channel = NULL;
199 #if 0
200                 AccessControlList *acList = NULL;
201                 ByteArray command, result;
202                 int channelID = 1;
203                 int rv;
204
205                 acList = ((ServerReader *)reader)->getAccessControlList();
206                 if (acList == NULL) {
207                         _ERR("unauthorized access, aid %s, hash %s");
208
209                         return channel;
210                 }
211
212                 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
213                         _ERR("unauthorized access, aid : %s", aid.toString().c_str());
214
215                         return channel;
216                 }
217
218                 /* open channel */
219                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_OPEN_LOGICAL_CHANNEL, 0, ByteArray::EMPTY);
220                 rv = terminal->transmitSync(command, result);
221
222                 if (rv == 0 && result.size() >= 2) {
223                         ResponseHelper resp(result);
224
225                         if (resp.getStatus() == 0) {
226                                 channelID = resp.getDataField()[0];
227                         } else {
228                                 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
229
230                                 return channel;
231                         }
232                 } else {
233                         _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
234
235                         return channel;
236                 }
237
238                 /* select aid */
239                 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, channelID, aid);
240                 rv = terminal->transmitSync(command, result);
241
242                 if (rv == 0 && result.size() >= 2) {
243                         ResponseHelper resp(result);
244
245                         if (resp.getStatus() == 0) {
246                                 channel = new ServerChannel(this, caller, channelID, terminal);
247                                 if (channel == NULL) {
248                                         _ERR("alloc failed");
249
250                                         return NULL;
251                                 }
252
253                                 channel->selectResponse = result;
254
255                                 channels.push_back(channel);
256                         } else {
257                                 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
258                         }
259                 } else {
260                         _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
261                 }
262 #endif
263                 return channel;
264         }
265
266         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length)
267         {
268                 unsigned char P2 = 0x00;
269                 ByteArray temp(aid, length);
270
271                 return openLogicalChannelSync(temp, P2);
272         }
273
274         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
275         {
276                 ByteArray temp(aid, length);
277
278                 return openLogicalChannelSync(temp, P2);
279         }
280
281         Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, void *caller)
282         {
283                 ByteArray temp(aid, length);
284
285                 return openLogicalChannelSync(temp, caller);
286         }
287
288         /* LCOV_EXCL_STOP */
289 } /* namespace smartcard_service_api */