Source file src/encoding/asn1/asn1.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package asn1 implements parsing of DER-encoded ASN.1 data structures,
     6  // as defined in ITU-T Rec X.690.
     7  //
     8  // See also “A Layman's Guide to a Subset of ASN.1, BER, and DER,”
     9  // http://luca.ntop.org/Teaching/Appunti/asn1.html.
    10  package asn1
    11  
    12  // ASN.1 is a syntax for specifying abstract objects and BER, DER, PER, XER etc
    13  // are different encoding formats for those objects. Here, we'll be dealing
    14  // with DER, the Distinguished Encoding Rules. DER is used in X.509 because
    15  // it's fast to parse and, unlike BER, has a unique encoding for every object.
    16  // When calculating hashes over objects, it's important that the resulting
    17  // bytes be the same at both ends and DER removes this margin of error.
    18  //
    19  // ASN.1 is very complex and this package doesn't attempt to implement
    20  // everything by any means.
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"internal/saferio"
    26  	"math"
    27  	"math/big"
    28  	"reflect"
    29  	"slices"
    30  	"strconv"
    31  	"strings"
    32  	"time"
    33  	"unicode/utf16"
    34  	"unicode/utf8"
    35  )
    36  
    37  // A StructuralError suggests that the ASN.1 data is valid, but the Go type
    38  // which is receiving it doesn't match.
    39  type StructuralError struct {
    40  	Msg string
    41  }
    42  
    43  func (e StructuralError) Error() string { return "asn1: structure error: " + e.Msg }
    44  
    45  // A SyntaxError suggests that the ASN.1 data is invalid.
    46  type SyntaxError struct {
    47  	Msg string
    48  }
    49  
    50  func (e SyntaxError) Error() string { return "asn1: syntax error: " + e.Msg }
    51  
    52  // We start by dealing with each of the primitive types in turn.
    53  
    54  // BOOLEAN
    55  
    56  func parseBool(bytes []byte) (ret bool, err error) {
    57  	if len(bytes) != 1 {
    58  		err = SyntaxError{"invalid boolean"}
    59  		return
    60  	}
    61  
    62  	// DER demands that "If the encoding represents the boolean value TRUE,
    63  	// its single contents octet shall have all eight bits set to one."
    64  	// Thus only 0 and 255 are valid encoded values.
    65  	switch bytes[0] {
    66  	case 0:
    67  		ret = false
    68  	case 0xff:
    69  		ret = true
    70  	default:
    71  		err = SyntaxError{"invalid boolean"}
    72  	}
    73  
    74  	return
    75  }
    76  
    77  // INTEGER
    78  
    79  // checkInteger returns nil if the given bytes are a valid DER-encoded
    80  // INTEGER and an error otherwise.
    81  func checkInteger(bytes []byte) error {
    82  	if len(bytes) == 0 {
    83  		return StructuralError{"empty integer"}
    84  	}
    85  	if len(bytes) == 1 {
    86  		return nil
    87  	}
    88  	if (bytes[0] == 0 && bytes[1]&0x80 == 0) || (bytes[0] == 0xff && bytes[1]&0x80 == 0x80) {
    89  		return StructuralError{"integer not minimally-encoded"}
    90  	}
    91  	return nil
    92  }
    93  
    94  // parseInt64 treats the given bytes as a big-endian, signed integer and
    95  // returns the result.
    96  func parseInt64(bytes []byte) (ret int64, err error) {
    97  	err = checkInteger(bytes)
    98  	if err != nil {
    99  		return
   100  	}
   101  	if len(bytes) > 8 {
   102  		// We'll overflow an int64 in this case.
   103  		err = StructuralError{"integer too large"}
   104  		return
   105  	}
   106  	for bytesRead := 0; bytesRead < len(bytes); bytesRead++ {
   107  		ret <<= 8
   108  		ret |= int64(bytes[bytesRead])
   109  	}
   110  
   111  	// Shift up and down in order to sign extend the result.
   112  	ret <<= 64 - uint8(len(bytes))*8
   113  	ret >>= 64 - uint8(len(bytes))*8
   114  	return
   115  }
   116  
   117  // parseInt32 treats the given bytes as a big-endian, signed integer and returns
   118  // the result.
   119  func parseInt32(bytes []byte) (int32, error) {
   120  	if err := checkInteger(bytes); err != nil {
   121  		return 0, err
   122  	}
   123  	ret64, err := parseInt64(bytes)
   124  	if err != nil {
   125  		return 0, err
   126  	}
   127  	if ret64 != int64(int32(ret64)) {
   128  		return 0, StructuralError{"integer too large"}
   129  	}
   130  	return int32(ret64), nil
   131  }
   132  
   133  var bigOne = big.NewInt(1)
   134  
   135  // parseBigInt treats the given bytes as a big-endian, signed integer and returns
   136  // the result.
   137  func parseBigInt(bytes []byte) (*big.Int, error) {
   138  	if err := checkInteger(bytes); err != nil {
   139  		return nil, err
   140  	}
   141  	ret := new(big.Int)
   142  	if len(bytes) > 0 && bytes[0]&0x80 == 0x80 {
   143  		// This is a negative number.
   144  		notBytes := make([]byte, len(bytes))
   145  		for i := range notBytes {
   146  			notBytes[i] = ^bytes[i]
   147  		}
   148  		ret.SetBytes(notBytes)
   149  		ret.Add(ret, bigOne)
   150  		ret.Neg(ret)
   151  		return ret, nil
   152  	}
   153  	ret.SetBytes(bytes)
   154  	return ret, nil
   155  }
   156  
   157  // BIT STRING
   158  
   159  // BitString is the structure to use when you want an ASN.1 BIT STRING type. A
   160  // bit string is padded up to the nearest byte in memory and the number of
   161  // valid bits is recorded. Padding bits will be zero.
   162  type BitString struct {
   163  	Bytes     []byte // bits packed into bytes.
   164  	BitLength int    // length in bits.
   165  }
   166  
   167  // At returns the bit at the given index. If the index is out of range it
   168  // returns 0.
   169  func (b BitString) At(i int) int {
   170  	if i < 0 || i >= b.BitLength {
   171  		return 0
   172  	}
   173  	x := i / 8
   174  	y := 7 - uint(i%8)
   175  	return int(b.Bytes[x]>>y) & 1
   176  }
   177  
   178  // RightAlign returns a slice where the padding bits are at the beginning. The
   179  // slice may share memory with the BitString.
   180  func (b BitString) RightAlign() []byte {
   181  	shift := uint(8 - (b.BitLength % 8))
   182  	if shift == 8 || len(b.Bytes) == 0 {
   183  		return b.Bytes
   184  	}
   185  
   186  	a := make([]byte, len(b.Bytes))
   187  	a[0] = b.Bytes[0] >> shift
   188  	for i := 1; i < len(b.Bytes); i++ {
   189  		a[i] = b.Bytes[i-1] << (8 - shift)
   190  		a[i] |= b.Bytes[i] >> shift
   191  	}
   192  
   193  	return a
   194  }
   195  
   196  // parseBitString parses an ASN.1 bit string from the given byte slice and returns it.
   197  func parseBitString(bytes []byte) (ret BitString, err error) {
   198  	if len(bytes) == 0 {
   199  		err = SyntaxError{"zero length BIT STRING"}
   200  		return
   201  	}
   202  	paddingBits := int(bytes[0])
   203  	if paddingBits > 7 ||
   204  		len(bytes) == 1 && paddingBits > 0 ||
   205  		bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
   206  		err = SyntaxError{"invalid padding bits in BIT STRING"}
   207  		return
   208  	}
   209  	ret.BitLength = (len(bytes)-1)*8 - paddingBits
   210  	ret.Bytes = bytes[1:]
   211  	return
   212  }
   213  
   214  // NULL
   215  
   216  // NullRawValue is a [RawValue] with its Tag set to the ASN.1 NULL type tag (5).
   217  var NullRawValue = RawValue{Tag: TagNull}
   218  
   219  // NullBytes contains bytes representing the DER-encoded ASN.1 NULL type.
   220  var NullBytes = []byte{TagNull, 0}
   221  
   222  // OBJECT IDENTIFIER
   223  
   224  // An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.
   225  type ObjectIdentifier []int
   226  
   227  // Equal reports whether oi and other represent the same identifier.
   228  func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool {
   229  	return slices.Equal(oi, other)
   230  }
   231  
   232  func (oi ObjectIdentifier) String() string {
   233  	var s strings.Builder
   234  	s.Grow(32)
   235  
   236  	buf := make([]byte, 0, 19)
   237  	for i, v := range oi {
   238  		if i > 0 {
   239  			s.WriteByte('.')
   240  		}
   241  		s.Write(strconv.AppendInt(buf, int64(v), 10))
   242  	}
   243  
   244  	return s.String()
   245  }
   246  
   247  // parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and
   248  // returns it. An object identifier is a sequence of variable length integers
   249  // that are assigned in a hierarchy.
   250  func parseObjectIdentifier(bytes []byte) (s ObjectIdentifier, err error) {
   251  	if len(bytes) == 0 {
   252  		err = SyntaxError{"zero length OBJECT IDENTIFIER"}
   253  		return
   254  	}
   255  
   256  	// In the worst case, we get two elements from the first byte (which is
   257  	// encoded differently) and then every varint is a single byte long.
   258  	s = make([]int, len(bytes)+1)
   259  
   260  	// The first varint is 40*value1 + value2:
   261  	// According to this packing, value1 can take the values 0, 1 and 2 only.
   262  	// When value1 = 0 or value1 = 1, then value2 is <= 39. When value1 = 2,
   263  	// then there are no restrictions on value2.
   264  	v, offset, err := parseBase128Int(bytes, 0)
   265  	if err != nil {
   266  		return
   267  	}
   268  	if v < 80 {
   269  		s[0] = v / 40
   270  		s[1] = v % 40
   271  	} else {
   272  		s[0] = 2
   273  		s[1] = v - 80
   274  	}
   275  
   276  	i := 2
   277  	for ; offset < len(bytes); i++ {
   278  		v, offset, err = parseBase128Int(bytes, offset)
   279  		if err != nil {
   280  			return
   281  		}
   282  		s[i] = v
   283  	}
   284  	s = s[0:i]
   285  	return
   286  }
   287  
   288  // ENUMERATED
   289  
   290  // An Enumerated is represented as a plain int.
   291  type Enumerated int
   292  
   293  // FLAG
   294  
   295  // A Flag accepts any data and is set to true if present.
   296  type Flag bool
   297  
   298  // parseBase128Int parses a base-128 encoded int from the given offset in the
   299  // given byte slice. It returns the value and the new offset.
   300  func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) {
   301  	offset = initOffset
   302  	var ret64 int64
   303  	for shifted := 0; offset < len(bytes); shifted++ {
   304  		// 5 * 7 bits per byte == 35 bits of data
   305  		// Thus the representation is either non-minimal or too large for an int32
   306  		if shifted == 5 {
   307  			err = StructuralError{"base 128 integer too large"}
   308  			return
   309  		}
   310  		ret64 <<= 7
   311  		b := bytes[offset]
   312  		// integers should be minimally encoded, so the leading octet should
   313  		// never be 0x80
   314  		if shifted == 0 && b == 0x80 {
   315  			err = SyntaxError{"integer is not minimally encoded"}
   316  			return
   317  		}
   318  		ret64 |= int64(b & 0x7f)
   319  		offset++
   320  		if b&0x80 == 0 {
   321  			ret = int(ret64)
   322  			// Ensure that the returned value fits in an int on all platforms
   323  			if ret64 > math.MaxInt32 {
   324  				err = StructuralError{"base 128 integer too large"}
   325  			}
   326  			return
   327  		}
   328  	}
   329  	err = SyntaxError{"truncated base 128 integer"}
   330  	return
   331  }
   332  
   333  // UTCTime
   334  
   335  func parseUTCTime(bytes []byte) (ret time.Time, err error) {
   336  	s := string(bytes)
   337  
   338  	formatStr := "0601021504Z0700"
   339  	ret, err = time.Parse(formatStr, s)
   340  	if err != nil {
   341  		formatStr = "060102150405Z0700"
   342  		ret, err = time.Parse(formatStr, s)
   343  	}
   344  	if err != nil {
   345  		return
   346  	}
   347  
   348  	if serialized := ret.Format(formatStr); serialized != s {
   349  		err = fmt.Errorf("asn1: time did not serialize back to the original value and may be invalid: given %q, but serialized as %q", s, serialized)
   350  		return
   351  	}
   352  
   353  	if ret.Year() >= 2050 {
   354  		// UTCTime only encodes times prior to 2050. See https://tools.ietf.org/html/rfc5280#section-4.1.2.5.1
   355  		ret = ret.AddDate(-100, 0, 0)
   356  	}
   357  
   358  	return
   359  }
   360  
   361  // parseGeneralizedTime parses the GeneralizedTime from the given byte slice
   362  // and returns the resulting time.
   363  func parseGeneralizedTime(bytes []byte) (ret time.Time, err error) {
   364  	const formatStr = "20060102150405.999999999Z0700"
   365  	s := string(bytes)
   366  
   367  	if ret, err = time.Parse(formatStr, s); err != nil {
   368  		return
   369  	}
   370  
   371  	if serialized := ret.Format(formatStr); serialized != s {
   372  		err = fmt.Errorf("asn1: time did not serialize back to the original value and may be invalid: given %q, but serialized as %q", s, serialized)
   373  	}
   374  
   375  	return
   376  }
   377  
   378  // NumericString
   379  
   380  // parseNumericString parses an ASN.1 NumericString from the given byte array
   381  // and returns it.
   382  func parseNumericString(bytes []byte) (ret string, err error) {
   383  	for _, b := range bytes {
   384  		if !isNumeric(b) {
   385  			return "", SyntaxError{"NumericString contains invalid character"}
   386  		}
   387  	}
   388  	return string(bytes), nil
   389  }
   390  
   391  // isNumeric reports whether the given b is in the ASN.1 NumericString set.
   392  func isNumeric(b byte) bool {
   393  	return '0' <= b && b <= '9' ||
   394  		b == ' '
   395  }
   396  
   397  // PrintableString
   398  
   399  // parsePrintableString parses an ASN.1 PrintableString from the given byte
   400  // array and returns it.
   401  func parsePrintableString(bytes []byte) (ret string, err error) {
   402  	for _, b := range bytes {
   403  		if !isPrintable(b, allowAsterisk, allowAmpersand) {
   404  			err = SyntaxError{"PrintableString contains invalid character"}
   405  			return
   406  		}
   407  	}
   408  	ret = string(bytes)
   409  	return
   410  }
   411  
   412  type asteriskFlag bool
   413  type ampersandFlag bool
   414  
   415  const (
   416  	allowAsterisk  asteriskFlag = true
   417  	rejectAsterisk asteriskFlag = false
   418  
   419  	allowAmpersand  ampersandFlag = true
   420  	rejectAmpersand ampersandFlag = false
   421  )
   422  
   423  // isPrintable reports whether the given b is in the ASN.1 PrintableString set.
   424  // If asterisk is allowAsterisk then '*' is also allowed, reflecting existing
   425  // practice. If ampersand is allowAmpersand then '&' is allowed as well.
   426  func isPrintable(b byte, asterisk asteriskFlag, ampersand ampersandFlag) bool {
   427  	return 'a' <= b && b <= 'z' ||
   428  		'A' <= b && b <= 'Z' ||
   429  		'0' <= b && b <= '9' ||
   430  		'\'' <= b && b <= ')' ||
   431  		'+' <= b && b <= '/' ||
   432  		b == ' ' ||
   433  		b == ':' ||
   434  		b == '=' ||
   435  		b == '?' ||
   436  		// This is technically not allowed in a PrintableString.
   437  		// However, x509 certificates with wildcard strings don't
   438  		// always use the correct string type so we permit it.
   439  		(bool(asterisk) && b == '*') ||
   440  		// This is not technically allowed either. However, not
   441  		// only is it relatively common, but there are also a
   442  		// handful of CA certificates that contain it. At least
   443  		// one of which will not expire until 2027.
   444  		(bool(ampersand) && b == '&')
   445  }
   446  
   447  // IA5String
   448  
   449  // parseIA5String parses an ASN.1 IA5String (ASCII string) from the given
   450  // byte slice and returns it.
   451  func parseIA5String(bytes []byte) (ret string, err error) {
   452  	for _, b := range bytes {
   453  		if b >= utf8.RuneSelf {
   454  			err = SyntaxError{"IA5String contains invalid character"}
   455  			return
   456  		}
   457  	}
   458  	ret = string(bytes)
   459  	return
   460  }
   461  
   462  // T61String
   463  
   464  // parseT61String parses an ASN.1 T61String (8-bit clean string) from the given
   465  // byte slice and returns it.
   466  func parseT61String(bytes []byte) (ret string, err error) {
   467  	// T.61 is a defunct ITU 8-bit character encoding which preceded Unicode.
   468  	// T.61 uses a code page layout that _almost_ exactly maps to the code
   469  	// page layout of the ISO 8859-1 (Latin-1) character encoding, with the
   470  	// exception that a number of characters in Latin-1 are not present
   471  	// in T.61.
   472  	//
   473  	// Instead of mapping which characters are present in Latin-1 but not T.61,
   474  	// we just treat these strings as being encoded using Latin-1. This matches
   475  	// what most of the world does, including BoringSSL.
   476  	buf := make([]byte, 0, len(bytes))
   477  	for _, v := range bytes {
   478  		// All the 1-byte UTF-8 runes map 1-1 with Latin-1.
   479  		buf = utf8.AppendRune(buf, rune(v))
   480  	}
   481  	return string(buf), nil
   482  }
   483  
   484  // UTF8String
   485  
   486  // parseUTF8String parses an ASN.1 UTF8String (raw UTF-8) from the given byte
   487  // array and returns it.
   488  func parseUTF8String(bytes []byte) (ret string, err error) {
   489  	if !utf8.Valid(bytes) {
   490  		return "", errors.New("asn1: invalid UTF-8 string")
   491  	}
   492  	return string(bytes), nil
   493  }
   494  
   495  // BMPString
   496  
   497  // parseBMPString parses an ASN.1 BMPString (Basic Multilingual Plane of
   498  // ISO/IEC/ITU 10646-1) from the given byte slice and returns it.
   499  func parseBMPString(bmpString []byte) (string, error) {
   500  	// BMPString uses the defunct UCS-2 16-bit character encoding, which
   501  	// covers the Basic Multilingual Plane (BMP). UTF-16 was an extension of
   502  	// UCS-2, containing all of the same code points, but also including
   503  	// multi-code point characters (by using surrogate code points). We can
   504  	// treat a UCS-2 encoded string as a UTF-16 encoded string, as long as
   505  	// we reject out the UTF-16 specific code points. This matches the
   506  	// BoringSSL behavior.
   507  
   508  	if len(bmpString)%2 != 0 {
   509  		return "", errors.New("invalid BMPString")
   510  	}
   511  
   512  	// Strip terminator if present.
   513  	if l := len(bmpString); l >= 2 && bmpString[l-1] == 0 && bmpString[l-2] == 0 {
   514  		bmpString = bmpString[:l-2]
   515  	}
   516  
   517  	s := make([]uint16, 0, len(bmpString)/2)
   518  	for len(bmpString) > 0 {
   519  		point := uint16(bmpString[0])<<8 + uint16(bmpString[1])
   520  		// Reject UTF-16 code points that are permanently reserved
   521  		// noncharacters (0xfffe, 0xffff, and 0xfdd0-0xfdef) and surrogates
   522  		// (0xd800-0xdfff).
   523  		if point == 0xfffe || point == 0xffff ||
   524  			(point >= 0xfdd0 && point <= 0xfdef) ||
   525  			(point >= 0xd800 && point <= 0xdfff) {
   526  			return "", errors.New("invalid BMPString")
   527  		}
   528  		s = append(s, point)
   529  		bmpString = bmpString[2:]
   530  	}
   531  
   532  	return string(utf16.Decode(s)), nil
   533  }
   534  
   535  // A RawValue represents an undecoded ASN.1 object.
   536  type RawValue struct {
   537  	Class, Tag int
   538  	IsCompound bool
   539  	Bytes      []byte
   540  	FullBytes  []byte // includes the tag and length
   541  }
   542  
   543  // RawContent is used to signal that the undecoded, DER data needs to be
   544  // preserved for a struct. To use it, the first field of the struct must have
   545  // this type. It's an error for any of the other fields to have this type.
   546  type RawContent []byte
   547  
   548  // Tagging
   549  
   550  // parseTagAndLength parses an ASN.1 tag and length pair from the given offset
   551  // into a byte slice. It returns the parsed data and the new offset. SET and
   552  // SET OF (tag 17) are mapped to SEQUENCE and SEQUENCE OF (tag 16) since we
   553  // don't distinguish between ordered and unordered objects in this code.
   554  func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset int, err error) {
   555  	offset = initOffset
   556  	// parseTagAndLength should not be called without at least a single
   557  	// byte to read. Thus this check is for robustness:
   558  	if offset >= len(bytes) {
   559  		err = errors.New("asn1: internal error in parseTagAndLength")
   560  		return
   561  	}
   562  	b := bytes[offset]
   563  	offset++
   564  	ret.class = int(b >> 6)
   565  	ret.isCompound = b&0x20 == 0x20
   566  	ret.tag = int(b & 0x1f)
   567  
   568  	// If the bottom five bits are set, then the tag number is actually base 128
   569  	// encoded afterwards
   570  	if ret.tag == 0x1f {
   571  		ret.tag, offset, err = parseBase128Int(bytes, offset)
   572  		if err != nil {
   573  			return
   574  		}
   575  		// Tags should be encoded in minimal form.
   576  		if ret.tag < 0x1f {
   577  			err = SyntaxError{"non-minimal tag"}
   578  			return
   579  		}
   580  	}
   581  	if offset >= len(bytes) {
   582  		err = SyntaxError{"truncated tag or length"}
   583  		return
   584  	}
   585  	b = bytes[offset]
   586  	offset++
   587  	if b&0x80 == 0 {
   588  		// The length is encoded in the bottom 7 bits.
   589  		ret.length = int(b & 0x7f)
   590  	} else {
   591  		// Bottom 7 bits give the number of length bytes to follow.
   592  		numBytes := int(b & 0x7f)
   593  		if numBytes == 0 {
   594  			err = SyntaxError{"indefinite length found (not DER)"}
   595  			return
   596  		}
   597  		ret.length = 0
   598  		for i := 0; i < numBytes; i++ {
   599  			if offset >= len(bytes) {
   600  				err = SyntaxError{"truncated tag or length"}
   601  				return
   602  			}
   603  			b = bytes[offset]
   604  			offset++
   605  			if ret.length >= 1<<23 {
   606  				// We can't shift ret.length up without
   607  				// overflowing.
   608  				err = StructuralError{"length too large"}
   609  				return
   610  			}
   611  			ret.length <<= 8
   612  			ret.length |= int(b)
   613  			if ret.length == 0 {
   614  				// DER requires that lengths be minimal.
   615  				err = StructuralError{"superfluous leading zeros in length"}
   616  				return
   617  			}
   618  		}
   619  		// Short lengths must be encoded in short form.
   620  		if ret.length < 0x80 {
   621  			err = StructuralError{"non-minimal length"}
   622  			return
   623  		}
   624  	}
   625  
   626  	return
   627  }
   628  
   629  // parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse
   630  // a number of ASN.1 values from the given byte slice and returns them as a
   631  // slice of Go values of the given type.
   632  func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type) (ret reflect.Value, err error) {
   633  	matchAny, expectedTag, compoundType, ok := getUniversalType(elemType)
   634  	if !ok {
   635  		err = StructuralError{"unknown Go type for slice"}
   636  		return
   637  	}
   638  
   639  	// First we iterate over the input and count the number of elements,
   640  	// checking that the types are correct in each case.
   641  	numElements := 0
   642  	for offset := 0; offset < len(bytes); {
   643  		var t tagAndLength
   644  		t, offset, err = parseTagAndLength(bytes, offset)
   645  		if err != nil {
   646  			return
   647  		}
   648  		switch t.tag {
   649  		case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString:
   650  			// We pretend that various other string types are
   651  			// PRINTABLE STRINGs so that a sequence of them can be
   652  			// parsed into a []string.
   653  			t.tag = TagPrintableString
   654  		case TagGeneralizedTime, TagUTCTime:
   655  			// Likewise, both time types are treated the same.
   656  			t.tag = TagUTCTime
   657  		}
   658  
   659  		if !matchAny && (t.class != ClassUniversal || t.isCompound != compoundType || t.tag != expectedTag) {
   660  			err = StructuralError{"sequence tag mismatch"}
   661  			return
   662  		}
   663  		if invalidLength(offset, t.length, len(bytes)) {
   664  			err = SyntaxError{"truncated sequence"}
   665  			return
   666  		}
   667  		offset += t.length
   668  		numElements++
   669  	}
   670  	elemSize := uint64(elemType.Size())
   671  	safeCap := saferio.SliceCapWithSize(elemSize, uint64(numElements))
   672  	if safeCap < 0 {
   673  		err = SyntaxError{fmt.Sprintf("%s slice too big: %d elements of %d bytes", elemType.Kind(), numElements, elemSize)}
   674  		return
   675  	}
   676  	ret = reflect.MakeSlice(sliceType, 0, safeCap)
   677  	params := fieldParameters{}
   678  	offset := 0
   679  	for i := 0; i < numElements; i++ {
   680  		ret = reflect.Append(ret, reflect.Zero(elemType))
   681  		offset, err = parseField(ret.Index(i), bytes, offset, params)
   682  		if err != nil {
   683  			return
   684  		}
   685  	}
   686  	return
   687  }
   688  
   689  var (
   690  	bitStringType        = reflect.TypeFor[BitString]()
   691  	objectIdentifierType = reflect.TypeFor[ObjectIdentifier]()
   692  	enumeratedType       = reflect.TypeFor[Enumerated]()
   693  	flagType             = reflect.TypeFor[Flag]()
   694  	timeType             = reflect.TypeFor[time.Time]()
   695  	rawValueType         = reflect.TypeFor[RawValue]()
   696  	rawContentsType      = reflect.TypeFor[RawContent]()
   697  	bigIntType           = reflect.TypeFor[*big.Int]()
   698  )
   699  
   700  // invalidLength reports whether offset + length > sliceLength, or if the
   701  // addition would overflow.
   702  func invalidLength(offset, length, sliceLength int) bool {
   703  	return offset+length < offset || offset+length > sliceLength
   704  }
   705  
   706  // parseField is the main parsing function. Given a byte slice and an offset
   707  // into the array, it will try to parse a suitable ASN.1 value out and store it
   708  // in the given Value.
   709  func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error) {
   710  	offset = initOffset
   711  	fieldType := v.Type()
   712  
   713  	// If we have run out of data, it may be that there are optional elements at the end.
   714  	if offset == len(bytes) {
   715  		if !setDefaultValue(v, params) {
   716  			err = SyntaxError{"sequence truncated"}
   717  		}
   718  		return
   719  	}
   720  
   721  	// Deal with the ANY type.
   722  	if ifaceType := fieldType; ifaceType.Kind() == reflect.Interface && ifaceType.NumMethod() == 0 {
   723  		var t tagAndLength
   724  		t, offset, err = parseTagAndLength(bytes, offset)
   725  		if err != nil {
   726  			return
   727  		}
   728  		if invalidLength(offset, t.length, len(bytes)) {
   729  			err = SyntaxError{"data truncated"}
   730  			return
   731  		}
   732  		var result any
   733  		if !t.isCompound && t.class == ClassUniversal {
   734  			innerBytes := bytes[offset : offset+t.length]
   735  			switch t.tag {
   736  			case TagBoolean:
   737  				result, err = parseBool(innerBytes)
   738  			case TagPrintableString:
   739  				result, err = parsePrintableString(innerBytes)
   740  			case TagNumericString:
   741  				result, err = parseNumericString(innerBytes)
   742  			case TagIA5String:
   743  				result, err = parseIA5String(innerBytes)
   744  			case TagT61String:
   745  				result, err = parseT61String(innerBytes)
   746  			case TagUTF8String:
   747  				result, err = parseUTF8String(innerBytes)
   748  			case TagInteger:
   749  				result, err = parseInt64(innerBytes)
   750  			case TagBitString:
   751  				result, err = parseBitString(innerBytes)
   752  			case TagOID:
   753  				result, err = parseObjectIdentifier(innerBytes)
   754  			case TagUTCTime:
   755  				result, err = parseUTCTime(innerBytes)
   756  			case TagGeneralizedTime:
   757  				result, err = parseGeneralizedTime(innerBytes)
   758  			case TagOctetString:
   759  				result = innerBytes
   760  			case TagBMPString:
   761  				result, err = parseBMPString(innerBytes)
   762  			default:
   763  				// If we don't know how to handle the type, we just leave Value as nil.
   764  			}
   765  		}
   766  		offset += t.length
   767  		if err != nil {
   768  			return
   769  		}
   770  		if result != nil {
   771  			v.Set(reflect.ValueOf(result))
   772  		}
   773  		return
   774  	}
   775  
   776  	t, offset, err := parseTagAndLength(bytes, offset)
   777  	if err != nil {
   778  		return
   779  	}
   780  	if params.explicit {
   781  		expectedClass := ClassContextSpecific
   782  		if params.application {
   783  			expectedClass = ClassApplication
   784  		}
   785  		if offset == len(bytes) {
   786  			err = StructuralError{"explicit tag has no child"}
   787  			return
   788  		}
   789  		if t.class == expectedClass && t.tag == *params.tag && (t.length == 0 || t.isCompound) {
   790  			if fieldType == rawValueType {
   791  				// The inner element should not be parsed for RawValues.
   792  			} else if t.length > 0 {
   793  				t, offset, err = parseTagAndLength(bytes, offset)
   794  				if err != nil {
   795  					return
   796  				}
   797  			} else {
   798  				if fieldType != flagType {
   799  					err = StructuralError{"zero length explicit tag was not an asn1.Flag"}
   800  					return
   801  				}
   802  				v.SetBool(true)
   803  				return
   804  			}
   805  		} else {
   806  			// The tags didn't match, it might be an optional element.
   807  			ok := setDefaultValue(v, params)
   808  			if ok {
   809  				offset = initOffset
   810  			} else {
   811  				err = StructuralError{"explicitly tagged member didn't match"}
   812  			}
   813  			return
   814  		}
   815  	}
   816  
   817  	matchAny, universalTag, compoundType, ok1 := getUniversalType(fieldType)
   818  	if !ok1 {
   819  		err = StructuralError{fmt.Sprintf("unknown Go type: %v", fieldType)}
   820  		return
   821  	}
   822  
   823  	// Special case for strings: all the ASN.1 string types map to the Go
   824  	// type string. getUniversalType returns the tag for PrintableString
   825  	// when it sees a string, so if we see a different string type on the
   826  	// wire, we change the universal type to match.
   827  	if universalTag == TagPrintableString {
   828  		if t.class == ClassUniversal {
   829  			switch t.tag {
   830  			case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString:
   831  				universalTag = t.tag
   832  			}
   833  		} else if params.stringType != 0 {
   834  			universalTag = params.stringType
   835  		}
   836  	}
   837  
   838  	// Special case for time: UTCTime and GeneralizedTime both map to the
   839  	// Go type time.Time. getUniversalType returns the tag for UTCTime when
   840  	// it sees a time.Time, so if we see a different time type on the wire,
   841  	// or the field is tagged with a different type, we change the universal
   842  	// type to match.
   843  	if universalTag == TagUTCTime {
   844  		if t.class == ClassUniversal {
   845  			if t.tag == TagGeneralizedTime {
   846  				universalTag = t.tag
   847  			}
   848  		} else if params.timeType != 0 {
   849  			universalTag = params.timeType
   850  		}
   851  	}
   852  
   853  	if params.set {
   854  		universalTag = TagSet
   855  	}
   856  
   857  	matchAnyClassAndTag := matchAny
   858  	expectedClass := ClassUniversal
   859  	expectedTag := universalTag
   860  
   861  	if !params.explicit && params.tag != nil {
   862  		expectedClass = ClassContextSpecific
   863  		expectedTag = *params.tag
   864  		matchAnyClassAndTag = false
   865  	}
   866  
   867  	if !params.explicit && params.application && params.tag != nil {
   868  		expectedClass = ClassApplication
   869  		expectedTag = *params.tag
   870  		matchAnyClassAndTag = false
   871  	}
   872  
   873  	if !params.explicit && params.private && params.tag != nil {
   874  		expectedClass = ClassPrivate
   875  		expectedTag = *params.tag
   876  		matchAnyClassAndTag = false
   877  	}
   878  
   879  	// We have unwrapped any explicit tagging at this point.
   880  	if !matchAnyClassAndTag && (t.class != expectedClass || t.tag != expectedTag) ||
   881  		(!matchAny && t.isCompound != compoundType) {
   882  		// Tags don't match. Again, it could be an optional element.
   883  		ok := setDefaultValue(v, params)
   884  		if ok {
   885  			offset = initOffset
   886  		} else {
   887  			err = StructuralError{fmt.Sprintf("tags don't match (%d vs %+v) %+v %s @%d", expectedTag, t, params, fieldType.Name(), offset)}
   888  		}
   889  		return
   890  	}
   891  	if invalidLength(offset, t.length, len(bytes)) {
   892  		err = SyntaxError{"data truncated"}
   893  		return
   894  	}
   895  	innerBytes := bytes[offset : offset+t.length]
   896  	offset += t.length
   897  
   898  	// We deal with the structures defined in this package first.
   899  	switch v := v.Addr().Interface().(type) {
   900  	case *RawValue:
   901  		*v = RawValue{t.class, t.tag, t.isCompound, innerBytes, bytes[initOffset:offset]}
   902  		return
   903  	case *ObjectIdentifier:
   904  		*v, err = parseObjectIdentifier(innerBytes)
   905  		return
   906  	case *BitString:
   907  		*v, err = parseBitString(innerBytes)
   908  		return
   909  	case *time.Time:
   910  		if universalTag == TagUTCTime {
   911  			*v, err = parseUTCTime(innerBytes)
   912  			return
   913  		}
   914  		*v, err = parseGeneralizedTime(innerBytes)
   915  		return
   916  	case *Enumerated:
   917  		parsedInt, err1 := parseInt32(innerBytes)
   918  		if err1 == nil {
   919  			*v = Enumerated(parsedInt)
   920  		}
   921  		err = err1
   922  		return
   923  	case *Flag:
   924  		*v = true
   925  		return
   926  	case **big.Int:
   927  		parsedInt, err1 := parseBigInt(innerBytes)
   928  		if err1 == nil {
   929  			*v = parsedInt
   930  		}
   931  		err = err1
   932  		return
   933  	}
   934  	switch val := v; val.Kind() {
   935  	case reflect.Bool:
   936  		parsedBool, err1 := parseBool(innerBytes)
   937  		if err1 == nil {
   938  			val.SetBool(parsedBool)
   939  		}
   940  		err = err1
   941  		return
   942  	case reflect.Int, reflect.Int32, reflect.Int64:
   943  		if val.Type().Size() == 4 {
   944  			parsedInt, err1 := parseInt32(innerBytes)
   945  			if err1 == nil {
   946  				val.SetInt(int64(parsedInt))
   947  			}
   948  			err = err1
   949  		} else {
   950  			parsedInt, err1 := parseInt64(innerBytes)
   951  			if err1 == nil {
   952  				val.SetInt(parsedInt)
   953  			}
   954  			err = err1
   955  		}
   956  		return
   957  	// TODO(dfc) Add support for the remaining integer types
   958  	case reflect.Struct:
   959  		structType := fieldType
   960  
   961  		for i := 0; i < structType.NumField(); i++ {
   962  			if !structType.Field(i).IsExported() {
   963  				err = StructuralError{"struct contains unexported fields"}
   964  				return
   965  			}
   966  		}
   967  
   968  		if structType.NumField() > 0 &&
   969  			structType.Field(0).Type == rawContentsType {
   970  			bytes := bytes[initOffset:offset]
   971  			val.Field(0).Set(reflect.ValueOf(RawContent(bytes)))
   972  		}
   973  
   974  		innerOffset := 0
   975  		for i := 0; i < structType.NumField(); i++ {
   976  			field := structType.Field(i)
   977  			if i == 0 && field.Type == rawContentsType {
   978  				continue
   979  			}
   980  			innerOffset, err = parseField(val.Field(i), innerBytes, innerOffset, parseFieldParameters(field.Tag.Get("asn1")))
   981  			if err != nil {
   982  				return
   983  			}
   984  		}
   985  		// We allow extra bytes at the end of the SEQUENCE because
   986  		// adding elements to the end has been used in X.509 as the
   987  		// version numbers have increased.
   988  		return
   989  	case reflect.Slice:
   990  		sliceType := fieldType
   991  		if sliceType.Elem().Kind() == reflect.Uint8 {
   992  			val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
   993  			reflect.Copy(val, reflect.ValueOf(innerBytes))
   994  			return
   995  		}
   996  		newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem())
   997  		if err1 == nil {
   998  			val.Set(newSlice)
   999  		}
  1000  		err = err1
  1001  		return
  1002  	case reflect.String:
  1003  		var v string
  1004  		switch universalTag {
  1005  		case TagPrintableString:
  1006  			v, err = parsePrintableString(innerBytes)
  1007  		case TagNumericString:
  1008  			v, err = parseNumericString(innerBytes)
  1009  		case TagIA5String:
  1010  			v, err = parseIA5String(innerBytes)
  1011  		case TagT61String:
  1012  			v, err = parseT61String(innerBytes)
  1013  		case TagUTF8String:
  1014  			v, err = parseUTF8String(innerBytes)
  1015  		case TagGeneralString:
  1016  			// GeneralString is specified in ISO-2022/ECMA-35,
  1017  			// A brief review suggests that it includes structures
  1018  			// that allow the encoding to change midstring and
  1019  			// such. We give up and pass it as an 8-bit string.
  1020  			v, err = parseT61String(innerBytes)
  1021  		case TagBMPString:
  1022  			v, err = parseBMPString(innerBytes)
  1023  
  1024  		default:
  1025  			err = SyntaxError{fmt.Sprintf("internal error: unknown string type %d", universalTag)}
  1026  		}
  1027  		if err == nil {
  1028  			val.SetString(v)
  1029  		}
  1030  		return
  1031  	}
  1032  	err = StructuralError{"unsupported: " + v.Type().String()}
  1033  	return
  1034  }
  1035  
  1036  // canHaveDefaultValue reports whether k is a Kind that we will set a default
  1037  // value for. (A signed integer, essentially.)
  1038  func canHaveDefaultValue(k reflect.Kind) bool {
  1039  	switch k {
  1040  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1041  		return true
  1042  	}
  1043  
  1044  	return false
  1045  }
  1046  
  1047  // setDefaultValue is used to install a default value, from a tag string, into
  1048  // a Value. It is successful if the field was optional, even if a default value
  1049  // wasn't provided or it failed to install it into the Value.
  1050  func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
  1051  	if !params.optional {
  1052  		return
  1053  	}
  1054  	ok = true
  1055  	if params.defaultValue == nil {
  1056  		return
  1057  	}
  1058  	if canHaveDefaultValue(v.Kind()) {
  1059  		v.SetInt(*params.defaultValue)
  1060  	}
  1061  	return
  1062  }
  1063  
  1064  // Unmarshal parses the DER-encoded ASN.1 data structure b
  1065  // and uses the reflect package to fill in an arbitrary value pointed at by val.
  1066  // Because Unmarshal uses the reflect package, the structs
  1067  // being written to must use upper case field names. If val
  1068  // is nil or not a pointer, Unmarshal returns an error.
  1069  //
  1070  // After parsing b, any bytes that were leftover and not used to fill
  1071  // val will be returned in rest. When parsing a SEQUENCE into a struct,
  1072  // any trailing elements of the SEQUENCE that do not have matching
  1073  // fields in val will not be included in rest, as these are considered
  1074  // valid elements of the SEQUENCE and not trailing data.
  1075  //
  1076  //   - An ASN.1 INTEGER can be written to an int, int32, int64,
  1077  //     or *[big.Int].
  1078  //     If the encoded value does not fit in the Go type,
  1079  //     Unmarshal returns a parse error.
  1080  //
  1081  //   - An ASN.1 BIT STRING can be written to a [BitString].
  1082  //
  1083  //   - An ASN.1 OCTET STRING can be written to a []byte.
  1084  //
  1085  //   - An ASN.1 OBJECT IDENTIFIER can be written to an [ObjectIdentifier].
  1086  //
  1087  //   - An ASN.1 ENUMERATED can be written to an [Enumerated].
  1088  //
  1089  //   - An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a [time.Time].
  1090  //
  1091  //   - An ASN.1 PrintableString, IA5String, or NumericString can be written to a string.
  1092  //
  1093  //   - Any of the above ASN.1 values can be written to an interface{}.
  1094  //     The value stored in the interface has the corresponding Go type.
  1095  //     For integers, that type is int64.
  1096  //
  1097  //   - An ASN.1 SEQUENCE OF x or SET OF x can be written
  1098  //     to a slice if an x can be written to the slice's element type.
  1099  //
  1100  //   - An ASN.1 SEQUENCE or SET can be written to a struct
  1101  //     if each of the elements in the sequence can be
  1102  //     written to the corresponding element in the struct.
  1103  //
  1104  // The following tags on struct fields have special meaning to Unmarshal:
  1105  //
  1106  //	application specifies that an APPLICATION tag is used
  1107  //	private     specifies that a PRIVATE tag is used
  1108  //	default:x   sets the default value for optional integer fields (only used if optional is also present)
  1109  //	explicit    specifies that an additional, explicit tag wraps the implicit one
  1110  //	optional    marks the field as ASN.1 OPTIONAL
  1111  //	set         causes a SET, rather than a SEQUENCE type to be expected
  1112  //	tag:x       specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC
  1113  //
  1114  // When decoding an ASN.1 value with an IMPLICIT tag into a string field,
  1115  // Unmarshal will default to a PrintableString, which doesn't support
  1116  // characters such as '@' and '&'. To force other encodings, use the following
  1117  // tags:
  1118  //
  1119  //	ia5     causes strings to be unmarshaled as ASN.1 IA5String values
  1120  //	numeric causes strings to be unmarshaled as ASN.1 NumericString values
  1121  //	utf8    causes strings to be unmarshaled as ASN.1 UTF8String values
  1122  //
  1123  // When decoding an ASN.1 value with an IMPLICIT tag into a time.Time field,
  1124  // Unmarshal will default to a UTCTime, which doesn't support time zones or
  1125  // fractional seconds. To force usage of GeneralizedTime, use the following
  1126  // tag:
  1127  //
  1128  //	generalized causes time.Times to be unmarshaled as ASN.1 GeneralizedTime values
  1129  //
  1130  // If the type of the first field of a structure is RawContent then the raw
  1131  // ASN1 contents of the struct will be stored in it.
  1132  //
  1133  // If the name of a slice type ends with "SET" then it's treated as if
  1134  // the "set" tag was set on it. This results in interpreting the type as a
  1135  // SET OF x rather than a SEQUENCE OF x. This can be used with nested slices
  1136  // where a struct tag cannot be given.
  1137  //
  1138  // Other ASN.1 types are not supported; if it encounters them,
  1139  // Unmarshal returns a parse error.
  1140  func Unmarshal(b []byte, val any) (rest []byte, err error) {
  1141  	return UnmarshalWithParams(b, val, "")
  1142  }
  1143  
  1144  // An invalidUnmarshalError describes an invalid argument passed to Unmarshal.
  1145  // (The argument to Unmarshal must be a non-nil pointer.)
  1146  type invalidUnmarshalError struct {
  1147  	Type reflect.Type
  1148  }
  1149  
  1150  func (e *invalidUnmarshalError) Error() string {
  1151  	if e.Type == nil {
  1152  		return "asn1: Unmarshal recipient value is nil"
  1153  	}
  1154  
  1155  	if e.Type.Kind() != reflect.Pointer {
  1156  		return "asn1: Unmarshal recipient value is non-pointer " + e.Type.String()
  1157  	}
  1158  	return "asn1: Unmarshal recipient value is nil " + e.Type.String()
  1159  }
  1160  
  1161  // UnmarshalWithParams allows field parameters to be specified for the
  1162  // top-level element. The form of the params is the same as the field tags.
  1163  func UnmarshalWithParams(b []byte, val any, params string) (rest []byte, err error) {
  1164  	v := reflect.ValueOf(val)
  1165  	if v.Kind() != reflect.Pointer || v.IsNil() {
  1166  		return nil, &invalidUnmarshalError{reflect.TypeOf(val)}
  1167  	}
  1168  	offset, err := parseField(v.Elem(), b, 0, parseFieldParameters(params))
  1169  	if err != nil {
  1170  		return nil, err
  1171  	}
  1172  	return b[offset:], nil
  1173  }
  1174  

View as plain text