Golang x509证书的OID值是以PRINTABLESTRING而不是UTF8STRING形式表示的。

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

Golang x509 certificate has OID values AS PRINTABLESTRING instead of UTF8STRING

问题

go版本 go1.18.4,darwin/arm64

我正在创建x509证书。

当我使用以下命令解析生成的pem证书时:

asn1parse -in

我可以看到通用名称(common name)显示为PRINTABLESTRING而不是UTF8STRING。

31:d=4 hl=2 l= 13 cons: SEQUENCE
33:d=5 hl=2 l= 3 prim: OBJECT :commonName
38:d=5 hl=2 l= 6 prim: PRINTABLESTRING :golang

据我了解,在golang中,字符串默认为utf8,但证书仍然显示为PRINTABLESTRING。

代码可在GO play url中找到。

另外,当我输入一些字符,例如é um,我可以看到它显示为UTF8STRING。

是否有办法将OID值设置为UTF8STRING而不是PRINTABLESTRING?

英文:

go version go1.18.4, darwin/arm64

I am creating x509 certificate.

when I parse the generated pem certificate using

asn1parse -in

I can see the common name as PRINTABLESTRING instead of UTF8STRING.

31:d=4 hl=2 l= 13 cons: SEQUENCE
33:d=5 hl=2 l= 3 prim: OBJECT :commonName
38:d=5 hl=2 l= 6 prim: PRINTABLESTRING :golang

as I understand, in golang the string is default utf8 but still certificate shows it as PRINTABLESTRING

The code is available at GO play url

Also,
when I put some characters like é um then I can see it as UTF8STRING.

Is there a way to put OID values as UTF8STRING instead of PRINTABLESTRING?

答案1

得分: 0

Go的asn1包只会在字符串需要时使用UTF8STRING(参见这里)。这本身没有任何问题,允许库自行选择似乎是可以的。

不过,如果你真的想要使用UTF8STRING,你可以尝试使用asn1.RawValue来实现:

name, err := asn1.MarshalWithParams("golang", "utf8")
if err != nil {
    // 处理错误
}

pkixAttrTypeValue := []pkix.AttributeTypeAndValue{
    {
        Type:  COMMON_NAME,
        Value: asn1.RawValue{FullBytes: name},
    },
}
英文:

The Go asn1 package will only use UTF8STRING if the string requires it. There is nothing inherently wrong with this, and allowing the library to make this choice seems fine.

That said, if you really want to have UTF8STRING used, you may be able to use asn1.RawValue to achieve this:

name, err := asn1.MarshalWithParams("golang", "utf8")
if err != nil {
	// handle error
}

pkixAttrTypeValue := []pkix.AttributeTypeAndValue{
	{
		Type:  COMMON_NAME,
		Value: asn1.RawValue{FullBytes: name},
	},
}

huangapple
  • 本文由 发表于 2022年11月21日 13:09:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/74514476.html
匿名

发表评论

匿名网友

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

确定