import UploadFile from '@mui/icons-material/UploadFile' import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, FormControlLabel, MenuItem, Radio, RadioGroup, Stack, TextField, } from '@mui/material' import { useState, type FormEvent } from 'react' import { ROUTER_TEMPLATES, type Pki } from './api.ts' /** A request of POST /ca/certificates: a CSR to sign, or a subject and names for a key the server makes into a PFX file. */ export type IssueRequest = | { ca: string; template: string; csr: string } | { ca: string; template: string; subject: string; sans: string[]; password: string } /** The labels of the kinds of common name of a template. */ const SUBJECT_LABELS: Record = { dns: 'a DNS name', name: 'a name', email: 'an e-mail address' } /** A list typed as text separated by commas. */ function listOf(text: string) { return text.split(',').map((item) => item.trim()).filter((item) => item !== '') } /** * A dialog that issues a certificate from an active issuing CA with one of the templates it issues but those only the * router issues: from a CSR, pasted or read from a file, or with a key the server makes for a subject and subject * alternative names, returned once in a PFX file protected by a password. onSave sends the request and saves what the * router answers; the dialog closes when it succeeds and shows the server's message when it fails. */ export default function CertificateDialog(props: { pki: Pki; onSave: (request: IssueRequest) => Promise; onCancel: () => void }) { const authorities = props.pki.status.authorities .map((authority) => ({ ...authority, templates: authority.templates.filter((name) => !(ROUTER_TEMPLATES as readonly string[]).includes(name)) })) .filter((authority) => authority.state === 'active' && authority.templates.length > 0) const [ca, setCa] = useState(authorities[0]?.name ?? '') const [template, setTemplate] = useState(authorities[0]?.templates[0] ?? '') const [mode, setMode] = useState<'csr' | 'key'>('csr') const [csr, setCsr] = useState('') const [subject, setSubject] = useState('') const [sans, setSans] = useState('') const [password, setPassword] = useState('') const [repeated, setRepeated] = useState('') const [saving, setSaving] = useState(false) const [error, setError] = useState(null) const templates = authorities.find((authority) => authority.name === ca)?.templates ?? [] const values = props.pki.templates[template] const csrProblem = mode === 'csr' && csr.trim() !== '' && !csr.includes('-----BEGIN CERTIFICATE REQUEST-----') ? 'A PEM certificate signing request.' : '' const repeatProblem = mode === 'key' && repeated !== '' && repeated !== password ? 'The passwords differ.' : '' const blocked = ca === '' || template === '' || (mode === 'csr' ? csr.trim() === '' || csrProblem !== '' : subject.trim() === '' || password === '' || password !== repeated) async function submit(event: FormEvent) { event.preventDefault() if (blocked) { return } setSaving(true) setError(null) try { await props.onSave(mode === 'csr' ? { ca, template, csr: csr.trim() } : { ca, template, subject: subject.trim(), sans: listOf(sans), password }) } catch (caught) { setError((caught as Error).message) setSaving(false) } } return ( reason !== 'backdropClick' && props.onCancel()} maxWidth="sm" fullWidth slotProps={{ paper: { component: 'form', onSubmit: submit } }}> Issue a certificate {error && {error}} {authorities.length === 0 && No active issuing CA issues certificates for other systems; add one in Settings.} { setCa(event.target.value) setTemplate(authorities.find((authority) => authority.name === event.target.value)?.templates[0] ?? '') }} > {authorities.map((authority) => {`${authority.name}: ${authority.common_name}`})} setTemplate(event.target.value)}> {templates.map((name) => {name})} setMode(event.target.value as 'csr' | 'key')}> } label="Sign a CSR" /> } label="Make a key and a PFX file" /> {mode === 'csr' ? ( <> setCsr(event.target.value)} /> ) : ( <> setSubject(event.target.value)} /> setSans(event.target.value)} /> setPassword(event.target.value)} /> setRepeated(event.target.value)} /> )} ) }