简体   繁体   English

使用 PowerShell 设置具有特定启用目的的 CA 证书

[英]Setting a CA Certificate, with specific Enabled Purposes, using PowerShell

How do I programmatically change the Enabled Purposes of a Certificate Authority, in the relevant Windows Certificate Store, using PowerShell?如何使用 PowerShell 在相关 Windows 证书存储中以编程方式更改证书颁发机构的启用目的?

This is possible to do in the Certificates MMC snap-in这可以在证书 MMC 管理单元中执行

Is this only possible using P/Invoke with CertSetCertificateContextProperty as per StackOverflow: How to set certificate purposes?这是否只能根据 StackOverflow 使用带有CertSetCertificateContextProperty的 P/Invoke :如何设置证书用途? {C#} {C#}

Ideally, I want to import a custom Trusted Root Certificate Authority and only enable it for the purpose of Client Authentication.理想情况下,我想导入一个自定义的受信任的根证书颁发机构,并且仅出于客户端身份验证的目的启用它。

A PowerShell Cmdlet that uses CertSetCertificateContextProperty at it's core.CertSetCertificateContextProperty为核心的 PowerShell Cmdlet。 Thank you to Crypt32 and their answer on another post for guidance.感谢Crypt32和他们在另一篇文章中的回答以获得指导。

Example Usage:示例用法:

Set-CertificateEku -StoreLocation 'CurrentUser' -StoreName 'Root' -CertificateThumbprint 'ffffffffffffffffffffffffffffffffffffffff' -Oids @("1.3.6.1.5.5.7.3.2") # Client Authentication
Function Set-CertificateEku {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory)]
        [ValidateSet('CurrentUser', 'LocalMachine')]
        $StoreLocation,
        
        [Parameter(Mandatory)]
        $StoreName,

        [Parameter(Mandatory)]
        $CertificateThumbprint,

        [Parameter(Mandatory)]
        $Oids
    )
    $StoreLocation = switch($StoreLocation) {
        'CurrentUser' {
            [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser
        }
        'LocalMachine' {
            [System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine
        }
    }
    try {
        $CertificateStore = [System.Security.Cryptography.X509Certificates.X509Store]::new($StoreName, $StoreLocation)
        $CertificateStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite -bor [System.Security.Cryptography.X509Certificates.OpenFlags]::OpenExistingOnly)
    } catch {
        Write-Error "Could not Open Certificate Store $StoreName in $StoreLocation"
        return $false
    }
    $Certificates = $CertificateStore.Certificates.Find(
        [System.Security.Cryptography.X509Certificates.X509FindType]::FindByThumbprint,
        $CertificateThumbprint,
        $false
    )
    if($Certificates.Count -eq 0) {
        Write-Error "Could not find Certificate $CertificateThumbprint in $StoreName in $StoreLocation"
        return $false
    }
    $Certificate = $Certificates[0]
    

    $PKICrypt32 = @"
    [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 $PKICrypt32 -Namespace 'PKI' -Name 'Crypt32'

    $OIDs = [Security.Cryptography.OidCollection]::new()
    foreach($Oid in $Oids) {
        [void]$OIDs.Add([Security.Cryptography.Oid]::new($Oid))
    }
    $EKU = [Security.Cryptography.X509Certificates.X509EnhancedKeyUsageExtension]::new($OIDs, $false)
    $pbData = [Runtime.InteropServices.Marshal]::AllocHGlobal($EKU.RawData.Length)
    [Runtime.InteropServices.Marshal]::Copy($EKU.RawData, 0, $pbData, $EKU.RawData.Length)

    $Blob = New-Object PKI.Crypt32+CRYPTOAPI_BLOB -Property @{
        cbData = $EKU.RawData.Length;
        pbData = $pbData;
    }
    $pvData = [Runtime.InteropServices.Marshal]::AllocHGlobal([Runtime.InteropServices.Marshal]::SizeOf([type][PKI.Crypt32+CRYPTOAPI_BLOB]))
    [Runtime.InteropServices.Marshal]::StructureToPtr($Blob, $pvData, $false)

    $Result = [PKI.Crypt32]::CertSetCertificateContextProperty($Certificate.Handle, 9, 0, $pvData)
    [Runtime.InteropServices.Marshal]::FreeHGlobal($pvData)
    [Runtime.InteropServices.Marshal]::FreeHGlobal($pbData)
    $CertificateStore.Close()
    return $Result
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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