common.go 5.1 KB

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