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


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

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

Instance Attribute Details

#bodyTypes::String, Header::Base


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

define_field :body, Types::String

#codeInteger

Returns 8-bit EAP code.

Returns:

  • (Integer)

    8-bit EAP code


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

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

#idInteger

Returns 8-bit identifier.

Returns:

  • (Integer)

    8-bit identifier


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

define_field :id, Types::Int8

#lengthInteger

Returns 16-bit length of EAP packet.

Returns:

  • (Integer)

    16-bit length of EAP packet


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

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


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

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


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

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


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

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


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

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


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

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


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

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


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

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

#human_codeString

Get human readable code

Returns:

  • (String)

Since:

  • 2.1.4


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

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


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

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

  self[:type].to_human
end

#old_readObject

Since:

  • 2.1.4


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

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


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

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


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

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


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

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

#response?Boolean

Is packet a response?

Returns:

  • (Boolean)

Since:

  • 2.1.4


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

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

#success?Boolean

Is packet a success?

Returns:

  • (Boolean)

Since:

  • 2.1.4


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

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

#type?Boolean

Say is this EAP header has #type field

Returns:

  • (Boolean)

Since:

  • 2.7.0


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

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