tls.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright 2018 The GoPacket Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the LICENSE file in the root of the source
  5. // tree.
  6. package layers
  7. import (
  8. "encoding/binary"
  9. "errors"
  10. "github.com/google/gopacket"
  11. )
  12. // TLSType defines the type of data after the TLS Record
  13. type TLSType uint8
  14. // TLSType known values.
  15. const (
  16. TLSChangeCipherSpec TLSType = 20
  17. TLSAlert TLSType = 21
  18. TLSHandshake TLSType = 22
  19. TLSApplicationData TLSType = 23
  20. TLSUnknown TLSType = 255
  21. )
  22. // String shows the register type nicely formatted
  23. func (tt TLSType) String() string {
  24. switch tt {
  25. default:
  26. return "Unknown"
  27. case TLSChangeCipherSpec:
  28. return "Change Cipher Spec"
  29. case TLSAlert:
  30. return "Alert"
  31. case TLSHandshake:
  32. return "Handshake"
  33. case TLSApplicationData:
  34. return "Application Data"
  35. }
  36. }
  37. // TLSVersion represents the TLS version in numeric format
  38. type TLSVersion uint16
  39. // Strings shows the TLS version nicely formatted
  40. func (tv TLSVersion) String() string {
  41. switch tv {
  42. default:
  43. return "Unknown"
  44. case 0x0200:
  45. return "SSL 2.0"
  46. case 0x0300:
  47. return "SSL 3.0"
  48. case 0x0301:
  49. return "TLS 1.0"
  50. case 0x0302:
  51. return "TLS 1.1"
  52. case 0x0303:
  53. return "TLS 1.2"
  54. case 0x0304:
  55. return "TLS 1.3"
  56. }
  57. }
  58. // TLS is specified in RFC 5246
  59. //
  60. // TLS Record Protocol
  61. // 0 1 2 3 4 5 6 7 8
  62. // +--+--+--+--+--+--+--+--+
  63. // | Content Type |
  64. // +--+--+--+--+--+--+--+--+
  65. // | Version (major) |
  66. // +--+--+--+--+--+--+--+--+
  67. // | Version (minor) |
  68. // +--+--+--+--+--+--+--+--+
  69. // | Length |
  70. // +--+--+--+--+--+--+--+--+
  71. // | Length |
  72. // +--+--+--+--+--+--+--+--+
  73. // TLS is actually a slide of TLSrecord structures
  74. type TLS struct {
  75. BaseLayer
  76. // TLS Records
  77. ChangeCipherSpec []TLSChangeCipherSpecRecord
  78. Handshake []TLSHandshakeRecord
  79. AppData []TLSAppDataRecord
  80. Alert []TLSAlertRecord
  81. }
  82. // TLSRecordHeader contains all the information that each TLS Record types should have
  83. type TLSRecordHeader struct {
  84. ContentType TLSType
  85. Version TLSVersion
  86. Length uint16
  87. }
  88. // LayerType returns gopacket.LayerTypeTLS.
  89. func (t *TLS) LayerType() gopacket.LayerType { return LayerTypeTLS }
  90. // decodeTLS decodes the byte slice into a TLS type. It also
  91. // setups the application Layer in PacketBuilder.
  92. func decodeTLS(data []byte, p gopacket.PacketBuilder) error {
  93. t := &TLS{}
  94. err := t.DecodeFromBytes(data, p)
  95. if err != nil {
  96. return err
  97. }
  98. p.AddLayer(t)
  99. p.SetApplicationLayer(t)
  100. return nil
  101. }
  102. // DecodeFromBytes decodes the slice into the TLS struct.
  103. func (t *TLS) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  104. t.BaseLayer.Contents = data
  105. t.BaseLayer.Payload = nil
  106. t.ChangeCipherSpec = t.ChangeCipherSpec[:0]
  107. t.Handshake = t.Handshake[:0]
  108. t.AppData = t.AppData[:0]
  109. t.Alert = t.Alert[:0]
  110. return t.decodeTLSRecords(data, df)
  111. }
  112. func (t *TLS) decodeTLSRecords(data []byte, df gopacket.DecodeFeedback) error {
  113. if len(data) < 5 {
  114. df.SetTruncated()
  115. return errors.New("TLS record too short")
  116. }
  117. // since there are no further layers, the baselayer's content is
  118. // pointing to this layer
  119. // TODO: Consider removing this
  120. t.BaseLayer = BaseLayer{Contents: data[:len(data)]}
  121. var h TLSRecordHeader
  122. h.ContentType = TLSType(data[0])
  123. h.Version = TLSVersion(binary.BigEndian.Uint16(data[1:3]))
  124. h.Length = binary.BigEndian.Uint16(data[3:5])
  125. if h.ContentType.String() == "Unknown" {
  126. return errors.New("Unknown TLS record type")
  127. }
  128. hl := 5 // header length
  129. tl := hl + int(h.Length)
  130. if len(data) < tl {
  131. df.SetTruncated()
  132. return errors.New("TLS packet length mismatch")
  133. }
  134. switch h.ContentType {
  135. default:
  136. return errors.New("Unknown TLS record type")
  137. case TLSChangeCipherSpec:
  138. var r TLSChangeCipherSpecRecord
  139. e := r.decodeFromBytes(h, data[hl:tl], df)
  140. if e != nil {
  141. return e
  142. }
  143. t.ChangeCipherSpec = append(t.ChangeCipherSpec, r)
  144. case TLSAlert:
  145. var r TLSAlertRecord
  146. e := r.decodeFromBytes(h, data[hl:tl], df)
  147. if e != nil {
  148. return e
  149. }
  150. t.Alert = append(t.Alert, r)
  151. case TLSHandshake:
  152. var r TLSHandshakeRecord
  153. e := r.decodeFromBytes(h, data[hl:tl], df)
  154. if e != nil {
  155. return e
  156. }
  157. t.Handshake = append(t.Handshake, r)
  158. case TLSApplicationData:
  159. var r TLSAppDataRecord
  160. e := r.decodeFromBytes(h, data[hl:tl], df)
  161. if e != nil {
  162. return e
  163. }
  164. t.AppData = append(t.AppData, r)
  165. }
  166. if len(data) == tl {
  167. return nil
  168. }
  169. return t.decodeTLSRecords(data[tl:len(data)], df)
  170. }
  171. // CanDecode implements gopacket.DecodingLayer.
  172. func (t *TLS) CanDecode() gopacket.LayerClass {
  173. return LayerTypeTLS
  174. }
  175. // NextLayerType implements gopacket.DecodingLayer.
  176. func (t *TLS) NextLayerType() gopacket.LayerType {
  177. return gopacket.LayerTypeZero
  178. }
  179. // Payload returns nil, since TLS encrypted payload is inside TLSAppDataRecord
  180. func (t *TLS) Payload() []byte {
  181. return nil
  182. }
  183. // SerializeTo writes the serialized form of this layer into the
  184. // SerializationBuffer, implementing gopacket.SerializableLayer.
  185. func (t *TLS) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  186. if !opts.FixLengths {
  187. return errors.New("serialization of packets without fixing lengths has not been implemented yet")
  188. }
  189. totalLength := 0
  190. for range t.ChangeCipherSpec {
  191. totalLength += 5 + 1 // length of header + record
  192. }
  193. for range t.Handshake {
  194. totalLength += 5
  195. }
  196. for _, record := range t.AppData {
  197. totalLength += 5 + len(record.Payload)
  198. }
  199. for _, record := range t.Alert {
  200. if len(record.EncryptedMsg) == 0 {
  201. totalLength += 5 + 2
  202. } else {
  203. totalLength += 5 + len(record.EncryptedMsg)
  204. }
  205. }
  206. data, err := b.PrependBytes(totalLength)
  207. if err != nil {
  208. return err
  209. }
  210. off := 0
  211. for _, record := range t.ChangeCipherSpec {
  212. off = encodeHeader(record.TLSRecordHeader, data, off)
  213. data[off] = byte(record.Message)
  214. off++
  215. }
  216. for _, record := range t.Handshake {
  217. off = encodeHeader(record.TLSRecordHeader, data, off)
  218. // TODO
  219. }
  220. for _, record := range t.AppData {
  221. off = encodeHeader(record.TLSRecordHeader, data, off)
  222. copy(data[off:], record.Payload)
  223. off += len(record.Payload)
  224. }
  225. for _, record := range t.Alert {
  226. off = encodeHeader(record.TLSRecordHeader, data, off)
  227. if len(record.EncryptedMsg) == 0 {
  228. data[off] = byte(record.Level)
  229. data[off+1] = byte(record.Description)
  230. off += 2
  231. } else {
  232. copy(data[off:], record.EncryptedMsg)
  233. off += len(record.EncryptedMsg)
  234. }
  235. }
  236. return nil
  237. }
  238. func encodeHeader(header TLSRecordHeader, data []byte, offset int) int {
  239. data[offset] = byte(header.ContentType)
  240. binary.BigEndian.PutUint16(data[offset+1:], uint16(header.Version))
  241. binary.BigEndian.PutUint16(data[offset+3:], header.Length)
  242. return offset + 5
  243. }