2 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
33 "go.cypherpunks.ru/ucspi"
37 CmdCerttool = "certtool"
41 accepted = make(map[string]string)
42 acceptedM sync.RWMutex
43 rejected = make(map[string]string)
44 rejectedM sync.RWMutex
47 func spkiHash(cert *x509.Certificate) string {
48 hsh := sha256.Sum256(cert.RawSubjectPublicKeyInfo)
49 return hex.EncodeToString(hsh[:])
52 func acceptedAdd(addr, h string) {
58 func rejectedAdd(addr, h string) {
64 type ErrRejected struct {
68 func (err ErrRejected) Error() string { return err.addr + " was rejected" }
70 func certInfo(certRaw []byte) string {
71 cmd := exec.Command(CmdCerttool, "--certificate-info", "--inder")
72 cmd.Stdin = bytes.NewReader(certRaw)
73 out, err := cmd.Output()
77 lines := make([]string, 0, 128)
78 for i, line := range strings.Split(string(out), "\n") {
79 if strings.Contains(line, "ASCII:") {
82 lines = append(lines, fmt.Sprintf(
83 "%03d %s", i, strings.ReplaceAll(line, `"`, `\"`),
86 return strings.Join(lines, "\n")
93 verifiedChains [][]*x509.Certificate,
95 var certTheir *x509.Certificate
97 if len(verifiedChains) > 0 {
98 certTheir = verifiedChains[0][0]
100 certTheir, err = x509.ParseCertificate(rawCerts[0])
105 certTheirHash := spkiHash(certTheir)
107 certOurHash := accepted[host]
109 if certTheirHash == certOurHash {
113 certOurHash = rejected[host]
115 if certTheirHash == certOurHash {
116 return ErrRejected{host}
118 daneExists, daneMatched := dane(host, certTheir)
121 sinkCert <- fmt.Sprintf("DANE\t%s\tmatched", host)
123 sinkErr <- fmt.Sprintf("DANE\t%s\tnot matched", host)
126 fn := filepath.Join(*certs, host)
127 certsOur, _, err := ucspi.CertPoolFromFile(fn)
128 if err == nil || dialErr != nil || (daneExists && !daneMatched) {
129 if certsOur != nil && certTheirHash == spkiHash(certsOur[0]) {
130 acceptedAdd(host, certTheirHash)
131 if bytes.Compare(certsOur[0].Raw, rawCerts[0]) != 0 {
132 sinkCert <- fmt.Sprintf("Refresh\t%s\t%s", host, certTheirHash)
138 b.WriteString(fmt.Sprintf("wm title . \"%s\"\n", host))
141 b.WriteString(fmt.Sprintf(`set tErr [text .tErr]
142 $tErr insert end "%s"
143 $tErr configure -wrap word -height 5
145 b.WriteString("grid .tErr -columnspan 3\n")
150 b.WriteString("label .lDANE -bg green -text \"DANE matched\"\n")
152 b.WriteString("label .lDANE -bg red -text \"DANE not matched!\"\n")
154 b.WriteString("grid .lDANE\n")
157 var bCerts bytes.Buffer
158 for i, rawCert := range rawCerts {
159 bCerts.WriteString(fmt.Sprintf("Their %d:\n", i))
160 bCerts.WriteString(certInfo(rawCert))
162 b.WriteString(fmt.Sprintf(`set tTheir [text .tTheir]
163 $tTheir insert end "%s"
164 set sbTheir [scrollbar .sbTheir -command [list $tTheir yview]]
165 $tTheir configure -wrap word -yscrollcommand [list $sbTheir set]
167 b.WriteString("grid $tTheir $sbTheir -sticky nsew -columnspan 3\n")
171 for i, cert := range certsOur {
172 bCerts.WriteString(fmt.Sprintf("Our %d:\n", i))
173 bCerts.WriteString(certInfo(cert.Raw))
175 b.WriteString(fmt.Sprintf(`set tOur [text .tOur]
176 $tOur insert end "%s"
177 set sbOur [scrollbar .sbOur -command [list $tOur yview]]
178 $tOur configure -wrap word -yscrollcommand [list $sbOur set]
180 b.WriteString("grid $tOur $sbOur -sticky nsew -columnspan 3\n")
184 proc doAccept {} { exit 10 }
185 proc doOnce {} { exit 11 }
186 proc doReject {} { exit 12 }
187 button .bAccept -text "Accept" -bg green -command doAccept
188 button .bOnce -text "Once" -command doOnce
189 button .bReject -text "Reject" -bg red -command doReject
190 grid .bAccept .bOnce .bReject
191 grid rowconfigure . 0 -weight 1
192 grid columnconfigure . 0 -weight 1
195 cmd := exec.Command(CmdWish)
196 // ioutil.WriteFile("/tmp/w.tcl", b.Bytes(), 0666)
199 exitError, ok := err.(*exec.ExitError)
201 sinkCert <- fmt.Sprintf("DENY\t%s\t%s", host, certTheirHash)
202 return ErrRejected{host}
204 switch exitError.ExitCode() {
206 sinkCert <- fmt.Sprintf("ADD\t%s\t%s", host, certTheirHash)
209 sinkCert <- fmt.Sprintf("ONCE\t%s\t%s", host, certTheirHash)
210 acceptedAdd(host, certTheirHash)
213 rejectedAdd(host, certTheirHash)
216 sinkCert <- fmt.Sprintf("DENY\t%s\t%s", host, certTheirHash)
217 return ErrRejected{host}
220 if !os.IsNotExist(err) {
223 sinkCert <- fmt.Sprintf("TOFU\t%s\t%s", host, certTheirHash)
226 tmp, err := os.CreateTemp(*certs, "")
230 for _, rawCert := range rawCerts {
231 err = pem.Encode(tmp, &pem.Block{Type: "CERTIFICATE", Bytes: rawCert})
237 os.Rename(tmp.Name(), fn)
238 acceptedAdd(host, certTheirHash)