]> Sergey Matveev's repositories - tofuproxy.git/blob - tlsauth.go
0965335a3ffb3ad3a3a07ee41f76d7fcbe826836
[tofuproxy.git] / tlsauth.go
1 /*
2 tofuproxy -- HTTP proxy with TLS certificates management
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 package tofuproxy
19
20 import (
21         "bytes"
22         "crypto/tls"
23         "crypto/x509"
24         "errors"
25         "fmt"
26         "log"
27         "os"
28         "os/exec"
29         "path/filepath"
30         "strconv"
31         "strings"
32
33         "go.cypherpunks.ru/ucspi"
34         "go.stargrave.org/tofuproxy/caches"
35         "go.stargrave.org/tofuproxy/fifos"
36 )
37
38 var CCerts string
39
40 type ClientCertificateGetter struct {
41         host string
42         auth bool
43 }
44
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()
51         if tlsCert != nil {
52                 return tlsCert, nil
53         }
54         var b bytes.Buffer
55         b.WriteString(fmt.Sprintf(`
56 tk_setPalette grey
57 wm title . "TLS client authentication: %s"
58
59 label .lVersion -text "Version: %s"
60 grib .lVersion
61
62 set lb [listbox .lb]
63 .lb insert end ""
64 `,
65                 g.host,
66                 ucspi.TLSVersion(cri.Version),
67         ))
68
69         ents, err := os.ReadDir(CCerts)
70         if err != nil {
71                 log.Fatalln(err)
72         }
73         certs := make([]*x509.Certificate, 0, len(ents))
74         tlsCerts := make([]*tls.Certificate, 0, len(ents))
75         for i, ent := range ents {
76                 p := filepath.Join(CCerts, ent.Name())
77                 _, cert, err := ucspi.CertificateFromFile(p)
78                 if err != nil {
79                         log.Fatalln(err)
80                 }
81                 prv, err := ucspi.PrivateKeyFromFile(p)
82                 if err != nil {
83                         log.Fatalln(err)
84                 }
85                 certs = append(certs, cert)
86                 tlsCerts = append(tlsCerts, &tls.Certificate{
87                         Certificate: [][]byte{cert.Raw},
88                         PrivateKey:  prv,
89                 })
90                 b.WriteString(fmt.Sprintf(".lb insert end \"%d: %s\"\n", i, cert.Subject))
91         }
92         b.WriteString(`
93 grid .lb
94
95 proc submit {} {
96     global lb
97     puts [$lb get active]
98     exit
99 }
100
101 button .submit -text "Use" -command submit
102 grid .submit
103 `)
104         cmd := exec.Command(CmdWish)
105         cmd.Stdin = &b
106         out, err := cmd.Output()
107         if err != nil {
108                 return nil, err
109         }
110         lines := strings.Split(string(out), "\n")
111         if len(lines) < 1 {
112                 return nil, errors.New("invalid output from authentication form")
113         }
114         t := strings.Split(lines[0], ":")[0]
115         i, err := strconv.Atoi(t)
116         if err != nil {
117                 return &tls.Certificate{}, nil
118         }
119         fifos.LogTLSAuth <- fmt.Sprintf("%s\t%s", g.host, certs[i].Subject)
120         caches.TLSAuthCacheM.Lock()
121         caches.TLSAuthCache[g.host] = tlsCerts[i]
122         caches.TLSAuthCacheM.Unlock()
123         g.auth = true
124         return tlsCerts[i], nil
125 }