xref: /aosp_15_r20/external/cronet/net/ntlm/ntlm.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1*6777b538SAndroid Build Coastguard Worker // Copyright 2017 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker 
5*6777b538SAndroid Build Coastguard Worker // Based on [MS-NLMP]: NT LAN Manager (NTLM) Authentication Protocol
6*6777b538SAndroid Build Coastguard Worker // Specification version 28.0 [1]. Additional NTLM reference [2].
7*6777b538SAndroid Build Coastguard Worker //
8*6777b538SAndroid Build Coastguard Worker // [1] https://msdn.microsoft.com/en-us/library/cc236621.aspx
9*6777b538SAndroid Build Coastguard Worker // [2] http://davenport.sourceforge.net/ntlm.html
10*6777b538SAndroid Build Coastguard Worker 
11*6777b538SAndroid Build Coastguard Worker #ifndef NET_NTLM_NTLM_H_
12*6777b538SAndroid Build Coastguard Worker #define NET_NTLM_NTLM_H_
13*6777b538SAndroid Build Coastguard Worker 
14*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
15*6777b538SAndroid Build Coastguard Worker #include <stdint.h>
16*6777b538SAndroid Build Coastguard Worker 
17*6777b538SAndroid Build Coastguard Worker #include <memory>
18*6777b538SAndroid Build Coastguard Worker #include <string>
19*6777b538SAndroid Build Coastguard Worker #include <vector>
20*6777b538SAndroid Build Coastguard Worker 
21*6777b538SAndroid Build Coastguard Worker #include "base/containers/span.h"
22*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_piece.h"
23*6777b538SAndroid Build Coastguard Worker #include "net/base/net_export.h"
24*6777b538SAndroid Build Coastguard Worker #include "net/ntlm/ntlm_constants.h"
25*6777b538SAndroid Build Coastguard Worker 
26*6777b538SAndroid Build Coastguard Worker namespace net::ntlm {
27*6777b538SAndroid Build Coastguard Worker 
28*6777b538SAndroid Build Coastguard Worker // Maps the bits in the NTLM Hash into 3 DES keys. The DES keys each have 56
29*6777b538SAndroid Build Coastguard Worker // bits stored in the 7 most significant bits of 8 bytes. The least
30*6777b538SAndroid Build Coastguard Worker // significant bit is undefined and will subsequently be set with odd parity
31*6777b538SAndroid Build Coastguard Worker // prior to use.
32*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void Create3DesKeysFromNtlmHash(
33*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kNtlmHashLen> ntlm_hash,
34*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, 24> keys);
35*6777b538SAndroid Build Coastguard Worker 
36*6777b538SAndroid Build Coastguard Worker // Generates the NTLMv1 Hash and writes the |kNtlmHashLen| byte result to
37*6777b538SAndroid Build Coastguard Worker // |hash|. Defined by NTOWFv1() in [MS-NLMP] Section 3.3.1.
38*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateNtlmHashV1(
39*6777b538SAndroid Build Coastguard Worker     const std::u16string& password,
40*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kNtlmHashLen> hash);
41*6777b538SAndroid Build Coastguard Worker 
42*6777b538SAndroid Build Coastguard Worker // Generates the |kResponseLenV1| byte NTLMv1 response field according to the
43*6777b538SAndroid Build Coastguard Worker // DESL(K, V) function in [MS-NLMP] Section 6.
44*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateResponseDesl(
45*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kNtlmHashLen> hash,
46*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> challenge,
47*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> response);
48*6777b538SAndroid Build Coastguard Worker 
49*6777b538SAndroid Build Coastguard Worker // Generates the NTLM Response field for NTLMv1 without extended session
50*6777b538SAndroid Build Coastguard Worker // security. Defined by ComputeResponse() in [MS-NLMP] Section 3.3.1 for the
51*6777b538SAndroid Build Coastguard Worker // case where NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY is not set.
52*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateNtlmResponseV1(
53*6777b538SAndroid Build Coastguard Worker     const std::u16string& password,
54*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> server_challenge,
55*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> ntlm_response);
56*6777b538SAndroid Build Coastguard Worker 
57*6777b538SAndroid Build Coastguard Worker // Generates both the LM Response and NTLM Response fields for NTLMv1 based
58*6777b538SAndroid Build Coastguard Worker // on the users password and the servers challenge. Both the LM and NTLM
59*6777b538SAndroid Build Coastguard Worker // Response are the result of |GenerateNtlmResponseV1|.
60*6777b538SAndroid Build Coastguard Worker //
61*6777b538SAndroid Build Coastguard Worker // NOTE: This should not be used. The default flags always include session
62*6777b538SAndroid Build Coastguard Worker // security. Session security can however be disabled in NTLMv1 by omitting
63*6777b538SAndroid Build Coastguard Worker // NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY from the flag set used to
64*6777b538SAndroid Build Coastguard Worker // initialize |NtlmClient|.
65*6777b538SAndroid Build Coastguard Worker //
66*6777b538SAndroid Build Coastguard Worker // The default flags include this flag and the client will not be
67*6777b538SAndroid Build Coastguard Worker // downgraded by the server.
68*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateResponsesV1(
69*6777b538SAndroid Build Coastguard Worker     const std::u16string& password,
70*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> server_challenge,
71*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> lm_response,
72*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> ntlm_response);
73*6777b538SAndroid Build Coastguard Worker 
74*6777b538SAndroid Build Coastguard Worker // The LM Response in V1 with extended session security is 8 bytes of the
75*6777b538SAndroid Build Coastguard Worker // |client_challenge| then 16 bytes of zero. This is the value
76*6777b538SAndroid Build Coastguard Worker // LmChallengeResponse in ComputeResponse() when
77*6777b538SAndroid Build Coastguard Worker // NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY is set. See [MS-NLMP] Section
78*6777b538SAndroid Build Coastguard Worker // 3.3.1.
79*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateLMResponseV1WithSessionSecurity(
80*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> client_challenge,
81*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> lm_response);
82*6777b538SAndroid Build Coastguard Worker 
83*6777b538SAndroid Build Coastguard Worker // The |session_hash| is MD5(CONCAT(server_challenge, client_challenge)).
84*6777b538SAndroid Build Coastguard Worker // It is used instead of just |server_challenge| in NTLMv1 when
85*6777b538SAndroid Build Coastguard Worker // NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY is set. See [MS-NLMP] Section
86*6777b538SAndroid Build Coastguard Worker // 3.3.1.
87*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateSessionHashV1WithSessionSecurity(
88*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> server_challenge,
89*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> client_challenge,
90*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kNtlmHashLen> session_hash);
91*6777b538SAndroid Build Coastguard Worker 
92*6777b538SAndroid Build Coastguard Worker // Generates the NTLM Response for NTLMv1 with session security.
93*6777b538SAndroid Build Coastguard Worker // Defined by ComputeResponse() in [MS-NLMP] Section 3.3.1 for the
94*6777b538SAndroid Build Coastguard Worker // case where NTLMSSP_NEGOTIATE_EXTENDED_SESSIONSECURITY is set.
95*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateNtlmResponseV1WithSessionSecurity(
96*6777b538SAndroid Build Coastguard Worker     const std::u16string& password,
97*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> server_challenge,
98*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> client_challenge,
99*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> ntlm_response);
100*6777b538SAndroid Build Coastguard Worker 
101*6777b538SAndroid Build Coastguard Worker // Generates the responses for V1 with extended session security.
102*6777b538SAndroid Build Coastguard Worker // This is also known as NTLM2 (which is not the same as NTLMv2).
103*6777b538SAndroid Build Coastguard Worker // |lm_response| is the result of |GenerateLMResponseV1WithSessionSecurity| and
104*6777b538SAndroid Build Coastguard Worker // |ntlm_response| is the result of |GenerateNtlmResponseV1WithSessionSecurity|.
105*6777b538SAndroid Build Coastguard Worker // See [MS-NLMP] Section 3.3.1.
106*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateResponsesV1WithSessionSecurity(
107*6777b538SAndroid Build Coastguard Worker     const std::u16string& password,
108*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> server_challenge,
109*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> client_challenge,
110*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> lm_response,
111*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kResponseLenV1> ntlm_response);
112*6777b538SAndroid Build Coastguard Worker 
113*6777b538SAndroid Build Coastguard Worker // Generates the NTLMv2 Hash and writes it into |v2_hash|.
114*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateNtlmHashV2(
115*6777b538SAndroid Build Coastguard Worker     const std::u16string& domain,
116*6777b538SAndroid Build Coastguard Worker     const std::u16string& username,
117*6777b538SAndroid Build Coastguard Worker     const std::u16string& password,
118*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kNtlmHashLen> v2_hash);
119*6777b538SAndroid Build Coastguard Worker 
120*6777b538SAndroid Build Coastguard Worker // In this implementation the Proof Input is the first 28 bytes of what
121*6777b538SAndroid Build Coastguard Worker // [MS-NLMP] section 3.3.2 calls "temp". "temp" is part of the input to
122*6777b538SAndroid Build Coastguard Worker // generate the NTLMv2 proof. "temp" is composed of a fixed 28 byte prefix
123*6777b538SAndroid Build Coastguard Worker // (the Proof Input), then the variable length updated target info that is
124*6777b538SAndroid Build Coastguard Worker // sent in the authenticate message, then followed by 4 zero bytes. See
125*6777b538SAndroid Build Coastguard Worker // [MS-NLMP] Section 2.2.2.7.
126*6777b538SAndroid Build Coastguard Worker //
127*6777b538SAndroid Build Coastguard Worker // |timestamp| contains a 64 bit Windows timestamp defined as the number of
128*6777b538SAndroid Build Coastguard Worker // 100 nanosecond ticks since midnight Jan 01, 1601 (UTC).
129*6777b538SAndroid Build Coastguard Worker //
130*6777b538SAndroid Build Coastguard Worker // The format of the returned |proof_input| is;
131*6777b538SAndroid Build Coastguard Worker //
132*6777b538SAndroid Build Coastguard Worker // [0-1]    - 0x0101                              (Version)
133*6777b538SAndroid Build Coastguard Worker // [2-7]    - 0x000000000000                      (Reserved - all zero)
134*6777b538SAndroid Build Coastguard Worker // [8-15]   - |timestamp|                         (Timestamp)
135*6777b538SAndroid Build Coastguard Worker // [16-23]  - |client_challenge|                  (Client challenge)
136*6777b538SAndroid Build Coastguard Worker // [24-27]  - 0x00000000                          (Reserved - all zero)
137*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE std::vector<uint8_t> GenerateProofInputV2(
138*6777b538SAndroid Build Coastguard Worker     uint64_t timestamp,
139*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> client_challenge);
140*6777b538SAndroid Build Coastguard Worker 
141*6777b538SAndroid Build Coastguard Worker // The NTLMv2 Proof is part of the NTLMv2 Response. See NTProofStr in [MS-NLMP]
142*6777b538SAndroid Build Coastguard Worker // Section 3.3.2.
143*6777b538SAndroid Build Coastguard Worker //
144*6777b538SAndroid Build Coastguard Worker // The NTLMv2 Proof is defined as;
145*6777b538SAndroid Build Coastguard Worker //     v2_proof = HMAC_MD5(
146*6777b538SAndroid Build Coastguard Worker //         v2_hash,
147*6777b538SAndroid Build Coastguard Worker //         CONCAT(server_challenge, v2_input, target_info, 0x00000000))
148*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateNtlmProofV2(
149*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kNtlmHashLen> v2_hash,
150*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kChallengeLen> server_challenge,
151*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kProofInputLenV2> v2_input,
152*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t> target_info,
153*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kNtlmProofLenV2> v2_proof);
154*6777b538SAndroid Build Coastguard Worker 
155*6777b538SAndroid Build Coastguard Worker // The session base key is used to generate the Message Integrity Check (MIC).
156*6777b538SAndroid Build Coastguard Worker // See [MS-NLMP] Section 3.3.2.
157*6777b538SAndroid Build Coastguard Worker //
158*6777b538SAndroid Build Coastguard Worker // It is defined as;
159*6777b538SAndroid Build Coastguard Worker //     session_key = HMAC_MD5(v2_hash, v2_proof)
160*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateSessionBaseKeyV2(
161*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kNtlmHashLen> v2_hash,
162*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kNtlmProofLenV2> v2_proof,
163*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kSessionKeyLenV2> session_key);
164*6777b538SAndroid Build Coastguard Worker 
165*6777b538SAndroid Build Coastguard Worker // The channel bindings hash is an MD5 hash of a data structure containing
166*6777b538SAndroid Build Coastguard Worker // a hash of the server's certificate.
167*6777b538SAndroid Build Coastguard Worker //
168*6777b538SAndroid Build Coastguard Worker // The |channel_bindings| string is supplied out of band (usually from a web
169*6777b538SAndroid Build Coastguard Worker // browser) and is a (21+sizeof(hash)) byte ASCII string, where 'hash' is
170*6777b538SAndroid Build Coastguard Worker // usually a SHA-256 of the servers certificate, but may be another hash
171*6777b538SAndroid Build Coastguard Worker // algorithm. The format as defined by RFC 5929 Section 4 is shown below;
172*6777b538SAndroid Build Coastguard Worker //
173*6777b538SAndroid Build Coastguard Worker // [0-20]                 - "tls-server-end-point:"   (Literal string)
174*6777b538SAndroid Build Coastguard Worker // [21-(20+sizeof(hash)]  - HASH(server_certificate)  (Certificate hash)
175*6777b538SAndroid Build Coastguard Worker //
176*6777b538SAndroid Build Coastguard Worker // The |channel_bindings| string is then combined into a data structure called
177*6777b538SAndroid Build Coastguard Worker // gss_channel_bindings_struct (on Windows SEC_CHANNEL_BINDINGS) and MD5 hashed
178*6777b538SAndroid Build Coastguard Worker // according to the rules in RFC 4121 Section 4.1.1.2. When simplified this
179*6777b538SAndroid Build Coastguard Worker // results in the input to the hash (aka "ClientChannelBindingsUnhashed")
180*6777b538SAndroid Build Coastguard Worker // being defined as follows;
181*6777b538SAndroid Build Coastguard Worker //
182*6777b538SAndroid Build Coastguard Worker // [0-15]   - 16 zero bytes                        (Collapsed fields)
183*6777b538SAndroid Build Coastguard Worker // [16-19]  - |strlen(channel_bindings)|           (Length=0x00000035)
184*6777b538SAndroid Build Coastguard Worker // [20-72]  - |channel_bindings|                   (Channel bindings)
185*6777b538SAndroid Build Coastguard Worker //
186*6777b538SAndroid Build Coastguard Worker // See also RFC 5056 and [MS-NLMP] Section 3.1.5.1.2.
187*6777b538SAndroid Build Coastguard Worker //
188*6777b538SAndroid Build Coastguard Worker // The channel bindings hash is then defined as;
189*6777b538SAndroid Build Coastguard Worker //     channel_bindings_hash = MD5(ClientChannelBindingsUnhashed)
190*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateChannelBindingHashV2(
191*6777b538SAndroid Build Coastguard Worker     const std::string& channel_bindings,
192*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kNtlmHashLen> channel_bindings_hash);
193*6777b538SAndroid Build Coastguard Worker 
194*6777b538SAndroid Build Coastguard Worker // The Message Integrity Check (MIC) is a hash calculated over all three
195*6777b538SAndroid Build Coastguard Worker // messages in the NTLM protocol. The MIC field in the authenticate message
196*6777b538SAndroid Build Coastguard Worker // is set to all zeros when calculating the hash. See [MS-NLMP] Section
197*6777b538SAndroid Build Coastguard Worker // 3.1.5.1.2.
198*6777b538SAndroid Build Coastguard Worker //
199*6777b538SAndroid Build Coastguard Worker // In this implementation NTLMSSP_NEGOTIATE_KEY_EXCH never negotiated and
200*6777b538SAndroid Build Coastguard Worker // the MIC for this case is defined as below. If NTLMSSP_NEGOTIATE_KEY_EXCH
201*6777b538SAndroid Build Coastguard Worker // was negotiated, an alternate key is used. See [MS-NLMP] SEction 3.1.5.1.2
202*6777b538SAndroid Build Coastguard Worker // for additional details.
203*6777b538SAndroid Build Coastguard Worker //
204*6777b538SAndroid Build Coastguard Worker //     mic = HMAC_MD5(
205*6777b538SAndroid Build Coastguard Worker //         session_base_key,
206*6777b538SAndroid Build Coastguard Worker //         CONCAT(negotiate_msg, challenge_msg, authenticate_msg))
207*6777b538SAndroid Build Coastguard Worker //
208*6777b538SAndroid Build Coastguard Worker // |session_key| must contain |kSessionKeyLenV2| bytes.
209*6777b538SAndroid Build Coastguard Worker // |mic| must contain |kMicLenV2| bytes.
210*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE void GenerateMicV2(
211*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t, kSessionKeyLenV2> session_key,
212*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t> negotiate_msg,
213*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t> challenge_msg,
214*6777b538SAndroid Build Coastguard Worker     base::span<const uint8_t> authenticate_msg,
215*6777b538SAndroid Build Coastguard Worker     base::span<uint8_t, kMicLenV2> mic);
216*6777b538SAndroid Build Coastguard Worker 
217*6777b538SAndroid Build Coastguard Worker // Updates the target info sent by the server, and generates the clients
218*6777b538SAndroid Build Coastguard Worker // response target info.
219*6777b538SAndroid Build Coastguard Worker NET_EXPORT_PRIVATE std::vector<uint8_t> GenerateUpdatedTargetInfo(
220*6777b538SAndroid Build Coastguard Worker     bool is_mic_enabled,
221*6777b538SAndroid Build Coastguard Worker     bool is_epa_enabled,
222*6777b538SAndroid Build Coastguard Worker     const std::string& channel_bindings,
223*6777b538SAndroid Build Coastguard Worker     const std::string& spn,
224*6777b538SAndroid Build Coastguard Worker     const std::vector<AvPair>& av_pairs,
225*6777b538SAndroid Build Coastguard Worker     uint64_t* server_timestamp);
226*6777b538SAndroid Build Coastguard Worker 
227*6777b538SAndroid Build Coastguard Worker }  // namespace net::ntlm
228*6777b538SAndroid Build Coastguard Worker 
229*6777b538SAndroid Build Coastguard Worker #endif  // NET_NTLM_NTLM_H_
230