我是使用 Go 的新手,我正在尝试使用以下代码生成 X.509 证书:
cert, err := x509.CreateCertificate(
random,
&certTemplate,
cert,
publicKey,
privateKey
)
其中 publicKey 变量的类型为 interface{},它是调用 x509.ParsePKIXPublicKey(bytes) 的结果。
我遇到的错误是:
x509:仅支持 RSA 和 ECDSA 公钥
我得出的结论是,这是将输入为 interface{} 的 publicKey 传递给 x509.CreateCertificate 函数的结果,因为它与该函数内的类型开关不匹配。我尝试以相同的结果传递 &publicKey。我还尝试使用 publicKey 进行类型断言,如下所示:
var pk *ecdsa.PublicKey
pk = publicKey.(*ecdsa.PublicKey)
cert, err := x509.CreateCertificate(
random,
&certTemplate,
cert,
pk,
privateKey
)
但后来我收到此错误:panic: interface conversion: interface {} is nil, not *ecdsa.PublicKey如果我将 &pk 作为参数传递,则会发生同样的错误。
这就是我生成公钥的方式:
// Generate key pair
curve := elliptic.P384()
privateKey := new(ecdsa.PrivateKey)
privateKey, err := ecdsa.GenerateKey(curve, rand.Reader)
publicKey := &privatekey.PublicKey
// Obtain bytes from public key for communication using protobuf
publicKeyBytes, err := x509.MarshalPKIXPublicKey(publicKey)
// Protobuf communication takes place here
receivedPublicKey, err := x509.ParsePKIXPublicKey(publicKeyBytes)
// verification of public key here (the verification ends successfully)
// populate X.509 template
// create certificate
certificate, err := x509.CreateCertificate(
random,
&certificateTemplate,
certificate,
receivedPublicKey,
privateKey,
)
如果有人能指出我正确的方向或知道如何解决这个问题,那将非常有帮助。提前致谢!
MMTTMM
繁花如伊
相关分类