go-gemini/tofu.go

141 lines
3.4 KiB
Go
Raw Normal View History

2020-10-24 19:15:32 +00:00
package gemini
2020-09-25 22:53:20 +00:00
import (
"bufio"
"crypto/sha512"
"crypto/x509"
"fmt"
"io"
2020-09-26 03:06:54 +00:00
"os"
2020-11-06 03:30:13 +00:00
"strconv"
2020-09-25 22:53:20 +00:00
"strings"
)
// Trust represents the trustworthiness of a certificate.
type Trust int
const (
TrustNone Trust = iota // The certificate is not trusted.
TrustOnce // The certificate is trusted once.
TrustAlways // The certificate is trusted always.
)
// KnownHosts represents a list of known hosts.
// The zero value for KnownHosts is an empty list ready to use.
2020-09-26 03:06:54 +00:00
type KnownHosts struct {
2020-11-05 20:27:12 +00:00
hosts map[string]Fingerprint
2020-09-26 03:06:54 +00:00
file *os.File
}
// Load loads the known hosts from the provided path.
// New known hosts will be appended to the file.
func (k *KnownHosts) Load(path string) error {
2020-09-26 03:06:54 +00:00
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644)
if err != nil {
return err
2020-09-26 03:06:54 +00:00
}
k.Parse(f)
f.Close()
// Open the file for append-only use
f, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644)
if err != nil {
return err
2020-09-26 03:06:54 +00:00
}
k.file = f
return nil
2020-09-26 03:06:54 +00:00
}
// Add adds a certificate to the list of known hosts.
2020-09-26 03:06:54 +00:00
// If KnownHosts was loaded from a file, Add will append to the file.
func (k *KnownHosts) Add(hostname string, cert *x509.Certificate) {
k.add(hostname, cert, true)
2020-09-26 03:06:54 +00:00
}
func (k *KnownHosts) add(hostname string, cert *x509.Certificate, write bool) {
if k.hosts == nil {
2020-11-05 20:27:12 +00:00
k.hosts = map[string]Fingerprint{}
}
2020-11-05 20:27:12 +00:00
fingerprint := NewFingerprint(cert)
k.hosts[hostname] = fingerprint
// Append to the file
if write && k.file != nil {
2020-11-05 20:27:12 +00:00
appendKnownHost(k.file, hostname, fingerprint)
}
2020-09-27 21:41:41 +00:00
}
2020-11-05 20:27:12 +00:00
// Lookup returns the fingerprint of the certificate corresponding to
// the given hostname.
func (k *KnownHosts) Lookup(hostname string) (Fingerprint, bool) {
c, ok := k.hosts[hostname]
return c, ok
}
2020-09-26 03:06:54 +00:00
// Parse parses the provided reader and adds the parsed known hosts to the list.
2020-09-26 00:55:37 +00:00
// Invalid lines are ignored.
2020-09-26 03:06:54 +00:00
func (k *KnownHosts) Parse(r io.Reader) {
if k.hosts == nil {
2020-11-05 20:27:12 +00:00
k.hosts = map[string]Fingerprint{}
}
2020-09-25 22:53:20 +00:00
scanner := bufio.NewScanner(r)
for scanner.Scan() {
text := scanner.Text()
parts := strings.Split(text, " ")
2020-11-06 03:30:13 +00:00
if len(parts) < 4 {
2020-09-26 00:55:37 +00:00
continue
2020-09-25 22:53:20 +00:00
}
hostname := parts[0]
algorithm := parts[1]
if algorithm != "SHA-512" {
continue
}
2020-09-25 22:53:20 +00:00
fingerprint := parts[2]
2020-11-06 03:30:13 +00:00
expires, err := strconv.ParseInt(parts[3], 10, 0)
if err != nil {
continue
}
2020-11-05 20:27:12 +00:00
k.hosts[hostname] = Fingerprint{
Algorithm: algorithm,
Hex: fingerprint,
2020-11-06 03:30:13 +00:00
Expires: expires,
}
2020-09-25 22:53:20 +00:00
}
2020-09-26 00:55:37 +00:00
}
2020-09-25 22:53:20 +00:00
// Write writes the known hosts to the provided io.Writer.
func (k *KnownHosts) Write(w io.Writer) {
for h, c := range k.hosts {
appendKnownHost(w, h, c)
}
}
2020-11-05 20:27:12 +00:00
func appendKnownHost(w io.Writer, hostname string, f Fingerprint) (int, error) {
2020-11-06 03:30:13 +00:00
return fmt.Fprintf(w, "%s %s %s %d\n", hostname, f.Algorithm, f.Hex, f.Expires)
2020-09-25 22:53:20 +00:00
}
2020-11-05 20:27:12 +00:00
// Fingerprint represents a fingerprint using a certain algorithm.
type Fingerprint struct {
Algorithm string // fingerprint algorithm e.g. SHA-512
Hex string // fingerprint in hexadecimal, with ':' between each octet
2020-11-06 03:30:13 +00:00
Expires int64 // unix time of the fingerprint expiration date
}
2020-11-05 20:27:12 +00:00
// NewFingerprint returns the SHA-512 fingerprint of the provided certificate.
func NewFingerprint(cert *x509.Certificate) Fingerprint {
2020-09-25 22:53:20 +00:00
sum512 := sha512.Sum512(cert.Raw)
2020-10-28 19:13:31 +00:00
var b strings.Builder
2020-09-25 22:53:20 +00:00
for i, f := range sum512 {
if i > 0 {
2020-10-28 19:13:31 +00:00
b.WriteByte(':')
2020-09-25 22:53:20 +00:00
}
2020-10-28 19:13:31 +00:00
fmt.Fprintf(&b, "%02X", f)
2020-09-25 22:53:20 +00:00
}
2020-11-05 20:27:12 +00:00
return Fingerprint{
Algorithm: "SHA-512",
Hex: b.String(),
2020-11-06 03:30:13 +00:00
Expires: cert.NotAfter.Unix(),
2020-11-05 20:27:12 +00:00
}
2020-09-25 22:53:20 +00:00
}