Skip to content

Commit 036d482

Browse files
committed
Implement operational certificates (including creation from TLVs and X509 DER encoding)
1 parent cd278d5 commit 036d482

File tree

8 files changed

+418
-132
lines changed

8 files changed

+418
-132
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// MatterDotNet Copyright (C) 2024
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published by
5+
// the Free Software Foundation, either version 3 of the License, or any later version.
6+
// This program is distributed in the hope that it will be useful,
7+
// but WITHOUT ANY WARRANTY, without even the implied warranty of
8+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9+
// See the GNU Affero General Public License for more details.
10+
// You should have received a copy of the GNU Affero General Public License
11+
// along with this program. If not, see <http://www.gnu.org/licenses/>.
12+
//
13+
// WARNING: This file was auto-generated. Do not edit.
14+
15+
using MatterDotNet.Protocol.Parsers;
16+
using MatterDotNet.Protocol.Payloads;
17+
using System.Diagnostics.CodeAnalysis;
18+
19+
namespace MatterDotNet.Messages.Certificates
20+
{
21+
public record Extension : TLVPayload
22+
{
23+
/// <inheritdoc />
24+
public Extension() {}
25+
26+
/// <inheritdoc />
27+
[SetsRequiredMembers]
28+
public Extension(Memory<byte> data) : this(new TLVReader(data)) {}
29+
30+
public BasicConstraints? BasicCnstr { get; set; }
31+
public ushort? KeyUsage { get; set; }
32+
public uint[]? ExtendedKeyUsage { get; set; }
33+
public byte[]? SubjectKeyId { get; set; }
34+
public byte[]? AuthorityKeyId { get; set; }
35+
public byte[]? FutureExtension { get; set; }
36+
37+
/// <inheritdoc />
38+
[SetsRequiredMembers]
39+
public Extension(TLVReader reader, long structNumber = -1) {
40+
if (reader.IsTag(1))
41+
BasicCnstr = new BasicConstraints(reader, 1);
42+
else if (reader.IsTag(2))
43+
KeyUsage = reader.GetUShort(2);
44+
else if (reader.IsTag(3))
45+
{
46+
reader.StartArray(3);
47+
List<uint> items = new();
48+
while (!reader.IsEndContainer()) {
49+
items.Add(reader.GetUInt(-1)!.Value);
50+
}
51+
reader.EndContainer();
52+
ExtendedKeyUsage = items.ToArray();
53+
}
54+
else if (reader.IsTag(4))
55+
SubjectKeyId = reader.GetBytes(4);
56+
else if (reader.IsTag(5))
57+
AuthorityKeyId = reader.GetBytes(5);
58+
else if (reader.IsTag(6))
59+
FutureExtension = reader.GetBytes(6);
60+
}
61+
62+
/// <inheritdoc />
63+
public override void Serialize(TLVWriter writer, long structNumber = -1) {
64+
if (BasicCnstr != null)
65+
BasicCnstr.Serialize(writer, 1);
66+
else if (KeyUsage != null)
67+
writer.WriteUShort(2, KeyUsage);
68+
else if (ExtendedKeyUsage != null)
69+
{
70+
writer.StartArray(3);
71+
foreach (var item in ExtendedKeyUsage) {
72+
writer.WriteUInt(-1, item);
73+
}
74+
writer.EndContainer();
75+
}
76+
else if (SubjectKeyId != null)
77+
writer.WriteBytes(4, SubjectKeyId);
78+
else if (AuthorityKeyId != null)
79+
writer.WriteBytes(5, AuthorityKeyId);
80+
else if (FutureExtension != null)
81+
writer.WriteBytes(6, FutureExtension);
82+
}
83+
}
84+
}

MatterDotNet/Messages/Certificates/MatterCertificate.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public MatterCertificate(Memory<byte> data) : this(new TLVReader(data)) {}
3636
public required ulong PubKeyAlgo { get; set; }
3737
public required ulong EcCurveId { get; set; }
3838
public required byte[] EcPubKey { get; set; }
39-
public required List<byte[]> Extensions { get; set; }
39+
public required List<Extension> Extensions { get; set; }
4040
public required byte[] Signature { get; set; }
4141

4242
/// <inheritdoc />
@@ -70,7 +70,7 @@ public MatterCertificate(TLVReader reader, long structNumber = -1) {
7070
reader.StartList(10);
7171
Extensions = new();
7272
while (!reader.IsEndContainer()) {
73-
Extensions.Add(reader.GetBytes(-1)!);
73+
Extensions.Add(new Extension(reader, -1));
7474
}
7575
reader.EndContainer();
7676
}
@@ -105,7 +105,7 @@ public override void Serialize(TLVWriter writer, long structNumber = -1) {
105105
{
106106
writer.StartList(10);
107107
foreach (var item in Extensions) {
108-
writer.WriteBytes(-1, item);
108+
item.Serialize(writer, -1);
109109
}
110110
writer.EndContainer();
111111
}

MatterDotNet/PKI/MatterCertificate.cs

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)