View Javadoc
1   // copy of code from apache-httpclient 4.5.13 package org.apache.http.impl.auth
2   // changes:
3   // - package name, this header, imports
4   // - fix minor errors/typos
5   // - allow class to be extended and flags to be customized (increase many things' visibility to protected and make class non-final)
6   // - expose Type3 message (public) so keys can be gathered
7   // - expose encryption methods
8   // - make flags injectable to Type1 message
9   
10  /*
11   * ====================================================================
12   * Licensed to the Apache Software Foundation (ASF) under one
13   * or more contributor license agreements.  See the NOTICE file
14   * distributed with this work for additional information
15   * regarding copyright ownership.  The ASF licenses this file
16   * to you under the Apache License, Version 2.0 (the
17   * "License");
18  /*-
19   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
20   * WinRM Java Client
21   * ჻჻჻჻჻჻
22   * Copyright 2023 - 2024 Metricshub
23   * ჻჻჻჻჻჻
24   * Licensed under the Apache License, Version 2.0 (the "License");
25   * you may not use this file except in compliance with the License.
26   * You may obtain a copy of the License at
27   *
28   *      http://www.apache.org/licenses/LICENSE-2.0
29   *
30   * Unless required by applicable law or agreed to in writing, software
31   * distributed under the License is distributed on an "AS IS" BASIS,
32   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33   * See the License for the specific language governing permissions and
34   * limitations under the License.
35   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
36   */
37  package org.metricshub.winrm.service.client.auth.ntlm;
38  
39  import org.apache.http.impl.auth.NTLMEngineException;
40  
41  /**
42   * Provides an implementation for NTLMv1, NTLMv2, and NTLM2 Session forms of the NTLM
43   * authentication protocol.
44   *
45   * Code from io.cloudsoft.winrm4j.client.ntlm.forks.httpclient.NTLMEngineImpl
46   * release 0.12.3 @link https://github.com/cloudsoft/winrm4j
47   * io.cloudsoft.winrm4j.client.ntlm.forks.httpclient is a fork of apache-httpclient 4.5.13
48   */
49  class NTLMEngineImpl implements NTLMEngine {
50  
51  	/** Strip dot suffix from a name */
52  	private static String stripDotSuffix(final String value) {
53  		if (value == null) {
54  			return null;
55  		}
56  		final int index = value.indexOf('.');
57  		if (index != -1) {
58  			return value.substring(0, index);
59  		}
60  		return value;
61  	}
62  
63  	/** Convert host to standard form */
64  	static String convertHost(final String host) {
65  		return stripDotSuffix(host);
66  	}
67  
68  	/** Convert domain to standard form */
69  	static String convertDomain(final String domain) {
70  		return stripDotSuffix(domain);
71  	}
72  
73  	@Override
74  	public String generateType1Msg(final String domain, final String workstation) throws NTLMEngineException {
75  		return new Type1Message(null, null, getDefaultFlags()).getResponse();
76  	}
77  
78  	// function overriden in NtlmMasqAsSpnegoScheme
79  	Integer getDefaultFlags() {
80  		return Type1Message.getDefaultFlags();
81  	}
82  
83  	@Override
84  	public String generateType3Msg(
85  		final String username,
86  		final String password,
87  		final String domain,
88  		final String workstation,
89  		final String challenge
90  	) throws NTLMEngineException {
91  		return generateType3MsgObject(username, password, domain, workstation, challenge).getResponse();
92  	}
93  
94  	@Override
95  	public Type3Message generateType3MsgObject(
96  		final String username,
97  		final String password,
98  		final String domain,
99  		final String workstation,
100 		final String challenge
101 	) throws NTLMEngineException {
102 		final Type2Message t2m = new Type2Message(challenge);
103 		return new Type3Message(
104 			domain,
105 			workstation,
106 			username,
107 			password,
108 			t2m.getChallenge(),
109 			t2m.getFlags(),
110 			t2m.getTarget(),
111 			t2m.getTargetInfo()
112 		);
113 	}
114 }