Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ToBeSignedCertificate.cs
5using System;
6using System.Diagnostics.CodeAnalysis;
7using System.Formats.Asn1;
8
10{
15 {
31 private ToBeSignedCertificate(byte[] Binary, int Version, System.Numerics.BigInteger SerialNumber,
33 DateTimeOffset NotAfter, Names Subject, byte[]? AuthorityKeyIdentifier,
34 byte[]? SubjectKeyIdentifier, IPublicKey CertificatePublicKey, Vector? Extensions)
35 {
36 this.Binary = Binary;
37 this.Version = Version;
38 this.SerialNumber = SerialNumber;
39 this.IssuerSignatureAlgorithm = IssuerSignatureAlgorithm;
40 this.Issuer = Issuer;
41 this.NotBefore = NotBefore;
42 this.NotAfter = NotAfter;
43 this.Subject = Subject;
44 this.AuthorityKeyIdentifier = AuthorityKeyIdentifier;
45 this.SubjectKeyIdentifier = SubjectKeyIdentifier;
46 this.PublicKey = CertificatePublicKey;
47 this.Extensions = Extensions;
48 }
49
56 public static bool TryParse(Vector TbsCert, [NotNullWhen(true)] out ToBeSignedCertificate? Parsed)
57 {
58 Parsed = null;
59
60 int c = TbsCert.Length;
61 int i = 0;
62 int Version = 0;
63
64 if (i < c)
65 {
66 if (TbsCert.FirstElement is not System.Numerics.BigInteger V)
67 {
68 if (TbsCert.FirstElement is not Vector VersionVector ||
69 VersionVector.Length != 1 ||
70 VersionVector.FirstElement is not System.Numerics.BigInteger V2)
71 {
72 return false;
73 }
74
75 V = V2;
76 }
77
78 if (V < int.MinValue || V > int.MaxValue)
79 return false;
80
81 Version = (int)V;
82 i++;
83 }
84
85 if (i >= c || TbsCert[i++] is not System.Numerics.BigInteger SerialNumber)
86 return false;
87
88 if (i >= c)
89 return false;
90
91 object? Obj = TbsCert[i++];
92
94 {
95 if (Obj is not Vector AlgorithmIdentifier)
96 return false;
97
98 ISignatureAlgorithm? IssuerSignatureAlgorithm2 = SignatureAlgorithm.TryDecode(AlgorithmIdentifier);
99 if (IssuerSignatureAlgorithm2 is null)
100 return false;
101
102 IssuerSignatureAlgorithm = IssuerSignatureAlgorithm2;
103 }
104
105 if (i >= c || TbsCert[i++] is not Vector Issuer)
106 return false;
107
108 if (i >= c ||
109 TbsCert[i++] is not Vector Validity ||
110 Validity.Length != 2 ||
111 Validity.FirstElementNested is not DateTimeOffset NotBefore ||
112 Validity.LastElementNested is not DateTimeOffset NotAfter)
113 {
114 return false;
115 }
116
117 if (i >= c || TbsCert[i++] is not Vector Subject)
118 return false;
119
120 if (i >= c || TbsCert[i++] is not Vector SubjectPublicKeyInfo)
121 return false;
122
123 if (SubjectPublicKeyInfo.Length != 2)
124 return false;
125
126 Obj = SubjectPublicKeyInfo.FirstElementNested;
127
128 if (Obj is not IPublicKey PublicKey)
129 {
130 if (Obj is Vector v &&
131 v.FirstElement is IPublicKey PublicKey2)
132 {
133 PublicKey = PublicKey2;
134 }
135 else
136 return false;
137 }
138
139 if (!PublicKey.IsConfigured ||
140 !PublicKey.SetPublicKey(SubjectPublicKeyInfo.LastElement))
141 {
142 return false;
143 }
144
145 // TODO: Check for optional issuerUniqueID and subjectUniqueID
146
147 Vector? ListExtensions;
148 byte[]? AuthorityKeyIdentifier = null;
149 byte[]? SubjectKeyIdentifier = null;
150
151 if (i < c && TbsCert[i] is byte[] ExtensionsBin)
152 {
153 if (!ASN1.TryDecodeDerAs(UniversalTagNumber.Sequence, ExtensionsBin, out object? Extensions2))
154 return false;
155
156 TbsCert.Elements.SetValue(Extensions2, i);
157 }
158
159 if (i < c && TbsCert[i] is Vector ListExtensions2)
160 {
161 i++;
162
163 if (ListExtensions2.Length == 1 &&
164 ListExtensions2.FirstElementNested is Vector ListExtensions3 &&
165 (ListExtensions2.SubSection[0] & 0x80) != 0)
166 {
167 ListExtensions2 = ListExtensions3;
168 }
169
170 ListExtensions = ListExtensions2;
171
172 foreach (object? Extension in ListExtensions2)
173 {
174 if (Extension is AuthorityKeyIdentifier Aki)
176 else if (Extension is SubjectKeyIdentifier Ski)
178 }
179 }
180 else
181 ListExtensions = null;
182
183 if (i < c)
184 return false;
185
186 Parsed = new ToBeSignedCertificate(TbsCert.SubSection, Version, SerialNumber,
189 PublicKey, ListExtensions);
190
191 return true;
192 }
193
197 public byte[] Binary { get; }
198
202 public int Version { get; }
203
207 public System.Numerics.BigInteger SerialNumber { get; }
208
213
217 public Names Issuer { get; }
218
222 public DateTimeOffset NotBefore { get; }
223
227 public DateTimeOffset NotAfter { get; }
228
232 public Names Subject { get; }
233
237 public byte[]? AuthorityKeyIdentifier { get; }
238
242 public byte[]? SubjectKeyIdentifier { get; }
243
247 public IPublicKey PublicKey { get; }
248
252 public Vector? Extensions { get; }
253 }
254}
Static class for parsing and decoding security objects encoded using Abstract Syntax Notation 1 (ASN....
Definition: ASN1.cs:22
static bool TryDecodeDerAs(UniversalTagNumber TagNumber, byte[] Data, out object? Value)
Decodes a DER-encoded object.
Definition: ASN1.cs:37
Certificate, without signature, as defined in RFC 5280, §4.1
ISignatureAlgorithm IssuerSignatureAlgorithm
Signature algorithm used by issuer to sign the certificate.
DateTimeOffset NotAfter
Signatures cannot be created after this timestamp.
DateTimeOffset NotBefore
Signatures cannot be created before this timestamp.
static bool TryParse(Vector TbsCert, [NotNullWhen(true)] out ToBeSignedCertificate? Parsed)
Tries to parse an ASN.1-encoded Certificate, as defined in RFC 5280, §4.1
byte[] Binary
Binary representation of certificate to be signed.
abstract bool SetPublicKey(object? PublicKey)
Sets the public key, based on the security information provided.
abstract bool IsConfigured
If the object has been configured.
Abstract base class for signature algorithms, as defined in RFC 5280.
static ? ISignatureAlgorithm TryDecode(Vector AlgorithmIdentifier)
Tries to decode an ASN.1-decoded algorithm identifier into an instance of the corresponding signature...
Interface for signature algorithms, as defined in RFC 5280.
class Names(Vector NamesVector)
Contains a collection of distinguished names.
Definition: Names.cs:9