2015-06-11 00:12:46 +02:00
|
|
|
package acme
|
2015-11-17 00:57:41 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
2016-01-01 15:36:30 +02:00
|
|
|
"encoding/base64"
|
2015-11-17 00:57:41 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-01-15 06:06:25 +02:00
|
|
|
"log"
|
2016-01-01 15:36:30 +02:00
|
|
|
"strings"
|
2015-11-17 00:57:41 +02:00
|
|
|
"time"
|
2016-01-22 02:38:15 +02:00
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
2015-11-17 00:57:41 +02:00
|
|
|
)
|
|
|
|
|
2016-01-22 03:25:27 +02:00
|
|
|
type preCheckDNSFunc func(domain, fqdn string) bool
|
2016-01-22 02:38:15 +02:00
|
|
|
|
2016-01-22 03:25:27 +02:00
|
|
|
var preCheckDNS preCheckDNSFunc = checkDNS
|
2016-01-22 02:38:15 +02:00
|
|
|
|
|
|
|
var preCheckDNSFallbackCount = 5
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
// DNS01Record returns a DNS record which will fulfill the `dns-01` challenge
|
|
|
|
func DNS01Record(domain, keyAuth string) (fqdn string, value string, ttl int) {
|
|
|
|
keyAuthShaBytes := sha256.Sum256([]byte(keyAuth))
|
|
|
|
// base64URL encoding without padding
|
|
|
|
keyAuthSha := base64.URLEncoding.EncodeToString(keyAuthShaBytes[:sha256.Size])
|
|
|
|
value = strings.TrimRight(keyAuthSha, "=")
|
|
|
|
ttl = 120
|
|
|
|
fqdn = fmt.Sprintf("_acme-challenge.%s.", domain)
|
|
|
|
return
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2015-11-17 00:57:41 +02:00
|
|
|
|
|
|
|
// dnsChallenge implements the dns-01 challenge according to ACME 7.5
|
|
|
|
type dnsChallenge struct {
|
2015-12-03 22:01:46 +02:00
|
|
|
jws *jws
|
2016-01-25 01:23:21 +02:00
|
|
|
validate validateFunc
|
2016-01-15 06:06:25 +02:00
|
|
|
provider ChallengeProvider
|
2015-11-17 00:57:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *dnsChallenge) Solve(chlng challenge, domain string) error {
|
2016-01-30 23:17:41 +02:00
|
|
|
logf("[INFO][%s] acme: Trying to solve DNS-01", domain)
|
2015-11-17 00:57:41 +02:00
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
if s.provider == nil {
|
|
|
|
return errors.New("No DNS Provider configured")
|
|
|
|
}
|
|
|
|
|
2015-11-17 00:57:41 +02:00
|
|
|
// Generate the Key Authorization for the challenge
|
|
|
|
keyAuth, err := getKeyAuthorization(chlng.Token, &s.jws.privKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-15 06:06:25 +02:00
|
|
|
err = s.provider.Present(domain, chlng.Token, keyAuth)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error presenting token %s", err)
|
2015-12-03 22:01:46 +02:00
|
|
|
}
|
2016-01-15 06:06:25 +02:00
|
|
|
defer func() {
|
|
|
|
err := s.provider.CleanUp(domain, chlng.Token, keyAuth)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error cleaning up %s %v ", domain, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
fqdn, _, _ := DNS01Record(domain, keyAuth)
|
2015-11-17 00:57:41 +02:00
|
|
|
|
2016-01-22 03:25:27 +02:00
|
|
|
preCheckDNS(domain, fqdn)
|
2016-01-22 02:38:15 +02:00
|
|
|
|
2016-01-25 01:32:47 +02:00
|
|
|
return s.validate(s.jws, domain, chlng.URI, challenge{Resource: "challenge", Type: chlng.Type, Token: chlng.Token, KeyAuthorization: keyAuth})
|
2015-11-17 00:57:41 +02:00
|
|
|
}
|
2016-01-22 03:25:27 +02:00
|
|
|
|
|
|
|
func checkDNS(domain, fqdn string) bool {
|
|
|
|
// check if the expected DNS entry was created. If not wait for some time and try again.
|
|
|
|
m := new(dns.Msg)
|
|
|
|
m.SetQuestion(domain+".", dns.TypeSOA)
|
|
|
|
c := new(dns.Client)
|
2016-01-30 20:39:10 +02:00
|
|
|
in, _, err := c.Exchange(m, "google-public-dns-a.google.com:53")
|
2016-01-22 03:25:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
var authorativeNS string
|
|
|
|
for _, answ := range in.Answer {
|
|
|
|
soa := answ.(*dns.SOA)
|
|
|
|
authorativeNS = soa.Ns
|
|
|
|
}
|
|
|
|
|
|
|
|
fallbackCnt := 0
|
|
|
|
for fallbackCnt < preCheckDNSFallbackCount {
|
|
|
|
m.SetQuestion(fqdn, dns.TypeTXT)
|
|
|
|
in, _, err = c.Exchange(m, authorativeNS+":53")
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(in.Answer) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fallbackCnt++
|
|
|
|
if fallbackCnt >= preCheckDNSFallbackCount {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * time.Duration(fallbackCnt))
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
2016-01-26 17:37:50 +02:00
|
|
|
|
|
|
|
// toFqdn converts the name into a fqdn appending a trailing dot.
|
|
|
|
func toFqdn(name string) string {
|
|
|
|
n := len(name)
|
|
|
|
if n == 0 || name[n-1] == '.' {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return name + "."
|
|
|
|
}
|
|
|
|
|
|
|
|
// unFqdn converts the fqdn into a name removing the trailing dot.
|
|
|
|
func unFqdn(name string) string {
|
|
|
|
n := len(name)
|
|
|
|
if n != 0 && name[n-1] == '.' {
|
|
|
|
return name[:n-1]
|
|
|
|
}
|
|
|
|
return name
|
|
|
|
}
|
2016-02-03 08:04:12 +02:00
|
|
|
|
|
|
|
// waitFor polls the given function 'f', once per second, up to 'timeout' seconds.
|
|
|
|
func waitFor(timeout int, f func() (bool, error)) error {
|
|
|
|
start := time.Now().Second()
|
|
|
|
for {
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
if delta := time.Now().Second() - start; delta >= timeout {
|
|
|
|
return fmt.Errorf("Time limit exceeded (%d seconds)", delta)
|
|
|
|
}
|
|
|
|
|
|
|
|
stop, err := f()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|