remoteServerList.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. "log"
  30. "net/http"
  31. "strings"
  32. )
  33. // RemoteServerList is a JSON record containing a list of Psiphon server
  34. // entries. As it may be downloaded from various sources, it is digitally
  35. // signed so that the data may be authenticated.
  36. type RemoteServerList struct {
  37. Data string `json:"data"`
  38. SigningPublicKeyDigest string `json:"signingPublicKeyDigest"`
  39. Signature string `json:"signature"`
  40. }
  41. // FetchRemoteServerList downloads a remote server list JSON record from
  42. // config.RemoteServerListUrl; validates its digital signature using the
  43. // public key config.RemoteServerListSignaturePublicKey; and parses the
  44. // data field into ServerEntry records.
  45. func FetchRemoteServerList(config *Config) (err error) {
  46. log.Printf("fetching remote server list")
  47. httpClient := http.Client{
  48. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  49. }
  50. response, err := httpClient.Get(config.RemoteServerListUrl)
  51. if err != nil {
  52. return ContextError(err)
  53. }
  54. defer response.Body.Close()
  55. body, err := ioutil.ReadAll(response.Body)
  56. if err != nil {
  57. return ContextError(err)
  58. }
  59. var remoteServerList *RemoteServerList
  60. err = json.Unmarshal(body, &remoteServerList)
  61. if err != nil {
  62. return ContextError(err)
  63. }
  64. err = validateRemoteServerList(config, remoteServerList)
  65. if err != nil {
  66. return ContextError(err)
  67. }
  68. for _, encodedServerEntry := range strings.Split(remoteServerList.Data, "\n") {
  69. serverEntry, err := DecodeServerEntry(encodedServerEntry)
  70. if err != nil {
  71. return ContextError(err)
  72. }
  73. err = StoreServerEntry(serverEntry, true)
  74. if err != nil {
  75. return ContextError(err)
  76. }
  77. }
  78. return nil
  79. }
  80. func validateRemoteServerList(config *Config, remoteServerList *RemoteServerList) (err error) {
  81. derEncodedPublicKey, err := base64.StdEncoding.DecodeString(config.RemoteServerListSignaturePublicKey)
  82. if err != nil {
  83. return ContextError(err)
  84. }
  85. publicKey, err := x509.ParsePKIXPublicKey(derEncodedPublicKey)
  86. if err != nil {
  87. return ContextError(err)
  88. }
  89. rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
  90. if !ok {
  91. return ContextError(errors.New("unexpected RemoteServerListSignaturePublicKey key type"))
  92. }
  93. signature, err := base64.StdEncoding.DecodeString(remoteServerList.Signature)
  94. if err != nil {
  95. return ContextError(err)
  96. }
  97. // TODO: can detect if signed with different key --
  98. // match digest(publicKey) against remoteServerList.signingPublicKeyDigest
  99. hash := sha256.New()
  100. hash.Write([]byte(remoteServerList.Data))
  101. digest := hash.Sum(nil)
  102. err = rsa.VerifyPKCS1v15(rsaPublicKey, crypto.SHA256, digest, signature)
  103. if err != nil {
  104. return ContextError(err)
  105. }
  106. return nil
  107. }