forked from OpenZWave/open-zwave
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurity.cpp
More file actions
269 lines (250 loc) · 10.1 KB
/
Copy pathSecurity.cpp
File metadata and controls
269 lines (250 loc) · 10.1 KB
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
//-----------------------------------------------------------------------------
//
// Security.cpp
//
// Implementation of the Z-Wave COMMAND_CLASS_Security
//
// Copyright (c) 2011 Mal Lansell <openzwave@lansell.org>
//
// SOFTWARE NOTICE AND LICENSE
//
// This file is part of OpenZWave.
//
// OpenZWave is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
//
// OpenZWave is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
//
//-----------------------------------------------------------------------------
#include <ctime>
#include "command_classes/CommandClasses.h"
#include "command_classes/Security.h"
#include "Defs.h"
#include "Msg.h"
#include "Node.h"
#include "Driver.h"
#include "platform/Log.h"
#include "Utils.h"
#include "value_classes/ValueBool.h"
namespace OpenZWave
{
namespace Internal
{
namespace CC
{
Security::Security(uint32 const _homeId, uint8 const _nodeId) :
CommandClass(_homeId, _nodeId), m_schemeagreed(false), m_secured(false)
{
/* We don't want the Driver to route "Security" messages back to us for Encryption,
* so disable SecureSupport for the Security Command Class
* (This stops this Command Class getting Marked as as IsSecured() if its listed
* in the SecurityCmd_SupportedReport from the device - Which some devices do)
*/
ClearSecureSupport();
}
Security::~Security()
{
}
bool Security::Init(uint32 const _instance)
{
Msg* msg = new Msg("SecurityCmd_SupportedGet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId());
msg->SetInstance(this, _instance);
msg->Append(GetNodeId());
msg->Append(2);
msg->Append(GetCommandClassId());
msg->Append(SecurityCmd_SupportedGet);
msg->Append(GetDriver()->GetTransmitOptions());
msg->setEncrypted();
GetDriver()->SendMsg(msg, Driver::MsgQueue_Command);
return true;
}
//-----------------------------------------------------------------------------
// <Security::RequestState>
// Request current state from the device
//-----------------------------------------------------------------------------
bool Security::ExchangeNetworkKeys()
{
if (GetNodeUnsafe()->IsAddingNode())
{
Msg * msg = new Msg("SecurityCmd_SchemeGet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId());
msg->Append(GetNodeId());
msg->Append(3);
msg->Append(GetCommandClassId());
msg->Append(SecurityCmd_SchemeGet);
msg->Append(0);
msg->Append(GetDriver()->GetTransmitOptions());
/* SchemeGet is unencrypted */
GetDriver()->SendMsg(msg, Driver::MsgQueue_Command);
return true;
}
return false;
}
//-----------------------------------------------------------------------------
// <Security::RequestState>
// Request current state from the device
//-----------------------------------------------------------------------------
bool Security::RequestState(uint32 const _requestFlags, uint8 const _instance, Driver::MsgQueue const _queue)
{
#if 0
if( _requestFlags & RequestFlag_Static )
{
}
return false;
#endif
return true;
}
//-----------------------------------------------------------------------------
// <Security::RequestValue>
// Request current state from the device
//-----------------------------------------------------------------------------
bool Security::RequestValue(uint32 const _requestFlags, uint16 const _index, uint8 const _instance, Driver::MsgQueue const _queue)
{
Log::Write(LogLevel_Info, GetNodeId(), "Got a RequestValue Call");
return true;
}
bool Security::HandleSupportedReport(uint8 const* _data, uint32 const _length, uint32 const _instance)
{
#ifdef DEBUG
PrintHex("Security Classes", _data, _length);
#endif
GetNodeUnsafe()->SetSecuredClasses(_data, _length, _instance);
return true;
}
//-----------------------------------------------------------------------------
// <Security::HandleMsg>
// Handle a message from the Z-Wave network
//-----------------------------------------------------------------------------
bool Security::HandleMsg(uint8 const* _data, uint32 const _length, uint32 const _instance // = 1
)
{
switch ((SecurityCmd) _data[0])
{
case SecurityCmd_SupportedReport:
{
/* this is a list of CommandClasses that should be Encrypted.
* and it might contain new command classes that were not present in the NodeInfoFrame
* so we have to run through, mark existing Command Classes as SetSecured (so SendMsg in the Driver
* class will route the unecrypted messages to our SendMsg) and for New Command
* Classes, create them, and of course, also do a SetSecured on them.
*
* This means we must do a SecurityCmd_SupportedGet request ASAP so we dont have
* Command Classes created after the Discovery Phase is completed!
*/
Log::Write(LogLevel_Info, GetNodeId(), "Received SecurityCmd_SupportedReport from node %d (instance %d)", GetNodeId(), _instance);
m_secured = true;
if (Internal::VC::ValueBool* value = static_cast<Internal::VC::ValueBool*>(GetValue(_instance, ValueID_Index_Security::Secured)))
{
value->OnValueRefreshed(m_secured);
value->Release();
}
HandleSupportedReport(&_data[2], _length - 3, _instance);
break;
}
case SecurityCmd_SchemeReport:
{
Log::Write(LogLevel_Info, GetNodeId(), "Received SecurityCmd_SchemeReport from node %d: %d", GetNodeId(), _data[1]);
uint8 schemes = _data[1];
if (m_schemeagreed == true)
{
Log::Write(LogLevel_Warning, GetNodeId(), " Already Received a SecurityCmd_SchemeReport from the node. Ignoring");
break;
}
if (schemes == SecurityScheme_Zero)
{
/* We're good to go. We now should send our NetworkKey to the device if this is the first
* time we have seen it
*/
Log::Write(LogLevel_Info, GetNodeId(), " Security scheme agreed.");
/* create the NetworkKey Packet. EncryptMessage will encrypt it for us (And request the NONCE) */
Msg * msg = new Msg("SecurityCmd_NetworkKeySet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId());
msg->Append(GetNodeId());
msg->Append(18);
msg->Append(GetCommandClassId());
msg->Append(SecurityCmd_NetworkKeySet);
for (int i = 0; i < 16; i++)
msg->Append(GetDriver()->GetNetworkKey()[i]);
msg->Append(GetDriver()->GetTransmitOptions());
msg->setEncrypted();
GetDriver()->SendMsg(msg, Driver::MsgQueue_Command);
m_schemeagreed = true;
}
else
{
/* No common security scheme. The device should continue as an unsecured node.
* but Some Command Classes might not be present...
*/
Log::Write(LogLevel_Warning, GetNodeId(), " No common security scheme. The device will continue as an unsecured node.");
}
break;
}
case SecurityCmd_NetworkKeySet:
{
/* we shouldn't get a NetworkKeySet from a node if we are the controller
* as we send it out to the Devices
*/
Log::Write(LogLevel_Info, GetNodeId(), "Received SecurityCmd_NetworkKeySet from node %d", GetNodeId());
break;
}
case SecurityCmd_NetworkKeyVerify:
{
/* if we can decrypt this packet, then we are assured that our NetworkKeySet is successfull
* and thus should set the Flag referenced in SecurityCmd_SchemeReport
*/
Log::Write(LogLevel_Info, GetNodeId(), "Received SecurityCmd_NetworkKeyVerify from node %d", GetNodeId());
/* now as for our SupportedGet */
Msg* msg = new Msg("SecurityCmd_SupportedGet", GetNodeId(), REQUEST, FUNC_ID_ZW_SEND_DATA, true, true, FUNC_ID_APPLICATION_COMMAND_HANDLER, GetCommandClassId());
msg->Append(GetNodeId());
msg->Append(2);
msg->Append(GetCommandClassId());
msg->Append(SecurityCmd_SupportedGet);
msg->Append(GetDriver()->GetTransmitOptions());
msg->setEncrypted();
GetDriver()->SendMsg(msg, Driver::MsgQueue_Command);
break;
}
case SecurityCmd_SchemeInherit:
{
/* only used in a Controller Replication Type enviroment.
*
*/
Log::Write(LogLevel_Info, GetNodeId(), "Received SecurityCmd_SchemeInherit from node %d", GetNodeId());
break;
}
/* the rest of these should be handled by the Driver Code (in Driver::ProcessMsg) */
case SecurityCmd_NonceGet:
case SecurityCmd_NonceReport:
case SecurityCmd_MessageEncap:
case SecurityCmd_MessageEncapNonceGet:
{
Log::Write(LogLevel_Warning, GetNodeId(), "Received a Security Message that should have been handled in the Driver");
break;
}
default:
{
return false;
}
}
return true;
}
//-----------------------------------------------------------------------------
// <Security::CreateVars>
// Create the values managed by this command class
//-----------------------------------------------------------------------------
void Security::CreateVars(uint8 const _instance)
{
if (Node* node = GetNodeUnsafe())
{
node->CreateValueBool(ValueID::ValueGenre_System, GetCommandClassId(), _instance, ValueID_Index_Security::Secured, "Secured", "", true, false, false, 0);
}
}
} // namespace CC
} // namespace Internal
} // namespace OpenZWave