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
33 ServerSession::ServerSession(ServerReader *reader,
34 const vector<ByteArray> &certHashes,
35 void *caller, Terminal *terminal) : SessionHelper(reader)
37 this->terminal = NULL;
41 _ERR("invalid param");
46 this->terminal = terminal;
47 this->certHashes = certHashes;
50 ServerSession::~ServerSession()
52 if (isClosed() == false)
56 const ByteArray ServerSession::getATRSync()
57 throw(ErrorIO &, ErrorIllegalState &)
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()
82 throw(ErrorIO &, ErrorIllegalState &)
84 if (isClosed() == false)
91 void ServerSession::closeChannels()
92 throw(ErrorIO &, ErrorIllegalState &)
96 for (i = 0; i < channels.size(); i++)
98 if (channels[i] != NULL)
99 channels[i]->closeSync();
105 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid)
106 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
108 return openBasicChannelSync(aid, (void *)NULL);
111 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, unsigned char P2)
112 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
114 return openBasicChannelSync(aid, (void *)NULL);
117 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, void *caller)
118 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
120 ServerChannel *channel = NULL;
122 AccessControlList *acList = NULL;
123 ByteArray command, result;
129 acList = ((ServerReader *)reader)->getAccessControlList();
130 if (acList == NULL) {
131 _ERR("acList is null");
136 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
137 _ERR("unauthorized access, aid : %s", aid.toString().c_str());
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);
148 if (resp.getStatus() == 0) {
149 channel = new ServerChannel(this, caller, channelID, terminal);
150 if (channel != NULL) {
151 channel->selectResponse = result;
153 channels.push_back(channel);
155 _ERR("alloc failed");
158 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
161 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
167 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length)
168 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
170 unsigned char P2 = 0x00;
171 ByteArray temp(aid, length);
173 return openBasicChannelSync(temp, P2);
176 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
177 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
179 ByteArray temp(aid, length);
181 return openBasicChannelSync(temp, P2);
184 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, void *caller)
185 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
187 ByteArray temp(aid, length);
189 return openBasicChannelSync(temp, caller);
192 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid)
193 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
196 return openLogicalChannelSync(aid, caller);
199 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, unsigned char P2)
200 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
203 return openLogicalChannelSync(aid, caller);
206 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, void *caller)
207 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
209 ServerChannel *channel = NULL;
211 AccessControlList *acList = NULL;
212 ByteArray command, result;
216 acList = ((ServerReader *)reader)->getAccessControlList();
217 if (acList == NULL) {
218 _ERR("unauthorized access, aid %s, hash %s");
223 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
224 _ERR("unauthorized access, aid : %s", aid.toString().c_str());
230 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_OPEN_LOGICAL_CHANNEL, 0, ByteArray::EMPTY);
231 rv = terminal->transmitSync(command, result);
233 if (rv == 0 && result.size() >= 2) {
234 ResponseHelper resp(result);
236 if (resp.getStatus() == 0) {
237 channelID = resp.getDataField()[0];
239 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
244 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
250 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, channelID, aid);
251 rv = terminal->transmitSync(command, result);
253 if (rv == 0 && result.size() >= 2) {
254 ResponseHelper resp(result);
256 if (resp.getStatus() == 0) {
257 channel = new ServerChannel(this, caller, channelID, terminal);
258 if (channel == NULL) {
259 _ERR("alloc failed");
264 channel->selectResponse = result;
266 channels.push_back(channel);
268 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
271 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
277 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length)
278 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
280 unsigned char P2 = 0x00;
281 ByteArray temp(aid, length);
283 return openLogicalChannelSync(temp, P2);
286 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
287 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
289 ByteArray temp(aid, length);
291 return openLogicalChannelSync(temp, P2);
294 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, void *caller)
295 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
297 ByteArray temp(aid, length);
299 return openLogicalChannelSync(temp, caller);
302 } /* namespace smartcard_service_api */