Class: PacketGen::Header::EAP

Inherits:
Base 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

Overview

Extensible Authentication Protocol (EAP), RFC 3748

A EAP header has:

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

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

And Expanded Types (type 254) packets also have:

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

Create EAP headers

An EAP header may be created this way:

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

Specialized headers

Some EAP has a specialized class:

  • EAP-MD5,

  • EAP-TLS,

  • EAP-TTLS,

  • EAP-FAST.

Creating such a header is fairly simple:

# Generate a EAP-TLS Response (type is forced to 13)
eap = EAP::TLS.new(code: 2)     # => PacketGen::Header::EAP::TLS

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.

Author:

  • Sylvain Daubert

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

Methods inherited from Types::Fields

#[], #[]=, #bits_on, define_bit_fields_on, define_field, define_field_after, define_field_before, #fields, fields, inherited, #inspect, #offset_of, #optional?, #optional_fields, #present?, remove_bit_fields_on, remove_field, #sz, #to_h, #to_s, update_field

Constructor Details

#initialize(options = {}) ⇒ EAP

Since:

  • 2.1.4



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

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

Instance Attribute Details

#bodyTypes::String, Header::Base



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

define_field :body, Types::String

#codeInteger

Returns 8-bit EAP code.

Returns:

  • (Integer)

    8-bit EAP code



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

define_field :code, Types::Int8Enum, enum: CODES

#idInteger

Returns 8-bit identifier.

Returns:

  • (Integer)

    8-bit identifier



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

define_field :id, Types::Int8

#lengthInteger

Returns 16-bit length of EAP packet.

Returns:

  • (Integer)

    16-bit length of EAP packet



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

define_field :length, Types::Int16, default: 4

#typeInteger

This field is present only for Request or Response packets, with type different from Expanded Types (254).

Returns:

  • (Integer)

    8-bit request or response type



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

define_field :type, Types::Int8Enum,
enum: TYPES,
optional: ->(eap) { eap.type? }

#vendor_idInteger

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

Returns:

  • (Integer)

    24-bit vendor ID



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

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

#vendor_typeInteger

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

Returns:

  • (Integer)

    32-bit vendor type



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

define_field :vendor_type, Types::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



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

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



203
204
205
# File 'lib/packetgen/header/eap.rb', line 203

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



195
196
197
198
199
# File 'lib/packetgen/header/eap.rb', line 195

def desired_auth_type
  raise ParseError, 'not a Nak response' if (code != 2) && (type != 3)

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

#failure?Boolean

Is packet a failure?

Returns:

  • (Boolean)

Since:

  • 2.1.4



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

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

#human_codeString

Get human readable code

Returns:

  • (String)

Since:

  • 2.1.4



155
156
157
# File 'lib/packetgen/header/eap.rb', line 155

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



162
163
164
165
166
# File 'lib/packetgen/header/eap.rb', line 162

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

  self[:type].to_human
end

#old_readObject

Since:

  • 2.1.4



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

alias old_read read

#read(str) ⇒ Dot11

Populate object from a binary string

Parameters:

  • str (String)

Returns:

  • (Dot11)

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

Since:

  • 2.1.4



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/packetgen/header/eap.rb', line 134

def read(str)
  super(str)
  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



228
229
230
231
232
233
234
# File 'lib/packetgen/header/eap.rb', line 228

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



170
171
172
# File 'lib/packetgen/header/eap.rb', line 170

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

#response?Boolean

Is packet a response?

Returns:

  • (Boolean)

Since:

  • 2.1.4



176
177
178
# File 'lib/packetgen/header/eap.rb', line 176

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

#success?Boolean

Is packet a success?

Returns:

  • (Boolean)

Since:

  • 2.1.4



182
183
184
# File 'lib/packetgen/header/eap.rb', line 182

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

#type?Boolean

Say is this EAP header has #type field

Returns:

  • (Boolean)

Since:

  • 2.7.0



210
211
212
# File 'lib/packetgen/header/eap.rb', line 210

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