导入证书时 CRYPT_E_NOT_FOUND

Ada*_*ram 6 iis ssl ssl-certificate

我正在尝试自动化生成证书签名请求的过程,然后从 Windows Server 2012 R2 服务器上的 CA 导入响应,以用作 IIS 中 SSL 绑定的证书。我能够生成 CSR,然后我将其提供给安全团队,然后安全团队为我提供然后导入的响应,但在导入时遇到了麻烦。

此服务器在工作组中。以为我会提到这一点,所以没有 AD 注册政策。

这是我的过程:

  1. 在相关服务器上使用 certreq.exe 生成 CSR。生成的 INF 文件如下所示:
[Version]
Signature = "$Windows NT$"
[NewRequest]
Subject = "C=US,S=California,L=City,O=Company,OU=IT,CN=hostname"
Exportable = TRUE
KeyLength = 2048
KeySpec = 1
KeyUsage = 0xa0
MachineKeySet = True
ProviderName = "Microsoft RSA SChannel Cryptographic Provider"
ProviderType = 12
Silent = True
SMIME = False
RequestType = PKCS10
Run Code Online (Sandbox Code Playgroud)

然后,通过执行以下操作,此 INF 文件将转换为 CSR .req 文件:

certreq.exe -new "C:\inffile.inf" "C:\certreq.req"
Run Code Online (Sandbox Code Playgroud)

REQ 文件被发送给安全团队,他们给我一个 .CER 文件,当手动导入时,它实际上添加了来自 Digicert 的三个证书。我期望的证书以及看起来像是一些中间 CA 的证书。

这是通过 MMC 证书管理单元导入时的样子。

在此处输入图片说明

如果我像这样通过 MMC 导入证书,它不会显示在服务器证书下的 IIS 管理器中,所以我看得更深一些。我尝试像这样通过 IIS 管理器完成证书签名请求,并且证书显示出来,我很高兴。

在此处输入图片说明

但是,我无法使用 GUI,因为我使用的是脚本。

我确认请求在带有私钥的证书注册请求中。

在此处输入图片说明

我确认CSR的公钥和我提供的p7b是一样的。

certutil -dump issuedcert.cer
certutil -dump certreq.req
Run Code Online (Sandbox Code Playgroud)

问题:我从证书注册请求中导出了 CSR 并查看了公钥。它与issuedcert.cer 中的不同。看起来这是问题所在,但为什么呢?

然后我尝试使用 certreq.exe 来接受响应,但它不起作用。

certreq.exe -accept -machine "C:\issuedcert.cer"
Run Code Online (Sandbox Code Playgroud)

快完成了,但没有。我不断收到此错误消息:

在此处输入图片说明

tho*_*her 0

这是我过去使用过的函数:

\n\n
function AddCertificate(\n    [string] $MachineName,\n    [string] $CertString, #String to search for in the Certificate Store to get the correct Thumbprint\n    [string] $SiteName,   #Sitename to bind the certificate to.\n    [string] $certname,   #File name of the certificate\n    [string] $certPass,   #Password for the certificate\n    [string] $certPath)   #path on the machine where this script runs that contains the certificate path needs to not have a Trailing \\\n{\n    $Protocol = "https"\n    $destinationFolder = "c$\\temp\\pfx-files"\n    $servers = $MachineName\n    $session = New-PsSession \xe2\x80\x93ComputerName $servers\n    $servers | foreach-Object{if(!(Test-Path -path ("\\\\$_\\"+$destinationFolder))) {New-Item ("\\\\$_\\"+$destinationFolder) -Type Directory}}\n    $servers | foreach-Object{copy-item -force -Path c:\\temp\\pfx-files\\*.* -Destination ("\\\\$_\\"+$destinationFolder)}\n    $certPath ="c:\\temp\\pfx-files" +"\\"+$certname\n    Invoke-command -Session $session -ScriptBlock {param($certPass,$certPath) certutil -p $certPass -importpfx ($certPath )}\n    $servers | foreach-object {Remove-Item -Path (("\\\\$_\\"+$destinationFolder) +"\\*.pfx")}\n    Invoke-Command -session $session {Import-Module WebAdministration}\n    $isBound = Invoke-Command -session $session {Get-WebBinding }\n    if (!(Select-String -Pattern "https" -InputObject $isbound -quiet)) \n    {\n        Invoke-command -Session $session -ScriptBlock {param([string] $S, [string] $Protocol)( New-WebBinding -Name $S -Protocol $Protocol -Port 443 -IPAddress "*" -SslFlags 0)} -ArgumentList $SiteName, $Protocol\n        Invoke-Command -session $session -ScriptBlock { param([string]$Certstring) $CertShop=Get-ChildItem -Path Cert:\\LocalMachine\\My | where-Object {$_.subject -like $CertString } | Select-Object -ExpandProperty Thumbprint}\n        Invoke-Command -Session $session -ScriptBlock {get-item -Path "cert:\\localmachine\\my\\$certShop" | new-item -path IIS:\\SslBindings\\0.0.0.0!443}\n    }\n    Exit-PSSession\n}\n
Run Code Online (Sandbox Code Playgroud)\n