Change the authorization sequence
[platform/core/connectivity/smartcard-service.git] / common / AccessControlList.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
18 /* standard library header */
19 #include <stdio.h>
20
21 /* SLP library header */
22
23 /* local header */
24 #include "Debug.h"
25 #include "AccessControlList.h"
26 #include "PKCS15.h"
27 #include "AccessCondition.h"
28
29 namespace smartcard_service_api
30 {
31         const unsigned char aid_all[] = { 0x00, 0x00 };
32         const unsigned char aid_default[] = { 0x00, 0x01 };
33
34         ByteArray AccessControlList::AID_ALL(ARRAY_AND_SIZE(aid_all));
35         ByteArray AccessControlList::AID_DEFAULT(ARRAY_AND_SIZE(aid_default));
36
37         AccessControlList::AccessControlList()
38         {
39                 allGranted = false;
40         }
41
42         AccessControlList::~AccessControlList()
43         {
44                 releaseACL();
45         }
46
47         void AccessControlList::releaseACL()
48         {
49                 mapConditions.clear();
50                 allGranted = false;
51         }
52
53         bool AccessControlList::isAuthorizedAccess(ByteArray aid, ByteArray certHash, bool update)
54         {
55                 bool result = allGranted;
56                 map<ByteArray, AccessCondition>::iterator iterMap;
57
58                 if (result == false)
59                 {
60                         SCARD_DEBUG("aid : %s", aid.toString());
61                         SCARD_DEBUG("hash : %s", certHash.toString());
62
63                         /* null aid means default applet */
64                         if (aid.isEmpty() == true)
65                         {
66                                 aid = AID_DEFAULT;
67                         }
68
69                         /* first.. find hashes matched with aid */
70                         if ((iterMap = mapConditions.find(aid)) != mapConditions.end())
71                         {
72                                 result = iterMap->second.isAuthorizedAccess(certHash);
73                         }
74                         /* finally.. find hashes in 'all' list */
75                         else if ((iterMap = mapConditions.find(AID_ALL)) != mapConditions.end())
76                         {
77                                 result = iterMap->second.isAuthorizedAccess(certHash);
78                         }
79                 }
80
81                 return result;
82         }
83
84         bool AccessControlList::isAuthorizedAccess(ByteArray aid, ByteArray certHash)
85         {
86                 return isAuthorizedAccess(aid, certHash, true);
87         }
88
89         bool AccessControlList::isAuthorizedAccess(unsigned char *aidBuffer, unsigned int aidLength, unsigned char *certHashBuffer, unsigned int certHashLength)
90         {
91                 return isAuthorizedAccess(ByteArray(aidBuffer, aidLength), ByteArray(certHashBuffer, certHashLength), true);
92         }
93
94         bool AccessControlList::isAuthorizedAccess(ByteArray aid, vector<ByteArray> &certHashes)
95         {
96                 bool result;
97                 size_t i;
98
99                 result = allGranted;
100
101                 if (result == false)
102                 {
103                         for (i = 0; result == false && i < certHashes.size(); i++)
104                         {
105                                 result = isAuthorizedAccess(aid, certHashes[i], false);
106                         }
107                 }
108
109                 return result;
110         }
111
112         void AccessControlList::printAccessControlList()
113         {
114                 ByteArray temp;
115
116                 /* release map and vector */
117                 map<ByteArray, AccessCondition>::iterator iterMap;
118
119                 SCARD_DEBUG("================ Certification Hashes ==================");
120                 for (iterMap = mapConditions.begin(); iterMap != mapConditions.end(); iterMap++)
121                 {
122                         temp = iterMap->first;
123
124                         SCARD_DEBUG("+ aid : %s", (temp == AID_DEFAULT) ? "DEFAULT" : (temp == AID_ALL) ? "ALL" : temp.toString());
125
126                         iterMap->second.printAccessConditions();
127                 }
128                 SCARD_DEBUG("========================================================");
129         }
130
131 } /* namespace smartcard_service_api */