auth_ntlm.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. challenge, ok := challenges["NTLM"]
  51. if challenge == "" {
  52. a.state = NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED
  53. } else {
  54. a.state = NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE1_GENERATED
  55. }
  56. if !ok {
  57. return proxyError(fmt.Errorf("Bad proxy response, no NTLM challenge for NTLMHttpAuthenticator"))
  58. }
  59. var ntlmMsg []byte
  60. session, err := ntlm.CreateClientSession(ntlm.Version2, ntlm.ConnectionOrientedMode)
  61. if err != nil {
  62. return proxyError(err)
  63. }
  64. if a.state == NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED {
  65. //generate TYPE 1 message
  66. negotiate, err := session.GenerateNegotiateMessage()
  67. if err != nil {
  68. return proxyError(err)
  69. }
  70. ntlmMsg = negotiate.Bytes()
  71. a.state = NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE1_GENERATED
  72. req.Header.Set("Proxy-Authorization", "NTLM "+base64.StdEncoding.EncodeToString(ntlmMsg))
  73. return nil
  74. } else if a.state == NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE1_GENERATED {
  75. // Parse username for domain in form DOMAIN\username
  76. var NTDomain, NTUser string
  77. parts := strings.SplitN(a.username, "\\", 2)
  78. if len(parts) == 2 {
  79. NTDomain = parts[0]
  80. NTUser = parts[1]
  81. } else {
  82. NTDomain = ""
  83. NTUser = a.username
  84. }
  85. challengeBytes, err := base64.StdEncoding.DecodeString(challenge)
  86. if err != nil {
  87. return proxyError(fmt.Errorf("NTLM challeenge base 64 decoding: %v", err))
  88. }
  89. session.SetUserInfo(NTUser, a.password, NTDomain)
  90. ntlmChallenge, err := ntlm.ParseChallengeMessage(challengeBytes)
  91. if err != nil {
  92. return proxyError(err)
  93. }
  94. session.ProcessChallengeMessage(ntlmChallenge)
  95. authenticate, err := session.GenerateAuthenticateMessage()
  96. if err != nil {
  97. return proxyError(err)
  98. }
  99. ntlmMsg = authenticate.Bytes()
  100. a.state = NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE3_GENERATED
  101. req.Header.Set("Proxy-Authorization", "NTLM "+base64.StdEncoding.EncodeToString(ntlmMsg))
  102. return nil
  103. }
  104. return proxyError(fmt.Errorf("Authorization is not accepted by the proxy server"))
  105. }
  106. func (a *NTLMHttpAuthenticator) IsConnectionBased() bool {
  107. return true
  108. }
  109. func (a *NTLMHttpAuthenticator) IsComplete() bool {
  110. return a.state == NTLM_HTTP_AUTH_STATE_RESPONSE_TYPE3_GENERATED
  111. }
  112. func (a *NTLMHttpAuthenticator) Reset() {
  113. a.state = NTLM_HTTP_AUTH_STATE_CHALLENGE_RECEIVED
  114. }
  115. func (a *NTLMHttpAuthenticator) PreAuthenticate(req *http.Request) error {
  116. return nil
  117. }