OpenZWave Library 1.2
Security.h
Go to the documentation of this file.
1//-----------------------------------------------------------------------------
2//
3// Security.h
4//
5// Implementation of the Z-Wave COMMAND_CLASS_Security
6//
7// Copyright (c) 2010 Mal Lansell <openzwave@lansell.org>
8//
9// SOFTWARE NOTICE AND LICENSE
10//
11// This file is part of OpenZWave.
12//
13// OpenZWave is free software: you can redistribute it and/or modify
14// it under the terms of the GNU Lesser General Public License as published
15// by the Free Software Foundation, either version 3 of the License,
16// or (at your option) any later version.
17//
18// OpenZWave is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU Lesser General Public License for more details.
22//
23// You should have received a copy of the GNU Lesser General Public License
24// along with OpenZWave. If not, see <http://www.gnu.org/licenses/>.
25//
26//-----------------------------------------------------------------------------
27
28#ifndef _Security_H
29#define _Security_H
30
31#include <ctime>
32#include "aes/aescpp.h"
34
35namespace OpenZWave
36{
40 typedef struct SecurityPayload {
44 string logmsg;
46
47 /* This should probably go into its own file, but its so simple... and only the Security Command Class uses it currently
48 */
49
50 class Timer {
51 public:
53 this->Reset();
54 };
55 virtual ~Timer() {};
56 void Reset() {
57 start = clock();
58 }
59 inline uint64 GetMilliseconds() {
60 return (uint64 )(((clock() - start) / (double)CLOCKS_PER_SEC) / 1000);
61 }
62 private:
63 clock_t start;
64 };
65
66 class Security: public CommandClass
67 {
68 public:
69 static CommandClass* Create( uint32 const _homeId, uint8 const _nodeId ){ return new Security( _homeId, _nodeId ); }
70 virtual ~Security();
71
72 static uint8 const StaticGetCommandClassId(){ return 0x98; }
73 static string const StaticGetCommandClassName(){ return "COMMAND_CLASS_SECURITY"; }
74 bool Init();
75 // From CommandClass
76 virtual uint8 const GetCommandClassId()const{ return StaticGetCommandClassId(); }
77 virtual string const GetCommandClassName()const{ return StaticGetCommandClassName(); }
78 virtual bool HandleMsg( uint8 const* _data, uint32 const _length, uint32 const _instance = 1 );
79 void ReadXML(TiXmlElement const* _ccElement);
80 void WriteXML(TiXmlElement* _ccElement);
81 void SendMsg( Msg* _msg );
82
83 protected:
84 void CreateVars( uint8 const _instance );
85
86 private:
87 Security( uint32 const _homeId, uint8 const _nodeId );
88 bool RequestState( uint32 const _requestFlags, uint8 const _instance, Driver::MsgQueue const _queue);
89 bool RequestValue( uint32 const _requestFlags, uint8 const _index, uint8 const _instance, Driver::MsgQueue const _queue);
90 bool HandleSupportedReport(uint8 const* _data, uint32 const _length);
91 void SendNonceReport();
92 void RequestNonce();
93 bool GenerateAuthentication( uint8 const* _data, uint32 const _length, uint8 const _sendingNode, uint8 const _receivingNode, uint8 *iv, uint8* _authentication);
94 bool DecryptMessage( uint8 const* _data, uint32 const _length );
95 bool EncryptMessage( uint8 const* _nonce );
96 void QueuePayload( SecurityPayload * _payload );
97 bool createIVFromPacket_inbound(uint8 const* _data, uint8 *iv);
98 bool createIVFromPacket_outbound(uint8 const* _data, uint8 *iv);
99 void SetupNetworkKey();
100
101 Mutex *m_queueMutex;
102 list<SecurityPayload *> m_queue; // Messages waiting to be sent when the device wakes up
103 bool m_waitingForNonce;
104 uint8 m_sequenceCounter;
105 Timer m_nonceTimer;
106 uint8 currentNonce[8];
107 bool m_networkkeyset;
108
109 aes_encrypt_ctx *AuthKey;
110 aes_encrypt_ctx *EncryptKey;
111 uint8 *nk;
112 bool m_schemeagreed;
113 bool m_secured;
114
115
116
117
118
119 };
120
121} // namespace OpenZWave
122
123#endif
124
unsigned int uint32
Definition: Defs.h:69
unsigned char uint8
Definition: Defs.h:63
Base class for all Z-Wave command classes.
Definition: CommandClass.h:47
MsgQueue
Definition: Driver.h:584
Message object to be passed to and from devices on the Z-Wave network.
Definition: Msg.h:43
Implements a platform-independent mutex–for serializing access to a shared resource.
Definition: Mutex.h:40
Definition: Security.h:67
static CommandClass * Create(uint32 const _homeId, uint8 const _nodeId)
Definition: Security.h:69
static uint8 const StaticGetCommandClassId()
Definition: Security.h:72
void ReadXML(TiXmlElement const *_ccElement)
Definition: Security.cpp:150
virtual ~Security()
Definition: Security.cpp:137
void SendMsg(Msg *_msg)
Definition: Security.cpp:501
virtual uint8 const GetCommandClassId() const
Definition: Security.h:76
void WriteXML(TiXmlElement *_ccElement)
Definition: Security.cpp:162
bool Init()
Definition: Security.cpp:269
static string const StaticGetCommandClassName()
Definition: Security.h:73
virtual string const GetCommandClassName() const
Definition: Security.h:77
void CreateVars(uint8 const _instance)
Definition: Security.cpp:1053
virtual bool HandleMsg(uint8 const *_data, uint32 const _length, uint32 const _instance=1)
Definition: Security.cpp:357
Definition: Security.h:50
uint64 GetMilliseconds()
Definition: Security.h:59
void Reset()
Definition: Security.h:56
Timer()
Definition: Security.h:52
virtual ~Timer()
Definition: Security.h:55
Definition: Bitfield.h:35
struct OpenZWave::SecurityPayload SecurityPayload
Implements COMMAND_CLASS_SECURITY (0x98), a Z-Wave device command class.
Implements COMMAND_CLASS_SECURITY (0x98), a Z-Wave device command class.
Definition: Security.h:40
uint8 m_data[32]
Definition: Security.h:43
string logmsg
Definition: Security.h:44
uint8 m_length
Definition: Security.h:41
uint8 m_part
Definition: Security.h:42
Definition: aes.h:81