patterns.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package handshake
  2. import (
  3. "fmt"
  4. "github.com/flynn/noise"
  5. "github.com/slackhq/nebula/header"
  6. )
  7. // msgFlags tracks what application data a handshake message carries.
  8. type msgFlags struct {
  9. expectsPayload bool // message carries indexes and time
  10. expectsCert bool // message carries the certificate
  11. }
  12. // subtypeInfo bundles the noise pattern with the per-message flags for a
  13. // given handshake subtype.
  14. type subtypeInfo struct {
  15. pattern noise.HandshakePattern
  16. msgs []msgFlags
  17. }
  18. // subtypeInfos defines the noise pattern and message content layout for each
  19. // handshake subtype.
  20. var subtypeInfos = map[header.MessageSubType]subtypeInfo{
  21. // IX: 2 messages, both carry payload and cert
  22. header.HandshakeIXPSK0: {
  23. pattern: noise.HandshakeIX,
  24. msgs: []msgFlags{
  25. {expectsPayload: true, expectsCert: true},
  26. {expectsPayload: true, expectsCert: true},
  27. },
  28. },
  29. // XX: 3 messages
  30. // msg1 (I->R): payload only
  31. // msg2 (R->I): payload + cert
  32. // msg3 (I->R): cert only
  33. //header.HandshakeXXPSK0: {
  34. // pattern: noise.HandshakeXX,
  35. // msgs: []msgFlags{
  36. // {expectsPayload: true, expectsCert: false},
  37. // {expectsPayload: true, expectsCert: true},
  38. // {expectsPayload: false, expectsCert: true},
  39. // },
  40. //},
  41. }
  42. func subtypeInfoFor(subtype header.MessageSubType) (subtypeInfo, error) {
  43. if info, ok := subtypeInfos[subtype]; ok {
  44. return info, nil
  45. }
  46. return subtypeInfo{}, fmt.Errorf("%w: %d", ErrUnknownSubtype, subtype)
  47. }