-
Notifications
You must be signed in to change notification settings - Fork 3
/
letter-wire.go
189 lines (162 loc) · 3.72 KB
/
letter-wire.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
package jess
import (
"errors"
"github.com/safing/structures/container"
)
/*
### Wire Format Version 1
- Wire Format Version: varint
- Flags: varint
- 1: Setup Msg (includes Version and Tools)
- 2: Sending Keys
- 4: Apply Keys
- Version: varint (if Setup Msg)
- SuiteID: byte block (if Setup Msg)
- Keys:
- Amount: varint
- IDs/Values: byte blocks
- Nonce: byte block
- Data: byte block
- MAC: byte block
*/
// ErrIncompatibleWireFormatVersion is returned when an incompatible wire format is encountered.
var ErrIncompatibleWireFormatVersion = errors.New("incompatible wire format version")
// ToWire serializes to letter for sending it over a network connection.
func (letter *Letter) ToWire() (*container.Container, error) {
c := container.New()
// Wire Format Version: varint
c.AppendNumber(1)
// Flags: varint
// - 1: Setup Msg (includes Version and Tools)
// - 2: Sending Keys
// - 4: Apply Keys
var flags uint64
if letter.Version > 0 {
flags |= 1
}
if len(letter.Keys) > 0 {
flags |= 2
}
if letter.ApplyKeys {
flags |= 4
}
c.AppendNumber(flags)
if letter.Version > 0 {
// Version: varint (if Setup Msg)
c.AppendNumber(uint64(letter.Version))
// SuiteID: byte block (if Setup Msg)
c.AppendAsBlock([]byte(letter.SuiteID))
}
if len(letter.Keys) > 0 {
// Keys:
// - Amount: varint
// - IDs/Values: byte blocks
c.AppendInt(len(letter.Keys))
for _, seal := range letter.Keys {
c.AppendAsBlock([]byte(seal.ID))
c.AppendAsBlock(seal.Value)
}
}
// Nonce: byte block
c.AppendAsBlock(letter.Nonce)
// Data: byte block
c.AppendAsBlock(letter.Data)
// MAC: byte block
c.AppendAsBlock(letter.Mac)
// debugging:
// fmt.Printf("%+v\n", c.CompileData())
return c, nil
}
// LetterFromWireData is a relay to LetterFromWire to quickly fix import issues of godep.
//
// Deprecated: Please use LetterFromWire with a fresh container directly.
func LetterFromWireData(data []byte) (*Letter, error) {
return LetterFromWire(container.New(data))
}
// LetterFromWire parses a letter sent over a network connection.
func LetterFromWire(c *container.Container) (*Letter, error) {
letter := &Letter{}
// Wire Format Version: varint
wireFormatVersion, err := c.GetNextN8()
if err != nil {
return nil, err
}
if wireFormatVersion != 1 {
return nil, ErrIncompatibleWireFormatVersion
}
// Flags: varint
// - 1: Setup Msg (includes Version and Tools)
// - 2: Sending Keys
// - 4: Apply Keys
var (
setupMsg bool
sendingKeys bool
)
flags, err := c.GetNextN64()
if err != nil {
return nil, err
}
if flags&1 > 0 {
setupMsg = true
}
if flags&2 > 0 {
sendingKeys = true
}
if flags&4 > 0 {
letter.ApplyKeys = true
}
if setupMsg {
// Version: varint (if Setup Msg)
n, err := c.GetNextN8()
if err != nil {
return nil, err
}
letter.Version = n
// SuiteID: byte block (if Setup Msg)
suiteID, err := c.GetNextBlock()
if err != nil {
return nil, err
}
letter.SuiteID = string(suiteID)
}
if sendingKeys {
// Keys:
// - Amount: varint
// - IDs/Values: byte blocks
n, err := c.GetNextN8()
if err != nil {
return nil, err
}
letter.Keys = make([]*Seal, n)
for i := 0; i < len(letter.Keys); i++ {
signetID, err := c.GetNextBlock()
if err != nil {
return nil, err
}
sealValue, err := c.GetNextBlock()
if err != nil {
return nil, err
}
letter.Keys[i] = &Seal{
ID: string(signetID),
Value: sealValue,
}
}
}
// Nonce: byte block
letter.Nonce, err = c.GetNextBlock()
if err != nil {
return nil, err
}
// Data: byte block
letter.Data, err = c.GetNextBlock()
if err != nil {
return nil, err
}
// MAC: byte block
letter.Mac, err = c.GetNextBlock()
if err != nil {
return nil, err
}
return letter, nil
}