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"
37 #include "smartcard-daemon.h"
40 #define EXTERN_API __attribute__((visibility("default")))
45 namespace smartcard_service_api
47 unsigned int IntegerHandle::newHandle = 0;
48 set<unsigned int> IntegerHandle::setHandles;
49 PMutex IntegerHandle::mutexLock;
51 unsigned int IntegerHandle::assignHandle()
55 pair<set<unsigned int>::iterator, bool> result;
60 if (newHandle == (unsigned int)-1)
65 result = setHandles.insert(newHandle);
68 while (!result.second);
71 _DBG("assign handle : newHandle [%d]", newHandle);
76 void IntegerHandle::releaseHandle(unsigned int handle)
78 _DBG("will be released : Handle [%d]", handle);
82 setHandles.erase(handle);
86 #define OMAPI_SE_PATH "/usr/lib/se"
88 ServerResource::ServerResource() : 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() const
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);
224 if (instance->getServiceCounts() == 0) {
226 /* remove client instance */
232 _ERR("client doesn't exist, name [%s]", name);
236 void ServerResource::removeServices(const char *name)
238 ClientInstance *instance = NULL;
240 if ((instance = getClient(name)) != NULL)
242 instance->removeServices();
244 /* remove client instance */
249 _ERR("client doesn't exist, name [%s]", name);
253 unsigned int ServerResource::createSession(const char *name, unsigned int handle, unsigned int readerID, vector<ByteArray> &certHashes, void *caller)
255 unsigned int result = -1;
256 Terminal *temp = NULL;
257 ServiceInstance *instance = NULL;
259 if ((instance = getService(name, handle)) != NULL)
261 if ((temp = getTerminalByReaderID(readerID)) != NULL)
263 result = instance->openSession(temp, certHashes, caller);
268 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
274 ServerSession *ServerResource::getSession(const char *name, unsigned int handle, unsigned int sessionID)
276 ServerSession *result = NULL;
277 ServiceInstance *instance = NULL;
279 if ((instance = getService(name, handle)) != NULL)
281 result = instance->getSession(sessionID);
285 _ERR("Session doesn't exist : name [%s], handle [%d], handle [%d]", name, handle, sessionID);
291 bool ServerResource::isValidSessionHandle(const char *name, unsigned int handle, unsigned int session)
293 ServiceInstance *instance = NULL;
295 return (((instance = getService(name, handle)) != NULL) && (instance->isVaildSessionHandle(session)));
298 unsigned int ServerResource::getChannelCount(const char *name, unsigned int handle, unsigned int sessionID)
300 unsigned int result = -1;
301 ServiceInstance *instance = NULL;
303 if ((instance = getService(name, handle)) != NULL)
305 result = instance->getChannelCountBySession(sessionID);
309 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
315 void ServerResource::removeSession(const char *name, unsigned int handle, unsigned int sessionID)
317 ServiceInstance *instance = NULL;
319 if ((instance = getService(name, handle)) != NULL)
321 instance->closeSession(sessionID);
325 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
329 unsigned int ServerResource::createChannel(const char *name, unsigned int handle, unsigned int sessionID, int channelType, ByteArray aid)
330 throw(ExceptionBase &)
332 unsigned int result = -1;
333 ServiceInstance *service = NULL;
335 if ((service = getService(name, handle)) != NULL)
337 if (service->isVaildSessionHandle(sessionID) == true)
339 ServerSession *session = NULL;
340 Terminal *terminal = NULL;
342 terminal = service->getTerminal(sessionID);
343 session = service->getSession(sessionID);
344 if (terminal != NULL && session != NULL)
346 result = _createChannel(terminal, service, channelType, sessionID, aid);
347 if (result == IntegerHandle::INVALID_HANDLE)
349 _ERR("create channel failed [%d]", sessionID);
354 _ERR("session is invalid [%d]", sessionID);
355 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
360 _ERR("session is invalid [%d]", sessionID);
361 throw ExceptionBase(SCARD_ERROR_ILLEGAL_STATE);
366 _ERR("getService is failed, name [%s], handle [%d]", name, handle);
367 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
373 Channel *ServerResource::getChannel(const char *name, unsigned int handle, unsigned int channelID)
375 Channel *result = NULL;
376 ServiceInstance *instance = NULL;
378 if ((instance = getService(name, handle)) != NULL)
380 result = instance->getChannel(channelID);
384 _ERR("Channel doesn't exist : name [%s], handle [%d], handle [%d]", name, handle, channelID);
390 void ServerResource::removeChannel(const char *name, unsigned int handle, unsigned int channelID)
392 ServiceInstance *instance = NULL;
394 if ((instance = getService(name, handle)) != NULL)
396 instance->closeChannel(channelID);
400 _ERR("getService doesn't exist : name [%s], handle [%d]", name, handle);
404 bool ServerResource::createClient(void *ioChannel, int socket,
405 int watchID, int state, int pid)
409 if (getClient(socket) == NULL)
411 ClientInstance *instance = new ClientInstance(ioChannel,
412 socket, watchID, state, pid);
413 if (instance != NULL)
415 mapClients.insert(make_pair(socket, instance));
420 _ERR("alloc failed");
425 _ERR("client already exist, socket[%d]", socket);
431 bool ServerResource::createClient(int pid)
435 if (getClient(pid) == NULL)
437 ClientInstance *instance = new ClientInstance(pid);
438 if (instance != NULL)
440 mapClients.insert(make_pair(pid, instance));
445 _ERR("alloc failed");
450 _ERR("client already exist, pid[%d]", pid);
456 ClientInstance *ServerResource::getClient(int socket)
458 ClientInstance *result = NULL;
459 map<int, ClientInstance *>::iterator item;
461 if ((item = mapClients.find(socket)) != mapClients.end())
463 result = item->second;
469 const ClientInstance *ServerResource::getClient(int socket) const
471 const ClientInstance *result = NULL;
472 map<int, ClientInstance *>::const_iterator item;
474 if ((item = mapClients.find(socket)) != mapClients.end())
476 result = item->second;
482 void ServerResource::setPID(int socket, int pid)
484 map<int, ClientInstance *>::iterator item;
486 if ((item = mapClients.find(socket)) != mapClients.end())
488 if (item->second->getPID() < 0)
489 item->second->setPID(pid);
493 void ServerResource::removeClient(int socket)
495 map<int, ClientInstance *>::iterator item;
497 if ((item = mapClients.find(socket)) != mapClients.end())
500 ServerIPC::getInstance()->releaseClient(item->second->getIOChannel(), item->second->getSocket(), item->second->getWatchID());
503 mapClients.erase(item);
507 _DBG("client removed already [%d]", socket);
511 void ServerResource::removeClients()
513 map<int, ClientInstance *>::iterator item;
515 for (item = mapClients.begin(); item != mapClients.end(); item++)
518 ServerIPC::getInstance()->releaseClient(item->second->getIOChannel(), item->second->getSocket(), item->second->getWatchID());
526 int ServerResource::getClientCount() const
528 return (int)mapClients.size();
531 ServiceInstance *ServerResource::createService(int socket)
533 ServiceInstance *result = NULL;
534 ClientInstance *instance = NULL;
536 if ((instance = getClient(socket)) != NULL)
538 if ((result = instance->createService()) == NULL)
540 _ERR("ClientInstance::createService failed [%d]", socket);
545 _ERR("client doesn't exist [%d]", socket);
551 ServiceInstance *ServerResource::getService(int socket, unsigned int handle)
553 ServiceInstance *result = NULL;
554 ClientInstance *instance = NULL;
556 if ((instance = getClient(socket)) != NULL)
558 result = instance->getService(handle);
562 _ERR("client doesn't exist [%d]", socket);
568 void ServerResource::removeService(int socket, unsigned int handle)
570 ClientInstance *instance = NULL;
572 if ((instance = getClient(socket)) != NULL)
574 instance->removeService(handle);
578 _ERR("client doesn't exist [%d]", socket);
582 void ServerResource::removeServices(int socket)
584 ClientInstance *instance = NULL;
586 if ((instance = getClient(socket)) != NULL)
588 instance->removeServices();
592 _ERR("client doesn't exist [%d]", socket);
596 unsigned int ServerResource::createSession(int socket, unsigned int handle, unsigned int readerID, const vector<ByteArray> &certHashes, void *caller)
598 unsigned int result = -1;
599 Terminal *temp = NULL;
600 ServiceInstance *instance = NULL;
602 if ((instance = getService(socket, handle)) != NULL)
604 if ((temp = getTerminalByReaderID(readerID)) != NULL)
606 result = instance->openSession(temp, certHashes, caller);
611 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
617 ServerSession *ServerResource::getSession(int socket, unsigned int handle, unsigned int sessionID)
619 ServerSession *result = NULL;
620 ServiceInstance *instance = NULL;
622 if ((instance = getService(socket, handle)) != NULL)
624 result = instance->getSession(sessionID);
628 _ERR("Session doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, sessionID);
634 bool ServerResource::isValidSessionHandle(int socket, unsigned int handle, unsigned int session)
636 ServiceInstance *instance = NULL;
638 return (((instance = getService(socket, handle)) != NULL) && (instance->isVaildSessionHandle(session)));
641 unsigned int ServerResource::getChannelCount(int socket, unsigned int handle, unsigned int sessionID)
643 unsigned int result = -1;
644 ServiceInstance *instance = NULL;
646 if ((instance = getService(socket, handle)) != NULL)
648 result = instance->getChannelCountBySession(sessionID);
652 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
658 void ServerResource::removeSession(int socket, unsigned int handle, unsigned int sessionID)
660 ServiceInstance *instance = NULL;
662 if ((instance = getService(socket, handle)) != NULL)
664 instance->closeSession(sessionID);
668 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
672 unsigned int ServerResource::createChannel(int socket, unsigned int handle, unsigned int sessionID, int channelType, const ByteArray &aid)
673 throw(ExceptionBase &)
675 unsigned int result = -1;
676 ServiceInstance *service = NULL;
678 if ((service = getService(socket, handle)) != NULL)
680 if (service->isVaildSessionHandle(sessionID) == true)
682 ServerSession *session = NULL;
683 Terminal *terminal = NULL;
685 terminal = service->getTerminal(sessionID);
686 session = service->getSession(sessionID);
687 if (terminal != NULL && session != NULL)
689 result = _createChannel(terminal, service, channelType, sessionID, aid);
690 if (result == IntegerHandle::INVALID_HANDLE)
692 _ERR("create channel failed [%d]", sessionID);
697 _ERR("session is invalid [%d]", sessionID);
698 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
703 _ERR("session is invalid [%d]", sessionID);
704 throw ExceptionBase(SCARD_ERROR_ILLEGAL_STATE);
709 _ERR("getService is failed [%d] [%d]", socket, handle);
710 throw ExceptionBase(SCARD_ERROR_UNAVAILABLE);
716 Channel *ServerResource::getChannel(int socket, unsigned int handle, unsigned int channelID)
718 Channel *result = NULL;
719 ServiceInstance *instance = NULL;
721 if ((instance = getService(socket, handle)) != NULL)
723 result = instance->getChannel(channelID);
727 _ERR("Channel doesn't exist : socket [%d], handle [%d], handle [%d]", socket, handle, channelID);
733 void ServerResource::removeChannel(int socket, unsigned int handle, unsigned int channelID)
735 ServiceInstance *instance = NULL;
737 if ((instance = getService(socket, handle)) != NULL)
739 instance->closeChannel(channelID);
743 _ERR("getService doesn't exist : socket [%d], handle [%d]", socket, handle);
747 Terminal *ServerResource::getTerminal(unsigned int terminalID)
749 Terminal *result = NULL;
750 map<unsigned int, Terminal *>::iterator item;
752 if ((item = mapTerminals.find(terminalID)) != mapTerminals.end())
754 result = item->second;
758 _ERR("Terminal doesn't exist [%d]", terminalID);
764 const Terminal *ServerResource::getTerminal(unsigned int terminalID) const
766 const Terminal *result = NULL;
767 map<unsigned int, Terminal *>::const_iterator item;
769 if ((item = mapTerminals.find(terminalID)) != mapTerminals.end())
771 result = item->second;
775 _ERR("Terminal doesn't exist [%d]", terminalID);
781 Terminal *ServerResource::getTerminal(const char *name)
783 Terminal *result = NULL;
784 map<unsigned int, Terminal *>::iterator item;
786 for (item = mapTerminals.begin(); item != mapTerminals.end(); item++)
788 if (strncmp(name, item->second->getName(), strlen(name)) == 0)
790 result = item->second;
798 Terminal *ServerResource::getTerminalByReaderID(unsigned int readerID)
800 Terminal *result = NULL;
801 map<unsigned int, unsigned int>::iterator item;
803 if ((item = mapReaders.find(readerID)) != mapReaders.end())
805 result = getTerminal(item->second);
809 _ERR("Terminal doesn't exist, reader ID [%d]", readerID);
815 const Terminal *ServerResource::getTerminalByReaderID(unsigned int readerID) const
817 const Terminal *result = NULL;
818 map<unsigned int, unsigned int>::const_iterator item;
820 if ((item = mapReaders.find(readerID)) != mapReaders.end())
822 result = getTerminal(item->second);
826 _ERR("Terminal doesn't exist, reader ID [%d]", readerID);
832 unsigned int ServerResource::getTerminalID(const char *name) const
834 unsigned int result = IntegerHandle::INVALID_HANDLE;
835 map<unsigned int, Terminal *>::const_iterator item;
837 for (item = mapTerminals.begin();
838 item != mapTerminals.end(); item++)
840 if (strncmp(name, item->second->getName(),
843 result = item->first;
851 bool ServerResource::_isAuthorizedAccess(ServerChannel *channel,
852 const ByteArray &aid, const vector<ByteArray> &hashes)
855 AccessControlList *acList = NULL;
857 /* request open channel sequence */
858 if ((acList = getAccessControlList(channel)) == NULL)
860 /* load access control defined by Global Platform */
861 GPACE *acl = new GPACE();
866 ret = acl->loadACL(channel);
867 if (ret >= SCARD_ERROR_OK)
870 addAccessControlList(channel, acList);
874 _ERR("unknown error, 0x%x", -ret);
880 _ERR("alloc failed");
885 acList->loadACL(channel);
890 result = acList->isAuthorizedAccess(aid, hashes);
896 int ServerResource::_openLogicalChannel(Terminal *terminal)
904 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_OPEN_LOGICAL_CHANNEL, 0, ByteArray::EMPTY);
905 rv = terminal->transmitSync(command, response);
906 if (rv == 0 && response.size() >= 2)
908 ResponseHelper resp(response);
910 if (resp.getStatus() >= 0)
912 result = resp.getDataField()[0];
916 result = resp.getStatus();
921 _ERR("transmitSync failed, rv [%d], length [%d]", rv, response.size());
927 int ServerResource::_closeLogicalChannel(Terminal *terminal, int channelNum)
929 int result = SCARD_ERROR_UNKNOWN;
935 command = APDUHelper::generateAPDU(APDUHelper::COMMAND_CLOSE_LOGICAL_CHANNEL, channelNum, ByteArray::EMPTY);
936 rv = terminal->transmitSync(command, response);
937 if (rv == 0 && response.size() >= 2)
939 ResponseHelper resp(response);
941 if (resp.getStatus() >= 0)
943 _DBG("channel closed [%d]", channelNum);
944 result = SCARD_ERROR_OK;
948 _ERR("status word [ %02X %02X ]", resp.getSW1(), resp.getSW2());
953 _ERR("select apdu is failed, rv [%d], length [%d]", rv, response.size());
959 unsigned int ServerResource::_createChannel(Terminal *terminal,
960 ServiceInstance *service, int channelType,
961 unsigned int sessionID, const ByteArray &aid)
962 throw(ExceptionBase &)
964 unsigned int result = IntegerHandle::INVALID_HANDLE;
966 ServerChannel *channel = NULL;
968 /* open logical channel */
969 if (channelType == 1)
971 channelNum = _openLogicalChannel(terminal);
974 _DBG("channelNum [%d]", channelNum);
978 _ERR("_openLogicalChannel failed [%d]", channelNum);
979 throw ExceptionBase(SCARD_ERROR_NOT_ENOUGH_RESOURCE);
983 /* create channel instance */
984 result = service->openChannel(sessionID, channelNum, ByteArray::EMPTY);
985 if (result == IntegerHandle::INVALID_HANDLE)
987 _ERR("channel is null.");
989 /* close logical channel */
992 _closeLogicalChannel(terminal, channelNum);
994 throw ExceptionBase(SCARD_ERROR_OUT_OF_MEMORY);
997 channel = service->getChannel(result);
1000 if (_isAuthorizedAccess(channel, aid,
1001 service->getParent()->getCertificationHashes()) == true)
1006 if (aid == PKCS15::PKCS15_AID)
1008 PKCS15 pkcs15(channel);
1010 rv = pkcs15.select();
1011 if (rv >= SCARD_ERROR_OK)
1013 /* remove privilege mode */
1014 channel->unsetPrivilegeMode();
1015 channel->setSelectResponse(pkcs15.getSelectResponse());
1019 _ERR("select failed, [%x]", -rv);
1021 service->closeChannel(result);
1022 throw ExceptionBase(SCARD_ERROR_IO_FAILED);
1027 FileObject file(channel);
1029 rv = file.select(aid);
1030 if (rv >= SCARD_ERROR_OK)
1032 /* remove privilege mode */
1033 channel->unsetPrivilegeMode();
1034 channel->setSelectResponse(file.getSelectResponse());
1038 _ERR("select failed [%x]", -rv);
1040 service->closeChannel(result);
1041 throw ExceptionBase(SCARD_ERROR_IO_FAILED);
1047 _ERR("unauthorized access");
1049 service->closeChannel(result);
1050 throw ExceptionBase(SCARD_ERROR_SECURITY_NOT_ALLOWED);
1056 void ServerResource::addAccessControlList(Terminal *terminal, AccessControlList *acl)
1058 map<Terminal *, AccessControlList *>::iterator item;
1060 if ((item = mapACL.find(terminal)) == mapACL.end())
1062 mapACL.insert(make_pair(terminal, acl));
1070 void ServerResource::addAccessControlList(ServerChannel *channel, AccessControlList *acl)
1072 map<Terminal *, AccessControlList *>::iterator item;
1074 if ((item = mapACL.find(channel->getTerminal())) == mapACL.end())
1076 mapACL.insert(make_pair(channel->getTerminal(), acl));
1084 AccessControlList *ServerResource::getAccessControlList(Terminal *terminal)
1086 AccessControlList *result = NULL;
1087 map<Terminal *, AccessControlList *>::iterator item;
1089 if ((item = mapACL.find(terminal)) != mapACL.end())
1091 result = item->second;
1097 AccessControlList *ServerResource::getAccessControlList(ServerChannel *channel)
1099 AccessControlList *result = NULL;
1100 map<Terminal *, AccessControlList *>::iterator item;
1102 if ((item = mapACL.find(channel->getTerminal())) != mapACL.end())
1104 result = item->second;
1110 Terminal *ServerResource::createInstance(void *library)
1112 Terminal *terminal = NULL;
1113 terminal_create_instance_fn createInstance = NULL;
1115 /* create se instance */
1116 createInstance = (terminal_create_instance_fn)dlsym(library, "create_instance");
1117 if (createInstance != NULL)
1119 terminal = (Terminal *)createInstance();
1120 if (terminal != NULL)
1122 _DBG("terminal [%p]", terminal);
1126 _ERR("terminal is null");
1131 _ERR("create_instance is null [%d]", errno);
1137 bool ServerResource::appendSELibrary(char *library)
1139 void *libHandle = NULL;
1140 bool result = false;
1142 libHandle = dlopen(library, RTLD_LAZY);
1143 if (libHandle != NULL)
1145 Terminal *terminal = NULL;
1147 terminal = createInstance(libHandle);
1148 if (terminal != NULL)
1150 unsigned int handle = IntegerHandle::assignHandle();
1152 mapTerminals.insert(make_pair(handle, terminal));
1153 libraries.push_back(libHandle);
1155 terminal->setStatusCallback(&ServerResource::terminalCallback);
1157 _DBG("register success [%s] [%p] [%s] [%p]", library, libHandle, terminal->getName(), terminal);
1159 if (terminal->isSecureElementPresence() == true)
1161 createReader(handle);
1168 _ERR("terminal is null [%s]", library);
1175 _ERR("it is not se file [%s] [%d]", library, errno);
1181 int ServerResource::loadSecureElements()
1185 if (seLoaded == false)
1188 struct dirent *entry;
1190 if ((dir = opendir(OMAPI_SE_PATH)) != NULL)
1192 while ((entry = readdir(dir)) != NULL)
1194 if (strncmp(entry->d_name, ".", 1) != 0 &&
1195 strncmp(entry->d_name, "..", 2) != 0)
1197 char fullPath[1024];
1199 /* TODO : need additional name rule :) */
1201 /* append each files */
1202 snprintf(fullPath, sizeof(fullPath),
1203 "%s/%s", OMAPI_SE_PATH, entry->d_name);
1205 SCARD_DEBUG("se name [%s]", fullPath);
1207 result = appendSELibrary(fullPath);
1224 void ServerResource::unloadSecureElements()
1226 if (seLoaded == true)
1229 map<unsigned int, Terminal *>::iterator item;
1231 for (item = mapTerminals.begin(); item != mapTerminals.end(); item++)
1233 item->second->finalize();
1235 IntegerHandle::releaseHandle(item->first);
1238 mapTerminals.clear();
1240 for (i = 0; i < libraries.size(); i++)
1242 if (libraries[i] != NULL)
1243 dlclose(libraries[i]);
1252 bool ServerResource::isValidReaderHandle(unsigned int reader) const
1254 return (getTerminalByReaderID(reader) != NULL);
1257 void ServerResource::getReaders(vector<pair<unsigned int, string> > &readers) const
1259 const Terminal *terminal;
1260 map<unsigned int, unsigned int>::const_iterator item;
1264 for (item = mapReaders.begin(); item != mapReaders.end(); item++)
1266 if (item->second != IntegerHandle::INVALID_HANDLE)
1268 terminal = getTerminal(item->second);
1269 if (terminal != NULL && terminal->isSecureElementPresence() == true)
1271 readers.push_back(make_pair(item->first, terminal->getName()));
1277 int ServerResource::getReadersInformation(ByteArray &info) const
1280 unsigned char *buffer = NULL;
1281 unsigned int length = 0;
1282 unsigned int offset = 0;
1283 unsigned int nameLen = 0;
1285 if (mapReaders.size() > 0)
1287 const Terminal *terminal = NULL;
1288 map<unsigned int, unsigned int>::const_iterator item;
1290 for (item = mapReaders.begin(); item != mapReaders.end(); item++)
1292 if (item->second != IntegerHandle::INVALID_HANDLE)
1294 terminal = getTerminal(item->second);
1295 if (terminal != NULL)
1297 if (terminal->isSecureElementPresence())
1299 length += sizeof(nameLen) + strlen(terminal->getName()) + sizeof(unsigned int);
1306 buffer = new unsigned char[length];
1309 memset(buffer, 0, length);
1311 for (item = mapReaders.begin(); item != mapReaders.end(); item++)
1313 if (item->second != IntegerHandle::INVALID_HANDLE)
1315 terminal = getTerminal(item->second);
1316 if (terminal != NULL)
1318 if (terminal->isSecureElementPresence())
1320 nameLen = strlen(terminal->getName());
1322 memcpy(buffer + offset, &nameLen, sizeof(nameLen));
1323 offset += sizeof(nameLen);
1325 memcpy(buffer + offset, terminal->getName(), nameLen);
1328 memcpy(buffer + offset, &item->first, sizeof(unsigned int));
1329 offset += sizeof(unsigned int);
1335 info.assign(buffer, length);
1340 _ERR("alloc failed");
1346 _INFO("no secure element");
1353 bool ServerResource::sendMessageToAllClients(const Message &msg)
1357 map<int, ClientInstance *>::const_iterator item;
1359 for (item = mapClients.begin();
1360 item != mapClients.end(); item++)
1362 if (item->second->sendMessageToAllServices(
1363 item->second->getSocket(), msg) == false)
1371 void ServerResource::terminalCallback(const void *terminal, int event,
1372 int error, void *user_param)
1374 _DBG("terminal [%s], event [%d], error [%d], user_param [%p]", (char *)terminal, event, error, user_param);
1378 case Terminal::NOTIFY_SE_AVAILABLE :
1380 ServerResource &instance = ServerResource::getInstance();
1381 unsigned int terminalID = IntegerHandle::INVALID_HANDLE;
1383 _INFO("[NOTIFY_SE_AVAILABLE]");
1385 terminalID = instance.getTerminalID((char *)terminal);
1386 if (terminalID != IntegerHandle::INVALID_HANDLE)
1388 unsigned int readerID = instance.createReader(terminalID);
1390 ServerGDBus::getInstance().emitReaderInserted(readerID, (const char *)terminal);
1394 /* send all client to refresh reader */
1395 msg.message = msg.MSG_NOTIFY_SE_INSERTED;
1396 msg.param1 = readerID;
1397 msg.data.assign((uint8_t *)terminal,
1398 strlen((char *)terminal) + 1);
1400 instance.sendMessageToAllClients(msg);
1406 case Terminal::NOTIFY_SE_NOT_AVAILABLE :
1408 ServerResource &instance = ServerResource::getInstance();
1409 unsigned int readerID = IntegerHandle::INVALID_HANDLE;
1411 _INFO("[NOTIFY_SE_NOT_AVAILABLE]");
1413 readerID = instance.getReaderID((char *)terminal);
1415 ServerGDBus::getInstance().emitReaderRemoved(
1416 readerID, (const char *)terminal);
1420 /* send all client to refresh reader */
1421 msg.message = msg.MSG_NOTIFY_SE_REMOVED;
1422 msg.param1 = readerID;
1423 msg.data.assign((uint8_t *)terminal,
1424 strlen((char *)terminal) + 1);
1426 instance.sendMessageToAllClients(msg);
1428 instance.removeReader(readerID);
1433 _DBG("terminal [%s], event [%d], error [%d], user_param [%p]", (char *)terminal, event, error, user_param);
1438 unsigned int ServerResource::createReader(unsigned int terminalID)
1440 unsigned int result = -1;
1442 result = IntegerHandle::assignHandle();
1444 mapReaders.insert(make_pair(result, terminalID));
1449 unsigned int ServerResource::getReaderID(const char *name) const
1451 unsigned int result = IntegerHandle::INVALID_HANDLE,
1452 terminalID = IntegerHandle::INVALID_HANDLE;
1454 terminalID = getTerminalID(name);
1455 if (terminalID != IntegerHandle::INVALID_HANDLE)
1457 map<unsigned int, unsigned int>::const_iterator item;
1459 for (item = mapReaders.begin();
1460 item != mapReaders.end(); item++)
1462 if (item->second == terminalID)
1464 result = item->first;
1473 void ServerResource::removeReader(unsigned int readerID)
1475 map<unsigned int, unsigned int>::iterator item;
1477 if ((item = mapReaders.find(readerID)) != mapReaders.end())
1479 item->second = IntegerHandle::INVALID_HANDLE;
1483 bool ServerResource::isAuthorizedNFCAccess(Terminal *terminal,
1484 const ByteArray &aid, const vector<ByteArray> &hashes)
1486 bool result = false;
1488 if (terminal == NULL) {
1492 int num = _openLogicalChannel(terminal);
1494 /* create channel instance */
1495 ServerChannel *channel = new ServerChannel(NULL, NULL, num, terminal);
1496 if (channel != NULL) {
1497 AccessControlList *acl = getAccessControlList(channel);
1500 /* load access control defined by Global Platform */
1505 ret = acl->loadACL(channel);
1506 if (ret >= SCARD_ERROR_OK) {
1507 addAccessControlList(channel, acl);
1509 _ERR("unknown error, 0x%x", -ret);
1513 _ERR("alloc failed");
1516 acl->updateACL(channel);
1520 result = acl->isAuthorizedNFCAccess(aid, hashes);
1522 _ERR("acl is null");
1527 _ERR("alloc failed");
1530 _ERR("_openLogicalChannel failed");
1536 void ServerResource::finish()
1538 if (getClientCount() == 0) {
1539 _INFO("no client connected. terminate server");
1541 smartcard_daemon_exit();
1544 } /* namespace smartcard_service_api */