Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DocumentSecurityObject.cs
1using System;
2using System.Diagnostics.CodeAnalysis;
3using System.Security.Cryptography.Pkcs;
7using Waher.Events;
8
10{
14 public class DocumentSecurityObject : DataObject
15 {
20 : base([])
21 {
22 }
23
31 : base(Value)
32 {
33 this.SignedData = SignedData;
34 this.LdsSecurityObject = LdsSecurityObject;
35 }
36
40 public override ushort Tag => 0x77;
41
45 public SignedData? SignedData { get; }
46
51
59 public override bool TryParse(byte[] Value, TravelDocumentsClient Client,
60 [NotNullWhen(true)] out IDataObject? Parsed)
61 {
62 Parsed = null;
63
64 if (Client.AppInfo?.HasAtLeastLdsVersion(1, 8) ?? false)
65 {
66 try
67 {
69 {
70 Client.Warning("Could not parse Signed CMS:\r\n\r\n " +
71 Convert.ToBase64String(Value, Base64FormattingOptions.InsertLineBreaks));
72 return false;
73 }
74
75 byte[]? Content;
76 string? ContentOid;
77
78 // First use platform/OS-independent signature validation implementation.
79
80 if (SignedData.CheckSignature(false, Client)) // No need to validate certificate at this point, as it is validated when the certificate chain is validated.
81 {
82 Content = SignedData.Data.EncapsulatedContent;
83 ContentOid = SignedData.Data.EncapsulatedContentOid;
84 }
85 else
86 {
87 if (!Client.PermitPlatformDependentValidation)
88 return false;
89
90 // If platform/OS-independent signature validation fails
91 // (implemetation error?), double-check with platform/OS-dependent
92 // signature validation.
93
94 try
95 {
96 SignedCms SignedDataX = new(); // Backup, in case of implementation error in SignedMessage.
97 SignedDataX.Decode(Value);
98
99 SignedDataX.CheckSignature(true);
100
101 Content = SignedDataX.ContentInfo?.Content;
102 ContentOid = SignedDataX.ContentInfo?.ContentType?.Value;
103
104 Log.Debug("Platform/OS-independent signature validation failed, but Platform/OS-dependent signature validation successful. Check communication logs for more details.");
105
106 Client.Warning("Platform/OS-independent signature validation failed, but Platform/OS-dependent signature validation successful:\r\n\r\n" +
107 Convert.ToBase64String(Value, Base64FormattingOptions.InsertLineBreaks));
108 }
109 catch (Exception)
110 {
111 Client.Warning("Could not validate signatures in Signed CMS:\r\n\r\n " +
112 Convert.ToBase64String(Value, Base64FormattingOptions.InsertLineBreaks));
113 return false;
114 }
115 }
116
117 if (Content is null || string.IsNullOrEmpty(ContentOid))
118 return false;
119
120 if (!ASN1.TryInstantiate(ContentOid, out ISecurityObject? SecurityObject))
121 {
122 Client.Warning("OID not recognized: " + ContentOid);
123 ASN1.ReportOidNotRecognized(ContentOid);
124 return false;
125 }
126
128 return false;
129
130 ASN1.TryDecodeDer(Client, Content, out object? ParsedContent);
131
132 if (ParsedContent is not Vector ContentVector)
133 return false;
134
136 {
137 if (!LdsSecurityObject.Configure(ContentVector))
138 {
140 return false;
141 }
142 }
143
144 Parsed = new DocumentSecurityObject(Value, SignedData.Data, LdsSecurityObject);
145 return true;
146 }
147 catch (Exception ex)
148 {
149 Client.Exception(ex);
150 return false;
151 }
152 }
153 else
154 {
155 // TODO: LDS version < 1.8 support
156
157 return false;
158 }
159 }
160
167 public bool ValidateDataGroup(int Nr, byte[] DataRead)
168 {
169 if (!(this.LdsSecurityObject?.DataGroupHashValues?.TryGetValue(Nr, out byte[]? ExpectedDigest) ?? false))
170 return false;
171
172 HashFunction[]? HashFunctions = this.LdsSecurityObject!.HashFunctions;
173 if (HashFunctions is null)
174 return false;
175
176 int i, c = ExpectedDigest.Length;
177
178 foreach (HashFunction H in HashFunctions)
179 {
180 byte[] Digest = H.ComputeHash(DataRead);
181
182 if (Digest.Length != c)
183 continue;
184
185 for (i = 0; i < c; i++)
186 {
187 if (Digest[i] != ExpectedDigest[i])
188 break;
189 }
190
191 if (i == c)
192 return true;
193 }
194
195 return false;
196 }
197 }
198}
Static class for parsing and decoding security objects encoded using Abstract Syntax Notation 1 (ASN....
Definition: ASN1.cs:22
static bool TryInstantiate(string Oid, [NotNullWhen(true)] out ISecurityObject? Object)
Tries to instantiate a new object of a given OID.
Definition: ASN1.cs:560
static int ReportOidNotRecognized(string Oid)
Records an OID as not recognized.
Definition: ASN1.cs:378
static int ReportOidNotConfigured(string Oid)
Records an OID as not configured properly.
Definition: ASN1.cs:399
static bool TryDecodeDer(byte[] Data, out object? Value)
Decodes a DER-encoded object.
Definition: ASN1.cs:77
Document Security Object. Reference: §4.6.2, EF.SOD, ICAO Doc 9303-10, Table 36.
DocumentSecurityObject(byte[] Value, SignedData? SignedData, LdsSecurityObject LdsSecurityObject)
Document Security Object. Reference: §4.6.2, EF.SOD, ICAO Doc 9303-10, Table 36.
DocumentSecurityObject()
Document Security Object. Reference: §4.6.2, EF.SOD, ICAO Doc 9303-10, Table 36.
bool ValidateDataGroup(int Nr, byte[] DataRead)
Validates data read from a data group, using the information in the LDS Security Object.
override bool TryParse(byte[] Value, TravelDocumentsClient Client, [NotNullWhen(true)] out IDataObject? Parsed)
Tries to parse a binary representation of the data object.
abstract byte[] ComputeHash(byte[] Data)
Computes a Hash Digest from binary data.
LDS Security Object V1. Reference: §4.6.2.3, ICAO Doc 9303-10.
override bool IsConfigured
If the object has been configured.
override string Oid
OID identifying the type of object.
override bool Configure(Vector SecurityInfo)
If the object can be configured by the security information provided.
Abstract base class for security objects.
Signed Data, as defined in RFC 5652, §5.1.
static bool TryParse(byte[] RawCertificate, [NotNullWhen(true)] out SignedMessage? Parsed)
Tries to parse an ASN.1-encoded Signed Data structure, as defined in RFC 5652, §5....
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Debug(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs a debug event.
Definition: Log.cs:228