Convert PFX to PEM using expect

  2021-03-14


Here’s a script which uses /expect/ to convert a PFX to PEM files:

!– HTML generated using hilite.me –>

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/expect -f
set filename [lindex $argv 0];
set timeout -1
spawn openssl pkcs12 -in $filename -nocerts -out temp
expect "Enter Import Password:"
send – "\n"
expect "Enter PEM pass phrase:"
send – "D4ndyB0y$\n"
expect "Verifying - Enter PEM pass phrase:"
send – "D4ndyB0y$\n"
expect eof
spawn openssl rsa -in  temp -out $filename-privatekey.pem
expect "Enter pass phrase for temp:"
send – "D4ndyB0y$\n"
expect eof
spawn openssl pkcs12 -in $filename -clcerts -nokeys -out $filename-cert.pem
expect "Enter Import Password:"
send – "\n"
expect eof
spawn openssl pkcs12 -in $filename -cacerts -nokeys -chain -out $filename-intermediate.pem
expect "Enter Import Password:"
send – "\n"
expect eof