Skip to content

Commit 15edfd9

Browse files
committed
merge revision(s) 50292: [Backport #9644]
* ext/openssl/lib/openssl/ssl.rb: stricter hostname verification following RFC 6125. with the patch provided by Tony Arcieri and Hiroshi Nakamura [ruby-core:61545] [Bug #9644] * test/openssl/test_ssl.rb: add tests for above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_2@50293 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent c620aca commit 15edfd9

File tree

4 files changed

+216
-5
lines changed

4 files changed

+216
-5
lines changed

ChangeLog

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
Mon Apr 13 22:11:21 2015 CHIKANAGA Tomoyuki <nagachika@ruby-lang.org>
2+
3+
* ext/openssl/lib/openssl/ssl.rb: stricter hostname verification
4+
following RFC 6125. with the patch provided by Tony Arcieri and
5+
Hiroshi Nakamura [ruby-core:61545] [Bug #9644]
6+
* test/openssl/test_ssl.rb: add tests for above.
7+
18
Mon Apr 13 13:03:33 2015 CHIKANAGA Tomoyuki <nagachika@ruby-lang.org>
29

310
* lib/securerandom.rb: skip Win32 libraries in SecureRandom if

ext/openssl/lib/openssl/ssl.rb

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ def verify_certificate_identity(cert, hostname)
143143
case san.tag
144144
when 2 # dNSName in GeneralName (RFC5280)
145145
should_verify_common_name = false
146-
reg = Regexp.escape(san.value).gsub(/\\\*/, "[^.]+")
147-
return true if /\A#{reg}\z/i =~ hostname
146+
return true if verify_hostname(hostname, san.value)
148147
when 7 # iPAddress in GeneralName (RFC5280)
149148
should_verify_common_name = false
150149
# follows GENERAL_NAME_print() in x509v3/v3_alt.c
@@ -159,20 +158,75 @@ def verify_certificate_identity(cert, hostname)
159158
if should_verify_common_name
160159
cert.subject.to_a.each{|oid, value|
161160
if oid == "CN"
162-
reg = Regexp.escape(value).gsub(/\\\*/, "[^.]+")
163-
return true if /\A#{reg}\z/i =~ hostname
161+
return true if verify_hostname(hostname, value)
164162
end
165163
}
166164
end
167165
return false
168166
end
169167
module_function :verify_certificate_identity
170168

169+
def verify_hostname(hostname, san) # :nodoc:
170+
# RFC 5280, IA5String is limited to the set of ASCII characters
171+
return false unless san.ascii_only?
172+
return false unless hostname.ascii_only?
173+
174+
# See RFC 6125, section 6.4.1
175+
# Matching is case-insensitive.
176+
san_parts = san.downcase.split(".")
177+
178+
# TODO: this behavior should probably be more strict
179+
return san == hostname if san_parts.size < 2
180+
181+
# Matching is case-insensitive.
182+
host_parts = hostname.downcase.split(".")
183+
184+
# RFC 6125, section 6.4.3, subitem 2.
185+
# If the wildcard character is the only character of the left-most
186+
# label in the presented identifier, the client SHOULD NOT compare
187+
# against anything but the left-most label of the reference
188+
# identifier (e.g., *.example.com would match foo.example.com but
189+
# not bar.foo.example.com or example.com).
190+
return false unless san_parts.size == host_parts.size
191+
192+
# RFC 6125, section 6.4.3, subitem 1.
193+
# The client SHOULD NOT attempt to match a presented identifier in
194+
# which the wildcard character comprises a label other than the
195+
# left-most label (e.g., do not match bar.*.example.net).
196+
return false unless verify_wildcard(host_parts.shift, san_parts.shift)
197+
198+
san_parts.join(".") == host_parts.join(".")
199+
end
200+
module_function :verify_hostname
201+
202+
def verify_wildcard(domain_component, san_component) # :nodoc:
203+
parts = san_component.split("*", -1)
204+
205+
return false if parts.size > 2
206+
return san_component == domain_component if parts.size == 1
207+
208+
# RFC 6125, section 6.4.3, subitem 3.
209+
# The client SHOULD NOT attempt to match a presented identifier
210+
# where the wildcard character is embedded within an A-label or
211+
# U-label of an internationalized domain name.
212+
return false if domain_component.start_with?("xn--") && san_component != "*"
213+
214+
parts[0].length + parts[1].length < domain_component.length &&
215+
domain_component.start_with?(parts[0]) &&
216+
domain_component.end_with?(parts[1])
217+
end
218+
module_function :verify_wildcard
219+
171220
class SSLSocket
172221
include Buffering
173222
include SocketForwarder
174223
include Nonblock
175224

225+
##
226+
# Perform hostname verification after an SSL connection is established
227+
#
228+
# This method MUST be called after calling #connect to ensure that the
229+
# hostname of a remote peer has been verified.
176230
def post_connection_check(hostname)
177231
unless OpenSSL::SSL.verify_certificate_identity(peer_cert, hostname)
178232
raise SSLError, "hostname \"#{hostname}\" does not match the server certificate"

test/openssl/test_ssl.rb

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,156 @@ def test_verify_certificate_identity
426426
end
427427
end
428428

429+
def test_verify_hostname
430+
assert_equal(true, OpenSSL::SSL.verify_hostname("www.example.com", "*.example.com"))
431+
assert_equal(false, OpenSSL::SSL.verify_hostname("www.subdomain.example.com", "*.example.com"))
432+
end
433+
434+
def test_verify_wildcard
435+
assert_equal(false, OpenSSL::SSL.verify_wildcard("foo", "x*"))
436+
assert_equal(true, OpenSSL::SSL.verify_wildcard("foo", "foo"))
437+
assert_equal(true, OpenSSL::SSL.verify_wildcard("foo", "f*"))
438+
assert_equal(true, OpenSSL::SSL.verify_wildcard("foo", "*"))
439+
assert_equal(false, OpenSSL::SSL.verify_wildcard("abc*bcd", "abcd"))
440+
assert_equal(false, OpenSSL::SSL.verify_wildcard("xn--qdk4b9b", "x*"))
441+
assert_equal(false, OpenSSL::SSL.verify_wildcard("xn--qdk4b9b", "*--qdk4b9b"))
442+
assert_equal(true, OpenSSL::SSL.verify_wildcard("xn--qdk4b9b", "xn--qdk4b9b"))
443+
end
444+
445+
# Comments in this test is excerpted from http://tools.ietf.org/html/rfc6125#page-27
446+
def test_post_connection_check_wildcard_san
447+
# case-insensitive ASCII comparison
448+
# RFC 6125, section 6.4.1
449+
#
450+
# "..matching of the reference identifier against the presented identifier
451+
# is performed by comparing the set of domain name labels using a
452+
# case-insensitive ASCII comparison, as clarified by [DNS-CASE] (e.g.,
453+
# "WWW.Example.Com" would be lower-cased to "www.example.com" for
454+
# comparison purposes)
455+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
456+
create_cert_with_san('DNS:*.example.com'), 'www.example.com'))
457+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
458+
create_cert_with_san('DNS:*.Example.COM'), 'www.example.com'))
459+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
460+
create_cert_with_san('DNS:*.example.com'), 'WWW.Example.COM'))
461+
# 1. The client SHOULD NOT attempt to match a presented identifier in
462+
# which the wildcard character comprises a label other than the
463+
# left-most label (e.g., do not match bar.*.example.net).
464+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
465+
create_cert_with_san('DNS:www.*.com'), 'www.example.com'))
466+
# 2. If the wildcard character is the only character of the left-most
467+
# label in the presented identifier, the client SHOULD NOT compare
468+
# against anything but the left-most label of the reference
469+
# identifier (e.g., *.example.com would match foo.example.com but
470+
# not bar.foo.example.com or example.com).
471+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
472+
create_cert_with_san('DNS:*.example.com'), 'foo.example.com'))
473+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
474+
create_cert_with_san('DNS:*.example.com'), 'bar.foo.example.com'))
475+
# 3. The client MAY match a presented identifier in which the wildcard
476+
# character is not the only character of the label (e.g.,
477+
# baz*.example.net and *baz.example.net and b*z.example.net would
478+
# be taken to match baz1.example.net and foobaz.example.net and
479+
# buzz.example.net, respectively). ...
480+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
481+
create_cert_with_san('DNS:baz*.example.com'), 'baz1.example.com'))
482+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
483+
create_cert_with_san('DNS:*baz.example.com'), 'foobaz.example.com'))
484+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
485+
create_cert_with_san('DNS:b*z.example.com'), 'buzz.example.com'))
486+
# Section 6.4.3 of RFC6125 states that client should NOT match identifier
487+
# where wildcard is other than left-most label.
488+
#
489+
# Also implicitly mentions the wildcard character only in singular form,
490+
# and discourages matching against more than one wildcard.
491+
#
492+
# See RFC 6125, section 7.2, subitem 2.
493+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
494+
create_cert_with_san('DNS:*b*.example.com'), 'abc.example.com'))
495+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
496+
create_cert_with_san('DNS:*b*.example.com'), 'ab.example.com'))
497+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
498+
create_cert_with_san('DNS:*b*.example.com'), 'bc.example.com'))
499+
# ... However, the client SHOULD NOT
500+
# attempt to match a presented identifier where the wildcard
501+
# character is embedded within an A-label or U-label [IDNA-DEFS] of
502+
# an internationalized domain name [IDNA-PROTO].
503+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
504+
create_cert_with_san('DNS:xn*.example.com'), 'xn1ca.example.com'))
505+
# part of A-label
506+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
507+
create_cert_with_san('DNS:xn--*.example.com'), 'xn--1ca.example.com'))
508+
# part of U-label
509+
# dNSName in RFC5280 is an IA5String so U-label should NOT be allowed
510+
# regardless of wildcard.
511+
#
512+
# See Section 7.2 of RFC 5280:
513+
# IA5String is limited to the set of ASCII characters.
514+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
515+
create_cert_with_san('DNS:á*.example.com'), 'á1.example.com'))
516+
end
517+
518+
def test_post_connection_check_wildcard_cn
519+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
520+
create_cert_with_name('*.example.com'), 'www.example.com'))
521+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
522+
create_cert_with_name('*.Example.COM'), 'www.example.com'))
523+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
524+
create_cert_with_name('*.example.com'), 'WWW.Example.COM'))
525+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
526+
create_cert_with_name('www.*.com'), 'www.example.com'))
527+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
528+
create_cert_with_name('*.example.com'), 'foo.example.com'))
529+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
530+
create_cert_with_name('*.example.com'), 'bar.foo.example.com'))
531+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
532+
create_cert_with_name('baz*.example.com'), 'baz1.example.com'))
533+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
534+
create_cert_with_name('*baz.example.com'), 'foobaz.example.com'))
535+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
536+
create_cert_with_name('b*z.example.com'), 'buzz.example.com'))
537+
# Section 6.4.3 of RFC6125 states that client should NOT match identifier
538+
# where wildcard is other than left-most label.
539+
#
540+
# Also implicitly mentions the wildcard character only in singular form,
541+
# and discourages matching against more than one wildcard.
542+
#
543+
# See RFC 6125, section 7.2, subitem 2.
544+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
545+
create_cert_with_name('*b*.example.com'), 'abc.example.com'))
546+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
547+
create_cert_with_name('*b*.example.com'), 'ab.example.com'))
548+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
549+
create_cert_with_name('*b*.example.com'), 'bc.example.com'))
550+
assert_equal(true, OpenSSL::SSL.verify_certificate_identity(
551+
create_cert_with_name('xn*.example.com'), 'xn1ca.example.com'))
552+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
553+
create_cert_with_name('xn--*.example.com'), 'xn--1ca.example.com'))
554+
# part of U-label
555+
# Subject in RFC5280 states case-insensitive ASCII comparison.
556+
#
557+
# See Section 7.2 of RFC 5280:
558+
# IA5String is limited to the set of ASCII characters.
559+
assert_equal(false, OpenSSL::SSL.verify_certificate_identity(
560+
create_cert_with_name('á*.example.com'), 'á1.example.com'))
561+
end
562+
563+
def create_cert_with_san(san)
564+
ef = OpenSSL::X509::ExtensionFactory.new
565+
cert = OpenSSL::X509::Certificate.new
566+
cert.subject = OpenSSL::X509::Name.parse("/DC=some/DC=site/CN=Some Site")
567+
ext = ef.create_ext('subjectAltName', san)
568+
cert.add_extension(ext)
569+
cert
570+
end
571+
572+
def create_cert_with_name(name)
573+
cert = OpenSSL::X509::Certificate.new
574+
cert.subject = OpenSSL::X509::Name.new([['DC', 'some'], ['DC', 'site'], ['CN', name]])
575+
cert
576+
end
577+
578+
429579
# Create NULL byte SAN certificate
430580
def create_null_byte_SAN_certificate(critical = false)
431581
ef = OpenSSL::X509::ExtensionFactory.new

version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#define RUBY_VERSION "2.2.2"
22
#define RUBY_RELEASE_DATE "2015-04-13"
3-
#define RUBY_PATCHLEVEL 94
3+
#define RUBY_PATCHLEVEL 95
44

55
#define RUBY_RELEASE_YEAR 2015
66
#define RUBY_RELEASE_MONTH 4

0 commit comments

Comments
 (0)