View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.auth;
28  
29  import java.nio.charset.Charset;
30  import java.nio.charset.StandardCharsets;
31  import java.security.Key;
32  import java.security.MessageDigest;
33  import java.security.NoSuchAlgorithmException;
34  import java.security.cert.Certificate;
35  import java.security.cert.CertificateEncodingException;
36  import java.util.Arrays;
37  import java.util.Locale;
38  import java.util.Random;
39  
40  import javax.crypto.Cipher;
41  import javax.crypto.spec.SecretKeySpec;
42  
43  import org.apache.hc.client5.http.utils.Base64;
44  import org.apache.hc.client5.http.utils.ByteArrayBuilder;
45  
46  /**
47   * Provides an implementation for NTLMv1, NTLMv2, and NTLM2 Session forms of the NTLM
48   * authentication protocol.
49   *
50   * @since 4.1
51   */
52  final class NTLMEngineImpl implements NTLMEngine {
53  
54      /** Unicode encoding */
55      private static final Charset UNICODE_LITTLE_UNMARKED = Charset.forName("UnicodeLittleUnmarked");
56      /** Character encoding */
57      private static final Charset DEFAULT_CHARSET = StandardCharsets.US_ASCII;
58  
59      // Flags we use; descriptions according to:
60      // http://davenport.sourceforge.net/ntlm.html
61      // and
62      // http://msdn.microsoft.com/en-us/library/cc236650%28v=prot.20%29.aspx
63      // [MS-NLMP] section 2.2.2.5
64      static final int FLAG_REQUEST_UNICODE_ENCODING = 0x00000001;      // Unicode string encoding requested
65      static final int FLAG_REQUEST_OEM_ENCODING = 0x00000002;      // OEM string encoding requested
66      static final int FLAG_REQUEST_TARGET = 0x00000004;                      // Requests target field
67      static final int FLAG_REQUEST_SIGN = 0x00000010;  // Requests all messages have a signature attached, in NEGOTIATE message.
68      static final int FLAG_REQUEST_SEAL = 0x00000020;  // Request key exchange for message confidentiality in NEGOTIATE message.  MUST be used in conjunction with 56BIT.
69      static final int FLAG_REQUEST_LAN_MANAGER_KEY = 0x00000080;    // Request Lan Manager key instead of user session key
70      static final int FLAG_REQUEST_NTLMv1 = 0x00000200; // Request NTLMv1 security.  MUST be set in NEGOTIATE and CHALLENGE both
71      static final int FLAG_DOMAIN_PRESENT = 0x00001000;        // Domain is present in message
72      static final int FLAG_WORKSTATION_PRESENT = 0x00002000;   // Workstation is present in message
73      static final int FLAG_REQUEST_ALWAYS_SIGN = 0x00008000;   // Requests a signature block on all messages.  Overridden by REQUEST_SIGN and REQUEST_SEAL.
74      static final int FLAG_REQUEST_NTLM2_SESSION = 0x00080000; // From server in challenge, requesting NTLM2 session security
75      static final int FLAG_REQUEST_VERSION = 0x02000000;       // Request protocol version
76      static final int FLAG_TARGETINFO_PRESENT = 0x00800000;    // From server in challenge message, indicating targetinfo is present
77      static final int FLAG_REQUEST_128BIT_KEY_EXCH = 0x20000000; // Request explicit 128-bit key exchange
78      static final int FLAG_REQUEST_EXPLICIT_KEY_EXCH = 0x40000000;     // Request explicit key exchange
79      static final int FLAG_REQUEST_56BIT_ENCRYPTION = 0x80000000;      // Must be used in conjunction with SEAL
80  
81      // Attribute-value identifiers (AvId)
82      // according to [MS-NLMP] section 2.2.2.1
83      static final int MSV_AV_EOL = 0x0000; // Indicates that this is the last AV_PAIR in the list.
84      static final int MSV_AV_NB_COMPUTER_NAME = 0x0001; // The server's NetBIOS computer name.
85      static final int MSV_AV_NB_DOMAIN_NAME = 0x0002; // The server's NetBIOS domain name.
86      static final int MSV_AV_DNS_COMPUTER_NAME = 0x0003; // The fully qualified domain name (FQDN) of the computer.
87      static final int MSV_AV_DNS_DOMAIN_NAME = 0x0004; // The FQDN of the domain.
88      static final int MSV_AV_DNS_TREE_NAME = 0x0005; // The FQDN of the forest.
89      static final int MSV_AV_FLAGS = 0x0006; // A 32-bit value indicating server or client configuration.
90      static final int MSV_AV_TIMESTAMP = 0x0007; // server local time
91      static final int MSV_AV_SINGLE_HOST = 0x0008; // A Single_Host_Data structure.
92      static final int MSV_AV_TARGET_NAME = 0x0009; // The SPN of the target server.
93      static final int MSV_AV_CHANNEL_BINDINGS = 0x000A; // A channel bindings hash.
94  
95      static final int MSV_AV_FLAGS_ACCOUNT_AUTH_CONSTAINED = 0x00000001; // Indicates to the client that the account authentication is constrained.
96      static final int MSV_AV_FLAGS_MIC = 0x00000002; // Indicates that the client is providing message integrity in the MIC field in the AUTHENTICATE_MESSAGE.
97      static final int MSV_AV_FLAGS_UNTRUSTED_TARGET_SPN = 0x00000004; // Indicates that the client is providing a target SPN generated from an untrusted source.
98  
99      /** Secure random generator */
100     private static final java.security.SecureRandom RND_GEN;
101     static {
102         java.security.SecureRandom rnd = null;
103         try {
104             rnd = java.security.SecureRandom.getInstance("SHA1PRNG");
105         } catch (final Exception ignore) {
106             // ignore
107         }
108         RND_GEN = rnd;
109     }
110 
111     /** The signature string as bytes in the default encoding */
112     private static final byte[] SIGNATURE = getNullTerminatedAsciiString("NTLMSSP");
113 
114     // Key derivation magic strings for the SIGNKEY algorithm defined in
115     // [MS-NLMP] section 3.4.5.2ASCII
116     private static final byte[] SIGN_MAGIC_SERVER = getNullTerminatedAsciiString(
117         "session key to server-to-client signing key magic constant");
118     private static final byte[] SIGN_MAGIC_CLIENT = getNullTerminatedAsciiString(
119         "session key to client-to-server signing key magic constant");
120     private static final byte[] SEAL_MAGIC_SERVER = getNullTerminatedAsciiString(
121         "session key to server-to-client sealing key magic constant");
122     private static final byte[] SEAL_MAGIC_CLIENT = getNullTerminatedAsciiString(
123         "session key to client-to-server sealing key magic constant");
124 
125     // prefix for GSS API channel binding
126     private static final byte[] MAGIC_TLS_SERVER_ENDPOINT = "tls-server-end-point:".getBytes(StandardCharsets.US_ASCII);
127 
128     private static byte[] getNullTerminatedAsciiString( final String source )
129     {
130         final byte[] bytesWithoutNull = source.getBytes(StandardCharsets.US_ASCII);
131         final byte[] target = new byte[bytesWithoutNull.length + 1];
132         System.arraycopy(bytesWithoutNull, 0, target, 0, bytesWithoutNull.length);
133         target[bytesWithoutNull.length] = (byte) 0x00;
134         return target;
135     }
136 
137     private static final String TYPE_1_MESSAGE = new Type1Message().getResponse();
138 
139     NTLMEngineImpl() {
140     }
141 
142     /**
143      * Returns the response for the given message.
144      *
145      * @param message
146      *            the message that was received from the server.
147      * @param username
148      *            the username to authenticate with.
149      * @param password
150      *            the password to authenticate with.
151      * @param host
152      *            The host.
153      * @param domain
154      *            the NT domain to authenticate in.
155      * @return The response.
156      */
157     static String getResponseFor(final String message, final String username, final char[] password,
158             final String host, final String domain) throws NTLMEngineException {
159 
160         final String response;
161         if (message == null || message.trim().equals("")) {
162             response = getType1Message(host, domain);
163         } else {
164             final Type2Message t2m = new Type2Message(message);
165             response = getType3Message(username, password, host, domain, t2m.getChallenge(),
166                 t2m.getFlags(), t2m.getTarget(), t2m.getTargetInfo());
167         }
168         return response;
169     }
170 
171     /**
172      * Returns the response for the given message.
173      *
174      * @param message
175      *            the message that was received from the server.
176      * @param username
177      *            the username to authenticate with.
178      * @param password
179      *            the password to authenticate with.
180      * @param host
181      *            The host.
182      * @param domain
183      *            the NT domain to authenticate in.
184      * @return The response.
185      */
186     static String getResponseFor(final String message, final String username, final char[] password,
187             final String host, final String domain, final Certificate peerServerCertificate) throws NTLMEngineException {
188 
189         final String response;
190         if (message == null || message.trim().equals("")) {
191             response = new Type1Message(host, domain).getResponse();
192         } else {
193             final Type1Message t1m = new Type1Message(host, domain);
194             final Type2Message t2m = new Type2Message(message);
195             response = getType3Message(username, password, host, domain, t2m.getChallenge(),
196                 t2m.getFlags(), t2m.getTarget(), t2m.getTargetInfo(),
197                 peerServerCertificate, t1m.getBytes(), t2m.getBytes());
198         }
199         return response;
200     }
201 
202     /**
203      * Creates the first message (type 1 message) in the NTLM authentication
204      * sequence. This message includes the user name, domain and host for the
205      * authentication session.
206      *
207      * @param host
208      *            the computer name of the host requesting authentication.
209      * @param domain
210      *            The domain to authenticate with.
211      * @return String the message to add to the HTTP request header.
212      */
213     static String getType1Message(final String host, final String domain) {
214         // For compatibility reason do not include domain and host in type 1 message
215         //return new Type1Message(domain, host).getResponse();
216         return TYPE_1_MESSAGE;
217     }
218 
219     /**
220      * Creates the type 3 message using the given server nonce. The type 3
221      * message includes all the information for authentication, host, domain,
222      * username and the result of encrypting the nonce sent by the server using
223      * the user's password as the key.
224      *
225      * @param user
226      *            The user name. This should not include the domain name.
227      * @param password
228      *            The password.
229      * @param host
230      *            The host that is originating the authentication request.
231      * @param domain
232      *            The domain to authenticate within.
233      * @param nonce
234      *            the 8 byte array the server sent.
235      * @return The type 3 message.
236      * @throws NTLMEngineException
237      *             If {@link Type3Message#Type3Message(String, String, String, char[], byte[], int, String, byte[])} fails.
238      */
239     static String getType3Message(final String user, final char[] password, final String host, final String domain,
240             final byte[] nonce, final int type2Flags, final String target, final byte[] targetInformation)
241             throws NTLMEngineException {
242         return new Type3Message(domain, host, user, password, nonce, type2Flags, target,
243                 targetInformation).getResponse();
244     }
245 
246     /**
247      * Creates the type 3 message using the given server nonce. The type 3
248      * message includes all the information for authentication, host, domain,
249      * username and the result of encrypting the nonce sent by the server using
250      * the user's password as the key.
251      *
252      * @param user
253      *            The user name. This should not include the domain name.
254      * @param password
255      *            The password.
256      * @param host
257      *            The host that is originating the authentication request.
258      * @param domain
259      *            The domain to authenticate within.
260      * @param nonce
261      *            the 8 byte array the server sent.
262      * @return The type 3 message.
263      */
264     static String getType3Message(final String user, final char[] password, final String host, final String domain,
265             final byte[] nonce, final int type2Flags, final String target, final byte[] targetInformation,
266             final Certificate peerServerCertificate, final byte[] type1Message, final byte[] type2Message)
267             throws NTLMEngineException {
268         return new Type3Message(domain, host, user, password, nonce, type2Flags, target,
269                 targetInformation, peerServerCertificate, type1Message, type2Message).getResponse();
270     }
271 
272     private static int readULong(final byte[] src, final int index) {
273         if (src.length < index + 4) {
274             return 0;
275         }
276         return (src[index] & 0xff) | ((src[index + 1] & 0xff) << 8)
277                 | ((src[index + 2] & 0xff) << 16) | ((src[index + 3] & 0xff) << 24);
278     }
279 
280     private static int readUShort(final byte[] src, final int index) {
281         if (src.length < index + 2) {
282             return 0;
283         }
284         return (src[index] & 0xff) | ((src[index + 1] & 0xff) << 8);
285     }
286 
287     private static byte[] readSecurityBuffer(final byte[] src, final int index) {
288         final int length = readUShort(src, index);
289         final int offset = readULong(src, index + 4);
290         if (src.length < offset + length) {
291             return new byte[length];
292         }
293         final byte[] buffer = new byte[length];
294         System.arraycopy(src, offset, buffer, 0, length);
295         return buffer;
296     }
297 
298     /** Calculate a challenge block */
299     private static byte[] makeRandomChallenge(final Random random) {
300         final byte[] rval = new byte[8];
301         synchronized (random) {
302             random.nextBytes(rval);
303         }
304         return rval;
305     }
306 
307     /** Calculate a 16-byte secondary key */
308     private static byte[] makeSecondaryKey(final Random random) {
309         final byte[] rval = new byte[16];
310         synchronized (random) {
311             random.nextBytes(rval);
312         }
313         return rval;
314     }
315 
316     static class CipherGen {
317 
318         final Random random;
319         final long currentTime;
320 
321         final String domain;
322         final String user;
323         final char[] password;
324         final byte[] challenge;
325         final String target;
326         final byte[] targetInformation;
327 
328         // Information we can generate but may be passed in (for testing)
329         byte[] clientChallenge;
330         byte[] clientChallenge2;
331         byte[] secondaryKey;
332         byte[] timestamp;
333 
334         // Stuff we always generate
335         byte[] lmHash;
336         byte[] lmResponse;
337         byte[] ntlmHash;
338         byte[] ntlmResponse;
339         byte[] ntlmv2Hash;
340         byte[] lmv2Hash;
341         byte[] lmv2Response;
342         byte[] ntlmv2Blob;
343         byte[] ntlmv2Response;
344         byte[] ntlm2SessionResponse;
345         byte[] lm2SessionResponse;
346         byte[] lmUserSessionKey;
347         byte[] ntlmUserSessionKey;
348         byte[] ntlmv2UserSessionKey;
349         byte[] ntlm2SessionResponseUserSessionKey;
350         byte[] lanManagerSessionKey;
351 
352         public CipherGen(final Random random, final long currentTime,
353             final String domain, final String user, final char[] password,
354             final byte[] challenge, final String target, final byte[] targetInformation,
355             final byte[] clientChallenge, final byte[] clientChallenge2,
356             final byte[] secondaryKey, final byte[] timestamp) {
357             this.random = random;
358             this.currentTime = currentTime;
359 
360             this.domain = domain;
361             this.target = target;
362             this.user = user;
363             this.password = password;
364             this.challenge = challenge;
365             this.targetInformation = targetInformation;
366             this.clientChallenge = clientChallenge;
367             this.clientChallenge2 = clientChallenge2;
368             this.secondaryKey = secondaryKey;
369             this.timestamp = timestamp;
370         }
371 
372         public CipherGen(final Random random, final long currentTime,
373             final String domain,
374             final String user,
375             final char[] password,
376             final byte[] challenge,
377             final String target,
378             final byte[] targetInformation) {
379             this(random, currentTime, domain, user, password, challenge, target, targetInformation, null, null, null, null);
380         }
381 
382         /** Calculate and return client challenge */
383         public byte[] getClientChallenge() {
384             if (clientChallenge == null) {
385                 clientChallenge = makeRandomChallenge(random);
386             }
387             return clientChallenge;
388         }
389 
390         /** Calculate and return second client challenge */
391         public byte[] getClientChallenge2() {
392             if (clientChallenge2 == null) {
393                 clientChallenge2 = makeRandomChallenge(random);
394             }
395             return clientChallenge2;
396         }
397 
398         /** Calculate and return random secondary key */
399         public byte[] getSecondaryKey() {
400             if (secondaryKey == null) {
401                 secondaryKey = makeSecondaryKey(random);
402             }
403             return secondaryKey;
404         }
405 
406         /** Calculate and return the LMHash */
407         public byte[] getLMHash()
408             throws NTLMEngineException {
409             if (lmHash == null) {
410                 lmHash = lmHash(password);
411             }
412             return lmHash;
413         }
414 
415         /** Calculate and return the LMResponse */
416         public byte[] getLMResponse()
417             throws NTLMEngineException {
418             if (lmResponse == null) {
419                 lmResponse = lmResponse(getLMHash(),challenge);
420             }
421             return lmResponse;
422         }
423 
424         /** Calculate and return the NTLMHash */
425         public byte[] getNTLMHash()
426             throws NTLMEngineException {
427             if (ntlmHash == null) {
428                 ntlmHash = ntlmHash(password);
429             }
430             return ntlmHash;
431         }
432 
433         /** Calculate and return the NTLMResponse */
434         public byte[] getNTLMResponse()
435             throws NTLMEngineException {
436             if (ntlmResponse == null) {
437                 ntlmResponse = lmResponse(getNTLMHash(),challenge);
438             }
439             return ntlmResponse;
440         }
441 
442         /** Calculate the LMv2 hash */
443         public byte[] getLMv2Hash()
444             throws NTLMEngineException {
445             if (lmv2Hash == null) {
446                 lmv2Hash = lmv2Hash(domain, user, getNTLMHash());
447             }
448             return lmv2Hash;
449         }
450 
451         /** Calculate the NTLMv2 hash */
452         public byte[] getNTLMv2Hash()
453             throws NTLMEngineException {
454             if (ntlmv2Hash == null) {
455                 ntlmv2Hash = ntlmv2Hash(domain, user, getNTLMHash());
456             }
457             return ntlmv2Hash;
458         }
459 
460         /** Calculate a timestamp */
461         public byte[] getTimestamp() {
462             if (timestamp == null) {
463                 long time = this.currentTime;
464                 time += 11644473600000L; // milliseconds from January 1, 1601 -> epoch.
465                 time *= 10000; // tenths of a microsecond.
466                 // convert to little-endian byte array.
467                 timestamp = new byte[8];
468                 for (int i = 0; i < 8; i++) {
469                     timestamp[i] = (byte) time;
470                     time >>>= 8;
471                 }
472             }
473             return timestamp;
474         }
475 
476         /** Calculate the NTLMv2Blob */
477         public byte[] getNTLMv2Blob() {
478             if (ntlmv2Blob == null) {
479                 ntlmv2Blob = createBlob(getClientChallenge2(), targetInformation, getTimestamp());
480             }
481             return ntlmv2Blob;
482         }
483 
484         /** Calculate the NTLMv2Response */
485         public byte[] getNTLMv2Response()
486             throws NTLMEngineException {
487             if (ntlmv2Response == null) {
488                 ntlmv2Response = lmv2Response(getNTLMv2Hash(),challenge,getNTLMv2Blob());
489             }
490             return ntlmv2Response;
491         }
492 
493         /** Calculate the LMv2Response */
494         public byte[] getLMv2Response()
495             throws NTLMEngineException {
496             if (lmv2Response == null) {
497                 lmv2Response = lmv2Response(getLMv2Hash(),challenge,getClientChallenge());
498             }
499             return lmv2Response;
500         }
501 
502         /** Get NTLM2SessionResponse */
503         public byte[] getNTLM2SessionResponse()
504             throws NTLMEngineException {
505             if (ntlm2SessionResponse == null) {
506                 ntlm2SessionResponse = ntlm2SessionResponse(getNTLMHash(),challenge,getClientChallenge());
507             }
508             return ntlm2SessionResponse;
509         }
510 
511         /** Calculate and return LM2 session response */
512         public byte[] getLM2SessionResponse() {
513             if (lm2SessionResponse == null) {
514                 final byte[] clntChallenge = getClientChallenge();
515                 lm2SessionResponse = new byte[24];
516                 System.arraycopy(clntChallenge, 0, lm2SessionResponse, 0, clntChallenge.length);
517                 Arrays.fill(lm2SessionResponse, clntChallenge.length, lm2SessionResponse.length, (byte) 0x00);
518             }
519             return lm2SessionResponse;
520         }
521 
522         /** Get LMUserSessionKey */
523         public byte[] getLMUserSessionKey()
524             throws NTLMEngineException {
525             if (lmUserSessionKey == null) {
526                 lmUserSessionKey = new byte[16];
527                 System.arraycopy(getLMHash(), 0, lmUserSessionKey, 0, 8);
528                 Arrays.fill(lmUserSessionKey, 8, 16, (byte) 0x00);
529             }
530             return lmUserSessionKey;
531         }
532 
533         /** Get NTLMUserSessionKey */
534         public byte[] getNTLMUserSessionKey()
535             throws NTLMEngineException {
536             if (ntlmUserSessionKey == null) {
537                 final MD4 md4 = new MD4();
538                 md4.update(getNTLMHash());
539                 ntlmUserSessionKey = md4.getOutput();
540             }
541             return ntlmUserSessionKey;
542         }
543 
544         /** GetNTLMv2UserSessionKey */
545         public byte[] getNTLMv2UserSessionKey()
546             throws NTLMEngineException {
547             if (ntlmv2UserSessionKey == null) {
548                 final byte[] ntlmv2hash = getNTLMv2Hash();
549                 final byte[] truncatedResponse = new byte[16];
550                 System.arraycopy(getNTLMv2Response(), 0, truncatedResponse, 0, 16);
551                 ntlmv2UserSessionKey = hmacMD5(truncatedResponse, ntlmv2hash);
552             }
553             return ntlmv2UserSessionKey;
554         }
555 
556         /** Get NTLM2SessionResponseUserSessionKey */
557         public byte[] getNTLM2SessionResponseUserSessionKey()
558             throws NTLMEngineException {
559             if (ntlm2SessionResponseUserSessionKey == null) {
560                 final byte[] ntlm2SessionResponseNonce = getLM2SessionResponse();
561                 final byte[] sessionNonce = new byte[challenge.length + ntlm2SessionResponseNonce.length];
562                 System.arraycopy(challenge, 0, sessionNonce, 0, challenge.length);
563                 System.arraycopy(ntlm2SessionResponseNonce, 0, sessionNonce, challenge.length, ntlm2SessionResponseNonce.length);
564                 ntlm2SessionResponseUserSessionKey = hmacMD5(sessionNonce,getNTLMUserSessionKey());
565             }
566             return ntlm2SessionResponseUserSessionKey;
567         }
568 
569         /** Get LAN Manager session key */
570         public byte[] getLanManagerSessionKey()
571             throws NTLMEngineException {
572             if (lanManagerSessionKey == null) {
573                 try {
574                     final byte[] keyBytes = new byte[14];
575                     System.arraycopy(getLMHash(), 0, keyBytes, 0, 8);
576                     Arrays.fill(keyBytes, 8, keyBytes.length, (byte)0xbd);
577                     final Key lowKey = createDESKey(keyBytes, 0);
578                     final Key highKey = createDESKey(keyBytes, 7);
579                     final byte[] truncatedResponse = new byte[8];
580                     System.arraycopy(getLMResponse(), 0, truncatedResponse, 0, truncatedResponse.length);
581                     Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
582                     des.init(Cipher.ENCRYPT_MODE, lowKey);
583                     final byte[] lowPart = des.doFinal(truncatedResponse);
584                     des = Cipher.getInstance("DES/ECB/NoPadding");
585                     des.init(Cipher.ENCRYPT_MODE, highKey);
586                     final byte[] highPart = des.doFinal(truncatedResponse);
587                     lanManagerSessionKey = new byte[16];
588                     System.arraycopy(lowPart, 0, lanManagerSessionKey, 0, lowPart.length);
589                     System.arraycopy(highPart, 0, lanManagerSessionKey, lowPart.length, highPart.length);
590                 } catch (final Exception e) {
591                     throw new NTLMEngineException(e.getMessage(), e);
592                 }
593             }
594             return lanManagerSessionKey;
595         }
596     }
597 
598     /** Calculates HMAC-MD5 */
599     static byte[] hmacMD5(final byte[] value, final byte[] key) {
600         final HMACMD5 hmacMD5 = new HMACMD5(key);
601         hmacMD5.update(value);
602         return hmacMD5.getOutput();
603     }
604 
605     /** Calculates RC4 */
606     static byte[] RC4(final byte[] value, final byte[] key)
607         throws NTLMEngineException {
608         try {
609             final Cipher rc4 = Cipher.getInstance("RC4");
610             rc4.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "RC4"));
611             return rc4.doFinal(value);
612         } catch (final Exception e) {
613             throw new NTLMEngineException(e.getMessage(), e);
614         }
615     }
616 
617     /**
618      * Calculates the NTLM2 Session Response for the given challenge, using the
619      * specified password and client challenge.
620      *
621      * @return The NTLM2 Session Response. This is placed in the NTLM response
622      *         field of the Type 3 message; the LM response field contains the
623      *         client challenge, null-padded to 24 bytes.
624      */
625     static byte[] ntlm2SessionResponse(final byte[] ntlmHash, final byte[] challenge,
626             final byte[] clientChallenge) throws NTLMEngineException {
627         try {
628             final MessageDigest md5 = getMD5();
629             md5.update(challenge);
630             md5.update(clientChallenge);
631             final byte[] digest = md5.digest();
632 
633             final byte[] sessionHash = new byte[8];
634             System.arraycopy(digest, 0, sessionHash, 0, 8);
635             return lmResponse(ntlmHash, sessionHash);
636         } catch (final Exception e) {
637             if (e instanceof NTLMEngineException) {
638                 throw (NTLMEngineException) e;
639             }
640             throw new NTLMEngineException(e.getMessage(), e);
641         }
642     }
643 
644     /**
645      * Creates the LM Hash of the user's password.
646      *
647      * @param password
648      *            The password.
649      *
650      * @return The LM Hash of the given password, used in the calculation of the
651      *         LM Response.
652      */
653     private static byte[] lmHash(final char[] password) throws NTLMEngineException {
654         try {
655             final char[] tmp = new char[password.length];
656             for (int i = 0; i < password.length; i++) {
657                 tmp[i] = Character.toUpperCase(password[i]);
658             }
659             final byte[] oemPassword = new ByteArrayBuilder().append(tmp).toByteArray();
660             final int length = Math.min(oemPassword.length, 14);
661             final byte[] keyBytes = new byte[14];
662             System.arraycopy(oemPassword, 0, keyBytes, 0, length);
663             final Key lowKey = createDESKey(keyBytes, 0);
664             final Key highKey = createDESKey(keyBytes, 7);
665             final byte[] magicConstant = "KGS!@#$%".getBytes(StandardCharsets.US_ASCII);
666             final Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
667             des.init(Cipher.ENCRYPT_MODE, lowKey);
668             final byte[] lowHash = des.doFinal(magicConstant);
669             des.init(Cipher.ENCRYPT_MODE, highKey);
670             final byte[] highHash = des.doFinal(magicConstant);
671             final byte[] lmHash = new byte[16];
672             System.arraycopy(lowHash, 0, lmHash, 0, 8);
673             System.arraycopy(highHash, 0, lmHash, 8, 8);
674             return lmHash;
675         } catch (final Exception e) {
676             throw new NTLMEngineException(e.getMessage(), e);
677         }
678     }
679 
680     /**
681      * Creates the NTLM Hash of the user's password.
682      *
683      * @param password
684      *            The password.
685      *
686      * @return The NTLM Hash of the given password, used in the calculation of
687      *         the NTLM Response and the NTLMv2 and LMv2 Hashes.
688      */
689     private static byte[] ntlmHash(final char[] password) throws NTLMEngineException {
690         final byte[] unicodePassword = new ByteArrayBuilder()
691                 .charset(UNICODE_LITTLE_UNMARKED).append(password).toByteArray();
692         final MD4 md4 = new MD4();
693         md4.update(unicodePassword);
694         return md4.getOutput();
695     }
696 
697     /**
698      * Creates the LMv2 Hash of the user's password.
699      *
700      * @return The LMv2 Hash, used in the calculation of the NTLMv2 and LMv2
701      *         Responses.
702      */
703     private static byte[] lmv2Hash(final String domain, final String user, final byte[] ntlmHash)
704             throws NTLMEngineException {
705         final HMACMD5 hmacMD5 = new HMACMD5(ntlmHash);
706         // Upper case username, upper case domain!
707         hmacMD5.update(user.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED));
708         if (domain != null) {
709             hmacMD5.update(domain.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED));
710         }
711         return hmacMD5.getOutput();
712     }
713 
714     /**
715      * Creates the NTLMv2 Hash of the user's password.
716      *
717      * @return The NTLMv2 Hash, used in the calculation of the NTLMv2 and LMv2
718      *         Responses.
719      */
720     private static byte[] ntlmv2Hash(final String domain, final String user, final byte[] ntlmHash)
721             throws NTLMEngineException {
722         final HMACMD5 hmacMD5 = new HMACMD5(ntlmHash);
723         // Upper case username, mixed case target!!
724         hmacMD5.update(user.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED));
725         if (domain != null) {
726             hmacMD5.update(domain.getBytes(UNICODE_LITTLE_UNMARKED));
727         }
728         return hmacMD5.getOutput();
729     }
730 
731     /**
732      * Creates the LM Response from the given hash and Type 2 challenge.
733      *
734      * @param hash
735      *            The LM or NTLM Hash.
736      * @param challenge
737      *            The server challenge from the Type 2 message.
738      *
739      * @return The response (either LM or NTLM, depending on the provided hash).
740      */
741     private static byte[] lmResponse(final byte[] hash, final byte[] challenge) throws NTLMEngineException {
742         try {
743             final byte[] keyBytes = new byte[21];
744             System.arraycopy(hash, 0, keyBytes, 0, 16);
745             final Key lowKey = createDESKey(keyBytes, 0);
746             final Key middleKey = createDESKey(keyBytes, 7);
747             final Key highKey = createDESKey(keyBytes, 14);
748             final Cipher des = Cipher.getInstance("DES/ECB/NoPadding");
749             des.init(Cipher.ENCRYPT_MODE, lowKey);
750             final byte[] lowResponse = des.doFinal(challenge);
751             des.init(Cipher.ENCRYPT_MODE, middleKey);
752             final byte[] middleResponse = des.doFinal(challenge);
753             des.init(Cipher.ENCRYPT_MODE, highKey);
754             final byte[] highResponse = des.doFinal(challenge);
755             final byte[] lmResponse = new byte[24];
756             System.arraycopy(lowResponse, 0, lmResponse, 0, 8);
757             System.arraycopy(middleResponse, 0, lmResponse, 8, 8);
758             System.arraycopy(highResponse, 0, lmResponse, 16, 8);
759             return lmResponse;
760         } catch (final Exception e) {
761             throw new NTLMEngineException(e.getMessage(), e);
762         }
763     }
764 
765     /**
766      * Creates the LMv2 Response from the given hash, client data, and Type 2
767      * challenge.
768      *
769      * @param hash
770      *            The NTLMv2 Hash.
771      * @param clientData
772      *            The client data (blob or client challenge).
773      * @param challenge
774      *            The server challenge from the Type 2 message.
775      *
776      * @return The response (either NTLMv2 or LMv2, depending on the client
777      *         data).
778      */
779     private static byte[] lmv2Response(final byte[] hash, final byte[] challenge, final byte[] clientData) {
780         final HMACMD5 hmacMD5 = new HMACMD5(hash);
781         hmacMD5.update(challenge);
782         hmacMD5.update(clientData);
783         final byte[] mac = hmacMD5.getOutput();
784         final byte[] lmv2Response = new byte[mac.length + clientData.length];
785         System.arraycopy(mac, 0, lmv2Response, 0, mac.length);
786         System.arraycopy(clientData, 0, lmv2Response, mac.length, clientData.length);
787         return lmv2Response;
788     }
789 
790     enum Mode
791     {
792         CLIENT, SERVER
793     }
794 
795     static class Handle
796     {
797         private final byte[] signingKey;
798         private byte[] sealingKey;
799         private final Cipher rc4;
800         final Mode mode;
801         final private boolean isConnection;
802         int sequenceNumber;
803 
804 
805         Handle( final byte[] exportedSessionKey, final Mode mode, final boolean isConnection ) throws NTLMEngineException
806         {
807             this.isConnection = isConnection;
808             this.mode = mode;
809             try
810             {
811                 final MessageDigest signMd5 = getMD5();
812                 final MessageDigest sealMd5 = getMD5();
813                 signMd5.update( exportedSessionKey );
814                 sealMd5.update( exportedSessionKey );
815                 if ( mode == Mode.CLIENT )
816                 {
817                     signMd5.update( SIGN_MAGIC_CLIENT );
818                     sealMd5.update( SEAL_MAGIC_CLIENT );
819                 }
820                 else
821                 {
822                     signMd5.update( SIGN_MAGIC_SERVER );
823                     sealMd5.update( SEAL_MAGIC_SERVER );
824                 }
825                 signingKey = signMd5.digest();
826                 sealingKey = sealMd5.digest();
827             }
828             catch ( final Exception e )
829             {
830                 throw new NTLMEngineException( e.getMessage(), e );
831             }
832             rc4 = initCipher();
833         }
834 
835         public byte[] getSigningKey()
836         {
837             return signingKey;
838         }
839 
840 
841         public byte[] getSealingKey()
842         {
843             return sealingKey;
844         }
845 
846         private Cipher initCipher() throws NTLMEngineException
847         {
848             final Cipher cipher;
849             try
850             {
851                 cipher = Cipher.getInstance( "RC4" );
852                 if ( mode == Mode.CLIENT )
853                 {
854                     cipher.init( Cipher.ENCRYPT_MODE, new SecretKeySpec( sealingKey, "RC4" ) );
855                 }
856                 else
857                 {
858                     cipher.init( Cipher.DECRYPT_MODE, new SecretKeySpec( sealingKey, "RC4" ) );
859                 }
860             }
861             catch ( final Exception e )
862             {
863                 throw new NTLMEngineException( e.getMessage(), e );
864             }
865             return cipher;
866         }
867 
868 
869         private void advanceMessageSequence() throws NTLMEngineException
870         {
871             if ( !isConnection )
872             {
873                 final MessageDigest sealMd5 = getMD5();
874                 sealMd5.update( sealingKey );
875                 final byte[] seqNumBytes = new byte[4];
876                 writeULong( seqNumBytes, sequenceNumber, 0 );
877                 sealMd5.update( seqNumBytes );
878                 sealingKey = sealMd5.digest();
879                 initCipher();
880             }
881             sequenceNumber++;
882         }
883 
884         private byte[] encrypt( final byte[] data )
885         {
886             return rc4.update( data );
887         }
888 
889         private byte[] decrypt( final byte[] data )
890         {
891             return rc4.update( data );
892         }
893 
894         private byte[] computeSignature( final byte[] message )
895         {
896             final byte[] sig = new byte[16];
897 
898             // version
899             sig[0] = 0x01;
900             sig[1] = 0x00;
901             sig[2] = 0x00;
902             sig[3] = 0x00;
903 
904             // HMAC (first 8 bytes)
905             final HMACMD5 hmacMD5 = new HMACMD5( signingKey );
906             hmacMD5.update( encodeLong( sequenceNumber ) );
907             hmacMD5.update( message );
908             final byte[] hmac = hmacMD5.getOutput();
909             final byte[] trimmedHmac = new byte[8];
910             System.arraycopy( hmac, 0, trimmedHmac, 0, 8 );
911             final byte[] encryptedHmac = encrypt( trimmedHmac );
912             System.arraycopy( encryptedHmac, 0, sig, 4, 8 );
913 
914             // sequence number
915             encodeLong( sig, 12, sequenceNumber );
916 
917             return sig;
918         }
919 
920         private boolean validateSignature( final byte[] signature, final byte[] message )
921         {
922             final byte[] computedSignature = computeSignature( message );
923             //            log.info( "SSSSS validateSignature("+seqNumber+")\n"
924             //                + "  received: " + DebugUtil.dump( signature ) + "\n"
925             //                + "  computed: " + DebugUtil.dump( computedSignature ) );
926             return Arrays.equals( signature, computedSignature );
927         }
928 
929         public byte[] signAndEncryptMessage( final byte[] cleartextMessage ) throws NTLMEngineException
930         {
931             final byte[] encryptedMessage = encrypt( cleartextMessage );
932             final byte[] signature = computeSignature( cleartextMessage );
933             final byte[] outMessage = new byte[signature.length + encryptedMessage.length];
934             System.arraycopy( signature, 0, outMessage, 0, signature.length );
935             System.arraycopy( encryptedMessage, 0, outMessage, signature.length, encryptedMessage.length );
936             advanceMessageSequence();
937             return outMessage;
938         }
939 
940         public byte[] decryptAndVerifySignedMessage( final byte[] inMessage ) throws NTLMEngineException
941         {
942             final byte[] signature = new byte[16];
943             System.arraycopy( inMessage, 0, signature, 0, signature.length );
944             final byte[] encryptedMessage = new byte[inMessage.length - 16];
945             System.arraycopy( inMessage, 16, encryptedMessage, 0, encryptedMessage.length );
946             final byte[] cleartextMessage = decrypt( encryptedMessage );
947             if ( !validateSignature( signature, cleartextMessage ) )
948             {
949                 throw new NTLMEngineException( "Wrong signature" );
950             }
951             advanceMessageSequence();
952             return cleartextMessage;
953         }
954 
955     }
956 
957     private static byte[] encodeLong( final int value )
958     {
959         final byte[] enc = new byte[4];
960         encodeLong( enc, 0, value );
961         return enc;
962     }
963 
964     private static void encodeLong( final byte[] buf, final int offset, final int value )
965     {
966         buf[offset + 0] = ( byte ) ( value & 0xff );
967         buf[offset + 1] = ( byte ) ( value >> 8 & 0xff );
968         buf[offset + 2] = ( byte ) ( value >> 16 & 0xff );
969         buf[offset + 3] = ( byte ) ( value >> 24 & 0xff );
970     }
971 
972     /**
973      * Creates the NTLMv2 blob from the given target information block and
974      * client challenge.
975      *
976      * @param targetInformation
977      *            The target information block from the Type 2 message.
978      * @param clientChallenge
979      *            The random 8-byte client challenge.
980      *
981      * @return The blob, used in the calculation of the NTLMv2 Response.
982      */
983     private static byte[] createBlob(final byte[] clientChallenge, final byte[] targetInformation, final byte[] timestamp) {
984         final byte[] blobSignature = new byte[] { (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00 };
985         final byte[] reserved = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
986         final byte[] unknown1 = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
987         final byte[] unknown2 = new byte[] { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
988         final byte[] blob = new byte[blobSignature.length + reserved.length + timestamp.length + 8
989                 + unknown1.length + targetInformation.length + unknown2.length];
990         int offset = 0;
991         System.arraycopy(blobSignature, 0, blob, offset, blobSignature.length);
992         offset += blobSignature.length;
993         System.arraycopy(reserved, 0, blob, offset, reserved.length);
994         offset += reserved.length;
995         System.arraycopy(timestamp, 0, blob, offset, timestamp.length);
996         offset += timestamp.length;
997         System.arraycopy(clientChallenge, 0, blob, offset, 8);
998         offset += 8;
999         System.arraycopy(unknown1, 0, blob, offset, unknown1.length);
1000         offset += unknown1.length;
1001         System.arraycopy(targetInformation, 0, blob, offset, targetInformation.length);
1002         offset += targetInformation.length;
1003         System.arraycopy(unknown2, 0, blob, offset, unknown2.length);
1004         offset += unknown2.length;
1005         return blob;
1006     }
1007 
1008     /**
1009      * Creates a DES encryption key from the given key material.
1010      *
1011      * @param bytes
1012      *            A byte array containing the DES key material.
1013      * @param offset
1014      *            The offset in the given byte array at which the 7-byte key
1015      *            material starts.
1016      *
1017      * @return A DES encryption key created from the key material starting at
1018      *         the specified offset in the given byte array.
1019      */
1020     private static Key createDESKey(final byte[] bytes, final int offset) {
1021         final byte[] keyBytes = new byte[7];
1022         System.arraycopy(bytes, offset, keyBytes, 0, 7);
1023         final byte[] material = new byte[8];
1024         material[0] = keyBytes[0];
1025         material[1] = (byte) (keyBytes[0] << 7 | (keyBytes[1] & 0xff) >>> 1);
1026         material[2] = (byte) (keyBytes[1] << 6 | (keyBytes[2] & 0xff) >>> 2);
1027         material[3] = (byte) (keyBytes[2] << 5 | (keyBytes[3] & 0xff) >>> 3);
1028         material[4] = (byte) (keyBytes[3] << 4 | (keyBytes[4] & 0xff) >>> 4);
1029         material[5] = (byte) (keyBytes[4] << 3 | (keyBytes[5] & 0xff) >>> 5);
1030         material[6] = (byte) (keyBytes[5] << 2 | (keyBytes[6] & 0xff) >>> 6);
1031         material[7] = (byte) (keyBytes[6] << 1);
1032         oddParity(material);
1033         return new SecretKeySpec(material, "DES");
1034     }
1035 
1036     /**
1037      * Applies odd parity to the given byte array.
1038      *
1039      * @param bytes
1040      *            The data whose parity bits are to be adjusted for odd parity.
1041      */
1042     private static void oddParity(final byte[] bytes) {
1043         for (int i = 0; i < bytes.length; i++) {
1044             final byte b = bytes[i];
1045             final boolean needsParity = (((b >>> 7) ^ (b >>> 6) ^ (b >>> 5) ^ (b >>> 4) ^ (b >>> 3)
1046                     ^ (b >>> 2) ^ (b >>> 1)) & 0x01) == 0;
1047             if (needsParity) {
1048                 bytes[i] |= (byte) 0x01;
1049             } else {
1050                 bytes[i] &= (byte) 0xfe;
1051             }
1052         }
1053     }
1054 
1055     /**
1056      * Find the character set based on the flags.
1057      * @param flags is the flags.
1058      * @return the character set.
1059      */
1060     private static Charset getCharset(final int flags) throws NTLMEngineException
1061     {
1062         if ((flags & FLAG_REQUEST_UNICODE_ENCODING) == 0) {
1063             return DEFAULT_CHARSET;
1064         }
1065         return UNICODE_LITTLE_UNMARKED;
1066     }
1067 
1068     /** NTLM message generation, base class */
1069     static class NTLMMessage {
1070         /** The current response */
1071         byte[] messageContents;
1072 
1073         /** The current output position */
1074         int currentOutputPosition;
1075 
1076         /** Constructor to use when message contents are not yet known */
1077         NTLMMessage() {
1078         }
1079 
1080         /** Constructor taking a string */
1081         NTLMMessage(final String messageBody, final int expectedType) throws NTLMEngineException {
1082             this(Base64.decodeBase64(messageBody.getBytes(DEFAULT_CHARSET)), expectedType);
1083         }
1084 
1085         /** Constructor to use when message bytes are known */
1086         NTLMMessage(final byte[] message, final int expectedType) throws NTLMEngineException {
1087             messageContents = message;
1088             // Look for NTLM message
1089             if (messageContents.length < SIGNATURE.length) {
1090                 throw new NTLMEngineException("NTLM message decoding error - packet too short");
1091             }
1092             int i = 0;
1093             while (i < SIGNATURE.length) {
1094                 if (messageContents[i] != SIGNATURE[i]) {
1095                     throw new NTLMEngineException(
1096                             "NTLM message expected - instead got unrecognized bytes");
1097                 }
1098                 i++;
1099             }
1100 
1101             // Check to be sure there's a type 2 message indicator next
1102             final int type = readULong(SIGNATURE.length);
1103             if (type != expectedType) {
1104                 throw new NTLMEngineException("NTLM type " + expectedType
1105                         + " message expected - instead got type " + type);
1106             }
1107 
1108             currentOutputPosition = messageContents.length;
1109         }
1110 
1111         /**
1112          * Get the length of the signature and flags, so calculations can adjust
1113          * offsets accordingly.
1114          */
1115         int getPreambleLength() {
1116             return SIGNATURE.length + 4;
1117         }
1118 
1119         /** Get the message length */
1120         int getMessageLength() {
1121             return currentOutputPosition;
1122         }
1123 
1124         /** Read a byte from a position within the message buffer */
1125         byte readByte(final int position) throws NTLMEngineException {
1126             if (messageContents.length < position + 1) {
1127                 throw new NTLMEngineException("NTLM: Message too short");
1128             }
1129             return messageContents[position];
1130         }
1131 
1132         /** Read a bunch of bytes from a position in the message buffer */
1133         void readBytes(final byte[] buffer, final int position) throws NTLMEngineException {
1134             if (messageContents.length < position + buffer.length) {
1135                 throw new NTLMEngineException("NTLM: Message too short");
1136             }
1137             System.arraycopy(messageContents, position, buffer, 0, buffer.length);
1138         }
1139 
1140         /** Read a ushort from a position within the message buffer */
1141         int readUShort(final int position) {
1142             return NTLMEngineImpl.readUShort(messageContents, position);
1143         }
1144 
1145         /** Read a ulong from a position within the message buffer */
1146         int readULong(final int position) {
1147             return NTLMEngineImpl.readULong(messageContents, position);
1148         }
1149 
1150         /** Read a security buffer from a position within the message buffer */
1151         byte[] readSecurityBuffer(final int position) {
1152             return NTLMEngineImpl.readSecurityBuffer(messageContents, position);
1153         }
1154 
1155         /**
1156          * Prepares the object to create a response of the given length.
1157          *
1158          * @param maxlength
1159          *            the maximum length of the response to prepare,
1160          *            including the type and the signature (which this method
1161          *            adds).
1162          */
1163         void prepareResponse(final int maxlength, final int messageType) {
1164             messageContents = new byte[maxlength];
1165             currentOutputPosition = 0;
1166             addBytes(SIGNATURE);
1167             addULong(messageType);
1168         }
1169 
1170         /**
1171          * Adds the given byte to the response.
1172          *
1173          * @param b
1174          *            the byte to add.
1175          */
1176         void addByte(final byte b) {
1177             messageContents[currentOutputPosition] = b;
1178             currentOutputPosition++;
1179         }
1180 
1181         /**
1182          * Adds the given bytes to the response.
1183          *
1184          * @param bytes
1185          *            the bytes to add.
1186          */
1187         void addBytes(final byte[] bytes) {
1188             if (bytes == null) {
1189                 return;
1190             }
1191             for (final byte b : bytes) {
1192                 messageContents[currentOutputPosition] = b;
1193                 currentOutputPosition++;
1194             }
1195         }
1196 
1197         /** Adds a USHORT to the response */
1198         void addUShort(final int value) {
1199             addByte((byte) (value & 0xff));
1200             addByte((byte) (value >> 8 & 0xff));
1201         }
1202 
1203         /** Adds a ULong to the response */
1204         void addULong(final int value) {
1205             addByte((byte) (value & 0xff));
1206             addByte((byte) (value >> 8 & 0xff));
1207             addByte((byte) (value >> 16 & 0xff));
1208             addByte((byte) (value >> 24 & 0xff));
1209         }
1210 
1211         /**
1212          * Returns the response that has been generated after shrinking the
1213          * array if required and base64 encodes the response.
1214          *
1215          * @return The response as above.
1216          */
1217         public String getResponse() {
1218             return new String(Base64.encodeBase64(getBytes()), StandardCharsets.US_ASCII);
1219         }
1220 
1221         public byte[] getBytes() {
1222             if (messageContents == null) {
1223                 buildMessage();
1224             }
1225             if (messageContents.length > currentOutputPosition) {
1226                 final byte[] tmp = new byte[currentOutputPosition];
1227                 System.arraycopy( messageContents, 0, tmp, 0, currentOutputPosition );
1228                 messageContents = tmp;
1229             }
1230             return messageContents;
1231         }
1232 
1233         void buildMessage() {
1234             throw new RuntimeException("Message builder not implemented for "+getClass().getName());
1235         }
1236     }
1237 
1238     /** Type 1 message assembly class */
1239     static class Type1Message extends NTLMMessage {
1240 
1241         private final byte[] hostBytes;
1242         private final byte[] domainBytes;
1243         private final int flags;
1244 
1245         Type1Message(final String domain, final String host) {
1246             this(domain, host, null);
1247         }
1248 
1249         Type1Message(final String domain, final String host, final Integer flags) {
1250             super();
1251             this.flags = ((flags == null)?getDefaultFlags(): flags.intValue());
1252 
1253             // See HTTPCLIENT-1662
1254             final String unqualifiedHost = host;
1255             final String unqualifiedDomain = domain;
1256 
1257             hostBytes = unqualifiedHost != null ?
1258                     unqualifiedHost.getBytes(UNICODE_LITTLE_UNMARKED) : null;
1259             domainBytes = unqualifiedDomain != null ?
1260                     unqualifiedDomain.toUpperCase(Locale.ROOT).getBytes(UNICODE_LITTLE_UNMARKED) : null;
1261         }
1262 
1263         Type1Message() {
1264             super();
1265             hostBytes = null;
1266             domainBytes = null;
1267             flags = getDefaultFlags();
1268         }
1269 
1270         private int getDefaultFlags() {
1271             return
1272                 //FLAG_WORKSTATION_PRESENT |
1273                 //FLAG_DOMAIN_PRESENT |
1274 
1275                 // Required flags
1276                 //FLAG_REQUEST_LAN_MANAGER_KEY |
1277                 FLAG_REQUEST_NTLMv1 |
1278                 FLAG_REQUEST_NTLM2_SESSION |
1279 
1280                 // Protocol version request
1281                 FLAG_REQUEST_VERSION |
1282 
1283                 // Recommended privacy settings
1284                 FLAG_REQUEST_ALWAYS_SIGN |
1285                 //FLAG_REQUEST_SEAL |
1286                 //FLAG_REQUEST_SIGN |
1287 
1288                 // These must be set according to documentation, based on use of SEAL above
1289                 FLAG_REQUEST_128BIT_KEY_EXCH |
1290                 FLAG_REQUEST_56BIT_ENCRYPTION |
1291                 //FLAG_REQUEST_EXPLICIT_KEY_EXCH |
1292 
1293                 FLAG_REQUEST_UNICODE_ENCODING;
1294 
1295         }
1296 
1297         /**
1298          * Getting the response involves building the message before returning
1299          * it
1300          */
1301         @Override
1302         void buildMessage() {
1303             int domainBytesLength = 0;
1304             if ( domainBytes != null ) {
1305                 domainBytesLength = domainBytes.length;
1306             }
1307             int hostBytesLength = 0;
1308             if ( hostBytes != null ) {
1309                 hostBytesLength = hostBytes.length;
1310             }
1311 
1312             // Now, build the message. Calculate its length first, including
1313             // signature or type.
1314             final int finalLength = 32 + 8 + hostBytesLength + domainBytesLength;
1315 
1316             // Set up the response. This will initialize the signature, message
1317             // type, and flags.
1318             prepareResponse(finalLength, 1);
1319 
1320             // Flags. These are the complete set of flags we support.
1321             addULong(flags);
1322 
1323             // Domain length (two times).
1324             addUShort(domainBytesLength);
1325             addUShort(domainBytesLength);
1326 
1327             // Domain offset.
1328             addULong(hostBytesLength + 32 + 8);
1329 
1330             // Host length (two times).
1331             addUShort(hostBytesLength);
1332             addUShort(hostBytesLength);
1333 
1334             // Host offset (always 32 + 8).
1335             addULong(32 + 8);
1336 
1337             // Version
1338             addUShort(0x0105);
1339             // Build
1340             addULong(2600);
1341             // NTLM revision
1342             addUShort(0x0f00);
1343 
1344             // Host (workstation) String.
1345             if (hostBytes != null) {
1346                 addBytes(hostBytes);
1347             }
1348             // Domain String.
1349             if (domainBytes != null) {
1350                 addBytes(domainBytes);
1351             }
1352         }
1353 
1354     }
1355 
1356     /** Type 2 message class */
1357     static class Type2Message extends NTLMMessage {
1358         final byte[] challenge;
1359         String target;
1360         byte[] targetInfo;
1361         final int flags;
1362 
1363         Type2Message(final String messageBody) throws NTLMEngineException {
1364             this(Base64.decodeBase64(messageBody.getBytes(DEFAULT_CHARSET)));
1365         }
1366 
1367         Type2Message(final byte[] message) throws NTLMEngineException {
1368             super(message, 2);
1369 
1370             // Type 2 message is laid out as follows:
1371             // First 8 bytes: NTLMSSP[0]
1372             // Next 4 bytes: Ulong, value 2
1373             // Next 8 bytes, starting at offset 12: target field (2 ushort lengths, 1 ulong offset)
1374             // Next 4 bytes, starting at offset 20: Flags, e.g. 0x22890235
1375             // Next 8 bytes, starting at offset 24: Challenge
1376             // Next 8 bytes, starting at offset 32: ??? (8 bytes of zeros)
1377             // Next 8 bytes, starting at offset 40: targetinfo field (2 ushort lengths, 1 ulong offset)
1378             // Next 2 bytes, major/minor version number (e.g. 0x05 0x02)
1379             // Next 8 bytes, build number
1380             // Next 2 bytes, protocol version number (e.g. 0x00 0x0f)
1381             // Next, various text fields, and a ushort of value 0 at the end
1382 
1383             // Parse out the rest of the info we need from the message
1384             // The nonce is the 8 bytes starting from the byte in position 24.
1385             challenge = new byte[8];
1386             readBytes(challenge, 24);
1387 
1388             flags = readULong(20);
1389 
1390             // Do the target!
1391             target = null;
1392             // The TARGET_DESIRED flag is said to not have understood semantics
1393             // in Type2 messages, so use the length of the packet to decide
1394             // how to proceed instead
1395             if (getMessageLength() >= 12 + 8) {
1396                 final byte[] bytes = readSecurityBuffer(12);
1397                 if (bytes.length != 0) {
1398                     target = new String(bytes, getCharset(flags));
1399                 }
1400             }
1401 
1402             // Do the target info!
1403             targetInfo = null;
1404             // TARGET_DESIRED flag cannot be relied on, so use packet length
1405             if (getMessageLength() >= 40 + 8) {
1406                 final byte[] bytes = readSecurityBuffer(40);
1407                 if (bytes.length != 0) {
1408                     targetInfo = bytes;
1409                 }
1410             }
1411         }
1412 
1413         /** Retrieve the challenge */
1414         byte[] getChallenge() {
1415             return challenge;
1416         }
1417 
1418         /** Retrieve the target */
1419         String getTarget() {
1420             return target;
1421         }
1422 
1423         /** Retrieve the target info */
1424         byte[] getTargetInfo() {
1425             return targetInfo;
1426         }
1427 
1428         /** Retrieve the response flags */
1429         int getFlags() {
1430             return flags;
1431         }
1432 
1433     }
1434 
1435     /** Type 3 message assembly class */
1436     static class Type3Message extends NTLMMessage {
1437         // For mic computation
1438         final byte[] type1Message;
1439         final byte[] type2Message;
1440         // Response flags from the type2 message
1441         final int type2Flags;
1442 
1443         final byte[] domainBytes;
1444         final byte[] hostBytes;
1445         final byte[] userBytes;
1446 
1447         byte[] lmResp;
1448         byte[] ntResp;
1449         final byte[] sessionKey;
1450         final byte[] exportedSessionKey;
1451 
1452         final boolean computeMic;
1453 
1454         /** More primitive constructor: don't include cert or previous messages.
1455         */
1456         Type3Message(final String domain,
1457             final String host,
1458             final String user,
1459             final char[] password,
1460             final byte[] nonce,
1461             final int type2Flags,
1462             final String target,
1463             final byte[] targetInformation)
1464             throws NTLMEngineException {
1465             this(domain, host, user, password, nonce, type2Flags, target, targetInformation, null, null, null);
1466         }
1467 
1468         /** More primitive constructor: don't include cert or previous messages.
1469         */
1470         Type3Message(final Random random, final long currentTime,
1471             final String domain,
1472             final String host,
1473             final String user,
1474             final char[] password,
1475             final byte[] nonce,
1476             final int type2Flags,
1477             final String target,
1478             final byte[] targetInformation)
1479             throws NTLMEngineException {
1480             this(random, currentTime, domain, host, user, password, nonce, type2Flags, target, targetInformation, null, null, null);
1481         }
1482 
1483         /** Constructor. Pass the arguments we will need */
1484         Type3Message(final String domain,
1485             final String host,
1486             final String user,
1487             final char[] password,
1488             final byte[] nonce,
1489             final int type2Flags,
1490             final String target,
1491             final byte[] targetInformation,
1492             final Certificate peerServerCertificate,
1493             final byte[] type1Message,
1494             final byte[] type2Message)
1495             throws NTLMEngineException {
1496             this(RND_GEN, System.currentTimeMillis(), domain, host, user, password, nonce, type2Flags, target, targetInformation, peerServerCertificate, type1Message, type2Message);
1497         }
1498 
1499         /** Constructor. Pass the arguments we will need */
1500         Type3Message(final Random random, final long currentTime,
1501             final String domain,
1502             final String host,
1503             final String user,
1504             final char[] password,
1505             final byte[] nonce,
1506             final int type2Flags,
1507             final String target,
1508             final byte[] targetInformation,
1509             final Certificate peerServerCertificate,
1510             final byte[] type1Message,
1511             final byte[] type2Message)
1512             throws NTLMEngineException {
1513 
1514             if (random == null) {
1515                 throw new NTLMEngineException("Random generator not available");
1516             }
1517 
1518             // Save the flags
1519             this.type2Flags = type2Flags;
1520             this.type1Message = type1Message;
1521             this.type2Message = type2Message;
1522 
1523             // All host name manipulations now take place in the credentials
1524             final String unqualifiedHost = host;
1525             // All domain name manipulations now take place in the credentials
1526             final String unqualifiedDomain = domain;
1527 
1528             byte[] responseTargetInformation = targetInformation;
1529             if (peerServerCertificate != null) {
1530                 responseTargetInformation = addGssMicAvsToTargetInfo(targetInformation, peerServerCertificate);
1531                 computeMic = true;
1532             } else {
1533                 computeMic = false;
1534             }
1535 
1536              // Create a cipher generator class.  Use domain BEFORE it gets modified!
1537             final CipherGen gen = new CipherGen(random, currentTime,
1538                 unqualifiedDomain,
1539                 user,
1540                 password,
1541                 nonce,
1542                 target,
1543                 responseTargetInformation);
1544 
1545             // Use the new code to calculate the responses, including v2 if that
1546             // seems warranted.
1547             byte[] userSessionKey;
1548             try {
1549                 // This conditional may not work on Windows Server 2008 R2 and above, where it has not yet
1550                 // been tested
1551                 if (((type2Flags & FLAG_TARGETINFO_PRESENT) != 0) &&
1552                     targetInformation != null && target != null) {
1553                     // NTLMv2
1554                     ntResp = gen.getNTLMv2Response();
1555                     lmResp = gen.getLMv2Response();
1556                     if ((type2Flags & FLAG_REQUEST_LAN_MANAGER_KEY) != 0) {
1557                         userSessionKey = gen.getLanManagerSessionKey();
1558                     } else {
1559                         userSessionKey = gen.getNTLMv2UserSessionKey();
1560                     }
1561                 } else {
1562                     // NTLMv1
1563                     if ((type2Flags & FLAG_REQUEST_NTLM2_SESSION) != 0) {
1564                         // NTLM2 session stuff is requested
1565                         ntResp = gen.getNTLM2SessionResponse();
1566                         lmResp = gen.getLM2SessionResponse();
1567                         if ((type2Flags & FLAG_REQUEST_LAN_MANAGER_KEY) != 0) {
1568                             userSessionKey = gen.getLanManagerSessionKey();
1569                         } else {
1570                             userSessionKey = gen.getNTLM2SessionResponseUserSessionKey();
1571                         }
1572                     } else {
1573                         ntResp = gen.getNTLMResponse();
1574                         lmResp = gen.getLMResponse();
1575                         if ((type2Flags & FLAG_REQUEST_LAN_MANAGER_KEY) != 0) {
1576                             userSessionKey = gen.getLanManagerSessionKey();
1577                         } else {
1578                             userSessionKey = gen.getNTLMUserSessionKey();
1579                         }
1580                     }
1581                 }
1582             } catch (final NTLMEngineException e) {
1583                 // This likely means we couldn't find the MD4 hash algorithm -
1584                 // fail back to just using LM
1585                 ntResp = new byte[0];
1586                 lmResp = gen.getLMResponse();
1587                 if ((type2Flags & FLAG_REQUEST_LAN_MANAGER_KEY) != 0) {
1588                     userSessionKey = gen.getLanManagerSessionKey();
1589                 } else {
1590                     userSessionKey = gen.getLMUserSessionKey();
1591                 }
1592             }
1593 
1594             if ((type2Flags & FLAG_REQUEST_SIGN) != 0) {
1595                 if ((type2Flags & FLAG_REQUEST_EXPLICIT_KEY_EXCH) != 0) {
1596                     exportedSessionKey = gen.getSecondaryKey();
1597                     sessionKey = RC4(exportedSessionKey, userSessionKey);
1598                 } else {
1599                     sessionKey = userSessionKey;
1600                     exportedSessionKey = sessionKey;
1601                 }
1602             } else {
1603                 if (computeMic) {
1604                     throw new NTLMEngineException("Cannot sign/seal: no exported session key");
1605                 }
1606                 sessionKey = null;
1607                 exportedSessionKey = null;
1608             }
1609             final Charset charset = getCharset(type2Flags);
1610             hostBytes = unqualifiedHost != null ? unqualifiedHost.getBytes(charset) : null;
1611              domainBytes = unqualifiedDomain != null ? unqualifiedDomain
1612                 .toUpperCase(Locale.ROOT).getBytes(charset) : null;
1613             userBytes = user.getBytes(charset);
1614         }
1615 
1616         public byte[] getEncryptedRandomSessionKey() {
1617             return sessionKey;
1618         }
1619 
1620         public byte[] getExportedSessionKey() {
1621             return exportedSessionKey;
1622         }
1623 
1624         /** Assemble the response */
1625         @Override
1626         void buildMessage() {
1627             final int ntRespLen = ntResp.length;
1628             final int lmRespLen = lmResp.length;
1629 
1630             final int domainLen = domainBytes != null ? domainBytes.length : 0;
1631             final int hostLen = hostBytes != null ? hostBytes.length: 0;
1632             final int userLen = userBytes.length;
1633             final int sessionKeyLen;
1634             if (sessionKey != null) {
1635                 sessionKeyLen = sessionKey.length;
1636             } else {
1637                 sessionKeyLen = 0;
1638             }
1639 
1640             // Calculate the layout within the packet
1641             final int lmRespOffset = 72 + // allocate space for the version
1642                 ( computeMic ? 16 : 0 ); // and MIC
1643             final int ntRespOffset = lmRespOffset + lmRespLen;
1644             final int domainOffset = ntRespOffset + ntRespLen;
1645             final int userOffset = domainOffset + domainLen;
1646             final int hostOffset = userOffset + userLen;
1647             final int sessionKeyOffset = hostOffset + hostLen;
1648             final int finalLength = sessionKeyOffset + sessionKeyLen;
1649 
1650             // Start the response. Length includes signature and type
1651             prepareResponse(finalLength, 3);
1652 
1653             // LM Resp Length (twice)
1654             addUShort(lmRespLen);
1655             addUShort(lmRespLen);
1656 
1657             // LM Resp Offset
1658             addULong(lmRespOffset);
1659 
1660             // NT Resp Length (twice)
1661             addUShort(ntRespLen);
1662             addUShort(ntRespLen);
1663 
1664             // NT Resp Offset
1665             addULong(ntRespOffset);
1666 
1667             // Domain length (twice)
1668             addUShort(domainLen);
1669             addUShort(domainLen);
1670 
1671             // Domain offset.
1672             addULong(domainOffset);
1673 
1674             // User Length (twice)
1675             addUShort(userLen);
1676             addUShort(userLen);
1677 
1678             // User offset
1679             addULong(userOffset);
1680 
1681             // Host length (twice)
1682             addUShort(hostLen);
1683             addUShort(hostLen);
1684 
1685             // Host offset
1686             addULong(hostOffset);
1687 
1688             // Session key length (twice)
1689             addUShort(sessionKeyLen);
1690             addUShort(sessionKeyLen);
1691 
1692             // Session key offset
1693             addULong(sessionKeyOffset);
1694 
1695             // Flags.
1696             addULong(
1697                     /*
1698                     //FLAG_WORKSTATION_PRESENT |
1699                     //FLAG_DOMAIN_PRESENT |
1700 
1701                     // Required flags
1702                     (type2Flags & FLAG_REQUEST_LAN_MANAGER_KEY) |
1703                     (type2Flags & FLAG_REQUEST_NTLMv1) |
1704                     (type2Flags & FLAG_REQUEST_NTLM2_SESSION) |
1705 
1706                     // Protocol version request
1707                     FLAG_REQUEST_VERSION |
1708 
1709                     // Recommended privacy settings
1710                     (type2Flags & FLAG_REQUEST_ALWAYS_SIGN) |
1711                     (type2Flags & FLAG_REQUEST_SEAL) |
1712                     (type2Flags & FLAG_REQUEST_SIGN) |
1713 
1714                     // These must be set according to documentation, based on use of SEAL above
1715                     (type2Flags & FLAG_REQUEST_128BIT_KEY_EXCH) |
1716                     (type2Flags & FLAG_REQUEST_56BIT_ENCRYPTION) |
1717                     (type2Flags & FLAG_REQUEST_EXPLICIT_KEY_EXCH) |
1718 
1719                     (type2Flags & FLAG_TARGETINFO_PRESENT) |
1720                     (type2Flags & FLAG_REQUEST_UNICODE_ENCODING) |
1721                     (type2Flags & FLAG_REQUEST_TARGET)
1722                         */
1723                 type2Flags
1724             );
1725 
1726             // Version
1727             addUShort(0x0105);
1728             // Build
1729             addULong(2600);
1730             // NTLM revision
1731             addUShort(0x0f00);
1732 
1733             int micPosition = -1;
1734             if ( computeMic ) {
1735                 micPosition = currentOutputPosition;
1736                 currentOutputPosition += 16;
1737             }
1738 
1739             // Add the actual data
1740             addBytes(lmResp);
1741             addBytes(ntResp);
1742             addBytes(domainBytes);
1743             addBytes(userBytes);
1744             addBytes(hostBytes);
1745             if (sessionKey != null) {
1746                 addBytes(sessionKey);
1747             }
1748 
1749             // Write the mic back into its slot in the message
1750 
1751             if (computeMic) {
1752                 // Computation of message integrity code (MIC) as specified in [MS-NLMP] section 3.2.5.1.2.
1753                 final HMACMD5 hmacMD5 = new HMACMD5( exportedSessionKey );
1754                 hmacMD5.update( type1Message );
1755                 hmacMD5.update( type2Message );
1756                 hmacMD5.update( messageContents );
1757                 final byte[] mic = hmacMD5.getOutput();
1758                 System.arraycopy( mic, 0, messageContents, micPosition, mic.length );
1759             }
1760         }
1761 
1762         /**
1763          * Add GSS channel binding hash and MIC flag to the targetInfo.
1764          * Looks like this is needed if we want to use exported session key for GSS wrapping.
1765          */
1766         private byte[] addGssMicAvsToTargetInfo( final byte[] originalTargetInfo,
1767             final Certificate peerServerCertificate ) throws NTLMEngineException
1768         {
1769             final byte[] newTargetInfo = new byte[originalTargetInfo.length + 8 + 20];
1770             final int appendLength = originalTargetInfo.length - 4; // last tag is MSV_AV_EOL, do not copy that
1771             System.arraycopy( originalTargetInfo, 0, newTargetInfo, 0, appendLength );
1772             writeUShort( newTargetInfo, MSV_AV_FLAGS, appendLength );
1773             writeUShort( newTargetInfo, 4, appendLength + 2 );
1774             writeULong( newTargetInfo, MSV_AV_FLAGS_MIC, appendLength + 4 );
1775             writeUShort( newTargetInfo, MSV_AV_CHANNEL_BINDINGS, appendLength + 8 );
1776             writeUShort( newTargetInfo, 16, appendLength + 10 );
1777 
1778             final byte[] channelBindingsHash;
1779             try
1780             {
1781                 final byte[] certBytes = peerServerCertificate.getEncoded();
1782                 final MessageDigest sha256 = MessageDigest.getInstance( "SHA-256" );
1783                 final byte[] certHashBytes = sha256.digest( certBytes );
1784                 final byte[] channelBindingStruct = new byte[16 + 4 + MAGIC_TLS_SERVER_ENDPOINT.length
1785                     + certHashBytes.length];
1786                 writeULong( channelBindingStruct, 0x00000035, 16 );
1787                 System.arraycopy( MAGIC_TLS_SERVER_ENDPOINT, 0, channelBindingStruct, 20,
1788                     MAGIC_TLS_SERVER_ENDPOINT.length );
1789                 System.arraycopy( certHashBytes, 0, channelBindingStruct, 20 + MAGIC_TLS_SERVER_ENDPOINT.length,
1790                     certHashBytes.length );
1791                 final MessageDigest md5 = getMD5();
1792                 channelBindingsHash = md5.digest( channelBindingStruct );
1793             }
1794             catch (final CertificateEncodingException | NoSuchAlgorithmException e )
1795             {
1796                 throw new NTLMEngineException( e.getMessage(), e );
1797             }
1798 
1799             System.arraycopy( channelBindingsHash, 0, newTargetInfo, appendLength + 12, 16 );
1800             return newTargetInfo;
1801          }
1802 
1803     }
1804 
1805     static void writeUShort(final byte[] buffer, final int value, final int offset) {
1806         buffer[offset] = ( byte ) ( value & 0xff );
1807         buffer[offset + 1] = ( byte ) ( value >> 8 & 0xff );
1808     }
1809 
1810     static void writeULong(final byte[] buffer, final int value, final int offset) {
1811         buffer[offset] = (byte) (value & 0xff);
1812         buffer[offset + 1] = (byte) (value >> 8 & 0xff);
1813         buffer[offset + 2] = (byte) (value >> 16 & 0xff);
1814         buffer[offset + 3] = (byte) (value >> 24 & 0xff);
1815     }
1816 
1817     static int F(final int x, final int y, final int z) {
1818         return ((x & y) | (~x & z));
1819     }
1820 
1821     static int G(final int x, final int y, final int z) {
1822         return ((x & y) | (x & z) | (y & z));
1823     }
1824 
1825     static int H(final int x, final int y, final int z) {
1826         return (x ^ y ^ z);
1827     }
1828 
1829     static int rotintlft(final int val, final int numbits) {
1830         return ((val << numbits) | (val >>> (32 - numbits)));
1831     }
1832 
1833     static MessageDigest getMD5() {
1834         try {
1835             return MessageDigest.getInstance("MD5");
1836         } catch (final NoSuchAlgorithmException ex) {
1837             throw new RuntimeException("MD5 message digest doesn't seem to exist - fatal error: "+ex.getMessage(), ex);
1838         }
1839     }
1840 
1841     /**
1842      * Cryptography support - MD4. The following class was based loosely on the
1843      * RFC and on code found at http://www.cs.umd.edu/~harry/jotp/src/md.java.
1844      * Code correctness was verified by looking at MD4.java from the jcifs
1845      * library (http://jcifs.samba.org). It was massaged extensively to the
1846      * final form found here by Karl Wright (kwright@metacarta.com).
1847      */
1848     static class MD4 {
1849         int A = 0x67452301;
1850         int B = 0xefcdab89;
1851         int C = 0x98badcfe;
1852         int D = 0x10325476;
1853         long count;
1854         final byte[] dataBuffer = new byte[64];
1855 
1856         MD4() {
1857         }
1858 
1859         void update(final byte[] input) {
1860             // We always deal with 512 bits at a time. Correspondingly, there is
1861             // a buffer 64 bytes long that we write data into until it gets
1862             // full.
1863             int curBufferPos = (int) (count & 63L);
1864             int inputIndex = 0;
1865             while (input.length - inputIndex + curBufferPos >= dataBuffer.length) {
1866                 // We have enough data to do the next step. Do a partial copy
1867                 // and a transform, updating inputIndex and curBufferPos
1868                 // accordingly
1869                 final int transferAmt = dataBuffer.length - curBufferPos;
1870                 System.arraycopy(input, inputIndex, dataBuffer, curBufferPos, transferAmt);
1871                 count += transferAmt;
1872                 curBufferPos = 0;
1873                 inputIndex += transferAmt;
1874                 processBuffer();
1875             }
1876 
1877             // If there's anything left, copy it into the buffer and leave it.
1878             // We know there's not enough left to process.
1879             if (inputIndex < input.length) {
1880                 final int transferAmt = input.length - inputIndex;
1881                 System.arraycopy(input, inputIndex, dataBuffer, curBufferPos, transferAmt);
1882                 count += transferAmt;
1883                 curBufferPos += transferAmt;
1884             }
1885         }
1886 
1887         byte[] getOutput() {
1888             // Feed pad/length data into engine. This must round out the input
1889             // to a multiple of 512 bits.
1890             final int bufferIndex = (int) (count & 63L);
1891             final int padLen = (bufferIndex < 56) ? (56 - bufferIndex) : (120 - bufferIndex);
1892             final byte[] postBytes = new byte[padLen + 8];
1893             // Leading 0x80, specified amount of zero padding, then length in
1894             // bits.
1895             postBytes[0] = (byte) 0x80;
1896             // Fill out the last 8 bytes with the length
1897             for (int i = 0; i < 8; i++) {
1898                 postBytes[padLen + i] = (byte) ((count * 8) >>> (8 * i));
1899             }
1900 
1901             // Update the engine
1902             update(postBytes);
1903 
1904             // Calculate final result
1905             final byte[] result = new byte[16];
1906             writeULong(result, A, 0);
1907             writeULong(result, B, 4);
1908             writeULong(result, C, 8);
1909             writeULong(result, D, 12);
1910             return result;
1911         }
1912 
1913         void processBuffer() {
1914             // Convert current buffer to 16 ulongs
1915             final int[] d = new int[16];
1916 
1917             for (int i = 0; i < 16; i++) {
1918                 d[i] = (dataBuffer[i * 4] & 0xff) + ((dataBuffer[i * 4 + 1] & 0xff) << 8)
1919                         + ((dataBuffer[i * 4 + 2] & 0xff) << 16)
1920                         + ((dataBuffer[i * 4 + 3] & 0xff) << 24);
1921             }
1922 
1923             // Do a round of processing
1924             final int AA = A;
1925             final int BB = B;
1926             final int CC = C;
1927             final int DD = D;
1928             round1(d);
1929             round2(d);
1930             round3(d);
1931             A += AA;
1932             B += BB;
1933             C += CC;
1934             D += DD;
1935 
1936         }
1937 
1938         void round1(final int[] d) {
1939             A = rotintlft((A + F(B, C, D) + d[0]), 3);
1940             D = rotintlft((D + F(A, B, C) + d[1]), 7);
1941             C = rotintlft((C + F(D, A, B) + d[2]), 11);
1942             B = rotintlft((B + F(C, D, A) + d[3]), 19);
1943 
1944             A = rotintlft((A + F(B, C, D) + d[4]), 3);
1945             D = rotintlft((D + F(A, B, C) + d[5]), 7);
1946             C = rotintlft((C + F(D, A, B) + d[6]), 11);
1947             B = rotintlft((B + F(C, D, A) + d[7]), 19);
1948 
1949             A = rotintlft((A + F(B, C, D) + d[8]), 3);
1950             D = rotintlft((D + F(A, B, C) + d[9]), 7);
1951             C = rotintlft((C + F(D, A, B) + d[10]), 11);
1952             B = rotintlft((B + F(C, D, A) + d[11]), 19);
1953 
1954             A = rotintlft((A + F(B, C, D) + d[12]), 3);
1955             D = rotintlft((D + F(A, B, C) + d[13]), 7);
1956             C = rotintlft((C + F(D, A, B) + d[14]), 11);
1957             B = rotintlft((B + F(C, D, A) + d[15]), 19);
1958         }
1959 
1960         void round2(final int[] d) {
1961             A = rotintlft((A + G(B, C, D) + d[0] + 0x5a827999), 3);
1962             D = rotintlft((D + G(A, B, C) + d[4] + 0x5a827999), 5);
1963             C = rotintlft((C + G(D, A, B) + d[8] + 0x5a827999), 9);
1964             B = rotintlft((B + G(C, D, A) + d[12] + 0x5a827999), 13);
1965 
1966             A = rotintlft((A + G(B, C, D) + d[1] + 0x5a827999), 3);
1967             D = rotintlft((D + G(A, B, C) + d[5] + 0x5a827999), 5);
1968             C = rotintlft((C + G(D, A, B) + d[9] + 0x5a827999), 9);
1969             B = rotintlft((B + G(C, D, A) + d[13] + 0x5a827999), 13);
1970 
1971             A = rotintlft((A + G(B, C, D) + d[2] + 0x5a827999), 3);
1972             D = rotintlft((D + G(A, B, C) + d[6] + 0x5a827999), 5);
1973             C = rotintlft((C + G(D, A, B) + d[10] + 0x5a827999), 9);
1974             B = rotintlft((B + G(C, D, A) + d[14] + 0x5a827999), 13);
1975 
1976             A = rotintlft((A + G(B, C, D) + d[3] + 0x5a827999), 3);
1977             D = rotintlft((D + G(A, B, C) + d[7] + 0x5a827999), 5);
1978             C = rotintlft((C + G(D, A, B) + d[11] + 0x5a827999), 9);
1979             B = rotintlft((B + G(C, D, A) + d[15] + 0x5a827999), 13);
1980 
1981         }
1982 
1983         void round3(final int[] d) {
1984             A = rotintlft((A + H(B, C, D) + d[0] + 0x6ed9eba1), 3);
1985             D = rotintlft((D + H(A, B, C) + d[8] + 0x6ed9eba1), 9);
1986             C = rotintlft((C + H(D, A, B) + d[4] + 0x6ed9eba1), 11);
1987             B = rotintlft((B + H(C, D, A) + d[12] + 0x6ed9eba1), 15);
1988 
1989             A = rotintlft((A + H(B, C, D) + d[2] + 0x6ed9eba1), 3);
1990             D = rotintlft((D + H(A, B, C) + d[10] + 0x6ed9eba1), 9);
1991             C = rotintlft((C + H(D, A, B) + d[6] + 0x6ed9eba1), 11);
1992             B = rotintlft((B + H(C, D, A) + d[14] + 0x6ed9eba1), 15);
1993 
1994             A = rotintlft((A + H(B, C, D) + d[1] + 0x6ed9eba1), 3);
1995             D = rotintlft((D + H(A, B, C) + d[9] + 0x6ed9eba1), 9);
1996             C = rotintlft((C + H(D, A, B) + d[5] + 0x6ed9eba1), 11);
1997             B = rotintlft((B + H(C, D, A) + d[13] + 0x6ed9eba1), 15);
1998 
1999             A = rotintlft((A + H(B, C, D) + d[3] + 0x6ed9eba1), 3);
2000             D = rotintlft((D + H(A, B, C) + d[11] + 0x6ed9eba1), 9);
2001             C = rotintlft((C + H(D, A, B) + d[7] + 0x6ed9eba1), 11);
2002             B = rotintlft((B + H(C, D, A) + d[15] + 0x6ed9eba1), 15);
2003 
2004         }
2005 
2006     }
2007 
2008     /**
2009      * Cryptography support - HMACMD5 - algorithmically based on various web
2010      * resources by Karl Wright
2011      */
2012     static class HMACMD5 {
2013         final byte[] ipad;
2014         final byte[] opad;
2015         final MessageDigest md5;
2016 
2017         HMACMD5(final byte[] input) {
2018             byte[] key = input;
2019             md5 = getMD5();
2020 
2021             // Initialize the pad buffers with the key
2022             ipad = new byte[64];
2023             opad = new byte[64];
2024 
2025             int keyLength = key.length;
2026             if (keyLength > 64) {
2027                 // Use MD5 of the key instead, as described in RFC 2104
2028                 md5.update(key);
2029                 key = md5.digest();
2030                 keyLength = key.length;
2031             }
2032             int i = 0;
2033             while (i < keyLength) {
2034                 ipad[i] = (byte) (key[i] ^ (byte) 0x36);
2035                 opad[i] = (byte) (key[i] ^ (byte) 0x5c);
2036                 i++;
2037             }
2038             while (i < 64) {
2039                 ipad[i] = (byte) 0x36;
2040                 opad[i] = (byte) 0x5c;
2041                 i++;
2042             }
2043 
2044             // Very important: processChallenge the digest with the ipad buffer
2045             md5.reset();
2046             md5.update(ipad);
2047 
2048         }
2049 
2050         /** Grab the current digest. This is the "answer". */
2051         byte[] getOutput() {
2052             final byte[] digest = md5.digest();
2053             md5.update(opad);
2054             return md5.digest(digest);
2055         }
2056 
2057         /** Update by adding a complete array */
2058         void update(final byte[] input) {
2059             md5.update(input);
2060         }
2061 
2062         /** Update the algorithm */
2063         void update(final byte[] input, final int offset, final int length) {
2064             md5.update(input, offset, length);
2065         }
2066 
2067     }
2068 
2069     @Override
2070     public String generateType1Msg(
2071             final String domain,
2072             final String workstation) throws NTLMEngineException {
2073         return getType1Message(workstation, domain);
2074     }
2075 
2076     @Override
2077     public String generateType3Msg(
2078             final String username,
2079             final char[] password,
2080             final String domain,
2081             final String workstation,
2082             final String challenge) throws NTLMEngineException {
2083         final Type2Message t2m = new Type2Message(challenge);
2084         return getType3Message(
2085                 username,
2086                 password,
2087                 workstation,
2088                 domain,
2089                 t2m.getChallenge(),
2090                 t2m.getFlags(),
2091                 t2m.getTarget(),
2092                 t2m.getTargetInfo());
2093     }
2094 
2095 }