簡體   English   中英

無需外部工具,自動將帶鏈的 x509 證書從 Server 2008 R2 導出到 p7b 文件?

[英]Automate export x509 certificate w/chain from Server 2008 R2 to a p7b file WITHOUT external tools?

我集中管理域控制器,但站點管理員在本地管理他們自己的數字發送器。 我可以通過向導輕松地將包含整個鏈的 X509 證書(不需要私鑰)從 Windows Server 2008 R2 域控制器導出到 p7b 文件:

~~~~~~~~~~~~~~~~~~

...5. 證書導出向導打開。 點擊下一步。

  1. 在“導出文件格式”對話框中,執行以下操作:

    一種。 選擇加密消息語法標准 – PKCS #7 證書 (.P7B)。

    如果可能,請選中在證書路徑中包含所有證書。

    C。 點擊下一步。

  2. 在要導出的文件對話框中,單擊瀏覽。

  3. 在另存為對話框中,執行以下操作:

    一種。 在文件名框中,鍵入 ciroots.p7b。

    在保存類型框中,選擇 PKCS #7 證書 (*.p7b)。

    C。 單擊保存。

  4. 在要導出的文件對話框中,單擊下一步。

  5. 在“完成證書導出向導”頁面上,單擊“完成”。

~~~~~~~~~~~~~~~~~~

它工作得很好。 生成的文件可以很好地導入到數字發送器中進行身份驗證。 它使站點管理員可以訪問鏈中的其他證書(如果他們尚未導入)。 它不需要包含私鑰,因為沒有它它也能正常工作。

問題是,我需要手動執行此操作,實際上是幾十次,每個業務站點一次,因為每個站點都有自己的域控制器,每個域控制器都有自己的證書。 必須有一種方法可以自動執行此證書導出(PowerShell w/.NET、certutil.exe 等)。 也許使用 System.Security.Cryptography.X509Certificates X509IncludeOption 和 WholeChain 的東西,但我無法讓它工作:

$Cert = (dir Cert:\\localmachine\\my)[0]

# PKCS7 證書導出文件擴展名為 .p7b。

$CertCollection = 新對象

System.Security.Cryptography.X509Certificates.X509Certificate2Collection

$證書| %{[void]$CertCollection.Add($_)}

$Exported_pkcs7 = $CertCollection.Export('Pkcs7')

$out_FileName = $ENV:COMPUTERNAME + ".p7b"

$My_Export_Path = 'd:\\CertFiles\\' + $out_FileName

Set-Content -path $My_Export_Path -Value $Exported_pkcs7 -encoding Byte

使用此代碼,我只能獲得證書,而不是其鏈中的其余證書。 我不需要整個腳本,只需要復制導出 w/chain 的部分,我已經可以通過 GUI 手動完成。

您需要構建證書鏈以獲取鏈證書並將它們添加到集合中:

function Export-Certificate {
[CmdletBinding()]
    param(
        [Parameter(Mandatory = $true)]
        [Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,
        [Parameter(Mandatory = $true)]
        [IO.FileInfo]$OutputFile,
        [switch]$IncludeAllCerts
    )
    $certs = New-Object Security.Cryptography.X509Certificates.X509Certificate2Collection
    if ($IncludeAllCerts) {
        $chain = New-Object Security.Cryptography.X509Certificates.X509Chain
        $chain.ChainPolicy.RevocationMode = "NoCheck"
        [void]$chain.Build($Certificate)
        $chain.ChainElements | ForEach-Object {[void]$certs.Add($_.Certificate)}
        $chain.Reset()
    } else {
        [void]$certs.Add($Certificate)
    }
    Set-Content -Path $OutputFile.FullName -Value $certs.Export("pkcs7") -Encoding Byte
}

這是發布了很長一段時間,但它有幫助。 我稍微改進了腳本以允許流水線操作並添加幫助。

function Export-CertificateChain {
<#
.SYNOPSIS
    Create p7b certificate container.
.DESCRIPTION
    Create p7b certificate container.
.EXAMPLE
    PS C:\> ls "C:\PKI Trust Chain\Certificates" -File -Recurse | Get-PfxCertificate | where issuer -match 'Internal|External' | Export-CertificateChain -OutputFile C:\Temp\PKITrustChain.p7b
    Loop thru the folder "C:\PKI Trust Chain\Certificates" (assuming it contains only certificates), load the certficiates and add all certificates where issuer matches into the p7b file. 
.EXAMPLE
    PS C:\> ls "cert:\localMachine\" -Recurse | where issuer -match 'Internal|External' | Export-CertificateChain -OutputFile C:\Temp\PKITrustChain.p7b
    Loop thru the certificate stroe where issuer matches and adds them into the p7b file. 
.INPUTS
    [Security.Cryptography.X509Certificates.X509Certificate2], [IO.FileInfo]
.OUTPUTS
    None.
.NOTES
Author: Patrick Sczepanski  (Vadims Podans)
Original script found: https://stackoverflow.com/questions/33512409/automate-export-x509-certificate-w-chain-from-server-2008-r2-to-a-p7b-file-witho
Version: 20200505
#>
    [CmdletBinding()]
    param(
        # certificate to add to p7b file
        [Parameter(Mandatory = $true, ValueFromPipeline=$true)]
        [Security.Cryptography.X509Certificates.X509Certificate2]$Certificate,
        # path an name of the p7b container
        [Parameter(Mandatory = $true)]
        [IO.FileInfo]$OutputFile,
        # automatically add the trust chain for each certificate. Requires the trust chain being available.
        [switch]$IncludeAllCerts
    )
    Begin{
        $certs = New-Object Security.Cryptography.X509Certificates.X509Certificate2Collection
    }
    Process {
        if ($IncludeAllCerts) {
            $chain = New-Object Security.Cryptography.X509Certificates.X509Chain
            $chain.ChainPolicy.RevocationMode = "NoCheck"
            [void]$chain.Build($Certificate)
            $chain.ChainElements | ForEach-Object {[void]$certs.Add($_.Certificate)}
            $chain.Reset()
        } else {
            [void]$certs.Add($Certificate)
        }
    }
    End {
        Set-Content -Path $OutputFile.FullName -Value $certs.Export("pkcs7") -Encoding Byte
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM