xbot/myirc/sasl_mechanism.cpp

80 lines
2.1 KiB
C++
Raw Normal View History

2025-02-01 11:04:33 -08:00
#include "myirc/sasl_mechanism.hpp"
#include "myirc/openssl_utils.hpp"
2025-01-30 16:39:23 -08:00
#include <openssl/evp.h>
2025-01-25 12:25:38 -08:00
2025-02-01 11:04:33 -08:00
namespace myirc {
2025-01-30 09:28:28 -08:00
auto SaslPlain::step(std::string_view msg) -> StepResult {
2025-01-25 12:25:38 -08:00
if (complete_) {
2025-01-30 09:28:28 -08:00
return Failure{};
2025-01-25 12:25:38 -08:00
} else {
std::string reply;
reply += authzid_;
reply += '\0';
reply += authcid_;
reply += '\0';
reply += password_;
complete_ = true;
2025-01-30 09:28:28 -08:00
return std::move(reply);
2025-01-25 12:25:38 -08:00
}
}
2025-01-28 20:01:51 -08:00
2025-01-30 09:28:28 -08:00
auto SaslExternal::step(std::string_view msg) -> StepResult {
2025-01-28 20:01:51 -08:00
if (complete_) {
2025-01-30 09:28:28 -08:00
return Failure{};
2025-01-28 20:01:51 -08:00
} else {
2025-01-30 16:39:23 -08:00
complete_ = true;
2025-01-30 09:28:28 -08:00
return std::move(authzid_);
2025-01-28 20:01:51 -08:00
}
}
2025-01-30 16:39:23 -08:00
auto SaslEcdsa::step(std::string_view msg) -> StepResult {
switch (stage_) {
case 0:
stage_ = 1;
return std::move(message1_);
case 1:
{
stage_ = 2;
Ref<EVP_PKEY_CTX> ctx {EVP_PKEY_CTX_new(key_.get(), nullptr)};
if (not ctx) {
log_openssl_errors("ECDSA new context: ");
return Failure{};
}
if (0 >= EVP_PKEY_sign_init(ctx.get()))
{
log_openssl_errors("ECDSA init: ");
return Failure{};
}
const auto input = reinterpret_cast<const unsigned char *>(msg.data());
size_t siglen;
if (0 >= EVP_PKEY_sign(ctx.get(), nullptr, &siglen, input, msg.size()))
{
log_openssl_errors("ECDSA signature (presize): ");
return Failure{};
}
std::string result(siglen, '\0');
const auto output = reinterpret_cast<unsigned char *>(result.data());
if (0 >= EVP_PKEY_sign(ctx.get(), output, &siglen, input, msg.size()))
{
log_openssl_errors("ECDSA signature: ");
return Failure{};
}
result.resize(siglen);
return std::move(result);
}
default:
return Failure{};
}
}
2025-02-01 11:04:33 -08:00
} // namespace myirc