2 * Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
17 * @file access-control.cpp
18 * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
20 * @brief DB access control layer implementation.
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>
29 const uid_t SYSTEM_SVC_MAX_UID = (5000 - 1);
30 } // anonymous namespace
34 AccessControl::AccessControl() : m_ccMode(false)
38 void AccessControl::updateCCMode()
40 /* newMode should be extracted from global property like buxton in product */
43 if ((newMode == 1) == m_ccMode)
46 if (FIPS_mode_set(newMode) == 0) {
47 LogError("Error to FIPS_mode_set with param " << newMode);
51 m_ccMode = (newMode == 1);
54 bool AccessControl::isCCMode() const
59 bool AccessControl::isSystemService(const uid_t uid) const
61 return uid <= SYSTEM_SVC_MAX_UID;
64 bool AccessControl::isSystemService(const CKM::Credentials &cred) const
66 return isSystemService(cred.clientUid);
70 int AccessControl::canSave(
71 const CKM::Credentials &accessorCred,
72 const ClientId &owner) const
74 if (isSystemService(accessorCred))
75 return CKM_API_SUCCESS;
77 if (owner != accessorCred.client)
78 return CKM_API_ERROR_ACCESS_DENIED;
80 return CKM_API_SUCCESS;
83 int AccessControl::canModify(
84 const CKM::Credentials &accessorCred,
85 const ClientId &owner) const
87 return canSave(accessorCred, owner);
90 int AccessControl::canRead(
91 const CKM::Credentials &accessorCred,
92 const PermissionMask &existingPermission) const
94 if (isSystemService(accessorCred))
95 return CKM_API_SUCCESS;
97 if (existingPermission & Permission::READ)
98 return CKM_API_SUCCESS;
100 return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
103 int AccessControl::canExport(
104 const CKM::Credentials &accessorCred,
106 const PermissionMask &existingPermission) const
110 if (CKM_API_SUCCESS != (ec = canRead(accessorCred, existingPermission)))
113 // check if can export
114 if (row.exportable == 0)
115 return CKM_API_ERROR_NOT_EXPORTABLE;
117 // prevent extracting private keys during cc-mode on
118 if (isCCMode() && row.dataType.isKeyPrivate())
119 return CKM_API_ERROR_BAD_REQUEST;
121 return CKM_API_SUCCESS;
124 int AccessControl::canDelete(
125 const CKM::Credentials &accessorCred,
126 const PermissionMask &existingPermission) const
128 if (isSystemService(accessorCred))
129 return CKM_API_SUCCESS;
131 if (existingPermission & Permission::REMOVE)
132 return CKM_API_SUCCESS;
134 if (existingPermission & Permission::READ)
135 return CKM_API_ERROR_ACCESS_DENIED;
137 return CKM_API_ERROR_DB_ALIAS_UNKNOWN;