]> Sergey Matveev's repositories - tofuproxy.git/blob - tls/dane.go
Unify copyright comment format
[tofuproxy.git] / tls / dane.go
1 // tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
2 //              manager, WARC/geminispace browser
3 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, version 3 of the License.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package tofuproxy
18
19 import (
20         "crypto/sha256"
21         "crypto/sha512"
22         "crypto/x509"
23         "encoding/hex"
24         "fmt"
25         "log"
26
27         "github.com/miekg/dns"
28 )
29
30 var DNSSrv string
31
32 func DANE(addr string, cert *x509.Certificate) (bool, bool) {
33         if DNSSrv == "" {
34                 return false, false
35         }
36         host, port, err := SplitHostPort(addr)
37         if err != nil {
38                 log.Printf("can not split host+port: %s: %+v\n", addr, err)
39                 return false, false
40         }
41         if port == "" {
42                 port = "443"
43         }
44         m := new(dns.Msg)
45         m.SetQuestion(dns.Fqdn(fmt.Sprintf("_%s._tcp.%s", port, host)), dns.TypeTLSA)
46         msg, err := dns.Exchange(m, DNSSrv)
47         if err != nil {
48                 log.Printf("DNS: %+v\n", err)
49                 return false, false
50         }
51         if msg.MsgHdr.Rcode != dns.RcodeSuccess {
52                 return false, false
53         }
54         exists := false
55         for _, answer := range msg.Answer {
56                 tlsa, ok := answer.(*dns.TLSA)
57                 if !ok {
58                         continue
59                 }
60                 if tlsa.Usage != 3 {
61                         // Non EE
62                         continue
63                 }
64                 exists = true
65                 var toMatch []byte
66                 switch tlsa.Selector {
67                 case 0:
68                         toMatch = cert.Raw
69                 case 1:
70                         toMatch = cert.RawSubjectPublicKeyInfo
71                 }
72                 var hsh []byte
73                 switch tlsa.MatchingType {
74                 case 0:
75                         hsh = toMatch
76                 case 1:
77                         our := sha256.Sum256(toMatch)
78                         hsh = our[:]
79                 case 2:
80                         our := sha512.Sum512(toMatch)
81                         hsh = our[:]
82                 }
83                 if tlsa.Certificate == hex.EncodeToString(hsh) {
84                         return true, true
85                 }
86         }
87         return exists, false
88 }