PowerShell Self Signed Certificate

Azure VPN Gateway 每年要更新憑證

根憑證

生成根憑證

$cert = New-SelfSignedCertificate -Type Custom -KeySpec Signature `
-Subject "CN=P2SRootCert22" -KeyExportPolicy Exportable `
-HashAlgorithm sha256 -KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" -KeyUsageProperty Sign -KeyUsage CertSign -FriendlyName "P2S Root Cert 2022"

更新根憑證

查詢舊憑證

Get-ChildItem -Path "Cert:\CurrentUser\My"
ThumbprintSubject
3AB2CE7FBDE37A37BCD73ABE4F34C6FF8A8AD8ACP2SRootCert21
9159C8F9FEACA4FF14E497B3BE773892105525E4P2SRootCert22

複製出一個新憑證 (預設期限一年)

$cert = Get-ChildItem -Path "Cert:\CurrentUser\My\9159C8F9FEACA4FF14E497B3BE773892105525E4"
New-SelfSignedCertificate -CloneCert $cert
ThumbprintSubject
3AB2CE7FBDE37A37BCD73ABE4F34C6FF8A8AD8ACP2SRootCert21
9159C8F9FEACA4FF14E497B3BE773892105525E4P2SRootCert22
3E3322B7D988A012BA054E849797A0FA31B4B7AEP2SRootCert23

複製時也可以改內容

New-SelfSignedCertificate -CloneCert $cert -Subject "CN=P2SRootCert23" -FriendlyName "P2S Root Cert 2023"

P.S. 9159C8F9FEACA4FF14E497B3BE773892105525E4 要改成你要改的憑證指紋

用戶端憑證

使用既有根憑證產生對應用戶端憑證 (子憑證)

New-SelfSignedCertificate -Type Custom -DnsName P2SChildCert23 -KeySpec Signature `
-Subject "CN=P2SChildCert" -KeyExportPolicy Exportable `
-HashAlgorithm sha256 -KeyLength 2048 `
-CertStoreLocation "Cert:\CurrentUser\My" `
-Signer $cert -TextExtension @("2.5.29.37={text}1.3.6.1.5.5.7.3.2")  -FriendlyName "P2S Child Cert 2023"
ThumbprintSubjectEnhancedKeyUsageList
3AB2CE7FBDE37A37BCD73ABE4F34C6FF8A8AD8ACCN=P2SRootCert21 
9159C8F9FEACA4FF14E497B3BE773892105525E4CN=P2SRootCert22 
3E3322B7D988A012BA054E849797A0FA31B4B7AECN=P2SRootCert23 
2CE7FBDE37A37BCD73A2BA054E849797A0FA31BCN=P2SChildCert用戶端驗證

匯出根憑證公開金鑰 (.cer)

產生及匯出 P2S 的憑證:PowerShell - Azure VPN Gateway | Microsoft Learn

certmgr.msc
螢幕擷取畫面顯示 [憑證] 視窗,已依序選取 [所有工作] 及 [匯出]。
螢幕擷取畫面顯示不要匯出私密金鑰。
螢幕擷取畫面顯示匯出 Base-64 編碼。
螢幕擷取畫面顯示記事本中開啟 CER 檔案,並醒目提示憑證資料。

匯出用戶端憑證

螢幕擷取畫面顯示 [憑證] 視窗,已選取 [所有工作] 和 [匯出]。
螢幕擷取畫面顯示已選取 [是,匯出私密金鑰]。
匯出檔案格式頁面的螢幕擷取畫面。
螢幕擷取畫面顯示輸入並確認密碼。

 

安裝匯出的用戶端憑證

透過 P2S 連線連接的每個用戶端都需要以本機方式安裝用戶端憑證。 

若要安裝用戶端憑證,請參閱安裝點對站連線的用戶端憑證

產生及匯出 P2S 的憑證:PowerShell - Azure VPN Gateway | Microsoft Learn

設定 VNet Gateway P2S 憑證

將上面匯出的根憑證用記事本打開後的內容 (MIIxxxx=)

填到 虛擬網路閘道 > 點對站設定 > 根憑證 > 公開憑證資料

要撤銷某個用戶端憑證,可以填到已撤銷的憑證中的指紋欄位

如果要撤銷整個根憑證下的全部用戶端,則直接刪除根憑證

憑證位置

憑證 > 位置
AzureClient.pfx > 目前的使用者\個人\憑證
AzureRoot.cer > 本機電腦\受信任的根憑證授權單位

疑難排解 Azure 點對站連線問題 - Azure VPN Gateway | Microsoft Learn

參照

Azure SQL 透過 Azure VPN Gateway 實現內網連接 | 御用小本本 - 點部落 (dotblogs.com.tw)

如何使用 PowerShell 建立開發測試用途的自簽憑證 (Self-Signed Certificate) | The Will Will Web (miniasp.com)

[Day24] 第二十四課 Azure 點對站(P2S)安全連線[安全] - iT 邦幫忙::一起幫忙解決難題,拯救 IT 人的一天 (ithome.com.tw)

New-SelfSignedCertificate (pki) | Microsoft Learn

產生及匯出 P2S 的憑證:PowerShell - Azure VPN Gateway | Microsoft Learn

PS5