tls.go中的流程梳理
创始人
2024-11-13 11:08:59
0

文章目录

      • tls.go中的流程梳理
        • b站博主的 tls 加密过程
        • 自己推理的过程(未完待续)
          • 发送ClientHello
          • 获取ServerHello

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 表示随后的信息都将用双方商定的加密方法和密钥发送

image.png

自己推理的过程(未完待续)

发送ClientHello
  1. 客户端发送 Dial(network, addr string, config *Config) (*Conn, error)

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

// 重要代码  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 后续要用到

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)分析
// 删掉无关代码 func (c *Conn) handshakeContext(ctx context.Context) (ret error) { 	// 在此处做了 client 的 handshake 	c.handshakeErr = c.handshakeFn(handshakeCtx) } 
  1. 点到 c.handshakeFn(handshakeCtx)
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读取数据
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指定的范围中。

相关内容

热门资讯

绝活儿辅助!广西老友玩老是输怎... 绝活儿辅助!广西老友玩老是输怎么办(辅助挂)都是真的有辅助app(讲解有挂)在进入广西老友玩老是输怎...
法门辅助!福建13水插件(辅助... 法门辅助!福建13水插件(辅助挂)一贯是有辅助技巧(有挂技术)1、许多玩家不知道福建13水插件辅助怎...
办法辅助!潮友会app下载官方... 办法辅助!潮友会app下载官方辅助器(辅助挂)真是真的是有辅助app(有挂教程)该软件可以轻松地帮助...
妙招辅助!邯郸胡乐挂辅助(辅助... 妙招辅助!邯郸胡乐挂辅助(辅助挂)好像存在有辅助插件(有挂方略)1、上手简单,内置详细流程视频教学,...
教程书辅助!乐酷辅助(辅助挂)... 教程书辅助!乐酷辅助(辅助挂)其实存在有辅助脚本(有挂细节)乐酷辅助能透视中分为三种模型:乐酷辅助模...
学习辅助!决战卡五星辅助(辅助... 学习辅助!决战卡五星辅助(辅助挂)本来真的是有辅助软件(有人有挂)学习辅助!决战卡五星辅助(辅助挂)...
绝活辅助!边锋嘉兴麻将辅助器(... 绝活辅助!边锋嘉兴麻将辅助器(辅助挂)真是真的有辅助神器(新版有挂)1、边锋嘉兴麻将辅助器公共底牌简...
举措辅助!枫叶辅助器(辅助挂)... 举措辅助!枫叶辅助器(辅助挂)本来存在有辅助技巧(竟然有挂)1、下载好枫叶辅助器正确养号方法之后点击...
讲义辅助!点我达辅助(辅助挂)... 讲义辅助!点我达辅助(辅助挂)一直存在有辅助技巧(有人有挂)1、点我达辅助辅助器安装包、点我达辅助辅...
模块辅助!威信茶馆有挂的吗(辅... 模块辅助!威信茶馆有挂的吗(辅助挂)一直真的是有辅助脚本(揭秘有挂)1、玩家可以在威信茶馆有挂的吗线...