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()
58 throw(ErrorIO &, ErrorIllegalState &)
60 /* call get atr to terminal */
62 if (terminal != NULL) {
63 if (terminal->open() == true) {
64 int error = terminal->getATRSync(atr);
66 if (error < SCARD_ERROR_OK) {
67 _ERR("getATRSync failed, [%d]", error);
72 _ERR("terminal->open failed");
75 _ERR("terminal is null.");
82 void ServerSession::closeSync()
83 throw(ErrorIO &, ErrorIllegalState &)
85 if (isClosed() == false)
92 void ServerSession::closeChannels()
93 throw(ErrorIO &, ErrorIllegalState &)
97 for (i = 0; i < channels.size(); i++)
99 if (channels[i] != NULL)
100 channels[i]->closeSync();
106 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid)
107 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
109 return openBasicChannelSync(aid, (void *)NULL);
112 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, unsigned char P2)
113 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
115 return openBasicChannelSync(aid, (void *)NULL);
118 Channel *ServerSession::openBasicChannelSync(const ByteArray &aid, void *caller)
119 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
121 ServerChannel *channel = NULL;
123 AccessControlList *acList = NULL;
124 ByteArray command, result;
130 acList = ((ServerReader *)reader)->getAccessControlList();
131 if (acList == NULL) {
132 _ERR("acList is null");
137 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
138 _ERR("unauthorized access, aid : %s", aid.toString().c_str());
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);
149 if (resp.getStatus() == 0) {
150 channel = new ServerChannel(this, caller, channelID, terminal);
151 if (channel != NULL) {
152 channel->selectResponse = result;
154 channels.push_back(channel);
156 _ERR("alloc failed");
159 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
162 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
168 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length)
169 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
171 unsigned char P2 = 0x00;
172 ByteArray temp(aid, length);
174 return openBasicChannelSync(temp, P2);
177 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
178 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
180 ByteArray temp(aid, length);
182 return openBasicChannelSync(temp, P2);
185 Channel *ServerSession::openBasicChannelSync(const unsigned char *aid, unsigned int length, void *caller)
186 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
188 ByteArray temp(aid, length);
190 return openBasicChannelSync(temp, caller);
193 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid)
194 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
197 return openLogicalChannelSync(aid, caller);
200 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, unsigned char P2)
201 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
204 return openLogicalChannelSync(aid, caller);
207 Channel *ServerSession::openLogicalChannelSync(const ByteArray &aid, void *caller)
208 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
210 ServerChannel *channel = NULL;
212 AccessControlList *acList = NULL;
213 ByteArray command, result;
217 acList = ((ServerReader *)reader)->getAccessControlList();
218 if (acList == NULL) {
219 _ERR("unauthorized access, aid %s, hash %s");
224 if (acList->isAuthorizedAccess(aid, certHashes) == false) {
225 _ERR("unauthorized access, aid : %s", aid.toString().c_str());
231 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_OPEN_LOGICAL_CHANNEL, 0, ByteArray::EMPTY);
232 rv = terminal->transmitSync(command, result);
234 if (rv == 0 && result.size() >= 2) {
235 ResponseHelper resp(result);
237 if (resp.getStatus() == 0) {
238 channelID = resp.getDataField()[0];
240 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
245 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
251 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_SELECT_BY_DF_NAME, channelID, aid);
252 rv = terminal->transmitSync(command, result);
254 if (rv == 0 && result.size() >= 2) {
255 ResponseHelper resp(result);
257 if (resp.getStatus() == 0) {
258 channel = new ServerChannel(this, caller, channelID, terminal);
259 if (channel == NULL) {
260 _ERR("alloc failed");
265 channel->selectResponse = result;
267 channels.push_back(channel);
269 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
272 _ERR("select apdu is failed, rv [%d], length [%d]", rv, result.size());
278 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length)
279 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
281 unsigned char P2 = 0x00;
282 ByteArray temp(aid, length);
284 return openLogicalChannelSync(temp, P2);
287 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, unsigned char P2)
288 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
290 ByteArray temp(aid, length);
292 return openLogicalChannelSync(temp, P2);
295 Channel *ServerSession::openLogicalChannelSync(const unsigned char *aid, unsigned int length, void *caller)
296 throw(ErrorIO &, ErrorIllegalState &, ErrorIllegalParameter &, ErrorSecurity &)
298 ByteArray temp(aid, length);
300 return openLogicalChannelSync(temp, caller);
304 } /* namespace smartcard_service_api */