Skip to content

Commit 5f21f6c

Browse files
committed
Squashed commit of the following:
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 session version (when available) to the callback (as not yet committed for prekey), needed for protocol version check code that already exists.
1 parent c352666 commit 5f21f6c

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

libsignal-protocol-dotnet-tests/SessionBuilderTest.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/**
1+
/**
22
* Copyright (C) 2016 langboost
33
*
44
* This program is free software: you can redistribute it and/or modify
@@ -25,6 +25,7 @@
2525
using System;
2626
using System.Collections.Generic;
2727
using System.Text;
28+
using System.Threading.Tasks;
2829

2930
namespace libsignal_test
3031
{
@@ -46,10 +47,11 @@ public BobDecryptionCallback(SignalProtocolStore bobStore, String originalMessag
4647
this.originalMessage = originalMessage;
4748
}
4849

49-
public void handlePlaintext(byte[] plaintext)
50+
public Task handlePlaintext(byte[] plaintext, uint sessionVersion)
5051
{
5152
Assert.AreEqual(originalMessage, Encoding.UTF8.GetString(plaintext));
5253
Assert.IsFalse(bobStore.ContainsSession(ALICE_ADDRESS));
54+
return Task.CompletedTask;
5355
}
5456
}
5557

@@ -87,7 +89,7 @@ public void testBasicPreKeyV3()
8789
bobStore.StoreSignedPreKey(22, new SignedPreKeyRecord(22, DateUtil.currentTimeMillis(), bobSignedPreKeyPair, bobSignedPreKeySignature));
8890

8991
SessionCipher bobSessionCipher = new SessionCipher(bobStore, ALICE_ADDRESS);
90-
byte[] plaintext = bobSessionCipher.decrypt(incomingMessage, new BobDecryptionCallback(bobStore, originalMessage));
92+
byte[] plaintext = bobSessionCipher.decrypt(incomingMessage, new BobDecryptionCallback(bobStore, originalMessage)).Result;
9193

9294
Assert.IsTrue(bobStore.ContainsSession(ALICE_ADDRESS));
9395
Assert.AreEqual((uint)3, bobStore.LoadSession(ALICE_ADDRESS).getSessionState().getSessionVersion());
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, uint sessionVersion);
2324
}
2425
}

libsignal-protocol-dotnet/SessionCipher.cs

+14-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using libsignal.state;
88
using libsignal.util;
99
using Strilanc.Value;
10+
using System.Threading.Tasks;
1011

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

libsignal-protocol-dotnet/groups/GroupCipher.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
using libsignal.groups.ratchet;
2020
using libsignal.groups.state;
2121
using libsignal.protocol;
22+
using libsignal.state;
2223
using libsignal.util;
24+
using System.Threading.Tasks;
2325

2426
namespace libsignal.groups
2527
{
@@ -127,7 +129,7 @@ public byte[] decrypt(byte[] senderKeyMessageBytes, DecryptionCallback callback)
127129

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

130-
callback.handlePlaintext(plaintext);
132+
callback.handlePlaintext(plaintext, 931).Wait();//931 random as we don't have the real version
131133

132134
senderKeyStore.storeSenderKey(senderKeyId, record);
133135

@@ -210,7 +212,8 @@ private byte[] getCipherText(byte[] iv, byte[] key, byte[] plaintext)
210212

211213
private class NullDecryptionCallback : DecryptionCallback
212214
{
213-
public void handlePlaintext(byte[] plaintext) { }
214-
}
215+
public Task handlePlaintext(byte[] plaintext, uint sessionVersion) => Task.CompletedTask;
216+
217+
}
215218
}
216219
}

0 commit comments

Comments
 (0)