Leaked source code of windows server 2003
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.

87 lines
2.5 KiB

  1. package LWP::Authen::Digest;
  2. use strict;
  3. require MD5;
  4. sub authenticate
  5. {
  6. my($class, $ua, $proxy, $auth_param, $response,
  7. $request, $arg, $size) = @_;
  8. my($user, $pass) = $ua->get_basic_credentials($auth_param->{realm},
  9. $request->url, $proxy);
  10. return $response unless defined $user and defined $pass;
  11. my $nc = sprintf "%08X", ++$ua->{authen_md5_nonce_count}{$auth_param->{nonce}};
  12. my $cnonce = sprintf "%8x", time;
  13. my $uri = $request->url->path_query;
  14. $uri = "/" unless length $uri;
  15. my $md5 = new MD5;
  16. my(@digest);
  17. $md5->add(join(":", $user, $auth_param->{realm}, $pass));
  18. push(@digest, $md5->hexdigest);
  19. $md5->reset;
  20. push(@digest, $auth_param->{nonce});
  21. if ($auth_param->{qop}) {
  22. push(@digest, $nc, $cnonce, $auth_param->{qop});
  23. }
  24. $md5->add(join(":", $request->method, $uri));
  25. push(@digest, $md5->hexdigest);
  26. $md5->reset;
  27. $md5->add(join(":", @digest));
  28. my($digest) = $md5->hexdigest;
  29. $md5->reset;
  30. my %resp = map { $_ => $auth_param->{$_} } qw(realm nonce opaque);
  31. @resp{qw(username uri response algorithm)} = ($user, $uri, $digest, "MD5");
  32. if($auth_param->{qop} eq "auth") {
  33. @resp{qw(qop cnonce nc)} = ("auth", $cnonce, $nc);
  34. }
  35. my(@order) = qw(username realm qop algorithm uri nonce nc cnonce response);
  36. if($request->method =~ /^(?:POST|PUT)$/) {
  37. $md5->add($request->content);
  38. my $content = $md5->hexdigest;
  39. $md5->reset;
  40. $md5->add(join(":", @digest[0..1], $content));
  41. $md5->reset;
  42. $resp{"message-digest"} = $md5->hexdigest;
  43. push(@order, "message-digest");
  44. }
  45. push(@order, "opaque");
  46. my @pairs;
  47. for (@order) {
  48. next unless defined $resp{$_};
  49. push(@pairs, "$_=" . qq("$resp{$_}"));
  50. }
  51. my $auth_header = $proxy ? "Proxy-Authorization" : "Authorization";
  52. my $auth_value = "Digest " . join(", ", @pairs);
  53. # Need to check this isn't a repeated fail!
  54. my $r = $response;
  55. while ($r) {
  56. my $auth = $r->request->header($auth_header);
  57. if ($auth && $auth eq $auth_value) {
  58. # here we know this failed before
  59. $response->header("Client-Warning" =>
  60. "Credentials for '$user' failed before");
  61. return $response;
  62. }
  63. $r = $r->previous;
  64. }
  65. my $referral = $request->clone;
  66. $referral->header($auth_header => $auth_value);
  67. return $ua->request($referral, $arg, $size, $response);
  68. }
  69. 1;