forked from cozybit/authsae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.c
210 lines (189 loc) · 5.69 KB
/
common.c
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
/*
* Copyright (c) Dan Harkins, 2008, 2009, 2010
* Copyright (c) cozybit Inc., 2011
*
* Copyright holder grants permission for redistribution and use in source
* and binary forms, with or without modification, provided that the
* following conditions are met:
* 1. Redistribution of source code must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in all source files.
* 2. Redistribution in binary form must retain the above copyright
* notice, this list of conditions, and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* 3. All advertising materials and documentation mentioning features
* or use of this software must display the following acknowledgement:
*
* "This product includes software written by
* Dan Harkins (dharkins at lounge dot org)"
*
* "DISCLAIMER OF LIABILITY
*
* THIS SOFTWARE IS PROVIDED BY DAN HARKINS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INDUSTRIAL LOUNGE BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE."
*
* This license and distribution terms cannot be changed. In other words,
* this code cannot simply be copied and put under a different distribution
* license (including the GNU public license).
*/
#include "common.h"
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#include "ieee802_11.h"
unsigned int sae_debug_mask;
/*
* parser_buffer()
* given an attribute/value pair in the form of attr=value
* return a pointer to the value
*/
int parse_buffer(char *buf, char **val) {
char *ptr;
*val = NULL;
if (buf[0] == '#') {
return 0;
}
ptr = buf;
while (*ptr != '\n') {
ptr++;
}
if (ptr == buf) {
return 0;
}
*ptr = '\0';
ptr = strstr(buf, "=");
if (ptr == NULL) {
return -1;
}
*ptr = '\0';
do {
ptr++;
} while (*ptr == ' ');
*val = ptr;
return 1;
}
#define TIME_FMT "[%s.%06ld] "
void sae_debug(int level, const char *fmt, ...) {
va_list argptr;
struct timeval tv;
struct tm cur_tm;
char buf[9];
if (sae_debug_mask & level) {
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &cur_tm);
strftime(buf, sizeof(buf), "%H:%M:%S", &cur_tm);
va_start(argptr, fmt);
printf(TIME_FMT, buf, tv.tv_usec);
vfprintf(stdout, fmt, argptr);
va_end(argptr);
}
}
void sae_hexdump(
int level,
const char *label,
const unsigned char *start,
int len) {
const unsigned char *pos;
int i;
char buf[9];
struct timeval tv;
struct tm cur_tm;
gettimeofday(&tv, NULL);
localtime_r(&tv.tv_sec, &cur_tm);
strftime(buf, sizeof(buf), "%H:%M:%S", &cur_tm);
if (sae_debug_mask & level) {
fprintf(stdout, TIME_FMT "----------\n", buf, tv.tv_usec);
fprintf(stdout, TIME_FMT "%s hexdump", buf, tv.tv_usec, label);
pos = start;
for (i = 0; i < len; i++) {
if (!(i % 16))
fprintf(stdout, "\n" TIME_FMT "%08x ", buf, tv.tv_usec, i);
fprintf(stdout, "%02x ", (unsigned char)*pos++);
}
fprintf(stdout, "\n" TIME_FMT "----------\n\n", buf, tv.tv_usec);
fflush(stdout);
}
return;
}
void parse_ies(unsigned char *start, int len, struct info_elems *elems) {
int left = len;
unsigned char *pos = start;
memset(elems, 0, sizeof(*elems));
while (left >= 2) {
unsigned char id, elen;
id = *pos++;
elen = *pos++;
left -= 2;
// fprintf(stderr, "parse_ies id=%d elen=%d\n", id, elen);
if (elen > left)
break;
switch (id) {
case IEEE80211_EID_SUPPORTED_RATES:
elems->sup_rates = pos;
elems->sup_rates_len = elen;
break;
case IEEE80211_EID_EXTENDED_SUP_RATES:
elems->ext_rates = pos;
elems->ext_rates_len = elen;
break;
case IEEE80211_EID_RSN:
elems->rsn = pos;
elems->rsn_len = elen;
break;
case IEEE80211_EID_HT_CAPABILITY:
elems->ht_cap = pos;
elems->ht_cap_len = elen;
break;
case IEEE80211_EID_HT_OPERATION:
elems->ht_info = pos;
elems->ht_info_len = elen;
break;
case IEEE80211_EID_VHT_CAPABILITY:
elems->vht_cap = pos;
elems->vht_cap_len = elen;
break;
case IEEE80211_EID_VHT_OPERATION:
elems->vht_info = pos;
elems->vht_info_len = elen;
break;
case IEEE80211_EID_MESH_ID:
elems->mesh_id = pos;
elems->mesh_id_len = elen;
break;
case IEEE80211_EID_MESH_PEERING:
elems->mesh_peering = pos;
elems->mesh_peering_len = elen;
break;
case IEEE80211_EID_MESH_CONFIG:
elems->mesh_config = pos;
elems->mesh_config_len = elen;
break;
case IEEE80211_EID_AMPE:
elems->ampe = (struct ampe_ie *)pos;
elems->ampe_len = elen;
break;
case IEEE80211_EID_MIC:
elems->mic = pos;
elems->mic_len = elen;
/* After the MIC there IEs there's the AMPE encrypted IE. Stop here */
return;
default:
break;
}
left -= elen;
pos += elen;
}
}