Files
pyscrlink/gencert.sh
Shin'ichiro Kawasaki 3a784edbd5 gencert.sh: Automate certificate addition to FireFox and Chrome
As of today, bluepy-scratch-link users need to do special action to allow
local server certificates. This is trouble some and James Le Cuirot
suggested to automate the action with certutil tools

To avoid the user action, check if NSS DB of FireFox or Chrome exists. If
NSS DBs exist, add the certificate to those DBs.

Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
2020-04-19 21:46:57 +09:00

72 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
CERT_FILE=scratch-device-manager.cer
KEY_FILE=scratch-device-manager.key
# Generate certificate and key files
openssl req -x509 -out "${CERT_FILE}" -keyout "${KEY_FILE}" -newkey rsa:2048 \
-nodes -sha256 -days 3650 -extensions EXT -config /dev/stdin << HERE
[dn]
CN = device-manager.scratch.mit.edu
[req]
prompt = no
distinguished_name = dn
[EXT]
subjectAltName = DNS:device-manager.scratch.mit.edu
HERE
if ((!$?)); then
echo "Generated certificate: ${CERT_FILE}"
echo "Generated key: ${KEY_FILE}"
else
echo "Failed to generate certificate and key files."
exit 1
fi
if ! command -v certutil > /dev/null; then
echo "Certutil command not found. Do not add certificate."
exit 2
fi
add_cert() {
local dir="${1}"
local prefix=sql
if [[ -e ${dir}/key3.db ]]; then
prefix=dbm
fi
certutil -A -d "${prefix}:${1}" -n "device-manager.scratch.mit.edu" \
-t "C,," -i "${CERT_FILE}"
}
# Add certificate to FireFox
declare nssdb
for f in "${HOME}"/.mozilla/firefox/*/key*.db; do
if [[ ! -f ${f} ]]; then
continue
fi
nssdb=${f%/*}
if add_cert "${nssdb}"; then
echo "Added certificate to FireFox NSS DB: ${nssdb}"
else
echo "Failed to add certificate to FireFox NSS DB: ${nssdb}"
exit 3
fi
done
if [[ -z ${nssdb} ]]; then
echo "FireFox NSS DB not found. Do not add certificate."
fi
# Add certificate to Chrome
nssdb="${HOME}/.pki/nssdb"
if [[ -d ${nssdb} ]]; then
if add_cert "${nssdb}"; then
echo "Added certificate to Chrome"
else
echo "Failed to add certificate to Chrome"
exit 4
fi
else
echo "Chrome NSS DB not found. Do not add certificate."
fi