diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2025-04-16 17:27:20 -0400 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2025-04-16 17:27:20 -0400 |
| commit | 0d3f55a926e42ffa45febc4cc722f3d7873206e0 (patch) | |
| tree | 01a70ddd2fe4a3de79552aacde411fe3338ae22f | |
| parent | 48d43d62e22e02c862b285f8ae5db62256ae5ab6 (diff) | |
| download | hose-0d3f55a926e42ffa45febc4cc722f3d7873206e0.zip | |
call key decoding functions when parsing known_hosts file
| -rw-r--r-- | hosts/hosts.go | 25 | ||||
| -rw-r--r-- | key/box.go | 5 | ||||
| -rw-r--r-- | key/sig.go | 4 |
3 files changed, 16 insertions, 18 deletions
diff --git a/hosts/hosts.go b/hosts/hosts.go index 3dc3f33..0412e4a 100644 --- a/hosts/hosts.go +++ b/hosts/hosts.go @@ -2,7 +2,7 @@ package hosts import ( "bufio" - "encoding/hex" + "bytes" "errors" "fmt" "github.com/adrg/xdg" @@ -10,7 +10,6 @@ import ( "os" "path/filepath" "slices" - "strings" "git.samanthony.xyz/hose/key" "git.samanthony.xyz/hose/util" @@ -57,7 +56,7 @@ func Load() ([]Host, error) { scanner := bufio.NewScanner(f) for line := 1; scanner.Scan(); line++ { - host, err := parseHost(scanner.Text()) + host, err := parseHost(scanner.Bytes()) if err != nil { return hosts, fmt.Errorf("error parsing known hosts file: %s:%d: %v", knownHostsFile, line, err) } @@ -71,30 +70,24 @@ func Load() ([]Host, error) { } // parseHost parses a line of the known hosts file. -func parseHost(s string) (Host, error) { - fields := strings.Fields(s) +func parseHost(b []byte) (Host, error) { + fields := bytes.Fields(b) if len(fields) != 3 { return Host{}, fmt.Errorf("expected 3 fields; got %d", len(fields)) } - addr, err := netip.ParseAddr(fields[0]) + addr, err := netip.ParseAddr(string(fields[0])) if err != nil { return Host{}, err } - var boxPubKey key.BoxPublicKey - if hex.DecodedLen(len(fields[1])) != len(boxPubKey) { - return Host{}, fmt.Errorf("malformed box public key: %s", fields[1]) - } - if _, err := hex.Decode(boxPubKey[:], []byte(fields[1])); err != nil { + boxPubKey, err := key.DecodeBoxPublicKey(fields[1]) + if err != nil { return Host{}, err } - var sigPubKey key.SigPublicKey - if hex.DecodedLen(len(fields[2])) != len(sigPubKey) { - return Host{}, fmt.Errorf("malformed signature public key: %s", fields[2]) - } - if _, err := hex.Decode(sigPubKey[:], []byte(fields[2])); err != nil { + sigPubKey, err := key.DecodeSigPublicKey(fields[2]) + if err != nil { return Host{}, err } @@ -67,6 +67,11 @@ func (bpk1 BoxPublicKey) Compare(bpk2 BoxPublicKey) int { return bytes.Compare(bpk1[:], bpk2[:]) } +func DecodeBoxPublicKey(buf []byte) (BoxPublicKey, error) { + key, err := decodeBoxKey(buf) + return BoxPublicKey(key), err +} + func decodeBoxKey(buf []byte) ([32]byte, error) { var key [32]byte if hex.DecodedLen(len(buf)) != len(key) { @@ -37,14 +37,14 @@ func LoadSigPublicKey() (SigPublicKey, error) { } // Decode key. - return decodeSigPublicKey(buf) + return DecodeSigPublicKey(buf) } func (spk1 SigPublicKey) Compare(spk2 SigPublicKey) int { return bytes.Compare(spk1[:], spk2[:]) } -func decodeSigPublicKey(buf []byte) (SigPublicKey, error) { +func DecodeSigPublicKey(buf []byte) (SigPublicKey, error) { var key SigPublicKey if hex.DecodedLen(len(buf)) != len(key) { return SigPublicKey{}, fmt.Errorf("malformed signature verification key: expected %d bytes; got %d", |