简体   繁体   中英

Disable a certificate in the Root using PowerShell

Using Windows Server Core.

I want to disable a certificate in the store root's folder. I have the thumbprint of the certificate that I want to disable and as per the picture below I can do it via the Windows UI. But I want to do it via Powershell.

I couldn't find how to disable a certificate via PowerShell, do you know how?

NB I am not interested in deleting the certificate

If you are curious, this is a solution to the problem that is discussed here: https://www.namecheap.com/support/knowledgebase/article.aspx/9774/2238/incomplete-certificate-chain-on-windows-servers

在此处输入图片说明

The only way to do this is to call unmanaged CertSetCertificateContextProperty unmanaged function by using p/invoke interop and passing ASN-encoded empty X509 EKU extension value (which is two bytes, 0x30 and 0x0 ) to explicitly disable EKUs in the property.

The code would look like this:

# 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)

Note that function call requires valid X509Certificate2 certificate object in $cert variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM