handshake_ix.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package nebula
  2. import (
  3. "net/netip"
  4. "slices"
  5. "time"
  6. "github.com/flynn/noise"
  7. "github.com/sirupsen/logrus"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/slackhq/nebula/header"
  10. )
  11. // NOISE IX Handshakes
  12. // This function constructs a handshake packet, but does not actually send it
  13. // Sending is done by the handshake manager
  14. func ixHandshakeStage0(f *Interface, hh *HandshakeHostInfo) bool {
  15. err := f.handshakeManager.allocateIndex(hh)
  16. if err != nil {
  17. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  18. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to generate index")
  19. return false
  20. }
  21. cs := f.pki.getCertState()
  22. v := cs.initiatingVersion
  23. if hh.initiatingVersionOverride != cert.VersionPre1 {
  24. v = hh.initiatingVersionOverride
  25. } else if v < cert.Version2 {
  26. // If we're connecting to a v6 address we should encourage use of a V2 cert
  27. for _, a := range hh.hostinfo.vpnAddrs {
  28. if a.Is6() {
  29. v = cert.Version2
  30. break
  31. }
  32. }
  33. }
  34. crt := cs.getCertificate(v)
  35. if crt == nil {
  36. f.l.WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  37. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  38. WithField("certVersion", v).
  39. Error("Unable to handshake with host because no certificate is available")
  40. return false
  41. }
  42. crtHs := cs.getHandshakeBytes(v)
  43. if crtHs == nil {
  44. f.l.WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  45. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  46. WithField("certVersion", v).
  47. Error("Unable to handshake with host because no certificate handshake bytes is available")
  48. return false
  49. }
  50. ci, err := NewConnectionState(f.l, cs, crt, true, noise.HandshakeIX)
  51. if err != nil {
  52. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  53. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  54. WithField("certVersion", v).
  55. Error("Failed to create connection state")
  56. return false
  57. }
  58. hh.hostinfo.ConnectionState = ci
  59. hs := &NebulaHandshake{
  60. Details: &NebulaHandshakeDetails{
  61. InitiatorIndex: hh.hostinfo.localIndexId,
  62. Time: uint64(time.Now().UnixNano()),
  63. Cert: crtHs,
  64. CertVersion: uint32(v),
  65. },
  66. }
  67. hsBytes, err := hs.Marshal()
  68. if err != nil {
  69. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  70. WithField("certVersion", v).
  71. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  72. return false
  73. }
  74. h := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, 0, 1)
  75. msg, _, _, err := ci.H.WriteMessage(h, hsBytes)
  76. if err != nil {
  77. f.l.WithError(err).WithField("vpnAddrs", hh.hostinfo.vpnAddrs).
  78. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  79. return false
  80. }
  81. // We are sending handshake packet 1, so we don't expect to receive
  82. // handshake packet 1 from the responder
  83. ci.window.Update(f.l, 1)
  84. hh.hostinfo.HandshakePacket[0] = msg
  85. hh.ready = true
  86. return true
  87. }
  88. func ixHandshakeStage1(f *Interface, addr netip.AddrPort, via *ViaSender, packet []byte, h *header.H) {
  89. cs := f.pki.getCertState()
  90. crt := cs.GetDefaultCertificate()
  91. if crt == nil {
  92. f.l.WithField("udpAddr", addr).
  93. WithField("handshake", m{"stage": 0, "style": "ix_psk0"}).
  94. WithField("certVersion", cs.initiatingVersion).
  95. Error("Unable to handshake with host because no certificate is available")
  96. return
  97. }
  98. ci, err := NewConnectionState(f.l, cs, crt, false, noise.HandshakeIX)
  99. if err != nil {
  100. f.l.WithError(err).WithField("udpAddr", addr).
  101. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  102. Error("Failed to create connection state")
  103. return
  104. }
  105. // Mark packet 1 as seen so it doesn't show up as missed
  106. ci.window.Update(f.l, 1)
  107. msg, _, _, err := ci.H.ReadMessage(nil, packet[header.Len:])
  108. if err != nil {
  109. f.l.WithError(err).WithField("udpAddr", addr).
  110. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  111. Error("Failed to call noise.ReadMessage")
  112. return
  113. }
  114. hs := &NebulaHandshake{}
  115. err = hs.Unmarshal(msg)
  116. if err != nil || hs.Details == nil {
  117. f.l.WithError(err).WithField("udpAddr", addr).
  118. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  119. Error("Failed unmarshal handshake message")
  120. return
  121. }
  122. rc, err := cert.Recombine(cert.Version(hs.Details.CertVersion), hs.Details.Cert, ci.H.PeerStatic(), ci.Curve())
  123. if err != nil {
  124. f.l.WithError(err).WithField("udpAddr", addr).
  125. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  126. Info("Handshake did not contain a certificate")
  127. return
  128. }
  129. remoteCert, err := f.pki.GetCAPool().VerifyCertificate(time.Now(), rc)
  130. if err != nil {
  131. fp, fperr := rc.Fingerprint()
  132. if fperr != nil {
  133. fp = "<error generating certificate fingerprint>"
  134. }
  135. e := f.l.WithError(err).WithField("udpAddr", addr).
  136. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  137. WithField("certVpnNetworks", rc.Networks()).
  138. WithField("certFingerprint", fp)
  139. if f.l.Level >= logrus.DebugLevel {
  140. e = e.WithField("cert", rc)
  141. }
  142. e.Info("Invalid certificate from host")
  143. return
  144. }
  145. if remoteCert.Certificate.Version() != ci.myCert.Version() {
  146. // We started off using the wrong certificate version, lets see if we can match the version that was sent to us
  147. myCertOtherVersion := cs.getCertificate(remoteCert.Certificate.Version())
  148. if myCertOtherVersion == nil {
  149. if f.l.Level >= logrus.DebugLevel {
  150. f.l.WithError(err).WithFields(m{
  151. "udpAddr": addr,
  152. "handshake": m{"stage": 1, "style": "ix_psk0"},
  153. "cert": remoteCert,
  154. }).Debug("Might be unable to handshake with host due to missing certificate version")
  155. }
  156. } else {
  157. // Record the certificate we are actually using
  158. ci.myCert = myCertOtherVersion
  159. }
  160. }
  161. if len(remoteCert.Certificate.Networks()) == 0 {
  162. f.l.WithError(err).WithField("udpAddr", addr).
  163. WithField("cert", remoteCert).
  164. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  165. Info("No networks in certificate")
  166. return
  167. }
  168. var vpnAddrs []netip.Addr
  169. var filteredNetworks []netip.Prefix
  170. certName := remoteCert.Certificate.Name()
  171. certVersion := remoteCert.Certificate.Version()
  172. fingerprint := remoteCert.Fingerprint
  173. issuer := remoteCert.Certificate.Issuer()
  174. for _, network := range remoteCert.Certificate.Networks() {
  175. vpnAddr := network.Addr()
  176. if f.myVpnAddrsTable.Contains(vpnAddr) {
  177. f.l.WithField("vpnAddr", vpnAddr).WithField("udpAddr", addr).
  178. WithField("certName", certName).
  179. WithField("certVersion", certVersion).
  180. WithField("fingerprint", fingerprint).
  181. WithField("issuer", issuer).
  182. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Refusing to handshake with myself")
  183. return
  184. }
  185. // vpnAddrs outside our vpn networks are of no use to us, filter them out
  186. if !f.myVpnNetworksTable.Contains(vpnAddr) {
  187. continue
  188. }
  189. filteredNetworks = append(filteredNetworks, network)
  190. vpnAddrs = append(vpnAddrs, vpnAddr)
  191. }
  192. if len(vpnAddrs) == 0 {
  193. f.l.WithError(err).WithField("udpAddr", addr).
  194. WithField("certName", certName).
  195. WithField("certVersion", certVersion).
  196. WithField("fingerprint", fingerprint).
  197. WithField("issuer", issuer).
  198. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("No usable vpn addresses from host, refusing handshake")
  199. return
  200. }
  201. if addr.IsValid() {
  202. // addr can be invalid when the tunnel is being relayed.
  203. // We only want to apply the remote allow list for direct tunnels here
  204. if !f.lightHouse.GetRemoteAllowList().AllowAll(vpnAddrs, addr.Addr()) {
  205. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
  206. return
  207. }
  208. }
  209. myIndex, err := generateIndex(f.l)
  210. if err != nil {
  211. f.l.WithError(err).WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  212. WithField("certName", certName).
  213. WithField("certVersion", certVersion).
  214. WithField("fingerprint", fingerprint).
  215. WithField("issuer", issuer).
  216. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to generate index")
  217. return
  218. }
  219. hostinfo := &HostInfo{
  220. ConnectionState: ci,
  221. localIndexId: myIndex,
  222. remoteIndexId: hs.Details.InitiatorIndex,
  223. vpnAddrs: vpnAddrs,
  224. HandshakePacket: make(map[uint8][]byte, 0),
  225. lastHandshakeTime: hs.Details.Time,
  226. relayState: RelayState{
  227. relays: nil,
  228. relayForByAddr: map[netip.Addr]*Relay{},
  229. relayForByIdx: map[uint32]*Relay{},
  230. },
  231. }
  232. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  233. WithField("certName", certName).
  234. WithField("certVersion", certVersion).
  235. WithField("fingerprint", fingerprint).
  236. WithField("issuer", issuer).
  237. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  238. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  239. Info("Handshake message received")
  240. hs.Details.ResponderIndex = myIndex
  241. hs.Details.Cert = cs.getHandshakeBytes(ci.myCert.Version())
  242. if hs.Details.Cert == nil {
  243. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  244. WithField("certName", certName).
  245. WithField("certVersion", certVersion).
  246. WithField("fingerprint", fingerprint).
  247. WithField("issuer", issuer).
  248. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  249. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  250. WithField("certVersion", ci.myCert.Version()).
  251. Error("Unable to handshake with host because no certificate handshake bytes is available")
  252. return
  253. }
  254. hs.Details.CertVersion = uint32(ci.myCert.Version())
  255. // Update the time in case their clock is way off from ours
  256. hs.Details.Time = uint64(time.Now().UnixNano())
  257. hsBytes, err := hs.Marshal()
  258. if err != nil {
  259. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  260. WithField("certName", certName).
  261. WithField("certVersion", certVersion).
  262. WithField("fingerprint", fingerprint).
  263. WithField("issuer", issuer).
  264. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to marshal handshake message")
  265. return
  266. }
  267. nh := header.Encode(make([]byte, header.Len), header.Version, header.Handshake, header.HandshakeIXPSK0, hs.Details.InitiatorIndex, 2)
  268. msg, dKey, eKey, err := ci.H.WriteMessage(nh, hsBytes)
  269. if err != nil {
  270. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  271. WithField("certName", certName).
  272. WithField("certVersion", certVersion).
  273. WithField("fingerprint", fingerprint).
  274. WithField("issuer", issuer).
  275. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Failed to call noise.WriteMessage")
  276. return
  277. } else if dKey == nil || eKey == nil {
  278. f.l.WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  279. WithField("certName", certName).
  280. WithField("certVersion", certVersion).
  281. WithField("fingerprint", fingerprint).
  282. WithField("issuer", issuer).
  283. WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).Error("Noise did not arrive at a key")
  284. return
  285. }
  286. hostinfo.HandshakePacket[0] = make([]byte, len(packet[header.Len:]))
  287. copy(hostinfo.HandshakePacket[0], packet[header.Len:])
  288. // Regardless of whether you are the sender or receiver, you should arrive here
  289. // and complete standing up the connection.
  290. hostinfo.HandshakePacket[2] = make([]byte, len(msg))
  291. copy(hostinfo.HandshakePacket[2], msg)
  292. // We are sending handshake packet 2, so we don't expect to receive
  293. // handshake packet 2 from the initiator.
  294. ci.window.Update(f.l, 2)
  295. ci.peerCert = remoteCert
  296. ci.dKey = NewNebulaCipherState(dKey)
  297. ci.eKey = NewNebulaCipherState(eKey)
  298. hostinfo.remotes = f.lightHouse.QueryCache(vpnAddrs)
  299. hostinfo.SetRemote(addr)
  300. hostinfo.buildNetworks(filteredNetworks, remoteCert.Certificate.UnsafeNetworks())
  301. existing, err := f.handshakeManager.CheckAndComplete(hostinfo, 0, f)
  302. if err != nil {
  303. switch err {
  304. case ErrAlreadySeen:
  305. // Update remote if preferred
  306. if existing.SetRemoteIfPreferred(f.hostMap, addr) {
  307. // Send a test packet to ensure the other side has also switched to
  308. // the preferred remote
  309. f.SendMessageToVpnAddr(header.Test, header.TestRequest, vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  310. }
  311. msg = existing.HandshakePacket[2]
  312. f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
  313. if addr.IsValid() {
  314. err := f.outside.WriteTo(msg, addr)
  315. if err != nil {
  316. f.l.WithField("vpnAddrs", existing.vpnAddrs).WithField("udpAddr", addr).
  317. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  318. WithError(err).Error("Failed to send handshake message")
  319. } else {
  320. f.l.WithField("vpnAddrs", existing.vpnAddrs).WithField("udpAddr", addr).
  321. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  322. Info("Handshake message sent")
  323. }
  324. return
  325. } else {
  326. if via == nil {
  327. f.l.Error("Handshake send failed: both addr and via are nil.")
  328. return
  329. }
  330. hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
  331. f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
  332. f.l.WithField("vpnAddrs", existing.vpnAddrs).WithField("relay", via.relayHI.vpnAddrs[0]).
  333. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("cached", true).
  334. Info("Handshake message sent")
  335. return
  336. }
  337. case ErrExistingHostInfo:
  338. // This means there was an existing tunnel and this handshake was older than the one we are currently based on
  339. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  340. WithField("certName", certName).
  341. WithField("certVersion", certVersion).
  342. WithField("oldHandshakeTime", existing.lastHandshakeTime).
  343. WithField("newHandshakeTime", hostinfo.lastHandshakeTime).
  344. WithField("fingerprint", fingerprint).
  345. WithField("issuer", issuer).
  346. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  347. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  348. Info("Handshake too old")
  349. // Send a test packet to trigger an authenticated tunnel test, this should suss out any lingering tunnel issues
  350. f.SendMessageToVpnAddr(header.Test, header.TestRequest, vpnAddrs[0], []byte(""), make([]byte, 12, 12), make([]byte, mtu))
  351. return
  352. case ErrLocalIndexCollision:
  353. // This means we failed to insert because of collision on localIndexId. Just let the next handshake packet retry
  354. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  355. WithField("certName", certName).
  356. WithField("certVersion", certVersion).
  357. WithField("fingerprint", fingerprint).
  358. WithField("issuer", issuer).
  359. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  360. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  361. WithField("localIndex", hostinfo.localIndexId).WithField("collision", existing.vpnAddrs).
  362. Error("Failed to add HostInfo due to localIndex collision")
  363. return
  364. default:
  365. // Shouldn't happen, but just in case someone adds a new error type to CheckAndComplete
  366. // And we forget to update it here
  367. f.l.WithError(err).WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  368. WithField("certName", certName).
  369. WithField("certVersion", certVersion).
  370. WithField("fingerprint", fingerprint).
  371. WithField("issuer", issuer).
  372. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  373. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 1, "style": "ix_psk0"}).
  374. Error("Failed to add HostInfo to HostMap")
  375. return
  376. }
  377. }
  378. // Do the send
  379. f.messageMetrics.Tx(header.Handshake, header.MessageSubType(msg[1]), 1)
  380. if addr.IsValid() {
  381. err = f.outside.WriteTo(msg, addr)
  382. if err != nil {
  383. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  384. WithField("certName", certName).
  385. WithField("certVersion", certVersion).
  386. WithField("fingerprint", fingerprint).
  387. WithField("issuer", issuer).
  388. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  389. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  390. WithError(err).Error("Failed to send handshake")
  391. } else {
  392. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  393. WithField("certName", certName).
  394. WithField("certVersion", certVersion).
  395. WithField("fingerprint", fingerprint).
  396. WithField("issuer", issuer).
  397. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  398. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  399. Info("Handshake message sent")
  400. }
  401. } else {
  402. if via == nil {
  403. f.l.Error("Handshake send failed: both addr and via are nil.")
  404. return
  405. }
  406. hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
  407. // I successfully received a handshake. Just in case I marked this tunnel as 'Disestablished', ensure
  408. // it's correctly marked as working.
  409. via.relayHI.relayState.UpdateRelayForByIdxState(via.remoteIdx, Established)
  410. f.SendVia(via.relayHI, via.relay, msg, make([]byte, 12), make([]byte, mtu), false)
  411. f.l.WithField("vpnAddrs", vpnAddrs).WithField("relay", via.relayHI.vpnAddrs[0]).
  412. WithField("certName", certName).
  413. WithField("certVersion", certVersion).
  414. WithField("fingerprint", fingerprint).
  415. WithField("issuer", issuer).
  416. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  417. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  418. Info("Handshake message sent")
  419. }
  420. f.connectionManager.AddTrafficWatch(hostinfo)
  421. hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
  422. return
  423. }
  424. func ixHandshakeStage2(f *Interface, addr netip.AddrPort, via *ViaSender, hh *HandshakeHostInfo, packet []byte, h *header.H) bool {
  425. if hh == nil {
  426. // Nothing here to tear down, got a bogus stage 2 packet
  427. return true
  428. }
  429. hh.Lock()
  430. defer hh.Unlock()
  431. hostinfo := hh.hostinfo
  432. if addr.IsValid() {
  433. // The vpnAddr we know about is the one we tried to handshake with, use it to apply the remote allow list.
  434. if !f.lightHouse.GetRemoteAllowList().AllowAll(hostinfo.vpnAddrs, addr.Addr()) {
  435. f.l.WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).Debug("lighthouse.remote_allow_list denied incoming handshake")
  436. return false
  437. }
  438. }
  439. ci := hostinfo.ConnectionState
  440. msg, eKey, dKey, err := ci.H.ReadMessage(nil, packet[header.Len:])
  441. if err != nil {
  442. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  443. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).WithField("header", h).
  444. Error("Failed to call noise.ReadMessage")
  445. // We don't want to tear down the connection on a bad ReadMessage because it could be an attacker trying
  446. // to DOS us. Every other error condition after should to allow a possible good handshake to complete in the
  447. // near future
  448. return false
  449. } else if dKey == nil || eKey == nil {
  450. f.l.WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  451. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  452. Error("Noise did not arrive at a key")
  453. // This should be impossible in IX but just in case, if we get here then there is no chance to recover
  454. // the handshake state machine. Tear it down
  455. return true
  456. }
  457. hs := &NebulaHandshake{}
  458. err = hs.Unmarshal(msg)
  459. if err != nil || hs.Details == nil {
  460. f.l.WithError(err).WithField("vpnAddrs", hostinfo.vpnAddrs).WithField("udpAddr", addr).
  461. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("Failed unmarshal handshake message")
  462. // The handshake state machine is complete, if things break now there is no chance to recover. Tear down and start again
  463. return true
  464. }
  465. rc, err := cert.Recombine(cert.Version(hs.Details.CertVersion), hs.Details.Cert, ci.H.PeerStatic(), ci.Curve())
  466. if err != nil {
  467. f.l.WithError(err).WithField("udpAddr", addr).
  468. WithField("vpnAddrs", hostinfo.vpnAddrs).
  469. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  470. Info("Handshake did not contain a certificate")
  471. return true
  472. }
  473. remoteCert, err := f.pki.GetCAPool().VerifyCertificate(time.Now(), rc)
  474. if err != nil {
  475. fp, err := rc.Fingerprint()
  476. if err != nil {
  477. fp = "<error generating certificate fingerprint>"
  478. }
  479. e := f.l.WithError(err).WithField("udpAddr", addr).
  480. WithField("vpnAddrs", hostinfo.vpnAddrs).
  481. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  482. WithField("certFingerprint", fp).
  483. WithField("certVpnNetworks", rc.Networks())
  484. if f.l.Level >= logrus.DebugLevel {
  485. e = e.WithField("cert", rc)
  486. }
  487. e.Info("Invalid certificate from host")
  488. return true
  489. }
  490. if len(remoteCert.Certificate.Networks()) == 0 {
  491. f.l.WithError(err).WithField("udpAddr", addr).
  492. WithField("vpnAddrs", hostinfo.vpnAddrs).
  493. WithField("cert", remoteCert).
  494. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  495. Info("No networks in certificate")
  496. return true
  497. }
  498. vpnNetworks := remoteCert.Certificate.Networks()
  499. certName := remoteCert.Certificate.Name()
  500. certVersion := remoteCert.Certificate.Version()
  501. fingerprint := remoteCert.Fingerprint
  502. issuer := remoteCert.Certificate.Issuer()
  503. hostinfo.remoteIndexId = hs.Details.ResponderIndex
  504. hostinfo.lastHandshakeTime = hs.Details.Time
  505. // Store their cert and our symmetric keys
  506. ci.peerCert = remoteCert
  507. ci.dKey = NewNebulaCipherState(dKey)
  508. ci.eKey = NewNebulaCipherState(eKey)
  509. // Make sure the current udpAddr being used is set for responding
  510. if addr.IsValid() {
  511. hostinfo.SetRemote(addr)
  512. } else {
  513. hostinfo.relayState.InsertRelayTo(via.relayHI.vpnAddrs[0])
  514. }
  515. var vpnAddrs []netip.Addr
  516. var filteredNetworks []netip.Prefix
  517. for _, network := range vpnNetworks {
  518. // vpnAddrs outside our vpn networks are of no use to us, filter them out
  519. vpnAddr := network.Addr()
  520. if !f.myVpnNetworksTable.Contains(vpnAddr) {
  521. continue
  522. }
  523. filteredNetworks = append(filteredNetworks, network)
  524. vpnAddrs = append(vpnAddrs, vpnAddr)
  525. }
  526. if len(vpnAddrs) == 0 {
  527. f.l.WithError(err).WithField("udpAddr", addr).
  528. WithField("certName", certName).
  529. WithField("certVersion", certVersion).
  530. WithField("fingerprint", fingerprint).
  531. WithField("issuer", issuer).
  532. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).Error("No usable vpn addresses from host, refusing handshake")
  533. return true
  534. }
  535. // Ensure the right host responded
  536. if !slices.Contains(vpnAddrs, hostinfo.vpnAddrs[0]) {
  537. f.l.WithField("intendedVpnAddrs", hostinfo.vpnAddrs).WithField("haveVpnNetworks", vpnNetworks).
  538. WithField("udpAddr", addr).
  539. WithField("certName", certName).
  540. WithField("certVersion", certVersion).
  541. WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  542. Info("Incorrect host responded to handshake")
  543. // Release our old handshake from pending, it should not continue
  544. f.handshakeManager.DeleteHostInfo(hostinfo)
  545. // Create a new hostinfo/handshake for the intended vpn ip
  546. f.handshakeManager.StartHandshake(hostinfo.vpnAddrs[0], func(newHH *HandshakeHostInfo) {
  547. // Block the current used address
  548. newHH.hostinfo.remotes = hostinfo.remotes
  549. newHH.hostinfo.remotes.BlockRemote(addr)
  550. f.l.WithField("blockedUdpAddrs", newHH.hostinfo.remotes.CopyBlockedRemotes()).
  551. WithField("vpnNetworks", vpnNetworks).
  552. WithField("remotes", newHH.hostinfo.remotes.CopyAddrs(f.hostMap.GetPreferredRanges())).
  553. Info("Blocked addresses for handshakes")
  554. // Swap the packet store to benefit the original intended recipient
  555. newHH.packetStore = hh.packetStore
  556. hh.packetStore = []*cachedPacket{}
  557. // Finally, put the correct vpn addrs in the host info, tell them to close the tunnel, and return true to tear down
  558. hostinfo.vpnAddrs = vpnAddrs
  559. f.sendCloseTunnel(hostinfo)
  560. })
  561. return true
  562. }
  563. // Mark packet 2 as seen so it doesn't show up as missed
  564. ci.window.Update(f.l, 2)
  565. duration := time.Since(hh.startTime).Nanoseconds()
  566. f.l.WithField("vpnAddrs", vpnAddrs).WithField("udpAddr", addr).
  567. WithField("certName", certName).
  568. WithField("certVersion", certVersion).
  569. WithField("fingerprint", fingerprint).
  570. WithField("issuer", issuer).
  571. WithField("initiatorIndex", hs.Details.InitiatorIndex).WithField("responderIndex", hs.Details.ResponderIndex).
  572. WithField("remoteIndex", h.RemoteIndex).WithField("handshake", m{"stage": 2, "style": "ix_psk0"}).
  573. WithField("durationNs", duration).
  574. WithField("sentCachedPackets", len(hh.packetStore)).
  575. Info("Handshake message received")
  576. // Build up the radix for the firewall if we have subnets in the cert
  577. hostinfo.vpnAddrs = vpnAddrs
  578. hostinfo.buildNetworks(filteredNetworks, remoteCert.Certificate.UnsafeNetworks())
  579. // Complete our handshake and update metrics, this will replace any existing tunnels for the vpnAddrs here
  580. f.handshakeManager.Complete(hostinfo, f)
  581. f.connectionManager.AddTrafficWatch(hostinfo)
  582. if f.l.Level >= logrus.DebugLevel {
  583. hostinfo.logger(f.l).Debugf("Sending %d stored packets", len(hh.packetStore))
  584. }
  585. if len(hh.packetStore) > 0 {
  586. nb := make([]byte, 12, 12)
  587. out := make([]byte, mtu)
  588. for _, cp := range hh.packetStore {
  589. cp.callback(cp.messageType, cp.messageSubType, hostinfo, cp.packet, nb, out)
  590. }
  591. f.cachedPacketMetrics.sent.Inc(int64(len(hh.packetStore)))
  592. }
  593. hostinfo.remotes.RefreshFromHandshake(vpnAddrs)
  594. f.metricHandshakes.Update(duration)
  595. return false
  596. }