1 package org
.asamk
.signal
.manager
;
3 import org
.asamk
.signal
.AttachmentInvalidException
;
4 import org
.signal
.libsignal
.metadata
.certificate
.CertificateValidator
;
5 import org
.whispersystems
.libsignal
.IdentityKey
;
6 import org
.whispersystems
.libsignal
.InvalidKeyException
;
7 import org
.whispersystems
.libsignal
.ecc
.Curve
;
8 import org
.whispersystems
.libsignal
.ecc
.ECPublicKey
;
9 import org
.whispersystems
.libsignal
.fingerprint
.Fingerprint
;
10 import org
.whispersystems
.libsignal
.fingerprint
.NumericFingerprintGenerator
;
11 import org
.whispersystems
.libsignal
.util
.guava
.Optional
;
12 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
13 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentStream
;
14 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceEnvelope
;
15 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
16 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
17 import org
.whispersystems
.signalservice
.api
.util
.PhoneNumberFormatter
;
18 import org
.whispersystems
.signalservice
.api
.util
.StreamDetails
;
19 import org
.whispersystems
.signalservice
.internal
.util
.Base64
;
21 import java
.io
.DataInputStream
;
22 import java
.io
.DataOutputStream
;
24 import java
.io
.FileInputStream
;
25 import java
.io
.FileNotFoundException
;
26 import java
.io
.FileOutputStream
;
27 import java
.io
.IOException
;
28 import java
.io
.InputStream
;
29 import java
.io
.OutputStream
;
30 import java
.io
.UnsupportedEncodingException
;
32 import java
.net
.URLDecoder
;
33 import java
.net
.URLEncoder
;
34 import java
.nio
.file
.Files
;
35 import java
.util
.ArrayList
;
36 import java
.util
.Collection
;
37 import java
.util
.HashMap
;
38 import java
.util
.HashSet
;
39 import java
.util
.List
;
43 import static org
.whispersystems
.signalservice
.internal
.util
.Util
.isEmpty
;
47 static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
48 List
<SignalServiceAttachment
> SignalServiceAttachments
= null;
49 if (attachments
!= null) {
50 SignalServiceAttachments
= new ArrayList
<>(attachments
.size());
51 for (String attachment
: attachments
) {
53 SignalServiceAttachments
.add(createAttachment(new File(attachment
)));
54 } catch (IOException e
) {
55 throw new AttachmentInvalidException(attachment
, e
);
59 return SignalServiceAttachments
;
62 static SignalServiceAttachmentStream
createAttachment(File attachmentFile
) throws IOException
{
63 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
64 final long attachmentSize
= attachmentFile
.length();
65 String mime
= Files
.probeContentType(attachmentFile
.toPath());
67 mime
= "application/octet-stream";
69 // TODO mabybe add a parameter to set the voiceNote, preview, width, height and caption option
70 Optional
<byte[]> preview
= Optional
.absent();
71 Optional
<String
> caption
= Optional
.absent();
72 return new SignalServiceAttachmentStream(attachmentStream
, mime
, attachmentSize
, Optional
.of(attachmentFile
.getName()), false, preview
, 0, 0, caption
, null);
75 static StreamDetails
createStreamDetailsFromFile(File file
) throws IOException
{
76 InputStream stream
= new FileInputStream(file
);
77 final long size
= file
.length();
78 String mime
= Files
.probeContentType(file
.toPath());
80 mime
= "application/octet-stream";
82 return new StreamDetails(stream
, mime
, size
);
85 static CertificateValidator
getCertificateValidator() {
87 ECPublicKey unidentifiedSenderTrustRoot
= Curve
.decodePoint(Base64
.decode(BaseConfig
.UNIDENTIFIED_SENDER_TRUST_ROOT
), 0);
88 return new CertificateValidator(unidentifiedSenderTrustRoot
);
89 } catch (InvalidKeyException
| IOException e
) {
90 throw new AssertionError(e
);
94 private static Map
<String
, String
> getQueryMap(String query
) {
95 String
[] params
= query
.split("&");
96 Map
<String
, String
> map
= new HashMap
<>();
97 for (String param
: params
) {
99 final String
[] paramParts
= param
.split("=");
101 name
= URLDecoder
.decode(paramParts
[0], "utf-8");
102 } catch (UnsupportedEncodingException e
) {
107 value
= URLDecoder
.decode(paramParts
[1], "utf-8");
108 } catch (UnsupportedEncodingException e
) {
111 map
.put(name
, value
);
116 static String
createDeviceLinkUri(DeviceLinkInfo info
) {
118 return "tsdevice:/?uuid=" + URLEncoder
.encode(info
.deviceIdentifier
, "utf-8") + "&pub_key=" + URLEncoder
.encode(Base64
.encodeBytesWithoutPadding(info
.deviceKey
.serialize()), "utf-8");
119 } catch (UnsupportedEncodingException e
) {
125 static DeviceLinkInfo
parseDeviceLinkUri(URI linkUri
) throws IOException
, InvalidKeyException
{
126 Map
<String
, String
> query
= getQueryMap(linkUri
.getRawQuery());
127 String deviceIdentifier
= query
.get("uuid");
128 String publicKeyEncoded
= query
.get("pub_key");
130 if (isEmpty(deviceIdentifier
) || isEmpty(publicKeyEncoded
)) {
131 throw new RuntimeException("Invalid device link uri");
134 ECPublicKey deviceKey
= Curve
.decodePoint(Base64
.decode(publicKeyEncoded
), 0);
136 return new DeviceLinkInfo(deviceIdentifier
, deviceKey
);
139 static Set
<SignalServiceAddress
> getSignalServiceAddresses(Collection
<String
> recipients
, String localNumber
) {
140 Set
<SignalServiceAddress
> recipientsTS
= new HashSet
<>(recipients
.size());
141 for (String recipient
: recipients
) {
143 recipientsTS
.add(getPushAddress(recipient
, localNumber
));
144 } catch (InvalidNumberException e
) {
145 System
.err
.println("Failed to add recipient \"" + recipient
+ "\": " + e
.getMessage());
146 System
.err
.println("Aborting sending.");
153 static String
canonicalizeNumber(String number
, String localNumber
) throws InvalidNumberException
{
154 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
157 private static SignalServiceAddress
getPushAddress(String number
, String localNumber
) throws InvalidNumberException
{
158 String e164number
= canonicalizeNumber(number
, localNumber
);
159 return new SignalServiceAddress(e164number
);
162 static SignalServiceEnvelope
loadEnvelope(File file
) throws IOException
{
163 try (FileInputStream f
= new FileInputStream(file
)) {
164 DataInputStream
in = new DataInputStream(f
);
165 int version
= in.readInt();
169 int type
= in.readInt();
170 String source
= in.readUTF();
171 int sourceDevice
= in.readInt();
173 // read legacy relay field
176 long timestamp
= in.readLong();
177 byte[] content
= null;
178 int contentLen
= in.readInt();
179 if (contentLen
> 0) {
180 content
= new byte[contentLen
];
181 in.readFully(content
);
183 byte[] legacyMessage
= null;
184 int legacyMessageLen
= in.readInt();
185 if (legacyMessageLen
> 0) {
186 legacyMessage
= new byte[legacyMessageLen
];
187 in.readFully(legacyMessage
);
189 long serverTimestamp
= 0;
192 serverTimestamp
= in.readLong();
194 if ("".equals(uuid
)) {
198 return new SignalServiceEnvelope(type
, source
, sourceDevice
, timestamp
, legacyMessage
, content
, serverTimestamp
, uuid
);
202 static void storeEnvelope(SignalServiceEnvelope envelope
, File file
) throws IOException
{
203 try (FileOutputStream f
= new FileOutputStream(file
)) {
204 try (DataOutputStream out
= new DataOutputStream(f
)) {
205 out
.writeInt(2); // version
206 out
.writeInt(envelope
.getType());
207 out
.writeUTF(envelope
.getSource());
208 out
.writeInt(envelope
.getSourceDevice());
209 out
.writeLong(envelope
.getTimestamp());
210 if (envelope
.hasContent()) {
211 out
.writeInt(envelope
.getContent().length
);
212 out
.write(envelope
.getContent());
216 if (envelope
.hasLegacyMessage()) {
217 out
.writeInt(envelope
.getLegacyMessage().length
);
218 out
.write(envelope
.getLegacyMessage());
222 out
.writeLong(envelope
.getServerTimestamp());
223 String uuid
= envelope
.getUuid();
224 out
.writeUTF(uuid
== null ?
"" : uuid
);
229 static File
retrieveAttachment(SignalServiceAttachmentStream stream
, File outputFile
) throws IOException
{
230 InputStream input
= stream
.getInputStream();
232 try (OutputStream output
= new FileOutputStream(outputFile
)) {
233 byte[] buffer
= new byte[4096];
236 while ((read
= input
.read(buffer
)) != -1) {
237 output
.write(buffer
, 0, read
);
239 } catch (FileNotFoundException e
) {
246 static String
computeSafetyNumber(String ownUsername
, IdentityKey ownIdentityKey
, String theirUsername
, IdentityKey theirIdentityKey
) {
247 Fingerprint fingerprint
= new NumericFingerprintGenerator(5200).createFor(ownUsername
, ownIdentityKey
, theirUsername
, theirIdentityKey
);
248 return fingerprint
.getDisplayableFingerprint().getDisplayText();
251 static class DeviceLinkInfo
{
253 final String deviceIdentifier
;
254 final ECPublicKey deviceKey
;
256 DeviceLinkInfo(final String deviceIdentifier
, final ECPublicKey deviceKey
) {
257 this.deviceIdentifier
= deviceIdentifier
;
258 this.deviceKey
= deviceKey
;