revise exported headers and cleanup headers
[platform/core/connectivity/smartcard-service.git] / common / AccessCondition.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdio.h>
18
19 #include "Debug.h"
20 #include "SimpleTLV.h"
21 #include "AccessControlList.h"
22 #include "AccessCondition.h"
23
24 namespace smartcard_service_api
25 {
26         void AccessRule::addAPDUAccessRule(const ByteArray &apdu,
27                 const ByteArray &mask)
28         {
29                 pair<ByteArray, ByteArray> item(apdu, mask);
30
31                 listFilters.push_back(item);
32         }
33
34         bool AccessRule::isAuthorizedAPDUAccess(const ByteArray &command) const
35         {
36                 bool result = false;
37
38                 if (command.size() < 4) /* apdu header size */
39                         return false;
40
41                 if (listFilters.size() > 0)
42                 {
43                         unsigned int cmd, mask, rule;
44                         vector<pair<ByteArray, ByteArray> >::const_iterator item;
45
46                         cmd = *(unsigned int *)command.getBuffer();
47                         for (item = listFilters.begin(); item != listFilters.end(); item++)
48                         {
49                                 mask = *(unsigned int *)item->second.getBuffer();
50                                 rule = *(unsigned int *)item->first.getBuffer();
51
52                                 if ((cmd & mask) == rule)
53                                 {
54                                         result = true;
55                                         break;
56                                 }
57                         }
58                 }
59                 else
60                 {
61                         /* no filter entry. if permission is true, all access will be granted, if not, all access will be denied */
62                         result = apduRule;
63                 }
64
65                 return result;
66         }
67
68         bool AccessRule::isAuthorizedNFCAccess(void) const
69         {
70                 return nfcRule;
71         }
72
73         AccessRule *AccessCondition::getAccessRule(const ByteArray &certHash)
74         {
75                 AccessRule *result = NULL;
76                 map<ByteArray, AccessRule>::iterator item;
77
78                 item = mapRules.find(certHash);
79                 if (item != mapRules.end()) {
80                         result = &item->second;
81                 }
82
83                 return result;
84         }
85
86         const AccessRule *AccessCondition::getAccessRule(const ByteArray &certHash) const
87         {
88                 const AccessRule *result = NULL;
89                 map<ByteArray, AccessRule>::const_iterator item;
90
91                 item = mapRules.find(certHash);
92                 if (item != mapRules.end()) {
93                         result = &item->second;
94                 }
95
96                 return result;
97         }
98
99         void AccessCondition::addAccessRule(const ByteArray &hash)
100         {
101                 AccessRule rule;
102
103                 pair<ByteArray, AccessRule> item(hash, rule);
104
105                 mapRules.insert(item);
106         }
107
108         bool AccessCondition::isAuthorizedAccess(const ByteArray &certHash) const
109         {
110                 bool result = permission;
111                 const AccessRule *rule = getAccessRule(certHash);
112
113                 if (rule != NULL) {
114                         result = true;
115                 }
116
117                 return result;
118         }
119
120         void AccessCondition::setAPDUAccessRule(const ByteArray &certHash,
121                 bool rule)
122         {
123                 AccessRule *access = getAccessRule(certHash);
124
125                 if (access != NULL) {
126                         access->setAPDUAccessRule(rule);
127                 }
128         }
129
130         void AccessCondition::addAPDUAccessRule(const ByteArray &certHash,
131                 const ByteArray &apdu, const ByteArray &mask)
132         {
133                 AccessRule *access = getAccessRule(certHash);
134
135                 if (access != NULL) {
136                         access->addAPDUAccessRule(apdu, mask);
137                 }
138         }
139
140         void AccessCondition::addAPDUAccessRule(const ByteArray &certHash,
141                 const ByteArray &rule)
142         {
143                 if (rule.size() != 8)
144                         return;
145
146                 addAPDUAccessRule(certHash, rule.sub(0, 4), rule.sub(4, 4));
147         }
148
149         void AccessCondition::setNFCAccessRule(const ByteArray &certHash,
150                         bool rule)
151         {
152                 AccessRule *access = getAccessRule(certHash);
153
154                 if (access != NULL) {
155                         access->setNFCAccessRule(rule);
156                 }
157         }
158
159         bool AccessCondition::isAuthorizedAPDUAccess(const ByteArray &certHash,
160                 const ByteArray &command) const
161         {
162                 bool result = false;
163                 const AccessRule *rule = getAccessRule(certHash);
164
165                 if (rule != NULL) {
166                         result = rule->isAuthorizedAPDUAccess(command);
167                 }
168
169                 return result;
170         }
171
172         bool AccessCondition::isAuthorizedNFCAccess(const ByteArray &certHash) const
173         {
174                 bool result = false;
175                 const AccessRule *rule = getAccessRule(certHash);
176
177                 if (rule != NULL) {
178                         result = rule->isAuthorizedNFCAccess();
179                 }
180
181                 return result;
182         }
183 } /* namespace smartcard_service_api */