auth_ntlm.go 4.0 KB

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