remoteServerList.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 psiphon
  20. import (
  21. "crypto"
  22. "crypto/rsa"
  23. "crypto/sha256"
  24. "crypto/x509"
  25. "encoding/base64"
  26. "encoding/json"
  27. "errors"
  28. "io/ioutil"
  29. "net/http"
  30. )
  31. // RemoteServerList is a JSON record containing a list of Psiphon server
  32. // entries. As it may be downloaded from various sources, it is digitally
  33. // signed so that the data may be authenticated.
  34. type RemoteServerList struct {
  35. Data string `json:"data"`
  36. SigningPublicKeyDigest string `json:"signingPublicKeyDigest"`
  37. Signature string `json:"signature"`
  38. }
  39. // FetchRemoteServerList downloads a remote server list JSON record from
  40. // config.RemoteServerListUrl; validates its digital signature using the
  41. // public key config.RemoteServerListSignaturePublicKey; and parses the
  42. // data field into ServerEntry records.
  43. func FetchRemoteServerList(config *Config, pendingConns *Conns) (err error) {
  44. NoticeInfo("fetching remote server list")
  45. // Note: pendingConns may be used to interrupt the fetch remote server list
  46. // request. BindToDevice may be used to exclude requests from VPN routing.
  47. dialConfig := &DialConfig{
  48. UpstreamHttpProxyAddress: config.UpstreamHttpProxyAddress,
  49. PendingConns: pendingConns,
  50. BindToDeviceProvider: config.BindToDeviceProvider,
  51. BindToDeviceDnsServer: config.BindToDeviceDnsServer,
  52. }
  53. transport := &http.Transport{
  54. Dial: NewTCPDialer(dialConfig),
  55. }
  56. httpClient := http.Client{
  57. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  58. Transport: transport,
  59. }
  60. response, err := httpClient.Get(config.RemoteServerListUrl)
  61. if err != nil {
  62. return ContextError(err)
  63. }
  64. defer response.Body.Close()
  65. body, err := ioutil.ReadAll(response.Body)
  66. if err != nil {
  67. return ContextError(err)
  68. }
  69. var remoteServerList *RemoteServerList
  70. err = json.Unmarshal(body, &remoteServerList)
  71. if err != nil {
  72. return ContextError(err)
  73. }
  74. err = validateRemoteServerList(config, remoteServerList)
  75. if err != nil {
  76. return ContextError(err)
  77. }
  78. serverEntries, err := DecodeAndValidateServerEntryList(remoteServerList.Data)
  79. if err != nil {
  80. return ContextError(err)
  81. }
  82. err = StoreServerEntries(serverEntries, true)
  83. if err != nil {
  84. return ContextError(err)
  85. }
  86. return nil
  87. }
  88. func validateRemoteServerList(config *Config, remoteServerList *RemoteServerList) (err error) {
  89. derEncodedPublicKey, err := base64.StdEncoding.DecodeString(config.RemoteServerListSignaturePublicKey)
  90. if err != nil {
  91. return ContextError(err)
  92. }
  93. publicKey, err := x509.ParsePKIXPublicKey(derEncodedPublicKey)
  94. if err != nil {
  95. return ContextError(err)
  96. }
  97. rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
  98. if !ok {
  99. return ContextError(errors.New("unexpected RemoteServerListSignaturePublicKey key type"))
  100. }
  101. signature, err := base64.StdEncoding.DecodeString(remoteServerList.Signature)
  102. if err != nil {
  103. return ContextError(err)
  104. }
  105. // TODO: can detect if signed with different key --
  106. // match digest(publicKey) against remoteServerList.signingPublicKeyDigest
  107. hash := sha256.New()
  108. hash.Write([]byte(remoteServerList.Data))
  109. digest := hash.Sum(nil)
  110. err = rsa.VerifyPKCS1v15(rsaPublicKey, crypto.SHA256, digest, signature)
  111. if err != nil {
  112. return ContextError(err)
  113. }
  114. return nil
  115. }