Class: PacketGen::Header::EAP

Inherits:
Base
  • Object
show all
Defined in:
lib/packetgen/header/eap.rb,
lib/packetgen/header/eap/md5.rb,
lib/packetgen/header/eap/tls.rb,
lib/packetgen/header/eap/fast.rb,
lib/packetgen/header/eap/ttls.rb
more...

Overview

Extensible Authentication Protocol (EAP), RFC 3748

A EAP header has:

  • a #code field (BinStruct::Int8Enum),

  • a #id field (BinStruct::Int8),

  • a #length field (BinStruct::Int16).

Request (code 1) and Response (code 2) packets also have:

  • a #type field (BinStruct::Int8Enum).

And Expanded Types (type 254) packets also have:

Finally, all packets have a #body (BinStruct::String).

Specialized headers

Some EAP has a specialized class:

Header accessors

EAP headers may be accessed through Packet#eap accessor. As EAP has specialized subclasses (MD5, TLS, TTLS and FAST), these headers may be accessed through #eap_md5, #eap_tls, #eap_ttls and #eap_fast, respectively. But #eap is still here as a shortcut.

Parse EAP packets

When parsing an EAP packet, EAP subclass may be created from type value.

So result of parsing a EAP header may be a EAP, MD5, TLS, TTLS or FAST instance. But this instance is still accessible through Packet#eap.

Examples:

Create EAP headers

# create a request header with default type (1)
eap = PacketGen::Header::EAP.new(code: 1)
eap.human_code   #=> 'Request'
# the same
eap = PacketGen::Header::EAP.new(code: 'Request')
eap.code         #=> 1
# create a Response header of type Nak
nak = PacketGen::Header::EAP.new(code: 'Response', type: 'Nak')
nak.code      #=> 2
nak.type      #=> 3

Create a specialized EAP header

eap = PacketGen::Header::EAP::TLS.new(code: 2)
eap.class    #=> PacketGen::Header::EAP::TLS

Parse a specialized class from a binary string

pkt = PacketGen.parse("\x01\x00\x00\x0e\x04\x04\x00\x01\x02\x03name", first_header: 'EAP')
pkt.eap.class   # => PacketGen::Header::EAP::MD5

Author:

  • Sylvain Daubert

  • LemonTree55

Since:

  • 2.1.4

Direct Known Subclasses

MD5, TLS, TTLS

Defined Under Namespace

Classes: FAST, MD5, TLS, TTLS

Constant Summary collapse

CODES =

EAP known codes

Since:

  • 2.1.4

{
  'Request' => 1,
  'Response' => 2,
  'Success' => 3,
  'Failure' => 4
}.freeze
TYPES =

EAP known request/response types

Since:

  • 2.1.4

{
  'Identity' => 1,
  'Notification' => 2,
  'Nak' => 3,
  'MD5-Challenge' => 4,
  'One Time Password' => 5,
  'Generic Token Card' => 6,
  'EAP-TLS' => 13,
  'EAP-TTLS' => 21,
  'EAP-FAST' => 43,
  'Expanded Types' => 254,
  'Experimental Use' => 255
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

bind, calculate_and_set_length, #header_id, inherited, #ip_header, #ll_header

Methods included from PacketGen::Headerable

included, #method_name, #packet, #packet=, #parse?, #protocol_name, #to_s

Constructor Details

#initialize(options = {}) ⇒ EAP

Since:

  • 2.1.4

[View source]

139
140
141
142
# File 'lib/packetgen/header/eap.rb', line 139

def initialize(options={})
  super
  calc_length if options[:length].nil?
end

Instance Attribute Details

#bodyBinStruct::String, Headerable

EAP packet body

Returns:


136
# File 'lib/packetgen/header/eap.rb', line 136

define_attr :body, BinStruct::String

#codeInteger

8-bit EAP code. See known EAP codes

Returns:

  • (Integer)

96
# File 'lib/packetgen/header/eap.rb', line 96

define_attr :code, BinStruct::Int8Enum, enum: CODES

#idInteger

8-bit identifier

Returns:

  • (Integer)

101
# File 'lib/packetgen/header/eap.rb', line 101

define_attr :id, BinStruct::Int8

#lengthInteger

16-bit length of EAP packet

Returns:

  • (Integer)

106
# File 'lib/packetgen/header/eap.rb', line 106

define_attr :length, BinStruct::Int16, default: 4

#typeInteger

8-bit request or response type. This field is present only for Request or Response packets. See known EAP types.

Returns:

  • (Integer)

113
114
115
# File 'lib/packetgen/header/eap.rb', line 113

define_attr :type, BinStruct::Int8Enum,
enum: TYPES,
optional: lambda(&:type?)

#vendor_idInteger

24-bit vendor ID. This field is present only for Request or Response packets, with type equal to Expanded Types (254).

Returns:

  • (Integer)

122
123
# File 'lib/packetgen/header/eap.rb', line 122

define_attr :vendor_id, BinStruct::Int24,
optional: ->(eap) { eap.type? && (eap.type == 254) }

#vendor_typeInteger

32-bit vendor type. This field is present only for Request or Response packets, with type equal to Expanded Types (254).

Returns:

  • (Integer)

130
131
# File 'lib/packetgen/header/eap.rb', line 130

define_attr :vendor_type, BinStruct::Int32,
optional: ->(eap) { eap.type? && (eap.type == 254) }

Instance Method Details

#added_to_packet(packet) ⇒ void

This method returns an undefined value.

Callback called when a EAP header is added to a packet Here, add #eap method as a shortcut to existing #eap_(md5|tls|ttls|fast).

Parameters:

Since:

  • 2.1.4

[View source]

244
245
246
247
248
# File 'lib/packetgen/header/eap.rb', line 244

def added_to_packet(packet)
  return if packet.respond_to?(:eap)

  packet.instance_eval("def eap(arg=nil); header(#{self.class}, arg); end") # def eap(arg=nil); header(EAP, arg); end
end

#calc_lengthInteger

Calculate length field from content

Returns:

  • (Integer)

Since:

  • 2.1.4

[View source]

228
229
230
# File 'lib/packetgen/header/eap.rb', line 228

def calc_length
  Base.calculate_and_set_length(self)
end

#desired_auth_typeArray<Integer>

Return an array of desired authentication types from a Nak packet

Returns:

  • (Array<Integer>)

Raises:

Since:

  • 2.1.4

[View source]

220
221
222
223
224
# File 'lib/packetgen/header/eap.rb', line 220

def desired_auth_type
  raise ParseError, 'not a Nak response' unless nak?

  body.to_s.unpack('C*')
end

#failure?Boolean

Is packet a failure?

Returns:

  • (Boolean)

Since:

  • 2.1.4

[View source]

205
206
207
# File 'lib/packetgen/header/eap.rb', line 205

def failure?
  code == CODES['Failure']
end

#human_codeString

Get human readable code

Returns:

  • (String)

Since:

  • 2.1.4

[View source]

172
173
174
# File 'lib/packetgen/header/eap.rb', line 172

def human_code
  self[:code].to_human
end

#human_typeString

Get human readable type

Returns:

  • (String)

Raises:

  • (ParseError)

    not a Request nor a Response packet

Since:

  • 2.1.4

[View source]

179
180
181
182
183
# File 'lib/packetgen/header/eap.rb', line 179

def human_type
  raise ParseError, 'not a Request nor a Response' unless type?

  self[:type].to_human
end

#nak?Boolean

Is packet a NAK?

Returns:

  • (Boolean)

Author:

  • LemonTree55

Since:

  • 4.1.0

[View source]

213
214
215
# File 'lib/packetgen/header/eap.rb', line 213

def nak?
  (code == 2) && (type == 3)
end

#old_readObject

Since:

  • 2.1.4

[View source]

145
# File 'lib/packetgen/header/eap.rb', line 145

alias old_read read

#read(str) ⇒ EAP

Populate object from a binary string

Parameters:

  • str (String)

Returns:

  • (EAP)

    may return a subclass object if a more specific class may be determined

Since:

  • 2.1.4

[View source]

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/packetgen/header/eap.rb', line 151

def read(str)
  super
  return self unless self.instance_of?(EAP)
  return self unless type?

  case self.type
  when 4
    EAP::MD5.new.read(str)
  when 13
    EAP::TLS.new.read(str)
  when 21
    EAP::TTLS.new.read(str)
  when 43
    EAP::FAST.new.read(str)
  else
    self
  end
end

#reply!self

Invert between a request and a response packet. Not action for others codes.

Returns:

  • (self)

Since:

  • 2.1.4

[View source]

253
254
255
256
257
258
259
# File 'lib/packetgen/header/eap.rb', line 253

def reply!
  case self.code
  when 1 then self.code = 2
  when 2 then self.code = 1
  end
  self
end

#request?Boolean

Is packet a request?

Returns:

  • (Boolean)

Since:

  • 2.1.4

[View source]

187
188
189
# File 'lib/packetgen/header/eap.rb', line 187

def request?
  code == CODES['Request']
end

#response?Boolean

Is packet a response?

Returns:

  • (Boolean)

Since:

  • 2.1.4

[View source]

193
194
195
# File 'lib/packetgen/header/eap.rb', line 193

def response?
  code == CODES['Response']
end

#success?Boolean

Is packet a success?

Returns:

  • (Boolean)

Since:

  • 2.1.4

[View source]

199
200
201
# File 'lib/packetgen/header/eap.rb', line 199

def success?
  code == CODES['Success']
end

#type?Boolean

Say is this EAP header has #type field

Returns:

  • (Boolean)

Since:

  • 2.7.0

[View source]

235
236
237
# File 'lib/packetgen/header/eap.rb', line 235

def type?
  [1, 2].include?(self.code)
end