Skip to content

Refactor messages by subclasses #1621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 46 additions & 25 deletions adminapi/src/main/java/io/minio/admin/Crypto.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package io.minio.admin;

import io.minio.errors.MinioException;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.IOException;
Expand Down Expand Up @@ -156,22 +157,25 @@ private static byte[] generateKey(byte[] secret, byte[] salt) {
}

private static byte[] generateEncryptDecryptAdditionalData(
boolean encryptFlag, int aeadId, byte[] key, byte[] paddedNonce)
throws InvalidCipherTextException {
AEADCipher cipher = getEncryptCipher(aeadId, key, paddedNonce);
int outputLength = cipher.getMac().length;
byte[] additionalData = new byte[outputLength];
cipher.doFinal(additionalData, 0);
return appendBytes(new byte[] {0}, additionalData);
boolean encryptFlag, int aeadId, byte[] key, byte[] paddedNonce) throws MinioException {
try {
AEADCipher cipher = getEncryptCipher(aeadId, key, paddedNonce);
int outputLength = cipher.getMac().length;
byte[] additionalData = new byte[outputLength];
cipher.doFinal(additionalData, 0);
return appendBytes(new byte[] {0}, additionalData);
} catch (InvalidCipherTextException e) {
throw new MinioException(e);
}
}

private static byte[] generateEncryptAdditionalData(int aeadId, byte[] key, byte[] paddedNonce)
throws InvalidCipherTextException {
throws MinioException {
return generateEncryptDecryptAdditionalData(true, aeadId, key, paddedNonce);
}

private static byte[] generateDecryptAdditionalData(int aeadId, byte[] key, byte[] paddedNonce)
throws InvalidCipherTextException {
throws MinioException {
return generateEncryptDecryptAdditionalData(false, aeadId, key, paddedNonce);
}

Expand All @@ -190,7 +194,7 @@ private static byte[] updateNonceId(byte[] nonce, int idx) {
}

/** Encrypt data payload. */
public static byte[] encrypt(byte[] payload, String password) throws InvalidCipherTextException {
public static byte[] encrypt(byte[] payload, String password) throws MinioException {
byte[] nonce = random(NONCE_LENGTH);
byte[] salt = random(SALT_LENGTH);

Expand Down Expand Up @@ -219,7 +223,11 @@ public static byte[] encrypt(byte[] payload, String password) throws InvalidCiph
int outputLength = cipher.getOutputSize(chunk.length);
byte[] encryptedData = new byte[outputLength];
int outputOffset = cipher.processBytes(chunk, 0, chunk.length, encryptedData, 0);
cipher.doFinal(encryptedData, outputOffset);
try {
cipher.doFinal(encryptedData, outputOffset);
} catch (InvalidCipherTextException e) {
throw new MinioException(e);
}

result = appendBytes(result, encryptedData);

Expand All @@ -243,20 +251,24 @@ public static class DecryptReader {
private byte[] oneByte = null;
private boolean eof = false;

public DecryptReader(InputStream inputStream, byte[] secret)
throws EOFException, IOException, InvalidCipherTextException {
public DecryptReader(InputStream inputStream, byte[] secret) throws MinioException {
this.inputStream = inputStream;
this.secret = secret;
readFully(this.inputStream, this.salt, true);
readFully(this.inputStream, this.aeadId, true);
readFully(this.inputStream, this.nonce, true);
try {
readFully(this.inputStream, this.salt, true);
readFully(this.inputStream, this.aeadId, true);
readFully(this.inputStream, this.nonce, true);
} catch (EOFException e) {
throw new MinioException(e);
} catch (IOException e) {
throw new MinioException(e);
}
this.key = generateKey(this.secret, this.salt);
byte[] paddedNonce = appendBytes(this.nonce, new byte[] {0, 0, 0, 0});
this.additionalData = generateDecryptAdditionalData(this.aeadId[0], this.key, paddedNonce);
}

private byte[] decrypt(byte[] encryptedData, boolean lastChunk)
throws InvalidCipherTextException {
private byte[] decrypt(byte[] encryptedData, boolean lastChunk) throws MinioException {
this.count++;
if (lastChunk) {
this.additionalData = markAsLast(this.additionalData);
Expand All @@ -268,12 +280,16 @@ private byte[] decrypt(byte[] encryptedData, boolean lastChunk)
byte[] decryptedData = new byte[outputLength];
int outputOffset =
cipher.processBytes(encryptedData, 0, encryptedData.length, decryptedData, 0);
cipher.doFinal(decryptedData, outputOffset);
try {
cipher.doFinal(decryptedData, outputOffset);
} catch (InvalidCipherTextException e) {
throw new MinioException(e);
}
return decryptedData;
}

/** Read a chunk at least one byte more than chunk size. */
private byte[] readChunk() throws IOException {
private byte[] readChunk() throws EOFException, IOException {
if (this.eof) {
return new byte[] {};
}
Expand Down Expand Up @@ -302,19 +318,24 @@ private byte[] readChunk() throws IOException {
return baos.toByteArray();
}

public byte[] readAllBytes() throws IOException, InvalidCipherTextException {
public byte[] readAllBytes() throws MinioException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while (!this.eof) {
byte[] payload = this.readChunk();
baos.write(this.decrypt(payload, this.eof));
try {
byte[] payload = this.readChunk();
baos.write(this.decrypt(payload, this.eof));
} catch (EOFException e) {
throw new MinioException(e);
} catch (IOException e) {
throw new MinioException(e);
}
}
return baos.toByteArray();
}
}

/** Decrypt data stream. */
public static byte[] decrypt(InputStream inputStream, String password)
throws EOFException, IOException, InvalidCipherTextException {
public static byte[] decrypt(InputStream inputStream, String password) throws MinioException {
DecryptReader reader =
new DecryptReader(inputStream, password.getBytes(StandardCharsets.UTF_8));
return reader.readAllBytes();
Expand Down
Loading
Loading