You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.7 KiB

  1. commit 62dd58188d8f8987d24bd84951813a54a8bf5987
  2. Author: Gisle Aas <gisle@aas.no>
  3. Date: Mon Jan 24 23:19:59 2011 +0100
  4. Default to verifying hostnames when using SSL
  5. --- a/lib/LWP/Protocol/https.pm
  6. +++ b/lib/LWP/Protocol/https.pm
  7. @@ -11,18 +11,30 @@ sub socket_type
  8. return "https";
  9. }
  10. -sub _check_sock
  11. +sub _extra_sock_opts
  12. {
  13. - my($self, $req, $sock) = @_;
  14. - if ($sock->can("verify_hostname")) {
  15. - if (!$sock->verify_hostname($req->uri->host, "www")) {
  16. - my $subject = $sock->peer_certificate("subject");
  17. - die "SSL-peer fails verification [subject=$subject]\n";
  18. - }
  19. - else {
  20. - $req->{ssl_sock_verified}++;
  21. + my $self = shift;
  22. + my %ssl_opts = %{$self->{ua}{ssl_opts} || {}};
  23. + unless (exists $ssl_opts{SSL_verify_mode}) {
  24. + $ssl_opts{SSL_verify_mode} = 1;
  25. + }
  26. + if (delete $ssl_opts{verify_hostname}) {
  27. + $ssl_opts{SSL_verify_mode} ||= 1;
  28. + $ssl_opts{SSL_verifycn_scheme} = 'www';
  29. + }
  30. + if ($ssl_opts{SSL_verify_mode}) {
  31. + unless (exists $ssl_opts{SSL_ca_file} || exists $ssl_opts{SSL_ca_path}) {
  32. + require Mozilla::CA;
  33. + $ssl_opts{SSL_ca_file} = Mozilla::CA::SSL_ca_file();
  34. }
  35. }
  36. + $self->{ssl_opts} = \%ssl_opts;
  37. + return (%ssl_opts, $self->SUPER::_extra_sock_opts);
  38. +}
  39. +
  40. +sub _check_sock
  41. +{
  42. + my($self, $req, $sock) = @_;
  43. my $check = $req->header("If-SSL-Cert-Subject");
  44. if (defined $check) {
  45. my $cert = $sock->get_peer_certificate ||
  46. @@ -45,12 +57,11 @@ sub _get_sock_info
  47. $res->header("Client-SSL-Cert-Subject" => $cert->subject_name);
  48. $res->header("Client-SSL-Cert-Issuer" => $cert->issuer_name);
  49. }
  50. - if (!$res->request->{ssl_sock_verified}) {
  51. - if(! eval { $sock->get_peer_verify }) {
  52. - my $msg = "Peer certificate not verified";
  53. - $msg .= " [$@]" if $@;
  54. - $res->header("Client-SSL-Warning" => $msg);
  55. - }
  56. + if (!$self->{ssl_opts}{SSL_verify_mode}) {
  57. + $res->push_header("Client-SSL-Warning" => "Peer certificate not verified");
  58. + }
  59. + elsif (!$self->{ssl_opts}{SSL_verifycn_scheme}) {
  60. + $res->push_header("Client-SSL-Warning" => "Peer hostname match with certificate not verified");
  61. }
  62. $res->header("Client-SSL-Socket-Class" => $Net::HTTPS::SSL_SOCKET_CLASS);
  63. }
  64. --- a/lib/LWP/UserAgent.pm
  65. +++ b/lib/LWP/UserAgent.pm
  66. @@ -41,6 +41,7 @@ sub new
  67. my $timeout = delete $cnf{timeout};
  68. $timeout = 3*60 unless defined $timeout;
  69. my $local_address = delete $cnf{local_address};
  70. + my $ssl_opts = delete $cnf{ssl_opts};
  71. my $use_eval = delete $cnf{use_eval};
  72. $use_eval = 1 unless defined $use_eval;
  73. my $parse_head = delete $cnf{parse_head};
  74. @@ -83,6 +84,7 @@ sub new
  75. def_headers => $def_headers,
  76. timeout => $timeout,
  77. local_address => $local_address,
  78. + ssl_opts => { $ssl_opts ? %$ssl_opts : (verify_hostname => 1) },
  79. use_eval => $use_eval,
  80. show_progress=> $show_progress,
  81. max_size => $max_size,
  82. @@ -582,6 +584,20 @@ sub max_size { shift->_elem('max_siz
  83. sub max_redirect { shift->_elem('max_redirect', @_); }
  84. sub show_progress{ shift->_elem('show_progress', @_); }
  85. +sub ssl_opts {
  86. + my $self = shift;
  87. + if (@_ == 1) {
  88. + my $k = shift;
  89. + return $self->{ssl_opts}{$k};
  90. + }
  91. + if (@_) {
  92. + %{$self->{ssl_opts}} = (%{$self->{ssl_opts}}, @_);
  93. + }
  94. + else {
  95. + return keys %{$self->{ssl_opts}};
  96. + }
  97. +}
  98. +
  99. sub parse_head {
  100. my $self = shift;
  101. if (@_) {
  102. @@ -1040,6 +1056,7 @@ The following options correspond to attr
  103. cookie_jar undef
  104. default_headers HTTP::Headers->new
  105. local_address undef
  106. + ssl_opts { verify_hostname => 1 }
  107. max_size undef
  108. max_redirect 7
  109. parse_head 1