Simplify the Ghostryde API

First step of RDE encoding refactoring.

Creates a single InputStream (OutputStream) to decode (encode) Ghostryde files.
This replaces the 3 InputStreams (OutputStreams) that were needed before.

Also removes a lot of classes, and removes the "injection" of the Ghostryde
class. It's an encoding, there's no point in injecting it.

Finally, removed the buffer-size configuration and replaced with a static final
const value. It's just a buffer size - it doesn't actually affect much. There
are much more "important" fields that weren't configured (such as the
compression algorithm and whether or not to do integrity checks)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=202319102
This commit is contained in:
guyben 2018-06-27 09:10:19 -07:00 committed by Ben McIlwain
parent bee3d6a5a4
commit 6ff48b7dae
15 changed files with 347 additions and 589 deletions

View file

@ -638,17 +638,6 @@ public final class RegistryConfig {
return projectId + "-rde-import"; return projectId + "-rde-import";
} }
/**
* Size of Ghostryde buffer in bytes for each layer in the pipeline.
*
* @see google.registry.rde.Ghostryde
*/
@Provides
@Config("rdeGhostrydeBufferSize")
public static Integer provideRdeGhostrydeBufferSize() {
return 64 * 1024;
}
/** /**
* Amount of time between RDE deposits. * Amount of time between RDE deposits.
* *

View file

@ -16,7 +16,6 @@ package google.registry.rde;
import static google.registry.model.rde.RdeMode.THIN; import static google.registry.model.rde.RdeMode.THIN;
import static google.registry.request.Action.Method.POST; import static google.registry.request.Action.Method.POST;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.common.flogger.FluentLogger; import com.google.common.flogger.FluentLogger;
@ -29,7 +28,6 @@ import google.registry.request.Action;
import google.registry.request.Parameter; import google.registry.request.Parameter;
import google.registry.request.RequestParameters; import google.registry.request.RequestParameters;
import google.registry.request.auth.Auth; import google.registry.request.auth.Auth;
import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
@ -66,7 +64,6 @@ public final class BrdaCopyAction implements Runnable {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Inject GcsUtils gcsUtils; @Inject GcsUtils gcsUtils;
@Inject Ghostryde ghostryde;
@Inject RydePgpCompressionOutputStreamFactory pgpCompressionFactory; @Inject RydePgpCompressionOutputStreamFactory pgpCompressionFactory;
@Inject RydePgpFileOutputStreamFactory pgpFileFactory; @Inject RydePgpFileOutputStreamFactory pgpFileFactory;
@Inject RydePgpEncryptionOutputStreamFactory pgpEncryptionFactory; @Inject RydePgpEncryptionOutputStreamFactory pgpEncryptionFactory;
@ -102,10 +99,7 @@ public final class BrdaCopyAction implements Runnable {
logger.atInfo().log("Writing %s", rydeFile); logger.atInfo().log("Writing %s", rydeFile);
byte[] signature; byte[] signature;
try (InputStream gcsInput = gcsUtils.openInputStream(xmlFilename); try (InputStream gcsInput = gcsUtils.openInputStream(xmlFilename);
Ghostryde.Decryptor decryptor = ghostryde.openDecryptor(gcsInput, stagingDecryptionKey); InputStream ghostrydeDecoder = Ghostryde.decoder(gcsInput, stagingDecryptionKey);
Ghostryde.Decompressor decompressor = ghostryde.openDecompressor(decryptor);
Ghostryde.Input ghostInput = ghostryde.openInput(decompressor);
BufferedInputStream xmlInput = new BufferedInputStream(ghostInput);
OutputStream gcsOutput = gcsUtils.openOutputStream(rydeFile); OutputStream gcsOutput = gcsUtils.openOutputStream(rydeFile);
RydePgpSigningOutputStream signLayer = pgpSigningFactory.create(gcsOutput, signingKey)) { RydePgpSigningOutputStream signLayer = pgpSigningFactory.create(gcsOutput, signingKey)) {
try (OutputStream encryptLayer = pgpEncryptionFactory.create(signLayer, receiverKey); try (OutputStream encryptLayer = pgpEncryptionFactory.create(signLayer, receiverKey);
@ -113,7 +107,7 @@ public final class BrdaCopyAction implements Runnable {
OutputStream fileLayer = pgpFileFactory.create(compressLayer, watermark, prefix + ".tar"); OutputStream fileLayer = pgpFileFactory.create(compressLayer, watermark, prefix + ".tar");
OutputStream tarLayer = OutputStream tarLayer =
tarFactory.create(fileLayer, xmlLength, watermark, prefix + ".xml")) { tarFactory.create(fileLayer, xmlLength, watermark, prefix + ".xml")) {
ByteStreams.copy(xmlInput, tarLayer); ByteStreams.copy(ghostrydeDecoder, tarLayer);
} }
signature = signLayer.getSignature(); signature = signLayer.getSignature();
} }
@ -127,7 +121,7 @@ public final class BrdaCopyAction implements Runnable {
/** Reads the contents of a file from Cloud Storage that contains nothing but an integer. */ /** Reads the contents of a file from Cloud Storage that contains nothing but an integer. */
private long readXmlLength(GcsFilename xmlLengthFilename) throws IOException { private long readXmlLength(GcsFilename xmlLengthFilename) throws IOException {
try (InputStream input = gcsUtils.openInputStream(xmlLengthFilename)) { try (InputStream input = gcsUtils.openInputStream(xmlLengthFilename)) {
return Long.parseLong(new String(ByteStreams.toByteArray(input), UTF_8).trim()); return Ghostryde.readLength(input);
} }
} }
} }

View file

@ -17,15 +17,16 @@ package google.registry.rde;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Preconditions.checkState;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.bouncycastle.bcpg.CompressionAlgorithmTags.ZLIB; import static org.bouncycastle.bcpg.CompressionAlgorithmTags.ZLIB;
import static org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags.AES_128; import static org.bouncycastle.bcpg.SymmetricKeyAlgorithmTags.AES_128;
import static org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME; import static org.bouncycastle.jce.provider.BouncyCastleProvider.PROVIDER_NAME;
import static org.bouncycastle.openpgp.PGPLiteralData.BINARY; import static org.bouncycastle.openpgp.PGPLiteralData.BINARY;
import static org.joda.time.DateTimeZone.UTC;
import com.google.common.flogger.FluentLogger; import com.google.common.flogger.FluentLogger;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import google.registry.config.RegistryConfig.Config; import com.google.common.io.Closer;
import google.registry.util.ImprovedInputStream; import google.registry.util.ImprovedInputStream;
import google.registry.util.ImprovedOutputStream; import google.registry.util.ImprovedOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
@ -38,11 +39,7 @@ import java.security.ProviderException;
import java.security.SecureRandom; import java.security.SecureRandom;
import javax.annotation.CheckReturnValue; import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.WillCloseWhenClosed;
import javax.annotation.WillNotClose; import javax.annotation.WillNotClose;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import javax.inject.Inject;
import org.bouncycastle.openpgp.PGPCompressedData; import org.bouncycastle.openpgp.PGPCompressedData;
import org.bouncycastle.openpgp.PGPCompressedDataGenerator; import org.bouncycastle.openpgp.PGPCompressedDataGenerator;
import org.bouncycastle.openpgp.PGPEncryptedDataGenerator; import org.bouncycastle.openpgp.PGPEncryptedDataGenerator;
@ -68,39 +65,42 @@ import org.joda.time.DateTime;
* eyes of anyone with access to the <a href="https://cloud.google.com/console">Google Cloud * eyes of anyone with access to the <a href="https://cloud.google.com/console">Google Cloud
* Console</a>. * Console</a>.
* *
* <p>This class has an unusual API that's designed to take advantage of Java 7 try-with-resource * <p>The encryption is similar to the "regular" RyDE RDE deposit file encryption. The main
* statements to the greatest extent possible, while also maintaining security contracts at * difference (and the reason we had to create a custom encryption) is that the RDE deposit has a
* compile-time. * tar file in the encoding. A tar file needs to know its final size in the header, which means we
* have to create the entire deposit before we can start encoding it.
*
* <p>Deposits are big, and there's no reason to hold it all in memory. Instead, save a "staging"
* version encrypted with Ghostryde instead of "RyDE" (the RDE encryption/encoding), using the
* "rde-staging" keys. We also remember the actual data size during the staging creation.
*
* <p>Then when we want to create the actual deposits, we decrypt the staging version, and using the
* saved value for the data size we can encrypt with "RyDE" using the receiver key.
* *
* <p>Here's how you write a file: * <p>Here's how you write a file:
* *
* <pre> {@code * <pre> {@code
* File in = new File("lol.txt"); * File in = new File("lol.txt");
* File out = new File("lol.txt.ghostryde"); * File out = new File("lol.txt.ghostryde");
* Ghostryde ghost = new Ghostryde(1024); * File lengthOut = new File("lol.length.ghostryde");
* try (OutputStream output = new FileOutputStream(out); * try (OutputStream output = new FileOutputStream(out);
* Ghostryde.Encryptor encryptor = ghost.openEncryptor(output, publicKey); * OutputStream lengthOutput = new FileOutputStream(lengthOut);
* Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); * OutputStream ghostrydeEncoder = Ghostryde.encoder(output, publicKey, lengthOut);
* OutputStream go = ghost.openOutput(kompressor, in.getName(), DateTime.now(UTC)); * InputStream input = new FileInputStream(in)) &lbrace;
* InputStream input = new FileInputStream(in)) &lbrace; * ByteStreams.copy(input, ghostrydeEncoder);
* ByteStreams.copy(input, go); * &rbrace;}</pre>
* &rbrace;}</pre>
* *
* <p>Here's how you read a file: * <p>Here's how you read a file:
* *
* <pre> {@code * <pre> {@code
* File in = new File("lol.txt.ghostryde"); * File in = new File("lol.txt.ghostryde");
* File out = new File("lol.txt"); * File out = new File("lol.txt");
* Ghostryde ghost = new Ghostryde(1024); * Ghostryde ghost = new Ghostryde(1024);
* try (InputStream fileInput = new FileInputStream(in); * try (InputStream fileInput = new FileInputStream(in);
* Ghostryde.Decryptor decryptor = ghost.openDecryptor(fileInput, privateKey); * InputStream ghostrydeDecoder = new Ghostryde.decoder(fileInput, privateKey);
* Ghostryde.Decompressor decompressor = ghost.openDecompressor(decryptor); * OutputStream fileOutput = new FileOutputStream(out)) &lbrace;
* Ghostryde.Input input = ghost.openInput(decompressor); * ByteStreams.copy(ghostryderDecoder, fileOutput);
* OutputStream fileOutput = new FileOutputStream(out)) &lbrace; * &rbrace;}</pre>
* System.out.println("name = " + input.getName());
* System.out.println("modified = " + input.getModified());
* ByteStreams.copy(input, fileOutput);
* &rbrace;}</pre>
* *
* <h2>Simple API</h2> * <h2>Simple API</h2>
* *
@ -108,10 +108,11 @@ import org.joda.time.DateTime;
* static methods more convenient: * static methods more convenient:
* *
* <pre> {@code * <pre> {@code
* byte[] data = "hello kitty".getBytes(UTF_8); * byte[] data = "hello kitty".getBytes(UTF_8);
* byte[] blob = Ghostryde.encode(data, publicKey, "lol.txt", DateTime.now(UTC)); * byte[] blob = Ghostryde.encode(data, publicKey);
* Ghostryde.Result result = Ghostryde.decode(blob, privateKey); * byte[] result = Ghostryde.decode(blob, privateKey);
* }</pre> *
* }</pre>
* *
* <h2>GhostRYDE Format</h2> * <h2>GhostRYDE Format</h2>
* *
@ -122,11 +123,13 @@ import org.joda.time.DateTime;
* <p>Ghostryde is different from RyDE in the sense that ghostryde is only used for <i>internal</i> * <p>Ghostryde is different from RyDE in the sense that ghostryde is only used for <i>internal</i>
* storage; whereas RyDE is meant to protect data being stored by a third-party. * storage; whereas RyDE is meant to protect data being stored by a third-party.
*/ */
@Immutable
public final class Ghostryde { public final class Ghostryde {
private static final FluentLogger logger = FluentLogger.forEnclosingClass(); private static final FluentLogger logger = FluentLogger.forEnclosingClass();
/** Size of the buffer used by the intermediate streams. */
static final int BUFFER_SIZE = 64 * 1024;
/** /**
* Compression algorithm to use when creating ghostryde files. * Compression algorithm to use when creating ghostryde files.
* *
@ -162,22 +165,27 @@ public final class Ghostryde {
*/ */
static final String RANDOM_SOURCE = "NativePRNG"; static final String RANDOM_SOURCE = "NativePRNG";
/**
* For backwards compatibility reasons, we wrap the data in a PGP file, which preserves the
* original filename and modification time. However, these values are never used, so we just
* set them to a constant value.
*/
static final String INNER_FILENAME = "file.xml";
static final DateTime INNER_MODIFICATION_TIME = DateTime.parse("2000-01-01TZ");
/** /**
* Creates a ghostryde file from an in-memory byte array. * Creates a ghostryde file from an in-memory byte array.
* *
* @throws PGPException * @throws PGPException
* @throws IOException * @throws IOException
*/ */
public static byte[] encode(byte[] data, PGPPublicKey key, String name, DateTime modified) public static byte[] encode(byte[] data, PGPPublicKey key)
throws IOException, PGPException { throws IOException, PGPException {
checkNotNull(data, "data"); checkNotNull(data, "data");
checkArgument(key.isEncryptionKey(), "not an encryption key"); checkArgument(key.isEncryptionKey(), "not an encryption key");
Ghostryde ghost = new Ghostryde(1024 * 64);
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
try (Encryptor encryptor = ghost.openEncryptor(output, key); try (OutputStream encoder = encoder(output, key)) {
Compressor kompressor = ghost.openCompressor(encryptor); encoder.write(data);
OutputStream go = ghost.openOutput(kompressor, name, modified)) {
go.write(data);
} }
return output.toByteArray(); return output.toByteArray();
} }
@ -188,191 +196,106 @@ public final class Ghostryde {
* @throws PGPException * @throws PGPException
* @throws IOException * @throws IOException
*/ */
public static DecodeResult decode(byte[] data, PGPPrivateKey key) public static byte[] decode(byte[] data, PGPPrivateKey key)
throws IOException, PGPException { throws IOException, PGPException {
checkNotNull(data, "data"); checkNotNull(data, "data");
Ghostryde ghost = new Ghostryde(1024 * 64);
ByteArrayInputStream dataStream = new ByteArrayInputStream(data); ByteArrayInputStream dataStream = new ByteArrayInputStream(data);
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
String name; try (InputStream ghostrydeDecoder = decoder(dataStream, key)) {
DateTime modified; ByteStreams.copy(ghostrydeDecoder, output);
try (Decryptor decryptor = ghost.openDecryptor(dataStream, key);
Decompressor decompressor = ghost.openDecompressor(decryptor);
Input input = ghost.openInput(decompressor)) {
name = input.getName();
modified = input.getModified();
ByteStreams.copy(input, output);
} }
return new DecodeResult(output.toByteArray(), name, modified); return output.toByteArray();
} }
/** Result class for the {@link Ghostryde#decode(byte[], PGPPrivateKey)} method. */ /** Reads the value of a length stream - see {@link #encoder}. */
@Immutable public static long readLength(InputStream lengthStream) throws IOException {
public static final class DecodeResult { return Long.parseLong(new String(ByteStreams.toByteArray(lengthStream), UTF_8).trim());
private final byte[] data;
private final String name;
private final DateTime modified;
DecodeResult(byte[] data, String name, DateTime modified) {
this.data = checkNotNull(data, "data");
this.name = checkNotNull(name, "name");
this.modified = checkNotNull(modified, "modified");
}
/** Returns the decoded ghostryde content bytes. */
public byte[] getData() {
return data;
}
/** Returns the name of the original file, taken from the literal data packet. */
public String getName() {
return name;
}
/** Returns the time this file was created or modified, take from the literal data packet. */
public DateTime getModified() {
return modified;
}
} }
/** /**
* PGP literal file {@link InputStream}. * Creates a Ghostryde Encoder.
* *
* @see Ghostryde#openInput(Decompressor) * <p>Optionally can also save the total length of the data written to an OutputStream.
*
* <p>This is necessary because the RyDE format uses a tar file which requires the total length in
* the header. We don't want to have to decrypt the entire ghostryde file to determine the length,
* so we just save it separately.
*
* @param output where to write the encrypted data
* @param encryptionKey the encryption key to use
* @param lengthOutput if not null - will save the total length of the data written to this
* output. See {@link #readLength}.
*/ */
@NotThreadSafe public static ImprovedOutputStream encoder(
public static final class Input extends ImprovedInputStream { OutputStream output, PGPPublicKey encryptionKey, @Nullable OutputStream lengthOutput)
private final String name; throws IOException, PGPException {
private final DateTime modified;
Input(@WillCloseWhenClosed InputStream input, String name, DateTime modified) { // We use a Closer to handle the stream .close, to make sure it's done correctly.
super("Input", input); Closer closer = Closer.create();
this.name = checkNotNull(name, "name"); OutputStream encryptionLayer = closer.register(openEncryptor(output, encryptionKey));
this.modified = checkNotNull(modified, "modified"); OutputStream kompressor = closer.register(openCompressor(encryptionLayer));
} OutputStream fileLayer =
closer.register(
openPgpFileOutputStream(kompressor, INNER_FILENAME, INNER_MODIFICATION_TIME));
/** Returns the name of the original file, taken from the literal data packet. */ return new ImprovedOutputStream("GhostrydeEncoder", fileLayer) {
public String getName() { @Override
return name; public void onClose() throws IOException {
} // Close all the streams we opened
closer.close();
/** Returns the time this file was created or modified, take from the literal data packet. */ // Optionally also output the size of the encoded data - which is needed for the RyDE
public DateTime getModified() { // encoding.
return modified; if (lengthOutput != null) {
} lengthOutput.write(Long.toString(getBytesWritten()).getBytes(US_ASCII));
}
/**
* PGP literal file {@link OutputStream}.
*
* <p>This class isn't needed for ordering safety, but is included regardless for consistency and
* to improve the appearance of log messages.
*
* @see Ghostryde#openOutput(Compressor, String, DateTime)
*/
@NotThreadSafe
public static final class Output extends ImprovedOutputStream {
Output(@WillCloseWhenClosed OutputStream os) {
super("Output", os);
}
}
/**
* Encryption {@link OutputStream}.
*
* <p>This type exists to guarantee {@code open*()} methods are called in the correct order.
*
* @see Ghostryde#openEncryptor(OutputStream, PGPPublicKey)
*/
@NotThreadSafe
public static final class Encryptor extends ImprovedOutputStream {
Encryptor(@WillCloseWhenClosed OutputStream os) {
super("Encryptor", os);
}
}
/**
* Decryption {@link InputStream}.
*
* <p>This type exists to guarantee {@code open*()} methods are called in the correct order.
*
* @see Ghostryde#openDecryptor(InputStream, PGPPrivateKey)
*/
@NotThreadSafe
public static final class Decryptor extends ImprovedInputStream {
private final PGPPublicKeyEncryptedData crypt;
Decryptor(@WillCloseWhenClosed InputStream input, PGPPublicKeyEncryptedData crypt) {
super("Decryptor", input);
this.crypt = checkNotNull(crypt, "crypt");
}
/**
* Verifies that the ciphertext wasn't corrupted or tampered with.
*
* <p>Note: If {@link Ghostryde#USE_INTEGRITY_PACKET} is {@code true}, any ghostryde file
* without an integrity packet will be considered invalid and an exception will be thrown.
*
* @throws IllegalStateException to propagate {@link PGPException}
* @throws IOException
*/
@Override
protected void onClose() throws IOException {
if (USE_INTEGRITY_PACKET) {
try {
if (!crypt.verify()) {
throw new PGPException("ghostryde integrity check failed: possible tampering D:");
}
} catch (PGPException e) {
throw new IllegalStateException(e);
} }
} }
} };
} }
/** /**
* Compression {@link OutputStream}. * Creates a Ghostryde Encoder.
* *
* <p>This type exists to guarantee {@code open*()} methods are called in the correct order. * @param output where to write the encrypted data
* * @param encryptionKey the encryption key to use
* @see Ghostryde#openCompressor(Encryptor)
*/ */
@NotThreadSafe public static ImprovedOutputStream encoder(OutputStream output, PGPPublicKey encryptionKey)
public static final class Compressor extends ImprovedOutputStream { throws IOException, PGPException {
Compressor(@WillCloseWhenClosed OutputStream os) { return encoder(output, encryptionKey, null);
super("Compressor", os);
}
} }
/** /**
* Decompression {@link InputStream}. * Creates a Ghostryde decoder.
* *
* <p>This type exists to guarantee {@code open*()} methods are called in the correct order. * @param input from where to read the encrypted data
* * @param decryptionKey the decryption key to use
* @see Ghostryde#openDecompressor(Decryptor)
*/ */
@NotThreadSafe public static ImprovedInputStream decoder(InputStream input, PGPPrivateKey decryptionKey)
public static final class Decompressor extends ImprovedInputStream { throws IOException, PGPException {
Decompressor(@WillCloseWhenClosed InputStream input) {
super("Decompressor", input); // We use a Closer to handle the stream .close, to make sure it's done correctly.
} Closer closer = Closer.create();
InputStream decryptionLayer = closer.register(openDecryptor(input, decryptionKey));
InputStream decompressor = closer.register(openDecompressor(decryptionLayer));
InputStream fileLayer = closer.register(openPgpFileInputStream(decompressor));
return new ImprovedInputStream("GhostryderDecoder", fileLayer) {
@Override
public void onClose() throws IOException {
// Close all the streams we opened
closer.close();
}
};
} }
private final int bufferSize; private Ghostryde() {}
/** Constructs a new {@link Ghostryde} object. */
@Inject
public Ghostryde(
@Config("rdeGhostrydeBufferSize") int bufferSize) {
checkArgument(bufferSize > 0, "bufferSize");
this.bufferSize = bufferSize;
}
/** /**
* Opens a new {@link Encryptor} (Writing Step 1/3) * Opens a new encryptor (Writing Step 1/3)
* *
* <p>This is the first step in creating a ghostryde file. After this method, you'll want to * <p>This is the first step in creating a ghostryde file. After this method, you'll want to call
* call {@link #openCompressor(Encryptor)}. * {@link #openCompressor}.
*
* <p>TODO(b/110465985): merge with the RyDE version.
* *
* @param os is the upstream {@link OutputStream} to which the result is written. * @param os is the upstream {@link OutputStream} to which the result is written.
* @param publicKey is the public encryption key of the recipient. * @param publicKey is the public encryption key of the recipient.
@ -380,19 +303,20 @@ public final class Ghostryde {
* @throws PGPException * @throws PGPException
*/ */
@CheckReturnValue @CheckReturnValue
public Encryptor openEncryptor(@WillNotClose OutputStream os, PGPPublicKey publicKey) private static ImprovedOutputStream openEncryptor(
throws IOException, PGPException { @WillNotClose OutputStream os, PGPPublicKey publicKey) throws IOException, PGPException {
PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator( PGPEncryptedDataGenerator encryptor = new PGPEncryptedDataGenerator(
new JcePGPDataEncryptorBuilder(CIPHER) new JcePGPDataEncryptorBuilder(CIPHER)
.setWithIntegrityPacket(USE_INTEGRITY_PACKET) .setWithIntegrityPacket(USE_INTEGRITY_PACKET)
.setSecureRandom(getRandom()) .setSecureRandom(getRandom())
.setProvider(PROVIDER_NAME)); .setProvider(PROVIDER_NAME));
encryptor.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey)); encryptor.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(publicKey));
return new Encryptor(encryptor.open(os, new byte[bufferSize])); return new ImprovedOutputStream(
"GhostrydeEncryptor", encryptor.open(os, new byte[BUFFER_SIZE]));
} }
/** Does stuff. */ /** Does stuff. */
private SecureRandom getRandom() { private static SecureRandom getRandom() {
SecureRandom random; SecureRandom random;
try { try {
random = SecureRandom.getInstance(RANDOM_SOURCE); random = SecureRandom.getInstance(RANDOM_SOURCE);
@ -403,44 +327,57 @@ public final class Ghostryde {
} }
/** /**
* Opens a new {@link Compressor} (Writing Step 2/3) * Opens a new compressor (Writing Step 2/3)
* *
* <p>This is the second step in creating a ghostryde file. After this method, you'll want to * <p>This is the second step in creating a ghostryde file. After this method, you'll want to call
* call {@link #openOutput(Compressor, String, DateTime)}. * {@link #openPgpFileOutputStream}.
*
* <p>TODO(b/110465985): merge with the RyDE version.
* *
* @param os is the value returned by {@link #openEncryptor(OutputStream, PGPPublicKey)}. * @param os is the value returned by {@link #openEncryptor(OutputStream, PGPPublicKey)}.
* @throws IOException * @throws IOException
* @throws PGPException * @throws PGPException
*/ */
@CheckReturnValue @CheckReturnValue
public Compressor openCompressor(@WillNotClose Encryptor os) throws IOException, PGPException { private static ImprovedOutputStream openCompressor(@WillNotClose OutputStream os)
throws IOException, PGPException {
PGPCompressedDataGenerator kompressor = new PGPCompressedDataGenerator(COMPRESSION_ALGORITHM); PGPCompressedDataGenerator kompressor = new PGPCompressedDataGenerator(COMPRESSION_ALGORITHM);
return new Compressor(kompressor.open(os, new byte[bufferSize])); return new ImprovedOutputStream(
"GhostrydeCompressor", kompressor.open(os, new byte[BUFFER_SIZE]));
} }
/** /**
* Opens an {@link OutputStream} to which the actual data should be written (Writing Step 3/3) * Opens an {@link OutputStream} to which the actual data should be written (Writing Step 3/3)
* *
* <p>This is the third and final step in creating a ghostryde file. You'll want to write data * <p>This is the third and final step in creating a ghostryde file. You'll want to write data to
* to the returned object. * the returned object.
* *
* @param os is the value returned by {@link #openCompressor(Encryptor)}. * <p>TODO(b/110465985): merge with the RyDE version.
*
* @param os is the value returned by {@link #openCompressor}.
* @param name is a filename for your data which gets written in the literal tag. * @param name is a filename for your data which gets written in the literal tag.
* @param modified is a timestamp for your data which gets written to the literal tags. * @param modified is a timestamp for your data which gets written to the literal tags.
* @throws IOException * @throws IOException
*/ */
@CheckReturnValue @CheckReturnValue
public Output openOutput(@WillNotClose Compressor os, String name, DateTime modified) private static ImprovedOutputStream openPgpFileOutputStream(
throws IOException { @WillNotClose OutputStream os, String name, DateTime modified) throws IOException {
return new Output(new PGPLiteralDataGenerator().open( return new ImprovedOutputStream(
os, BINARY, name, modified.toDate(), new byte[bufferSize])); "GhostrydePgpFileOutput",
new PGPLiteralDataGenerator()
.open(os, BINARY, name, modified.toDate(), new byte[BUFFER_SIZE]));
} }
/** /**
* Opens a new {@link Decryptor} (Reading Step 1/3) * Opens a new decryptor (Reading Step 1/3)
* *
* <p>This is the first step in opening a ghostryde file. After this method, you'll want to * <p>This is the first step in opening a ghostryde file. After this method, you'll want to call
* call {@link #openDecompressor(Decryptor)}. * {@link #openDecompressor}.
*
* <p>Note: If {@link Ghostryde#USE_INTEGRITY_PACKET} is {@code true}, any ghostryde file without
* an integrity packet will be considered invalid and an exception will be thrown.
*
* <p>TODO(b/110465985): merge with the RyDE version.
* *
* @param input is an {@link InputStream} of the ghostryde file data. * @param input is an {@link InputStream} of the ghostryde file data.
* @param privateKey is the private encryption key of the recipient (which is us!) * @param privateKey is the private encryption key of the recipient (which is us!)
@ -448,60 +385,81 @@ public final class Ghostryde {
* @throws PGPException * @throws PGPException
*/ */
@CheckReturnValue @CheckReturnValue
public Decryptor openDecryptor(@WillNotClose InputStream input, PGPPrivateKey privateKey) private static ImprovedInputStream openDecryptor(
throws IOException, PGPException { @WillNotClose InputStream input, PGPPrivateKey privateKey) throws IOException, PGPException {
checkNotNull(privateKey, "privateKey"); checkNotNull(privateKey, "privateKey");
PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input")); PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input"));
PGPEncryptedDataList crypts = pgpCast(fact.nextObject(), PGPEncryptedDataList.class); PGPEncryptedDataList ciphertexts = pgpCast(fact.nextObject(), PGPEncryptedDataList.class);
checkState(crypts.size() > 0); checkState(ciphertexts.size() > 0);
if (crypts.size() > 1) { if (ciphertexts.size() > 1) {
logger.atWarning().log("crypts.size() is %d (should be 1)", crypts.size()); logger.atWarning().log("crypts.size() is %d (should be 1)", ciphertexts.size());
} }
PGPPublicKeyEncryptedData crypt = pgpCast(crypts.get(0), PGPPublicKeyEncryptedData.class); PGPPublicKeyEncryptedData cyphertext =
if (crypt.getKeyID() != privateKey.getKeyID()) { pgpCast(ciphertexts.get(0), PGPPublicKeyEncryptedData.class);
if (cyphertext.getKeyID() != privateKey.getKeyID()) {
throw new PGPException(String.format( throw new PGPException(String.format(
"Message was encrypted for keyid %x but ours is %x", "Message was encrypted for keyid %x but ours is %x",
crypt.getKeyID(), privateKey.getKeyID())); cyphertext.getKeyID(), privateKey.getKeyID()));
} }
return new Decryptor(
crypt.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey)), // We want an input stream that also verifies ciphertext wasn't corrupted or tampered with when
crypt); // the stream is closed.
return new ImprovedInputStream(
"GhostrydeDecryptor",
cyphertext.getDataStream(new BcPublicKeyDataDecryptorFactory(privateKey))) {
@Override
protected void onClose() throws IOException {
if (USE_INTEGRITY_PACKET) {
try {
if (!cyphertext.verify()) {
throw new PGPException("ghostryde integrity check failed: possible tampering D:");
}
} catch (PGPException e) {
throw new IllegalStateException(e);
}
}
}
};
} }
/** /**
* Opens a new {@link Decompressor} (Reading Step 2/3) * Opens a new decompressor (Reading Step 2/3)
* *
* <p>This is the second step in reading a ghostryde file. After this method, you'll want to * <p>This is the second step in reading a ghostryde file. After this method, you'll want to call
* call {@link #openInput(Decompressor)}. * {@link #openPgpFileInputStream}.
*
* <p>TODO(b/110465985): merge with the RyDE version.
* *
* @param input is the value returned by {@link #openDecryptor}. * @param input is the value returned by {@link #openDecryptor}.
* @throws IOException * @throws IOException
* @throws PGPException * @throws PGPException
*/ */
@CheckReturnValue @CheckReturnValue
public Decompressor openDecompressor(@WillNotClose Decryptor input) private static ImprovedInputStream openDecompressor(@WillNotClose InputStream input)
throws IOException, PGPException { throws IOException, PGPException {
PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input")); PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input"));
PGPCompressedData compressed = pgpCast(fact.nextObject(), PGPCompressedData.class); PGPCompressedData compressed = pgpCast(fact.nextObject(), PGPCompressedData.class);
return new Decompressor(compressed.getDataStream()); return new ImprovedInputStream("GhostrydeDecompressor", compressed.getDataStream());
} }
/** /**
* Opens a new {@link Input} for reading the original contents (Reading Step 3/3) * Opens a new decoder for reading the original contents (Reading Step 3/3)
* *
* <p>This is the final step in reading a ghostryde file. After calling this method, you should * <p>This is the final step in reading a ghostryde file. After calling this method, you should
* call the read methods on the returned {@link InputStream}. * call the read methods on the returned {@link InputStream}.
* *
* <p>TODO(b/110465985): merge with the RyDE version.
*
* @param input is the value returned by {@link #openDecompressor}. * @param input is the value returned by {@link #openDecompressor}.
* @throws IOException * @throws IOException
* @throws PGPException * @throws PGPException
*/ */
@CheckReturnValue @CheckReturnValue
public Input openInput(@WillNotClose Decompressor input) throws IOException, PGPException { private static ImprovedInputStream openPgpFileInputStream(@WillNotClose InputStream input)
throws IOException, PGPException {
PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input")); PGPObjectFactory fact = new BcPGPObjectFactory(checkNotNull(input, "input"));
PGPLiteralData literal = pgpCast(fact.nextObject(), PGPLiteralData.class); PGPLiteralData literal = pgpCast(fact.nextObject(), PGPLiteralData.class);
DateTime modified = new DateTime(literal.getModificationTime(), UTC); return new ImprovedInputStream("GhostrydePgpFileInputStream", literal.getDataStream());
return new Input(literal.getDataStream(), literal.getFileName(), modified);
} }
/** Safely extracts an object from an OpenPGP message. */ /** Safely extracts an object from an OpenPGP message. */

View file

@ -23,7 +23,6 @@ import static google.registry.request.Action.Method.POST;
import static google.registry.util.DateTimeUtils.isBeforeOrAt; import static google.registry.util.DateTimeUtils.isBeforeOrAt;
import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsFilename;
import com.google.common.flogger.FluentLogger;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import google.registry.config.RegistryConfig.Config; import google.registry.config.RegistryConfig.Config;
import google.registry.gcs.GcsUtils; import google.registry.gcs.GcsUtils;
@ -59,10 +58,7 @@ public final class RdeReportAction implements Runnable, EscrowTask {
static final String PATH = "/_dr/task/rdeReport"; static final String PATH = "/_dr/task/rdeReport";
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
@Inject GcsUtils gcsUtils; @Inject GcsUtils gcsUtils;
@Inject Ghostryde ghostryde;
@Inject EscrowTaskRunner runner; @Inject EscrowTaskRunner runner;
@Inject Response response; @Inject Response response;
@Inject RdeReporter reporter; @Inject RdeReporter reporter;
@ -101,10 +97,8 @@ public final class RdeReportAction implements Runnable, EscrowTask {
/** Reads and decrypts the XML file from cloud storage. */ /** Reads and decrypts the XML file from cloud storage. */
private byte[] readReportFromGcs(GcsFilename reportFilename) throws IOException, PGPException { private byte[] readReportFromGcs(GcsFilename reportFilename) throws IOException, PGPException {
try (InputStream gcsInput = gcsUtils.openInputStream(reportFilename); try (InputStream gcsInput = gcsUtils.openInputStream(reportFilename);
Ghostryde.Decryptor decryptor = ghostryde.openDecryptor(gcsInput, stagingDecryptionKey); InputStream ghostrydeDecoder = Ghostryde.decoder(gcsInput, stagingDecryptionKey)) {
Ghostryde.Decompressor decompressor = ghostryde.openDecompressor(decryptor); return ByteStreams.toByteArray(ghostrydeDecoder);
Ghostryde.Input xmlInput = ghostryde.openInput(decompressor)) {
return ByteStreams.toByteArray(xmlInput);
} }
} }
} }

View file

@ -23,7 +23,6 @@ import static google.registry.model.common.Cursor.getCursorTimeOrStartOfTime;
import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.xml.ValidationMode.LENIENT; import static google.registry.xml.ValidationMode.LENIENT;
import static google.registry.xml.ValidationMode.STRICT; import static google.registry.xml.ValidationMode.STRICT;
import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsFilename;
@ -74,7 +73,6 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
private final LockHandler lockHandler; private final LockHandler lockHandler;
private final int gcsBufferSize; private final int gcsBufferSize;
private final String bucket; private final String bucket;
private final int ghostrydeBufferSize;
private final Duration lockTimeout; private final Duration lockTimeout;
private final byte[] stagingKeyBytes; private final byte[] stagingKeyBytes;
private final RdeMarshaller marshaller; private final RdeMarshaller marshaller;
@ -85,7 +83,6 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
LockHandler lockHandler, LockHandler lockHandler,
@Config("gcsBufferSize") int gcsBufferSize, @Config("gcsBufferSize") int gcsBufferSize,
@Config("rdeBucket") String bucket, @Config("rdeBucket") String bucket,
@Config("rdeGhostrydeBufferSize") int ghostrydeBufferSize,
@Config("rdeStagingLockTimeout") Duration lockTimeout, @Config("rdeStagingLockTimeout") Duration lockTimeout,
@KeyModule.Key("rdeStagingEncryptionKey") byte[] stagingKeyBytes, @KeyModule.Key("rdeStagingEncryptionKey") byte[] stagingKeyBytes,
@Parameter(RdeModule.PARAM_LENIENT) boolean lenient) { @Parameter(RdeModule.PARAM_LENIENT) boolean lenient) {
@ -93,7 +90,6 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
this.lockHandler = lockHandler; this.lockHandler = lockHandler;
this.gcsBufferSize = gcsBufferSize; this.gcsBufferSize = gcsBufferSize;
this.bucket = bucket; this.bucket = bucket;
this.ghostrydeBufferSize = ghostrydeBufferSize;
this.lockTimeout = lockTimeout; this.lockTimeout = lockTimeout;
this.stagingKeyBytes = stagingKeyBytes; this.stagingKeyBytes = stagingKeyBytes;
this.marshaller = new RdeMarshaller(lenient ? LENIENT : STRICT); this.marshaller = new RdeMarshaller(lenient ? LENIENT : STRICT);
@ -119,7 +115,6 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
Security.addProvider(new BouncyCastleProvider()); Security.addProvider(new BouncyCastleProvider());
// Construct things that Dagger would inject if this wasn't serialized. // Construct things that Dagger would inject if this wasn't serialized.
Ghostryde ghostryde = new Ghostryde(ghostrydeBufferSize);
PGPPublicKey stagingKey = PgpHelper.loadPublicKeyBytes(stagingKeyBytes); PGPPublicKey stagingKey = PgpHelper.loadPublicKeyBytes(stagingKeyBytes);
GcsUtils cloudStorage = GcsUtils cloudStorage =
new GcsUtils(createGcsService(RetryParams.getDefaultInstance()), gcsBufferSize); new GcsUtils(createGcsService(RetryParams.getDefaultInstance()), gcsBufferSize);
@ -139,21 +134,25 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
prefix = "manual/" + key.directoryWithTrailingSlash() + prefix; prefix = "manual/" + key.directoryWithTrailingSlash() + prefix;
} }
GcsFilename xmlFilename = new GcsFilename(bucket, prefix + ".xml.ghostryde"); GcsFilename xmlFilename = new GcsFilename(bucket, prefix + ".xml.ghostryde");
// This file will containg the byte length (ASCII) of the raw unencrypted XML.
//
// This is necessary because RdeUploadAction creates a tar file which requires that the length
// be outputted. We don't want to have to decrypt the entire ghostryde file to determine the
// length, so we just save it separately.
GcsFilename xmlLengthFilename = new GcsFilename(bucket, prefix + ".xml.length"); GcsFilename xmlLengthFilename = new GcsFilename(bucket, prefix + ".xml.length");
GcsFilename reportFilename = new GcsFilename(bucket, prefix + "-report.xml.ghostryde"); GcsFilename reportFilename = new GcsFilename(bucket, prefix + "-report.xml.ghostryde");
// These variables will be populated as we write the deposit XML and used for other files. // These variables will be populated as we write the deposit XML and used for other files.
boolean failed = false; boolean failed = false;
long xmlLength;
XjcRdeHeader header; XjcRdeHeader header;
// Write a gigantic XML file to GCS. We'll start by opening encrypted out/err file handles. // Write a gigantic XML file to GCS. We'll start by opening encrypted out/err file handles.
logger.atInfo().log("Writing %s", xmlFilename);
logger.atInfo().log("Writing %s and %s", xmlFilename, xmlLengthFilename);
try (OutputStream gcsOutput = cloudStorage.openOutputStream(xmlFilename); try (OutputStream gcsOutput = cloudStorage.openOutputStream(xmlFilename);
Ghostryde.Encryptor encryptor = ghostryde.openEncryptor(gcsOutput, stagingKey); OutputStream lengthOutput = cloudStorage.openOutputStream(xmlLengthFilename);
Ghostryde.Compressor kompressor = ghostryde.openCompressor(encryptor); OutputStream ghostrydeEncoder = Ghostryde.encoder(gcsOutput, stagingKey, lengthOutput);
Ghostryde.Output gOutput = ghostryde.openOutput(kompressor, prefix + ".xml", watermark); Writer output = new OutputStreamWriter(ghostrydeEncoder, UTF_8)) {
Writer output = new OutputStreamWriter(gOutput, UTF_8)) {
// Output the top portion of the XML document. // Output the top portion of the XML document.
output.write(marshaller.makeHeader(id, watermark, RdeResourceType.getUris(mode), revision)); output.write(marshaller.makeHeader(id, watermark, RdeResourceType.getUris(mode), revision));
@ -182,9 +181,6 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
// Output the bottom of the XML document. // Output the bottom of the XML document.
output.write(marshaller.makeFooter()); output.write(marshaller.makeFooter());
// And we're done! How many raw XML bytes did we write?
output.flush();
xmlLength = gOutput.getBytesWritten();
} catch (IOException | PGPException e) { } catch (IOException | PGPException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -192,29 +188,14 @@ public final class RdeStagingReducer extends Reducer<PendingDeposit, DepositFrag
// If an entity was broken, abort after writing as much logs/deposit data as possible. // If an entity was broken, abort after writing as much logs/deposit data as possible.
verify(!failed, "RDE staging failed for TLD %s", tld); verify(!failed, "RDE staging failed for TLD %s", tld);
// Write a file to GCS containing the byte length (ASCII) of the raw unencrypted XML.
//
// This is necessary because RdeUploadAction creates a tar file which requires that the length
// be outputted. We don't want to have to decrypt the entire ghostryde file to determine the
// length, so we just save it separately.
logger.atInfo().log("Writing %s", xmlLengthFilename);
try (OutputStream gcsOutput = cloudStorage.openOutputStream(xmlLengthFilename)) {
gcsOutput.write(Long.toString(xmlLength).getBytes(US_ASCII));
} catch (IOException e) {
throw new RuntimeException(e);
}
// Write a tiny XML file to GCS containing some information about the deposit. // Write a tiny XML file to GCS containing some information about the deposit.
// //
// This will be sent to ICANN once we're done uploading the big XML to the escrow provider. // This will be sent to ICANN once we're done uploading the big XML to the escrow provider.
if (mode == RdeMode.FULL) { if (mode == RdeMode.FULL) {
logger.atInfo().log("Writing %s", reportFilename); logger.atInfo().log("Writing %s", reportFilename);
String innerName = prefix + "-report.xml";
try (OutputStream gcsOutput = cloudStorage.openOutputStream(reportFilename); try (OutputStream gcsOutput = cloudStorage.openOutputStream(reportFilename);
Ghostryde.Encryptor encryptor = ghostryde.openEncryptor(gcsOutput, stagingKey); OutputStream ghostrydeEncoder = Ghostryde.encoder(gcsOutput, stagingKey)) {
Ghostryde.Compressor kompressor = ghostryde.openCompressor(encryptor); counter.makeReport(id, watermark, header, revision).marshal(ghostrydeEncoder, UTF_8);
Ghostryde.Output output = ghostryde.openOutput(kompressor, innerName, watermark)) {
counter.makeReport(id, watermark, header, revision).marshal(output, UTF_8);
} catch (IOException | PGPException | XmlException e) { } catch (IOException | PGPException | XmlException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View file

@ -24,7 +24,6 @@ import static google.registry.model.ofy.ObjectifyService.ofy;
import static google.registry.model.rde.RdeMode.FULL; import static google.registry.model.rde.RdeMode.FULL;
import static google.registry.request.Action.Method.POST; import static google.registry.request.Action.Method.POST;
import static google.registry.util.DateTimeUtils.isBeforeOrAt; import static google.registry.util.DateTimeUtils.isBeforeOrAt;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import com.google.appengine.api.taskqueue.Queue; import com.google.appengine.api.taskqueue.Queue;
@ -92,7 +91,6 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
@Inject Clock clock; @Inject Clock clock;
@Inject GcsUtils gcsUtils; @Inject GcsUtils gcsUtils;
@Inject Ghostryde ghostryde;
@Inject EscrowTaskRunner runner; @Inject EscrowTaskRunner runner;
// Using Lazy<JSch> instead of JSch to prevent fetching of rdeSsh*Keys before we know we're // Using Lazy<JSch> instead of JSch to prevent fetching of rdeSsh*Keys before we know we're
@ -213,9 +211,7 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
throws Exception { throws Exception {
logger.atInfo().log("Uploading XML file '%s' to remote path '%s'.", xmlFile, uploadUrl); logger.atInfo().log("Uploading XML file '%s' to remote path '%s'.", xmlFile, uploadUrl);
try (InputStream gcsInput = gcsUtils.openInputStream(xmlFile); try (InputStream gcsInput = gcsUtils.openInputStream(xmlFile);
Ghostryde.Decryptor decryptor = ghostryde.openDecryptor(gcsInput, stagingDecryptionKey); InputStream ghostrydeDecoder = Ghostryde.decoder(gcsInput, stagingDecryptionKey)) {
Ghostryde.Decompressor decompressor = ghostryde.openDecompressor(decryptor);
Ghostryde.Input xmlInput = ghostryde.openInput(decompressor)) {
try (JSchSshSession session = jschSshSessionFactory.create(lazyJsch.get(), uploadUrl); try (JSchSshSession session = jschSshSessionFactory.create(lazyJsch.get(), uploadUrl);
JSchSftpChannel ftpChan = session.openSftpChannel()) { JSchSftpChannel ftpChan = session.openSftpChannel()) {
byte[] signature; byte[] signature;
@ -231,7 +227,7 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
OutputStream fileLayer = pgpFileFactory.create(kompressor, watermark, name + ".tar"); OutputStream fileLayer = pgpFileFactory.create(kompressor, watermark, name + ".tar");
OutputStream tarLayer = OutputStream tarLayer =
tarFactory.create(fileLayer, xmlLength, watermark, name + ".xml")) { tarFactory.create(fileLayer, xmlLength, watermark, name + ".xml")) {
ByteStreams.copy(xmlInput, tarLayer); ByteStreams.copy(ghostrydeDecoder, tarLayer);
} }
signature = signer.getSignature(); signature = signer.getSignature();
logger.atInfo().log("uploaded %,d bytes: %s.ryde", signer.getBytesWritten(), name); logger.atInfo().log("uploaded %,d bytes: %s.ryde", signer.getBytesWritten(), name);
@ -247,7 +243,7 @@ public final class RdeUploadAction implements Runnable, EscrowTask {
/** Reads the contents of a file from Cloud Storage that contains nothing but an integer. */ /** Reads the contents of a file from Cloud Storage that contains nothing but an integer. */
private long readXmlLength(GcsFilename xmlLengthFilename) throws IOException { private long readXmlLength(GcsFilename xmlLengthFilename) throws IOException {
try (InputStream input = gcsUtils.openInputStream(xmlLengthFilename)) { try (InputStream input = gcsUtils.openInputStream(xmlLengthFilename)) {
return Long.parseLong(new String(ByteStreams.toByteArray(input), UTF_8).trim()); return Ghostryde.readLength(input);
} }
} }

View file

@ -16,7 +16,6 @@ package google.registry.tools;
import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import static org.joda.time.DateTimeZone.UTC;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
@ -31,13 +30,11 @@ import java.io.OutputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Provider; import javax.inject.Provider;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey; import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.joda.time.DateTime;
/** Command to encrypt/decrypt {@code .ghostryde} files. */ /** Command to encrypt/decrypt {@code .ghostryde} files. */
@Parameters(separators = " =", commandDescription = "Encrypt/decrypt a ghostryde file.") @Parameters(separators = " =", commandDescription = "Encrypt/decrypt a ghostryde file.")
@ -61,16 +58,14 @@ final class GhostrydeCommand implements RemoteApiCommand {
@Parameter( @Parameter(
names = {"-o", "--output"}, names = {"-o", "--output"},
description = "Output file. If this is a directory, then in --encrypt mode, the output " description =
+ "filename will be the input filename with '.ghostryde' appended, and in --decrypt " "Output file. If this is a directory: (a) in --encrypt mode, the output "
+ "mode, the output filename will be determined based on the name stored within the " + "filename will be the input filename with '.ghostryde' appended, and will have an "
+ "archive.", + "extra '<filename>.length' file with the original file's length; (b) In --decrypt "
+ "mode, the output filename will be the input filename with '.decrypt' appended.",
validateWith = PathParameter.class) validateWith = PathParameter.class)
private Path output = Paths.get("/dev/stdout"); private Path output = Paths.get("/dev/stdout");
@Inject
Ghostryde ghostryde;
@Inject @Inject
@Key("rdeStagingEncryptionKey") @Key("rdeStagingEncryptionKey")
Provider<PGPPublicKey> rdeStagingEncryptionKey; Provider<PGPPublicKey> rdeStagingEncryptionKey;
@ -93,30 +88,23 @@ final class GhostrydeCommand implements RemoteApiCommand {
Path outFile = Files.isDirectory(output) Path outFile = Files.isDirectory(output)
? output.resolve(input.getFileName() + ".ghostryde") ? output.resolve(input.getFileName() + ".ghostryde")
: output; : output;
Path lenOutFile =
Files.isDirectory(output) ? output.resolve(input.getFileName() + ".length") : null;
try (OutputStream out = Files.newOutputStream(outFile); try (OutputStream out = Files.newOutputStream(outFile);
Ghostryde.Encryptor encryptor = OutputStream lenOut = lenOutFile == null ? null : Files.newOutputStream(lenOutFile);
ghostryde.openEncryptor(out, rdeStagingEncryptionKey.get()); OutputStream ghostrydeEncoder =
Ghostryde.Compressor kompressor = ghostryde.openCompressor(encryptor); Ghostryde.encoder(out, rdeStagingEncryptionKey.get(), lenOut);
Ghostryde.Output ghostOutput =
ghostryde.openOutput(kompressor, input.getFileName().toString(),
new DateTime(Files.getLastModifiedTime(input).toMillis(), UTC));
InputStream in = Files.newInputStream(input)) { InputStream in = Files.newInputStream(input)) {
ByteStreams.copy(in, ghostOutput); ByteStreams.copy(in, ghostrydeEncoder);
} }
} }
private void runDecrypt() throws IOException, PGPException { private void runDecrypt() throws IOException, PGPException {
try (InputStream in = Files.newInputStream(input); try (InputStream in = Files.newInputStream(input);
Ghostryde.Decryptor decryptor = InputStream ghostDecoder = Ghostryde.decoder(in, rdeStagingDecryptionKey.get())) {
ghostryde.openDecryptor(in, rdeStagingDecryptionKey.get()); Path outFile =
Ghostryde.Decompressor decompressor = ghostryde.openDecompressor(decryptor); Files.isDirectory(output) ? output.resolve(input.getFileName() + ".decrypt") : output;
Ghostryde.Input ghostInput = ghostryde.openInput(decompressor)) { Files.copy(ghostDecoder, outFile, REPLACE_EXISTING);
Path outFile = Files.isDirectory(output)
? output.resolve(ghostInput.getName())
: output;
Files.copy(ghostInput, outFile, REPLACE_EXISTING);
Files.setLastModifiedTime(outFile,
FileTime.fromMillis(ghostInput.getModified().getMillis()));
} }
} }
} }

View file

@ -65,13 +65,10 @@ final class ValidateEscrowDepositCommand implements Command {
@Override @Override
public void run() throws Exception { public void run() throws Exception {
if (input.toString().endsWith(".ghostryde")) { if (input.toString().endsWith(".ghostryde")) {
Ghostryde ghostryde = new Ghostryde(64 * 1024);
try (InputStream in = Files.newInputStream(input); try (InputStream in = Files.newInputStream(input);
Ghostryde.Decryptor decryptor = InputStream ghostrydeDecoder =
ghostryde.openDecryptor(in, keyring.getRdeStagingDecryptionKey()); Ghostryde.decoder(in, keyring.getRdeStagingDecryptionKey())) {
Ghostryde.Decompressor decompressor = ghostryde.openDecompressor(decryptor); validateXmlStream(ghostrydeDecoder);
Ghostryde.Input ghostInput = ghostryde.openInput(decompressor)) {
validateXmlStream(ghostInput);
} }
} else { } else {
try (InputStream inputStream = Files.newInputStream(input)) { try (InputStream inputStream = Files.newInputStream(input)) {

View file

@ -19,7 +19,6 @@ import static com.google.common.truth.Truth.assertWithMessage;
import static google.registry.testing.GcsTestingUtils.readGcsFile; import static google.registry.testing.GcsTestingUtils.readGcsFile;
import static google.registry.testing.SystemInfo.hasCommand; import static google.registry.testing.SystemInfo.hasCommand;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.joda.time.DateTimeZone.UTC;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
import com.google.appengine.tools.cloudstorage.GcsFilename; import com.google.appengine.tools.cloudstorage.GcsFilename;
@ -101,7 +100,6 @@ public class BrdaCopyActionTest extends ShardableTestCase {
@Before @Before
public void before() throws Exception { public void before() throws Exception {
action.gcsUtils = gcsUtils; action.gcsUtils = gcsUtils;
action.ghostryde = new Ghostryde(23);
action.pgpCompressionFactory = new RydePgpCompressionOutputStreamFactory(() -> 1024); action.pgpCompressionFactory = new RydePgpCompressionOutputStreamFactory(() -> 1024);
action.pgpEncryptionFactory = new RydePgpEncryptionOutputStreamFactory(() -> 1024); action.pgpEncryptionFactory = new RydePgpEncryptionOutputStreamFactory(() -> 1024);
action.pgpFileFactory = new RydePgpFileOutputStreamFactory(() -> 1024); action.pgpFileFactory = new RydePgpFileOutputStreamFactory(() -> 1024);
@ -116,8 +114,7 @@ public class BrdaCopyActionTest extends ShardableTestCase {
action.stagingDecryptionKey = decryptKey; action.stagingDecryptionKey = decryptKey;
byte[] xml = DEPOSIT_XML.read(); byte[] xml = DEPOSIT_XML.read();
GcsTestingUtils.writeGcsFile(gcsService, STAGE_FILE, GcsTestingUtils.writeGcsFile(gcsService, STAGE_FILE, Ghostryde.encode(xml, encryptKey));
Ghostryde.encode(xml, encryptKey, "lobster.xml", new DateTime(UTC)));
GcsTestingUtils.writeGcsFile(gcsService, STAGE_LENGTH_FILE, GcsTestingUtils.writeGcsFile(gcsService, STAGE_LENGTH_FILE,
Long.toString(xml.length).getBytes(UTF_8)); Long.toString(xml.length).getBytes(UTF_8));
} }

View file

@ -34,7 +34,6 @@ import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStream; import java.io.OutputStream;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.joda.time.DateTime;
import org.junit.Rule; import org.junit.Rule;
import org.junit.experimental.theories.DataPoints; import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theories;
@ -61,18 +60,6 @@ public class GhostrydeGpgIntegrationTest extends ShardableTestCase {
new GpgCommand("gpg2"), new GpgCommand("gpg2"),
}; };
@DataPoints
public static BufferSize[] bufferSizes = new BufferSize[] {
new BufferSize(1),
new BufferSize(7),
};
@DataPoints
public static Filename[] filenames = new Filename[] {
new Filename("lol.txt"),
// new Filename("(◕‿◕).txt"), // gpg displays this with zany hex characters.
};
@DataPoints @DataPoints
public static Content[] contents = new Content[] { public static Content[] contents = new Content[] {
new Content("(◕‿◕)"), new Content("(◕‿◕)"),
@ -82,21 +69,16 @@ public class GhostrydeGpgIntegrationTest extends ShardableTestCase {
}; };
@Theory @Theory
public void test(GpgCommand cmd, BufferSize bufferSize, Filename filename, Content content) public void test(GpgCommand cmd, Content content) throws Exception {
throws Exception {
assumeTrue(hasCommand(cmd.get() + " --version")); assumeTrue(hasCommand(cmd.get() + " --version"));
Keyring keyring = new FakeKeyringModule().get(); Keyring keyring = new FakeKeyringModule().get();
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey(); PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
File file = new File(gpg.getCwd(), "love.gpg"); File file = new File(gpg.getCwd(), "love.gpg");
byte[] data = content.get().getBytes(UTF_8); byte[] data = content.get().getBytes(UTF_8);
DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z");
Ghostryde ghost = new Ghostryde(bufferSize.get());
try (OutputStream output = new FileOutputStream(file); try (OutputStream output = new FileOutputStream(file);
Ghostryde.Encryptor encryptor = ghost.openEncryptor(output, publicKey); OutputStream ghostrydeEncoder = Ghostryde.encoder(output, publicKey)) {
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); ghostrydeEncoder.write(data);
OutputStream os = ghost.openOutput(kompressor, filename.get(), mtime)) {
os.write(data);
} }
Process pid = gpg.exec(cmd.get(), "--list-packets", "--keyid-format", "long", file.getPath()); Process pid = gpg.exec(cmd.get(), "--list-packets", "--keyid-format", "long", file.getPath());
@ -106,13 +88,13 @@ public class GhostrydeGpgIntegrationTest extends ShardableTestCase {
assertThat(stdout).contains(":compressed packet:"); assertThat(stdout).contains(":compressed packet:");
assertThat(stdout).contains(":encrypted data packet:"); assertThat(stdout).contains(":encrypted data packet:");
assertThat(stdout).contains("version 3, algo 1, keyid A59C132F3589A1D5"); assertThat(stdout).contains("version 3, algo 1, keyid A59C132F3589A1D5");
assertThat(stdout).contains("name=\"" + filename.get() + "\""); assertThat(stdout).contains("name=\"" + Ghostryde.INNER_FILENAME + "\"");
assertThat(stderr).contains("encrypted with 2048-bit RSA key, ID A59C132F3589A1D5"); assertThat(stderr).contains("encrypted with 2048-bit RSA key, ID A59C132F3589A1D5");
pid = gpg.exec(cmd.get(), "--use-embedded-filename", file.getPath()); pid = gpg.exec(cmd.get(), "--use-embedded-filename", file.getPath());
stderr = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8)); stderr = CharStreams.toString(new InputStreamReader(pid.getErrorStream(), UTF_8));
assertWithMessage(stderr).that(pid.waitFor()).isEqualTo(0); assertWithMessage(stderr).that(pid.waitFor()).isEqualTo(0);
File dataFile = new File(gpg.getCwd(), filename.get()); File dataFile = new File(gpg.getCwd(), Ghostryde.INNER_FILENAME);
assertThat(dataFile.exists()).isTrue(); assertThat(dataFile.exists()).isTrue();
assertThat(slurp(dataFile)).isEqualTo(content.get()); assertThat(slurp(dataFile)).isEqualTo(content.get());
} }
@ -133,30 +115,6 @@ public class GhostrydeGpgIntegrationTest extends ShardableTestCase {
} }
} }
private static class BufferSize {
private final int value;
BufferSize(int value) {
this.value = value;
}
int get() {
return value;
}
}
private static class Filename {
private final String value;
Filename(String value) {
this.value = value;
}
String get() {
return value;
}
}
private static class Content { private static class Content {
private final String value; private final String value;

View file

@ -26,17 +26,17 @@ import static org.junit.Assume.assumeThat;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import google.registry.keyring.api.Keyring; import google.registry.keyring.api.Keyring;
import google.registry.rde.Ghostryde.DecodeResult;
import google.registry.testing.BouncyCastleProviderRule; import google.registry.testing.BouncyCastleProviderRule;
import google.registry.testing.FakeKeyringModule; import google.registry.testing.FakeKeyringModule;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Base64;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPKeyPair; import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPPrivateKey; import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey; import org.bouncycastle.openpgp.PGPPublicKey;
import org.joda.time.DateTime;
import org.junit.Ignore; import org.junit.Ignore;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -53,18 +53,6 @@ public class GhostrydeTest {
@Rule @Rule
public final BouncyCastleProviderRule bouncy = new BouncyCastleProviderRule(); public final BouncyCastleProviderRule bouncy = new BouncyCastleProviderRule();
@DataPoints
public static BufferSize[] bufferSizes = new BufferSize[] {
new BufferSize(1),
new BufferSize(7),
};
@DataPoints
public static Filename[] filenames = new Filename[] {
new Filename("lol.txt"),
// new Filename("(◕‿◕).txt"), // gpg displays this with zany hex characters.
};
@DataPoints @DataPoints
public static Content[] contents = new Content[] { public static Content[] contents = new Content[] {
new Content("hi"), new Content("hi"),
@ -75,46 +63,34 @@ public class GhostrydeTest {
}; };
@Theory @Theory
public void testSimpleApi(Filename filename, Content content) throws Exception { public void testSimpleApi(Content content) throws Exception {
Keyring keyring = new FakeKeyringModule().get(); Keyring keyring = new FakeKeyringModule().get();
byte[] data = content.get().getBytes(UTF_8); byte[] data = content.get().getBytes(UTF_8);
DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z");
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey(); PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey(); PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
byte[] blob = Ghostryde.encode(data, publicKey, filename.get(), mtime); byte[] blob = Ghostryde.encode(data, publicKey);
DecodeResult result = Ghostryde.decode(blob, privateKey); byte[] result = Ghostryde.decode(blob, privateKey);
assertThat(result.getName()).isEqualTo(filename.get()); assertThat(new String(result, UTF_8)).isEqualTo(content.get());
assertThat(result.getModified()).isEqualTo(mtime);
assertThat(new String(result.getData(), UTF_8)).isEqualTo(content.get());
} }
@Theory @Theory
public void testStreamingApi(BufferSize bufferSize, Filename filename, Content content) public void testStreamingApi(Content content) throws Exception {
throws Exception {
Keyring keyring = new FakeKeyringModule().get(); Keyring keyring = new FakeKeyringModule().get();
byte[] data = content.get().getBytes(UTF_8); byte[] data = content.get().getBytes(UTF_8);
DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z");
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey(); PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey(); PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
Ghostryde ghost = new Ghostryde(bufferSize.get());
ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey); try (OutputStream encoder = Ghostryde.encoder(bsOut, publicKey)) {
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); encoder.write(data);
OutputStream output = ghost.openOutput(kompressor, filename.get(), mtime)) {
output.write(data);
} }
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray()); ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
bsOut.reset(); bsOut.reset();
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey); try (InputStream decoder = Ghostryde.decoder(bsIn, privateKey)) {
Ghostryde.Decompressor decompressor = ghost.openDecompressor(decryptor); ByteStreams.copy(decoder, bsOut);
Ghostryde.Input input = ghost.openInput(decompressor)) {
assertThat(input.getName()).isEqualTo(filename.get());
assertThat(input.getModified()).isEqualTo(mtime);
ByteStreams.copy(input, bsOut);
} }
assertThat(bsOut.size()).isEqualTo(data.length); assertThat(bsOut.size()).isEqualTo(data.length);
@ -122,51 +98,20 @@ public class GhostrydeTest {
} }
@Theory @Theory
public void testEncryptOnly(Content content) throws Exception { public void testStreamingApi_withSize(Content content) throws Exception {
Keyring keyring = new FakeKeyringModule().get(); Keyring keyring = new FakeKeyringModule().get();
byte[] data = content.get().getBytes(UTF_8); byte[] data = content.get().getBytes(UTF_8);
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey(); PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey)) { ByteArrayOutputStream lenOut = new ByteArrayOutputStream();
encryptor.write(data); try (OutputStream encoder = Ghostryde.encoder(bsOut, publicKey, lenOut)) {
encoder.write(data);
} }
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray()); assertThat(Ghostryde.readLength(new ByteArrayInputStream(lenOut.toByteArray())))
bsOut.reset(); .isEqualTo(data.length);
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) { assertThat(Long.parseLong(new String(lenOut.toByteArray(), UTF_8))).isEqualTo(data.length);
ByteStreams.copy(decryptor, bsOut);
}
assertThat(new String(bsOut.toByteArray(), UTF_8)).isEqualTo(content.get());
}
@Theory
public void testEncryptCompressOnly(Content content) throws Exception {
Keyring keyring = new FakeKeyringModule().get();
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
byte[] data = content.get().getBytes(UTF_8);
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey);
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor)) {
kompressor.write(data);
}
assertThat(new String(bsOut.toByteArray(), UTF_8)).isNotEqualTo(content.get());
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
bsOut.reset();
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey);
Ghostryde.Decompressor decompressor = ghost.openDecompressor(decryptor)) {
ByteStreams.copy(decompressor, bsOut);
}
assertThat(new String(bsOut.toByteArray(), UTF_8)).isEqualTo(content.get());
} }
@Theory @Theory
@ -177,26 +122,22 @@ public class GhostrydeTest {
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey(); PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey(); PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
byte[] data = content.get().getBytes(UTF_8); byte[] data = content.get().getBytes(UTF_8);
DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z");
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey); try (OutputStream encoder = Ghostryde.encoder(bsOut, publicKey)) {
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); encoder.write(data);
OutputStream output = ghost.openOutput(kompressor, "lol", mtime)) {
output.write(data);
} }
byte[] ciphertext = bsOut.toByteArray(); byte[] ciphertext = bsOut.toByteArray();
korruption(ciphertext, ciphertext.length / 2); korruption(ciphertext, ciphertext.length - 1);
ByteArrayInputStream bsIn = new ByteArrayInputStream(ciphertext); ByteArrayInputStream bsIn = new ByteArrayInputStream(ciphertext);
IllegalStateException thrown = IllegalStateException thrown =
assertThrows( assertThrows(
IllegalStateException.class, IllegalStateException.class,
() -> { () -> {
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) { try (InputStream decoder = Ghostryde.decoder(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream()); ByteStreams.copy(decoder, ByteStreams.nullOutputStream());
} }
}); });
assertThat(thrown).hasMessageThat().contains("tampering"); assertThat(thrown).hasMessageThat().contains("tampering");
@ -210,14 +151,10 @@ public class GhostrydeTest {
PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey(); PGPPublicKey publicKey = keyring.getRdeStagingEncryptionKey();
PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey(); PGPPrivateKey privateKey = keyring.getRdeStagingDecryptionKey();
byte[] data = content.get().getBytes(UTF_8); byte[] data = content.get().getBytes(UTF_8);
DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z");
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey); try (OutputStream encoder = Ghostryde.encoder(bsOut, publicKey)) {
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); encoder.write(data);
OutputStream output = ghost.openOutput(kompressor, "lol", mtime)) {
output.write(data);
} }
byte[] ciphertext = bsOut.toByteArray(); byte[] ciphertext = bsOut.toByteArray();
@ -227,28 +164,58 @@ public class GhostrydeTest {
assertThrows( assertThrows(
PGPException.class, PGPException.class,
() -> { () -> {
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) { try (InputStream decoder = Ghostryde.decoder(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream()); ByteStreams.copy(decoder, ByteStreams.nullOutputStream());
} }
}); });
} }
@Test
public void testFullEncryption() throws Exception {
// Check that the full encryption hasn't changed. All the other tests check that encrypting and
// decrypting results in the original data, but not whether the encryption method has changed.
FakeKeyringModule keyringModule = new FakeKeyringModule();
PGPKeyPair dsa = keyringModule.get("rde-unittest@registry.test", ENCRYPT);
PGPPrivateKey privateKey = dsa.getPrivateKey();
// Encryption is inconsistent because it uses a random state. But decryption is consistent!
//
// If the encryption has legitimately changed - uncomment the following code, and copy the new
// encryptedInputBase64 from the test error:
//
// assertThat(
// Base64.getMimeEncoder()
// .encodeToString(
// Ghostryde.encode("Some data!!!111!!!".getBytes(UTF_8), dsa.getPublicKey())))
// .isEqualTo("expect error");
String encryptedInputBase64 =
" hQEMA6WcEy81iaHVAQgAnn9bS6IOCTW2uZnITPWH8zIYr6K7YJslv38c4YU5eQqVhHC5PN0NhM2l\n"
+ " i89U3lUE6gp3DdEEbTbugwXCHWyRL4fYTlpiHZjBn2vZdSS21EAG+q1XuTaD8DTjkC2G060/sW6i\n"
+ " 0gSIkksqgubbSVZTxHEqh92tv35KCqiYc52hjKZIIGI8FHhpJOtDa3bhMMad8nrMy3vbv5LiYNh5\n"
+ " j3DUCFhskU8Ldi1vBfXIonqUNLBrD/R471VVJyQ3NoGQTVUF9uXLoy+2dL0oBLc1Avj1XNP5PQ08\n"
+ " MWlqmezkLdY0oHnQqTHYhYDxRo/Sw7xO1GLwWR11rcx/IAJloJbKSHTFeNJUAcKFnKvPDwBk3nnr\n"
+ " uR505HtOj/tZDT5weVjhrlnmWXzaBRmYASy6PXZu6KzTbPUQTf4JeeJWdyw7glLMr2WPdMVPGZ8e\n"
+ " gcFAjSJZjZlqohZyBUpP\n";
byte[] result =
Ghostryde.decode(Base64.getMimeDecoder().decode(encryptedInputBase64), privateKey);
assertThat(new String(result, UTF_8)).isEqualTo("Some data!!!111!!!");
}
@Test @Test
public void testFailure_keyMismatch() throws Exception { public void testFailure_keyMismatch() throws Exception {
FakeKeyringModule keyringModule = new FakeKeyringModule(); FakeKeyringModule keyringModule = new FakeKeyringModule();
byte[] data = "Fanatics have their dreams, wherewith they weave.".getBytes(UTF_8); byte[] data = "Fanatics have their dreams, wherewith they weave.".getBytes(UTF_8);
DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z");
PGPKeyPair dsa1 = keyringModule.get("rde-unittest@registry.test", ENCRYPT); PGPKeyPair dsa1 = keyringModule.get("rde-unittest@registry.test", ENCRYPT);
PGPKeyPair dsa2 = keyringModule.get("rde-unittest-dsa@registry.test", ENCRYPT); PGPKeyPair dsa2 = keyringModule.get("rde-unittest-dsa@registry.test", ENCRYPT);
PGPPublicKey publicKey = dsa1.getPublicKey(); PGPPublicKey publicKey = dsa1.getPublicKey();
PGPPrivateKey privateKey = dsa2.getPrivateKey(); PGPPrivateKey privateKey = dsa2.getPrivateKey();
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey); try (OutputStream encoder = Ghostryde.encoder(bsOut, publicKey)) {
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); encoder.write(data);
OutputStream output = ghost.openOutput(kompressor, "lol", mtime)) {
output.write(data);
} }
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray()); ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
@ -256,8 +223,8 @@ public class GhostrydeTest {
assertThrows( assertThrows(
PGPException.class, PGPException.class,
() -> { () -> {
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) { try (InputStream decoder = Ghostryde.decoder(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream()); ByteStreams.copy(decoder, ByteStreams.nullOutputStream());
} }
}); });
assertThat(thrown) assertThat(thrown)
@ -270,7 +237,6 @@ public class GhostrydeTest {
public void testFailure_keyCorruption() throws Exception { public void testFailure_keyCorruption() throws Exception {
FakeKeyringModule keyringModule = new FakeKeyringModule(); FakeKeyringModule keyringModule = new FakeKeyringModule();
byte[] data = "Fanatics have their dreams, wherewith they weave.".getBytes(UTF_8); byte[] data = "Fanatics have their dreams, wherewith they weave.".getBytes(UTF_8);
DateTime mtime = DateTime.parse("1984-12-18T00:30:00Z");
PGPKeyPair rsa = keyringModule.get("rde-unittest@registry.test", ENCRYPT); PGPKeyPair rsa = keyringModule.get("rde-unittest@registry.test", ENCRYPT);
PGPPublicKey publicKey = rsa.getPublicKey(); PGPPublicKey publicKey = rsa.getPublicKey();
@ -282,17 +248,14 @@ public class GhostrydeTest {
rsa.getPrivateKey().getPublicKeyPacket(), rsa.getPrivateKey().getPublicKeyPacket(),
rsa.getPrivateKey().getPrivateKeyDataPacket()); rsa.getPrivateKey().getPrivateKeyDataPacket());
Ghostryde ghost = new Ghostryde(1024);
ByteArrayOutputStream bsOut = new ByteArrayOutputStream(); ByteArrayOutputStream bsOut = new ByteArrayOutputStream();
try (Ghostryde.Encryptor encryptor = ghost.openEncryptor(bsOut, publicKey); try (OutputStream encoder = Ghostryde.encoder(bsOut, publicKey)) {
Ghostryde.Compressor kompressor = ghost.openCompressor(encryptor); encoder.write(data);
OutputStream output = ghost.openOutput(kompressor, "lol", mtime)) {
output.write(data);
} }
ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray()); ByteArrayInputStream bsIn = new ByteArrayInputStream(bsOut.toByteArray());
try (Ghostryde.Decryptor decryptor = ghost.openDecryptor(bsIn, privateKey)) { try (InputStream decoder = Ghostryde.decoder(bsIn, privateKey)) {
ByteStreams.copy(decryptor, ByteStreams.nullOutputStream()); ByteStreams.copy(decoder, ByteStreams.nullOutputStream());
} }
} }
@ -304,30 +267,6 @@ public class GhostrydeTest {
} }
} }
private static class BufferSize {
private final int value;
BufferSize(int value) {
this.value = value;
}
int get() {
return value;
}
}
private static class Filename {
private final String value;
Filename(String value) {
this.value = value;
}
String get() {
return value;
}
}
private static class Content { private static class Content {
private final String value; private final String value;

View file

@ -26,7 +26,6 @@ import static google.registry.testing.GcsTestingUtils.writeGcsFile;
import static google.registry.testing.JUnitBackports.assertThrows; import static google.registry.testing.JUnitBackports.assertThrows;
import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST; import static javax.servlet.http.HttpServletResponse.SC_BAD_REQUEST;
import static javax.servlet.http.HttpServletResponse.SC_OK; import static javax.servlet.http.HttpServletResponse.SC_OK;
import static org.joda.time.DateTimeZone.UTC;
import static org.joda.time.Duration.standardDays; import static org.joda.time.Duration.standardDays;
import static org.joda.time.Duration.standardSeconds; import static org.joda.time.Duration.standardSeconds;
import static org.mockito.Matchers.any; import static org.mockito.Matchers.any;
@ -105,7 +104,6 @@ public class RdeReportActionTest {
reporter.retrier = new Retrier(new FakeSleeper(new FakeClock()), 3); reporter.retrier = new Retrier(new FakeSleeper(new FakeClock()), 3);
RdeReportAction action = new RdeReportAction(); RdeReportAction action = new RdeReportAction();
action.gcsUtils = new GcsUtils(gcsService, 1024); action.gcsUtils = new GcsUtils(gcsService, 1024);
action.ghostryde = new Ghostryde(1024);
action.response = response; action.response = response;
action.bucket = "tub"; action.bucket = "tub";
action.tld = "test"; action.tld = "test";
@ -125,10 +123,7 @@ public class RdeReportActionTest {
Cursor.create(RDE_REPORT, DateTime.parse("2006-06-06TZ"), Registry.get("test"))); Cursor.create(RDE_REPORT, DateTime.parse("2006-06-06TZ"), Registry.get("test")));
persistResource( persistResource(
Cursor.create(RDE_UPLOAD, DateTime.parse("2006-06-07TZ"), Registry.get("test"))); Cursor.create(RDE_UPLOAD, DateTime.parse("2006-06-07TZ"), Registry.get("test")));
writeGcsFile( writeGcsFile(gcsService, reportFile, Ghostryde.encode(REPORT_XML.read(), encryptKey));
gcsService,
reportFile,
Ghostryde.encode(REPORT_XML.read(), encryptKey, "darkside.xml", DateTime.now(UTC)));
} }
@Test @Test

View file

@ -136,7 +136,6 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
new FakeLockHandler(true), new FakeLockHandler(true),
0, // gcsBufferSize 0, // gcsBufferSize
"rde-bucket", // bucket "rde-bucket", // bucket
31337, // ghostrydeBufferSize
Duration.standardHours(1), // lockTimeout Duration.standardHours(1), // lockTimeout
PgpHelper.convertPublicKeyToBytes(encryptKey), // stagingKeyBytes PgpHelper.convertPublicKeyToBytes(encryptKey), // stagingKeyBytes
false); // lenient false); // lenient
@ -330,9 +329,9 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.run(); action.run();
executeTasksUntilEmpty("mapreduce", clock); executeTasksUntilEmpty("mapreduce", clock);
XjcRdeDeposit deposit = unmarshal( XjcRdeDeposit deposit =
XjcRdeDeposit.class, unmarshal(
Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey).getData()); XjcRdeDeposit.class, Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey));
XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit); XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit);
assertThat(header.getTld()).isEqualTo("lol"); assertThat(header.getTld()).isEqualTo("lol");
@ -361,9 +360,9 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.run(); action.run();
executeTasksUntilEmpty("mapreduce", clock); executeTasksUntilEmpty("mapreduce", clock);
XjcRdeDeposit deposit = unmarshal( XjcRdeDeposit deposit =
XjcRdeDeposit.class, unmarshal(
Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey).getData()); XjcRdeDeposit.class, Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey));
assertThat(deposit.getType()).isEqualTo(XjcRdeDepositTypeType.FULL); assertThat(deposit.getType()).isEqualTo(XjcRdeDepositTypeType.FULL);
assertThat(deposit.getId()).isEqualTo(RdeUtil.timestampToId(DateTime.parse("2000-01-01TZ"))); assertThat(deposit.getId()).isEqualTo(RdeUtil.timestampToId(DateTime.parse("2000-01-01TZ")));
assertThat(deposit.getWatermark()).isEqualTo(DateTime.parse("2000-01-01TZ")); assertThat(deposit.getWatermark()).isEqualTo(DateTime.parse("2000-01-01TZ"));
@ -403,9 +402,9 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.run(); action.run();
executeTasksUntilEmpty("mapreduce", clock); executeTasksUntilEmpty("mapreduce", clock);
XjcRdeDeposit deposit = unmarshal( XjcRdeDeposit deposit =
XjcRdeDeposit.class, unmarshal(
Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey).getData()); XjcRdeDeposit.class, Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey));
XjcRdeRegistrar registrar1 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit); XjcRdeRegistrar registrar1 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit);
XjcRdeRegistrar registrar2 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit); XjcRdeRegistrar registrar2 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit);
XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit); XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit);
@ -492,9 +491,9 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
for (GcsFilename filename : asList( for (GcsFilename filename : asList(
new GcsFilename("rde-bucket", "fop_1971-01-01_full_S1_R0.xml.ghostryde"), new GcsFilename("rde-bucket", "fop_1971-01-01_full_S1_R0.xml.ghostryde"),
new GcsFilename("rde-bucket", "fop_1971-01-05_thin_S1_R0.xml.ghostryde"))) { new GcsFilename("rde-bucket", "fop_1971-01-05_thin_S1_R0.xml.ghostryde"))) {
XjcRdeDeposit deposit = unmarshal( XjcRdeDeposit deposit =
XjcRdeDeposit.class, unmarshal(
Ghostryde.decode(readGcsFile(gcsService, filename), decryptKey).getData()); XjcRdeDeposit.class, Ghostryde.decode(readGcsFile(gcsService, filename), decryptKey));
XjcRdeRegistrar registrar1 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit); XjcRdeRegistrar registrar1 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit);
XjcRdeRegistrar registrar2 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit); XjcRdeRegistrar registrar2 = extractAndRemoveContentWithType(XjcRdeRegistrar.class, deposit);
XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit); XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit);
@ -524,9 +523,9 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
executeTasksUntilEmpty("mapreduce", clock); executeTasksUntilEmpty("mapreduce", clock);
GcsFilename filename = new GcsFilename("rde-bucket", "fop_2000-01-01_full_S1_R0.xml.ghostryde"); GcsFilename filename = new GcsFilename("rde-bucket", "fop_2000-01-01_full_S1_R0.xml.ghostryde");
XjcRdeDeposit deposit = unmarshal( XjcRdeDeposit deposit =
XjcRdeDeposit.class, unmarshal(
Ghostryde.decode(readGcsFile(gcsService, filename), decryptKey).getData()); XjcRdeDeposit.class, Ghostryde.decode(readGcsFile(gcsService, filename), decryptKey));
XjcRdeDomain domain = extractAndRemoveContentWithType(XjcRdeDomain.class, deposit); XjcRdeDomain domain = extractAndRemoveContentWithType(XjcRdeDomain.class, deposit);
XjcRdeIdn firstIdn = extractAndRemoveContentWithType(XjcRdeIdn.class, deposit); XjcRdeIdn firstIdn = extractAndRemoveContentWithType(XjcRdeIdn.class, deposit);
XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit); XjcRdeHeader header = extractAndRemoveContentWithType(XjcRdeHeader.class, deposit);
@ -566,7 +565,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
action.run(); action.run();
executeTasksUntilEmpty("mapreduce", clock); executeTasksUntilEmpty("mapreduce", clock);
byte[] deposit = Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey).getData(); byte[] deposit = Ghostryde.decode(readGcsFile(gcsService, XML_FILE), decryptKey);
assertThat(Integer.parseInt(new String(readGcsFile(gcsService, LENGTH_FILE), UTF_8))) assertThat(Integer.parseInt(new String(readGcsFile(gcsService, LENGTH_FILE), UTF_8)))
.isEqualTo(deposit.length); .isEqualTo(deposit.length);
} }
@ -818,7 +817,7 @@ public class RdeStagingActionTest extends MapreduceTestCase<RdeStagingAction> {
private String readXml(String objectName) throws IOException, PGPException { private String readXml(String objectName) throws IOException, PGPException {
GcsFilename file = new GcsFilename("rde-bucket", objectName); GcsFilename file = new GcsFilename("rde-bucket", objectName);
return new String(Ghostryde.decode(readGcsFile(gcsService, file), decryptKey).getData(), UTF_8); return new String(Ghostryde.decode(readGcsFile(gcsService, file), decryptKey), UTF_8);
} }
private <T extends XjcRdeContentType> private <T extends XjcRdeContentType>

View file

@ -187,7 +187,6 @@ public class RdeUploadActionTest {
RdeUploadAction action = new RdeUploadAction(); RdeUploadAction action = new RdeUploadAction();
action.clock = clock; action.clock = clock;
action.gcsUtils = new GcsUtils(gcsService, BUFFER_SIZE); action.gcsUtils = new GcsUtils(gcsService, BUFFER_SIZE);
action.ghostryde = new Ghostryde(BUFFER_SIZE);
action.lazyJsch = action.lazyJsch =
() -> () ->
JSchModule.provideJSch( JSchModule.provideJSch(
@ -239,18 +238,12 @@ public class RdeUploadActionTest {
createTld("tld"); createTld("tld");
PGPPublicKey encryptKey = new FakeKeyringModule().get().getRdeStagingEncryptionKey(); PGPPublicKey encryptKey = new FakeKeyringModule().get().getRdeStagingEncryptionKey();
writeGcsFile(gcsService, GHOSTRYDE_FILE, writeGcsFile(gcsService, GHOSTRYDE_FILE, Ghostryde.encode(DEPOSIT_XML.read(), encryptKey));
Ghostryde.encode(DEPOSIT_XML.read(), encryptKey, "lobster.xml", clock.nowUtc())); writeGcsFile(gcsService, GHOSTRYDE_R1_FILE, Ghostryde.encode(DEPOSIT_XML.read(), encryptKey));
writeGcsFile(gcsService, GHOSTRYDE_R1_FILE, writeGcsFile(gcsService, LENGTH_FILE, Long.toString(DEPOSIT_XML.size()).getBytes(UTF_8));
Ghostryde.encode(DEPOSIT_XML.read(), encryptKey, "lobster.xml", clock.nowUtc())); writeGcsFile(gcsService, LENGTH_R1_FILE, Long.toString(DEPOSIT_XML.size()).getBytes(UTF_8));
writeGcsFile(gcsService, LENGTH_FILE, writeGcsFile(gcsService, REPORT_FILE, Ghostryde.encode(REPORT_XML.read(), encryptKey));
Long.toString(DEPOSIT_XML.size()).getBytes(UTF_8)); writeGcsFile(gcsService, REPORT_R1_FILE, Ghostryde.encode(REPORT_XML.read(), encryptKey));
writeGcsFile(gcsService, LENGTH_R1_FILE,
Long.toString(DEPOSIT_XML.size()).getBytes(UTF_8));
writeGcsFile(gcsService, REPORT_FILE,
Ghostryde.encode(REPORT_XML.read(), encryptKey, "dieform.xml", clock.nowUtc()));
writeGcsFile(gcsService, REPORT_R1_FILE,
Ghostryde.encode(REPORT_XML.read(), encryptKey, "dieform.xml", clock.nowUtc()));
ofy() ofy()
.transact( .transact(
() -> { () -> {

View file

@ -19,15 +19,12 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import google.registry.keyring.api.Keyring; import google.registry.keyring.api.Keyring;
import google.registry.rde.Ghostryde; import google.registry.rde.Ghostryde;
import google.registry.rde.Ghostryde.DecodeResult;
import google.registry.testing.BouncyCastleProviderRule; import google.registry.testing.BouncyCastleProviderRule;
import google.registry.testing.FakeKeyringModule; import google.registry.testing.FakeKeyringModule;
import google.registry.testing.InjectRule; import google.registry.testing.InjectRule;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.attribute.FileTime;
import org.joda.time.DateTime;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
@ -35,7 +32,6 @@ import org.junit.Test;
/** Unit tests for {@link GhostrydeCommand}. */ /** Unit tests for {@link GhostrydeCommand}. */
public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> { public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
private static final DateTime MODIFIED_TIME = DateTime.parse("1984-12-18T04:20:00Z");
private static final byte[] SONG_BY_CHRISTINA_ROSSETTI = ("" private static final byte[] SONG_BY_CHRISTINA_ROSSETTI = (""
+ "When I am dead, my dearest, \n" + "When I am dead, my dearest, \n"
+ " Sing no sad songs for me; \n" + " Sing no sad songs for me; \n"
@ -66,7 +62,6 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
@Before @Before
public void before() { public void before() {
keyring = new FakeKeyringModule().get(); keyring = new FakeKeyringModule().get();
command.ghostryde = new Ghostryde(1024);
command.rdeStagingDecryptionKey = keyring::getRdeStagingDecryptionKey; command.rdeStagingDecryptionKey = keyring::getRdeStagingDecryptionKey;
command.rdeStagingEncryptionKey = keyring::getRdeStagingEncryptionKey; command.rdeStagingEncryptionKey = keyring::getRdeStagingEncryptionKey;
} }
@ -76,14 +71,10 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
Path inFile = Paths.get(tmpDir.newFile("atrain.txt").toString()); Path inFile = Paths.get(tmpDir.newFile("atrain.txt").toString());
Path outFile = Paths.get(tmpDir.newFile().toString()); Path outFile = Paths.get(tmpDir.newFile().toString());
Files.write(inFile, SONG_BY_CHRISTINA_ROSSETTI); Files.write(inFile, SONG_BY_CHRISTINA_ROSSETTI);
Files.setLastModifiedTime(inFile, FileTime.fromMillis(MODIFIED_TIME.getMillis()));
runCommand("--encrypt", "--input=" + inFile, "--output=" + outFile); runCommand("--encrypt", "--input=" + inFile, "--output=" + outFile);
DecodeResult decoded = Ghostryde.decode( byte[] decoded =
Files.readAllBytes(outFile), Ghostryde.decode(Files.readAllBytes(outFile), keyring.getRdeStagingDecryptionKey());
keyring.getRdeStagingDecryptionKey()); assertThat(decoded).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(decoded.getData()).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(decoded.getName()).isEqualTo("atrain.txt");
assertThat(decoded.getModified()).isEqualTo(MODIFIED_TIME);
} }
@Test @Test
@ -91,45 +82,34 @@ public class GhostrydeCommandTest extends CommandTestCase<GhostrydeCommand> {
Path inFile = Paths.get(tmpDir.newFile("atrain.txt").toString()); Path inFile = Paths.get(tmpDir.newFile("atrain.txt").toString());
Path outDir = Paths.get(tmpDir.newFolder().toString()); Path outDir = Paths.get(tmpDir.newFolder().toString());
Files.write(inFile, SONG_BY_CHRISTINA_ROSSETTI); Files.write(inFile, SONG_BY_CHRISTINA_ROSSETTI);
Files.setLastModifiedTime(inFile, FileTime.fromMillis(MODIFIED_TIME.getMillis()));
runCommand("--encrypt", "--input=" + inFile, "--output=" + outDir); runCommand("--encrypt", "--input=" + inFile, "--output=" + outDir);
Path lenOutFile = outDir.resolve("atrain.txt.length");
assertThat(Ghostryde.readLength(Files.newInputStream(lenOutFile)))
.isEqualTo(SONG_BY_CHRISTINA_ROSSETTI.length);
Path outFile = outDir.resolve("atrain.txt.ghostryde"); Path outFile = outDir.resolve("atrain.txt.ghostryde");
DecodeResult decoded = Ghostryde.decode( byte[] decoded =
Files.readAllBytes(outFile), Ghostryde.decode(Files.readAllBytes(outFile), keyring.getRdeStagingDecryptionKey());
keyring.getRdeStagingDecryptionKey()); assertThat(decoded).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(decoded.getData()).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(decoded.getName()).isEqualTo("atrain.txt");
assertThat(decoded.getModified()).isEqualTo(MODIFIED_TIME);
} }
@Test @Test
public void testDecrypt_outputIsAFile_writesToFile() throws Exception { public void testDecrypt_outputIsAFile_writesToFile() throws Exception {
Path inFile = Paths.get(tmpDir.newFile().toString()); Path inFile = Paths.get(tmpDir.newFile().toString());
Path outFile = Paths.get(tmpDir.newFile().toString()); Path outFile = Paths.get(tmpDir.newFile().toString());
Files.write(inFile, Ghostryde.encode( Files.write(
SONG_BY_CHRISTINA_ROSSETTI, inFile, Ghostryde.encode(SONG_BY_CHRISTINA_ROSSETTI, keyring.getRdeStagingEncryptionKey()));
keyring.getRdeStagingEncryptionKey(),
"atrain.txt",
MODIFIED_TIME));
runCommand("--decrypt", "--input=" + inFile, "--output=" + outFile); runCommand("--decrypt", "--input=" + inFile, "--output=" + outFile);
assertThat(Files.readAllBytes(outFile)).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI); assertThat(Files.readAllBytes(outFile)).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(Files.getLastModifiedTime(outFile))
.isEqualTo(FileTime.fromMillis(MODIFIED_TIME.getMillis()));
} }
@Test @Test
public void testDecrypt_outputIsADirectory_writesToFileFromInnerName() throws Exception { public void testDecrypt_outputIsADirectory_AppendsDecryptExtension() throws Exception {
Path inFile = Paths.get(tmpDir.newFile().toString()); Path inFile = Paths.get(tmpDir.newFolder().toString()).resolve("atrain.ghostryde");
Path outDir = Paths.get(tmpDir.newFolder().toString()); Path outDir = Paths.get(tmpDir.newFolder().toString());
Files.write(inFile, Ghostryde.encode( Files.write(
SONG_BY_CHRISTINA_ROSSETTI, inFile, Ghostryde.encode(SONG_BY_CHRISTINA_ROSSETTI, keyring.getRdeStagingEncryptionKey()));
keyring.getRdeStagingEncryptionKey(),
"atrain.txt",
MODIFIED_TIME));
runCommand("--decrypt", "--input=" + inFile, "--output=" + outDir); runCommand("--decrypt", "--input=" + inFile, "--output=" + outDir);
Path outFile = outDir.resolve("atrain.txt"); Path outFile = outDir.resolve("atrain.ghostryde.decrypt");
assertThat(Files.readAllBytes(outFile)).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI); assertThat(Files.readAllBytes(outFile)).isEqualTo(SONG_BY_CHRISTINA_ROSSETTI);
assertThat(Files.getLastModifiedTime(outFile))
.isEqualTo(FileTime.fromMillis(MODIFIED_TIME.getMillis()));
} }
} }