Store the incoming and outgoing secrets separately.

This commit is contained in:
akwizgran
2011-11-15 16:07:14 +00:00
parent f41d48eb9f
commit 6a15c03e81
26 changed files with 200 additions and 336 deletions

View File

@@ -29,7 +29,7 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
TransportIndex i, byte[] encryptedIv, byte[] secret) {
// Decrypt the IV
Cipher ivCipher = crypto.getIvCipher();
ErasableKey ivKey = crypto.deriveIncomingIvKey(secret);
ErasableKey ivKey = crypto.deriveIvKey(secret, true);
byte[] iv;
try {
ivCipher.init(Cipher.DECRYPT_MODE, ivKey);
@@ -57,15 +57,17 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
private ConnectionReader createConnectionReader(InputStream in,
boolean initiator, TransportIndex i, long connection,
byte[] secret) {
byte[] iv = IvEncoder.encodeIv(initiator, i, connection);
// Derive the keys and erase the secret
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
for(int j = 0; j < secret.length; j++) secret[j] = 0;
// Create the decrypter
byte[] iv = IvEncoder.encodeIv(initiator, i, connection);
Cipher frameCipher = crypto.getFrameCipher();
ErasableKey frameKey = crypto.deriveIncomingFrameKey(secret);
ConnectionDecrypter decrypter = new ConnectionDecrypterImpl(in, iv,
frameCipher, frameKey);
// Create the reader
Mac mac = crypto.getMac();
ErasableKey macKey = crypto.deriveIncomingMacKey(secret);
return new ConnectionReaderImpl(decrypter, mac, macKey);
}
}

View File

@@ -75,7 +75,9 @@ DatabaseListener {
}
private synchronized void calculateIvs(ContactId c) throws DbException {
ErasableKey ivKey = crypto.deriveIncomingIvKey(db.getSharedSecret(c));
byte[] secret = db.getSharedSecret(c, true);
ErasableKey ivKey = crypto.deriveIvKey(secret, true);
for(int i = 0; i < secret.length; i++) secret[i] = 0;
for(TransportId t : localTransportIds) {
TransportIndex i = db.getRemoteIndex(c, t);
if(i != null) {
@@ -131,7 +133,9 @@ DatabaseListener {
TransportIndex i1 = ctx1.getTransportIndex();
if(c1.equals(c) && i1.equals(i)) it.remove();
}
ErasableKey ivKey = crypto.deriveIncomingIvKey(db.getSharedSecret(c));
byte[] secret = db.getSharedSecret(c, true);
ErasableKey ivKey = crypto.deriveIvKey(secret, true);
for(int j = 0; j < secret.length; j++) secret[j] = 0;
calculateIvs(c, ctx.getTransportId(), i, ivKey, w);
} catch(NoSuchContactException e) {
// The contact was removed - clean up when we get the event
@@ -181,8 +185,9 @@ DatabaseListener {
private synchronized void calculateIvs(TransportId t) throws DbException {
for(ContactId c : db.getContacts()) {
try {
byte[] secret = db.getSharedSecret(c);
ErasableKey ivKey = crypto.deriveIncomingIvKey(secret);
byte[] secret = db.getSharedSecret(c, true);
ErasableKey ivKey = crypto.deriveIvKey(secret, true);
for(int i = 0; i < secret.length; i++) secret[i] = 0;
TransportIndex i = db.getRemoteIndex(c, t);
if(i != null) {
ConnectionWindow w = db.getConnectionWindow(c, i);

View File

@@ -36,7 +36,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
byte[] secret) {
// Decrypt the IV
Cipher ivCipher = crypto.getIvCipher();
ErasableKey ivKey = crypto.deriveIncomingIvKey(secret);
ErasableKey ivKey = crypto.deriveIvKey(secret, true);
byte[] iv;
try {
ivCipher.init(Cipher.DECRYPT_MODE, ivKey);
@@ -60,17 +60,19 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
private ConnectionWriter createConnectionWriter(OutputStream out,
long capacity, boolean initiator, TransportIndex i, long connection,
byte[] secret) {
// Derive the keys and erase the secret
ErasableKey ivKey = crypto.deriveIvKey(secret, initiator);
ErasableKey frameKey = crypto.deriveFrameKey(secret, initiator);
ErasableKey macKey = crypto.deriveMacKey(secret, initiator);
for(int j = 0; j < secret.length; j++) secret[j] = 0;
// Create the encrypter
Cipher ivCipher = crypto.getIvCipher();
Cipher frameCipher = crypto.getFrameCipher();
ErasableKey ivKey = crypto.deriveOutgoingIvKey(secret);
ErasableKey frameKey = crypto.deriveOutgoingFrameKey(secret);
byte[] iv = IvEncoder.encodeIv(initiator, i, connection);
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
capacity, iv, ivCipher, frameCipher, ivKey, frameKey);
// Create the writer
Mac mac = crypto.getMac();
ErasableKey macKey = crypto.deriveOutgoingMacKey(secret);
return new ConnectionWriterImpl(encrypter, mac, macKey);
}
}

View File

@@ -47,7 +47,7 @@ class IncomingBatchConnection {
void read() {
try {
byte[] secret = db.getSharedSecret(contactId);
byte[] secret = db.getSharedSecret(contactId, true);
ConnectionReader conn = connFactory.createConnectionReader(
reader.getInputStream(), transportIndex, encryptedIv,
secret);

View File

@@ -46,7 +46,7 @@ class OutgoingBatchConnection {
void write() {
try {
byte[] secret = db.getSharedSecret(contactId);
byte[] secret = db.getSharedSecret(contactId, false);
long connection = db.getConnectionNumber(contactId, transportIndex);
ConnectionWriter conn = connFactory.createConnectionWriter(
writer.getOutputStream(), writer.getCapacity(),

View File

@@ -33,7 +33,7 @@ public class IncomingStreamConnection extends StreamConnection {
@Override
protected ConnectionReader createConnectionReader() throws DbException,
IOException {
byte[] secret = db.getSharedSecret(contactId);
byte[] secret = db.getSharedSecret(contactId, true);
return connReaderFactory.createConnectionReader(
connection.getInputStream(), transportIndex, encryptedIv,
secret);
@@ -42,7 +42,7 @@ public class IncomingStreamConnection extends StreamConnection {
@Override
protected ConnectionWriter createConnectionWriter() throws DbException,
IOException {
byte[] secret = db.getSharedSecret(contactId);
byte[] secret = db.getSharedSecret(contactId, false);
return connWriterFactory.createConnectionWriter(
connection.getOutputStream(), Long.MAX_VALUE, transportIndex,
encryptedIv, secret);

View File

@@ -37,7 +37,7 @@ public class OutgoingStreamConnection extends StreamConnection {
transportIndex);
}
}
byte[] secret = db.getSharedSecret(contactId);
byte[] secret = db.getSharedSecret(contactId, true);
return connReaderFactory.createConnectionReader(
connection.getInputStream(), transportIndex, connectionNum,
secret);
@@ -52,7 +52,7 @@ public class OutgoingStreamConnection extends StreamConnection {
transportIndex);
}
}
byte[] secret = db.getSharedSecret(contactId);
byte[] secret = db.getSharedSecret(contactId, false);
return connWriterFactory.createConnectionWriter(
connection.getOutputStream(), Long.MAX_VALUE, transportIndex,
connectionNum, secret);