From c8c8c462b9e6b940e1545695b82631cc684930e6 Mon Sep 17 00:00:00 2001 From: wipedlifepotato Date: Thu, 21 Aug 2025 04:30:28 +0700 Subject: [PATCH] fix: pre init fixes, not tested --- famtool.cpp | 19 ++++++++++-------- i2pbase64.cpp | 53 +++++++++++++++++++++++++++++-------------------- i2pd | 2 +- regaddr.cpp | 8 ++++---- regaddr_3ld.cpp | 16 +++++++-------- verifyhost.cpp | 5 +++-- x25519.cpp | 10 +++++----- 7 files changed, 63 insertions(+), 50 deletions(-) diff --git a/famtool.cpp b/famtool.cpp index 0a40689..30d6d17 100644 --- a/famtool.cpp +++ b/famtool.cpp @@ -74,7 +74,7 @@ static std::shared_ptr LoadCertificate (const std::string& filename) bn2buf (y, signingKey + 32, 32); BN_free (x); BN_free (y); verifier = std::make_shared(); - verifier->SetPublicKey (signingKey); + verifier->SetPublicKey (signingKey); } } EC_KEY_free (ecKey); @@ -113,11 +113,11 @@ static bool CreateFamilySignature (const std::string& family, const IdentHash& i len += 32; signer.Sign (buf, len, signature); len = Base64EncodingBufferSize (64); - char * b64 = new char[len+1]; - len = ByteStreamToBase64 (signature, 64, b64, len); - b64[len] = 0; + //char * b64 = new char[len+1]; + auto b64 = ByteStreamToBase64 (signature, len); + //b64[len] = 0; sig = b64; - delete[] b64; + //delete[] b64; } else return false; @@ -383,9 +383,12 @@ int main(int argc, char * argv[]) memcpy(buf, fam.c_str(), len); memcpy(buf + len, (const uint8_t *) ident, 32); len += 32; - uint8_t sigbuf[64]; - Base64ToByteStream(sig.c_str(), sig.length(), sigbuf, 64); - if(!v->Verify(buf, len, sigbuf)) { + //uint8_t sigbuf[64]; + auto b64 = ByteStreamToBase64(reinterpret_cast(sig.c_str()), sig.length()); + + //Base64ToByteStream(sig.c_str(), sig.length(), sigbuf, 64); + if (!v->Verify(buf, len, + reinterpret_cast(b64.data()))) { std::cout << "invalid signature" << std::endl; return 1; } diff --git a/i2pbase64.cpp b/i2pbase64.cpp index 1734365..ad3c4ba 100644 --- a/i2pbase64.cpp +++ b/i2pbase64.cpp @@ -14,26 +14,35 @@ static int printHelp(const char * exe, int exitcode) return exitcode; } -template -static int operate(std::function f, int infile, int outfile) -{ - InCh inbuf[isz]; - OutCh outbuf[osz]; - ssize_t sz; - size_t outsz; - while((sz = read(infile, inbuf, sizeof(inbuf))) > 0) - { - outsz = f(inbuf, sz, outbuf, sizeof(outbuf)); - if(outsz && outsz <= sizeof(outbuf)) - { - write(outfile, outbuf, outsz); - } - else - { - return -1; - } - } - return errno; +int operate_b64_decode(int infile, int outfile) { + constexpr size_t BUFFSZ = 4096; + char inbuf[BUFFSZ*4]; + uint8_t outbuf[BUFFSZ*3]; + ssize_t sz; + while ((sz = read(infile, inbuf, sizeof(inbuf))) > 0) { + std::string_view chunk(inbuf, sz); + + size_t outsz = i2p::data::Base64ToByteStream(chunk, outbuf, sizeof(outbuf)); + if (outsz > 0) { + write(outfile, outbuf, outsz); + } else { + return -1; + } + } + return errno; +} + + +int operate_b64_encode(int infile, int outfile) { + constexpr size_t BUFFSZ = 4096; + uint8_t inbuf[BUFFSZ*3]; + //char outbuf[BUFFSZ*4]; + ssize_t sz; + while((sz = read(infile, inbuf, sizeof(inbuf))) > 0) { + std::string out = i2p::data::ByteStreamToBase64(inbuf, sz); + write(outfile, out.data(), out.size()); + } + return errno; } int main(int argc, char * argv[]) @@ -71,11 +80,11 @@ int main(int argc, char * argv[]) int retcode = 0; if(decode) { - retcode = operate(i2p::data::Base64ToByteStream, infile, 1); + retcode = operate_b64_decode(infile, 1); } else { - retcode = operate(&i2p::data::ByteStreamToBase64, infile, 1); + retcode = operate_b64_encode(infile, 1); } close(infile); return retcode; diff --git a/i2pd b/i2pd index dcd15cc..de14e81 160000 --- a/i2pd +++ b/i2pd @@ -1 +1 @@ -Subproject commit dcd15cc2449d6320de6351054e61ef2ee7ebee40 +Subproject commit de14e81f50e24b78eb3b190cd4f1ca8514bc4e64 diff --git a/regaddr.cpp b/regaddr.cpp index 6dbf1fd..84f14af 100644 --- a/regaddr.cpp +++ b/regaddr.cpp @@ -29,16 +29,16 @@ int main (int argc, char * argv[]) { auto signatureLen = keys.GetPublic ()->GetSignatureLen (); uint8_t * signature = new uint8_t[signatureLen]; - char * sig = new char[signatureLen*2]; + //char * sig = new char[signatureLen*2]; std::stringstream out; out << argv[2] << "="; // address out << keys.GetPublic ()->ToBase64 (); keys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature); - auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2); - sig[len] = 0; + auto sig = i2p::data::ByteStreamToBase64 (signature, signatureLen);//, sig, signatureLen*2); + //sig[len] = 0; out << "#!sig=" << sig; delete[] signature; - delete[] sig; + //delete[] sig; std::cout << out.str () << std::endl; } else diff --git a/regaddr_3ld.cpp b/regaddr_3ld.cpp index 4c94b09..918c8a3 100644 --- a/regaddr_3ld.cpp +++ b/regaddr_3ld.cpp @@ -66,16 +66,16 @@ int main (int argc, char * argv[]) if(keys.FromBuffer (buf, len)) { auto signatureLen = keys.GetPublic ()->GetSignatureLen (); uint8_t * signature = new uint8_t[signatureLen]; - char * sig = new char[signatureLen*2]; + //char * sig = new char[signatureLen*2]; out << "#date=" << std::time(nullptr); out << "#olddest=" << keys.GetPublic ()->ToBase64 (); out << "#oldname=" << argv[4]; keys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature); - auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2); - sig[len] = 0; + auto sig = i2p::data::ByteStreamToBase64 (signature, signatureLen);//, sig, signatureLen*2); + //sig[len] = 0; out << "#oldsig=" << sig; delete[] signature; - delete[] sig; + //delete[] sig; std::cout << out.str () << std::endl; } else std::cout << "Failed to load keyfile " << argv[1] << std::endl; @@ -108,13 +108,13 @@ int main (int argc, char * argv[]) if(keys.FromBuffer (buf, len)) { auto signatureLen = keys.GetPublic ()->GetSignatureLen (); uint8_t * signature = new uint8_t[signatureLen]; - char * sig = new char[signatureLen*2]; + //char * sig = new char[signatureLen*2]; keys.Sign ((uint8_t *)out.str ().c_str (), out.str ().length (), signature); - auto len = i2p::data::ByteStreamToBase64 (signature, signatureLen, sig, signatureLen*2); - sig[len] = 0; + auto sig = i2p::data::ByteStreamToBase64 (signature, signatureLen);//, sig, signatureLen*2); + //sig[len] = 0; out << "#sig=" << sig; delete[] signature; - delete[] sig; + //delete[] sig; std::cout << out.str () << std::endl; } else std::cout << "Failed to load keyfile " << argv[1] << std::endl; diff --git a/verifyhost.cpp b/verifyhost.cpp index 52c4cff..4e843b4 100644 --- a/verifyhost.cpp +++ b/verifyhost.cpp @@ -51,7 +51,8 @@ int main (int argc, char * argv[]) uint8_t * signature = new uint8_t[signatureLen]; // validate signature - i2p::data::Base64ToByteStream(sig.c_str (), sig.length(), signature, signatureLen); + // size_t Base64ToByteStream (std::string_view base64Str, uint8_t * OutBuffer, size_t len); + i2p::data::Base64ToByteStream(sig, signature, signatureLen); if (!Identity.Verify ((uint8_t *)hostNoSig.c_str (), hostNoSig.length (), signature)) { std::cout << "Invalid destination signature." << std::endl; @@ -85,7 +86,7 @@ int main (int argc, char * argv[]) std::string oldSig = oldSigCut.substr (0, pos); // validate signature - i2p::data::Base64ToByteStream(oldSig.c_str (), oldSig.length(), signature, signatureLen); + i2p::data::Base64ToByteStream(oldSig, signature, signatureLen); bool oldSignValid = OldIdentity.Verify ((uint8_t *)hostNoOldSig.c_str (), hostNoOldSig.length (), signature); if(!oldSignValid) diff --git a/x25519.cpp b/x25519.cpp index 4a93b1e..44ea748 100644 --- a/x25519.cpp +++ b/x25519.cpp @@ -57,17 +57,17 @@ int main(int argc, char * argv[]) BoxKeys newKeys = getKeyPair(); - const size_t len_out = 50; - char b64Public[len_out] = {0}; - char b64Private[len_out] = {0}; + //const size_t len_out = 50; + //char b64Public[len_out] = {0}; + //char b64Private[len_out] = {0}; - i2p::data::ByteStreamToBase64 (newKeys.PublicKey, len, b64Public, len_out); + auto b64Public = i2p::data::ByteStreamToBase64 (newKeys.PublicKey, len);//, b64Public, len_out); std::cout << "PublicKey: "; for (int i = 0; b64Public[i] != 0; ++i) std::cout << b64Public[i]; - i2p::data::ByteStreamToBase64 (newKeys.PrivateKey, len, b64Private, len_out); + auto b64Private = i2p::data::ByteStreamToBase64 (newKeys.PrivateKey, len);//, b64Private, len_out); std::cout << "\nPrivateKey: "; for (int i = 0; b64Private[i] != 0; ++i)