forked from scionproto/scion
/
extn_scmp_auth_drkey.go
103 lines (87 loc) · 3.06 KB
/
extn_scmp_auth_drkey.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
// Copyright 2017 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 contains the router's representation of the end-to-end SCMPAuthDRKey
// extension.
package rpkt
import (
"fmt"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/spse"
"github.com/scionproto/scion/go/lib/spse/scmp_auth"
)
var _ rExtension = (*rSCMPAuthDRKeyExtn)(nil)
// rSCMPAuthDRKeyExtn is the router's representation of the SCMPAuthDRKey extension.
type rSCMPAuthDRKeyExtn struct {
*rSPSBaseExtn
}
// rSCMPAuthDRKeyExtnFromRaw creates an rSCMPAuthDRKeyExtn instance from raw bytes,
// keeping a reference to the location in the packet's buffer.
func rSCMPAuthDRKeyExtnFromRaw(rp *RtrPkt, start, end int) (*rSCMPAuthDRKeyExtn, error) {
raw := rp.Raw[start:end]
mode := spse.SecMode(raw[0])
s := &rSCMPAuthDRKeyExtn{&rSPSBaseExtn{rp: rp, raw: raw, SecMode: mode}}
s.Logger = rp.Logger.New("ext", "SCMPAuthDRKeyExt")
return s, nil
}
func (s *rSCMPAuthDRKeyExtn) String() string {
// Delegate string representation to scmp_auth.AuthDRKeyExtn
extn, err := s.GetExtn()
if err != nil {
return fmt.Sprintf("SCMPAuthDRKey: %v", err)
}
return extn.String()
}
func (s *rSCMPAuthDRKeyExtn) RegisterHooks(h *hooks) error {
h.Validate = append(h.Validate, s.Validate)
return nil
}
func (s *rSCMPAuthDRKeyExtn) Validate() (HookResult, error) {
if len(s.raw) != scmp_auth.DRKeyTotalLength {
return HookError, common.NewBasicError("Invalid header length", nil,
"expected", scmp_auth.DRKeyTotalLength, "actual", len(s.raw))
}
return HookContinue, nil
}
// GetExtn returns the scmp_auth.DRKeyExtn representation,
// which does not have direct access to the underlying buffer.
func (s *rSCMPAuthDRKeyExtn) GetExtn() (common.Extension, error) {
extn := scmp_auth.NewDRKeyExtn()
if err := extn.SetDirection(s.Direction()); err != nil {
return nil, err
}
extn.SetMAC(s.MAC())
return extn, nil
}
func (s *rSCMPAuthDRKeyExtn) Direction() scmp_auth.Dir {
return scmp_auth.Dir(s.raw[scmp_auth.DirectionOffset])
}
func (s *rSCMPAuthDRKeyExtn) SetDirection(dir scmp_auth.Dir) {
s.raw[scmp_auth.DirectionOffset] = uint8(dir)
}
func (s *rSCMPAuthDRKeyExtn) MAC() common.RawBytes {
return s.raw[scmp_auth.MACOffset:scmp_auth.DRKeyTotalLength]
}
func (s *rSCMPAuthDRKeyExtn) SetMAC(mac common.RawBytes) error {
if len(mac) != scmp_auth.MACLength {
return common.NewBasicError("Invalid MAC length", nil,
"expected", len(s.MAC()), "actual", len(mac))
}
copy(s.MAC(), mac)
return nil
}
func (s *rSCMPAuthDRKeyExtn) ResetMac() {
for i := range s.MAC() {
s.MAC()[i] = 0
}
}