Files
windows-rdp-keylogger/internal/tlsutil/trust.go
T

109 lines
3.5 KiB
Go
Executable File

package tlsutil
import (
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"time"
)
// TrustedDevice is one entry in the trust store: a certificate fingerprint
// this client has approved, plus a human-friendly label so multiple
// distinct devices (a laptop, a desktop, a tablet, ...) stay identifiable
// instead of being a wall of hex fingerprints.
type TrustedDevice struct {
Label string `json:"label"`
FirstSeen time.Time `json:"first_seen"`
LastSeen time.Time `json:"last_seen"`
}
// TrustStore is a client-side trust-on-first-use record of which
// certificate fingerprints have been approved, keyed by fingerprint
// rather than by network address or listen endpoint.
//
// Trust has to travel with the certificate's identity, not the connection
// path: the same device can be reached at different addresses over time
// (DHCP renewal, roaming), and — since multiple different devices can all
// call back to the same -listen endpoint — several distinct fingerprints
// legitimately need to be trusted under what used to be a single
// address-keyed slot. Keying by fingerprint is also just the same model
// SSH's known_hosts uses: it records which keys you've approved, not
// which address you approved them at.
type TrustStore struct {
path string
entries map[string]TrustedDevice // key: fingerprint
}
func LoadTrustStore(path string) (*TrustStore, error) {
ts := &TrustStore{path: path, entries: map[string]TrustedDevice{}}
data, err := os.ReadFile(path)
if errors.Is(err, os.ErrNotExist) {
return ts, nil
}
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &ts.entries); err != nil {
// Most likely an older known_hosts.json from before multi-device
// support (address-keyed, plain fingerprint-string values, which
// won't unmarshal into TrustedDevice). Rather than fail to start,
// treat it as empty — every device just gets re-trusted once on
// its next connection.
ts.entries = map[string]TrustedDevice{}
}
return ts, nil
}
func (ts *TrustStore) save() error {
if err := os.MkdirAll(filepath.Dir(ts.path), 0o700); err != nil {
return err
}
data, err := json.MarshalIndent(ts.entries, "", " ")
if err != nil {
return err
}
return os.WriteFile(ts.path, data, 0o600)
}
// Lookup returns the trusted entry for fingerprint, if any.
func (ts *TrustStore) Lookup(fingerprint string) (TrustedDevice, bool) {
d, ok := ts.entries[fingerprint]
return d, ok
}
// Trust records fingerprint as approved under label, persisting to disk.
// Calling it again for an already-trusted fingerprint updates the label
// and LastSeen without touching FirstSeen.
func (ts *TrustStore) Trust(fingerprint, label string) error {
d, existed := ts.entries[fingerprint]
now := time.Now()
if !existed {
d.FirstSeen = now
}
d.Label = label
d.LastSeen = now
ts.entries[fingerprint] = d
return ts.save()
}
// Touch updates LastSeen for an already-trusted fingerprint. Intended to
// be non-fatal to callers — losing a last-seen timestamp isn't worth
// aborting a connection over.
func (ts *TrustStore) Touch(fingerprint string) error {
d, ok := ts.entries[fingerprint]
if !ok {
return fmt.Errorf("tlsutil: %s is not a trusted fingerprint", fingerprint)
}
d.LastSeen = time.Now()
ts.entries[fingerprint] = d
return ts.save()
}
// All returns every trusted device, keyed by fingerprint, for listing or
// management. Callers must not mutate the returned map.
func (ts *TrustStore) All() map[string]TrustedDevice {
return ts.entries
}