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

Open
barlevd opened this issue Nov 19, 2018 · 3 comments

Comments

@barlevd
Copy link

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

@barlevd

This comment has been minimized.

Copy link
Author

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
}
@rsc

This comment has been minimized.

Copy link
Contributor

commented Nov 28, 2018

@andybons

This comment has been minimized.

Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.