如何以编程方式或使用命令行程序更新现有 CA 模板的有效期

How to update the validity period of an existing CA template programatically or using command-line program

我正在寻找一种方法来更新现有 CA 模板的有效期,您知道这是否可以使用 certutil、任何其他命令或以编程方式使用 Powershell 或 C#。

CA 在 Windows Server 2008 R2 上 运行ning。

我的目标是 运行 一个每天更新特定模板有效期的脚本,以便使用该模板注册的任何请求都在特定日期过期,比如 2016 年 12 月 31 日。

谢谢,

http://www.expta.com/2010/08/how-to-create-certificates-with-longer.html

据此,您可以使用certutil更改CA Lifetime或Maximum validity period。我假设您的模板过期限制了您。也许创建一个有效期更长的新模板?

假设您是 运行 AD CS 企业 CA,证书模板存储在 Active Directory 中,位于配置 NC 中。

(正如 CryptoGuy 在评论中指出的那样,Microsoft 不支持 这种方法 - 你真的应该只使用证书模板mmc, certtmpl.msc, 对于这个任务)

要检索证书模板:

$CertTemplateParams = @{
    LDAPFilter = '(&(objectClass=pKICertificateTemplate))'
    SearchBase = 'CN=Certificate Templates,CN=Public Key Services,CN=Services,{0}' -f ([adsi]'LDAP://RootDSE').configurationNamingContext[0]
    Properties = 'pKIExpirationPeriod'
}
$Templates = Get-ADObject @CertTemplateParams

筛选您需要的模板:

$UserTemplate = $Templates |Where-Object { $_.Name -eq "User" }

pKIExpirationPeriod attribute 表示 64 位 FILETIME 结构,但您可以使用 [BitConverter]::ToInt64():

将其转换为时间跨度
# File time type counts in 100-nanosecond intervals, we need seconds
$Validity = New-TimeSpan -Seconds $([System.BitConverter]::ToInt64($UserTemplate.pKIExpirationPeriod, 0) * -.0000001)

现在向时间跨度添加一些时间:

$Validity.Add($(New-TimeSpan -Days 365))

将其转换回 64 位字节数组:

$NewExpirationPeriod = [System.BitConverter]::GetBytes($($Validity.TotalSeconds * -10000000))

使用Set-ADObject更改模板对象:

Set-ADObject -Identity $UserTemplate.objectGuid -Replace @{pKIExpirationPeriod = $NewExpirationPeriod}

计算

$NewExpirationPeriod = [System.BitConverter]::GetBytes($([Int64]$Validity.TotalSeconds * -10000000))