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 */
24 /* SLP library header */
28 #include "ServerResource.h"
29 #include "TerminalInterface.h"
30 #include "APDUHelper.h"
31 #include "SignatureHelper.h"
35 #include "ServerGDBus.h"
39 #define EXTERN_API __attribute__((visibility("default")))
44 namespace smartcard_service_api
46 unsigned int IntegerHandle::newHandle = 0;
47 set<unsigned int> IntegerHandle::setHandles;
48 PMutex IntegerHandle::mutexLock;
50 unsigned int IntegerHandle::assignHandle()
54 pair<set<unsigned int>::iterator, bool> result;
59 if (newHandle == (unsigned int)-1)
64 result = setHandles.insert(newHandle);
67 while (!result.second);
70 _DBG("assign handle : newHandle [%d]", newHandle);
75 void IntegerHandle::releaseHandle(unsigned int handle)
77 _DBG("will be released : Handle [%d]", handle);
81 setHandles.erase(handle);
85 #define OMAPI_SE_PATH "/usr/lib/se"
87 ServerResource::ServerResource()
88 : mainLoop(NULL), seLoaded(false)
92 serverIPC = ServerIPC::getInstance();
93 serverDispatcher = ServerDispatcher::getInstance();
98 ServerResource::~ServerResource()
102 ServerResource &ServerResource::getInstance()
104 static ServerResource serverResource;
106 return serverResource;
110 bool ServerResource::createClient(const char *name, pid_t pid)
114 if (getClient(name) == NULL)
116 ClientInstance *instance = new ClientInstance(name, pid);
117 if (instance != NULL)
119 mapClients.insert(make_pair(name, instance));
124 _ERR("alloc failed");
129 _ERR("client already exist, name [%s]", name);
135 ClientInstance *ServerResource::getClient(const char *name)
137 ClientInstance *result = NULL;
138 map<string, ClientInstance *>::iterator item;
140 if ((item = mapClients.find(name)) != mapClients.end())
142 result = item->second;
148 void ServerResource::removeClient(const char *name)
150 map<string, ClientInstance *>::iterator item;
152 if ((item = mapClients.find(name)) != mapClients.end())
155 mapClients.erase(item);
159 _DBG("client removed already, name [%s]", name);
163 void ServerResource::removeClients()
165 map<string, ClientInstance *>::iterator item;
167 for (item = mapClients.begin(); item != mapClients.end(); item++)
175 int ServerResource::getClientCount()
177 return (int)mapClients.size();
180 ServiceInstance *ServerResource::createService(const char *name)
182 ServiceInstance *result = NULL;
183 ClientInstance *instance = NULL;
185 if ((instance = getClient(name)) != NULL)
187 if ((result = instance->createService()) == NULL)
189 _ERR("ClientInstance::createService failed [%d]", name);
194 _ERR("client doesn't exist, name [%s]", name);
200 ServiceInstance *ServerResource::getService(const char *name, unsigned int handle)
202 ServiceInstance *result = NULL;
203 ClientInstance *instance = NULL;
205 if ((instance = getClient(name)) != NULL)
207 result = instance->getService(handle);
211 _ERR("client doesn't exist, name [%s]", name);
217 void ServerResource::removeService(const char *name, unsigned int handle)
219 ClientInstance *instance = NULL;
221 if ((instance = getClient(name)) != NULL)
223 instance->removeService(handle);
227 _ERR("client doesn't exist, name [%s]", name);
231 void ServerResource::removeServices(const char *name)
233 ClientInstance *instance = NULL;
235 if ((instance = getClient(name)) != NULL)
237 instance->removeServices();
241 _ERR("client doesn't exist, name [%s]", name);
245 unsigned int ServerResource::createSession(const char *name, unsigned int handle, unsigned int readerID, vector<ByteArray> &certHashes, void *caller)
247 unsigned int result = -1;
248 Terminal *temp = NULL;
249 ServiceInstance *instance = NULL;
251 if ((instance = getService(name, handle)) != NULL)
253 if ((temp = getTerminalByReaderID(readerID)) != NULL)
255 result = instance->openSession(temp, certHashes, caller);
260 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
266 ServerSession *ServerResource::getSession(const char *name, unsigned int handle, unsigned int sessionID)
268 ServerSession *result = NULL;
269 ServiceInstance *instance = NULL;
271 if ((instance = getService(name, handle)) != NULL)
273 result = instance->getSession(sessionID);
277 _ERR("Session doesn't exist : name [%s], handle [%d], handle [%d]", name, handle, sessionID);
283 bool ServerResource::isValidSessionHandle(const char *name, unsigned int handle, unsigned int session)
285 ServiceInstance *instance = NULL;
287 return (((instance = getService(name, handle)) != NULL) && (instance->isVaildSessionHandle(session)));
290 unsigned int ServerResource::getChannelCount(const char *name, unsigned int handle, unsigned int sessionID)
292 unsigned int result = -1;
293 ServiceInstance *instance = NULL;
295 if ((instance = getService(name, handle)) != NULL)
297 result = instance->getChannelCountBySession(sessionID);
301 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
307 void ServerResource::removeSession(const char *name, unsigned int handle, unsigned int sessionID)
309 ServiceInstance *instance = NULL;
311 if ((instance = getService(name, handle)) != NULL)
313 instance->closeSession(sessionID);
317 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
321 unsigned int ServerResource::createChannel(const char *name, unsigned int handle, unsigned int sessionID, int channelType, ByteArray aid)
322 throw(ExceptionBase &)
324 unsigned int result = -1;
325 ServiceInstance *service = NULL;
327 if ((service = getService(name, handle)) != NULL)
329 if (service->isVaildSessionHandle(sessionID) == true)
331 ServerSession *session = NULL;
332 Terminal *terminal = NULL;
334 terminal = service->getTerminal(sessionID);
335 session = service->getSession(sessionID);
336 if (terminal != NULL && session != NULL)
338 result = _createChannel(terminal, service, channelType, sessionID, aid);
339 if (result == IntegerHandle::INVALID_HANDLE)
341 _ERR("create channel failed [%d]", sessionID);
346 _ERR("session is invalid [%d]", sessionID);
347 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
352 _ERR("session is invalid [%d]", sessionID);
353 throw ExceptionBase(SCARD_ERROR_ILLEGAL_STATE);
358 _ERR("getService is failed, name [%s], handle [%d]", name, handle);
359 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
365 Channel *ServerResource::getChannel(const char *name, unsigned int handle, unsigned int channelID)
367 Channel *result = NULL;
368 ServiceInstance *instance = NULL;
370 if ((instance = getService(name, handle)) != NULL)
372 result = instance->getChannel(channelID);
376 _ERR("Channel doesn't exist : name [%s], handle [%d], handle [%d]", name, handle, channelID);
382 void ServerResource::removeChannel(const char *name, unsigned int handle, unsigned int channelID)
384 ServiceInstance *instance = NULL;
386 if ((instance = getService(name, handle)) != NULL)
388 instance->closeChannel(channelID);
392 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
396 bool ServerResource::createClient(void *ioChannel, int socket, int watchID, int state, int pid)
400 if (getClient(socket) == NULL)
402 ClientInstance *instance = new ClientInstance(ioChannel, socket, watchID, state, pid);
403 if (instance != NULL)
405 mapClients.insert(make_pair(socket, instance));
410 _ERR("alloc failed");
415 _ERR("client already exist, socket[%d]", socket);
421 bool ServerResource::createClient(int pid)
425 if (getClient(pid) == NULL)
427 ClientInstance *instance = new ClientInstance(pid);
428 if (instance != NULL)
430 mapClients.insert(make_pair(pid, instance));
435 _ERR("alloc failed");
440 _ERR("client already exist, pid[%d]", pid);
446 ClientInstance *ServerResource::getClient(int socket)
448 ClientInstance *result = NULL;
449 map<int, ClientInstance *>::iterator item;
451 if ((item = mapClients.find(socket)) != mapClients.end())
453 result = item->second;
459 void ServerResource::setPID(int socket, int pid)
461 map<int, ClientInstance *>::iterator item;
463 if ((item = mapClients.find(socket)) != mapClients.end())
465 if (item->second->getPID() < 0)
466 item->second->setPID(pid);
470 void ServerResource::removeClient(int socket)
472 map<int, ClientInstance *>::iterator item;
474 if ((item = mapClients.find(socket)) != mapClients.end())
477 ServerIPC::getInstance()->releaseClient(item->second->getIOChannel(), item->second->getSocket(), item->second->getWatchID());
480 mapClients.erase(item);
484 _DBG("client removed already [%d]", socket);
488 void ServerResource::removeClients()
490 map<int, ClientInstance *>::iterator item;
492 for (item = mapClients.begin(); item != mapClients.end(); item++)
495 ServerIPC::getInstance()->releaseClient(item->second->getIOChannel(), item->second->getSocket(), item->second->getWatchID());
503 int ServerResource::getClientCount()
505 return (int)mapClients.size();
508 ServiceInstance *ServerResource::createService(int socket)
510 ServiceInstance *result = NULL;
511 ClientInstance *instance = NULL;
513 if ((instance = getClient(socket)) != NULL)
515 if ((result = instance->createService()) == NULL)
517 _ERR("ClientInstance::createService failed [%d]", socket);
522 _ERR("client doesn't exist [%d]", socket);
528 ServiceInstance *ServerResource::getService(int socket, unsigned int handle)
530 ServiceInstance *result = NULL;
531 ClientInstance *instance = NULL;
533 if ((instance = getClient(socket)) != NULL)
535 result = instance->getService(handle);
539 _ERR("client doesn't exist [%d]", socket);
545 void ServerResource::removeService(int socket, unsigned int handle)
547 ClientInstance *instance = NULL;
549 if ((instance = getClient(socket)) != NULL)
551 instance->removeService(handle);
555 _ERR("client doesn't exist [%d]", socket);
559 void ServerResource::removeServices(int socket)
561 ClientInstance *instance = NULL;
563 if ((instance = getClient(socket)) != NULL)
565 instance->removeServices();
569 _ERR("client doesn't exist [%d]", socket);
573 unsigned int ServerResource::createSession(int socket, unsigned int handle, unsigned int readerID, vector<ByteArray> &certHashes, void *caller)
575 unsigned int result = -1;
576 Terminal *temp = NULL;
577 ServiceInstance *instance = NULL;
579 if ((instance = getService(socket, handle)) != NULL)
581 if ((temp = getTerminalByReaderID(readerID)) != NULL)
583 result = instance->openSession(temp, certHashes, caller);
588 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
594 ServerSession *ServerResource::getSession(int socket, unsigned int handle, unsigned int sessionID)
596 ServerSession *result = NULL;
597 ServiceInstance *instance = NULL;
599 if ((instance = getService(socket, handle)) != NULL)
601 result = instance->getSession(sessionID);
605 _ERR("Session doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, sessionID);
611 bool ServerResource::isValidSessionHandle(int socket, unsigned int handle, unsigned int session)
613 ServiceInstance *instance = NULL;
615 return (((instance = getService(socket, handle)) != NULL) && (instance->isVaildSessionHandle(session)));
618 unsigned int ServerResource::getChannelCount(int socket, unsigned int handle, unsigned int sessionID)
620 unsigned int result = -1;
621 ServiceInstance *instance = NULL;
623 if ((instance = getService(socket, handle)) != NULL)
625 result = instance->getChannelCountBySession(sessionID);
629 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
635 void ServerResource::removeSession(int socket, unsigned int handle, unsigned int sessionID)
637 ServiceInstance *instance = NULL;
639 if ((instance = getService(socket, handle)) != NULL)
641 instance->closeSession(sessionID);
645 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
649 unsigned int ServerResource::createChannel(int socket, unsigned int handle, unsigned int sessionID, int channelType, ByteArray aid)
650 throw(ExceptionBase &)
652 unsigned int result = -1;
653 ServiceInstance *service = NULL;
655 if ((service = getService(socket, handle)) != NULL)
657 if (service->isVaildSessionHandle(sessionID) == true)
659 ServerSession *session = NULL;
660 Terminal *terminal = NULL;
662 terminal = service->getTerminal(sessionID);
663 session = service->getSession(sessionID);
664 if (terminal != NULL && session != NULL)
666 result = _createChannel(terminal, service, channelType, sessionID, aid);
667 if (result == IntegerHandle::INVALID_HANDLE)
669 _ERR("create channel failed [%d]", sessionID);
674 _ERR("session is invalid [%d]", sessionID);
675 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
680 _ERR("session is invalid [%d]", sessionID);
681 throw ExceptionBase(SCARD_ERROR_ILLEGAL_STATE);
686 _ERR("getService is failed [%d] [%d]", socket, handle);
687 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
693 Channel *ServerResource::getChannel(int socket, unsigned int handle, unsigned int channelID)
695 Channel *result = NULL;
696 ServiceInstance *instance = NULL;
698 if ((instance = getService(socket, handle)) != NULL)
700 result = instance->getChannel(channelID);
704 _ERR("Channel doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, channelID);
710 void ServerResource::removeChannel(int socket, unsigned int handle, unsigned int channelID)
712 ServiceInstance *instance = NULL;
714 if ((instance = getService(socket, handle)) != NULL)
716 instance->closeChannel(channelID);
720 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
724 Terminal *ServerResource::getTerminal(unsigned int terminalID)
726 Terminal *result = NULL;
727 map<unsigned int, Terminal *>::iterator item;
729 if ((item = mapTerminals.find(terminalID)) != mapTerminals.end())
731 result = item->second;
735 _ERR("Terminal doesn't exist [%d]", terminalID);
741 Terminal *ServerResource::getTerminal(const char *name)
743 Terminal *result = NULL;
744 map<unsigned int, Terminal *>::iterator item;
746 for (item = mapTerminals.begin(); item != mapTerminals.end(); item++)
748 if (strncmp(name, item->second->getName(), strlen(name)) == 0)
750 result = item->second;
758 Terminal *ServerResource::getTerminalByIndex(int index)
761 Terminal *result = NULL;
762 map<unsigned int, Terminal *>::iterator item;
764 for (item = mapTerminals.begin(), count = 0; item != mapTerminals.end(); item++, count++) {
765 if (count == index) {
766 result = item->second;
774 Terminal *ServerResource::getTerminalByReaderID(unsigned int readerID)
776 Terminal *result = NULL;
777 map<unsigned int, unsigned int>::iterator item;
779 if ((item = mapReaders.find(readerID)) != mapReaders.end())
781 result = getTerminal(item->second);
785 _ERR("Terminal doesn't exist, reader ID [%d]", readerID);
791 unsigned int ServerResource::getTerminalID(const char *name)
793 unsigned int result = IntegerHandle::INVALID_HANDLE;
794 map<unsigned int, Terminal *>::iterator item;
796 for (item = mapTerminals.begin(); item != mapTerminals.end(); item++)
798 if (strncmp(name, item->second->getName(), strlen(name)) == 0)
800 result = item->first;
808 bool ServerResource::_isAuthorizedAccess(ServerChannel *channel, ByteArray aid, vector<ByteArray> &hashes)
811 AccessControlList *acList = NULL;
813 /* request open channel sequence */
814 if ((acList = getAccessControlList(channel)) == NULL)
816 /* load access control defined by Global Platform */
817 GPACE *acl = new GPACE();
822 ret = acl->loadACL(channel);
823 if (ret >= SCARD_ERROR_OK)
826 addAccessControlList(channel, acList);
830 _ERR("unknown error, 0x%x", -ret);
836 _ERR("alloc failed");
841 acList->loadACL(channel);
846 result = acList->isAuthorizedAccess(aid, hashes);
852 int ServerResource::_openLogicalChannel(Terminal *terminal)
860 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_OPEN_LOGICAL_CHANNEL, 0, ByteArray::EMPTY);
861 rv = terminal->transmitSync(command, response);
862 if (rv == 0 && response.getLength() >= 2)
864 ResponseHelper resp(response);
866 if (resp.getStatus() >= 0)
868 result = resp.getDataField()[0];
872 result = resp.getStatus();
877 _ERR("transmitSync failed, rv [%d], length [%d]", rv, response.getLength());
883 int ServerResource::_closeLogicalChannel(Terminal *terminal, int channelNum)
885 int result = SCARD_ERROR_UNKNOWN;
891 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_CLOSE_LOGICAL_CHANNEL, channelNum, ByteArray::EMPTY);
892 rv = terminal->transmitSync(command, response);
893 if (rv == 0 && response.getLength() >= 2)
895 ResponseHelper resp(response);
897 if (resp.getStatus() >= 0)
899 _DBG("channel closed [%d]", channelNum);
900 result = SCARD_ERROR_OK;
904 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
909 _ERR("select apdu is failed, rv [%d], length [%d]", rv, response.getLength());
915 unsigned int ServerResource::_createChannel(Terminal *terminal, ServiceInstance *service, int channelType, unsigned int sessionID, ByteArray aid)
916 throw(ExceptionBase &)
918 unsigned int result = IntegerHandle::INVALID_HANDLE;
920 ServerChannel *channel = NULL;
922 /* open logical channel */
923 if (channelType == 1)
925 channelNum = _openLogicalChannel(terminal);
928 _DBG("channelNum [%d]", channelNum);
932 _ERR("_openLogicalChannel failed [%d]", channelNum);
933 throw ExceptionBase(SCARD_ERROR_NOT_ENOUGH_RESOURCE);
937 /* create channel instance */
938 result = service->openChannel(sessionID, channelNum, ByteArray::EMPTY);
939 if (result == IntegerHandle::INVALID_HANDLE)
941 _ERR("channel is null.");
943 /* close logical channel */
946 _closeLogicalChannel(terminal, channelNum);
948 throw ExceptionBase(SCARD_ERROR_OUT_OF_MEMORY);
951 channel = service->getChannel(result);
954 if (_isAuthorizedAccess(channel, aid,
955 service->getParent()->getCertificationHashes()) == true)
960 if (aid == PKCS15::PKCS15_AID)
962 PKCS15 pkcs15(channel);
964 rv = pkcs15.select();
965 if (rv >= SCARD_ERROR_OK)
967 /* remove privilege mode */
968 channel->unsetPrivilegeMode();
969 channel->setSelectResponse(pkcs15.getSelectResponse());
973 _ERR("select failed, [%x]", -rv);
975 service->closeChannel(result);
976 throw ExceptionBase(SCARD_ERROR_IO_FAILED);
981 FileObject file(channel);
983 rv = file.select(aid);
984 if (rv >= SCARD_ERROR_OK)
986 /* remove privilege mode */
987 channel->unsetPrivilegeMode();
988 channel->setSelectResponse(file.getSelectResponse());
992 _ERR("select failed [%x]", -rv);
994 service->closeChannel(result);
995 throw ExceptionBase(SCARD_ERROR_IO_FAILED);
1001 _ERR("unauthorized access");
1003 service->closeChannel(result);
1004 throw ExceptionBase(SCARD_ERROR_SECURITY_NOT_ALLOWED);
1010 void ServerResource::addAccessControlList(Terminal *terminal, AccessControlList *acl)
1012 map<Terminal *, AccessControlList *>::iterator item;
1014 if ((item = mapACL.find(terminal)) == mapACL.end())
1016 mapACL.insert(make_pair(terminal, acl));
1024 void ServerResource::addAccessControlList(ServerChannel *channel, AccessControlList *acl)
1026 map<Terminal *, AccessControlList *>::iterator item;
1028 if ((item = mapACL.find(channel->getTerminal())) == mapACL.end())
1030 mapACL.insert(make_pair(channel->getTerminal(), acl));
1038 AccessControlList *ServerResource::getAccessControlList(Terminal *terminal)
1040 AccessControlList *result = NULL;
1041 map<Terminal *, AccessControlList *>::iterator item;
1043 if ((item = mapACL.find(terminal)) != mapACL.end())
1045 result = item->second;
1051 AccessControlList *ServerResource::getAccessControlList(ServerChannel *channel)
1053 AccessControlList *result = NULL;
1054 map<Terminal *, AccessControlList *>::iterator item;
1056 if ((item = mapACL.find(channel->getTerminal())) != mapACL.end())
1058 result = item->second;
1064 Terminal *ServerResource::createInstance(void *library)
1066 Terminal *terminal = NULL;
1067 terminal_create_instance_fn createInstance = NULL;
1069 /* create se instance */
1070 createInstance = (terminal_create_instance_fn)dlsym(library, "create_instance");
1071 if (createInstance != NULL)
1073 terminal = (Terminal *)createInstance();
1074 if (terminal != NULL)
1076 _DBG("terminal [%p]", terminal);
1080 _ERR("terminal is null");
1085 _ERR("create_instance is null [%d]", errno);
1091 bool ServerResource::appendSELibrary(char *library)
1093 void *libHandle = NULL;
1094 bool result = false;
1096 libHandle = dlopen(library, RTLD_LAZY);
1097 if (libHandle != NULL)
1099 Terminal *terminal = NULL;
1101 terminal = createInstance(libHandle);
1102 if (terminal != NULL)
1104 unsigned int handle = IntegerHandle::assignHandle();
1106 mapTerminals.insert(make_pair(handle, terminal));
1107 libraries.push_back(libHandle);
1109 terminal->setStatusCallback(&ServerResource::terminalCallback);
1111 _DBG("register success [%s] [%p] [%s] [%p]", library, libHandle, terminal->getName(), terminal);
1113 if (terminal->isSecureElementPresence() == true)
1115 createReader(handle);
1122 _ERR("terminal is null [%s]", library);
1129 _ERR("it is not se file [%s] [%d]", library, errno);
1135 int ServerResource::loadSecureElements()
1139 if (seLoaded == false)
1142 struct dirent *entry;
1144 if ((dir = opendir(OMAPI_SE_PATH)) != NULL)
1146 while ((entry = readdir(dir)) != NULL)
1148 if (strncmp(entry->d_name, ".", 1) != 0 &&
1149 strncmp(entry->d_name, "..", 2) != 0)
1151 char fullPath[1024];
1153 /* TODO : need additional name rule :) */
1155 /* append each files */
1156 snprintf(fullPath, sizeof(fullPath),
1157 "%s/%s", OMAPI_SE_PATH, entry->d_name);
1159 SCARD_DEBUG("se name [%s]", fullPath);
1161 result = appendSELibrary(fullPath);
1178 void ServerResource::unloadSecureElements()
1180 if (seLoaded == true)
1183 map<unsigned int, Terminal *>::iterator item;
1185 for (item = mapTerminals.begin(); item != mapTerminals.end(); item++)
1187 item->second->finalize();
1189 IntegerHandle::releaseHandle(item->first);
1192 mapTerminals.clear();
1194 for (i = 0; i < libraries.size(); i++)
1196 if (libraries[i] != NULL)
1197 dlclose(libraries[i]);
1206 bool ServerResource::isValidReaderHandle(unsigned int reader)
1208 return (getTerminalByReaderID(reader) != NULL);
1211 void ServerResource::getReaders(vector<pair<unsigned int, string> > &readers)
1214 map<unsigned int, unsigned int>::iterator item;
1218 for (item = mapReaders.begin(); item != mapReaders.end(); item++)
1220 if (item->second != IntegerHandle::INVALID_HANDLE)
1222 terminal = getTerminal(item->second);
1223 if (terminal != NULL && terminal->isSecureElementPresence() == true)
1225 readers.push_back(make_pair(item->first, terminal->getName()));
1231 int ServerResource::getReadersInformation(ByteArray &info)
1234 unsigned char *buffer = NULL;
1235 unsigned int length = 0;
1236 unsigned int offset = 0;
1237 unsigned int nameLen = 0;
1239 if (mapReaders.size() > 0)
1241 Terminal *terminal = NULL;
1242 map<unsigned int, unsigned int>::iterator item;
1244 for (item = mapReaders.begin(); item != mapReaders.end(); item++)
1246 if (item->second != IntegerHandle::INVALID_HANDLE)
1248 terminal = getTerminal(item->second);
1249 if (terminal != NULL)
1251 if (terminal->isSecureElementPresence())
1253 length += sizeof(nameLen) + strlen(terminal->getName()) + sizeof(unsigned int);
1260 buffer = new unsigned char[length];
1263 memset(buffer, 0, length);
1265 for (item = mapReaders.begin(); item != mapReaders.end(); item++)
1267 if (item->second != IntegerHandle::INVALID_HANDLE)
1269 terminal = getTerminal(item->second);
1270 if (terminal != NULL)
1272 if (terminal->isSecureElementPresence())
1274 nameLen = strlen(terminal->getName());
1276 memcpy(buffer + offset, &nameLen, sizeof(nameLen));
1277 offset += sizeof(nameLen);
1279 memcpy(buffer + offset, terminal->getName(), nameLen);
1282 memcpy(buffer + offset, &item->first, sizeof(unsigned int));
1283 offset += sizeof(unsigned int);
1289 info.setBuffer(buffer, length);
1294 _ERR("alloc failed");
1300 _INFO("no secure element");
1307 bool ServerResource::sendMessageToAllClients(Message &msg)
1311 map<int, ClientInstance *>::iterator item;
1313 for (item = mapClients.begin(); item != mapClients.end(); item++)
1315 if (item->second->sendMessageToAllServices(item->second->getSocket(), msg) == false)
1323 void ServerResource::terminalCallback(void *terminal, int event, int error, void *user_param)
1325 _DBG("terminal [%s], event [%d], error [%d], user_param [%p]", (char *)terminal, event, error, user_param);
1329 case Terminal::NOTIFY_SE_AVAILABLE :
1331 ServerResource &instance = ServerResource::getInstance();
1332 unsigned int terminalID = IntegerHandle::INVALID_HANDLE;
1334 _INFO("[NOTIFY_SE_AVAILABLE]");
1336 terminalID = instance.getTerminalID((char *)terminal);
1337 if (terminalID != IntegerHandle::INVALID_HANDLE)
1339 unsigned int readerID = instance.createReader(terminalID);
1341 ServerGDBus::getInstance().emitReaderInserted(readerID, (const char *)terminal);
1345 /* send all client to refresh reader */
1346 msg.message = msg.MSG_NOTIFY_SE_INSERTED;
1347 msg.param1 = readerID;
1348 msg.data.setBuffer((unsigned char *)terminal, strlen((char *)terminal) + 1);
1350 instance.sendMessageToAllClients(msg);
1356 case Terminal::NOTIFY_SE_NOT_AVAILABLE :
1358 ServerResource &instance = ServerResource::getInstance();
1359 unsigned int readerID = IntegerHandle::INVALID_HANDLE;
1361 _INFO("[NOTIFY_SE_NOT_AVAILABLE]");
1363 readerID = instance.getReaderID((char *)terminal);
1365 ServerGDBus::getInstance().emitReaderRemoved(readerID, (const char *)terminal);
1369 /* send all client to refresh reader */
1370 msg.message = msg.MSG_NOTIFY_SE_REMOVED;
1371 msg.param1 = readerID;
1372 msg.data.setBuffer((unsigned char *)terminal, strlen((char *)terminal) + 1);
1374 instance.sendMessageToAllClients(msg);
1376 instance.removeReader(readerID);
1381 _DBG("terminal [%s], event [%d], error [%d], user_param [%p]", (char *)terminal, event, error, user_param);
1386 unsigned int ServerResource::createReader(unsigned int terminalID)
1388 unsigned int result = -1;
1390 result = IntegerHandle::assignHandle();
1392 mapReaders.insert(make_pair(result, terminalID));
1397 unsigned int ServerResource::getReaderID(const char *name)
1399 unsigned int result = IntegerHandle::INVALID_HANDLE, terminalID = IntegerHandle::INVALID_HANDLE;
1401 terminalID = getTerminalID(name);
1402 if (terminalID != IntegerHandle::INVALID_HANDLE)
1404 map<unsigned int, unsigned int>::iterator item;
1406 for (item = mapReaders.begin(); item != mapReaders.end(); item++)
1408 if (item->second == terminalID)
1410 result = item->first;
1419 void ServerResource::removeReader(unsigned int readerID)
1421 map<unsigned int, unsigned int>::iterator item;
1423 if ((item = mapReaders.find(readerID)) != mapReaders.end())
1425 item->second = IntegerHandle::INVALID_HANDLE;
1429 bool ServerResource::isAuthorizedNFCAccess(Terminal *terminal, ByteArray &aid, vector<ByteArray> &hashes)
1431 bool result = false;
1433 if (terminal == NULL) {
1437 int num = _openLogicalChannel(terminal);
1439 /* create channel instance */
1440 ServerChannel *channel = new ServerChannel(NULL, NULL, num, terminal);
1441 if (channel != NULL) {
1442 AccessControlList *acl = getAccessControlList(channel);
1445 /* load access control defined by Global Platform */
1450 ret = acl->loadACL(channel);
1451 if (ret >= SCARD_ERROR_OK) {
1452 addAccessControlList(channel, acl);
1454 _ERR("unknown error, 0x%x", -ret);
1458 _ERR("alloc failed");
1461 acl->updateACL(channel);
1465 result = acl->isAuthorizedNFCAccess(aid, hashes);
1467 _ERR("acl is null");
1472 _ERR("alloc failed");
1475 _ERR("_openLogicalChannel failed");
1480 } /* namespace smartcard_service_api */
1482 using namespace smartcard_service_api;
1484 EXTERN_API void server_resource_set_main_loop_instance(void *instance)
1486 ServerResource::getInstance().setMainLoopInstance(instance);