我有一个通过 OpenSSL 在服务器上创建 PKCS12 存储的脚本。我正在 Go 中开发 TLS/SSL 服务器,需要从创建的 PKCS12 存储加载密钥对。如何从 PCKS12 存储中提取密钥对并加载它们?
这是我的代码片段,它给了我一个错误:
src/server.go:59:19: cannot use cert (type interface {}) as type []byte in argument to ioutil.WriteFile: need type assertion
src/server.go:60:19: cannot use key (type *x509.Certificate) as type []byte in argument to ioutil.WriteFile
import "golang.org/x/crypto/pkcs12"
// Read byte data from pkcs12 keystore
p12_data, err := ioutil.ReadFile("../identify.p12")
if err != nil {
log.Fatal(err)
}
// Extract cert and key from pkcs keystore
cert, key, err := pkcs12.Decode(p12_data, "123456")
if err != nil {
log.Println(err)
return
}
//Write cert and key out to filepath
ioutil.WriteFile("cert.pem", cert, 777)
ioutil.WriteFile("key.pem", key, 777)
log.SetFlags(log.Lshortfile)
cer, err := tls.LoadX509KeyPair("cert.pem", "key.pem")
if err != nil {
log.Println(err)
return
}
智慧大石
相关分类