178 lines
7.6 KiB
TypeScript
178 lines
7.6 KiB
TypeScript
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<string, string> = {
|
|
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<PkiSettings, 'issuing_cas' | 'templates' | 'extended_key_usages'>) => Promise<void>
|
|
onCancel: () => void
|
|
}) {
|
|
const [settings, setSettings] = useState<PkiSettings>(props.pki.settings)
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
const change = (values: Partial<PkiSettings>) => 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 (
|
|
<Dialog open onClose={(_event, reason) => reason !== 'backdropClick' && props.onCancel()} maxWidth="md" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}>
|
|
<DialogTitle>Edit PKI settings</DialogTitle>
|
|
<DialogContent>
|
|
<Stack sx={{ gap: 2, pt: 1 }}>
|
|
{error && <Alert severity="error" sx={{ whiteSpace: 'pre-line' }}>{error}</Alert>}
|
|
<Typography variant="body2" color="text.secondary">
|
|
Names, lifetimes and the key type apply when the server creates a CA; URLs apply to the certificates signed afterwards.
|
|
</Typography>
|
|
<Typography variant="subtitle2">CAs</Typography>
|
|
{(['root', 'p2s', 's2s'] as const).map((name) => {
|
|
const option = `${name}_name` as 'root_name' | 'p2s_name' | 's2s_name'
|
|
return (
|
|
<TextField
|
|
key={name}
|
|
label={{ root: 'Root CA name', p2s: 'P2S CA name', s2s: 'Site-to-site CA name' }[name]}
|
|
value={settings[option]}
|
|
autoComplete="off"
|
|
error={nameProblem(settings[option]) !== ''}
|
|
helperText={nameProblem(settings[option])}
|
|
onChange={(event) => change({ [option]: event.target.value })}
|
|
/>
|
|
)
|
|
})}
|
|
<Stack direction="row" sx={{ gap: 2 }}>
|
|
<TextField
|
|
fullWidth
|
|
label="Root days"
|
|
type="number"
|
|
value={settings.root_days}
|
|
error={daysProblem(settings.root_days) !== ''}
|
|
helperText={daysProblem(settings.root_days)}
|
|
onChange={(event) => change({ root_days: number(event.target.value) })}
|
|
/>
|
|
<TextField
|
|
fullWidth
|
|
label="Issuing CA days"
|
|
type="number"
|
|
value={settings.ca_days}
|
|
error={daysProblem(settings.ca_days) !== ''}
|
|
helperText={daysProblem(settings.ca_days)}
|
|
onChange={(event) => change({ ca_days: number(event.target.value) })}
|
|
/>
|
|
<TextField select fullWidth label="CA key" value={settings.key} onChange={(event) => change({ key: event.target.value })}>
|
|
{PKI_KEYS.map((item) => <MenuItem key={item} value={item}>{item}</MenuItem>)}
|
|
</TextField>
|
|
</Stack>
|
|
<Typography variant="subtitle2">CRLs</Typography>
|
|
<Stack direction="row" sx={{ gap: 2 }}>
|
|
<TextField
|
|
fullWidth
|
|
label="Issuing CA CRL days"
|
|
type="number"
|
|
value={settings.crl_days}
|
|
error={daysProblem(settings.crl_days) !== ''}
|
|
helperText={daysProblem(settings.crl_days) || 'Signed again at every revocation and daily after half of it.'}
|
|
onChange={(event) => change({ crl_days: number(event.target.value) })}
|
|
/>
|
|
<TextField
|
|
fullWidth
|
|
label="Root CRL days"
|
|
type="number"
|
|
value={settings.root_crl_days}
|
|
error={daysProblem(settings.root_crl_days) !== ''}
|
|
helperText={daysProblem(settings.root_crl_days) || 'When the server signs it, with the root key on the server.'}
|
|
onChange={(event) => change({ root_crl_days: number(event.target.value) })}
|
|
/>
|
|
</Stack>
|
|
<Typography variant="subtitle2">Published certificates and CRLs</Typography>
|
|
<Box>
|
|
{PKI_PUBLISH.map((item) => (
|
|
<FormControlLabel
|
|
key={item}
|
|
sx={{ display: 'flex' }}
|
|
control={
|
|
<Checkbox
|
|
checked={settings.publish.includes(item)}
|
|
onChange={(event) => change({
|
|
publish: PKI_PUBLISH.filter((name) => (name === item ? event.target.checked : settings.publish.includes(name))),
|
|
})}
|
|
/>
|
|
}
|
|
label={PUBLISH_LABELS[item]}
|
|
/>
|
|
))}
|
|
</Box>
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions>
|
|
<Button onClick={props.onCancel}>Cancel</Button>
|
|
<Button type="submit" variant="contained" disabled={saving || blocked}>Save</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
)
|
|
}
|