Search code examples
gox509certificatex509asn.1

Golang x509 certificate has OID values AS PRINTABLESTRING instead of UTF8STRING


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?


Solution

  • 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},
        },
    }