#概要
動作確認できているセルフホスト式のRESTFul WEBサービスをSSLで通信するように修正しました。
ルート証明書(オレオレ)、クライアント証明書を作り、ルート証明書とポート番号を紐づけ、クライアント証明書をクライアントにインストールしました。
ちゃんと動作することは確認できたのですが、やってることは大雑把にあっていますでしょうか?
よろしくお願いいたします。
#質問内容
###1. クライアント証明書の作成方法は、以下で特におかしくないでしょうか?
PowerShell
1#5.クライアント証明書を作成する。 2$client_cert = New-SelfSignedCertificate -Type Custom -DnsName $client_cert_name -KeySpec Signature -Subject "CN=${client_cert_name}" -KeyExportPolicy Exportable -HashAlgorithm sha256 -KeyLength 2048 -CertStoreLocation "Cert:\CurrentUser\My" -Signer $root_cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")
###2. クライアント証明書のインストール手順は以下で正しいでしょうか?
サーバーで、クライアント証明書をエクスポート
クライアントで、クライアント証明書をインポートする
図2.クライアントで、クライアント証明書をインポートする手順
###3. サーバーで、クライアント証明書をエクスポートする作業(図1)を、PowerShellのExport-PfxCertificateコマンドを使ってバッチ処理に組み込みたいのですが、クライアントでのインポート時にパスワードエラーになってインポートできません。何が原因でしょうか?
図3
###4. その他の疑問
- サーバー上でクライアント証明書用のpfxファイルを作成する際に同梱する秘密鍵は、SSL通信が行われるときに各クライアントがサーバーが送ってくる共通鍵を解読するために使われるものでしょうか?
- クライアント証明書は、クライアントコンピューターに配布してインストールしてもらうものですか?
- クライアント証明書を複数のクライアントコンピューターにインストールする度に、クライアント証明書を作らなくても大丈夫でしょうか?
参考にしたサイト
PowerShell を使用したポイント対サイトの証明書の生成とエクスポート
PFXとは
よろしくお願い致します。
ソースコード
PowerShell
1$current_directory = 'D:\Work\TestService2' 2$root_cert_name = 'FirstRootCert' 3$client_cert_name = 'FirstClientCert' 4#$imd_sert_name = 'FirstImdCert' 5$pwd = 'xxxxxxxx' 6$port_no = 5000 7$service_guid = '541eea84-c788-4d23-b6b2-f5210bcdf5c5' 8 9#1.カレントディレクトリを変更する。 10Set-Location $current_directory 11 12#2.パスワードを暗号化する。 13[System.Security.SecureString]$secure_pwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText 14 15#3.ルート証明書を作成する。 16$root_cert = New-SelfSignedCertificate -Type Custom -KeySpec Signature -Subject "CN=${root_cert_name}" -KeyExportPolicy Exportable -HashAlgorithm sha256 -KeyLength 2048 -CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign 17 18#4.ルート証明書から個人情報交換ファイルと秘密キーをエクスポートする。 19[String]$rootCertPath = Join-Path -Path 'cert:\CurrentUser\My\' -ChildPath "$($root_cert.Thumbprint)" 20Export-PfxCertificate -Cert $rootCertPath -FilePath "${root_cert_name}.pfx" -Password $secure_pwd 21Export-Certificate -Cert $rootCertPath -FilePath "${root_cert_name}.crt" 22 23#5.クライアント証明書を作成する。 24$client_cert = New-SelfSignedCertificate -Type Custom -DnsName $client_cert_name -KeySpec Signature -Subject "CN=${client_cert_name}" -KeyExportPolicy Exportable -HashAlgorithm sha256 -KeyLength 2048 -CertStoreLocation "Cert:\CurrentUser\My" -Signer $root_cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2") 25 26#6.クライアント証明書から個人情報交換ファイルと秘密キーをエクスポートする。 27[String]$rootCertPath = Join-Path -Path 'cert:\CurrentUser\My\' -ChildPath "$($client_cert.Thumbprint)" 28Export-PfxCertificate -Cert $rootCertPath -FilePath "${client_cert_name}.pfx" -Password $secure_pwd 29Export-Certificate -Cert $rootCertPath -FilePath "${client_cert_name}.crt" 30 31#7.ルート証明書によって署名された新しい証明書を作成する 32#$imd_cert = New-SelfSignedCertificate -CertStoreLocation Cert:\LocalMachine\My -DnsName "${imd_sert_name}" -KeyExportPolicy Exportable -KeyLength 2048 -KeyUsage DigitalSignature,KeyEncipherment -Signer $root_Cert 33#[String]$imdCertPath = Join-Path -Path 'cert:\LocalMachine\My\' -ChildPath "$($imd_cert.Thumbprint)" 34#Export-PfxCertificate -Cert $imdCertPath -FilePath "${imd_sert_name}.pfx" -Password $secure_pwd 35#Export-Certificate -Cert $imdCertPath -FilePath "${imd_sert_name}.crt" 36 37#8.ローカルコンピューター(LocalMachine)の信頼されたルート証明機関(Root)にルート証明書をインポートし、捺印を取得する。 38$temp_cert = Import-PfxCertificate -FilePath "${root_cert_name}.pfx" -CertStoreLocation Cert:\LocalMachine\Root -Password $secure_pwd 39$hash_code = $temp_cert.Thumbprint 40 41#9.ポート番号にSSL証明書を紐づける。 42$Command = "http delete sslcert ipport=0.0.0.0:${port_no}" 43$Command | netsh 44$Command = "http add sslcert ipport=0.0.0.0:${port_no} certstorename=Root certhash=${hash_code} appid={${service_guid}} clientcertnegotiation=enable " 45$Command | netsh
環境
Microsoft Windows 10 Pro (Version 1903)
Microsoft Visual Studio Community 2019
Microsoft .NET Framework 4.7.2
あなたの回答
tips
プレビュー