-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathIEndToEndEncryption.cs
More file actions
401 lines (373 loc) · 21.9 KB
/
IEndToEndEncryption.cs
File metadata and controls
401 lines (373 loc) · 21.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using Waher.Events;
using Waher.Networking.XMPP.Events;
namespace Waher.Networking.XMPP
{
/// <summary>
/// End-to-end encryption mode.
/// </summary>
public enum E2ETransmission
{
/// <summary>
/// Will only send the stanza, if end-to-end encryption using Post-Quantum Cryptography
/// can be used. If not, an exception is thrown.
/// </summary>
AssertE2EPQC,
/// <summary>
/// Will only send the stanza, if end-to-end encryption can be used. If not, an exception is thrown.
/// </summary>
AssertE2E,
/// <summary>
/// Will only send the stanza, if end-to-end encryption can be used. If not, the stanza is ignored.
/// </summary>
IgnoreIfNotE2E,
/// <summary>
/// Will send the stanza using end-to-end encryption, if possible. If not, the stanza is sent as a normal
/// stanza.
/// </summary>
NormalIfNotE2E
}
/// <summary>
/// End-to-end encryption interface.
/// </summary>
public interface IEndToEndEncryption : IDisposable
{
/// <summary>
/// ID of <see cref="IEndToEndEncryption"/> instance.
/// </summary>
Guid InstanceId { get; }
/// <summary>
/// Tries to get a symmetric cipher from a reference.
/// </summary>
/// <param name="LocalName">Local Name</param>
/// <param name="Namespace">Namespace</param>
/// <param name="Cipher">Symmetric cipher, if found.</param>
/// <returns>If a symmetric cipher was found with the given name.</returns>
bool TryGetSymmetricCipher(string LocalName, string Namespace, out IE2eSymmetricCipher Cipher);
/// <summary>
/// Encrypts binary data that can be sent to an XMPP client out of band.
/// </summary>
/// <param name="Id">ID Attribute.</param>
/// <param name="Type">Type Attribute.</param>
/// <param name="From">From attribute.</param>
/// <param name="To">To attribute.</param>
/// <param name="Data">Data to encrypt.</param>
/// <param name="EndpointReference">Endpoint used for encryption.</param>
/// <returns>Encrypted data, if encryption was possible to the recipient, or null if not.</returns>
Task<byte[]> Encrypt(string Id, string Type, string From, string To, byte[] Data, out IE2eEndpoint EndpointReference);
/// <summary>
/// Decrypts binary data received from an XMPP client out of band.
/// </summary>
/// <param name="EndpointReference">Endpoint reference.</param>
/// <param name="Id">ID Attribute.</param>
/// <param name="Type">Type Attribute.</param>
/// <param name="From">From attribute.</param>
/// <param name="To">To attribute.</param>
/// <param name="Data">Data to decrypt.</param>
/// <param name="SymmetricCipher">Type of symmetric cipher to use to decrypt content.</param>
/// <returns>Decrypted data, if decryption was possible from the recipient, or null if not.</returns>
Task<byte[]> Decrypt(string EndpointReference, string Id, string Type, string From, string To, byte[] Data,
IE2eSymmetricCipher SymmetricCipher);
/// <summary>
/// Encrypts binary data that can be sent to an XMPP client out of band.
/// </summary>
/// <param name="Id">ID Attribute.</param>
/// <param name="Type">Type Attribute.</param>
/// <param name="From">From attribute.</param>
/// <param name="To">To attribute.</param>
/// <param name="Data">Data to encrypt.</param>
/// <param name="Encrypted">Encrypted data will be stored here.</param>
/// <returns>If encryption was possible, a reference to the endpoint performing the encryption, null otherwise.</returns>
Task<IE2eEndpoint> Encrypt(string Id, string Type, string From, string To, Stream Data, Stream Encrypted);
/// <summary>
/// Decrypts binary data received from an XMPP client out of band.
/// </summary>
/// <param name="EndpointReference">Endpoint reference.</param>
/// <param name="Id">ID Attribute.</param>
/// <param name="Type">Type Attribute.</param>
/// <param name="From">From attribute.</param>
/// <param name="To">To attribute.</param>
/// <param name="Data">Data to decrypt.</param>
/// <param name="SymmetricCipher">Type of symmetric cipher to use to decrypt content.</param>
/// <returns>Decrypted data, if decryption was possible from the recipient, or null if not.</returns>
Task<Stream> Decrypt(string EndpointReference, string Id, string Type, string From, string To, Stream Data,
IE2eSymmetricCipher SymmetricCipher);
/// <summary>
/// Encrypts data into XML that can be sent over XMPP.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="Id">ID Attribute.</param>
/// <param name="Type">Type Attribute.</param>
/// <param name="From">From attribute.</param>
/// <param name="To">To attribute.</param>
/// <param name="DataXml">Data to encrypt.</param>
/// <param name="Xml">XML containing the encrypted data will be output here.</param>
/// <returns>If encryption was possible to the recipient, or not.</returns>
bool Encrypt(XmppClient Client, string Id, string Type, string From, string To, string DataXml, StringBuilder Xml);
/// <summary>
/// Decrypts data from XML that has been received over XMPP.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="Id">ID Attribute.</param>
/// <param name="Type">Type Attribute.</param>
/// <param name="From">From attribute.</param>
/// <param name="To">To attribute.</param>
/// <param name="E2eElement">XML element containing the encrypted data.</param>
/// <param name="SymmetricCipher">Type of symmetric cipher to use to decrypt content.</param>
/// <returns>Decrypted XML, together with an endpoint reference, if successful, null otherwise..</returns>
Tuple<string, string> Decrypt(XmppClient Client, string Id, string Type, string From, string To, XmlElement E2eElement,
IE2eSymmetricCipher SymmetricCipher);
/// <summary>
/// Sends an end-to-end encrypted message, if possible. If recipient does not support end-to-end
/// encryption, the message is sent normally.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="QoS">Quality of Service level of message.</param>
/// <param name="Type">Type of message to send.</param>
/// <param name="Id">Message ID</param>
/// <param name="To">Destination address</param>
/// <param name="CustomXml">Custom XML</param>
/// <param name="Body">Body text of chat message.</param>
/// <param name="Subject">Subject</param>
/// <param name="Language">Language used.</param>
/// <param name="ThreadId">Thread ID</param>
/// <param name="ParentThreadId">Parent Thread ID</param>
/// <param name="DeliveryCallback">Callback to call when message has been sent, or failed to be sent.</param>
/// <param name="State">State object to pass on to the callback method.</param>
Task SendMessage(XmppClient Client, E2ETransmission E2ETransmission, QoSLevel QoS, MessageType Type,
string Id, string To, string CustomXml, string Body, string Subject, string Language, string ThreadId,
string ParentThreadId, EventHandlerAsync<DeliveryEventArgs> DeliveryCallback, object State);
/// <summary>
/// Sends an IQ Get request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqGet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State);
/// <summary>
/// Sends an IQ Get request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqGet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries);
/// <summary>
/// Sends an IQ Get request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <param name="DropOff">If the retry timeout should be doubled between retries (true), or if the same retry timeout
/// should be used for all retries. The retry timeout will never exceed <paramref name="MaxRetryTimeout"/>.</param>
/// <param name="MaxRetryTimeout">Maximum retry timeout. Used if <paramref name="DropOff"/> is true.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqGet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries, bool DropOff,
int MaxRetryTimeout);
/// <summary>
/// Sends an IQ Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqSet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State);
/// <summary>
/// Sends an IQ Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqSet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries);
/// <summary>
/// Sends an IQ Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <param name="DropOff">If the retry timeout should be doubled between retries (true), or if the same retry timeout
/// should be used for all retries. The retry timeout will never exceed <paramref name="MaxRetryTimeout"/>.</param>
/// <param name="MaxRetryTimeout">Maximum retry timeout. Used if <paramref name="DropOff"/> is true.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqSet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries, bool DropOff,
int MaxRetryTimeout);
/// <summary>
/// Sends an IQ Get request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="DeliveryCallback">Optional callback called when request has been sent.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqGet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, EventHandlerAsync<DeliveryEventArgs> DeliveryCallback);
/// <summary>
/// Sends an IQ Get request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <param name="DeliveryCallback">Optional callback called when request has been sent.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqGet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries,
EventHandlerAsync<DeliveryEventArgs> DeliveryCallback);
/// <summary>
/// Sends an IQ Get request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <param name="DropOff">If the retry timeout should be doubled between retries (true), or if the same retry timeout
/// should be used for all retries. The retry timeout will never exceed <paramref name="MaxRetryTimeout"/>.</param>
/// <param name="MaxRetryTimeout">Maximum retry timeout. Used if <paramref name="DropOff"/> is true.</param>
/// <param name="DeliveryCallback">Optional callback called when request has been sent.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqGet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries, bool DropOff,
int MaxRetryTimeout, EventHandlerAsync<DeliveryEventArgs> DeliveryCallback);
/// <summary>
/// Sends an IQ Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="DeliveryCallback">Optional callback called when request has been sent.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqSet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, EventHandlerAsync<DeliveryEventArgs> DeliveryCallback);
/// <summary>
/// Sends an IQ Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <param name="DeliveryCallback">Optional callback called when request has been sent.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqSet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries,
EventHandlerAsync<DeliveryEventArgs> DeliveryCallback);
/// <summary>
/// Sends an IQ Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the request.</param>
/// <param name="Callback">Callback method to call when response is returned.</param>
/// <param name="State">State object to pass on to the callback method.</param>
/// <param name="RetryTimeout">Retry Timeout, in milliseconds.</param>
/// <param name="NrRetries">Number of retries.</param>
/// <param name="DropOff">If the retry timeout should be doubled between retries (true), or if the same retry timeout
/// should be used for all retries. The retry timeout will never exceed <paramref name="MaxRetryTimeout"/>.</param>
/// <param name="MaxRetryTimeout">Maximum retry timeout. Used if <paramref name="DropOff"/> is true.</param>
/// <param name="DeliveryCallback">Optional callback called when request has been sent.</param>
/// <returns>ID of IQ stanza</returns>
Task<uint> SendIqSet(XmppClient Client, E2ETransmission E2ETransmission, string To, string Xml,
EventHandlerAsync<IqResultEventArgs> Callback, object State, int RetryTimeout, int NrRetries, bool DropOff,
int MaxRetryTimeout, EventHandlerAsync<DeliveryEventArgs> DeliveryCallback);
/// <summary>
/// Returns a response to an IQ Get/Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="Id">ID attribute of original IQ request.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the response.</param>
Task SendIqResult(XmppClient Client, E2ETransmission E2ETransmission, string Id, string To, string Xml);
/// <summary>
/// Returns an error response to an IQ Get/Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="Id">ID attribute of original IQ request.</param>
/// <param name="To">Destination address</param>
/// <param name="Xml">XML to embed into the response.</param>
Task SendIqError(XmppClient Client, E2ETransmission E2ETransmission, string Id, string To, string Xml);
/// <summary>
/// Returns an error response to an IQ Get/Set request.
/// </summary>
/// <param name="Client">XMPP client to send the end-to-end encrypted stanza through.</param>
/// <param name="E2ETransmission">How transmission is to be handled if no end-to-end encryption
/// can be performed.</param>
/// <param name="Id">ID attribute of original IQ request.</param>
/// <param name="To">Destination address</param>
/// <param name="ex">Internal exception object.</param>
Task SendIqError(XmppClient Client, E2ETransmission E2ETransmission, string Id, string To, Exception ex);
}
}