forked from skycoin/dmsg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
242 lines (207 loc) · 5.76 KB
/
types.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package dmsg
import (
"errors"
"fmt"
"strings"
"time"
"github.com/skycoin/dmsg/cipher"
)
const (
// Type returns the stream type string.
Type = "dmsg"
// HandshakePayloadVersion contains payload version to maintain compatibility with future versions
// of HandshakeData format.
HandshakePayloadVersion = "2.0"
)
var (
// HandshakeTimeout defines the duration a stream handshake should take.
HandshakeTimeout = time.Second * 20
// AcceptBufferSize defines the size of the accepts buffer.
AcceptBufferSize = 20
)
// Addr implements net.Addr for dmsg addresses.
type Addr struct {
PK cipher.PubKey `json:"public_key"`
Port uint16 `json:"port"`
}
// Network returns "dmsg"
func (Addr) Network() string {
return Type
}
// String returns public key and port of node split by colon.
func (a Addr) String() string {
if a.Port == 0 {
return fmt.Sprintf("%s:~", a.PK)
}
return fmt.Sprintf("%s:%d", a.PK, a.Port)
}
// ShortString returns a shortened string representation of the address.
func (a Addr) ShortString() string {
const PKLen = 8
if a.Port == 0 {
return fmt.Sprintf("%s:~", a.PK.String()[:PKLen])
}
return fmt.Sprintf("%s:%d", a.PK.String()[:PKLen], a.Port)
}
// Set implements pflag.Value for Addr.
func (a *Addr) Set(s string) error {
parts := strings.Split(s, ":")
for i, part := range parts {
parts[i] = strings.TrimSpace(part)
}
switch len(parts) {
case 0:
a.PK = cipher.PubKey{}
a.Port = 0
return nil
case 1:
return a.PK.Set(parts[0])
case 2:
if parts[0] == "" {
a.PK = cipher.PubKey{}
} else {
if err := a.PK.Set(parts[0]); err != nil {
return err
}
}
if parts[1] == "~" || parts[1] == "" {
a.Port = 0
} else {
_, err := fmt.Sscan(parts[1], &a.Port)
return err
}
return nil
default:
return errors.New("invalid dmsg.Addr string")
}
}
// Type implements pflag.Value for Addr.
func (Addr) Type() string {
return "dmsg.Addr"
}
/* Request & Response */
const sigLen = len(cipher.Sig{})
// SignedObject represents a gob-encoded structure prepended with a signature.
type SignedObject []byte
// MakeSignedStreamRequest encodes and signs a StreamRequest into a SignedObject format.
func MakeSignedStreamRequest(req *StreamRequest, sk cipher.SecKey) SignedObject {
obj := encodeGob(req)
sig := SignBytes(obj, sk)
signedObj := append(sig[:], obj...)
req.raw = signedObj
return signedObj
}
// MakeSignedStreamResponse encodes and signs a StreamResponse into a SignedObject format.
func MakeSignedStreamResponse(resp *StreamResponse, sk cipher.SecKey) SignedObject {
obj := encodeGob(resp)
sig := SignBytes(obj, sk)
signedObj := append(sig[:], obj...)
resp.raw = signedObj
return signedObj
}
// Valid returns true if the SignedObject has a valid length.
func (so SignedObject) Valid() bool {
return len(so) > sigLen
}
// Hash returns the hash of the SignedObject.
func (so SignedObject) Hash() cipher.SHA256 {
return cipher.SumSHA256(so)
}
// Sig returns the prepended signature section of the SignedObject.
func (so SignedObject) Sig() cipher.Sig {
var sig cipher.Sig
copy(sig[:], so)
return sig
}
// Object returns the bytes of the SignedObject that contain the encoded object.
func (so SignedObject) Object() []byte {
return so[sigLen:]
}
// ObtainStreamRequest obtains a StreamRequest from the encoded object bytes.
func (so SignedObject) ObtainStreamRequest() (StreamRequest, error) {
if !so.Valid() {
return StreamRequest{}, ErrSignedObjectInvalid
}
var req StreamRequest
err := decodeGob(&req, so[sigLen:])
req.raw = so
return req, err
}
// ObtainStreamResponse obtains a StreamResponse from the encoded object bytes.
func (so SignedObject) ObtainStreamResponse() (StreamResponse, error) {
if !so.Valid() {
return StreamResponse{}, ErrSignedObjectInvalid
}
var resp StreamResponse
err := decodeGob(&resp, so[sigLen:])
resp.raw = so
return resp, err
}
// StreamRequest represents a stream dial request object.
type StreamRequest struct {
Timestamp int64
SrcAddr Addr
DstAddr Addr
NoiseMsg []byte
raw SignedObject `enc:"-"` // back reference.
}
// Verify verifies the StreamRequest.
func (req StreamRequest) Verify(lastTimestamp int64) error {
// Check fields.
if req.SrcAddr.PK.Null() {
return ErrReqInvalidSrcPK
}
if req.SrcAddr.Port == 0 {
return ErrReqInvalidSrcPort
}
if req.DstAddr.PK.Null() {
return ErrReqInvalidDstPK
}
if req.DstAddr.Port == 0 {
return ErrReqInvalidDstPort
}
if req.Timestamp <= lastTimestamp {
return ErrReqInvalidTimestamp
}
// Check signature.
if err := cipher.VerifyPubKeySignedPayload(req.SrcAddr.PK, req.raw.Sig(), req.raw.Object()); err != nil {
return ErrReqInvalidSig.Wrap(err)
}
return nil
}
// StreamResponse is the response of a StreamRequest.
type StreamResponse struct {
ReqHash cipher.SHA256 // Hash of associated dial request.
Accepted bool // Whether the request is accepted.
ErrCode errorCode // Check if not accepted.
NoiseMsg []byte
raw SignedObject `enc:"-"` // back reference.
}
// Verify verifies the StreamResponse.
func (resp StreamResponse) Verify(req StreamRequest) error {
// Check fields.
if resp.ReqHash != req.raw.Hash() {
return ErrDialRespInvalidHash
}
// Check signature.
if err := cipher.VerifyPubKeySignedPayload(req.DstAddr.PK, resp.raw.Sig(), resp.raw.Object()); err != nil {
return ErrDialRespInvalidSig.Wrap(err)
}
// Check whether response states that the request is accepted.
if !resp.Accepted {
ok, err := ErrorFromCode(resp.ErrCode)
if !ok {
err = ErrDialRespNotAccepted
}
return err
}
return nil
}
// SignBytes signs the provided bytes with the given secret key.
func SignBytes(b []byte, sk cipher.SecKey) cipher.Sig {
sig, err := cipher.SignPayload(b, sk)
if err != nil {
panic(fmt.Errorf("dmsg: unexpected error occurred during StreamDialObject.Sign(): %v", err))
}
return sig
}