|
#!/usr/bin/env bash |
|
|
|
set -e |
|
|
|
# Setup |
|
DEVICE_ID="$1" |
|
CONFIG_FILE="openssl_root_ca.cnf" |
|
|
|
# Existing Intermediate CA Certificate & Key |
|
# CA_INTERMEDIATE_CERT="ROOTS/Recycleye_IOT_Intermediate_CA.crt.pem" |
|
CA_INTERMEDIATE_CERT_FULL="ROOTS/Recycleye_IOT_Intermediate_CA_full_chain.crt.pem" |
|
CA_INTERMEDIATE_KEY="ROOTS/Recycleye_IOT_Intermediate_CA.key.pem" |
|
CA_ROOT_CERT="ROOTS/Recycleye_IOT_CA.pem" |
|
|
|
OUTDIR="certificates/${DEVICE_ID}-certificates" |
|
CONFIG_FILE_TEMP="${OUTDIR}/${CONFIG_FILE}.tmp" |
|
mkdir -p "$OUTDIR" |
|
|
|
# Add the device id as commonName to the config file |
|
# Should be able to use CN = $ENV::DEVICE_ID in the conf file but it doesn't work |
|
sed "s/DEVICE_ID/${DEVICE_ID}/g" ${CONFIG_FILE} >"${CONFIG_FILE_TEMP}" |
|
|
|
# 1. Generate the device private key |
|
echo "--> Generate device private key" |
|
openssl genrsa -out "${OUTDIR}/device.key.pem" 4096 &>/dev/null |
|
chmod 444 "${OUTDIR}/device.key.pem" |
|
echo " ... ${OUTDIR}/device.key.pem" |
|
|
|
# 2. Generate a certificate signing request for the device identity certificate |
|
echo "--> Generate certificate signing request for device identity certificate" |
|
openssl req -new \ |
|
-config "${CONFIG_FILE_TEMP}" \ |
|
-key "${OUTDIR}/device.key.pem" \ |
|
-out "${OUTDIR}/device.csr" &>/dev/null |
|
echo " ... ${OUTDIR}/device.csr" |
|
|
|
# view the CSR with |
|
# openssl req -noout -text -in "${OUTDIR}/device.csr" |
|
|
|
# 3. Create the device certificate using the CSR |
|
echo "--> Generate the device identity certificate using our intermediate ca cert" |
|
openssl x509 -req \ |
|
-days 3650 \ |
|
-in "${OUTDIR}/device.csr" \ |
|
-CA "${CA_INTERMEDIATE_CERT_FULL}" \ |
|
-CAkey "${CA_INTERMEDIATE_KEY}" \ |
|
-CAcreateserial \ |
|
-out "${OUTDIR}/device.cert.pem" \ |
|
-extfile "${CONFIG_FILE_TEMP}" \ |
|
-extensions user_crt &>/dev/null |
|
echo " ... ${OUTDIR}/device.cert.pem" |
|
|
|
# Look at a certificate Subject (in sensible format) with |
|
# openssl x509 -in "${OUTDIR}/device.cert.pem" -noout -subject -nameopt oneline,-esc_msb |
|
|
|
# 4. Create full chain device certificate |
|
echo "--> Generate a full chain device certificate (with ca intermediate full chain cert)" |
|
cat "${OUTDIR}/device.cert.pem" "${CA_INTERMEDIATE_CERT_FULL}" >"${OUTDIR}/device-full-chain.cert.pem" |
|
echo " ... ${OUTDIR}/device-full-chain.cert.pem" |
|
|
|
# 5. Create device CA certificate CSR |
|
echo "--> Generate certificate signing request for device ca certificate" |
|
openssl req -new \ |
|
-config "${CONFIG_FILE_TEMP}" \ |
|
-key "${OUTDIR}/device.key.pem" \ |
|
-extensions v3_intermediate_ca \ |
|
-out "${OUTDIR}/device-ca.csr" &>/dev/null |
|
echo " ... ${OUTDIR}/device-ca.csr" |
|
|
|
# 6. Create device CA certifiate |
|
echo "--> Generate the device ca certificate using our intermediate ca cert" |
|
openssl x509 -req \ |
|
-days 3650 \ |
|
-in "${OUTDIR}/device-ca.csr" \ |
|
-CA "${CA_INTERMEDIATE_CERT_FULL}" \ |
|
-CAkey "${CA_INTERMEDIATE_KEY}" \ |
|
-CAcreateserial \ |
|
-out "${OUTDIR}/device-ca.cert.pem" \ |
|
-extfile "${CONFIG_FILE_TEMP}" \ |
|
-extensions v3_intermediate_ca &>/dev/null |
|
echo " ... ${OUTDIR}/device-ca.cert.pem" |
|
|
|
# 7. Create full chain device CA certificate |
|
echo "--> Generate a full chain device ca certificate (with ca intermediate full chain cert)" |
|
cat "${OUTDIR}/device-ca.cert.pem" "${CA_INTERMEDIATE_CERT_FULL}" >"${OUTDIR}/device-ca-full-chain.cert.pem" |
|
echo " ... ${OUTDIR}/device-ca-full-chain.cert.pem" |
|
|
|
# 8. Copy & rename certs for device |
|
echo "--> Copy & rename certificates to copy to device" |
|
mkdir -p "$OUTDIR/to-device" |
|
cp "${OUTDIR}/device-ca-full-chain.cert.pem" "$OUTDIR/to-device/device-ca.crt" |
|
cp "${OUTDIR}/device-full-chain.cert.pem" "${OUTDIR}/to-device/device.crt" |
|
cp "${OUTDIR}/device.key.pem" "${OUTDIR}/to-device/device.key" |
|
cp "${CA_ROOT_CERT}" "${OUTDIR}/to-device/ca.crt" |
|
echo " ... ${OUTDIR}/to-device" |
|
|
|
rm -f "${CONFIG_FILE_TEMP}" |