/// <summary> /// Decrypt a received <see cref="SignalServiceEnvelope"/> /// </summary> /// <param name="envelope">The received SignalServiceEnvelope</param> /// <returns>a decrypted SignalServiceContent</returns> public SignalServiceContent decrypt(SignalServiceEnvelope envelope) { try { SignalServiceContent content = new SignalServiceContent(); if (envelope.hasLegacyMessage()) { DataMessage message = DataMessage.ParseFrom(decrypt(envelope, envelope.getLegacyMessage())); content = new SignalServiceContent(createSignalServiceMessage(envelope, message)); } else if (envelope.hasContent()) { Content message = Content.ParseFrom(decrypt(envelope, envelope.getContent())); if (message.HasDataMessage) { content = new SignalServiceContent(createSignalServiceMessage(envelope, message.DataMessage)); } else if (message.HasSyncMessage && localAddress.getNumber().Equals(envelope.getSource())) { content = new SignalServiceContent(createSynchronizeMessage(envelope, message.SyncMessage)); } } return(content); } catch (InvalidProtocolBufferException e) { throw new InvalidMessageException(e); } }
private void handleUntrustedIdentityMessage(SignalServiceEnvelope envelope, May <long> smsMessageId) { try { var database = DatabaseFactory.getTextMessageDatabase(); //getEncryptingSmsDatabase(context); Recipients recipients = RecipientFactory.getRecipientsFromString(envelope.getSource(), false); long recipientId = recipients.getPrimaryRecipient().getRecipientId(); PreKeySignalMessage whisperMessage = new PreKeySignalMessage(envelope.getLegacyMessage()); IdentityKey identityKey = whisperMessage.getIdentityKey(); String encoded = Base64.encodeBytes(envelope.getLegacyMessage()); IncomingTextMessage textMessage = new IncomingTextMessage(envelope.getSource(), envelope.getSourceDevice(), envelope.getTimestamp(), encoded, May <SignalServiceGroup> .NoValue); if (!smsMessageId.HasValue) { IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded); Pair <long, long> messageAndThreadId = database.InsertMessageInbox(bundleMessage); database.SetMismatchedIdentity(messageAndThreadId.first(), recipientId, identityKey); //MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second); } else { var messageId = smsMessageId.ForceGetValue(); database.UpdateMessageBody(messageId, encoded); database.MarkAsPreKeyBundle(messageId); database.SetMismatchedIdentity(messageId, recipientId, identityKey); } } catch (InvalidMessageException e) { throw new InvalidOperationException(e.Message); } catch (InvalidVersionException e) { throw new InvalidOperationException(e.Message); } }
public long Insert(SignalServiceEnvelope envelope) { // TODO check if exists var push = new Push() { Type = envelope.getType(), Source = envelope.getSource(), DeviceId = envelope.getSourceDevice(), LegacyMessage = envelope.hasLegacyMessage() ? Base64.encodeBytes(envelope.getLegacyMessage()) : "", Content = envelope.hasContent() ? Base64.encodeBytes(envelope.getContent()) : "", Timestamp = TimeUtil.GetDateTime(envelope.getTimestamp()) }; try { conn.Insert(push); } catch (Exception e) { Debug.WriteLine(e.Message); } return(push.PushId); }