为IIS创建有效的测试SSL证书

Mov*_*GP0 8 iis powershell ssl wcf certificate

我想在IIS的开发环境中测试SSL连接.为此,我需要创建一个安装在机器商店中的自签名根证书,以及另一个使用根证书签名以在IIS中安装的证书.

这样做makecert现在已被弃用,所以我想知道如何使用Powershell和New-SelfSignedCertificate命令来完成它.

如果您获得正确的密钥使用设置,则可获得奖励积分:-)

注意:直接在IIS中使用自签名证书不起作用,因为浏览器和WCF认为它们无效.


供参考,以下是使用makecert的方法:

# create the self signed root certificate 
makecert -n "CN=root.lan" -r -sv root.pvk root.cer

# create the certificate for IIS that gets signed with the root certificate 
makecert -sk "Local Certificate" -iv root.pvk -n "CN=localhost" -ic root.cer -sr localmachine -ss my -sky exchange -pe

# convert to other formats 
cert2spc localhost.cer localhost.spc
pvk2pfx -pvk localhost.pvk -spc localhost.spc -pfx localhost.pfx
Run Code Online (Sandbox Code Playgroud)

Ole*_*leg 8

此处New-SelfSignedCertificate描述包含在Windows 10上的新版本.人们可以使用New-SelfSignedCertificate -?get-help New-SelfSignedCertificate -examples获得一些额外的信息.

文档和示例似乎仍然不够清晰,无法创建两个证书:

  • 一个自签名证书,将用作示例中的CA证书
  • 第二个SSL证书,使用第一个证书签名.

实现可能如下(我在下面写了多行选项,只是为了使文本更具可读性):

New-SelfSignedCertificate -HashAlgorithm sha384 -KeyAlgorithm RSA -KeyLength 4096
    -Subject "CN=My Test (PowerShell) Root Authority,O=OK soft GmbH,C=DE"
    -KeyUsage DigitalSignature,CertSign -NotAfter (get-date).AddYears(10)
    -CertStoreLocation "Cert:\CurrentUser\My" -Type Custom 
Run Code Online (Sandbox Code Playgroud)

输出看起来像

    Directory: Microsoft.PowerShell.Security\Certificate::CurrentUser\My


Thumbprint                                Subject
----------                                -------
B7DE93CB88E99B01D166A986F7BF2D82A0E541FF  CN=My Test (PowerShell) Root Authority, O=OK soft GmbH, C=DE
Run Code Online (Sandbox Code Playgroud)

该值B7DE93CB88E99B01D166A986F7BF2D82A0E541FF对于使用证书进行签名很重要.如果您忘记了该值,您可以通过CN名称找到它

dir cert:\CurrentUser\My | where Subject -Like "CN=My Test (PowerShell)*"
Run Code Online (Sandbox Code Playgroud)

或者certutil.exe -user -store My用于在我当前用户的商店上显示证书.

要创建SSL证书并根据以前创建的证书对其进行签名,可以执行以下操作

New-SelfSignedCertificate -Type Custom -Subject "CN=ok01.no-ip.org"
    -HashAlgorithm sha256 -KeyAlgorithm RSA -KeyLength 2048
    -KeyUsage KeyEncipherment,DigitalSignature
    -CertStoreLocation "cert:\LocalMachine\My"
    -Signer cert:\CurrentUser\My\B7DE93CB88E99B01D166A986F7BF2D82A0E541FF
    -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.1,1.3.6.1.5.5.7.3.2","2.5.29.17={text}DNS=ok01.no-ip.org&DNS=ok01.fritz.box")
Run Code Online (Sandbox Code Playgroud)

在我看来,最终证书将具有所需的所有属性.很明显,来自上述参数的许多值只包含您根据自己的要求修改的示例.我在这里没有描述一些其他常见步骤,如在受信任的根中导入根证书,导出证书等.这些步骤不是你主要问题的psrt.