remoteServerList.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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) (err error) {
  45. Notice(NOTICE_INFO, "fetching remote server list")
  46. httpClient := http.Client{
  47. Timeout: FETCH_REMOTE_SERVER_LIST_TIMEOUT,
  48. }
  49. response, err := httpClient.Get(config.RemoteServerListUrl)
  50. if err != nil {
  51. return ContextError(err)
  52. }
  53. defer response.Body.Close()
  54. body, err := ioutil.ReadAll(response.Body)
  55. if err != nil {
  56. return ContextError(err)
  57. }
  58. var remoteServerList *RemoteServerList
  59. err = json.Unmarshal(body, &remoteServerList)
  60. if err != nil {
  61. return ContextError(err)
  62. }
  63. err = validateRemoteServerList(config, remoteServerList)
  64. if err != nil {
  65. return ContextError(err)
  66. }
  67. for _, encodedServerEntry := range strings.Split(remoteServerList.Data, "\n") {
  68. serverEntry, err := DecodeServerEntry(encodedServerEntry)
  69. if err != nil {
  70. return ContextError(err)
  71. }
  72. err = StoreServerEntry(serverEntry, true)
  73. if err != nil {
  74. return ContextError(err)
  75. }
  76. }
  77. return nil
  78. }
  79. func validateRemoteServerList(config *Config, remoteServerList *RemoteServerList) (err error) {
  80. derEncodedPublicKey, err := base64.StdEncoding.DecodeString(config.RemoteServerListSignaturePublicKey)
  81. if err != nil {
  82. return ContextError(err)
  83. }
  84. publicKey, err := x509.ParsePKIXPublicKey(derEncodedPublicKey)
  85. if err != nil {
  86. return ContextError(err)
  87. }
  88. rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
  89. if !ok {
  90. return ContextError(errors.New("unexpected RemoteServerListSignaturePublicKey key type"))
  91. }
  92. signature, err := base64.StdEncoding.DecodeString(remoteServerList.Signature)
  93. if err != nil {
  94. return ContextError(err)
  95. }
  96. // TODO: can detect if signed with different key --
  97. // match digest(publicKey) against remoteServerList.signingPublicKeyDigest
  98. hash := sha256.New()
  99. hash.Write([]byte(remoteServerList.Data))
  100. digest := hash.Sum(nil)
  101. err = rsa.VerifyPKCS1v15(rsaPublicKey, crypto.SHA256, digest, signature)
  102. if err != nil {
  103. return ContextError(err)
  104. }
  105. return nil
  106. }