Small script to import freemail S/MIME certificate into GPGSM

Nicholas Sushkin nsushkin at sushkins.net
Wed May 18 23:45:52 CEST 2005


After reading "Small HowTo on how to import freemail S/MIME certificates into 
GPGSM" by Matthias Welwarsky  mwelwarsky at web.de, I wrote the following BASH 
script.

Save it into a file called import-cert.sh, and execute it using the following 
syntax:

bash import-cert.sh myThawteCertExportedFromMozilla.p12

It works with gpgsm 1.9.16

----------------CUT HERE--------------------
set -e

mozCert="$1"
basename=$(basename "$mozCert" .p12)
dirname=$(dirname "$mozCert")
bundle="$dirname/${basename}.pem"
key="$dirname/${basename}.privatekey.p12"

echo "Converting p12 certificate to pem bundle"
openssl pkcs12 -in "$mozCert" -out "$bundle" -nodes 
echo "Extracting private key from the pem bundle"
openssl pkcs12 -in "$bundle" -export -nocerts -nodes -out "$key"
echo "Importing private key into gpgsm"
gpgsm --import "$key"
rm "$key"
certCount=0
inCert=0
cat "$bundle" | while read line; do
    if [ "$(echo "$line" | tr -d "-")" == "BEGIN CERTIFICATE" ]; then
        certCount=$((certCount + 1))
        cert="$dirname/${basename}.cert${certCount}.txt"
        inCert=1
        echo "Extracting certificate #$certCount from the bundle"
        : > "$cert"
    fi
    if [ $inCert == 1 ]; then
        echo "$line" >> "$cert"
    fi
    if [ "$(echo "$line" | tr -d "-")" == "END CERTIFICATE" ]; then
        inCert=0
        echo "Importing certificate #$certCount into gpgsm"
        gpgsm --import "$cert"
        rm "$cert"
    fi
done
rm "$bundle"
----------------CUT HERE--------------------

-- 
Nick



More information about the Gnupg-users mailing list