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