2026-11-01 18:35:23 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
|
|
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
2026-10-06 15:39:17 +02:00
|
|
|
|
|
|
|
|
module;
|
|
|
|
|
|
|
|
|
|
#ifdef NOSTRO_OPENSSL_ENABLED
|
|
|
|
|
#include <openssl/types.h>
|
|
|
|
|
#include <openssl/err.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
export module Nostro:Crypto;
|
|
|
|
|
#ifdef NOSTRO_OPENSSL_ENABLED
|
|
|
|
|
|
|
|
|
|
import std;
|
|
|
|
|
|
|
|
|
|
export namespace Nostro::Crypto {
|
2026-11-01 18:35:23 +01:00
|
|
|
std::int64_t ThrowOnOpenssl(std::int64_t status);
|
2026-10-06 15:39:17 +02:00
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
T* ThrowOnOpenssl(T* pointer) {
|
|
|
|
|
if(pointer == nullptr) {
|
|
|
|
|
ERR_print_errors_fp(stderr);
|
|
|
|
|
throw std::runtime_error("openssl error");
|
|
|
|
|
}
|
|
|
|
|
return pointer;
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
// A key that frees itself; copies share it.
|
|
|
|
|
using Key = std::shared_ptr<EVP_PKEY>;
|
|
|
|
|
Key OwnKey(EVP_PKEY* key);
|
|
|
|
|
|
|
|
|
|
// RSA PKCS#1 v1.5 over SHA-256, base64: what bunq expects in X-Bunq-Client-Signature.
|
2026-10-06 15:39:17 +02:00
|
|
|
std::string SignBody(EVP_PKEY* privateKey, std::string_view body);
|
2026-11-01 18:35:23 +01:00
|
|
|
// Checks a base64 signature made as SignBody makes one, with the signer's public key.
|
|
|
|
|
bool VerifyBody(EVP_PKEY* publicKey, std::string_view body, std::string_view signature);
|
|
|
|
|
// A new 2048-bit RSA key pair and its public half in PEM.
|
2026-10-06 15:39:17 +02:00
|
|
|
std::tuple<EVP_PKEY*, std::string> CreateKey();
|
|
|
|
|
EVP_PKEY* LoadKeyFromPem(std::string_view pem);
|
2026-11-01 18:35:23 +01:00
|
|
|
EVP_PKEY* LoadPublicKeyFromPem(std::string_view pem);
|
|
|
|
|
std::string PrivateKeyToPem(EVP_PKEY* key);
|
|
|
|
|
std::string PublicKeyToPem(EVP_PKEY* key);
|
|
|
|
|
// `bytes` random bytes from the system's CSPRNG, as lower-case hex.
|
|
|
|
|
std::string RandomHex(std::size_t bytes);
|
2026-10-06 15:39:17 +02:00
|
|
|
};
|
|
|
|
|
|
2026-11-01 18:35:23 +01:00
|
|
|
#endif
|