diff --git a/httpclient/src/main/java/org/apache/http/impl/auth/NTLMEngineImpl.java b/httpclient/src/main/java/org/apache/http/impl/auth/NTLMEngineImpl.java index e1a1d5ecf..bc7412b0f 100644 --- a/httpclient/src/main/java/org/apache/http/impl/auth/NTLMEngineImpl.java +++ b/httpclient/src/main/java/org/apache/http/impl/auth/NTLMEngineImpl.java @@ -26,6 +26,7 @@ */ package org.apache.http.impl.auth; +import java.io.UnsupportedEncodingException; import java.security.Key; import java.security.MessageDigest; import java.util.Arrays; @@ -183,6 +184,9 @@ void setCredentialCharset(final String credentialCharset) { /** Strip dot suffix from a name */ private static String stripDotSuffix(final String value) { + if (value == null) { + return null; + } final int index = value.indexOf("."); if (index != -1) return value.substring(0, index); @@ -917,6 +921,9 @@ protected void addByte(final byte b) { * the bytes to add. */ protected void addBytes(final byte[] bytes) { + if (bytes == null) { + return; + } for (final byte b : bytes) { messageContents[currentOutputPosition] = b; currentOutputPosition++; @@ -971,8 +978,9 @@ static class Type1Message extends NTLMMessage { // Use only the base domain name! final String unqualifiedDomain = convertDomain(domain); - hostBytes = unqualifiedHost.getBytes("ASCII"); - domainBytes = unqualifiedDomain.toUpperCase(Locale.US).getBytes("ASCII"); + hostBytes = unqualifiedHost != null? unqualifiedHost.getBytes("ASCII") : null; + domainBytes = unqualifiedDomain != null ? unqualifiedDomain + .toUpperCase(Locale.US).getBytes("ASCII") : null; } catch (java.io.UnsupportedEncodingException e) { throw new NTLMEngineException("Unicode unsupported: " + e.getMessage(), e); } @@ -1219,10 +1227,12 @@ static class Type3Message extends NTLMMessage { } try { - domainBytes = unqualifiedDomain.toUpperCase(Locale.US).getBytes("UnicodeLittleUnmarked"); - hostBytes = unqualifiedHost.getBytes("UnicodeLittleUnmarked"); + hostBytes = unqualifiedHost != null ? unqualifiedHost + .getBytes("UnicodeLittleUnmarked") : null; + domainBytes = unqualifiedDomain != null ? unqualifiedDomain + .toUpperCase(Locale.US).getBytes("UnicodeLittleUnmarked") : null; userBytes = user.getBytes("UnicodeLittleUnmarked"); - } catch (java.io.UnsupportedEncodingException e) { + } catch (UnsupportedEncodingException e) { throw new NTLMEngineException("Unicode not supported: " + e.getMessage(), e); } } @@ -1233,8 +1243,8 @@ String getResponse() { final int ntRespLen = ntResp.length; final int lmRespLen = lmResp.length; - final int domainLen = domainBytes.length; - final int hostLen = hostBytes.length; + final int domainLen = domainBytes != null ? domainBytes.length : 0; + final int hostLen = hostBytes != null ? hostBytes.length: 0; final int userLen = userBytes.length; final int sessionKeyLen; if (sessionKey != null) diff --git a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFakeNTLM.java b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFakeNTLM.java index 032ba62ea..09cae31bd 100644 --- a/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFakeNTLM.java +++ b/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthenticationFakeNTLM.java @@ -82,7 +82,7 @@ public void testNTLMAuthenticationFailure() throws Exception { final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, - new NTCredentials("test", "test", "", "")); + new NTCredentials("test", "test", null, null)); this.httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider) @@ -127,7 +127,7 @@ public void testNTLMType2() throws Exception { final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(AuthScope.ANY, - new NTCredentials("test", "test", "", "")); + new NTCredentials("test", "test", null, null)); this.httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credsProvider)