[
https://issues.apache.org/jira/browse/QPID-8504?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17283825#comment-17283825
]
ASF GitHub Bot commented on QPID-8504:
--------------------------------------
Dedeepya-T commented on a change in pull request #81:
URL: https://github.com/apache/qpid-broker-j/pull/81#discussion_r575359249
##########
File path:
broker-core/src/main/java/org/apache/qpid/server/security/encryption/AESGCMKeyFileEncrypter.java
##########
@@ -0,0 +1,118 @@
+/*
+ * 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.qpid.server.security.encryption;
+
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.util.Base64;
+
+import javax.crypto.Cipher;
+import javax.crypto.CipherInputStream;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.GCMParameterSpec;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+import org.apache.qpid.server.plugin.PluggableService;
+import org.apache.qpid.server.util.Strings;
+
+class AESGCMKeyFileEncrypter implements ConfigurationSecretEncrypter
+{
+ private static final String CIPHER_NAME = "AES/GCM/NoPadding";
+ private static final int GCM_INITIALIZATION_VECTOR_LENGTH = 12;
+ private static final int GCM_TAG_LENGTH = 128;
+ private static final String AES_ALGORITHM = "AES";
+ private final SecureRandom _random = new SecureRandom();
+ private final SecretKey _secretKey;
+
+ AESGCMKeyFileEncrypter(SecretKey secretKey)
+ {
+ if(secretKey == null)
+ {
+ throw new NullPointerException("A non null secret key must be
supplied");
+ }
+ if(!AES_ALGORITHM.equals(secretKey.getAlgorithm()))
+ {
+ throw new IllegalArgumentException("Provided secret key was for
the algorithm: " + secretKey.getAlgorithm()
+ + " when " + AES_ALGORITHM + "
was needed.");
+ }
+ _secretKey = secretKey;
+ }
+
+ @Override
+ public String encrypt(final String unencrypted)
+ {
+ byte[] unencryptedBytes = unencrypted.getBytes(StandardCharsets.UTF_8);
+ try
+ {
+ byte[] ivbytes = new byte[GCM_INITIALIZATION_VECTOR_LENGTH];
+ _random.nextBytes(ivbytes);
+ Cipher cipher = Cipher.getInstance(CIPHER_NAME);
+ GCMParameterSpec gcmParameterSpec = new
GCMParameterSpec(GCM_TAG_LENGTH, ivbytes);
+ cipher.init(Cipher.ENCRYPT_MODE, _secretKey, gcmParameterSpec);
+ byte[] encryptedBytes =
EncryptionHelper.readFromCipherStream(unencryptedBytes, cipher);
+ byte[] output = new byte[GCM_INITIALIZATION_VECTOR_LENGTH +
encryptedBytes.length];
+ System.arraycopy(ivbytes, 0, output, 0,
GCM_INITIALIZATION_VECTOR_LENGTH);
+ System.arraycopy(encryptedBytes, 0, output,
GCM_INITIALIZATION_VECTOR_LENGTH, encryptedBytes.length);
+ return Base64.getEncoder().encodeToString(output);
+ }
+ catch (IOException | InvalidAlgorithmParameterException |
InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e)
+ {
+ throw new IllegalArgumentException("Unable to encrypt secret", e);
+ }
+ }
+
+ @Override
+ public String decrypt(final String encrypted)
+ {
+ if (!EncryptionHelper.isValidBase64(encrypted))
+ {
+ throw new IllegalArgumentException("Encrypted value is not valid
Base 64 data: '" + encrypted + "'");
+ }
+ byte[] encryptedBytes = Strings.decodeBase64(encrypted);
+ try
+ {
+ Cipher cipher = Cipher.getInstance(CIPHER_NAME);
+ byte[] ivbytes = new byte[GCM_INITIALIZATION_VECTOR_LENGTH];
+ if(encryptedBytes.length > 0)
Review comment:
This check was not present in original recommendation , however in unit
testing there was an issue with the length of encrypted for empty/null
encrypted bytes. IMHO throwing the exception would be better instead of
comparing it with IV length
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Usage of default mode for "AES" is insecure
> -------------------------------------------
>
> Key: QPID-8504
> URL: https://issues.apache.org/jira/browse/QPID-8504
> Project: Qpid
> Issue Type: Improvement
> Reporter: Md Mahir Asef Kabir
> Priority: Major
>
> In file
> https://github.com/apache/qpid-broker-j/blob/a70ed6f5edbcf0e8690447d48a1fe64e599cb703/broker-core/src/main/java/org/apache/qpid/server/security/encryption/AESKeyFileEncrypter.java
> (at Line 55), the default "AES" algorithm has been used which imposes
> insecure "ECB" mode.
> *Security Impact*:
> ECB mode allows the attacker to do the following -
> detect whether two ECB-encrypted messages are identical;
> detect whether two ECB-encrypted messages share a common prefix;
> detect whether two ECB-encrypted messages share other common substrings, as
> long as those substrings are aligned at block boundaries; or
> detect whether (and where) a single ECB-encrypted message contains repetitive
> data (such as long runs of spaces or null bytes, repeated header fields, or
> coincidentally repeated phrases in the text). - Collected from
> [here|https://crypto.stackexchange.com/questions/20941/why-shouldnt-i-use-ecb-encryption#:~:text=The%20main%20reason%20not%20to,will%20leak%20to%20some%20extent).]
> *Useful Resources*:
> https://blog.filippo.io/the-ecb-penguin/
> *Solution we suggest*:
> Use GCM mode instead of default or ECB mode.
> *Please share with us your opinions/comments if there is any*:
> Is the bug report helpful?
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]