diff options
| author | adnano <[email protected]> | 2020-09-25 23:06:54 -0400 |
|---|---|---|
| committer | adnano <[email protected]> | 2020-09-25 23:18:14 -0400 |
| commit | 927dfd29c598f2ec79fec711877bc582ffd18749 (patch) | |
| tree | a6cd29fa36b890c067fc6e84562ff5ac05056c11 /gemini.go | |
| parent | Implement basic TOFU (diff) | |
| download | go-gemini-927dfd29c598f2ec79fec711877bc582ffd18749.tar.xz go-gemini-927dfd29c598f2ec79fec711877bc582ffd18749.zip | |
Refactor TOFU
Diffstat (limited to 'gemini.go')
| -rw-r--r-- | gemini.go | 52 |
1 files changed, 0 insertions, 52 deletions
@@ -1,13 +1,5 @@ package gemini -import ( - "crypto/x509" - "errors" - "log" - "os" - "path/filepath" -) - // Status codes. const ( StatusInput = 10 @@ -43,47 +35,3 @@ const ( var ( crlf = []byte("\r\n") ) - -// TOFUClient is a client that implements Trust-On-First-Use. -type TOFUClient struct { - // Trusts, if not nil, will be called to determine whether the client should - // trust the provided certificate. - Trusts func(cert *x509.Certificate, req *Request) bool -} - -func (t *TOFUClient) VerifyCertificate(cert *x509.Certificate, req *Request) error { - if knownHosts.Has(req.URL.Host, cert) { - return nil - } - if t.Trusts != nil && t.Trusts(cert, req) { - host := NewKnownHost(cert) - knownHosts = append(knownHosts, host) - knownHostsFile, err := os.OpenFile(knownHostsPath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) - if err != nil { - log.Print(err) - } - if _, err := host.Write(knownHostsFile); err != nil { - log.Print(err) - } - return nil - } - return errors.New("gemini: certificate not trusted") -} - -var ( - knownHosts KnownHosts - knownHostsPath string - knownHostsFile *os.File -) - -func init() { - configDir, err := os.UserConfigDir() - knownHostsPath = filepath.Join(configDir, "gemini") - os.MkdirAll(knownHostsPath, 0755) - knownHostsPath = filepath.Join(knownHostsPath, "known_hosts") - knownHostsFile, err = os.OpenFile(knownHostsPath, os.O_CREATE|os.O_RDONLY, 0644) - if err != nil { - return - } - knownHosts = ParseKnownHosts(knownHostsFile) -} |