dongsonghen9931 2019-09-24 14:03
浏览 175

前往https客户端证书

Trying to migrate a working java code with https client certificate authentication to golang but getting tls handshake error

go version go1.12.9 linux/amd64

client certificate details

openssl pkcs12 -info -in p12file.p12 
Enter Import Password:
MAC: sha1, Iteration 2048
MAC length: 20, salt length: 8
PKCS7 Encrypted dat..
Certificate bag
Bag Attributes
    friendlyName: test
    localKeyID:..
subject=CN = *....com
issuer=C = US, O = DigiCert Inc...

-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----

Certificate bag
Bag Attributes: <No Attributes>
subject=C = US, O = DigiCert Inc, OU = www.digicert.com, CN = RapidS...
issuer=C = US, O = DigiCert Inc, OU = www.digicert.com, CN = DigiCert Global Root CA
....

working Java code

url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
//geting 405 status - so its working

able to establish https connection with this arg -Djavax.net.ssl.keyStoreType=pkcs12 -Djavax.net.ssl.keyStore=./p12file.p12 -Djavax.net.ssl.keyStorePassword=password

golang issue (removed error handling part)

fb, err := ioutil.ReadFile(p12file)
b, err := pkcs12.ToPEM(fb, password)

cert, err := tls.X509KeyPair(pem.EncodeToMemory(b[0]), pem.EncodeToMemory(b[2]))
//b[0] and b[1] is CERTIFICATE, b[2] is PRIVATE KEY, with 1&2 getting 'private key does not match public key'

//also tried 
// openssl pkcs12 -in ./p12file.p12 -clcerts -nokeys -out certfile.crt 
// openssl pkcs12 -in ./p12file.p12 -nocerts -nodes -out keyfile.key
// cert, err := tls.LoadX509KeyPair(certFile, keyFile)
tlsConfig := &tls.Config{
    Certificates:       []tls.Certificate{cert},
    InsecureSkipVerify: true,
}
tlsConfig.BuildNameToCertificate()
transport := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: transport}

resp, err := client.Get(url)

getting 'remote error: tls: handshake failure'

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 luckysheet
    • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
    • ¥15 找一位技术过硬的游戏pj程序员
    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误