Fix SVACE and C++ issues
[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 AccessControl::AccessControl() : m_ccMode(false)
35 {
36 }
37
38 void AccessControl::updateCCMode()
39 {
40         /* newMode should be extracted from global property like buxton in product */
41         int newMode = 0;
42
43         if ((newMode == 1) == m_ccMode)
44                 return;
45
46         if (FIPS_mode_set(newMode) == 0) {
47                 LogError("Error to FIPS_mode_set with param " << newMode);
48                 return;
49         }
50
51         m_ccMode = (newMode == 1);
52 }
53
54 bool AccessControl::isCCMode() const
55 {
56         return m_ccMode;
57 }
58
59 bool AccessControl::isSystemService(const uid_t uid) const
60 {
61         return uid <= SYSTEM_SVC_MAX_UID;
62 }
63
64 bool AccessControl::isSystemService(const CKM::Credentials &cred) const
65 {
66         return isSystemService(cred.clientUid);
67 }
68
69
70 int AccessControl::canSave(
71         const CKM::Credentials &accessorCred,
72         const ClientId &owner) const
73 {
74         if (isSystemService(accessorCred))
75                 return CKM_API_SUCCESS;
76
77         if (owner != accessorCred.client)
78                 return CKM_API_ERROR_ACCESS_DENIED;
79
80         return CKM_API_SUCCESS;
81 }
82
83 int AccessControl::canModify(
84         const CKM::Credentials &accessorCred,
85         const ClientId &owner) const
86 {
87         return canSave(accessorCred, owner);
88 }
89
90 int AccessControl::canRead(
91         const CKM::Credentials &accessorCred,
92         const PermissionMask &existingPermission) const
93 {
94         if (isSystemService(accessorCred))
95                 return CKM_API_SUCCESS;
96
97         if (existingPermission & Permission::READ)
98                 return CKM_API_SUCCESS;
99
100         return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
101 }
102
103 int AccessControl::canExport(
104         const CKM::Credentials &accessorCred,
105         const DB::Row &row,
106         const PermissionMask &existingPermission) const
107 {
108         int ec;
109
110         if (CKM_API_SUCCESS != (ec = canRead(accessorCred, existingPermission)))
111                 return ec;
112
113         // check if can export
114         if (row.exportable == 0)
115                 return CKM_API_ERROR_NOT_EXPORTABLE;
116
117         // prevent extracting private keys during cc-mode on
118         if (isCCMode() && row.dataType.isKeyPrivate())
119                 return CKM_API_ERROR_BAD_REQUEST;
120
121         return CKM_API_SUCCESS;
122 }
123
124 int AccessControl::canDelete(
125         const CKM::Credentials &accessorCred,
126         const PermissionMask &existingPermission) const
127 {
128         if (isSystemService(accessorCred))
129                 return CKM_API_SUCCESS;
130
131         if (existingPermission & Permission::REMOVE)
132                 return CKM_API_SUCCESS;
133
134         if (existingPermission & Permission::READ)
135                 return CKM_API_ERROR_ACCESS_DENIED;
136
137         return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
138 }
139
140
141
142 } // namespace CKM