remoteServerList.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright (c) 2014, 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. "strings"
  31. )
  32. // RemoteServerList is a JSON record containing a list of Psiphon server
  33. // entries. As it may be downloaded from various sources, it is digitally
  34. // signed so that the data may be authenticated.
  35. type RemoteServerList struct {
  36. Data string `json:"data"`
  37. SigningPublicKeyDigest string `json:"signingPublicKeyDigest"`
  38. Signature string `json:"signature"`
  39. }
  40. // FetchRemoteServerList downloads a remote server list JSON record from
  41. // config.RemoteServerListUrl; validates its digital signature using the
  42. // public key config.RemoteServerListSignaturePublicKey; and parses the
  43. // data field into ServerEntry records.
  44. func FetchRemoteServerList(config *Config, pendingConns *Conns) (err error) {
  45. Notice(NOTICE_INFO, "fetching remote server list")
  46. // Note: pendingConns may be used to interrupt the fetch remote server list
  47. // request. BindToDevice may be used to exclude requests from VPN routing.
  48. dialConfig := &DialConfig{
  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. for _, encodedServerEntry := range strings.Split(remoteServerList.Data, "\n") {
  79. serverEntry, err := DecodeServerEntry(encodedServerEntry)
  80. if err != nil {
  81. return ContextError(err)
  82. }
  83. err = StoreServerEntry(serverEntry, true)
  84. if err != nil {
  85. return ContextError(err)
  86. }
  87. }
  88. return nil
  89. }
  90. func validateRemoteServerList(config *Config, remoteServerList *RemoteServerList) (err error) {
  91. derEncodedPublicKey, err := base64.StdEncoding.DecodeString(config.RemoteServerListSignaturePublicKey)
  92. if err != nil {
  93. return ContextError(err)
  94. }
  95. publicKey, err := x509.ParsePKIXPublicKey(derEncodedPublicKey)
  96. if err != nil {
  97. return ContextError(err)
  98. }
  99. rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
  100. if !ok {
  101. return ContextError(errors.New("unexpected RemoteServerListSignaturePublicKey key type"))
  102. }
  103. signature, err := base64.StdEncoding.DecodeString(remoteServerList.Signature)
  104. if err != nil {
  105. return ContextError(err)
  106. }
  107. // TODO: can detect if signed with different key --
  108. // match digest(publicKey) against remoteServerList.signingPublicKeyDigest
  109. hash := sha256.New()
  110. hash.Write([]byte(remoteServerList.Data))
  111. digest := hash.Sum(nil)
  112. err = rsa.VerifyPKCS1v15(rsaPublicKey, crypto.SHA256, digest, signature)
  113. if err != nil {
  114. return ContextError(err)
  115. }
  116. return nil
  117. }