簡體   English   中英

使用 PowerShell 設置具有特定啟用目的的 CA 證書

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

如何使用 PowerShell 在相關 Windows 證書存儲中以編程方式更改證書頒發機構的啟用目的?

這可以在證書 MMC 管理單元中執行

這是否只能根據 StackOverflow 使用帶有CertSetCertificateContextProperty的 P/Invoke :如何設置證書用途? {C#}

理想情況下,我想導入一個自定義的受信任的根證書頒發機構,並且僅出於客戶端身份驗證的目的啟用它。

CertSetCertificateContextProperty為核心的 PowerShell Cmdlet。 感謝Crypt32和他們在另一篇文章中的回答以獲得指導。

示例用法:

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