You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
4.6 KiB

  1. // Copyright 2017 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 cryptobyte contains types that help with parsing and constructing
  5. // length-prefixed, binary messages, including ASN.1 DER. (The asn1 subpackage
  6. // contains useful ASN.1 constants.)
  7. //
  8. // The String type is for parsing. It wraps a []byte slice and provides helper
  9. // functions for consuming structures, value by value.
  10. //
  11. // The Builder type is for constructing messages. It providers helper functions
  12. // for appending values and also for appending length-prefixed submessages –
  13. // without having to worry about calculating the length prefix ahead of time.
  14. //
  15. // See the documentation and examples for the Builder and String types to get
  16. // started.
  17. package cryptobyte // import "golang.org/x/crypto/cryptobyte"
  18. // String represents a string of bytes. It provides methods for parsing
  19. // fixed-length and length-prefixed values from it.
  20. type String []byte
  21. // read advances a String by n bytes and returns them. If less than n bytes
  22. // remain, it returns nil.
  23. func (s *String) read(n int) []byte {
  24. if len(*s) < n {
  25. return nil
  26. }
  27. v := (*s)[:n]
  28. *s = (*s)[n:]
  29. return v
  30. }
  31. // Skip advances the String by n byte and reports whether it was successful.
  32. func (s *String) Skip(n int) bool {
  33. return s.read(n) != nil
  34. }
  35. // ReadUint8 decodes an 8-bit value into out and advances over it. It
  36. // returns true on success and false on error.
  37. func (s *String) ReadUint8(out *uint8) bool {
  38. v := s.read(1)
  39. if v == nil {
  40. return false
  41. }
  42. *out = uint8(v[0])
  43. return true
  44. }
  45. // ReadUint16 decodes a big-endian, 16-bit value into out and advances over it.
  46. // It returns true on success and false on error.
  47. func (s *String) ReadUint16(out *uint16) bool {
  48. v := s.read(2)
  49. if v == nil {
  50. return false
  51. }
  52. *out = uint16(v[0])<<8 | uint16(v[1])
  53. return true
  54. }
  55. // ReadUint24 decodes a big-endian, 24-bit value into out and advances over it.
  56. // It returns true on success and false on error.
  57. func (s *String) ReadUint24(out *uint32) bool {
  58. v := s.read(3)
  59. if v == nil {
  60. return false
  61. }
  62. *out = uint32(v[0])<<16 | uint32(v[1])<<8 | uint32(v[2])
  63. return true
  64. }
  65. // ReadUint32 decodes a big-endian, 32-bit value into out and advances over it.
  66. // It returns true on success and false on error.
  67. func (s *String) ReadUint32(out *uint32) bool {
  68. v := s.read(4)
  69. if v == nil {
  70. return false
  71. }
  72. *out = uint32(v[0])<<24 | uint32(v[1])<<16 | uint32(v[2])<<8 | uint32(v[3])
  73. return true
  74. }
  75. func (s *String) readUnsigned(out *uint32, length int) bool {
  76. v := s.read(length)
  77. if v == nil {
  78. return false
  79. }
  80. var result uint32
  81. for i := 0; i < length; i++ {
  82. result <<= 8
  83. result |= uint32(v[i])
  84. }
  85. *out = result
  86. return true
  87. }
  88. func (s *String) readLengthPrefixed(lenLen int, outChild *String) bool {
  89. lenBytes := s.read(lenLen)
  90. if lenBytes == nil {
  91. return false
  92. }
  93. var length uint32
  94. for _, b := range lenBytes {
  95. length = length << 8
  96. length = length | uint32(b)
  97. }
  98. if int(length) < 0 {
  99. // This currently cannot overflow because we read uint24 at most, but check
  100. // anyway in case that changes in the future.
  101. return false
  102. }
  103. v := s.read(int(length))
  104. if v == nil {
  105. return false
  106. }
  107. *outChild = v
  108. return true
  109. }
  110. // ReadUint8LengthPrefixed reads the content of an 8-bit length-prefixed value
  111. // into out and advances over it. It returns true on success and false on
  112. // error.
  113. func (s *String) ReadUint8LengthPrefixed(out *String) bool {
  114. return s.readLengthPrefixed(1, out)
  115. }
  116. // ReadUint16LengthPrefixed reads the content of a big-endian, 16-bit
  117. // length-prefixed value into out and advances over it. It returns true on
  118. // success and false on error.
  119. func (s *String) ReadUint16LengthPrefixed(out *String) bool {
  120. return s.readLengthPrefixed(2, out)
  121. }
  122. // ReadUint24LengthPrefixed reads the content of a big-endian, 24-bit
  123. // length-prefixed value into out and advances over it. It returns true on
  124. // success and false on error.
  125. func (s *String) ReadUint24LengthPrefixed(out *String) bool {
  126. return s.readLengthPrefixed(3, out)
  127. }
  128. // ReadBytes reads n bytes into out and advances over them. It returns true on
  129. // success and false and error.
  130. func (s *String) ReadBytes(out *[]byte, n int) bool {
  131. v := s.read(n)
  132. if v == nil {
  133. return false
  134. }
  135. *out = v
  136. return true
  137. }
  138. // CopyBytes copies len(out) bytes into out and advances over them. It returns
  139. // true on success and false on error.
  140. func (s *String) CopyBytes(out []byte) bool {
  141. n := len(out)
  142. v := s.read(n)
  143. if v == nil {
  144. return false
  145. }
  146. return copy(out, v) == n
  147. }
  148. // Empty reports whether the string does not contain any bytes.
  149. func (s String) Empty() bool {
  150. return len(s) == 0
  151. }