exchange_test.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (c) 2019, 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. "encoding/base64"
  22. "fmt"
  23. "io/ioutil"
  24. "os"
  25. "testing"
  26. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
  27. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/prng"
  28. "github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
  29. )
  30. func TestServerEntryExchange(t *testing.T) {
  31. // Prepare an empty database
  32. testDataDirName, err := ioutil.TempDir("", "psiphon-exchange-test")
  33. if err != nil {
  34. t.Fatalf("TempDir failed: %s", err)
  35. }
  36. defer os.RemoveAll(testDataDirName)
  37. SetNoticeWriter(ioutil.Discard)
  38. // Generate signing and exchange key material
  39. obfuscationKeyBytes, err := common.MakeSecureRandomBytes(32)
  40. if err != nil {
  41. t.Fatalf("MakeRandomBytes failed: %s", err)
  42. }
  43. obfuscationKey := base64.StdEncoding.EncodeToString(obfuscationKeyBytes)
  44. publicKey, privateKey, err := protocol.NewServerEntrySignatureKeyPair()
  45. if err != nil {
  46. t.Fatalf("NewServerEntrySignatureKeyPair failed: %s", err)
  47. }
  48. // Initialize config required for datastore operation
  49. networkID := prng.HexString(8)
  50. configJSONTemplate := `
  51. {
  52. "SponsorId" : "0",
  53. "PropagationChannelId" : "0",
  54. "ServerEntrySignaturePublicKey" : "%s",
  55. "ExchangeObfuscationKey" : "%s",
  56. "NetworkID" : "%s"
  57. }`
  58. configJSON := fmt.Sprintf(
  59. configJSONTemplate,
  60. publicKey,
  61. obfuscationKey,
  62. networkID)
  63. config, err := LoadConfig([]byte(configJSON))
  64. if err != nil {
  65. t.Fatalf("LoadConfig failed: %s", err)
  66. }
  67. config.DataRootDirectory = testDataDirName
  68. err = config.Commit(false)
  69. if err != nil {
  70. t.Fatalf("Commit failed: %s", err)
  71. }
  72. err = OpenDataStore(config)
  73. if err != nil {
  74. t.Fatalf("OpenDataStore failed: %s", err)
  75. }
  76. defer CloseDataStore()
  77. // Generate server entries to test different cases
  78. //
  79. // Note: invalid signature cases are exercised in
  80. // protocol.TestServerEntryListSignatures
  81. makeServerEntryFields := func(IPAddress string) protocol.ServerEntryFields {
  82. n := 16
  83. fields := make(protocol.ServerEntryFields)
  84. fields["ipAddress"] = IPAddress
  85. fields["sshPort"] = 22
  86. fields["sshUsername"] = prng.HexString(n)
  87. fields["sshPassword"] = prng.HexString(n)
  88. fields["sshHostKey"] = prng.HexString(n)
  89. fields["sshObfuscatedPort"] = 23
  90. fields["sshObfuscatedQUICPort"] = 24
  91. fields["sshObfuscatedKey"] = prng.HexString(n)
  92. fields["capabilities"] = []string{"SSH", "OSSH", "QUIC", "ssh-api-requests"}
  93. fields["region"] = "US"
  94. fields["configurationVersion"] = 1
  95. return fields
  96. }
  97. serverEntry0 := makeServerEntryFields("192.168.1.1")
  98. tunnelProtocol0 := "SSH"
  99. serverEntry1 := makeServerEntryFields("192.168.1.2")
  100. err = serverEntry1.AddSignature(publicKey, privateKey)
  101. if err != nil {
  102. t.Fatalf("AddSignature failed: %s", err)
  103. }
  104. tunnelProtocol1 := "OSSH"
  105. serverEntry2 := makeServerEntryFields("192.168.1.3")
  106. err = serverEntry2.AddSignature(publicKey, privateKey)
  107. if err != nil {
  108. t.Fatalf("AddSignature failed: %s", err)
  109. }
  110. tunnelProtocol2 := "QUIC-OSSH"
  111. serverEntry3 := makeServerEntryFields("192.168.1.4")
  112. err = serverEntry3.AddSignature(publicKey, privateKey)
  113. if err != nil {
  114. t.Fatalf("AddSignature failed: %s", err)
  115. }
  116. tunnelProtocol3 := ""
  117. // paveServerEntry stores a server entry in the datastore with source
  118. // EMBEDDED, promotes the server entry to the affinity/export candidate
  119. // position, and generates and stores associated dial parameters when
  120. // specified. This creates potential candidates for export.
  121. //
  122. // When tunnelProtocol is "", no dial parameters are created.
  123. paveServerEntry := func(
  124. fields protocol.ServerEntryFields, tunnelProtocol string) {
  125. fields.SetLocalSource(protocol.SERVER_ENTRY_SOURCE_EMBEDDED)
  126. fields.SetLocalTimestamp(
  127. common.TruncateTimestampToHour(common.GetCurrentTimestamp()))
  128. err = StoreServerEntry(fields, true)
  129. if err != nil {
  130. t.Fatalf("StoreServerEntry failed: %s", err)
  131. }
  132. err = PromoteServerEntry(config, fields["ipAddress"].(string))
  133. if err != nil {
  134. t.Fatalf("PromoteServerEntry failed: %s", err)
  135. }
  136. if tunnelProtocol != "" {
  137. serverEntry, err := fields.GetServerEntry()
  138. if err != nil {
  139. t.Fatalf("ServerEntryFields.GetServerEntry failed: %s", err)
  140. }
  141. canReplay := func(serverEntry *protocol.ServerEntry, replayProtocol string) bool {
  142. return true
  143. }
  144. selectProtocol := func(serverEntry *protocol.ServerEntry) (string, bool) {
  145. return tunnelProtocol, true
  146. }
  147. dialParams, err := MakeDialParameters(
  148. config,
  149. nil,
  150. canReplay,
  151. selectProtocol,
  152. serverEntry,
  153. false,
  154. 0,
  155. 0)
  156. if err != nil {
  157. t.Fatalf("MakeDialParameters failed: %s", err)
  158. }
  159. err = SetDialParameters(serverEntry.IpAddress, networkID, dialParams)
  160. if err != nil {
  161. t.Fatalf("SetDialParameters failed: %s", err)
  162. }
  163. }
  164. }
  165. // checkFirstServerEntry checks that the current affinity server entry has
  166. // the expected ID (IP address), and that any associated, stored dial
  167. // parameters are in the expected exchanged state. This is used to verify
  168. // that an import has succeed and set the datastore correctly.
  169. checkFirstServerEntry := func(
  170. fields protocol.ServerEntryFields, tunnelProtocol string, isExchanged bool) {
  171. _, iterator, err := NewServerEntryIterator(config)
  172. if err != nil {
  173. t.Fatalf("NewServerEntryIterator failed: %s", err)
  174. }
  175. defer iterator.Close()
  176. serverEntry, err := iterator.Next()
  177. if err != nil {
  178. t.Fatalf("ServerEntryIterator.Next failed: %s", err)
  179. }
  180. if serverEntry == nil {
  181. t.Fatalf("unexpected nil server entry")
  182. }
  183. if serverEntry.IpAddress != fields["ipAddress"] {
  184. t.Fatalf("unexpected server entry IP address")
  185. }
  186. if isExchanged {
  187. if serverEntry.LocalSource != protocol.SERVER_ENTRY_SOURCE_EXCHANGED {
  188. t.Fatalf("unexpected non-exchanged server entry source")
  189. }
  190. } else {
  191. if serverEntry.LocalSource == protocol.SERVER_ENTRY_SOURCE_EXCHANGED {
  192. t.Fatalf("unexpected exchanged server entry source")
  193. }
  194. }
  195. dialParams, err := GetDialParameters(config, serverEntry.IpAddress, networkID)
  196. if err != nil {
  197. t.Fatalf("GetDialParameters failed: %s", err)
  198. }
  199. if tunnelProtocol == "" {
  200. if dialParams != nil {
  201. t.Fatalf("unexpected non-nil dial parameters")
  202. }
  203. } else if isExchanged {
  204. if !dialParams.IsExchanged {
  205. t.Fatalf("unexpected non-exchanged dial parameters")
  206. }
  207. if dialParams.TunnelProtocol != tunnelProtocol {
  208. t.Fatalf("unexpected exchanged dial parameters tunnel protocol")
  209. }
  210. } else {
  211. if dialParams.IsExchanged {
  212. t.Fatalf("unexpected exchanged dial parameters")
  213. }
  214. if dialParams.TunnelProtocol != tunnelProtocol {
  215. t.Fatalf("unexpected dial parameters tunnel protocol")
  216. }
  217. }
  218. }
  219. // Test: pave only an unsigned server entry; export should fail
  220. paveServerEntry(serverEntry0, tunnelProtocol0)
  221. payload := ExportExchangePayload(config)
  222. if payload != "" {
  223. t.Fatalf("ExportExchangePayload unexpectedly succeeded")
  224. }
  225. // Test: pave two signed server entries; serverEntry2 is the affinity server
  226. // entry and should be the exported server entry
  227. paveServerEntry(serverEntry1, tunnelProtocol1)
  228. paveServerEntry(serverEntry2, tunnelProtocol2)
  229. payload = ExportExchangePayload(config)
  230. if payload == "" {
  231. t.Fatalf("ExportExchangePayload failed")
  232. }
  233. // Test: import; serverEntry2 should be imported
  234. // Before importing the exported payload, move serverEntry1 to the affinity
  235. // position. After the import, we expect serverEntry2 to be at the affinity
  236. // position and its dial parameters to be IsExchanged and and have the
  237. // exchanged tunnel protocol.
  238. err = PromoteServerEntry(config, serverEntry1["ipAddress"].(string))
  239. if err != nil {
  240. t.Fatalf("PromoteServerEntry failed: %s", err)
  241. }
  242. checkFirstServerEntry(serverEntry1, tunnelProtocol1, false)
  243. if !ImportExchangePayload(config, payload) {
  244. t.Fatalf("ImportExchangePayload failed")
  245. }
  246. checkFirstServerEntry(serverEntry2, tunnelProtocol2, true)
  247. // Test: nil exchanged dial parameters case
  248. paveServerEntry(serverEntry3, tunnelProtocol3)
  249. payload = ExportExchangePayload(config)
  250. if payload == "" {
  251. t.Fatalf("ExportExchangePayload failed")
  252. }
  253. err = PromoteServerEntry(config, serverEntry1["ipAddress"].(string))
  254. if err != nil {
  255. t.Fatalf("PromoteServerEntry failed: %s", err)
  256. }
  257. checkFirstServerEntry(serverEntry1, tunnelProtocol1, false)
  258. if !ImportExchangePayload(config, payload) {
  259. t.Fatalf("ImportExchangePayload failed")
  260. }
  261. checkFirstServerEntry(serverEntry3, tunnelProtocol3, true)
  262. }