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 */
20 /* SLP library header */
24 #include "SimpleTLV.h"
25 #include "AccessControlList.h"
26 #include "AccessCondition.h"
28 namespace smartcard_service_api
30 void AccessRule::addAPDUAccessRule(const ByteArray &apdu,
31 const ByteArray &mask)
33 pair<ByteArray, ByteArray> item(apdu, mask);
35 listFilters.push_back(item);
38 bool AccessRule::isAuthorizedAPDUAccess(const ByteArray &command)
42 if (command.getLength() < 4) /* apdu header size */
45 if (listFilters.size() > 0)
47 unsigned int cmd, mask, rule;
48 vector<pair<ByteArray, ByteArray> >::iterator item;
50 cmd = *(unsigned int *)command.getBuffer();
51 for (item = listFilters.begin(); item != listFilters.end(); item++)
53 mask = *(unsigned int *)item->second.getBuffer();
54 rule = *(unsigned int *)item->first.getBuffer();
56 if ((cmd & mask) == rule)
65 /* no filter entry. if permission is true, all access will be granted, if not, all access will be denied */
72 void AccessRule::printAccessRules()
74 if (listFilters.size() > 0)
76 vector<pair<ByteArray, ByteArray> >::iterator item;
78 _DBG(" +---- Granted APDUs");
80 for (item = listFilters.begin(); item != listFilters.end(); item++)
82 _DBG(" +----- APDU : %s, Mask : %s", item->first.toString(), item->second.toString());
87 _DBG(" +---- APDU Access ALLOW : %s", apduRule ? "ALWAYS" : "NEVER");
90 _DBG(" +---- NFC Access ALLOW : %s", nfcRule ? "ALWAYS" : "NEVER");
93 bool AccessRule::isAuthorizedNFCAccess(void)
98 AccessRule *AccessCondition::getAccessRule(const ByteArray &certHash)
100 AccessRule *result = NULL;
101 map<ByteArray, AccessRule>::iterator item;
103 item = mapRules.find(certHash);
104 if (item != mapRules.end()) {
105 result = &item->second;
111 void AccessCondition::addAccessRule(const ByteArray &hash)
115 pair<ByteArray, AccessRule> item(hash, rule);
117 mapRules.insert(item);
120 bool AccessCondition::isAuthorizedAccess(const ByteArray &certHash)
123 map<ByteArray, AccessRule>::iterator item;
125 item = mapRules.find(certHash);
126 if (item != mapRules.end())
139 void AccessCondition::printAccessConditions()
141 _DBG(" +-- Access Condition");
143 if (mapRules.size() > 0)
145 map<ByteArray, AccessRule>::iterator item;
147 for (item = mapRules.begin(); item != mapRules.end(); item++)
149 ByteArray temp = item->first;
151 _DBG(" +--- hash : %s", (temp == AccessControlList::ALL_DEVICE_APPS) ? "All device applications" : temp.toString());
152 item->second.printAccessRules();
157 _DBG(" +--- permission : %s", permission ? "granted all" : "denied all");
161 void AccessCondition::setAPDUAccessRule(const ByteArray &certHash,
164 AccessRule *access = getAccessRule(certHash);
166 if (access != NULL) {
167 access->setAPDUAccessRule(rule);
171 void AccessCondition::addAPDUAccessRule(const ByteArray &certHash,
172 const ByteArray &apdu, const ByteArray &mask)
174 AccessRule *access = getAccessRule(certHash);
176 if (access != NULL) {
177 access->addAPDUAccessRule(apdu, mask);
181 void AccessCondition::addAPDUAccessRule(const ByteArray &certHash,
182 const ByteArray &rule)
184 if (rule.getLength() != 8)
187 addAPDUAccessRule(certHash, rule.sub(0, 4), rule.sub(4, 4));
190 void AccessCondition::setNFCAccessRule(const ByteArray &certHash,
193 AccessRule *access = getAccessRule(certHash);
195 if (access != NULL) {
196 access->setNFCAccessRule(rule);
200 bool AccessCondition::isAuthorizedAPDUAccess(const ByteArray &certHash,
201 const ByteArray &command)
204 AccessRule *access = getAccessRule(certHash);
206 if (access != NULL) {
207 result = access->isAuthorizedAPDUAccess(command);
213 bool AccessCondition::isAuthorizedNFCAccess(const ByteArray &certHash)
216 AccessRule *access = getAccessRule(certHash);
218 if (access != NULL) {
219 result = access->isAuthorizedNFCAccess();
224 } /* namespace smartcard_service_api */