Rewrite CURL in PowerShell using Invoke-WebRequest with multiple certificates

和自甴很熟 提交于 2020-12-12 10:22:07

问题


I use the following curl command to publish some data from an IoT Thing to AWS's IoT Core service.

curl.exe --tlsv1.2 --cacert root-CA.pem --cert certificate.pem --key private.pem -X POST -d "$($Body)" "https://ats.iot.eu-west-1.amazonaws.com:8443/topics/example/1"

The command works perfectly, but I would like to leverage the features of the Invoke-WebRequest commandlet. Unfortunately I cannot figure out how to rewrite the curl command, primarily because of the two certificates and key file.

What I have so far is:

# Set TLS to use version 1.2 (required by AWS)
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

# Make the request
Invoke-WebRequest -Method POST -UseBasicParsing -Uri "https://ats.iot.eu-west-1.amazonaws.com:8443/topics/example/1" -Certificate (Get-PfxCertificate .\certificate.pem) -Body "Some example data!" -ContentType "application/json"

The output of the command above is:

Invoke-WebRequest : The request was aborted: Could not create SSL/TLS secure channel.

How can I use the Key and the Root CA cert as I am in the CURL command?


回答1:


I got this working by using OpenSSL.exe to create a PFX certificate containing the Private Key and Client Certifiate (following a tip from Tomalak in OP comments).

openssl.exe pkcs12 -export -in ..\certificate.pfx -inkey ..\private.pem.key -out ..\CertificateWithKey.pfx

I was then able to use Invoke-WebRequest as required to communicate with AWS Services:

Invoke-WebRequest -Method POST -UseBasicParsing -Uri $URI -Certificate (Get-PfxCertificate CertificateWithKey.pfx) -Body "{'message':'Hey hey!'}" -ContentType application/json



来源:https://stackoverflow.com/questions/57679101/rewrite-curl-in-powershell-using-invoke-webrequest-with-multiple-certificates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!