-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
304 lines (272 loc) · 7.92 KB
/
builder.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package stun
import (
"crypto/sha256"
"encoding/binary"
"net"
)
const (
maxUsernameByteLength = 513
maxRealmByteLength = 763
)
// @TODO Enforce attributes to each STUN message class they belong
// @TODO Check for duplicate attributes appended?
type Builder struct {
err error
msg []byte
key []byte
messageIntegritySHA256Length int
messageIntegrity bool
fingerprint bool
}
func New(t Type, txID TxID) *Builder {
return &Builder{msg: newHeader(make([]byte, 0, 512), t, txID)}
}
// See https://tools.ietf.org/html/rfc8489#section-14.1
func (b *Builder) SetMappingAddress(addr *net.UDPAddr) {
if b.err != nil {
return
}
if len(addr.IP) != net.IPv4len && len(addr.IP) != net.IPv6len {
b.err = ErrInvalidIPAddress
return
}
b.msg = appendMappedAddress(b.msg, addr.IP, uint16(addr.Port))
}
// https://tools.ietf.org/html/rfc8489#section-14.2
func (b *Builder) SetXorMappingAddress(addr *net.UDPAddr) {
if b.err != nil {
return
}
if len(addr.IP) != net.IPv4len && len(addr.IP) != net.IPv6len {
b.err = ErrInvalidIPAddress
return
}
b.msg = appendXorMappedAddress(b.msg, addr.IP, uint16(addr.Port))
}
// See https://tools.ietf.org/html/rfc8489#section-14.3
func (b *Builder) SetUsername(username string) {
if b.err != nil {
return
}
if len(username) > maxUsernameByteLength {
b.err = ErrUsernameTooLong
return
}
b.msg = appendUsername(b.msg, username)
}
// See https://tools.ietf.org/html/rfc8489#section-14.4
func (b *Builder) SetUserHash(username, realm string) {
if b.err != nil {
return
}
if len(username) > maxUsernameByteLength {
b.err = ErrUsernameTooLong
return
}
if len(realm) > maxRealmByteLength {
b.err = ErrRealmTooLong
return
}
b.msg = appendUserHash(b.msg, username, realm)
}
// See https://tools.ietf.org/html/rfc8489#section-14.5
func (b *Builder) AddMessageIntegrity() {
b.messageIntegrity = true
}
// AddMessageIntegritySHA256 ensures a messageintegritysha256 attribute is added when message is built.
// See https://tools.ietf.org/html/rfc8489#section-14.6
func (b *Builder) AddMessageIntegritySHA256() {
b.AddMessageIntegritySHA256Truncated(sha256.Size)
}
// AddMessageIntegritySHA256 ensures a messageintegritysha256 attribute is added when message is built.
// The length allows for a truncated messageintegritysha256 value to be used, must be > 16 and divisible by 4.
func (b *Builder) AddMessageIntegritySHA256Truncated(length int) {
if b.err != nil {
return
}
if length > sha256.Size || length < 16 || length%4 != 0 {
b.err = ErrInvalidMessageIntegritySHA256Length
return
}
b.messageIntegritySHA256Length = length
}
// AddFingerprint ensures a fingerprint attribute is added as the last attribute when message is built.
// See https://tools.ietf.org/html/rfc8489#section-14.7
func (b *Builder) AddFingerprint() {
b.fingerprint = true
}
// See https://tools.ietf.org/html/rfc8489#section-14.8
func (b *Builder) SetErrorCode(errorCode ErrorCode, reason string) {
const maxReasonByteLength = 763
if b.err != nil {
return
}
if errorCode < 300 || errorCode > 699 {
b.err = ErrInvalidErrorCode
return
}
if len(reason) > maxReasonByteLength {
b.err = ErrReasonTooLong
return
}
b.msg = appendErrorCode(b.msg, errorCode, reason)
}
// See https://tools.ietf.org/html/rfc8489#section-14.9
func (b *Builder) SetRealm(realm string) {
if b.err != nil {
return
}
if len(realm) > maxRealmByteLength {
b.err = ErrRealmTooLong
return
}
b.msg = appendRealm(b.msg, realm)
}
// See https://tools.ietf.org/html/rfc8489#section-14.10
func (b *Builder) SetNonce(nonce []byte) {
const maxNonceByteLength = 763
if b.err != nil {
return
}
if len(nonce) > maxNonceByteLength {
b.err = ErrNonceTooLong
return
}
b.msg = appendNonce(b.msg, nonce)
}
// See https://tools.ietf.org/html/rfc8489#section-14.10
// & https://tools.ietf.org/html/rfc8489#section-9.2.1
func (b *Builder) SetNonceWithSecurityFeatures(features Features, nonce []byte) {
const maxNonceByteLength = 763
if b.err != nil {
return
}
if len(nonce) > maxNonceByteLength-len(nonceSecurityFeaturesPrefix)-4 {
b.err = ErrNonceTooLong
return
}
b.msg = appendNonceWithSecurityFeatures(b.msg, features, nonce)
}
// See https://tools.ietf.org/html/rfc8489#section-14.11
func (b *Builder) SetPasswordAlgorithms() {
// @TODO
}
// SetUnknownAttributes
// Adds an Error Code attribute of ErrorCodeUnknownAttribute and with the given reason
// See https://tools.ietf.org/html/rfc8489#section-14.13
func (b *Builder) SetUnknownAttributes(reason string, attributes ...uint16) {
if b.err != nil {
return
}
b.msg = appendErrorCode(b.msg, ErrorCodeUnknownAttribute, reason)
b.msg = appendUnknownAttributes(b.msg, attributes)
}
// SetSoftware appends Software attribute to the STUN message
// See https://tools.ietf.org/html/rfc8489#section-14.14
func (b *Builder) SetSoftware(software string) {
const maxSoftwareByteLength = 763
if b.err != nil {
return
}
if len(software) > maxSoftwareByteLength {
b.err = ErrSoftwareTooLong
return
}
b.msg = appendSoftware(b.msg, software)
}
// See https://tools.ietf.org/html/rfc8489#section-14.15
func (b *Builder) SetAlternateServer(ip net.IP, port uint16) {
if b.err != nil {
return
}
if len(ip) != net.IPv4len && len(ip) != net.IPv6len {
b.err = ErrInvalidIPAddress
return
}
b.msg = appendAlternateServer(b.msg, ip, port)
}
// See https://tools.ietf.org/html/rfc8489#section-14.16
func (b *Builder) SetAlternateDomain(domain string) {
const maxAlternateDomainByteLength = 255
if b.err != nil {
return
}
if len(domain) > maxAlternateDomainByteLength {
b.err = ErrDomainTooLong
return
}
b.msg = appendAlternateDomain(b.msg, domain)
}
func (b *Builder) SetPriority(typePref uint8, localPref uint16, componentID uint8) {
if b.err != nil {
return
}
if componentID < 1 {
b.err = ErrInvalidPriorityComponentID
return
}
b.msg = appendPriority(b.msg, typePref, localPref, componentID)
}
func (b *Builder) SetICEControlled(iceControlled uint64) {
if b.err != nil {
return
}
b.msg = appendICEControlled(b.msg, iceControlled)
}
// SetPassword sets the short term key used in computing the MessageIntegrity and MessageIntegritySHA256 attributes
func (b *Builder) SetPassword(password string) {
if b.err != nil {
return
}
if len(b.key) > 0 {
b.err = ErrKeySet
return
}
b.key = append(b.key[:0], password...)
}
// SetKeyLongTerm sets the long term key used in computing the MessageIntegrity and MessageIntegritySHA256 attributes
// Will automatically add PASSWORD-ALGORITHM attribute if passwordAlgorithm is anything other than PasswordAlgorithmMD5
func (b *Builder) SetKeyLongTerm(passwordAlgorithm PasswordAlgorithm, username, realm, password string) {
if b.err != nil {
return
}
if len(b.key) > 0 {
b.err = ErrKeySet
return
}
switch passwordAlgorithm {
case PasswordAlgorithmMD5:
b.key = appendLongTermKeyMD5String(b.key[:0], username, realm, password)
case PasswordAlgorithmSHA256:
b.msg = appendPasswordAlgorithm(b.msg, PasswordAlgorithmSHA256, nil)
b.key = appendLongTermKeySHA256String(b.key[:0], username, realm, password)
default:
b.err = ErrUnknownPasswordAlgorithm
}
}
// Build return the raw STUN message or an error if one occurred during it's building.
func (b *Builder) Build() ([]byte, error) {
if b.err != nil {
return nil, b.err
}
m := b.msg
if b.messageIntegrity || b.messageIntegritySHA256Length > 0 {
if len(b.key) == 0 {
return nil, ErrMissingMessageIntegrityKey
}
if b.messageIntegrity {
m = appendMessageIntegrity(m, b.key)
}
if b.messageIntegritySHA256Length > 0 {
m = appendMessageIntegritySHA256(m, b.key, b.messageIntegritySHA256Length)
}
} else if len(b.key) > 0 {
// SetPassword() or SetLongTermKey() was called but neither MessageIntegrity or MessageIntegritySHA256 used.
return nil, ErrKeyNotUsed
}
if b.fingerprint {
m = appendFingerprint(m)
}
binary.BigEndian.PutUint16(m[2:4], uint16(len(m)-headerSize))
return m, nil
}