common.go 5.3 KB

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