Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
SignerInfo.cs
1using System.Diagnostics.CodeAnalysis;
2using System.Formats.Asn1;
10
12{
16 public class SignerInfo
17 {
21 private SignerInfo()
22 {
23 }
24
28 public System.Numerics.BigInteger Version { get; private set; }
29
33 public Names? Signer { get; private set; }
34
38 public System.Numerics.BigInteger? SerialNumber { get; private set; }
39
43 public byte[]? SubjectKeyIdentifier { get; private set; }
44
48 public HashFunction? DigestAlgorithm { get; private set; }
49
53 public object[] SignedAttributes { get; private set; } = [];
54
59 public byte[] SignedAttributesData { get; private set; } = [];
60
64 public bool HasSignedAttributes { get; private set; } = false;
65
69 public string? ContentType { get; private set; }
70
74 public byte[]? MessageDigest { get; private set; }
75
79 public ISignatureAlgorithm? SignatureAlgorithm { get; private set; }
80
84 public byte[] Signature { get; private set; } = [];
85
89 public object[] UnsignedAttributes { get; private set; } = [];
90
98 public static bool TryParse(Vector SignerInfoVector, [NotNullWhen(true)] out SignerInfo? Parsed)
99 {
100 Parsed = null;
101
102 int i, c;
103
104 if ((c = SignerInfoVector.Length) < 5)
105 return false;
106
107 if (SignerInfoVector.FirstElement is not System.Numerics.BigInteger Version)
108 return false;
109
110 System.Numerics.BigInteger? SerialNumber = null;
111 byte[]? SubjectKeyIdentifier = null;
112 Names? Signer = null;
113
114 if (SignerInfoVector[1] is byte[] KeyIdentifier)
116 else if (SignerInfoVector[1] is Vector SignerIdentifierVector)
117 {
118 foreach (object Item in SignerIdentifierVector.Elements)
119 {
120 if (Item is Vector v)
121 Signer ??= new Names(v);
122 else if (Item is System.Numerics.BigInteger SN)
123 SerialNumber = SN;
124 else if (Item is SubjectKeyIdentifier Ski)
126 else if (Item is byte[] Bin)
128 }
129 }
130 else
131 return false;
132
133 if (SignerInfoVector[2] is not HashFunction DigestAlgorithm)
134 {
135 if (SignerInfoVector[2] is Vector DigestAlgorithmVector &&
136 DigestAlgorithmVector.FirstElement is HashFunction DigestAlgorithm2)
137 {
138 DigestAlgorithm = DigestAlgorithm2;
139 }
140 else
141 return false;
142 }
143
144 i = 3;
145
148 byte[] SignedAttributesBinary = [];
149 ContentType? ContentType = null;
151 bool HasSignedAttributes = false;
152
153 if (i < c &&
154 SignerInfoVector[i] is ContextSpecific SignedAttributesVector &&
155 SignedAttributesVector.Tag == 0)
156 {
157 SignedAttributesBinary = (byte[])SignedAttributesVector.SubSection.Clone();
158 SignedAttributesBinary[0] = (byte)UniversalTagNumber.Set | 0x20;
159 HasSignedAttributes = true;
160
161 foreach (object Attribute in SignedAttributesVector.Elements)
162 {
163 if (Attribute is ContentType ContentTypeAttribute)
164 ContentType = ContentTypeAttribute;
165 else if (Attribute is MessageDigest MessageDigestAttribute)
166 MessageDigest = MessageDigestAttribute;
167
168 SignedAttributes.Add(Attribute);
169 }
170
171 if (ContentType is null || MessageDigest is null)
172 return false;
173
174 i++;
175 }
176
177 if (i >= c)
178 return false;
179
181
182 if (SignatureAlgorithm is null)
183 {
184 if (SignerInfoVector[i] is Vector AlgorithmIdentifier)
185 {
186 SignatureAlgorithm = Security.SignatureAlgorithms.SignatureAlgorithm.TryDecode(AlgorithmIdentifier);
187 if (SignatureAlgorithm is null)
188 return false;
189 }
190 else
191 return false;
192 }
193
194 i++;
195
196 if (i >= c)
197 return false;
198
199 if (SignerInfoVector[i] is not byte[] Signature)
200 {
201 if (SignerInfoVector[i] is Vector v)
202 Signature = v.SubSection;
203 else
204 return false;
205 }
206
207 i++;
208
209 if (i < c &&
210 SignerInfoVector[i] is ContextSpecific UnsignedAttributesVector &&
211 UnsignedAttributesVector.Tag == 1)
212 {
213 foreach (object Attribute in UnsignedAttributesVector.Elements)
214 UnsignedAttributes.Add(Attribute);
215
216 i++;
217 }
218
219 Parsed = new SignerInfo()
220 {
222 Signer = Signer,
227 SignedAttributesData = SignedAttributesBinary,
234 };
235
236 return true;
237 }
238 }
239}
Abstract base class for signature algorithms, as defined in RFC 5280.
SignatureAlgorithm()
Abstract base class for signature algorithms, as defined in RFC 5280.
Signer Information, as defined in RFC 5652, §5.3.
Definition: SignerInfo.cs:17
byte[] SignedAttributesData
Binart representation of the signed attributes, as they are used in signature validation.
Definition: SignerInfo.cs:59
static bool TryParse(Vector SignerInfoVector, [NotNullWhen(true)] out SignerInfo? Parsed)
Tries to parse an ASN.1-encoded Signer Information structure, as defined in RFC 5652,...
Definition: SignerInfo.cs:98
System.Numerics.BigInteger Version
Version of encoding
Definition: SignerInfo.cs:28
byte?[] SubjectKeyIdentifier
Signer Subject Key Identifier.
Definition: SignerInfo.cs:43
System.Numerics.? BigInteger SerialNumber
Signer serial number.
Definition: SignerInfo.cs:38
bool HasSignedAttributes
If there are signed attributes.
Definition: SignerInfo.cs:64
ISignatureAlgorithm? SignatureAlgorithm
Signature algorithm
Definition: SignerInfo.cs:79
A chunked list is a linked list of chunks of objects of type T .
Definition: ChunkedList.cs:54
Interface for signature algorithms, as defined in RFC 5280.
class Names(Vector NamesVector)
Contains a collection of distinguished names.
Definition: Names.cs:9
class ContextSpecific(int Tag, Array Elements, byte[] SubSection)
A context-specific object (or set of objects).