From 0141fb51b90928181775ada63bbbb9ad36af2ea6 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Tue, 7 May 2019 10:21:47 +0200 Subject: [PATCH] WIP --- RadiusAttribute.cs | 4 ++-- RadiusMessage.cs | 33 +++++++++++++++++++++++++++++++++ RadiusServer.cs | 2 -- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/RadiusAttribute.cs b/RadiusAttribute.cs index f62045c..0003a02 100644 --- a/RadiusAttribute.cs +++ b/RadiusAttribute.cs @@ -63,8 +63,8 @@ namespace ln.radius return String.Format("[RadiusAttribute Type={0} {1}]", Name, Encoding.ASCII.GetString(Bytes)); } - static Dictionary attributeNames = new Dictionary(); - static Dictionary attributeFactories = new Dictionary(); + static internal Dictionary attributeNames = new Dictionary(); + static internal Dictionary attributeFactories = new Dictionary(); public static RadiusAttribute Create(byte attributeType) { if (attributeFactories.ContainsKey(attributeType)) diff --git a/RadiusMessage.cs b/RadiusMessage.cs index 31b76cd..8160e4c 100644 --- a/RadiusMessage.cs +++ b/RadiusMessage.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.IO; using ln.types; using System.Security.Cryptography; +using ln.logging; namespace ln.radius { public enum RadiusCode : byte { @@ -54,6 +55,15 @@ namespace ln.radius Code = radiusCode; } + public RAT GetAttribute(string attrName) where RAT : RadiusAttribute => (RAT)GetAttribute(attrName); + public RadiusAttribute GetAttribute(string attrName) + { + foreach (RadiusAttribute radiusAttribute in radiusAttributes) + if (radiusAttribute.Name.Equals(attrName)) + return radiusAttribute; + throw new KeyNotFoundException(); + } + public byte[] Authenticate(byte[] secret) { byte[] packet = ToBytes(); @@ -65,6 +75,29 @@ namespace ln.radius return packet; } + public bool IsAuthentic(byte[] secret) + { + byte[] authenticator = Authenticator; + bool authentic = false; + + try + { + Authenticator = new byte[16]; + byte[] packet = ToBytes(); + + MD5 md5 = MD5.Create(); + md5.TransformBlock(packet, 0, packet.Length, null, 0); + md5.TransformFinalBlock(secret, 0, secret.Length); + authentic = authenticator.AreEqual(md5.Hash); + } catch (Exception e) + { + Logging.Log(LogLevel.ERROR, "RadiusMessage.IsAuthentic(): {0}", e); + Logging.Log(e); + } + Authenticator = authenticator; + return authentic; + } + public byte[] ToBytes() { using (MemoryStream memoryStream = new MemoryStream()) diff --git a/RadiusServer.cs b/RadiusServer.cs index c5b02c0..7fffce0 100644 --- a/RadiusServer.cs +++ b/RadiusServer.cs @@ -61,7 +61,6 @@ namespace ln.radius } } - private void ListenerThread(UdpClient udp) { try @@ -73,7 +72,6 @@ namespace ln.radius try { RadiusMessage radiusMessage = RadiusMessage.FromBytes(rx, remoteEndPoint); - Logging.Log("RX: {0}",radiusMessage.ToString()); RequestPool.Enqueue(() => MessageReceived(this, radiusMessage)); } catch (Exception e) {