github.com/zmap/zcrypto@v0.0.0-20240512203510-0fef58d9a9db/encoding/asn1/common.go (about) 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 var part string 96 for len(str) > 0 { 97 // This loop uses IndexByte and explicit slicing 98 // instead of strings.Split(str, ",") to reduce allocations. 99 i := strings.IndexByte(str, ',') 100 if i < 0 { 101 part, str = str, "" 102 } else { 103 part, str = str[:i], str[i+1:] 104 } 105 switch { 106 case part == "optional": 107 ret.optional = true 108 case part == "explicit": 109 ret.explicit = true 110 if ret.tag == nil { 111 ret.tag = new(int) 112 } 113 case part == "generalized": 114 ret.timeType = TagGeneralizedTime 115 case part == "utc": 116 ret.timeType = TagUTCTime 117 case part == "ia5": 118 ret.stringType = TagIA5String 119 case part == "printable": 120 ret.stringType = TagPrintableString 121 case part == "numeric": 122 ret.stringType = TagNumericString 123 case part == "utf8": 124 ret.stringType = TagUTF8String 125 case strings.HasPrefix(part, "default:"): 126 i, err := strconv.ParseInt(part[8:], 10, 64) 127 if err == nil { 128 ret.defaultValue = new(int64) 129 *ret.defaultValue = i 130 } 131 case strings.HasPrefix(part, "tag:"): 132 i, err := strconv.Atoi(part[4:]) 133 if err == nil { 134 ret.tag = new(int) 135 *ret.tag = i 136 } 137 case part == "set": 138 ret.set = true 139 case part == "application": 140 ret.application = true 141 if ret.tag == nil { 142 ret.tag = new(int) 143 } 144 case part == "private": 145 ret.private = true 146 if ret.tag == nil { 147 ret.tag = new(int) 148 } 149 case part == "omitempty": 150 ret.omitEmpty = true 151 } 152 } 153 return 154 } 155 156 // Given a reflected Go type, getUniversalType returns the default tag number 157 // and expected compound flag. 158 func getUniversalType(t reflect.Type) (matchAny bool, tagNumber int, isCompound, ok bool) { 159 switch t { 160 case rawValueType: 161 return true, -1, false, true 162 case objectIdentifierType: 163 return false, TagOID, false, true 164 case bitStringType: 165 return false, TagBitString, false, true 166 case timeType: 167 return false, TagUTCTime, false, true 168 case enumeratedType: 169 return false, TagEnum, false, true 170 case bigIntType: 171 return false, TagInteger, false, true 172 } 173 switch t.Kind() { 174 case reflect.Bool: 175 return false, TagBoolean, false, true 176 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 177 return false, TagInteger, false, true 178 case reflect.Struct: 179 return false, TagSequence, true, true 180 case reflect.Slice: 181 if t.Elem().Kind() == reflect.Uint8 { 182 return false, TagOctetString, false, true 183 } 184 if strings.HasSuffix(t.Name(), "SET") { 185 return false, TagSet, true, true 186 } 187 return false, TagSequence, true, true 188 case reflect.String: 189 return false, TagPrintableString, false, true 190 } 191 return false, 0, false, false 192 }