Skip to content

Commit 87c1b1f

Browse files
committed
Improved callback support added Task support and passing SessionRecord
This modifies the existing callback support to work with Task based callbacks. It also passes the SessionRecord (when available) to the callback (as not yet committed for prekey), needed for protocol version check code that already exists).
1 parent 8cbd533 commit 87c1b1f

File tree

3 files changed

+39
-30
lines changed

3 files changed

+39
-30
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
/**
2-
* Copyright (C) 2016 smndtrl, langboost
3-
*
4-
* This program is free software: you can redistribute it and/or modify
5-
* it under the terms of the GNU General Public License as published by
6-
* the Free Software Foundation, either version 3 of the License, or
7-
* (at your option) any later version.
8-
*
9-
* This program is distributed in the hope that it will be useful,
10-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12-
* GNU General Public License for more details.
13-
*
14-
* You should have received a copy of the GNU General Public License
15-
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
*/
17-
1+
using libsignal.state;
2+
using System.Threading.Tasks;
3+
/**
4+
* Copyright (C) 2016 smndtrl, langboost
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
1819
namespace libsignal
1920
{
2021
public interface DecryptionCallback
2122
{
22-
void handlePlaintext(byte[] plaintext);
23+
Task handlePlaintext(byte[] plaintext, SessionRecord sessionRecord);
2324
}
2425
}

libsignal-protocol-dotnet/SessionCipher.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
using Strilanc.Value;
2525
using System;
2626
using System.Collections.Generic;
27+
using System.Threading.Tasks;
2728

2829
namespace 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
}

libsignal-protocol-dotnet/groups/GroupCipher.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
using libsignal.groups.ratchet;
1919
using libsignal.groups.state;
2020
using libsignal.protocol;
21+
using libsignal.state;
2122
using libsignal.util;
2223
using System;
24+
using System.Threading.Tasks;
2325

2426
namespace libsignal.groups
2527
{
@@ -134,7 +136,7 @@ public byte[] decrypt(byte[] senderKeyMessageBytes, DecryptionCallback callback)
134136

135137
byte[] plaintext = getPlainText(senderKey.getIv(), senderKey.getCipherKey(), senderKeyMessage.getCipherText());
136138

137-
callback.handlePlaintext(plaintext);
139+
callback.handlePlaintext(plaintext, null).Wait();
138140

139141
senderKeyStore.storeSenderKey(senderKeyId, record);
140142

@@ -217,8 +219,9 @@ private byte[] getCipherText(byte[] iv, byte[] key, byte[] plaintext)
217219

218220
private class NullDecryptionCallback : DecryptionCallback
219221
{
220-
public void handlePlaintext(byte[] plaintext) { }
221-
}
222+
public Task handlePlaintext(byte[] plaintext, SessionRecord sessionRecord) => Task.CompletedTask;
223+
224+
}
222225

223226
}
224227
}

0 commit comments

Comments
 (0)