2 tofuproxy -- HTTP proxy with TLS certificates management
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
34 "go.cypherpunks.ru/ucspi"
35 "go.stargrave.org/tofuproxy/fifos"
39 CmdCerttool = "certtool"
43 accepted = make(map[string]string)
44 acceptedM sync.RWMutex
45 rejected = make(map[string]string)
46 rejectedM sync.RWMutex
50 func spkiHash(cert *x509.Certificate) string {
51 hsh := sha256.Sum256(cert.RawSubjectPublicKeyInfo)
52 return hex.EncodeToString(hsh[:])
55 func acceptedAdd(addr, h string) {
61 func rejectedAdd(addr, h string) {
67 type ErrRejected struct {
71 func (err ErrRejected) Error() string { return err.addr + " was rejected" }
73 func certInfo(certRaw []byte) string {
74 cmd := exec.Command(CmdCerttool, "--certificate-info", "--inder")
75 cmd.Stdin = bytes.NewReader(certRaw)
76 out, err := cmd.Output()
80 lines := make([]string, 0, 128)
81 for i, line := range strings.Split(string(out), "\n") {
82 if strings.Contains(line, "ASCII:") {
85 lines = append(lines, fmt.Sprintf(
86 "%03d %s", i, strings.ReplaceAll(line, `"`, `\"`),
89 return strings.Join(lines, "\n")
96 verifiedChains [][]*x509.Certificate,
98 var certTheir *x509.Certificate
100 if len(verifiedChains) > 0 {
101 certTheir = verifiedChains[0][0]
103 certTheir, err = x509.ParseCertificate(rawCerts[0])
108 certTheirHash := spkiHash(certTheir)
110 defer VerifyM.Unlock()
112 certOurHash := accepted[host]
114 if certTheirHash == certOurHash {
118 certOurHash = rejected[host]
120 if certTheirHash == certOurHash {
121 return ErrRejected{host}
123 daneExists, daneMatched := dane(host, certTheir)
126 fifos.SinkCert <- fmt.Sprintf("DANE\t%s\tmatched", host)
128 fifos.SinkErr <- fmt.Sprintf("DANE\t%s\tnot matched", host)
131 fn := filepath.Join(Certs, host)
132 certsOur, _, err := ucspi.CertPoolFromFile(fn)
133 if err == nil || dialErr != nil || (daneExists && !daneMatched) {
134 if certsOur != nil && certTheirHash == spkiHash(certsOur[0]) {
135 acceptedAdd(host, certTheirHash)
136 if bytes.Compare(certsOur[0].Raw, rawCerts[0]) != 0 {
137 fifos.SinkCert <- fmt.Sprintf("Refresh\t%s\t%s", host, certTheirHash)
143 b.WriteString(fmt.Sprintf("wm title . \"%s\"\n", host))
146 b.WriteString(fmt.Sprintf(`set tErr [text .tErr]
147 $tErr insert end "%s"
148 $tErr configure -wrap word -height 5
150 b.WriteString("grid .tErr -columnspan 3\n")
155 b.WriteString("label .lDANE -bg green -text \"DANE matched\"\n")
157 b.WriteString("label .lDANE -bg red -text \"DANE not matched!\"\n")
159 b.WriteString("grid .lDANE\n")
162 var bCerts bytes.Buffer
163 for i, rawCert := range rawCerts {
164 bCerts.WriteString(fmt.Sprintf("Their %d:\n", i))
165 bCerts.WriteString(certInfo(rawCert))
167 b.WriteString(fmt.Sprintf(`set tTheir [text .tTheir]
168 $tTheir insert end "%s"
169 set sbTheir [scrollbar .sbTheir -command [list $tTheir yview]]
170 $tTheir configure -wrap word -yscrollcommand [list $sbTheir set]
172 b.WriteString("grid $tTheir $sbTheir -sticky nsew -columnspan 3\n")
176 for i, cert := range certsOur {
177 bCerts.WriteString(fmt.Sprintf("Our %d:\n", i))
178 bCerts.WriteString(certInfo(cert.Raw))
180 b.WriteString(fmt.Sprintf(`set tOur [text .tOur]
181 $tOur insert end "%s"
182 set sbOur [scrollbar .sbOur -command [list $tOur yview]]
183 $tOur configure -wrap word -yscrollcommand [list $sbOur set]
185 b.WriteString("grid $tOur $sbOur -sticky nsew -columnspan 3\n")
189 proc doAccept {} { exit 10 }
190 proc doOnce {} { exit 11 }
191 proc doReject {} { exit 12 }
192 button .bAccept -text "Accept" -bg green -command doAccept
193 button .bOnce -text "Once" -command doOnce
194 button .bReject -text "Reject" -bg red -command doReject
195 grid .bAccept .bOnce .bReject
196 grid rowconfigure . 0 -weight 1
197 grid columnconfigure . 0 -weight 1
200 cmd := exec.Command(CmdWish)
201 // ioutil.WriteFile("/tmp/w.tcl", b.Bytes(), 0666)
204 exitError, ok := err.(*exec.ExitError)
206 fifos.SinkCert <- fmt.Sprintf("DENY\t%s\t%s", host, certTheirHash)
207 return ErrRejected{host}
209 switch exitError.ExitCode() {
211 fifos.SinkCert <- fmt.Sprintf("ADD\t%s\t%s", host, certTheirHash)
214 fifos.SinkCert <- fmt.Sprintf("ONCE\t%s\t%s", host, certTheirHash)
215 acceptedAdd(host, certTheirHash)
218 rejectedAdd(host, certTheirHash)
221 fifos.SinkCert <- fmt.Sprintf("DENY\t%s\t%s", host, certTheirHash)
222 return ErrRejected{host}
225 if !os.IsNotExist(err) {
228 fifos.SinkCert <- fmt.Sprintf("TOFU\t%s\t%s", host, certTheirHash)
231 tmp, err := os.CreateTemp(Certs, "")
235 for _, rawCert := range rawCerts {
236 err = pem.Encode(tmp, &pem.Block{Type: "CERTIFICATE", Bytes: rawCert})
242 os.Rename(tmp.Name(), fn)
243 acceptedAdd(host, certTheirHash)