import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, MenuItem, Stack, TextField, Typography, } from '@mui/material' import { useState, type FormEvent } from 'react' import { PKI_KEYS, type Pki, type PkiSettings } from './api.ts' import { ROOT_SOURCE_LABELS, daysProblem, made, nameProblem, urlProblem } from './pki.ts' /** * A dialog that edits the server's pki section but the general-purpose issuing CAs, the templates and the custom * extended key usages, which IssuingCaDialog, TemplateDialog and EkuDialog edit: the names of the P2S and site-to-site * CAs, the lifetimes and key type of the CAs, the validity of the CRLs and, with a Simple CA root, the URL of the * service. The source of the root is shown and not edited, as it is fixed once * the root exists. The root and the Servers CA are made by the first setup, so their names are not shown; the P2S and * site-to-site CAs are made when the VPN and the router are switched on, and their names are fixed from then on. The * server applies the lifetimes and key type when it creates a CA. onSave sends the settings; the dialog closes when it * succeeds and shows the server's message when it fails. */ export default function PkiSettingsDialog(props: { pki: Pki onSave: (settings: Omit) => Promise onCancel: () => void }) { const [settings, setSettings] = useState(props.pki.settings) const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const change = (values: Partial) => setSettings({ ...settings, ...values }) const fixed = (name: string) => props.pki.status.authorities.some((authority) => authority.name === name && made(authority)) const blocked = [ nameProblem(settings.p2s_name), nameProblem(settings.s2s_name), daysProblem(settings.root_days), daysProblem(settings.ca_days), daysProblem(settings.crl_days), daysProblem(settings.root_crl_days), settings.root === 'simple-ca' ? urlProblem(settings.simple_ca_url) : '', ].some((problem) => problem !== '') async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) const { issuing_cas: _issuingCas, templates: _templates, extended_key_usages: _usages, ...rest } = settings try { await props.onSave(rest) } catch (caught) { setError((caught as Error).message) setSaving(false) } } const number = (value: string) => Math.trunc(Number(value) || 0) return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="md" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> Edit PKI settings {error && {error}} Names, lifetimes and the key type apply when the server creates a CA; URLs apply to the certificates signed afterwards. The P2S and site-to-site CAs are created when the VPN and the router are switched on. CAs {settings.root === 'simple-ca' && ( change({ simple_ca_url: event.target.value.trim() })} /> )} {(['p2s', 's2s'] as const).map((name) => { const option = `${name}_name` as 'p2s_name' | 's2s_name' return ( change({ [option]: event.target.value })} /> ) })} change({ root_days: number(event.target.value) })} /> change({ ca_days: number(event.target.value) })} /> change({ key: event.target.value })}> {PKI_KEYS.map((item) => {item})} CRLs change({ crl_days: number(event.target.value) })} /> change({ root_crl_days: number(event.target.value) })} /> ) }