eapol.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. // Copyright 2012 Google, Inc. 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. "fmt"
  10. "github.com/google/gopacket"
  11. )
  12. // EAPOL defines an EAP over LAN (802.1x) layer.
  13. type EAPOL struct {
  14. BaseLayer
  15. Version uint8
  16. Type EAPOLType
  17. Length uint16
  18. }
  19. // LayerType returns LayerTypeEAPOL.
  20. func (e *EAPOL) LayerType() gopacket.LayerType { return LayerTypeEAPOL }
  21. // DecodeFromBytes decodes the given bytes into this layer.
  22. func (e *EAPOL) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  23. e.Version = data[0]
  24. e.Type = EAPOLType(data[1])
  25. e.Length = binary.BigEndian.Uint16(data[2:4])
  26. e.BaseLayer = BaseLayer{data[:4], data[4:]}
  27. return nil
  28. }
  29. // SerializeTo writes the serialized form of this layer into the
  30. // SerializationBuffer, implementing gopacket.SerializableLayer
  31. func (e *EAPOL) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  32. bytes, _ := b.PrependBytes(4)
  33. bytes[0] = e.Version
  34. bytes[1] = byte(e.Type)
  35. binary.BigEndian.PutUint16(bytes[2:], e.Length)
  36. return nil
  37. }
  38. // CanDecode returns the set of layer types that this DecodingLayer can decode.
  39. func (e *EAPOL) CanDecode() gopacket.LayerClass {
  40. return LayerTypeEAPOL
  41. }
  42. // NextLayerType returns the layer type contained by this DecodingLayer.
  43. func (e *EAPOL) NextLayerType() gopacket.LayerType {
  44. return e.Type.LayerType()
  45. }
  46. func decodeEAPOL(data []byte, p gopacket.PacketBuilder) error {
  47. e := &EAPOL{}
  48. return decodingLayerDecoder(e, data, p)
  49. }
  50. // EAPOLKeyDescriptorType is an enumeration of key descriptor types
  51. // as specified by 802.1x in the EAPOL-Key frame
  52. type EAPOLKeyDescriptorType uint8
  53. // Enumeration of EAPOLKeyDescriptorType
  54. const (
  55. EAPOLKeyDescriptorTypeRC4 EAPOLKeyDescriptorType = 1
  56. EAPOLKeyDescriptorTypeDot11 EAPOLKeyDescriptorType = 2
  57. EAPOLKeyDescriptorTypeWPA EAPOLKeyDescriptorType = 254
  58. )
  59. func (kdt EAPOLKeyDescriptorType) String() string {
  60. switch kdt {
  61. case EAPOLKeyDescriptorTypeRC4:
  62. return "RC4"
  63. case EAPOLKeyDescriptorTypeDot11:
  64. return "802.11"
  65. case EAPOLKeyDescriptorTypeWPA:
  66. return "WPA"
  67. default:
  68. return fmt.Sprintf("unknown descriptor type %d", kdt)
  69. }
  70. }
  71. // EAPOLKeyDescriptorVersion is an enumeration of versions specifying the
  72. // encryption algorithm for the key data and the authentication for the
  73. // message integrity code (MIC)
  74. type EAPOLKeyDescriptorVersion uint8
  75. // Enumeration of EAPOLKeyDescriptorVersion
  76. const (
  77. EAPOLKeyDescriptorVersionOther EAPOLKeyDescriptorVersion = 0
  78. EAPOLKeyDescriptorVersionRC4HMACMD5 EAPOLKeyDescriptorVersion = 1
  79. EAPOLKeyDescriptorVersionAESHMACSHA1 EAPOLKeyDescriptorVersion = 2
  80. EAPOLKeyDescriptorVersionAES128CMAC EAPOLKeyDescriptorVersion = 3
  81. )
  82. func (v EAPOLKeyDescriptorVersion) String() string {
  83. switch v {
  84. case EAPOLKeyDescriptorVersionOther:
  85. return "Other"
  86. case EAPOLKeyDescriptorVersionRC4HMACMD5:
  87. return "RC4-HMAC-MD5"
  88. case EAPOLKeyDescriptorVersionAESHMACSHA1:
  89. return "AES-HMAC-SHA1-128"
  90. case EAPOLKeyDescriptorVersionAES128CMAC:
  91. return "AES-128-CMAC"
  92. default:
  93. return fmt.Sprintf("unknown version %d", v)
  94. }
  95. }
  96. // EAPOLKeyType is an enumeration of key derivation types describing
  97. // the purpose of the keys being derived.
  98. type EAPOLKeyType uint8
  99. // Enumeration of EAPOLKeyType
  100. const (
  101. EAPOLKeyTypeGroupSMK EAPOLKeyType = 0
  102. EAPOLKeyTypePairwise EAPOLKeyType = 1
  103. )
  104. func (kt EAPOLKeyType) String() string {
  105. switch kt {
  106. case EAPOLKeyTypeGroupSMK:
  107. return "Group/SMK"
  108. case EAPOLKeyTypePairwise:
  109. return "Pairwise"
  110. default:
  111. return fmt.Sprintf("unknown key type %d", kt)
  112. }
  113. }
  114. // EAPOLKey defines an EAPOL-Key frame for 802.1x authentication
  115. type EAPOLKey struct {
  116. BaseLayer
  117. KeyDescriptorType EAPOLKeyDescriptorType
  118. KeyDescriptorVersion EAPOLKeyDescriptorVersion
  119. KeyType EAPOLKeyType
  120. KeyIndex uint8
  121. Install bool
  122. KeyACK bool
  123. KeyMIC bool
  124. Secure bool
  125. MICError bool
  126. Request bool
  127. HasEncryptedKeyData bool
  128. SMKMessage bool
  129. KeyLength uint16
  130. ReplayCounter uint64
  131. Nonce []byte
  132. IV []byte
  133. RSC uint64
  134. ID uint64
  135. MIC []byte
  136. KeyDataLength uint16
  137. EncryptedKeyData []byte
  138. }
  139. // LayerType returns LayerTypeEAPOLKey.
  140. func (ek *EAPOLKey) LayerType() gopacket.LayerType {
  141. return LayerTypeEAPOLKey
  142. }
  143. // NextLayerType returns layers.LayerTypeDot11InformationElement if the key
  144. // data exists and is unencrypted, otherwise it does not expect a next layer.
  145. func (ek *EAPOLKey) NextLayerType() gopacket.LayerType {
  146. if !ek.HasEncryptedKeyData && ek.KeyDataLength > 0 {
  147. return LayerTypeDot11InformationElement
  148. }
  149. return gopacket.LayerTypePayload
  150. }
  151. const eapolKeyFrameLen = 95
  152. // DecodeFromBytes decodes the given bytes into this layer.
  153. func (ek *EAPOLKey) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  154. if len(data) < eapolKeyFrameLen {
  155. df.SetTruncated()
  156. return fmt.Errorf("EAPOLKey length %v too short, %v required",
  157. len(data), eapolKeyFrameLen)
  158. }
  159. ek.KeyDescriptorType = EAPOLKeyDescriptorType(data[0])
  160. info := binary.BigEndian.Uint16(data[1:3])
  161. ek.KeyDescriptorVersion = EAPOLKeyDescriptorVersion(info & 0x0007)
  162. ek.KeyType = EAPOLKeyType((info & 0x0008) >> 3)
  163. ek.KeyIndex = uint8((info & 0x0030) >> 4)
  164. ek.Install = (info & 0x0040) != 0
  165. ek.KeyACK = (info & 0x0080) != 0
  166. ek.KeyMIC = (info & 0x0100) != 0
  167. ek.Secure = (info & 0x0200) != 0
  168. ek.MICError = (info & 0x0400) != 0
  169. ek.Request = (info & 0x0800) != 0
  170. ek.HasEncryptedKeyData = (info & 0x1000) != 0
  171. ek.SMKMessage = (info & 0x2000) != 0
  172. ek.KeyLength = binary.BigEndian.Uint16(data[3:5])
  173. ek.ReplayCounter = binary.BigEndian.Uint64(data[5:13])
  174. ek.Nonce = data[13:45]
  175. ek.IV = data[45:61]
  176. ek.RSC = binary.BigEndian.Uint64(data[61:69])
  177. ek.ID = binary.BigEndian.Uint64(data[69:77])
  178. ek.MIC = data[77:93]
  179. ek.KeyDataLength = binary.BigEndian.Uint16(data[93:95])
  180. totalLength := eapolKeyFrameLen + int(ek.KeyDataLength)
  181. if len(data) < totalLength {
  182. df.SetTruncated()
  183. return fmt.Errorf("EAPOLKey data length %d too short, %d required",
  184. len(data)-eapolKeyFrameLen, ek.KeyDataLength)
  185. }
  186. if ek.HasEncryptedKeyData {
  187. ek.EncryptedKeyData = data[eapolKeyFrameLen:totalLength]
  188. ek.BaseLayer = BaseLayer{
  189. Contents: data[:totalLength],
  190. Payload: data[totalLength:],
  191. }
  192. } else {
  193. ek.BaseLayer = BaseLayer{
  194. Contents: data[:eapolKeyFrameLen],
  195. Payload: data[eapolKeyFrameLen:],
  196. }
  197. }
  198. return nil
  199. }
  200. // SerializeTo writes the serialized form of this layer into the
  201. // SerializationBuffer, implementing gopacket.SerializableLayer.
  202. // See the docs for gopacket.SerializableLayer for more info.
  203. func (ek *EAPOLKey) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  204. buf, err := b.PrependBytes(eapolKeyFrameLen + len(ek.EncryptedKeyData))
  205. if err != nil {
  206. return err
  207. }
  208. buf[0] = byte(ek.KeyDescriptorType)
  209. var info uint16
  210. info |= uint16(ek.KeyDescriptorVersion)
  211. info |= uint16(ek.KeyType) << 3
  212. info |= uint16(ek.KeyIndex) << 4
  213. if ek.Install {
  214. info |= 0x0040
  215. }
  216. if ek.KeyACK {
  217. info |= 0x0080
  218. }
  219. if ek.KeyMIC {
  220. info |= 0x0100
  221. }
  222. if ek.Secure {
  223. info |= 0x0200
  224. }
  225. if ek.MICError {
  226. info |= 0x0400
  227. }
  228. if ek.Request {
  229. info |= 0x0800
  230. }
  231. if ek.HasEncryptedKeyData {
  232. info |= 0x1000
  233. }
  234. if ek.SMKMessage {
  235. info |= 0x2000
  236. }
  237. binary.BigEndian.PutUint16(buf[1:3], info)
  238. binary.BigEndian.PutUint16(buf[3:5], ek.KeyLength)
  239. binary.BigEndian.PutUint64(buf[5:13], ek.ReplayCounter)
  240. copy(buf[13:45], ek.Nonce)
  241. copy(buf[45:61], ek.IV)
  242. binary.BigEndian.PutUint64(buf[61:69], ek.RSC)
  243. binary.BigEndian.PutUint64(buf[69:77], ek.ID)
  244. copy(buf[77:93], ek.MIC)
  245. binary.BigEndian.PutUint16(buf[93:95], ek.KeyDataLength)
  246. if len(ek.EncryptedKeyData) > 0 {
  247. copy(buf[95:95+len(ek.EncryptedKeyData)], ek.EncryptedKeyData)
  248. }
  249. return nil
  250. }
  251. func decodeEAPOLKey(data []byte, p gopacket.PacketBuilder) error {
  252. ek := &EAPOLKey{}
  253. return decodingLayerDecoder(ek, data, p)
  254. }