Guest User

Untitled

a guest
May 27th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Author: Michael Lefkovits
  4. # Date: 23.5.18
  5.  
  6. # This script extracts the client certificate and private key, as well as the entire certificate chain, from a .pfx file.
  7. # The script will prompt the user for the pfx password.
  8. # Disclaimers:
  9. # 1. Developed and tested on Ubuntu 16.04 server.
  10. # 2. Requires 'openssl' installed on the server.
  11. # 3. Didn't handle password argument for simplicity (also receiving the password as an inline argument isn't secure because its shown on the process)
  12.  
  13. # Arguments:
  14. # 1st arg - relative path to the .pfx file
  15.  
  16. # Output:
  17. # .key file - contains private key
  18. # .crt file - contains client certificate
  19. # intermediate.crt - contains intermediate and root certificates
  20. # .chained.crt - contains the entire certificate chain. This is the final crt file used along with .key on the web server.
  21.  
  22. # Variable Assignment
  23. PFXFILE=$1
  24. CERTNAME=$( basename $1 .pfx )
  25.  
  26. # Extract private key from pfx
  27. openssl pkcs12 -in $PFXFILE -nocerts -nodes | \
  28. sed -ne '/-BEGIN PRIVATE KEY-/,/-END PRIVATE KEY-/p' > $CERTNAME.key
  29.  
  30. # Extract client certificate from pfx
  31. openssl pkcs12 -in $PFXFILE -clcerts -nokeys | \
  32. sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > $CERTNAME.crt
  33.  
  34. # Check validity of crt and key
  35. echo '=== Print md5 checksum of crt and key to verify validity. Must match ! ==='
  36. echo $( openssl x509 -noout -modulus -in $CERTNAME.crt | openssl md5 )
  37. echo $( openssl rsa -noout -modulus -in $CERTNAME.key | openssl md5 )
  38. echo '=========================================================================='
  39.  
  40. # Extract root and intermediate certificates from pfx (certificate chain)
  41. openssl pkcs12 -in $PFXFILE -cacerts -nokeys -chain | \
  42. sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > intermediate.crt
  43.  
  44. # Bundle client and intermediate.crt to final crt file
  45. cat $CERTNAME.crt intermediate.crt > $CERTNAME.chained.crt
Add Comment
Please, Sign In to add comment