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/>.
33 "go.cypherpunks.ru/ucspi"
34 "go.stargrave.org/tofuproxy/caches"
35 "go.stargrave.org/tofuproxy/fifos"
40 type ClientCertificateGetter struct {
45 func (g *ClientCertificateGetter) get(
46 cri *tls.CertificateRequestInfo,
47 ) (*tls.Certificate, error) {
48 caches.TLSAuthCacheM.RLock()
49 tlsCert := caches.TLSAuthCache[g.host]
50 caches.TLSAuthCacheM.RUnlock()
54 sigSchemes := make([]string, 0, len(cri.SignatureSchemes))
55 for _, ss := range cri.SignatureSchemes {
56 sigSchemes = append(sigSchemes, ss.String())
59 b.WriteString(fmt.Sprintf(`
61 wm title . "TLS client authentication: %s"
73 button .submit -text "Use" -command submit
76 label .lTLSVersion -text "TLS version: %s"
80 foreach sigScheme {%s} {
81 label .lSignatureScheme$sigSchemeRow -text "Signature scheme: $sigScheme"
82 grid .lSignatureScheme$sigSchemeRow
88 ucspi.TLSVersion(cri.Version),
89 strings.Join(sigSchemes, " "),
92 ents, err := os.ReadDir(CCerts)
96 certs := make([]*x509.Certificate, 0, len(ents))
97 tlsCerts := make([]*tls.Certificate, 0, len(ents))
98 for i, ent := range ents {
99 p := filepath.Join(CCerts, ent.Name())
100 _, cert, err := ucspi.CertificateFromFile(p)
104 prv, err := ucspi.PrivateKeyFromFile(p)
108 certs = append(certs, cert)
109 tlsCerts = append(tlsCerts, &tls.Certificate{
110 Certificate: [][]byte{cert.Raw},
113 b.WriteString(fmt.Sprintf(".lb insert end \"%d: %s\"\n", i, cert.Subject))
115 // ioutil.WriteFile("/tmp/tls-auth-dialog.tcl", b.Bytes(), 0666)
116 cmd := exec.Command(CmdWish)
118 out, err := cmd.Output()
122 lines := strings.Split(string(out), "\n")
124 return nil, errors.New("invalid output from authentication form")
126 t := strings.Split(lines[0], ":")[0]
127 i, err := strconv.Atoi(t)
129 return &tls.Certificate{}, nil
131 fifos.LogTLSAuth <- fmt.Sprintf("%s\t%s", g.host, certs[i].Subject)
132 caches.TLSAuthCacheM.Lock()
133 caches.TLSAuthCache[g.host] = tlsCerts[i]
134 caches.TLSAuthCacheM.Unlock()
136 return tlsCerts[i], nil