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

creating valid test SSL certificates for IIS

我想在带有 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

包含在 Windows 10 中的 New-SelfSignedCertificate 的新版本被描述为 here。可以使用 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 

输出看起来像

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


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

B7DE93CB88E99B01D166A986F7BF2D82A0E541FF 对于使用证书进行签名很重要。如果您忘记了值,您可以通过 CN name

找到它
dir cert:\CurrentUser\My | where Subject -Like "CN=My Test (PowerShell)*"

或使用 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")

在我看来,最终证书将具有所需的所有属性。很明显,上述参数中的许多值仅包含示例,您必须根据自己的要求在那里进行修改。在Trusted Root中导入根证书、导出证书等其他一些常见的步骤在此不再赘述。这些步骤不是你主要问题的答案。