forked from Psiphon-Labs/psiphon-tunnel-core
/
auth_ntlm.go
136 lines (121 loc) · 3.97 KB
/
auth_ntlm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
* Copyright (c) 2015, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package upstreamproxy
import (
"encoding/base64"
"fmt"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/upstreamproxy/go-ntlm/ntlm"
"net/http"
"strings"
)
type NTLMHttpAuthState int
const (
NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED NTLMHttpAuthState = iota
NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE1_GENERATED
NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE3_GENERATED
)
type NTLMHttpAuthenticator struct {
state NTLMHttpAuthState
username string
password string
}
func newNTLMAuthenticator(username, password string) *NTLMHttpAuthenticator {
return &NTLMHttpAuthenticator{
state: NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED,
username: username,
password: password,
}
}
func (a *NTLMHttpAuthenticator) Authenticate(req *http.Request, resp *http.Response) error {
if a.state == NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE3_GENERATED {
return proxyError(fmt.Errorf("authorization is not accepted by the proxy server"))
}
challenges, err := parseAuthChallenge(resp)
if err != nil {
// Already wrapped in proxyError
return err
}
challenge, ok := challenges["NTLM"]
if challenge == "" {
a.state = NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED
} else {
a.state = NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE1_GENERATED
}
if !ok {
return proxyError(fmt.Errorf("bad proxy response, no NTLM challenge for NTLMHttpAuthenticator"))
}
var ntlmMsg []byte
session, err := ntlm.CreateClientSession(ntlm.Version2, ntlm.ConnectionOrientedMode)
if err != nil {
return proxyError(err)
}
if a.state == NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED {
// Generate TYPE 1 message
negotiate, err := session.GenerateNegotiateMessage()
if err != nil {
return proxyError(err)
}
ntlmMsg = negotiate.Bytes()
a.state = NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE1_GENERATED
req.Header.Set("Proxy-Authorization", "NTLM "+base64.StdEncoding.EncodeToString(ntlmMsg))
return nil
} else if a.state == NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE1_GENERATED {
// Parse username for domain in form DOMAIN\username
var NTDomain, NTUser string
parts := strings.SplitN(a.username, "\\", 2)
if len(parts) == 2 {
NTDomain = parts[0]
NTUser = parts[1]
} else {
NTDomain = ""
NTUser = a.username
}
challengeBytes, err := base64.StdEncoding.DecodeString(challenge)
if err != nil {
return proxyError(fmt.Errorf("NTLM challenge base 64 decoding: %v", err))
}
session.SetUserInfo(NTUser, a.password, NTDomain)
ntlmChallenge, err := ntlm.ParseChallengeMessage(challengeBytes)
if err != nil {
return proxyError(err)
}
session.ProcessChallengeMessage(ntlmChallenge)
authenticate, err := session.GenerateAuthenticateMessage()
if err != nil {
return proxyError(err)
}
ntlmMsg = authenticate.Bytes()
a.state = NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE3_GENERATED
req.Header.Set("Proxy-Authorization", "NTLM "+base64.StdEncoding.EncodeToString(ntlmMsg))
return nil
}
return proxyError(fmt.Errorf("authorization is not accepted by the proxy server"))
}
func (a *NTLMHttpAuthenticator) IsConnectionBased() bool {
return true
}
func (a *NTLMHttpAuthenticator) IsComplete() bool {
return a.state == NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE3_GENERATED
}
func (a *NTLMHttpAuthenticator) Reset() {
a.state = NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED
}
func (a *NTLMHttpAuthenticator) PreAuthenticate(req *http.Request) error {
return nil
}