简体   繁体   English

如何使用Powershell通过https将自签名证书发送给客户端

[英]How to send a self signed certificate to clients over https with powershell

I am creating a REST api in PowerShell work over https protocol for testing purpose. 我正在PowerShell中通过https协议创建REST api,以进行测试。 My aim is to create a pseudo https API and use that to test a functionality of our application (the app needs to call my test https api). 我的目的是创建一个伪https API并使用它来测试我们应用程序的功能(该应用程序需要调用我的测试https api)。 I am able to create a self signed certificate using the following code. 我可以使用以下代码创建自签名证书。

$hostIP = Get-NetIPAddress | where{ ($_.InterfaceAlias -in @('Mgmt', 'Ethernet', 'management')) -and ($_.AddressFamily -eq 'IPv4')}
$win_path= 'c:\my_temp\'

$Cert = New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -   dnsname $hostIP.IPAddress -NotAfter (Get-Date).AddYears(2)
$Certname = $hostIP.IPAddress.Replace('.','_')
$pw = ConvertTo-SecureString -String "Pazzword" -Force -AsPlainText
$thumbprint = $Cert.Thumbprint
Export-PfxCertificate -cert cert:\localMachine\my\$thumbprint -FilePath 
$win_path\$Certname.pfx  -Force -Password $pw

The REST api I am writing looks something like this 我正在编写的REST API看起来像这样

$listener = New-Object System.Net.HttpListener
$httpUrl = "http://" + $givenArgs.HostName + ":" + $givenArgs.Port + "/"
Write-Output $httpUrl
$listener.Prefixes.Add($httpUrl) 
$httpsUrl = "https://" + $givenArgs.HostName + ":" + 443 + "/"
Write-Output $httpsUrl
$listener.Prefixes.Add($httpsUrl)
$listener.Start()

The question I have is, if I install the certificate that I have created in my machine manually, I am able to send queries to my REST api successfully but only from my machine. 我的问题是,如果我手动安装在计算机中创建的证书,则能够成功地将查询发送到我的REST api,但只能从我的计算机发送。

I would like to send the certificate (send a copy of the certificate for clients on intial request without using any CA) to clients so that when a client queries the server for the first time, it gets a copy of the certificate and can save it for further communication. 我想将证书(在不使用任何CA的情况下向初始请求发送证书的副本)发送给客户端,以便当客户端第一次查询服务器时,它将获得证书的副本并可以保存以便进一步沟通。

I have tried looking this up online but I could only find solutions leading to 1.Ignoring the certificate or 2. Importing the certificate manually which donot serve my need. 我尝试过在线查找,但只能找到导致1.忽略证书或2.手动导入证书的解决方案,这不符合我的需要。

Appreciate your time for looking into this and your help. 感谢您抽出宝贵的时间来研究此内容和您的帮助。 Thanks in advance. 提前致谢。

You don't have to explicitly send the certificate. 您不必显式发送证书。 Every time the client visits the https site, the public key certificate is always returned with the request. 客户端每次访问https站点时,公钥证书始终随请求一起返回。 The issue you have is that after that, the client then has to decide what to do with the certificate. 您遇到的问题是,此后,客户端必须决定如何处理证书。

The client will first look at the certificate, and look to see if it is trusted. 客户端将首先查看证书,然后查看它是否受信任。 If it is a trusted certificate (ie you purchase a Verisign Certificate), then the connection is accepted. 如果它是受信任的证书(即您购买了Verisign证书),则接受连接。 For self signed certificates, since they are not trusted, there are only 2 options that the client has: 对于自签名证书,由于它们不受信任,因此客户端只有两个选项:

  1. Ignore the certificate origin and blindly connect 忽略证书来源并盲目连接
  2. Accept that the origin can be trusted and Import the certificate 接受可以信任的来源并导入证书

You can't get around this fundamental fact. 您无法绕开这个基本事实。 Self signed certificates are like delivering a bomb shaped object to a client with a sticker on it saying "Not a Bomb - Trust me, I'm @user3543477", they can either ignore the sticker, or accept that I can Trust @user3543477. 自签名证书就像将带有炸弹形状的对象交付给客户端,上面贴有“不属于炸弹-相信我,我是@ user3543477”这样的标签,它们可以忽略该标签,也可以接受我可以信任@ user3543477。 You can't shove the package through the front door without their explicit consent. 未经他们的明确同意,您不能将包裹推过前门。 If the sticker said "Not a Bomb - Trust me, I've been verified by Bomb Experts", since the client trusts Bomb Experts, the client will accept the package without question. 如果标贴上写着“不是炸弹-相信我,我已经被炸弹专家验证”,因为客户信任炸弹专家,那么客户将毫无疑问地接受该包裹。

Self signed certificates are only used for Development purposes. 自签名证书用于开发目的。 I continually say to people to don't even try to make them work because you can't. 我不断地对人们说,不要尝试让他们工作,因为你做不到。

The right way to do certificates is to get a proper trusted certificate from the likes of Verisign or, what I now recommend, which is especially tailored for API's, is to get a free certificate from Let's Encrypt . 进行证书的正确方法是从Verisign之类获得适当的受信任证书,或者,我现在特别推荐为API量身定制的,是从Let's Encrypt获得免费证书。 That way you get a proper trusted certificate, and you don't deal with self signed certificates. 这样,您将获得适当的受信任证书,并且无需处理自签名证书。

When a certificate is used, a certification path has to be established. 使用证书时,必须建立证书路径。 root CA certificates are self signed. 根CA证书自签名的。 Point is, they are trusted beforehand. 重点是,他们事先被信任。

On your local machine where your self signed certificate has been generated the certificate is trusted. 在已生成自签名证书的本地计算机上,该证书是受信任的。 Other machines won't have that certificate: to make them trust your certificate 其他机器将没有该证书:使它们信任您的证书

  • the certificate has to be added (your option number 2.) usually to the Trusted Root Certificate store where the self signed certificates of public CA have been added by your operating system logics OR 通常必须将证书添加到您的“受信任的根证书”存储区(您的选项号2)。在该目录中,您的操作系统逻辑已添加了公共CA的自签名证书,或者
  • the certification path has to be trusted even in presence of a untrusted root (your option 1.) 即使存在不受信任的根,也必须信任证书路径(您的选项1。)

So basically your options are the ones you summarized. 因此,基本上,您的选择就是您所总结的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM