i2pd/Reseed.cpp

418 lines
12 KiB
C++
Raw Normal View History

2014-12-12 20:45:39 +00:00
#include <string.h>
#include <fstream>
2014-12-11 20:41:04 +00:00
#include <sstream>
#include <boost/regex.hpp>
2014-02-02 19:37:43 +00:00
#include <boost/filesystem.hpp>
2014-12-12 02:14:04 +00:00
#include <cryptopp/osrng.h>
2014-12-12 20:45:39 +00:00
#include <cryptopp/asn.h>
#include <cryptopp/base64.h>
2014-12-10 17:07:25 +00:00
#include <cryptopp/zinflate.h>
#include "I2PEndian.h"
#include "Reseed.h"
#include "Log.h"
2014-12-09 21:44:58 +00:00
#include "Identity.h"
2014-12-11 20:41:04 +00:00
#include "NetDb.h"
#include "util.h"
namespace i2p
{
namespace data
{
2014-01-31 12:32:34 +00:00
static std::vector<std::string> httpReseedHostList = {
"http://193.150.121.66/netDb/",
"http://netdb.i2p2.no/",
"http://reseed.i2p-projekt.de/",
"http://cowpuncher.drollette.com/netdb/",
"http://i2p.mooo.com/netDb/",
"http://reseed.info/",
"http://uk.reseed.i2p2.no/",
"http://us.reseed.i2p2.no/",
"http://jp.reseed.i2p2.no/",
2014-01-31 12:32:34 +00:00
"http://i2p-netdb.innovatio.no/",
"http://ieb9oopo.mooo.com"
};
//TODO: Remember to add custom port support. Not all serves on 443
static std::vector<std::string> httpsReseedHostList = {
"https://193.150.121.66/netDb/",
"https://netdb.i2p2.no/",
"https://reseed.i2p-projekt.de/",
"https://cowpuncher.drollette.com/netdb/",
"https://i2p.mooo.com/netDb/",
"https://reseed.info/",
"https://i2p-netdb.innovatio.no/",
"https://ieb9oopo.mooo.com/",
"https://ssl.webpack.de/ivae2he9.sg4.e-plaza.de/" // Only HTTPS and SU3 (v2) support
};
2014-01-31 12:32:34 +00:00
2014-01-31 06:43:56 +00:00
//TODO: Implement v2 reseeding. Lightweight zip library is needed.
//TODO: Implement SU3, utils.
Reseeder::Reseeder()
{
}
Reseeder::~Reseeder()
{
}
bool Reseeder::reseedNow()
{
try
{
// Seems like the best place to try to intercept with SSL
/*ssl_server = true;
try {
// SSL
}
catch (std::exception& e)
{
LogPrint("Exception in SSL: ", e.what());
}*/
std::string reseedHost = httpReseedHostList[(rand() % httpReseedHostList.size())];
LogPrint("Reseeding from ", reseedHost);
std::string content = i2p::util::http::httpRequest(reseedHost);
if (content == "")
{
LogPrint("Reseed failed");
return false;
}
2014-01-31 06:43:56 +00:00
boost::regex e("<\\s*A\\s+[^>]*href\\s*=\\s*\"([^\"]*)\"", boost::regex::normal | boost::regbase::icase);
boost::sregex_token_iterator i(content.begin(), content.end(), e, 1);
boost::sregex_token_iterator j;
2014-01-31 06:43:56 +00:00
//TODO: Ugly code, try to clean up.
//TODO: Try to reduce N number of variables
std::string name;
std::string routerInfo;
std::string tmpUrl;
std::string filename;
std::string ignoreFileSuffix = ".su3";
2014-02-02 19:37:43 +00:00
boost::filesystem::path root = i2p::util::filesystem::GetDataDir();
while (i != j)
{
name = *i++;
if (name.find(ignoreFileSuffix)!=std::string::npos)
continue;
LogPrint("Downloading ", name);
tmpUrl = reseedHost;
tmpUrl.append(name);
routerInfo = i2p::util::http::httpRequest(tmpUrl);
2014-02-02 19:37:43 +00:00
if (routerInfo.size()==0)
continue;
filename = root.string();
#ifndef _WIN32
filename += "/netDb/r";
#else
filename += "\\netDb\\r";
#endif
2014-01-31 06:43:56 +00:00
filename += name.at(11); // first char in id
2014-02-02 19:37:43 +00:00
#ifndef _WIN32
filename.append("/");
2014-02-02 19:37:43 +00:00
#else
filename.append("\\");
#endif
filename.append(name.c_str());
std::ofstream outfile (filename, std::ios::binary);
outfile << routerInfo;
outfile.close();
}
return true;
}
catch (std::exception& ex)
{
//TODO: error reporting
return false;
}
return false;
}
2014-12-11 20:41:04 +00:00
int Reseeder::ReseedNowSU3 ()
{
2014-12-12 02:14:04 +00:00
CryptoPP::AutoSeededRandomPool rnd;
auto ind = rnd.GenerateWord32 (0, httpReseedHostList.size() - 1);
std::string reseedHost = httpReseedHostList[ind];
2014-12-11 20:41:04 +00:00
return ReseedFromSU3 (reseedHost);
}
int Reseeder::ReseedFromSU3 (const std::string& host)
{
std::string url = host + "i2pseeds.su3";
LogPrint (eLogInfo, "Dowloading SU3 from ", host);
std::string su3 = i2p::util::http::httpRequest (url);
if (su3.length () > 0)
{
std::stringstream s(su3);
return ProcessSU3Stream (s);
}
else
{
LogPrint (eLogWarning, "SU3 download failed");
return 0;
}
}
2014-12-09 21:44:58 +00:00
2014-12-12 15:34:50 +00:00
int Reseeder::ProcessSU3File (const char * filename)
2014-12-11 20:41:04 +00:00
{
std::ifstream s(filename, std::ifstream::binary);
if (s.is_open ())
return ProcessSU3Stream (s);
else
{
LogPrint (eLogError, "Can't open file ", filename);
return 0;
}
}
2014-12-09 21:44:58 +00:00
const char SU3_MAGIC_NUMBER[]="I2Psu3";
2014-12-12 15:34:50 +00:00
const uint32_t ZIP_HEADER_SIGNATURE = 0x04034B50;
const uint16_t ZIP_BIT_FLAG_DATA_DESCRIPTOR = 0x0008;
int Reseeder::ProcessSU3Stream (std::istream& s)
2014-11-20 19:29:22 +00:00
{
2014-12-11 20:41:04 +00:00
char magicNumber[7];
s.read (magicNumber, 7); // magic number and zero byte 6
if (strcmp (magicNumber, SU3_MAGIC_NUMBER))
{
LogPrint (eLogError, "Unexpected SU3 magic number");
return 0;
}
s.seekg (1, std::ios::cur); // su3 file format version
SigningKeyType signatureType;
s.read ((char *)&signatureType, 2); // signature type
signatureType = be16toh (signatureType);
uint16_t signatureLength;
s.read ((char *)&signatureLength, 2); // signature length
signatureLength = be16toh (signatureLength);
s.seekg (1, std::ios::cur); // unused
uint8_t versionLength;
s.read ((char *)&versionLength, 1); // version length
s.seekg (1, std::ios::cur); // unused
uint8_t signerIDLength;
s.read ((char *)&signerIDLength, 1); // signer ID length
uint64_t contentLength;
s.read ((char *)&contentLength, 8); // content length
contentLength = be64toh (contentLength);
s.seekg (1, std::ios::cur); // unused
uint8_t fileType;
s.read ((char *)&fileType, 1); // file type
if (fileType != 0x00) // zip file
{
LogPrint (eLogError, "Can't handle file type ", (int)fileType);
return 0;
}
s.seekg (1, std::ios::cur); // unused
uint8_t contentType;
s.read ((char *)&contentType, 1); // content type
if (contentType != 0x03) // reseed data
{
LogPrint (eLogError, "Unexpected content type ", (int)contentType);
return 0;
}
s.seekg (12, std::ios::cur); // unused
s.seekg (versionLength, std::ios::cur); // skip version
s.seekg (signerIDLength, std::ios::cur); // skip signer ID
// handle content
int numFiles = 0;
size_t contentPos = s.tellg ();
while (!s.eof ())
2014-11-20 19:29:22 +00:00
{
2014-12-11 20:41:04 +00:00
uint32_t signature;
s.read ((char *)&signature, 4);
2014-12-12 15:34:50 +00:00
signature = le32toh (signature);
if (signature == ZIP_HEADER_SIGNATURE)
2014-12-09 21:44:58 +00:00
{
2014-12-11 20:41:04 +00:00
// next local file
2014-12-12 15:34:50 +00:00
s.seekg (2, std::ios::cur); // version
uint16_t bitFlag;
s.read ((char *)&bitFlag, 2);
bitFlag = le16toh (bitFlag);
2014-12-12 01:44:08 +00:00
uint16_t compressionMethod;
s.read ((char *)&compressionMethod, 2);
compressionMethod = le16toh (compressionMethod);
s.seekg (8, std::ios::cur); // skip fields we don't care about
2014-12-11 20:41:04 +00:00
uint32_t compressedSize, uncompressedSize;
s.read ((char *)&compressedSize, 4);
compressedSize = le32toh (compressedSize);
s.read ((char *)&uncompressedSize, 4);
uncompressedSize = le32toh (uncompressedSize);
uint16_t fileNameLength, extraFieldLength;
s.read ((char *)&fileNameLength, 2);
2014-12-12 01:44:08 +00:00
fileNameLength = le16toh (fileNameLength);
2014-12-11 20:41:04 +00:00
s.read ((char *)&extraFieldLength, 2);
2014-12-12 01:44:08 +00:00
extraFieldLength = le16toh (extraFieldLength);
2014-12-11 20:41:04 +00:00
char localFileName[255];
s.read (localFileName, fileNameLength);
localFileName[fileNameLength] = 0;
s.seekg (extraFieldLength, std::ios::cur);
2014-12-12 18:36:02 +00:00
// take care about data desriptor if presented
if (bitFlag & ZIP_BIT_FLAG_DATA_DESCRIPTOR)
{
size_t pos = s.tellg ();
if (!FindZipDataDescriptor (s))
{
LogPrint (eLogError, "SU3 archive data descriptor not found");
return numFiles;
}
s.seekg (4, std::ios::cur); // skip CRC-32
s.read ((char *)&compressedSize, 4);
compressedSize = le32toh (compressedSize) + 4; // ??? we must consider signature as part of compressed data
s.read ((char *)&uncompressedSize, 4);
uncompressedSize = le32toh (uncompressedSize);
// now we know compressed and uncompressed size
s.seekg (pos, std::ios::beg); // back to compressed data
}
2014-12-11 20:41:04 +00:00
LogPrint (eLogDebug, "Proccessing file ", localFileName, " ", compressedSize, " bytes");
2014-12-12 03:31:39 +00:00
if (!compressedSize)
{
LogPrint (eLogWarning, "Unexpected size 0. Skipped");
continue;
}
2014-12-11 20:41:04 +00:00
uint8_t * compressed = new uint8_t[compressedSize];
s.read ((char *)compressed, compressedSize);
2014-12-12 01:44:08 +00:00
if (compressionMethod) // we assume Deflate
{
CryptoPP::Inflator decompressor;
decompressor.Put (compressed, compressedSize);
2014-12-12 18:36:02 +00:00
decompressor.MessageEnd();
if (decompressor.MaxRetrievable () <= uncompressedSize)
2014-12-12 01:44:08 +00:00
{
uint8_t * uncompressed = new uint8_t[uncompressedSize];
2014-12-12 18:36:02 +00:00
decompressor.Get (uncompressed, uncompressedSize);
i2p::data::netdb.AddRouterInfo (uncompressed, uncompressedSize);
2014-12-12 01:44:08 +00:00
numFiles++;
delete[] uncompressed;
}
else
LogPrint (eLogError, "Actual uncompressed size ", decompressor.MaxRetrievable (), " exceed ", uncompressedSize, " from header");
}
else // no compression
2014-11-20 19:29:22 +00:00
{
2014-12-12 01:44:08 +00:00
i2p::data::netdb.AddRouterInfo (compressed, compressedSize);
2014-12-11 20:41:04 +00:00
numFiles++;
2014-12-12 01:44:08 +00:00
}
delete[] compressed;
2014-12-12 18:36:02 +00:00
if (bitFlag & ZIP_BIT_FLAG_DATA_DESCRIPTOR)
s.seekg (12, std::ios::cur); // skip data descriptor section if presented (12 = 16 - 4)
2014-11-20 19:29:22 +00:00
}
2014-12-11 20:41:04 +00:00
else
break; // no more files
size_t end = s.tellg ();
if (end - contentPos >= contentLength)
break; // we are beyond contentLength
2014-11-20 19:29:22 +00:00
}
2014-12-11 20:41:04 +00:00
return numFiles;
2014-11-20 19:29:22 +00:00
}
2014-12-12 18:36:02 +00:00
const uint8_t ZIP_DATA_DESCRIPTOR_SIGNATURE[] = { 0x50, 0x4B, 0x07, 0x08 };
bool Reseeder::FindZipDataDescriptor (std::istream& s)
{
size_t nextInd = 0;
while (!s.eof ())
{
uint8_t nextByte;
s.read ((char *)&nextByte, 1);
if (nextByte == ZIP_DATA_DESCRIPTOR_SIGNATURE[nextInd])
{
nextInd++;
if (nextInd >= sizeof (ZIP_DATA_DESCRIPTOR_SIGNATURE))
return true;
}
else
nextInd = 0;
}
return s;
}
2014-12-12 20:45:39 +00:00
const char CERTIFICATE_HEADER[] = "-----BEGIN CERTIFICATE-----";
const char CERTIFICATE_FOOTER[] = "-----END CERTIFICATE-----";
void Reseeder::LoadCertificate (const std::string& filename)
{
std::ifstream s(filename, std::ifstream::binary);
if (s.is_open ())
{
2014-12-12 21:17:53 +00:00
s.seekg (0, std::ios::end);
size_t len = s.tellg ();
s.seekg (0, std::ios::beg);
char buf[2048];
s.read (buf, len);
std::string cert (buf, len);
2014-12-12 20:45:39 +00:00
// assume file in pem format
auto pos1 = cert.find (CERTIFICATE_HEADER);
auto pos2 = cert.find (CERTIFICATE_FOOTER);
if (pos1 == std::string::npos || pos2 == std::string::npos)
{
LogPrint (eLogError, "Malformed certificate file");
return;
}
pos1 += strlen (CERTIFICATE_HEADER);
pos2 -= pos1;
std::string base64 = cert.substr (pos1, pos2);
CryptoPP::ByteQueue queue;
CryptoPP::Base64Decoder decoder; // regular base64 rather than I2P
decoder.Attach (new CryptoPP::Redirector (queue));
decoder.Put ((const uint8_t *)base64.data(), base64.length());
decoder.MessageEnd ();
// extract X.509
CryptoPP::BERSequenceDecoder x509Cert (queue);
CryptoPP::BERSequenceDecoder tbsCert (x509Cert);
// version
uint32_t ver;
2014-12-13 13:32:41 +00:00
CryptoPP::BERGeneralDecoder context (tbsCert, CryptoPP::CONTEXT_SPECIFIC | CryptoPP::CONSTRUCTED);
2014-12-12 21:17:53 +00:00
CryptoPP::BERDecodeUnsigned<uint32_t>(context, ver, CryptoPP::INTEGER);
2014-12-12 20:45:39 +00:00
// serial
CryptoPP::Integer serial;
serial.BERDecode(tbsCert);
// signature
CryptoPP::BERSequenceDecoder signature (tbsCert);
signature.SkipAll();
2014-12-13 13:32:41 +00:00
2014-12-12 20:45:39 +00:00
// issuer
CryptoPP::BERSequenceDecoder issuer (tbsCert);
2014-12-13 13:32:41 +00:00
{
CryptoPP::BERSetDecoder c (issuer); c.SkipAll();
CryptoPP::BERSetDecoder st (issuer); st.SkipAll();
CryptoPP::BERSetDecoder l (issuer); l.SkipAll();
CryptoPP::BERSetDecoder o (issuer); o.SkipAll();
CryptoPP::BERSetDecoder ou (issuer); ou.SkipAll();
CryptoPP::BERSetDecoder cn (issuer);
{
CryptoPP::BERSequenceDecoder attributes (cn);
{
CryptoPP::BERGeneralDecoder ident(attributes, CryptoPP::OBJECT_IDENTIFIER);
ident.SkipAll ();
std::string name;
CryptoPP::BERDecodeTextString (attributes, name, CryptoPP::UTF8_STRING);
LogPrint (eLogInfo, "Issuer name: ", name);
}
}
}
2014-12-12 20:45:39 +00:00
issuer.SkipAll();
// validity
CryptoPP::BERSequenceDecoder validity (tbsCert);
validity.SkipAll();
// subject
CryptoPP::BERSequenceDecoder subject (tbsCert);
subject.SkipAll();
// public key
CryptoPP::BERSequenceDecoder publicKey (tbsCert);
publicKey.SkipAll();
tbsCert.SkipAll();
x509Cert.SkipAll();
}
else
LogPrint (eLogError, "Can't open certificate file ", filename);
}
}
}