exchange_test.go 9.3 KB

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