You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by gg...@apache.org on 2023/07/16 13:24:57 UTC

[mina] branch 2.1.X updated (398fafc9a -> 32854791a)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch 2.1.X
in repository https://gitbox.apache.org/repos/asf/mina.git


    from 398fafc9a [maven-release-plugin] prepare for next development iteration
     new 0a7872f44 Use stock JRE Charset instead of magic string
     new 32854791a Use stock JRE Charset instead of magic string

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../mina/proxy/handlers/http/ntlm/NTLMResponses.java     |  9 +++++++--
 .../mina/proxy/handlers/socks/Socks4LogicHandler.java    |  5 +++--
 .../mina/proxy/handlers/socks/Socks5LogicHandler.java    | 16 ++++++----------
 .../java/org/apache/mina/proxy/utils/ByteUtilities.java  |  9 +++++----
 .../org/apache/mina/proxy/utils/StringUtilities.java     |  3 ++-
 .../mina/transport/AbstractTrafficControlTest.java       |  3 ++-
 .../mina/filter/compression/CompressionFilterTest.java   |  6 ++++--
 .../org/apache/mina/filter/compression/ZlibTest.java     | 11 ++++++-----
 8 files changed, 35 insertions(+), 27 deletions(-)


[mina] 01/02: Use stock JRE Charset instead of magic string

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 2.1.X
in repository https://gitbox.apache.org/repos/asf/mina.git

commit 0a7872f445cdd0aee5e4d26ce360e5cc965710d8
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 16 09:01:00 2023 -0400

    Use stock JRE Charset instead of magic string
    
    Throw GeneralSecurityException instead of Exception in private method
---
 .../org/apache/mina/proxy/handlers/http/ntlm/NTLMResponses.java  | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/mina-core/src/main/java/org/apache/mina/proxy/handlers/http/ntlm/NTLMResponses.java b/mina-core/src/main/java/org/apache/mina/proxy/handlers/http/ntlm/NTLMResponses.java
index 93f3b9512..bb153ece7 100644
--- a/mina-core/src/main/java/org/apache/mina/proxy/handlers/http/ntlm/NTLMResponses.java
+++ b/mina-core/src/main/java/org/apache/mina/proxy/handlers/http/ntlm/NTLMResponses.java
@@ -19,10 +19,14 @@
  */
 package org.apache.mina.proxy.handlers.http.ntlm;
 
+import java.nio.charset.StandardCharsets;
+import java.security.GeneralSecurityException;
 import java.security.Key;
 import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
 
 import javax.crypto.Cipher;
+import javax.crypto.NoSuchPaddingException;
 import javax.crypto.spec.SecretKeySpec;
 
 /**
@@ -179,9 +183,10 @@ public class NTLMResponses {
      *
      * @return The LM Hash of the given password, used in the calculation
      * of the LM Response.
+     * @throws GeneralSecurityException if an encryption problem occurs.
      */
-    private static byte[] lmHash(String password) throws Exception {
-        byte[] oemPassword = password.toUpperCase().getBytes("US-ASCII");
+    private static byte[] lmHash(String password) throws GeneralSecurityException{
+        byte[] oemPassword = password.toUpperCase().getBytes(StandardCharsets.US_ASCII);
         int length = Math.min(oemPassword.length, 14);
         byte[] keyBytes = new byte[14];
         System.arraycopy(oemPassword, 0, keyBytes, 0, length);


[mina] 02/02: Use stock JRE Charset instead of magic string

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch 2.1.X
in repository https://gitbox.apache.org/repos/asf/mina.git

commit 32854791a07972663a03e9e408dcf4dec3418793
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 16 09:16:14 2023 -0400

    Use stock JRE Charset instead of magic string
    
    Clean up exceptions on non-public methods
---
 .../mina/proxy/handlers/socks/Socks4LogicHandler.java    |  5 +++--
 .../mina/proxy/handlers/socks/Socks5LogicHandler.java    | 16 ++++++----------
 .../java/org/apache/mina/proxy/utils/ByteUtilities.java  |  9 +++++----
 .../org/apache/mina/proxy/utils/StringUtilities.java     |  3 ++-
 .../mina/transport/AbstractTrafficControlTest.java       |  3 ++-
 .../mina/filter/compression/CompressionFilterTest.java   |  6 ++++--
 .../org/apache/mina/filter/compression/ZlibTest.java     | 11 ++++++-----
 7 files changed, 28 insertions(+), 25 deletions(-)

diff --git a/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks4LogicHandler.java b/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks4LogicHandler.java
index bb1354ba7..8d86197d6 100644
--- a/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks4LogicHandler.java
+++ b/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks4LogicHandler.java
@@ -19,6 +19,7 @@
  */
 package org.apache.mina.proxy.handlers.socks;
 
+import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 
 import org.apache.mina.core.buffer.IoBuffer;
@@ -72,8 +73,8 @@ public class Socks4LogicHandler extends AbstractSocksLogicHandler {
     protected void writeRequest(final NextFilter nextFilter, final SocksProxyRequest request) {
         try {
             boolean isV4ARequest = Arrays.equals(request.getIpAddress(), SocksProxyConstants.FAKE_IP);
-            byte[] userID = request.getUserName().getBytes("ASCII");
-            byte[] host = isV4ARequest ? request.getHost().getBytes("ASCII") : null;
+            byte[] userID = request.getUserName().getBytes(StandardCharsets.US_ASCII);
+            byte[] host = isV4ARequest ? request.getHost().getBytes(StandardCharsets.US_ASCII) : null;
 
             int len = 9 + userID.length;
 
diff --git a/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java b/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java
index 4d7fa907e..a23c12d43 100644
--- a/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java
+++ b/mina-core/src/main/java/org/apache/mina/proxy/handlers/socks/Socks5LogicHandler.java
@@ -19,10 +19,10 @@
  */
 package org.apache.mina.proxy.handlers.socks;
 
-import java.io.UnsupportedEncodingException;
 import java.net.Inet4Address;
 import java.net.Inet6Address;
 import java.net.InetSocketAddress;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.mina.core.buffer.IoBuffer;
 import org.apache.mina.core.filterchain.IoFilter.NextFilter;
@@ -113,10 +113,8 @@ public class Socks5LogicHandler extends AbstractSocksLogicHandler {
      * 
      * @param request the socks proxy request data
      * @return the encoded buffer
-     * @throws UnsupportedEncodingException if request's hostname charset 
-     * can't be converted to ASCII. 
      */
-    private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request) throws UnsupportedEncodingException {
+    private IoBuffer encodeProxyRequestPacket(final SocksProxyRequest request) {
         int len = 6;
         InetSocketAddress adr = request.getEndpointAddress();
         byte addressType = 0;
@@ -131,7 +129,7 @@ public class Socks5LogicHandler extends AbstractSocksLogicHandler {
                 addressType = SocksProxyConstants.IPV4_ADDRESS_TYPE;
             }
         } else {
-            host = request.getHost() != null ? request.getHost().getBytes("ASCII") : null;
+            host = request.getHost() != null ? request.getHost().getBytes(StandardCharsets.US_ASCII) : null;
 
             if (host != null) {
                 len += 1 + host.length;
@@ -167,11 +165,9 @@ public class Socks5LogicHandler extends AbstractSocksLogicHandler {
      * @return the encoded buffer, if null then authentication step is over 
      * and handshake process can jump immediately to the next step without waiting
      * for a server reply.
-     * @throws UnsupportedEncodingException if some string charset convertion fails
      * @throws GSSException when something fails while using GSSAPI
      */
-    private IoBuffer encodeAuthenticationPacket(final SocksProxyRequest request) throws UnsupportedEncodingException,
-            GSSException {
+    private IoBuffer encodeAuthenticationPacket(final SocksProxyRequest request) throws GSSException {
         byte method = ((Byte) getSession().getAttribute(Socks5LogicHandler.SELECTED_AUTH_METHOD)).byteValue();
 
         switch (method) {
@@ -186,8 +182,8 @@ public class Socks5LogicHandler extends AbstractSocksLogicHandler {
 
         case SocksProxyConstants.BASIC_AUTH:
             // The basic auth scheme packet is sent
-            byte[] user = request.getUserName().getBytes("ASCII");
-            byte[] pwd = request.getPassword().getBytes("ASCII");
+            byte[] user = request.getUserName().getBytes(StandardCharsets.US_ASCII);
+            byte[] pwd = request.getPassword().getBytes(StandardCharsets.US_ASCII);
             IoBuffer buf = IoBuffer.allocate(3 + user.length + pwd.length);
 
             buf.put(SocksProxyConstants.BASIC_AUTH_SUBNEGOTIATION_VERSION);
diff --git a/mina-core/src/main/java/org/apache/mina/proxy/utils/ByteUtilities.java b/mina-core/src/main/java/org/apache/mina/proxy/utils/ByteUtilities.java
index b663ee840..6d076ce23 100644
--- a/mina-core/src/main/java/org/apache/mina/proxy/utils/ByteUtilities.java
+++ b/mina-core/src/main/java/org/apache/mina/proxy/utils/ByteUtilities.java
@@ -20,6 +20,7 @@
 package org.apache.mina.proxy.utils;
 
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 
 /**
  * ByteUtilities.java - Byte manipulation functions.
@@ -192,10 +193,10 @@ public class ByteUtilities {
      * 
      * @param s the string to convert
      * @return the result byte array
-     * @throws UnsupportedEncodingException if the string is not an OEM string
+     * @throws UnsupportedEncodingException Never thrown.
      */
     public static final byte[] getOEMStringAsByteArray(String s) throws UnsupportedEncodingException {
-        return s.getBytes("ASCII");
+        return s.getBytes(StandardCharsets.US_ASCII);
     }
 
     /**
@@ -203,10 +204,10 @@ public class ByteUtilities {
      * 
      * @param s the string to convert
      * @return the result byte array
-     * @throws UnsupportedEncodingException if the string is not an UTF-16LE string
+     * @throws UnsupportedEncodingException Never thrown.
      */
     public static final byte[] getUTFStringAsByteArray(String s) throws UnsupportedEncodingException {
-        return s.getBytes("UTF-16LE");
+        return s.getBytes(StandardCharsets.UTF_16LE);
     }
 
     /**
diff --git a/mina-core/src/main/java/org/apache/mina/proxy/utils/StringUtilities.java b/mina-core/src/main/java/org/apache/mina/proxy/utils/StringUtilities.java
index 18b3cbb5e..cb60f45c2 100644
--- a/mina-core/src/main/java/org/apache/mina/proxy/utils/StringUtilities.java
+++ b/mina-core/src/main/java/org/apache/mina/proxy/utils/StringUtilities.java
@@ -21,6 +21,7 @@ package org.apache.mina.proxy.utils;
 
 import java.io.ByteArrayOutputStream;
 import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -288,7 +289,7 @@ public class StringUtilities {
             return "";
         }
 
-        return new String(str.getBytes("UTF8"), "8859_1");
+        return new String(str.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
     }
 
     /**
diff --git a/mina-core/src/test/java/org/apache/mina/transport/AbstractTrafficControlTest.java b/mina-core/src/test/java/org/apache/mina/transport/AbstractTrafficControlTest.java
index 9a633494f..5923626f9 100644
--- a/mina-core/src/test/java/org/apache/mina/transport/AbstractTrafficControlTest.java
+++ b/mina-core/src/test/java/org/apache/mina/transport/AbstractTrafficControlTest.java
@@ -20,6 +20,7 @@
 package org.apache.mina.transport;
 
 import java.net.SocketAddress;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.mina.core.buffer.IoBuffer;
 import org.apache.mina.core.future.ConnectFuture;
@@ -167,7 +168,7 @@ public abstract class AbstractTrafficControlTest {
     }
 
     private void write(IoSession session, String s) throws Exception {
-        session.write(IoBuffer.wrap(s.getBytes("ASCII")));
+        session.write(IoBuffer.wrap(s.getBytes(StandardCharsets.US_ASCII)));
     }
 
     private int read(IoSession session) throws Exception {
diff --git a/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/CompressionFilterTest.java b/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/CompressionFilterTest.java
index 5b097c55e..299d8b335 100644
--- a/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/CompressionFilterTest.java
+++ b/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/CompressionFilterTest.java
@@ -21,6 +21,8 @@ package org.apache.mina.filter.compression;
 
 import static org.junit.Assert.assertTrue;
 
+import java.nio.charset.StandardCharsets;
+
 import org.apache.mina.core.buffer.IoBuffer;
 import org.apache.mina.core.filterchain.IoFilterChain;
 import org.apache.mina.core.filterchain.IoFilter.NextFilter;
@@ -102,7 +104,7 @@ public class CompressionFilterTest {
     @Test
     public void testCompression() throws Exception {
         // prepare the input data
-        IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
+        IoBuffer buf = IoBuffer.wrap(strCompress.getBytes(StandardCharsets.UTF_8));
         IoBuffer actualOutput = actualDeflater.deflate(buf);
         buf.flip();
         WriteRequest writeRequest = new DefaultWriteRequest(buf);
@@ -147,7 +149,7 @@ public class CompressionFilterTest {
     @Test
     public void testDecompression() throws Exception {
         // prepare the input data
-        IoBuffer buf = IoBuffer.wrap(strCompress.getBytes("UTF8"));
+        IoBuffer buf = IoBuffer.wrap(strCompress.getBytes(StandardCharsets.UTF_8));
         IoBuffer byteInput = actualDeflater.deflate(buf);
         IoBuffer actualOutput = actualInflater.inflate(byteInput);
 
diff --git a/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/ZlibTest.java b/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/ZlibTest.java
index 99a37b7ce..a3c3da493 100644
--- a/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/ZlibTest.java
+++ b/mina-filter-compression/src/test/java/org/apache/mina/filter/compression/ZlibTest.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
 
 import org.apache.mina.core.buffer.IoBuffer;
 import org.junit.Before;
@@ -52,7 +53,7 @@ public class ZlibTest {
         for (int i = 0; i < 10; i++) {
             strInput += "The quick brown fox jumps over the lazy dog.  ";
         }
-        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
+        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));
 
         // increase the count to have the compression and decompression
         // done using the same instance of Zlib
@@ -68,21 +69,21 @@ public class ZlibTest {
     @Test
     public void testCorruptedData() throws Exception {
         String strInput = "Hello World";
-        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
+        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));
 
         IoBuffer byteCompressed = deflater.deflate(byteInput);
         // change the contents to something else. Since this doesn't check
         // for integrity, it wont throw an exception
         byteCompressed.put(5, (byte) 0xa);
         IoBuffer byteUncompressed = inflater.inflate(byteCompressed);
-        String strOutput = byteUncompressed.getString(Charset.forName("UTF8").newDecoder());
+        String strOutput = byteUncompressed.getString(StandardCharsets.UTF_8.newDecoder());
         assertFalse(strOutput.equals(strInput));
     }
 
     @Test
     public void testCorruptedHeader() throws Exception {
         String strInput = "Hello World";
-        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
+        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));
 
         IoBuffer byteCompressed = deflater.deflate(byteInput);
         // write a bad value into the zlib header. Make sure that
@@ -103,7 +104,7 @@ public class ZlibTest {
         for (int i = 0; i < 10; i++) {
             strInput += "The quick brown fox jumps over the lazy dog.  ";
         }
-        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes("UTF8"));
+        IoBuffer byteInput = IoBuffer.wrap(strInput.getBytes(StandardCharsets.UTF_8));
         IoBuffer byteCompressed = null;
 
         for (int i = 0; i < 5; i++) {