![](/img/trans.png)
[英]Using certificate and private key from Windows cert store with OpenSSL
[英]Getting error while fetching certificate from windows certificate store in Golang using `certtostore`?
我想使用 Windows 證書商店中的證書包,誰能告訴我我在這里做錯了什么?
我的代碼:
package main
import (
"fmt"
"runtime"
"github.com/google/certtostore"
)
type certmgr struct {
certToStore certtostore.CertStorage
}
func main() {
if runtime.GOOS == "windows" {
var cert certmgr
certInStore, err := cert.certToStore.Cert()
if err != nil {
fmt.Println("message", "Error in getting system store certificate ...")
}
fmt.Println("Windows System Store Certificate", *certInStore)
}
}
我得到的錯誤:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0xbe2dda]
goroutine 1 [running]:
main.main()
C:/Users/prajwal.bhagat/go/src/phoenix/mainsvc/cmd/main/test.go:17 +0x1a
exit status 2
您可以使用像google/certtostore
這樣的庫,它是一個多平台包,允許您在 Linux 上使用 x509 證書和在 Windows 上使用證書存儲。
它不直接獲取證書包,而是使用Windows certGetCertificateChain
調用,它從最終證書開始構建證書鏈上下文,並在可能的情況下返回到 受信任的根 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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.