SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TwoWayAuthentication.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
21
22
23 #include "NativeFeatureIncludes.h"
24 #if _RAKNET_SUPPORT_TwoWayAuthentication==1
25
26 #ifndef __TWO_WAY_AUTHENTICATION_H
27 #define __TWO_WAY_AUTHENTICATION_H
28
29 // How often to change the nonce.
30 #define NONCE_TIMEOUT_MS 10000
31 // How often to check for ID_TWO_WAY_AUTHENTICATION_OUTGOING_CHALLENGE_TIMEOUT, and the minimum timeout time. Maximum is double this value.
32 #define CHALLENGE_MINIMUM_TIMEOUT 3000
33
34 #if LIBCAT_SECURITY==1
35 // From CPP FILE:
36 // static const int HASH_BITS = 256;
37 // static const int HASH_BYTES = HASH_BITS / 8;
38 // static const int STRENGTHENING_FACTOR = 1000;
39 #define TWO_WAY_AUTHENTICATION_NONCE_LENGTH 32
40 #define HASHED_NONCE_AND_PW_LENGTH 32
41 #else
42 #include "DR_SHA1.h"
43 #define TWO_WAY_AUTHENTICATION_NONCE_LENGTH 20
44 #define HASHED_NONCE_AND_PW_LENGTH SHA1_LENGTH
45 #endif
46
47 #include "PluginInterface2.h"
48 #include "memoryoverride.h"
49 #include "NativeTypes.h"
50 #include "string.h"
51 #include "DS_Hash.h"
52 #include "DS_Queue.h"
53
54 typedef int64_t FCM2Guid;
55
56 namespace SLNet
57 {
59 class RakPeerInterface;
60
67 class RAK_DLL_EXPORT TwoWayAuthentication : public PluginInterface2
68 {
69 public:
70 // GetInstance() and DestroyInstance(instance*)
71 STATIC_FACTORY_DECLARATIONS(TwoWayAuthentication)
72
73 TwoWayAuthentication();
74 virtual ~TwoWayAuthentication();
75
83 bool AddPassword(SLNet::RakString identifier, SLNet::RakString password);
84
90 bool Challenge(SLNet::RakString identifier, AddressOrGUID remoteSystem);
91
93 void Clear(void);
94
96 virtual void Update(void);
98 virtual PluginReceiveResult OnReceive(Packet *packet);
100 virtual void OnRakPeerShutdown(void);
102 virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
103
105 struct PendingChallenge
106 {
107 SLNet::RakString identifier;
108 AddressOrGUID remoteSystem;
109 SLNet::Time time;
110 bool sentHash;
111 };
112
113 DataStructures::Queue<PendingChallenge> outgoingChallenges;
114
116 struct NonceAndRemoteSystemRequest
117 {
118 char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH];
119 SLNet::AddressOrGUID remoteSystem;
120 unsigned short requestId;
121 SLNet::Time whenGenerated;
122 };
124 struct RAK_DLL_EXPORT NonceGenerator
125 {
126 NonceGenerator();
127 ~NonceGenerator();
128 void GetNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short *requestId, SLNet::AddressOrGUID remoteSystem);
129 void GenerateNonce(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH]);
130 bool GetNonceById(char nonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], unsigned short requestId, SLNet::AddressOrGUID remoteSystem, bool popIfFound);
131 void Clear(void);
132 void ClearByAddress(SLNet::AddressOrGUID remoteSystem);
133 void Update(SLNet::Time curTime);
134
135 DataStructures::List<TwoWayAuthentication::NonceAndRemoteSystemRequest*> generatedNonces;
136 unsigned short nextRequestId;
137 };
138
139 protected:
140 void PushToUser(MessageID messageId, SLNet::RakString password, SLNet::AddressOrGUID remoteSystem);
141 // Key is identifier, data is password
142 DataStructures::Hash<SLNet::RakString, SLNet::RakString, 16, SLNet::RakString::ToInteger > passwords;
143
144 SLNet::Time whenLastTimeoutCheck;
145
146 NonceGenerator nonceGenerator;
147
148 void OnNonceRequest(Packet *packet);
149 void OnNonceReply(Packet *packet);
150 PluginReceiveResult OnHashedNonceAndPassword(Packet *packet);
151 void OnPasswordResult(Packet *packet);
152 void Hash(char thierNonce[TWO_WAY_AUTHENTICATION_NONCE_LENGTH], SLNet::RakString password, char out[HASHED_NONCE_AND_PW_LENGTH]);
153 };
154
155 } // namespace SLNet
156
157 #endif
158
159 #endif // _RAKNET_SUPPORT_*