cli MVP
This commit is contained in:
parent
367c19386c
commit
1ea50c50bc
62 changed files with 9982 additions and 608 deletions
|
|
@ -1,5 +1,5 @@
|
|||
//SPDX-License-Identifier: LGPL-3.0-only
|
||||
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
module;
|
||||
|
||||
|
|
@ -7,6 +7,7 @@ module;
|
|||
#include <openssl/pem.h>
|
||||
#include <openssl/rsa.h>
|
||||
#include <openssl/err.h>
|
||||
#include <openssl/rand.h>
|
||||
|
||||
module Nostro:Crypto_impl;
|
||||
import :Crypto;
|
||||
|
|
@ -14,7 +15,19 @@ import std;
|
|||
|
||||
using namespace Nostro;
|
||||
|
||||
long Crypto::ThrowOnOpenssl(long status) {
|
||||
namespace {
|
||||
std::string ReadBio(BIO* bio) {
|
||||
// BIO_get_mem_data hands out its buffer through a char*.
|
||||
// lint-disable-next-line no-char-pointer
|
||||
char* data;
|
||||
const std::int64_t len = Crypto::ThrowOnOpenssl(BIO_get_mem_data(bio, &data));
|
||||
std::string out(data, len);
|
||||
BIO_free(bio);
|
||||
return out;
|
||||
}
|
||||
}
|
||||
|
||||
std::int64_t Crypto::ThrowOnOpenssl(std::int64_t status) {
|
||||
if(status <= 0) {
|
||||
ERR_print_errors_fp(stderr);
|
||||
throw std::runtime_error("openssl error");
|
||||
|
|
@ -22,6 +35,10 @@ long Crypto::ThrowOnOpenssl(long status) {
|
|||
return status;
|
||||
}
|
||||
|
||||
Crypto::Key Crypto::OwnKey(EVP_PKEY* key) {
|
||||
return Key(key, EVP_PKEY_free);
|
||||
}
|
||||
|
||||
std::string Crypto::SignBody(EVP_PKEY* privateKey, std::string_view body) {
|
||||
EVP_MD_CTX* ctx = ThrowOnOpenssl(EVP_MD_CTX_new());
|
||||
ThrowOnOpenssl(EVP_DigestSignInit(ctx, nullptr, EVP_sha256(), nullptr, privateKey));
|
||||
|
|
@ -32,30 +49,49 @@ std::string Crypto::SignBody(EVP_PKEY* privateKey, std::string_view body) {
|
|||
|
||||
std::string sig;
|
||||
sig.resize(sigLen);
|
||||
ThrowOnOpenssl(EVP_DigestSignFinal(ctx, reinterpret_cast<unsigned char*>(sig.data()), &sigLen));
|
||||
ThrowOnOpenssl(EVP_DigestSignFinal(ctx, reinterpret_cast<std::uint8_t*>(sig.data()), &sigLen));
|
||||
sig.resize(sigLen);
|
||||
|
||||
std::string sig64;
|
||||
sig64.resize(4 * ((sig.size() + 2) / 3));
|
||||
int n = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(sig64.data()), reinterpret_cast<const unsigned char*>(sig.data()), static_cast<int>(sig.size()));
|
||||
const std::int32_t n = EVP_EncodeBlock(reinterpret_cast<unsigned char*>(sig64.data()), reinterpret_cast<const unsigned char*>(sig.data()), static_cast<int>(sig.size()));
|
||||
sig64.resize(n);
|
||||
|
||||
EVP_MD_CTX_free(ctx);
|
||||
return sig64;
|
||||
}
|
||||
|
||||
bool Crypto::VerifyBody(EVP_PKEY* publicKey, std::string_view body, std::string_view signature) {
|
||||
// EVP_DecodeBlock keeps the bytes the '=' padding stands for, as zeros: count the padding and drop them.
|
||||
if(signature.empty() || signature.size() % 4 != 0) {
|
||||
return false;
|
||||
}
|
||||
std::string decoded(3 * (signature.size() / 4), '\0');
|
||||
const int n = EVP_DecodeBlock(reinterpret_cast<unsigned char*>(decoded.data()), reinterpret_cast<const unsigned char*>(signature.data()), static_cast<int>(signature.size()));
|
||||
if(n < 0) {
|
||||
return false;
|
||||
}
|
||||
std::size_t padding = 0;
|
||||
if(signature.ends_with("==")) {
|
||||
padding = 2;
|
||||
} else if(signature.ends_with('=')) {
|
||||
padding = 1;
|
||||
}
|
||||
decoded.resize(static_cast<std::size_t>(n) - padding);
|
||||
|
||||
EVP_MD_CTX* ctx = ThrowOnOpenssl(EVP_MD_CTX_new());
|
||||
const bool valid = EVP_DigestVerifyInit(ctx, nullptr, EVP_sha256(), nullptr, publicKey) == 1
|
||||
&& EVP_DigestVerifyUpdate(ctx, body.data(), body.length()) == 1
|
||||
&& EVP_DigestVerifyFinal(ctx, reinterpret_cast<const unsigned char*>(decoded.data()), decoded.size()) == 1;
|
||||
EVP_MD_CTX_free(ctx);
|
||||
// A failed verification leaves its reason on OpenSSL's error queue; it is not an error of the next call.
|
||||
ERR_clear_error();
|
||||
return valid;
|
||||
}
|
||||
|
||||
std::tuple<EVP_PKEY*, std::string> Crypto::CreateKey() {
|
||||
EVP_PKEY* pkey = ThrowOnOpenssl(EVP_RSA_gen(2048));
|
||||
|
||||
char* data;
|
||||
|
||||
BIO* pubBio = ThrowOnOpenssl(BIO_new(BIO_s_mem()));
|
||||
ThrowOnOpenssl(PEM_write_bio_PUBKEY(pubBio, pkey));
|
||||
const long len = ThrowOnOpenssl(BIO_get_mem_data(pubBio, &data));
|
||||
std::string publicKey(data, len);
|
||||
BIO_free(pubBio);
|
||||
|
||||
return {pkey, publicKey};
|
||||
return {pkey, PublicKeyToPem(pkey)};
|
||||
}
|
||||
|
||||
EVP_PKEY* Crypto::LoadKeyFromPem(std::string_view pem) {
|
||||
|
|
@ -63,4 +99,33 @@ EVP_PKEY* Crypto::LoadKeyFromPem(std::string_view pem) {
|
|||
EVP_PKEY* pkey = PEM_read_bio_PrivateKey(bio, nullptr, nullptr, nullptr);
|
||||
BIO_free(bio);
|
||||
return ThrowOnOpenssl(pkey);
|
||||
}
|
||||
}
|
||||
|
||||
EVP_PKEY* Crypto::LoadPublicKeyFromPem(std::string_view pem) {
|
||||
BIO* bio = ThrowOnOpenssl(BIO_new_mem_buf(pem.data(), static_cast<int>(pem.size())));
|
||||
EVP_PKEY* pkey = PEM_read_bio_PUBKEY(bio, nullptr, nullptr, nullptr);
|
||||
BIO_free(bio);
|
||||
return ThrowOnOpenssl(pkey);
|
||||
}
|
||||
|
||||
std::string Crypto::PrivateKeyToPem(EVP_PKEY* key) {
|
||||
BIO* bio = ThrowOnOpenssl(BIO_new(BIO_s_mem()));
|
||||
ThrowOnOpenssl(PEM_write_bio_PrivateKey(bio, key, nullptr, nullptr, 0, nullptr, nullptr));
|
||||
return ReadBio(bio);
|
||||
}
|
||||
|
||||
std::string Crypto::PublicKeyToPem(EVP_PKEY* key) {
|
||||
BIO* bio = ThrowOnOpenssl(BIO_new(BIO_s_mem()));
|
||||
ThrowOnOpenssl(PEM_write_bio_PUBKEY(bio, key));
|
||||
return ReadBio(bio);
|
||||
}
|
||||
|
||||
std::string Crypto::RandomHex(std::size_t bytes) {
|
||||
std::vector<unsigned char> random(bytes);
|
||||
ThrowOnOpenssl(RAND_bytes(random.data(), static_cast<std::int32_t>(random.size())));
|
||||
std::string out;
|
||||
for(const unsigned char byte : random) {
|
||||
out += std::format("{:02x}", byte);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue