【问题标题】:How to create a certificate signing request with extensions and atributes values in GO?如何在 GO 中创建具有扩展名和属性值的证书签名请求?
【发布时间】:2019-07-14 10:13:16
【问题描述】:

我正在尝试使用 crypto/x509 包创建证书请求 (csr),但我不知道如何添加扩展和属性参数。

CertificateRequest struct 中我们可以看到扩展是pkix.Extension 类型。这是pki.Extension的结构:

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 但价值是多少?

我不知道我的方法是否正确。有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: go certificate csr


    【解决方案1】:

    Attributes 字段被认为已弃用且无法正常工作。

    当我必须将属性添加到 csr 时,我将 x509.CreateCertificateRequest 所需的功能复制到内部文件,并通过在编组 certificateRequest 的 tbsCSR 字段之前以原始格式添加我的属性来修复 CreateCertificateRequest。注意:certificateRequest 结构不是 x509.CertfiacteRequest,该结构用于封送处理。下面是 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,
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-21
      • 1970-01-01
      • 2014-11-20
      • 2016-07-21
      • 1970-01-01
      • 2014-10-26
      相关资源
      最近更新 更多