LoginSignup
16
24

More than 3 years have passed since last update.

自己署名証明書の作り方がいつの間にか変わっていました

Last updated at Posted at 2017-10-31

PowershellDSCでAzureManagementAPIを証明書認証で利用していて、今回新たに環境構築していました。

その際に、windowsのPCで自己署名証明書を作成したかったのですが、
以前はmakecertを使用していましたが、いつの間にか使用できなくなっていたのであたらしいやり方でやってみようと思います。

以前のやりかた

makecertで証明書を作成し、パスワードを設定してエクスポートするというやりかたです。

まずは、

証明書を作成します。
cmdで実行します。

"C:\Program Files (x86)\Windows Kits\8.1\bin\x86\makecert.exe" -sky exchange -r -n "CN=AzureManagementAPI" -pe -a sha256 -len 2048 -ss My "AzureManagementAPI.cer"

次に

パスワードを作成しておきます。
ここからはpowershellで実行します。

$MyPwd = ConvertTo-SecureString -String "パスワード" -Force ?AsPlainText

先ほど作成した証明書を探します。

$AzureCert = Get-ChildItem -Path Cert:\CurrentUser\My | where {$_.Subject -match "AzureManagementAPI"}

最後に

証明書をエクスポートします

Export-PfxCertificate -FilePath C:\AzureManagementAPI.pfx -Password $MyPwd -Cert $AzureCert

ポータルへアップロード

エクスポートしたファイルをAzureポータルにアップロードして完了です。

あたらしいやり方

まずは

証明書作成

makecertの代わりになるのがNew-SelfSignedCertificateのようです。
New-SelfSignedCertificateを実行して証明書を作成します。

$cert = New-SelfSignedCertificate -DnsName AzureManagementAPI -CertStoreLocation "cert:\LocalMachine\My" -NotAfter (Get-Date).AddYears(1)

次に

パスワード作成

$password = ConvertTo-SecureString -String "パスワード" -Force -AsPlainText

最後に

エクスポートします。

pfxでエクスポートする場合は以下を実行

Export-PfxCertificate -Cert $cert -FilePath "C:\azuremanagementapi.pfx" -Password $password

または、cerでエクスポートする場合は以下を実行

証明書をAzure管理ポータルで使用する場合は以下を実行します。

Export-Certificate -Type CERT -Cert $cert -FilePath C:\azuremanagementapi.cer

アップロード

上記で作成したcerファイルの方の証明書をAzureのポータルへアップロードすることで
AzureManagementAPIを利用することができるようになります。

以上です。

最後にシェルの全文を載せておきます。
信頼されたルートへは一旦ファイルへ掃き出しインストールするようになっています。


$DnsName = "AzureManagementAPI"
$NotAfter = (Get-Date).AddHours(1)
$passwordString = "password"
$filename = "mycert"

$CertDir = "C:\"
$pfxFileName = $CertDir + $filename + ".pfx"
$cerFileName = $CertDir + $filename +  ".cer"
$cert = New-SelfSignedCertificate -DnsName $DnsName -CertStoreLocation "cert:\CurrentUser\My" -NotAfter $NotAfter -KeyAlgorithm RSA
#PFX形式
$password = ConvertTo-SecureString -String $passwordString -Force -AsPlainText
Export-PfxCertificate -Cert $cert -FilePath $pfxFileName -Password $password
Import-PfxCertificate -Password $password -FilePath $pfxFileName -CertStoreLocation "cert:\CurrentUser\Root"

#CER形式
#Export-Certificate -Type CERT -Cert $cert -FilePath $cerFileName
#Import-Certificate -FilePath $cerFileName -CertStoreLocation "cert:\CurrentUser\Root"

参考

https://docs.microsoft.com/ja-jp/azure/cloud-services/cloud-services-certs-create#what-are-management-certificates
https://docs.microsoft.com/ja-jp/azure/azure-api-management-certs

16
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
24