Skip to content

Commit

Permalink
Rewrite IPAddr.new.hton with pure Ruby operation
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt committed Jul 23, 2024
1 parent c959729 commit 4e9c478
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/openssl/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
if defined?(OpenSSL::SSL)

require "io/nonblock"
require "ipaddr"
require "socket"

module OpenSSL
Expand Down Expand Up @@ -311,6 +310,28 @@ def timeout=(value)
end
end

private def ip_to_bytes(ip)
if ip.count('.') == 3 # IPv4
ip.split('.').map(&:to_i).pack('C*')
elsif ip.include?(':') # IPv6
hextets = ip.split(':')
if hextets.count('') > 1
raise ArgumentError, "Invalid IP address format"
end
if hextets.include?('')
empty_index = hextets.index('')
sub_hextets = hextets[empty_index + 1..-1]
hextets.delete_at(empty_index)
hextets.fill('0', empty_index, 8 - hextets.size)
hextets += sub_hextets
end
hextets.map { |h| h.hex }.pack('n*')
else
raise ArgumentError, "Invalid IP address format"
end
end
module_function :ip_to_bytes

def verify_certificate_identity(cert, hostname)
should_verify_common_name = true
cert.extensions.each{|ext|
Expand All @@ -326,8 +347,8 @@ def verify_certificate_identity(cert, hostname)
should_verify_common_name = false
if san.value.size == 4 || san.value.size == 16
begin
return true if san.value == IPAddr.new(hostname).hton
rescue IPAddr::InvalidAddressError
return true if san.value == ip_to_bytes(hostname)
rescue ArgumentError
end
end
end
Expand Down

0 comments on commit 4e9c478

Please sign in to comment.