import type { PkiAuthority, PkiRootSource, PkiTemplate } from './api.ts' /** The labels of the sources of the root CA. */ export const ROOT_SOURCE_LABELS: Record = { local: 'Created by the server', offline: 'Offline', 'simple-ca': 'Simple CA', } /** 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', 'upstream', 'p2s', 's2s', 'servers'] /** 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: [], } /** Whether an issuing CA has its certificate or its CSR, which fix its common name. */ export function made(authority: PkiAuthority) { return authority.state === 'active' || authority.state === 'pending' } /** 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.` : '' } /** The problem of the URL of a Simple CA service, or ''. */ export function urlProblem(value: string) { try { const url = new URL(value.trim()) return url.protocol === 'https:' && url.hostname !== '' && url.search === '' && url.hash === '' ? '' : 'An https:// URL.' } catch { return 'An https:// URL.' } } /** A lifetime's problem, or ''. */ export function daysProblem(value: number) { return !Number.isInteger(value) || value < 1 || value > DAYS_MAX ? `1 to ${DAYS_MAX} days.` : '' }