Skip to content

Instantly share code, notes, and snippets.

@AfroThundr3007730
Last active April 16, 2025 02:30
Show Gist options
  • Save AfroThundr3007730/ba99753dda66fc4abaf30fb5c0e5d012 to your computer and use it in GitHub Desktop.
Save AfroThundr3007730/ba99753dda66fc4abaf30fb5c0e5d012 to your computer and use it in GitHub Desktop.
Import DoD root certificates into linux CA store
#!/bin/bash
# Imports DoD root certificates into Linux CA store
# Version 0.4.1 updated 20241216 by AfroThundr
# SPDX-License-Identifier: GPL-3.0-or-later
# For issues or updated versions of this script, browse to the following URL:
# https://gist.github.com/AfroThundr3007730/ba99753dda66fc4abaf30fb5c0e5d012
# Dependencies: curl gawk openssl unzip wget
set -euo pipefail
shopt -s extdebug nullglob
add_dod_certs() {
local bundle cert certdir file form tmpdir url update
trap '[[ -d ${tmpdir:-} ]] && rm -fr $tmpdir' EXIT INT TERM
# Location of bundle from DISA site
url='https://public.cyber.mil/pki-pke/pkipke-document-library/'
bundle=$(curl -s $url | awk -F '"' 'tolower($2) ~ /dod.zip/ {print $2}')
# Set cert directory and update command based on OS
[[ -f /etc/os-release ]] && source /etc/os-release
if [[ ${ID:-} =~ (fedora|rhel|centos) ||
${ID_LIKE:-} =~ (fedora|rhel|centos) ]]; then
certdir=/etc/pki/ca-trust/source/anchors
update='update-ca-trust'
elif [[ ${ID:-} =~ (debian|ubuntu|mint) ||
${ID_LIKE:-} =~ (debian|ubuntu|mint) ]]; then
certdir=/usr/local/share/ca-certificates
update='update-ca-certificates'
else
certdir=${1:-} && update=${2:-}
fi
[[ ${certdir:-} && ${update:-} ]] || {
printf 'Unable to autodetect OS using /etc/os-release.\n'
printf 'Please provide CA certificate directory and update command.\n'
printf 'Example: %s /cert/store/location update-cmd\n' "${0##*/}"
exit 1
}
# Extract the bundle
wget -qP "${tmpdir:=$(mktemp -d)}" "$bundle"
unzip -qj "$tmpdir"/"${bundle##*/}" -d "$tmpdir"
# Check for existence of PEM or DER format p7b.
for file in "$tmpdir"/*_dod_{pem,der}.p7b; do
# Iterate over glob instead of testing directly (SC2144)
[[ -f ${file:-} ]] && form=${file%.*} && form=${form##*_} && break
done
[[ ${form:-} && ${file:-} ]] || { printf 'No bundles found!\n' && exit 1; }
# Convert the PKCS#7 bundle into individual PEM files
openssl pkcs7 -print_certs -inform "$form" -in "$file" |
awk -v d="$tmpdir" \
'BEGIN {c=0} /subject=/ {c++} {print > d "/cert." c ".pem"}'
# Rename the files based on the CA name
for cert in "$tmpdir"/cert.*.pem; do
mv "$cert" "$certdir"/"$(
openssl x509 -noout -subject -in "$cert" |
awk -F '(=|= )' '{print gensub(/ /, "_", 1, $NF)}'
)".crt
done
# Remove temp files and update certificate stores
rm -fr "$tmpdir" && $update
}
# Only execute if not being sourced
[[ ${BASH_SOURCE[0]} == "$0" ]] || return 0 && add_dod_certs "$@"
@Amndeep7
Copy link

Amndeep7 commented Jan 5, 2024

@danielcjacobs if you look in the if/else block, you'll see that those args are supposed to be set to the certdir and whatever shellscript/binary is being used to update all the cert bundles. the gist tries to provide good defaults, but you can also overwrite if you so desire.

@njohbillie
Copy link

Will this run on mac?

@AfroThundr3007730
Copy link
Author

AfroThundr3007730 commented Mar 2, 2024

@senterfd-jrg Thanks for catching that. I've updated the script to incorporate it (and fix various shellcheck warnings).

@njohbillie I haven't researched how a mac imports trusted certificates. If it works similarly to Linux certificate management, then you could adapt the script to make it work, if not then you may need to look at another tool to do it.

@flickerfly
Copy link

For the bots that drive search, this works on WSL - Windows Subsystem for Linux Ubuntu nicely. Thank you @AfroThundr3007730 for saving me a bunch of time.

@nathanhoeller
Copy link

Looks like the naming convention changed again on the .p7b files. I was able to get it to work by modifying the following lines. This is written in a way that no longer depends on "dod" in the filenames.

From:

# Check for existence of PEM or DER format p7b.
    for file in "$tmpdir"/*_dod_{pem,der}.p7b; do
        # Iterate over glob instead of testing directly (SC2144)
        [[ -f ${file:-} ]] && form=${file%.*} && form=${form##*_} && break
    done
    [[ ${form:-} && ${file:-} ]] || { printf 'No bundles found!\n' && exit 1; }

To:

# Check for existence of PEM or DER format p7b.
    for file in "$tmpdir"/*.p7b; do
        [[ -f ${file:-} ]] || continue
        if [[ $file =~ (_pem|\.pem)\.p7b$ ]]; then
            form="pem"
        elif [[ $file =~ (_der|\.der)\.p7b$ ]]; then
            form="der"
        else
            continue
        fi
        break
    done
    [[ ${form:-} && ${file:-} ]] || { printf 'No valid bundles found!\n' && exit 1; }

@senterfd-jrg
Copy link

senterfd-jrg commented Mar 7, 2025

@nathanhoeller - Another good catch for the change to the naming convention. Upgrading to this version with your change also revealed that this new version does not replace all the space chars within the certificate's filename with "_" (underscore). I am not a fan of spaces within filenames, so I changed the 1 to "g" in the following gensub() command.

From:

                awk -F '(=|= )' '{print gensub(/ /, "_", 1, $NF)}'

To:

                awk -F '(=|= )' '{print gensub(/ /, "_", "g", $NF)}'

@senterfd-jrg
Copy link

senterfd-jrg commented Mar 10, 2025

Public.cyber.mil made another change and now the command to initialize bundle, near line 22, is having problems. The previous calls to initialize bundle would return a single value for the variable bundle. Now we are seeing two values. I have added the grep command to use only the line beginning with the phrase http in my file. See below.

From:

    bundle=$(curl -s $url | awk -F '"' 'tolower($2) ~ /dod.zip/ {print $2}')

To:

    bundle=$(curl -s $url | awk -F '"' 'tolower($2) ~ /dod.zip/ {print $2}' | grep "^http")

@nathanhoeller
Copy link

@senterfd-jrg - I see the spaces in the filenames in /etc/ssl/certs/. It looks like it automatically added the underscores after running the update command, but I can understand wanting to add this to keep the file names consistent. Thanks for the catch! I'll be adding this to my code.

Also, I verified the issue you mentioned with the bundle URL. That's another helpful addition. Thanks!

@allebone
Copy link

Is this currently functional, as-is above? I see lots of suggestions for fixes, but wasn't sure if this is still being maintained.

@senterfd-jrg
Copy link

@allebone - I use the code above with some of the modifications that the users have reported. @AfroThundr3007730 did incorporate a bunch of stuff as he mentioned. His script is a big help. The DoD site continually changes their structures which requires a modification to the script. I have come here to this chat and seen recommended updates within a day to hours of required changes based on DoD site changes.

@aaronlippold
Copy link

aaronlippold commented Apr 16, 2025 via email

@allebone
Copy link

@senterfd-jrg @AfroThundr3007730 I hope my comment wasn’t taken as a complaint or criticism. TBH, I was a little tired and on a small screen with a half-wit understanding of the GIST revisions tab. (Which I didn’t see until today)

I’m just getting started on a base install of 72 servers, and this problem plagues us! (Times a ton)

I’m not scripting savvy but if there is anything I can do to help…let me know. Or I can go back to lurking until someone wants a layman to test. :)

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