Skip to content

Commit

Permalink
Verify hostnames in TLS connections
Browse files Browse the repository at this point in the history
This is done in connect_ldaps() and start_tls(), and calls
IO::Socket::SSL's verify_hostname method.

The default (for backwards compatibility?) is to not check, but pass
check => 1 if you want checking.

Signed-off-by: chrisridd@mac.com
  • Loading branch information
chrisridd authored and gbarr committed Sep 5, 2011
1 parent dfd757f commit 4dc845e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
16 changes: 14 additions & 2 deletions lib/Net/LDAP.pm
Expand Up @@ -184,6 +184,11 @@ sub connect_ldaps {
_SSL_context_init_args($arg) _SSL_context_init_args($arg)
) or return undef; ) or return undef;


if ($arg->{'check'} &&
$ldap->{'net_ldap_socket'}->verify_hostname( $host, 'ldap' )) {
$ldap->disconnect();
return undef;
}
$ldap->{net_ldap_host} = $host; $ldap->{net_ldap_host} = $host;
$ldap->{net_ldap_port} = $port; $ldap->{net_ldap_port} = $port;
} }
Expand Down Expand Up @@ -1034,8 +1039,15 @@ sub start_tls {
IO::Socket::SSL::context_init( { _SSL_context_init_args($arg) } ); IO::Socket::SSL::context_init( { _SSL_context_init_args($arg) } );
my $sock_class = ref($sock); my $sock_class = ref($sock);


return $mesg if (IO::Socket::SSL->start_SSL($sock, {_SSL_context_init_args($arg)})) {
if IO::Socket::SSL->start_SSL($sock, {_SSL_context_init_args($arg)}); my $host = $ldap->{'net_ldap_host'};
if ($arg->{'check'} &&
$sock->{'net_ldap_socket'}->verify_hostname( $host, 'ldap' )) {
$ldap->disconnect();
return undef;
}
return $mesg;
}


my $err = $@ || $IO::Socket::SSL::SSL_ERROR || $IO::Socket::SSL::SSL_ERROR || ''; # avoid use on once warning my $err = $@ || $IO::Socket::SSL::SSL_ERROR || $IO::Socket::SSL::SSL_ERROR || ''; # avoid use on once warning


Expand Down
8 changes: 7 additions & 1 deletion lib/Net/LDAP.pod
Expand Up @@ -169,7 +169,7 @@ If it resolves to an IPv4 address, the connection is tried using IPv4,
the same way as if this option was not given. the same way as if this option was not given.


Please note that IPv6 support is considered experimental in Please note that IPv6 support is considered experimental in
IO::Socket::SSL, which is used of SSL/TLS support, and there are a few IO::Socket::SSL, which is used for SSL/TLS support, and there are a few
issues to take care of. See L<IO::Socket::SSL/IPv6> for details. issues to take care of. See L<IO::Socket::SSL/IPv6> for details.


=back =back
Expand Down Expand Up @@ -755,6 +755,12 @@ The server must provide a certificate, and it must be valid.
If you set verify to optional or require, you must also set either If you set verify to optional or require, you must also set either
cafile or capath. The most secure option is B<require>. cafile or capath. The most secure option is B<require>.


=item check =E<gt> 1 | 0

This controls whether the name in the server's certificate is checked
against the hostname you tried to connect to. The default is to not
check. The most secure option is B<1>.

=item sslversion =E<gt> 'sslv2' | 'sslv3' | 'sslv2/3' | 'tlsv1' =item sslversion =E<gt> 'sslv2' | 'sslv3' | 'sslv2/3' | 'tlsv1'


This defines the version of the SSL/TLS protocol to use. Defaults to This defines the version of the SSL/TLS protocol to use. Defaults to
Expand Down
2 changes: 2 additions & 0 deletions lib/Net/LDAPS.pm
Expand Up @@ -29,13 +29,15 @@ Net::LDAPS - use LDAP over an SSL connection
$ldaps = Net::LDAPS->new('myhost.example.com', $ldaps = Net::LDAPS->new('myhost.example.com',
port => '10000', port => '10000',
verify => 'require', verify => 'require',
check => 1,
capath => '/usr/local/cacerts/'); capath => '/usr/local/cacerts/');
# alternate way # alternate way
use Net::LDAP; use Net::LDAP;
$ldaps = Net::LDAP->new('ldaps://myhost.example.com:10000', $ldaps = Net::LDAP->new('ldaps://myhost.example.com:10000',
verify => 'require', verify => 'require',
check => 1,
capath => '/usr/local/cacerts/'); capath => '/usr/local/cacerts/');
=head1 DESCRIPTION =head1 DESCRIPTION
Expand Down

0 comments on commit 4dc845e

Please sign in to comment.