Komaki's blog <(・`彡 )з

Windows ServerでLet's EncryptのDNS認証を試みて失敗した話

以前、FileMaker ServerにLet’s EncryptのSSL証明書を設定するというブログを書きました。

ブログを書いている時は、HTTPでの認証が一般的だろう程度でやっていました。

しかし、もっと簡単にできるはずだ!と思いググってみた。

更新

2019-02-02

以下の記事を書きましたので、よろしければご覧ください。

legoを使ってLet’s EncryptのSSL証明書をDNS認証で発行しFileMaker Serverに設定する方法


DNSのTXTレコードを設定すれば、DNS認証ができると分かったので、せっかくなのでチャレンジしてみた。

結果的には途中で諦めて、DebianでDNS認証をしちゃいましたが、Windows環境で色々と模索したので、メモを残しておきます。

win-acmeでDNS認証を試みる

GitHubのWikiを確認する

こういうときは、Wiki Homeを見ましょう。

win-acmeは、DNS認証は対応しているとのこと。

CLIツールレベルで対応しているのは、Azure DNS validationだけのようです。

その他のDNSを使っている場合は、**自分でスクリプトを書いて指定してね。**ということみたい。

DNS認証時に利用するスクリプト

スクリプトについて、具体的にどのように記述して指定するのかをググってみた。

このリンクの方が、具体的なコードを書いてくれていました。

https://github.com/PKISharp/win-acme/issues/817#issuecomment-381164264

I created scripts to do this that work on server 2012.
As my server is behind a firewall this turned out to be the wrong method to use, but you may find them useful.
Replace DNSServer with the name of your DNSserver and repeat that line if you have more than one server.
Replace YourZoneName with your domain name.

create-dns.bat:
powershell -file ./create-dns.ps1 %1 %2 %3

create-dns.ps1:
Import-module dnsserver
Add-DnsServerResourceRecord -Name $args[1] -Computername DNSServer-TXT -ZoneName "YourZoneName" -DescriptiveText $args[2]

delete-dns.bat:
powershell -file ./delete-dns.ps1 %1 %2

delete-dns.ps1:
Import-module dnsserver
Remove-DnsServerResourceRecord -Force -Computername DNSServer-ZoneName "YourZoneName" -RRType "Txt" -Name $args[1]

DNSサーバーを追加

Windows ServerにDNSサーバーを追加しました。

実行してみた時のログ

 [INFO] A Simple ACME Client for Windows (WACS)
 [INFO] Software version 1911.2.6726.40690 (RELEASE)
 [INFO] IIS version 10.0
 [INFO] ACME server https://acme-v01.api.letsencrypt.org/
 [INFO] Please report issues at https://github.com/PKISharp/win-acme

 N: Create new certificate
 M: Create new certificate with advanced options
 L: List scheduled renewals
 R: Renew scheduled
 S: Renew specific
 A: Renew *all*
 V: Revoke certificate
 C: Cancel scheduled renewal
 X: Cancel *all* scheduled renewals
 Q: Quit

 Please choose from the menu: m

 [INFO] Running in Advanced mode

 1: Single binding of an IIS site
 2: SAN certificate for all bindings of an IIS site
 3: SAN certificate for all bindings of multiple IIS sites
 4: Manually input host names
 C: Cancel

 Which kind of certificate would you like to create?: 4

 Enter comma-separated list of host names, starting with the primary one: hoge2.frudens.app

 [INFO] Plugin Manual generated target [Manual] [1 binding - hoge2.frudens.app]

 1: [dns-01] Azure DNS
 2: [dns-01] Run external program/script to create and update records
 3: [http-01] Save file on local (network) path
 4: [http-01] Self-host verification files (recommended)
 5: [http-01] Upload verification file to FTP(S) server
 6: [http-01] Upload verification file to WebDav path
 C: Cancel

 How would you like to validate this certificate?: 2

 Path to script that creates DNS records. Parameters passed are the hostname, record name and token: scripts\create-dns.bat

 Path to script that deletes DNS records. Parameters passed are the hostname and record name: scripts\delete-dns.bat

 1: Create or update ftps bindings in IIS
 2: Create or update https bindings in IIS
 3: Do not run any installation steps
 4: Run a custom script
 C: Cancel

 Which installer should run for the certificate?: 3

 [INFO] Authorize identifier: hoge2.frudens.app
 [INFO] Authorizing hoge2.frudens.app using dns-01 validation (DnsScript)
 [INFO] Script scripts\create-dns.bat starting with parameters hoge2.frudens.app _acme-challenge.hoge2.frudens.app hw6PTBqElAV9MUhPjNVavDu948jnQ1PGLDeilb6kVnU [INFO]
C:\Users\teruhirokomaki\Desktop\win-acme.v1.9.11.2>powershell.exe -ExecutionPolicy RemoteSigned -File .\scripts\create-dns.ps1 hoge2.frudens.app _acme-challenge.hoge2.frudens.app hw6PTBqElAV9MUhPjNVavDu948jnQ1PGLDeilb6kVnU

 [WARN] Script execution timed out after 5 minutes, will keep running in the background
 [INFO] Answer should now be available at _acme-challenge.hoge2.frudens.app
 [INFO] Script scripts\delete-dns.bat starting with parameters hoge2.frudens.app _acme-challenge.hoge2.frudens.app
 [EROR] Script error: Remove-DnsServerResourceRecord : Failed to get the zone information for hoge2.frudens.app on server DNSServer-ZoneName.
 [EROR] Script error: At C:\Users\teruhirokomaki\Desktop\win-acme.v1.9.11.2\scripts\delete-dns.ps1:2 char:1
 [EROR] Script error: + Remove-DnsServerResourceRecord -Force -Computername DNSServer-ZoneNam ...
 [EROR] Script error: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 [EROR] Script error:     + CategoryInfo          : NotSpecified: (_acme-challenge.hoge2.frudens.app:root/Microsoft/...rResourceRecord) [Rem
 [EROR] Script error:    ove-DnsServerResourceRecord], CimException
 [EROR] Script error:     + FullyQualifiedErrorId : WIN32 1722,Remove-DnsServerResourceRecord
 [INFO]
C:\Users\teruhirokomaki\Desktop\win-acme.v1.9.11.2>powershell.exe -ExecutionPolicy RemoteSigned -File .\scripts\delete-dns.ps1 hoge2.frudens.app _acme-challenge.hoge2.frudens.app
Error: Remove-DnsServerResourceRecord : Failed to get the zone information for hoge2.frudens.app on server DNSServer-ZoneName.
Error: At C:\Users\teruhirokomaki\Desktop\win-acme.v1.9.11.2\scripts\delete-dns.ps1:2 char:1
Error: + Remove-DnsServerResourceRecord -Force -Computername DNSServer-ZoneNam ...
Error: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Error:     + CategoryInfo          : NotSpecified: (_acme-challenge.hoge2.frudens.app:root/Microsoft/...rResourceRecord) [Rem
Error:    ove-DnsServerResourceRecord], CimException
Error:     + FullyQualifiedErrorId : WIN32 1722,Remove-DnsServerResourceRecord

 [EROR] Error authorizing [Manual] [1 binding - hoge2.frudens.app]
 [EROR] AcmeWebException: Unexpected error
 +Response from server:
        + Code: BadRequest
        + Content: {
  "type": "urn:acme:error:badNonce",
  "detail": "JWS has invalid anti-replay nonce Y50_OdnTDpPBS4zJ10kh7_iojVNWr_hScGKEUl2w2rI",
  "status": 400
}
 [EROR] Create certificate failed

 N: Create new certificate
 M: Create new certificate with advanced options
 L: List scheduled renewals
 R: Renew scheduled
 S: Renew specific
 A: Renew *all*
 V: Revoke certificate
 C: Cancel scheduled renewal
 X: Cancel *all* scheduled renewals
 Q: Quit

そもそも

(上の内容のDNSserverは置き換えてください。)

Cloud DNSやRoute53であれば、API使えば設定できるけど、なんの認証もなく、DNSにTXTレコードを作成したり、削除することはできるわけないよね。

この作業って、TXTレコードに設定する文字列を取得するための作業だと思っていたけど、指定しているスクリプトを改めて見てみると、TXTレコードを作成したり、削除するような内容なんですよね。

そりゃ、できるわけないよね…

Go製のACMEクライアントを使えば良いのではないか?

ということで、ACME clientsを見てみる。

このためにGoをインストールしたくないしな…

そもそもWindowsじゃなくて良いのではないか?

ここまで来たけど、Windowsで作業するのが嫌になってきたので、macOSかLinuxでDNS認証しよう!と思い、今回のチャレンジは失敗しました。

あとがき

Windows 10 であれえば、WSL(Windows Subsystem for Linux)でLinuxをインストールして、certbotを実行すれば良いんですけどね…

今回は、Windows Server ですので…

Windowsとか、PowerShellとか、本当に辛い…

#Windows Server #Let's Encrypt #SSL #DNS #win-acme