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 void AccessControl::updateCCMode()
36 /* newMode should be extracted from global property like buxton in product */
39 if ((newMode == 1) == m_ccMode)
42 if (FIPS_mode_set(newMode) == 0) {
43 LogError("Error to FIPS_mode_set with param " << newMode);
47 m_ccMode = (newMode == 1);
50 bool AccessControl::isCCMode() const
55 bool AccessControl::isSystemService(const uid_t uid) const
57 return uid <= SYSTEM_SVC_MAX_UID;
60 bool AccessControl::isSystemService(const CKM::Credentials &cred) const
62 return isSystemService(cred.clientUid);
66 int AccessControl::canSave(
67 const CKM::Credentials &accessorCred,
68 const ClientId &owner) const
70 if (isSystemService(accessorCred))
71 return CKM_API_SUCCESS;
73 if (owner != accessorCred.client)
74 return CKM_API_ERROR_ACCESS_DENIED;
76 return CKM_API_SUCCESS;
79 int AccessControl::canModify(
80 const CKM::Credentials &accessorCred,
81 const ClientId &owner) const
83 return canSave(accessorCred, owner);
86 int AccessControl::canRead(
87 const CKM::Credentials &accessorCred,
88 const PermissionMask &existingPermission) const
90 if (isSystemService(accessorCred))
91 return CKM_API_SUCCESS;
93 if (existingPermission & Permission::READ)
94 return CKM_API_SUCCESS;
96 return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
99 int AccessControl::canExport(
100 const CKM::Credentials &accessorCred,
102 const PermissionMask &existingPermission) const
106 if (CKM_API_SUCCESS != (ec = canRead(accessorCred, existingPermission)))
109 // check if can export
110 if (row.exportable == 0)
111 return CKM_API_ERROR_NOT_EXPORTABLE;
113 // prevent extracting private keys during cc-mode on
114 if (isCCMode() && row.dataType.isKeyPrivate())
115 return CKM_API_ERROR_BAD_REQUEST;
117 return CKM_API_SUCCESS;
120 int AccessControl::canDelete(
121 const CKM::Credentials &accessorCred,
122 const PermissionMask &existingPermission) const
124 if (isSystemService(accessorCred))
125 return CKM_API_SUCCESS;
127 if (existingPermission & Permission::REMOVE)
128 return CKM_API_SUCCESS;
130 if (existingPermission & Permission::READ)
131 return CKM_API_ERROR_ACCESS_DENIED;
133 return CKM_API_ERROR_DB_ALIAS_UNKNOWN;