Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
E2eEndpoint.cs
1using System;
2using System.IO;
3using System.Text;
4using System.Xml;
5
7{
11 public abstract class E2eEndpoint : IE2eEndpoint
12 {
13 private IE2eSymmetricCipher defaultSymmetricCipher;
14 private IE2eEndpoint prev = null;
15 private uint counter = 0;
16
22 {
23 this.defaultSymmetricCipher = DefaultSymmetricCipher;
24 }
25
29 public virtual void Dispose()
30 {
31 this.defaultSymmetricCipher?.Dispose();
32 this.defaultSymmetricCipher = null;
33 }
34
38 public abstract int SecurityStrength
39 {
40 get;
41 }
42
47 {
48 get => this.prev;
49 set => this.prev = value;
50 }
51
55 public abstract string LocalName
56 {
57 get;
58 }
59
64
68 public abstract byte[] PublicKey
69 {
70 get;
71 }
72
76 public abstract string PublicKeyBase64
77 {
78 get;
79 }
80
86 public abstract IE2eEndpoint Create(int SecurityStrength);
87
93 public abstract IE2eEndpoint CreatePrivate(byte[] Secret);
94
100 public abstract IE2eEndpoint CreatePublic(byte[] PublicKey);
101
107 public IE2eEndpoint Parse(XmlElement Xml)
108 {
109 foreach (XmlAttribute Attr in Xml.Attributes)
110 {
111 switch (Attr.Name)
112 {
113 case "pub":
114 return this.CreatePublic(Convert.FromBase64String(Attr.Value));
115
116 case "d":
117 return this.CreatePrivate(Convert.FromBase64String(Attr.Value));
118 }
119 }
120
121 return null;
122 }
123
129 public void ToXml(StringBuilder Xml, string ParentNamespace)
130 {
131 Xml.Append('<');
132 Xml.Append(this.LocalName);
133 Xml.Append(" pub=\"");
134 Xml.Append(this.PublicKeyBase64);
135
136 string ns = this.Namespace;
137 if (ns != ParentNamespace)
138 {
139 Xml.Append("\" xmlns=\"");
140 Xml.Append(ns);
141 }
142
143 Xml.Append("\"/>");
144 }
145
149 public virtual bool SupportsSharedSecrets => true;
150
156 public virtual byte[] EncryptSecret(byte[] Secret)
157 {
158 throw new NotSupportedException("Encrypting secrets not supported.");
159 }
160
166 public virtual byte[] DecryptSecret(byte[] Secret)
167 {
168 throw new NotSupportedException("Decrypting secrets not supported.");
169 }
170
176 public abstract byte[] GetSharedSecret(IE2eEndpoint RemoteEndpoint);
177
181 public virtual bool SupportsSignatures => true;
182
188 public abstract byte[] Sign(byte[] Data);
189
195 public abstract byte[] Sign(Stream Data);
196
203 public abstract bool Verify(byte[] Data, byte[] Signature);
204
211 public abstract bool Verify(Stream Data, byte[] Signature);
212
216 public virtual bool Safe => true;
217
221 public virtual bool Slow => false;
222
224 public override string ToString()
225 {
226 StringBuilder Xml = new StringBuilder();
227 this.ToXml(Xml, string.Empty);
228 return Xml.ToString();
229 }
230
232 public override abstract bool Equals(object obj);
233
235 public override abstract int GetHashCode();
236
240 public int Score
241 {
242 get
243 {
244 int Result = 0;
245
246 if (this.SupportsSharedSecrets)
247 Result++;
248
249 if (this.SupportsSignatures)
250 Result++;
251
252 if (this.Safe)
253 Result++;
254
255 if (!this.Slow)
256 Result++;
257
258 return Result;
259 }
260 }
261
265 public virtual IE2eSymmetricCipher DefaultSymmetricCipher => this.defaultSymmetricCipher;
266
271 public uint GetNextCounter()
272 {
273 return ++this.counter;
274 }
275 }
276}
Abstract base class for End-to-End encryption schemes.
Definition: E2eEndpoint.cs:12
abstract byte[] Sign(Stream Data)
Signs binary data using the local private key.
virtual bool Slow
If implementation is slow, compared to other options.
Definition: E2eEndpoint.cs:221
virtual bool SupportsSignatures
If signatures are supported.
Definition: E2eEndpoint.cs:181
abstract byte[] Sign(byte[] Data)
Signs binary data using the local private key.
virtual IE2eSymmetricCipher DefaultSymmetricCipher
Default symmetric cipher.
Definition: E2eEndpoint.cs:265
abstract IE2eEndpoint Create(int SecurityStrength)
Creates a new key.
uint GetNextCounter()
Gets the next counter value.
Definition: E2eEndpoint.cs:271
abstract bool Verify(Stream Data, byte[] Signature)
Verifies a signature.
virtual bool Safe
If endpoint is considered safe (i.e. there are no suspected backdoors)
Definition: E2eEndpoint.cs:216
virtual byte[] EncryptSecret(byte[] Secret)
Encrypts a secret. Used if shared secrets cannot be calculated.
Definition: E2eEndpoint.cs:156
virtual void Dispose()
IDisposable.Dispose
Definition: E2eEndpoint.cs:29
abstract IE2eEndpoint CreatePublic(byte[] PublicKey)
Creates a new endpoint given a public key.
abstract IE2eEndpoint CreatePrivate(byte[] Secret)
Creates a new endpoint given a private key.
abstract override bool Equals(object obj)
abstract bool Verify(byte[] Data, byte[] Signature)
Verifies a signature.
void ToXml(StringBuilder Xml, string ParentNamespace)
Exports the public key information to XML.
Definition: E2eEndpoint.cs:129
IE2eEndpoint Parse(XmlElement Xml)
Parses endpoint information from an XML element.
Definition: E2eEndpoint.cs:107
E2eEndpoint(IE2eSymmetricCipher DefaultSymmetricCipher)
Abstract base class for End-to-End encryption schemes.
Definition: E2eEndpoint.cs:21
virtual bool SupportsSharedSecrets
If shared secrets can be calculated from the endpoints keys.
Definition: E2eEndpoint.cs:149
virtual string Namespace
Namespace of the E2E encryption scheme
Definition: E2eEndpoint.cs:63
int Score
Provides a score for the endpoint. More features, higher score.
Definition: E2eEndpoint.cs:241
abstract string PublicKeyBase64
Remote public key, as a Base64 string.
Definition: E2eEndpoint.cs:77
virtual byte[] DecryptSecret(byte[] Secret)
Decrypts a secret. Used if shared secrets cannot be calculated.
Definition: E2eEndpoint.cs:166
abstract int SecurityStrength
Security strength of End-to-End encryption scheme.
Definition: E2eEndpoint.cs:39
abstract byte[] GetSharedSecret(IE2eEndpoint RemoteEndpoint)
Gets a shared secret
IE2eEndpoint Previous
Previous keys.
Definition: E2eEndpoint.cs:47
abstract string LocalName
Local name of the E2E encryption scheme
Definition: E2eEndpoint.cs:56
abstract byte[] PublicKey
Remote public key.
Definition: E2eEndpoint.cs:69
Class managing end-to-end encryption.
const string IoTHarmonizationE2ECurrent
Current namespace for End-to-End encryption.
Abstract base class for End-to-End encryption schemes.
Definition: IE2eEndpoint.cs:12
Interface for symmetric ciphers.