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: weird error handling for parseBitString #29908

Closed
gypsyfeng opened this issue Jan 23, 2019 · 2 comments
Closed

encoding/asn1: weird error handling for parseBitString #29908

gypsyfeng opened this issue Jan 23, 2019 · 2 comments
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@gypsyfeng
Copy link

https://github.com/golang/go/blob/3813edf26edb78620632dc9c7d66096e5b2b5019/src/encoding/asn1/asn1.go

For the following code, error handling "invalid padding bits in BIT STRING" is confusing and doesn't look right. Many bitmap is just one byte long and definitely bigger than 0. Maybe I understand asn1's bit string wrong.

func parseBitString(bytes []byte) (ret BitString, err error) {
	if len(bytes) == 0 {
		err = SyntaxError{"zero length BIT STRING"}
		return
	}
	paddingBits := int(bytes[0])
	if paddingBits > 7 ||
		len(bytes) == 1 && paddingBits > 0 ||
		bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
		err = SyntaxError{"invalid padding bits in BIT STRING"}
		return
	}
	ret.BitLength = (len(bytes)-1)*8 - paddingBits
	ret.Bytes = bytes[1:]
	return
}
@bcmills
Copy link
Contributor

bcmills commented Jan 29, 2019

CC @FiloSottile @agl for encoding/asn1.

Please provide more detail: do you have an example of a specific input that produces an incorrect or misleading error?

Per the description at http://luca.ntop.org/Teaching/Appunti/asn1.html:

In a primitive encoding, the first contents octet gives the number of bits by which the length of the bit string is less than the next multiple of eight (this is called the "number of unused bits"). The second and following contents octets give the value of the bit string, converted to an octet string.

The condition in the code rejects:

  • paddingBits > 7 (a number of bits that exceeds an octet)
  • len(bytes) == 1 && paddingBits > 0 (a nonzero number of bits when no bits remain in the string)
  • bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0, padding bits filled with a value other than 0.

Which of those “doesn't look right”, or what's missing?

@bcmills bcmills added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Jan 29, 2019
@bcmills bcmills changed the title asn1 package: wired error handling for parseBitString encoding/asn1: weird error handling for parseBitString Jan 29, 2019
@gopherbot
Copy link

Timed out in state WaitingForInfo. Closing.

(I am just a bot, though. Please speak up if this is a mistake or you have the requested information.)

@golang golang locked and limited conversation to collaborators Feb 29, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

3 participants