keysigner

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 15 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 keysigner 实现 XOps v2 基于 golang.org/x/crypto/ssh 的私钥解析、指纹核验与签名器适配器 (ports.KeySignerPort,ADR-011 / INV-F1)。

本包隶属 core/internal/sshnet/dial 明文消费白名单层(LayerCoreSSHNetDial),严格维持以下安全不变量:

  • 所有私钥原始字节均经 OperatorReadFS 有界读取后立即装入 secret.WithBytes 同步闭包零化消费, 仅保留不透明 ssh.Signer 与公钥指纹,绝不将原始私钥材料持久化或暴露至日志/错误;
  • 口令保护私钥经 SecretVault.WithSecret + 嵌套 keyBox.Use -> passBox.Use 解密并强制校验 INV-F1 指纹绑定;
  • 全包零行内 nolint 豁免、0 裸 go 语句与 0 全局状态。

Index

Constants

View Source
const (
	// MaxPrivateKeyFileBytes 限制单个本地私钥文件的读取字节上限(1 MiB)。
	MaxPrivateKeyFileBytes int64 = 1 << 20
	// MaxCertFileBytes 限制单个本地用户证书文件的读取字节上限(1 MiB)。
	MaxCertFileBytes int64 = 1 << 20
)

Variables

This section is empty.

Functions

func AttachMatchingCert

func AttachMatchingCert(signer ssh.Signer, certs []*ssh.Certificate) ssh.Signer

AttachMatchingCert 若 certs 中存在公钥指纹与 signer 匹配的 SSH 证书,则返回证书签名包装器,否则原样返回。

func FingerprintOfSSHKey

func FingerprintOfSSHKey(key ssh.PublicKey) inventory.KeyFingerprint

FingerprintOfSSHKey 计算 ssh.PublicKey 的 SHA-256 规范指纹;nil 公钥返回无效零值。

func LoadCertificates

func LoadCertificates(
	ctx context.Context,
	readFS ports.OperatorReadFS,
	env ports.EnvPort,
	tokenCtx option.TokenContext,
	certPaths []inventory.CertPathSpec,
) ([]*ssh.Certificate, error)

LoadCertificates 按路径清单读取并解析本地 OpenSSH 用户证书(缺失的证书文件按 OpenSSH 惯例静默跳过)。

func LoadPrivateKeyFile

LoadPrivateKeyFile 通过 OperatorReadFS 读取有界私钥文件并在同步 SecretBox 闭包内解密为不透明 ssh.Signer。 强制执行 INV-F1:当绑定口令凭据时,解密后公钥指纹必须与 KeyPassphraseBinding.KeyFingerprint 一致。

func ParsePrivateKeyBytes

func ParsePrivateKeyBytes(
	ctx context.Context,
	vault ports.SecretVault,
	keyBytes []byte,
	passOpt algebraic.Option[inventory.KeyPassphraseBinding],
) (ssh.Signer, error)

ParsePrivateKeyBytes 在同步明文作用域内解析未加密或口令保护私钥,并强制校验 INV-F1 口令指纹绑定。

func PublicKeyInfoFromSSH

func PublicKeyInfoFromSSH(key ssh.PublicKey) (ports.PublicKeyInfo, error)

PublicKeyInfoFromSSH 将 golang.org/x/crypto/ssh.PublicKey 转换为协议库无关的不可变 ports.PublicKeyInfo。

func ReadBoundedFile

func ReadBoundedFile(ctx context.Context, readFS ports.OperatorReadFS, path pathalg.LocalSourcePath, maxBytes int64) ([]byte, error)

ReadBoundedFile 经 OperatorReadFS 在 maxBytes 上限内安全读取单个文件字节(失败或超限时立即 clear 局部缓冲)。

func ResolveLocalFilePath

func ResolveLocalFilePath(env ports.EnvPort, tokenCtx option.TokenContext, raw string) (pathalg.LocalSourcePath, error)

ResolveLocalFilePath 展开路径模板中的 OpenSSH 令牌与 "~/" 主目录前缀,并解析为不可变 LocalSourcePath。

func VerifyPassphraseBindingLoaded

func VerifyPassphraseBindingLoaded(rawSigners []ssh.Signer, passOpt algebraic.Option[inventory.KeyPassphraseBinding]) error

VerifyPassphraseBindingLoaded 强制校验 INV-F1 防掉包不变式: 当 AuthPrivateKey 声明了 KeyPassphraseBinding 时,必须在附加证书前确认已加载的原始私钥集合中 至少包含一枚与 binding.KeyFingerprint 一致的私钥,彻底阻断将加密私钥静默替换为未加密伪造私钥的绕过攻击。

func WrapPortSigners

func WrapPortSigners(keys []ports.PublicKeyInfo, signFn PortSignFunc) ([]ssh.Signer, error)

WrapPortSigners 将一组 ports.PublicKeyInfo 包装为可传入 ssh.PublicKeys 的 ssh.Signer 列表。

Types

type Config

type Config struct {
	// ReadFS 提供操作员本地私钥与证书文件的受控只读能力(经 SymlinkFollowOperatorSource)。
	ReadFS ports.OperatorReadFS
	// Vault 可选注入凭据库,用于读取口令保护私钥绑定的解密口令。
	Vault ports.SecretVault
	// Agent 可选注入本机 SSH Agent 端口,用于 AuthAgent 变体列举与签名回退。
	Agent ports.SSHAgentPort
	// Env 可选注入本机身份信息端口,用于展开私钥/证书路径中的 "~/" 前缀。
	Env ports.EnvPort
	// Entropy 可选注入显式随机字节源供签名算法使用;为空时回退 crypto/rand.Reader。
	Entropy ports.Entropy
}

Config 描述 KeySigner 的显式出站依赖配置(全依赖注入,零全局单例与隐式环境变量)。

type KeySigner

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

KeySigner 实现 ports.KeySignerPort,提供并发安全的私钥加载、证书匹配、签名与指纹核验能力;零值恒失败关闭。

func NewKeySigner

func NewKeySigner(cfg Config) (*KeySigner, error)

NewKeySigner 校验至少注入 ReadFS 或 Agent 端口后构造不可变 KeySigner。

func (*KeySigner) InspectPrivateKeyFingerprint

func (k *KeySigner) InspectPrivateKeyFingerprint(
	ctx context.Context,
	keyPath inventory.KeyPathSpec,
	passphrase algebraic.Option[secret.Ref],
) (inventory.KeyFingerprint, error)

InspectPrivateKeyFingerprint 读取并核验未加密或口令保护私钥文件的 SHA-256 公钥指纹(支撑加密私钥原子登记)。

func (*KeySigner) ListPublicKeys

func (k *KeySigner) ListPublicKeys(ctx context.Context, auth inventory.AuthMethod) ([]ports.PublicKeyInfo, error)

ListPublicKeys 根据认证方式加载并缓存不透明签名器,返回协议库无关的公钥元数据列表。

func (*KeySigner) Sign

func (k *KeySigner) Sign(ctx context.Context, key inventory.KeyFingerprint, algorithm string, data []byte) (ports.Signature, error)

Sign 使用已加载的本地不透明 ssh.Signer 或已登记的 Agent 公钥对 data 执行指定算法签名。

type PortSignFunc

type PortSignFunc func(fp inventory.KeyFingerprint, algorithm string, data []byte) (ports.Signature, error)

PortSignFunc 是适配外部 KeySignerPort / SSHAgentPort 签名回调的函数类型。

Jump to

Keyboard shortcuts

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