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) const
42 if (command.size() < 4) /* apdu header size */
45 if (listFilters.size() > 0)
47 unsigned int cmd, mask, rule;
48 vector<pair<ByteArray, ByteArray> >::const_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 bool AccessRule::isAuthorizedNFCAccess(void) const
77 AccessRule *AccessCondition::getAccessRule(const ByteArray &certHash)
79 AccessRule *result = NULL;
80 map<ByteArray, AccessRule>::iterator item;
82 item = mapRules.find(certHash);
83 if (item != mapRules.end()) {
84 result = &item->second;
90 const AccessRule *AccessCondition::getAccessRule(const ByteArray &certHash) const
92 const AccessRule *result = NULL;
93 map<ByteArray, AccessRule>::const_iterator item;
95 item = mapRules.find(certHash);
96 if (item != mapRules.end()) {
97 result = &item->second;
103 void AccessCondition::addAccessRule(const ByteArray &hash)
107 pair<ByteArray, AccessRule> item(hash, rule);
109 mapRules.insert(item);
112 bool AccessCondition::isAuthorizedAccess(const ByteArray &certHash) const
114 bool result = permission;
115 const AccessRule *rule = getAccessRule(certHash);
124 void AccessCondition::setAPDUAccessRule(const ByteArray &certHash,
127 AccessRule *access = getAccessRule(certHash);
129 if (access != NULL) {
130 access->setAPDUAccessRule(rule);
134 void AccessCondition::addAPDUAccessRule(const ByteArray &certHash,
135 const ByteArray &apdu, const ByteArray &mask)
137 AccessRule *access = getAccessRule(certHash);
139 if (access != NULL) {
140 access->addAPDUAccessRule(apdu, mask);
144 void AccessCondition::addAPDUAccessRule(const ByteArray &certHash,
145 const ByteArray &rule)
147 if (rule.size() != 8)
150 addAPDUAccessRule(certHash, rule.sub(0, 4), rule.sub(4, 4));
153 void AccessCondition::setNFCAccessRule(const ByteArray &certHash,
156 AccessRule *access = getAccessRule(certHash);
158 if (access != NULL) {
159 access->setNFCAccessRule(rule);
163 bool AccessCondition::isAuthorizedAPDUAccess(const ByteArray &certHash,
164 const ByteArray &command) const
167 const AccessRule *rule = getAccessRule(certHash);
170 result = rule->isAuthorizedAPDUAccess(command);
176 bool AccessCondition::isAuthorizedNFCAccess(const ByteArray &certHash) const
179 const AccessRule *rule = getAccessRule(certHash);
182 result = rule->isAuthorizedNFCAccess();
187 } /* namespace smartcard_service_api */