From 927dfd29c598f2ec79fec711877bc582ffd18749 Mon Sep 17 00:00:00 2001 From: adnano Date: Fri, 25 Sep 2020 23:06:54 -0400 Subject: Refactor TOFU --- gemini.go | 52 ---------------------------------------------------- 1 file changed, 52 deletions(-) (limited to 'gemini.go') diff --git a/gemini.go b/gemini.go index 5b95b6a..ccdb5df 100644 --- a/gemini.go +++ b/gemini.go @@ -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) -} -- cgit v1.2.3