2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
21 /* SLP library header */
25 #include "ServerSession.h"
26 #include "ServerReader.h"
27 #include "ServerChannel.h"
28 #include "APDUHelper.h"
31 namespace smartcard_service_api
34 ServerSession::ServerSession(ServerReader *reader,
35 const vector<ByteArray> &certHashes,
36 void *caller, Terminal *terminal) : SessionHelper(reader)
38 this->terminal = NULL;
42 _ERR("invalid param");
47 this->terminal = terminal;
48 this->certHashes = certHashes;
51 ServerSession::~ServerSession()
53 if (isClosed() == false)
57 const ByteArray ServerSession::getATRSync()
59 /* call get atr to terminal */
61 if (terminal != NULL) {
62 if (terminal->open() == true) {
63 int error = terminal->getATRSync(atr);
65 if (error < SCARD_ERROR_OK) {
66 _ERR("getATRSync failed, [%d]", error);
71 _ERR("terminal->open failed");
74 _ERR("terminal is null.");
81 void ServerSession::closeSync()
83 if (isClosed() == false)
90 void ServerSession::closeChannels()
94 for (i = 0; i < channels.size(); i++)
96 if (channels[i] != NULL)
97 channels[i]->closeSync();
103 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid)
105 return openBasicChannelSync(aid, (void *)NULL);
108 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, unsigned char P2)
110 return openBasicChannelSync(aid, (void *)NULL);
113 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, void *caller)
115 ServerChannel *channel = NULL;
117 AccessControlList *acList = NULL;
118 ByteArray command, result;
124 acList = ((ServerReader *)reader)->getAccessControlList();
125 if (acList == NULL) {
126 _ERR("acList is null");
131 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
132 _ERR("unauthorized access, aid : %s", aid.toString().c_str());
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);
143 if (resp.getStatus() == 0) {
144 channel = new ServerChannel(this, caller, channelID, terminal);
145 if (channel != NULL) {
146 channel->selectResponse = result;
148 channels.push_back(channel);
150 _ERR("alloc failed");
153 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
156 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
162 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length)
164 unsigned char P2 = 0x00;
165 ByteArray temp(aid, length);
167 return openBasicChannelSync(temp, P2);
170 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
172 ByteArray temp(aid, length);
174 return openBasicChannelSync(temp, P2);
177 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, void *caller)
179 ByteArray temp(aid, length);
181 return openBasicChannelSync(temp, caller);
184 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid)
187 return openLogicalChannelSync(aid, caller);
190 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, unsigned char P2)
193 return openLogicalChannelSync(aid, caller);
196 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, void *caller)
198 ServerChannel *channel = NULL;
200 AccessControlList *acList = NULL;
201 ByteArray command, result;
205 acList = ((ServerReader *)reader)->getAccessControlList();
206 if (acList == NULL) {
207 _ERR("unauthorized access, aid %s, hash %s");
212 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
213 _ERR("unauthorized access, aid : %s", aid.toString().c_str());
219 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_OPEN_LOGICAL_CHANNEL, 0, ByteArray::EMPTY);
220 rv = terminal->transmitSync(command, result);
222 if (rv == 0 && result.size() >= 2) {
223 ResponseHelper resp(result);
225 if (resp.getStatus() == 0) {
226 channelID = resp.getDataField()[0];
228 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
233 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
239 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, channelID, aid);
240 rv = terminal->transmitSync(command, result);
242 if (rv == 0 && result.size() >= 2) {
243 ResponseHelper resp(result);
245 if (resp.getStatus() == 0) {
246 channel = new ServerChannel(this, caller, channelID, terminal);
247 if (channel == NULL) {
248 _ERR("alloc failed");
253 channel->selectResponse = result;
255 channels.push_back(channel);
257 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
260 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
266 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length)
268 unsigned char P2 = 0x00;
269 ByteArray temp(aid, length);
271 return openLogicalChannelSync(temp, P2);
274 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
276 ByteArray temp(aid, length);
278 return openLogicalChannelSync(temp, P2);
281 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, void *caller)
283 ByteArray temp(aid, length);
285 return openLogicalChannelSync(temp, caller);
289 } /* namespace smartcard_service_api */