Debian Codegraph @

pkg/systemd/overview-cards/cryptoPolicies.jsx

raw
1 /*
2 * Copyright (C) 2022 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React, { useState } from 'react';
8 import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js";
9 import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js";
10 import {
11 Modal, ModalBody, ModalFooter, ModalHeader
12 } from '@patternfly/react-core/dist/esm/components/Modal/index.js';
13 import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js";
14 import { ExclamationTriangleIcon, ExternalLinkSquareAltIcon, HelpIcon } from '@patternfly/react-icons';
15
16 import { ModalError } from 'cockpit-components-inline-notification.jsx';
17 import { PrivilegedButton } from "cockpit-components-privileged";
18 import { ProfilesMenuDialogBody } from "./profiles-menu-dialog-body";
19 import { useDialogs } from "dialogs.jsx";
20 import { useInit } from "hooks";
21
22 import "./cryptoPolicies.scss";
23
24 const _ = cockpit.gettext;
25
26 const displayProfileText = profile => profile === "DEFAULT" ? _("Default") : profile;
27 const isInconsistentPolicy = (policy, fipsEnabled) => policy === "FIPS" !== fipsEnabled;
28
29 const getFipsConfigurable = () => cockpit.spawn(["/bin/sh", "-c", "command -v fips-mode-setup"], { error: "ignore" })
30 .then(() => true)
31 .catch(() => false);
32
33 export const CryptoPolicyRow = () => {
34 const Dialogs = useDialogs();
35 const [currentCryptoPolicy, setCurrentCryptoPolicy] = useState(null);
36 const [fipsEnabled, setFipsEnabled] = useState(null);
37 const [fipsConfigurable, setFipsConfigurable] = useState(null);
38 const [shaSubPolicyAvailable, setShaSubPolicyAvailable] = useState(null);
39
40 useInit(() => {
41 cockpit.file("/proc/sys/crypto/fips_enabled").read()
42 .then(content => setFipsEnabled(content ? content.trim() === "1" : false));
43 getFipsConfigurable().then(v => setFipsConfigurable(v));
44 cockpit.file("/etc/crypto-policies/config")
45 .watch(async contents => {
46 // Ask crypto-policies to get correct FIPS state, as that dominates the configured policy
47 try {
48 setCurrentCryptoPolicy((await cockpit.spawn(["update-crypto-policies", "--show"])).trim());
49 } catch (error) {
50 console.warn("Failed to get current crypto policy:", error.toString(),
51 "; falling back to /etc/crypto-policies/config");
52 const filteredContent = contents?.split('\n').filter(line => !line.startsWith("#")).join('\n');
53 setCurrentCryptoPolicy(filteredContent?.trim() ?? null);
54 }
55 });
56 // RHEL-8-8 has no SHA1 subpolicy
57 cockpit.file("/usr/share/crypto-policies/policies/modules/SHA1.pmod").read()
58 .then(content => setShaSubPolicyAvailable(content ? content.trim() : false));
59 });
60
61 if (currentCryptoPolicy === null || fipsEnabled === null || fipsConfigurable === null)
62 return null;
63
64 const policyRender = (currentCryptoPolicy.startsWith("FIPS") && !fipsConfigurable)
65 /* read-only mode; can't switch away from FIPS without fips-mode-setup */
66 ? <span id="crypto-policy-current">{displayProfileText(currentCryptoPolicy)}</span>
67 : <PrivilegedButton variant="link" buttonId="crypto-policy-button" tooltipId="tip-crypto-policy"
68 excuse={ _("The user $0 is not permitted to change cryptographic policies") }
69 onClick={() => Dialogs.show(<CryptoPolicyDialog
70 currentCryptoPolicy={currentCryptoPolicy}
71 setCurrentCryptoPolicy={setCurrentCryptoPolicy}
72 fipsEnabled={fipsEnabled}
73 fipsConfigurable={fipsConfigurable}
74 shaSubPolicyAvailable={shaSubPolicyAvailable} />)}>
75 {displayProfileText(currentCryptoPolicy)}
76 </PrivilegedButton>;
77
78 return (
79 <tr className="pf-v6-c-table__tr">
80 <th className="pf-v6-c-table__th" scope="row">{_("Cryptographic policy")}</th>
81 <td className="pf-v6-c-table__td">{policyRender}</td>
82 </tr>
83 );
84 };
85
86 const setPolicy = async (policy, setError, setInProgress, fipsConfigurable) => {
87 setInProgress(true);
88
89 try {
90 if (policy === "FIPS") {
91 cockpit.assert(fipsConfigurable, "calling setPolicy(FIPS) without fips-mode-setup");
92 await cockpit.spawn(["fips-mode-setup", "--enable"], { superuser: "require", err: "message" });
93 } else {
94 if (fipsConfigurable)
95 await cockpit.spawn(["fips-mode-setup", "--disable"], { superuser: "require", err: "message" });
96 await cockpit.spawn(["update-crypto-policies", "--set", policy], { superuser: "require", err: "message" });
97 }
98
99 await cockpit.spawn(["shutdown", "--reboot", "now"], { superuser: "require", err: "message" });
100 } catch (error) {
101 setError(error);
102 } finally {
103 setInProgress(false);
104 }
105 };
106
107 const CryptoPolicyDialog = ({
108 currentCryptoPolicy,
109 fipsEnabled,
110 fipsConfigurable,
111 reApply,
112 shaSubPolicyAvailable,
113 }) => {
114 const Dialogs = useDialogs();
115 const [error, setError] = useState();
116 const [inProgress, setInProgress] = useState(false);
117 const [selected, setSelected] = useState(currentCryptoPolicy);
118
119 // Found in /usr/share/crypto-policies/policies/
120 const cryptopolicies = {
121 DEFAULT: _("Recommended, secure settings for current threat models."),
122 "DEFAULT:SHA1": _("DEFAULT with SHA-1 signature verification allowed."),
123 LEGACY: _("Higher interoperability at the cost of an increased attack surface."),
124 "LEGACY:AD-SUPPORT": _("LEGACY with Active Directory interoperability."),
125 FIPS: (<Flex alignItems={{ default: 'alignItemsCenter' }}>
126 {_("Only use approved and allowed algorithms when booting in FIPS mode.")}
127 <Button component='a'
128 rel="noopener noreferrer" target="_blank"
129 variant='link'
130 isInline
131 icon={<ExternalLinkSquareAltIcon />} iconPosition="right"
132 href="https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/security_hardening/using-the-system-wide-cryptographic-policies_security-hardening">
133 {_("Learn more")}
134 </Button>
135 </Flex>),
136 "FIPS:OSPP": _("FIPS with further Common Criteria restrictions."),
137 FUTURE: _("Protects from anticipated near-term future attacks at the expense of interoperability."),
138 };
139
140 const policies = Object.keys(cryptopolicies)
141 .filter(pol => pol.endsWith(':SHA1') ? shaSubPolicyAvailable : true)
142 // cannot enable fips without fips-mode-setup
143 .filter(pol => pol.startsWith("FIPS") ? fipsConfigurable : true)
144 .map(policy => ({
145 name: policy,
146 title: displayProfileText(policy),
147 description: cryptopolicies[policy],
148 active: !isInconsistentPolicy(policy, fipsEnabled) && policy === currentCryptoPolicy,
149 inconsistent: isInconsistentPolicy(policy, fipsEnabled) && policy === currentCryptoPolicy,
150 recommended: policy === 'DEFAULT',
151 }));
152
153 // Custom profile
154 if (!(currentCryptoPolicy in cryptopolicies)) {
155 policies.push({
156 name: currentCryptoPolicy,
157 title: displayProfileText(currentCryptoPolicy),
158 description: _("Custom cryptographic policy"),
159 active: !isInconsistentPolicy(currentCryptoPolicy, fipsEnabled),
160 inconsistent: isInconsistentPolicy(currentCryptoPolicy, fipsEnabled),
161 recommended: false,
162 });
163 }
164
165 const help = (
166 <Popover
167 id="crypto-policies-help"
168 bodyContent={
169 <div>
170 {_("Cryptographic Policies is a system component that configures the core cryptographic subsystems, covering the TLS, IPSec, SSH, DNSSec, and Kerberos protocols.")}
171 </div>
172 }
173 footerContent={
174 <Button component='a'
175 rel="noopener noreferrer" target="_blank"
176 variant='link'
177 isInline
178 icon={<ExternalLinkSquareAltIcon />} iconPosition="right"
179 href="https://docs.redhat.com/en/documentation/red_hat_enterprise_linux/9/html/security_hardening/using-the-system-wide-cryptographic-policies_security-hardening">
180 {_("Learn more")}
181 </Button>
182 }
183 >
184 <Button icon={<HelpIcon />} variant="plain" aria-label={_("Help")} />
185 </Popover>
186 );
187
188 return (
189 <Modal position="top" variant="medium"
190 className="ct-m-stretch-body"
191 isOpen
192 onClose={Dialogs.close}
193 id="crypto-policy-dialog"
194 >
195 <ModalHeader title={_("Change cryptographic policy")}
196 help={help}
197 />
198 <ModalBody>
199 {error && <ModalError dialogError={typeof error == 'string' ? error : error.message} />}
200 {currentCryptoPolicy && <ProfilesMenuDialogBody active_profile={currentCryptoPolicy}
201 change_selected={setSelected}
202 isDisabled={inProgress}
203 profiles={policies} />}
204 </ModalBody>
205 <ModalFooter>
206 {inProgress &&
207 <Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}>
208 {_("Applying new policy... This may take a few minutes.")}
209 </Flex>}
210 <Button id="crypto-policy-save-reboot" variant='primary'
211 onClick={() => setPolicy(selected, setError, setInProgress, fipsConfigurable)}
212 isDisabled={inProgress} isLoading={inProgress}
213 >
214 {reApply ? _("Reapply and reboot") : _("Apply and reboot")}
215 </Button>
216 <Button variant='link' onClick={Dialogs.close} isDisabled={inProgress}>
217 {_("Cancel")}
218 </Button>
219 </ModalFooter>
220 </Modal>
221 );
222 };
223
224 export const CryptoPolicyStatus = () => {
225 const Dialogs = useDialogs();
226 const [currentCryptoPolicy, setCurrentCryptoPolicy] = useState(null);
227 const [fipsEnabled, setFipsEnabled] = useState(null);
228 const [fipsConfigurable, setFipsConfigurable] = useState(null);
229
230 useInit(() => {
231 cockpit.file("/etc/crypto-policies/state/current")
232 .watch(content => setCurrentCryptoPolicy(content ? content.trim().split(':', 1)[0] : undefined));
233 getFipsConfigurable().then(v => setFipsConfigurable(v));
234 cockpit.file("/proc/sys/crypto/fips_enabled").read()
235 .then(content => setFipsEnabled(content ? content.trim() === "1" : false));
236 });
237
238 if (currentCryptoPolicy === null || fipsConfigurable === null)
239 return null;
240
241 if (isInconsistentPolicy(currentCryptoPolicy, fipsEnabled)) {
242 return (
243 <li className="system-health-crypto-policies">
244 <Flex flexWrap={{ default: 'nowrap' }}>
245 <FlexItem><ExclamationTriangleIcon className="crypto-policies-health-card-icon" /></FlexItem>
246 <div>
247 <div id="inconsistent_crypto_policy">
248 {currentCryptoPolicy === "FIPS" ? _("FIPS is not properly enabled") : _("Cryptographic policy is inconsistent")}
249 </div>
250 <Button isInline variant="link" className="pf-v6-u-font-size-sm"
251 onClick={() => Dialogs.show(<CryptoPolicyDialog currentCryptoPolicy={currentCryptoPolicy}
252 fipsEnabled={fipsEnabled}
253 fipsConfigurable={fipsConfigurable}
254 reApply />)}>
255 {_("Review cryptographic policy")}
256 </Button>
257 </div>
258 </Flex>
259 </li>
260 );
261 }
262
263 return null;
264 };