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