-
Notifications
You must be signed in to change notification settings - Fork 16
/
hpp_packet.go
153 lines (120 loc) · 3.46 KB
/
hpp_packet.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
package nex
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
)
// HPPPacket holds all the data about an HPP request
type HPPPacket struct {
sender *HPPClient
accessKeySignature []byte
passwordSignature []byte
payload []byte
message *RMCMessage
processed chan bool
}
// Sender returns the Client who sent the packet
func (p *HPPPacket) Sender() ConnectionInterface {
return p.sender
}
// Payload returns the packets payload
func (p *HPPPacket) Payload() []byte {
return p.payload
}
// SetPayload sets the packets payload
func (p *HPPPacket) SetPayload(payload []byte) {
p.payload = payload
}
func (p *HPPPacket) validateAccessKeySignature(signature string) error {
signatureBytes, err := hex.DecodeString(signature)
if err != nil {
return fmt.Errorf("Failed to decode access key signature. %s", err)
}
p.accessKeySignature = signatureBytes
calculatedSignature, err := p.calculateAccessKeySignature()
if err != nil {
return fmt.Errorf("Failed to calculate access key signature. %s", err)
}
if !bytes.Equal(calculatedSignature, p.accessKeySignature) {
return errors.New("Access key signature does not match")
}
return nil
}
func (p *HPPPacket) calculateAccessKeySignature() ([]byte, error) {
accessKey := p.Sender().Endpoint().AccessKey()
accessKeyBytes, err := hex.DecodeString(accessKey)
if err != nil {
return nil, err
}
signature, err := p.calculateSignature(p.payload, accessKeyBytes)
if err != nil {
return nil, err
}
return signature, nil
}
func (p *HPPPacket) validatePasswordSignature(signature string) error {
signatureBytes, err := hex.DecodeString(signature)
if err != nil {
return fmt.Errorf("Failed to decode password signature. %s", err)
}
p.passwordSignature = signatureBytes
calculatedSignature, err := p.calculatePasswordSignature()
if err != nil {
return fmt.Errorf("Failed to calculate password signature. %s", err)
}
if !bytes.Equal(calculatedSignature, p.passwordSignature) {
return errors.New("Password signature does not match")
}
return nil
}
func (p *HPPPacket) calculatePasswordSignature() ([]byte, error) {
sender := p.Sender()
pid := sender.PID()
account, _ := sender.Endpoint().(*HPPServer).AccountDetailsByPID(pid)
if account == nil {
return nil, errors.New("PID does not exist")
}
key := DeriveKerberosKey(pid, []byte(account.Password))
signature, err := p.calculateSignature(p.payload, key)
if err != nil {
return nil, err
}
return signature, nil
}
func (p *HPPPacket) calculateSignature(buffer []byte, key []byte) ([]byte, error) {
mac := hmac.New(md5.New, key)
_, err := mac.Write(buffer)
if err != nil {
return nil, err
}
hmac := mac.Sum(nil)
return hmac, nil
}
// RMCMessage returns the packets RMC Message
func (p *HPPPacket) RMCMessage() *RMCMessage {
return p.message
}
// SetRMCMessage sets the packets RMC Message
func (p *HPPPacket) SetRMCMessage(message *RMCMessage) {
p.message = message
}
// NewHPPPacket creates and returns a new HPPPacket using the provided Client and payload
func NewHPPPacket(client *HPPClient, payload []byte) (*HPPPacket, error) {
hppPacket := &HPPPacket{
sender: client,
payload: payload,
processed: make(chan bool),
}
if payload != nil {
rmcMessage := NewRMCRequest(client.Endpoint())
err := rmcMessage.FromBytes(payload)
if err != nil {
return nil, fmt.Errorf("Failed to decode HPP request. %s", err)
}
hppPacket.SetRMCMessage(rmcMessage)
}
return hppPacket, nil
}