forked from gosnmp/gosnmp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3_usm_test.go
226 lines (179 loc) · 8.14 KB
/
v3_usm_test.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
package gosnmp
import (
"encoding/hex"
"io/ioutil"
"log"
"testing"
"github.com/stretchr/testify/require"
)
/**
* This tests use hex dumps from real network traffic produced using net-snmp's snmpget with demo.snmplabs.com as SNMP agent.
*/
func authorativeEngineID(t *testing.T) string {
// engine ID of demo.snmplabs.com
engineID, err := hex.DecodeString("80004fb805636c6f75644dab22cc")
require.NoError(t, err, "EngineId decoding failed.")
return string(engineID)
}
func correctKeySHA224(t *testing.T) []byte {
correctKey, err := hex.DecodeString("f2a2ebaa9677ad286255596286ca4fb7ec22f52405cb0aac334c5f15")
require.NoError(t, err, "Correct key initialization failed.")
return correctKey
}
func packetSHA224NoAuthentication(t *testing.T) []byte {
packet, err := hex.DecodeString("308184020103300e02025f84020205c0040105020103043f303d040e80004fb805636c6f75644dab22cc02012b0203203ea5040f7573722d7368613232342d6e6f6e650410000000000000000000000000000000000400302e040e80004fb805636c6f75644dab22cc0400a01a02023ced020100020100300e300c06082b060102010101000500")
require.NoError(t, err, "Non-authenticated packet data SHA224 decoding failed.")
return packet
}
func packetSHA224Authenticated(t *testing.T) []byte {
packet, err := hex.DecodeString("308184020103300e02025f84020205c0040105020103043f303d040e80004fb805636c6f75644dab22cc02012b0203203ea5040f7573722d7368613232342d6e6f6e65041066cd2d9b04cd48b02a9df0c77dc3415d0400302e040e80004fb805636c6f75644dab22cc0400a01a02023ced020100020100300e300c06082b060102010101000500")
require.NoError(t, err, "Authenticated packet data SHA224 decoding failed.")
return packet
}
func packetSHA224AuthenticationParams(t *testing.T) string {
params, err := hex.DecodeString("66cd2d9b04cd48b02a9df0c77dc3415d")
require.NoError(t, err, "Authentication parameters SHA224 decoding failed.")
return string(params)
}
func TestAuthenticationSHA224(t *testing.T) {
var err error
sp := UsmSecurityParameters{
localAESSalt: 0,
localDESSalt: 0,
AuthoritativeEngineBoots: 43,
AuthoritativeEngineID: authorativeEngineID(t),
AuthoritativeEngineTime: 2113189,
UserName: "usr-sha224-none",
AuthenticationParameters: "",
PrivacyParameters: nil,
AuthenticationProtocol: SHA224,
PrivacyProtocol: 0,
AuthenticationPassphrase: "authkey1",
PrivacyPassphrase: "",
SecretKey: nil,
Logger: NewLogger(log.New(ioutil.Discard, "", 0)),
PrivacyKey: nil,
}
sp.SecretKey, err = genlocalkey(sp.AuthenticationProtocol,
sp.AuthenticationPassphrase,
sp.AuthoritativeEngineID)
require.NoError(t, err, "Generation of key failed")
require.Equal(t, correctKeySHA224(t), sp.SecretKey, "Wrong key generated")
srcPacket := packetSHA224NoAuthentication(t)
err = sp.authenticate(srcPacket)
require.NoError(t, err, "Authentication of packet failed")
require.Equal(t, packetSHA224Authenticated(t), srcPacket, "Wrong message authentication parameters.")
}
func TestIsAuthenticaSHA224(t *testing.T) {
var err error
sp := UsmSecurityParameters{
localAESSalt: 0,
localDESSalt: 0,
AuthoritativeEngineBoots: 43,
AuthoritativeEngineID: authorativeEngineID(t),
AuthoritativeEngineTime: 2113189,
UserName: "usr-sha224-none",
AuthenticationParameters: packetSHA224AuthenticationParams(t),
PrivacyParameters: nil,
AuthenticationProtocol: SHA224,
PrivacyProtocol: 0,
AuthenticationPassphrase: "authkey1",
PrivacyPassphrase: "",
SecretKey: nil,
PrivacyKey: nil,
Logger: NewLogger(log.New(ioutil.Discard, "", 0)),
}
sp.SecretKey, err = genlocalkey(sp.AuthenticationProtocol,
sp.AuthenticationPassphrase,
sp.AuthoritativeEngineID)
require.NoError(t, err, "Generation of key failed")
require.Equal(t, correctKeySHA224(t), sp.SecretKey, "Wrong key generated")
srcPacket := packetSHA224NoAuthentication(t)
snmpPacket := SnmpPacket{
SecurityParameters: &sp,
}
authentic, err := sp.isAuthentic(srcPacket, &snmpPacket)
require.NoError(t, err, "Authentication check of key failed")
require.True(t, authentic, "Packet was not considered to be authentic")
}
func correctKeySHA512(t *testing.T) []byte {
correctKey, err := hex.DecodeString("c336e5e6396926813d623984610e8f0cd7f419da75c82ac50927c84fd92027f7cdd849ce983036dca67bfb1e8fde2a8c2d45cd2f0d3e0b0b929f7dda462a58cf")
require.NoError(t, err, "Correct key initialization failed.")
return correctKey
}
func packetSHA512NoAuthentication(t *testing.T) []byte {
packet, err := hex.DecodeString("3081a4020103300e0202366e020205c0040105020103045f305d040e80004fb805636c6f75644dab22cc02012b0203203eea040f7573722d7368613531322d6e6f6e6504300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400302e040e80004fb805636c6f75644dab22cc0400a01a020214d9020100020100300e300c06082b060102010101000500")
require.NoError(t, err, "Not-authenticated packet data SHA512 decoding failed.")
return packet
}
func packetSHA512Authenticated(t *testing.T) []byte {
packet, err := hex.DecodeString("3081a4020103300e0202366e020205c0040105020103045f305d040e80004fb805636c6f75644dab22cc02012b0203203eea040f7573722d7368613531322d6e6f6e65043026f8087ced336a394642b8698eba9810929a9bfa44afbf43975a7ad6c4cc55bd279b549a77ec56d791467612747d6f570400302e040e80004fb805636c6f75644dab22cc0400a01a020214d9020100020100300e300c06082b060102010101000500")
require.NoError(t, err, "Authenticated packet data SHA512 decoding failed.")
return packet
}
func packetSHA512AuthenticationParams(t *testing.T) string {
params, err := hex.DecodeString("26f8087ced336a394642b8698eba9810929a9bfa44afbf43975a7ad6c4cc55bd279b549a77ec56d791467612747d6f57")
require.NoError(t, err, "Authentication parameters SHA512 decoding failed.")
return string(params)
}
func TestAuthenticationSHA512(t *testing.T) {
var err error
sp := UsmSecurityParameters{
localAESSalt: 0,
localDESSalt: 0,
AuthoritativeEngineBoots: 43,
AuthoritativeEngineID: authorativeEngineID(t),
AuthoritativeEngineTime: 2113258,
UserName: "usr-sha512-none",
AuthenticationParameters: "",
PrivacyParameters: nil,
AuthenticationProtocol: SHA512,
PrivacyProtocol: 0,
AuthenticationPassphrase: "authkey1",
PrivacyPassphrase: "",
SecretKey: nil,
PrivacyKey: nil,
Logger: NewLogger(log.New(ioutil.Discard, "", 0)),
}
sp.SecretKey, err = genlocalkey(sp.AuthenticationProtocol,
sp.AuthenticationPassphrase,
sp.AuthoritativeEngineID)
require.NoError(t, err, "Generation of key failed")
require.Equal(t, correctKeySHA512(t), sp.SecretKey, "Wrong key generated")
srcPacket := packetSHA512NoAuthentication(t)
err = sp.authenticate(srcPacket)
require.NoError(t, err, "Generation of key failed")
require.Equal(t, packetSHA512Authenticated(t), srcPacket, "Wrong message authentication parameters.")
}
func TestIsAuthenticaSHA512(t *testing.T) {
var err error
sp := UsmSecurityParameters{
localAESSalt: 0,
localDESSalt: 0,
AuthoritativeEngineBoots: 43,
AuthoritativeEngineID: authorativeEngineID(t),
AuthoritativeEngineTime: 2113189,
UserName: "usr-sha512-none",
AuthenticationParameters: packetSHA512AuthenticationParams(t),
PrivacyParameters: nil,
AuthenticationProtocol: SHA512,
PrivacyProtocol: 0,
AuthenticationPassphrase: "authkey1",
PrivacyPassphrase: "",
SecretKey: nil,
Logger: NewLogger(log.New(ioutil.Discard, "", 0)),
PrivacyKey: nil,
}
sp.SecretKey, err = genlocalkey(sp.AuthenticationProtocol,
sp.AuthenticationPassphrase,
sp.AuthoritativeEngineID)
require.NoError(t, err, "Generation of key failed")
require.Equal(t, correctKeySHA512(t), sp.SecretKey, "Wrong key generated")
srcPacket := packetSHA512NoAuthentication(t)
snmpPacket := SnmpPacket{
SecurityParameters: &sp,
}
authentic, err := sp.isAuthentic(srcPacket, &snmpPacket)
require.NoError(t, err, "Authentication check of key failed")
require.True(t, authentic, "Packet was not considered to be authentic")
}