2020-10-24 13:15:32 -06:00
|
|
|
package gemini
|
2020-09-25 16:53:20 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"crypto/sha512"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2020-09-25 21:06:54 -06:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2020-09-25 16:53:20 -06:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2020-09-25 18:22:48 -06:00
|
|
|
"time"
|
2020-09-25 16:53:20 -06:00
|
|
|
)
|
|
|
|
|
2020-10-31 20:34:51 -06:00
|
|
|
// 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.
|
|
|
|
)
|
|
|
|
|
2020-09-25 18:22:48 -06:00
|
|
|
// KnownHosts represents a list of known hosts.
|
2020-09-27 12:18:30 -06:00
|
|
|
// The zero value for KnownHosts is an empty list ready to use.
|
2020-09-25 21:06:54 -06:00
|
|
|
type KnownHosts struct {
|
2020-10-13 17:54:48 -06:00
|
|
|
hosts map[string]certInfo
|
2020-09-25 21:06:54 -06:00
|
|
|
file *os.File
|
|
|
|
}
|
|
|
|
|
2020-10-12 14:56:26 -06:00
|
|
|
// LoadDefault loads the known hosts from the default known hosts path, which is
|
2020-09-27 20:19:24 -06:00
|
|
|
// $XDG_DATA_HOME/gemini/known_hosts.
|
2020-09-26 11:35:56 -06:00
|
|
|
// It creates the path and any of its parent directories if they do not exist.
|
2020-09-27 12:18:30 -06:00
|
|
|
// KnownHosts will append to the file whenever a certificate is added.
|
2020-10-12 14:56:26 -06:00
|
|
|
func (k *KnownHosts) LoadDefault() error {
|
2020-09-26 11:35:56 -06:00
|
|
|
path, err := defaultKnownHostsPath()
|
|
|
|
if err != nil {
|
2020-09-27 12:18:30 -06:00
|
|
|
return err
|
2020-09-26 11:35:56 -06:00
|
|
|
}
|
2020-10-12 14:56:26 -06:00
|
|
|
return k.Load(path)
|
2020-09-26 11:35:56 -06:00
|
|
|
}
|
|
|
|
|
2020-10-12 14:56:26 -06:00
|
|
|
// Load loads the known hosts from the provided path.
|
2020-09-25 21:06:54 -06:00
|
|
|
// It creates the path and any of its parent directories if they do not exist.
|
2020-09-27 12:18:30 -06:00
|
|
|
// KnownHosts will append to the file whenever a certificate is added.
|
2020-10-12 14:56:26 -06:00
|
|
|
func (k *KnownHosts) Load(path string) error {
|
2020-09-25 21:06:54 -06:00
|
|
|
if dir := filepath.Dir(path); dir != "." {
|
|
|
|
err := os.MkdirAll(dir, 0755)
|
|
|
|
if err != nil {
|
2020-09-27 12:18:30 -06:00
|
|
|
return err
|
2020-09-25 21:06:54 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDONLY, 0644)
|
|
|
|
if err != nil {
|
2020-09-27 12:18:30 -06:00
|
|
|
return err
|
2020-09-25 21:06:54 -06: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 {
|
2020-09-27 12:18:30 -06:00
|
|
|
return err
|
2020-09-25 21:06:54 -06:00
|
|
|
}
|
|
|
|
k.file = f
|
2020-09-27 12:18:30 -06:00
|
|
|
return nil
|
2020-09-25 21:06:54 -06:00
|
|
|
}
|
|
|
|
|
2020-09-27 12:18:30 -06:00
|
|
|
// Add adds a certificate to the list of known hosts.
|
2020-09-25 21:06:54 -06:00
|
|
|
// If KnownHosts was loaded from a file, Add will append to the file.
|
2020-09-27 14:06:17 -06:00
|
|
|
func (k *KnownHosts) Add(hostname string, cert *x509.Certificate) {
|
2020-10-13 17:54:48 -06:00
|
|
|
k.add(hostname, cert, true)
|
2020-09-25 21:06:54 -06:00
|
|
|
}
|
2020-09-25 18:22:48 -06:00
|
|
|
|
2020-10-13 17:54:48 -06:00
|
|
|
// AddTemporary adds a certificate to the list of known hosts
|
|
|
|
// without writing it to the known hosts file.
|
2020-09-27 15:41:41 -06:00
|
|
|
func (k *KnownHosts) AddTemporary(hostname string, cert *x509.Certificate) {
|
2020-10-13 17:54:48 -06:00
|
|
|
k.add(hostname, cert, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *KnownHosts) add(hostname string, cert *x509.Certificate, write bool) {
|
|
|
|
if k.hosts == nil {
|
|
|
|
k.hosts = map[string]certInfo{}
|
|
|
|
}
|
|
|
|
info := certInfo{
|
|
|
|
Algorithm: "SHA-512",
|
|
|
|
Fingerprint: Fingerprint(cert),
|
|
|
|
Expires: cert.NotAfter.Unix(),
|
|
|
|
}
|
|
|
|
k.hosts[hostname] = info
|
|
|
|
// Append to the file
|
|
|
|
if write && k.file != nil {
|
|
|
|
appendKnownHost(k.file, hostname, info)
|
|
|
|
}
|
2020-09-27 15:41:41 -06:00
|
|
|
}
|
|
|
|
|
2020-09-26 11:27:03 -06:00
|
|
|
// Lookup looks for the provided certificate in the list of known hosts.
|
2020-10-31 20:34:51 -06:00
|
|
|
// If the hostname is not in the list, Lookup returns ErrCertificateNotFound.
|
|
|
|
// If the fingerprint doesn't match, Lookup returns ErrCertificateNotTrusted.
|
|
|
|
// Otherwise, Lookup returns nil.
|
2020-09-27 13:03:46 -06:00
|
|
|
func (k *KnownHosts) Lookup(hostname string, cert *x509.Certificate) error {
|
2020-09-26 11:27:03 -06:00
|
|
|
now := time.Now().Unix()
|
|
|
|
fingerprint := Fingerprint(cert)
|
2020-10-13 17:54:48 -06:00
|
|
|
if c, ok := k.hosts[hostname]; ok {
|
|
|
|
if c.Expires <= now {
|
2020-09-26 11:27:03 -06:00
|
|
|
// Certificate is expired
|
2020-10-31 20:34:51 -06:00
|
|
|
return ErrCertificateExpired
|
2020-09-26 11:27:03 -06:00
|
|
|
}
|
2020-10-13 17:54:48 -06:00
|
|
|
if c.Fingerprint != fingerprint {
|
|
|
|
// Fingerprint does not match
|
|
|
|
return ErrCertificateNotTrusted
|
2020-09-26 11:27:03 -06:00
|
|
|
}
|
2020-10-31 20:34:51 -06:00
|
|
|
// Certificate is found
|
2020-10-13 17:54:48 -06:00
|
|
|
return nil
|
2020-09-26 11:27:03 -06:00
|
|
|
}
|
2020-10-31 20:34:51 -06:00
|
|
|
return ErrCertificateNotFound
|
2020-09-26 11:27:03 -06:00
|
|
|
}
|
|
|
|
|
2020-09-25 21:06:54 -06:00
|
|
|
// Parse parses the provided reader and adds the parsed known hosts to the list.
|
2020-09-25 18:55:37 -06:00
|
|
|
// Invalid lines are ignored.
|
2020-09-25 21:06:54 -06:00
|
|
|
func (k *KnownHosts) Parse(r io.Reader) {
|
2020-10-13 17:54:48 -06:00
|
|
|
if k.hosts == nil {
|
|
|
|
k.hosts = map[string]certInfo{}
|
|
|
|
}
|
2020-09-25 16:53:20 -06:00
|
|
|
scanner := bufio.NewScanner(r)
|
|
|
|
for scanner.Scan() {
|
|
|
|
text := scanner.Text()
|
|
|
|
parts := strings.Split(text, " ")
|
|
|
|
if len(parts) < 4 {
|
2020-09-25 18:55:37 -06:00
|
|
|
continue
|
2020-09-25 16:53:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
hostname := parts[0]
|
|
|
|
algorithm := parts[1]
|
2020-10-13 17:54:48 -06:00
|
|
|
if algorithm != "SHA-512" {
|
|
|
|
continue
|
|
|
|
}
|
2020-09-25 16:53:20 -06:00
|
|
|
fingerprint := parts[2]
|
2020-09-25 18:22:48 -06:00
|
|
|
expires, err := strconv.ParseInt(parts[3], 10, 0)
|
2020-09-25 16:53:20 -06:00
|
|
|
if err != nil {
|
2020-09-25 18:55:37 -06:00
|
|
|
continue
|
2020-09-25 16:53:20 -06:00
|
|
|
}
|
|
|
|
|
2020-10-13 17:54:48 -06:00
|
|
|
k.hosts[hostname] = certInfo{
|
2020-09-25 16:53:20 -06:00
|
|
|
Algorithm: algorithm,
|
|
|
|
Fingerprint: fingerprint,
|
2020-09-25 18:22:48 -06:00
|
|
|
Expires: expires,
|
2020-10-13 17:54:48 -06:00
|
|
|
}
|
2020-09-25 16:53:20 -06:00
|
|
|
}
|
2020-09-25 18:55:37 -06:00
|
|
|
}
|
2020-09-25 16:53:20 -06:00
|
|
|
|
2020-09-27 12:18:30 -06:00
|
|
|
// Write writes the known hosts to the provided io.Writer.
|
|
|
|
func (k *KnownHosts) Write(w io.Writer) {
|
2020-10-13 17:54:48 -06:00
|
|
|
for h, c := range k.hosts {
|
|
|
|
appendKnownHost(w, h, c)
|
2020-09-27 12:18:30 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 17:54:48 -06:00
|
|
|
type certInfo struct {
|
2020-09-25 18:55:37 -06:00
|
|
|
Algorithm string // fingerprint algorithm e.g. SHA-512
|
|
|
|
Fingerprint string // fingerprint in hexadecimal, with ':' between each octet
|
|
|
|
Expires int64 // unix time of certificate notAfter date
|
2020-09-25 16:53:20 -06:00
|
|
|
}
|
|
|
|
|
2020-10-13 17:54:48 -06:00
|
|
|
func appendKnownHost(w io.Writer, hostname string, c certInfo) (int, error) {
|
|
|
|
return fmt.Fprintf(w, "%s %s %s %d\n", hostname, c.Algorithm, c.Fingerprint, c.Expires)
|
2020-09-25 18:22:48 -06:00
|
|
|
}
|
|
|
|
|
2020-09-25 16:53:20 -06:00
|
|
|
// Fingerprint returns the SHA-512 fingerprint of the provided certificate.
|
|
|
|
func Fingerprint(cert *x509.Certificate) string {
|
|
|
|
sum512 := sha512.Sum512(cert.Raw)
|
2020-10-28 13:13:31 -06:00
|
|
|
var b strings.Builder
|
2020-09-25 16:53:20 -06:00
|
|
|
for i, f := range sum512 {
|
|
|
|
if i > 0 {
|
2020-10-28 13:13:31 -06:00
|
|
|
b.WriteByte(':')
|
2020-09-25 16:53:20 -06:00
|
|
|
}
|
2020-10-28 13:13:31 -06:00
|
|
|
fmt.Fprintf(&b, "%02X", f)
|
2020-09-25 16:53:20 -06:00
|
|
|
}
|
2020-10-28 13:13:31 -06:00
|
|
|
return b.String()
|
2020-09-25 16:53:20 -06:00
|
|
|
}
|
2020-09-26 11:35:56 -06:00
|
|
|
|
|
|
|
// defaultKnownHostsPath returns the default known_hosts path.
|
|
|
|
// The default path is $XDG_DATA_HOME/gemini/known_hosts
|
|
|
|
func defaultKnownHostsPath() (string, error) {
|
|
|
|
dataDir, err := userDataDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(dataDir, "gemini", "known_hosts"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// userDataDir returns the user data directory.
|
|
|
|
func userDataDir() (string, error) {
|
|
|
|
dataDir, ok := os.LookupEnv("XDG_DATA_HOME")
|
|
|
|
if ok {
|
|
|
|
return dataDir, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return filepath.Join(home, ".local", "share"), nil
|
|
|
|
}
|