2424using Strilanc . Value ;
2525using System ;
2626using System . Collections . Generic ;
27+ using System . Threading . Tasks ;
2728
2829namespace libsignal
2930{
@@ -141,7 +142,9 @@ public CiphertextMessage encrypt(byte[] paddedMessage)
141142 */
142143 public byte [ ] decrypt ( PreKeySignalMessage ciphertext )
143144 {
144- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
145+ var tsk = ( decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ) ;
146+ tsk . Wait ( ) ;
147+ return tsk . Result ;
145148 }
146149
147150 /**
@@ -165,7 +168,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext)
165168 * @throws InvalidKeyException when the message is formatted incorrectly.
166169 * @throws UntrustedIdentityException when the {@link IdentityKey} of the sender is untrusted.
167170 */
168- public byte [ ] decrypt ( PreKeySignalMessage ciphertext , DecryptionCallback callback )
171+ public Task < byte [ ] > decrypt ( PreKeySignalMessage ciphertext , DecryptionCallback callback )
169172 {
170173 lock ( SESSION_LOCK )
171174 {
@@ -175,7 +178,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
175178
176179 identityKeyStore . SaveIdentity ( remoteAddress , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
177180
178- callback . handlePlaintext ( plaintext ) ;
181+ callback . handlePlaintext ( plaintext , sessionRecord ) . Wait ( ) ;
179182
180183 sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
181184
@@ -184,7 +187,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
184187 preKeyStore . RemovePreKey ( unsignedPreKeyId . ForceGetValue ( ) ) ;
185188 }
186189
187- return plaintext ;
190+ return Task . FromResult ( plaintext ) ;
188191 }
189192 }
190193
@@ -202,7 +205,9 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
202205 */
203206 public byte [ ] decrypt ( SignalMessage ciphertext )
204207 {
205- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
208+ var tsk = decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
209+ tsk . Wait ( ) ;
210+ return tsk . Result ;
206211 }
207212
208213 /**
@@ -223,7 +228,7 @@ public byte[] decrypt(SignalMessage ciphertext)
223228 * is no longer supported.
224229 * @throws NoSessionException if there is no established session for this contact.
225230 */
226- public byte [ ] decrypt ( SignalMessage ciphertext , DecryptionCallback callback )
231+ public Task < byte [ ] > decrypt ( SignalMessage ciphertext , DecryptionCallback callback )
227232 {
228233 lock ( SESSION_LOCK )
229234 {
@@ -241,11 +246,11 @@ public byte[] decrypt(SignalMessage ciphertext, DecryptionCallback callback)
241246 throw new UntrustedIdentityException ( remoteAddress . Name , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
242247 }
243248
244- callback . handlePlaintext ( plaintext ) ;
249+ callback . handlePlaintext ( plaintext , sessionRecord ) . Wait ( ) ; //no async in a lock
245250
246251 sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
247252
248- return plaintext ;
253+ return Task . FromResult ( plaintext ) ;
249254 }
250255 }
251256
@@ -425,7 +430,7 @@ private byte[] getPlaintext(MessageKeys messageKeys, byte[] cipherText)
425430 private class NullDecryptionCallback : DecryptionCallback
426431 {
427432
428- public void handlePlaintext ( byte [ ] plaintext ) { }
433+ public Task handlePlaintext ( byte [ ] plaintext , SessionRecord sessionRecord ) => Task . CompletedTask ;
429434 }
430435 }
431436}
0 commit comments