7
7
using libsignal . state ;
8
8
using libsignal . util ;
9
9
using Strilanc . Value ;
10
+ using System . Threading . Tasks ;
10
11
11
12
namespace libsignal
12
13
{
@@ -117,7 +118,9 @@ public CiphertextMessage encrypt(byte[] paddedMessage)
117
118
/// <exception cref="UntrustedIdentityException">when the <see cref="IdentityKey"/> of the sender is untrusted.</exception>
118
119
public byte [ ] decrypt ( PreKeySignalMessage ciphertext )
119
120
{
120
- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
121
+ var tsk = ( decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ) ;
122
+ tsk . Wait ( ) ;
123
+ return tsk . Result ;
121
124
}
122
125
123
126
/// <summary>
@@ -137,7 +140,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext)
137
140
///
138
141
/// <exception cref="InvalidKeyException">when the message is formatted incorrectly.</exception>
139
142
/// <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 )
141
144
{
142
145
lock ( SESSION_LOCK )
143
146
{
@@ -147,7 +150,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
147
150
148
151
identityKeyStore . SaveIdentity ( remoteAddress , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
149
152
150
- callback . handlePlaintext ( plaintext ) ;
153
+ callback . handlePlaintext ( plaintext , sessionRecord . getSessionState ( ) . getSessionVersion ( ) ) . Wait ( ) ;
151
154
152
155
sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
153
156
@@ -156,7 +159,7 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
156
159
preKeyStore . RemovePreKey ( unsignedPreKeyId . ForceGetValue ( ) ) ;
157
160
}
158
161
159
- return plaintext ;
162
+ return Task . FromResult ( plaintext ) ;
160
163
}
161
164
}
162
165
@@ -172,7 +175,9 @@ public byte[] decrypt(PreKeySignalMessage ciphertext, DecryptionCallback callbac
172
175
/// <exception cref="NoSessionException">if there is no established session for this contact.</exception>
173
176
public byte [ ] decrypt ( SignalMessage ciphertext )
174
177
{
175
- return decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
178
+ var tsk = decrypt ( ciphertext , new NullDecryptionCallback ( ) ) ;
179
+ tsk . Wait ( ) ;
180
+ return tsk . Result ;
176
181
}
177
182
178
183
/// <summary>
@@ -189,7 +194,7 @@ public byte[] decrypt(SignalMessage ciphertext)
189
194
/// <exception cref="LegacyMessageException">if the input is a message formatted by a protocol version that is
190
195
/// no longer supported.</exception>
191
196
/// <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 )
193
198
{
194
199
lock ( SESSION_LOCK )
195
200
{
@@ -207,11 +212,11 @@ public byte[] decrypt(SignalMessage ciphertext, DecryptionCallback callback)
207
212
throw new UntrustedIdentityException ( remoteAddress . Name , sessionRecord . getSessionState ( ) . getRemoteIdentityKey ( ) ) ;
208
213
}
209
214
210
- callback . handlePlaintext ( plaintext ) ;
215
+ callback . handlePlaintext ( plaintext , sessionRecord . getSessionState ( ) . getSessionVersion ( ) ) . Wait ( ) ; //no async in a lock
211
216
212
217
sessionStore . StoreSession ( remoteAddress , sessionRecord ) ;
213
218
214
- return plaintext ;
219
+ return Task . FromResult ( plaintext ) ;
215
220
}
216
221
}
217
222
@@ -391,7 +396,7 @@ private byte[] getPlaintext(MessageKeys messageKeys, byte[] cipherText)
391
396
private class NullDecryptionCallback : DecryptionCallback
392
397
{
393
398
394
- public void handlePlaintext ( byte [ ] plaintext ) { }
399
+ public Task handlePlaintext ( byte [ ] plaintext , uint sessionVersion ) => Task . CompletedTask ;
395
400
}
396
401
}
397
402
}
0 commit comments