Windowsにて自己署名証明書の作成

自己署名証明書Powershellで作成できるようなったらしく、最近ではそいつを使うのが一般的みたいです。その方法の備忘録。WEBサーバで利用できるSSL証明書を作成します。

Powershellバージョン

実行方法

管理者権限でPowershellプロンプトを起動して、以下コマンドを実行しています。

自己署名証明書を作成して、ローカルPC内に証明書を格納します。有効期限は30年後です。

> $cert = New-SelfSignedCertificate `
            -Type SSLServerAuthentication `
            -Subject "sample.com" `
            -DnsName "sample.com" `
            -CertStoreLocation "cert:\LocalMachine\My" `
            -KeyDescription "Self-signed certificate" `
            -KeyExportPolicy Exportable `
            -NotAfter (Get-Date).AddYears(30)

細かいパラメータは、下記の公式リファレンスを参照。

New-SelfSignedCertificate

証明書マネージャ(Certlm.msc)から、作成した証明書を確認できます。

f:id:goodbyegangster:20191009231301p:plain

上記の証明書を、CERTファイルでエクスポートします。

> Export-Certificate `
            -Type CERT `
            -Cert $cert `
            -FilePath "C:\self-signed.cer"

Export-Certificate

上記の証明書を、PFXファイルでエクスポートします。まず、PFXファイルに設定するパスワードを定義します。

> $MyPwd = ConvertTo-SecureString `
            -String "Passw0rd" `
            -Force `
            -AsPlainText

ConvertTo-SecureString

PFXファイルとしてエクスポートします。

> Export-PfxCertificate `
            -Cert $cert `
            -Password $MyPwd `
            -FilePath "C:\self-signed.pfx"

Export-PfxCertificate

最後に、作成されている証明書を削除します。

> Remove-Item $cert.PSPath

Remove-Item