]> snippets.scripts.mit.edu Git - Scripts/git/.git/blame - certs/pkcs2pem
Add a script for importing PKCS12 files from Firefox to PEM
[Scripts/git/.git] / certs / pkcs2pem
CommitLineData
6e450839
NE
1#!/bin/sh
2
3set -e
4
5usage() {
6 cat <<EOF
7Usage: $0 <pkcs12 file> <output directory>
8
9Transforms a .p12 file, for instance as exported by Firefox's
10cerfiticate "backup" feature, into a pair of a PEM certificate file
11and private key.
12
13To export your certificate from Firefox, go to Edit|Preferences,
14Advanced|Security|View Certificates, and ``Backup'' your certificate
15to a file. Firefox will save it as a PKCS12 certificate. You must
16enter a passphrase, which this script will prompt you for.
17
18EOF
19 exit 1
20}
21
22[ "$#" -eq 2 ] || usage
23
24pkcs="$1"
25outdir="$2"
26
27echo -n "Password for $pkcs: "
28stty -echo
29read pass
30stty echo
31echo
32
33echo "$pass" | openssl pkcs12 -in "$pkcs" -nodes -out "$outdir"/cert.pem -passin stdin
34echo "$pass" | openssl pkcs12 -in "$pkcs" -nodes -nocerts -out "$outdir"/privkey.pem -passin stdin
35
36cat >&2 <<EOF
37Certificate written to $outdir/cert.pem
38Private key written to $outdir/privkey.pem
39
40Keep these files safe!
41
42You can pass these to wget's --certificate and --private-key options,
43or to curl's --cert/--key options.
44
45To use them with perl's LWP, set the following environment variables:
46
47EOF
48
49outdir="$(readlink -f "$outdir")"
50
51# No, this doesn't handle quoting properly.
52echo HTTPS_CERT_FILE="$outdir/cert.pem"
53echo HTTPS_KEY_FILE="$outdir/privkey.pem"
54