@@ -191,12 +191,9 @@ class << self
191
191
alias default_ssl_port default_tls_port
192
192
end
193
193
194
- def SMTP . default_ssl_context ( verify_peer = true )
194
+ def SMTP . default_ssl_context ( ssl_context_params = nil )
195
195
context = OpenSSL ::SSL ::SSLContext . new
196
- context . verify_mode = verify_peer ? OpenSSL ::SSL ::VERIFY_PEER : OpenSSL ::SSL ::VERIFY_NONE
197
- store = OpenSSL ::X509 ::Store . new
198
- store . set_default_paths
199
- context . cert_store = store
196
+ context . set_params ( ssl_context_params ? ssl_context_params : { } )
200
197
context
201
198
end
202
199
@@ -409,14 +406,14 @@ def debug_output=(arg)
409
406
410
407
#
411
408
# :call-seq:
412
- # start(address, port = nil, helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil) { |smtp| ... }
409
+ # start(address, port = nil, helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil, ssl_context_params: nil ) { |smtp| ... }
413
410
# start(address, port = nil, helo = 'localhost', user = nil, secret = nil, authtype = nil) { |smtp| ... }
414
411
#
415
412
# Creates a new Net::SMTP object and connects to the server.
416
413
#
417
414
# This method is equivalent to:
418
415
#
419
- # Net::SMTP.new(address, port).start(helo: helo_domain, user: account, secret: password, authtype: authtype, tls_verify: flag, tls_hostname: hostname)
416
+ # Net::SMTP.new(address, port).start(helo: helo_domain, user: account, secret: password, authtype: authtype, tls_verify: flag, tls_hostname: hostname, ssl_context_params: nil )
420
417
#
421
418
# === Example
422
419
#
@@ -450,6 +447,11 @@ def debug_output=(arg)
450
447
# If the hostname in the server certificate is different from +address+,
451
448
# it can be specified with +tls_hostname+.
452
449
#
450
+ # Additional SSLContext params can be added to +ssl_context_params+ hash argument and are passed to
451
+ # +OpenSSL::SSL::SSLContext#set_params+
452
+ #
453
+ # +tls_verify: true+ is equivalent to +ssl_context_params: { verify_mode: OpenSSL::SSL::VERIFY_PEER }+.
454
+ #
453
455
# === Errors
454
456
#
455
457
# This method may raise:
@@ -465,14 +467,14 @@ def debug_output=(arg)
465
467
#
466
468
def SMTP . start ( address , port = nil , *args , helo : nil ,
467
469
user : nil , secret : nil , password : nil , authtype : nil ,
468
- tls_verify : true , tls_hostname : nil ,
470
+ tls_verify : true , tls_hostname : nil , ssl_context_params : nil ,
469
471
&block )
470
472
raise ArgumentError , "wrong number of arguments (given #{ args . size + 2 } , expected 1..6)" if args . size > 4
471
473
helo ||= args [ 0 ] || 'localhost'
472
474
user ||= args [ 1 ]
473
475
secret ||= password || args [ 2 ]
474
476
authtype ||= args [ 3 ]
475
- new ( address , port ) . start ( helo : helo , user : user , secret : secret , authtype : authtype , tls_verify : tls_verify , tls_hostname : tls_hostname , &block )
477
+ new ( address , port ) . start ( helo : helo , user : user , secret : secret , authtype : authtype , tls_verify : tls_verify , tls_hostname : tls_hostname , ssl_context_params : ssl_context_params , &block )
476
478
end
477
479
478
480
# +true+ if the SMTP session has been started.
@@ -482,7 +484,7 @@ def started?
482
484
483
485
#
484
486
# :call-seq:
485
- # start(helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil) { |smtp| ... }
487
+ # start(helo: 'localhost', user: nil, secret: nil, authtype: nil, tls_verify: true, tls_hostname: nil, ssl_context_params: nil ) { |smtp| ... }
486
488
# start(helo = 'localhost', user = nil, secret = nil, authtype = nil) { |smtp| ... }
487
489
#
488
490
# Opens a TCP connection and starts the SMTP session.
@@ -501,6 +503,11 @@ def started?
501
503
# If the hostname in the server certificate is different from +address+,
502
504
# it can be specified with +tls_hostname+.
503
505
#
506
+ # Additional SSLContext params can be added to +ssl_context_params+ hash argument and are passed to
507
+ # +OpenSSL::SSL::SSLContext#set_params+
508
+ #
509
+ # +tls_verify: true+ is equivalent to +ssl_context_params: { verify_mode: OpenSSL::SSL::VERIFY_PEER }+.
510
+ #
504
511
# === Block Usage
505
512
#
506
513
# When this methods is called with a block, the newly-started SMTP
@@ -539,17 +546,23 @@ def started?
539
546
# * IOError
540
547
#
541
548
def start ( *args , helo : nil ,
542
- user : nil , secret : nil , password : nil , authtype : nil , tls_verify : true , tls_hostname : nil )
549
+ user : nil , secret : nil , password : nil , authtype : nil , tls_verify : true , tls_hostname : nil , ssl_context_params : nil )
543
550
raise ArgumentError , "wrong number of arguments (given #{ args . size } , expected 0..4)" if args . size > 4
544
551
helo ||= args [ 0 ] || 'localhost'
545
552
user ||= args [ 1 ]
546
553
secret ||= password || args [ 2 ]
547
554
authtype ||= args [ 3 ]
555
+ ssl_context_params = ssl_context_params ? ssl_context_params : { }
556
+ if ssl_context_params . has_key? ( :verify_mode )
557
+ tls_verify = ssl_context_params [ :verify_mode ] != OpenSSL ::SSL ::VERIFY_NONE
558
+ else
559
+ ssl_context_params [ :verify_mode ] = tls_verify ? OpenSSL ::SSL ::VERIFY_PEER : OpenSSL ::SSL ::VERIFY_NONE
560
+ end
548
561
if @tls && @ssl_context_tls . nil?
549
- @ssl_context_tls = SMTP . default_ssl_context ( tls_verify )
562
+ @ssl_context_tls = SMTP . default_ssl_context ( ssl_context_params )
550
563
end
551
564
if @starttls && @ssl_context_starttls . nil?
552
- @ssl_context_starttls = SMTP . default_ssl_context ( tls_verify )
565
+ @ssl_context_starttls = SMTP . default_ssl_context ( ssl_context_params )
553
566
end
554
567
@tls_hostname = tls_hostname
555
568
if block_given?
0 commit comments