问题
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