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
48 func spkiHash(cert *x509.Certificate) string {
49 hsh := sha256.Sum256(cert.RawSubjectPublicKeyInfo)
50 return hex.EncodeToString(hsh[:])
53 func acceptedAdd(addr, h string) {
59 func rejectedAdd(addr, h string) {
65 type ErrRejected struct {
69 func (err ErrRejected) Error() string { return err.addr + " was rejected" }
71 func certInfo(certRaw []byte) string {
72 cmd := exec.Command(CmdCerttool, "--certificate-info", "--inder")
73 cmd.Stdin = bytes.NewReader(certRaw)
74 out, err := cmd.Output()
78 lines := make([]string, 0, 128)
79 for i, line := range strings.Split(string(out), "\n") {
80 if strings.Contains(line, "ASCII:") {
83 lines = append(lines, fmt.Sprintf(
84 "%03d %s", i, strings.ReplaceAll(line, `"`, `\"`),
87 return strings.Join(lines, "\n")
94 verifiedChains [][]*x509.Certificate,
96 var certTheir *x509.Certificate
98 if len(verifiedChains) > 0 {
99 certTheir = verifiedChains[0][0]
101 certTheir, err = x509.ParseCertificate(rawCerts[0])
106 certTheirHash := spkiHash(certTheir)
108 defer VerifyM.Unlock()
110 certOurHash := accepted[host]
112 if certTheirHash == certOurHash {
116 certOurHash = rejected[host]
118 if certTheirHash == certOurHash {
119 return ErrRejected{host}
121 daneExists, daneMatched := dane(host, certTheir)
124 sinkCert <- fmt.Sprintf("DANE\t%s\tmatched", host)
126 sinkErr <- fmt.Sprintf("DANE\t%s\tnot matched", host)
129 fn := filepath.Join(*certs, host)
130 certsOur, _, err := ucspi.CertPoolFromFile(fn)
131 if err == nil || dialErr != nil || (daneExists && !daneMatched) {
132 if certsOur != nil && certTheirHash == spkiHash(certsOur[0]) {
133 acceptedAdd(host, certTheirHash)
134 if bytes.Compare(certsOur[0].Raw, rawCerts[0]) != 0 {
135 sinkCert <- fmt.Sprintf("Refresh\t%s\t%s", host, certTheirHash)
141 b.WriteString(fmt.Sprintf("wm title . \"%s\"\n", host))
144 b.WriteString(fmt.Sprintf(`set tErr [text .tErr]
145 $tErr insert end "%s"
146 $tErr configure -wrap word -height 5
148 b.WriteString("grid .tErr -columnspan 3\n")
153 b.WriteString("label .lDANE -bg green -text \"DANE matched\"\n")
155 b.WriteString("label .lDANE -bg red -text \"DANE not matched!\"\n")
157 b.WriteString("grid .lDANE\n")
160 var bCerts bytes.Buffer
161 for i, rawCert := range rawCerts {
162 bCerts.WriteString(fmt.Sprintf("Their %d:\n", i))
163 bCerts.WriteString(certInfo(rawCert))
165 b.WriteString(fmt.Sprintf(`set tTheir [text .tTheir]
166 $tTheir insert end "%s"
167 set sbTheir [scrollbar .sbTheir -command [list $tTheir yview]]
168 $tTheir configure -wrap word -yscrollcommand [list $sbTheir set]
170 b.WriteString("grid $tTheir $sbTheir -sticky nsew -columnspan 3\n")
174 for i, cert := range certsOur {
175 bCerts.WriteString(fmt.Sprintf("Our %d:\n", i))
176 bCerts.WriteString(certInfo(cert.Raw))
178 b.WriteString(fmt.Sprintf(`set tOur [text .tOur]
179 $tOur insert end "%s"
180 set sbOur [scrollbar .sbOur -command [list $tOur yview]]
181 $tOur configure -wrap word -yscrollcommand [list $sbOur set]
183 b.WriteString("grid $tOur $sbOur -sticky nsew -columnspan 3\n")
187 proc doAccept {} { exit 10 }
188 proc doOnce {} { exit 11 }
189 proc doReject {} { exit 12 }
190 button .bAccept -text "Accept" -bg green -command doAccept
191 button .bOnce -text "Once" -command doOnce
192 button .bReject -text "Reject" -bg red -command doReject
193 grid .bAccept .bOnce .bReject
194 grid rowconfigure . 0 -weight 1
195 grid columnconfigure . 0 -weight 1
198 cmd := exec.Command(CmdWish)
199 // ioutil.WriteFile("/tmp/w.tcl", b.Bytes(), 0666)
202 exitError, ok := err.(*exec.ExitError)
204 sinkCert <- fmt.Sprintf("DENY\t%s\t%s", host, certTheirHash)
205 return ErrRejected{host}
207 switch exitError.ExitCode() {
209 sinkCert <- fmt.Sprintf("ADD\t%s\t%s", host, certTheirHash)
212 sinkCert <- fmt.Sprintf("ONCE\t%s\t%s", host, certTheirHash)
213 acceptedAdd(host, certTheirHash)
216 rejectedAdd(host, certTheirHash)
219 sinkCert <- fmt.Sprintf("DENY\t%s\t%s", host, certTheirHash)
220 return ErrRejected{host}
223 if !os.IsNotExist(err) {
226 sinkCert <- fmt.Sprintf("TOFU\t%s\t%s", host, certTheirHash)
229 tmp, err := os.CreateTemp(*certs, "")
233 for _, rawCert := range rawCerts {
234 err = pem.Encode(tmp, &pem.Block{Type: "CERTIFICATE", Bytes: rawCert})
240 os.Rename(tmp.Name(), fn)
241 acceptedAdd(host, certTheirHash)