英文:
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},
},
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论