Skip to content

Commit

Permalink
DRILL-6705: Fix various failures in Crypto / Network / Phonetic funct…
Browse files Browse the repository at this point in the history
…ions when invalid input is given

1. aes_decrypt / aes_ecrypt - moved cyper init part into eval method since it not a constant and can be different for each input
2. double_metaphone - fixed NPE when given string is empty
3. in_network / address_count / broadcast_address / netmask / low_address / high_address / - fixed IllegalArgumentException in case of invalid input
4. is_private_ip / inet_aton - fixed ArrayIndexOutOfBoundsException / NumberFormatException in case of invalid input
5. is_valid_IP / is_valid_IPv4 / is_valid_IPv6 - removed unnecessary checks
6. Added appropriate unit tests

closes #1443
  • Loading branch information
arina-ielchiieva authored and Ben-Zvi committed Sep 8, 2018
1 parent ea83672 commit f88a73c
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 178 deletions.
Expand Up @@ -22,10 +22,8 @@
import org.apache.drill.exec.expr.annotations.FunctionTemplate;
import org.apache.drill.exec.expr.annotations.Output;
import org.apache.drill.exec.expr.annotations.Param;
import org.apache.drill.exec.expr.annotations.Workspace;
import org.apache.drill.exec.expr.holders.VarCharHolder;

import javax.crypto.Cipher;
import javax.inject.Inject;

public class CryptoFunctions {
Expand Down Expand Up @@ -271,34 +269,25 @@ public static class AESEncryptFunction implements DrillSimpleFunc {
@Inject
DrillBuf buffer;

@Workspace
Cipher cipher;

@Override
public void setup() {
String key = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(rawKey.start, rawKey.end, rawKey.buffer);

try {
byte[] keyByteArray = key.getBytes("UTF-8");
java.security.MessageDigest sha = java.security.MessageDigest.getInstance("SHA-1");
keyByteArray = sha.digest(keyByteArray);
keyByteArray = java.util.Arrays.copyOf(keyByteArray, 16);
javax.crypto.spec.SecretKeySpec secretKey = new javax.crypto.spec.SecretKeySpec(keyByteArray, "AES");

cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
} catch (Exception e) {
//Exceptions are ignored
}
}

@Override
public void eval() {

String key = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(rawKey.start, rawKey.end, rawKey.buffer);
String input = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(rawInput.start, rawInput.end, rawInput.buffer);
String encryptedText = "";
try {
encryptedText = javax.xml.bind.DatatypeConverter.printBase64Binary(cipher.doFinal(input.getBytes("UTF-8")));
byte[] keyByteArray = key.getBytes(java.nio.charset.StandardCharsets.UTF_8);
java.security.MessageDigest sha = java.security.MessageDigest.getInstance("SHA-1");
keyByteArray = sha.digest(keyByteArray);
keyByteArray = java.util.Arrays.copyOf(keyByteArray, 16);
javax.crypto.spec.SecretKeySpec secretKey = new javax.crypto.spec.SecretKeySpec(keyByteArray, "AES");

javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(javax.crypto.Cipher.ENCRYPT_MODE, secretKey);
encryptedText = javax.xml.bind.DatatypeConverter.printBase64Binary(cipher.doFinal(input.getBytes(java.nio.charset.StandardCharsets.UTF_8)));
} catch (Exception e) {
//Exceptions are ignored
}
Expand Down Expand Up @@ -331,33 +320,24 @@ public static class AESDecryptFunction implements DrillSimpleFunc {
@Inject
DrillBuf buffer;

@Workspace
Cipher cipher;

@Override
public void setup() {
String key = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(rawKey.start, rawKey.end, rawKey.buffer);

try {
byte[] keyByteArray = key.getBytes("UTF-8");
java.security.MessageDigest sha = java.security.MessageDigest.getInstance("SHA-1");
keyByteArray = sha.digest(keyByteArray);
keyByteArray = java.util.Arrays.copyOf(keyByteArray, 16);
javax.crypto.spec.SecretKeySpec secretKey = new javax.crypto.spec.SecretKeySpec(keyByteArray, "AES");

cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, secretKey);
} catch (Exception e) {
//Exceptions are ignored
}
}

@Override
public void eval() {

String key = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(rawKey.start, rawKey.end, rawKey.buffer);
String input = org.apache.drill.exec.expr.fn.impl.StringFunctionHelpers.toStringFromUTF8(rawInput.start, rawInput.end, rawInput.buffer);
String decryptedText = "";
try {
byte[] keyByteArray = key.getBytes(java.nio.charset.StandardCharsets.UTF_8);
java.security.MessageDigest sha = java.security.MessageDigest.getInstance("SHA-1");
keyByteArray = sha.digest(keyByteArray);
keyByteArray = java.util.Arrays.copyOf(keyByteArray, 16);
javax.crypto.spec.SecretKeySpec secretKey = new javax.crypto.spec.SecretKeySpec(keyByteArray, "AES");

javax.crypto.Cipher cipher = javax.crypto.Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(javax.crypto.Cipher.DECRYPT_MODE, secretKey);
decryptedText = new String(cipher.doFinal(javax.xml.bind.DatatypeConverter.parseBase64Binary(input)));
} catch (Exception e) {
//Exceptions are ignored
Expand Down

0 comments on commit f88a73c

Please sign in to comment.