From 7dc04d52f7e3608b42c0a3498b336826ee875112 Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 8 Feb 2019 14:53:58 -0500 Subject: [PATCH] offline keys added --- Makefile | 7 +++-- keyinfo.cpp | 1 + offlinekeys.cpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) create mode 100644 offlinekeys.cpp diff --git a/Makefile b/Makefile index 7b8b3ec..75ee3f4 100644 --- a/Makefile +++ b/Makefile @@ -44,7 +44,7 @@ OBJECTS = $(SOURCES:.cpp=.o) I2PD_LIB = libi2pd.a -all: keygen keyinfo famtool routerinfo regaddr regaddr_3ld vain i2pbase64 +all: keygen keyinfo famtool routerinfo regaddr regaddr_3ld vain i2pbase64 offlinekeys routerinfo: $(OBJECTS) $(CXX) -o routerinfo routerinfo.o $(LDFLAGS) $(LIBS) @@ -70,6 +70,9 @@ vain: $(OBJECTS) i2pbase64: $(OBJECTS) $(CXX) -o i2pbase64 i2pbase64.o $(LDFLAGS) $(LIBS) +offlinekeys: $(OBJECTS) + $(CXX) -o offlinekeys offlinekeys.o $(LDFLAGS) $(LIBS) + $(OBJECTS): libi2pd.a .SUFFIXES: @@ -91,7 +94,7 @@ clean-obj: rm -f $(OBJECTS) clean-bin: - rm -f keyinfo keygen famtool regaddr regaddr_3ld routerinfo i2pbase64 vain + rm -f keyinfo keygen famtool regaddr regaddr_3ld routerinfo i2pbase64 vain offlinekeys clean: clean-i2pd clean-obj clean-bin diff --git a/keyinfo.cpp b/keyinfo.cpp index 25d4d23..a2e236b 100644 --- a/keyinfo.cpp +++ b/keyinfo.cpp @@ -62,6 +62,7 @@ int main(int argc, char * argv[]) std::cout << "B32 Address: " << ident.ToBase32() << ".b32.i2p" << std::endl; std::cout << "Signature Type: " << SigTypeToName(dest->GetSigningKeyType()) << std::endl; std::cout << "Encryption Type: " << (int) dest->GetCryptoKeyType() << std::endl; + if (keys.IsOfflineSignature ()) std::cout << "Offline signature" << std::endl; } else { if(print_full) { std::cout << dest->ToBase64() << std::endl; diff --git a/offlinekeys.cpp b/offlinekeys.cpp new file mode 100644 index 0000000..dac8112 --- /dev/null +++ b/offlinekeys.cpp @@ -0,0 +1,70 @@ +#include +#include +#include +#include +#include "Crypto.h" +#include "Identity.h" +#include "Timestamp.h" +#include "common/key.hpp" + +int main (int argc, char * argv[]) +{ + if (argc < 3) + { + std::cout << "Usage: offlinekeys " << std::endl; + return -1; + } + i2p::crypto::InitCrypto (false); + + std::string fname(argv[2]); + i2p::data::PrivateKeys keys; + { + std::vector buff; + std::ifstream inf; + inf.open(fname); + if (!inf.is_open()) { + std::cout << "cannot open keys file " << fname << std::endl; + return 2; + } + inf.seekg(0, std::ios::end); + const std::size_t len = inf.tellg(); + inf.seekg(0, std::ios::beg); + buff.resize(len); + inf.read((char*)buff.data(), buff.size()); + if (!keys.FromBuffer(buff.data(), buff.size())) { + std::cout << "bad keys file format" << std::endl; + return 3; + } + } + + i2p::data::SigningKeyType type = i2p::data::SIGNING_KEY_TYPE_EDDSA_SHA512_ED25519; // EdDSA by default + if (argc > 3) + { + std::string str(argv[3]); + type = NameToSigType(str); + } + + int days = 365; // 1 year by default + if (argc > 4) + days = std::stoi (argv[4]); + uint32_t expires = i2p::util::GetSecondsSinceEpoch () + days*24*60*60; + + auto offlineKeys = keys.CreateOfflineKeys (type, expires); + std::ofstream f (argv[1], std::ofstream::binary | std::ofstream::out); + if (f) + { + size_t len = offlineKeys.GetFullLen (); + uint8_t * buf = new uint8_t[len]; + len = offlineKeys.ToBuffer (buf, len); + f.write ((char *)buf, len); + delete[] buf; + std::cout << "Offline keys for destination " << offlineKeys.GetPublic ()->GetIdentHash ().ToBase32 () << " created" << std::endl; + } + else + std::cout << "Can't create file " << argv[1] << std::endl; + + i2p::crypto::TerminateCrypto (); + + return 0; +} +