11/*
2- * Copyright (c) 2005, 2023 Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2005, 2024 Oracle and/or its affiliates. All rights reserved.
33 *
44 * This program and the accompanying materials are made available under the
55 * terms of the Eclipse Public License v. 2.0, which is available at
@@ -54,14 +54,14 @@ public class Ntlm {
5454 private SecretKeyFactory fac ;
5555 private Cipher cipher ;
5656 private MD4 md4 ;
57- private String hostname ;
58- private String ntdomain ;
59- private String username ;
60- private String password ;
57+ private final String hostname ;
58+ private final String ntdomain ;
59+ private final String username ;
60+ private final String password ;
6161
6262 private Mac hmac ;
6363
64- private MailLogger logger ;
64+ private final MailLogger logger ;
6565
6666 // NTLM flags, as defined in Microsoft NTLM spec
6767 // https://msdn.microsoft.com/en-us/library/cc236621.aspx
@@ -105,7 +105,7 @@ private void init0() {
105105 cipher = Cipher .getInstance ("DES/ECB/NoPadding" );
106106 md4 = new MD4 ();
107107 } catch (NoSuchPaddingException | NoSuchAlgorithmException e ) {
108- assert false ;
108+ assert false : e ;
109109 }
110110 }
111111
@@ -149,7 +149,7 @@ private void copybytes(byte[] dest, int destpos, String src, String enc) {
149149 byte [] x = src .getBytes (enc );
150150 System .arraycopy (x , 0 , dest , destpos , x .length );
151151 } catch (UnsupportedEncodingException e ) {
152- assert false ;
152+ assert false : e ;
153153 }
154154 }
155155
@@ -189,8 +189,8 @@ public String generateType1Msg(int flags, boolean v2) {
189189 if (logger .isLoggable (Level .FINE ))
190190 logger .fine ("type 1 message: " + toHex (msg ));
191191
192- String result = null ;
193- result = new String ( Base64 . getEncoder (). encode ( msg ), StandardCharsets .ISO_8859_1 );
192+ String result = new String ( Base64 . getEncoder (). encode ( msg ),
193+ StandardCharsets .ISO_8859_1 );
194194 return result ;
195195 }
196196
@@ -223,7 +223,7 @@ private byte[] hmacMD5(byte[] key, byte[] text) {
223223 if (hmac == null )
224224 hmac = Mac .getInstance ("HmacMD5" );
225225 } catch (NoSuchAlgorithmException ex ) {
226- throw new AssertionError ();
226+ throw new AssertionError (ex );
227227 }
228228 try {
229229 byte [] nk = new byte [16 ];
@@ -232,15 +232,15 @@ private byte[] hmacMD5(byte[] key, byte[] text) {
232232 hmac .init (skey );
233233 return hmac .doFinal (text );
234234 } catch (InvalidKeyException | RuntimeException ex ) {
235- assert false ;
235+ assert false : ex ;
236236 }
237237 return null ;
238238 }
239239
240240 private byte [] calcLMHash () throws GeneralSecurityException {
241241 byte [] magic = {0x4b , 0x47 , 0x53 , 0x21 , 0x40 , 0x23 , 0x24 , 0x25 };
242- byte [] pwb = null ;
243- pwb = password . toUpperCase ( Locale . ENGLISH ). getBytes ( StandardCharsets .ISO_8859_1 );
242+ byte [] pwb = password . toUpperCase ( Locale . ENGLISH ). getBytes (
243+ StandardCharsets .ISO_8859_1 );
244244 byte [] pwb1 = new byte [14 ];
245245 int len = password .length ();
246246 if (len > 14 )
@@ -268,7 +268,7 @@ private byte[] calcNTHash() throws GeneralSecurityException {
268268 try {
269269 pw = password .getBytes ("UnicodeLittleUnmarked" );
270270 } catch (UnsupportedEncodingException e ) {
271- assert false ;
271+ assert false : e ;
272272 }
273273 byte [] out = md4 .digest (pw );
274274 byte [] result = new byte [21 ];
@@ -315,7 +315,7 @@ private byte[] calcV2Response(byte[] nthash, byte[] blob, byte[] challenge)
315315 getBytes ("UnicodeLittleUnmarked" );
316316 } catch (UnsupportedEncodingException ex ) {
317317 // should never happen
318- assert false ;
318+ assert false : ex ;
319319 }
320320 byte [] ntlmv2hash = hmacMD5 (nthash , txt );
321321 byte [] cb = new byte [blob .length + 8 ];
@@ -332,8 +332,8 @@ public String generateType3Msg(String type2msg) {
332332
333333 /* First decode the type2 message to get the server challenge */
334334 /* challenge is located at type2[24] for 8 bytes */
335- byte [] type2 = null ;
336- type2 = Base64 . getDecoder (). decode ( type2msg .getBytes (StandardCharsets .US_ASCII ));
335+ byte [] type2 = Base64 . getDecoder (). decode (
336+ type2msg .getBytes (StandardCharsets .US_ASCII ));
337337 if (logger .isLoggable (Level .FINE ))
338338 logger .fine ("type 2 message: " + toHex (type2 ));
339339
@@ -366,10 +366,9 @@ public String generateType3Msg(String type2msg) {
366366 writeInt (type3 , 48 , l );
367367 l += hlen ;
368368
369- byte [] msg = null ;
370- byte [] lmresponse = null ;
371- byte [] ntresponse = null ;
372369 int flags = readInt (type2 , 20 );
370+ byte [] lmresponse ;
371+ byte [] ntresponse ;
373372
374373 // did the server agree to NTLMv2?
375374 if ((flags & NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY ) != 0 ) {
@@ -420,17 +419,16 @@ public String generateType3Msg(String type2msg) {
420419 writeInt (type3 , 24 , l );
421420 l += ntresponse .length ;
422421 writeShort (type3 , 56 , l );
422+ writeInt (type3 , 60 , type3flags );
423423
424- msg = new byte [l ];
424+ byte [] msg = new byte [l ];
425425 System .arraycopy (type3 , 0 , msg , 0 , l );
426426
427- writeInt (type3 , 60 , type3flags );
428-
429427 if (logger .isLoggable (Level .FINE ))
430428 logger .fine ("type 3 message: " + toHex (msg ));
431429
432- String result = null ;
433- result = new String ( Base64 . getEncoder (). encode ( msg ), StandardCharsets .ISO_8859_1 );
430+ String result = new String ( Base64 . getEncoder (). encode ( msg ),
431+ StandardCharsets .ISO_8859_1 );
434432 return result ;
435433
436434 } catch (GeneralSecurityException ex ) {
@@ -464,7 +462,7 @@ private void writeInt(byte[] b, int off, int data) {
464462 b [off + 3 ] = (byte ) ((data >> 24 ) & 0xff );
465463 }
466464
467- private static char [] hex =
465+ private static final char [] hex =
468466 {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' };
469467
470468 private static String toHex (byte [] b ) {
0 commit comments