【问题标题】:Import pfx file into particular certificate store from command line从命令行将 pfx 文件导入特定的证书存储
【发布时间】:2011-07-07 11:11:45
【问题描述】:

使用 CertUtil 将证书从 pfx 文件导入用户的个人存储相对容易:

certutil –f –p [certificate_password] –importpfx C:\[certificate_path_and_name].pfx 

但这最终会出现在当前用户的个人商店中。我在 LocalMachine 上的 TrustedPeople 中需要它。

有什么方法可以从命令行执行此操作,通过在 certutil importpfx 上调用不同的参数,使用另一个 certutil 命令或不同的实用程序? Powershell是另一种可能,虽然我不太了解。

干杯, 马特

【问题讨论】:

    标签: powershell certificate certutil


    【解决方案1】:

    在这里为未来的读者锚定我的发现。

    将证书导入本地计算机上的受信任根证书颁发机构:

    CERTUTIL -addstore -enterprise -f -v root "somCertificat.cer"
    

    在本地机器上将 pfx 导入个人

    CERTUTIL -f -p somePassword -importpfx "somePfx.pfx"
    

    将 pfx 导入本地计算机上的受信任人员 - Link 导入 pfx.exe

    importpfx.exe -f "somePfx.pfx" -p "somePassword" -t MACHINE -s "TRUSTEDPEOPLE"
    

    将证书导入本地计算机上的受信任人员

    Certutil -addstore -f "TRUSTEDPEOPLE" "someCertificate.cer"
    

    【讨论】:

    • 令人沮丧的是,CERTUTIL 无法将 PFX 导入 TRUSTEDPEOPLE。 CertUtil 与远程 PSSession (PowerShell) 一起工作正常,但 importpfx 不能(仅供参考,importpfx 的源是home.fnal.gov/~jklemenc/src/importpfx.cpp)。我不确定 CERTUTIL 有什么不同,但它确实适用于远程 PS 会话,但我无法将证书放入 Trusted People。叹。这两天很郁闷。
    • 我们最终编写了一组 powershell 函数来完成这项艰巨的工作。查看 CiPsLib.Certificates.psm1 -> Import-Certificate github.com/rasmus/CiPsLib/tree/master/tools
    • 如何在不使用密码的情况下导入?有可能吗?
    • @drgmak,如果证书受空密码保护,则使用 -p ""。如果它受密码保护,您需要知道密码。
    【解决方案2】:

    对于寻找这个的其他人,我无法将certutil -importpfx 用于特定商店,并且我不想下载 jaspernygaard 的答案提供的 importpfx 工具以避免复制文件的要求到大量服务器。我最终在here 显示的 powershell 脚本中找到了答案。

    代码使用System.Security.Cryptography.X509Certificates 导入证书,然后将其移动到所需的存储区:

    function Import-PfxCertificate { 
    
        param([String]$certPath,[String]$certRootStore = "localmachine",[String]$certStore = "My",$pfxPass = $null) 
        $pfx = new-object System.Security.Cryptography.X509Certificates.X509Certificate2 
    
        if ($pfxPass -eq $null) 
        {
            $pfxPass = read-host "Password" -assecurestring
        } 
    
        $pfx.import($certPath,$pfxPass,"Exportable,PersistKeySet") 
        
        $store = new-object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore) 
        $store.open("MaxAllowed") 
        $store.add($pfx) 
        $store.close() 
    }
    

    【讨论】:

    • 你能帮我理解价值观及其含义吗? “最大允许”、“我的”、
    • @RaviKhambhati:我是我正在使用的证书商店的名称。有关证书存储位置的更多信息,请参阅 msdn.microsoft.com/en-us/library/windows/desktop/…。 MaxAllowed 是我用来打开的 OpenFlags 的值。老实说,我只是复制并粘贴了该部分,但您可以在此处了解有关它的可能值的更多信息:msdn.microsoft.com/en-us/library/…
    • 非常感谢。当我们从 IIS 执行相同的操作时,这些值将是什么
    • 您的代码确实是错误。您正在使用非 ASCII ,这会导致 powershell 中出现未定义的行为。它可能会随机失败,随机线,随机原因,在farking不可靠的powershell解析器中。
    • 我已经调整了它@recolic。我不确定这是否适用于我的环境,或者我是否已修复它但忘记更新此答案。
    【解决方案3】:

    检查这些链接: http://www.orcsweb.com/blog/james/powershell-ing-on-windows-server-how-to-import-certificates-using-powershell/

    进口证书:http://poshcode.org/1937

    你可以这样做:

    dir -Path C:\Certs -Filter *.cer | Import-Certificate -CertFile $_ -StoreNames AuthRoot, Root -LocalMachine -Verbose
    

    【讨论】:

      【解决方案4】:

      对于 Windows 10:

      将证书导入当前用户的受信任根证书颁发机构:

      certutil -f -user -p oracle -importpfx root "example.pfx"
      

      将证书导入当前用户的可信人员:

      certutil -f -user -p oracle -importpfx TrustedPeople "example.pfx"
      

      将证书导入本地计算机上的受信任根证书颁发机构:

      certutil -f -user -p oracle -enterprise -importpfx root "example.pfx"
      

      将证书导入本地计算机上的受信任人员:

      certutil -f -user -p oracle -enterprise -importpfx TrustedPeople "example.pfx"
      

      【讨论】:

      • 我真的很难将用户证书添加到新商店。最后一个例子对我有用。注意:如果您使用商店名称(例如“ABC”)而不是“TrustedPeople”,则会创建商店!无需使用 -addstore 参数来添加商店……这是我一直坚持的事情。
      【解决方案5】:

      在 Windows 2012 R2 (Win 8.1) 及更高版本中,您还拥有“官方”Import-PfxCertificate cmdlet

      以下是代码的一些基本部分(一个适应性示例):

      Invoke-Command -ComputerName $Computer -ScriptBlock {
              param(
                  [string] $CertFileName,
                  [string] $CertRootStore,
                  [string] $CertStore,
                  [string] $X509Flags,
                  $PfxPass)
              $CertPath = "$Env:SystemRoot\$CertFileName"
              $Pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
              # Flags to send in are documented here: https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509keystorageflags%28v=vs.110%29.aspx
              $Pfx.Import($CertPath, $PfxPass, $X509Flags) #"Exportable,PersistKeySet")
              $Store = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $CertStore, $CertRootStore
              $Store.Open("MaxAllowed")
              $Store.Add($Pfx)
              if ($?)
              {
                  "${Env:ComputerName}: Successfully added certificate."
              }
              else
              {
                  "${Env:ComputerName}: Failed to add certificate! $($Error[0].ToString() -replace '[\r\n]+', ' ')"
              }
              $Store.Close()
              Remove-Item -LiteralPath $CertPath
          } -ArgumentList $TempCertFileName, $CertRootStore, $CertStore, $X509Flags, $Password
      

      基于 mao47 的代码和一些研究,我写了一篇小文章和一个简单的 cmdlet,用于将 PFX 证书导入/推送到远程计算机。

      Here's 我的文章包含更多详细信息和完整代码,也适用于 PSv2(Server 2008 R2 / Windows 7 上的默认设置),只要您启用了 SMB 和管理共享访问权限。

      【讨论】:

      • 虽然这在理论上可以回答问题,it would be preferable 在这里包含答案的基本部分,并提供链接以供参考。
      【解决方案6】:

      这里是完整代码,导入pfx,添加iis网站,添加ssl绑定:

      $SiteName = "MySite"
      $HostName = "localhost"
      $CertificatePassword = '1234'
      $SiteFolder = Join-Path -Path 'C:\inetpub\wwwroot' -ChildPath $SiteName
      $certPath = 'c:\cert.pfx'
      
      
      Write-Host 'Import pfx certificate' $certPath
      $certRootStore = “LocalMachine”
      $certStore = "My"
      $pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
      $pfx.Import($certPath,$CertificatePassword,"Exportable,PersistKeySet") 
      $store = New-Object System.Security.Cryptography.X509Certificates.X509Store($certStore,$certRootStore) 
      $store.Open('ReadWrite')
      $store.Add($pfx) 
      $store.Close() 
      $certThumbprint = $pfx.Thumbprint
      
      
      Write-Host 'Add website' $SiteName
      New-WebSite -Name $SiteName -PhysicalPath $SiteFolder -Force
      $IISSite = "IIS:\Sites\$SiteName"
      Set-ItemProperty $IISSite -name  Bindings -value @{protocol="https";bindingInformation="*:443:$HostName"}
      if($applicationPool) { Set-ItemProperty $IISSite -name  ApplicationPool -value $IISApplicationPool }
      
      
      Write-Host 'Bind certificate with Thumbprint' $certThumbprint
      $obj = get-webconfiguration "//sites/site[@name='$SiteName']"
      $binding = $obj.bindings.Collection[0]
      $method = $binding.Methods["AddSslCertificate"]
      $methodInstance = $method.CreateInstance()
      $methodInstance.Input.SetAttributeValue("certificateHash", $certThumbprint)
      $methodInstance.Input.SetAttributeValue("certificateStoreName", $certStore)
      $methodInstance.Execute()
      

      【讨论】:

      • 您可能希望导入标志为"Exportable,MachineKeySet,PersistKeySet",以便将私钥放入机器密钥,而不是放入当前用户的配置文件中。
      【解决方案7】:

      在较新版本的 Windows 中,Certuil 具有 [CertificateStoreName],我们可以在其中提供商店名称。在早期版本的 Windows 中,这是不可能的。

      安装 *.pfx 证书: certutil -f -p "" -enterprise -importpfx root ""

      安装 *.cer 证书: certutil -addstore -enterprise -f -v root ""

      下面的命令可以在 windows cmd 中执行。 C:>certutil -importpfx -? 用法: CertUtil [选项] -importPFX [CertificateStoreName] PFXFile [修饰符]

      【讨论】:

        猜你喜欢
        • 2013-05-25
        • 2015-04-01
        • 2014-10-25
        • 2011-03-02
        • 1970-01-01
        • 2012-02-26
        • 2016-12-14
        • 2017-02-03
        • 1970-01-01
        相关资源
        最近更新 更多