remoteServerList.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Notice(NOTICE_INFO, "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. PendingConns: pendingConns,
  49. BindToDeviceProvider: config.BindToDeviceProvider,
  50. BindToDeviceDnsServer: config.BindToDeviceDnsServer,
  51. }
  52. transport := &http.Transport{
  53. Dial: NewTCPDialer(dialConfig),
  54. }
  55. httpClient := http.Client{
  56. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  57. Transport: transport,
  58. }
  59. response, err := httpClient.Get(config.RemoteServerListUrl)
  60. if err != nil {
  61. return ContextError(err)
  62. }
  63. defer response.Body.Close()
  64. body, err := ioutil.ReadAll(response.Body)
  65. if err != nil {
  66. return ContextError(err)
  67. }
  68. var remoteServerList *RemoteServerList
  69. err = json.Unmarshal(body, &remoteServerList)
  70. if err != nil {
  71. return ContextError(err)
  72. }
  73. err = validateRemoteServerList(config, remoteServerList)
  74. if err != nil {
  75. return ContextError(err)
  76. }
  77. serverEntries, err := DecodeServerEntryList(remoteServerList.Data)
  78. if err != nil {
  79. return ContextError(err)
  80. }
  81. err = StoreServerEntries(serverEntries, true)
  82. if err != nil {
  83. return ContextError(err)
  84. }
  85. return nil
  86. }
  87. func validateRemoteServerList(config *Config, remoteServerList *RemoteServerList) (err error) {
  88. derEncodedPublicKey, err := base64.StdEncoding.DecodeString(config.RemoteServerListSignaturePublicKey)
  89. if err != nil {
  90. return ContextError(err)
  91. }
  92. publicKey, err := x509.ParsePKIXPublicKey(derEncodedPublicKey)
  93. if err != nil {
  94. return ContextError(err)
  95. }
  96. rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
  97. if !ok {
  98. return ContextError(errors.New("unexpected RemoteServerListSignaturePublicKey key type"))
  99. }
  100. signature, err := base64.StdEncoding.DecodeString(remoteServerList.Signature)
  101. if err != nil {
  102. return ContextError(err)
  103. }
  104. // TODO: can detect if signed with different key --
  105. // match digest(publicKey) against remoteServerList.signingPublicKeyDigest
  106. hash := sha256.New()
  107. hash.Write([]byte(remoteServerList.Data))
  108. digest := hash.Sum(nil)
  109. err = rsa.VerifyPKCS1v15(rsaPublicKey, crypto.SHA256, digest, signature)
  110. if err != nil {
  111. return ContextError(err)
  112. }
  113. return nil
  114. }