如何在 GO 中创建带有扩展名和属性值的证书签名请求?
时间:2024-03-04
浏览:381
作者:佚名
“纵有疾风来,人生不言弃”,这句话送给正在学习Golang的朋友们,也希望在阅读本文《如何在 GO 中创建带有扩展名和属性值的证书签名请求?》后,能够真的帮助到大家。我也会在后续的文章中,陆续更新Golang相关的技术文章,有好的建议欢迎大家在评论留言,非常感谢!
我正在尝试使用 crypto/x509 包创建证书请求 (csr),但我不知道如何添加扩展和属性参数。
在certificaterequest结构中我们可以看到扩展是pkix.extension类型。这是pki的结构。扩展名:
type extension struct {
id asn1.objectidentifier
critical bool `asn1:"optional"`
value []byte
}
在代码中搜索,我在 https://golang.org/src/crypto/x509/x509.go 中发现了以下常量:
var (
oidExtKeyUsageAny = asn1.ObjectIdentifier{2, 5, 29, 37, 0}
oidExtKeyUsageServerAuth = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 1}
oidExtKeyUsageClientAuth = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 2}
oidExtKeyUsageCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 3}
oidExtKeyUsageEmailProtection = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 4}
oidExtKeyUsageIPSECEndSystem = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 5}
oidExtKeyUsageIPSECTunnel = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 6}
oidExtKeyUsageIPSECUser = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 7}
oidExtKeyUsageTimeStamping = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 8}
oidExtKeyUsageOCSPSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 9}
oidExtKeyUsageMicrosoftServerGatedCrypto = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 10, 3, 3}
oidExtKeyUsageNetscapeServerGatedCrypto = asn1.ObjectIdentifier{2, 16, 840, 1, 113730, 4, 1}
oidExtKeyUsageMicrosoftCommercialCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 2, 1, 22}
oidExtKeyUsageMicrosoftKernelCodeSigning = asn1.ObjectIdentifier{1, 3, 6, 1, 4, 1, 311, 61, 1, 1}
)
好的,现在我有了参数的 id。就我而言,我想将 keyusage 扩展设置为 serverauth。我有 id,但值是多少?
我不知道我的方法是否正确。有人可以帮我解决这个问题吗?
解决方案
属性字段被视为已弃用,并且无法正常工作。
当我必须向 csr 添加属性时,我复制到 x509.createcertificaterequest 所需功能的内部文件,并修复了 createcertificaterequest,并在编组certificaterequest 的 tbscsr 字段之前以原始格式添加我的属性。注意:certificaterequest 结构不是 x509.certifiacterequest,该结构用于封送。以下是passwordchallengeattribute 的代码示例:
var oidchallengepassword = asn1.objectidentifier{1, 2, 840, 113549, 1, 9, 7}
type passwordchallengeattribute struct {
type asn1.objectidentifier
value []string `asn1:"set"`
}
passwordattribute := passwordchallengeattribute{
type: oidchallengepassword,
value: []string{challenge},
}
b, err := asn1.marshal(passwordattribute)
if err != nil {
//
}
var rawattribute asn1.rawvalue
asn1.unmarshal(b, &rawattribute)
tbscsr.rawattributes = append(tbscsr.rawattributes, rawattribute)
但是使用扩展更好并且受支持,因此您只需要实现表示所需扩展的数据结构,将其编组并添加到扩展列表中。例如,添加basicconstraints扩展:
type basicConstraints struct {
IsCA bool `asn1:"optional"`
MaxPathLen int `asn1:"optional,default:-1"`
}
var extensions []pkix.Extension
basicCon := basicConstraints{IsCA: true, MaxPathLen: -1}
bitstr, err := asn1.Marshal(basicCon)
if err != nil {
//
}
var oidExtensionBasicConstraints = []int{2, 5, 29, 19} //export from x509 package
bcExt := pkix.Extension{Id: oidExtensionBasicConstraints, Value: bitstr}
extensions = append(extensions, bcExt)
csrTmpl := &x509.CertificateRequest{
Extensions: extensions,
}终于介绍完啦!小伙伴们,这篇关于《如何在 GO 中创建带有扩展名和属性值的证书签名请求?》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
站内热门
最新文章
热门专题
