Bash – How to export the pem file to pfx with a password on the command line

bashconvertopensslpassword

I need to pass the password via the command line during the exporting in a bash script.

IBM has this on their website

openssl pkcs12 -export -in "$pem" -inkey "$key" -out "$pfx" -passout pass:pkcs12 "$pfxpass";

The above does not work for me.

The command below works but then you are prompted to enter and reenter a password.

openssl pkcs12 -export -in "$pem" -inkey "$key" -out "$pfx";

How can this be scripted?

Best Answer

You need to use the -passin in your command, due to the key you've used in the -inkey needs a password. Also, the exported pkcs12 file will need a password, so you need to use -passout as well. So, assuming you'll use the same password for the imported an exported keys, you should use this command.

openssl pkcs12 \
  -export \
  -in "$pem" -inkey "$key" -passin pass:"$pfxpass" \
  -passout pass:"$pfxpass" -out "$pfx" 

Hope it helps!