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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
// Copyright (c) 2014-2016, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "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 COPYRIGHT HOLDER OR CONTRIBUTORS 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.
#include "gtest/gtest.h"
#include "net/http_auth.h"
#include <boost/algorithm/string/predicate.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/range/algorithm/find_if.hpp>
#include <boost/range/iterator_range_core.hpp>
#include <boost/spirit/include/karma_char.hpp>
#include <boost/spirit/include/karma_list.hpp>
#include <boost/spirit/include/karma_generate.hpp>
#include <boost/spirit/include/karma_right_alignment.hpp>
#include <boost/spirit/include/karma_sequence.hpp>
#include <boost/spirit/include/karma_string.hpp>
#include <boost/spirit/include/karma_uint.hpp>
#include <boost/spirit/include/qi_alternative.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_char_class.hpp>
#include <boost/spirit/include/qi_difference.hpp>
#include <boost/spirit/include/qi_eoi.hpp>
#include <boost/spirit/include/qi_list.hpp>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/qi_plus.hpp>
#include <boost/spirit/include/qi_sequence.hpp>
#include <boost/spirit/include/qi_string.hpp>
#include <cstdint>
#include <iterator>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "md5_l.h"
#include "string_tools.h"
namespace {
using fields = std::unordered_map<std::string, std::string>;
using auth_responses = std::vector<fields>;
std::string quoted(std::string str)
{
str.insert(str.begin(), '"');
str.push_back('"');
return str;
}
epee::net_utils::http::http_request_info make_request(const fields& args)
{
namespace karma = boost::spirit::karma;
std::string out{" DIGEST "};
karma::generate(
std::back_inserter(out),
(karma::string << " = " << karma::string) % " , ",
args);
epee::net_utils::http::http_request_info request{};
request.m_http_method_str = "NOP";
request.m_header_info.m_etc_fields.push_back(
std::make_pair(u8"authorization", std::move(out))
);
return request;
}
bool has_same_fields(const auth_responses& in)
{
const std::vector<std::string> check{u8"nonce", u8"qop", u8"realm", u8"stale"};
auto current = in.begin();
const auto end = in.end();
if (current == end)
return true;
++current;
for ( ; current != end; ++current )
{
for (const auto& field : check)
{
const std::string& expected = in[0].at(field);
const std::string& actual = current->at(field);
EXPECT_EQ(expected, actual);
if (expected != actual)
return false;
}
}
return true;
}
bool is_unauthorized(const epee::net_utils::http::http_response_info& response)
{
EXPECT_EQ(401, response.m_response_code);
EXPECT_STREQ(u8"Unauthorized", response.m_response_comment.c_str());
EXPECT_STREQ(u8"text/html", response.m_mime_tipe.c_str());
return response.m_response_code == 401 &&
response.m_response_comment == u8"Unauthorized" &&
response.m_mime_tipe == u8"text/html";
}
auth_responses parse_response(const epee::net_utils::http::http_response_info& response)
{
namespace qi = boost::spirit::qi;
auth_responses result{};
const auto end = response.m_additional_fields.end();
for (auto current = response.m_additional_fields.begin(); current != end; ++current)
{
current = std::find_if(current, end, [] (const std::pair<std::string, std::string>& field) {
return boost::iequals(u8"www-authenticate", field.first);
});
if (current == end)
return result;
std::unordered_map<std::string, std::string> fields{};
const bool rc = qi::parse(
current->second.begin(), current->second.end(),
qi::lit(u8"Digest ") >> ((
+qi::ascii::alpha >>
qi::lit('=') >> (
(qi::lit('"') >> +(qi::ascii::char_ - '"') >> qi::lit('"')) |
+(qi::ascii::graph - qi::ascii::char_(u8"()<>@,;:\\\"/[]?={}"))
)
) % ','
) >> qi::eoi,
fields
);
if (rc)
result.push_back(std::move(fields));
}
return result;
}
std::string md5_hex(const std::string& in)
{
md5::MD5_CTX ctx{};
md5::MD5Init(std::addressof(ctx));
md5::MD5Update(
std::addressof(ctx),
reinterpret_cast<const std::uint8_t*>(in.data()),
in.size()
);
std::array<std::uint8_t, 16> digest{{}};
md5::MD5Final(digest.data(), std::addressof(ctx));
return epee::string_tools::pod_to_hex(digest);
}
std::string get_a1(const epee::net_utils::http::http_auth::login& user, const auth_responses& responses)
{
const std::string& realm = responses.at(0).at(u8"realm");
return boost::join(
std::vector<std::string>{user.username, realm, user.password}, u8":"
);
}
std::string get_a1_sess(const epee::net_utils::http::http_auth::login& user, const std::string& cnonce, const auth_responses& responses)
{
const std::string& nonce = responses.at(0).at(u8"nonce");
return boost::join(
std::vector<std::string>{md5_hex(get_a1(user, responses)), nonce, cnonce}, u8":"
);
}
std::string get_a2(const std::string& uri)
{
return boost::join(std::vector<std::string>{"NOP", uri}, u8":");
}
std::string get_nc(std::uint32_t count)
{
namespace karma = boost::spirit::karma;
std::string out;
karma::generate(
std::back_inserter(out),
karma::right_align(8, '0')[karma::uint_generator<std::uint32_t, 16>{}],
count
);
return out;
}
}
TEST(HTTP_Auth, NotRequired)
{
epee::net_utils::http::http_auth auth{};
EXPECT_FALSE(auth.get_response(epee::net_utils::http::http_request_info{}));
}
TEST(HTTP_Auth, MissingAuth)
{
epee::net_utils::http::http_auth auth{{"foo", "bar"}};
EXPECT_TRUE(auth.get_response(epee::net_utils::http::http_request_info{}));
{
epee::net_utils::http::http_request_info request{};
request.m_header_info.m_etc_fields.push_back({"\xFF", "\xFF"});
EXPECT_TRUE(auth.get_response(request));
}
}
TEST(HTTP_Auth, BadSyntax)
{
epee::net_utils::http::http_auth auth{{"foo", "bar"}};
EXPECT_TRUE(auth.get_response(make_request({{u8"algorithm", "fo\xFF"}})));
EXPECT_TRUE(auth.get_response(make_request({{u8"cnonce", "\"000\xFF\""}})));
EXPECT_TRUE(auth.get_response(make_request({{u8"cnonce \xFF =", "\"000\xFF\""}})));
EXPECT_TRUE(auth.get_response(make_request({{u8" \xFF cnonce", "\"000\xFF\""}})));
}
TEST(HTTP_Auth, MD5)
{
epee::net_utils::http::http_auth::login user{"foo", "bar"};
epee::net_utils::http::http_auth auth{user};
const auto response = auth.get_response(make_request({}));
ASSERT_TRUE(response);
EXPECT_TRUE(is_unauthorized(*response));
const auto fields = parse_response(*response);
ASSERT_LE(2u, fields.size());
EXPECT_TRUE(has_same_fields(fields));
const std::string& nonce = fields[0].at(u8"nonce");
EXPECT_EQ(24, nonce.size());
const std::string uri{"/some_foo_thing"};
const std::string a1 = get_a1(user, fields);
const std::string a2 = get_a2(uri);
const std::string auth_code = md5_hex(
boost::join(std::vector<std::string>{md5_hex(a1), nonce, md5_hex(a2)}, u8":")
);
const auto request = make_request({
{u8"nonce", quoted(nonce)},
{u8"realm", quoted(fields[0].at(u8"realm"))},
{u8"response", quoted(auth_code)},
{u8"uri", quoted(uri)},
{u8"username", quoted(user.username)}
});
EXPECT_FALSE(auth.get_response(request));
const auto response2 = auth.get_response(request);
ASSERT_TRUE(response2);
EXPECT_TRUE(is_unauthorized(*response2));
const auto fields2 = parse_response(*response2);
ASSERT_LE(2u, fields2.size());
EXPECT_TRUE(has_same_fields(fields2));
EXPECT_NE(nonce, fields2[0].at(u8"nonce"));
EXPECT_STREQ(u8"true", fields2[0].at(u8"stale").c_str());
}
TEST(HTTP_Auth, MD5_sess)
{
constexpr const char cnonce[] = "not a good cnonce";
epee::net_utils::http::http_auth::login user{"foo", "bar"};
epee::net_utils::http::http_auth auth{user};
const auto response = auth.get_response(make_request({}));
ASSERT_TRUE(response);
EXPECT_TRUE(is_unauthorized(*response));
const auto fields = parse_response(*response);
ASSERT_LE(2u, fields.size());
EXPECT_TRUE(has_same_fields(fields));
const std::string& nonce = fields[0].at(u8"nonce");
EXPECT_EQ(24, nonce.size());
const std::string uri{"/some_foo_thing"};
const std::string a1 = get_a1_sess(user, cnonce, fields);
const std::string a2 = get_a2(uri);
const std::string auth_code = md5_hex(
boost::join(std::vector<std::string>{md5_hex(a1), nonce, md5_hex(a2)}, u8":")
);
const auto request = make_request({
{u8"algorithm", u8"md5-sess"},
{u8"cnonce", quoted(cnonce)},
{u8"nonce", quoted(nonce)},
{u8"realm", quoted(fields[0].at(u8"realm"))},
{u8"response", quoted(auth_code)},
{u8"uri", quoted(uri)},
{u8"username", quoted(user.username)}
});
EXPECT_FALSE(auth.get_response(request));
const auto response2 = auth.get_response(request);
ASSERT_TRUE(response2);
EXPECT_TRUE(is_unauthorized(*response2));
const auto fields2 = parse_response(*response2);
ASSERT_LE(2u, fields2.size());
EXPECT_TRUE(has_same_fields(fields2));
EXPECT_NE(nonce, fields2[0].at(u8"nonce"));
EXPECT_STREQ(u8"true", fields2[0].at(u8"stale").c_str());
}
TEST(HTTP_Auth, MD5_auth)
{
constexpr const char cnonce[] = "not a nonce";
constexpr const char qop[] = "auth";
epee::net_utils::http::http_auth::login user{"foo", "bar"};
epee::net_utils::http::http_auth auth{user};
const auto response = auth.get_response(make_request({}));
ASSERT_TRUE(response);
EXPECT_TRUE(is_unauthorized(*response));
const auto parsed = parse_response(*response);
ASSERT_LE(2u, parsed.size());
EXPECT_TRUE(has_same_fields(parsed));
const std::string& nonce = parsed[0].at(u8"nonce");
EXPECT_EQ(24, nonce.size());
const std::string uri{"/some_foo_thing"};
const std::string a1 = get_a1(user, parsed);
const std::string a2 = get_a2(uri);
std::string nc = get_nc(1);
const auto generate_auth = [&] {
return md5_hex(
boost::join(
std::vector<std::string>{md5_hex(a1), nonce, nc, cnonce, qop, md5_hex(a2)}, u8":"
)
);
};
fields args{
{u8"algorithm", quoted(u8"md5")},
{u8"cnonce", quoted(cnonce)},
{u8"nc", nc},
{u8"nonce", quoted(nonce)},
{u8"qop", quoted(qop)},
{u8"realm", quoted(parsed[0].at(u8"realm"))},
{u8"response", quoted(generate_auth())},
{u8"uri", quoted(uri)},
{u8"username", quoted(user.username)}
};
const auto request = make_request(args);
EXPECT_FALSE(auth.get_response(request));
for (unsigned i = 2; i < 20; ++i)
{
nc = get_nc(i);
args.at(u8"nc") = nc;
args.at(u8"response") = quoted(generate_auth());
EXPECT_FALSE(auth.get_response(make_request(args)));
}
const auto replay = auth.get_response(request);
ASSERT_TRUE(replay);
EXPECT_TRUE(is_unauthorized(*replay));
const auto parsed_replay = parse_response(*replay);
ASSERT_LE(2u, parsed_replay.size());
EXPECT_TRUE(has_same_fields(parsed_replay));
EXPECT_NE(nonce, parsed_replay[0].at(u8"nonce"));
EXPECT_STREQ(u8"true", parsed_replay[0].at(u8"stale").c_str());
}
TEST(HTTP_Auth, MD5_sess_auth)
{
constexpr const char cnonce[] = "not a nonce";
constexpr const char qop[] = "auth";
epee::net_utils::http::http_auth::login user{"foo", "bar"};
epee::net_utils::http::http_auth auth{user};
const auto response = auth.get_response(make_request({}));
ASSERT_TRUE(response);
EXPECT_TRUE(is_unauthorized(*response));
const auto parsed = parse_response(*response);
ASSERT_LE(2u, parsed.size());
EXPECT_TRUE(has_same_fields(parsed));
const std::string& nonce = parsed[0].at(u8"nonce");
EXPECT_EQ(24, nonce.size());
const std::string uri{"/some_foo_thing"};
const std::string a1 = get_a1_sess(user, cnonce, parsed);
const std::string a2 = get_a2(uri);
std::string nc = get_nc(1);
const auto generate_auth = [&] {
return md5_hex(
boost::join(
std::vector<std::string>{md5_hex(a1), nonce, nc, cnonce, qop, md5_hex(a2)}, u8":"
)
);
};
fields args{
{u8"algorithm", u8"md5-sess"},
{u8"cnonce", quoted(cnonce)},
{u8"nc", nc},
{u8"nonce", quoted(nonce)},
{u8"qop", qop},
{u8"realm", quoted(parsed[0].at(u8"realm"))},
{u8"response", quoted(generate_auth())},
{u8"uri", quoted(uri)},
{u8"username", quoted(user.username)}
};
const auto request = make_request(args);
EXPECT_FALSE(auth.get_response(request));
for (unsigned i = 2; i < 20; ++i)
{
nc = get_nc(i);
args.at(u8"nc") = nc;
args.at(u8"response") = quoted(generate_auth());
EXPECT_FALSE(auth.get_response(make_request(args)));
}
const auto replay = auth.get_response(request);
ASSERT_TRUE(replay);
EXPECT_TRUE(is_unauthorized(*replay));
const auto parsed_replay = parse_response(*replay);
ASSERT_LE(2u, parsed_replay.size());
EXPECT_TRUE(has_same_fields(parsed_replay));
EXPECT_NE(nonce, parsed_replay[0].at(u8"nonce"));
EXPECT_STREQ(u8"true", parsed_replay[0].at(u8"stale").c_str());
}
|