787bdd0dd82b30203b3555324bff34179140802e
[platform/core/connectivity/smartcard-service.git] / common / include / AccessCondition.h
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 #ifndef ACCESSCONDITION_H_
18 #define ACCESSCONDITION_H_
19
20 /* standard library header */
21 #include <map>
22 #include <vector>
23
24 /* SLP library header */
25
26 /* local header */
27 #include "ByteArray.h"
28
29 using namespace std;
30
31 namespace smartcard_service_api
32 {
33         class AccessRule
34         {
35         private :
36                 bool apduRule;
37                 bool nfcRule;
38                 vector<pair<ByteArray, ByteArray> > listFilters;
39
40         public :
41                 AccessRule() : apduRule(true), nfcRule(true) {}
42
43                 inline void setAPDUAccessRule(bool rule) { apduRule = rule; }
44                 inline void setNFCAccessRule(bool rule) { nfcRule = rule; }
45
46                 void addAPDUAccessRule(const ByteArray &apdu,
47                         const ByteArray &mask);
48
49                 inline bool isAuthorizedAccess(void) const
50                 {
51                         return (apduRule || (listFilters.size() > 0));
52                 }
53                 bool isAuthorizedAPDUAccess(const ByteArray &command) const;
54                 bool isAuthorizedNFCAccess(void) const;
55
56                 friend class AccessCondition;
57         };
58
59         class AccessCondition
60         {
61         private :
62                 bool permission;
63                 ByteArray aid;
64                 map<ByteArray, AccessRule> mapRules;
65
66         public :
67                 AccessCondition() : permission(false) {}
68
69                 inline void setAID(const ByteArray &aid) { this->aid = aid; }
70                 inline const ByteArray getAID() const { return aid; }
71                 inline void setAccessCondition(bool rule) { permission = rule; }
72                 void addAccessRule(const ByteArray &hash);
73
74                 void setAPDUAccessRule(const ByteArray &certHash, bool rule);
75                 void addAPDUAccessRule(const ByteArray &certHash,
76                         const ByteArray &apdu, const ByteArray &mask);
77                 void addAPDUAccessRule(const ByteArray &certHash,
78                         const ByteArray &rule);
79
80                 void setNFCAccessRule(const ByteArray &certHash, bool rule);
81
82                 bool isAuthorizedAccess(const ByteArray &certHash) const;
83                 bool isAuthorizedAPDUAccess(const ByteArray &certHash,
84                         const ByteArray &command) const;
85                 bool isAuthorizedNFCAccess(const ByteArray &certHash) const;
86
87                 AccessRule *getAccessRule(const ByteArray &certHash);
88                 const AccessRule *getAccessRule(const ByteArray &certHash) const;
89
90                 friend class AccessControlList;
91         };
92
93 } /* namespace smartcard_service_api */
94 #endif /* ACCESSCONDITION_H_ */