PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/linkedfs/usr/lib/perl5/vendor_perl/5.8.4/Authen/SASL/Perl/DIGEST_MD5.pm

https://bitbucket.org/harakiri/trk
Perl | 171 lines | 111 code | 52 blank | 8 comment | 6 complexity | 0cffc638e7b97b22d1edd596b7299f54 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  1. # Copyright (c) 2003 Graham Barr, Djamel Boudjerda, Paul Connolly, Julian Onions and Nexor.
  2. # All rights reserved. This program is free software; you can redistribute
  3. # it and/or modify it under the same terms as Perl itself.
  4. # See http://www.ietf.org/rfc/rfc2831.txt for details
  5. package Authen::SASL::Perl::DIGEST_MD5;
  6. use strict;
  7. use vars qw($VERSION @ISA $CNONCE);
  8. use Digest::MD5 qw(md5_hex md5);
  9. $VERSION = "1.04";
  10. @ISA = qw(Authen::SASL::Perl);
  11. my %secflags = (
  12. noplaintext => 1,
  13. noanonymous => 1,
  14. );
  15. # some have to be quoted - some don't - sigh!
  16. my %qdval; @qdval{qw(username realm nonce cnonce digest-uri)} = ();
  17. sub _order { 3 }
  18. sub _secflags {
  19. shift;
  20. scalar grep { $secflags{$_} } @_;
  21. }
  22. sub mechanism { 'DIGEST-MD5' }
  23. # no initial value passed to the server
  24. sub client_start {
  25. '';
  26. }
  27. sub client_step # $self, $server_sasl_credentials
  28. {
  29. my ($self, $challenge) = @_;
  30. $self->{server_params} = \my %sparams;
  31. # Parse response parameters
  32. while($challenge =~ s/^(?:\s*,)?\s*(\w+)=("([^\\"]+|\\.)*"|[^,]+)\s*//) {
  33. my ($k, $v) = ($1,$2);
  34. if ($v =~ /^"(.*)"$/s) {
  35. ($v = $1) =~ s/\\//g;
  36. }
  37. $sparams{$k} = $v;
  38. }
  39. return $self->set_error("Bad challenge: '$challenge'")
  40. if length $challenge;
  41. return $self->set_error("Server does not support auth (qop = $sparams{'qop'})")
  42. unless grep { /^auth$/ } split(/,/, $sparams{'qop'});
  43. my %response = (
  44. nonce => $sparams{'nonce'},
  45. username => $self->_call('user'),
  46. realm => $sparams{'realm'},
  47. nonce => $sparams{'nonce'},
  48. cnonce => md5_hex($CNONCE || join (":", $$, time, rand)),
  49. 'digest-uri' => $self->service . '/' . $self->host,
  50. qop => 'auth',
  51. nc => sprintf("%08d", ++$self->{nonce}{$sparams{'nonce'}}),
  52. charset => $sparams{'charset'},
  53. );
  54. my $serv_name = $self->_call('serv');
  55. if (defined $serv_name) {
  56. $response{'digest_uri'} .= '/' . $serv_name;
  57. }
  58. my $password = $self->_call('pass');
  59. # Generate the response value
  60. my $A1 = join (":",
  61. md5(join (":", @response{qw(username realm)}, $password)),
  62. @response{qw(nonce cnonce)}
  63. );
  64. my $A2 = "AUTHENTICATE:" . $response{'digest-uri'};
  65. $A2 .= ":00000000000000000000000000000000"
  66. if $response{'qop'} and $response{'qop'} =~ /^auth-(conf|int)$/;
  67. $response{'response'} = md5_hex(
  68. join (":", md5_hex($A1), @response{qw(nonce nc cnonce qop)}, md5_hex($A2))
  69. );
  70. join (",", map { _qdval($_, $response{$_}) } sort keys %response);
  71. }
  72. sub _qdval {
  73. my ($k, $v) = @_;
  74. if (!defined $v) {
  75. return;
  76. }
  77. elsif (exists $qdval{$k}) {
  78. $v =~ s/([\\"])/\\$1/g;
  79. return qq{$k="$v"};
  80. }
  81. return "$k=$v";
  82. }
  83. 1;
  84. __END__
  85. =head1 NAME
  86. Authen::SASL::Perl::DIGEST_MD5 - Digest MD5 Authentication class
  87. =head1 SYNOPSIS
  88. use Authen::SASL;
  89. $sasl = Authen::SASL->new(
  90. mechanism => 'DIGEST-MD5',
  91. callback => {
  92. user => $user,
  93. pass => $pass,
  94. serv => $serv
  95. },
  96. );
  97. =head1 DESCRIPTION
  98. This method implements the DIGEST MD5 SASL algorithm, as described in RFC-2831.
  99. =head2 CALLBACK
  100. The callbacks used are:
  101. =over 4
  102. =item user
  103. The username to be used in the response
  104. =item pass
  105. The password to be used in the response
  106. =item serv
  107. The service name when authenticating to a replicated service
  108. =back
  109. =head1 SEE ALSO
  110. L<Authen::SASL>
  111. =head1 AUTHORS
  112. Graham Barr, Djamel Boudjerda (NEXOR) Paul Connolly, Julian Onions (NEXOR)
  113. Please report any bugs, or post any suggestions, to the perl-ldap mailing list
  114. <perl-ldap-dev@lists.sourceforge.net>
  115. =head1 COPYRIGHT
  116. Copyright (c) 2003 Graham Barr, Djamel Boudjerda, Paul Connolly, Julian Onions and Nexor.
  117. All rights reserved. This program is free software; you can redistribute
  118. it and/or modify it under the same terms as Perl itself.
  119. =cut