2
0

handshake_ix.go 25 KB

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