Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/asn1: string type tags are not documented to work in Unmarshal #42570

Open
mjg59 opened this issue Nov 12, 2020 · 4 comments
Open

encoding/asn1: string type tags are not documented to work in Unmarshal #42570

mjg59 opened this issue Nov 12, 2020 · 4 comments

Comments

@mjg59
Copy link

@mjg59 mjg59 commented Nov 12, 2020

What version of Go are you using (go version)?

1.14

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

linux amd64

What did you do?

I have the following struct:

type ComponentIdentifierV2 struct {
        ComponentClass           ComponentClass
        ComponentManufacturer    string
        ComponentModel           string
        ComponentSerial          string                `asn1:"optional,tag:0"`
        ComponentRevision        string                `asn1:"optional,tag:1"`
        ComponentManufacturerID  int                   `asn1:"optional,tag:2"`
        FieldReplaceable         bool                  `asn1:"optional,tag:3"`
        ComponentAddresses       []ComponentAddress    `asn1:"optional,tag:4"`
        ComponentPlatformCert    CertificateIdentifier `asn1:"optional,tag:5"`
        ComponentPlatformCertURI string                `asn1:"optional,tag:6"`
        Status                   asn1.Enumerated       `asn1:"optional,tag:7"`
}

Note that ComponentRevision is tag:1, so context specific. The fact that it's a string means that getUniversalType() will return PrintableString, and this will not be overridden because there's no explicit tag type in the encoded stream.

The spec definition of this struct is:

ComponentIdentifier ::= SEQUENCE {
componentClass ComponentClass,
componentManufacturer UTF8String (SIZE (1..STRMAX)),
componentModel UTF8String (SIZE (1..STRMAX)),
componentSerial[0] IMPLICIT UTF8String (SIZE (1..STRMAX)) OPTIONAL,
componentRevision [1] IMPLICIT UTF8String (SIZE (1..STRMAX)) OPTIONAL,
componentManufacturerId [2] IMPLICIT PrivateEnterpriseNumber OPTIONAL,
fieldReplaceable [3] IMPLICIT BOOLEAN OPTIONAL,
componentAddresses [4] IMPLICIT SEQUENCE(SIZE(1.. MAX)) OF ComponentAddress OPTIONAL
componentPlatformCert [5] IMPLICIT CertificateIdentifier OPTIONAL,
componentPlatformCertUri [6] IMPLICIT URIReference 
}

ie, componentRevision is a UTF8String, not a PrintableString. The certificate I'm parsing has an @ sign in, which is legitimate in a UTF8String but not a PrintableString. Since we have no explicit information in the stream to treat this as a UTF8String we continue treating it as a PrintableString, and then fail because of the "invalid" character.

What did you expect to see?

A string containing an @ sign.

What did you see instead?

asn1: syntax error: PrintableString contains invalid character

@mjg59
Copy link
Author

@mjg59 mjg59 commented Nov 12, 2020

This actually works if I do:

asn1:"utf8,optional,tag:1"

which is documented as doing something in Marshal() but not in Unmarshal().

@mjg59 mjg59 changed the title encoding/asn1 doesn't let me specify the expected type of a string encoding/asn1 doesn't document string types in unmarshal() Nov 12, 2020
@FiloSottile FiloSottile added this to the Go1.16 milestone Nov 12, 2020
@FiloSottile FiloSottile changed the title encoding/asn1 doesn't document string types in unmarshal() encoding/asn1: string type tags are not documented to work in Unmarshal Nov 12, 2020
@ericchiang
Copy link

@ericchiang ericchiang commented Nov 12, 2020

I can take the documentation fix

imxyb added a commit to imxyb/go that referenced this issue Nov 13, 2020
encoding/ans1: add utf8 tag doc for Unmarshal method.
imxyb added a commit to imxyb/go that referenced this issue Nov 13, 2020
encoding/asn1: add utf8 tag doc for Unmarshal method.
@gopherbot
Copy link

@gopherbot gopherbot commented Nov 13, 2020

Change https://golang.org/cl/269777 mentions this issue: encoding/asn1: add utf8 tag doc for Unmarshal method.

@gopherbot
Copy link

@gopherbot gopherbot commented Nov 13, 2020

Change https://golang.org/cl/269798 mentions this issue: encoding/asn1: document unmarshaling behavior for IMPLICIT string fields

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants
You can’t perform that action at this time.