mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 14:37:45 +00:00
WIP: clean up signal protocols (stickers)
This commit is contained in:
@@ -864,7 +864,7 @@ public class SignalServiceMessageSender {
|
||||
String body = Base64.encodeBytes(ciphertext);
|
||||
int type = isClosedGroup ? SignalServiceProtos.Envelope.Type.CLOSED_GROUP_CIPHERTEXT_VALUE :
|
||||
SignalServiceProtos.Envelope.Type.UNIDENTIFIED_SENDER_VALUE;
|
||||
OutgoingPushMessage message = new OutgoingPushMessage(type, 1, 0, body);
|
||||
OutgoingPushMessage message = new OutgoingPushMessage(type, body);
|
||||
messages.add(message);
|
||||
|
||||
return new OutgoingPushMessageList(publicKey, timestamp, messages, false);
|
||||
|
@@ -1,91 +0,0 @@
|
||||
/**
|
||||
* Copyright (C) 2014-2016 Open Whisper Systems
|
||||
*
|
||||
* Licensed according to the LICENSE file in this repository.
|
||||
*/
|
||||
|
||||
package org.session.libsignal.service.internal.crypto;
|
||||
|
||||
import com.google.protobuf.ByteString;
|
||||
|
||||
import org.session.libsignal.libsignal.InvalidKeyException;
|
||||
import org.session.libsignal.libsignal.ecc.Curve;
|
||||
import org.session.libsignal.libsignal.ecc.ECKeyPair;
|
||||
import org.session.libsignal.libsignal.ecc.ECPublicKey;
|
||||
import org.session.libsignal.libsignal.kdf.HKDFv3;
|
||||
import org.session.libsignal.service.internal.util.Util;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
|
||||
import static org.session.libsignal.service.internal.push.ProvisioningProtos.ProvisionEnvelope;
|
||||
import static org.session.libsignal.service.internal.push.ProvisioningProtos.ProvisionMessage;
|
||||
|
||||
|
||||
public class ProvisioningCipher {
|
||||
|
||||
private static final String TAG = ProvisioningCipher.class.getSimpleName();
|
||||
|
||||
private final ECPublicKey theirPublicKey;
|
||||
|
||||
public ProvisioningCipher(ECPublicKey theirPublicKey) {
|
||||
this.theirPublicKey = theirPublicKey;
|
||||
}
|
||||
|
||||
public byte[] encrypt(ProvisionMessage message) throws InvalidKeyException {
|
||||
ECKeyPair ourKeyPair = Curve.generateKeyPair();
|
||||
byte[] sharedSecret = Curve.calculateAgreement(theirPublicKey, ourKeyPair.getPrivateKey());
|
||||
byte[] derivedSecret = new HKDFv3().deriveSecrets(sharedSecret, "TextSecure Provisioning Message".getBytes(), 64);
|
||||
byte[][] parts = Util.split(derivedSecret, 32, 32);
|
||||
|
||||
byte[] version = {0x01};
|
||||
byte[] ciphertext = getCiphertext(parts[0], message.toByteArray());
|
||||
byte[] mac = getMac(parts[1], Util.join(version, ciphertext));
|
||||
byte[] body = Util.join(version, ciphertext, mac);
|
||||
|
||||
return ProvisionEnvelope.newBuilder()
|
||||
.setPublicKey(ByteString.copyFrom(ourKeyPair.getPublicKey().serialize()))
|
||||
.setBody(ByteString.copyFrom(body))
|
||||
.build()
|
||||
.toByteArray();
|
||||
}
|
||||
|
||||
private byte[] getCiphertext(byte[] key, byte[] message) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
|
||||
|
||||
return Util.join(cipher.getIV(), cipher.doFinal(message));
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (NoSuchPaddingException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (java.security.InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (IllegalBlockSizeException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (BadPaddingException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] getMac(byte[] key, byte[] message) {
|
||||
try {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(key, "HmacSHA256"));
|
||||
|
||||
return mac.doFinal(message);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new AssertionError(e);
|
||||
} catch (java.security.InvalidKeyException e) {
|
||||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@@ -14,20 +14,11 @@ public class OutgoingPushMessage {
|
||||
@JsonProperty
|
||||
public int type;
|
||||
@JsonProperty
|
||||
private int destinationDeviceId;
|
||||
@JsonProperty
|
||||
private int destinationRegistrationId;
|
||||
@JsonProperty
|
||||
public String content;
|
||||
|
||||
public OutgoingPushMessage(int type,
|
||||
int destinationDeviceId,
|
||||
int destinationRegistrationId,
|
||||
String content)
|
||||
public OutgoingPushMessage(int type, String content)
|
||||
{
|
||||
this.type = type;
|
||||
this.destinationDeviceId = destinationDeviceId;
|
||||
this.destinationRegistrationId = destinationRegistrationId;
|
||||
this.content = content;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user