handshake_ix.go 13 KB

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