hostkey

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package hostkey 实现 XOps v2 的 SSH 主机密钥信任库适配器(ports.HostTrustPort,INV-C5 / INV-E2)。

本包完全构建于 ports.ScopedRootFS 与 core/osfs 旁路锁原语之上:

  • 严禁绕过 core/osfs 直接调用裸 os.Open/os.Rename 或 path/filepath;
  • 单次核验/查询在共享旁路锁 <name>.lock 与 O_NOFOLLOW 约束下读取单一 known_hosts 快照, 拒绝符号链接、目录与特殊文件;
  • 持久化写入在独占旁路锁 <name>.lock 内完成读-合并-原子替换(绝不对数据文件本身加锁后 Rename, 彻底规避 Windows ERROR_SHARING_VIOLATION);
  • 主机匹配严格按 netaddr.Endpoint (Host, Port) 隔离(INV-E2),支持 OpenSSH 明文、 通配符/否定模式、|1|<salt>|<hmac-sha1> 哈希条目、@revoked 吊销标记、 @cert-authority 主机证书验签(拒绝 TOFU 落盘原始证书)与可选 DNSSEC SSHFP。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// RootFS 是必填的沙箱本地文件系统端口,用于全部 O_NOFOLLOW 读取、旁路锁与原子替换。
	RootFS ports.ScopedRootFS
	// DefaultDir 是未显式指定 UserKnownHostsFile 时的默认沙箱根目录(如 ~/.ssh)。
	DefaultDir pathalg.LocalDir
	// DefaultFileName 是默认目录内的 known_hosts 文件名,留空时默认 "known_hosts"。
	DefaultFileName string
	// HomeDir 可选提供本机主目录,用于展开 UserKnownHostsFile 中的 "~/" 或 "%d" 模板。
	HomeDir algebraic.Option[pathalg.LocalDir]
	// MaxFileBytes 可选指定单个 known_hosts 文件的读取字节上限,<= 0 时默认 16 MiB。
	MaxFileBytes int64
	// SSHFPLookup 可选注入 DNS SSHFP 只读查询端口(仅信任 Authenticated==true 的 DNSSEC 记录)。
	SSHFPLookup ports.SSHFPLookupPort
	// HashHostnames 为 true 时,RecordHostKey 以 OpenSSH |1|<salt>|<hmac-sha1> 哈希格式写入主机名。
	HashHostnames bool
	// Entropy 在 HashHostnames 为 true 时提供显式密码学随机字节,用于生成 20 字节哈希盐。
	Entropy ports.Entropy
	// Clock 可选注入时钟端口,用于校验 @cert-authority SSH 主机证书有效期;为 nil 时回退真实 UTC 时钟。
	Clock ports.Clock
}

Config 描述 KnownHostsDB 的显式出站依赖与默认路径配置(全依赖注入,零隐式环境变量)。

type KnownHostsDB

type KnownHostsDB struct {
	// contains filtered or unexported fields
}

KnownHostsDB 是基于单旁路锁 O_NOFOLLOW known_hosts 快照的 ports.HostTrustPort 生产实现。 零值恒未初始化且所有方法均严格失败关闭。

func NewKnownHostsDB

func NewKnownHostsDB(cfg Config) (*KnownHostsDB, error)

NewKnownHostsDB 校验配置依赖、默认目录/文件名、读取上限及可选哈希熵源并构造不可变 KnownHostsDB。

func (*KnownHostsDB) CleanupSidecarLock

func (db *KnownHostsDB) CleanupSidecarLock(ctx context.Context, endpoint netaddr.Endpoint, knownHosts option.KnownHostsList) (bool, error)

CleanupSidecarLock 按三平台 Windows 安全删锁协议清理首个目标文件对应的旁路锁 <name>.lock: 持独占锁截断清空 -> 解锁关句柄 -> 删除(忽略 ErrNotFound),消除 Windows 共享冲突。

func (*KnownHostsDB) PreferredAlgorithms

func (db *KnownHostsDB) PreferredAlgorithms(ctx context.Context, endpoint netaddr.Endpoint, knownHosts option.KnownHostsList) ([]string, error)

PreferredAlgorithms 在共享旁路锁与 O_NOFOLLOW 约束下读取 known_hosts 快照, 返回指定端点已记录的主机密钥算法去重顺序列表,供 SSH 握手优先协商。

func (*KnownHostsDB) RecordHostKey

func (db *KnownHostsDB) RecordHostKey(ctx context.Context, endpoint netaddr.Endpoint, key ports.PublicKeyInfo, knownHosts option.KnownHostsList) error

RecordHostKey 在独占旁路锁 <name>.lock 保护下读取并合并首个可写 known_hosts 文件, 随后以同目录临时文件原子替换落盘(绝不对数据文件本身加锁后 Rename,兼容 Windows)。 严格拒绝以 TOFU 方式记录原始 SSH 主机证书(*-cert-v01@openssh.com)。

func (*KnownHostsDB) VerifyHostKey

VerifyHostKey 在单次共享旁路锁与 O_NOFOLLOW 约束下读取 known_hosts 快照并核验远端呈递公钥(INV-C5 / INV-E2)。 核验严格遵循安全优先级:@revoked > @cert-authority/已信任匹配 > 已知端点冲突(Mismatch)> 已认证 DNSSEC SSHFP > 未知(Unknown)。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL