@@ -103,26 +103,24 @@ func (f *Interface) readOutsidePackets(via ViaSender, out []byte, packet []byte,
103103 }
104104
105105 // All remaining packets are encrypted
106- ci := hostinfo .ConnectionState
107- if ! ci .window .Check (f .l , h .MessageCounter ) {
108- return
109- }
110-
111- // Relay packets are special
112106 if isMessageRelay {
113- f .handleOutsideRelayPacket (hostinfo , via , out , packet , h , fwPacket , lhf , nb , q , localCache )
114-
107+ var signedPayload []byte
108+ // Relay packets are special, this branch should always early-return
109+ signedPayload , err = hostinfo .ConnectionState .VerifyRelay (f .l , h .MessageCounter , out , packet , nb )
110+ if err != nil {
111+ if f .l .Enabled (context .Background (), slog .LevelDebug ) {
112+ hostinfo .logger (f .l ).Debug ("Failed to verify relay packet" , "error" , err , "from" , via , "header" , h )
113+ }
114+ return
115+ }
116+ f .handleOutsideRelayPacket (hostinfo , via , out , signedPayload , h , fwPacket , lhf , nb , q , localCache )
115117 return
116118 }
117119
118- out , err = f . decrypt ( hostinfo , h .MessageCounter , out , packet , h , nb )
120+ out , err = hostinfo . ConnectionState . Decrypt ( f . l , h .MessageCounter , out , packet , nb )
119121 if err != nil {
120122 if f .l .Enabled (context .Background (), slog .LevelDebug ) {
121- hostinfo .logger (f .l ).Debug ("Failed to decrypt packet" ,
122- "error" , err ,
123- "from" , via ,
124- "header" , h ,
125- )
123+ hostinfo .logger (f .l ).Debug ("Failed to decrypt packet" , "error" , err , "from" , via , "header" , h )
126124 }
127125 return
128126 }
@@ -151,7 +149,7 @@ func (f *Interface) readOutsidePackets(via ViaSender, out []byte, packet []byte,
151149 // No-op, useful for the Roaming and connectionManager side-effects above
152150 case header .TestRequest :
153151 //recycle the input packet ciphertext as our output buffer
154- f .send (header .Test , header .TestReply , ci , hostinfo , out , nb , packet )
152+ f .send (header .Test , header .TestReply , hostinfo . ConnectionState , hostinfo , out , nb , packet )
155153 default :
156154 hostinfo .logger (f .l ).Error ("IsValidSubType was true, but unexpected test subtype seen" , "from" , via , "header" , h )
157155 return
@@ -169,28 +167,7 @@ func (f *Interface) readOutsidePackets(via ViaSender, out []byte, packet []byte,
169167 }
170168}
171169
172- func (f * Interface ) handleOutsideRelayPacket (hostinfo * HostInfo , via ViaSender , out []byte , packet []byte , h * header.H , fwPacket * firewall.Packet , lhf * LightHouseHandler , nb []byte , q int , localCache firewall.ConntrackCache ) {
173- // The entire body is sent as AD, not encrypted.
174- // The packet consists of a 16-byte parsed Nebula header, Associated Data-protected payload, and a trailing 16-byte AEAD signature value.
175- // The packet is guaranteed to be at least 16 bytes at this point, b/c it got past the h.Parse() call above. If it's
176- // otherwise malformed (meaning, there is no trailing 16 byte AEAD value), then this will result in at worst a 0-length slice
177- // which will gracefully fail in the DecryptDanger call.
178- signedPayload := packet [:len (packet )- hostinfo .ConnectionState .dKey .Overhead ()]
179- signatureValue := packet [len (packet )- hostinfo .ConnectionState .dKey .Overhead ():]
180- var err error
181- out , err = hostinfo .ConnectionState .dKey .DecryptDanger (out , signedPayload , signatureValue , h .MessageCounter , nb )
182- if err != nil {
183- return
184- }
185- // Advance the replay window now that the frame is authenticated
186- if ! hostinfo .ConnectionState .window .Update (f .l , h .MessageCounter ) {
187- if f .l .Enabled (context .Background (), slog .LevelDebug ) {
188- hostinfo .logger (f .l ).Debug ("dropping out of window relay packet" , "header" , h )
189- }
190- return
191- }
192- // Successfully validated the thing. Get rid of the Relay header.
193- signedPayload = signedPayload [header .Len :]
170+ func (f * Interface ) handleOutsideRelayPacket (hostinfo * HostInfo , via ViaSender , out []byte , signedPayload []byte , h * header.H , fwPacket * firewall.Packet , lhf * LightHouseHandler , nb []byte , q int , localCache firewall.ConntrackCache ) {
194171 // Pull the Roaming parts up here, and return in all call paths.
195172 f .handleHostRoaming (hostinfo , via )
196173 // Track usage of both the HostInfo and the Relay for the received & authenticated packet
@@ -504,20 +481,6 @@ func parseV4(data []byte, incoming bool, fp *firewall.Packet) error {
504481 return nil
505482}
506483
507- func (f * Interface ) decrypt (hostinfo * HostInfo , mc uint64 , out []byte , packet []byte , h * header.H , nb []byte ) ([]byte , error ) {
508- var err error
509- out , err = hostinfo .ConnectionState .dKey .DecryptDanger (out , packet [:header .Len ], packet [header .Len :], mc , nb )
510- if err != nil {
511- return nil , err
512- }
513-
514- if ! hostinfo .ConnectionState .window .Update (f .l , mc ) {
515- return nil , ErrOutOfWindow
516- }
517-
518- return out , nil
519- }
520-
521484func (f * Interface ) handleOutsideMessagePacket (hostinfo * HostInfo , out []byte , packet []byte , fwPacket * firewall.Packet , nb []byte , q int , localCache firewall.ConntrackCache ) {
522485 err := newPacket (out , true , fwPacket )
523486 if err != nil {
0 commit comments