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