1 回答
TA贡献1775条经验 获得超8个赞
您可以使用类似 的库google/certtostore
,它是一个多平台包,允许您在 Linux 上使用 x509 证书,在 Windows 上使用证书存储。
它不直接获取证书包,而是使用WindowscertGetCertificateChain
调用,该调用从最终证书开始构建证书链上下文,并在可能的情况下返回到受信任的根 CA。
它由 所使用CertWithContext()
,它使用创建时提供的颁发者值执行证书查找WinCertStore
。
它返回证书及其 Windows 上下文,可用于执行其他操作,例如使用 查找私钥CertKey()
。
无效的内存地址或 nil 指针取消引用
你需要初始化var cert certmgr
更一般地说,您需要先获取商店,如本例所示:
fmt.Println("open cert store")
// Open the local cert store. Provider generally shouldn't matter, so use Software which is ubiquitous. See comments in getHostKey.
store, err := certtostore.OpenWinCertStore(certtostore.ProviderMSSoftware, "", []string{"localhost"}, nil, false)
if err != nil {
fmt.Errorf("OpenWinCertStore: %v", err)
return
}
fmt.Println("get cert from cert store")
// Obtain the first cert matching all of container/issuers/intermediates in the store.
// This function is indifferent to the provider the store was opened with, as the store lists certs
// from all providers.
crt, context, err := store.CertWithContext()
if err != nil {
fmt.Println("failed to get cert from cert store. ", err)
return
}
if crt == nil {
fmt.Println("no cert")
return
}
fmt.Println("get key from cert")
// Obtain the private key from the cert. This *should* work regardless of provider because
// the key is directly linked to the certificate.
key, err := store.CertKey(context)
if err != nil {
fmt.Printf("private key not found in %s, %s", store.ProvName, err)
return
}
if key == nil {
fmt.Println("no key")
return
}
fmt.Printf("find cert '%s' with private key in container '%s', algo '%s'\n", crt.Subject, key.Container, key.AlgorithmGroup)
- 1 回答
- 0 关注
- 196 浏览
添加回答
举报