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 · 6 comments
Open

proposal: encoding/asn1: expose parseTagAndLength #28872

barlevd opened this issue Nov 19, 2018 · 6 comments
Labels
Proposal Proposal-Crypto Proposal related to crypto packages or other security issues
Milestone

Comments

@barlevd
Copy link

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
@barlevd barlevd changed the title proposal: encoing/asn1: expose parseTagAndLength proposal: encoding/asn1: expose parseTagAndLength Nov 19, 2018
@barlevd
Copy link
Author

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
}

@rsc
Copy link
Contributor

rsc commented Nov 28, 2018

/cc @agl @FiloSottile

@andybons andybons added the Proposal-Crypto Proposal related to crypto packages or other security issues label Dec 5, 2018
@andybons
Copy link
Member

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.

@FiloSottile
Copy link
Contributor

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

I feel like this might be a better fit in golang.org/x/crypto/cryptobyte. Maybe String.PeekASN1() (tag asn1.Tag, length int)?

@yaozongyou
Copy link

yaozongyou commented Nov 27, 2020

I encountered the same issue, we are communicating message encoded using asn over tcp connection, and for receiving message, we need to know the message length to determine the message boundary. My suggestion is make https://github.com/golang/go/blob/master/src/encoding/asn1/asn1.go#L527 this parseTagAndLength func public, so our code could goes like this:

func ReadFull(conn net.Conn) ([]byte, error) {
        result := bytes.NewBuffer(nil)
        var buf [512]byte
        for {
                n, err := conn.Read(buf[0:])
                result.Write(buf[0:n])
                if err != nil {
                        if err == io.EOF {
                                break
                        }
                        return nil, err
                }

                tagAndLength, offset, err := asn1.ParseTagAndLength(result.Bytes(), 0)
                if err == nil {
                        if (result.Len() - offset) >= tagAndLength.length {
                                break
                        }
                }
        }
        return result.Bytes(), nil
}

@yaozongyou
Copy link

ping @agl @FiloSottile

@ianlancetaylor ianlancetaylor added this to Incoming in Proposals (old) Feb 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Proposal Proposal-Crypto Proposal related to crypto packages or other security issues
Projects
Status: Incoming
Development

No branches or pull requests

6 participants