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

proposal: encoding/asn1: expose parseTagAndLength #28872

barlevd opened this issue Nov 19, 2018 · 4 comments

proposal: encoding/asn1: expose parseTagAndLength #28872

barlevd opened this issue Nov 19, 2018 · 4 comments


Copy link

@barlevd barlevd commented Nov 19, 2018

When working with ldap over the network, the reader does not know in advance the size of the request (the size of the buffer to read).
To know that the full request was read you need to parse the tag and length. This is just about what the function parseTagAndLength does. You only need to add the offset to the length.
To expose this function I suggest adding a public ParseTagAndLength that exposes this functionality and calculates the total expected buffer length.

usage example:

func readRequest(conn net.Conn, readBuf []byte) ([]byte, error) {
   const minReadBuf = 1024
   var err error
   readBuf = readBuf[0:cap(readBuf)]

   var nRead = 0
   var tagSize = 1

   for nRead < tagSize {
      nBufStart := nRead
      if (len(readBuf) - nBufStart) < minReadBuf {
         readBuf = append(readBuf, make([]byte, minReadBuf)...)

      nRead, err = conn.Read(readBuf[nBufStart:])
      // no request: return
      if err != nil || nRead == 0 {
         return readBuf[0:nBufStart], err

      nRead += nBufStart
      var rawVal asn1.RawValue
      rawVal, tagSize, err = asn1.ParseTagAndLength(readBuf[0:nRead])

   return readBuf[0:nRead], err
@gopherbot gopherbot added this to the Proposal milestone Nov 19, 2018
@gopherbot gopherbot added the Proposal label Nov 19, 2018
@barlevd barlevd changed the title proposal: encoing/asn1: expose parseTagAndLength proposal: encoding/asn1: expose parseTagAndLength Nov 19, 2018
Copy link

@barlevd barlevd commented Nov 19, 2018

this is the proposed new code:

// ParseTagAndLength : get the tag information
// - used in network i/o as the buffer may not be complete
func ParseTagAndLength(bytesBuf []byte) (rawVal RawValue, length int, err error) {

	// parse the buffer ASN.1 information
	var tagAndLen tagAndLength
	var offset int
	tagAndLen, offset, err = parseTagAndLength(bytesBuf, 0)

	length = tagAndLen.length + offset
	bufLen := length
	if bufLen > len(bytesBuf) {
		bufLen = len(bytesBuf)

	rawVal = RawValue{Class: tagAndLen.class,
		Tag:        tagAndLen.tag,
		IsCompound: tagAndLen.isCompound,
		Bytes:      bytesBuf[offset:bufLen],
		FullBytes:  bytesBuf[:bufLen]}

	return rawVal, length, err
Copy link

@rsc rsc commented Nov 28, 2018

Copy link

@andybons andybons commented Dec 5, 2018

Adding Proposal-Crypto to put this on @agl and @FiloSottile's queue. I’m aware this is used outside of just crypto.

Copy link

@FiloSottile FiloSottile commented Apr 8, 2020

In your example the returned RawValue.Bytes would be partial, right?

I feel like this might be a better fit in Maybe String.PeekASN1() (tag asn1.Tag, length int)?

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

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.