使用PowerShell在Root中禁用证书

时间:2017-11-24 23:24:06

标签: powershell ssl ssl-certificate x509certificate

使用Windows Server Core。

我想在商店根目录的文件夹中禁用证书。我有要禁用的证书的指纹,根据下图,我可以通过Windows用户界面执行此操作。但我想通过Powershell做到这一点。

我无法通过PowerShell找到如何禁用证书,你知道怎么做吗?

N.B。我对删除证书不感兴趣

如果您很好奇,这是此处讨论的问题的解决方案: production.log empty on Rails 4 / Capistrano / Passenger / Nginx server (digital ocean)

https://www.namecheap.com/support/knowledgebase/article.aspx/9774/2238/incomplete-certificate-chain-on-windows-servers

1 个答案:

答案 0 :(得分:1)

执行此操作的唯一方法是使用p / invoke interop调用非托管CertSetCertificateContextProperty非托管函数,并传递ASN编码的空X509 EKU扩展值(两个字节,0x30和{{ 1}})明确禁用属性中的EKU。

代码如下所示:

0x0

请注意,函数调用需要# define unmanaged function interop signatures $signature = @" [DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern bool CertSetCertificateContextProperty( IntPtr pCertContext, uint dwPropId, uint dwFlags, IntPtr pvData ); [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)] public struct CRYPTOAPI_BLOB { public uint cbData; public IntPtr pbData; } "@ Add-Type -MemberDefinition $signature -Namespace PKI -Name Crypt32 # create empty X509 EKU extension value. Empty value literally disables all EKU $bytes = New-Object byte[] -ArgumentList 2 $bytes[0] = 48 $bytes[1] = 0 # do unmanaged stuff $pbData = [Runtime.InteropServices.Marshal]::AllocHGlobal(2) [Runtime.InteropServices.Marshal]::Copy($bytes, 0, $pbData, 2) # fill pvData structure $blob = New-Object PKI.Crypt32+CRYPTOAPI_BLOB -Property @{ cbData = 2; pbData = $pbData; } # do more unmanaged stuff $pvData = [Runtime.InteropServices.Marshal]::AllocHGlobal([Runtime.InteropServices.Marshal]::SizeOf([type][PKI.Crypt32+CRYPTOAPI_BLOB])) # copy data value to unmanaged memory [Runtime.InteropServices.Marshal]::StructureToPtr($blob, $pvData, $false) # call CertSetCertificateContextProperty function [PKI.Crypt32]::CertSetCertificateContextProperty($Cert.Handle,9,0,$pvData) # release unmanaged memory to prevent memory leak [Runtime.InteropServices.Marshal]::FreeHGlobal($pbData) [Runtime.InteropServices.Marshal]::FreeHGlobal($pvData) 变量中的有效X509Certificate2证书对象。