1
0

credential.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package handshake
  2. import (
  3. "crypto/rand"
  4. "github.com/flynn/noise"
  5. "github.com/slackhq/nebula/cert"
  6. )
  7. // Credential holds everything needed to participate in a handshake
  8. // at a given cert version. Version and Curve are read from Cert; the public
  9. // half of the static keypair likewise comes from Cert.PublicKey().
  10. type Credential struct {
  11. Cert cert.Certificate // the certificate
  12. Bytes []byte // pre-marshaled certificate bytes
  13. privateKey []byte // static private key (public half lives in Cert)
  14. cipherSuite noise.CipherSuite // pre-built cipher suite (DH + cipher + hash)
  15. }
  16. // NewCredential creates a Credential with all material needed for handshake
  17. // participation. The cipherSuite should be pre-built by the caller with the
  18. // appropriate DH function, cipher, and hash.
  19. func NewCredential(
  20. c cert.Certificate,
  21. hsBytes []byte,
  22. privateKey []byte,
  23. cipherSuite noise.CipherSuite,
  24. ) *Credential {
  25. return &Credential{
  26. Cert: c,
  27. Bytes: hsBytes,
  28. privateKey: privateKey,
  29. cipherSuite: cipherSuite,
  30. }
  31. }
  32. // buildHandshakeState creates a noise.HandshakeState from this credential.
  33. func (hc *Credential) buildHandshakeState(initiator bool, pattern noise.HandshakePattern) (*noise.HandshakeState, error) {
  34. return noise.NewHandshakeState(noise.Config{
  35. CipherSuite: hc.cipherSuite,
  36. Random: rand.Reader,
  37. Pattern: pattern,
  38. Initiator: initiator,
  39. StaticKeypair: noise.DHKey{Private: hc.privateKey, Public: hc.Cert.PublicKey()},
  40. PresharedKey: []byte{},
  41. PresharedKeyPlacement: 0,
  42. })
  43. }
  44. // GetCredentialFunc returns the handshake credential for the given version,
  45. // or nil if that version is not available.
  46. //
  47. // Implementations must return credentials drawn from a snapshot stable for
  48. // the lifetime of any single Machine. The Machine may call this multiple
  49. // times during a handshake (e.g. when negotiating to the peer's version)
  50. // and assumes the underlying static keypair is consistent across calls.
  51. type GetCredentialFunc func(v cert.Version) *Credential