dep2ptls

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2025 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const ID = "/tls/1.0.0"

ID 是协议标识符(用于多流协议协商)

Variables

This section is empty.

Functions

func GenerateSignedExtension

func GenerateSignedExtension(sk ic.PrivKey, pubKey crypto.PublicKey) (pkix.Extension, error)

GenerateSignedExtension 使用提供的私钥签名公钥,并在pkix.Extension中返回签名 参数:

  • sk: ic.PrivKey 私钥
  • pubKey: crypto.PublicKey 公钥

返回值:

  • pkix.Extension 扩展对象
  • error 错误信息

注意:

  • 此扩展包含在证书中以加密方式将其绑定到dep2p私钥

func PubKeyFromCertChain

func PubKeyFromCertChain(chain []*x509.Certificate) (ic.PubKey, error)

PubKeyFromCertChain 验证证书链并提取远程节点的公钥 参数:

  • chain: []*x509.Certificate 证书链

返回值:

  • ic.PubKey 公钥
  • error 错误信息

Types

type Identity

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

Identity 用于安全连接

func NewIdentity

func NewIdentity(privKey ic.PrivKey, opts ...IdentityOption) (*Identity, error)

NewIdentity 创建新的身份 参数:

  • privKey: ic.PrivKey 私钥
  • opts: ...IdentityOption 可选的配置选项

返回值:

  • *Identity 身份对象
  • error 错误信息

func (*Identity) ConfigForPeer

func (i *Identity) ConfigForPeer(remote peer.ID) (*tls.Config, <-chan ic.PubKey)

ConfigForPeer 创建新的单次使用TLS配置,用于验证对等节点的证书链 并通过channel返回对等节点的公钥 参数:

  • remote: peer.ID 远程节点ID

返回值:

  • *tls.Config TLS配置
  • <-chan ic.PubKey 公钥通道

注意:

  • 如果peer ID为空,返回的配置将接受任何对等节点

type IdentityConfig

type IdentityConfig struct {
	// 证书模板
	CertTemplate *x509.Certificate
	// 密钥日志写入器
	KeyLogWriter io.Writer
}

IdentityConfig 用于配置Identity

type IdentityOption

type IdentityOption func(r *IdentityConfig)

IdentityOption 用于转换IdentityConfig以应用可选设置

func WithCertTemplate

func WithCertTemplate(template *x509.Certificate) IdentityOption

WithCertTemplate 指定生成新证书时使用的模板 参数:

  • template: *x509.Certificate 证书模板

返回值:

  • IdentityOption 身份配置选项函数

func WithKeyLogWriter

func WithKeyLogWriter(w io.Writer) IdentityOption

WithKeyLogWriter 可选地指定用于TLS主密钥的NSS密钥日志格式的目标 这可以允许Wireshark等外部程序解密TLS连接 参数:

  • w: io.Writer 日志写入器

返回值:

  • IdentityOption 身份配置选项函数

注意:

  • 使用KeyLogWriter会降低安全性,仅用于调试

type Transport

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

Transport 为对等节点构建安全通信会话

func New

func New(id protocol.ID, key ci.PrivKey, muxers []tptu.StreamMuxer) (*Transport, error)

New 创建一个TLS加密传输层 参数:

  • id: protocol.ID 协议标识符
  • key: ci.PrivKey 私钥
  • muxers: []tptu.StreamMuxer 多路复用器列表

返回值:

  • *Transport 传输层实例
  • error 错误信息

func (*Transport) ID

func (t *Transport) ID() protocol.ID

ID 返回协议标识符 返回值:

  • protocol.ID 协议标识符

func (*Transport) SecureInbound

func (t *Transport) SecureInbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error)

SecureInbound 作为服务端执行TLS握手 参数:

  • ctx: context.Context 上下文
  • insecure: net.Conn 未加密连接
  • p: peer.ID 对端ID(为空时接受任意对端)

返回值:

  • sec.SecureConn 安全连接
  • error 错误信息

func (*Transport) SecureOutbound

func (t *Transport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error)

SecureOutbound 作为客户端执行TLS握手 参数:

  • ctx: context.Context 上下文
  • insecure: net.Conn 未加密连接
  • p: peer.ID 对端ID

返回值:

  • sec.SecureConn 安全连接
  • error 错误信息

注意:

  • 如果服务器不接受证书,SecureOutbound不会返回错误
  • 这是因为在TLS 1.3中,客户端在同一个飞行中发送其证书和ClientFinished,并且可以立即发送应用数据
  • 如果握手失败,服务器将关闭连接。客户端在调用Read时会在1个RTT后注意到这一点

Jump to

Keyboard shortcuts

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