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