0704147640b708ca6a298e552864dac2436ad0cb
[platform/core/security/key-manager.git] / src / manager / service / access-control.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 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  * @file        access-control.cpp
18  * @author      Maciej Karpiuk (m.karpiuk2@samsung.com)
19  * @version     1.0
20  * @brief       DB access control layer implementation.
21  */
22 #include <access-control.h>
23 #include <dpl/log/log.h>
24 #include <ckm/ckm-error.h>
25 #include <ckm/ckm-type.h>
26 #include <openssl/crypto.h>
27
28 namespace {
29 const uid_t SYSTEM_SVC_MAX_UID = (5000 - 1);
30 } // anonymous namespace
31
32 namespace CKM {
33
34 void AccessControl::updateCCMode()
35 {
36         /* newMode should be extracted from global property like buxton in product */
37         int newMode = 0;
38
39         if ((newMode == 1) == m_ccMode)
40                 return;
41
42         if (FIPS_mode_set(newMode) == 0) {
43                 LogError("Error to FIPS_mode_set with param " << newMode);
44                 return;
45         }
46
47         m_ccMode = (newMode == 1);
48 }
49
50 bool AccessControl::isCCMode() const
51 {
52         return m_ccMode;
53 }
54
55 bool AccessControl::isSystemService(const uid_t uid) const
56 {
57         return uid <= SYSTEM_SVC_MAX_UID;
58 }
59
60 bool AccessControl::isSystemService(const CKM::Credentials &cred) const
61 {
62         return isSystemService(cred.clientUid);
63 }
64
65
66 int AccessControl::canSave(
67         const CKM::Credentials &accessorCred,
68         const ClientId &owner) const
69 {
70         if (isSystemService(accessorCred))
71                 return CKM_API_SUCCESS;
72
73         if (owner != accessorCred.client)
74                 return CKM_API_ERROR_ACCESS_DENIED;
75
76         return CKM_API_SUCCESS;
77 }
78
79 int AccessControl::canModify(
80         const CKM::Credentials &accessorCred,
81         const ClientId &owner) const
82 {
83         return canSave(accessorCred, owner);
84 }
85
86 int AccessControl::canRead(
87         const CKM::Credentials &accessorCred,
88         const PermissionMask &existingPermission) const
89 {
90         if (isSystemService(accessorCred))
91                 return CKM_API_SUCCESS;
92
93         if (existingPermission & Permission::READ)
94                 return CKM_API_SUCCESS;
95
96         return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
97 }
98
99 int AccessControl::canExport(
100         const CKM::Credentials &accessorCred,
101         const DB::Row &row,
102         const PermissionMask &existingPermission) const
103 {
104         int ec;
105
106         if (CKM_API_SUCCESS != (ec = canRead(accessorCred, existingPermission)))
107                 return ec;
108
109         // check if can export
110         if (row.exportable == 0)
111                 return CKM_API_ERROR_NOT_EXPORTABLE;
112
113         // prevent extracting private keys during cc-mode on
114         if (isCCMode() && row.dataType.isKeyPrivate())
115                 return CKM_API_ERROR_BAD_REQUEST;
116
117         return CKM_API_SUCCESS;
118 }
119
120 int AccessControl::canDelete(
121         const CKM::Credentials &accessorCred,
122         const PermissionMask &existingPermission) const
123 {
124         if (isSystemService(accessorCred))
125                 return CKM_API_SUCCESS;
126
127         if (existingPermission & Permission::REMOVE)
128                 return CKM_API_SUCCESS;
129
130         if (existingPermission & Permission::READ)
131                 return CKM_API_ERROR_ACCESS_DENIED;
132
133         return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
134 }
135
136
137
138 } // namespace CKM