Erase keys after using them. (Copies created by ciphers, etc, may

still exist.)
This commit is contained in:
akwizgran
2011-11-16 16:22:35 +00:00
parent ece03038f4
commit f10512d787
10 changed files with 115 additions and 62 deletions

View File

@@ -4,6 +4,9 @@ import javax.crypto.SecretKey;
public interface ErasableKey extends SecretKey { public interface ErasableKey extends SecretKey {
/** Returns a copy of the key. */
ErasableKey copy();
/** Erases the key from memory. */ /** Erases the key from memory. */
void erase(); void erase();
} }

View File

@@ -33,6 +33,10 @@ class ErasableKeyImpl implements ErasableKey {
return "RAW"; return "RAW";
} }
public ErasableKey copy() {
return new ErasableKeyImpl(getEncoded(), algorithm);
}
public void erase() { public void erase() {
if(erased) throw new IllegalStateException(); if(erased) throw new IllegalStateException();
ByteUtils.erase(key); ByteUtils.erase(key);

View File

@@ -43,6 +43,7 @@ implements ConnectionDecrypter {
} }
public void readMac(byte[] mac) throws IOException { public void readMac(byte[] mac) throws IOException {
try {
if(betweenFrames) throw new IllegalStateException(); if(betweenFrames) throw new IllegalStateException();
// If we have any plaintext in the buffer, copy it into the MAC // If we have any plaintext in the buffer, copy it into the MAC
System.arraycopy(buf, bufOff, mac, 0, bufLen); System.arraycopy(buf, bufOff, mac, 0, bufLen);
@@ -68,13 +69,21 @@ implements ConnectionDecrypter {
} }
bufOff = bufLen = 0; bufOff = bufLen = 0;
betweenFrames = true; betweenFrames = true;
} catch(IOException e) {
frameKey.erase();
throw e;
}
} }
@Override @Override
public int read() throws IOException { public int read() throws IOException {
try {
if(betweenFrames) initialiseCipher(); if(betweenFrames) initialiseCipher();
if(bufLen == 0) { if(bufLen == 0) {
if(!readBlock()) return -1; if(!readBlock()) {
frameKey.erase();
return -1;
}
bufOff = 0; bufOff = 0;
bufLen = buf.length; bufLen = buf.length;
} }
@@ -82,6 +91,10 @@ implements ConnectionDecrypter {
bufOff++; bufOff++;
bufLen--; bufLen--;
return i < 0 ? i + 256 : i; return i < 0 ? i + 256 : i;
} catch(IOException e) {
frameKey.erase();
throw e;
}
} }
@Override @Override
@@ -91,9 +104,13 @@ implements ConnectionDecrypter {
@Override @Override
public int read(byte[] b, int off, int len) throws IOException { public int read(byte[] b, int off, int len) throws IOException {
try {
if(betweenFrames) initialiseCipher(); if(betweenFrames) initialiseCipher();
if(bufLen == 0) { if(bufLen == 0) {
if(!readBlock()) return -1; if(!readBlock()) {
frameKey.erase();
return -1;
}
bufOff = 0; bufOff = 0;
bufLen = buf.length; bufLen = buf.length;
} }
@@ -102,6 +119,10 @@ implements ConnectionDecrypter {
bufOff += length; bufOff += length;
bufLen -= length; bufLen -= length;
return length; return length;
} catch(IOException e) {
frameKey.erase();
throw e;
}
} }
// Although we're using CTR mode, which doesn't require full blocks of // Although we're using CTR mode, which doesn't require full blocks of

View File

@@ -46,6 +46,7 @@ implements ConnectionEncrypter {
} }
if(encryptedIv.length != IV_LENGTH) if(encryptedIv.length != IV_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
ivKey.erase();
} }
public OutputStream getOutputStream() { public OutputStream getOutputStream() {
@@ -53,6 +54,7 @@ implements ConnectionEncrypter {
} }
public void writeMac(byte[] mac) throws IOException { public void writeMac(byte[] mac) throws IOException {
try {
if(!ivWritten || betweenFrames) throw new IllegalStateException(); if(!ivWritten || betweenFrames) throw new IllegalStateException();
try { try {
out.write(frameCipher.doFinal(mac)); out.write(frameCipher.doFinal(mac));
@@ -63,6 +65,10 @@ implements ConnectionEncrypter {
} }
capacity -= mac.length; capacity -= mac.length;
betweenFrames = true; betweenFrames = true;
} catch(IOException e) {
frameKey.erase();
throw e;
}
} }
public long getRemainingCapacity() { public long getRemainingCapacity() {
@@ -71,11 +77,16 @@ implements ConnectionEncrypter {
@Override @Override
public void write(int b) throws IOException { public void write(int b) throws IOException {
try {
if(!ivWritten) writeIv(); if(!ivWritten) writeIv();
if(betweenFrames) initialiseCipher(); if(betweenFrames) initialiseCipher();
byte[] ciphertext = frameCipher.update(new byte[] {(byte) b}); byte[] ciphertext = frameCipher.update(new byte[] {(byte) b});
if(ciphertext != null) out.write(ciphertext); if(ciphertext != null) out.write(ciphertext);
capacity--; capacity--;
} catch(IOException e) {
frameKey.erase();
throw e;
}
} }
@Override @Override
@@ -85,11 +96,16 @@ implements ConnectionEncrypter {
@Override @Override
public void write(byte[] b, int off, int len) throws IOException { public void write(byte[] b, int off, int len) throws IOException {
try {
if(!ivWritten) writeIv(); if(!ivWritten) writeIv();
if(betweenFrames) initialiseCipher(); if(betweenFrames) initialiseCipher();
byte[] ciphertext = frameCipher.update(b, off, len); byte[] ciphertext = frameCipher.update(b, off, len);
if(ciphertext != null) out.write(ciphertext); if(ciphertext != null) out.write(ciphertext);
capacity -= len; capacity -= len;
} catch(IOException e) {
frameKey.erase();
throw e;
}
} }
private void writeIv() throws IOException { private void writeIv() throws IOException {

View File

@@ -42,6 +42,7 @@ class ConnectionReaderFactoryImpl implements ConnectionReaderFactory {
} catch(InvalidKeyException badKey) { } catch(InvalidKeyException badKey) {
throw new IllegalArgumentException(badKey); throw new IllegalArgumentException(badKey);
} }
ivKey.erase();
// Validate the IV // Validate the IV
if(!IvEncoder.validateIv(iv, true, ctx)) if(!IvEncoder.validateIv(iv, true, ctx))
throw new IllegalArgumentException(); throw new IllegalArgumentException();

View File

@@ -40,6 +40,7 @@ implements ConnectionReader {
} catch(InvalidKeyException e) { } catch(InvalidKeyException e) {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
macKey.erase();
maxPayloadLength = MAX_FRAME_LENGTH - 4 - mac.getMacLength(); maxPayloadLength = MAX_FRAME_LENGTH - 4 - mac.getMacLength();
header = new byte[4]; header = new byte[4];
payload = new byte[maxPayloadLength]; payload = new byte[maxPayloadLength];

View File

@@ -92,6 +92,7 @@ DatabaseListener {
byte[] secret = e.getValue(); byte[] secret = e.getValue();
ErasableKey ivKey = crypto.deriveIvKey(secret, true); ErasableKey ivKey = crypto.deriveIvKey(secret, true);
Bytes iv = new Bytes(encryptIv(i, unseen, ivKey)); Bytes iv = new Bytes(encryptIv(i, unseen, ivKey));
ivKey.erase();
expected.put(iv, new ConnectionContextImpl(c, i, unseen, secret)); expected.put(iv, new ConnectionContextImpl(c, i, unseen, secret));
} }
} }

View File

@@ -47,6 +47,7 @@ class ConnectionWriterFactoryImpl implements ConnectionWriterFactory {
} catch(InvalidKeyException badKey) { } catch(InvalidKeyException badKey) {
throw new RuntimeException(badKey); throw new RuntimeException(badKey);
} }
ivKey.erase();
// Validate the IV // Validate the IV
if(!IvEncoder.validateIv(iv, true, ctx)) if(!IvEncoder.validateIv(iv, true, ctx))
throw new IllegalArgumentException(); throw new IllegalArgumentException();

View File

@@ -41,6 +41,7 @@ implements ConnectionWriter {
} catch(InvalidKeyException badKey) { } catch(InvalidKeyException badKey) {
throw new IllegalArgumentException(badKey); throw new IllegalArgumentException(badKey);
} }
macKey.erase();
maxPayloadLength = MAX_FRAME_LENGTH - 4 - mac.getMacLength(); maxPayloadLength = MAX_FRAME_LENGTH - 4 - mac.getMacLength();
buf = new ByteArrayOutputStream(maxPayloadLength); buf = new ByteArrayOutputStream(maxPayloadLength);
header = new byte[4]; header = new byte[4];

View File

@@ -74,12 +74,16 @@ public class FrameReadWriteTest extends TestCase {
random.nextBytes(frame); random.nextBytes(frame);
byte[] frame1 = new byte[321]; byte[] frame1 = new byte[321];
random.nextBytes(frame1); random.nextBytes(frame1);
// Copy the keys - the copies will be erased
ErasableKey frameCopy = frameKey.copy();
ErasableKey ivCopy = ivKey.copy();
ErasableKey macCopy = macKey.copy();
// Write the frames // Write the frames
ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream();
ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out, ConnectionEncrypter encrypter = new ConnectionEncrypterImpl(out,
Long.MAX_VALUE, iv, ivCipher, frameCipher, ivKey, frameKey); Long.MAX_VALUE, iv, ivCipher, frameCipher, ivCopy, frameCopy);
ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac, ConnectionWriter writer = new ConnectionWriterImpl(encrypter, mac,
macKey); macCopy);
OutputStream out1 = writer.getOutputStream(); OutputStream out1 = writer.getOutputStream();
out1.write(frame); out1.write(frame);
out1.flush(); out1.flush();