Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
GeneralName.cs
1using System;
2using System.Diagnostics.CodeAnalysis;
3using System.Formats.Asn1;
4using System.Net;
5
7{
11 public abstract class GeneralName : SecurityObject
12 {
13 private object? name;
14
20 public override bool Configure(Vector SecurityInfo)
21 {
22 if (SecurityInfo.Length != 2)
23 return false;
24
25 object? Last = SecurityInfo.LastElementNested;
26
27 if (Last is Vector Context)
28 return TryParse(Context, out this.name);
29 else if (Last is byte[] BinName)
30 {
31 this.name = BinName;
32 return true;
33 }
34 else if (Last is Array A && A.Length == 0)
35 {
36 this.name = string.Empty;
37 return true;
38 }
39 else
40 return false;
41 }
42
46 public override bool IsConfigured => this.name is not null;
47
51 public object? Name => this.name;
52
59 public static bool TryParse(Vector ParsedVector, [NotNullWhen(true)] out object? Name)
60 {
61 object? First = ParsedVector.FirstElement;
62 Name = null;
63
64 if (First is not byte[] GeneralName)
65 {
66 while (First is Vector v && v.Length == 1)
67 {
68 ParsedVector = v;
69 First = ParsedVector.FirstElement;
70 }
71
72 if (First is byte[] GeneralName2)
73 GeneralName = GeneralName2;
74 else if (First is Vector v &&
75 v.Length > 0 &&
76 v.FirstElement is byte[] GeneralName3) // First is absolute name, second a relative name
77 {
78 GeneralName = GeneralName3;
79 }
80 else
81 {
82 Name = ParsedVector;
83 return true;
84 }
85 }
86
87 if (GeneralName.Length == 0)
88 return false;
89
90 switch (GeneralName[0])
91 {
92 case 0x83: // ORAddress
93 case 0x85: // EDIPartyName
94 return ASN1.TryDecodeDerAs(UniversalTagNumber.Sequence, GeneralName, out Name);
95
96 case 0x84: // Name
98 return true;
99
100 case 0x81: // IA5String
101 case 0x82:
102 case 0x86:
103 return ASN1.TryDecodeDerAs(UniversalTagNumber.IA5String, GeneralName, out Name);
104
105 case 0x80: // OtherName
106 return ASN1.TryDecodeDerAs(UniversalTagNumber.OctetString, GeneralName, out Name);
107
108 case 0x87: // IpAddress
109 if (!ASN1.TryDecodeDerAs(UniversalTagNumber.OctetString, GeneralName, out Name))
110 return false;
111
112 if (Name is byte[] BinIpAddress)
113 Name = new IPAddress(BinIpAddress);
114 else if (Name is null)
115 return false;
116
117 return true;
118
119 case 0x88:
120 return ASN1.TryDecodeDerAs(UniversalTagNumber.ObjectIdentifier, GeneralName, out Name);
121
122 default:
123 return false;
124 }
125 }
126 }
127}
Static class for parsing and decoding security objects encoded using Abstract Syntax Notation 1 (ASN....
Definition: ASN1.cs:22
static bool TryDecodeDerAs(UniversalTagNumber TagNumber, byte[] Data, out object? Value)
Decodes a DER-encoded object.
Definition: ASN1.cs:37
Abstract base class for general name properties.
Definition: GeneralName.cs:12
static bool TryParse(Vector ParsedVector, [NotNullWhen(true)] out object? Name)
Tries to parse a general name from an ASN.1 decoded vector.
Definition: GeneralName.cs:59
override bool Configure(Vector SecurityInfo)
If the object can be configured by the security information provided.
Definition: GeneralName.cs:20
override bool IsConfigured
If the object has been configured.
Definition: GeneralName.cs:46
Abstract base class for security objects.