tizen 2.4 release
[framework/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 CKM {
29
30 void AccessControl::updateCCMode() {
31     int fipsModeStatus = 0;
32     int rc = 0;
33     bool newMode = false;
34
35     if (newMode == m_ccMode)
36         return;
37
38     m_ccMode = newMode;
39
40     fipsModeStatus = FIPS_mode();
41
42     if (m_ccMode) {
43         if (fipsModeStatus == 0) { // If FIPS mode off
44             rc = FIPS_mode_set(1); // Change FIPS_mode from off to on
45             if (rc == 0) {
46                 LogError("Error in FIPS_mode_set function");
47             }
48         }
49     } else {
50         if (fipsModeStatus == 1) { // If FIPS mode on
51             rc = FIPS_mode_set(0); // Change FIPS_mode from on to off
52             if (rc == 0) {
53                 LogError("Error in FIPS_mode_set function");
54             }
55         }
56     }
57 }
58
59 bool AccessControl::isCCMode() const
60 {
61     return m_ccMode;
62 }
63
64 int AccessControl::canSave(
65         const Label & ownerLabel,
66         const Label & accessorLabel) const
67 {
68     if(ownerLabel != accessorLabel)
69         return CKM_API_ERROR_ACCESS_DENIED;
70
71     return CKM_API_SUCCESS;
72 }
73
74 int AccessControl::canModify(
75         const Label & ownerLabel,
76         const Label & accessorLabel) const
77 {
78     return canSave(ownerLabel, accessorLabel);
79 }
80
81 int AccessControl::canRead(
82         const PermissionForLabel & permissionLabel) const
83 {
84     if(permissionLabel & Permission::READ)
85         return CKM_API_SUCCESS;
86
87     return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
88 }
89
90 int AccessControl::canExport(
91         const DB::Row & row,
92         const PermissionForLabel & permissionLabel) const
93 {
94     int ec;
95     if(CKM_API_SUCCESS != (ec = canRead(permissionLabel)))
96         return ec;
97
98     // check if can export
99     if(row.exportable == 0)
100         return CKM_API_ERROR_NOT_EXPORTABLE;
101
102     // prevent extracting private keys during cc-mode on
103     if (isCCMode() && row.dataType.isKeyPrivate())
104         return CKM_API_ERROR_BAD_REQUEST;
105
106     return CKM_API_SUCCESS;
107 }
108
109 int AccessControl::canDelete(
110         const PermissionForLabel & permissionLabel) const
111 {
112     if(permissionLabel & Permission::REMOVE)
113         return CKM_API_SUCCESS;
114     if(permissionLabel & Permission::READ)
115         return CKM_API_ERROR_ACCESS_DENIED;
116
117     return CKM_API_ERROR_DB_ALIAS_UNKNOWN;
118 }
119
120
121
122 } // namespace CKM