使用具有多个证书的 Invoke-WebRequest 在 PowerShell 中重写 CURL

2024-04-13

我使用以下curl命令将一些数据从IoT Thing发布到AWS的IoT Core服务。

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"

该命令运行完美,但我想利用Invoke-WebRequest命令行开关。不幸的是,我无法弄清楚如何重写curl命令,主要是因为两个证书和密钥文件。

到目前为止我所拥有的是:

# 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"

上面命令的输出是:

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

如何像在 CURL 命令中一样使用密钥和根 CA 证书?


我通过使用 OpenSSL.exe 创建一个包含私钥和客户端证书的 PFX 证书来完成此工作(遵循 OP 评论中 Tomalak 的提示)。

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

然后,我可以根据需要使用 Invoke-WebRequest 与 AWS 服务进行通信:

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用具有多个证书的 Invoke-WebRequest 在 PowerShell 中重写 CURL 的相关文章

随机推荐