77using libsignal . state ;
88using libsignal . util ;
99using Strilanc . Value ;
10+ using System . Threading . Tasks ;
1011
1112namespace libsignal
1213{
@@ -117,7 +118,9 @@ public CiphertextMessage encrypt(byte[] paddedMessage)
117118 /// <exception cref="UntrustedIdentityException">when the <see cref="IdentityKey"/> of the sender is untrusted.</exception>
118119 public byte [ ] decrypt ( PreKeySignalMessage ciphertext )
119120 {
120- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
121+ var tsk = ( decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ) ;
122+ tsk . Wait ( ) ;
123+ return tsk . Result ;
121124 }
122125
123126 /// <summary>
@@ -137,7 +140,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext)
137140 ///
138141 /// <exception cref="InvalidKeyException">when the message is formatted incorrectly.</exception>
139142 /// <exception cref="UntrustedIdentityException">when the <see cref="IdentityKey"/> of the sender is untrusted.</exception>
140- public byte [ ] decrypt ( PreKeySignalMessage ciphertext , DecryptionCallback callback )
143+ public Task < byte [ ] > decrypt ( PreKeySignalMessage ciphertext , DecryptionCallback callback )
141144 {
142145 lock ( SESSION_LOCK )
143146 {
@@ -147,7 +150,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
147150
148151 identityKeyStore . SaveIdentity ( remoteAddress , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
149152
150- callback . handlePlaintext ( plaintext ) ;
153+ callback . handlePlaintext ( plaintext , sessionRecord . getSessionState ( ) . getSessionVersion ( ) ) . Wait ( ) ;
151154
152155 sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
153156
@@ -156,7 +159,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
156159 preKeyStore . RemovePreKey ( unsignedPreKeyId . ForceGetValue ( ) ) ;
157160 }
158161
159- return plaintext ;
162+ return Task . FromResult ( plaintext ) ;
160163 }
161164 }
162165
@@ -172,7 +175,9 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
172175 /// <exception cref="NoSessionException">if there is no established session for this contact.</exception>
173176 public byte [ ] decrypt ( SignalMessage ciphertext )
174177 {
175- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
178+ var tsk = decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
179+ tsk . Wait ( ) ;
180+ return tsk . Result ;
176181 }
177182
178183 /// <summary>
@@ -189,7 +194,7 @@ public byte[] decrypt(SignalMessage ciphertext)
189194 /// <exception cref="LegacyMessageException">if the input is a message formatted by a protocol version that is
190195 /// no longer supported.</exception>
191196 /// <exception cref="NoSessionException">if there is no established session for this contact.</exception>
192- public byte [ ] decrypt ( SignalMessage ciphertext , DecryptionCallback callback )
197+ public Task < byte [ ] > decrypt ( SignalMessage ciphertext , DecryptionCallback callback )
193198 {
194199 lock ( SESSION_LOCK )
195200 {
@@ -207,11 +212,11 @@ public byte[] decrypt(SignalMessage ciphertext, DecryptionCallback callback)
207212 throw new UntrustedIdentityException ( remoteAddress . Name , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
208213 }
209214
210- callback . handlePlaintext ( plaintext ) ;
215+ callback . handlePlaintext ( plaintext , sessionRecord . getSessionState ( ) . getSessionVersion ( ) ) . Wait ( ) ; //no async in a lock
211216
212217 sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
213218
214- return plaintext ;
219+ return Task . FromResult ( plaintext ) ;
215220 }
216221 }
217222
@@ -391,7 +396,7 @@ private byte[] getPlaintext(MessageKeys messageKeys, byte[] cipherText)
391396 private class NullDecryptionCallback : DecryptionCallback
392397 {
393398
394- public void handlePlaintext ( byte [ ] plaintext ) { }
399+ public Task handlePlaintext ( byte [ ] plaintext , uint sessionVersion ) => Task . CompletedTask ;
395400 }
396401 }
397402}
0 commit comments