update the latest source
[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                 channel = NULL;
40                 terminal = NULL;
41         }
42
43         AccessControlList::AccessControlList(Channel *channel)
44         {
45                 channel = NULL;
46                 terminal = NULL;
47
48                 setChannel(channel);
49         }
50
51         AccessControlList::AccessControlList(Terminal *terminal)
52         {
53                 channel = NULL;
54                 terminal = NULL;
55
56                 setTerminal(terminal);
57         }
58
59         AccessControlList::~AccessControlList()
60         {
61                 releaseACL();
62
63                 if (terminal != NULL && channel != NULL)
64                 {
65                         delete channel;
66                 }
67         }
68
69         int AccessControlList::setChannel(Channel *channel)
70         {
71                 this->channel = channel;
72
73                 return 0;
74         }
75
76         int AccessControlList::updateACL()
77         {
78                 releaseACL();
79
80                 return loadACL();
81         }
82
83         void AccessControlList::releaseACL()
84         {
85                 mapConditions.clear();
86         }
87
88         bool AccessControlList::isAuthorizedAccess(ByteArray aid, ByteArray certHash)
89         {
90                 bool result = false;
91                 map<ByteArray, AccessCondition>::iterator iterMap;
92
93                 SCARD_DEBUG("aid : %s", aid.toString());
94                 SCARD_DEBUG("hash : %s", certHash.toString());
95
96                 /* null aid means default applet */
97                 if (aid.isEmpty() == true)
98                 {
99                         aid = AID_DEFAULT;
100                 }
101
102                 /* first.. find hashes matched with aid */
103                 if ((iterMap = mapConditions.find(aid)) != mapConditions.end())
104                 {
105                         result = iterMap->second.isAuthorizedAccess(certHash);
106                 }
107                 /* finally.. find hashes in 'all' list */
108                 else if ((iterMap = mapConditions.find(AID_ALL)) != mapConditions.end())
109                 {
110                         result = iterMap->second.isAuthorizedAccess(certHash);
111                 }
112
113                 return result;
114         }
115
116         bool AccessControlList::isAuthorizedAccess(unsigned char *aidBuffer, unsigned int aidLength, unsigned char *certHashBuffer, unsigned int certHashLength)
117         {
118                 return isAuthorizedAccess(ByteArray(aidBuffer, aidLength), ByteArray(certHashBuffer, certHashLength));
119         }
120
121         void AccessControlList::printAccessControlList()
122         {
123                 ByteArray temp;
124
125                 /* release map and vector */
126                 map<ByteArray, AccessCondition>::iterator iterMap;
127
128                 SCARD_DEBUG("================ Certification Hashes ==================");
129                 for (iterMap = mapConditions.begin(); iterMap != mapConditions.end(); iterMap++)
130                 {
131                         temp = iterMap->first;
132
133                         SCARD_DEBUG("+ aid : %s", (temp == AID_DEFAULT) ? "DEFAULT" : (temp == AID_ALL) ? "ALL" : temp.toString());
134
135                         iterMap->second.printAccessConditions();
136                 }
137                 SCARD_DEBUG("========================================================");
138         }
139
140 } /* namespace smartcard_service_api */