Skip to content

Commit d55f8a8

Browse files
committed
added several external services
1 parent aa9ec32 commit d55f8a8

File tree

6 files changed

+62
-52
lines changed

6 files changed

+62
-52
lines changed

cmd/sane/main.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ var (
4444
hnsdPath = flag.String("hnsd", os.Getenv("HNSD_PATH"), "path to hnsd executable, also may be set as environment variable HNSD_PATH")
4545
hnsdCheckpointPath = flag.String("checkpoint", "", "path to hnsd checkpoint location, default ~/.hnsd")
4646
resyncInterval = flag.Duration("resync-interval", 24*time.Hour, "interval for roots resyncronization")
47-
externalService = flag.String("external-service", "", "uri to an external service providing SANE data")
47+
externalService = flag.String("external-service", "", "uri to an external service providing SANE data, can be a comma-separated list")
4848
)
4949

5050
func getConfPath() string {
@@ -213,6 +213,8 @@ func main() {
213213
return
214214
}
215215

216+
services := strings.Split(*externalService, ",")
217+
216218
if *verbose {
217219
log.SetFlags(log.LstdFlags | log.Lshortfile)
218220
debuglog.Logger.Verbose = true
@@ -224,6 +226,9 @@ func main() {
224226
if *hnsdCheckpointPath == "" {
225227
home, _ := os.UserHomeDir() //above already fails if it doesn't exist
226228
*hnsdCheckpointPath = path.Join(home, ".hnsd")
229+
if err := os.MkdirAll(*hnsdCheckpointPath, 0777); err != nil {
230+
log.Fatalf("error creating directory at %s : %s", *hnsdCheckpointPath, err)
231+
}
227232
}
228233

229234
sync.GetRoots(*hnsdPath, p, *hnsdCheckpointPath)
@@ -278,7 +283,7 @@ func main() {
278283
SkipNameChecks: *skipNameChecks,
279284
Verbose: *verbose,
280285
RootsPath: path.Join(p, "roots.json"),
281-
ExternalService: *externalService,
286+
ExternalService: services,
282287
}
283288
log.Printf("Listening on %s", *addr)
284289
log.Fatal(c.Run(*addr))

prove/external.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import (
66
"fmt"
77
"net/http"
88
"strings"
9+
"time"
10+
11+
"github.com/randomlogin/sane/debuglog"
912
)
1013

1114
type UrkelJson struct {
@@ -16,14 +19,37 @@ type DNSSECJson struct {
1619
Dnssec string `json:"dnssec"`
1720
}
1821

19-
var defaultURL = "https://sdaneproofs.htools.work/proofs/"
22+
var timeout = 1 * time.Second
2023

21-
func fetchDNSSEC(domain, server string) ([]byte, error) {
24+
func fetchDNSSEC(domain string, externalServices []string) ([]byte, error) {
25+
for _, link := range externalServices {
26+
if result, err := fetchOneDNSSEC(domain, link); err == nil {
27+
return result, nil
28+
}
29+
debuglog.Logger.Debugf("couldn't fetch dnssec data for domain %s from %s", domain, link)
30+
}
31+
return nil, fmt.Errorf("could not fetch any external services")
32+
}
33+
34+
func fetchUrkel(domain string, externalServices []string) ([]byte, error) {
35+
for _, link := range externalServices {
36+
if result, err := fetchOneUrkel(domain, link); err == nil {
37+
return result, nil
38+
}
39+
debuglog.Logger.Debugf("couldn't fetch urkel data for domain %s from %s", domain, link)
40+
}
41+
return nil, fmt.Errorf("could not fetch any external services")
42+
}
43+
44+
func fetchOneDNSSEC(domain, server string) ([]byte, error) {
2245
if !strings.HasSuffix(server, "/") {
2346
server += "/"
2447
}
2548
url := server + domain + "?dnssec"
26-
response, err := http.Get(url)
49+
client := http.Client{
50+
Timeout: timeout,
51+
}
52+
response, err := client.Get(url)
2753
if err != nil {
2854
return nil, fmt.Errorf("error making GET request: %s", err)
2955
}
@@ -44,12 +70,16 @@ func fetchDNSSEC(domain, server string) ([]byte, error) {
4470
return val, nil
4571
}
4672

47-
func fetchUrkel(domain, server string) ([]byte, error) {
73+
func fetchOneUrkel(domain, server string) ([]byte, error) {
4874
if !strings.HasSuffix(server, "/") {
4975
server += "/"
5076
}
5177
url := server + domain + "?urkel"
52-
response, err := http.Get(url)
78+
client := http.Client{
79+
Timeout: timeout,
80+
}
81+
response, err := client.Get(url)
82+
// response, err := http.Get(url)
5383
if err != nil {
5484
return nil, fmt.Errorf("Error making GET request: %s", err)
5585
}

prove/lazydane

Lines changed: 0 additions & 26 deletions
This file was deleted.

prove/prove.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,24 @@ func verifyUrkelExt(extensionValue []byte, domain string, roots []sync.BlockInfo
8080
for _, block := range roots {
8181
// found tree root among stored ones
8282
if hexstr == block.TreeRoot {
83-
debuglog.Logger.Debug("found tree root ", hexstr, " from the certificate in the stored roots")
83+
debuglog.Logger.Debug("found tree root", hexstr, "from the certificate in the stored roots")
8484
return nil
8585
}
8686
}
8787
extensionValue = extensionValue[32+*length:]
88-
debuglog.Logger.Debug("could not find tree root ", hexstr, " from the certificate in the stored roots")
88+
debuglog.Logger.Debug("could not find tree root", hexstr, "from the certificate in the stored roots")
8989
}
9090
return fmt.Errorf("could not find tree root in the stored ones")
9191
}
9292

9393
// extracts proof data from the certificate then verifies if the proof is correct
94-
func VerifyCertificateExtensions(roots []sync.BlockInfo, cert x509.Certificate, tlsa *dns.TLSA, externalService string) error {
94+
func VerifyCertificateExtensions(roots []sync.BlockInfo, cert x509.Certificate, tlsa *dns.TLSA, externalServices []string) error {
9595
if len(cert.DNSNames) == 0 {
9696
return fmt.Errorf("certificate has empty dns names")
9797
}
9898

9999
for _, domain := range cert.DNSNames {
100-
err := verifyDomain(domain, cert, roots, tlsa, externalService)
100+
err := verifyDomain(domain, cert, roots, tlsa, externalServices)
101101
if err == nil {
102102
debuglog.Logger.Debug("successfully verified certificate extensions for the domain " + domain)
103103
return nil
@@ -108,7 +108,7 @@ func VerifyCertificateExtensions(roots []sync.BlockInfo, cert x509.Certificate,
108108
}
109109

110110
// verifyDomain is called to check every domain listed in the certificate
111-
func verifyDomain(domain string, cert x509.Certificate, roots []sync.BlockInfo, tlsa *dns.TLSA, externalService string) error {
111+
func verifyDomain(domain string, cert x509.Certificate, roots []sync.BlockInfo, tlsa *dns.TLSA, externalServices []string) error {
112112
var foundUrkel, foundDnssec bool
113113
var urkelExtension, dnssecExtension []byte
114114
var UrkelVerificationError, DNSSECVerificationError error = errors.New("urkel tree proof extension not found"), errors.New("DNSSEC chain extension not found")
@@ -129,23 +129,24 @@ func verifyDomain(domain string, cert x509.Certificate, roots []sync.BlockInfo,
129129
}
130130

131131
if !foundUrkel {
132-
if externalService == "" {
132+
if len(externalServices) == 0 {
133133
return fmt.Errorf("certificate does not have urkel proof extension and external service is disabled")
134134
}
135-
urkelExtension, err = fetchUrkel(domain, externalService)
135+
urkelExtension, err = fetchUrkel(domain, externalServices)
136136
if err != nil {
137-
debuglog.Logger.Debugf("failed to fetch DNSSEC data from %s for the domain %s: %s", externalService, domain, err)
137+
debuglog.Logger.Debugf("failed to fetch DNSSEC data from %s for the domain %s: %s", externalServices, domain, err)
138138
return err
139139
}
140140
}
141141

142142
if !foundDnssec {
143-
if externalService == "" {
143+
if len(externalServices) == 0 {
144+
// if externalServices == []"" {
144145
return fmt.Errorf("certificate does not have dnssec chain extension and external service is disabled")
145146
}
146-
dnssecExtension, err = fetchDNSSEC(domain, externalService)
147+
dnssecExtension, err = fetchDNSSEC(domain, externalServices)
147148
if err != nil {
148-
debuglog.Logger.Debugf("failed to fetch DNSSEC data from %s for the domain %s: %s", externalService, domain, err)
149+
debuglog.Logger.Debugf("failed to fetch DNSSEC data from %s for the domain %s: %s", externalServices, domain, err)
149150
return err
150151
}
151152
}

tls.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func (t *tlsError) Error() string {
2222
}
2323

2424
// newTLSConfig creates a new tls configuration capable of validating DANE.
25-
func newTLSConfig(host string, rrs []*dns.TLSA, nameCheck bool, roots []sync.BlockInfo, externalService string) *tls.Config {
25+
func newTLSConfig(host string, rrs []*dns.TLSA, nameCheck bool, roots []sync.BlockInfo, externalServices []string) *tls.Config {
2626
return &tls.Config{
2727
InsecureSkipVerify: true, // lgtm[go/disabled-certificate-check]
28-
VerifyConnection: verifyConnection(rrs, nameCheck, host, roots, externalService),
28+
VerifyConnection: verifyConnection(rrs, nameCheck, host, roots, externalServices),
2929
ServerName: host,
3030
MinVersion: tls.VersionTLS12,
3131
// Supported TLS 1.2 cipher suites
@@ -45,7 +45,7 @@ func newTLSConfig(host string, rrs []*dns.TLSA, nameCheck bool, roots []sync.Blo
4545
}
4646

4747
// verifyConnection returns a function that verifies the given tls connection state using the host and rrs
48-
func verifyConnection(rrs []*dns.TLSA, nameCheck bool, host string, roots []sync.BlockInfo, externalService string) func(cs tls.ConnectionState) error {
48+
func verifyConnection(rrs []*dns.TLSA, nameCheck bool, host string, roots []sync.BlockInfo, externalServices []string) func(cs tls.ConnectionState) error {
4949
return func(cs tls.ConnectionState) error {
5050
// the host can be ignored per RFC 7671. Not Before, Not After are ignored as well.
5151
// https://tools.ietf.org/html/rfc7671
@@ -63,7 +63,7 @@ func verifyConnection(rrs []*dns.TLSA, nameCheck bool, host string, roots []sync
6363
continue
6464
}
6565
if err := t.Verify(cs.PeerCertificates[0]); err == nil {
66-
if err := prove.VerifyCertificateExtensions(roots, *cert, t, externalService); err != nil {
66+
if err := prove.VerifyCertificateExtensions(roots, *cert, t, externalServices); err != nil {
6767
debuglog.Logger.Debug(err)
6868
return err
6969
}

tunnel.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
var (
24-
Version = "0.0.7"
24+
Version = "0.0.9"
2525
)
2626

2727
const (
@@ -37,7 +37,7 @@ type Config struct {
3737
SkipNameChecks bool
3838
Verbose bool
3939
RootsPath string
40-
ExternalService string
40+
ExternalService []string
4141

4242
// For handling relative urls/non-proxy requests
4343
ContentHandler http.Handler
@@ -47,7 +47,7 @@ type tunneler struct {
4747
mitm *mitmConfig
4848
dialer *dialer
4949
RootsPath string
50-
ExternalService string
50+
ExternalService []string
5151
nameChecks bool
5252
constraints map[string]struct{}
5353
logger
@@ -230,7 +230,7 @@ func httpError(w http.ResponseWriter, error string, code int) {
230230
w.Header().Set("Content-Type", "text/html; charset=utf-8")
231231
w.Header().Set("X-Content-Type-Options", "nosniff")
232232
w.WriteHeader(code)
233-
fmt.Fprintf(w, "<h1>%d %s</h1><p>%s</p><hr>letsdane/v%s",
233+
fmt.Fprintf(w, "<h1>%d %s</h1><p>%s</p><hr>sane/v%s",
234234
code, http.StatusText(code), html.EscapeString(error), Version)
235235
}
236236

0 commit comments

Comments
 (0)