handshake_ix.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. package nebula
  2. import (
  3. "bytes"
  4. "sync/atomic"
  5. "time"
  6. "github.com/flynn/noise"
  7. "github.com/golang/protobuf/proto"
  8. )
  9. // NOISE IX Handshakes
  10. // This function constructs a handshake packet, but does not actually send it
  11. // Sending is done by the handshake manager
  12. func ixHandshakeStage0(f *Interface, vpnIp uint32, hostinfo *HostInfo) {
  13. // This queries the lighthouse if we don't know a remote for the host
  14. if hostinfo.remote == nil {
  15. ips, err := f.lightHouse.Query(vpnIp, f)
  16. if err != nil {
  17. //l.Debugln(err)
  18. }
  19. for _, ip := range ips {
  20. hostinfo.AddRemote(ip)
  21. }
  22. }
  23. myIndex, err := generateIndex()
  24. if err != nil {
  25. l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).
  26. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to generate index")
  27. return
  28. }
  29. ci := hostinfo.ConnectionState
  30. f.handshakeManager.AddIndexHostInfo(myIndex, hostinfo)
  31. hsProto := &NebulaHandshakeDetails{
  32. InitiatorIndex: myIndex,
  33. Time: uint64(time.Now().Unix()),
  34. Cert: ci.certState.rawCertificateNoKey,
  35. }
  36. hsBytes := []byte{}
  37. hs := &NebulaHandshake{
  38. Details: hsProto,
  39. }
  40. hsBytes, err = proto.Marshal(hs)
  41. if err != nil {
  42. l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).
  43. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  44. return
  45. }
  46. header := HeaderEncode(make([]byte, HeaderLen), Version, uint8(handshake), handshakeIXPSK0, 0, 1)
  47. atomic.AddUint64(&ci.atomicMessageCounter, 1)
  48. msg, _, _, err := ci.H.WriteMessage(header, hsBytes)
  49. if err != nil {
  50. l.WithError(err).WithField("vpnIp", IntIp(vpnIp)).
  51. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  52. return
  53. }
  54. // We are sending handshake packet 1, so we don't expect to receive
  55. // handshake packet 1 from the responder
  56. ci.window.Update(1)
  57. hostinfo.HandshakePacket[0] = msg
  58. hostinfo.HandshakeReady = true
  59. hostinfo.handshakeStart = time.Now()
  60. }
  61. func ixHandshakeStage1(f *Interface, addr *udpAddr, hostinfo *HostInfo, packet []byte, h *Header) bool {
  62. var ip uint32
  63. if h.RemoteIndex == 0 {
  64. ci := f.newConnectionState(false, noise.HandshakeIX, []byte{}, 0)
  65. // Mark packet 1 as seen so it doesn't show up as missed
  66. ci.window.Update(1)
  67. msg, _, _, err := ci.H.ReadMessage(nil, packet[HeaderLen:])
  68. if err != nil {
  69. l.WithError(err).WithField("udpAddr", addr).
  70. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.ReadMessage")
  71. return true
  72. }
  73. hs := &NebulaHandshake{}
  74. err = proto.Unmarshal(msg, hs)
  75. /*
  76. l.Debugln("GOT INDEX: ", hs.Details.InitiatorIndex)
  77. */
  78. if err != nil || hs.Details == nil {
  79. l.WithError(err).WithField("udpAddr", addr).
  80. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  81. return true
  82. }
  83. hostinfo, _ := f.handshakeManager.pendingHostMap.QueryReverseIndex(hs.Details.InitiatorIndex)
  84. if hostinfo != nil {
  85. hostinfo.RLock()
  86. defer hostinfo.RUnlock()
  87. if bytes.Equal(hostinfo.HandshakePacket[0], packet[HeaderLen:]) {
  88. if msg, ok := hostinfo.HandshakePacket[2]; ok {
  89. f.messageMetrics.Tx(handshake, NebulaMessageSubType(msg[1]), 1)
  90. err := f.outside.WriteTo(msg, addr)
  91. if err != nil {
  92. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  93. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  94. WithError(err).Error("Failed to send handshake message")
  95. } else {
  96. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  97. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  98. Info("Handshake message sent")
  99. }
  100. return false
  101. }
  102. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  103. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).WithField("cached", true).
  104. WithField("packets", hostinfo.HandshakePacket).
  105. Error("Seen this handshake packet already but don't have a cached packet to return")
  106. }
  107. }
  108. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert)
  109. if err != nil {
  110. l.WithError(err).WithField("udpAddr", addr).
  111. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).WithField("cert", remoteCert).
  112. Info("Invalid certificate from host")
  113. return true
  114. }
  115. vpnIP := ip2int(remoteCert.Details.Ips[0].IP)
  116. certName := remoteCert.Details.Name
  117. fingerprint, _ := remoteCert.Sha256Sum()
  118. myIndex, err := generateIndex()
  119. if err != nil {
  120. l.WithError(err).WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  121. WithField("certName", certName).
  122. WithField("fingerprint", fingerprint).
  123. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to generate index")
  124. return true
  125. }
  126. hostinfo, err = f.handshakeManager.AddIndex(myIndex, ci)
  127. if err != nil {
  128. l.WithError(err).WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  129. WithField("certName", certName).
  130. WithField("fingerprint", fingerprint).
  131. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Error adding index to connection manager")
  132. return true
  133. }
  134. hostinfo.Lock()
  135. defer hostinfo.Unlock()
  136. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  137. WithField("certName", certName).
  138. WithField("fingerprint", fingerprint).
  139. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  140. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  141. Info("Handshake message received")
  142. f.handshakeManager.addRemoteIndexHostInfo(hs.Details.InitiatorIndex, hostinfo)
  143. hs.Details.ResponderIndex = myIndex
  144. hs.Details.Cert = ci.certState.rawCertificateNoKey
  145. hsBytes, err := proto.Marshal(hs)
  146. if err != nil {
  147. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  148. WithField("certName", certName).
  149. WithField("fingerprint", fingerprint).
  150. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  151. return true
  152. }
  153. header := HeaderEncode(make([]byte, HeaderLen), Version, uint8(handshake), handshakeIXPSK0, hs.Details.InitiatorIndex, 2)
  154. msg, dKey, eKey, err := ci.H.WriteMessage(header, hsBytes)
  155. if err != nil {
  156. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  157. WithField("certName", certName).
  158. WithField("fingerprint", fingerprint).
  159. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  160. return true
  161. }
  162. if f.hostMap.CheckHandshakeCompleteIP(vpnIP) && vpnIP < ip2int(f.certState.certificate.Details.Ips[0].IP) {
  163. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  164. WithField("certName", certName).
  165. WithField("fingerprint", fingerprint).
  166. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  167. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  168. Info("Prevented a handshake race")
  169. // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
  170. f.SendMessageToVpnIp(test, testRequest, vpnIP, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  171. return true
  172. }
  173. hostinfo.HandshakePacket[0] = make([]byte, len(packet[HeaderLen:]))
  174. copy(hostinfo.HandshakePacket[0], packet[HeaderLen:])
  175. // Regardless of whether you are the sender or receiver, you should arrive here
  176. // and complete standing up the connection.
  177. if dKey != nil && eKey != nil {
  178. hostinfo.HandshakePacket[2] = make([]byte, len(msg))
  179. copy(hostinfo.HandshakePacket[2], msg)
  180. // We are sending handshake packet 2, so we don't expect to receive
  181. // handshake packet 2 from the initiator.
  182. ci.window.Update(2)
  183. f.messageMetrics.Tx(handshake, NebulaMessageSubType(msg[1]), 1)
  184. err := f.outside.WriteTo(msg, addr)
  185. if err != nil {
  186. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  187. WithField("certName", certName).
  188. WithField("fingerprint", fingerprint).
  189. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  190. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  191. WithError(err).Error("Failed to send handshake")
  192. } else {
  193. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  194. WithField("certName", certName).
  195. WithField("fingerprint", fingerprint).
  196. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  197. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  198. Info("Handshake message sent")
  199. }
  200. ip = ip2int(remoteCert.Details.Ips[0].IP)
  201. ci.peerCert = remoteCert
  202. ci.dKey = NewNebulaCipherState(dKey)
  203. ci.eKey = NewNebulaCipherState(eKey)
  204. //l.Debugln("got symmetric pairs")
  205. //hostinfo.ClearRemotes()
  206. hostinfo.AddRemote(*addr)
  207. hostinfo.CreateRemoteCIDR(remoteCert)
  208. f.lightHouse.AddRemoteAndReset(ip, addr)
  209. if f.serveDns {
  210. dnsR.Add(remoteCert.Details.Name+".", remoteCert.Details.Ips[0].IP.String())
  211. }
  212. ho, err := f.hostMap.QueryVpnIP(vpnIP)
  213. if err == nil && ho.localIndexId != 0 {
  214. l.WithField("vpnIp", vpnIP).
  215. WithField("certName", certName).
  216. WithField("fingerprint", fingerprint).
  217. WithField("action", "removing stale index").
  218. WithField("index", ho.localIndexId).
  219. WithField("remoteIndex", ho.remoteIndexId).
  220. Debug("Handshake processing")
  221. f.hostMap.DeleteHostInfo(ho)
  222. }
  223. f.hostMap.AddVpnIPHostInfo(vpnIP, hostinfo)
  224. hostinfo.handshakeComplete()
  225. } else {
  226. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  227. WithField("certName", certName).
  228. WithField("fingerprint", fingerprint).
  229. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  230. Error("Noise did not arrive at a key")
  231. return true
  232. }
  233. }
  234. f.hostMap.AddRemote(ip, addr)
  235. return false
  236. }
  237. func ixHandshakeStage2(f *Interface, addr *udpAddr, hostinfo *HostInfo, packet []byte, h *Header) bool {
  238. if hostinfo == nil {
  239. return true
  240. }
  241. hostinfo.Lock()
  242. defer hostinfo.Unlock()
  243. if bytes.Equal(hostinfo.HandshakePacket[2], packet[HeaderLen:]) {
  244. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  245. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  246. Error("Already seen this handshake packet")
  247. return false
  248. }
  249. ci := hostinfo.ConnectionState
  250. // Mark packet 2 as seen so it doesn't show up as missed
  251. ci.window.Update(2)
  252. hostinfo.HandshakePacket[2] = make([]byte, len(packet[HeaderLen:]))
  253. copy(hostinfo.HandshakePacket[2], packet[HeaderLen:])
  254. msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[HeaderLen:])
  255. if err != nil {
  256. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  257. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  258. Error("Failed to call noise.ReadMessage")
  259. // We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
  260. // to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
  261. // near future
  262. return false
  263. }
  264. hs := &NebulaHandshake{}
  265. err = proto.Unmarshal(msg, hs)
  266. if err != nil || hs.Details == nil {
  267. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  268. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  269. return true
  270. }
  271. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert)
  272. if err != nil {
  273. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  274. WithField("cert", remoteCert).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  275. Error("Invalid certificate from host")
  276. return true
  277. }
  278. vpnIP := ip2int(remoteCert.Details.Ips[0].IP)
  279. certName := remoteCert.Details.Name
  280. fingerprint, _ := remoteCert.Sha256Sum()
  281. duration := time.Since(hostinfo.handshakeStart).Nanoseconds()
  282. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  283. WithField("certName", certName).
  284. WithField("fingerprint", fingerprint).
  285. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  286. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  287. WithField("durationNs", duration).
  288. Info("Handshake message received")
  289. //ci.remoteIndex = hs.ResponderIndex
  290. hostinfo.remoteIndexId = hs.Details.ResponderIndex
  291. hs.Details.Cert = ci.certState.rawCertificateNoKey
  292. /*
  293. hsBytes, err := proto.Marshal(hs)
  294. if err != nil {
  295. l.Debugln("Failed to marshal handshake: ", err)
  296. return
  297. }
  298. */
  299. // Regardless of whether you are the sender or receiver, you should arrive here
  300. // and complete standing up the connection.
  301. if dKey != nil && eKey != nil {
  302. ip := ip2int(remoteCert.Details.Ips[0].IP)
  303. ci.peerCert = remoteCert
  304. ci.dKey = NewNebulaCipherState(dKey)
  305. ci.eKey = NewNebulaCipherState(eKey)
  306. //l.Debugln("got symmetric pairs")
  307. //hostinfo.ClearRemotes()
  308. f.hostMap.AddRemote(ip, addr)
  309. hostinfo.CreateRemoteCIDR(remoteCert)
  310. f.lightHouse.AddRemoteAndReset(ip, addr)
  311. if f.serveDns {
  312. dnsR.Add(remoteCert.Details.Name+".", remoteCert.Details.Ips[0].IP.String())
  313. }
  314. ho, err := f.hostMap.QueryVpnIP(vpnIP)
  315. if err == nil && ho.localIndexId != 0 {
  316. l.WithField("vpnIp", vpnIP).
  317. WithField("certName", certName).
  318. WithField("fingerprint", fingerprint).
  319. WithField("action", "removing stale index").
  320. WithField("index", ho.localIndexId).
  321. WithField("remoteIndex", ho.remoteIndexId).
  322. Debug("Handshake processing")
  323. f.hostMap.DeleteHostInfo(ho)
  324. }
  325. f.hostMap.AddVpnIPHostInfo(vpnIP, hostinfo)
  326. hostinfo.handshakeComplete()
  327. f.metricHandshakes.Update(duration)
  328. } else {
  329. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  330. WithField("certName", certName).
  331. WithField("fingerprint", fingerprint).
  332. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  333. Error("Noise did not arrive at a key")
  334. return true
  335. }
  336. return false
  337. }