Debian Codegraph @

pkg/storaged/crypto/tang.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8
9 import { ClipboardCopy } from "@patternfly/react-core/dist/esm/components/ClipboardCopy/index.js";
10 import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js";
11
12 import { useInit } from "hooks";
13
14 import stable_stringify from "json-stable-stringify-without-jsonify";
15
16 const _ = cockpit.gettext;
17
18 async function digest(text, hash) {
19 const encoder = new TextEncoder();
20 const data = encoder.encode(text);
21 const digest = await window.crypto.subtle.digest(hash, data);
22 return [...new Uint8Array(digest)];
23 }
24
25 export function validate_url(url) {
26 if (url.length === 0)
27 return _("Address cannot be empty");
28 if (!parse_url(url))
29 return _("Address is not a valid URL");
30 }
31
32 export function get_tang_adv(url) {
33 return cockpit.spawn(["curl", "-sSf", url + "/adv"], { err: "message" })
34 .then(JSON.parse)
35 .catch(error => {
36 return Promise.reject(error.toString().replace(/^curl: \([0-9]+\) /, ""));
37 });
38 }
39
40 function parse_url(url) {
41 // clevis-encrypt-tang defaults to "http://" (via curl), so we do the same here.
42 if (!/^[a-zA-Z]+:\/\//.test(url))
43 url = "http://" + url;
44 try {
45 return new URL(url);
46 } catch (e) {
47 if (e instanceof TypeError)
48 return null;
49 throw e;
50 }
51 }
52
53 function tang_adv_payload(adv) {
54 return JSON.parse(window.atob(adv.payload));
55 }
56
57 function jwk_b64_encode(bytes) {
58 // Use the urlsafe character set, and strip the padding.
59 return cockpit.base64_encode(bytes).replace(/\+/g, "-")
60 .replace(/\//g, "_")
61 .replace(/=+$/, '');
62 }
63
64 async function compute_thp(jwk) {
65 const REQUIRED_ATTRS = {
66 RSA: ['kty', 'p', 'd', 'q', 'dp', 'dq', 'qi', 'oth'],
67 EC: ['kty', 'crv', 'x', 'y'],
68 oct: ['kty', 'k'],
69 };
70
71 if (!jwk.kty)
72 return "(no key type attribute=";
73 if (!REQUIRED_ATTRS[jwk.kty])
74 return cockpit.format("(unknown keytype $0)", jwk.kty);
75
76 const req = REQUIRED_ATTRS[jwk.kty];
77 const norm = { };
78 req.forEach(k => { if (k in jwk) norm[k] = jwk[k]; });
79
80 const hashes = {};
81 try {
82 const sha256 = jwk_b64_encode(await digest(stable_stringify(norm), "SHA-256"));
83 hashes.sha256 = sha256;
84 } catch (err) {
85 console.warn("Unable to create a sha256 hash", err);
86 }
87
88 try {
89 const sha1 = jwk_b64_encode(await digest(stable_stringify(norm), "SHA-1"));
90 hashes.sha1 = sha1;
91 } catch (err) {
92 console.warn("Unable to create a sha1 hash", err);
93 }
94
95 return hashes;
96 }
97
98 function compute_sigkey_thps(adv) {
99 function is_signing_key(jwk) {
100 if (!jwk.use && !jwk.key_ops)
101 return true;
102 if (jwk.use == "sig")
103 return true;
104 if (jwk.key_ops && jwk.key_ops.indexOf("verify") >= 0)
105 return true;
106 return false;
107 }
108
109 return adv.keys.filter(is_signing_key).map(compute_thp);
110 }
111
112 export const TangKeyVerification = ({ url, adv }) => {
113 const parsed = parse_url(url);
114 const cmd = cockpit.format("ssh $0 tang-show-keys $1", parsed.hostname, parsed.port);
115 const [sigkey_thps, setSigKey] = React.useState(null);
116
117 useInit(async () => {
118 const sigkey = await Promise.all(compute_sigkey_thps(tang_adv_payload(adv)));
119 setSigKey(sigkey);
120 });
121
122 if (sigkey_thps === null)
123 return null;
124
125 return (
126 <>
127 <Content component={ContentVariants.p}>{_("Check the key hash with the Tang server.")}</Content>
128
129 <Content component={ContentVariants.h3}>{_("How to check")}</Content>
130 <span>{_("In a terminal, run: ")}</span>
131 <ClipboardCopy hoverTip={_("Copy to clipboard")}
132 clickTip={_("Successfully copied to clipboard!")}
133 variant="inline-compact"
134 isCode>
135 {cmd}
136 </ClipboardCopy>
137 <Content component={ContentVariants.p}>
138 {_("Check that the SHA-256 or SHA-1 hash from the command matches this dialog.")}
139 </Content>
140
141 <Content component={ContentVariants.h3}>{_("SHA-256")}</Content>
142 { sigkey_thps.map(s => <Content key={s.sha256} component={ContentVariants.pre}>{s.sha256}</Content>) }
143
144 <Content component={ContentVariants.h3}>{_("SHA-1")}</Content>
145 { sigkey_thps.map(s => <Content key={s.sha1} component={ContentVariants.pre}>{s.sha1}</Content>) }
146 </>);
147 };