1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package davmail.http;
21
22 import jcifs.ntlmssp.NtlmFlags;
23 import jcifs.ntlmssp.Type1Message;
24 import jcifs.ntlmssp.Type2Message;
25 import jcifs.ntlmssp.Type3Message;
26 import jcifs.util.Base64;
27 import org.apache.http.impl.auth.NTLMEngine;
28 import org.apache.http.impl.auth.NTLMEngineException;
29 import org.apache.log4j.Logger;
30
31 import java.io.IOException;
32
33
34
35
36 public final class JCIFSEngine implements NTLMEngine {
37 static final Logger LOGGER = Logger.getLogger("davmail.http.JCIFSEngine");
38
39 private static final int TYPE_1_FLAGS =
40 NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
41 NtlmFlags.NTLMSSP_NEGOTIATE_OEM_WORKSTATION_SUPPLIED | NtlmFlags.NTLMSSP_NEGOTIATE_OEM_DOMAIN_SUPPLIED |
42 NtlmFlags.NTLMSSP_NEGOTIATE_NTLM | NtlmFlags.NTLMSSP_REQUEST_TARGET |
43 NtlmFlags.NTLMSSP_NEGOTIATE_OEM | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE |
44 NtlmFlags.NTLMSSP_NEGOTIATE_56 | NtlmFlags.NTLMSSP_NEGOTIATE_128;
45
46 public String generateType1Msg(final String domain, final String workstation) {
47 final Type1Message type1Message = new Type1Message(TYPE_1_FLAGS, domain, workstation);
48 LOGGER.debug("Generate Type1Msg "+type1Message);
49 return Base64.encode(type1Message.toByteArray());
50 }
51
52 public String generateType3Msg(final String username, final String password,
53 final String domain, final String workstation, final String challenge)
54 throws NTLMEngineException {
55 Type2Message type2Message;
56 try {
57 type2Message = new Type2Message(Base64.decode(challenge));
58 } catch (final IOException exception) {
59 throw new NTLMEngineException("Invalid NTLM type 2 message", exception);
60 }
61 LOGGER.debug("Received Type2Msg " + type2Message);
62
63
64
65
66 int type3Flags = NtlmFlags.NTLMSSP_NEGOTIATE_NTLM2 | NtlmFlags.NTLMSSP_NEGOTIATE_ALWAYS_SIGN |
67 NtlmFlags.NTLMSSP_NEGOTIATE_NTLM | NtlmFlags.NTLMSSP_NEGOTIATE_UNICODE;
68 Type3Message type3Message = new Type3Message(type2Message, password,
69 domain, username, workstation, type3Flags);
70 LOGGER.debug("Generate Type3Msg "+type3Message);
71 return Base64.encode(type3Message.toByteArray());
72 }
73
74 }