Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CertificateStore.cs
2using System;
3using System.Globalization;
4using System.Threading.Tasks;
5using Waher.Content;
6using Waher.Events;
10
12{
16 public static class CertificateStore
17 {
21 private const int maxDaysInCache = 7;
22
30 public static Task<Certificate?> TryLoadCertificate(string IdDomain, string Country, byte[] KeyReference)
31 {
32 return TryLoadCertificate(IdDomain, Country, KeyReference, null);
33 }
34
43 public static async Task<Certificate?> TryLoadCertificate(string IdDomain, string Country, byte[] KeyReference,
44 ICommunicationLayer? Client)
45 {
46 Country = Country.ToUpper(CultureInfo.InvariantCulture);
47 string KeyReferenceString = Hashes.BinaryToString(KeyReference).ToUpper(CultureInfo.InvariantCulture);
48
49 string Uri = "https://" + IdDomain + "/IcaoPki/" + Country + "/" + KeyReferenceString + ".cer";
50
51 Client?.Information("Loading certificate from " + Uri);
52
53 ContentResponse? Response = await TryCachedGet(Uri, Client);
54
55 if (Response is null)
56 return null;
57
58 try
59 {
60 if (Certificate.TryParse(Response.Encoded, out Certificate? Result))
61 return Result;
62 else
63 {
64 Client?.Error("Unable to parse certificate.");
65 return null;
66 }
67 }
68 catch (Exception ex)
69 {
70 if (Client is null)
71 Log.Exception(ex);
72 else
73 Client?.Error(ex.Message);
74
75 return null;
76 }
77 }
78
84 public static Task<CertificateList?> TryLoadCrl(string Url)
85 {
86 return TryLoadCrl(Url, null);
87 }
88
95 public static async Task<CertificateList?> TryLoadCrl(string Url, ICommunicationLayer? Client)
96 {
97 Client?.Information("Loading CRL from " + Url);
98
99 ContentResponse? Response = await TryCachedGet(Url, Client);
100
101 if (Response is null || Response.Decoded is not CertificateList Crl)
102 return null;
103
104 return Crl;
105 }
106
113 public static Task<ContentResponse?> TryCachedGet(string Uri)
114 {
115 return TryCachedGet(Uri, null);
116 }
117
125 public static async Task<ContentResponse?> TryCachedGet(string Url, ICommunicationLayer? Client)
126 {
127 string UriKey = "Cache.Uri." + Url;
128 string TimestampKey = "Cache.Timestamp." + Url;
129 string ContentTypeKey = "Cache.ContentType." + Url;
130 DateTime Now = DateTime.UtcNow;
131 Uri Uri = new(Url);
132 ContentResponse Response;
133
134 try
135 {
136 string Base64 = await RuntimeSettings.GetAsync(UriKey, string.Empty);
137 DateTime Timestamp = await RuntimeSettings.GetAsync(TimestampKey, DateTime.MinValue);
138 string ContentType = await RuntimeSettings.GetAsync(ContentTypeKey, string.Empty);
139
140 if (!string.IsNullOrEmpty(Base64) &&
141 (Now - Timestamp).TotalDays < maxDaysInCache &&
142 !string.IsNullOrEmpty(ContentType))
143 {
144 byte[] Bin = Convert.FromBase64String(Base64);
145 Response = await InternetContent.DecodeAsync(ContentType, Bin, Uri);
146
147 if (!Response.HasError)
148 {
149 Client?.Information("Using cached result for " + Url);
150
151 return Response;
152 }
153 }
154 }
155 catch (Exception)
156 {
157 // Ignore
158 }
159
160 Client?.Information("Retrieving " + Url);
161
162 Response = await InternetContent.GetAsync(Uri);
163
164 if (Response.HasError)
165 {
166 if (Client is null)
167 Log.Exception(Response.Error);
168 else
169 Client?.Error(Response.Error.Message);
170
171 return null;
172 }
173
174 Client?.Information("Storing content in cache.");
175
176 await RuntimeSettings.SetAsync(UriKey, Convert.ToBase64String(Response.Encoded));
177 await RuntimeSettings.SetAsync(TimestampKey, Now);
178 await RuntimeSettings.SetAsync(ContentTypeKey, Response.ContentType);
179
180 return Response;
181 }
182 }
183}
Certificate, as defined in RFC 5280, §4.1.
Definition: Certificate.cs:13
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.
Definition: Certificate.cs:36
static Task< Certificate?> TryLoadCertificate(string IdDomain, string Country, byte[] KeyReference)
Tries to load an ICAO certificate, provided its country and key reference.
static Task< ContentResponse?> TryCachedGet(string Uri)
Tries to GET content from an URI. If content is cached, it is returned directly. Otherwise,...
static async Task< CertificateList?> TryLoadCrl(string Url, ICommunicationLayer? Client)
Tries to load an ICAO-compliant CRL, provided its URL.
static Task< CertificateList?> TryLoadCrl(string Url)
Tries to load an ICAO-compliant CRL, provided its URL.
static async Task< Certificate?> TryLoadCertificate(string IdDomain, string Country, byte[] KeyReference, ICommunicationLayer? Client)
Tries to load an ICAO certificate, provided its country and key reference.
static async Task< ContentResponse?> TryCachedGet(string Url, ICommunicationLayer? Client)
Tries to GET content from an URI. If content is cached, it is returned directly. Otherwise,...
Certificate List, as defined in RFC 5280, §5.1
Contains information about a response to a content request.
byte[] Encoded
Encoded object.
string ContentType
Internet Content-Type of encoded object.
bool HasError
If an error occurred.
object Decoded
Decoded object.
Exception Error
Error response.
Static class managing encoding and decoding of internet content.
static Task< ContentResponse > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
static Task< ContentResponse > GetAsync(Uri Uri, params KeyValuePair< string, string >[] Headers)
Gets a resource, given its URI.
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Contains methods for simple hash calculations.
Definition: Hashes.cs:57
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:63
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.