BASH 9
Change-pfx-password.sh By allan on 25th August 2026 07:49:05 AM
  1. #!/usr/bin/env bash
  2. #
  3. #
  4. #   PURPOSE/NOTES
  5. #     Write a new PFX file with a new password.
  6. #
  7. #   REVISIONS:
  8. #   Ver        Date        Author           Description
  9. #
  10. #   1.0        2026.08.25  CEGAL.DK/amg     created
  11. #   1.1        2026.08.25  CEGAL.DK/amg     added optional current password argument
  12. #
  13.  
  14. ##set -x
  15. set -euo pipefail
  16.  
  17. VERSION='1.1'
  18.  
  19. usage() {
  20.   printf 'Script %s has version %s\n' "${0##*/}" "$VERSION" >&2
  21.   printf 'Usage: %s -i input.pfx [-p current-password] [-o output.pfx]\n' "${0##*/}" >&2
  22.   printf '\nSet STANDARD_PASSPHRASE in the environment for the replacement password.\n' >&2
  23.   printf 'If -p is omitted, the current password is read from masked stdin.\n' >&2
  24.   exit 2
  25. }
  26.  
  27. input=''
  28. output=''
  29. current_password=''
  30. while getopts ':i:o:p:h' option; do
  31.   case "$option" in
  32.     i) input=$OPTARG ;;
  33.     o) output=$OPTARG ;;
  34.     p) current_password=$OPTARG ;;
  35.     h) usage ;;
  36.     *) usage ;;
  37.   esac
  38. done
  39.  
  40. [[ -n "$input" ]] || usage
  41. [[ -f "$input" ]] || { printf 'Input file not found: %s\n' "$input" >&2; exit 1; }
  42. [[ -n "$output" ]] || output="${input%.pfx}_STD.pfx"
  43. [[ -n "${STANDARD_PASSPHRASE:-}" ]] || {
  44.   printf 'STANDARD_PASSPHRASE is not set.\n' >&2
  45.   printf 'I.e.: export STANDARD_PASSPHRASE='\''your_password'\''\n' >&2
  46.   exit 1
  47. }
  48.  
  49. password_file=$(mktemp)
  50. cleanup() {
  51.   rm -f "$password_file"
  52. }
  53. trap cleanup EXIT
  54. chmod 600 "$password_file"
  55.  
  56. if [[ -z "$current_password" ]]; then
  57.   printf 'Enter the current PFX password: ' >&2
  58.   IFS= read -r -s current_password
  59.   printf '\n' >&2
  60. fi
  61. printf '%s' "$current_password" > "$password_file"
  62.  
  63. openssl pkcs12 \
  64.   -in "$input" \
  65.   -passin "file:$password_file" \
  66.   -nodes \
  67.   | openssl pkcs12 \
  68.       -export \
  69.       -out "$output" \
  70.       -passout "env:STANDARD_PASSPHRASE"
  71.  
  72. printf 'Created %s\n' "$output"

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.