#!/bin/bash # Copyright 2026 Gentoo Authors # Distributed under the terms of the GNU General Public License v2 # Per-user loopback TLS setup, following the AUR launcher approach. set -uo pipefail _af_prepare_identity() ( umask 077 local directory=$1 temporary pfx pfx="${directory}/autofirma.pfx" temporary=$(mktemp -d "${directory}/.tls-XXXXXX") || return trap 'rm -rf -- "${temporary}"' EXIT if [[ ! -e ${pfx} ]]; then openssl req -x509 -newkey rsa:3072 -noenc \ -keyout "${temporary}/ca.key" -out "${temporary}/ca.pem" \ -sha256 -days 3650 -subj '/CN=AutoFirma ROOT' \ -addext 'basicConstraints=critical,CA:TRUE,pathlen:0' \ -addext 'keyUsage=critical,keyCertSign,cRLSign' \ 2> "${temporary}/openssl.log" || return openssl req -new -newkey rsa:2048 -noenc \ -keyout "${temporary}/server.key" -out "${temporary}/server.csr" \ -subj '/CN=127.0.0.1' 2> "${temporary}/openssl.log" || return cat > "${temporary}/server.ext" <<- _EOF_ || return basicConstraints=critical,CA:FALSE keyUsage=critical,digitalSignature,keyEncipherment extendedKeyUsage=serverAuth subjectAltName=DNS:localhost,IP:127.0.0.1,IP:::1 _EOF_ openssl x509 -req -in "${temporary}/server.csr" \ -CA "${temporary}/ca.pem" -CAkey "${temporary}/ca.key" -CAcreateserial \ -out "${temporary}/server.pem" -days 3650 -sha256 \ -extfile "${temporary}/server.ext" 2> "${temporary}/openssl.log" || return openssl pkcs12 -export -inkey "${temporary}/server.key" \ -in "${temporary}/server.pem" -certfile "${temporary}/ca.pem" \ -name socketautofirma -passout pass:654321 \ -out "${temporary}/autofirma.pfx" 2> "${temporary}/openssl.log" || return mv -- "${temporary}/autofirma.pfx" "${pfx}" || return fi chmod 0600 "${pfx}" || return # Recover the CA from an existing upstream/AUR identity without replacing it. openssl pkcs12 -in "${pfx}" -passin pass:654321 -cacerts -nokeys \ 2> "${temporary}/openssl.log" | openssl x509 -outform PEM > "${temporary}/exported.pem" || return if ! cmp -s "${temporary}/exported.pem" "${directory}/Autofirma_ROOT.cer"; then mv -- "${temporary}/exported.pem" "${directory}/Autofirma_ROOT.cer" || return fi ) _af_trim() { REPLY=${1#"${1%%[![:space:]]*}"} REPLY=${REPLY%"${REPLY##*[![:space:]]}"} } _af_profile_entry() { [[ ${active} == 1 && -n ${profile_path} ]] || return 0 if [[ ${relative} != 0 && ${profile_path} != /* ]]; then profiles["${root}/${profile_path}"]=1 else profiles["${profile_path}"]=1 fi } _af_read_profiles() { local root=$1 active=0 relative=1 profile_path='' line key value REPLY [[ -r ${root}/profiles.ini ]] || return 0 while IFS= read -r line || [[ -n ${line} ]]; do line=${line#$'\xef\xbb\xbf'} _af_trim "${line}" line=${REPLY} case ${line} in ''|'#'*|';'*) continue ;; '['*']') _af_profile_entry active=0 relative=1 profile_path='' [[ ${line} == '[Profile'*']' ]] && active=1 ;; *=*) _af_trim "${line%%=*}"; key=${REPLY,,} _af_trim "${line#*=}"; value=${REPLY} case ${key} in path) profile_path=${value} ;; isrelative) relative=${value} ;; esac ;; esac done < "${root}/profiles.ini" _af_profile_entry } _af_has_identity() { local database=$1 nickname=$2 fingerprint=$3 alias pem actual for alias in "${nickname}" 'AutoFirma ROOT'; do pem=$(certutil -L -d "${database}" -n "${alias}" -a 2>/dev/null) || continue actual=$(printf '%s\n' "${pem}" | openssl x509 -outform DER 2>/dev/null | sha256sum) || continue [[ ${actual%% *} == "${fingerprint}" ]] && return 0 done return 1 } autofirma_browser_setup() ( umask 077 local user_home=$1 config_home=$2 data_home=$3 local directory="${1}/.afirma/Autofirma" lock_fd root path database fingerprint nickname failed=0 local -A profiles=() mkdir -p -m 0700 -- "${directory}" || return exec {lock_fd}> "${directory}/.browser-setup.lock" || return flock -x "${lock_fd}" || return if ! _af_prepare_identity "${directory}"; then printf '%s\n' 'AutoFirma: cannot prepare the local TLS keystore; existing identities are preserved.' >&2 return 1 fi fingerprint=$(openssl x509 -in "${directory}/Autofirma_ROOT.cer" -outform DER | sha256sum) || return fingerprint=${fingerprint%% *} nickname="AutoFirma ROOT (Esparto ${fingerprint:0:16})" profiles["${user_home}/.pki/nssdb"]=1 profiles["${data_home}/pki/nssdb"]=1 for root in "${user_home}/.mozilla/firefox" "${config_home}/mozilla/firefox" \ "${user_home}/.config/mozilla/firefox" "${user_home}/snap/firefox/common/.mozilla/firefox"; do _af_read_profiles "${root}" done for path in "${!profiles[@]}"; do if [[ -f ${path}/cert9.db ]]; then database="sql:${path}" elif [[ -f ${path}/cert8.db ]]; then database="dbm:${path}" else continue fi _af_has_identity "${database}" "${nickname}" "${fingerprint}" && continue if ! certutil -A -d "${database}" -n "${nickname}" -t C,, -i "${directory}/Autofirma_ROOT.cer"; then printf 'AutoFirma: cannot update %s; close the browser and launch AutoFirma again.\n' "${path}" >&2 failed=1 fi done return "${failed}" ) if [[ ${BASH_SOURCE[0]} == "${0}" ]]; then if (( EUID == 0 )); then printf '%s\n' 'AutoFirma browser setup must run as your normal desktop user.' >&2 exit 1 fi autofirma_browser_setup "${HOME}" "${XDG_CONFIG_HOME:-${HOME}/.config}" \ "${XDG_DATA_HOME:-${HOME}/.local/share}" fi