19 public override string Oid =>
"1.2.840.113549.1.7.2";
24 public Vector? Asn1Vector {
get;
private set; }
29 public System.Numerics.BigInteger Version {
get;
private set; }
34 public HashFunction[] DigestAlgorithms {
get;
private set; } = [];
39 public string EncapsulatedContentOid {
get;
private set; } =
string.Empty;
44 public byte[] EncapsulatedContent {
get;
private set; } = [];
49 public SignerInfo[] SignerInfos {
get;
private set; } = [];
54 public Certificate[] Certificates {
get;
private set; } = [];
64 public override bool IsConfigured => this.Asn1Vector is not
null;
71 public override bool Configure(Vector SecurityInfo)
73 if (SecurityInfo.Length != 2 ||
74 SecurityInfo.LastElement is not Vector SignedDataVector)
79 if (SignedDataVector.Length == 1 &&
80 SignedDataVector.FirstElement is Vector SignedDataVector2)
82 SignedDataVector = SignedDataVector2;
85 if (SignedDataVector.Length < 4)
88 if (SignedDataVector.FirstElement is not
System.Numerics.BigInteger Version)
91 if (SignedDataVector[1] is not Vector DigestAlgorithms)
94 if (SignedDataVector[2] is not Vector EncapsulatedContent ||
95 EncapsulatedContent.Length < 2)
100 byte[] BinaryContent;
105 else if (EncapsulatedContent.FirstElement is
string s)
107 else if (EncapsulatedContent.FirstElement is Vector v &&
108 v.FirstElement is
string s2)
115 if (EncapsulatedContent[1] is
byte[] Bin)
117 else if (EncapsulatedContent[1] is Vector ContentVector)
119 if (ContentVector.Length == 1)
121 if (ContentVector.FirstElement is
byte[] EmbeddedContent)
122 BinaryContent = EmbeddedContent;
123 else if (ContentVector.FirstElement is Vector EmbeddedContent2)
124 BinaryContent = EmbeddedContent2.SubSection;
126 BinaryContent = ContentVector.SubSection;
129 BinaryContent = ContentVector.SubSection;
137 for (
int i = 3; i < SignedDataVector.Length - 1; i++)
148 RevocationInfoChoices = v;
155 if (SignedDataVector.LastElement is not Vector SignerInfos)
160 foreach (
object Item
in SignerInfos.Elements)
162 if (Item is not Vector SignerInfoVector ||
168 SignerInfoList.
Add(Parsed);
173 while (DigestAlgorithms.Length == 1 &&
174 DigestAlgorithms.FirstElement is Vector v)
176 DigestAlgorithms = v;
179 foreach (
object Algorithm
in DigestAlgorithms.Elements)
187 if (CertificateSet is not
null)
189 foreach (
object Item
in CertificateSet.Elements)
191 if (Item is Vector CertificateVector &&
194 Certificates.
Add(Parsed);
201 this.Asn1Vector = SecurityInfo;
202 this.Version = Version;
203 this.DigestAlgorithms = [.. HashAlgorithms];
204 this.EncapsulatedContentOid = ContentOid;
205 this.EncapsulatedContent = BinaryContent;
206 this.Certificates = [.. Certificates];
207 this.RevocationInfoChoices = RevocationInfoChoices;
208 this.SignerInfos = [.. SignerInfoList];
218 public bool CheckSignature(
bool VerifyCertificates)
220 return this.CheckSignature(VerifyCertificates,
null);
231 if (!this.IsConfigured ||
232 this.SignerInfos is
null ||
233 this.EncapsulatedContent is
null)
238 bool SignatureValidated =
false;
257 if (Convert.ToBase64String(Digest) != Convert.ToBase64String(Info.
MessageDigest))
259 Client?.
Error(
"Computed digest does not match message digest in signer info.");
263 Client?.
Information(
"Computed digest matches message digest in signer info.");
276 KeyValuePair<string?, byte[]?> P = TravelDocumentsClient.GetSubjectKeyIdentifier(
Certificate);
278 if (P.Value is not
null &&
280 Convert.ToBase64String(P.Value))
288 if (SelectedCertificate is
null)
290 Client?.
Error(
"Certificate used to sign message not found among included certificates.");
298 SelectedCertificate.PublicKey, Client))
300 Client?.
Information(
"Signature validated successfully.");
301 SignatureValidated =
true;
303 if (VerifyCertificates)
311 Client?.
Error(
"Signature validation failed.");
316 if (!SignatureValidated)
318 Client?.
Error(
"No signature validated.");
Certificate, as defined in RFC 5280, §4.1.
static bool TryParse(byte[] RawCertificate, [NotNullWhen(true)] out Certificate? Parsed)
Tries to parse an ASN.1-encoded Certificate, as defined in RFC 5280, §4.1.
System.Numerics.BigInteger SerialNumber
Serial Number
Abstract base class for hash functions.
abstract byte[] ComputeHash(byte[] Data)
Computes a Hash Digest from binary data.
Abstract base class for security objects.
abstract string Oid
OID identifying the type of object.
Signer Information, as defined in RFC 5652, §5.3.
string? ContentType
OID of content
byte[] SignedAttributesData
Binart representation of the signed attributes, as they are used in signature validation.
HashFunction? DigestAlgorithm
Digest algorithm.
byte[] Signature
Signature
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,...
byte?[] SubjectKeyIdentifier
Signer Subject Key Identifier.
System.Numerics.? BigInteger SerialNumber
Signer serial number.
bool HasSignedAttributes
If there are signed attributes.
byte?[] MessageDigest
Message digest
ISignatureAlgorithm? SignatureAlgorithm
Signature algorithm
A chunked list is a linked list of chunks of objects of type T .
void Add(T Item)
Adds an item to the collection.
Interface for security objects.
void Information(string Comment)
Called to inform the viewer of something.
void Error(string Error)
Called to inform the viewer of an error state.
Interface for observable classes implementing communication protocols.
class ContextSpecific(int Tag, Array Elements, byte[] SubSection)
A context-specific object (or set of objects).
class SignedData()
Signed Data, as defined in RFC 5652, §5.1.