Skip to content

Commit ee4a966

Browse files
committed
netstandard20
update pipeline for releasing version
1 parent 1751880 commit ee4a966

16 files changed

+122
-63
lines changed

.github/workflows/build.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ name: Build
22

33
on:
44
push:
5-
branches: ["main"]
5+
branches:
6+
- main
7+
- release/**
68
tags:
79
- "v[0-9]+.[0-9]+.[0-9]+"
810
pull_request:
@@ -39,17 +41,16 @@ jobs:
3941
VERSION=${TAG#v}
4042
dotnet pack -c Release --no-build -p:PackageVersion=$VERSION
4143
42-
- name: Upload nuget package
43-
if: (github.ref == 'refs/heads/main' && github.event_name != 'pull_request') || startsWith(github.ref, 'refs/tags/v')
44+
- name: Upload nuget package artifact
45+
if: (github.ref == 'refs/heads/main' && github.event_name != 'pull_request') || startsWith(github.ref, 'refs/tags/v') || startsWith(github.ref, 'refs/heads/release/')
4446
uses: actions/upload-artifact@v4
4547
with:
4648
name: nugetpackage
4749
path: ./**/*.nupkg
4850

49-
# todo enable again when project has been modernized...
5051
prerelease:
5152
needs: build
52-
if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request'
53+
if: (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/heads/release/')) && github.event_name != 'pull_request'
5354
runs-on: ubuntu-latest
5455

5556
steps:

Flexinets.Radius.Core.Tests/Tests/RadiusCoreTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,8 @@ public void TestMessageAuthenticatorResponsePacketBlastRadiusMissingAuthenticato
481481

482482
const string secret = "xyzzy5461";
483483

484-
var radiusPacketParser = new RadiusPacketParser(NullLogger<RadiusPacketParser>.Instance, GetDictionary());
484+
var radiusPacketParser =
485+
new RadiusPacketParser(NullLogger<RadiusPacketParser>.Instance, GetDictionary(), false);
485486
Assert.Throws<MessageAuthenticatorException>(() =>
486487
radiusPacketParser.Parse(response, Encoding.UTF8.GetBytes(secret), requestAuthenticator));
487488
}

Flexinets.Radius.Core/Flexinets.Radius.Core.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netstandard2.1</TargetFramework>
5-
<Nullable>enable</Nullable>
4+
<TargetFramework>netstandard2.0</TargetFramework>
65
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
76
<IsPackable>true</IsPackable>
87
<JsonSerializerIsReflectionEnabledByDefault>false</JsonSerializerIsReflectionEnabledByDefault>
98
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net8.0'))">true</IsAotCompatible>
109
</PropertyGroup>
1110

1211
<PropertyGroup>
13-
<VersionPrefix>3.0.1</VersionPrefix>
12+
<VersionPrefix>2.0.1</VersionPrefix>
1413
<Title>Flexinets.Radius.Core</Title>
1514
<Authors>Verner Fortelius</Authors>
1615
<Description>Library for parsing and assembling radius packets. Includes a dictionary with attributes and some vendor specific attributes</Description>

Flexinets.Radius.Core/src/Dictionary/IRadiusDictionary.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@ public interface IRadiusDictionary
55
/// <summary>
66
/// Get a vendor specific attribute by vendorId and vendorCode
77
/// </summary>
8-
DictionaryVendorAttribute? GetVendorAttribute(uint vendorId, byte vendorCode);
8+
DictionaryVendorAttribute GetVendorAttribute(uint vendorId, byte vendorCode);
99

1010

1111
/// <summary>
1212
/// Get an RFC attribute by code
1313
/// </summary>
14-
DictionaryAttribute? GetAttribute(byte code);
14+
DictionaryAttribute GetAttribute(byte code);
1515

1616

1717
/// <summary>
1818
/// Get an attribute or vendor attribute by name
1919
/// </summary>
20-
DictionaryAttribute? GetAttribute(string name);
20+
DictionaryAttribute GetAttribute(string name);
2121
}
2222
}

Flexinets.Radius.Core/src/Dictionary/RadiusDictionary.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static IRadiusDictionary Parse(string dictionaryFileContent)
6161
/// Read and parse dictionary from file in Radiator format
6262
/// </summary>
6363
public static async Task<IRadiusDictionary> LoadAsync(string dictionaryFilePath) =>
64-
Parse(await File.ReadAllTextAsync(dictionaryFilePath));
64+
Parse(await Task.FromResult(File.ReadAllText(dictionaryFilePath)));
6565

6666

6767
/// <summary>
@@ -88,13 +88,15 @@ private RadiusDictionary()
8888
}
8989

9090

91-
public DictionaryVendorAttribute? GetVendorAttribute(uint vendorId, byte vendorCode) =>
91+
public DictionaryVendorAttribute GetVendorAttribute(uint vendorId, byte vendorCode) =>
9292
VendorSpecificAttributes.FirstOrDefault(o => o.VendorId == vendorId && o.VendorCode == vendorCode);
9393

9494

95-
public DictionaryAttribute? GetAttribute(byte typecode) => Attributes.GetValueOrDefault(typecode);
95+
public DictionaryAttribute GetAttribute(byte typecode) =>
96+
Attributes.TryGetValue(typecode, out var attribute) ? attribute : null;
9697

9798

98-
public DictionaryAttribute? GetAttribute(string name) => AttributeNames.GetValueOrDefault(name);
99+
public DictionaryAttribute GetAttribute(string name) =>
100+
AttributeNames.TryGetValue(name, out var attribute) ? attribute : null;
99101
}
100102
}

Flexinets.Radius.Core/src/Dictionary/VendorSpecificAttribute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public class VendorSpecificAttribute
1717
/// <param name="contentBytes"></param>
1818
public VendorSpecificAttribute(byte[] contentBytes)
1919
{
20-
VendorId = BitConverter.ToUInt32(contentBytes[..4].Reverse().ToArray());
20+
VendorId = BitConverter.ToUInt32(contentBytes.Take(4).Reverse().ToArray(), 0);
2121
VendorCode = contentBytes[4];
2222
Length = contentBytes[5];
23-
Value = contentBytes[6..];
23+
Value = contentBytes.Skip(6).ToArray();
2424
}
2525
}
2626
}

Flexinets.Radius.Core/src/IPacketHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ namespace Flexinets.Radius.Core
44
{
55
public interface IPacketHandler : IDisposable
66
{
7-
IRadiusPacket? HandlePacket(IRadiusPacket packet);
7+
IRadiusPacket HandlePacket(IRadiusPacket packet);
88
}
99
}

Flexinets.Radius.Core/src/Packet/Attribute.cs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,46 @@ public static object ToObject(
1515
string type,
1616
uint code,
1717
byte[] authenticator,
18-
byte[] sharedSecret) =>
19-
type switch
18+
byte[] sharedSecret)
19+
{
20+
switch (type)
2021
{
21-
"string" => Encoding.UTF8.GetString(contentBytes),
22-
"tagged-string" => Encoding.UTF8.GetString(contentBytes),
23-
"octet" when code == 2 => RadiusPassword.Decrypt(sharedSecret, authenticator, contentBytes),
24-
"octet" => contentBytes,
25-
"integer" => BitConverter.ToUInt32(contentBytes.Reverse().ToArray(), 0),
26-
"tagged-integer" => BitConverter.ToUInt32(contentBytes.Reverse().ToArray(), 0),
27-
"ipaddr" => new IPAddress(contentBytes),
28-
_ => throw new ArgumentException("Unknown type")
29-
};
22+
case "string":
23+
case "tagged-string":
24+
return Encoding.UTF8.GetString(contentBytes);
25+
case "octet" when code == 2:
26+
return RadiusPassword.Decrypt(sharedSecret, authenticator, contentBytes);
27+
case "octet":
28+
return contentBytes;
29+
case "integer":
30+
case "tagged-integer":
31+
return BitConverter.ToUInt32(contentBytes.Reverse().ToArray(), 0);
32+
case "ipaddr":
33+
return new IPAddress(contentBytes);
34+
default:
35+
throw new ArgumentException("Unknown type");
36+
}
37+
}
3038

3139

3240
/// <summary>
3341
/// Gets the byte representation of an attribute object
3442
/// </summary>
35-
public static byte[] ToBytes(object value) =>
36-
value switch
43+
public static byte[] ToBytes(object value)
44+
{
45+
switch (value)
3746
{
38-
string stringValue => Encoding.UTF8.GetBytes(stringValue),
39-
uint uintValue => BitConverter.GetBytes(uintValue).Reverse().ToArray(),
40-
byte[] byteArray => byteArray,
41-
IPAddress ipAddress => ipAddress.GetAddressBytes(),
42-
_ => throw new NotImplementedException()
43-
};
47+
case string stringValue:
48+
return Encoding.UTF8.GetBytes(stringValue);
49+
case uint uintValue:
50+
return BitConverter.GetBytes(uintValue).Reverse().ToArray();
51+
case byte[] byteArray:
52+
return byteArray;
53+
case IPAddress ipAddress:
54+
return ipAddress.GetAddressBytes();
55+
default:
56+
throw new NotImplementedException();
57+
}
58+
}
4459
}
4560
}

Flexinets.Radius.Core/src/Packet/IRadiusPacket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface IRadiusPacket
99
byte[] Authenticator { get; }
1010
byte[] SharedSecret { get; }
1111
PacketCode Code { get; }
12-
byte[]? RequestAuthenticator { get; }
12+
byte[] RequestAuthenticator { get; }
1313
IRadiusPacket CreateResponsePacket(PacketCode responseCode);
1414

1515
T GetAttribute<T>(string name);

Flexinets.Radius.Core/src/Packet/IRadiusPacketParser.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ namespace Flexinets.Radius.Core
66
public interface IRadiusPacketParser
77
{
88
byte[] GetBytes(IRadiusPacket packet);
9-
IRadiusPacket Parse(byte[] packetBytes, byte[] sharedSecret, byte[]? requestAuthenticator = null);
9+
IRadiusPacket Parse(byte[] packetBytes, byte[] sharedSecret, byte[] requestAuthenticator = null);
1010

1111
[Obsolete("Use parse instead... this isnt async anyway")]
12-
bool TryParsePacketFromStream(Stream stream, out IRadiusPacket? packet, byte[] sharedSecret, byte[]? requestAuthenticator = null);
12+
bool TryParsePacketFromStream(Stream stream, out IRadiusPacket packet, byte[] sharedSecret, byte[] requestAuthenticator = null);
1313
}
1414
}

Flexinets.Radius.Core/src/Packet/RadiusPacket.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class RadiusPacket : IRadiusPacket
1717
public byte[] Authenticator { get; internal set; } = new byte[16];
1818
public IDictionary<string, List<object>> Attributes { get; set; } = new Dictionary<string, List<object>>();
1919
public byte[] SharedSecret { get; internal set; } = Array.Empty<byte>();
20-
public byte[]? RequestAuthenticator { get; internal set; }
20+
public byte[] RequestAuthenticator { get; internal set; }
2121

2222

2323
internal RadiusPacket()
@@ -37,8 +37,10 @@ public RadiusPacket(PacketCode code, byte identifier, string secret)
3737
// Generate random authenticator for access request packets
3838
if (Code == PacketCode.AccessRequest || Code == PacketCode.StatusServer)
3939
{
40-
using var csp = RandomNumberGenerator.Create();
41-
csp.GetNonZeroBytes(Authenticator);
40+
using (var csp = RandomNumberGenerator.Create())
41+
{
42+
csp.GetNonZeroBytes(Authenticator);
43+
}
4244
}
4345

4446
// A Message authenticator is required in status server packets, calculated last

Flexinets.Radius.Core/src/Packet/RadiusPacketParser.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class RadiusPacketParser : IRadiusPacketParser
1818
public RadiusPacketParser(
1919
ILogger<RadiusPacketParser> logger,
2020
IRadiusDictionary radiusDictionary,
21-
bool skipBlastRadiusChecks = false)
21+
bool skipBlastRadiusChecks = true)
2222
{
2323
_logger = logger;
2424
_radiusDictionary = radiusDictionary;
@@ -29,7 +29,7 @@ public RadiusPacketParser(
2929
/// <summary>
3030
/// Parses packet bytes and returns an IRadiusPacket
3131
/// </summary>
32-
public IRadiusPacket Parse(byte[] packetBytes, byte[] sharedSecret, byte[]? requestAuthenticator = null)
32+
public IRadiusPacket Parse(byte[] packetBytes, byte[] sharedSecret, byte[] requestAuthenticator = null)
3333
{
3434
var packetLength = BitConverter.ToUInt16(packetBytes.Skip(2).Take(2).Reverse().ToArray(), 0);
3535
if (packetBytes.Length < packetLength)
@@ -43,7 +43,7 @@ public IRadiusPacket Parse(byte[] packetBytes, byte[] sharedSecret, byte[]? requ
4343
SharedSecret = sharedSecret,
4444
Identifier = packetBytes[1],
4545
Code = (PacketCode)packetBytes[0],
46-
Authenticator = packetBytes[4..20],
46+
Authenticator = packetBytes.Skip(4).Take(16).ToArray(),
4747
};
4848

4949
if ((packet.Code == PacketCode.AccountingRequest || packet.Code == PacketCode.DisconnectRequest) &&
@@ -203,7 +203,7 @@ private static void HandleRequestMessageAuthenticator(byte[] sharedSecret, int m
203203
headerBytes[7] = (byte)(2 + contentBytes.Length); // length of the vsa part
204204
break;
205205

206-
case { } attributeType:
206+
case DictionaryAttribute attributeType:
207207
headerBytes[0] = attributeType.Code;
208208

209209
// Encrypt password if this is a User-Password attribute
@@ -249,7 +249,7 @@ private int AddAttributesToPacket(RadiusPacket packet, byte[] packetBytes, int p
249249
{
250250
var typeCode = packetBytes[position];
251251
var attributeLength = packetBytes[position + 1];
252-
var attributeValueBytes = packetBytes[(position + 2)..(position + attributeLength)];
252+
var attributeValueBytes = packetBytes.Skip(position + 2).Take(attributeLength - 2).ToArray();
253253

254254
try
255255
{

Flexinets.Radius.Core/src/Packet/RadiusPacketParserObsolete.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public partial class RadiusPacketParser
1414
[Obsolete("Use parse instead... this isnt async anyway")]
1515
public bool TryParsePacketFromStream(
1616
Stream stream,
17-
out IRadiusPacket? packet,
17+
out IRadiusPacket packet,
1818
byte[] sharedSecret,
19-
byte[]? requestAuthenticator = null)
19+
byte[] requestAuthenticator = null)
2020
{
2121
var packetHeaderBytes = new byte[4];
2222
var i = stream.Read(packetHeaderBytes, 0, 4);

Flexinets.Radius.Core/src/Packet/RadiusPassword.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ private static byte[] EncryptDecrypt(byte[] input, byte[] key)
2828
/// </summary>
2929
private static byte[] CreateKey(byte[] sharedSecret, byte[] authenticator)
3030
{
31-
using var md5 = MD5.Create();
32-
return md5.ComputeHash(sharedSecret.Concat(authenticator).ToArray());
31+
using (var md5 = MD5.Create())
32+
{
33+
return md5.ComputeHash(sharedSecret.Concat(authenticator).ToArray());
34+
}
3335
}
3436

3537

Flexinets.Radius.Core/src/Utils.cs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public static string ToHexString(this byte[] bytes) =>
3636
/// <summary>
3737
/// Get the mccmnc as a string from a 3GPP-User-Location-Info vendor attribute.
3838
/// </summary>
39-
public static (LocationType locationType, string? mccmnc) GetMccMncFrom3GPPLocationInfo(byte[] bytes)
39+
public static (LocationType locationType, string mccmnc) GetMccMncFrom3GPPLocationInfo(byte[] bytes)
4040
{
41-
string? mccmnc = null;
41+
string mccmnc = null;
4242
var type = (LocationType)bytes[0];
4343

4444
if (type == LocationType.CGI
@@ -102,7 +102,7 @@ public static byte[] CalculateRequestMessageAuthenticator(byte[] packetBytes, by
102102
private static byte[] CalculateMessageAuthenticator(
103103
byte[] packetBytes,
104104
byte[] sharedSecret,
105-
byte[]? requestAuthenticator,
105+
byte[] requestAuthenticator,
106106
int index)
107107
{
108108
var temp = new byte[packetBytes.Length];
@@ -111,8 +111,10 @@ private static byte[] CalculateMessageAuthenticator(
111111

112112
requestAuthenticator?.CopyTo(temp, 4);
113113

114-
using var md5 = new HMACMD5(sharedSecret);
115-
return md5.ComputeHash(temp);
114+
using (var md5 = new HMACMD5(sharedSecret))
115+
{
116+
return md5.ComputeHash(temp);
117+
}
116118
}
117119

118120

@@ -130,8 +132,10 @@ public static byte[] CalculateResponseAuthenticator(
130132
var bytes = packetBytes.Concat(sharedSecret).ToArray();
131133
Buffer.BlockCopy(requestAuthenticator, 0, bytes, 4, 16);
132134

133-
using var md5 = MD5.Create();
134-
return md5.ComputeHash(bytes);
135+
using (var md5 = MD5.Create())
136+
{
137+
return md5.ComputeHash(bytes);
138+
}
135139
}
136140

137141

@@ -143,10 +147,9 @@ public static bool ValidateMessageAuthenticator(
143147
int packetLength,
144148
int messageAuthenticatorPosition,
145149
byte[] sharedSecret,
146-
byte[]? requestAuthenticator)
150+
byte[] requestAuthenticator)
147151
{
148-
var messageAuthenticator =
149-
packetBytes[(messageAuthenticatorPosition + 2)..(messageAuthenticatorPosition + 16 + 2)];
152+
var messageAuthenticator = packetBytes.Skip(messageAuthenticatorPosition + 2).Take(16).ToArray();
150153

151154
var tempPacket = new byte[packetLength];
152155
Buffer.BlockCopy(packetBytes, 0, tempPacket, 0, packetLength);

0 commit comments

Comments
 (0)