import { Alert, Box, Button, Checkbox, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Stack, TextField, Typography, } from '@mui/material' import { useState, type FormEvent } from 'react' import { PKI_KEYS, PKI_PUBLISH, type Pki, type PkiSettings, type PkiTemplate } from './api.ts' /** The labels of the publishing endpoints. */ export const PUBLISH_LABELS: Record = { http: 'HTTP on the status port, served by the web service', 'http-80': 'HTTP on port 80, served by Caddy', https: 'HTTPS on the HTTPS status port, served by Caddy', 'https-default': 'HTTPS on port 443, served by Caddy', } /** The name of an issuing CA or a template, as the server accepts it. */ export const NAME_PATTERN = /^[a-z][a-z0-9-]{0,30}$/ /** The names the issuing CAs of pki.issuing_cas cannot have. */ export const RESERVED = ['root', 'p2s', 's2s'] /** The longest common name of a CA, in characters. */ const CA_NAME_MAX = 64 /** The longest lifetime the server accepts, in days. */ const DAYS_MAX = 36500 /** A new template's options. */ export const NEW_TEMPLATE: PkiTemplate = { key: 'rsa-3072', days: 365, key_usage: ['digital_signature'], extended_key_usage: [], subject: 'name', sans: [], policies: [], } /** A common name's problem, or ''. */ export function nameProblem(value: string) { return value.trim() === '' || value.trim().length > CA_NAME_MAX ? `1 to ${CA_NAME_MAX} characters.` : '' } /** A lifetime's problem, or ''. */ export function daysProblem(value: number) { return !Number.isInteger(value) || value < 1 || value > DAYS_MAX ? `1 to ${DAYS_MAX} days.` : '' } /** * 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, lifetimes and key type of * the CAs, the validity of the CRLs and * where certificates and CRLs are published. The server applies the names, 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 blocked = [ nameProblem(settings.root_name), 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), ].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. CAs {(['root', 'p2s', 's2s'] as const).map((name) => { const option = `${name}_name` as 'root_name' | '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) })} /> Published certificates and CRLs {PKI_PUBLISH.map((item) => ( change({ publish: PKI_PUBLISH.filter((name) => (name === item ? event.target.checked : settings.publish.includes(name))), })} /> } label={PUBLISH_LABELS[item]} /> ))} ) }