-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPKDataStrong4.cc
More file actions
281 lines (229 loc) · 11.1 KB
/
PKDataStrong4.cc
File metadata and controls
281 lines (229 loc) · 11.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
270
271
272
273
274
275
276
277
278
279
280
281
/***************************************************************************
* *
* _ _____ ____ *
* /\ | | | __ \ /\ | _ \ /\ *
* / \ | | | | | | / \ | |_) | / \ *
* / /\ \ | | | | | | / /\ \ | _ < / /\ \ *
* / ____ \ | |___ | |__| / / ____ \ | |_) / / ____ \ *
* /_/ \_\ | ____| |_____/ /_/ \_\ |____/ /_/ \_\ *
* *
* == {Port Knocking/Single Packet Authorization} Security Suite == *
* *
***************************************************************************
* *
* This file is part of Aldaba Knocking Suite. *
* *
* Copyright (c) 2010, Luis MartinGarcia. (aldabaknocking@gmail.com) *
* *
* Aldaba is free software; you can redistribute it and/or modify it under *
* the terms of the GNU General Public License as published by the Free *
* Software Foundation; Version 2 of the License, with the exceptions, *
* conditions and clarifications described in the file named LICENSE.txt, *
* distributed with Aldaba or available from: *
* <http://www.aldabaknocking.com/LICENSE.txt> *
* *
* Aldaba 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 General Public License *
* v2.0 for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with Aldaba; if not, write to the Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
* *
* Please check file LICENSE.txt for the complete version of the license, *
* as this disclaimer does not contain the complete information. Also, note*
* that although Aldaba is licensed under the GNU GPL v2.0 license, it may *
* be possible to obtain copies of it under different, less restrictive, *
* alternative licenses. Requests will be studied on a case by case basis. *
* If you wish to obtain Aldaba under a different license, please use the *
* email address shown above. *
* *
***************************************************************************/
#include "RawData.h"
#include "aldaba.h"
#include "output.h"
#include "IPAddress.h"
#include "PKDataStrong4.h"
#include "tools.h"
#include "sha256.h"
#include "hmac_sha256.h"
#include "GeneralOps.h"
extern GeneralOps o;
PKDataStrong4::PKDataStrong4(){
this->reset();
} /* End of PKDataStrong4 contructor */
PKDataStrong4::~PKDataStrong4(){
} /* End of PKDataStrong4 destructor */
/** Puts the object back to its initial state */
void PKDataStrong4::reset(){
memset(&(this->h), 0, sizeof(this->h));
this->length=0; /* Zero until set() is called. */
return;
} /* End of reset() */
/** Returns a pointer to the knock data.
* @warning The caller must ensure setKnockData() has been called before
* calling this method. Otherwise a zeroed buffer will be returned */
u8 *PKDataStrong4::getBufferPointer(){
return (u8 *)(&h);
} /* End of getBufferPointer() */
/** Returns a pointer to the knock data.
* @warning The caller must ensure setKnockData() has been called before
* calling this method. Otherwise a zeroed buffer will be returned
* @param len is a pointer to an unsigned integer var where the knock data
* length is stored by this function (if not NULL). */
u8 *PKDataStrong4::getBufferPointer(u32 *len){
if(len!=NULL)
*len=(u32)this->length;
return (u8 *)(&h);
} /* End of getBufferPointer() */
/** Stores supplied data in the internal buffer so the information
* can be accessed using the standard get & set methods.
* @warning The PKDataStrong4 class is able to hold a maximum of
* PK_STRONG_IPv4_DATA_LEN bytes. If the supplied buffer is longer than that, it
* will fatal().
* @warning Supplied len MUST be PK_STRONG_IPv4_DATA_LEN bytes (min IP header
* length).
* @return OP_SUCCESS on success. Fatals on error. */
int PKDataStrong4::storeRecvData(const u8 *buf, size_t len){
if(buf==NULL || len!=PK_STRONG_IPv4_DATA_LEN){
fatal(OUT_2, "%s(): Invalid parameters.", __func__);
}else{
this->reset(); /* Re-init the object, just in case the caller had used it already */
this->length=PK_STRONG_IPv4_DATA_LEN;
memcpy(&(this->h), buf, PK_STRONG_IPv4_DATA_LEN);
}
return OP_SUCCESS;
} /* End of storeRecvData() */
/** Stores knock data in the supplied buffer.
* @warning supplied pointer MUST be able to hold at least
* PK_STRONG_IPv4_DATA_LEN bytes.
* @returns the number of bytes written to the buffer or -1 in case of error
* @warning If setKnockData() has not been called before, 0 bytes will be
* written and the value 0 will be returned. Therefore, caller should check
* for positive, non-zero, values to make sure the call succeded. */
void PKDataStrong4::getKnockData(u8 *buff, u32 *final_len){
if(buff!=NULL)
memcpy(buff, &(this->h), this->length);
if(final_len!=NULL)
*final_len=(u32)this->length;
} /* End of getKnockData() */
/** Returns a pointer to the knock data.
* @warning The caller must ensure setKnockData() has been called before
* calling this method. Otherwise a zeroed buffer will be returned
* @param len is a pointer to an unsigned integer var where the knock data
* length is stored by this function (if not NULL). */
u8 *PKDataStrong4::getKnockData(u32 *len){
if(len!=NULL)
*len=(u32)this->length;
return (u8 *)(&h);
} /* End of getKnockData() */
/** Returns a pointer to the knock data.
* @warning The caller must ensure setKnockData() has been called before
* calling this method. Otherwise a zeroed buffer will be returned */
u8 *PKDataStrong4::getKnockData(){
return (u8 *)(&h);
} /* End of getKnockData() */
/** Stores caller supplied knock data in the object's internal buffer.
* Normally the caller would store the knock data so it can later call
* validate(), which will determine if the data is valid or not.
* @warning supplied pointer MUST hold at least PK_STRONG_IPv4_DATA_LEN bytes.
* @returns OP_SUCCESS on success and OP_FAILURE in case of error.
* @warning This method overwrites the object's internal buffers. This includes
* data produced by the other setKnockData(). */
int PKDataStrong4::setKnockData(u8 *buff){
if(buff==NULL)
return OP_FAILURE;
memcpy(&(this->h), buff, PK_STRONG_IPv4_DATA_LEN);
this->length=PK_STRONG_IPv4_DATA_LEN;
return OP_SUCCESS;
} /* End of getKnockData() */
/** Encode knock data.
* @param ip is the knock IP address, which MUST be an IPv4 address
* @param port is the knock port.
* @param action MUST be one of OPEN_PORT or CLOSE_PORT
* @param buffer is the target buffer where the knock data will be stored. Note
* that it MUST be able to hold at least PK_LIGHT_IPv4_DATA_LEN bytes. */
int PKDataStrong4::setKnockData(u8 *buffer, u32 *final_len, IPAddress ip, tcp_port_t port, int action, u8 *key, size_t keylen){
u8 hmac[HMAC_SHA256_LEN];
pk_strong_header_ipv4 *p = (pk_strong_header_ipv4 *)buffer;
if(buffer==NULL)
fatal(OUT_2, "%s(): NULL parameter supplied", __func__);
if( ip.getVersion()!=AF_INET )
fatal(OUT_2, "%s() Expected IP version 4 address. Please report a bug.", __func__);
memcpy(p->nonce, o.rand.getRandomData(PK_STRONG_IPv4_NONCE_LEN), PK_STRONG_IPv4_NONCE_LEN);
/* Set or unset the least significant bit */
if(action==ACTION_OPEN)
p->nonce[PK_STRONG_IPv4_NONCE_LEN-1] |= 0x01;
else
p->nonce[PK_STRONG_IPv4_NONCE_LEN-1] &= ~(0x01);
p->timestamp = htonl( (u32)time(NULL) );
p->knock_ip=ip.getIPv4Address();
p->knock_port=htons(port);
memset(p->mac, 0, PK_STRONG_IPv4_MAC_LEN);
/* Now compute the HMAC-SHA256. Note that the last two bytes are set to zero,
* except for the "action bit", which may be one or zero, depending on whether
* the specified action is "OPEN_PORT" or "CLOSE_PORT" respectively */
HMAC_SHA256::hmac_sha256(key, keylen, buffer, PK_STRONG_IPv4_DATA_LEN, hmac, HMAC_SHA256_LEN);
/* Copy the 6 most significant bytes of the hash */
memcpy(p->mac, hmac, PK_STRONG_IPv4_MAC_LEN);
/* Update length */
if(final_len!=NULL)
*final_len=PK_STRONG_IPv4_DATA_LEN;
return OP_SUCCESS;
} /* End of getKnockData_IPv4() */
/** Encode knock data.
* @param ip is the knock IP address, which MUST be an IPv4 address
* @param port is the knock port.
* @param action MUST be one of OPEN_PORT or CLOSE_PORT */
int PKDataStrong4::setKnockData(IPAddress ip, tcp_port_t port, int action, u8 *key, size_t keylen){
this->length=PK_STRONG_IPv4_DATA_LEN;
return setKnockData((u8 *)&this->h, &this->length, ip, port, action, key, keylen);
}
IPAddress PKDataStrong4::getAddress(){
IPAddress ip;
ip.setAddress(this->h.knock_ip);
return ip;
} /* End of getAddress() */
tcp_port_t PKDataStrong4::getPort(){
return ntohs(this->h.knock_port);
}
int PKDataStrong4::getAction(){
if( this->h.nonce[PK_STRONG_IPv4_NONCE_LEN-1]%2==0 )
return ACTION_CLOSE;
else
return ACTION_OPEN;
}/* End of getPort() */
u32 PKDataStrong4::getTimestamp(){
return ntohl(this->h.timestamp);
} /* End of getTimestamp() */
u8 *PKDataStrong4::getNonce(size_t *final_len){
if(final_len!=NULL)
*final_len=PK_STRONG_IPv4_NONCE_LEN;
return this->h.nonce;
} /* End of getNonce() */
u8 *PKDataStrong4::getNonce(){
return this->getNonce(NULL);
} /* End of getNonce() */
size_t PKDataStrong4::getNonceLength(){
return PK_STRONG_IPv4_NONCE_LEN;
} /* End of getNonceLength() */
bool PKDataStrong4::validateKnockData(u8 *key, size_t keylen){
u8 hmac[HMAC_SHA256_LEN];
u8 backup[PK_STRONG_IPv4_MAC_LEN];
memcpy(backup, this->h.mac,PK_STRONG_IPv4_MAC_LEN);
memset(this->h.mac, 0, PK_STRONG_IPv4_MAC_LEN);
HMAC_SHA256::hmac_sha256(key, keylen, (u8 *)&this->h , PK_STRONG_IPv4_DATA_LEN, hmac, HMAC_SHA256_LEN);
memcpy(this->h.mac, backup, PK_STRONG_IPv4_MAC_LEN);
if(memcmp(backup, hmac, PK_STRONG_IPv4_MAC_LEN)==0)
return true;
else
return false;
} /* End of validateKnockData() */
/** Prints knock data as hexadecimal bytes.*/
const char *PKDataStrong4::toString(){
static char buffer[256];
snprintf(buffer, 256, "%s;%d;", this->getAddress().toString(), this->getPort());
/** @todo finish this! */
return buffer;
} /* End of IP_Id_printString() */