exchange_test.go 9.1 KB

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