8 Star 33 Fork 14

Cliven/gotlcp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
main.go 4.30 KB
一键复制 编辑 原始数据 按行查看 历史
Cliven 提交于 2023-02-21 19:39 . 补充了文档
package main
import (
"fmt"
"gitee.com/Trisia/gotlcp/tlcp"
"github.com/emmansun/gmsm/smx509"
)
// 客户端双向身份认证
func main() {
loadCert()
// 构造根证书列表
pool := smx509.NewCertPool()
pool.AddCert(rootCert)
// ECDHE系列套件 同时需要 认证密钥对 与 加密密钥对
config := &tlcp.Config{
RootCAs: pool,
Certificates: []tlcp.Certificate{authCertKeypair, encCertKeypair},
CipherSuites: []uint16{
tlcp.ECDHE_SM4_GCM_SM3,
tlcp.ECDHE_SM4_CBC_SM3,
// 注意:不能出现 ECC 系列套件,否则服务端可能选择ECC系列套件。
},
}
//// 兼容向量模式的密钥交换参数
//config.ClientECDHEParamsAsVector = true
conn, err := tlcp.Dial("tcp", "127.0.0.1:8450", config)
if err != nil {
panic(err)
}
defer conn.Close()
_, err = conn.Write([]byte("Hello Go TLCP!"))
if err != nil {
panic(err)
}
buff := make([]byte, 512)
n, err := conn.Read(buff)
if err != nil {
panic(err)
}
fmt.Printf("<< %s\n", buff[:n])
}
const (
ROOT_CERT_PEM = `-----BEGIN CERTIFICATE-----
MIIB3jCCAYOgAwIBAgIIAs4MAPwpIBcwCgYIKoEcz1UBg3UwQjELMAkGA1UEBhMC
Q04xDzANBgNVBAgMBua1meaxnzEPMA0GA1UEBwwG5p2t5beeMREwDwYDVQQKDAjm
tYvor5VDQTAeFw0yMTEyMjMwODQ4MzNaFw0zMTEyMjMwODQ4MzNaMEIxCzAJBgNV
BAYTAkNOMQ8wDQYDVQQIDAbmtZnmsZ8xDzANBgNVBAcMBuadreW3njERMA8GA1UE
CgwI5rWL6K+VQ0EwWTATBgcqhkjOPQIBBggqgRzPVQGCLQNCAARKs6B5ZBy753Os
ZSeIfv8zScbiiXkL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2VirZMlcZCERQpAMx
8/91/Op17FjF7Dp7
-----END CERTIFICATE-----
`
AUTH_KEY_PEM = `-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgZF5oB3V13GrOsZoE
RPi+m3tksNaWiEGK80lD2UqHaYCgCgYIKoEcz1UBgi2hRANCAAS8uH5ew73CxFMN
+lknVWW0//B5tXfLI+yy0aLIEySw9JcBa3kZlD0LEu6Lwe1jGecuk3FlHdzhEvWg
t4Ep2cM3
-----END PRIVATE KEY-----
`
ENC_CERT_PEM = `-----BEGIN CERTIFICATE-----
MIICGTCCAb6gAwIBAgIIAs64yjcYSQgwCgYIKoEcz1UBg3UwQjELMAkGA1UEBhMC
Q04xDzANBgNVBAgMBua1meaxnzEPMA0GA1UEBwwG5p2t5beeMREwDwYDVQQKDAjm
tYvor5VDQTAeFw0yMzAyMjExMTI1NDlaFw0yNDAyMjExMTI1NDlaMGsxCzAJBgNV
BAYTAkNOMQ8wDQYDVQQIDAbmtZnmsZ8xDzANBgNVBAcMBuadreW3njEPMA0GA1UE
CgwG5rWL6K+VMQ8wDQYDVQQLDAbmtYvor5UxGDAWBgNVBAMMD+a1i+ivleWuouaI
t+errzBZMBMGByqGSM49AgEGCCqBHM9VAYItA0IABCCV8MWmwYY1MC7L23+bplrC
2nxH5RJMR4hh55TItZ5XMfCJceSVtjKPpd6zr2xvA1kb4G5ydHwmGSfRk6OmjDmj
dTBzMA4GA1UdDwEB/wQEAwIDODATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMB
Af8EAjAAMB0GA1UdDgQWBBTqqUJAy1bB0KiYgh8CUh4x3TW/YTAfBgNVHSMEGDAW
gBQ2k+MU50UJ+tXv6i8SLdOhljzCpDAKBggqgRzPVQGDdQNJADBGAiEA57snhYf5
8y/lCX6OOXr1/3H71aY9c54Wpf38WWDRaskCIQCmslF8tguQIhQqS831kmJ9nFf9
TN4y0RkQBH74aBqeQQ==
-----END CERTIFICATE-----
`
ENC_KEY_PEM = `-----BEGIN PRIVATE KEY-----
MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQgkwiV92G2uKq40+gI
b8cMuaSxv2VdCWRAZJ3MOXSO2JSgCgYIKoEcz1UBgi2hRANCAAQglfDFpsGGNTAu
y9t/m6Zawtp8R+USTEeIYeeUyLWeVzHwiXHklbYyj6Xes69sbwNZG+BucnR8Jhkn
0ZOjpow5
-----END PRIVATE KEY-----
`
)
var (
rootCert *smx509.Certificate
authCertKeypair tlcp.Certificate
encCertKeypair tlcp.Certificate
)
func loadCert() {
var err error
rootCert, err = smx509.ParseCertificatePEM([]byte(ROOT_CERT_PEM))
if err != nil {
panic(err)
}
authCertKeypair, err = tlcp.X509KeyPair([]byte(AUTH_CERT_PEM), []byte(AUTH_KEY_PEM))
if err != nil {
panic(err)
}
encCertKeypair, err = tlcp.X509KeyPair([]byte(ENC_CERT_PEM), []byte(ENC_KEY_PEM))
if err != nil {
panic(err)
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/Trisia/gotlcp.git
git@gitee.com:Trisia/gotlcp.git
Trisia
gotlcp
gotlcp
v1.3.15

搜索帮助

0d507c66 1850385 C8b1a773 1850385