Black Lives Matter. Support the Equal Justice Initiative.

Source file src/encoding/asn1/common.go

Documentation: encoding/asn1

     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
     6  
     7  import (
     8  	"reflect"
     9  	"strconv"
    10  	"strings"
    11  )
    12  
    13  // ASN.1 objects have metadata preceding them:
    14  //   the tag: the type of the object
    15  //   a flag denoting if this object is compound or not
    16  //   the class type: the namespace of the tag
    17  //   the length of the object, in bytes
    18  
    19  // Here are some standard tags and classes
    20  
    21  // ASN.1 tags represent the type of the following object.
    22  const (
    23  	TagBoolean         = 1
    24  	TagInteger         = 2
    25  	TagBitString       = 3
    26  	TagOctetString     = 4
    27  	TagNull            = 5
    28  	TagOID             = 6
    29  	TagEnum            = 10
    30  	TagUTF8String      = 12
    31  	TagSequence        = 16
    32  	TagSet             = 17
    33  	TagNumericString   = 18
    34  	TagPrintableString = 19
    35  	TagT61String       = 20
    36  	TagIA5String       = 22
    37  	TagUTCTime         = 23
    38  	TagGeneralizedTime = 24
    39  	TagGeneralString   = 27
    40  	TagBMPString       = 30
    41  )
    42  
    43  // ASN.1 class types represent the namespace of the tag.
    44  const (
    45  	ClassUniversal       = 0
    46  	ClassApplication     = 1
    47  	ClassContextSpecific = 2
    48  	ClassPrivate         = 3
    49  )
    50  
    51  type tagAndLength struct {
    52  	class, tag, length int
    53  	isCompound         bool
    54  }
    55  
    56  // ASN.1 has IMPLICIT and EXPLICIT tags, which can be translated as "instead
    57  // of" and "in addition to". When not specified, every primitive type has a
    58  // default tag in the UNIVERSAL class.
    59  //
    60  // For example: a BIT STRING is tagged [UNIVERSAL 3] by default (although ASN.1
    61  // doesn't actually have a UNIVERSAL keyword). However, by saying [IMPLICIT
    62  // CONTEXT-SPECIFIC 42], that means that the tag is replaced by another.
    63  //
    64  // On the other hand, if it said [EXPLICIT CONTEXT-SPECIFIC 10], then an
    65  // /additional/ tag would wrap the default tag. This explicit tag will have the
    66  // compound flag set.
    67  //
    68  // (This is used in order to remove ambiguity with optional elements.)
    69  //
    70  // You can layer EXPLICIT and IMPLICIT tags to an arbitrary depth, however we
    71  // don't support that here. We support a single layer of EXPLICIT or IMPLICIT
    72  // tagging with tag strings on the fields of a structure.
    73  
    74  // fieldParameters is the parsed representation of tag string from a structure field.
    75  type fieldParameters struct {
    76  	optional     bool   // true iff the field is OPTIONAL
    77  	explicit     bool   // true iff an EXPLICIT tag is in use.
    78  	application  bool   // true iff an APPLICATION tag is in use.
    79  	private      bool   // true iff a PRIVATE tag is in use.
    80  	defaultValue *int64 // a default value for INTEGER typed fields (maybe nil).
    81  	tag          *int   // the EXPLICIT or IMPLICIT tag (maybe nil).
    82  	stringType   int    // the string tag to use when marshaling.
    83  	timeType     int    // the time tag to use when marshaling.
    84  	set          bool   // true iff this should be encoded as a SET
    85  	omitEmpty    bool   // true iff this should be omitted if empty when marshaling.
    86  
    87  	// Invariants:
    88  	//   if explicit is set, tag is non-nil.
    89  }
    90  
    91  // Given a tag string with the format specified in the package comment,
    92  // parseFieldParameters will parse it into a fieldParameters structure,
    93  // ignoring unknown parts of the string.
    94  func parseFieldParameters(str string) (ret fieldParameters) {
    95  	for _, part := range strings.Split(str, ",") {
    96  		switch {
    97  		case part == "optional":
    98  			ret.optional = true
    99  		case part == "explicit":
   100  			ret.explicit = true
   101  			if ret.tag == nil {
   102  				ret.tag = new(int)
   103  			}
   104  		case part == "generalized":
   105  			ret.timeType = TagGeneralizedTime
   106  		case part == "utc":
   107  			ret.timeType = TagUTCTime
   108  		case part == "ia5":
   109  			ret.stringType = TagIA5String
   110  		case part == "printable":
   111  			ret.stringType = TagPrintableString
   112  		case part == "numeric":
   113  			ret.stringType = TagNumericString
   114  		case part == "utf8":
   115  			ret.stringType = TagUTF8String
   116  		case strings.HasPrefix(part, "default:"):
   117  			i, err := strconv.ParseInt(part[8:], 10, 64)
   118  			if err == nil {
   119  				ret.defaultValue = new(int64)
   120  				*ret.defaultValue = i
   121  			}
   122  		case strings.HasPrefix(part, "tag:"):
   123  			i, err := strconv.Atoi(part[4:])
   124  			if err == nil {
   125  				ret.tag = new(int)
   126  				*ret.tag = i
   127  			}
   128  		case part == "set":
   129  			ret.set = true
   130  		case part == "application":
   131  			ret.application = true
   132  			if ret.tag == nil {
   133  				ret.tag = new(int)
   134  			}
   135  		case part == "private":
   136  			ret.private = true
   137  			if ret.tag == nil {
   138  				ret.tag = new(int)
   139  			}
   140  		case part == "omitempty":
   141  			ret.omitEmpty = true
   142  		}
   143  	}
   144  	return
   145  }
   146  
   147  // Given a reflected Go type, getUniversalType returns the default tag number
   148  // and expected compound flag.
   149  func getUniversalType(t reflect.Type) (matchAny bool, tagNumber int, isCompound, ok bool) {
   150  	switch t {
   151  	case rawValueType:
   152  		return true, -1, false, true
   153  	case objectIdentifierType:
   154  		return false, TagOID, false, true
   155  	case bitStringType:
   156  		return false, TagBitString, false, true
   157  	case timeType:
   158  		return false, TagUTCTime, false, true
   159  	case enumeratedType:
   160  		return false, TagEnum, false, true
   161  	case bigIntType:
   162  		return false, TagInteger, false, true
   163  	}
   164  	switch t.Kind() {
   165  	case reflect.Bool:
   166  		return false, TagBoolean, false, true
   167  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
   168  		return false, TagInteger, false, true
   169  	case reflect.Struct:
   170  		return false, TagSequence, true, true
   171  	case reflect.Slice:
   172  		if t.Elem().Kind() == reflect.Uint8 {
   173  			return false, TagOctetString, false, true
   174  		}
   175  		if strings.HasSuffix(t.Name(), "SET") {
   176  			return false, TagSet, true, true
   177  		}
   178  		return false, TagSequence, true, true
   179  	case reflect.String:
   180  		return false, TagPrintableString, false, true
   181  	}
   182  	return false, 0, false, false
   183  }
   184  

View as plain text