2022-10-17 02:03:59 -04:00
|
|
|
package noise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
|
2024-10-22 17:37:17 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
2022-10-17 02:03:59 -04:00
|
|
|
"github.com/flynn/noise"
|
|
|
|
)
|
|
|
|
|
2022-11-14 00:10:58 -05:00
|
|
|
func (c *NoiseSession) RunOutgoingHandshake() error {
|
2024-10-19 10:44:08 -04:00
|
|
|
log.Debug("Starting outgoing handshake")
|
|
|
|
|
2022-12-15 23:52:05 +00:00
|
|
|
negData, msg, state, err := ComposeInitiatorHandshakeMessage(c.HandKey, nil, nil, nil)
|
2022-12-12 17:44:43 +00:00
|
|
|
if err != nil {
|
2024-10-19 10:44:08 -04:00
|
|
|
log.WithError(err).Error("Failed to compose initiator handshake message")
|
2022-10-17 02:03:59 -04:00
|
|
|
return err
|
|
|
|
}
|
2024-10-19 10:44:08 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"negData_length": len(negData),
|
|
|
|
"msg_length": len(msg),
|
|
|
|
}).Debug("Initiator handshake message composed")
|
2024-11-04 15:20:56 -05:00
|
|
|
c.HandshakeState = &HandshakeState{
|
|
|
|
protocol: state,
|
|
|
|
}
|
2024-10-19 10:44:08 -04:00
|
|
|
|
2022-10-17 02:03:59 -04:00
|
|
|
if _, err = c.Write(negData); err != nil {
|
2024-10-19 10:44:08 -04:00
|
|
|
log.WithError(err).Error("Failed to write negotiation data")
|
2022-10-17 02:03:59 -04:00
|
|
|
return err
|
|
|
|
}
|
2024-10-19 10:44:08 -04:00
|
|
|
log.Debug("Negotiation data written successfully")
|
|
|
|
|
2022-10-17 02:03:59 -04:00
|
|
|
if _, err = c.Write(msg); err != nil {
|
2024-10-19 10:44:08 -04:00
|
|
|
log.WithError(err).Error("Failed to write handshake message")
|
2022-10-17 02:03:59 -04:00
|
|
|
return err
|
|
|
|
}
|
2024-10-19 10:44:08 -04:00
|
|
|
log.Debug("Handshake message written successfully")
|
|
|
|
log.WithField("state", state).Debug("Handshake state after message write")
|
2022-12-15 23:52:05 +00:00
|
|
|
log.Println(state)
|
2022-10-17 02:03:59 -04:00
|
|
|
c.handshakeComplete = true
|
2024-10-19 10:44:08 -04:00
|
|
|
log.Debug("Outgoing handshake completed successfully")
|
2022-10-17 02:03:59 -04:00
|
|
|
return nil
|
|
|
|
}
|
2024-11-04 15:20:56 -05:00
|
|
|
|
|
|
|
func ComposeInitiatorHandshakeMessage(s noise.DHKey, rs []byte, payload []byte, ePrivate []byte) (negData, msg []byte, state *noise.HandshakeState, err error) {
|
|
|
|
log.Debug("Starting ComposeInitiatorHandshakeMessage")
|
|
|
|
|
|
|
|
if len(rs) != 0 && len(rs) != noise.DH25519.DHLen() {
|
|
|
|
return nil, nil, nil, errors.New("only 32 byte curve25519 public keys are supported")
|
|
|
|
}
|
|
|
|
|
|
|
|
negData = make([]byte, 6)
|
|
|
|
copy(negData, initNegotiationData(nil))
|
|
|
|
pattern := noise.HandshakeXK
|
|
|
|
negData[5] = NOISE_PATTERN_XK
|
|
|
|
|
|
|
|
var random io.Reader
|
|
|
|
if len(ePrivate) == 0 {
|
|
|
|
random = rand.Reader
|
|
|
|
} else {
|
|
|
|
random = bytes.NewBuffer(ePrivate)
|
|
|
|
}
|
|
|
|
|
|
|
|
config := noise.Config{
|
|
|
|
CipherSuite: noise.NewCipherSuite(noise.DH25519, noise.CipherAESGCM, noise.HashSHA256),
|
|
|
|
Pattern: pattern,
|
|
|
|
Initiator: true,
|
|
|
|
StaticKeypair: s,
|
|
|
|
Random: random,
|
|
|
|
}
|
|
|
|
|
|
|
|
state, err = noise.NewHandshakeState(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
2024-11-04 15:25:54 -05:00
|
|
|
// Write message, expecting no CipherStates yet since this is message 1
|
2024-11-04 15:20:56 -05:00
|
|
|
msg, cs0, cs1, err := state.WriteMessage(nil, payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify no CipherStates are returned yet
|
|
|
|
if cs0 != nil || cs1 != nil {
|
|
|
|
return nil, nil, nil, errors.New("unexpected cipher states in message 1")
|
|
|
|
}
|
|
|
|
|
|
|
|
return negData, msg, state, nil
|
|
|
|
}
|