24
24
using Strilanc . Value ;
25
25
using System ;
26
26
using System . Collections . Generic ;
27
+ using System . Threading . Tasks ;
27
28
28
29
namespace libsignal
29
30
{
@@ -141,7 +142,9 @@ public CiphertextMessage encrypt(byte[] paddedMessage)
141
142
*/
142
143
public byte [ ] decrypt ( PreKeySignalMessage ciphertext )
143
144
{
144
- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
145
+ var tsk = ( decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ) ;
146
+ tsk . Wait ( ) ;
147
+ return tsk . Result ;
145
148
}
146
149
147
150
/**
@@ -165,7 +168,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext)
165
168
* @throws InvalidKeyException when the message is formatted incorrectly.
166
169
* @throws UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted.
167
170
*/
168
- public byte [ ] decrypt ( PreKeySignalMessage ciphertext , DecryptionCallback callback )
171
+ public Task < byte [ ] > decrypt ( PreKeySignalMessage ciphertext , DecryptionCallback callback )
169
172
{
170
173
lock ( SESSION_LOCK )
171
174
{
@@ -175,7 +178,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
175
178
176
179
identityKeyStore . SaveIdentity ( remoteAddress , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
177
180
178
- callback . handlePlaintext ( plaintext ) ;
181
+ callback . handlePlaintext ( plaintext , sessionRecord ) . Wait ( ) ;
179
182
180
183
sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
181
184
@@ -184,7 +187,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
184
187
preKeyStore . RemovePreKey ( unsignedPreKeyId . ForceGetValue ( ) ) ;
185
188
}
186
189
187
- return plaintext ;
190
+ return Task . FromResult ( plaintext ) ;
188
191
}
189
192
}
190
193
@@ -202,7 +205,9 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
202
205
*/
203
206
public byte [ ] decrypt ( SignalMessage ciphertext )
204
207
{
205
- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
208
+ var tsk = decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
209
+ tsk . Wait ( ) ;
210
+ return tsk . Result ;
206
211
}
207
212
208
213
/**
@@ -223,7 +228,7 @@ public byte[] decrypt(SignalMessage ciphertext)
223
228
* is no longer supported.
224
229
* @throws NoSessionException if there is no established session for this contact.
225
230
*/
226
- public byte [ ] decrypt ( SignalMessage ciphertext , DecryptionCallback callback )
231
+ public Task < byte [ ] > decrypt ( SignalMessage ciphertext , DecryptionCallback callback )
227
232
{
228
233
lock ( SESSION_LOCK )
229
234
{
@@ -241,11 +246,11 @@ public byte[] decrypt(SignalMessage ciphertext, DecryptionCallback callback)
241
246
throw new UntrustedIdentityException ( remoteAddress . Name , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
242
247
}
243
248
244
- callback . handlePlaintext ( plaintext ) ;
249
+ callback . handlePlaintext ( plaintext , sessionRecord ) . Wait ( ) ; //no async in a lock
245
250
246
251
sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
247
252
248
- return plaintext ;
253
+ return Task . FromResult ( plaintext ) ;
249
254
}
250
255
}
251
256
@@ -425,7 +430,7 @@ private byte[] getPlaintext(MessageKeys messageKeys, byte[] cipherText)
425
430
private class NullDecryptionCallback : DecryptionCallback
426
431
{
427
432
428
- public void handlePlaintext ( byte [ ] plaintext ) { }
433
+ public Task handlePlaintext ( byte [ ] plaintext , SessionRecord sessionRecord ) => Task . CompletedTask ;
429
434
}
430
435
}
431
436
}
0 commit comments