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
.internal
.util
.Base64
;
22 import java
.net
.URLDecoder
;
23 import java
.net
.URLEncoder
;
24 import java
.nio
.file
.Files
;
27 import static org
.whispersystems
.signalservice
.internal
.util
.Util
.isEmpty
;
31 static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
32 List
<SignalServiceAttachment
> SignalServiceAttachments
= null;
33 if (attachments
!= null) {
34 SignalServiceAttachments
= new ArrayList
<>(attachments
.size());
35 for (String attachment
: attachments
) {
37 SignalServiceAttachments
.add(createAttachment(new File(attachment
)));
38 } catch (IOException e
) {
39 throw new AttachmentInvalidException(attachment
, e
);
43 return SignalServiceAttachments
;
46 static SignalServiceAttachmentStream
createAttachment(File attachmentFile
) throws IOException
{
47 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
48 final long attachmentSize
= attachmentFile
.length();
49 String mime
= Files
.probeContentType(attachmentFile
.toPath());
51 mime
= "application/octet-stream";
53 // TODO mabybe add a parameter to set the voiceNote, preview, width, height and caption option
54 Optional
<byte[]> preview
= Optional
.absent();
55 Optional
<String
> caption
= Optional
.absent();
56 return new SignalServiceAttachmentStream(attachmentStream
, mime
, attachmentSize
, Optional
.of(attachmentFile
.getName()), false, preview
, 0, 0, caption
, null);
59 static CertificateValidator
getCertificateValidator() {
61 ECPublicKey unidentifiedSenderTrustRoot
= Curve
.decodePoint(Base64
.decode(BaseConfig
.UNIDENTIFIED_SENDER_TRUST_ROOT
), 0);
62 return new CertificateValidator(unidentifiedSenderTrustRoot
);
63 } catch (InvalidKeyException
| IOException e
) {
64 throw new AssertionError(e
);
68 private static Map
<String
, String
> getQueryMap(String query
) {
69 String
[] params
= query
.split("&");
70 Map
<String
, String
> map
= new HashMap
<>();
71 for (String param
: params
) {
73 final String
[] paramParts
= param
.split("=");
75 name
= URLDecoder
.decode(paramParts
[0], "utf-8");
76 } catch (UnsupportedEncodingException e
) {
81 value
= URLDecoder
.decode(paramParts
[1], "utf-8");
82 } catch (UnsupportedEncodingException e
) {
90 static String
createDeviceLinkUri(DeviceLinkInfo info
) {
92 return "tsdevice:/?uuid=" + URLEncoder
.encode(info
.deviceIdentifier
, "utf-8") + "&pub_key=" + URLEncoder
.encode(Base64
.encodeBytesWithoutPadding(info
.deviceKey
.serialize()), "utf-8");
93 } catch (UnsupportedEncodingException e
) {
99 static DeviceLinkInfo
parseDeviceLinkUri(URI linkUri
) throws IOException
, InvalidKeyException
{
100 Map
<String
, String
> query
= getQueryMap(linkUri
.getRawQuery());
101 String deviceIdentifier
= query
.get("uuid");
102 String publicKeyEncoded
= query
.get("pub_key");
104 if (isEmpty(deviceIdentifier
) || isEmpty(publicKeyEncoded
)) {
105 throw new RuntimeException("Invalid device link uri");
108 ECPublicKey deviceKey
= Curve
.decodePoint(Base64
.decode(publicKeyEncoded
), 0);
110 return new DeviceLinkInfo(deviceIdentifier
, deviceKey
);
113 static Set
<SignalServiceAddress
> getSignalServiceAddresses(Collection
<String
> recipients
, String localNumber
) {
114 Set
<SignalServiceAddress
> recipientsTS
= new HashSet
<>(recipients
.size());
115 for (String recipient
: recipients
) {
117 recipientsTS
.add(getPushAddress(recipient
, localNumber
));
118 } catch (InvalidNumberException e
) {
119 System
.err
.println("Failed to add recipient \"" + recipient
+ "\": " + e
.getMessage());
120 System
.err
.println("Aborting sending.");
127 static String
canonicalizeNumber(String number
, String localNumber
) throws InvalidNumberException
{
128 return PhoneNumberFormatter
.formatNumber(number
, localNumber
);
131 private static SignalServiceAddress
getPushAddress(String number
, String localNumber
) throws InvalidNumberException
{
132 String e164number
= canonicalizeNumber(number
, localNumber
);
133 return new SignalServiceAddress(e164number
);
136 static SignalServiceEnvelope
loadEnvelope(File file
) throws IOException
{
137 try (FileInputStream f
= new FileInputStream(file
)) {
138 DataInputStream
in = new DataInputStream(f
);
139 int version
= in.readInt();
143 int type
= in.readInt();
144 String source
= in.readUTF();
145 int sourceDevice
= in.readInt();
147 // read legacy relay field
150 long timestamp
= in.readLong();
151 byte[] content
= null;
152 int contentLen
= in.readInt();
153 if (contentLen
> 0) {
154 content
= new byte[contentLen
];
155 in.readFully(content
);
157 byte[] legacyMessage
= null;
158 int legacyMessageLen
= in.readInt();
159 if (legacyMessageLen
> 0) {
160 legacyMessage
= new byte[legacyMessageLen
];
161 in.readFully(legacyMessage
);
163 long serverTimestamp
= 0;
166 serverTimestamp
= in.readLong();
168 if ("".equals(uuid
)) {
172 return new SignalServiceEnvelope(type
, source
, sourceDevice
, timestamp
, legacyMessage
, content
, serverTimestamp
, uuid
);
176 static void storeEnvelope(SignalServiceEnvelope envelope
, File file
) throws IOException
{
177 try (FileOutputStream f
= new FileOutputStream(file
)) {
178 try (DataOutputStream out
= new DataOutputStream(f
)) {
179 out
.writeInt(2); // version
180 out
.writeInt(envelope
.getType());
181 out
.writeUTF(envelope
.getSource());
182 out
.writeInt(envelope
.getSourceDevice());
183 out
.writeLong(envelope
.getTimestamp());
184 if (envelope
.hasContent()) {
185 out
.writeInt(envelope
.getContent().length
);
186 out
.write(envelope
.getContent());
190 if (envelope
.hasLegacyMessage()) {
191 out
.writeInt(envelope
.getLegacyMessage().length
);
192 out
.write(envelope
.getLegacyMessage());
196 out
.writeLong(envelope
.getServerTimestamp());
197 String uuid
= envelope
.getUuid();
198 out
.writeUTF(uuid
== null ?
"" : uuid
);
203 static File
retrieveAttachment(SignalServiceAttachmentStream stream
, File outputFile
) throws IOException
{
204 InputStream input
= stream
.getInputStream();
206 try (OutputStream output
= new FileOutputStream(outputFile
)) {
207 byte[] buffer
= new byte[4096];
210 while ((read
= input
.read(buffer
)) != -1) {
211 output
.write(buffer
, 0, read
);
213 } catch (FileNotFoundException e
) {
220 static String
computeSafetyNumber(String ownUsername
, IdentityKey ownIdentityKey
, String theirUsername
, IdentityKey theirIdentityKey
) {
221 Fingerprint fingerprint
= new NumericFingerprintGenerator(5200).createFor(ownUsername
, ownIdentityKey
, theirUsername
, theirIdentityKey
);
222 return fingerprint
.getDisplayableFingerprint().getDisplayText();
225 static class DeviceLinkInfo
{
227 final String deviceIdentifier
;
228 final ECPublicKey deviceKey
;
230 DeviceLinkInfo(final String deviceIdentifier
, final ECPublicKey deviceKey
) {
231 this.deviceIdentifier
= deviceIdentifier
;
232 this.deviceKey
= deviceKey
;