exchange_test.go 9.0 KB

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