Skip to content

Commit a9fc749

Browse files
tverlaanbearice
authored andcommitted
add verification of authenticator field - rfc2865
1 parent 4271830 commit a9fc749

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

lib/radius/packet.ex

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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
537539
end

test/radius_packet_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,10 @@ defmodule Radius.PacketTest do
115115
assert reply == @sample_binary_rep_signed
116116
end
117117

118-
test "verify message authenticator signature on request" do
118+
test "verify (message) authenticator signature on request" do
119119
assert Radius.Packet.verify(@sample_req)
120+
assert Radius.Packet.verify(%{@sample_req | attrs: []})
120121
refute Radius.Packet.verify(%{@sample_req | id: 14})
121-
refute Radius.Packet.verify(%{@sample_req | attrs: []})
122122
end
123123

124124
test "verify message authenticator signature on reply" do

0 commit comments

Comments
 (0)