forked from kryptco/kr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport_mock_response.go
132 lines (123 loc) · 2.79 KB
/
transport_mock_response.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
package kr
import (
"bytes"
"crypto"
"crypto/rand"
"encoding/json"
"sync"
"testing"
"time"
)
var SHORT_ACK_DELAY = 500 * time.Millisecond
type ResponseTransport struct {
ImmediatePairTransport
*testing.T
sync.Mutex
responses [][]byte
sentNoOps int
RespondToAlertOnly bool
DoNotRespond bool
Ack bool
SendAfterHalfAckDelay bool
}
func (t *ResponseTransport) respondToMessage(ps *PairingSecret, m []byte, ackSent bool) (err error) {
if t.DoNotRespond {
return
}
me, sk, _ := TestMe(t.T)
var request Request
err = json.Unmarshal(m, &request)
if err != nil {
t.T.Fatal(err)
}
if request.IsNoOp() {
t.sentNoOps += 1
return
}
response := Response{
RequestID: request.RequestID,
}
if request.SendACK && !ackSent && t.Ack {
response.AckResponse = &AckResponse{}
if t.SendAfterHalfAckDelay {
go func() {
<-time.After(SHORT_ACK_DELAY / 2)
t.Lock()
defer t.Unlock()
t.respondToMessage(ps, m, true)
}()
}
} else {
if request.MeRequest != nil {
response.MeResponse = &MeResponse{
Me: me,
}
}
if request.SignRequest != nil {
fp := me.PublicKeyFingerprint()
if !bytes.Equal(request.SignRequest.PublicKeyFingerprint, fp[:]) {
t.Fatal("wrong public key")
}
sig, err := sk.Sign(rand.Reader, request.SignRequest.Data, crypto.SHA256)
if err != nil {
t.T.Fatal(err)
}
response.SignResponse = &SignResponse{
Signature: &sig,
}
}
}
respJson, err := json.Marshal(response)
if err != nil {
t.T.Fatal(err)
}
t.responses = append(t.responses, respJson)
return
}
func (t *ResponseTransport) SendMessage(ps *PairingSecret, m []byte) (err error) {
t.Lock()
defer t.Unlock()
if t.RespondToAlertOnly {
return
}
err = t.respondToMessage(ps, m, false)
return
}
func (t *ResponseTransport) PushAlert(ps *PairingSecret, alertText string, message []byte) (err error) {
t.Lock()
defer t.Unlock()
err = t.respondToMessage(ps, message, false)
return
}
func (t *ResponseTransport) Read(notifier *Notifier, ps *PairingSecret) (ciphertexts [][]byte, err error) {
pairCiphertexts, err := t.ImmediatePairTransport.Read(notifier, ps)
ciphertexts = append(ciphertexts, pairCiphertexts...)
t.Lock()
defer t.Unlock()
for _, responseBytes := range t.responses {
ctxt, err := ps.EncryptMessage(responseBytes)
if err != nil {
t.T.Fatal(err)
}
ciphertexts = append(ciphertexts, ctxt)
}
t.responses = [][]byte{}
return
}
func (t *ResponseTransport) GetSentNoOps() int {
t.Lock()
defer t.Unlock()
return t.sentNoOps
}
func (t *ResponseTransport) RemoteUnpair() {
t.Lock()
defer t.Unlock()
unpairResponse := Response{
UnpairResponse: &UnpairResponse{},
}
respJson, err := json.Marshal(unpairResponse)
if err != nil {
t.Fatal(err)
}
t.responses = append(t.responses, respJson)
}