Github user alopresto commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/201#discussion_r51640371
  
    --- Diff: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/crypto/OpenSSLPKCS5CipherProvider.java
 ---
    @@ -0,0 +1,211 @@
    +/*
    + * 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.processors.standard.util.crypto;
    +
    +import org.apache.commons.lang3.StringUtils;
    +import org.apache.nifi.processor.exception.ProcessException;
    +import org.apache.nifi.security.util.EncryptionMethod;
    +import org.apache.nifi.stream.io.StreamUtils;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import javax.crypto.Cipher;
    +import javax.crypto.NoSuchPaddingException;
    +import javax.crypto.SecretKey;
    +import javax.crypto.SecretKeyFactory;
    +import javax.crypto.spec.PBEKeySpec;
    +import javax.crypto.spec.PBEParameterSpec;
    +import java.io.IOException;
    +import java.io.InputStream;
    +import java.io.OutputStream;
    +import java.nio.charset.StandardCharsets;
    +import java.security.InvalidAlgorithmParameterException;
    +import java.security.InvalidKeyException;
    +import java.security.NoSuchAlgorithmException;
    +import java.security.NoSuchProviderException;
    +import java.security.SecureRandom;
    +import java.security.spec.InvalidKeySpecException;
    +import java.util.Arrays;
    +
    +public class OpenSSLPKCS5CipherProvider implements PBECipherProvider {
    +    private static final Logger logger = 
LoggerFactory.getLogger(OpenSSLPKCS5CipherProvider.class);
    +
    +    // Legacy magic number value
    +    private static final int ITERATION_COUNT = 0;
    +    private static final int DEFAULT_SALT_LENGTH = 8;
    +    private static final byte[] EMPTY_SALT = new byte[8];
    +
    +    private static final String OPENSSL_EVP_HEADER_MARKER = "Salted__";
    +    private static final int OPENSSL_EVP_HEADER_SIZE = 8;
    +
    +    /**
    +     * Returns an initialized cipher for the specified algorithm. The key 
(and IV if necessary) are derived using the
    +     * <a 
href="https://www.openssl.org/docs/manmaster/crypto/EVP_BytesToKey.html";>OpenSSL
 EVP_BytesToKey proprietary KDF</a> [essentially {@code MD5(password || salt) 
}].
    +     *
    +     * @param encryptionMethod the {@link EncryptionMethod}
    +     * @param password         the secret input
    +     * @param keyLength        the desired key length in bits (ignored 
because OpenSSL ciphers provide key length in algorithm name)
    +     * @param encryptMode      true for encrypt, false for decrypt
    +     * @return the initialized cipher
    +     * @throws Exception if there is a problem initializing the cipher
    +     */
    +    @Override
    +    public Cipher getCipher(EncryptionMethod encryptionMethod, String 
password, int keyLength, boolean encryptMode) throws Exception {
    +        return getCipher(encryptionMethod, password, new byte[0], 
keyLength, encryptMode);
    +    }
    +
    +    /**
    +     * Returns an initialized cipher for the specified algorithm. The key 
(and IV if necessary) are derived using the
    +     * <a 
href="https://www.openssl.org/docs/manmaster/crypto/EVP_BytesToKey.html";>OpenSSL
 EVP_BytesToKey proprietary KDF</a> [essentially {@code MD5(password || salt) 
}].
    +     *
    +     * @param encryptionMethod the {@link EncryptionMethod}
    +     * @param password         the secret input
    +     * @param salt             the salt
    +     * @param keyLength        the desired key length in bits (ignored 
because OpenSSL ciphers provide key length in algorithm name)
    +     * @param encryptMode      true for encrypt, false for decrypt
    +     * @return the initialized cipher
    +     * @throws Exception if there is a problem initializing the cipher
    +     */
    +    @Override
    +    public Cipher getCipher(EncryptionMethod encryptionMethod, String 
password, byte[] salt, int keyLength, boolean encryptMode) throws Exception {
    +        try {
    +            return getInitializedCipher(encryptionMethod, password, salt, 
encryptMode);
    +        } catch (IllegalArgumentException e) {
    --- End diff --
    
    During unit testing I was treating these differently. I guess in a 
production application the validation is not done any earlier, so it may not 
provide any value. I just figured that with crypto stuff, so much of it is 
opaque to a user that if I could separate out "you need to provide a good value 
for _X_" vs. "_magic_ went wrong" exceptions, that would be helpful. 


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to