我该如何为本地测试创建客户端证书以进行SSL双向认证?

26

我在尝试在运行IIS7的Web应用上设置双向认证。客户端大多数将是移动设备,首先我正在尝试使用第三代iPad运行演示。我想从我的工作站(也在运行IIS)开始,然后将工作证书复制到iPad。

然而,我遇到了障碍。

我已经使网站通过https安全运行,并安装了自签名的服务器证书,但我似乎无法弄清楚如何生成客户端证书,我可以将其安装在iPad上。由于我正在运行Windows 7的本地工作站上工作,因此我无法使用通常的http://machinename/CertSvr来做到这一点。

所以我想知道是否有办法让makecert生成测试客户端证书,或者我是否可以更改服务器证书中的使用标志,使其适用于客户端。或者也许还有一些工具,最近一天的谷歌搜索尚未发现?

更新:

我找到了这个指南,并且完全按照指南操作。它似乎都起作用了,没有错误,并且我最终得到了两个pfx文件,一个用于服务器,一个用于客户端(我使用pvk2pfx生成了这些文件,并保留了原始的.pvk.cer文件以防万一)。

我将服务器证书安装在Certificates (Local Computer) > Trusted Root Certification Authority下,客户端证书则安装在Certificates (Current User) > Personal下。我还将服务器证书(CA证书)导入了IIS。当IIS配置为接受或忽略客户端证书时,一切都正常运作。然而,一旦将其设置为“必需”,在请求站点时就会收到403.7的错误提示。我也尝试了将客户端证书导入IE / Chrome的证书存储中,但同样没有成功。

我是否有什么明显的错误?


没有人回应,但最终我找到了这篇文章,并确认所有操作都完美无误。希望这对有需要的人有所帮助。 - immutabl
我刚刚为该命令输入了一些密码 - C:\Windows\system32>makecert -r -pe -ss my -sr LocalMachine -n “CN=YangsoftCA” -sv “YangsoftCA.pvk” YangsoftCA.cer - Steam
注意 - 不要直接复制粘贴该代码。它包含非法/微软双引号,这会导致命令失败。您必须使用键盘键入它们。如果不这样做,您将收到错误提示 - makecert 失败 - 错误:CryptCertStrToNameW 失败。然而,此错误还有许多其他原因。 - Steam
C:\Windows\system32>makecert -r -pe -ss my -sr LocalMachine -n "CN=YangsoftCA" -sv "YangsoftCA.pvk" YangsoftCA.cer - immutabl
在Windows 8之后,makecert包含在“Windows软件开发工具包(SDK)”和“Windows驱动程序开发工具包(WDK)”中。因此,您需要先安装其中一个,然后打开“VS2015开发人员命令提示符”来使用makecert。链接:https://developer.microsoft.com/en-us/windows/downloads/windows-8-1-sdk - Mert Can Ilis
2个回答

5

启用客户端证书在本地的IIS Express中:

将\YourSlnFolder\.vs\config\applicationhost.config文件中的<section name="access" overrideModeDefault="Deny" />改为<section name="access" overrideModeDefault="Allow" />

<sectionGroup name="system.webServer">
...
  <sectionGroup name="security">
  ...
    <section name="access" overrideModeDefault="Allow" />

然后按照以下方式编辑您的Web.config:

<configuration>
    <system.webServer>
        <security>
            <access sslFlags="SslRequireCert" />
        </security>
    </system.webServer>
</configuration>

启用IIS上的客户端证书:

在IIS管理器中访问网站并单击SSL设置。然后将应用程序设置为需要SSL和需要客户端证书。

enter image description here

创建新证书:

启动VS开发人员命令提示符

根证书:

makecert.exe -r -n "CN=TestRootCertificate" -pe -sv TestRootCertificate.pvk -a sha1 -len 2048 -b 01/01/2017 -e 01/01/2030 -cy authority TestRootCertificate.cer

请输入您的密码。

创建证书吊销列表(CRL)

makecert -crl -n "CN=TestRootCertificate" -r -sv TestRootCertificate.pvk TestRootCertificate.crl

将Bundle文件转换成.pfx文件(pvk2pfx.exe需要安装VS2017的“桌面开发与C++”组件)

pvk2pfx.exe -pvk TestRootCertificate.pvk -pi {password} -spc TestRootCertificate.cer -pfx TestRootCertificate.pfx

从根证书获取客户端证书:

makecert.exe -ic TestRootCertificate.cer -iv TestRootCertificate.pvk -pe -sv localtestclientcert.pvk -a sha1 -n "CN=localtestclientcert" -len 2048 -b 01/01/2015 -e 01/01/2030 -sky exchange localtestclientcert.cer -eku 1.3.6.1.5.5.7.3.2

请输入密码。

pvk2pfx.exe -pvk localtestclientcert.pvk -pi {password} -spc localtestclientcert.cer -pfx localtestclientcert.pfx

导入证书。

启动mmc.exe。

File -> Add or Remove Snap-ins -> Certificates -> Add -> Computer account -> Local computer

Certificates (Local Computer) -> Personal -> Certificates -> Right click -> All tasks -> Import -> localtestclientcert.pfx

Certificates (Local Computer) -> Trusted Root Certification Authorities -> Certificates -> Right click -> All tasks -> Import -> RootCertificate.cer

用于浏览器身份验证:

File -> Add or Remove Snap-ins -> Certificates -> Add -> My user account

Certificates - Current User -> Personal -> Certificates -> Right click -> All tasks -> Import -> localtestclientcert.pfx

现在访问您的网站需要一个客户端证书,该证书由服务器信任:

enter image description here

如果您按照此指南进行操作并出现以下错误:

HTTP Error 500.19 - Internal Server Error
The requested page cannot be accessed because the related configuration data for the page is invalid.

或者

HTTP Error 403.7 - Forbidden
The page you are attempting to access requires your browser to have a Secure Sockets Layer (SSL) client certificate that the Web server recognizes.

您可能需要重新启动计算机。请注意,仅关闭iisexpress进程或Visual Studio是不够的。可以通过解决500.19错误来解决,但证书比较棘手,因此建议重新启动计算机。

如果出现The request was aborted: Could not create SSL/TLS secure channel错误,则可能是由于应用程序池无法访问特定证书。

证书(本地计算机)-> 个人-> 证书-> localtestclientcert-> 右键单击-> 所有任务-> 管理私钥-> 添加IIS APPPOOL\YourWebSite并授予完全控制权限。


如果您正在使用基于OpenSSL的Web服务器进行测试,建议更改为“-a sha256”,以避免出现“ca md too weak”错误。 - Fredrik Orderud

4
也许在您提出这个问题时它还不存在,但是微软现在有一个指南,可以完全做到这一点。易于跟随并对我非常有效!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接