forked from redredgroovy/easy-ca
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-server-p12
More file actions
executable file
·67 lines (52 loc) · 1.71 KB
/
create-server-p12
File metadata and controls
executable file
·67 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash
# Pontus Ullgren
usage() {
echo "Usage: $0 -s SERVER_NAME [-a ALT_NAME]..."
echo "Creates a P12 for use with SERVER_NAME"
echo
echo "Options:"
echo " -s SERVER_NAME Server hostname (commonName) for the cert to include in the P12"
echo
exit 2
}
SERVER_NAME=
ALT_NAME=
while getopts s:a: FLAG; do
case $FLAG in
s) SERVER_NAME=${OPTARG}
;;
*) usage
;;
esac
done
if [ "${SERVER_NAME}" == "" ]; then
usage
fi
BIN_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
source ${BIN_DIR}/functions
source ${BIN_DIR}/defaults.conf
# Sanitize the commonName to make it suitable for use in filenames
SAFE_NAME=`echo ${SERVER_NAME} | sed 's/\*/star/g'`
SAFE_NAME=`echo ${SAFE_NAME} | sed 's/[^A-Za-z0-9-]/-/g'`
KEY=private/${SAFE_NAME}.server.key
LEAFCERT=certs/${SAFE_NAME}.server.crt
CHAINCERT=ca/ca.crt
TARGET_KEYSTORE=certs/${SAFE_NAME}.server.p12
if [ ! -f ${LEAFCERT} ] && [ ! -f ${KEY} ]; then
echo "Server certificate or key does not exists for '${SERVER_NAME}', exiting."
exit 1
fi
# ----
# Create PKCS#12 file
# ----
# From https://www.sslshopper.com/ssl-converter.html:
# The PKCS#12 or PFX format is a binary format for storing the server certificate,
# any intermediate certificates, and the private key in one encryptable file. PFX
# files usually have extensions such as .pfx and .p12. PFX files are typically used
# on Windows machines to import and export certificates and private keys.
TRANSITFILE=`mktemp`
if [[ $? != 0 ]]; then
echo "Creation of temporary transit file failed -- exiting" >&2; exit 1
fi
cat "$KEY" "$LEAFCERT" > "$TRANSITFILE"
openssl pkcs12 -export -in "$TRANSITFILE" -name "${SAFE_NAME}" > "$TARGET_KEYSTORE"