certificate-transparency-go用例

2024-01-24 12:44

本文主要是介绍certificate-transparency-go用例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 证书的SCT列表
  • 验证SCT
    • 依赖包
    • 加载证书
    • 初始化log机构信息
    • 离线验证+在线验证

证书的SCT列表

浏览器对证书链的合法性检查通过后,会再检查服务端证书附件里的SCT列表(Signed Certificate Timestamp);
浏览器内置了一批certificate transparency log机构的公钥和访问地址,如果SCT申明证书在某个log机构注册了,但是SCT里的签名通过不了log机构的公钥验证,则抛出错误NET::ERR_CERTIFICATE_TRANSPARENCY_REQUIRED

验证SCT

依赖包

使用github.com/google/certificate-transparency-go工具

import ("context""encoding/base64""encoding/pem""errors""io""log""net/http""os""time"ct "github.com/google/certificate-transparency-go""github.com/google/certificate-transparency-go/ctutil""github.com/google/certificate-transparency-go/loglist3"ctX509 "github.com/google/certificate-transparency-go/x509""github.com/google/certificate-transparency-go/x509util"
)

加载证书

假设服务端证书以及签发该证书的上级CA证书,已保存为PEM格式的文件

func VerifySCT(certLocation string, issuerLocation string) error {// 服务端证书certByte, err := os.ReadFile(certLocation)if err != nil {return err}block, _ := pem.Decode(certByte)if block == nil || len(block.Bytes) == 0 {return errors.New("error decoding certificate")}cert, err := ctX509.ParseCertificate(block.Bytes)if err != nil {return err}// 上级CAcertByte, _ = os.ReadFile(issuerLocation)block, _ = pem.Decode(certByte)if block == nil || len(block.Bytes) == 0 {return errors.New("error decoding issuer CA")}issuer, _ := ctX509.ParseCertificate(block.Bytes)err = cert.CheckSignatureFrom(issuer)if err != nil {log.Printf("证书%s的签名算法是%s,CA签名没有验证成功", cert.Subject, cert.SignatureAlgorithm.String())return err}// 生成merkle tree leaf,用于验证sct(Signed Certificate Timestamp)merkleLeaf, err := ct.MerkleTreeLeafForEmbeddedSCT([]*ctX509.Certificate{cert, issuer}, 0)if err != nil {return err}// 获取证书里附带的sct列表sctList, err := x509util.ParseSCTsFromSCTList(&cert.SCTList)if err != nil {log.Printf("ParseCertificate failed %v", err)return err}log.Printf("验证证书%s的SCT列表", cert.Subject)

初始化log机构信息

使用和chrome一致的机构列表:https://www.gstatic.com/ct/log_list/v3/log_list.json

	// 获取chrome使用的certificate transparency log机构列表,包含机构使用的公钥和查询api地址resp, err := http.DefaultClient.Get(loglist3.LogListURL)if err != nil {return errors.New("下载certificate transparency log地址列表失败")}defer resp.Body.Close()body, err := io.ReadAll(resp.Body)if err != nil {return errors.New("下载certificate transparency log地址列表失败")}loglistEntry, _ := loglist3.NewFromJSON(body)logsByHash, _ := ctutil.LogInfoByKeyHash(loglistEntry, http.DefaultClient)

离线验证+在线验证

logInfo.VerifySCTSignature方法不需要和log机构在线交互,是使用已知的log机构公钥对SCT进行离线验证

	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)defer cancel()for _, sct := range sctList {// 验证sct,参考https://github.com/google/certificate-transparency-go/blob/master/ctutil/sctscan/sctscan.golog.Printf("sct signature: %s, %s", base64.StdEncoding.EncodeToString(sct.Signature.Signature), time.Unix(0, int64(sct.Timestamp)*int64(time.Millisecond)).Format(time.RFC3339Nano))logInfo, ok := logsByHash[sct.LogID.KeyID]if !ok {log.Printf("sct key_hash: %s,不存在对应certificate transparency log机构", base64.StdEncoding.EncodeToString(sct.LogID.KeyID[:]))continue}log.Printf("颁发sct的certificate transparency log机构是: %s,地址:%s, 公钥哈希:%s", logInfo.Description,logInfo.Client.BaseURI(), base64.StdEncoding.EncodeToString(sct.LogID.KeyID[:]))err = logInfo.VerifySCTSignature(*sct, *merkleLeaf)if err != nil {log.Printf("Verify SCT failed %v", err)continue}log.Println("Verify SCT offline OK")// 线上验证,非必须if _, err := logInfo.VerifyInclusionLatest(ctx, *merkleLeaf, sct.Timestamp); err != nil {sth := logInfo.LastSTH()if sth != nil {delta := time.Duration(sth.Timestamp-sct.Timestamp) * time.Millisecondif delta < logInfo.MMD {// 如果生效时间(logInfo.MMD)还未到,那么机构查询不到该sct的merkle tree leaf信息是正常的log.Printf("SCT's MMD has not passed %d -> %d < %v", sct.Timestamp, sth.Timestamp, logInfo.MMD)continue}}log.Printf("Failed to verify SCT online: %v", err)} else {log.Println("Verify SCT online OK")}}

每个SCT分别是不同log机构签发的,如果一个证书附带的两个SCT是由同一个log机构签发,或者SCT列表涉及的log机构合计不足3个,浏览器似乎也会报错;
目前,证书检查通过后,chrome的F12里才显示解析的SCT列表;ERR_CERTIFICATE_TRANSPARENCY_REQUIRED错误发生时,需要使用这个程序来查看SCT列表里是否存在重复注册、注册的log机构数量不足,甚至是否无法通过SCT签名检查

这篇关于certificate-transparency-go用例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/639744

相关文章

Go Playground 在线编程环境

For all examples in this and the next chapter, we will use Go Playground. Go Playground represents a web service that can run programs written in Go. It can be opened in a web browser using the follow

go基础知识归纳总结

无缓冲的 channel 和有缓冲的 channel 的区别? 在 Go 语言中,channel 是用来在 goroutines 之间传递数据的主要机制。它们有两种类型:无缓冲的 channel 和有缓冲的 channel。 无缓冲的 channel 行为:无缓冲的 channel 是一种同步的通信方式,发送和接收必须同时发生。如果一个 goroutine 试图通过无缓冲 channel

如何确定 Go 语言中 HTTP 连接池的最佳参数?

确定 Go 语言中 HTTP 连接池的最佳参数可以通过以下几种方式: 一、分析应用场景和需求 并发请求量: 确定应用程序在特定时间段内可能同时发起的 HTTP 请求数量。如果并发请求量很高,需要设置较大的连接池参数以满足需求。例如,对于一个高并发的 Web 服务,可能同时有数百个请求在处理,此时需要较大的连接池大小。可以通过压力测试工具模拟高并发场景,观察系统在不同并发请求下的性能表现,从而

【Go】go连接clickhouse使用TCP协议

离开你是傻是对是错 是看破是软弱 这结果是爱是恨或者是什么 如果是种解脱 怎么会还有眷恋在我心窝 那么爱你为什么                      🎵 黄品源/莫文蔚《那么爱你为什么》 package mainimport ("context""fmt""log""time""github.com/ClickHouse/clickhouse-go/v2")func main(

Go Select的实现

select语法总结 select对应的每个case如果有已经准备好的case 则进行chan读写操作;若没有则执行defualt语句;若都没有则阻塞当前goroutine,直到某个chan准备好可读或可写,完成对应的case后退出。 Select的内存布局 了解chanel的实现后对select的语法有个疑问,select如何实现多路复用的,为什么没有在第一个channel操作时阻塞 从而导

Go Channel的实现

channel作为goroutine间通信和同步的重要途径,是Go runtime层实现CSP并发模型重要的成员。在不理解底层实现时,经常在使用中对channe相关语法的表现感到疑惑,尤其是select case的行为。因此在了解channel的应用前先看一眼channel的实现。 Channel内存布局 channel是go的内置类型,它可以被存储到变量中,可以作为函数的参数或返回值,它在r

Go 数组赋值问题

package mainimport "fmt"type Student struct {Name stringAge int}func main() {data := make(map[string]*Student)list := []Student{{Name:"a",Age:1},{Name:"b",Age:2},{Name:"c",Age:3},}// 错误 都指向了最后一个v// a

Go组合

摘要 golang并非完全面向对象的程序语言,为了实现面向对象的继承这一神奇的功能,golang允许struct间使用匿名引入的方式实现对象属性方法的组合 组合使用注意项 使用匿名引入的方式来组合其他struct 默认优先调用外层方法 可以指定匿名struct以调用内层方法 代码 package mainimport ("fmt")type People struct{}type Pe

Go语言构建单链表

package mainimport "fmt"type ListNode struct {Val intNext *ListNode}func main() {list := []int{2,4,3}head := &ListNode{Val:list[0]}tail := head //需要头尾两个指针for i:=1;i<len(list);i++ {//方法一 数组直接构建链表tai

Go 在orm中使用反射

作为静态语言,golang 稍显笨拙,还好 go 的标准包reflect(反射)包弥补了这点不足,它提供了一系列强大的 API,能够根据执行过程中对象的类型来改变程序控制流。本文将通过设计并实现一个简易的 mysql orm 来学习它,要求读者了解mysql基本知识,并且跟我一样至少已经接触 golang 两到三个月。 orm 这个概念相信同学们都非常熟悉,尤其是写过rails的同学,对acti