Skip to content

Commit 623c355

Browse files
committed
misc fixes
1 parent 6ac17a6 commit 623c355

File tree

5 files changed

+21
-16
lines changed

5 files changed

+21
-16
lines changed

MatterDotNet/PKI/Fabric.cs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,15 @@ public class Fabric : OperationalCertificate
2525

2626
private Dictionary<ulong, OperationalCertificate> nodes = new Dictionary<ulong, OperationalCertificate>();
2727

28-
public Fabric(ulong rcac, ulong fabricId, byte[] ipk) : base()
28+
public Fabric(string fabricName, ulong fabricId, byte[] ipk) : base()
2929
{
3030
if (fabricId == 0)
3131
throw new ArgumentException("Invalid Fabric ID");
3232
if (ipk.Length != 16)
3333
throw new ArgumentException("Epoch Key must be 16 bytes");
34-
this.RCAC = rcac;
34+
this.RCAC = (ulong)Random.Shared.NextInt64();
3535
this.FabricID = fabricId;
36+
this.CommonName = fabricName;
3637
EpochKey = ipk;
3738
X500DistinguishedNameBuilder builder = new X500DistinguishedNameBuilder();
3839
builder.Add(OID_RCAC, $"{RCAC:X16}", UniversalTagNumber.UTF8String);
@@ -45,17 +46,17 @@ public Fabric(ulong rcac, ulong fabricId, byte[] ipk) : base()
4546
req.CertificateExtensions.Add(subjectKeyIdentifier);
4647
req.CertificateExtensions.Add(X509AuthorityKeyIdentifierExtension.CreateFromSubjectKeyIdentifier(subjectKeyIdentifier));
4748
this.cert = req.CreateSelfSigned(DateTime.Now.Subtract(TimeSpan.FromSeconds(30)), DateTime.Now.AddYears(10));
48-
byte[] fabricIDBytes = new byte[8];
49-
BinaryPrimitives.WriteUInt64BigEndian(fabricIDBytes, FabricID);
50-
CompressedFabricID = Crypto.KDF(PublicKey.AsSpan(1), fabricIDBytes, COMPRESSED_FABRIC_INFO, 64);
51-
OperationalIdentityProtectionKey = Crypto.KDF(EpochKey, CompressedFabricID, Encoding.ASCII.GetBytes("GroupKey v1.0"), Crypto.SYMMETRIC_KEY_LENGTH_BITS);
49+
GenerateIDs();
5250
}
5351

54-
protected Fabric(X509Certificate2 cert, byte[] ipk)
52+
protected Fabric(X509Certificate2 cert, byte[] ipk) : base(cert)
5553
{
56-
this.cert = cert;
5754
EpochKey = ipk;
58-
ParseCert();
55+
GenerateIDs();
56+
}
57+
58+
private void GenerateIDs()
59+
{
5960
byte[] fabricIDBytes = new byte[8];
6061
BinaryPrimitives.WriteUInt64BigEndian(fabricIDBytes, FabricID);
6162
CompressedFabricID = Crypto.KDF(PublicKey.AsSpan(1), fabricIDBytes, COMPRESSED_FABRIC_INFO, 64);
@@ -179,9 +180,9 @@ public byte[] ComputeDestinationID(byte[] random, ulong nodeId)
179180
return Crypto.HMAC(OperationalIdentityProtectionKey, message);
180181
}
181182

182-
public byte[] CompressedFabricID { get; init; }
183+
public byte[] CompressedFabricID { get; set; }
183184
public OperationalCertificate? Commissioner { get; private set; }
184-
public byte[] OperationalIdentityProtectionKey { get; init; }
185+
public byte[] OperationalIdentityProtectionKey { get; set; }
185186
public byte[] EpochKey { get; init; }
186187
}
187188
}

MatterDotNet/PKI/OperationalCertificate.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
using MatterDotNet.Messages.Certificates;
1414
using MatterDotNet.Protocol.Payloads;
15+
using MatterDotNet.Util;
1516
using System.Formats.Asn1;
1617
using System.Globalization;
1718
using System.Numerics;
@@ -275,8 +276,8 @@ public MatterCertificate ToMatterCertificate()
275276
SigAlgo = 0x1,
276277
EcPubKey = cert.GetPublicKey(),
277278
SerialNum = cert.SerialNumberBytes.ToArray(),
278-
NotBefore = (uint)((DateTimeOffset)cert.NotBefore - EPOCH).ToUnixTimeSeconds(),
279-
NotAfter = (uint)((DateTimeOffset)cert.NotAfter - EPOCH).ToUnixTimeSeconds(),
279+
NotBefore = TimeUtil.ToEpochSeconds(cert.NotBefore),
280+
NotAfter = TimeUtil.ToEpochSeconds(cert.NotAfter),
280281
Signature = GetSignature(),
281282
Extensions = extensions,
282283
Issuer = GetDNs(cert.IssuerName),
@@ -325,7 +326,7 @@ public bool VerifyData(byte[] message, byte[] signature)
325326

326327
public string IssuerName { get; private set; } = string.Empty;
327328

328-
public string CommonName { get; private set; } = string.Empty;
329+
public string CommonName { get; protected set; } = string.Empty;
329330

330331
public ulong NodeID { get; private set; }
331332

MatterDotNet/Protocol/Connection/MRPConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal class MRPConnection : IConnection
3939

4040
public MRPConnection(IPEndPoint ep)
4141
{
42-
client = new UdpClient(AddressFamily.InterNetwork);
42+
client = new UdpClient(ep.AddressFamily);
4343
client.Connect(ep);
4444
Task.Factory.StartNew(Run);
4545
}

MatterDotNet/Protocol/Sessions/SessionManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ public static SessionContext GetUnsecureSession(IPEndPoint ep, bool initiator)
3333

3434
internal static SessionContext GetUnsecureSession(IConnection connection, bool initiator)
3535
{
36+
if (sessions.TryGetValue(0, out SessionContext? existing))
37+
return existing;
3638
SessionContext ctx = new SessionContext(connection, initiator, 0, 0, 0, 0, new MessageState());
3739
sessions.TryAdd(0, ctx);
3840
return ctx;

Test/MatterCertTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,13 @@ public void NOCEncoding()
7272
tlv.Serialize(new TLVWriter(output));
7373
Assert.That(nocTLV, Is.EqualTo(output.GetPayload().Span.ToArray()).AsCollection);
7474
Assert.That(cert.PublicKey, Is.EqualTo(tlv.EcPubKey));
75+
Assert.That(control.NotBefore, Is.EqualTo(tlv.NotBefore));
7576
}
7677

7778
[Test]
7879
public void NOCSigning()
7980
{
80-
Fabric fabric = new Fabric((ulong)Random.Shared.NextInt64(), 0x1, RandomNumberGenerator.GetBytes(16));
81+
Fabric fabric = new Fabric("Test Fabric", 0x1, RandomNumberGenerator.GetBytes(16));
8182
ECDsa key = ECDsa.Create();
8283
CertificateRequest req = new CertificateRequest("CN=Test", key, HashAlgorithmName.SHA256);
8384
OperationalCertificate noc = fabric.Sign(req);

0 commit comments

Comments
 (0)