It worked fine Linux(curl 7.15.5 (x86_64-redhat-linux-gnu))
But the same command with the same certificate failed in Windows(curl 7.23.1 (x86_64-pc-win32)).
This error could be due to invalid certificate password or invalid certificate format, It seems the curl in windows is picker on certificate format than the curl in Linux.$curl -k -s -o /dev/null --connect-timeout 5 -w "\ntime_total=%{time_total}\nhttp_code=%{http_code}\n" --cert cert2.pem: Pass123 --key cert2.pem -d @data.xml https://server1/
curl: (58) unable to set private key file: 'cert2.pem' type PEM
In general, you can combine both certificate and key into one file by copy&paste the text in a text editor. But it seems the curl for Windows only accept the “formal format”, which has additional lines of “Bag Attributes” ( or maybe just different behaviour in different versions)
General certificate
#cat cert1.pem
-----BEGIN CERTIFICATE-----
………………..-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTEDDEK-Info: DES-EDE3-CBC,D49E12E8D074DA59…………….-----END RSA PRIVATE KEY-----
”Formal” certificate with “Bag Attributes”
Bag AttributeslocalKeyID: 92 47 FA AC C8 D4 94 D8 0C B0 D0 1D 9F D7 43 0B B5 D8 23 ADsubject=/C=AU/ST=NSW/L=Sydney/O=ORG1/CN=client1issuer=/C=AU/ST=NSW/O=ORG1/CN=CA-----BEGIN CERTIFICATE-----
………………….-----END CERTIFICATE-----
Bag AttributeslocalKeyID: 92 47 FA AC C8 D4 94 D8 0C B0 D0 1D 9F D7 43 0B B5 D8 23 ADKey Attributes: <No Attributes>-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTEDDEK-Info: DES-EDE3-CBC,6F6BA23BB03B5049……………..-----END RSA PRIVATE KEY-----
How to add the “Bag Attributes”?
#I found the “bag Attributes” can be added by converting PEM to pkcs12 format then convert back to PEM.
#Convert from PEM to PKCS12
$openssl pkcs12 -export -in cert1.pem -out cert1.pfx
#Convert back from PKCS12 to PEM
$ openssl pkcs12 -in cert1.pfx -out cert2.pem -clcerts
Enter Import Password:MAC verified OKEnter PEM pass phrase:Verifying - Enter PEM pass phrase:#It worked with the new certificate
C:\temp\curl\curl -k -s -o /dev/null --connect-timeout 5 -w "\ntime_total=%{time_total}\nhttp_code=%{http_code}\n" --cert cert2.pem:Pass123 --key cert2.pem -d @data.xml https://server1/
time_total=2.812http_code=200