From bbc31b8303d34053562c9cb6e2c1132f124bfdb1 Mon Sep 17 00:00:00 2001 From: Alexander Scheel Date: Wed, 16 Jan 2019 17:16:09 -0500 Subject: [PATCH 1/2] Add validate-then-import certificate utility The NSS utility certutil requires certificates to be imported (`certutil -A`) prior to validating their signatures and usage (`certutil -V -e`). PKICertImport avoids this pitfall by handling both import and validation in the same step, so if the validation fails, the certificate is removed. This ensures it is not accidentally used before it is verified. Signed-off-by: Alexander Scheel (cherry picked from commit a187cccc269968e310d97eafc99771d2cd5b366e) --- base/util/PKICertImport.bash | 328 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 328 insertions(+) create mode 100755 base/util/PKICertImport.bash diff --git a/base/util/PKICertImport.bash b/base/util/PKICertImport.bash new file mode 100755 index 0000000..d0b54c7 --- /dev/null +++ b/base/util/PKICertImport.bash @@ -0,0 +1,328 @@ +#!/bin/bash + +# Copyright (C) 2018 Red Hat + +# PKICertImport performs a validate-then-import strategy for importing +# certificates into a NSS DB or HSM Token by wrapping both calls to +# certutil (`certutil -A` and `certutil -V`) such that the certificate +# will be removed if import fails (`certutil -D`). This helps to ensure +# that certificates are not used prior to validation. +function PKICertImport() { + ## [ overview ] ## + + # This script has four major sections: + # + # 1. Globals -- the definitions of all script-global variables + # 2. Helper functions -- functions which don't perform key operations + # 3. Core commands -- functions which interact with the NSS DB via + # certutil + # 4. Program flow -- main flow of the program + + + ## [ globals ] ## + + # Path to certificate; parsed from arguments. + local CERT_PATH="" + + # User-given nickname for the certificate. + local CERT_NICKNAME="" + + # Whether or not the certificate is in ASCII/PEM format. + local CERT_ASCII="false" + + # What trust flags to use when importing the certificate. + local CERT_TRUST="" + + # What usage flags to validate the certificate against. + local CERT_USAGE="" + + # Location of the original NSS DB. + local NSSDB="" + + # Type of the NSSDB. + local NSSDB_TYPE="" + + # Location to the NSS DB Password file, if present. + local NSSDB_PASSWORD="" + + # Name of the HSM token, if used. + local HSM_TOKEN="" + + + ## [ helper functions ] ## + + # __e prints error messages, prefixing them with "e: " and writing the + # output to stderr instead of stdout. + function __e() { + echo "e:" "$@" 1>&2 + } + + # __v prints debug messages in verbose mode; these also go to stderr. + # Messages are only present if the environment variable VERBOSE is set. + function __v() { + if [ "x$VERBOSE" != "x" ]; then + echo "v:" "$@" 1>&2 + fi + } + + ## [ core commands ] ## + + # Parse the command line arguments and set the appropriate global + # variables. Return status of non-zero indicates a fatal error; help + # should be displayed. Return status of zero indicates no error and help + # should not be displayed. + function _parse_args() { + # Use a read-and-shift approach to parse both "