使用根 CA 签名者生成自签名证书
Generate Self-signed certificate with Root CA Signer
场景:我在 Windows 服务器 2012r2 上使用 PowerShell 生成根证书,并希望使用它动态签署新创建的中级和 Web 证书生成(并销毁)dev/test 个环境。这些脚本是远程部署的,目的是尽可能保持纯 PowerShell。在 Windows 10/2016 中,这相对容易,在生成根证书后:
$Cert = New-SelfSignedCertificate -Signer $Root -Subject "CN=$Subject"
我已经使用 COM X509Enrollment.CX509CertificateRequestCertificate
和 Security.Cryptography.X509Certificates.X509Certificate2
在我已经使用了一段时间的混蛋 PS 中生成了根证书,主要是因为我需要确保主题和用法设置得非常具体。我不太确定如何在没有上述(我以前使用过)的情况下使用它来签署标准证书。
有一些在 C# 中使用 Bouncy Castle(见下文)的示例,我可以将它们绑定到 PowerShell,但是我需要在动态 dev/test 环境中额外部署它,我希望能够在 Powershell 中(如果需要,通过 COM)以最少的依赖性执行此操作。
- C# Generate Self Signed Certificates on the Fly
- How do I use Bouncy Castle in Powershell
就我而言,避免使用 makecert 和 openssl 的最终解决方案是使用 Powershell 和 BouncyCastle。我从 PSBouncyCastle by RLipscombe and pushed 1.8.1 Bouncy Castle in. My forked version is the one I've used for the script, the fork resides at Forked: PSBouncyCastle.New 分叉了 PSBouncyCastle 存储库。
然后我以Whosebug: C# Generate Certificates on the Fly为灵感写了下面的powershell,我会把这个添加到我的GitHub并发表评论,我会尽快修改我做:
Import-Module -Name PSBouncyCastle.New
function New-SelfSignedCertificate {
[CmdletBinding()]
param (
[string]$SubjectName,
[string]$FriendlyName = "New Certificate",
[object]$Issuer,
[bool]$IsCA = $false,
[int]$KeyStrength = 2048,
[int]$ValidYears = 2,
[hashtable]$EKU = @{}
)
# Needed generators
$random = New-SecureRandom
$certificateGenerator = New-CertificateGenerator
if($Issuer -ne $null -and $Issuer.HasPrivateKey -eq $true)
{
$IssuerName = $Issuer.IssuerName.Name
$IssuerPrivateKey = $Issuer.PrivateKey
}
# Create and set a random certificate serial number
$serial = New-SerialNumber -Random $random
$certificateGenerator.SetSerialNumber($serial)
# The signature algorithm
$certificateGenerator.SetSignatureAlgorithm('SHA256WithRSA')
# Basic Constraints - certificate is allowed to be used as intermediate.
# Powershell requires either a $null or reassignment or it will return this from the function
$certificateGenerator = Add-BasicConstraints -isCertificateAuthority $IsCA -certificateGenerator $certificateGenerator
# Key Usage
if($EKU.Count -gt 0)
{
$certificateGenerator = $certificateGenerator | Add-ExtendedKeyUsage @EKU
}
# Create and set the Issuer and Subject name
$subjectDN = New-X509Name -Name ($SubjectName)
if($Issuer -ne $null) {
$IssuerDN = New-X509Name -Name ($IssuerName)
}
else
{
$IssuerDN = New-X509Name -Name ($SubjectName)
}
$certificateGenerator.SetSubjectDN($subjectDN)
$certificateGenerator.SetIssuerDN($IssuerDN)
# Authority Key and Subject Identifier
if($Issuer -ne $null)
{
$IssuerKeyPair = ConvertTo-BouncyCastleKeyPair -PrivateKey $IssuerPrivateKey
$IssuerSerial = [Org.BouncyCastle.Math.BigInteger]$Issuer.GetSerialNumber()
$authorityKeyIdentifier = New-AuthorityKeyIdentifier -name $Issuer.IssuerName.Name -publicKey $IssuerKeyPair.Public -serialNumber $IssuerSerial
$certificateGenerator = Add-AuthorityKeyIdentifier -certificateGenerator $certificateGenerator -authorityKeyIdentifier $authorityKeyIdentifier
}
# Validity range of the certificate
[DateTime]$notBefore = (Get-Date).AddDays(-1)
if($ValidYears -gt 0) {
[DateTime]$notAfter = $notBefore.AddYears($ValidYears)
}
$certificateGenerator.SetNotBefore($notBefore)
$certificateGenerator.SetNotAfter($notAfter)
# Subject public key ~and private
$subjectKeyPair = New-KeyPair -Strength $keyStrength -Random $random
if($IssuerPrivateKey -ne $null)
{
$IssuerKeyPair = [Org.BouncyCastle.Security.DotNetUtilities]::GetKeyPair($IssuerPrivateKey)
}
else
{
$IssuerKeyPair = $subjectKeyPair
}
$certificateGenerator.SetPublicKey($subjectKeyPair.Public)
# Create the Certificate
$IssuerKeyPair = $subjectKeyPair
$certificate = $certificateGenerator.Generate($IssuerKeyPair.Private, $random)
# At this point you have the certificate and need to convert it and export, I return the private key for signing the next cert
$pfxCertificate = ConvertFrom-BouncyCastleCertificate -certificate $certificate -subjectKeyPair $subjectKeyPair -friendlyName $FriendlyName
return $pfxCertificate
}
这个 powershell 的几个用法示例是:
生成根 CA
$TestRootCA = New-SelfSignedCertificate -subjectName "CN=TestRootCA" -IsCA $true
Export-Certificate -Certificate $test -OutputFile "TestRootCA.pfx" -X509ContentType Pfx
生成标准自签名
$TestSS = New-SelfSignedCertificate -subjectName "CN=TestLocal"
Export-Certificate -Certificate $TestSS -OutputFile "TestLocal.pfx" -X509ContentType Pfx
生成证书,用根证书签名
$TestRootCA = New-SelfSignedCertificate -subjectName "CN=TestRootCA" -IsCA $true
$TestSigned = New-SelfSignedCertificate -subjectName "CN=TestSignedByRoot" -issuer $TestRootCA
Export-Certificate -Certificate $test -OutputFile "TestRootCA.pfx" -X509ContentType Pfx
Export-Certificate -Certificate $test -OutputFile "TestRootCA.pfx" -X509ContentType Pfx
生成具有特定用途的自签名
$TestServerCert = New-SelfSignedCertificate -subjectName "CN=TestServerCert" -EKU @{ "ServerAuthentication" = $true }
请注意,-EKU 参数通过拼接接受,这样做是为了确保添加到 Add-ExtendedKeyUsage 的任何内容都得到有效传递。它接受以下证书用法:
- 数字签名
- 不可否认性
- 密钥加密
- 数据加密
- 密钥协议
- KeyCertSign
- CrlSign
- 仅加密
- 仅解密
这符合我的需要,而且似乎适用于我们用于动态环境的所有 Windows 平台。
"Itiverba Self-Signed certificate generator" (http://www.itiverba.com/en/software/itisscg.php) 是 Windows 的免费 GUI 工具,允许您创建自己的 CA 证书并使用它签署最终证书。您可以导出 PEM、CER、DER、PFX 文件格式的证书。
只需 3 行编码:
主题:CN="Testcorp - Private CA"
基本约束:V(选中)
基本约束/主题类型:CA
输入文件名和 select 文件格式,然后单击 "create certificate" 按钮。您的自定义 CA 证书已完成。
简单地这样做怎么样:
$cert = New-SelfSignedCertificate -FriendlyName "MyCA"
-KeyExportPolicy ExportableEncrypted
-Provider "Microsoft Strong Cryptographic Provider"
-Subject "SN=TestRootCA" -NotAfter (Get-Date).AddYears($ExpiryInYears)
-CertStoreLocation Cert:\LocalMachine\My -KeyUsageProperty All
-KeyUsage CertSign, CRLSign, DigitalSignature
重要的参数是 -KeyUsageProperty
和 -KeyUsage
。
创建根证书的简单方法是执行以下操作。请注意确保证书是根证书的文本扩展名。这样的证书 必须 放在 root 证书库中以表示信任。例如。 'cert:\LocalMachine\My' 商店。
确保 KeyUsage 是您想要的。这当然可以更改,但 Microsoft 并不擅长记录为什么你应该按照他们的建议去做。
证书的moving/copying必须通过导出证书并再次导入来完成。或者在正确的地方创建证书。请注意,一般情况下,证书只会在我的商店中创建。 Certificate Provider PowerShell functions.
中描述了一些支持命令
证书默认是可导出的。
$rootCert = New-SelfSignedCertificate -CertStoreLocation Cert:\CurrentUser\My `
-DnsName "RootCA" `
-TextExtension @("2.5.29.19={text}CA=true") `
-KeyUsage CertSign,CrlSign,DigitalSignature;
中提取的
场景:我在 Windows 服务器 2012r2 上使用 PowerShell 生成根证书,并希望使用它动态签署新创建的中级和 Web 证书生成(并销毁)dev/test 个环境。这些脚本是远程部署的,目的是尽可能保持纯 PowerShell。在 Windows 10/2016 中,这相对容易,在生成根证书后:
$Cert = New-SelfSignedCertificate -Signer $Root -Subject "CN=$Subject"
我已经使用 COM X509Enrollment.CX509CertificateRequestCertificate
和 Security.Cryptography.X509Certificates.X509Certificate2
在我已经使用了一段时间的混蛋 PS 中生成了根证书,主要是因为我需要确保主题和用法设置得非常具体。我不太确定如何在没有上述(我以前使用过)的情况下使用它来签署标准证书。
有一些在 C# 中使用 Bouncy Castle(见下文)的示例,我可以将它们绑定到 PowerShell,但是我需要在动态 dev/test 环境中额外部署它,我希望能够在 Powershell 中(如果需要,通过 COM)以最少的依赖性执行此操作。
- C# Generate Self Signed Certificates on the Fly
- How do I use Bouncy Castle in Powershell
就我而言,避免使用 makecert 和 openssl 的最终解决方案是使用 Powershell 和 BouncyCastle。我从 PSBouncyCastle by RLipscombe and pushed 1.8.1 Bouncy Castle in. My forked version is the one I've used for the script, the fork resides at Forked: PSBouncyCastle.New 分叉了 PSBouncyCastle 存储库。
然后我以Whosebug: C# Generate Certificates on the Fly为灵感写了下面的powershell,我会把这个添加到我的GitHub并发表评论,我会尽快修改我做:
Import-Module -Name PSBouncyCastle.New
function New-SelfSignedCertificate {
[CmdletBinding()]
param (
[string]$SubjectName,
[string]$FriendlyName = "New Certificate",
[object]$Issuer,
[bool]$IsCA = $false,
[int]$KeyStrength = 2048,
[int]$ValidYears = 2,
[hashtable]$EKU = @{}
)
# Needed generators
$random = New-SecureRandom
$certificateGenerator = New-CertificateGenerator
if($Issuer -ne $null -and $Issuer.HasPrivateKey -eq $true)
{
$IssuerName = $Issuer.IssuerName.Name
$IssuerPrivateKey = $Issuer.PrivateKey
}
# Create and set a random certificate serial number
$serial = New-SerialNumber -Random $random
$certificateGenerator.SetSerialNumber($serial)
# The signature algorithm
$certificateGenerator.SetSignatureAlgorithm('SHA256WithRSA')
# Basic Constraints - certificate is allowed to be used as intermediate.
# Powershell requires either a $null or reassignment or it will return this from the function
$certificateGenerator = Add-BasicConstraints -isCertificateAuthority $IsCA -certificateGenerator $certificateGenerator
# Key Usage
if($EKU.Count -gt 0)
{
$certificateGenerator = $certificateGenerator | Add-ExtendedKeyUsage @EKU
}
# Create and set the Issuer and Subject name
$subjectDN = New-X509Name -Name ($SubjectName)
if($Issuer -ne $null) {
$IssuerDN = New-X509Name -Name ($IssuerName)
}
else
{
$IssuerDN = New-X509Name -Name ($SubjectName)
}
$certificateGenerator.SetSubjectDN($subjectDN)
$certificateGenerator.SetIssuerDN($IssuerDN)
# Authority Key and Subject Identifier
if($Issuer -ne $null)
{
$IssuerKeyPair = ConvertTo-BouncyCastleKeyPair -PrivateKey $IssuerPrivateKey
$IssuerSerial = [Org.BouncyCastle.Math.BigInteger]$Issuer.GetSerialNumber()
$authorityKeyIdentifier = New-AuthorityKeyIdentifier -name $Issuer.IssuerName.Name -publicKey $IssuerKeyPair.Public -serialNumber $IssuerSerial
$certificateGenerator = Add-AuthorityKeyIdentifier -certificateGenerator $certificateGenerator -authorityKeyIdentifier $authorityKeyIdentifier
}
# Validity range of the certificate
[DateTime]$notBefore = (Get-Date).AddDays(-1)
if($ValidYears -gt 0) {
[DateTime]$notAfter = $notBefore.AddYears($ValidYears)
}
$certificateGenerator.SetNotBefore($notBefore)
$certificateGenerator.SetNotAfter($notAfter)
# Subject public key ~and private
$subjectKeyPair = New-KeyPair -Strength $keyStrength -Random $random
if($IssuerPrivateKey -ne $null)
{
$IssuerKeyPair = [Org.BouncyCastle.Security.DotNetUtilities]::GetKeyPair($IssuerPrivateKey)
}
else
{
$IssuerKeyPair = $subjectKeyPair
}
$certificateGenerator.SetPublicKey($subjectKeyPair.Public)
# Create the Certificate
$IssuerKeyPair = $subjectKeyPair
$certificate = $certificateGenerator.Generate($IssuerKeyPair.Private, $random)
# At this point you have the certificate and need to convert it and export, I return the private key for signing the next cert
$pfxCertificate = ConvertFrom-BouncyCastleCertificate -certificate $certificate -subjectKeyPair $subjectKeyPair -friendlyName $FriendlyName
return $pfxCertificate
}
这个 powershell 的几个用法示例是:
生成根 CA
$TestRootCA = New-SelfSignedCertificate -subjectName "CN=TestRootCA" -IsCA $true
Export-Certificate -Certificate $test -OutputFile "TestRootCA.pfx" -X509ContentType Pfx
生成标准自签名
$TestSS = New-SelfSignedCertificate -subjectName "CN=TestLocal"
Export-Certificate -Certificate $TestSS -OutputFile "TestLocal.pfx" -X509ContentType Pfx
生成证书,用根证书签名
$TestRootCA = New-SelfSignedCertificate -subjectName "CN=TestRootCA" -IsCA $true
$TestSigned = New-SelfSignedCertificate -subjectName "CN=TestSignedByRoot" -issuer $TestRootCA
Export-Certificate -Certificate $test -OutputFile "TestRootCA.pfx" -X509ContentType Pfx
Export-Certificate -Certificate $test -OutputFile "TestRootCA.pfx" -X509ContentType Pfx
生成具有特定用途的自签名
$TestServerCert = New-SelfSignedCertificate -subjectName "CN=TestServerCert" -EKU @{ "ServerAuthentication" = $true }
请注意,-EKU 参数通过拼接接受,这样做是为了确保添加到 Add-ExtendedKeyUsage 的任何内容都得到有效传递。它接受以下证书用法:
- 数字签名
- 不可否认性
- 密钥加密
- 数据加密
- 密钥协议
- KeyCertSign
- CrlSign
- 仅加密
- 仅解密
这符合我的需要,而且似乎适用于我们用于动态环境的所有 Windows 平台。
"Itiverba Self-Signed certificate generator" (http://www.itiverba.com/en/software/itisscg.php) 是 Windows 的免费 GUI 工具,允许您创建自己的 CA 证书并使用它签署最终证书。您可以导出 PEM、CER、DER、PFX 文件格式的证书。
只需 3 行编码:
主题:CN="Testcorp - Private CA"
基本约束:V(选中)
基本约束/主题类型:CA
输入文件名和 select 文件格式,然后单击 "create certificate" 按钮。您的自定义 CA 证书已完成。
简单地这样做怎么样:
$cert = New-SelfSignedCertificate -FriendlyName "MyCA"
-KeyExportPolicy ExportableEncrypted
-Provider "Microsoft Strong Cryptographic Provider"
-Subject "SN=TestRootCA" -NotAfter (Get-Date).AddYears($ExpiryInYears)
-CertStoreLocation Cert:\LocalMachine\My -KeyUsageProperty All
-KeyUsage CertSign, CRLSign, DigitalSignature
重要的参数是 -KeyUsageProperty
和 -KeyUsage
。
创建根证书的简单方法是执行以下操作。请注意确保证书是根证书的文本扩展名。这样的证书 必须 放在 root 证书库中以表示信任。例如。 'cert:\LocalMachine\My' 商店。
确保 KeyUsage 是您想要的。这当然可以更改,但 Microsoft 并不擅长记录为什么你应该按照他们的建议去做。
证书的moving/copying必须通过导出证书并再次导入来完成。或者在正确的地方创建证书。请注意,一般情况下,证书只会在我的商店中创建。 Certificate Provider PowerShell functions.
中描述了一些支持命令证书默认是可导出的。
$rootCert = New-SelfSignedCertificate -CertStoreLocation Cert:\CurrentUser\My `
-DnsName "RootCA" `
-TextExtension @("2.5.29.19={text}CA=true") `
-KeyUsage CertSign,CrlSign,DigitalSignature;
中提取的