tls.go中的流程梳理

文章目录

tls.go中的流程梳理

b站博主的 tls 加密过程
  1. 客户端发送ClentHello(tls版本 +加密套件+ 随机数1)
  2. 服务器发送ServerHello(tls版本 + 加密套件 +随机数2)
    1. 这个阶段之后,双方都知道了tls版本,选定的加密算法,两个随机数
  3. 服务器发送一个X.509证书,客户端用于验证且知道了服务器的公钥,用于后续传输数据加密
  4. 服务器发送它自己的公钥,若上一步有,则这一步不需要
  5. 服务器发送 server Hello Done
  6. 客户端生成 随机数3(预主密钥),并用服务器公钥发送给客户端
    1. 至此 双方都知道了3个随机数,根据3个随机数得到对称加密的秘钥
  7. Change Cipher Spec 表示随后的信息都将用双方商定的加密方法和密钥发送
自己推理的过程(未完待续)
发送ClientHello
  1. 客户端发送 Dial(network, addr string, config *Config) (*Conn, error)

首先调用了Dialer拨号方法得到了 rawConn,然后通过Client(conn net.Conn, config *Config)封装了tls包下的Conn结构。然后进行握手c.HandshakeContext

go 复制代码
// 重要代码  ctx context.Context 
func dial(ctx context.Context, netDialer *net.Dialer, network, addr string, config *Config) (*Conn, error) {
    rawConn, err := netDialer.DialContext(ctx, network, addr)
    
    // 获取主机名 hostname
    colonPos := strings.LastIndex(addr, ":")
    if colonPos == -1 {
        colonPos = len(addr)
    }
    hostname := addr[:colonPos]
    
    // 握手阶段 此处初始化了Client
    conn := Client(rawConn, config)
    if err := conn.HandshakeContext(ctx); err != nil {
        rawConn.Close()
        return nil, err
    }
    return conn, nil
}
  1. 分析 conn := Client(rawConn, config)

发现有一个函数 c.handshakeFn = c.clientHandshake 后续要用到

go 复制代码
func Client(conn net.Conn, config *Config) *Conn {
	c := &Conn{
		conn:     conn,
		config:   config,
		isClient: true,
	}
	c.handshakeFn = c.clientHandshake
	return c
}
  1. 点到 conn.HandshakeContext(ctx)分析
go 复制代码
// 删掉无关代码
func (c *Conn) handshakeContext(ctx context.Context) (ret error) {
	// 在此处做了 client 的 handshake
	c.handshakeErr = c.handshakeFn(handshakeCtx)
}
  1. 点到 c.handshakeFn(handshakeCtx)
go 复制代码
func (c *Conn) clientHandshake(ctx context.Context) (err error) {	
    // 此处初始化了 hello 报文
	hello, ecdheKey, err := c.makeClientHello()
}

// 下面的函数生成了 hello 报文  包括密钥空间 密钥等等
func (c *Conn) makeClientHello() (*clientHelloMsg, *ecdh.PrivateKey, error) {
	hello := &clientHelloMsg{
		vers:                         clientHelloVersion,
		compressionMethods:           []uint8{compressionNone},
		random:                       make([]byte, 32),
		extendedMasterSecret:         true,
		ocspStapling:                 true,
		scts:                         true,
		serverName:                   hostnameInSNI(config.ServerName),
		supportedCurves:              config.curvePreferences(),
		supportedPoints:              []uint8{pointFormatUncompressed},
		secureRenegotiationSupported: true,
		alpnProtocols:                config.NextProtos,
		supportedVersions:            supportedVersions,
	}
	var key *ecdh.PrivateKey
	return hello, key, nil
}
  1. 生成hello报文后,调用函数c.writeHandshakeRecord发送数据,c.readHandshake读取数据
go 复制代码
func (c *Conn) clientHandshake(ctx context.Context) (err error) {
	hello, ecdheKey, err := c.makeClientHello()
	
	if _, err := c.writeHandshakeRecord(hello, nil); err != nil {
		return err
	}

	// serverHelloMsg is not included in the transcript
	msg, err := c.readHandshake(nil)

	serverHello, ok := msg.(*serverHelloMsg)

	return nil
}
获取ServerHello

如上:在发送完ClientHello信息后使用c.readHandshake(),获取从服务器过来的ServerHello信息。然后是使用类型强转serverHello, ok := msg.(*serverHelloMsg)

然后根据SeverHello中选择的TLS版本和ClientHello中的版本范围进行校验。看服务器发送过来的TLS版本是否在ClientHello指定的范围中。

相关推荐
超龄超能程序猿3 分钟前
Bitvisse SSH Client 安装配置文档
运维·ssh·github
丶意冷1 小时前
mybatisPlus分页方言设置错误问题 mybatisPlus对于Oceanbase的Oracle租户分页识别错误
java·数据库·oracle·oceanbase
时序数据说3 小时前
为什么时序数据库IoTDB选择Java作为开发语言
java·大数据·开发语言·数据库·物联网·时序数据库·iotdb
戒不掉的伤怀3 小时前
【Navicat 连接MySQL时出现错误1251:客户端不支持服务器请求的身份验证协议;请考虑升级MySQL客户端】
服务器·数据库·mysql
cv高级工程师YKY3 小时前
服务器 - - QPS与TPS介绍
数据库
nbsaas-boot3 小时前
高可扩展属性建模设计:架构师的全局思考与落地方案
数据库
Natsume17103 小时前
嵌入式开发:GPIO、UART、SPI、I2C 驱动开发详解与实战案例
c语言·驱动开发·stm32·嵌入式硬件·mcu·架构·github
爱上语文3 小时前
Redis基础(5):Redis的Java客户端
java·开发语言·数据库·redis·后端
荔枝吻4 小时前
【AI总结】Git vs GitHub vs GitLab:深度解析三者联系与核心区别
人工智能·git·github
幻凡ss4 小时前
github pages使用免费CDN加速-netlify
github·github pages·github cdn·github pages加速·netlify加速github·hexo博客免费cdn加速·个人博客免费cdn加速