-
Notifications
You must be signed in to change notification settings - Fork 26
/
request.go
176 lines (150 loc) · 5.09 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package handshake
import (
"context"
"errors"
ggio "github.com/gogo/protobuf/io"
p2pcrypto "github.com/libp2p/go-libp2p/core/crypto"
"go.uber.org/zap"
"golang.org/x/crypto/nacl/box"
"berty.tech/weshnet/pkg/cryptoutil"
"berty.tech/weshnet/pkg/errcode"
"berty.tech/weshnet/pkg/tyber"
)
// RequestUsingReaderWriter init a handshake with the responder, using provided ggio reader and writer
func RequestUsingReaderWriter(ctx context.Context, logger *zap.Logger, reader ggio.Reader, writer ggio.Writer, ownAccountID p2pcrypto.PrivKey, peerAccountID p2pcrypto.PubKey) error {
hc := &handshakeContext{
reader: reader,
writer: writer,
ownAccountID: ownAccountID,
peerAccountID: peerAccountID,
sharedEphemeral: &[cryptoutil.KeySize]byte{},
}
// Handshake steps on requester side (see comments below)
if err := hc.sendRequesterHello(); err != nil {
return errcode.ErrHandshakeRequesterHello.Wrap(err)
}
tyber.LogStep(ctx, logger, "Sent hello", hc.toTyberStepMutator())
if err := hc.receiveResponderHello(); err != nil {
return errcode.ErrHandshakeResponderHello.Wrap(err)
}
tyber.LogStep(ctx, logger, "Received hello", hc.toTyberStepMutator())
if err := hc.sendRequesterAuthenticate(); err != nil {
return errcode.ErrHandshakeRequesterAuthenticate.Wrap(err)
}
tyber.LogStep(ctx, logger, "Sent authenticate", hc.toTyberStepMutator())
if err := hc.receiveResponderAccept(); err != nil {
return errcode.ErrHandshakeResponderAccept.Wrap(err)
}
tyber.LogStep(ctx, logger, "Received accept", hc.toTyberStepMutator())
if err := hc.sendRequesterAcknowledge(); err != nil {
return errcode.ErrHandshakeRequesterAcknowledge.Wrap(err)
}
tyber.LogStep(ctx, logger, "Sent acknowledge", hc.toTyberStepMutator())
return nil
}
// 1st step - Requester sends: a
func (hc *handshakeContext) sendRequesterHello() error {
if err := hc.generateOwnEphemeralAndSendPubKey(); err != nil {
return errcode.ErrHandshakeOwnEphemeralKeyGenSend.Wrap(err)
}
return nil
}
// 2nd step - Requester receives: b
func (hc *handshakeContext) receiveResponderHello() error {
if err := hc.receivePeerEphemeralPubKey(); err != nil {
return errcode.ErrHandshakePeerEphemeralKeyRecv.Wrap(err)
}
// Compute shared key from Ephemeral keys
box.Precompute(hc.sharedEphemeral, hc.peerEphemeral, hc.ownEphemeral)
return nil
}
// 3rd step - Requester sends: box[a.b|a.B](A,sig[A](a.b))
func (hc *handshakeContext) sendRequesterAuthenticate() error {
var (
request RequesterAuthenticatePayload
err error
)
// Set own AccountID pub key and proof (shared_a_b signed by own AccountID)
// in RequesterAuthenticatePayload message before marshaling it
request.RequesterAccountId, err = p2pcrypto.MarshalPublicKey(hc.ownAccountID.GetPublic())
if err != nil {
return errcode.ErrSerialization.Wrap(err)
}
request.RequesterAccountSig, err = hc.ownAccountID.Sign(hc.sharedEphemeral[:])
if err != nil {
return errcode.ErrCryptoSignature.Wrap(err)
}
requestBytes, err := request.Marshal()
if err != nil {
return errcode.ErrSerialization.Wrap(err)
}
// Compute box key and seal marshaled RequesterAuthenticatePayload using
// constant nonce (see handshake.go)
boxKey, err := hc.computeRequesterAuthenticateBoxKey(true)
if err != nil {
return errcode.ErrHandshakeRequesterAuthenticateBoxKeyGen.Wrap(err)
}
boxContent := box.SealAfterPrecomputation(
nil,
requestBytes,
&nonceRequesterAuthenticate,
boxKey,
)
// Send BoxEnvelope to responder
if err = hc.writer.WriteMsg(&BoxEnvelope{Box: boxContent}); err != nil {
return errcode.ErrStreamWrite.Wrap(err)
}
return nil
}
// 4th step - Requester receives: box[a.b|A.B](sig[B](a.b))
func (hc *handshakeContext) receiveResponderAccept() error {
var (
boxEnvelope BoxEnvelope
response ResponderAcceptPayload
)
// Receive BoxEnvelope from responder
if err := hc.reader.ReadMsg(&boxEnvelope); err != nil {
return errcode.ErrStreamRead.Wrap(err)
}
// Compute box key and open marshaled RequesterAuthenticatePayload using
// constant nonce (see handshake.go)
boxKey, err := hc.computeResponderAcceptBoxKey()
if err != nil {
return errcode.ErrHandshakeResponderAcceptBoxKeyGen.Wrap(err)
}
respBytes, _ := box.OpenAfterPrecomputation(
nil,
boxEnvelope.Box,
&nonceResponderAccept,
boxKey,
)
if respBytes == nil {
err := errors.New("box opening failed")
return errcode.ErrCryptoDecrypt.Wrap(err)
}
// Unmarshal ResponderAcceptPayload
err = response.Unmarshal(respBytes)
if err != nil {
return errcode.ErrDeserialization.Wrap(err)
}
// Verify proof (shared_a_b signed by peer's AccountID)
valid, err := hc.peerAccountID.Verify(
hc.sharedEphemeral[:],
response.ResponderAccountSig,
)
if err != nil {
return errcode.ErrCryptoSignatureVerification.Wrap(err)
} else if !valid {
return errcode.ErrCryptoSignatureVerification
}
return nil
}
// 5th step - Requester sends: ok
func (hc *handshakeContext) sendRequesterAcknowledge() error {
acknowledge := &RequesterAcknowledgePayload{Success: true}
// Send Acknowledge to responder
if err := hc.writer.WriteMsg(acknowledge); err != nil {
return errcode.ErrStreamWrite.Wrap(err)
}
return nil
}