@@ -288,12 +288,7 @@ defmodule Radius.Packet do
288288 packet = % { packet | auth: request_authenticator }
289289 { header , attrs } = encode_packet ( packet , options )
290290
291- resp_auth =
292- :crypto . hash_init ( :md5 )
293- |> :crypto . hash_update ( header )
294- |> :crypto . hash_update ( attrs )
295- |> :crypto . hash_update ( packet . secret )
296- |> :crypto . hash_final ( )
291+ resp_auth = :crypto . hash ( :md5 , [ header , attrs , packet . secret ] )
297292
298293 header = << header :: bytes - size ( 4 ) , resp_auth :: binary >>
299294
@@ -507,6 +502,7 @@ defmodule Radius.Packet do
507502 @ doc """
508503 Verify if the packet signature is valid.
509504
505+ (https://www.ietf.org/rfc/rfc2865.txt)
510506 (https://www.ietf.org/rfc/rfc2869.txt)
511507 """
512508 def verify ( packet ) do
@@ -516,6 +512,9 @@ defmodule Radius.Packet do
516512 def verify ( packet , request_authenticator ) do
517513 case Radius.Packet . get_attr ( packet , "Message-Authenticator" ) do
518514 [ sig1 ] ->
515+ { header , attrs } = encode_packet ( % { packet | auth: request_authenticator } , [ ] )
516+ resp_auth = :crypto . hash ( :md5 , [ header , attrs , packet . secret ] )
517+
519518 attrs =
520519 Enum . map ( packet . attrs , fn
521520 { "Message-Authenticator" , _ } -> { "Message-Authenticator" , << 0 :: size ( 128 ) >> }
@@ -526,12 +525,15 @@ defmodule Radius.Packet do
526525 { header , attrs } = encode_packet ( packet , [ ] )
527526 << code , id , length :: size ( 16 ) , _resp_auth :: binary >> = header
528527 sign_header = << code , id , length :: size ( 16 ) , request_authenticator :: binary >>
529-
530528 sig2 = message_authenticator ( packet . secret , [ sign_header , attrs ] )
531- sig1 == sig2
529+
530+ ( packet . auth == request_authenticator or packet . auth == resp_auth ) and sig1 == sig2
532531
533532 _ ->
534- false
533+ { header , attrs } = encode_packet ( % { packet | auth: request_authenticator } , [ ] )
534+ resp_auth = :crypto . hash ( :md5 , [ header , attrs , packet . secret ] )
535+
536+ packet . auth == request_authenticator or packet . auth == resp_auth
535537 end
536538 end
537539end
0 commit comments