-
Notifications
You must be signed in to change notification settings - Fork 155
/
pkt_auth.go
226 lines (192 loc) · 6.76 KB
/
pkt_auth.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
// Copyright 2022 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file includes the SPAO header implementation as specified
// in https://docs.scion.org/en/latest/protocols/authenticator-option.html
// The Authenticator option format is as follows:
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | NextHdr=UDP | ExtLen | OptType=2 | OptDataLen |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Security Parameter Index |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | Algorithm | RSV | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
// | Timestamp / Sequence Number |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
// | |
// + +
// | |
// + 16-octet MAC data +
// | |
// + +
// | |
// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
package slayers
import (
"encoding/binary"
"github.com/scionproto/scion/pkg/private/serrors"
)
const (
PacketAuthASHost uint8 = iota
PacketAuthHostHost
)
const (
PacketAuthSenderSide uint8 = iota
PacketAuthReceiverSide
)
const (
// PacketAuthOptionMetadataLen is the size of the SPAO Metadata and
// corresponds the minimum size of the SPAO OptData.
// The SPAO header contains the following fixed-length fields:
// SPI (4 Bytes), Algorithm (1 Byte), RSV (1 Byte) and
// Timestamp / Sequence Number (6 Bytes).
PacketAuthOptionMetadataLen = 12
)
// PacketAuthSPI (Security Parameter Index) is the identifier for the key
// used for the packet authentication option. DRKey values are in the
// range [1, 2^21-1].
type PacketAuthSPI uint32
func (p PacketAuthSPI) Type() uint8 {
if p&(1<<17) == 0 {
return PacketAuthASHost
}
return PacketAuthHostHost
}
func (p PacketAuthSPI) Direction() uint8 {
if p&(1<<16) == 0 {
return PacketAuthSenderSide
}
return PacketAuthReceiverSide
}
func (p PacketAuthSPI) DRKeyProto() uint16 {
return uint16(p)
}
func (p PacketAuthSPI) IsDRKey() bool {
return p > 0 && p < (1<<21)
}
func MakePacketAuthSPIDRKey(
proto uint16,
drkeyType uint8,
dir uint8,
) (PacketAuthSPI, error) {
if proto < 1 {
return 0, serrors.New("Invalid proto identifier value")
}
if drkeyType > 1 {
return 0, serrors.New("Invalid DRKeyType value")
}
if dir > 1 {
return 0, serrors.New("Invalid DRKeyDirection value")
}
spi := uint32((drkeyType & 0x1)) << 17
spi |= uint32((dir & 0x1)) << 16
spi |= uint32(proto)
return PacketAuthSPI(spi), nil
}
// PacketAuthAlg is the enumerator for authenticator algorithm types in the
// packet authenticator option.
type PacketAuthAlg uint8
const (
PacketAuthCMAC PacketAuthAlg = iota
PacketAuthSHA1_AES_CBC
)
type PacketAuthOptionParams struct {
SPI PacketAuthSPI
Algorithm PacketAuthAlg
TimestampSN uint64
Auth []byte
}
// PacketAuthOption wraps an EndToEndOption of OptTypeAuthenticator.
// This can be used to serialize and parse the internal structure of the packet authenticator
// option.
type PacketAuthOption struct {
*EndToEndOption
}
// NewPacketAuthOption creates a new EndToEndOption of
// OptTypeAuthenticator, initialized with the given SPAO data.
func NewPacketAuthOption(
p PacketAuthOptionParams,
) (PacketAuthOption, error) {
o := PacketAuthOption{EndToEndOption: new(EndToEndOption)}
err := o.Reset(p)
return o, err
}
// ParsePacketAuthOption parses o as a packet authenticator option.
// Performs minimal checks to ensure that SPI, algorithm, timestamp, RSV, and
// sequence number are set.
// Checking the size and content of the Authenticator data must be done by the
// caller.
func ParsePacketAuthOption(o *EndToEndOption) (PacketAuthOption, error) {
if o.OptType != OptTypeAuthenticator {
return PacketAuthOption{},
serrors.New("wrong option type", "expected", OptTypeAuthenticator, "actual", o.OptType)
}
if len(o.OptData) < PacketAuthOptionMetadataLen {
return PacketAuthOption{},
serrors.New("buffer too short", "expected at least", 12, "actual", len(o.OptData))
}
return PacketAuthOption{o}, nil
}
// Reset reinitializes the underlying EndToEndOption with the SPAO data.
// Reuses the OptData buffer if it is of sufficient capacity.
func (o PacketAuthOption) Reset(
p PacketAuthOptionParams,
) error {
if p.TimestampSN >= (1 << 48) {
return serrors.New("Timestamp value should be smaller than 2^48")
}
o.OptType = OptTypeAuthenticator
n := PacketAuthOptionMetadataLen + len(p.Auth)
if n <= cap(o.OptData) {
o.OptData = o.OptData[:n]
} else {
o.OptData = make([]byte, n)
}
binary.BigEndian.PutUint32(o.OptData[:4], uint32(p.SPI))
o.OptData[4] = byte(p.Algorithm)
o.OptData[5] = byte(0)
bigEndianPutUint48(o.OptData[6:12], p.TimestampSN)
copy(o.OptData[12:], p.Auth)
o.OptAlign = [2]uint8{4, 2}
// reset unused/implicit fields
o.OptDataLen = 0
o.ActualLength = 0
return nil
}
// SPI returns the value set in the Security Parameter Index in the extension.
func (o PacketAuthOption) SPI() PacketAuthSPI {
return PacketAuthSPI(binary.BigEndian.Uint32(o.OptData[:4]))
}
// Algorithm returns the algorithm type stored in the data buffer.
func (o PacketAuthOption) Algorithm() PacketAuthAlg {
return PacketAuthAlg(o.OptData[4])
}
// Timestamp returns the value set in the homonym field in the extension.
func (o PacketAuthOption) TimestampSN() uint64 {
return bigEndianUint48(o.OptData[6:12])
}
// Authenticator returns slice of the underlying auth buffer.
// Changes to this slice will be reflected on the wire when
// the extension is serialized.
func (o PacketAuthOption) Authenticator() []byte {
return o.OptData[12:]
}
func bigEndianUint48(b []byte) uint64 {
return uint64(b[0])<<40 + uint64(b[1])<<32 +
uint64(binary.BigEndian.Uint32(b[2:6]))
}
func bigEndianPutUint48(b []byte, v uint64) {
b[0] = byte(v >> 40)
b[1] = byte(v >> 32)
binary.BigEndian.PutUint32(b[2:6], uint32(v))
}