Debian Codegraph @

handshake/credential.go

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