Golang 1.23 后量子加密安全的TLS实现探讨

Golang 1.23 后量子加密安全的TLS实现探讨 我正在尝试根据这篇Medium文章搭建一个使用PQC的TLS服务器示例 - Go开发者,为量子安全TLS做好准备 | 作者:Gil Adda | CyberArk Engineering | 2024年7月 | Medium

使用下面的代码通过Chrome或curl连接时,我原本希望能看到它提及x25519Kyber768Draft00,但它只显示:

2024/09/02 09:06:34 TLS cipher suite: TLS_AES_128_GCM_SHA256, TLS 1.3

我哪里做错了?

package main

import (
	"crypto/tls"
	"io"
	"log"
	"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {

	connState := r.TLS
	if connState != nil {
		cipherSuiteName := tls.CipherSuiteName(connState.CipherSuite)
		tlsVersionName := tls.VersionName(connState.Version)
		log.Printf("TLS cipher suite: %s, %s", cipherSuiteName, tlsVersionName)

	}

	io.WriteString(w, "Hello, world!\n")
}

func main() {
	http.HandleFunc("/hello", helloHandler)

	serverConfig := &tls.Config{
		MinVersion: uint16(tls.VersionTLS13),
		MaxVersion: uint16(tls.VersionTLS13),
	}

	server := &http.Server{
		Addr:      ":8443",
		TLSConfig: serverConfig,
	}

	log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}

更多关于Golang 1.23 后量子加密安全的TLS实现探讨的实战教程也可以访问 https://www.itying.com/category-94-b0.html

3 回复

一篇包含更多示例的新文章在这里: https://medium.com/cyberark-engineering/a-post-quantum-cryptography-web-server-in-go-1-23-9f7e98db7b39

GitHub仓库预览图

GitHub - GilAddaCyberark/golang-pqc-examples

为 GilAddaCyberark/golang-pqc-examples 的开发做出贡献,请在 GitHub 上创建一个账户。

更多关于Golang 1.23 后量子加密安全的TLS实现探讨的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


哦,实际上在调试器中查看,我发现 curvid 使用的是 x25519Kyber768Draft00。太棒了!抱歉造成了干扰。

image

更新代码以访问该私有字段:

package main

import (
	"crypto/tls"
	"io"
	"log"
	"net/http"
	"reflect"
	"unsafe"
)

func curveID(conn *tls.ConnectionState) string {
	value := reflect.ValueOf(conn).Elem().FieldByName("testingOnlyCurveID")

	if !value.IsValid() {
		return "???"
	}

	value = reflect.NewAt(value.Type(), unsafe.Pointer(value.UnsafeAddr())).Elem()
	return value.Interface().(tls.CurveID).String()
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
	connState := r.TLS
	if connState != nil {
		cipherSuiteName := tls.CipherSuiteName(connState.CipherSuite)
		tlsVersionName := tls.VersionName(connState.Version)
		curveName := curveID(connState)
		log.Printf("TLS cipher suite: %s, %s, %s", cipherSuiteName, tlsVersionName, curveName)

	}

	io.WriteString(w, "Hello, world!\n")
}

func main() {
	http.HandleFunc("/hello", helloHandler)

	serverConfig := &tls.Config{
		MinVersion: uint16(tls.VersionTLS13),
		MaxVersion: uint16(tls.VersionTLS13),
	}

	server := &http.Server{
		Addr:      ":8443",
		TLSConfig: serverConfig,
	}

	log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}

你的代码没有启用后量子加密(PQC)密钥交换算法。在Go 1.23中,需要显式配置TLS配置的CurvePreferences来包含PQC曲线。以下是修改后的代码:

package main

import (
	"crypto/tls"
	"io"
	"log"
	"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
	connState := r.TLS
	if connState != nil {
		cipherSuiteName := tls.CipherSuiteName(connState.CipherSuite)
		tlsVersionName := tls.VersionName(connState.Version)
		log.Printf("TLS cipher suite: %s, %s", cipherSuiteName, tlsVersionName)
		
		// 打印协商的曲线
		if len(connState.VerifiedChains) > 0 {
			log.Printf("Negotiated curve: %v", connState.VerifiedChains[0][0].PublicKey)
		}
	}
	io.WriteString(w, "Hello, world!\n")
}

func main() {
	http.HandleFunc("/hello", helloHandler)

	serverConfig := &tls.Config{
		MinVersion:       tls.VersionTLS13,
		MaxVersion:       tls.VersionTLS13,
		CurvePreferences: []tls.CurveID{
			tls.X25519Kyber768Draft00,  // PQC混合密钥交换
			tls.X25519,                  // 传统曲线作为备选
		},
		CipherSuites: []uint16{
			tls.TLS_AES_128_GCM_SHA256,
			tls.TLS_AES_256_GCM_SHA384,
		},
	}

	server := &http.Server{
		Addr:      ":8443",
		TLSConfig: serverConfig,
	}

	log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}

关键修改:

  1. 添加了CurvePreferences字段,将tls.X25519Kyber768Draft00作为首选曲线
  2. 显式设置了TLS 1.3的密码套件
  3. 在handler中添加了曲线协商的日志输出

要验证PQC是否生效,可以使用以下curl命令测试:

curl -v --tlsv1.3 --tls-max 1.3 --cipher 'TLS_AES_128_GCM_SHA256' https://localhost:8443/hello

或者使用Go客户端测试:

package main

import (
	"crypto/tls"
	"fmt"
	"io"
	"log"
	"net/http"
)

func main() {
	client := &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				MinVersion: tls.VersionTLS13,
				MaxVersion: tls.VersionTLS13,
				CurvePreferences: []tls.CurveID{
					tls.X25519Kyber768Draft00,
				},
				CipherSuites: []uint16{
					tls.TLS_AES_128_GCM_SHA256,
				},
			},
		},
	}

	resp, err := client.Get("https://localhost:8443/hello")
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	body, _ := io.ReadAll(resp.Body)
	fmt.Println(string(body))
}

注意:tls.X25519Kyber768Draft00是Go 1.23中引入的实验性PQC曲线,需要确保你的Go版本至少是1.23,并且编译时启用了相关特性。

回到顶部