@ -55,7 +55,7 @@ public class PushTransport extends BaseTransport {
String recipientCanonicalNumber = PhoneNumberFormatter . formatNumber ( recipient . getNumber ( ) ,
String recipientCanonicalNumber = PhoneNumberFormatter . formatNumber ( recipient . getNumber ( ) ,
localNumber ) ;
localNumber ) ;
Pair < Integer , String > typeAndCiphertext = getEncryptedMessage ( socket , recipient , recipientCanonicalNumber , plaintext ) ;
Pair < Integer , byte [ ] > typeAndCiphertext = getEncryptedMessage ( socket , recipient , recipientCanonicalNumber , plaintext ) ;
socket . sendMessage ( recipientCanonicalNumber , typeAndCiphertext . second , typeAndCiphertext . first ) ;
socket . sendMessage ( recipientCanonicalNumber , typeAndCiphertext . second , typeAndCiphertext . first ) ;
@ -71,11 +71,19 @@ public class PushTransport extends BaseTransport {
String localNumber = TextSecurePreferences . getLocalNumber ( context ) ;
String localNumber = TextSecurePreferences . getLocalNumber ( context ) ;
String password = TextSecurePreferences . getPushServerPassword ( context ) ;
String password = TextSecurePreferences . getPushServerPassword ( context ) ;
PushServiceSocket socket = new PushServiceSocket ( context , localNumber , password ) ;
PushServiceSocket socket = new PushServiceSocket ( context , localNumber , password ) ;
String messageText = PartParser . getMessageText ( message . getBody ( ) ) ;
byte [ ] messageText = PartParser . getMessageText ( message . getBody ( ) ) . getBytes ( ) ;
List < PushAttachmentData > attachments = getAttachmentsFromBody ( message . getBody ( ) ) ;
List < PushAttachmentData > attachments = getAttachmentsFromBody ( message . getBody ( ) ) ;
if ( attachments . isEmpty ( ) ) socket . sendMessage ( destinations , messageText , OutgoingPushMessage . TYPE_MESSAGE_PLAINTEXT ) ;
List < byte [ ] > messagesList = new LinkedList < byte [ ] > ( ) ;
else socket . sendMessage ( destinations , messageText , attachments , OutgoingPushMessage . TYPE_MESSAGE_PLAINTEXT ) ;
List < List < PushAttachmentData > > attachmentsList = new LinkedList < List < PushAttachmentData > > ( ) ;
for ( String recipient : destinations ) {
messagesList . add ( messageText ) ;
attachmentsList . add ( attachments ) ;
}
socket . sendMessage ( destinations , messagesList , attachmentsList ,
OutgoingPushMessage . TYPE_MESSAGE_PLAINTEXT ) ;
} catch ( RateLimitException e ) {
} catch ( RateLimitException e ) {
Log . w ( "PushTransport" , e ) ;
Log . w ( "PushTransport" , e ) ;
throw new IOException ( "Rate limit exceeded." ) ;
throw new IOException ( "Rate limit exceeded." ) ;
@ -99,26 +107,26 @@ public class PushTransport extends BaseTransport {
return attachments ;
return attachments ;
}
}
private Pair < Integer , String > getEncryptedMessage ( PushServiceSocket socket , Recipient recipient ,
private Pair < Integer , byte [ ] > getEncryptedMessage ( PushServiceSocket socket , Recipient recipient ,
String canonicalRecipientNumber , String plaintext )
String canonicalRecipientNumber , String plaintext )
throws IOException
throws IOException
{
{
if ( KeyUtil . isNonPrekeySessionFor ( context , masterSecret , recipient ) ) {
if ( KeyUtil . isNonPrekeySessionFor ( context , masterSecret , recipient ) ) {
Log . w ( "PushTransport" , "Sending standard ciphertext message..." ) ;
Log . w ( "PushTransport" , "Sending standard ciphertext message..." ) ;
String ciphertext = getEncryptedMessageForExistingSession ( recipient , plaintext ) ;
byte [ ] ciphertext = getEncryptedMessageForExistingSession ( recipient , plaintext ) ;
return new Pair < Integer , String > ( OutgoingPushMessage . TYPE_MESSAGE_CIPHERTEXT , ciphertext ) ;
return new Pair < Integer , byte [ ] > ( OutgoingPushMessage . TYPE_MESSAGE_CIPHERTEXT , ciphertext ) ;
} else if ( KeyUtil . isSessionFor ( context , recipient ) ) {
} else if ( KeyUtil . isSessionFor ( context , recipient ) ) {
Log . w ( "PushTransport" , "Sending prekeybundle ciphertext message for existing session..." ) ;
Log . w ( "PushTransport" , "Sending prekeybundle ciphertext message for existing session..." ) ;
String ciphertext = getEncryptedPrekeyBundleMessageForExistingSession ( recipient , plaintext ) ;
byte [ ] ciphertext = getEncryptedPrekeyBundleMessageForExistingSession ( recipient , plaintext ) ;
return new Pair < Integer , String > ( OutgoingPushMessage . TYPE_MESSAGE_PREKEY_BUNDLE , ciphertext ) ;
return new Pair < Integer , byte [ ] > ( OutgoingPushMessage . TYPE_MESSAGE_PREKEY_BUNDLE , ciphertext ) ;
} else {
} else {
Log . w ( "PushTransport" , "Sending prekeybundle ciphertext message for new session..." ) ;
Log . w ( "PushTransport" , "Sending prekeybundle ciphertext message for new session..." ) ;
String ciphertext = getEncryptedPrekeyBundleMessageForNewSession ( socket , recipient , canonicalRecipientNumber , plaintext ) ;
byte [ ] ciphertext = getEncryptedPrekeyBundleMessageForNewSession ( socket , recipient , canonicalRecipientNumber , plaintext ) ;
return new Pair < Integer , String > ( OutgoingPushMessage . TYPE_MESSAGE_PREKEY_BUNDLE , ciphertext ) ;
return new Pair < Integer , byte [ ] > ( OutgoingPushMessage . TYPE_MESSAGE_PREKEY_BUNDLE , ciphertext ) ;
}
}
}
}
private String getEncryptedPrekeyBundleMessageForExistingSession ( Recipient recipient ,
private byte [ ] getEncryptedPrekeyBundleMessageForExistingSession ( Recipient recipient ,
String plaintext )
String plaintext )
{
{
IdentityKeyPair identityKeyPair = IdentityKeyUtil . getIdentityKeyPair ( context , masterSecret ) ;
IdentityKeyPair identityKeyPair = IdentityKeyUtil . getIdentityKeyPair ( context , masterSecret ) ;
@ -131,7 +139,7 @@ public class PushTransport extends BaseTransport {
return preKeyBundleMessage . serialize ( ) ;
return preKeyBundleMessage . serialize ( ) ;
}
}
private String getEncryptedPrekeyBundleMessageForNewSession ( PushServiceSocket socket ,
private byte [ ] getEncryptedPrekeyBundleMessageForNewSession ( PushServiceSocket socket ,
Recipient recipient ,
Recipient recipient ,
String canonicalRecipientNumber ,
String canonicalRecipientNumber ,
String plaintext )
String plaintext )
@ -151,14 +159,14 @@ public class PushTransport extends BaseTransport {
return preKeyBundleMessage . serialize ( ) ;
return preKeyBundleMessage . serialize ( ) ;
}
}
private String getEncryptedMessageForExistingSession ( Recipient recipient , String plaintext )
private byte [ ] getEncryptedMessageForExistingSession ( Recipient recipient , String plaintext )
throws IOException
throws IOException
{
{
IdentityKeyPair identityKeyPair = IdentityKeyUtil . getIdentityKeyPair ( context , masterSecret ) ;
IdentityKeyPair identityKeyPair = IdentityKeyUtil . getIdentityKeyPair ( context , masterSecret ) ;
MessageCipher messageCipher = new MessageCipher ( context , masterSecret , identityKeyPair ,
MessageCipher messageCipher = new MessageCipher ( context , masterSecret , identityKeyPair ,
new PushTransportDetails ( ) ) ;
new PushTransportDetails ( ) ) ;
return new String ( messageCipher . encrypt ( recipient , plaintext . getBytes ( ) ) ) ;
return messageCipher . encrypt ( recipient , plaintext . getBytes ( ) ) ;
}
}
}
}