Skip to content

Commit dcfee16

Browse files
committed
Implement DeviceGroupsInputStream (and fix its dependencies)
1 parent def6432 commit dcfee16

16 files changed

+654
-581
lines changed

libsignal-service-dotnet/SignalServiceMessageReceiver.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
using libsignalservice.websocket;
1111
using System;
1212
using System.Collections.Generic;
13+
using System.Diagnostics;
1314
using System.IO;
1415
using System.Security.Cryptography;
16+
using System.Text;
1517
using System.Threading;
1618
using static libsignalservice.messages.SignalServiceAttachment;
1719
using static libsignalservice.SignalServiceMessagePipe;
@@ -88,6 +90,7 @@ public Stream RetrieveProfileAvatar(string path, FileStream destination, byte[]
8890
public Stream RetrieveAttachment(SignalServiceAttachmentPointer pointer, Stream tmpCipherDestination, int maxSizeBytes, IProgressListener listener)
8991
{
9092
Socket.RetrieveAttachment(pointer.Relay, pointer.Id, tmpCipherDestination, maxSizeBytes);
93+
tmpCipherDestination.Position = 0;
9194
return AttachmentCipherInputStream.CreateFor(tmpCipherDestination, pointer.Size != null ? pointer.Size.Value : 0, pointer.Key, pointer.Digest);
9295
}
9396

libsignal-service-dotnet/SignalServiceMessageSender.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ public void SendMessage(SignalServiceSyncMessage message)
180180
SendMessage(message.Verified);
181181
return;
182182
}
183+
else if (message.Request != null)
184+
{
185+
content = CreateRequestContent(message.Request);
186+
}
183187
else
184188
{
185189
throw new Exception("Unsupported sync message!");
@@ -454,6 +458,16 @@ private byte[] CreateMultiDeviceReadContent(List<ReadMessage> readMessages)
454458
return content.ToByteArray();
455459
}
456460

461+
private byte[] CreateRequestContent(RequestMessage request)
462+
{
463+
Content content = new Content { };
464+
SyncMessage syncMessage = CreateSyncMessage();
465+
466+
syncMessage.Request = request.Request;
467+
content.SyncMessage = syncMessage;
468+
return content.ToByteArray();
469+
}
470+
457471
private byte[] CreateMultiDeviceBlockedContent(BlockedListMessage blocked)
458472
{
459473
Content content = new Content { };
@@ -645,7 +659,7 @@ private AttachmentPointer CreateAttachmentPointer(SignalServiceAttachmentStream
645659
byte[] attachmentKey = Util.GetSecretBytes(64);
646660
long paddedLength = PaddingInputStream.GetPaddedSize(attachment.Length);
647661
long ciphertextLength = AttachmentCipherInputStream.GetCiphertextLength(paddedLength);
648-
PushAttachmentData attachmentData = new PushAttachmentData(attachment.getContentType(),
662+
PushAttachmentData attachmentData = new PushAttachmentData(attachment.ContentType,
649663
new PaddingInputStream(attachment.InputStream, attachment.Length),
650664
ciphertextLength,
651665
new AttachmentCipherOutputStreamFactory(attachmentKey),
@@ -655,7 +669,7 @@ private AttachmentPointer CreateAttachmentPointer(SignalServiceAttachmentStream
655669

656670
var attachmentPointer = new AttachmentPointer
657671
{
658-
ContentType = attachment.getContentType(),
672+
ContentType = attachment.ContentType,
659673
Id = id,
660674
Key = ByteString.CopyFrom(attachmentKey),
661675
Digest = ByteString.CopyFrom(digest),
@@ -694,7 +708,7 @@ private AttachmentPointer CreateAttachmentPointerFromPointer(SignalServiceAttach
694708
{
695709
var attachmentPointer = new AttachmentPointer()
696710
{
697-
ContentType = attachment.getContentType(),
711+
ContentType = attachment.ContentType,
698712
Id = attachment.Id,
699713
Key = ByteString.CopyFrom(attachment.Key),
700714
Digest = ByteString.CopyFrom(attachment.Digest),

libsignal-service-dotnet/crypto/AttachmentCipherInputStream.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using libsignalservice.util;
33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
78
using System.Security.Cryptography;
@@ -21,7 +22,7 @@ public class AttachmentCipherInputStream : Stream
2122
private readonly CryptoStream Cipher;
2223
private readonly ICryptoTransform Decryptor;
2324
private readonly MemoryStream TmpStream = new MemoryStream();
24-
private long TotalDataSize;
25+
private readonly long TotalDataSize;
2526
private long TotalRead = 0;
2627

2728
public override bool CanRead => true;
@@ -33,7 +34,7 @@ public class AttachmentCipherInputStream : Stream
3334
public static Stream CreateFor(Stream inputStream, long plaintextLength, byte[] combinedKeyMaterial, byte[] digest)
3435
{
3536
long fileSize = inputStream.Length;
36-
byte[][] keyParts = Util.Split(combinedKeyMaterial, 32, 32);
37+
byte[][] keyParts = Util.Split(combinedKeyMaterial, CIPHER_KEY_SIZE, MAC_KEY_SIZE);
3738
IncrementalHash mac = IncrementalHash.CreateHMAC(HashAlgorithmName.SHA256, keyParts[1]);
3839
VerifyMac(inputStream, mac, digest);
3940
inputStream.Seek(0, SeekOrigin.Begin);
@@ -79,6 +80,7 @@ public override int Read(byte[] buffer, int offset, int count)
7980
{
8081
int read = Cipher.Read(buffer, offset, (int)Math.Min(count, TotalDataSize - TotalRead));
8182
TotalRead += read;
83+
8284
return read;
8385
}
8486
return 0;

libsignal-service-dotnet/crypto/SignalServiceCipher.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Google.Protobuf;
22
using libsignal;
3+
using libsignal.messages.multidevice;
34
using libsignal.protocol;
45
using libsignal.state;
56
using libsignal_service_dotnet.messages.calls;
@@ -148,7 +149,7 @@ private SignalServiceDataMessage CreateSignalServiceMessage(SignalServiceEnvelop
148149

149150
foreach (AttachmentPointer pointer in content.Attachments)
150151
{
151-
attachments.Add(CreateAttachmentPointer(envelope, pointer));
152+
attachments.Add(CreateAttachmentPointer(envelope.GetRelay(), pointer));
152153
}
153154

154155
if (content.TimestampOneofCase == DataMessage.TimestampOneofOneofCase.Timestamp && (long) content.Timestamp != envelope.GetTimestamp())
@@ -199,6 +200,18 @@ private SignalServiceSyncMessage CreateSynchronizeMessage(SignalServiceEnvelope
199200
return SignalServiceSyncMessage.ForRead(readMessages);
200201
}
201202

203+
if (content.ContactsOneofCase == SyncMessage.ContactsOneofOneofCase.Contacts)
204+
{
205+
AttachmentPointer pointer = content.Contacts.Blob;
206+
return SignalServiceSyncMessage.ForContacts(new ContactsMessage(CreateAttachmentPointer(envelope.GetRelay(), pointer), content.Contacts.Complete));
207+
}
208+
209+
if (content.GroupsOneofCase == SyncMessage.GroupsOneofOneofCase.Groups)
210+
{
211+
AttachmentPointer pointer = content.Groups.Blob;
212+
return SignalServiceSyncMessage.ForGroups(CreateAttachmentPointer(envelope.GetRelay(), pointer));
213+
}
214+
202215
if (content.VerifiedOneofCase == SyncMessage.VerifiedOneofOneofCase.Verified)
203216
{
204217
try
@@ -348,7 +361,7 @@ private SignalServiceDataMessage.SignalServiceQuote CreateQuote(SignalServiceEnv
348361
{
349362
attachments.Add(new SignalServiceQuotedAttachment(pointer.ContentType,
350363
pointer.FileName,
351-
pointer.ThumbnailOneofCase == Types.Quote.Types.QuotedAttachment.ThumbnailOneofOneofCase.Thumbnail ? CreateAttachmentPointer(envelope, pointer.Thumbnail) : null));
364+
pointer.ThumbnailOneofCase == Types.Quote.Types.QuotedAttachment.ThumbnailOneofOneofCase.Thumbnail ? CreateAttachmentPointer(envelope.GetRelay(), pointer.Thumbnail) : null));
352365
}
353366

354367
return new SignalServiceDataMessage.SignalServiceQuote((long) content.Quote.Id,
@@ -357,7 +370,7 @@ private SignalServiceDataMessage.SignalServiceQuote CreateQuote(SignalServiceEnv
357370
attachments);
358371
}
359372

360-
private SignalServiceAttachmentPointer CreateAttachmentPointer(SignalServiceEnvelope envelope, AttachmentPointer pointer)
373+
private SignalServiceAttachmentPointer CreateAttachmentPointer(string relay, AttachmentPointer pointer)
361374
{
362375
uint? size = null;
363376
if (pointer.SizeOneofCase == AttachmentPointer.SizeOneofOneofCase.Size)
@@ -367,7 +380,7 @@ private SignalServiceAttachmentPointer CreateAttachmentPointer(SignalServiceEnve
367380
return new SignalServiceAttachmentPointer(pointer.Id,
368381
pointer.ContentType,
369382
pointer.Key.ToByteArray(),
370-
envelope.GetRelay(),
383+
relay,
371384
size,
372385
pointer.ThumbnailOneofCase == AttachmentPointer.ThumbnailOneofOneofCase.Thumbnail ? pointer.Thumbnail.ToByteArray() : null,
373386
(int) pointer.Width,

libsignal-service-dotnet/libsignal-service-dotnet.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
<PackageReference Include="libphonenumber-csharp" Version="8.8.6" />
3232
<PackageReference Include="libsignal-protocol-dotnet" Version="2.6.2" />
3333
<PackageReference Include="Microsoft.Extensions.Logging" Version="1.1.2" />
34-
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
34+
<PackageReference Include="Newtonsoft.Json" Version="11.0.1" />
3535
<PackageReference Include="Portable.BouncyCastle" Version="1.8.2" />
3636
<PackageReference Include="System.Net.WebSockets.Client" Version="4.3.2" />
3737
<PackageReference Include="System.ValueTuple" Version="4.4.0" />

libsignal-service-dotnet/messages/SignalServiceAttachment.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ internal SignalServiceAttachment(String contentType)
1313
this.ContentType = contentType;
1414
}
1515

16-
public String getContentType()
17-
{
18-
return ContentType;
19-
}
20-
2116
public abstract bool IsStream();
2217

2318
public abstract bool IsPointer();

0 commit comments

Comments
 (0)