diff --git a/src/lib/utils/crypto/KeyCodeHelper.cpp b/src/lib/utils/crypto/KeyCodeHelper.cpp index b3a53bc2e..c15858478 100644 --- a/src/lib/utils/crypto/KeyCodeHelper.cpp +++ b/src/lib/utils/crypto/KeyCodeHelper.cpp @@ -22,7 +22,7 @@ bool decrypt_keycode(const bts::crypto::PublicKey& public_key, const bts::config if(public_key.decrypt(kss, out)) { bts::config::KeyCode kc; - google::protobuf::io::IstreamInputStream kc_stream(&kss); + google::protobuf::io::IstreamInputStream kc_stream(&out); google::protobuf::TextFormat::Parse(&kc_stream, &kc); if(dk) { diff --git a/src/lib/utils/crypto/PrivateKey.cpp b/src/lib/utils/crypto/PrivateKey.cpp index f9c1b06ab..16cc4c89e 100644 --- a/src/lib/utils/crypto/PrivateKey.cpp +++ b/src/lib/utils/crypto/PrivateKey.cpp @@ -164,7 +164,10 @@ bool encrypt_decrypt_function(const std::iostream& in_dat, std::iostream& out_da unsigned char *in, *out; long inlen; size_t outlen; int i = 0; iostreambio biostream(const_cast(&in_dat)); if(!encrypt) - i = openssl::PEM_read_bio(biostream.getBIO(), &name, &header, &in, &inlen); + { + auto bio = biostream.getBIO(); + i = openssl::PEM_read_bio(bio, &name, &header, &in, &inlen); + } else { std::iostream seeker(in_dat.rdbuf()); diff --git a/src/lib/utils/crypto/iostreambio.cpp b/src/lib/utils/crypto/iostreambio.cpp index 69943e0e7..d26bc9350 100644 --- a/src/lib/utils/crypto/iostreambio.cpp +++ b/src/lib/utils/crypto/iostreambio.cpp @@ -27,7 +27,7 @@ const int iostreambio::type = BIO_TYPE_SOURCE_SINK; static int bwrite(BIO *bio, const char *buf, int len); static int bread(BIO *bio, char *buf, int len); //static int bputs(BIO *bio, const char *); -//static int bgets(BIO *bio, char *, int); +static int bgets(BIO *bio, char *, int); static long ctrl(BIO *bio, int cmd, long num, void *ptr); static int create(BIO *bio); //static int destroy(BIO *bio); @@ -37,12 +37,13 @@ static int create(BIO *bio); BIO_METHOD * BIO_iostream_method() { static BIO_METHOD *iostream_method = NULL; - if (iostream_method) { + if (!iostream_method) { iostream_method = BIO_meth_new(iostreambio::type, iostreambio::name); BIO_meth_set_write(iostream_method, bwrite); BIO_meth_set_read(iostream_method, bread); BIO_meth_set_ctrl(iostream_method, ctrl); BIO_meth_set_create(iostream_method, create); + BIO_meth_set_gets(iostream_method, bgets); } return iostream_method; } @@ -172,37 +173,37 @@ static int bread(BIO *bio, char *buf, int len) return num_bytes; } -// static int bgets(BIO *bio, char *buf, int len) -// { -// iostream *stream = reinterpret_cast(BIO_get_data(bio)); -// #if OPENSSL_VERSION_NUMBER >= 0x10100000L -// int num_bytes = 0; -// #else -// streamsize num_bytes = bio->num; -// #endif -// try -// { -// /* make sure to read at least 1 byte and then read as much as we can */ -// //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; -// int i = 0; -// while(stream->good() && i < len) -// { -// buf[i] = stream->get(); -// if(buf[i] == '\n') -// { -// i++; -// break; -// } -// i++; -// } -// buf[i] = '\0'; -// num_bytes = i; -// } -// catch(ios_base::failure &) -// { -// } -// return num_bytes; -// } +static int bgets(BIO *bio, char *buf, int len) +{ + iostream *stream = reinterpret_cast(BIO_get_data(bio)); +#if OPENSSL_VERSION_NUMBER >= 0x10100000L + int num_bytes = 0; +#else + streamsize num_bytes = bio->num; +#endif + try + { + /* make sure to read at least 1 byte and then read as much as we can */ + //num_bytes = stream->read(buf, 1).readsome(buf+1, len-1) + 1; + int i = 0; + while(stream->good() && i < len) + { + buf[i] = stream->get(); + if(buf[i] == '\n') + { + i++; + break; + } + i++; + } + buf[i] = '\0'; + num_bytes = i; + } + catch(ios_base::failure &) + { + } + return num_bytes; +} static int bwrite(BIO *bio, const char *buf, int len) diff --git a/src/lib/utils/datetime.cpp b/src/lib/utils/datetime.cpp index c8d68f42d..4e9d52b3d 100755 --- a/src/lib/utils/datetime.cpp +++ b/src/lib/utils/datetime.cpp @@ -178,6 +178,14 @@ std::string iso_date_time_string (PTime const &t) return boost::posix_time::to_iso_extended_string(t); } +PTime parse_iso_date_time_string (std::string const &t) +{ + if(t.find('T') == t.npos) + return boost::posix_time::from_iso_extended_string(t + "T00:00:00"); + else + return boost::posix_time::from_iso_extended_string(t); +} + std::string simple_date_time_string (PTime const &t) { return boost::posix_time::to_simple_string(t); @@ -185,7 +193,10 @@ std::string simple_date_time_string (PTime const &t) PTime parse_simple_date_time_string (std::string const &t) { - return boost::posix_time::time_from_string(t); + if(t.find(' ') == t.npos) + return boost::posix_time::time_from_string(t + " 00:00:00"); + else + return boost::posix_time::time_from_string(t); } std::string FIX_date_time_string (PTime const &t) diff --git a/src/lib/utils/datetime.h b/src/lib/utils/datetime.h index dc8f7185b..b9ba82dca 100755 --- a/src/lib/utils/datetime.h +++ b/src/lib/utils/datetime.h @@ -61,6 +61,7 @@ BTS_EXPORT long long msecs_since (PTime const &t1); BTS_EXPORT std::string time_string (PTime const &t); BTS_EXPORT std::string date_string (PTime const &t); BTS_EXPORT std::string iso_date_time_string (PTime const &t); +BTS_EXPORT PTime parse_iso_date_time_string (std::string const &t); BTS_EXPORT std::string simple_date_time_string (PTime const &t); BTS_EXPORT PTime parse_simple_date_time_string (std::string const &t); BTS_EXPORT std::string FIX_date_time_string (PTime const &t); diff --git a/src/server/sparkd/cmds/KeyCode.cpp b/src/server/sparkd/cmds/KeyCode.cpp index a6ac1e71d..0104db3b5 100644 --- a/src/server/sparkd/cmds/KeyCode.cpp +++ b/src/server/sparkd/cmds/KeyCode.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,7 @@ std::string KeyCode::helpstr () { std::ostringstream oss; oss << "\n" - << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<1/23/2050>\n " + << "ENCODE -keyfile:[keyfile] -pass:password -users: -expiration:<2020-05-18>\n " << "DECODE -keyfile:[keyfile] -pass:password -source:[file] \n " << "KEYGEN -keyfile:[keyfile_prefix]\n "; @@ -49,8 +50,8 @@ std::string KeyCode::helpstr () std::string parse_date (std::string const &date) { - auto the_date = bts::datetime::parse_simple_date_time_string(date); - return bts::datetime::simple_date_time_string(the_date); + auto the_date = bts::datetime::parse_iso_date_time_string(date); + return bts::datetime::iso_date_time_string(the_date); } @@ -137,7 +138,7 @@ void write_keycode(const std::string& users, const std::string& expiration, bts: std::ostringstream out_stream; auto nusers = (uint32_t) std::atoi(users.c_str()); - auto expir = bts::datetime::parse_simple_date_time_string(expiration); + auto expir = bts::datetime::parse_iso_date_time_string(expiration); auto expir_ll = bts::datetime::ptime_to_ll(expir); auto now = bts::datetime::ll_now(); @@ -178,10 +179,16 @@ int encrypt(const std::string& keyfile, const std::string& pass, const std::stri ekc.set_keycode(encrypted.str()); std::string pb; google::protobuf::TextFormat::PrintToString(ekc, &pb); + std::cout << std::endl << std::endl; std::cout << "#nusers: " << users << std::endl; std::cout << "#expires: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.expiration())) << " " << std::to_string(kc.expiration()) << std::endl; std::cout << "#generation_time: " << datetime::simple_date_time_string(datetime::ll_to_ptime(kc.generation_time())) << " " << std::to_string(kc.generation_time()) << std::endl; - std::cout << pb; + std::cout << "EncryptedKeyCode {" << std::endl; + std::cout << " value {" << std::endl; + std::cout << " " << pb; + std::cout << " }" << std::endl; + std::cout << "}" << std::endl; + std::cout << std::endl << std::endl; return 0; } } @@ -208,10 +215,13 @@ int decode(const std::vector& args) //KeycodePayloadHelper helper; bts::config::EncryptedKeyCode ekc; std::fstream fs(source); - google::protobuf::io::IstreamInputStream pay_stream(&fs); - google::protobuf::TextFormat::Parse(&pay_stream, &ekc); + //google::protobuf::io::IstreamInputStream pay_stream(&fs); + //google::protobuf::TextFormat::Parse(&pay_stream, &ekc); + std::string str((std::istreambuf_iterator(fs)), std::istreambuf_iterator()); + config::internal::initialize_for_test(str); + ekc = config::get(); + std::cout << std::endl << std::endl; - bts::config::KeyCode kc; std::string string_out; bts::crypto::decrypt_keycode(*pub, ekc, &kc); google::protobuf::TextFormat::PrintToString(kc, &string_out);