handshake_ix.go 12 KB

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