Skip to content

Commit 387854b

Browse files
committed
Merge branch 'packetgen4'
2 parents 0930594 + d98a06f commit 387854b

29 files changed

+232
-240
lines changed

Gemfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ gemspec
66

77
gem 'bundler', '>= 1.17', '< 3'
88

9+
group :development do
10+
gem 'rake', '~>13.0'
11+
gem 'rspec', '~>3.13'
12+
end
13+
914
group :noci do
1015
gem 'rubocop', '~> 1.12'
1116
gem 'rubocop-performance', '~> 1.13'

lib/packetgen/plugin/crypto.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def decipher(data)
7474
end
7575

7676
# Compute and set IV for deciphering mode
77-
# @param [PacketGen::Types::String] salt
77+
# @param [BinStruct::String] salt
7878
# @param [String] msg ciphered message
7979
# @return [String] iv
8080
def compute_iv_for_decrypting(salt, msg)
@@ -100,7 +100,7 @@ def compute_iv_for_decrypting(salt, msg)
100100
# @param [String] salt salt to use
101101
# @return [void]
102102
def compute_iv_for_encrypting(iv, salt) # rubocop:disable Naming/MethodParameterName
103-
real_iv = force_binary(salt) + force_binary(iv)
103+
real_iv = salt.b + iv.b
104104
real_iv += [1].pack('N') if confidentiality_mode == 'ctr'
105105
@conf.iv = real_iv
106106
end

lib/packetgen/plugin/esp.rb

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
module PacketGen::Plugin
1313
# A ESP header consists of:
14-
# * a Security Parameters Index (#{spi}, {PacketGen::Types::Int32} type),
14+
# * a Security Parameters Index (#{spi}, {BinStruct::Int32} type),
1515
# * a Sequence Number ({#sn}, +Int32+ type),
1616
# * a {#body} (variable length),
1717
# * an optional TFC padding ({#tfc}, variable length),
1818
# * an optional {#padding} (to align ESP on 32-bit boundary, variable length),
19-
# * a {#pad_length} ({PacketGen::Types::Int8}),
19+
# * a {#pad_length} ({BinStruct::Int8}),
2020
# * a Next header field ({#next}, +Int8+),
2121
# * and an optional Integrity Check Value ({#icv}, variable length).
2222
#
@@ -80,34 +80,34 @@ class ESP < PacketGen::Header::Base
8080
# @!attribute spi
8181
# 32-bit Security Parameter Index
8282
# @return [Integer]
83-
define_field :spi, PacketGen::Types::Int32
83+
define_attr :spi, BinStruct::Int32
8484
# @!attribute sn
8585
# 32-bit Sequence Number
8686
# @return [Integer]
87-
define_field :sn, PacketGen::Types::Int32
87+
define_attr :sn, BinStruct::Int32
8888
# @!attribute body
89-
# @return [PacketGen::Types::String,PacketGen::Header::Base]
90-
define_field :body, PacketGen::Types::String
89+
# @return [BinStruct::String,PacketGen::Header::Base]
90+
define_attr :body, BinStruct::String
9191
# @!attribute tfc
9292
# Traffic Flow Confidentiality padding
93-
# @return [PacketGen::Types::String,PacketGen::Header::Base]
94-
define_field :tfc, PacketGen::Types::String
93+
# @return [BinStruct::String,PacketGen::Header::Base]
94+
define_attr :tfc, BinStruct::String
9595
# @!attribute padding
9696
# ESP padding
97-
# @return [PacketGen::Types::String,PacketGen::Header::Base]
98-
define_field :padding, PacketGen::Types::String
97+
# @return [BinStruct::String,PacketGen::Header::Base]
98+
define_attr :padding, BinStruct::String
9999
# @!attribute pad_length
100100
# 8-bit padding length
101101
# @return [Integer]
102-
define_field :pad_length, PacketGen::Types::Int8
102+
define_attr :pad_length, BinStruct::Int8
103103
# @!attribute next
104104
# 8-bit next protocol value
105105
# @return [Integer]
106-
define_field :next, PacketGen::Types::Int8
106+
define_attr :next, BinStruct::Int8
107107
# @!attribute icv
108108
# Integrity Check Value
109-
# @return [PacketGen::Types::String,PacketGen::Header::Base]
110-
define_field :icv, PacketGen::Types::String
109+
# @return [BinStruct::String,PacketGen::Header::Base]
110+
define_attr :icv, BinStruct::String
111111

112112
# ICV (Integrity Check Value) length
113113
# @return [Integer]
@@ -140,11 +140,11 @@ def initialize(options={})
140140
def read(str)
141141
return self if str.nil?
142142

143-
force_binary str
144-
self[:spi].read str[0, 4]
145-
self[:sn].read str[4, 4]
146-
self[:tfc].read ''
147-
self[:padding].read ''
143+
str = str.b
144+
self[:spi].read(str[0, 4])
145+
self[:sn].read(str[4, 4])
146+
self[:tfc].read('')
147+
self[:padding].read('')
148148

149149
read_icv_dependent_fields(str[8..-1])
150150
read_icv(str)
@@ -239,7 +239,7 @@ def read_icv(str)
239239
def get_auth_data(opt)
240240
ad = self[:spi].to_s
241241
if opt[:esn]
242-
@esn = PacketGen::Types::Int32.new(opt[:esn])
242+
@esn = BinStruct::Int32.new(value: opt[:esn])
243243
ad << @esn.to_s if @conf.authenticated?
244244
end
245245
ad << self[:sn].to_s
@@ -273,12 +273,12 @@ def encrypt_set_pad_length
273273
def encrypt_set_padding(opt)
274274
if opt[:pad_length]
275275
self.pad_length = opt[:pad_length]
276-
padding = force_binary(opt[:padding] || (1..self.pad_length).to_a.pack('C*'))
277-
self[:padding].read padding
276+
padding = opt[:padding] || (1..self.pad_length).to_a.pack('C*')
278277
else
279-
padding = force_binary(opt[:padding] || (1..self.pad_length).to_a.pack('C*'))
280-
self[:padding].read padding[0...self.pad_length]
278+
padding = opt[:padding] || (1..self.pad_length).to_a.pack('C*')
279+
padding = padding[0...self.pad_length]
281280
end
281+
self[:padding].read(padding)
282282
end
283283

284284
def generate_tfc(opt)
@@ -293,7 +293,7 @@ def generate_tfc(opt)
293293
else
294294
(tfc_size / 4) * 4
295295
end
296-
tfc = force_binary("\0" * tfc_size)
296+
tfc = "\0".b * tfc_size
297297
end
298298
tfc
299299
end
@@ -309,7 +309,7 @@ def encrypt_body(opt, iv) # rubocop:disable Naming/MethodParameterName
309309
end
310310

311311
def encrypt_set_encrypted_fields(msg, iv) # rubocop:disable Naming/MethodParameterName
312-
self[:body] = PacketGen::Types::String.new.read(iv)
312+
self[:body] = BinStruct::String.new.read(iv)
313313
self[:body] << msg[0..-3]
314314
self[:pad_length].read msg[-2]
315315
self[:next].read msg[-1]
@@ -433,7 +433,7 @@ def remove_tfc_if_needed(real_length)
433433
lambda { |f|
434434
(f.dport == ESP::UDP_PORT ||
435435
f.sport == ESP::UDP_PORT) &&
436-
PacketGen::Types::Int32.new.read(f.body[0..3]).to_i.positive?
436+
BinStruct::Int32.new.read(f.body[0..3]).to_i.positive?
437437
}]
438438
ESP.bind PacketGen::Header::IP, next: 4
439439
ESP.bind PacketGen::Header::IPv6, next: 41

lib/packetgen/plugin/ike.rb

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ class NonESPMarker < PacketGen::Header::Base
1313
# @!attribute non_esp_marker
1414
# 32-bit zero marker to differentiate IKE packet over UDP port 4500 from ESP ones
1515
# @return [Integer]
16-
define_field :non_esp_marker, PacketGen::Types::Int32, default: 0
16+
define_attr :non_esp_marker, BinStruct::Int32, default: 0
1717
# @!attribute body
18-
# @return [PacketGen::Types::String,PacketGen::Header::Base]
19-
define_field :body, PacketGen::Types::String
18+
# @return [BinStruct::String,PacketGen::Header::Base]
19+
define_attr :body, BinStruct::String
2020

2121
# Check non_esp_marker field
2222
# @see [PacketGen::Header::Base#parse?]
@@ -48,15 +48,15 @@ def parse?
4848
# | Length |
4949
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
5050
# A IKE Plugin consists of:
51-
# * a IKE SA initiator SPI ({#init_spi}, {PacketGen::Types::Int64} type),
52-
# * a IKE SA responder SPI ({#resp_spi}, {PacketGen::Types::Int64} type),
53-
# * a Next Payload field ({#next}, {PacketGen::Types::Int8} type),
54-
# * a Version field ({#version}, {PacketGen::Types::Int8} type, with first 4-bit field
51+
# * a IKE SA initiator SPI ({#init_spi}, {BinStruct::Int64} type),
52+
# * a IKE SA responder SPI ({#resp_spi}, {BinStruct::Int64} type),
53+
# * a Next Payload field ({#next}, {BinStruct::Int8} type),
54+
# * a Version field ({#version}, {BinStruct::Int8} type, with first 4-bit field
5555
# as major number, and last 4-bit field as minor number),
56-
# * a Exchange type ({#exchange_type}, {PacketGen::Types::Int8} type),
57-
# * a {#flags} field ({PacketGen::Types::Int8} type),
58-
# * a Message ID ({#message_id}, {PacketGen::Types::Int32} type),
59-
# * and a {#length} ({PacketGen::Types::Int32} type).
56+
# * a Exchange type ({#exchange_type}, {BinStruct::Int8} type),
57+
# * a {#flags} field ({BinStruct::Int8} type),
58+
# * a Message ID ({#message_id}, {BinStruct::Int32} type),
59+
# * and a {#length} ({BinStruct::Int32} type).
6060
#
6161
# == Create a IKE Plugin
6262
# === Standalone
@@ -94,48 +94,32 @@ class IKE < PacketGen::Header::Base
9494
# @!attribute init_spi
9595
# 64-bit initiator SPI
9696
# @return [Integer]
97-
define_field :init_spi, PacketGen::Types::Int64
97+
define_attr :init_spi, BinStruct::Int64
9898
# @!attribute resp_spi
9999
# 64-bit responder SPI
100100
# @return [Integer]
101-
define_field :resp_spi, PacketGen::Types::Int64
101+
define_attr :resp_spi, BinStruct::Int64
102102
# @!attribute next
103103
# 8-bit next payload type
104104
# @return [Integer]
105-
define_field :next, PacketGen::Types::Int8
105+
define_attr :next, BinStruct::Int8
106106
# @!attribute version
107107
# 8-bit IKE version
108108
# @return [Integer]
109-
define_field :version, PacketGen::Types::Int8, default: 0x20
109+
# @!attribute mjver
110+
# 4-bit major version value ({#version}'s 4 most significant bits)
111+
# @return [Integer]
112+
# @!attribute mnver
113+
# 4-bit minor version value ({#version}'s 4 least significant bits)
114+
# @return [Integer]
115+
define_bit_attr :version, default: 0x20, mjver: 4, mver: 4
110116
# @!attribute [r] exchange_type
111117
# 8-bit exchange type
112118
# @return [Integer]
113-
define_field :exchange_type, PacketGen::Types::Int8Enum, enum: EXCHANGE_TYPES
114-
# @!attribute flags
119+
define_attr :exchange_type, BinStruct::Int8Enum, enum: EXCHANGE_TYPES
120+
# @!attribute flags. See {#flag_r}, {#flag_v} and {#flag_i}.
115121
# 8-bit flags
116122
# @return [Integer]
117-
define_field :flags, PacketGen::Types::Int8
118-
# @!attribute message_id
119-
# 32-bit message ID
120-
# @return [Integer]
121-
define_field :message_id, PacketGen::Types::Int32
122-
# @!attribute length
123-
# 32-bit length of total message (Plugin + payloads)
124-
# @return [Integer]
125-
define_field :length, PacketGen::Types::Int32
126-
127-
# Defining a body permits using Packet#parse to parse IKE payloads.
128-
# But this method is hidden as prefered way to access payloads is via #payloads
129-
define_field :body, PacketGen::Types::String
130-
131-
# @!attribute mjver
132-
# 4-bit major version value
133-
# @return [Integer]
134-
# @!attribute mnver
135-
# 4-bit minor version value
136-
# @return [Integer]
137-
define_bit_fields_on :version, :mjver, 4, :mnver, 4
138-
139123
# @!attribute rsv1
140124
# @return [Integer]
141125
# @!attribute rsv2
@@ -149,7 +133,19 @@ class IKE < PacketGen::Header::Base
149133
# @!attribute flag_v
150134
# version flag. Ignored by IKEv2 peers, and should be set to 0
151135
# @return [Boolean]
152-
define_bit_fields_on :flags, :rsv1, 2, :flag_r, :flag_v, :flag_i, :rsv2, 3
136+
define_bit_attr :flags, rsv1: 2, flag_r: 1, flag_v: 1, flag_i: 1, rsv2: 3
137+
# @!attribute message_id
138+
# 32-bit message ID
139+
# @return [Integer]
140+
define_attr :message_id, BinStruct::Int32
141+
# @!attribute length
142+
# 32-bit length of total message (Plugin + payloads)
143+
# @return [Integer]
144+
define_attr :length, BinStruct::Int32
145+
146+
# Defining a body permits using Packet#parse to parse IKE payloads.
147+
# But this method is hidden as prefered way to access payloads is via #payloads
148+
define_attr :body, BinStruct::String
153149

154150
# @param [Hash] options
155151
# @see PacketGen::Header::Base#initialize

lib/packetgen/plugin/ike/auth.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class Auth < Payload
5353
# @attribute [r] auth_method
5454
# 8-bit Auth Method
5555
# @return [Integer]
56-
define_field_before :content, :auth_method, PacketGen::Types::Int8Enum, enum: METHODS
56+
define_attr_before :content, :auth_method, BinStruct::Int8Enum, enum: METHODS
5757
# @attribute reserved
5858
# 24-bit reserved field
5959
# @return [Integer]
60-
define_field_before :content, :reserved, PacketGen::Types::Int24
60+
define_attr_before :content, :reserved, BinStruct::Int24
6161

6262
# Check authentication (see RFC 7296 §2.15)
6363
# @param [Packet] init_msg first IKE message sent by peer

lib/packetgen/plugin/ike/cert.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Cert < Payload
5656
# @attribute encoding
5757
# 8-bit certificate encoding
5858
# @return [Integer]
59-
define_field_before :content, :encoding, PacketGen::Types::Int8Enum, enum: ENCODINGS
59+
define_attr_before :content, :encoding, BinStruct::Int8Enum, enum: ENCODINGS
6060

6161
def initialize(options={})
6262
super

lib/packetgen/plugin/ike/id.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ class IDi < Payload
5353
# @attribute [r] type
5454
# 8-bit ID type
5555
# @return [Integer]
56-
define_field_before :content, :type, PacketGen::Types::Int8Enum, enum: TYPES
56+
define_attr_before :content, :type, BinStruct::Int8Enum, enum: TYPES
5757
# @attribute reserved
5858
# 24-bit reserved field
5959
# @return [Integer]
60-
define_field_before :content, :reserved, PacketGen::Types::Int24
60+
define_attr_before :content, :reserved, BinStruct::Int24
6161

6262
# Get ID type name
6363
# @return [String]

lib/packetgen/plugin/ike/ke.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class IKE
2424
# | |
2525
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
2626
# These specific fields are:
27-
# * {#group_num} (type {PacketGen::Types::Int16}),
28-
# * {#reserved} (type {PacketGen::Types::Int16}),
29-
# * and {#content} (type {PacketGen::Types::String}).
27+
# * {#group_num} (type {BinStruct::Int16}),
28+
# * {#reserved} (type {BinStruct::Int16}),
29+
# * and {#content} (type {BinStruct::String}).
3030
#
3131
# == Create a KE payload
3232
# # Create a IKE packet with a KE payload
@@ -44,11 +44,11 @@ class KE < Payload
4444
# @!attribute group_num
4545
# 16-bit DH group number
4646
# @return [Integer]
47-
define_field_before :content, :group_num, PacketGen::Types::Int16
47+
define_attr_before :content, :group_num, BinStruct::Int16
4848
# @!attribute reserved
4949
# 16-bit reserved field
5050
# @return [Integer]
51-
define_field_before :content, :reserved, PacketGen::Types::Int16, default: 0
51+
define_attr_before :content, :reserved, BinStruct::Int16, default: 0
5252

5353
def initialize(options={})
5454
super

lib/packetgen/plugin/ike/nonce.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class IKE
1111
# This class handles Nonce payloads, as defined in RFC 7296 §3.9.
1212
#
1313
# A Nonce payload contains a generic payload Plugin (see {Payload}) and
14-
# data field (type {PacketGen::Types::String}):
14+
# data field (type {BinStruct::String}):
1515
# 1 2 3
1616
# 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
1717
# +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

0 commit comments

Comments
 (0)