如何使用 OID 读取扩展值,例如键信息和键签名。

huangapple go评论90阅读模式
英文:

How to read extension values like key info and key signature with OID

问题

var cert *x509.Certificate
cert, err := x509.ParseCertificate(myCertificate)

从这段代码中,我需要获取扩展值的字节。我有要使用的OID。我能找到添加扩展到证书的代码,但是没有找到获取扩展的代码。有人可以分享一段示例代码吗?

更新:

现在,我能够获取扩展了

var extensions []pkix.Extension
var cert *x509.Certificate
byteValue, _ := base64.StdEncoding.DecodeString(myCert)
cert, err := x509.ParseCertificate(byteValue)
if err != nil {
fmt.Println("解析证书时出错")
}
extensions = cert.Extensions
checkId := []int{2, 5, 6, 135, 3, 2, 41}
for _, ext := range extensions {
if reflect.DeepEqual(ext.Id, checkId) {
fmt.Println("true")
}
}
}

但是深度比较返回false(特定的OID存在),我需要获取该特定扩展的值。有人可以提供一些步骤吗?

英文:
var cert *x509.Certificate
cert, err := x509.ParseCertificate(myCertificate)

From this I need to retrieve extension values in bytes. I have the OID to be used for that. I am able to find code for adding extensions to a certificate. But not retrieving it. Can anyone share a sample code on how to do that?

Update:

Now, I am able to get the extensions

var extensions []pkix.Extension
var cert *x509.Certificate
byteValue, _ := base64.StdEncoding.DecodeString(myCert)
cert, err := x509.ParseCertificate(byteValue)
if err != nil {
	fmt.Println("Error in parsing certificate")
}
extensions = cert.Extensions
checkId := []int{2, 5, 6, 135, 3, 2, 41}
for _, ext := range extensions {
	if reflect.DeepEqual(ext.Id, checkId) {
		fmt.Println("true")
	}
}

}

But the deep equal return false (the particular oid is present), I need to retrieve the value of that particular extension. Can anyone suggest some steps to do that?

答案1

得分: 1

reflect.DeepEqual不起作用,因为类型不同。检查OID的常规方法是使用asn1.ObjectIdentifier,代码如下:

checkID := asn1.ObjectIdentifier{2, 5, 6, 135, 3, 2, 41}
for _, ext := range extensions {
    if ext.Id.Equal(checkId) {
        fmt.Println("true")
    }
}

如果不存在,请不要忘记检查ExtraExtensions

英文:

reflect.DeepEqual does not work because the types are different. The normal way to check OIDs is using the asn1.ObjectIdentifier as follows:

checkID := asn1.ObjectIdentifier{2, 5, 6, 135, 3, 2, 41}
for _, ext := range extensions {
    if ext.Id.Equal(checkId) {
        fmt.Println("true")
    }
}

If it isn't there, don't forget to check ExtraExtensions.

huangapple
  • 本文由 发表于 2020年8月29日 13:05:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63643631.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定