Github user alopresto commented on a diff in the pull request:
https://github.com/apache/nifi/pull/1686#discussion_r112980940
--- Diff:
nifi-commons/nifi-data-provenance-utils/src/main/java/org/apache/nifi/provenance/CryptoUtils.java
---
@@ -0,0 +1,228 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.provenance;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
+import java.util.Base64;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Pattern;
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.nifi.security.util.EncryptionMethod;
+import org.apache.nifi.security.util.crypto.AESKeyedCipherProvider;
+import org.apache.nifi.util.NiFiProperties;
+import org.bouncycastle.util.encoders.Hex;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class CryptoUtils {
+ private static final Logger logger =
LoggerFactory.getLogger(StaticKeyProvider.class);
+ private static final String STATIC_KEY_PROVIDER_CLASS_NAME =
"org.apache.nifi.provenance.StaticKeyProvider";
+ private static final String FILE_BASED_KEY_PROVIDER_CLASS_NAME =
"org.apache.nifi.provenance.FileBasedKeyProvider";
+ private static final Pattern HEX_PATTERN =
Pattern.compile("(?i)^[0-9a-f]+$");
+
+ public static final int IV_LENGTH = 16;
+
+ public static boolean isUnlimitedStrengthCryptoAvailable() {
+ try {
+ return Cipher.getMaxAllowedKeyLength("AES") > 128;
+ } catch (NoSuchAlgorithmException e) {
+ logger.warn("Tried to determine if unlimited strength crypto
is available but the AES algorithm is not available");
+ return false;
+ }
+ }
+
+ /**
+ * Utility method which returns true if the string is null, empty, or
entirely whitespace.
+ *
+ * @param src the string to evaluate
+ * @return true if empty
+ */
+ public static boolean isEmpty(String src) {
+ return src == null || src.trim().isEmpty();
+ }
+
+ /**
+ * Concatenates multiple byte[] into a single byte[]. Because it uses
a {@link ByteArrayOutputStream}
+ * rather than {@link System#arraycopy(Object, int, Object, int, int)}
the performance is much better
+ * with an arbitrary number of input byte[]s.
+ *
+ * @param arrays the component byte[] in order
+ * @return a concatenated byte[]
+ * @throws IOException this should never be thrown
+ */
+ public static byte[] concatByteArrays(byte[]... arrays) throws
IOException {
+ ByteArrayOutputStream boas = new ByteArrayOutputStream();
+ for (byte[] arr : arrays) {
+ boas.write(arr);
+ }
+ return boas.toByteArray();
+ }
+
+ public static boolean isValidKeyProvider(String
keyProviderImplementation, String keyProviderLocation, String keyId, String
encryptionKeyHex) {
+ if
(STATIC_KEY_PROVIDER_CLASS_NAME.equals(keyProviderImplementation)) {
+ // Ensure the keyId and key are valid
+ return keyIsValid(encryptionKeyHex) &&
StringUtils.isNotEmpty(keyId);
+ } else if
(FILE_BASED_KEY_PROVIDER_CLASS_NAME.equals(keyProviderImplementation)) {
+ // Ensure the file can be read and the keyId is populated
(does not read file to validate)
+ final File kpf = new File(keyProviderLocation);
+ return kpf.exists() && kpf.canRead() &&
StringUtils.isNotEmpty(keyId);
+ } else {
+ logger.error("The attempt to validate the key provider failed
keyProviderImplementation = "
+ + keyProviderImplementation + " , keyProviderLocation
= "
+ + keyProviderLocation + " , keyId = "
+ + keyId + " , encryptedKeyHex = "
+ + (StringUtils.isNotEmpty(encryptionKeyHex) ?
"********" : ""));
+
+ return false;
+ }
+ }
+
+ /**
+ * Returns true if the provided key is valid hex and is the correct
length for the current system's JCE policies.
+ *
+ * @param encryptionKeyHex the key in hexadecimal
+ * @return true if this key is valid
+ */
+ public static boolean keyIsValid(String encryptionKeyHex) {
+ return isHexString(encryptionKeyHex)
+ && (isUnlimitedStrengthCryptoAvailable()
+ ? Arrays.asList(32, 48,
64).contains(encryptionKeyHex.length())
--- End diff --
Will do.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---