登录
首页 >  Golang >  Go问答

如何将私钥添加到证书中

来源:stackoverflow

时间:2024-04-13 10:24:41 278浏览 收藏

对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《如何将私钥添加到证书中》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!

问题内容

我正在尝试使用 go 一体化实用程序创建 csr,发送它,然后(在安全人员批准后)获取签名证书,最后创建证书 + 私有以将其添加到浏览器或系统。

现在我可以做所有事情直到最终剪辑:将私有添加到签名证书中。使用 openssl 我可以通过以下方式做到这一点:

openssl pkcs12 -export -out sergo.kurbanov.p12 -in sergo.kurbanov.crt -inkey sergo.kurbanov.key -name "Sergo Kurbanov"

有人可以建议如何在 go 中做到这一点吗?

附注我正在使用 dogtag 证书系统


解决方案


我找到了决定:不幸的是,标准 go pkcs12 库不包含所需的功能,但 hashicorp 的版本“github.com/hashicorp/packer/builder/azure/pkcs12”包包含所需的功能功能:

// Read our key created by openssl genrsa -out... or by Go 
// rsa.GenerateKey/EncryptPEMBlock...
pemKey,_ := ioutil.ReadFile("private.key")

// Convert pem to rsa key because it required for pkcs12.Encode
var rsaKey *rsa.PrivateKey
rsaKey,_ = dogtag.PemToRSA(pemKey,"our_private_secret")

// Get signed cert from dogtag CMS
var cert *x509.Certificate
cert,_ = dogtag.GetCert("0xF0F05A8")

// Create combined certificate
pfx,_ := pkcs12.Encode(cert.Raw, pemKey, "somesecret")

outFile,_ := os.Create("priv_plus_cert.p12")
defer outFile.Close()
outFile.Write(pfx)

最终我们获得适合添加到钥匙串或浏览器的证书。

本篇关于《如何将私钥添加到证书中》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>