Skip to content

Instantly share code, notes, and snippets.

@gocha
Last active October 9, 2019 10:52
Show Gist options
  • Save gocha/719cb5fe9cf51450edfe8189fd58137e to your computer and use it in GitHub Desktop.
Save gocha/719cb5fe9cf51450edfe8189fd58137e to your computer and use it in GitHub Desktop.
開発用のルートCA証明書とクライアント証明書を一括作成する
# 方法: 開発中に使用する一時的な証明書を作成する | Microsoft Docs
# https://docs.microsoft.com/ja-jp/dotnet/framework/wcf/feature-details/how-to-create-temporary-certificates-for-use-during-development
$ErrorActionPreference = "Stop"
#-------------------------------------------------------------------------------
# ルート証明書を作成する
$rootCertPassword = Read-Host -Prompt "CA 証明書の秘密キー" -AsSecureString
$rootCert = New-SelfSignedCertificate -CertStoreLocation Cert:\CurrentUser\My `
-DnsName "MyDevelopmentCA" `
-NotBefore (Get-Date).AddMinutes(-5) `
-NotAfter (Get-Date).AddYears(10) `
-KeyAlgorithm RSA `
-KeyLength 2048 `
-HashAlgorithm SHA256 `
-TextExtension @("2.5.29.19={text}CA=true") `
-KeyUsage CertSign,CrlSign,DigitalSignature
$rootCertPath = Join-Path -Path Cert:\CurrentUser\My -ChildPath $rootCert.Thumbprint
Export-PfxCertificate -Cert $rootCertPath -FilePath 'RootCA.pfx' -Password $rootCertPassword
Export-Certificate -Cert $rootCertPath -FilePath 'RootCA.crt'
#-------------------------------------------------------------------------------
# クライアント証明書を一括作成する
# 以前のステップで Cert:\CurrentUser\My に秘密キーを含む CA 証明書がインストールされていること。
# 前提条件を満たさない場合、Import-Certificate で証明書をストアにインストールしてください。
#$rootCert = Get-PfxCertificate -FilePath "RootCA.pfx"
$password = Read-Host -Prompt "クライアント証明書の秘密キー" -AsSecureString
for ($i = 1; $i -le 100; $i++) {
$basename = "Client $i"
$commonName = "My Development Client #$i"
$testCert = New-SelfSignedCertificate -CertStoreLocation Cert:\CurrentUser\My `
-Subject "CN=$commonName" `
-NotAfter (Get-Date).AddYears(10) `
-KeyLength 2048 `
-KeyUsage DigitalSignature `
-Signer $rootCert `
-TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")
Export-PfxCertificate -Cert $testCert -FilePath "$basename.pfx" -Password $password
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment