premiere-libtorrent/test/test_pe_crypto.cpp

247 lines
7.4 KiB
C++
Raw Normal View History

2007-06-06 02:41:20 +02:00
/*
Copyright (c) 2007, Un Shyam
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* 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.
* Neither the name of the author 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 OWNER 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 <algorithm>
#include <iostream>
#include "libtorrent/hasher.hpp"
#include "libtorrent/pe_crypto.hpp"
#include "libtorrent/session.hpp"
2013-09-01 19:39:40 +02:00
#include "libtorrent/rsa.hpp"
2007-06-06 02:41:20 +02:00
#include "setup_transfer.hpp"
#include "test.hpp"
#ifndef TORRENT_DISABLE_ENCRYPTION
char const* pe_policy(libtorrent::pe_settings::enc_policy policy)
2007-06-06 02:41:20 +02:00
{
using namespace libtorrent;
if (policy == pe_settings::disabled) return "disabled";
else if (policy == pe_settings::enabled) return "enabled";
else if (policy == pe_settings::forced) return "forced";
return "unknown";
2007-06-06 02:41:20 +02:00
}
void display_pe_settings(libtorrent::pe_settings s)
{
using namespace libtorrent;
fprintf(stderr, "out_enc_policy - %s\tin_enc_policy - %s\n"
, pe_policy(s.out_enc_policy), pe_policy(s.in_enc_policy));
2007-06-06 02:41:20 +02:00
fprintf(stderr, "enc_level - %s\t\tprefer_rc4 - %s\n"
, s.allowed_enc_level == pe_settings::plaintext ? "plaintext"
: s.allowed_enc_level == pe_settings::rc4 ? "rc4"
: s.allowed_enc_level == pe_settings::both ? "both" : "unknown"
, s.prefer_rc4 ? "true": "false");
2007-06-06 02:41:20 +02:00
}
void test_transfer(libtorrent::pe_settings::enc_policy policy,
libtorrent::pe_settings::enc_level level = libtorrent::pe_settings::both,
2013-07-29 10:20:16 +02:00
bool pref_rc4 = false)
2007-06-06 02:41:20 +02:00
{
using namespace libtorrent;
// these are declared before the session objects
// so that they are destructed last. This enables
// the sessions to destruct in parallel
session_proxy p1;
session_proxy p2;
session ses1(fingerprint("LT", 0, 1, 0, 0), std::make_pair(48800, 49000), "0.0.0.0", 0);
session ses2(fingerprint("LT", 0, 1, 0, 0), std::make_pair(49800, 50000), "0.0.0.0", 0);
2007-06-06 02:41:20 +02:00
pe_settings s;
s.out_enc_policy = libtorrent::pe_settings::enabled;
s.in_enc_policy = libtorrent::pe_settings::enabled;
s.allowed_enc_level = pe_settings::both;
ses2.set_pe_settings(s);
s.out_enc_policy = policy;
s.in_enc_policy = policy;
s.allowed_enc_level = level;
s.prefer_rc4 = pref_rc4;
ses1.set_pe_settings(s);
s = ses1.get_pe_settings();
fprintf(stderr, " Session1 \n");
display_pe_settings(s);
s = ses2.get_pe_settings();
fprintf(stderr, " Session2 \n");
display_pe_settings(s);
2007-06-06 02:41:20 +02:00
torrent_handle tor1;
torrent_handle tor2;
2007-06-10 22:46:09 +02:00
using boost::tuples::ignore;
boost::tie(tor1, tor2, ignore) = setup_transfer(&ses1, &ses2, 0, true, false, true
2013-07-29 10:20:16 +02:00
, "_pe", 16 * 1024, 0, false, 0, true);
2007-06-06 02:41:20 +02:00
fprintf(stderr, "waiting for transfer to complete\n");
2007-06-06 02:41:20 +02:00
for (int i = 0; i < 50; ++i)
{
torrent_status s = tor2.status();
print_alerts(ses1, "ses1");
print_alerts(ses2, "ses2");
2007-06-06 02:41:20 +02:00
if (s.is_seeding) break;
test_sleep(1000);
2007-06-06 02:41:20 +02:00
}
TEST_CHECK(tor2.status().is_seeding);
if (tor2.status().is_seeding) fprintf(stderr, "done\n");
ses1.remove_torrent(tor1);
ses2.remove_torrent(tor2);
// this allows shutting down the sessions in parallel
p1 = ses1.abort();
p2 = ses2.abort();
error_code ec;
2012-03-17 20:06:11 +01:00
remove_all("tmp1_pe", ec);
remove_all("tmp2_pe", ec);
remove_all("tmp3_pe", ec);
2007-06-06 02:41:20 +02:00
}
void test_enc_handler(libtorrent::encryption_handler* a, libtorrent::encryption_handler* b)
{
int repcount = 128;
for (int rep = 0; rep < repcount; ++rep)
{
std::size_t buf_len = rand() % (512 * 1024);
char* buf = new char[buf_len];
char* cmp_buf = new char[buf_len];
std::generate(buf, buf + buf_len, &std::rand);
std::memcpy(cmp_buf, buf, buf_len);
a->encrypt(buf, buf_len);
TEST_CHECK(!std::equal(buf, buf + buf_len, cmp_buf));
b->decrypt(buf, buf_len);
TEST_CHECK(std::equal(buf, buf + buf_len, cmp_buf));
b->encrypt(buf, buf_len);
TEST_CHECK(!std::equal(buf, buf + buf_len, cmp_buf));
a->decrypt(buf, buf_len);
TEST_CHECK(std::equal(buf, buf + buf_len, cmp_buf));
delete[] buf;
delete[] cmp_buf;
}
}
2007-06-06 02:41:20 +02:00
2013-09-01 08:52:50 +02:00
#endif
2007-06-06 02:41:20 +02:00
int test_main()
{
using namespace libtorrent;
2013-09-01 08:52:50 +02:00
#ifndef TORRENT_DISABLE_ENCRYPTION
2009-04-10 10:40:13 +02:00
int repcount = 128;
2007-06-06 02:41:20 +02:00
for (int rep = 0; rep < repcount; ++rep)
{
2008-06-29 20:55:23 +02:00
dh_key_exchange DH1, DH2;
2007-06-06 02:41:20 +02:00
DH1.compute_secret(DH2.get_local_key());
DH2.compute_secret(DH1.get_local_key());
TEST_CHECK(std::equal(DH1.get_secret(), DH1.get_secret() + 96, DH2.get_secret()));
}
2008-06-29 20:55:23 +02:00
dh_key_exchange DH1, DH2;
2007-06-06 02:41:20 +02:00
DH1.compute_secret(DH2.get_local_key());
DH2.compute_secret(DH1.get_local_key());
TEST_CHECK(std::equal(DH1.get_secret(), DH1.get_secret() + 96, DH2.get_secret()));
sha1_hash test1_key = hasher("test1_key",8).final();
sha1_hash test2_key = hasher("test2_key",8).final();
fprintf(stderr, "testing RC4 handler\n");
rc4_handler rc41;
rc41.set_incoming_key(&test2_key[0], 20);
rc41.set_outgoing_key(&test1_key[0], 20);
rc4_handler rc42;
rc42.set_incoming_key(&test1_key[0], 20);
rc42.set_outgoing_key(&test2_key[0], 20);
test_enc_handler(&rc41, &rc42);
2007-06-06 02:41:20 +02:00
test_transfer(pe_settings::disabled);
test_transfer(pe_settings::forced, pe_settings::plaintext);
test_transfer(pe_settings::forced, pe_settings::rc4);
test_transfer(pe_settings::forced, pe_settings::both, false);
test_transfer(pe_settings::forced, pe_settings::both, true);
test_transfer(pe_settings::enabled, pe_settings::plaintext);
test_transfer(pe_settings::enabled, pe_settings::rc4);
test_transfer(pe_settings::enabled, pe_settings::both, false);
test_transfer(pe_settings::enabled, pe_settings::both, true);
2013-09-01 08:52:50 +02:00
#else
fprintf(stderr, "PE test not run because it's disabled\n");
#endif
2007-06-06 02:41:20 +02:00
2013-09-01 08:52:50 +02:00
#if defined TORRENT_USE_OPENSSL
// test sign_rsa and verify_rsa
char private_key[1192];
int private_len = sizeof(private_key);
char public_key[268];
int public_len = sizeof(public_key);
2007-06-06 02:41:20 +02:00
2013-09-01 19:39:40 +02:00
int ret = generate_rsa_keys(public_key, &public_len, private_key, &private_len, 2048);
2013-09-01 08:52:50 +02:00
fprintf(stderr, "keysizes: pub: %d priv: %d\n", public_len, private_len);
2013-09-01 08:52:50 +02:00
TEST_CHECK(ret);
char test_message[1024];
std::generate(test_message, test_message + 1024, &std::rand);
2013-09-01 08:52:50 +02:00
char signature[256];
int sig_len = sign_rsa(hasher(test_message, sizeof(test_message)).final()
, private_key, private_len, signature, sizeof(signature));
TEST_CHECK(sig_len == 256);
ret = verify_rsa(hasher(test_message, sizeof(test_message)).final()
, public_key, public_len, signature, sig_len);
TEST_CHECK(ret == 1);
#endif
2013-09-01 08:52:50 +02:00
return 0;
}