handshake_ix.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. package nebula
  2. import (
  3. "sync/atomic"
  4. "time"
  5. "bytes"
  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.messageCounter, 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. hostinfo.HandshakePacket[0] = msg
  55. hostinfo.HandshakeReady = true
  56. hostinfo.handshakeStart = time.Now()
  57. }
  58. func ixHandshakeStage1(f *Interface, addr *udpAddr, hostinfo *HostInfo, packet []byte, h *Header) bool {
  59. var ip uint32
  60. if h.RemoteIndex == 0 {
  61. ci := f.newConnectionState(false, noise.HandshakeIX, []byte{}, 0)
  62. // Mark packet 1 as seen so it doesn't show up as missed
  63. ci.window.Update(1)
  64. msg, _, _, err := ci.H.ReadMessage(nil, packet[HeaderLen:])
  65. if err != nil {
  66. l.WithError(err).WithField("udpAddr", addr).
  67. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.ReadMessage")
  68. return true
  69. }
  70. hs := &NebulaHandshake{}
  71. err = proto.Unmarshal(msg, hs)
  72. /*
  73. l.Debugln("GOT INDEX: ", hs.Details.InitiatorIndex)
  74. */
  75. if err != nil || hs.Details == nil {
  76. l.WithError(err).WithField("udpAddr", addr).
  77. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  78. return true
  79. }
  80. hostinfo, _ := f.handshakeManager.pendingHostMap.QueryReverseIndex(hs.Details.InitiatorIndex)
  81. if hostinfo != nil && bytes.Equal(hostinfo.HandshakePacket[0], packet[HeaderLen:]) {
  82. if msg, ok := hostinfo.HandshakePacket[2]; ok {
  83. err := f.outside.WriteTo(msg, addr)
  84. if err != nil {
  85. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  86. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  87. WithError(err).Error("Failed to send handshake message")
  88. } else {
  89. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  90. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  91. Info("Handshake message sent")
  92. }
  93. return false
  94. }
  95. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  96. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).WithField("cached", true).
  97. WithField("packets", hostinfo.HandshakePacket).
  98. Error("Seen this handshake packet already but don't have a cached packet to return")
  99. }
  100. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert)
  101. if err != nil {
  102. l.WithError(err).WithField("udpAddr", addr).
  103. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).WithField("cert", remoteCert).
  104. Info("Invalid certificate from host")
  105. return true
  106. }
  107. vpnIP := ip2int(remoteCert.Details.Ips[0].IP)
  108. certName := remoteCert.Details.Name
  109. myIndex, err := generateIndex()
  110. if err != nil {
  111. l.WithError(err).WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  112. WithField("certName", certName).
  113. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to generate index")
  114. return true
  115. }
  116. hostinfo, err = f.handshakeManager.AddIndex(myIndex, ci)
  117. if err != nil {
  118. l.WithError(err).WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  119. WithField("certName", certName).
  120. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Error adding index to connection manager")
  121. return true
  122. }
  123. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  124. WithField("certName", certName).
  125. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  126. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  127. Info("Handshake message received")
  128. hostinfo.remoteIndexId = hs.Details.InitiatorIndex
  129. hs.Details.ResponderIndex = myIndex
  130. hs.Details.Cert = ci.certState.rawCertificateNoKey
  131. hsBytes, err := proto.Marshal(hs)
  132. if err != nil {
  133. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  134. WithField("certName", certName).
  135. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  136. return true
  137. }
  138. header := HeaderEncode(make([]byte, HeaderLen), Version, uint8(handshake), handshakeIXPSK0, hs.Details.InitiatorIndex, 2)
  139. msg, dKey, eKey, err := ci.H.WriteMessage(header, hsBytes)
  140. if err != nil {
  141. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  142. WithField("certName", certName).
  143. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  144. return true
  145. }
  146. if f.hostMap.CheckHandshakeCompleteIP(vpnIP) && vpnIP < ip2int(f.certState.certificate.Details.Ips[0].IP) {
  147. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  148. WithField("certName", certName).
  149. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  150. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  151. Info("Prevented a handshake race")
  152. // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
  153. f.SendMessageToVpnIp(test, testRequest, vpnIP, []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  154. return true
  155. }
  156. hostinfo.HandshakePacket[0] = make([]byte, len(packet[HeaderLen:]))
  157. copy(hostinfo.HandshakePacket[0], packet[HeaderLen:])
  158. // Regardless of whether you are the sender or receiver, you should arrive here
  159. // and complete standing up the connection.
  160. if dKey != nil && eKey != nil {
  161. hostinfo.HandshakePacket[2] = make([]byte, len(msg))
  162. copy(hostinfo.HandshakePacket[2], msg)
  163. err := f.outside.WriteTo(msg, addr)
  164. if err != nil {
  165. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  166. WithField("certName", certName).
  167. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  168. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  169. WithError(err).Error("Failed to send handshake")
  170. } else {
  171. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  172. WithField("certName", certName).
  173. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  174. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  175. Info("Handshake message sent")
  176. }
  177. ip = ip2int(remoteCert.Details.Ips[0].IP)
  178. ci.peerCert = remoteCert
  179. ci.dKey = NewNebulaCipherState(dKey)
  180. ci.eKey = NewNebulaCipherState(eKey)
  181. //l.Debugln("got symmetric pairs")
  182. //hostinfo.ClearRemotes()
  183. hostinfo.AddRemote(*addr)
  184. hostinfo.CreateRemoteCIDR(remoteCert)
  185. f.lightHouse.AddRemoteAndReset(ip, addr)
  186. if f.serveDns {
  187. dnsR.Add(remoteCert.Details.Name+".", remoteCert.Details.Ips[0].IP.String())
  188. }
  189. ho, err := f.hostMap.QueryVpnIP(vpnIP)
  190. if err == nil && ho.localIndexId != 0 {
  191. l.WithField("vpnIp", vpnIP).
  192. WithField("certName", certName).
  193. WithField("action", "removing stale index").
  194. WithField("index", ho.localIndexId).
  195. Debug("Handshake processing")
  196. f.hostMap.DeleteIndex(ho.localIndexId)
  197. }
  198. f.hostMap.AddIndexHostInfo(hostinfo.localIndexId, hostinfo)
  199. f.hostMap.AddVpnIPHostInfo(vpnIP, hostinfo)
  200. hostinfo.handshakeComplete()
  201. } else {
  202. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  203. WithField("certName", certName).
  204. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  205. Error("Noise did not arrive at a key")
  206. return true
  207. }
  208. }
  209. f.hostMap.AddRemote(ip, addr)
  210. return false
  211. }
  212. func ixHandshakeStage2(f *Interface, addr *udpAddr, hostinfo *HostInfo, packet []byte, h *Header) bool {
  213. if hostinfo == nil {
  214. return true
  215. }
  216. if bytes.Equal(hostinfo.HandshakePacket[2], packet[HeaderLen:]) {
  217. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  218. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  219. Error("Already seen this handshake packet")
  220. return false
  221. }
  222. ci := hostinfo.ConnectionState
  223. // Mark packet 2 as seen so it doesn't show up as missed
  224. ci.window.Update(2)
  225. hostinfo.HandshakePacket[2] = make([]byte, len(packet[HeaderLen:]))
  226. copy(hostinfo.HandshakePacket[2], packet[HeaderLen:])
  227. msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[HeaderLen:])
  228. if err != nil {
  229. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  230. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  231. Error("Failed to call noise.ReadMessage")
  232. // We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
  233. // to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
  234. // near future
  235. return false
  236. }
  237. hs := &NebulaHandshake{}
  238. err = proto.Unmarshal(msg, hs)
  239. if err != nil || hs.Details == nil {
  240. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  241. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  242. return true
  243. }
  244. remoteCert, err := RecombineCertAndValidate(ci.H, hs.Details.Cert)
  245. if err != nil {
  246. l.WithError(err).WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  247. WithField("cert", remoteCert).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  248. Error("Invalid certificate from host")
  249. return true
  250. }
  251. vpnIP := ip2int(remoteCert.Details.Ips[0].IP)
  252. certName := remoteCert.Details.Name
  253. duration := time.Since(hostinfo.handshakeStart).Nanoseconds()
  254. l.WithField("vpnIp", IntIp(vpnIP)).WithField("udpAddr", addr).
  255. WithField("certName", certName).
  256. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  257. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  258. WithField("durationNs", duration).
  259. Info("Handshake message received")
  260. //ci.remoteIndex = hs.ResponderIndex
  261. hostinfo.remoteIndexId = hs.Details.ResponderIndex
  262. hs.Details.Cert = ci.certState.rawCertificateNoKey
  263. /*
  264. hsBytes, err := proto.Marshal(hs)
  265. if err != nil {
  266. l.Debugln("Failed to marshal handshake: ", err)
  267. return
  268. }
  269. */
  270. // Regardless of whether you are the sender or receiver, you should arrive here
  271. // and complete standing up the connection.
  272. if dKey != nil && eKey != nil {
  273. ip := ip2int(remoteCert.Details.Ips[0].IP)
  274. ci.peerCert = remoteCert
  275. ci.dKey = NewNebulaCipherState(dKey)
  276. ci.eKey = NewNebulaCipherState(eKey)
  277. //l.Debugln("got symmetric pairs")
  278. //hostinfo.ClearRemotes()
  279. f.hostMap.AddRemote(ip, addr)
  280. hostinfo.CreateRemoteCIDR(remoteCert)
  281. f.lightHouse.AddRemoteAndReset(ip, addr)
  282. if f.serveDns {
  283. dnsR.Add(remoteCert.Details.Name+".", remoteCert.Details.Ips[0].IP.String())
  284. }
  285. ho, err := f.hostMap.QueryVpnIP(vpnIP)
  286. if err == nil && ho.localIndexId != 0 {
  287. l.WithField("vpnIp", vpnIP).
  288. WithField("certName", certName).
  289. WithField("action", "removing stale index").
  290. WithField("index", ho.localIndexId).
  291. Debug("Handshake processing")
  292. f.hostMap.DeleteIndex(ho.localIndexId)
  293. }
  294. f.hostMap.AddVpnIPHostInfo(vpnIP, hostinfo)
  295. f.hostMap.AddIndexHostInfo(hostinfo.localIndexId, hostinfo)
  296. hostinfo.handshakeComplete()
  297. f.metricHandshakes.Update(duration)
  298. } else {
  299. l.WithField("vpnIp", IntIp(hostinfo.hostId)).WithField("udpAddr", addr).
  300. WithField("certName", certName).
  301. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  302. Error("Noise did not arrive at a key")
  303. return true
  304. }
  305. return false
  306. }