diff --git a/include/anope.h b/include/anope.h index 57fbfce69..865727260 100644 --- a/include/anope.h +++ b/include/anope.h @@ -463,18 +463,6 @@ namespace Anope extern CoreExport void Unhex(const string &src, string &dest); extern CoreExport void Unhex(const string &src, char *dest, size_t sz); - /** Base 64 encode a string - * @param src The string to encode - * @param target Where the encoded string is placed - */ - extern CoreExport void B64Encode(const string &src, string &target); - - /** Base 64 decode a string - * @param src The base64 encoded string - * @param target The plain text result - */ - extern CoreExport void B64Decode(const string &src, string &target); - /** Encrypts what is in 'src' to 'dest' * @param src The source string to encrypt * @param dest The destination where the encrypted string is placed diff --git a/include/modules/rpc.h b/include/modules/rpc.h index e4fb9fb59..a026876f1 100644 --- a/include/modules/rpc.h +++ b/include/modules/rpc.h @@ -228,9 +228,7 @@ public: if (header.compare(0, 7, "Bearer ", 7) != 0) return false; // No token provided. - Anope::string rawtoken; - Anope::B64Decode(header.substr(7), rawtoken); - + auto rawtoken = Anope::B64Decode(header.substr(7)); for (const auto &token : tokens) { if (!CompareToken(token, rawtoken)) diff --git a/include/textproc.h b/include/textproc.h index fd0f57c37..622c249d8 100644 --- a/include/textproc.h +++ b/include/textproc.h @@ -21,6 +21,16 @@ namespace Anope { + /** Encode a string as base-64. + * @param str The string to encode as base-64 . + */ + extern CoreExport Anope::string B64Encode(const Anope::string &str); + + /** Decode a string from base-64. + * @param str The string to decode from base-64. + */ + extern CoreExport Anope::string B64Decode(const Anope::string &str); + /** Calculates the levenshtein distance between two strings. * @param s1 The first string. * @param s2 The second string. diff --git a/modules/database/db_atheme.cpp b/modules/database/db_atheme.cpp index 11df9765d..fbdd67d8c 100644 --- a/modules/database/db_atheme.cpp +++ b/modules/database/db_atheme.cpp @@ -443,10 +443,9 @@ private: if (pass.compare(0, 18, "$anope$enc_sha256$", 18) == 0) { auto sep = pass.find('$', 18); - Anope::string iv, pass; - Anope::B64Decode(pass.substr(18, sep - 18), iv); - Anope::B64Decode(pass.substr(sep + 1), pass); - nc->pass = "sha256:" + Anope::Hex(pass) + ":" + Anope::Hex(iv); + auto iv = Anope::B64Decode(pass.substr(18, sep - 18)); + auto pw = Anope::B64Decode(pass.substr(sep + 1)); + nc->pass = "sha256:" + Anope::Hex(pw) + ":" + Anope::Hex(iv); } else if (pass.compare(0, 9, "$argon2d$", 9) == 0) @@ -460,8 +459,7 @@ private: else if (pass.compare(0, 8, "$base64$", 8) == 0) { - Anope::string rawpass; - Anope::B64Decode(pass.substr(8), rawpass); + auto rawpass = Anope::B64Decode(pass.substr(8)); Anope::Encrypt(rawpass, nc->pass); } diff --git a/modules/encryption/enc_none.cpp b/modules/encryption/enc_none.cpp index 309a317c0..f6970fa1f 100644 --- a/modules/encryption/enc_none.cpp +++ b/modules/encryption/enc_none.cpp @@ -35,9 +35,7 @@ public: if (!hash_method.equals_cs("plain")) return; - Anope::string b64pass; - Anope::B64Encode(req->GetPassword(), b64pass); - auto enc = "plain:" + b64pass; + auto enc = "plain:" + Anope::B64Encode(req->GetPassword()); if (nc->pass.equals_cs(enc)) { // If we are NOT the first encryption module we want to re-encrypt diff --git a/modules/nickserv/ns_sasl.cpp b/modules/nickserv/ns_sasl.cpp index 2f318a6c0..39c336736 100644 --- a/modules/nickserv/ns_sasl.cpp +++ b/modules/nickserv/ns_sasl.cpp @@ -100,8 +100,7 @@ public: else if (m.type == "C") { // message = [authzid] UTF8NUL authcid UTF8NUL passwd - Anope::string message; - Anope::B64Decode(m.data[0], message); + auto message = Anope::B64Decode(m.data[0]); size_t zcsep = message.find('\0'); if (zcsep == Anope::string::npos) @@ -218,8 +217,7 @@ public: } else if (m.type == "C") { - Anope::string decoded; - Anope::B64Decode(m.data[0], decoded); + auto decoded = Anope::B64Decode(m.data[0]); auto user = sess->GetUserInfo(); if (!decoded.empty()) diff --git a/modules/protocol/inspircd.cpp b/modules/protocol/inspircd.cpp index b1b168401..11f186501 100644 --- a/modules/protocol/inspircd.cpp +++ b/modules/protocol/inspircd.cpp @@ -1121,8 +1121,7 @@ struct IRCDMessageCapab final if (challenge.empty() || !sha256) return Config->Uplinks[Anope::CurrentUplink].password; - Anope::string b64challenge; - Anope::B64Encode(sha256->HMAC(Config->Uplinks[Anope::CurrentUplink].password, challenge), b64challenge); + auto b64challenge = Anope::B64Encode(sha256->HMAC(Config->Uplinks[Anope::CurrentUplink].password, challenge)); challenge.clear(); return "AUTH:" + b64challenge.rtrim('='); diff --git a/modules/protocol/unrealircd.cpp b/modules/protocol/unrealircd.cpp index fe5eac084..19287bc6d 100644 --- a/modules/protocol/unrealircd.cpp +++ b/modules/protocol/unrealircd.cpp @@ -1233,8 +1233,7 @@ struct IRCDMessageNick final Anope::string ip; if (params[9] != "*") { - Anope::string decoded_ip; - Anope::B64Decode(params[9], decoded_ip); + auto decoded_ip = Anope::B64Decode(params[9]); sockaddrs ip_addr; ip_addr.ntop(params[9].length() == 8 ? AF_INET : AF_INET6, decoded_ip.c_str()); @@ -1646,8 +1645,7 @@ struct IRCDMessageUID final if (ip != "*") { - Anope::string decoded_ip; - Anope::B64Decode(ip, decoded_ip); + auto decoded_ip = Anope::B64Decode(ip); sockaddrs ip_addr; ip_addr.ntop(ip.length() == 8 ? AF_INET : AF_INET6, decoded_ip.c_str()); diff --git a/src/base64.cpp b/src/base64.cpp index 19df82086..92498f82b 100644 --- a/src/base64.cpp +++ b/src/base64.cpp @@ -11,6 +11,7 @@ #include "services.h" #include "anope.h" +#include "textproc.h" static const Anope::string Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char Pad64 = '='; @@ -78,12 +79,12 @@ static const char Pad64 = '='; characters followed by one "=" padding character. */ -void Anope::B64Encode(const Anope::string &src, Anope::string &target) +Anope::string Anope::B64Encode(const Anope::string &src) { size_t src_pos = 0, src_len = src.length(); unsigned char input[3] = { '\0', '\0', '\0' }; - target.clear(); + Anope::string target; while (src_len - src_pos > 2) { @@ -112,6 +113,7 @@ void Anope::B64Encode(const Anope::string &src, Anope::string &target) target += Base64[((input[1] & 0x0f) << 2) + (input[2] >> 6)]; target += Pad64; } + return target; } /* skips all whitespace anywhere. @@ -119,9 +121,9 @@ void Anope::B64Encode(const Anope::string &src, Anope::string &target) src from base - 64 numbers into three 8 bit bytes in the target area. */ -void Anope::B64Decode(const Anope::string &src, Anope::string &target) +Anope::string Anope::B64Decode(const Anope::string &src) { - target.clear(); + Anope::string target; unsigned state = 0; Anope::string::const_iterator ch = src.begin(), end = src.end(); @@ -135,7 +137,7 @@ void Anope::B64Decode(const Anope::string &src, Anope::string &target) size_t pos = Base64.find(*ch); if (pos == Anope::string::npos) /* A non-base64 character */ - return; + return {}; switch (state) { @@ -160,4 +162,5 @@ void Anope::B64Decode(const Anope::string &src, Anope::string &target) } if (!target.empty() && !target[target.length() - 1]) target.erase(target.length() - 1); + return target; }