Skip to content

Instantly share code, notes, and snippets.

@jijiechen
Last active July 16, 2020 14:54
Show Gist options
  • Save jijiechen/907f0600a25744e4dda518981ec388fb to your computer and use it in GitHub Desktop.
Save jijiechen/907f0600a25744e4dda518981ec388fb to your computer and use it in GitHub Desktop.
Trust ca cert on Linux based on hostname and port
#!/bin/sh
FULL_HOST=$1
if [ -z "$FULL_HOST" ]; then
FULL_HOST=localhost:443
fi
HOST=$(echo $FULL_HOST | cut -d ':' -f 1)
PORT=$(echo $FULL_HOST | cut -d ':' -f 2)
if [ "$HOST" = "$PORT" ]; then
PORT=443
fi
mkdir /tmp/trusting-ca && openssl s_client -showcerts \
-connect $HOST:$PORT -servername $HOST < /dev/null \
| awk '/BEGIN/,/END/{ if(/BEGIN/){i++}; out="/tmp/trusting-ca/"i".crt"; print >out}' \
&& mv /tmp/trusting-ca/$(ls /tmp/trusting-ca/ -1 | tail -1) /usr/local/share/ca-certificates/$HOST-ca.crt \
&& rm -rf /tmp/trusting-ca
update-ca-certificates
@jijiechen
Copy link
Author

Usage:

trust-ca-cert.sh <hostname>[:<port>]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment