Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PrivateKeyUsagePeriod.cs
1using System;
2using System.Diagnostics.CodeAnalysis;
3using System.Formats.Asn1;
4using System.Text;
6
8{
13 {
14 private DateTimeOffset? notBefore = null;
15 private DateTimeOffset? notAfter = null;
16 protected bool configured = false;
17
21 public override string Oid => "2.5.29.16";
22
26 public override bool IsConfigured => this.configured;
27
33 public override bool Configure(Vector SecurityInfo)
34 {
35 if (SecurityInfo.LastElementNested is not Vector UsagePeriod)
36 return false;
37
38 int c = UsagePeriod.Length;
39
40 if (c > 0)
41 {
42 if (!TryParseGeneralizedTime(UsagePeriod[0], out DateTimeOffset? NotBefore))
43 return false;
44
45 this.notBefore = NotBefore;
46
47 if (c > 1)
48 {
49 if (!TryParseGeneralizedTime(UsagePeriod[1], out DateTimeOffset? NotAfter))
50 return false;
51
52 this.notAfter = NotAfter;
53 }
54 }
55
56 this.configured = true;
57
58 return true;
59 }
60
61 public static bool TryParseGeneralizedTime(object? Data,
62 [NotNullWhen(true)] out DateTimeOffset? Result)
63 {
64 if (Data is DateTimeOffset Timestamp)
65 {
66 Result = Timestamp;
67 return true;
68 }
69
70 if (Data is byte[] Bin && Bin.Length > 0 && (Bin[0] & 0x80) != 0 &&
71 Bin.Length == Bin[1] + 2)
72 {
73 Bin = (byte[])Bin.Clone();
74 Bin[0] = (byte)UniversalTagNumber.IA5String;
75 AsnReader Reader = new(Bin, AsnEncodingRules.DER);
76 string s = Reader.ReadCharacterString((UniversalTagNumber)Bin[0]);
77
78 if (s.Length > 12)
79 {
80 StringBuilder sb = new();
81
82 sb.Append(s[..4]);
83 sb.Append('-');
84 sb.Append(s.AsSpan(4, 2));
85 sb.Append('-');
86 sb.Append(s.AsSpan(6, 2));
87 sb.Append('T');
88 sb.Append(s.AsSpan(8, 2));
89 sb.Append(':');
90 sb.Append(s.AsSpan(10, 2));
91 sb.Append(':');
92 sb.Append(s[12..]);
93
94 if (XML.TryParse(sb.ToString(), out Timestamp))
95 {
96 Result = Timestamp;
97 return true;
98 }
99 }
100 }
101
102 Result = null;
103 return false;
104 }
105
109 public DateTimeOffset? NotBefore => this.notBefore;
110
114 public DateTimeOffset? NotAfter => this.notAfter;
115 }
116}
override bool Configure(Vector SecurityInfo)
If the object can be configured by the security information provided.
Abstract base class for security objects.
Helps with common XML-related tasks.
Definition: XML.cs:21
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:892