Skip to content

Commit 9122e94

Browse files
committed
Added the min_version: and max_version: keyword arguments (closes #499).
1 parent 864956d commit 9122e94

File tree

3 files changed

+331
-7
lines changed

3 files changed

+331
-7
lines changed

lib/ronin/support/network/ssl.rb

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,12 @@ def self.cert=(new_cert)
104104
# @param [1, 1.1, 1.2, Symbol, nil] version
105105
# The SSL version to use.
106106
#
107+
# @param [1, 1.1, 1.2, Symbol, nil] min_version
108+
# The minimum SSL version to use.
109+
#
110+
# @param [1, 1.1, 1.2, Symbol, nil] version
111+
# The maximum SSL version to use.
112+
#
107113
# @param [Symbol, Boolean] verify
108114
# Specifies whether to verify the SSL certificate.
109115
# May be one of the following:
@@ -139,19 +145,29 @@ def self.cert=(new_cert)
139145
#
140146
# @since 1.0.0
141147
#
142-
def self.context(version: nil,
143-
verify: :none,
144-
key: nil,
145-
key_file: nil,
146-
cert: nil,
147-
cert_file: nil,
148-
ca_bundle: nil)
148+
def self.context(version: nil,
149+
min_version: nil,
150+
max_version: nil,
151+
verify: :none,
152+
key: nil,
153+
key_file: nil,
154+
cert: nil,
155+
cert_file: nil,
156+
ca_bundle: nil)
149157
context = OpenSSL::SSL::SSLContext.new
150158

151159
if version
152160
version = VERSIONS.fetch(version,version)
153161

154162
context.min_version = context.max_version = version
163+
else min_version || max_version
164+
if min_version
165+
context.min_version = VERSIONS.fetch(min_version,min_version)
166+
end
167+
168+
if max_version
169+
context.max_version = VERSIONS.fetch(max_version,max_version)
170+
end
155171
end
156172

157173
context.verify_mode = VERIFY[verify]

spec/network/ssl/mixin_spec.rb

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,158 @@
128128
end
129129
end
130130

131+
context "when given the min_version: keyword argument" do
132+
let(:context) { double(OpenSSL::SSL::SSLContext) }
133+
134+
context "and it's 1" do
135+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_VERSION" do
136+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
137+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_VERSION)
138+
allow(context).to receive(:verify_mode=).with(0)
139+
140+
subject.ssl_context(min_version: 1)
141+
end
142+
end
143+
144+
context "and it's 1.1" do
145+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_1_VERSION" do
146+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
147+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
148+
allow(context).to receive(:verify_mode=).with(0)
149+
150+
subject.ssl_context(min_version: 1.1)
151+
end
152+
end
153+
154+
context "and it's 1_2" do
155+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_2_VERSION" do
156+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
157+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
158+
allow(context).to receive(:verify_mode=).with(0)
159+
160+
subject.ssl_context(min_version: 1.2)
161+
end
162+
end
163+
164+
context "and it's a Symbol" do
165+
let(:symbol) { :TLS1 }
166+
167+
it "must call OpenSSL::SSL::SSLContext#min_version= with the Symbol" do
168+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
169+
expect(context).to receive(:min_version=).with(symbol)
170+
allow(context).to receive(:verify_mode=).with(0)
171+
172+
subject.ssl_context(min_version: symbol)
173+
end
174+
175+
context "but it's :TLSv1" do
176+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_VERSION" do
177+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
178+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_VERSION)
179+
allow(context).to receive(:verify_mode=).with(0)
180+
181+
subject.ssl_context(min_version: :TLSv1)
182+
end
183+
end
184+
185+
context "but it's :TLSv1_1" do
186+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_1_VERSION" do
187+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
188+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
189+
allow(context).to receive(:verify_mode=).with(0)
190+
191+
subject.ssl_context(min_version: :TLSv1_1)
192+
end
193+
end
194+
195+
context "but it's :TLSv1_2" do
196+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_2_VERSION" do
197+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
198+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
199+
allow(context).to receive(:verify_mode=).with(0)
200+
201+
subject.ssl_context(min_version: :TLSv1_2)
202+
end
203+
end
204+
end
205+
end
206+
207+
context "when given the max_version: keyword argument" do
208+
let(:context) { double(OpenSSL::SSL::SSLContext) }
209+
210+
context "and it's 1" do
211+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_VERSION" do
212+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
213+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_VERSION)
214+
allow(context).to receive(:verify_mode=).with(0)
215+
216+
subject.ssl_context(max_version: 1)
217+
end
218+
end
219+
220+
context "and it's 1.1" do
221+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_1_VERSION" do
222+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
223+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
224+
allow(context).to receive(:verify_mode=).with(0)
225+
226+
subject.ssl_context(max_version: 1.1)
227+
end
228+
end
229+
230+
context "and it's 1_2" do
231+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_2_VERSION" do
232+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
233+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
234+
allow(context).to receive(:verify_mode=).with(0)
235+
236+
subject.ssl_context(max_version: 1.2)
237+
end
238+
end
239+
240+
context "and it's a Symbol" do
241+
let(:symbol) { :TLS1 }
242+
243+
it "must call OpenSSL::SSL::SSLContext#max_version= with the Symbol" do
244+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
245+
expect(context).to receive(:max_version=).with(symbol)
246+
allow(context).to receive(:verify_mode=).with(0)
247+
248+
subject.ssl_context(max_version: symbol)
249+
end
250+
251+
context "but it's :TLSv1" do
252+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_VERSION" do
253+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
254+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_VERSION)
255+
allow(context).to receive(:verify_mode=).with(0)
256+
257+
subject.ssl_context(max_version: :TLSv1)
258+
end
259+
end
260+
261+
context "but it's :TLSv1_1" do
262+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_1_VERSION" do
263+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
264+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
265+
allow(context).to receive(:verify_mode=).with(0)
266+
267+
subject.ssl_context(max_version: :TLSv1_1)
268+
end
269+
end
270+
271+
context "but it's :TLSv1_2" do
272+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_2_VERSION" do
273+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
274+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
275+
allow(context).to receive(:verify_mode=).with(0)
276+
277+
subject.ssl_context(max_version: :TLSv1_2)
278+
end
279+
end
280+
end
281+
end
282+
131283
describe "when given the verify: keyword argument" do
132284
subject { super().ssl_context(verify: :peer) }
133285

spec/network/ssl_spec.rb

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,162 @@
203203
end
204204
end
205205

206+
context "when given the min_version: keyword argument" do
207+
subject { described_class }
208+
209+
let(:context) { double(OpenSSL::SSL::SSLContext) }
210+
211+
context "and it's 1" do
212+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_VERSION" do
213+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
214+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_VERSION)
215+
allow(context).to receive(:verify_mode=).with(0)
216+
217+
subject.context(min_version: 1)
218+
end
219+
end
220+
221+
context "and it's 1.1" do
222+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_1_VERSION" do
223+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
224+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
225+
allow(context).to receive(:verify_mode=).with(0)
226+
227+
subject.context(min_version: 1.1)
228+
end
229+
end
230+
231+
context "and it's 1_2" do
232+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_2_VERSION" do
233+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
234+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
235+
allow(context).to receive(:verify_mode=).with(0)
236+
237+
subject.context(min_version: 1.2)
238+
end
239+
end
240+
241+
context "and it's a Symbol" do
242+
let(:symbol) { :TLS1 }
243+
244+
it "must call OpenSSL::SSL::SSLContext#min_version= with the Symbol" do
245+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
246+
expect(context).to receive(:min_version=).with(symbol)
247+
allow(context).to receive(:verify_mode=).with(0)
248+
249+
subject.context(min_version: symbol)
250+
end
251+
252+
context "but it's :TLSv1" do
253+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_VERSION" do
254+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
255+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_VERSION)
256+
allow(context).to receive(:verify_mode=).with(0)
257+
258+
subject.context(min_version: :TLSv1)
259+
end
260+
end
261+
262+
context "but it's :TLSv1_1" do
263+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_1_VERSION" do
264+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
265+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
266+
allow(context).to receive(:verify_mode=).with(0)
267+
268+
subject.context(min_version: :TLSv1_1)
269+
end
270+
end
271+
272+
context "but it's :TLSv1_2" do
273+
it "must call OpenSSL::SSL::SSLContext#min_version= with OpenSSL::SSL::TLS1_2_VERSION" do
274+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
275+
expect(context).to receive(:min_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
276+
allow(context).to receive(:verify_mode=).with(0)
277+
278+
subject.context(min_version: :TLSv1_2)
279+
end
280+
end
281+
end
282+
end
283+
284+
context "when given the max_version: keyword argument" do
285+
subject { described_class }
286+
287+
let(:context) { double(OpenSSL::SSL::SSLContext) }
288+
289+
context "and it's 1" do
290+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_VERSION" do
291+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
292+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_VERSION)
293+
allow(context).to receive(:verify_mode=).with(0)
294+
295+
subject.context(max_version: 1)
296+
end
297+
end
298+
299+
context "and it's 1.1" do
300+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_1_VERSION" do
301+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
302+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
303+
allow(context).to receive(:verify_mode=).with(0)
304+
305+
subject.context(max_version: 1.1)
306+
end
307+
end
308+
309+
context "and it's 1_2" do
310+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_2_VERSION" do
311+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
312+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
313+
allow(context).to receive(:verify_mode=).with(0)
314+
315+
subject.context(max_version: 1.2)
316+
end
317+
end
318+
319+
context "and it's a Symbol" do
320+
let(:symbol) { :TLS1 }
321+
322+
it "must call OpenSSL::SSL::SSLContext#max_version= with the Symbol" do
323+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
324+
expect(context).to receive(:max_version=).with(symbol)
325+
allow(context).to receive(:verify_mode=).with(0)
326+
327+
subject.context(max_version: symbol)
328+
end
329+
330+
context "but it's :TLSv1" do
331+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_VERSION" do
332+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
333+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_VERSION)
334+
allow(context).to receive(:verify_mode=).with(0)
335+
336+
subject.context(max_version: :TLSv1)
337+
end
338+
end
339+
340+
context "but it's :TLSv1_1" do
341+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_1_VERSION" do
342+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
343+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_1_VERSION)
344+
allow(context).to receive(:verify_mode=).with(0)
345+
346+
subject.context(max_version: :TLSv1_1)
347+
end
348+
end
349+
350+
context "but it's :TLSv1_2" do
351+
it "must call OpenSSL::SSL::SSLContext#max_version= with OpenSSL::SSL::TLS1_2_VERSION" do
352+
expect(OpenSSL::SSL::SSLContext).to receive(:new).and_return(context)
353+
expect(context).to receive(:max_version=).with(OpenSSL::SSL::TLS1_2_VERSION)
354+
allow(context).to receive(:verify_mode=).with(0)
355+
356+
subject.context(max_version: :TLSv1_2)
357+
end
358+
end
359+
end
360+
end
361+
206362
context "when given the verify: keyword argument" do
207363
subject { described_class.context(verify: :peer) }
208364

0 commit comments

Comments
 (0)