简体   繁体   English

Golang X509 证书如何编组目标信息扩展

[英]Golang X509 certificate how to marshal target information extension

I want to add to X509 certificate id-ce-targetingInformation extension with several strings identifying cert targets.我想向 X509 证书 id-ce-targetingInformation 扩展添加几个标识证书目标的字符串。 This strings represents targets IDs.此字符串表示目标 ID。 I've found OID of extension {2, 5, 29, 55} According to spec it have format below:我发现扩展名 {2, 5, 29, 55} 的 OID 根据规范,它的格式如下:

    targetingInformation EXTENSION ::= {
SYNTAX SEQUENCE SIZE (1..MAX) OF Targets
IDENTIFIED BY id-ce-targetingInformation }
Targets ::= SEQUENCE SIZE (1..MAX) OF Target
Target ::= CHOICE {
targetName [0] GeneralName,
targetGroup [1] GeneralName,
targetCert [2] TargetCert,
}

I add extension in such way我以这种方式添加扩展名

targets := []string{"targetID1", "targetID2"}
asn1Bytes, err := asn1.Marshal(targets)
extraExtensions:=[]pkix.Extension{
            pkix.Extension{
                Id:       asn1.ObjectIdentifier{2, 5, 29, 55},
                Critical: true,
                Value:    asn1Bytes,
            },
        }

But I feel that Value field should be marshaled in another way.但我觉得 Value 字段应该以另一种方式编组。 How properly (according to spec) generate value field of extension?如何正确(根据规范)生成扩展的值字段?

I don't see a lot of documentation about this.我没有看到很多关于此的文档。 But this should help.但这应该会有所帮助。

    oidExtensionExtendedKeyUsage      := []int{2, 5, 29, 37}
    oidExtKeyUsageClientAuth                    := asn1.ObjectIdentifier{1, 3, 6, 1, 5, 5, 7, 3, 2}
    usages := []asn1.ObjectIdentifier{oidExtKeyUsageClientAuth}
    marshalledUsages, err := asn1.Marshal(usages)
    if err != nil {
        fmt.Printf("%v", err)
        return
    }
    template := x509.CertificateRequest{
        Subject:            subj,
        Version:            3,
        SignatureAlgorithm: x509.SHA256WithRSA,
        ExtraExtensions: []pkix.Extension{
            {
                Id: oidExtensionExtendedKeyUsage,
                Critical: false,
                Value:  marshalledUsages,
            },
        },
        IPAddresses: []net.IP {
            net.IPv4(8,8,8,8),
        },
    }

    csrBytes, _ := x509.CreateCertificateRequest(rand.Reader, &template, keyBytes)
    pem.Encode(os.Stdout, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes})

The output output

Certificate Request:
    Data:
        Version: 0 (0x0)
        Subject: C=AU, ST=Some-State, L=MyCity1, O=Company Ltd, OU=IT, CN=example.com/emailAddress=test@example.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (1024 bit)
                Modulus:
                    00:e5:12:aa:05:91:bf:3a:cf:84:33:f1:88:65:85:
                    09:32:b7:8c:ef:47:1e:71:c7:12:2e:d8:02:62:39:
                    01:b7:90:db:66:fd:12:22:55:32:26:fc:4f:a2:e0:
                    10:70:ec:46:51:62:aa:2c:e5:a7:87:61:94:3d:5a:
                    5b:cc:08:b8:0c:c1:ab:98:d9:9b:91:f0:ba:b9:65:
                    57:49:58:8b:25:77:71:3b:5a:53:4e:0f:0e:4a:bc:
                    77:71:6f:82:20:ee:19:25:7a:31:b3:91:aa:32:c7:
                    b8:78:cd:95:8b:d0:0e:32:9e:89:a1:8d:6c:d9:2e:
                    8a:ed:12:d4:c3:2b:25:0b:73
                Exponent: 65537 (0x10001)
        Attributes:
        Requested Extensions:
            X509v3 Subject Alternative Name:
                IP Address:8.8.8.8
            X509v3 Extended Key Usage:
                TLS Web Client Authentication
    Signature Algorithm: sha256WithRSAEncryption
         81:0d:48:4c:e1:d1:59:27:76:53:46:18:93:11:c7:e1:27:0b:
         65:83:72:f5:f4:b6:e0:05:13:6a:80:08:4e:aa:cf:2b:57:69:
         85:73:c6:3f:d5:2a:47:06:16:2d:23:2b:64:2f:b5:7f:87:15:
         9b:23:3a:79:5a:38:98:97:49:a8:38:82:ab:57:e6:69:f0:c8:
         09:49:a0:2e:7a:f6:d7:21:6c:9c:20:f9:8d:88:8e:5c:30:62:
         9b:1b:a2:33:eb:a9:01:a7:de:b0:f0:1d:1a:cb:6b:99:93:4b:
         dd:31:66:61:87:aa:07:4a:88:fa:f3:04:36:33:ec:0e:1c:7a:
         41:99

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以人类可读的格式解码X509证书-Golang - Decoding a X509 Certificate in Human readable format - Golang Golang HTTP x509:由未知权限签名的证书错误 - Golang HTTP x509: certificate signed by unknown authority error 如何在golang中解码x509证书中的复合自定义扩展? 当前代码错误是:asn1:结构错误:序列标签不匹配 - How to Decode composite custom extensions in x509 certificate in golang? With current code Error is : asn1: structure error: sequence tag mismatch 如何在Go中从http客户端获取x509证书 - How to get x509 Certificate from http Client in Go 如何在 gocloak 中跳过(X509)证书验证? - How to skip (X509) certificate validation in gocloak? Golang:使用与指定公钥对应的私钥验证x509证书是否已签名 - Golang: verify x509 certificate was signed using private key that corresponds to the specified public key 使用 Golang 从 Docker 连接到雪花数据库时出现错误“x509:证书由未知机构签名” - Error "x509: certificate signed by unknown authority" while connecting to Snowflake Database from Docker Using Golang 由于 x509 证书依赖于旧的 Common Name 字段,因此无法使用 Golang 连接到服务器 - Failed to connect to a server with Golang due x509 certificate relies on legacy Common Name field 在 Go 中解析 X509 证书 - Parsing X509 certificate in Go 由未知授权机构签署的x509证书 - x509 certificate signed by unknown authority
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM